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.
855 maybe_new_partial_specialization (tree type
)
857 // An implicit instantiation of an incomplete type implies
858 // the definition of a new class template.
860 // template<typename T>
863 // template<typename T>
866 // Here, S<T*> is an implicit instantiation of S whose type
868 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
871 // It can also be the case that TYPE is a completed specialization.
872 // Continuing the previous example, suppose we also declare:
874 // template<typename T>
875 // requires Integral<T>
878 // Here, S<T*> refers to the specialization S<T*> defined
879 // above. However, we need to differentiate definitions because
880 // we intend to define a new partial specialization. In this case,
881 // we rely on the fact that the constraints are different for
882 // this declaration than that above.
884 // Note that we also get here for injected class names and
885 // late-parsed template definitions. We must ensure that we
886 // do not create new type declarations for those cases.
887 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
889 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
890 tree args
= CLASSTYPE_TI_ARGS (type
);
892 // If there are no template parameters, this cannot be a new
893 // partial template specializtion?
894 if (!current_template_parms
)
897 // The injected-class-name is not a new partial specialization.
898 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
901 // If the constraints are not the same as those of the primary
902 // then, we can probably create a new specialization.
903 tree type_constr
= current_template_constraints ();
905 if (type
== TREE_TYPE (tmpl
))
907 tree main_constr
= get_constraints (tmpl
);
908 if (equivalent_constraints (type_constr
, main_constr
))
912 // Also, if there's a pre-existing specialization with matching
913 // constraints, then this also isn't new.
914 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
917 tree spec_tmpl
= TREE_VALUE (specs
);
918 tree spec_args
= TREE_PURPOSE (specs
);
919 tree spec_constr
= get_constraints (spec_tmpl
);
920 if (comp_template_args (args
, spec_args
)
921 && equivalent_constraints (type_constr
, spec_constr
))
923 specs
= TREE_CHAIN (specs
);
926 // Create a new type node (and corresponding type decl)
927 // for the newly declared specialization.
928 tree t
= make_class_type (TREE_CODE (type
));
929 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
930 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
932 /* We only need a separate type node for storing the definition of this
933 partial specialization; uses of S<T*> are unconstrained, so all are
934 equivalent. So keep TYPE_CANONICAL the same. */
935 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
937 // Build the corresponding type decl.
938 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
939 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
940 DECL_SOURCE_LOCATION (d
) = input_location
;
948 /* The TYPE is being declared. If it is a template type, that means it
949 is a partial specialization. Do appropriate error-checking. */
952 maybe_process_partial_specialization (tree type
)
956 if (type
== error_mark_node
)
957 return error_mark_node
;
959 /* A lambda that appears in specialization context is not itself a
961 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
964 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
966 error ("name of class shadows template template parameter %qD",
968 return error_mark_node
;
971 context
= TYPE_CONTEXT (type
);
973 if (TYPE_ALIAS_P (type
))
975 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
977 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
978 error ("specialization of alias template %qD",
979 TI_TEMPLATE (tinfo
));
981 error ("explicit specialization of non-template %qT", type
);
982 return error_mark_node
;
984 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
986 /* This is for ordinary explicit specialization and partial
987 specialization of a template class such as:
989 template <> class C<int>;
993 template <class T> class C<T*>;
995 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
997 if (tree t
= maybe_new_partial_specialization (type
))
999 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1000 && !at_namespace_scope_p ())
1001 return error_mark_node
;
1002 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1003 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1004 if (processing_template_decl
)
1006 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1007 if (decl
== error_mark_node
)
1008 return error_mark_node
;
1009 return TREE_TYPE (decl
);
1012 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1013 error ("specialization of %qT after instantiation", type
);
1014 else if (errorcount
&& !processing_specialization
1015 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1016 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1017 /* Trying to define a specialization either without a template<> header
1018 or in an inappropriate place. We've already given an error, so just
1019 bail now so we don't actually define the specialization. */
1020 return error_mark_node
;
1022 else if (CLASS_TYPE_P (type
)
1023 && !CLASSTYPE_USE_TEMPLATE (type
)
1024 && CLASSTYPE_TEMPLATE_INFO (type
)
1025 && context
&& CLASS_TYPE_P (context
)
1026 && CLASSTYPE_TEMPLATE_INFO (context
))
1028 /* This is for an explicit specialization of member class
1029 template according to [temp.expl.spec/18]:
1031 template <> template <class U> class C<int>::D;
1033 The context `C<int>' must be an implicit instantiation.
1034 Otherwise this is just a member class template declared
1037 template <> class C<int> { template <class U> class D; };
1038 template <> template <class U> class C<int>::D;
1040 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1041 while in the second case, `C<int>::D' is a primary template
1042 and `C<T>::D' may not exist. */
1044 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1045 && !COMPLETE_TYPE_P (type
))
1048 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1050 if (current_namespace
1051 != decl_namespace_context (tmpl
))
1053 if (permerror (input_location
,
1054 "specialization of %qD in different namespace",
1056 inform (DECL_SOURCE_LOCATION (tmpl
),
1057 "from definition of %q#D", tmpl
);
1060 /* Check for invalid specialization after instantiation:
1062 template <> template <> class C<int>::D<int>;
1063 template <> template <class U> class C<int>::D; */
1065 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1066 t
; t
= TREE_CHAIN (t
))
1068 tree inst
= TREE_VALUE (t
);
1069 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1070 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1072 /* We already have a full specialization of this partial
1073 instantiation, or a full specialization has been
1074 looked up but not instantiated. Reassign it to the
1075 new member specialization template. */
1079 elt
.tmpl
= most_general_template (tmpl
);
1080 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1083 type_specializations
->remove_elt (&elt
);
1086 CLASSTYPE_TI_ARGS (inst
)
1087 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1090 = type_specializations
->find_slot (&elt
, INSERT
);
1091 entry
= ggc_alloc
<spec_entry
> ();
1096 /* But if we've had an implicit instantiation, that's a
1097 problem ([temp.expl.spec]/6). */
1098 error ("specialization %qT after instantiation %qT",
1102 /* Mark TYPE as a specialization. And as a result, we only
1103 have one level of template argument for the innermost
1105 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1106 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1107 CLASSTYPE_TI_ARGS (type
)
1108 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1111 else if (processing_specialization
)
1113 /* Someday C++0x may allow for enum template specialization. */
1114 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1115 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1116 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1117 "of %qD not allowed by ISO C++", type
);
1120 error ("explicit specialization of non-template %qT", type
);
1121 return error_mark_node
;
1128 /* Returns nonzero if we can optimize the retrieval of specializations
1129 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1130 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1133 optimize_specialization_lookup_p (tree tmpl
)
1135 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1136 && DECL_CLASS_SCOPE_P (tmpl
)
1137 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1139 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1140 /* The optimized lookup depends on the fact that the
1141 template arguments for the member function template apply
1142 purely to the containing class, which is not true if the
1143 containing class is an explicit or partial
1145 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1146 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1147 && !DECL_CONV_FN_P (tmpl
)
1148 /* It is possible to have a template that is not a member
1149 template and is not a member of a template class:
1151 template <typename T>
1152 struct S { friend A::f(); };
1154 Here, the friend function is a template, but the context does
1155 not have template information. The optimized lookup relies
1156 on having ARGS be the template arguments for both the class
1157 and the function template. */
1158 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1161 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1162 gone through coerce_template_parms by now. */
1165 verify_unstripped_args_1 (tree inner
)
1167 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1169 tree arg
= TREE_VEC_ELT (inner
, i
);
1170 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1172 else if (TYPE_P (arg
))
1173 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1174 else if (ARGUMENT_PACK_P (arg
))
1175 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1176 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1177 /* Allow typedefs on the type of a non-type argument, since a
1178 parameter can have them. */;
1180 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1185 verify_unstripped_args (tree args
)
1187 ++processing_template_decl
;
1188 if (!any_dependent_template_arguments_p (args
))
1189 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1190 --processing_template_decl
;
1193 /* Retrieve the specialization (in the sense of [temp.spec] - a
1194 specialization is either an instantiation or an explicit
1195 specialization) of TMPL for the given template ARGS. If there is
1196 no such specialization, return NULL_TREE. The ARGS are a vector of
1197 arguments, or a vector of vectors of arguments, in the case of
1198 templates with more than one level of parameters.
1200 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1201 then we search for a partial specialization matching ARGS. This
1202 parameter is ignored if TMPL is not a class template.
1204 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1205 result is a NONTYPE_ARGUMENT_PACK. */
1208 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1210 if (tmpl
== NULL_TREE
)
1213 if (args
== error_mark_node
)
1216 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1217 || TREE_CODE (tmpl
) == FIELD_DECL
);
1219 /* There should be as many levels of arguments as there are
1220 levels of parameters. */
1221 gcc_assert (TMPL_ARGS_DEPTH (args
)
1222 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1223 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1224 : template_class_depth (DECL_CONTEXT (tmpl
))));
1227 verify_unstripped_args (args
);
1229 /* Lambda functions in templates aren't instantiated normally, but through
1230 tsubst_lambda_expr. */
1231 if (lambda_fn_in_template_p (tmpl
))
1234 if (optimize_specialization_lookup_p (tmpl
))
1236 /* The template arguments actually apply to the containing
1237 class. Find the class specialization with those
1239 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1240 tree class_specialization
1241 = retrieve_specialization (class_template
, args
, 0);
1242 if (!class_specialization
)
1245 /* Find the instance of TMPL. */
1246 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1247 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1250 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1251 /* using-declarations can add base methods to the method vec,
1252 and we don't want those here. */
1253 && DECL_CONTEXT (fn
) == class_specialization
)
1262 hash_table
<spec_hasher
> *specializations
;
1266 elt
.spec
= NULL_TREE
;
1268 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1269 specializations
= type_specializations
;
1271 specializations
= decl_specializations
;
1274 hash
= spec_hasher::hash (&elt
);
1275 found
= specializations
->find_with_hash (&elt
, hash
);
1283 /* Like retrieve_specialization, but for local declarations. */
1286 retrieve_local_specialization (tree tmpl
)
1288 if (local_specializations
== NULL
)
1291 tree
*slot
= local_specializations
->get (tmpl
);
1292 return slot
? *slot
: NULL_TREE
;
1295 /* Returns nonzero iff DECL is a specialization of TMPL. */
1298 is_specialization_of (tree decl
, tree tmpl
)
1302 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1306 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1312 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1314 for (t
= TREE_TYPE (decl
);
1316 t
= CLASSTYPE_USE_TEMPLATE (t
)
1317 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1318 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1325 /* Returns nonzero iff DECL is a specialization of friend declaration
1326 FRIEND_DECL according to [temp.friend]. */
1329 is_specialization_of_friend (tree decl
, tree friend_decl
)
1331 bool need_template
= true;
1334 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1335 || TREE_CODE (decl
) == TYPE_DECL
);
1337 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1338 of a template class, we want to check if DECL is a specialization
1340 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1341 && DECL_TEMPLATE_INFO (friend_decl
)
1342 && !DECL_USE_TEMPLATE (friend_decl
))
1344 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1345 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1346 need_template
= false;
1348 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1349 && !PRIMARY_TEMPLATE_P (friend_decl
))
1350 need_template
= false;
1352 /* There is nothing to do if this is not a template friend. */
1353 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1356 if (is_specialization_of (decl
, friend_decl
))
1360 A member of a class template may be declared to be a friend of a
1361 non-template class. In this case, the corresponding member of
1362 every specialization of the class template is a friend of the
1363 class granting friendship.
1365 For example, given a template friend declaration
1367 template <class T> friend void A<T>::f();
1369 the member function below is considered a friend
1371 template <> struct A<int> {
1375 For this type of template friend, TEMPLATE_DEPTH below will be
1376 nonzero. To determine if DECL is a friend of FRIEND, we first
1377 check if the enclosing class is a specialization of another. */
1379 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1381 && DECL_CLASS_SCOPE_P (decl
)
1382 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1383 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1385 /* Next, we check the members themselves. In order to handle
1386 a few tricky cases, such as when FRIEND_DECL's are
1388 template <class T> friend void A<T>::g(T t);
1389 template <class T> template <T t> friend void A<T>::h();
1393 void A<int>::g(int);
1394 template <int> void A<int>::h();
1396 we need to figure out ARGS, the template arguments from
1397 the context of DECL. This is required for template substitution
1398 of `T' in the function parameter of `g' and template parameter
1399 of `h' in the above examples. Here ARGS corresponds to `int'. */
1401 tree context
= DECL_CONTEXT (decl
);
1402 tree args
= NULL_TREE
;
1403 int current_depth
= 0;
1405 while (current_depth
< template_depth
)
1407 if (CLASSTYPE_TEMPLATE_INFO (context
))
1409 if (current_depth
== 0)
1410 args
= TYPE_TI_ARGS (context
);
1412 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1415 context
= TYPE_CONTEXT (context
);
1418 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1423 tree friend_args_type
;
1424 tree decl_args_type
;
1426 /* Make sure that both DECL and FRIEND_DECL are templates or
1428 is_template
= DECL_TEMPLATE_INFO (decl
)
1429 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1430 if (need_template
^ is_template
)
1432 else if (is_template
)
1434 /* If both are templates, check template parameter list. */
1436 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1438 if (!comp_template_parms
1439 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1443 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1446 decl_type
= TREE_TYPE (decl
);
1448 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1449 tf_none
, NULL_TREE
);
1450 if (friend_type
== error_mark_node
)
1453 /* Check if return types match. */
1454 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1457 /* Check if function parameter types match, ignoring the
1458 `this' parameter. */
1459 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1460 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1462 friend_args_type
= TREE_CHAIN (friend_args_type
);
1463 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1464 decl_args_type
= TREE_CHAIN (decl_args_type
);
1466 return compparms (decl_args_type
, friend_args_type
);
1470 /* DECL is a TYPE_DECL */
1472 tree decl_type
= TREE_TYPE (decl
);
1474 /* Make sure that both DECL and FRIEND_DECL are templates or
1477 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1478 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1480 if (need_template
^ is_template
)
1482 else if (is_template
)
1485 /* If both are templates, check the name of the two
1486 TEMPLATE_DECL's first because is_friend didn't. */
1487 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1488 != DECL_NAME (friend_decl
))
1491 /* Now check template parameter list. */
1493 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1495 return comp_template_parms
1496 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1500 return (DECL_NAME (decl
)
1501 == DECL_NAME (friend_decl
));
1507 /* Register the specialization SPEC as a specialization of TMPL with
1508 the indicated ARGS. IS_FRIEND indicates whether the specialization
1509 is actually just a friend declaration. ATTRLIST is the list of
1510 attributes that the specialization is declared with or NULL when
1511 it isn't. Returns SPEC, or an equivalent prior declaration, if
1514 We also store instantiations of field packs in the hash table, even
1515 though they are not themselves templates, to make lookup easier. */
1518 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1522 spec_entry
**slot
= NULL
;
1525 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1526 || (TREE_CODE (tmpl
) == FIELD_DECL
1527 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1529 if (TREE_CODE (spec
) == FUNCTION_DECL
1530 && uses_template_parms (DECL_TI_ARGS (spec
)))
1531 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1532 register it; we want the corresponding TEMPLATE_DECL instead.
1533 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1534 the more obvious `uses_template_parms (spec)' to avoid problems
1535 with default function arguments. In particular, given
1536 something like this:
1538 template <class T> void f(T t1, T t = T())
1540 the default argument expression is not substituted for in an
1541 instantiation unless and until it is actually needed. */
1544 if (optimize_specialization_lookup_p (tmpl
))
1545 /* We don't put these specializations in the hash table, but we might
1546 want to give an error about a mismatch. */
1547 fn
= retrieve_specialization (tmpl
, args
, 0);
1555 hash
= spec_hasher::hash (&elt
);
1558 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1560 fn
= ((spec_entry
*) *slot
)->spec
;
1565 /* We can sometimes try to re-register a specialization that we've
1566 already got. In particular, regenerate_decl_from_template calls
1567 duplicate_decls which will update the specialization list. But,
1568 we'll still get called again here anyhow. It's more convenient
1569 to simply allow this than to try to prevent it. */
1572 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1574 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1576 if (DECL_ODR_USED (fn
)
1577 || DECL_EXPLICIT_INSTANTIATION (fn
))
1579 error ("specialization of %qD after instantiation",
1581 return error_mark_node
;
1586 /* This situation should occur only if the first
1587 specialization is an implicit instantiation, the
1588 second is an explicit specialization, and the
1589 implicit instantiation has not yet been used. That
1590 situation can occur if we have implicitly
1591 instantiated a member function and then specialized
1594 We can also wind up here if a friend declaration that
1595 looked like an instantiation turns out to be a
1598 template <class T> void foo(T);
1599 class S { friend void foo<>(int) };
1600 template <> void foo(int);
1602 We transform the existing DECL in place so that any
1603 pointers to it become pointers to the updated
1606 If there was a definition for the template, but not
1607 for the specialization, we want this to look as if
1608 there were no definition, and vice versa. */
1609 DECL_INITIAL (fn
) = NULL_TREE
;
1610 duplicate_decls (spec
, fn
, is_friend
);
1611 /* The call to duplicate_decls will have applied
1614 An explicit specialization of a function template
1615 is inline only if it is explicitly declared to be,
1616 and independently of whether its function template
1619 to the primary function; now copy the inline bits to
1620 the various clones. */
1621 FOR_EACH_CLONE (clone
, fn
)
1623 DECL_DECLARED_INLINE_P (clone
)
1624 = DECL_DECLARED_INLINE_P (fn
);
1625 DECL_SOURCE_LOCATION (clone
)
1626 = DECL_SOURCE_LOCATION (fn
);
1627 DECL_DELETED_FN (clone
)
1628 = DECL_DELETED_FN (fn
);
1630 check_specialization_namespace (tmpl
);
1635 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1637 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1638 if (dd
== error_mark_node
)
1639 /* We've already complained in duplicate_decls. */
1640 return error_mark_node
;
1642 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1643 /* Dup decl failed, but this is a new definition. Set the
1644 line number so any errors match this new
1646 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1652 return duplicate_decls (spec
, fn
, is_friend
);
1654 /* A specialization must be declared in the same namespace as the
1655 template it is specializing. */
1656 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1657 && !check_specialization_namespace (tmpl
))
1658 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1660 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1662 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1663 gcc_assert (tmpl
&& args
&& spec
);
1666 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1667 && PRIMARY_TEMPLATE_P (tmpl
)
1668 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1669 || variable_template_p (tmpl
))
1670 /* If TMPL is a forward declaration of a template function, keep a list
1671 of all specializations in case we need to reassign them to a friend
1672 template later in tsubst_friend_function.
1674 Also keep a list of all variable template instantiations so that
1675 process_partial_specialization can check whether a later partial
1676 specialization would have used it. */
1677 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1678 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1684 /* Returns true iff two spec_entry nodes are equivalent. */
1686 int comparing_specializations
;
1689 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1693 ++comparing_specializations
;
1694 equal
= (e1
->tmpl
== e2
->tmpl
1695 && comp_template_args (e1
->args
, e2
->args
));
1696 if (equal
&& flag_concepts
1697 /* tmpl could be a FIELD_DECL for a capture pack. */
1698 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1699 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1700 && uses_template_parms (e1
->args
))
1702 /* Partial specializations of a variable template can be distinguished by
1704 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1705 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1706 equal
= equivalent_constraints (c1
, c2
);
1708 --comparing_specializations
;
1713 /* Returns a hash for a template TMPL and template arguments ARGS. */
1716 hash_tmpl_and_args (tree tmpl
, tree args
)
1718 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1719 return iterative_hash_template_arg (args
, val
);
1722 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1726 spec_hasher::hash (spec_entry
*e
)
1728 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1731 /* Recursively calculate a hash value for a template argument ARG, for use
1732 in the hash tables of template specializations. */
1735 iterative_hash_template_arg (tree arg
, hashval_t val
)
1737 unsigned HOST_WIDE_INT i
;
1738 enum tree_code code
;
1741 if (arg
== NULL_TREE
)
1742 return iterative_hash_object (arg
, val
);
1747 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1750 code
= TREE_CODE (arg
);
1751 tclass
= TREE_CODE_CLASS (code
);
1753 val
= iterative_hash_object (code
, val
);
1760 case IDENTIFIER_NODE
:
1761 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1765 int i
, len
= TREE_VEC_LENGTH (arg
);
1766 for (i
= 0; i
< len
; ++i
)
1767 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1771 case TYPE_PACK_EXPANSION
:
1772 case EXPR_PACK_EXPANSION
:
1773 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1774 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1776 case TYPE_ARGUMENT_PACK
:
1777 case NONTYPE_ARGUMENT_PACK
:
1778 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1781 for (; arg
; arg
= TREE_CHAIN (arg
))
1782 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1786 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1787 val
= iterative_hash_template_arg (*iter
, val
);
1793 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1794 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1796 val
= iterative_hash_template_arg (field
, val
);
1797 val
= iterative_hash_template_arg (value
, val
);
1803 if (!DECL_ARTIFICIAL (arg
))
1805 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1806 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1808 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1811 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1814 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1815 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1817 case TEMPLATE_PARM_INDEX
:
1818 val
= iterative_hash_template_arg
1819 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1820 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1821 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1824 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1825 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1826 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1829 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1831 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1835 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1836 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1837 val
= iterative_hash_object (code
, val
);
1838 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1841 /* [temp.over.link] Two lambda-expressions are never considered
1844 So just hash the closure type. */
1845 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1848 case IMPLICIT_CONV_EXPR
:
1849 case STATIC_CAST_EXPR
:
1850 case REINTERPRET_CAST_EXPR
:
1851 case CONST_CAST_EXPR
:
1852 case DYNAMIC_CAST_EXPR
:
1854 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1855 /* Now hash operands as usual. */
1860 tree fn
= CALL_EXPR_FN (arg
);
1861 if (tree name
= dependent_name (fn
))
1863 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1864 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1867 val
= iterative_hash_template_arg (fn
, val
);
1868 call_expr_arg_iterator ai
;
1869 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1870 x
= next_call_expr_arg (&ai
))
1871 val
= iterative_hash_template_arg (x
, val
);
1882 if (alias_template_specialization_p (arg
))
1884 // We want an alias specialization that survived strip_typedefs
1885 // to hash differently from its TYPE_CANONICAL, to avoid hash
1886 // collisions that compare as different in template_args_equal.
1887 // These could be dependent specializations that strip_typedefs
1888 // left alone, or untouched specializations because
1889 // coerce_template_parms returns the unconverted template
1890 // arguments if it sees incomplete argument packs.
1891 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1892 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1894 if (TYPE_CANONICAL (arg
))
1895 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1897 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1898 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1899 /* Otherwise just compare the types during lookup. */
1902 case tcc_declaration
:
1904 return iterative_hash_expr (arg
, val
);
1907 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1909 unsigned n
= cp_tree_operand_length (arg
);
1910 for (i
= 0; i
< n
; ++i
)
1911 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1919 /* Unregister the specialization SPEC as a specialization of TMPL.
1920 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1921 if the SPEC was listed as a specialization of TMPL.
1923 Note that SPEC has been ggc_freed, so we can't look inside it. */
1926 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1931 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1932 elt
.args
= TI_ARGS (tinfo
);
1933 elt
.spec
= NULL_TREE
;
1935 entry
= decl_specializations
->find (&elt
);
1938 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1939 gcc_assert (new_spec
!= NULL_TREE
);
1940 entry
->spec
= new_spec
;
1947 /* Like register_specialization, but for local declarations. We are
1948 registering SPEC, an instantiation of TMPL. */
1951 register_local_specialization (tree spec
, tree tmpl
)
1953 gcc_assert (tmpl
!= spec
);
1954 local_specializations
->put (tmpl
, spec
);
1957 /* TYPE is a class type. Returns true if TYPE is an explicitly
1958 specialized class. */
1961 explicit_class_specialization_p (tree type
)
1963 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1965 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1968 /* Print the list of functions at FNS, going through all the overloads
1969 for each element of the list. Alternatively, FNS cannot be a
1970 TREE_LIST, in which case it will be printed together with all the
1973 MORE and *STR should respectively be FALSE and NULL when the function
1974 is called from the outside. They are used internally on recursive
1975 calls. print_candidates manages the two parameters and leaves NULL
1976 in *STR when it ends. */
1979 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1981 if (TREE_CODE (fns
) == TREE_LIST
)
1982 for (; fns
; fns
= TREE_CHAIN (fns
))
1983 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1985 for (lkp_iterator
iter (fns
); iter
;)
1990 const char *pfx
= *str
;
1994 pfx
= _("candidates are:");
1996 pfx
= _("candidate is:");
1997 *str
= get_spaces (pfx
);
1999 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2003 /* Print the list of candidate FNS in an error message. FNS can also
2004 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2007 print_candidates (tree fns
)
2010 print_candidates_1 (fns
, &str
);
2014 /* Get a (possibly) constrained template declaration for the
2015 purpose of ordering candidates. */
2017 get_template_for_ordering (tree list
)
2019 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2020 tree f
= TREE_VALUE (list
);
2021 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2022 return TI_TEMPLATE (ti
);
2026 /* Among candidates having the same signature, return the
2027 most constrained or NULL_TREE if there is no best candidate.
2028 If the signatures of candidates vary (e.g., template
2029 specialization vs. member function), then there can be no
2032 Note that we don't compare constraints on the functions
2033 themselves, but rather those of their templates. */
2035 most_constrained_function (tree candidates
)
2037 // Try to find the best candidate in a first pass.
2038 tree champ
= candidates
;
2039 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2041 int winner
= more_constrained (get_template_for_ordering (champ
),
2042 get_template_for_ordering (c
));
2044 champ
= c
; // The candidate is more constrained
2045 else if (winner
== 0)
2046 return NULL_TREE
; // Neither is more constrained
2049 // Verify that the champ is better than previous candidates.
2050 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2051 if (!more_constrained (get_template_for_ordering (champ
),
2052 get_template_for_ordering (c
)))
2060 /* Returns the template (one of the functions given by TEMPLATE_ID)
2061 which can be specialized to match the indicated DECL with the
2062 explicit template args given in TEMPLATE_ID. The DECL may be
2063 NULL_TREE if none is available. In that case, the functions in
2064 TEMPLATE_ID are non-members.
2066 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2067 specialization of a member template.
2069 The TEMPLATE_COUNT is the number of references to qualifying
2070 template classes that appeared in the name of the function. See
2071 check_explicit_specialization for a more accurate description.
2073 TSK indicates what kind of template declaration (if any) is being
2074 declared. TSK_TEMPLATE indicates that the declaration given by
2075 DECL, though a FUNCTION_DECL, has template parameters, and is
2076 therefore a template function.
2078 The template args (those explicitly specified and those deduced)
2079 are output in a newly created vector *TARGS_OUT.
2081 If it is impossible to determine the result, an error message is
2082 issued. The error_mark_node is returned to indicate failure. */
2085 determine_specialization (tree template_id
,
2088 int need_member_template
,
2094 tree explicit_targs
;
2095 tree candidates
= NULL_TREE
;
2097 /* A TREE_LIST of templates of which DECL may be a specialization.
2098 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2099 corresponding TREE_PURPOSE is the set of template arguments that,
2100 when used to instantiate the template, would produce a function
2101 with the signature of DECL. */
2102 tree templates
= NULL_TREE
;
2104 cp_binding_level
*b
;
2106 *targs_out
= NULL_TREE
;
2108 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2109 return error_mark_node
;
2111 /* We shouldn't be specializing a member template of an
2112 unspecialized class template; we already gave an error in
2113 check_specialization_scope, now avoid crashing. */
2115 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2116 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2118 gcc_assert (errorcount
);
2119 return error_mark_node
;
2122 fns
= TREE_OPERAND (template_id
, 0);
2123 explicit_targs
= TREE_OPERAND (template_id
, 1);
2125 if (fns
== error_mark_node
)
2126 return error_mark_node
;
2128 /* Check for baselinks. */
2129 if (BASELINK_P (fns
))
2130 fns
= BASELINK_FUNCTIONS (fns
);
2132 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2134 error_at (DECL_SOURCE_LOCATION (decl
),
2135 "%qD is not a function template", fns
);
2136 return error_mark_node
;
2138 else if (VAR_P (decl
) && !variable_template_p (fns
))
2140 error ("%qD is not a variable template", fns
);
2141 return error_mark_node
;
2144 /* Count the number of template headers specified for this
2147 for (b
= current_binding_level
;
2148 b
->kind
== sk_template_parms
;
2152 tree orig_fns
= fns
;
2154 if (variable_template_p (fns
))
2156 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2157 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2158 tf_warning_or_error
,
2159 /*req_all*/true, /*use_defarg*/true);
2160 if (targs
!= error_mark_node
)
2161 templates
= tree_cons (targs
, fns
, templates
);
2163 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2167 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2169 tree decl_arg_types
;
2173 /* In case of explicit specialization, we need to check if
2174 the number of template headers appearing in the specialization
2175 is correct. This is usually done in check_explicit_specialization,
2176 but the check done there cannot be exhaustive when specializing
2177 member functions. Consider the following code:
2179 template <> void A<int>::f(int);
2180 template <> template <> void A<int>::f(int);
2182 Assuming that A<int> is not itself an explicit specialization
2183 already, the first line specializes "f" which is a non-template
2184 member function, whilst the second line specializes "f" which
2185 is a template member function. So both lines are syntactically
2186 correct, and check_explicit_specialization does not reject
2189 Here, we can do better, as we are matching the specialization
2190 against the declarations. We count the number of template
2191 headers, and we check if they match TEMPLATE_COUNT + 1
2192 (TEMPLATE_COUNT is the number of qualifying template classes,
2193 plus there must be another header for the member template
2196 Notice that if header_count is zero, this is not a
2197 specialization but rather a template instantiation, so there
2198 is no check we can perform here. */
2199 if (header_count
&& header_count
!= template_count
+ 1)
2202 /* Check that the number of template arguments at the
2203 innermost level for DECL is the same as for FN. */
2204 if (current_binding_level
->kind
== sk_template_parms
2205 && !current_binding_level
->explicit_spec_p
2206 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2207 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2208 (current_template_parms
))))
2211 /* DECL might be a specialization of FN. */
2212 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2213 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2215 /* For a non-static member function, we need to make sure
2216 that the const qualification is the same. Since
2217 get_bindings does not try to merge the "this" parameter,
2218 we must do the comparison explicitly. */
2219 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2221 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2222 TREE_VALUE (decl_arg_types
)))
2225 /* And the ref-qualification. */
2226 if (type_memfn_rqual (TREE_TYPE (decl
))
2227 != type_memfn_rqual (TREE_TYPE (fn
)))
2231 /* Skip the "this" parameter and, for constructors of
2232 classes with virtual bases, the VTT parameter. A
2233 full specialization of a constructor will have a VTT
2234 parameter, but a template never will. */
2236 = skip_artificial_parms_for (decl
, decl_arg_types
);
2238 = skip_artificial_parms_for (fn
, fn_arg_types
);
2240 /* Function templates cannot be specializations; there are
2241 no partial specializations of functions. Therefore, if
2242 the type of DECL does not match FN, there is no
2245 Note that it should never be the case that we have both
2246 candidates added here, and for regular member functions
2248 if (tsk
== tsk_template
)
2250 if (compparms (fn_arg_types
, decl_arg_types
))
2251 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2255 /* See whether this function might be a specialization of this
2256 template. Suppress access control because we might be trying
2257 to make this specialization a friend, and we have already done
2258 access control for the declaration of the specialization. */
2259 push_deferring_access_checks (dk_no_check
);
2260 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2261 pop_deferring_access_checks ();
2264 /* We cannot deduce template arguments that when used to
2265 specialize TMPL will produce DECL. */
2268 if (uses_template_parms (targs
))
2269 /* We deduced something involving 'auto', which isn't a valid
2270 template argument. */
2273 /* Remove, from the set of candidates, all those functions
2274 whose constraints are not satisfied. */
2275 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2278 // Then, try to form the new function type.
2279 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2280 if (insttype
== error_mark_node
)
2283 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2284 if (!compparms (fn_arg_types
, decl_arg_types
))
2287 /* Save this template, and the arguments deduced. */
2288 templates
= tree_cons (targs
, fn
, templates
);
2290 else if (need_member_template
)
2291 /* FN is an ordinary member function, and we need a
2292 specialization of a member template. */
2294 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2295 /* We can get IDENTIFIER_NODEs here in certain erroneous
2298 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2299 /* This is just an ordinary non-member function. Nothing can
2300 be a specialization of that. */
2302 else if (DECL_ARTIFICIAL (fn
))
2303 /* Cannot specialize functions that are created implicitly. */
2307 tree decl_arg_types
;
2309 /* This is an ordinary member function. However, since
2310 we're here, we can assume its enclosing class is a
2311 template class. For example,
2313 template <typename T> struct S { void f(); };
2314 template <> void S<int>::f() {}
2316 Here, S<int>::f is a non-template, but S<int> is a
2317 template class. If FN has the same type as DECL, we
2318 might be in business. */
2320 if (!DECL_TEMPLATE_INFO (fn
))
2321 /* Its enclosing class is an explicit specialization
2322 of a template class. This is not a candidate. */
2325 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2326 TREE_TYPE (TREE_TYPE (fn
))))
2327 /* The return types differ. */
2330 /* Adjust the type of DECL in case FN is a static member. */
2331 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2332 if (DECL_STATIC_FUNCTION_P (fn
)
2333 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2334 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2336 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2340 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2341 && (type_memfn_rqual (TREE_TYPE (decl
))
2342 != type_memfn_rqual (TREE_TYPE (fn
))))
2345 // If the deduced arguments do not satisfy the constraints,
2346 // this is not a candidate.
2347 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2350 // Add the candidate.
2351 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2355 if (templates
&& TREE_CHAIN (templates
))
2361 It is possible for a specialization with a given function
2362 signature to be instantiated from more than one function
2363 template. In such cases, explicit specification of the
2364 template arguments must be used to uniquely identify the
2365 function template specialization being specialized.
2367 Note that here, there's no suggestion that we're supposed to
2368 determine which of the candidate templates is most
2369 specialized. However, we, also have:
2373 Partial ordering of overloaded function template
2374 declarations is used in the following contexts to select
2375 the function template to which a function template
2376 specialization refers:
2378 -- when an explicit specialization refers to a function
2381 So, we do use the partial ordering rules, at least for now.
2382 This extension can only serve to make invalid programs valid,
2383 so it's safe. And, there is strong anecdotal evidence that
2384 the committee intended the partial ordering rules to apply;
2385 the EDG front end has that behavior, and John Spicer claims
2386 that the committee simply forgot to delete the wording in
2387 [temp.expl.spec]. */
2388 tree tmpl
= most_specialized_instantiation (templates
);
2389 if (tmpl
!= error_mark_node
)
2392 TREE_CHAIN (templates
) = NULL_TREE
;
2396 // Concepts allows multiple declarations of member functions
2397 // with the same signature. Like above, we need to rely on
2398 // on the partial ordering of those candidates to determine which
2400 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2402 if (tree cand
= most_constrained_function (candidates
))
2405 TREE_CHAIN (cand
) = NULL_TREE
;
2409 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2411 error ("template-id %qD for %q+D does not match any template "
2412 "declaration", template_id
, decl
);
2413 if (header_count
&& header_count
!= template_count
+ 1)
2414 inform (DECL_SOURCE_LOCATION (decl
),
2415 "saw %d %<template<>%>, need %d for "
2416 "specializing a member function template",
2417 header_count
, template_count
+ 1);
2419 print_candidates (orig_fns
);
2420 return error_mark_node
;
2422 else if ((templates
&& TREE_CHAIN (templates
))
2423 || (candidates
&& TREE_CHAIN (candidates
))
2424 || (templates
&& candidates
))
2426 error ("ambiguous template specialization %qD for %q+D",
2428 candidates
= chainon (candidates
, templates
);
2429 print_candidates (candidates
);
2430 return error_mark_node
;
2433 /* We have one, and exactly one, match. */
2436 tree fn
= TREE_VALUE (candidates
);
2437 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2439 /* Propagate the candidate's constraints to the declaration. */
2440 set_constraints (decl
, get_constraints (fn
));
2442 /* DECL is a re-declaration or partial instantiation of a template
2444 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2446 /* It was a specialization of an ordinary member function in a
2448 return DECL_TI_TEMPLATE (fn
);
2451 /* It was a specialization of a template. */
2452 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2453 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2455 *targs_out
= copy_node (targs
);
2456 SET_TMPL_ARGS_LEVEL (*targs_out
,
2457 TMPL_ARGS_DEPTH (*targs_out
),
2458 TREE_PURPOSE (templates
));
2461 *targs_out
= TREE_PURPOSE (templates
);
2462 return TREE_VALUE (templates
);
2465 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2466 but with the default argument values filled in from those in the
2470 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2473 tree new_spec_types
;
2478 if (spec_types
== void_list_node
)
2479 return void_list_node
;
2481 /* Substitute into the rest of the list. */
2483 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2484 TREE_CHAIN (tmpl_types
));
2486 /* Add the default argument for this parameter. */
2487 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2488 TREE_VALUE (spec_types
),
2492 /* DECL is an explicit specialization. Replicate default arguments
2493 from the template it specializes. (That way, code like:
2495 template <class T> void f(T = 3);
2496 template <> void f(double);
2499 works, as required.) An alternative approach would be to look up
2500 the correct default arguments at the call-site, but this approach
2501 is consistent with how implicit instantiations are handled. */
2504 copy_default_args_to_explicit_spec (tree decl
)
2509 tree new_spec_types
;
2513 tree object_type
= NULL_TREE
;
2514 tree in_charge
= NULL_TREE
;
2515 tree vtt
= NULL_TREE
;
2517 /* See if there's anything we need to do. */
2518 tmpl
= DECL_TI_TEMPLATE (decl
);
2519 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2520 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2521 if (TREE_PURPOSE (t
))
2526 old_type
= TREE_TYPE (decl
);
2527 spec_types
= TYPE_ARG_TYPES (old_type
);
2529 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2531 /* Remove the this pointer, but remember the object's type for
2533 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2534 spec_types
= TREE_CHAIN (spec_types
);
2535 tmpl_types
= TREE_CHAIN (tmpl_types
);
2537 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2539 /* DECL may contain more parameters than TMPL due to the extra
2540 in-charge parameter in constructors and destructors. */
2541 in_charge
= spec_types
;
2542 spec_types
= TREE_CHAIN (spec_types
);
2544 if (DECL_HAS_VTT_PARM_P (decl
))
2547 spec_types
= TREE_CHAIN (spec_types
);
2551 /* Compute the merged default arguments. */
2553 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2555 /* Compute the new FUNCTION_TYPE. */
2559 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2564 /* Put the in-charge parameter back. */
2565 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2566 TREE_VALUE (in_charge
),
2569 new_type
= build_method_type_directly (object_type
,
2570 TREE_TYPE (old_type
),
2574 new_type
= build_function_type (TREE_TYPE (old_type
),
2576 new_type
= cp_build_type_attribute_variant (new_type
,
2577 TYPE_ATTRIBUTES (old_type
));
2578 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2580 TREE_TYPE (decl
) = new_type
;
2583 /* Return the number of template headers we expect to see for a definition
2584 or specialization of CTYPE or one of its non-template members. */
2587 num_template_headers_for_class (tree ctype
)
2589 int num_templates
= 0;
2591 while (ctype
&& CLASS_TYPE_P (ctype
))
2593 /* You're supposed to have one `template <...>' for every
2594 template class, but you don't need one for a full
2595 specialization. For example:
2597 template <class T> struct S{};
2598 template <> struct S<int> { void f(); };
2599 void S<int>::f () {}
2601 is correct; there shouldn't be a `template <>' for the
2602 definition of `S<int>::f'. */
2603 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2604 /* If CTYPE does not have template information of any
2605 kind, then it is not a template, nor is it nested
2606 within a template. */
2608 if (explicit_class_specialization_p (ctype
))
2610 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2613 ctype
= TYPE_CONTEXT (ctype
);
2616 return num_templates
;
2619 /* Do a simple sanity check on the template headers that precede the
2620 variable declaration DECL. */
2623 check_template_variable (tree decl
)
2625 tree ctx
= CP_DECL_CONTEXT (decl
);
2626 int wanted
= num_template_headers_for_class (ctx
);
2627 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2628 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2630 if (cxx_dialect
< cxx14
)
2631 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2632 "variable templates only available with "
2633 "%<-std=c++14%> or %<-std=gnu++14%>");
2635 // Namespace-scope variable templates should have a template header.
2638 if (template_header_count
> wanted
)
2640 auto_diagnostic_group d
;
2641 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2642 "too many template headers for %qD "
2645 if (warned
&& CLASS_TYPE_P (ctx
)
2646 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2647 inform (DECL_SOURCE_LOCATION (decl
),
2648 "members of an explicitly specialized class are defined "
2649 "without a template header");
2653 /* An explicit specialization whose declarator-id or class-head-name is not
2654 qualified shall be declared in the nearest enclosing namespace of the
2655 template, or, if the namespace is inline (7.3.1), any namespace from its
2656 enclosing namespace set.
2658 If the name declared in the explicit instantiation is an unqualified name,
2659 the explicit instantiation shall appear in the namespace where its template
2660 is declared or, if that namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set. */
2664 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2666 tree tmpl
= most_general_template (t
);
2667 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2668 && !is_nested_namespace (current_namespace
,
2669 CP_DECL_CONTEXT (tmpl
), true))
2671 if (processing_specialization
)
2672 permerror (loc
, "explicit specialization of %qD outside its "
2673 "namespace must use a nested-name-specifier", tmpl
);
2674 else if (processing_explicit_instantiation
2675 && cxx_dialect
>= cxx11
)
2676 /* This was allowed in C++98, so only pedwarn. */
2677 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2678 "outside its namespace must use a nested-name-"
2683 /* Warn for a template specialization SPEC that is missing some of a set
2684 of function or type attributes that the template TEMPL is declared with.
2685 ATTRLIST is a list of additional attributes that SPEC should be taken
2686 to ultimately be declared with. */
2689 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2691 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2692 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2694 /* Avoid warning if the difference between the primary and
2695 the specialization is not in one of the attributes below. */
2696 const char* const blacklist
[] = {
2697 "alloc_align", "alloc_size", "assume_aligned", "format",
2698 "format_arg", "malloc", "nonnull", NULL
2701 /* Put together a list of the black listed attributes that the primary
2702 template is declared with that the specialization is not, in case
2703 it's not apparent from the most recent declaration of the primary. */
2705 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2711 auto_diagnostic_group d
;
2712 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2713 "explicit specialization %q#D may be missing attributes",
2715 inform (DECL_SOURCE_LOCATION (tmpl
),
2717 ? G_("missing primary template attributes %s")
2718 : G_("missing primary template attribute %s"),
2719 pp_formatted_text (&str
));
2722 /* Check to see if the function just declared, as indicated in
2723 DECLARATOR, and in DECL, is a specialization of a function
2724 template. We may also discover that the declaration is an explicit
2725 instantiation at this point.
2727 Returns DECL, or an equivalent declaration that should be used
2728 instead if all goes well. Issues an error message if something is
2729 amiss. Returns error_mark_node if the error is not easily
2732 FLAGS is a bitmask consisting of the following flags:
2734 2: The function has a definition.
2735 4: The function is a friend.
2737 The TEMPLATE_COUNT is the number of references to qualifying
2738 template classes that appeared in the name of the function. For
2741 template <class T> struct S { void f(); };
2744 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2745 classes are not counted in the TEMPLATE_COUNT, so that in
2747 template <class T> struct S {};
2748 template <> struct S<int> { void f(); }
2749 template <> void S<int>::f();
2751 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2752 invalid; there should be no template <>.)
2754 If the function is a specialization, it is marked as such via
2755 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2756 is set up correctly, and it is added to the list of specializations
2757 for that template. */
2760 check_explicit_specialization (tree declarator
,
2766 int have_def
= flags
& 2;
2767 int is_friend
= flags
& 4;
2768 bool is_concept
= flags
& 8;
2769 int specialization
= 0;
2770 int explicit_instantiation
= 0;
2771 int member_specialization
= 0;
2772 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2773 tree dname
= DECL_NAME (decl
);
2778 if (!processing_specialization
)
2781 tsk
= tsk_excessive_parms
;
2784 tsk
= current_tmpl_spec_kind (template_count
);
2789 if (processing_specialization
&& !VAR_P (decl
))
2792 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2794 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2797 /* This could be something like:
2799 template <class T> void f(T);
2800 class S { friend void f<>(int); } */
2804 /* This case handles bogus declarations like template <>
2805 template <class T> void f<int>(); */
2807 error_at (cp_expr_loc_or_input_loc (declarator
),
2808 "template-id %qE in declaration of primary template",
2815 case tsk_invalid_member_spec
:
2816 /* The error has already been reported in
2817 check_specialization_scope. */
2818 return error_mark_node
;
2820 case tsk_invalid_expl_inst
:
2821 error ("template parameter list used in explicit instantiation");
2827 error ("definition provided for explicit instantiation");
2829 explicit_instantiation
= 1;
2832 case tsk_excessive_parms
:
2833 case tsk_insufficient_parms
:
2834 if (tsk
== tsk_excessive_parms
)
2835 error ("too many template parameter lists in declaration of %qD",
2837 else if (template_header_count
)
2838 error("too few template parameter lists in declaration of %qD", decl
);
2840 error("explicit specialization of %qD must be introduced by "
2841 "%<template <>%>", decl
);
2846 error ("explicit specialization declared %<concept%>");
2848 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2849 /* In cases like template<> constexpr bool v = true;
2850 We'll give an error in check_template_variable. */
2853 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2855 member_specialization
= 1;
2861 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2863 /* This case handles bogus declarations like template <>
2864 template <class T> void f<int>(); */
2866 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2867 error_at (cp_expr_loc_or_input_loc (declarator
),
2868 "template-id %qE in declaration of primary template",
2870 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2872 /* Partial specialization of variable template. */
2873 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2877 else if (cxx_dialect
< cxx14
)
2878 error_at (cp_expr_loc_or_input_loc (declarator
),
2879 "non-type partial specialization %qE "
2880 "is not allowed", declarator
);
2882 error_at (cp_expr_loc_or_input_loc (declarator
),
2883 "non-class, non-variable partial specialization %qE "
2884 "is not allowed", declarator
);
2889 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2890 /* This is a specialization of a member template, without
2891 specialization the containing class. Something like:
2893 template <class T> struct S {
2894 template <class U> void f (U);
2896 template <> template <class U> void S<int>::f(U) {}
2898 That's a specialization -- but of the entire template. */
2906 if ((specialization
|| member_specialization
)
2907 /* This doesn't apply to variable templates. */
2908 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2910 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2911 for (; t
; t
= TREE_CHAIN (t
))
2912 if (TREE_PURPOSE (t
))
2914 permerror (input_location
,
2915 "default argument specified in explicit specialization");
2920 if (specialization
|| member_specialization
|| explicit_instantiation
)
2922 tree tmpl
= NULL_TREE
;
2923 tree targs
= NULL_TREE
;
2924 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2926 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2927 if (!was_template_id
)
2931 gcc_assert (identifier_p (declarator
));
2936 /* If there is no class context, the explicit instantiation
2937 must be at namespace scope. */
2938 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2940 /* Find the namespace binding, using the declaration
2942 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2944 if (fns
== error_mark_node
)
2945 /* If lookup fails, look for a friend declaration so we can
2946 give a better diagnostic. */
2947 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2948 /*type*/false, /*complain*/true,
2951 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2953 error ("%qD is not a template function", dname
);
2954 fns
= error_mark_node
;
2958 declarator
= lookup_template_function (fns
, NULL_TREE
);
2961 if (declarator
== error_mark_node
)
2962 return error_mark_node
;
2964 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2966 if (!explicit_instantiation
)
2967 /* A specialization in class scope. This is invalid,
2968 but the error will already have been flagged by
2969 check_specialization_scope. */
2970 return error_mark_node
;
2973 /* It's not valid to write an explicit instantiation in
2976 class C { template void f(); }
2978 This case is caught by the parser. However, on
2981 template class C { void f(); };
2983 (which is invalid) we can get here. The error will be
2990 else if (ctype
!= NULL_TREE
2991 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2993 // We'll match variable templates in start_decl.
2997 /* Find the list of functions in ctype that have the same
2998 name as the declared function. */
2999 tree name
= TREE_OPERAND (declarator
, 0);
3001 if (constructor_name_p (name
, ctype
))
3003 if (DECL_CONSTRUCTOR_P (decl
)
3004 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3005 : !CLASSTYPE_DESTRUCTOR (ctype
))
3007 /* From [temp.expl.spec]:
3009 If such an explicit specialization for the member
3010 of a class template names an implicitly-declared
3011 special member function (clause _special_), the
3012 program is ill-formed.
3014 Similar language is found in [temp.explicit]. */
3015 error ("specialization of implicitly-declared special member function");
3016 return error_mark_node
;
3019 name
= DECL_NAME (decl
);
3022 /* For a type-conversion operator, We might be looking for
3023 `operator int' which will be a specialization of
3024 `operator T'. Grab all the conversion operators, and
3025 then select from them. */
3026 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3027 ? conv_op_identifier
: name
);
3029 if (fns
== NULL_TREE
)
3031 error ("no member function %qD declared in %qT", name
, ctype
);
3032 return error_mark_node
;
3035 TREE_OPERAND (declarator
, 0) = fns
;
3038 /* Figure out what exactly is being specialized at this point.
3039 Note that for an explicit instantiation, even one for a
3040 member function, we cannot tell a priori whether the
3041 instantiation is for a member template, or just a member
3042 function of a template class. Even if a member template is
3043 being instantiated, the member template arguments may be
3044 elided if they can be deduced from the rest of the
3046 tmpl
= determine_specialization (declarator
, decl
,
3048 member_specialization
,
3052 if (!tmpl
|| tmpl
== error_mark_node
)
3053 /* We couldn't figure out what this declaration was
3055 return error_mark_node
;
3058 if (TREE_CODE (decl
) == FUNCTION_DECL
3059 && DECL_HIDDEN_FRIEND_P (tmpl
))
3061 auto_diagnostic_group d
;
3062 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3063 "friend declaration %qD is not visible to "
3064 "explicit specialization", tmpl
))
3065 inform (DECL_SOURCE_LOCATION (tmpl
),
3066 "friend declaration here");
3068 else if (!ctype
&& !is_friend
3069 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3070 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3072 tree gen_tmpl
= most_general_template (tmpl
);
3074 if (explicit_instantiation
)
3076 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3077 is done by do_decl_instantiation later. */
3079 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3080 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3082 if (arg_depth
> parm_depth
)
3084 /* If TMPL is not the most general template (for
3085 example, if TMPL is a friend template that is
3086 injected into namespace scope), then there will
3087 be too many levels of TARGS. Remove some of them
3092 new_targs
= make_tree_vec (parm_depth
);
3093 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3094 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3095 = TREE_VEC_ELT (targs
, i
);
3099 return instantiate_template (tmpl
, targs
, tf_error
);
3102 /* If we thought that the DECL was a member function, but it
3103 turns out to be specializing a static member function,
3104 make DECL a static member function as well. */
3105 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3106 && DECL_STATIC_FUNCTION_P (tmpl
)
3107 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3108 revert_static_member_fn (decl
);
3110 /* If this is a specialization of a member template of a
3111 template class, we want to return the TEMPLATE_DECL, not
3112 the specialization of it. */
3113 if (tsk
== tsk_template
&& !was_template_id
)
3115 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3116 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3117 DECL_INITIAL (result
) = NULL_TREE
;
3121 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3122 DECL_SOURCE_LOCATION (result
)
3123 = DECL_SOURCE_LOCATION (decl
);
3124 /* We want to use the argument list specified in the
3125 definition, not in the original declaration. */
3126 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3127 for (parm
= DECL_ARGUMENTS (result
); parm
;
3128 parm
= DECL_CHAIN (parm
))
3129 DECL_CONTEXT (parm
) = result
;
3131 return register_specialization (tmpl
, gen_tmpl
, targs
,
3135 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3136 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3138 if (was_template_id
)
3139 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3141 /* Inherit default function arguments from the template
3142 DECL is specializing. */
3143 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3144 copy_default_args_to_explicit_spec (decl
);
3146 /* This specialization has the same protection as the
3147 template it specializes. */
3148 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3149 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3151 /* 7.1.1-1 [dcl.stc]
3153 A storage-class-specifier shall not be specified in an
3154 explicit specialization...
3156 The parser rejects these, so unless action is taken here,
3157 explicit function specializations will always appear with
3160 The action recommended by the C++ CWG in response to C++
3161 defect report 605 is to make the storage class and linkage
3162 of the explicit specialization match the templated function:
3164 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3166 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3168 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3169 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3171 /* A concept cannot be specialized. */
3172 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3174 error ("explicit specialization of function concept %qD",
3176 return error_mark_node
;
3179 /* This specialization has the same linkage and visibility as
3180 the function template it specializes. */
3181 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3182 if (! TREE_PUBLIC (decl
))
3184 DECL_INTERFACE_KNOWN (decl
) = 1;
3185 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3187 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3188 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3190 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3191 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3195 /* If DECL is a friend declaration, declared using an
3196 unqualified name, the namespace associated with DECL may
3197 have been set incorrectly. For example, in:
3199 template <typename T> void f(T);
3201 struct S { friend void f<int>(int); }
3204 we will have set the DECL_CONTEXT for the friend
3205 declaration to N, rather than to the global namespace. */
3206 if (DECL_NAMESPACE_SCOPE_P (decl
))
3207 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3209 if (is_friend
&& !have_def
)
3210 /* This is not really a declaration of a specialization.
3211 It's just the name of an instantiation. But, it's not
3212 a request for an instantiation, either. */
3213 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3214 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3215 /* A specialization is not necessarily COMDAT. */
3216 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3217 && DECL_DECLARED_INLINE_P (decl
));
3218 else if (VAR_P (decl
))
3219 DECL_COMDAT (decl
) = false;
3221 /* If this is a full specialization, register it so that we can find
3222 it again. Partial specializations will be registered in
3223 process_partial_specialization. */
3224 if (!processing_template_decl
)
3226 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3228 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3233 /* A 'structor should already have clones. */
3234 gcc_assert (decl
== error_mark_node
3235 || variable_template_p (tmpl
)
3236 || !(DECL_CONSTRUCTOR_P (decl
)
3237 || DECL_DESTRUCTOR_P (decl
))
3238 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3245 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3246 parameters. These are represented in the same format used for
3247 DECL_TEMPLATE_PARMS. */
3250 comp_template_parms (const_tree parms1
, const_tree parms2
)
3255 if (parms1
== parms2
)
3258 for (p1
= parms1
, p2
= parms2
;
3259 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3260 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3262 tree t1
= TREE_VALUE (p1
);
3263 tree t2
= TREE_VALUE (p2
);
3266 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3267 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3269 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3272 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3274 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3275 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3277 /* If either of the template parameters are invalid, assume
3278 they match for the sake of error recovery. */
3279 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3282 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3285 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3286 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3287 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3289 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3294 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3295 /* One set of parameters has more parameters lists than the
3302 /* Returns true if two template parameters are declared with
3303 equivalent constraints. */
3306 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3308 tree req1
= TREE_TYPE (parm1
);
3309 tree req2
= TREE_TYPE (parm2
);
3313 return cp_tree_equal (req1
, req2
);
3317 /* Returns true when two template parameters are equivalent. */
3320 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3322 tree decl1
= TREE_VALUE (parm1
);
3323 tree decl2
= TREE_VALUE (parm2
);
3325 /* If either of the template parameters are invalid, assume
3326 they match for the sake of error recovery. */
3327 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3330 /* ... they declare parameters of the same kind. */
3331 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3334 /* ... one parameter was introduced by a parameter declaration, then
3335 both are. This case arises as a result of eagerly rewriting declarations
3337 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3340 /* ... if either declares a pack, they both do. */
3341 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3344 if (TREE_CODE (decl1
) == PARM_DECL
)
3346 /* ... if they declare non-type parameters, the types are equivalent. */
3347 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3350 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3352 /* ... if they declare template template parameters, their template
3353 parameter lists are equivalent. */
3354 if (!template_heads_equivalent_p (decl1
, decl2
))
3358 /* ... if they are declared with a qualified-concept name, they both
3359 are, and those names are equivalent. */
3360 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3363 /* Returns true if two template parameters lists are equivalent.
3364 Two template parameter lists are equivalent if they have the
3365 same length and their corresponding parameters are equivalent.
3367 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3368 data structure returned by DECL_TEMPLATE_PARMS.
3370 This is generally the same implementation as comp_template_parms
3371 except that it also the concept names and arguments used to
3372 introduce parameters. */
3375 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3377 if (parms1
== parms2
)
3380 const_tree p1
= parms1
;
3381 const_tree p2
= parms2
;
3382 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3384 tree list1
= TREE_VALUE (p1
);
3385 tree list2
= TREE_VALUE (p2
);
3387 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3390 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3392 tree parm1
= TREE_VEC_ELT (list1
, i
);
3393 tree parm2
= TREE_VEC_ELT (list2
, i
);
3394 if (!template_parameters_equivalent_p (parm1
, parm2
))
3398 p1
= TREE_CHAIN (p1
);
3399 p2
= TREE_CHAIN (p2
);
3402 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3408 /* Return true if the requires-clause of the template parameter lists are
3409 equivalent and false otherwise. */
3411 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3413 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3414 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3415 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3417 if (!cp_tree_equal (req1
, req2
))
3422 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3423 Two template heads are equivalent if their template parameter
3424 lists are equivalent and their requires clauses are equivalent.
3426 In pre-C++20, this is equivalent to calling comp_template_parms
3427 for the template parameters of TMPL1 and TMPL2. */
3430 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3432 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3433 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3435 /* Don't change the matching rules for pre-C++20. */
3436 if (cxx_dialect
< cxx2a
)
3437 return comp_template_parms (parms1
, parms2
);
3439 /* ... have the same number of template parameters, and their
3440 corresponding parameters are equivalent. */
3441 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3444 /* ... if either has a requires-clause, they both do and their
3445 corresponding constraint-expressions are equivalent. */
3446 return template_requirements_equivalent_p (parms1
, parms2
);
3449 /* Determine whether PARM is a parameter pack. */
3452 template_parameter_pack_p (const_tree parm
)
3454 /* Determine if we have a non-type template parameter pack. */
3455 if (TREE_CODE (parm
) == PARM_DECL
)
3456 return (DECL_TEMPLATE_PARM_P (parm
)
3457 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3458 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3459 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3461 /* If this is a list of template parameters, we could get a
3462 TYPE_DECL or a TEMPLATE_DECL. */
3463 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3464 parm
= TREE_TYPE (parm
);
3466 /* Otherwise it must be a type template parameter. */
3467 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3468 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3469 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3472 /* Determine if T is a function parameter pack. */
3475 function_parameter_pack_p (const_tree t
)
3477 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3478 return DECL_PACK_P (t
);
3482 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3483 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3486 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3488 if (! primary_func_tmpl_inst
3489 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3490 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3493 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3496 /* Return true iff the function parameter PARAM_DECL was expanded
3497 from the function parameter pack PACK. */
3500 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3502 if (DECL_ARTIFICIAL (param_decl
)
3503 || !function_parameter_pack_p (pack
))
3506 /* The parameter pack and its pack arguments have the same
3508 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3511 /* Determine whether ARGS describes a variadic template args list,
3512 i.e., one that is terminated by a template argument pack. */
3515 template_args_variadic_p (tree args
)
3520 if (args
== NULL_TREE
)
3523 args
= INNERMOST_TEMPLATE_ARGS (args
);
3524 nargs
= TREE_VEC_LENGTH (args
);
3529 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3531 return ARGUMENT_PACK_P (last_parm
);
3534 /* Generate a new name for the parameter pack name NAME (an
3535 IDENTIFIER_NODE) that incorporates its */
3538 make_ith_pack_parameter_name (tree name
, int i
)
3540 /* Munge the name to include the parameter index. */
3541 #define NUMBUF_LEN 128
3542 char numbuf
[NUMBUF_LEN
];
3546 if (name
== NULL_TREE
)
3548 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3549 newname_len
= IDENTIFIER_LENGTH (name
)
3550 + strlen (numbuf
) + 2;
3551 newname
= (char*)alloca (newname_len
);
3552 snprintf (newname
, newname_len
,
3553 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3554 return get_identifier (newname
);
3557 /* Return true if T is a primary function, class or alias template
3558 specialization, not including the template pattern. */
3561 primary_template_specialization_p (const_tree t
)
3566 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3567 return (DECL_LANG_SPECIFIC (t
)
3568 && DECL_USE_TEMPLATE (t
)
3569 && DECL_TEMPLATE_INFO (t
)
3570 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3571 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3572 return (CLASSTYPE_TEMPLATE_INFO (t
)
3573 && CLASSTYPE_USE_TEMPLATE (t
)
3574 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3575 else if (alias_template_specialization_p (t
))
3580 /* Return true if PARM is a template template parameter. */
3583 template_template_parameter_p (const_tree parm
)
3585 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3588 /* Return true iff PARM is a DECL representing a type template
3592 template_type_parameter_p (const_tree parm
)
3595 && (TREE_CODE (parm
) == TYPE_DECL
3596 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3597 && DECL_TEMPLATE_PARM_P (parm
));
3600 /* Return the template parameters of T if T is a
3601 primary template instantiation, NULL otherwise. */
3604 get_primary_template_innermost_parameters (const_tree t
)
3606 tree parms
= NULL
, template_info
= NULL
;
3608 if ((template_info
= get_template_info (t
))
3609 && primary_template_specialization_p (t
))
3610 parms
= INNERMOST_TEMPLATE_PARMS
3611 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3616 /* Return the template parameters of the LEVELth level from the full list
3617 of template parameters PARMS. */
3620 get_template_parms_at_level (tree parms
, int level
)
3624 || TREE_CODE (parms
) != TREE_LIST
3625 || level
> TMPL_PARMS_DEPTH (parms
))
3628 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3629 if (TMPL_PARMS_DEPTH (p
) == level
)
3635 /* Returns the template arguments of T if T is a template instantiation,
3639 get_template_innermost_arguments (const_tree t
)
3641 tree args
= NULL
, template_info
= NULL
;
3643 if ((template_info
= get_template_info (t
))
3644 && TI_ARGS (template_info
))
3645 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3650 /* Return the argument pack elements of T if T is a template argument pack,
3654 get_template_argument_pack_elems (const_tree t
)
3656 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3657 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3660 return ARGUMENT_PACK_ARGS (t
);
3663 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3664 ARGUMENT_PACK_SELECT represents. */
3667 argument_pack_select_arg (tree t
)
3669 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3670 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3672 /* If the selected argument is an expansion E, that most likely means we were
3673 called from gen_elem_of_pack_expansion_instantiation during the
3674 substituting of an argument pack (of which the Ith element is a pack
3675 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3676 In this case, the Ith element resulting from this substituting is going to
3677 be a pack expansion, which pattern is the pattern of E. Let's return the
3678 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3679 resulting pack expansion from it. */
3680 if (PACK_EXPANSION_P (arg
))
3682 /* Make sure we aren't throwing away arg info. */
3683 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3684 arg
= PACK_EXPANSION_PATTERN (arg
);
3691 /* True iff FN is a function representing a built-in variadic parameter
3695 builtin_pack_fn_p (tree fn
)
3698 || TREE_CODE (fn
) != FUNCTION_DECL
3699 || !DECL_IS_BUILTIN (fn
))
3702 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3708 /* True iff CALL is a call to a function representing a built-in variadic
3712 builtin_pack_call_p (tree call
)
3714 if (TREE_CODE (call
) != CALL_EXPR
)
3716 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3719 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3722 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3725 tree ohi
= CALL_EXPR_ARG (call
, 0);
3726 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3727 false/*fn*/, true/*int_cst*/);
3729 if (value_dependent_expression_p (hi
))
3733 call
= copy_node (call
);
3734 CALL_EXPR_ARG (call
, 0) = hi
;
3736 tree ex
= make_pack_expansion (call
, complain
);
3737 tree vec
= make_tree_vec (1);
3738 TREE_VEC_ELT (vec
, 0) = ex
;
3743 hi
= cxx_constant_value (hi
);
3744 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3746 /* Calculate the largest value of len that won't make the size of the vec
3747 overflow an int. The compiler will exceed resource limits long before
3748 this, but it seems a decent place to diagnose. */
3749 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3751 if (len
< 0 || len
> max
)
3753 if ((complain
& tf_error
)
3754 && hi
!= error_mark_node
)
3755 error ("argument to %<__integer_pack%> must be between 0 and %d",
3757 return error_mark_node
;
3760 tree vec
= make_tree_vec (len
);
3762 for (int i
= 0; i
< len
; ++i
)
3763 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3769 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3773 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3776 if (!builtin_pack_call_p (call
))
3779 tree fn
= CALL_EXPR_FN (call
);
3781 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3782 return expand_integer_pack (call
, args
, complain
, in_decl
);
3787 /* Structure used to track the progress of find_parameter_packs_r. */
3788 struct find_parameter_pack_data
3790 /* TREE_LIST that will contain all of the parameter packs found by
3792 tree
* parameter_packs
;
3794 /* Set of AST nodes that have been visited by the traversal. */
3795 hash_set
<tree
> *visited
;
3797 /* True iff we're making a type pack expansion. */
3798 bool type_pack_expansion_p
;
3801 /* Identifies all of the argument packs that occur in a template
3802 argument and appends them to the TREE_LIST inside DATA, which is a
3803 find_parameter_pack_data structure. This is a subroutine of
3804 make_pack_expansion and uses_parameter_packs. */
3806 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3809 struct find_parameter_pack_data
* ppd
=
3810 (struct find_parameter_pack_data
*)data
;
3811 bool parameter_pack_p
= false;
3813 /* Handle type aliases/typedefs. */
3814 if (TYPE_ALIAS_P (t
))
3816 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3817 cp_walk_tree (&TI_ARGS (tinfo
),
3818 &find_parameter_packs_r
,
3824 /* Identify whether this is a parameter pack or not. */
3825 switch (TREE_CODE (t
))
3827 case TEMPLATE_PARM_INDEX
:
3828 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3829 parameter_pack_p
= true;
3832 case TEMPLATE_TYPE_PARM
:
3833 t
= TYPE_MAIN_VARIANT (t
);
3835 case TEMPLATE_TEMPLATE_PARM
:
3836 /* If the placeholder appears in the decl-specifier-seq of a function
3837 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3838 is a pack expansion, the invented template parameter is a template
3840 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3841 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3842 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3843 parameter_pack_p
= true;
3848 if (DECL_PACK_P (t
))
3850 /* We don't want to walk into the type of a PARM_DECL,
3851 because we don't want to see the type parameter pack. */
3853 parameter_pack_p
= true;
3858 if (DECL_PACK_P (t
))
3860 /* We don't want to walk into the type of a variadic capture proxy,
3861 because we don't want to see the type parameter pack. */
3863 parameter_pack_p
= true;
3865 else if (variable_template_specialization_p (t
))
3867 cp_walk_tree (&DECL_TI_ARGS (t
),
3868 find_parameter_packs_r
,
3875 if (builtin_pack_call_p (t
))
3876 parameter_pack_p
= true;
3880 parameter_pack_p
= true;
3883 /* Not a parameter pack. */
3887 if (parameter_pack_p
)
3889 /* Add this parameter pack to the list. */
3890 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3894 cp_walk_tree (&TYPE_CONTEXT (t
),
3895 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3897 /* This switch statement will return immediately if we don't find a
3899 switch (TREE_CODE (t
))
3901 case TEMPLATE_PARM_INDEX
:
3904 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3905 /* Check the template itself. */
3906 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3907 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3908 /* Check the template arguments. */
3909 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3914 case TEMPLATE_TYPE_PARM
:
3915 case TEMPLATE_TEMPLATE_PARM
:
3922 /* Ignore the declaration of a capture proxy for a parameter pack. */
3923 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3928 if (TYPE_PTRMEMFUNC_P (t
))
3934 if (TYPE_TEMPLATE_INFO (t
))
3935 cp_walk_tree (&TYPE_TI_ARGS (t
),
3936 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3942 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3947 cp_walk_tree (&TREE_TYPE (t
),
3948 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3952 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3957 case TYPE_PACK_EXPANSION
:
3958 case EXPR_PACK_EXPANSION
:
3963 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3968 case IDENTIFIER_NODE
:
3969 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3976 /* Look at explicit captures. */
3977 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3978 cap
; cap
= TREE_CHAIN (cap
))
3979 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3981 /* Since we defer implicit capture, look in the parms and body. */
3982 tree fn
= lambda_function (t
);
3983 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3985 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3993 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3994 type_pack_expansion_p to false so that any placeholders
3995 within the expression don't get marked as parameter packs. */
3996 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3997 ppd
->type_pack_expansion_p
= false;
3998 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4000 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4006 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4008 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4010 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4012 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4023 /* Determines if the expression or type T uses any parameter packs. */
4025 uses_parameter_packs (tree t
)
4027 tree parameter_packs
= NULL_TREE
;
4028 struct find_parameter_pack_data ppd
;
4029 ppd
.parameter_packs
= ¶meter_packs
;
4030 ppd
.visited
= new hash_set
<tree
>;
4031 ppd
.type_pack_expansion_p
= false;
4032 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4034 return parameter_packs
;
4037 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4038 representation a base-class initializer into a parameter pack
4039 expansion. If all goes well, the resulting node will be an
4040 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4043 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4046 tree parameter_packs
= NULL_TREE
;
4047 bool for_types
= false;
4048 struct find_parameter_pack_data ppd
;
4050 if (!arg
|| arg
== error_mark_node
)
4053 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4055 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4056 class initializer. In this case, the TREE_PURPOSE will be a
4057 _TYPE node (representing the base class expansion we're
4058 initializing) and the TREE_VALUE will be a TREE_LIST
4059 containing the initialization arguments.
4061 The resulting expansion looks somewhat different from most
4062 expansions. Rather than returning just one _EXPANSION, we
4063 return a TREE_LIST whose TREE_PURPOSE is a
4064 TYPE_PACK_EXPANSION containing the bases that will be
4065 initialized. The TREE_VALUE will be identical to the
4066 original TREE_VALUE, which is a list of arguments that will
4067 be passed to each base. We do not introduce any new pack
4068 expansion nodes into the TREE_VALUE (although it is possible
4069 that some already exist), because the TREE_PURPOSE and
4070 TREE_VALUE all need to be expanded together with the same
4071 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4072 resulting TREE_PURPOSE will mention the parameter packs in
4073 both the bases and the arguments to the bases. */
4076 tree parameter_packs
= NULL_TREE
;
4078 /* Determine which parameter packs will be used by the base
4080 ppd
.visited
= new hash_set
<tree
>;
4081 ppd
.parameter_packs
= ¶meter_packs
;
4082 ppd
.type_pack_expansion_p
= false;
4083 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4084 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4087 if (parameter_packs
== NULL_TREE
)
4089 if (complain
& tf_error
)
4090 error ("base initializer expansion %qT contains no parameter packs",
4093 return error_mark_node
;
4096 if (TREE_VALUE (arg
) != void_type_node
)
4098 /* Collect the sets of parameter packs used in each of the
4099 initialization arguments. */
4100 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4102 /* Determine which parameter packs will be expanded in this
4104 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4111 /* Create the pack expansion type for the base type. */
4112 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4113 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4114 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4115 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4117 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4118 they will rarely be compared to anything. */
4119 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4121 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4124 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4127 /* Build the PACK_EXPANSION_* node. */
4129 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4130 : make_node (EXPR_PACK_EXPANSION
);
4131 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4132 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4134 /* Propagate type and const-expression information. */
4135 TREE_TYPE (result
) = TREE_TYPE (arg
);
4136 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4137 /* Mark this read now, since the expansion might be length 0. */
4138 mark_exp_read (arg
);
4141 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4142 they will rarely be compared to anything. */
4143 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4145 /* Determine which parameter packs will be expanded. */
4146 ppd
.parameter_packs
= ¶meter_packs
;
4147 ppd
.visited
= new hash_set
<tree
>;
4148 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4149 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4152 /* Make sure we found some parameter packs. */
4153 if (parameter_packs
== NULL_TREE
)
4155 if (complain
& tf_error
)
4158 error ("expansion pattern %qT contains no parameter packs", arg
);
4160 error ("expansion pattern %qE contains no parameter packs", arg
);
4162 return error_mark_node
;
4164 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4166 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4171 /* Checks T for any "bare" parameter packs, which have not yet been
4172 expanded, and issues an error if any are found. This operation can
4173 only be done on full expressions or types (e.g., an expression
4174 statement, "if" condition, etc.), because we could have expressions like:
4176 foo(f(g(h(args)))...)
4178 where "args" is a parameter pack. check_for_bare_parameter_packs
4179 should not be called for the subexpressions args, h(args),
4180 g(h(args)), or f(g(h(args))), because we would produce erroneous
4183 Returns TRUE and emits an error if there were bare parameter packs,
4184 returns FALSE otherwise. */
4186 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4188 tree parameter_packs
= NULL_TREE
;
4189 struct find_parameter_pack_data ppd
;
4191 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4194 /* A lambda might use a parameter pack from the containing context. */
4195 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4196 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4199 if (TREE_CODE (t
) == TYPE_DECL
)
4202 ppd
.parameter_packs
= ¶meter_packs
;
4203 ppd
.visited
= new hash_set
<tree
>;
4204 ppd
.type_pack_expansion_p
= false;
4205 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4208 if (parameter_packs
)
4210 if (loc
== UNKNOWN_LOCATION
)
4211 loc
= cp_expr_loc_or_input_loc (t
);
4212 error_at (loc
, "parameter packs not expanded with %<...%>:");
4213 while (parameter_packs
)
4215 tree pack
= TREE_VALUE (parameter_packs
);
4216 tree name
= NULL_TREE
;
4218 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4219 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4220 name
= TYPE_NAME (pack
);
4221 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4222 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4223 else if (TREE_CODE (pack
) == CALL_EXPR
)
4224 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4226 name
= DECL_NAME (pack
);
4229 inform (loc
, " %qD", name
);
4231 inform (loc
, " %s", "<anonymous>");
4233 parameter_packs
= TREE_CHAIN (parameter_packs
);
4242 /* Expand any parameter packs that occur in the template arguments in
4245 expand_template_argument_pack (tree args
)
4247 if (args
== error_mark_node
)
4248 return error_mark_node
;
4250 tree result_args
= NULL_TREE
;
4251 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4252 int num_result_args
= -1;
4253 int non_default_args_count
= -1;
4255 /* First, determine if we need to expand anything, and the number of
4256 slots we'll need. */
4257 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4259 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4260 if (arg
== NULL_TREE
)
4262 if (ARGUMENT_PACK_P (arg
))
4264 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4265 if (num_result_args
< 0)
4266 num_result_args
= in_arg
+ num_packed
;
4268 num_result_args
+= num_packed
;
4272 if (num_result_args
>= 0)
4277 /* If no expansion is necessary, we're done. */
4278 if (num_result_args
< 0)
4281 /* Expand arguments. */
4282 result_args
= make_tree_vec (num_result_args
);
4283 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4284 non_default_args_count
=
4285 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4286 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4288 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4289 if (ARGUMENT_PACK_P (arg
))
4291 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4292 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4293 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4294 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4295 if (non_default_args_count
> 0)
4296 non_default_args_count
+= num_packed
- 1;
4300 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4304 if (non_default_args_count
>= 0)
4305 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4309 /* Checks if DECL shadows a template parameter.
4311 [temp.local]: A template-parameter shall not be redeclared within its
4312 scope (including nested scopes).
4314 Emits an error and returns TRUE if the DECL shadows a parameter,
4315 returns FALSE otherwise. */
4318 check_template_shadow (tree decl
)
4322 /* If we're not in a template, we can't possibly shadow a template
4324 if (!current_template_parms
)
4327 /* Figure out what we're shadowing. */
4328 decl
= OVL_FIRST (decl
);
4329 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4331 /* If there's no previous binding for this name, we're not shadowing
4332 anything, let alone a template parameter. */
4336 /* If we're not shadowing a template parameter, we're done. Note
4337 that OLDDECL might be an OVERLOAD (or perhaps even an
4338 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4340 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4343 /* We check for decl != olddecl to avoid bogus errors for using a
4344 name inside a class. We check TPFI to avoid duplicate errors for
4345 inline member templates. */
4347 || (DECL_TEMPLATE_PARM_P (decl
)
4348 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4351 /* Don't complain about the injected class name, as we've already
4352 complained about the class itself. */
4353 if (DECL_SELF_REFERENCE_P (decl
))
4356 if (DECL_TEMPLATE_PARM_P (decl
))
4357 error ("declaration of template parameter %q+D shadows "
4358 "template parameter", decl
);
4360 error ("declaration of %q+#D shadows template parameter", decl
);
4361 inform (DECL_SOURCE_LOCATION (olddecl
),
4362 "template parameter %qD declared here", olddecl
);
4366 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4367 ORIG_LEVEL, DECL, and TYPE. */
4370 build_template_parm_index (int index
,
4376 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4377 TEMPLATE_PARM_IDX (t
) = index
;
4378 TEMPLATE_PARM_LEVEL (t
) = level
;
4379 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4380 TEMPLATE_PARM_DECL (t
) = decl
;
4381 TREE_TYPE (t
) = type
;
4382 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4383 TREE_READONLY (t
) = TREE_READONLY (decl
);
4388 /* Find the canonical type parameter for the given template type
4389 parameter. Returns the canonical type parameter, which may be TYPE
4390 if no such parameter existed. */
4393 canonical_type_parameter (tree type
)
4396 int idx
= TEMPLATE_TYPE_IDX (type
);
4397 if (!canonical_template_parms
)
4398 vec_alloc (canonical_template_parms
, idx
+ 1);
4400 if (canonical_template_parms
->length () <= (unsigned) idx
)
4401 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4403 list
= (*canonical_template_parms
)[idx
];
4404 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4405 list
= TREE_CHAIN (list
);
4408 return TREE_VALUE (list
);
4411 (*canonical_template_parms
)[idx
]
4412 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4417 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4418 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4419 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4420 new one is created. */
4423 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4424 tsubst_flags_t complain
)
4426 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4427 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4428 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4429 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4431 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4433 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4434 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4436 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4437 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4438 DECL_ARTIFICIAL (decl
) = 1;
4439 SET_DECL_TEMPLATE_PARM_P (decl
);
4441 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4442 TEMPLATE_PARM_LEVEL (index
) - levels
,
4443 TEMPLATE_PARM_ORIG_LEVEL (index
),
4445 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4446 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4447 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4449 /* Template template parameters need this. */
4451 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4453 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4454 TYPE_DECL
, DECL_NAME (decl
), type
);
4455 DECL_TEMPLATE_RESULT (decl
) = inner
;
4456 DECL_ARTIFICIAL (inner
) = true;
4457 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4458 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4461 /* Attach the TPI to the decl. */
4462 if (TREE_CODE (inner
) == TYPE_DECL
)
4463 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4465 DECL_INITIAL (decl
) = tpi
;
4468 return TEMPLATE_PARM_DESCENDANTS (index
);
4471 /* Process information from new template parameter PARM and append it
4472 to the LIST being built. This new parameter is a non-type
4473 parameter iff IS_NON_TYPE is true. This new parameter is a
4474 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4478 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4479 bool is_non_type
, bool is_parameter_pack
)
4484 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4485 tree defval
= TREE_PURPOSE (parm
);
4486 tree constr
= TREE_TYPE (parm
);
4490 tree p
= tree_last (list
);
4492 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4495 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4496 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4498 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4506 parm
= TREE_VALUE (parm
);
4508 SET_DECL_TEMPLATE_PARM_P (parm
);
4510 if (TREE_TYPE (parm
) != error_mark_node
)
4514 The top-level cv-qualifiers on the template-parameter are
4515 ignored when determining its type. */
4516 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4517 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4518 TREE_TYPE (parm
) = error_mark_node
;
4519 else if (uses_parameter_packs (TREE_TYPE (parm
))
4520 && !is_parameter_pack
4521 /* If we're in a nested template parameter list, the template
4522 template parameter could be a parameter pack. */
4523 && processing_template_parmlist
== 1)
4525 /* This template parameter is not a parameter pack, but it
4526 should be. Complain about "bare" parameter packs. */
4527 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4529 /* Recover by calling this a parameter pack. */
4530 is_parameter_pack
= true;
4534 /* A template parameter is not modifiable. */
4535 TREE_CONSTANT (parm
) = 1;
4536 TREE_READONLY (parm
) = 1;
4537 decl
= build_decl (parm_loc
,
4538 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4539 TREE_CONSTANT (decl
) = 1;
4540 TREE_READONLY (decl
) = 1;
4541 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4542 = build_template_parm_index (idx
, processing_template_decl
,
4543 processing_template_decl
,
4544 decl
, TREE_TYPE (parm
));
4546 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4547 = is_parameter_pack
;
4552 parm
= TREE_VALUE (TREE_VALUE (parm
));
4554 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4556 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4557 /* This is for distinguishing between real templates and template
4558 template parameters */
4559 TREE_TYPE (parm
) = t
;
4560 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4565 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4566 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4567 decl
= build_decl (parm_loc
,
4568 TYPE_DECL
, parm
, t
);
4571 TYPE_NAME (t
) = decl
;
4572 TYPE_STUB_DECL (t
) = decl
;
4574 TEMPLATE_TYPE_PARM_INDEX (t
)
4575 = build_template_parm_index (idx
, processing_template_decl
,
4576 processing_template_decl
,
4577 decl
, TREE_TYPE (parm
));
4578 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4579 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4581 DECL_ARTIFICIAL (decl
) = 1;
4582 SET_DECL_TEMPLATE_PARM_P (decl
);
4584 /* Build requirements for the type/template parameter.
4585 This must be done after SET_DECL_TEMPLATE_PARM_P or
4586 process_template_parm could fail. */
4587 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4589 decl
= pushdecl (decl
);
4593 /* Build the parameter node linking the parameter declaration,
4594 its default argument (if any), and its constraints (if any). */
4595 parm
= build_tree_list (defval
, parm
);
4596 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4598 return chainon (list
, parm
);
4601 /* The end of a template parameter list has been reached. Process the
4602 tree list into a parameter vector, converting each parameter into a more
4603 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4607 end_template_parm_list (tree parms
)
4611 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4613 /* Pop the dummy parameter level and add the real one. */
4614 current_template_parms
= TREE_CHAIN (current_template_parms
);
4616 current_template_parms
4617 = tree_cons (size_int (processing_template_decl
),
4618 saved_parmlist
, current_template_parms
);
4620 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4622 next
= TREE_CHAIN (parm
);
4623 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4624 TREE_CHAIN (parm
) = NULL_TREE
;
4627 --processing_template_parmlist
;
4629 return saved_parmlist
;
4632 // Explicitly indicate the end of the template parameter list. We assume
4633 // that the current template parameters have been constructed and/or
4634 // managed explicitly, as when creating new template template parameters
4635 // from a shorthand constraint.
4637 end_template_parm_list ()
4639 --processing_template_parmlist
;
4642 /* end_template_decl is called after a template declaration is seen. */
4645 end_template_decl (void)
4647 reset_specialization ();
4649 if (! processing_template_decl
)
4652 /* This matches the pushlevel in begin_template_parm_list. */
4655 --processing_template_decl
;
4656 current_template_parms
= TREE_CHAIN (current_template_parms
);
4659 /* Takes a TREE_LIST representing a template parameter and convert it
4660 into an argument suitable to be passed to the type substitution
4661 functions. Note that If the TREE_LIST contains an error_mark
4662 node, the returned argument is error_mark_node. */
4665 template_parm_to_arg (tree t
)
4669 || TREE_CODE (t
) != TREE_LIST
)
4672 if (error_operand_p (TREE_VALUE (t
)))
4673 return error_mark_node
;
4677 if (TREE_CODE (t
) == TYPE_DECL
4678 || TREE_CODE (t
) == TEMPLATE_DECL
)
4682 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4684 /* Turn this argument into a TYPE_ARGUMENT_PACK
4685 with a single element, which expands T. */
4686 tree vec
= make_tree_vec (1);
4688 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4690 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4692 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4693 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4698 t
= DECL_INITIAL (t
);
4700 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4702 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4703 with a single element, which expands T. */
4704 tree vec
= make_tree_vec (1);
4706 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4708 t
= convert_from_reference (t
);
4709 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4711 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4712 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4715 t
= convert_from_reference (t
);
4720 /* Given a single level of template parameters (a TREE_VEC), return it
4721 as a set of template arguments. */
4724 template_parms_level_to_args (tree parms
)
4726 tree a
= copy_node (parms
);
4727 TREE_TYPE (a
) = NULL_TREE
;
4728 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4729 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4732 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4737 /* Given a set of template parameters, return them as a set of template
4738 arguments. The template parameters are represented as a TREE_VEC, in
4739 the form documented in cp-tree.h for template arguments. */
4742 template_parms_to_args (tree parms
)
4745 tree args
= NULL_TREE
;
4746 int length
= TMPL_PARMS_DEPTH (parms
);
4749 /* If there is only one level of template parameters, we do not
4750 create a TREE_VEC of TREE_VECs. Instead, we return a single
4751 TREE_VEC containing the arguments. */
4753 args
= make_tree_vec (length
);
4755 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4757 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4760 TREE_VEC_ELT (args
, --l
) = a
;
4768 /* Within the declaration of a template, return the currently active
4769 template parameters as an argument TREE_VEC. */
4772 current_template_args (void)
4774 return template_parms_to_args (current_template_parms
);
4777 /* Return the fully generic arguments for of TMPL, i.e. what
4778 current_template_args would be while parsing it. */
4781 generic_targs_for (tree tmpl
)
4783 if (tmpl
== NULL_TREE
)
4785 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4786 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4787 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4788 template parameter, it has no TEMPLATE_INFO; for a partial
4789 specialization, it has the arguments for the primary template, and we
4790 want the arguments for the partial specialization. */;
4791 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4792 if (tree ti
= get_template_info (result
))
4793 return TI_ARGS (ti
);
4794 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4797 /* Update the declared TYPE by doing any lookups which were thought to be
4798 dependent, but are not now that we know the SCOPE of the declarator. */
4801 maybe_update_decl_type (tree orig_type
, tree scope
)
4803 tree type
= orig_type
;
4805 if (type
== NULL_TREE
)
4808 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4809 type
= TREE_TYPE (type
);
4811 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4812 && dependent_type_p (type
)
4813 /* Don't bother building up the args in this case. */
4814 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4816 /* tsubst in the args corresponding to the template parameters,
4817 including auto if present. Most things will be unchanged, but
4818 make_typename_type and tsubst_qualified_id will resolve
4819 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4820 tree args
= current_template_args ();
4821 tree auto_node
= type_uses_auto (type
);
4825 tree auto_vec
= make_tree_vec (1);
4826 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4827 args
= add_to_template_args (args
, auto_vec
);
4829 pushed
= push_scope (scope
);
4830 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4835 if (type
== error_mark_node
)
4838 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4840 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4843 type
= TYPE_NAME (type
);
4848 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4849 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4850 the new template is a member template. */
4853 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4855 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4856 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4857 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4858 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4859 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4860 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4865 struct template_parm_data
4867 /* The level of the template parameters we are currently
4871 /* The index of the specialization argument we are currently
4875 /* An array whose size is the number of template parameters. The
4876 elements are nonzero if the parameter has been used in any one
4877 of the arguments processed so far. */
4880 /* An array whose size is the number of template arguments. The
4881 elements are nonzero if the argument makes use of template
4882 parameters of this level. */
4883 int* arg_uses_template_parms
;
4886 /* Subroutine of push_template_decl used to see if each template
4887 parameter in a partial specialization is used in the explicit
4888 argument list. If T is of the LEVEL given in DATA (which is
4889 treated as a template_parm_data*), then DATA->PARMS is marked
4893 mark_template_parm (tree t
, void* data
)
4897 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4899 template_parm_level_and_index (t
, &level
, &idx
);
4901 if (level
== tpd
->level
)
4903 tpd
->parms
[idx
] = 1;
4904 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4907 /* In C++17 the type of a non-type argument is a deduced context. */
4908 if (cxx_dialect
>= cxx17
4909 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4910 for_each_template_parm (TREE_TYPE (t
),
4911 &mark_template_parm
,
4914 /*include_nondeduced_p=*/false);
4916 /* Return zero so that for_each_template_parm will continue the
4917 traversal of the tree; we want to mark *every* template parm. */
4921 /* Process the partial specialization DECL. */
4924 process_partial_specialization (tree decl
)
4926 tree type
= TREE_TYPE (decl
);
4927 tree tinfo
= get_template_info (decl
);
4928 tree maintmpl
= TI_TEMPLATE (tinfo
);
4929 tree specargs
= TI_ARGS (tinfo
);
4930 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4931 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4934 int nargs
= TREE_VEC_LENGTH (inner_args
);
4937 bool did_error_intro
= false;
4938 struct template_parm_data tpd
;
4939 struct template_parm_data tpd2
;
4941 gcc_assert (current_template_parms
);
4943 /* A concept cannot be specialized. */
4944 if (flag_concepts
&& variable_concept_p (maintmpl
))
4946 error ("specialization of variable concept %q#D", maintmpl
);
4947 return error_mark_node
;
4950 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4951 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4953 /* We check that each of the template parameters given in the
4954 partial specialization is used in the argument list to the
4955 specialization. For example:
4957 template <class T> struct S;
4958 template <class T> struct S<T*>;
4960 The second declaration is OK because `T*' uses the template
4961 parameter T, whereas
4963 template <class T> struct S<int>;
4965 is no good. Even trickier is:
4976 The S2<T> declaration is actually invalid; it is a
4977 full-specialization. Of course,
4980 struct S2<T (*)(U)>;
4982 or some such would have been OK. */
4983 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4984 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4985 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4987 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4988 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4989 for (i
= 0; i
< nargs
; ++i
)
4991 tpd
.current_arg
= i
;
4992 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4993 &mark_template_parm
,
4996 /*include_nondeduced_p=*/false);
4998 for (i
= 0; i
< ntparms
; ++i
)
4999 if (tpd
.parms
[i
] == 0)
5001 /* One of the template parms was not used in a deduced context in the
5003 if (!did_error_intro
)
5005 error ("template parameters not deducible in "
5006 "partial specialization:");
5007 did_error_intro
= true;
5010 inform (input_location
, " %qD",
5011 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5014 if (did_error_intro
)
5015 return error_mark_node
;
5017 /* [temp.class.spec]
5019 The argument list of the specialization shall not be identical to
5020 the implicit argument list of the primary template. */
5022 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5023 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5025 || !strictly_subsumes (current_template_constraints (),
5026 inner_args
, maintmpl
)))
5029 error ("partial specialization %q+D does not specialize "
5030 "any template arguments; to define the primary template, "
5031 "remove the template argument list", decl
);
5033 error ("partial specialization %q+D does not specialize any "
5034 "template arguments and is not more constrained than "
5035 "the primary template; to define the primary template, "
5036 "remove the template argument list", decl
);
5037 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5040 /* A partial specialization that replaces multiple parameters of the
5041 primary template with a pack expansion is less specialized for those
5043 if (nargs
< DECL_NTPARMS (maintmpl
))
5045 error ("partial specialization is not more specialized than the "
5046 "primary template because it replaces multiple parameters "
5047 "with a pack expansion");
5048 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5049 /* Avoid crash in process_partial_specialization. */
5053 /* If we aren't in a dependent class, we can actually try deduction. */
5054 else if (tpd
.level
== 1
5055 /* FIXME we should be able to handle a partial specialization of a
5056 partial instantiation, but currently we can't (c++/41727). */
5057 && TMPL_ARGS_DEPTH (specargs
) == 1
5058 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5060 auto_diagnostic_group d
;
5061 if (permerror (input_location
, "partial specialization %qD is not "
5062 "more specialized than", decl
))
5063 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5067 /* [temp.class.spec]
5069 A partially specialized non-type argument expression shall not
5070 involve template parameters of the partial specialization except
5071 when the argument expression is a simple identifier.
5073 The type of a template parameter corresponding to a specialized
5074 non-type argument shall not be dependent on a parameter of the
5077 Also, we verify that pack expansions only occur at the
5078 end of the argument list. */
5079 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
5081 for (i
= 0; i
< nargs
; ++i
)
5083 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5084 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5085 tree packed_args
= NULL_TREE
;
5088 if (ARGUMENT_PACK_P (arg
))
5090 /* Extract the arguments from the argument pack. We'll be
5091 iterating over these in the following loop. */
5092 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5093 len
= TREE_VEC_LENGTH (packed_args
);
5096 for (j
= 0; j
< len
; j
++)
5099 /* Get the Jth argument in the parameter pack. */
5100 arg
= TREE_VEC_ELT (packed_args
, j
);
5102 if (PACK_EXPANSION_P (arg
))
5104 /* Pack expansions must come at the end of the
5106 if ((packed_args
&& j
< len
- 1)
5107 || (!packed_args
&& i
< nargs
- 1))
5109 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5110 error ("parameter pack argument %qE must be at the "
5111 "end of the template argument list", arg
);
5113 error ("parameter pack argument %qT must be at the "
5114 "end of the template argument list", arg
);
5118 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5119 /* We only care about the pattern. */
5120 arg
= PACK_EXPANSION_PATTERN (arg
);
5122 if (/* These first two lines are the `non-type' bit. */
5124 && TREE_CODE (arg
) != TEMPLATE_DECL
5125 /* This next two lines are the `argument expression is not just a
5126 simple identifier' condition and also the `specialized
5127 non-type argument' bit. */
5128 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5129 && !((REFERENCE_REF_P (arg
)
5130 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5131 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5133 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5134 || (packed_args
&& uses_template_parms (arg
)))
5135 error_at (cp_expr_loc_or_input_loc (arg
),
5136 "template argument %qE involves template "
5137 "parameter(s)", arg
);
5140 /* Look at the corresponding template parameter,
5141 marking which template parameters its type depends
5143 tree type
= TREE_TYPE (parm
);
5147 /* We haven't yet initialized TPD2. Do so now. */
5148 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5149 /* The number of parameters here is the number in the
5150 main template, which, as checked in the assertion
5152 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5154 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5157 /* Mark the template parameters. But this time, we're
5158 looking for the template parameters of the main
5159 template, not in the specialization. */
5160 tpd2
.current_arg
= i
;
5161 tpd2
.arg_uses_template_parms
[i
] = 0;
5162 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5163 for_each_template_parm (type
,
5164 &mark_template_parm
,
5167 /*include_nondeduced_p=*/false);
5169 if (tpd2
.arg_uses_template_parms
[i
])
5171 /* The type depended on some template parameters.
5172 If they are fully specialized in the
5173 specialization, that's OK. */
5176 for (j
= 0; j
< nargs
; ++j
)
5177 if (tpd2
.parms
[j
] != 0
5178 && tpd
.arg_uses_template_parms
[j
])
5181 error_n (input_location
, count
,
5182 "type %qT of template argument %qE depends "
5183 "on a template parameter",
5184 "type %qT of template argument %qE depends "
5185 "on template parameters",
5194 /* We should only get here once. */
5195 if (TREE_CODE (decl
) == TYPE_DECL
)
5196 gcc_assert (!COMPLETE_TYPE_P (type
));
5198 // Build the template decl.
5199 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5200 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5201 TREE_TYPE (tmpl
) = type
;
5202 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5203 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5204 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5205 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5207 /* Give template template parms a DECL_CONTEXT of the template
5208 for which they are a parameter. */
5209 for (i
= 0; i
< ntparms
; ++i
)
5211 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5212 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5213 DECL_CONTEXT (parm
) = tmpl
;
5217 /* We didn't register this in check_explicit_specialization so we could
5218 wait until the constraints were set. */
5219 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5221 associate_classtype_constraints (type
);
5223 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5224 = tree_cons (specargs
, tmpl
,
5225 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5226 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5228 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5229 inst
= TREE_CHAIN (inst
))
5231 tree instance
= TREE_VALUE (inst
);
5232 if (TYPE_P (instance
)
5233 ? (COMPLETE_TYPE_P (instance
)
5234 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5235 : DECL_TEMPLATE_INSTANTIATION (instance
))
5237 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5238 tree inst_decl
= (DECL_P (instance
)
5239 ? instance
: TYPE_NAME (instance
));
5242 else if (spec
== error_mark_node
)
5243 permerror (input_location
,
5244 "declaration of %qD ambiguates earlier template "
5245 "instantiation for %qD", decl
, inst_decl
);
5246 else if (TREE_VALUE (spec
) == tmpl
)
5247 permerror (input_location
,
5248 "partial specialization of %qD after instantiation "
5249 "of %qD", decl
, inst_decl
);
5256 /* PARM is a template parameter of some form; return the corresponding
5257 TEMPLATE_PARM_INDEX. */
5260 get_template_parm_index (tree parm
)
5262 if (TREE_CODE (parm
) == PARM_DECL
5263 || TREE_CODE (parm
) == CONST_DECL
)
5264 parm
= DECL_INITIAL (parm
);
5265 else if (TREE_CODE (parm
) == TYPE_DECL
5266 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5267 parm
= TREE_TYPE (parm
);
5268 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5269 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5270 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5271 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5272 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5276 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5277 parameter packs used by the template parameter PARM. */
5280 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5282 /* A type parm can't refer to another parm. */
5283 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5285 else if (TREE_CODE (parm
) == PARM_DECL
)
5287 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5292 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5294 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5295 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5297 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5298 if (template_parameter_pack_p (p
))
5299 /* Any packs in the type are expanded by this parameter. */;
5301 fixed_parameter_pack_p_1 (p
, ppd
);
5305 /* PARM is a template parameter pack. Return any parameter packs used in
5306 its type or the type of any of its template parameters. If there are
5307 any such packs, it will be instantiated into a fixed template parameter
5308 list by partial instantiation rather than be fully deduced. */
5311 fixed_parameter_pack_p (tree parm
)
5313 /* This can only be true in a member template. */
5314 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5316 /* This can only be true for a parameter pack. */
5317 if (!template_parameter_pack_p (parm
))
5319 /* A type parm can't refer to another parm. */
5320 if (TREE_CODE (parm
) == TYPE_DECL
)
5323 tree parameter_packs
= NULL_TREE
;
5324 struct find_parameter_pack_data ppd
;
5325 ppd
.parameter_packs
= ¶meter_packs
;
5326 ppd
.visited
= new hash_set
<tree
>;
5327 ppd
.type_pack_expansion_p
= false;
5329 fixed_parameter_pack_p_1 (parm
, &ppd
);
5332 return parameter_packs
;
5335 /* Check that a template declaration's use of default arguments and
5336 parameter packs is not invalid. Here, PARMS are the template
5337 parameters. IS_PRIMARY is true if DECL is the thing declared by
5338 a primary template. IS_PARTIAL is true if DECL is a partial
5341 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5342 function template declaration or a friend class template
5343 declaration. In the function case, 1 indicates a declaration, 2
5344 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5345 emitted for extraneous default arguments.
5347 Returns TRUE if there were no errors found, FALSE otherwise. */
5350 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5351 bool is_partial
, int is_friend_decl
)
5354 int last_level_to_check
;
5356 bool no_errors
= true;
5360 A default template-argument shall not be specified in a
5361 function template declaration or a function template definition, nor
5362 in the template-parameter-list of the definition of a member of a
5365 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5366 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5367 /* You can't have a function template declaration in a local
5368 scope, nor you can you define a member of a class template in a
5372 if ((TREE_CODE (decl
) == TYPE_DECL
5374 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5375 || (TREE_CODE (decl
) == FUNCTION_DECL
5376 && LAMBDA_FUNCTION_P (decl
)))
5377 /* A lambda doesn't have an explicit declaration; don't complain
5378 about the parms of the enclosing class. */
5381 if (current_class_type
5382 && !TYPE_BEING_DEFINED (current_class_type
)
5383 && DECL_LANG_SPECIFIC (decl
)
5384 && DECL_DECLARES_FUNCTION_P (decl
)
5385 /* If this is either a friend defined in the scope of the class
5386 or a member function. */
5387 && (DECL_FUNCTION_MEMBER_P (decl
)
5388 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5389 : DECL_FRIEND_CONTEXT (decl
)
5390 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5392 /* And, if it was a member function, it really was defined in
5393 the scope of the class. */
5394 && (!DECL_FUNCTION_MEMBER_P (decl
)
5395 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5396 /* We already checked these parameters when the template was
5397 declared, so there's no need to do it again now. This function
5398 was defined in class scope, but we're processing its body now
5399 that the class is complete. */
5402 /* Core issue 226 (C++0x only): the following only applies to class
5405 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5409 If a template-parameter has a default template-argument, all
5410 subsequent template-parameters shall have a default
5411 template-argument supplied. */
5412 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5414 tree inner_parms
= TREE_VALUE (parm_level
);
5415 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5416 int seen_def_arg_p
= 0;
5419 for (i
= 0; i
< ntparms
; ++i
)
5421 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5423 if (parm
== error_mark_node
)
5426 if (TREE_PURPOSE (parm
))
5428 else if (seen_def_arg_p
5429 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5431 error ("no default argument for %qD", TREE_VALUE (parm
));
5432 /* For better subsequent error-recovery, we indicate that
5433 there should have been a default argument. */
5434 TREE_PURPOSE (parm
) = error_mark_node
;
5437 else if (!is_partial
5439 /* Don't complain about an enclosing partial
5441 && parm_level
== parms
5442 && TREE_CODE (decl
) == TYPE_DECL
5444 && template_parameter_pack_p (TREE_VALUE (parm
))
5445 /* A fixed parameter pack will be partially
5446 instantiated into a fixed length list. */
5447 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5449 /* A primary class template can only have one
5450 parameter pack, at the end of the template
5453 error ("parameter pack %q+D must be at the end of the"
5454 " template parameter list", TREE_VALUE (parm
));
5456 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5464 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5468 /* For an ordinary class template, default template arguments are
5469 allowed at the innermost level, e.g.:
5470 template <class T = int>
5472 but, in a partial specialization, they're not allowed even
5473 there, as we have in [temp.class.spec]:
5475 The template parameter list of a specialization shall not
5476 contain default template argument values.
5478 So, for a partial specialization, or for a function template
5479 (in C++98/C++03), we look at all of them. */
5482 /* But, for a primary class template that is not a partial
5483 specialization we look at all template parameters except the
5485 parms
= TREE_CHAIN (parms
);
5487 /* Figure out what error message to issue. */
5488 if (is_friend_decl
== 2)
5489 msg
= G_("default template arguments may not be used in function template "
5490 "friend re-declaration");
5491 else if (is_friend_decl
)
5492 msg
= G_("default template arguments may not be used in template "
5493 "friend declarations");
5494 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5495 msg
= G_("default template arguments may not be used in function templates "
5496 "without %<-std=c++11%> or %<-std=gnu++11%>");
5497 else if (is_partial
)
5498 msg
= G_("default template arguments may not be used in "
5499 "partial specializations");
5500 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5501 msg
= G_("default argument for template parameter for class enclosing %qD");
5503 /* Per [temp.param]/9, "A default template-argument shall not be
5504 specified in the template-parameter-lists of the definition of
5505 a member of a class template that appears outside of the member's
5506 class.", thus if we aren't handling a member of a class template
5507 there is no need to examine the parameters. */
5510 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5511 /* If we're inside a class definition, there's no need to
5512 examine the parameters to the class itself. On the one
5513 hand, they will be checked when the class is defined, and,
5514 on the other, default arguments are valid in things like:
5515 template <class T = double>
5516 struct S { template <class U> void f(U); };
5517 Here the default argument for `S' has no bearing on the
5518 declaration of `f'. */
5519 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5521 /* Check everything. */
5522 last_level_to_check
= 0;
5524 for (parm_level
= parms
;
5525 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5526 parm_level
= TREE_CHAIN (parm_level
))
5528 tree inner_parms
= TREE_VALUE (parm_level
);
5532 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5533 for (i
= 0; i
< ntparms
; ++i
)
5535 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5538 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5543 if (is_friend_decl
== 2)
5550 /* Clear out the default argument so that we are not
5552 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5556 /* At this point, if we're still interested in issuing messages,
5557 they must apply to classes surrounding the object declared. */
5559 msg
= G_("default argument for template parameter for class "
5566 /* Worker for push_template_decl_real, called via
5567 for_each_template_parm. DATA is really an int, indicating the
5568 level of the parameters we are interested in. If T is a template
5569 parameter of that level, return nonzero. */
5572 template_parm_this_level_p (tree t
, void* data
)
5574 int this_level
= *(int *)data
;
5577 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5578 level
= TEMPLATE_PARM_LEVEL (t
);
5580 level
= TEMPLATE_TYPE_LEVEL (t
);
5581 return level
== this_level
;
5584 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5585 DATA is really an int, indicating the innermost outer level of parameters.
5586 If T is a template parameter of that level or further out, return
5590 template_parm_outer_level (tree t
, void *data
)
5592 int this_level
= *(int *)data
;
5595 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5596 level
= TEMPLATE_PARM_LEVEL (t
);
5598 level
= TEMPLATE_TYPE_LEVEL (t
);
5599 return level
<= this_level
;
5602 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5603 parameters given by current_template_args, or reuses a
5604 previously existing one, if appropriate. Returns the DECL, or an
5605 equivalent one, if it is replaced via a call to duplicate_decls.
5607 If IS_FRIEND is true, DECL is a friend declaration. */
5610 push_template_decl_real (tree decl
, bool is_friend
)
5618 int new_template_p
= 0;
5619 /* True if the template is a member template, in the sense of
5621 bool member_template_p
= false;
5623 if (decl
== error_mark_node
|| !current_template_parms
)
5624 return error_mark_node
;
5626 /* See if this is a partial specialization. */
5627 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5628 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5629 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5631 && DECL_LANG_SPECIFIC (decl
)
5632 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5633 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5635 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5639 /* For a friend, we want the context of the friend, not
5640 the type of which it is a friend. */
5641 ctx
= CP_DECL_CONTEXT (decl
);
5642 else if (CP_DECL_CONTEXT (decl
)
5643 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5644 /* In the case of a virtual function, we want the class in which
5646 ctx
= CP_DECL_CONTEXT (decl
);
5648 /* Otherwise, if we're currently defining some class, the DECL
5649 is assumed to be a member of the class. */
5650 ctx
= current_scope ();
5652 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5655 if (!DECL_CONTEXT (decl
))
5656 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5658 /* See if this is a primary template. */
5659 if (is_friend
&& ctx
5660 && uses_template_parms_level (ctx
, processing_template_decl
))
5661 /* A friend template that specifies a class context, i.e.
5662 template <typename T> friend void A<T>::f();
5665 else if (TREE_CODE (decl
) == TYPE_DECL
5666 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5669 is_primary
= template_parm_scope_p ();
5673 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5675 if (DECL_CLASS_SCOPE_P (decl
))
5676 member_template_p
= true;
5677 if (TREE_CODE (decl
) == TYPE_DECL
5678 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5680 error ("template class without a name");
5681 return error_mark_node
;
5683 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5685 if (member_template_p
)
5687 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5688 error ("member template %qD may not have virt-specifiers", decl
);
5690 if (DECL_DESTRUCTOR_P (decl
))
5694 A destructor shall not be a member template. */
5695 error_at (DECL_SOURCE_LOCATION (decl
),
5696 "destructor %qD declared as member template", decl
);
5697 return error_mark_node
;
5699 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5700 && (!prototype_p (TREE_TYPE (decl
))
5701 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5702 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5703 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5704 == void_list_node
)))
5706 /* [basic.stc.dynamic.allocation]
5708 An allocation function can be a function
5709 template. ... Template allocation functions shall
5710 have two or more parameters. */
5711 error ("invalid template declaration of %qD", decl
);
5712 return error_mark_node
;
5715 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5716 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5718 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5719 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5720 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5722 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5723 if (TREE_CODE (t
) == TYPE_DECL
)
5725 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5726 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5729 else if (TREE_CODE (decl
) == TYPE_DECL
5730 && TYPE_DECL_ALIAS_P (decl
))
5731 /* alias-declaration */
5732 gcc_assert (!DECL_ARTIFICIAL (decl
));
5733 else if (VAR_P (decl
))
5734 /* C++14 variable template. */;
5735 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5736 /* C++2a concept definitions. */;
5739 error ("template declaration of %q#D", decl
);
5740 return error_mark_node
;
5744 /* Check to see that the rules regarding the use of default
5745 arguments are not being violated. We check args for a friend
5746 functions when we know whether it's a definition, introducing
5747 declaration or re-declaration. */
5748 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5749 check_default_tmpl_args (decl
, current_template_parms
,
5750 is_primary
, is_partial
, is_friend
);
5752 /* Ensure that there are no parameter packs in the type of this
5753 declaration that have not been expanded. */
5754 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5756 /* Check each of the arguments individually to see if there are
5757 any bare parameter packs. */
5758 tree type
= TREE_TYPE (decl
);
5759 tree arg
= DECL_ARGUMENTS (decl
);
5760 tree argtype
= TYPE_ARG_TYPES (type
);
5762 while (arg
&& argtype
)
5764 if (!DECL_PACK_P (arg
)
5765 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5767 /* This is a PARM_DECL that contains unexpanded parameter
5768 packs. We have already complained about this in the
5769 check_for_bare_parameter_packs call, so just replace
5770 these types with ERROR_MARK_NODE. */
5771 TREE_TYPE (arg
) = error_mark_node
;
5772 TREE_VALUE (argtype
) = error_mark_node
;
5775 arg
= DECL_CHAIN (arg
);
5776 argtype
= TREE_CHAIN (argtype
);
5779 /* Check for bare parameter packs in the return type and the
5780 exception specifiers. */
5781 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5782 /* Errors were already issued, set return type to int
5783 as the frontend doesn't expect error_mark_node as
5785 TREE_TYPE (type
) = integer_type_node
;
5786 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5787 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5789 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5790 && TYPE_DECL_ALIAS_P (decl
))
5791 ? DECL_ORIGINAL_TYPE (decl
)
5792 : TREE_TYPE (decl
)))
5794 TREE_TYPE (decl
) = error_mark_node
;
5795 return error_mark_node
;
5799 return process_partial_specialization (decl
);
5801 args
= current_template_args ();
5804 || TREE_CODE (ctx
) == FUNCTION_DECL
5805 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5806 || (TREE_CODE (decl
) == TYPE_DECL
5807 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5808 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5810 if (DECL_LANG_SPECIFIC (decl
)
5811 && DECL_TEMPLATE_INFO (decl
)
5812 && DECL_TI_TEMPLATE (decl
))
5813 tmpl
= DECL_TI_TEMPLATE (decl
);
5814 /* If DECL is a TYPE_DECL for a class-template, then there won't
5815 be DECL_LANG_SPECIFIC. The information equivalent to
5816 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5817 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5818 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5819 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5821 /* Since a template declaration already existed for this
5822 class-type, we must be redeclaring it here. Make sure
5823 that the redeclaration is valid. */
5824 redeclare_class_template (TREE_TYPE (decl
),
5825 current_template_parms
,
5826 current_template_constraints ());
5827 /* We don't need to create a new TEMPLATE_DECL; just use the
5828 one we already had. */
5829 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5833 tmpl
= build_template_decl (decl
, current_template_parms
,
5837 if (DECL_LANG_SPECIFIC (decl
)
5838 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5840 /* A specialization of a member template of a template
5842 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5843 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5844 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5850 tree a
, t
, current
, parms
;
5852 tree tinfo
= get_template_info (decl
);
5856 error ("template definition of non-template %q#D", decl
);
5857 return error_mark_node
;
5860 tmpl
= TI_TEMPLATE (tinfo
);
5862 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5863 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5864 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5865 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5869 /* The declaration is a specialization of a member
5870 template, declared outside the class. Therefore, the
5871 innermost template arguments will be NULL, so we
5872 replace them with the arguments determined by the
5873 earlier call to check_explicit_specialization. */
5874 args
= DECL_TI_ARGS (decl
);
5877 = build_template_decl (decl
, current_template_parms
,
5879 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5880 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5881 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5882 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5883 DECL_TEMPLATE_INFO (new_tmpl
)
5884 = build_template_info (tmpl
, args
);
5886 register_specialization (new_tmpl
,
5887 most_general_template (tmpl
),
5893 /* Make sure the template headers we got make sense. */
5895 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5896 i
= TMPL_PARMS_DEPTH (parms
);
5897 if (TMPL_ARGS_DEPTH (args
) != i
)
5899 error ("expected %d levels of template parms for %q#D, got %d",
5900 i
, decl
, TMPL_ARGS_DEPTH (args
));
5901 DECL_INTERFACE_KNOWN (decl
) = 1;
5902 return error_mark_node
;
5905 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5907 a
= TMPL_ARGS_LEVEL (args
, i
);
5908 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5910 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5912 if (current
== decl
)
5913 error ("got %d template parameters for %q#D",
5914 TREE_VEC_LENGTH (a
), decl
);
5916 error ("got %d template parameters for %q#T",
5917 TREE_VEC_LENGTH (a
), current
);
5918 error (" but %d required", TREE_VEC_LENGTH (t
));
5919 /* Avoid crash in import_export_decl. */
5920 DECL_INTERFACE_KNOWN (decl
) = 1;
5921 return error_mark_node
;
5924 if (current
== decl
)
5926 else if (current
== NULL_TREE
)
5927 /* Can happen in erroneous input. */
5930 current
= get_containing_scope (current
);
5933 /* Check that the parms are used in the appropriate qualifying scopes
5934 in the declarator. */
5935 if (!comp_template_args
5937 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5939 error ("template arguments to %qD do not match original "
5940 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5941 if (!uses_template_parms (TI_ARGS (tinfo
)))
5942 inform (input_location
, "use %<template<>%> for"
5943 " an explicit specialization");
5944 /* Avoid crash in import_export_decl. */
5945 DECL_INTERFACE_KNOWN (decl
) = 1;
5946 return error_mark_node
;
5950 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5951 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5953 /* Push template declarations for global functions and types. Note
5954 that we do not try to push a global template friend declared in a
5955 template class; such a thing may well depend on the template
5956 parameters of the class. */
5957 if (new_template_p
&& !ctx
5958 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5960 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5961 if (tmpl
== error_mark_node
)
5962 return error_mark_node
;
5964 /* Hide template friend classes that haven't been declared yet. */
5965 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5967 DECL_ANTICIPATED (tmpl
) = 1;
5968 DECL_FRIEND_P (tmpl
) = 1;
5974 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5976 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5978 /* Give template template parms a DECL_CONTEXT of the template
5979 for which they are a parameter. */
5980 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5981 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5983 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5984 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5985 DECL_CONTEXT (parm
) = tmpl
;
5988 if (TREE_CODE (decl
) == TYPE_DECL
5989 && TYPE_DECL_ALIAS_P (decl
)
5990 && complex_alias_template_p (tmpl
))
5991 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5994 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5995 back to its most general template. If TMPL is a specialization,
5996 ARGS may only have the innermost set of arguments. Add the missing
5997 argument levels if necessary. */
5998 if (DECL_TEMPLATE_INFO (tmpl
))
5999 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6001 info
= build_template_info (tmpl
, args
);
6003 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6004 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6008 retrofit_lang_decl (decl
);
6009 if (DECL_LANG_SPECIFIC (decl
))
6010 DECL_TEMPLATE_INFO (decl
) = info
;
6013 if (flag_implicit_templates
6015 && TREE_PUBLIC (decl
)
6016 && VAR_OR_FUNCTION_DECL_P (decl
))
6017 /* Set DECL_COMDAT on template instantiations; if we force
6018 them to be emitted by explicit instantiation,
6019 mark_needed will tell cgraph to do the right thing. */
6020 DECL_COMDAT (decl
) = true;
6022 return DECL_TEMPLATE_RESULT (tmpl
);
6026 push_template_decl (tree decl
)
6028 return push_template_decl_real (decl
, false);
6031 /* FN is an inheriting constructor that inherits from the constructor
6032 template INHERITED; turn FN into a constructor template with a matching
6036 add_inherited_template_parms (tree fn
, tree inherited
)
6039 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6040 inner_parms
= copy_node (inner_parms
);
6042 = tree_cons (size_int (processing_template_decl
+ 1),
6043 inner_parms
, current_template_parms
);
6044 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6045 tree args
= template_parms_to_args (parms
);
6046 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6047 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
6048 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
6049 DECL_ARTIFICIAL (tmpl
) = true;
6050 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6054 /* Called when a class template TYPE is redeclared with the indicated
6055 template PARMS, e.g.:
6057 template <class T> struct S;
6058 template <class T> struct S {}; */
6061 redeclare_class_template (tree type
, tree parms
, tree cons
)
6067 if (!TYPE_TEMPLATE_INFO (type
))
6069 error ("%qT is not a template type", type
);
6073 tmpl
= TYPE_TI_TEMPLATE (type
);
6074 if (!PRIMARY_TEMPLATE_P (tmpl
))
6075 /* The type is nested in some template class. Nothing to worry
6076 about here; there are no new template parameters for the nested
6082 error ("template specifiers not specified in declaration of %qD",
6087 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6088 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6090 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6092 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6093 "redeclared with %d template parameter",
6094 "redeclared with %d template parameters",
6095 TREE_VEC_LENGTH (parms
));
6096 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6097 "previous declaration %qD used %d template parameter",
6098 "previous declaration %qD used %d template parameters",
6099 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6103 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6110 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6111 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6114 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6115 if (error_operand_p (tmpl_parm
))
6118 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6119 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6120 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6122 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6124 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6125 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6126 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6127 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6128 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6129 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6130 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6131 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6132 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6134 error ("template parameter %q+#D", tmpl_parm
);
6135 error ("redeclared here as %q#D", parm
);
6139 /* The parameters can be declared to introduce different
6141 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6142 tree p2
= TREE_VEC_ELT (parms
, i
);
6143 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6145 error ("declaration of template parameter %q+#D with different "
6146 "constraints", parm
);
6147 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6148 "original declaration appeared here");
6152 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6154 /* We have in [temp.param]:
6156 A template-parameter may not be given default arguments
6157 by two different declarations in the same scope. */
6158 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6159 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6160 "original definition appeared here");
6164 if (parm_default
!= NULL_TREE
)
6165 /* Update the previous template parameters (which are the ones
6166 that will really count) with the new default value. */
6167 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6168 else if (tmpl_default
!= NULL_TREE
)
6169 /* Update the new parameters, too; they'll be used as the
6170 parameters for any members. */
6171 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6173 /* Give each template template parm in this redeclaration a
6174 DECL_CONTEXT of the template for which they are a parameter. */
6175 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6177 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6178 DECL_CONTEXT (parm
) = tmpl
;
6181 if (TREE_CODE (parm
) == TYPE_DECL
)
6182 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6185 tree ci
= get_constraints (tmpl
);
6186 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6187 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6189 /* Two classes with different constraints declare different entities. */
6190 if (!cp_tree_equal (req1
, req2
))
6192 error_at (input_location
, "redeclaration %q#D with different "
6193 "constraints", tmpl
);
6194 inform (DECL_SOURCE_LOCATION (tmpl
),
6195 "original declaration appeared here");
6202 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6203 to be used when the caller has already checked
6204 (processing_template_decl
6205 && !instantiation_dependent_expression_p (expr)
6206 && potential_constant_expression (expr))
6207 and cleared processing_template_decl. */
6210 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6212 return tsubst_copy_and_build (expr
,
6215 /*in_decl=*/NULL_TREE
,
6216 /*function_p=*/false,
6217 /*integral_constant_expression_p=*/true);
6220 /* Simplify EXPR if it is a non-dependent expression. Returns the
6221 (possibly simplified) expression. */
6224 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6226 if (expr
== NULL_TREE
)
6229 /* If we're in a template, but EXPR isn't value dependent, simplify
6230 it. We're supposed to treat:
6232 template <typename T> void f(T[1 + 1]);
6233 template <typename T> void f(T[2]);
6235 as two declarations of the same function, for example. */
6236 if (processing_template_decl
6237 && is_nondependent_constant_expression (expr
))
6239 processing_template_decl_sentinel s
;
6240 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6246 instantiate_non_dependent_expr (tree expr
)
6248 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6251 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6252 an uninstantiated expression. */
6255 instantiate_non_dependent_or_null (tree expr
)
6257 if (expr
== NULL_TREE
)
6259 if (processing_template_decl
)
6261 if (!is_nondependent_constant_expression (expr
))
6265 processing_template_decl_sentinel s
;
6266 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6272 /* True iff T is a specialization of a variable template. */
6275 variable_template_specialization_p (tree t
)
6277 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6279 tree tmpl
= DECL_TI_TEMPLATE (t
);
6280 return variable_template_p (tmpl
);
6283 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6284 template declaration, or a TYPE_DECL for an alias declaration. */
6287 alias_type_or_template_p (tree t
)
6291 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6294 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6295 || DECL_ALIAS_TEMPLATE_P (t
));
6298 /* Return TRUE iff T is a specialization of an alias template. */
6301 alias_template_specialization_p (const_tree t
)
6303 /* It's an alias template specialization if it's an alias and its
6304 TYPE_NAME is a specialization of a primary template. */
6305 if (TYPE_ALIAS_P (t
))
6306 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6307 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6312 /* An alias template is complex from a SFINAE perspective if a template-id
6313 using that alias can be ill-formed when the expansion is not, as with
6314 the void_t template. We determine this by checking whether the
6315 expansion for the alias template uses all its template parameters. */
6317 struct uses_all_template_parms_data
6324 uses_all_template_parms_r (tree t
, void *data_
)
6326 struct uses_all_template_parms_data
&data
6327 = *(struct uses_all_template_parms_data
*)data_
;
6328 tree idx
= get_template_parm_index (t
);
6330 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6331 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6336 complex_alias_template_p (const_tree tmpl
)
6338 struct uses_all_template_parms_data data
;
6339 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6340 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6341 data
.level
= TMPL_PARMS_DEPTH (parms
);
6342 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6343 data
.seen
= XALLOCAVEC (bool, len
);
6344 for (int i
= 0; i
< len
; ++i
)
6345 data
.seen
[i
] = false;
6347 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6348 for (int i
= 0; i
< len
; ++i
)
6354 /* Return TRUE iff T is a specialization of a complex alias template with
6355 dependent template-arguments. */
6358 dependent_alias_template_spec_p (const_tree t
)
6360 if (!alias_template_specialization_p (t
))
6363 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6364 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6367 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6368 if (!any_dependent_template_arguments_p (args
))
6374 /* Return the number of innermost template parameters in TMPL. */
6377 num_innermost_template_parms (tree tmpl
)
6379 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6380 return TREE_VEC_LENGTH (parms
);
6383 /* Return either TMPL or another template that it is equivalent to under DR
6384 1286: An alias that just changes the name of a template is equivalent to
6385 the other template. */
6388 get_underlying_template (tree tmpl
)
6390 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6391 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6393 /* Determine if the alias is equivalent to an underlying template. */
6394 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6395 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6399 tree underlying
= TI_TEMPLATE (tinfo
);
6400 if (!PRIMARY_TEMPLATE_P (underlying
)
6401 || (num_innermost_template_parms (tmpl
)
6402 != num_innermost_template_parms (underlying
)))
6405 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6406 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6409 /* Alias is equivalent. Strip it and repeat. */
6416 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6417 must be a reference-to-function or a pointer-to-function type, as specified
6418 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6419 and check that the resulting function has external linkage. */
6422 convert_nontype_argument_function (tree type
, tree expr
,
6423 tsubst_flags_t complain
)
6427 linkage_kind linkage
;
6429 fn
= instantiate_type (type
, fns
, tf_none
);
6430 if (fn
== error_mark_node
)
6431 return error_mark_node
;
6433 if (value_dependent_expression_p (fn
))
6436 fn_no_ptr
= strip_fnptr_conv (fn
);
6437 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6438 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6439 if (BASELINK_P (fn_no_ptr
))
6440 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6442 /* [temp.arg.nontype]/1
6444 A template-argument for a non-type, non-template template-parameter
6447 -- the address of an object or function with external [C++11: or
6448 internal] linkage. */
6450 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6451 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6453 if (complain
& tf_error
)
6455 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6456 error_at (loc
, "%qE is not a valid template argument for type %qT",
6458 if (TYPE_PTR_P (type
))
6459 inform (loc
, "it must be the address of a function "
6460 "with external linkage");
6462 inform (loc
, "it must be the name of a function with "
6463 "external linkage");
6468 linkage
= decl_linkage (fn_no_ptr
);
6469 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6471 if (complain
& tf_error
)
6473 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6474 if (cxx_dialect
>= cxx11
)
6475 error_at (loc
, "%qE is not a valid template argument for type "
6476 "%qT because %qD has no linkage",
6477 expr
, type
, fn_no_ptr
);
6479 error_at (loc
, "%qE is not a valid template argument for type "
6480 "%qT because %qD does not have external linkage",
6481 expr
, type
, fn_no_ptr
);
6487 if (TYPE_REF_P (type
))
6489 if (REFERENCE_REF_P (fn
))
6490 fn
= TREE_OPERAND (fn
, 0);
6492 fn
= build_address (fn
);
6494 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6495 fn
= build_nop (type
, fn
);
6500 /* Subroutine of convert_nontype_argument.
6501 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6502 Emit an error otherwise. */
6505 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6506 tsubst_flags_t complain
)
6508 tree orig_expr
= expr
;
6510 if (null_ptr_cst_p (expr
))
6512 if (TREE_CODE (expr
) == PTRMEM_CST
6513 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6514 PTRMEM_CST_CLASS (expr
)))
6516 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6518 if (processing_template_decl
6519 && TREE_CODE (expr
) == ADDR_EXPR
6520 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6522 if (complain
& tf_error
)
6524 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6525 error_at (loc
, "%qE is not a valid template argument for type %qT",
6527 if (TREE_CODE (expr
) != PTRMEM_CST
)
6528 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6530 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6535 /* Returns TRUE iff the address of OP is value-dependent.
6537 14.6.2.4 [temp.dep.temp]:
6538 A non-integral non-type template-argument is dependent if its type is
6539 dependent or it has either of the following forms
6542 and contains a nested-name-specifier which specifies a class-name that
6543 names a dependent type.
6545 We generalize this to just say that the address of a member of a
6546 dependent class is value-dependent; the above doesn't cover the
6547 address of a static data member named with an unqualified-id. */
6550 has_value_dependent_address (tree op
)
6552 STRIP_ANY_LOCATION_WRAPPER (op
);
6554 /* We could use get_inner_reference here, but there's no need;
6555 this is only relevant for template non-type arguments, which
6556 can only be expressed as &id-expression. */
6559 tree ctx
= CP_DECL_CONTEXT (op
);
6560 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6567 /* The next set of functions are used for providing helpful explanatory
6568 diagnostics for failed overload resolution. Their messages should be
6569 indented by two spaces for consistency with the messages in
6573 unify_success (bool /*explain_p*/)
6578 /* Other failure functions should call this one, to provide a single function
6579 for setting a breakpoint on. */
6582 unify_invalid (bool /*explain_p*/)
6588 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6591 inform (input_location
,
6592 " couldn%'t deduce template parameter %qD", parm
);
6593 return unify_invalid (explain_p
);
6597 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6600 inform (input_location
,
6601 " types %qT and %qT have incompatible cv-qualifiers",
6603 return unify_invalid (explain_p
);
6607 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6610 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6611 return unify_invalid (explain_p
);
6615 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6618 inform (input_location
,
6619 " template parameter %qD is not a parameter pack, but "
6622 return unify_invalid (explain_p
);
6626 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6629 inform (input_location
,
6630 " template argument %qE does not match "
6631 "pointer-to-member constant %qE",
6633 return unify_invalid (explain_p
);
6637 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6640 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6641 return unify_invalid (explain_p
);
6645 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6648 inform (input_location
,
6649 " inconsistent parameter pack deduction with %qT and %qT",
6651 return unify_invalid (explain_p
);
6655 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6660 inform (input_location
,
6661 " deduced conflicting types for parameter %qT (%qT and %qT)",
6662 parm
, first
, second
);
6664 inform (input_location
,
6665 " deduced conflicting values for non-type parameter "
6666 "%qE (%qE and %qE)", parm
, first
, second
);
6668 return unify_invalid (explain_p
);
6672 unify_vla_arg (bool explain_p
, tree arg
)
6675 inform (input_location
,
6676 " variable-sized array type %qT is not "
6677 "a valid template argument",
6679 return unify_invalid (explain_p
);
6683 unify_method_type_error (bool explain_p
, tree arg
)
6686 inform (input_location
,
6687 " member function type %qT is not a valid template argument",
6689 return unify_invalid (explain_p
);
6693 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6698 inform_n (input_location
, wanted
,
6699 " candidate expects at least %d argument, %d provided",
6700 " candidate expects at least %d arguments, %d provided",
6703 inform_n (input_location
, wanted
,
6704 " candidate expects %d argument, %d provided",
6705 " candidate expects %d arguments, %d provided",
6708 return unify_invalid (explain_p
);
6712 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6714 return unify_arity (explain_p
, have
, wanted
);
6718 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6719 bool least_p
= false)
6721 return unify_arity (explain_p
, have
, wanted
, least_p
);
6725 unify_arg_conversion (bool explain_p
, tree to_type
,
6726 tree from_type
, tree arg
)
6729 inform (cp_expr_loc_or_input_loc (arg
),
6730 " cannot convert %qE (type %qT) to type %qT",
6731 arg
, from_type
, to_type
);
6732 return unify_invalid (explain_p
);
6736 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6737 tree parm
, tree arg
)
6742 case tbr_ambiguous_baseclass
:
6743 inform (input_location
, " %qT is an ambiguous base class of %qT",
6747 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6750 return unify_invalid (explain_p
);
6754 unify_inconsistent_template_template_parameters (bool explain_p
)
6757 inform (input_location
,
6758 " template parameters of a template template argument are "
6759 "inconsistent with other deduced template arguments");
6760 return unify_invalid (explain_p
);
6764 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6767 inform (input_location
,
6768 " cannot deduce a template for %qT from non-template type %qT",
6770 return unify_invalid (explain_p
);
6774 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6777 inform (input_location
,
6778 " template argument %qE does not match %qE", arg
, parm
);
6779 return unify_invalid (explain_p
);
6782 /* True if T is a C++20 template parameter object to store the argument for a
6783 template parameter of class type. */
6786 template_parm_object_p (const_tree t
)
6788 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6789 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6792 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6793 argument for TYPE, points to an unsuitable object. */
6796 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6798 switch (TREE_CODE (expr
))
6801 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6805 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6810 unsigned i
; tree elt
;
6811 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6812 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6819 tree decl
= TREE_OPERAND (expr
, 0);
6823 if (complain
& tf_error
)
6824 error_at (cp_expr_loc_or_input_loc (expr
),
6825 "%qE is not a valid template argument of type %qT "
6826 "because %qE is not a variable", expr
, type
, decl
);
6829 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6831 if (complain
& tf_error
)
6832 error_at (cp_expr_loc_or_input_loc (expr
),
6833 "%qE is not a valid template argument of type %qT "
6834 "in C++98 because %qD does not have external linkage",
6838 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6839 && decl_linkage (decl
) == lk_none
)
6841 if (complain
& tf_error
)
6842 error_at (cp_expr_loc_or_input_loc (expr
),
6843 "%qE is not a valid template argument of type %qT "
6844 "because %qD has no linkage", expr
, type
, decl
);
6847 /* C++17: For a non-type template-parameter of reference or pointer
6848 type, the value of the constant expression shall not refer to (or
6849 for a pointer type, shall not be the address of):
6850 * a subobject (4.5),
6851 * a temporary object (15.2),
6852 * a string literal (5.13.5),
6853 * the result of a typeid expression (8.2.8), or
6854 * a predefined __func__ variable (11.4.1). */
6855 else if (DECL_ARTIFICIAL (decl
))
6857 if (complain
& tf_error
)
6858 error ("the address of %qD is not a valid template argument",
6862 else if (!same_type_ignoring_top_level_qualifiers_p
6863 (strip_array_types (TREE_TYPE (type
)),
6864 strip_array_types (TREE_TYPE (decl
))))
6866 if (complain
& tf_error
)
6867 error ("the address of the %qT subobject of %qD is not a "
6868 "valid template argument", TREE_TYPE (type
), decl
);
6871 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6873 if (complain
& tf_error
)
6874 error ("the address of %qD is not a valid template argument "
6875 "because it does not have static storage duration",
6883 if (!INDIRECT_TYPE_P (type
))
6884 /* We're only concerned about pointers and references here. */;
6885 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6886 /* Null pointer values are OK in C++11. */;
6891 if (complain
& tf_error
)
6892 error ("%qD is not a valid template argument "
6893 "because %qD is a variable, not the address of "
6894 "a variable", expr
, expr
);
6899 if (complain
& tf_error
)
6900 error ("%qE is not a valid template argument for %qT "
6901 "because it is not the address of a variable",
6911 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6912 template argument EXPR. */
6915 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6917 if (TREE_CODE (expr
) == TARGET_EXPR
)
6918 expr
= TARGET_EXPR_INITIAL (expr
);
6920 if (!TREE_CONSTANT (expr
))
6922 if ((complain
& tf_error
)
6923 && require_rvalue_constant_expression (expr
))
6924 cxx_constant_value (expr
);
6925 return error_mark_node
;
6927 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6928 return error_mark_node
;
6930 tree name
= mangle_template_parm_object (expr
);
6931 tree decl
= get_global_binding (name
);
6935 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6936 decl
= create_temporary_var (type
);
6937 TREE_STATIC (decl
) = true;
6938 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6939 TREE_READONLY (decl
) = true;
6940 DECL_NAME (decl
) = name
;
6941 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6942 DECL_CONTEXT (decl
) = global_namespace
;
6943 comdat_linkage (decl
);
6944 pushdecl_top_level_and_finish (decl
, expr
);
6948 /* Attempt to convert the non-type template parameter EXPR to the
6949 indicated TYPE. If the conversion is successful, return the
6950 converted value. If the conversion is unsuccessful, return
6951 NULL_TREE if we issued an error message, or error_mark_node if we
6952 did not. We issue error messages for out-and-out bad template
6953 parameters, but not simply because the conversion failed, since we
6954 might be just trying to do argument deduction. Both TYPE and EXPR
6955 must be non-dependent.
6957 The conversion follows the special rules described in
6958 [temp.arg.nontype], and it is much more strict than an implicit
6961 This function is called twice for each template argument (see
6962 lookup_template_class for a more accurate description of this
6963 problem). This means that we need to handle expressions which
6964 are not valid in a C++ source, but can be created from the
6965 first call (for instance, casts to perform conversions). These
6966 hacks can go away after we fix the double coercion problem. */
6969 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6972 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6974 /* Detect immediately string literals as invalid non-type argument.
6975 This special-case is not needed for correctness (we would easily
6976 catch this later), but only to provide better diagnostic for this
6977 common user mistake. As suggested by DR 100, we do not mention
6978 linkage issues in the diagnostic as this is not the point. */
6979 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
6981 if (complain
& tf_error
)
6982 error ("%qE is not a valid template argument for type %qT "
6983 "because string literals can never be used in this context",
6988 /* Add the ADDR_EXPR now for the benefit of
6989 value_dependent_expression_p. */
6990 if (TYPE_PTROBV_P (type
)
6991 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6993 expr
= decay_conversion (expr
, complain
);
6994 if (expr
== error_mark_node
)
6995 return error_mark_node
;
6998 /* If we are in a template, EXPR may be non-dependent, but still
6999 have a syntactic, rather than semantic, form. For example, EXPR
7000 might be a SCOPE_REF, rather than the VAR_DECL to which the
7001 SCOPE_REF refers. Preserving the qualifying scope is necessary
7002 so that access checking can be performed when the template is
7003 instantiated -- but here we need the resolved form so that we can
7004 convert the argument. */
7005 bool non_dep
= false;
7006 if (TYPE_REF_OBJ_P (type
)
7007 && has_value_dependent_address (expr
))
7008 /* If we want the address and it's value-dependent, don't fold. */;
7009 else if (processing_template_decl
7010 && is_nondependent_constant_expression (expr
))
7012 if (error_operand_p (expr
))
7013 return error_mark_node
;
7014 expr_type
= TREE_TYPE (expr
);
7016 /* If the argument is non-dependent, perform any conversions in
7017 non-dependent context as well. */
7018 processing_template_decl_sentinel
s (non_dep
);
7020 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7022 if (value_dependent_expression_p (expr
))
7023 expr
= canonicalize_expr_argument (expr
, complain
);
7025 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7026 to a non-type argument of "nullptr". */
7027 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7028 expr
= fold_simple (convert (type
, expr
));
7030 /* In C++11, integral or enumeration non-type template arguments can be
7031 arbitrary constant expressions. Pointer and pointer to
7032 member arguments can be general constant expressions that evaluate
7033 to a null value, but otherwise still need to be of a specific form. */
7034 if (cxx_dialect
>= cxx11
)
7036 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7037 /* A PTRMEM_CST is already constant, and a valid template
7038 argument for a parameter of pointer to member type, we just want
7039 to leave it in that form rather than lower it to a
7041 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7042 || cxx_dialect
>= cxx17
)
7044 /* Calling build_converted_constant_expr might create a call to
7045 a conversion function with a value-dependent argument, which
7046 could invoke taking the address of a temporary representing
7047 the result of the conversion. */
7048 if (COMPOUND_LITERAL_P (expr
)
7049 && CONSTRUCTOR_IS_DEPENDENT (expr
)
7050 && MAYBE_CLASS_TYPE_P (expr_type
)
7051 && TYPE_HAS_CONVERSION (expr_type
))
7053 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
7054 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7057 /* C++17: A template-argument for a non-type template-parameter shall
7058 be a converted constant expression (8.20) of the type of the
7059 template-parameter. */
7060 expr
= build_converted_constant_expr (type
, expr
, complain
);
7061 if (expr
== error_mark_node
)
7062 /* Make sure we return NULL_TREE only if we have really issued
7063 an error, as described above. */
7064 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7065 expr
= maybe_constant_value (expr
, NULL_TREE
,
7066 /*manifestly_const_eval=*/true);
7067 expr
= convert_from_reference (expr
);
7069 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7071 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7072 /*manifestly_const_eval=*/true);
7073 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7074 : null_member_pointer_value_p (folded
))
7079 if (TYPE_REF_P (type
))
7080 expr
= mark_lvalue_use (expr
);
7082 expr
= mark_rvalue_use (expr
);
7084 /* HACK: Due to double coercion, we can get a
7085 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7086 which is the tree that we built on the first call (see
7087 below when coercing to reference to object or to reference to
7088 function). We just strip everything and get to the arg.
7089 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7091 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7093 tree probe_type
, probe
= expr
;
7094 if (REFERENCE_REF_P (probe
))
7095 probe
= TREE_OPERAND (probe
, 0);
7096 probe_type
= TREE_TYPE (probe
);
7097 if (TREE_CODE (probe
) == NOP_EXPR
)
7099 /* ??? Maybe we could use convert_from_reference here, but we
7100 would need to relax its constraints because the NOP_EXPR
7101 could actually change the type to something more cv-qualified,
7102 and this is not folded by convert_from_reference. */
7103 tree addr
= TREE_OPERAND (probe
, 0);
7104 if (TYPE_REF_P (probe_type
)
7105 && TREE_CODE (addr
) == ADDR_EXPR
7106 && TYPE_PTR_P (TREE_TYPE (addr
))
7107 && (same_type_ignoring_top_level_qualifiers_p
7108 (TREE_TYPE (probe_type
),
7109 TREE_TYPE (TREE_TYPE (addr
)))))
7111 expr
= TREE_OPERAND (addr
, 0);
7112 expr_type
= TREE_TYPE (probe_type
);
7117 /* [temp.arg.nontype]/5, bullet 1
7119 For a non-type template-parameter of integral or enumeration type,
7120 integral promotions (_conv.prom_) and integral conversions
7121 (_conv.integral_) are applied. */
7122 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7124 if (cxx_dialect
< cxx11
)
7126 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7127 t
= maybe_constant_value (t
);
7128 if (t
!= error_mark_node
)
7132 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7133 return error_mark_node
;
7135 /* Notice that there are constant expressions like '4 % 0' which
7136 do not fold into integer constants. */
7137 if (TREE_CODE (expr
) != INTEGER_CST
7138 && !value_dependent_expression_p (expr
))
7140 if (complain
& tf_error
)
7142 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7143 if (!require_potential_constant_expression (expr
))
7144 expr
= error_mark_node
;
7146 expr
= cxx_constant_value (expr
);
7147 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7148 inform (loc
, "in template argument for type %qT", type
);
7149 if (expr
== error_mark_node
)
7151 /* else cxx_constant_value complained but gave us
7152 a real constant, so go ahead. */
7153 if (TREE_CODE (expr
) != INTEGER_CST
)
7155 /* Some assemble time constant expressions like
7156 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7157 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7158 as we can emit them into .rodata initializers of
7159 variables, yet they can't fold into an INTEGER_CST at
7160 compile time. Refuse them here. */
7161 gcc_checking_assert (reduced_constant_expression_p (expr
));
7162 error_at (loc
, "template argument %qE for type %qT not "
7163 "a constant integer", expr
, type
);
7171 /* Avoid typedef problems. */
7172 if (TREE_TYPE (expr
) != type
)
7173 expr
= fold_convert (type
, expr
);
7175 /* [temp.arg.nontype]/5, bullet 2
7177 For a non-type template-parameter of type pointer to object,
7178 qualification conversions (_conv.qual_) and the array-to-pointer
7179 conversion (_conv.array_) are applied. */
7180 else if (TYPE_PTROBV_P (type
))
7182 tree decayed
= expr
;
7184 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7185 decay_conversion or an explicit cast. If it's a problematic cast,
7186 we'll complain about it below. */
7187 if (TREE_CODE (expr
) == NOP_EXPR
)
7191 if (TREE_CODE (probe
) == ADDR_EXPR
7192 && TYPE_PTR_P (TREE_TYPE (probe
)))
7195 expr_type
= TREE_TYPE (expr
);
7199 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7201 A template-argument for a non-type, non-template template-parameter
7202 shall be one of: [...]
7204 -- the name of a non-type template-parameter;
7205 -- the address of an object or function with external linkage, [...]
7206 expressed as "& id-expression" where the & is optional if the name
7207 refers to a function or array, or if the corresponding
7208 template-parameter is a reference.
7210 Here, we do not care about functions, as they are invalid anyway
7211 for a parameter of type pointer-to-object. */
7213 if (value_dependent_expression_p (expr
))
7214 /* Non-type template parameters are OK. */
7216 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7217 /* Null pointer values are OK in C++11. */;
7218 else if (TREE_CODE (expr
) != ADDR_EXPR
7219 && !INDIRECT_TYPE_P (expr_type
))
7220 /* Other values, like integer constants, might be valid
7221 non-type arguments of some other type. */
7222 return error_mark_node
;
7223 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7228 expr
= perform_qualification_conversions (type
, expr
);
7229 if (expr
== error_mark_node
)
7230 return error_mark_node
;
7232 /* [temp.arg.nontype]/5, bullet 3
7234 For a non-type template-parameter of type reference to object, no
7235 conversions apply. The type referred to by the reference may be more
7236 cv-qualified than the (otherwise identical) type of the
7237 template-argument. The template-parameter is bound directly to the
7238 template-argument, which must be an lvalue. */
7239 else if (TYPE_REF_OBJ_P (type
))
7241 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7243 return error_mark_node
;
7245 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7247 if (complain
& tf_error
)
7248 error ("%qE is not a valid template argument for type %qT "
7249 "because of conflicts in cv-qualification", expr
, type
);
7253 if (!lvalue_p (expr
))
7255 if (complain
& tf_error
)
7256 error ("%qE is not a valid template argument for type %qT "
7257 "because it is not an lvalue", expr
, type
);
7261 /* [temp.arg.nontype]/1
7263 A template-argument for a non-type, non-template template-parameter
7264 shall be one of: [...]
7266 -- the address of an object or function with external linkage. */
7267 if (INDIRECT_REF_P (expr
)
7268 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7270 expr
= TREE_OPERAND (expr
, 0);
7273 if (complain
& tf_error
)
7274 error ("%q#D is not a valid template argument for type %qT "
7275 "because a reference variable does not have a constant "
7276 "address", expr
, type
);
7281 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7282 && value_dependent_expression_p (expr
))
7283 /* OK, dependent reference. We don't want to ask whether a DECL is
7284 itself value-dependent, since what we want here is its address. */;
7287 expr
= build_address (expr
);
7289 if (invalid_tparm_referent_p (type
, expr
, complain
))
7293 if (!same_type_p (type
, TREE_TYPE (expr
)))
7294 expr
= build_nop (type
, expr
);
7296 /* [temp.arg.nontype]/5, bullet 4
7298 For a non-type template-parameter of type pointer to function, only
7299 the function-to-pointer conversion (_conv.func_) is applied. If the
7300 template-argument represents a set of overloaded functions (or a
7301 pointer to such), the matching function is selected from the set
7303 else if (TYPE_PTRFN_P (type
))
7305 /* If the argument is a template-id, we might not have enough
7306 context information to decay the pointer. */
7307 if (!type_unknown_p (expr_type
))
7309 expr
= decay_conversion (expr
, complain
);
7310 if (expr
== error_mark_node
)
7311 return error_mark_node
;
7314 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7315 /* Null pointer values are OK in C++11. */
7316 return perform_qualification_conversions (type
, expr
);
7318 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7319 if (!expr
|| expr
== error_mark_node
)
7322 /* [temp.arg.nontype]/5, bullet 5
7324 For a non-type template-parameter of type reference to function, no
7325 conversions apply. If the template-argument represents a set of
7326 overloaded functions, the matching function is selected from the set
7328 else if (TYPE_REFFN_P (type
))
7330 if (TREE_CODE (expr
) == ADDR_EXPR
)
7332 if (complain
& tf_error
)
7334 error ("%qE is not a valid template argument for type %qT "
7335 "because it is a pointer", expr
, type
);
7336 inform (input_location
, "try using %qE instead",
7337 TREE_OPERAND (expr
, 0));
7342 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7343 if (!expr
|| expr
== error_mark_node
)
7346 /* [temp.arg.nontype]/5, bullet 6
7348 For a non-type template-parameter of type pointer to member function,
7349 no conversions apply. If the template-argument represents a set of
7350 overloaded member functions, the matching member function is selected
7351 from the set (_over.over_). */
7352 else if (TYPE_PTRMEMFUNC_P (type
))
7354 expr
= instantiate_type (type
, expr
, tf_none
);
7355 if (expr
== error_mark_node
)
7356 return error_mark_node
;
7358 /* [temp.arg.nontype] bullet 1 says the pointer to member
7359 expression must be a pointer-to-member constant. */
7360 if (!value_dependent_expression_p (expr
)
7361 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7364 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7365 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7366 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7367 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7369 /* [temp.arg.nontype]/5, bullet 7
7371 For a non-type template-parameter of type pointer to data member,
7372 qualification conversions (_conv.qual_) are applied. */
7373 else if (TYPE_PTRDATAMEM_P (type
))
7375 /* [temp.arg.nontype] bullet 1 says the pointer to member
7376 expression must be a pointer-to-member constant. */
7377 if (!value_dependent_expression_p (expr
)
7378 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7381 expr
= perform_qualification_conversions (type
, expr
);
7382 if (expr
== error_mark_node
)
7385 else if (NULLPTR_TYPE_P (type
))
7387 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7389 if (complain
& tf_error
)
7390 error ("%qE is not a valid template argument for type %qT "
7391 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7396 else if (CLASS_TYPE_P (type
))
7398 /* Replace the argument with a reference to the corresponding template
7399 parameter object. */
7400 if (!value_dependent_expression_p (expr
))
7401 expr
= get_template_parm_object (expr
, complain
);
7402 if (expr
== error_mark_node
)
7405 /* A template non-type parameter must be one of the above. */
7409 /* Sanity check: did we actually convert the argument to the
7411 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7412 (type
, TREE_TYPE (expr
)));
7413 return convert_from_reference (expr
);
7416 /* Subroutine of coerce_template_template_parms, which returns 1 if
7417 PARM_PARM and ARG_PARM match using the rule for the template
7418 parameters of template template parameters. Both PARM and ARG are
7419 template parameters; the rest of the arguments are the same as for
7420 coerce_template_template_parms.
7423 coerce_template_template_parm (tree parm
,
7425 tsubst_flags_t complain
,
7429 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7430 || parm
== NULL_TREE
|| error_operand_p (parm
))
7433 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7436 switch (TREE_CODE (parm
))
7439 /* We encounter instantiations of templates like
7440 template <template <template <class> class> class TT>
7443 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7444 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7446 if (!coerce_template_template_parms
7447 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7453 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7454 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7455 /* Argument is a parameter pack but parameter is not. */
7460 /* The tsubst call is used to handle cases such as
7462 template <int> class C {};
7463 template <class T, template <T> class TT> class D {};
7466 i.e. the parameter list of TT depends on earlier parameters. */
7467 if (!uses_template_parms (TREE_TYPE (arg
)))
7469 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7470 if (!uses_template_parms (t
)
7471 && !same_type_p (t
, TREE_TYPE (arg
)))
7475 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7476 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7477 /* Argument is a parameter pack but parameter is not. */
7489 /* Coerce template argument list ARGLIST for use with template
7490 template-parameter TEMPL. */
7493 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7494 tsubst_flags_t complain
)
7496 /* Consider an example where a template template parameter declared as
7498 template <class T, class U = std::allocator<T> > class TT
7500 The template parameter level of T and U are one level larger than
7501 of TT. To proper process the default argument of U, say when an
7502 instantiation `TT<int>' is seen, we need to build the full
7503 arguments containing {int} as the innermost level. Outer levels,
7504 available when not appearing as default template argument, can be
7505 obtained from the arguments of the enclosing template.
7507 Suppose that TT is later substituted with std::vector. The above
7508 instantiation is `TT<int, std::allocator<T> >' with TT at
7509 level 1, and T at level 2, while the template arguments at level 1
7510 becomes {std::vector} and the inner level 2 is {int}. */
7512 tree outer
= DECL_CONTEXT (templ
);
7514 outer
= generic_targs_for (outer
);
7515 else if (current_template_parms
)
7517 /* This is an argument of the current template, so we haven't set
7518 DECL_CONTEXT yet. */
7519 tree relevant_template_parms
;
7521 /* Parameter levels that are greater than the level of the given
7522 template template parm are irrelevant. */
7523 relevant_template_parms
= current_template_parms
;
7524 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7525 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7526 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7528 outer
= template_parms_to_args (relevant_template_parms
);
7532 arglist
= add_to_template_args (outer
, arglist
);
7534 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7535 return coerce_template_parms (parmlist
, arglist
, templ
,
7537 /*require_all_args=*/true,
7538 /*use_default_args=*/true);
7541 /* A cache of template template parameters with match-all default
7543 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7545 /* T is a bound template template-parameter. Copy its arguments into default
7546 arguments of the template template-parameter's template parameters. */
7549 add_defaults_to_ttp (tree otmpl
)
7551 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7554 tree ntmpl
= copy_node (otmpl
);
7556 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7557 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7558 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7559 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7560 TYPE_NAME (ntype
) = ntmpl
;
7561 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7563 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7564 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7565 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7566 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7568 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7569 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7570 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7571 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7572 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7574 tree o
= TREE_VEC_ELT (vec
, i
);
7575 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7577 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7578 TREE_PURPOSE (n
) = any_targ_node
;
7582 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7586 /* ARG is a bound potential template template-argument, and PARGS is a list
7587 of arguments for the corresponding template template-parameter. Adjust
7588 PARGS as appropriate for application to ARG's template, and if ARG is a
7589 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7590 arguments to the template template parameter. */
7593 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7595 ++processing_template_decl
;
7596 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7597 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7599 /* When comparing two template template-parameters in partial ordering,
7600 rewrite the one currently being used as an argument to have default
7601 arguments for all parameters. */
7602 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7603 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7604 if (pargs
!= error_mark_node
)
7605 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7606 TYPE_TI_ARGS (arg
));
7611 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7612 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7613 /*require_all*/true,
7614 /*use_default*/true);
7616 --processing_template_decl
;
7620 /* Subroutine of unify for the case when PARM is a
7621 BOUND_TEMPLATE_TEMPLATE_PARM. */
7624 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7627 tree parmvec
= TYPE_TI_ARGS (parm
);
7628 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7630 /* The template template parm might be variadic and the argument
7631 not, so flatten both argument lists. */
7632 parmvec
= expand_template_argument_pack (parmvec
);
7633 argvec
= expand_template_argument_pack (argvec
);
7637 /* In keeping with P0522R0, adjust P's template arguments
7638 to apply to A's template; then flatten it again. */
7639 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7640 nparmvec
= expand_template_argument_pack (nparmvec
);
7642 if (unify (tparms
, targs
, nparmvec
, argvec
,
7643 UNIFY_ALLOW_NONE
, explain_p
))
7646 /* If the P0522 adjustment eliminated a pack expansion, deduce
7649 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7650 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7651 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7656 /* Deduce arguments T, i from TT<T> or TT<i>.
7657 We check each element of PARMVEC and ARGVEC individually
7658 rather than the whole TREE_VEC since they can have
7659 different number of elements, which is allowed under N2555. */
7661 int len
= TREE_VEC_LENGTH (parmvec
);
7663 /* Check if the parameters end in a pack, making them
7665 int parm_variadic_p
= 0;
7667 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7668 parm_variadic_p
= 1;
7670 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7671 /* If the template argument list of P contains a pack
7672 expansion that is not the last template argument, the
7673 entire template argument list is a non-deduced
7675 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7676 return unify_success (explain_p
);
7678 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7679 return unify_too_few_arguments (explain_p
,
7680 TREE_VEC_LENGTH (argvec
), len
);
7682 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7683 if (unify (tparms
, targs
,
7684 TREE_VEC_ELT (parmvec
, i
),
7685 TREE_VEC_ELT (argvec
, i
),
7686 UNIFY_ALLOW_NONE
, explain_p
))
7690 && unify_pack_expansion (tparms
, targs
,
7693 /*subr=*/true, explain_p
))
7700 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7701 template template parameters. Both PARM_PARMS and ARG_PARMS are
7702 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7705 Consider the example:
7706 template <class T> class A;
7707 template<template <class U> class TT> class B;
7709 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7710 the parameters to A, and OUTER_ARGS contains A. */
7713 coerce_template_template_parms (tree parm_parms
,
7715 tsubst_flags_t complain
,
7719 int nparms
, nargs
, i
;
7723 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7724 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7726 nparms
= TREE_VEC_LENGTH (parm_parms
);
7727 nargs
= TREE_VEC_LENGTH (arg_parms
);
7731 /* P0522R0: A template template-parameter P is at least as specialized as
7732 a template template-argument A if, given the following rewrite to two
7733 function templates, the function template corresponding to P is at
7734 least as specialized as the function template corresponding to A
7735 according to the partial ordering rules for function templates
7736 ([temp.func.order]). Given an invented class template X with the
7737 template parameter list of A (including default arguments):
7739 * Each of the two function templates has the same template parameters,
7740 respectively, as P or A.
7742 * Each function template has a single function parameter whose type is
7743 a specialization of X with template arguments corresponding to the
7744 template parameters from the respective function template where, for
7745 each template parameter PP in the template parameter list of the
7746 function template, a corresponding template argument AA is formed. If
7747 PP declares a parameter pack, then AA is the pack expansion
7748 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7750 If the rewrite produces an invalid type, then P is not at least as
7751 specialized as A. */
7753 /* So coerce P's args to apply to A's parms, and then deduce between A's
7754 args and the converted args. If that succeeds, A is at least as
7755 specialized as P, so they match.*/
7756 tree pargs
= template_parms_level_to_args (parm_parms
);
7757 pargs
= add_outermost_template_args (outer_args
, pargs
);
7758 ++processing_template_decl
;
7759 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7760 /*require_all*/true, /*use_default*/true);
7761 --processing_template_decl
;
7762 if (pargs
!= error_mark_node
)
7764 tree targs
= make_tree_vec (nargs
);
7765 tree aargs
= template_parms_level_to_args (arg_parms
);
7766 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7772 /* Determine whether we have a parameter pack at the end of the
7773 template template parameter's template parameter list. */
7774 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7776 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7778 if (error_operand_p (parm
))
7781 switch (TREE_CODE (parm
))
7785 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7790 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7800 && !(variadic_p
&& nargs
>= nparms
- 1))
7803 /* Check all of the template parameters except the parameter pack at
7804 the end (if any). */
7805 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7807 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7808 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7811 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7812 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7814 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7822 /* Check each of the template parameters in the template
7823 argument against the template parameter pack at the end of
7824 the template template parameter. */
7825 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7828 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7830 for (; i
< nargs
; ++i
)
7832 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7835 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7837 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7846 /* Verifies that the deduced template arguments (in TARGS) for the
7847 template template parameters (in TPARMS) represent valid bindings,
7848 by comparing the template parameter list of each template argument
7849 to the template parameter list of its corresponding template
7850 template parameter, in accordance with DR150. This
7851 routine can only be called after all template arguments have been
7852 deduced. It will return TRUE if all of the template template
7853 parameter bindings are okay, FALSE otherwise. */
7855 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7857 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7860 /* We're dealing with template parms in this process. */
7861 ++processing_template_decl
;
7863 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7865 for (i
= 0; i
< ntparms
; ++i
)
7867 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7868 tree targ
= TREE_VEC_ELT (targs
, i
);
7870 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7872 tree packed_args
= NULL_TREE
;
7875 if (ARGUMENT_PACK_P (targ
))
7877 /* Look inside the argument pack. */
7878 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7879 len
= TREE_VEC_LENGTH (packed_args
);
7882 for (idx
= 0; idx
< len
; ++idx
)
7884 tree targ_parms
= NULL_TREE
;
7887 /* Extract the next argument from the argument
7889 targ
= TREE_VEC_ELT (packed_args
, idx
);
7891 if (PACK_EXPANSION_P (targ
))
7892 /* Look at the pattern of the pack expansion. */
7893 targ
= PACK_EXPANSION_PATTERN (targ
);
7895 /* Extract the template parameters from the template
7897 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7898 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7899 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7900 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7902 /* Verify that we can coerce the template template
7903 parameters from the template argument to the template
7904 parameter. This requires an exact match. */
7906 && !coerce_template_template_parms
7907 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7922 --processing_template_decl
;
7926 /* Since type attributes aren't mangled, we need to strip them from
7927 template type arguments. */
7930 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7932 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7934 bool removed_attributes
= false;
7935 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7936 if (removed_attributes
7937 && (complain
& tf_warning
))
7938 warning (OPT_Wignored_attributes
,
7939 "ignoring attributes on template argument %qT", arg
);
7943 /* And from inside dependent non-type arguments like sizeof(Type). */
7946 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7948 if (!arg
|| arg
== error_mark_node
)
7950 bool removed_attributes
= false;
7951 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7952 if (removed_attributes
7953 && (complain
& tf_warning
))
7954 warning (OPT_Wignored_attributes
,
7955 "ignoring attributes in template argument %qE", arg
);
7959 // A template declaration can be substituted for a constrained
7960 // template template parameter only when the argument is more
7961 // constrained than the parameter.
7963 is_compatible_template_arg (tree parm
, tree arg
)
7965 tree parm_cons
= get_constraints (parm
);
7967 /* For now, allow constrained template template arguments
7968 and unconstrained template template parameters. */
7969 if (parm_cons
== NULL_TREE
)
7972 /* If the template parameter is constrained, we need to rewrite its
7973 constraints in terms of the ARG's template parameters. This ensures
7974 that all of the template parameter types will have the same depth.
7976 Note that this is only valid when coerce_template_template_parm is
7977 true for the innermost template parameters of PARM and ARG. In other
7978 words, because coercion is successful, this conversion will be valid. */
7979 tree new_args
= NULL_TREE
;
7982 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7983 new_args
= template_parms_level_to_args (aparms
);
7984 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
7985 tf_none
, NULL_TREE
);
7986 if (parm_cons
== error_mark_node
)
7990 return weakly_subsumes (parm_cons
, new_args
, arg
);
7993 // Convert a placeholder argument into a binding to the original
7994 // parameter. The original parameter is saved as the TREE_TYPE of
7997 convert_wildcard_argument (tree parm
, tree arg
)
7999 TREE_TYPE (arg
) = parm
;
8003 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8004 because one of them is dependent. But we need to represent the
8005 conversion for the benefit of cp_tree_equal. */
8008 maybe_convert_nontype_argument (tree type
, tree arg
)
8010 /* Auto parms get no conversion. */
8011 if (type_uses_auto (type
))
8013 /* We don't need or want to add this conversion now if we're going to use the
8014 argument for deduction. */
8015 if (value_dependent_expression_p (arg
))
8018 type
= cv_unqualified (type
);
8019 tree argtype
= TREE_TYPE (arg
);
8020 if (same_type_p (type
, argtype
))
8023 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8024 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8028 /* Convert the indicated template ARG as necessary to match the
8029 indicated template PARM. Returns the converted ARG, or
8030 error_mark_node if the conversion was unsuccessful. Error and
8031 warning messages are issued under control of COMPLAIN. This
8032 conversion is for the Ith parameter in the parameter list. ARGS is
8033 the full set of template arguments deduced so far. */
8036 convert_template_argument (tree parm
,
8039 tsubst_flags_t complain
,
8045 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8047 if (parm
== error_mark_node
|| error_operand_p (arg
))
8048 return error_mark_node
;
8050 /* Trivially convert placeholders. */
8051 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8052 return convert_wildcard_argument (parm
, arg
);
8054 if (arg
== any_targ_node
)
8057 if (TREE_CODE (arg
) == TREE_LIST
8058 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8060 /* The template argument was the name of some
8061 member function. That's usually
8062 invalid, but static members are OK. In any
8063 case, grab the underlying fields/functions
8064 and issue an error later if required. */
8065 TREE_TYPE (arg
) = unknown_type_node
;
8070 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8071 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8072 || requires_tmpl_type
);
8074 /* When determining whether an argument pack expansion is a template,
8075 look at the pattern. */
8076 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8077 arg
= PACK_EXPANSION_PATTERN (arg
);
8079 /* Deal with an injected-class-name used as a template template arg. */
8080 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8082 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8083 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8085 if (cxx_dialect
>= cxx11
)
8086 /* OK under DR 1004. */;
8087 else if (complain
& tf_warning_or_error
)
8088 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8089 " used as template template argument", TYPE_NAME (arg
));
8090 else if (flag_pedantic_errors
)
8098 ((TREE_CODE (arg
) == TEMPLATE_DECL
8099 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8100 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8101 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8102 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8105 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8106 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8107 arg
= TYPE_STUB_DECL (arg
);
8109 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8111 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8112 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8114 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8116 if (complain
& tf_error
)
8117 error ("invalid use of destructor %qE as a type", orig_arg
);
8118 return error_mark_node
;
8121 permerror (input_location
,
8122 "to refer to a type member of a template parameter, "
8123 "use %<typename %E%>", orig_arg
);
8125 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8126 TREE_OPERAND (arg
, 1),
8132 if (is_type
!= requires_type
)
8136 if (complain
& tf_error
)
8138 error ("type/value mismatch at argument %d in template "
8139 "parameter list for %qD",
8143 /* The template argument is a type, but we're expecting
8145 inform (input_location
,
8146 " expected a constant of type %qT, got %qT",
8148 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8149 /* [temp.arg]/2: "In a template-argument, an ambiguity
8150 between a type-id and an expression is resolved to a
8151 type-id, regardless of the form of the corresponding
8152 template-parameter." So give the user a clue. */
8153 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8154 inform (input_location
, " ambiguous template argument "
8155 "for non-type template parameter is treated as "
8158 else if (requires_tmpl_type
)
8159 inform (input_location
,
8160 " expected a class template, got %qE", orig_arg
);
8162 inform (input_location
,
8163 " expected a type, got %qE", orig_arg
);
8166 return error_mark_node
;
8168 if (is_tmpl_type
^ requires_tmpl_type
)
8170 if (in_decl
&& (complain
& tf_error
))
8172 error ("type/value mismatch at argument %d in template "
8173 "parameter list for %qD",
8176 inform (input_location
,
8177 " expected a type, got %qT", DECL_NAME (arg
));
8179 inform (input_location
,
8180 " expected a class template, got %qT", orig_arg
);
8182 return error_mark_node
;
8185 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8186 /* We already did the appropriate conversion when packing args. */
8190 if (requires_tmpl_type
)
8192 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8193 /* The number of argument required is not known yet.
8194 Just accept it for now. */
8198 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8201 /* Strip alias templates that are equivalent to another
8203 arg
= get_underlying_template (arg
);
8204 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8206 if (coerce_template_template_parms (parmparm
, argparm
,
8212 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8214 if (val
!= error_mark_node
)
8216 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8217 val
= TREE_TYPE (val
);
8218 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8219 val
= make_pack_expansion (val
, complain
);
8224 if (in_decl
&& (complain
& tf_error
))
8226 error ("type/value mismatch at argument %d in "
8227 "template parameter list for %qD",
8229 inform (input_location
,
8230 " expected a template of type %qD, got %qT",
8234 val
= error_mark_node
;
8237 // Check that the constraints are compatible before allowing the
8239 if (val
!= error_mark_node
)
8240 if (!is_compatible_template_arg (parm
, arg
))
8242 if (in_decl
&& (complain
& tf_error
))
8244 error ("constraint mismatch at argument %d in "
8245 "template parameter list for %qD",
8247 inform (input_location
, " expected %qD but got %qD",
8250 val
= error_mark_node
;
8256 /* We only form one instance of each template specialization.
8257 Therefore, if we use a non-canonical variant (i.e., a
8258 typedef), any future messages referring to the type will use
8259 the typedef, which is confusing if those future uses do not
8260 themselves also use the typedef. */
8262 val
= canonicalize_type_argument (val
, complain
);
8266 tree t
= TREE_TYPE (parm
);
8268 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8269 > TMPL_ARGS_DEPTH (args
))
8270 /* We don't have enough levels of args to do any substitution. This
8271 can happen in the context of -fnew-ttp-matching. */;
8272 else if (tree a
= type_uses_auto (t
))
8274 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8275 if (t
== error_mark_node
)
8276 return error_mark_node
;
8279 t
= tsubst (t
, args
, complain
, in_decl
);
8281 if (invalid_nontype_parm_type_p (t
, complain
))
8282 return error_mark_node
;
8284 if (t
!= TREE_TYPE (parm
))
8285 t
= canonicalize_type_argument (t
, complain
);
8287 if (!type_dependent_expression_p (orig_arg
)
8288 && !uses_template_parms (t
))
8289 /* We used to call digest_init here. However, digest_init
8290 will report errors, which we don't want when complain
8291 is zero. More importantly, digest_init will try too
8292 hard to convert things: for example, `0' should not be
8293 converted to pointer type at this point according to
8294 the standard. Accepting this is not merely an
8295 extension, since deciding whether or not these
8296 conversions can occur is part of determining which
8297 function template to call, or whether a given explicit
8298 argument specification is valid. */
8299 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8302 val
= canonicalize_expr_argument (orig_arg
, complain
);
8303 val
= maybe_convert_nontype_argument (t
, val
);
8307 if (val
== NULL_TREE
)
8308 val
= error_mark_node
;
8309 else if (val
== error_mark_node
&& (complain
& tf_error
))
8310 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8311 "could not convert template argument %qE from %qT to %qT",
8312 orig_arg
, TREE_TYPE (orig_arg
), t
);
8314 if (INDIRECT_REF_P (val
))
8316 /* Reject template arguments that are references to built-in
8317 functions with no library fallbacks. */
8318 const_tree inner
= TREE_OPERAND (val
, 0);
8319 const_tree innertype
= TREE_TYPE (inner
);
8321 && TYPE_REF_P (innertype
)
8322 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8323 && TREE_OPERAND_LENGTH (inner
) > 0
8324 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8325 return error_mark_node
;
8328 if (TREE_CODE (val
) == SCOPE_REF
)
8330 /* Strip typedefs from the SCOPE_REF. */
8331 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8332 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8334 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8335 QUALIFIED_NAME_IS_TEMPLATE (val
));
8342 /* Coerces the remaining template arguments in INNER_ARGS (from
8343 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8344 Returns the coerced argument pack. PARM_IDX is the position of this
8345 parameter in the template parameter list. ARGS is the original
8346 template argument list. */
8348 coerce_template_parameter_pack (tree parms
,
8356 tsubst_flags_t complain
)
8358 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8359 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8362 tree packed_parms
= NULL_TREE
;
8364 if (arg_idx
> nargs
)
8367 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8369 /* When the template parameter is a non-type template parameter pack
8370 or template template parameter pack whose type or template
8371 parameters use parameter packs, we know exactly how many arguments
8372 we are looking for. Build a vector of the instantiated decls for
8373 these template parameters in PACKED_PARMS. */
8374 /* We can't use make_pack_expansion here because it would interpret a
8375 _DECL as a use rather than a declaration. */
8376 tree decl
= TREE_VALUE (parm
);
8377 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8378 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8379 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8380 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8382 TREE_VEC_LENGTH (args
)--;
8383 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8384 TREE_VEC_LENGTH (args
)++;
8386 if (packed_parms
== error_mark_node
)
8387 return error_mark_node
;
8389 /* If we're doing a partial instantiation of a member template,
8390 verify that all of the types used for the non-type
8391 template parameter pack are, in fact, valid for non-type
8392 template parameters. */
8394 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8396 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8397 for (j
= 0; j
< len
; ++j
)
8399 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8400 if (TREE_CODE (t
) == PARM_DECL
8401 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8402 return error_mark_node
;
8404 /* We don't know how many args we have yet, just
8405 use the unconverted ones for now. */
8409 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8411 /* Check if we have a placeholder pack, which indicates we're
8412 in the context of a introduction list. In that case we want
8413 to match this pack to the single placeholder. */
8414 else if (arg_idx
< nargs
8415 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8416 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8418 nargs
= arg_idx
+ 1;
8419 packed_args
= make_tree_vec (1);
8422 packed_args
= make_tree_vec (nargs
- arg_idx
);
8424 /* Convert the remaining arguments, which will be a part of the
8425 parameter pack "parm". */
8426 int first_pack_arg
= arg_idx
;
8427 for (; arg_idx
< nargs
; ++arg_idx
)
8429 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8430 tree actual_parm
= TREE_VALUE (parm
);
8431 int pack_idx
= arg_idx
- first_pack_arg
;
8435 /* Once we've packed as many args as we have types, stop. */
8436 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8438 else if (PACK_EXPANSION_P (arg
))
8439 /* We don't know how many args we have yet, just
8440 use the unconverted ones for now. */
8443 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8446 if (arg
== error_mark_node
)
8448 if (complain
& tf_error
)
8449 error ("template argument %d is invalid", arg_idx
+ 1);
8452 arg
= convert_template_argument (actual_parm
,
8453 arg
, new_args
, complain
, parm_idx
,
8455 if (arg
== error_mark_node
)
8457 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8460 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8461 && TREE_VEC_LENGTH (packed_args
) > 0)
8463 if (complain
& tf_error
)
8464 error ("wrong number of template arguments (%d, should be %d)",
8465 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8466 return error_mark_node
;
8469 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8470 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8471 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8474 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8475 TREE_CONSTANT (argument_pack
) = 1;
8478 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8480 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8481 TREE_VEC_LENGTH (packed_args
));
8482 return argument_pack
;
8485 /* Returns the number of pack expansions in the template argument vector
8489 pack_expansion_args_count (tree args
)
8494 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8496 tree elt
= TREE_VEC_ELT (args
, i
);
8497 if (elt
&& PACK_EXPANSION_P (elt
))
8503 /* Convert all template arguments to their appropriate types, and
8504 return a vector containing the innermost resulting template
8505 arguments. If any error occurs, return error_mark_node. Error and
8506 warning messages are issued under control of COMPLAIN.
8508 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8509 for arguments not specified in ARGS. Otherwise, if
8510 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8511 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8512 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8516 coerce_template_parms (tree parms
,
8519 tsubst_flags_t complain
,
8520 bool require_all_args
,
8521 bool use_default_args
)
8523 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8524 tree orig_inner_args
;
8527 tree new_inner_args
;
8529 /* When used as a boolean value, indicates whether this is a
8530 variadic template parameter list. Since it's an int, we can also
8531 subtract it from nparms to get the number of non-variadic
8534 int variadic_args_p
= 0;
8535 int post_variadic_parms
= 0;
8537 /* Adjustment to nparms for fixed parameter packs. */
8538 int fixed_pack_adjust
= 0;
8539 int fixed_packs
= 0;
8542 /* Likewise for parameters with default arguments. */
8545 if (args
== error_mark_node
)
8546 return error_mark_node
;
8548 nparms
= TREE_VEC_LENGTH (parms
);
8550 /* Determine if there are any parameter packs or default arguments. */
8551 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8553 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8555 ++post_variadic_parms
;
8556 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8558 if (TREE_PURPOSE (parm
))
8562 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8563 /* If there are no parameters that follow a parameter pack, we need to
8564 expand any argument packs so that we can deduce a parameter pack from
8565 some non-packed args followed by an argument pack, as in variadic85.C.
8566 If there are such parameters, we need to leave argument packs intact
8567 so the arguments are assigned properly. This can happen when dealing
8568 with a nested class inside a partial specialization of a class
8569 template, as in variadic92.C, or when deducing a template parameter pack
8570 from a sub-declarator, as in variadic114.C. */
8571 if (!post_variadic_parms
)
8572 inner_args
= expand_template_argument_pack (inner_args
);
8574 /* Count any pack expansion args. */
8575 variadic_args_p
= pack_expansion_args_count (inner_args
);
8577 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8578 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8579 || (nargs
< nparms
- variadic_p
8582 && (!use_default_args
8583 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8584 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8587 if (complain
& tf_error
)
8589 if (variadic_p
|| default_p
)
8591 nparms
-= variadic_p
+ default_p
;
8592 error ("wrong number of template arguments "
8593 "(%d, should be at least %d)", nargs
, nparms
);
8596 error ("wrong number of template arguments "
8597 "(%d, should be %d)", nargs
, nparms
);
8600 inform (DECL_SOURCE_LOCATION (in_decl
),
8601 "provided for %qD", in_decl
);
8604 return error_mark_node
;
8606 /* We can't pass a pack expansion to a non-pack parameter of an alias
8607 template (DR 1430). */
8609 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8610 || concept_definition_p (in_decl
))
8612 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8614 if (complain
& tf_error
)
8616 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8618 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8619 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8621 if (PACK_EXPANSION_P (arg
)
8622 && !template_parameter_pack_p (parm
))
8624 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8625 error_at (location_of (arg
),
8626 "pack expansion argument for non-pack parameter "
8627 "%qD of alias template %qD", parm
, in_decl
);
8629 error_at (location_of (arg
),
8630 "pack expansion argument for non-pack parameter "
8631 "%qD of concept %qD", parm
, in_decl
);
8632 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8639 return error_mark_node
;
8642 /* We need to evaluate the template arguments, even though this
8643 template-id may be nested within a "sizeof". */
8646 new_inner_args
= make_tree_vec (nparms
);
8647 new_args
= add_outermost_template_args (args
, new_inner_args
);
8648 int pack_adjust
= 0;
8649 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8654 /* Get the Ith template parameter. */
8655 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8657 if (parm
== error_mark_node
)
8659 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8663 /* Calculate the next argument. */
8664 if (arg_idx
< nargs
)
8665 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8669 if (template_parameter_pack_p (TREE_VALUE (parm
))
8670 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8671 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8673 /* Some arguments will be placed in the
8674 template parameter pack PARM. */
8675 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8676 inner_args
, arg_idx
,
8680 if (arg
== NULL_TREE
)
8682 /* We don't know how many args we have yet, just use the
8683 unconverted (and still packed) ones for now. */
8684 new_inner_args
= orig_inner_args
;
8689 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8691 /* Store this argument. */
8692 if (arg
== error_mark_node
)
8695 /* We are done with all of the arguments. */
8701 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8702 arg_idx
+= pack_adjust
;
8703 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8706 fixed_pack_adjust
+= pack_adjust
;
8714 if (PACK_EXPANSION_P (arg
))
8716 /* "If every valid specialization of a variadic template
8717 requires an empty template parameter pack, the template is
8718 ill-formed, no diagnostic required." So check that the
8719 pattern works with this parameter. */
8720 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8721 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8725 if (conv
== error_mark_node
)
8727 if (complain
& tf_error
)
8728 inform (input_location
, "so any instantiation with a "
8729 "non-empty parameter pack would be ill-formed");
8732 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8733 /* Recover from missing typename. */
8734 TREE_VEC_ELT (inner_args
, arg_idx
)
8735 = make_pack_expansion (conv
, complain
);
8737 /* We don't know how many args we have yet, just
8738 use the unconverted ones for now. */
8739 new_inner_args
= inner_args
;
8744 else if (require_all_args
)
8746 /* There must be a default arg in this case. */
8747 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8749 /* The position of the first default template argument,
8750 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8752 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8753 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8754 arg_idx
- pack_adjust
);
8759 if (arg
== error_mark_node
)
8761 if (complain
& tf_error
)
8762 error ("template argument %d is invalid", arg_idx
+ 1);
8766 /* This can occur if there was an error in the template
8767 parameter list itself (which we would already have
8768 reported) that we are trying to recover from, e.g., a class
8769 template with a parameter list such as
8770 template<typename..., typename> (cpp0x/variadic150.C). */
8773 /* This can also happen with a fixed parameter pack (71834). */
8774 if (arg_idx
>= nargs
)
8778 arg
= convert_template_argument (TREE_VALUE (parm
),
8779 arg
, new_args
, complain
,
8782 if (arg
== error_mark_node
)
8785 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8788 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8790 /* If we had fixed parameter packs, we didn't know how many arguments we
8791 actually needed earlier; now we do. */
8792 nparms
+= fixed_pack_adjust
;
8793 variadic_p
-= fixed_packs
;
8797 if (arg_idx
< nargs
)
8799 /* We had some pack expansion arguments that will only work if the packs
8800 are empty, but wait until instantiation time to complain.
8801 See variadic-ttp3.C. */
8803 /* Except that we can't provide empty packs to alias templates or
8804 concepts when there are no corresponding parameters. Basically,
8805 we can get here with this:
8807 template<typename T> concept C = true;
8809 template<typename... Args>
8813 When parsing C<Args...>, we try to form a concept check of
8814 C<?, Args...>. Without the extra check for substituting an empty
8815 pack past the last parameter, we can accept the check as valid.
8817 FIXME: This may be valid for alias templates (but I doubt it).
8819 FIXME: The error could be better also. */
8820 if (in_decl
&& concept_definition_p (in_decl
))
8822 if (complain
& tf_error
)
8823 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8824 "too many arguments");
8825 return error_mark_node
;
8828 int len
= nparms
+ (nargs
- arg_idx
);
8829 tree args
= make_tree_vec (len
);
8831 for (; i
< nparms
; ++i
)
8832 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8833 for (; i
< len
; ++i
, ++arg_idx
)
8834 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8835 arg_idx
- pack_adjust
);
8836 new_inner_args
= args
;
8841 gcc_assert (!(complain
& tf_error
) || seen_error ());
8842 return error_mark_node
;
8845 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8846 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8847 TREE_VEC_LENGTH (new_inner_args
));
8849 return new_inner_args
;
8852 /* Convert all template arguments to their appropriate types, and
8853 return a vector containing the innermost resulting template
8854 arguments. If any error occurs, return error_mark_node. Error and
8855 warning messages are not issued.
8857 Note that no function argument deduction is performed, and default
8858 arguments are used to fill in unspecified arguments. */
8860 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8862 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8865 /* Convert all template arguments to their appropriate type, and
8866 instantiate default arguments as needed. This returns a vector
8867 containing the innermost resulting template arguments, or
8868 error_mark_node if unsuccessful. */
8870 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8871 tsubst_flags_t complain
)
8873 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8876 /* Like coerce_template_parms. If PARMS represents all template
8877 parameters levels, this function returns a vector of vectors
8878 representing all the resulting argument levels. Note that in this
8879 case, only the innermost arguments are coerced because the
8880 outermost ones are supposed to have been coerced already.
8882 Otherwise, if PARMS represents only (the innermost) vector of
8883 parameters, this function returns a vector containing just the
8884 innermost resulting arguments. */
8887 coerce_innermost_template_parms (tree parms
,
8890 tsubst_flags_t complain
,
8891 bool require_all_args
,
8892 bool use_default_args
)
8894 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8895 int args_depth
= TMPL_ARGS_DEPTH (args
);
8898 if (parms_depth
> 1)
8900 coerced_args
= make_tree_vec (parms_depth
);
8904 for (level
= parms
, cur_depth
= parms_depth
;
8905 parms_depth
> 0 && level
!= NULL_TREE
;
8906 level
= TREE_CHAIN (level
), --cur_depth
)
8909 if (cur_depth
== args_depth
)
8910 l
= coerce_template_parms (TREE_VALUE (level
),
8911 args
, in_decl
, complain
,
8915 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8917 if (l
== error_mark_node
)
8918 return error_mark_node
;
8920 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8924 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8925 args
, in_decl
, complain
,
8928 return coerced_args
;
8931 /* Returns 1 if template args OT and NT are equivalent. */
8934 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8938 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8940 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8943 if (TREE_CODE (nt
) == TREE_VEC
)
8944 /* For member templates */
8945 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8946 else if (PACK_EXPANSION_P (ot
))
8947 return (PACK_EXPANSION_P (nt
)
8948 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8949 PACK_EXPANSION_PATTERN (nt
))
8950 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8951 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8952 else if (ARGUMENT_PACK_P (ot
))
8957 if (!ARGUMENT_PACK_P (nt
))
8960 opack
= ARGUMENT_PACK_ARGS (ot
);
8961 npack
= ARGUMENT_PACK_ARGS (nt
);
8962 len
= TREE_VEC_LENGTH (opack
);
8963 if (TREE_VEC_LENGTH (npack
) != len
)
8965 for (i
= 0; i
< len
; ++i
)
8966 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8967 TREE_VEC_ELT (npack
, i
)))
8971 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8973 else if (TYPE_P (nt
))
8977 /* Don't treat an alias template specialization with dependent
8978 arguments as equivalent to its underlying type when used as a
8979 template argument; we need them to be distinct so that we
8980 substitute into the specialization arguments at instantiation
8981 time. And aliases can't be equivalent without being ==, so
8982 we don't need to look any deeper.
8984 During partial ordering, however, we need to treat them normally so
8985 that we can order uses of the same alias with different
8986 cv-qualification (79960). */
8988 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8991 return same_type_p (ot
, nt
);
8993 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8997 /* Try to treat a template non-type argument that has been converted
8998 to the parameter type as equivalent to one that hasn't yet. */
8999 for (enum tree_code code1
= TREE_CODE (ot
);
9000 CONVERT_EXPR_CODE_P (code1
)
9001 || code1
== NON_LVALUE_EXPR
;
9002 code1
= TREE_CODE (ot
))
9003 ot
= TREE_OPERAND (ot
, 0);
9004 for (enum tree_code code2
= TREE_CODE (nt
);
9005 CONVERT_EXPR_CODE_P (code2
)
9006 || code2
== NON_LVALUE_EXPR
;
9007 code2
= TREE_CODE (nt
))
9008 nt
= TREE_OPERAND (nt
, 0);
9010 return cp_tree_equal (ot
, nt
);
9014 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9015 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9016 NEWARG_PTR with the offending arguments if they are non-NULL. */
9019 comp_template_args (tree oldargs
, tree newargs
,
9020 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9025 if (oldargs
== newargs
)
9028 if (!oldargs
|| !newargs
)
9031 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9034 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9036 tree nt
= TREE_VEC_ELT (newargs
, i
);
9037 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9039 if (! template_args_equal (ot
, nt
, partial_order
))
9041 if (oldarg_ptr
!= NULL
)
9043 if (newarg_ptr
!= NULL
)
9052 comp_template_args_porder (tree oargs
, tree nargs
)
9054 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9057 /* Implement a freelist interface for objects of type T.
9059 Head is a separate object, rather than a regular member, so that we
9060 can define it as a GTY deletable pointer, which is highly
9061 desirable. A data member could be declared that way, but then the
9062 containing object would implicitly get GTY((user)), which would
9063 prevent us from instantiating freelists as global objects.
9064 Although this way we can create freelist global objects, they're
9065 such thin wrappers that instantiating temporaries at every use
9066 loses nothing and saves permanent storage for the freelist object.
9068 Member functions next, anew, poison and reinit have default
9069 implementations that work for most of the types we're interested
9070 in, but if they don't work for some type, they should be explicitly
9071 specialized. See the comments before them for requirements, and
9072 the example specializations for the tree_list_freelist. */
9073 template <typename T
>
9076 /* Return the next object in a chain. We could just do type
9077 punning, but if we access the object with its underlying type, we
9078 avoid strict-aliasing trouble. This needs only work between
9079 poison and reinit. */
9080 static T
*&next (T
*obj
) { return obj
->next
; }
9082 /* Return a newly allocated, uninitialized or minimally-initialized
9083 object of type T. Any initialization performed by anew should
9084 either remain across the life of the object and the execution of
9085 poison, or be redone by reinit. */
9086 static T
*anew () { return ggc_alloc
<T
> (); }
9088 /* Optionally scribble all over the bits holding the object, so that
9089 they become (mostly?) uninitialized memory. This is called while
9090 preparing to make the object part of the free list. */
9091 static void poison (T
*obj
) {
9092 T
*p ATTRIBUTE_UNUSED
= obj
;
9093 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9095 #ifdef ENABLE_GC_CHECKING
9096 /* Poison the data, to indicate the data is garbage. */
9097 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9098 memset (p
, 0xa5, sizeof (*p
));
9100 /* Let valgrind know the object is free. */
9101 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9103 /* Let valgrind know the next portion of the object is available,
9104 but uninitialized. */
9105 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9108 /* Bring an object that underwent at least one lifecycle after anew
9109 and before the most recent free and poison, back to a usable
9110 state, reinitializing whatever is needed for it to be
9111 functionally equivalent to an object just allocated and returned
9112 by anew. This may poison or clear the next field, used by
9113 freelist housekeeping after poison was called. */
9114 static void reinit (T
*obj
) {
9115 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9117 #ifdef ENABLE_GC_CHECKING
9118 memset (q
, 0xa5, sizeof (*q
));
9120 /* Let valgrind know the entire object is available, but
9122 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9125 /* Reference a GTY-deletable pointer that points to the first object
9126 in the free list proper. */
9129 /* Construct a freelist object chaining objects off of HEAD. */
9130 freelist (T
*&head
) : head(head
) {}
9132 /* Add OBJ to the free object list. The former head becomes OBJ's
9141 /* Take an object from the free list, if one is available, or
9142 allocate a new one. Objects taken from the free list should be
9143 regarded as filled with garbage, except for bits that are
9144 configured to be preserved across free and alloc. */
9159 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9160 want to allocate a TREE_LIST using the usual interface, and ensure
9161 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9162 build_tree_list logic in reinit, so this could go out of sync. */
9165 freelist
<tree_node
>::next (tree obj
)
9167 return TREE_CHAIN (obj
);
9171 freelist
<tree_node
>::anew ()
9173 return build_tree_list (NULL
, NULL
);
9177 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9179 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9180 tree p ATTRIBUTE_UNUSED
= obj
;
9181 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9182 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9184 #ifdef ENABLE_GC_CHECKING
9185 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9187 /* Poison the data, to indicate the data is garbage. */
9188 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9189 memset (p
, 0xa5, size
);
9191 /* Let valgrind know the object is free. */
9192 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9193 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9194 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9195 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9197 #ifdef ENABLE_GC_CHECKING
9198 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9199 /* Keep TREE_CHAIN functional. */
9200 TREE_SET_CODE (obj
, TREE_LIST
);
9202 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9207 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9209 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9211 #ifdef ENABLE_GC_CHECKING
9212 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9213 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9214 memset (obj
, 0, sizeof (tree_list
));
9217 /* Let valgrind know the entire object is available, but
9219 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9221 #ifdef ENABLE_GC_CHECKING
9222 TREE_SET_CODE (obj
, TREE_LIST
);
9224 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9228 /* Point to the first object in the TREE_LIST freelist. */
9229 static GTY((deletable
)) tree tree_list_freelist_head
;
9230 /* Return the/an actual TREE_LIST freelist. */
9231 static inline freelist
<tree_node
>
9232 tree_list_freelist ()
9234 return tree_list_freelist_head
;
9237 /* Point to the first object in the tinst_level freelist. */
9238 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9239 /* Return the/an actual tinst_level freelist. */
9240 static inline freelist
<tinst_level
>
9241 tinst_level_freelist ()
9243 return tinst_level_freelist_head
;
9246 /* Point to the first object in the pending_template freelist. */
9247 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9248 /* Return the/an actual pending_template freelist. */
9249 static inline freelist
<pending_template
>
9250 pending_template_freelist ()
9252 return pending_template_freelist_head
;
9255 /* Build the TREE_LIST object out of a split list, store it
9256 permanently, and return it. */
9258 tinst_level::to_list ()
9260 gcc_assert (split_list_p ());
9261 tree ret
= tree_list_freelist ().alloc ();
9262 TREE_PURPOSE (ret
) = tldcl
;
9263 TREE_VALUE (ret
) = targs
;
9266 gcc_assert (tree_list_p ());
9270 const unsigned short tinst_level::refcount_infinity
;
9272 /* Increment OBJ's refcount unless it is already infinite. */
9273 static tinst_level
*
9274 inc_refcount_use (tinst_level
*obj
)
9276 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9281 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9283 tinst_level::free (tinst_level
*obj
)
9285 if (obj
->tree_list_p ())
9286 tree_list_freelist ().free (obj
->get_node ());
9287 tinst_level_freelist ().free (obj
);
9290 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9291 OBJ's DECL and OBJ, and start over with the tinst_level object that
9292 used to be referenced by OBJ's NEXT. */
9294 dec_refcount_use (tinst_level
*obj
)
9297 && obj
->refcount
!= tinst_level::refcount_infinity
9298 && !--obj
->refcount
)
9300 tinst_level
*next
= obj
->next
;
9301 tinst_level::free (obj
);
9306 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9307 and of the former PTR. Omitting the second argument is equivalent
9308 to passing (T*)NULL; this is allowed because passing the
9309 zero-valued integral constant NULL confuses type deduction and/or
9310 overload resolution. */
9311 template <typename T
>
9313 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9316 ptr
= inc_refcount_use (obj
);
9317 dec_refcount_use (save
);
9321 add_pending_template (tree d
)
9323 tree ti
= (TYPE_P (d
)
9324 ? CLASSTYPE_TEMPLATE_INFO (d
)
9325 : DECL_TEMPLATE_INFO (d
));
9326 struct pending_template
*pt
;
9329 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9332 /* We are called both from instantiate_decl, where we've already had a
9333 tinst_level pushed, and instantiate_template, where we haven't.
9335 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9336 level
= !current_tinst_level
9337 || current_tinst_level
->maybe_get_node () != d
;
9340 push_tinst_level (d
);
9342 pt
= pending_template_freelist ().alloc ();
9345 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9346 if (last_pending_template
)
9347 last_pending_template
->next
= pt
;
9349 pending_templates
= pt
;
9351 last_pending_template
= pt
;
9353 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9360 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9361 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9362 documentation for TEMPLATE_ID_EXPR. */
9365 lookup_template_function (tree fns
, tree arglist
)
9367 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9368 return error_mark_node
;
9370 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9372 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9374 error ("%q#D is not a function template", fns
);
9375 return error_mark_node
;
9378 if (BASELINK_P (fns
))
9380 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9382 BASELINK_FUNCTIONS (fns
),
9387 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9390 /* Within the scope of a template class S<T>, the name S gets bound
9391 (in build_self_reference) to a TYPE_DECL for the class, not a
9392 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9393 or one of its enclosing classes, and that type is a template,
9394 return the associated TEMPLATE_DECL. Otherwise, the original
9397 Also handle the case when DECL is a TREE_LIST of ambiguous
9398 injected-class-names from different bases. */
9401 maybe_get_template_decl_from_type_decl (tree decl
)
9403 if (decl
== NULL_TREE
)
9406 /* DR 176: A lookup that finds an injected-class-name (10.2
9407 [class.member.lookup]) can result in an ambiguity in certain cases
9408 (for example, if it is found in more than one base class). If all of
9409 the injected-class-names that are found refer to specializations of
9410 the same class template, and if the name is followed by a
9411 template-argument-list, the reference refers to the class template
9412 itself and not a specialization thereof, and is not ambiguous. */
9413 if (TREE_CODE (decl
) == TREE_LIST
)
9415 tree t
, tmpl
= NULL_TREE
;
9416 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9418 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9421 else if (tmpl
!= elt
)
9424 if (tmpl
&& t
== NULL_TREE
)
9430 return (decl
!= NULL_TREE
9431 && DECL_SELF_REFERENCE_P (decl
)
9432 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9433 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9436 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9437 parameters, find the desired type.
9439 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9441 IN_DECL, if non-NULL, is the template declaration we are trying to
9444 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9445 the class we are looking up.
9447 Issue error and warning messages under control of COMPLAIN.
9449 If the template class is really a local class in a template
9450 function, then the FUNCTION_CONTEXT is the function in which it is
9453 ??? Note that this function is currently called *twice* for each
9454 template-id: the first time from the parser, while creating the
9455 incomplete type (finish_template_type), and the second type during the
9456 real instantiation (instantiate_template_class). This is surely something
9457 that we want to avoid. It also causes some problems with argument
9458 coercion (see convert_nontype_argument for more information on this). */
9461 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9462 int entering_scope
, tsubst_flags_t complain
)
9464 tree templ
= NULL_TREE
, parmlist
;
9471 if (identifier_p (d1
))
9473 tree value
= innermost_non_namespace_value (d1
);
9474 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9479 push_decl_namespace (context
);
9480 templ
= lookup_name (d1
);
9481 templ
= maybe_get_template_decl_from_type_decl (templ
);
9483 pop_decl_namespace ();
9486 context
= DECL_CONTEXT (templ
);
9488 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9490 tree type
= TREE_TYPE (d1
);
9492 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9493 an implicit typename for the second A. Deal with it. */
9494 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9495 type
= TREE_TYPE (type
);
9497 if (CLASSTYPE_TEMPLATE_INFO (type
))
9499 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9500 d1
= DECL_NAME (templ
);
9503 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9504 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9506 templ
= TYPE_TI_TEMPLATE (d1
);
9507 d1
= DECL_NAME (templ
);
9509 else if (DECL_TYPE_TEMPLATE_P (d1
))
9512 d1
= DECL_NAME (templ
);
9513 context
= DECL_CONTEXT (templ
);
9515 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9518 d1
= DECL_NAME (templ
);
9521 /* Issue an error message if we didn't find a template. */
9524 if (complain
& tf_error
)
9525 error ("%qT is not a template", d1
);
9526 return error_mark_node
;
9529 if (TREE_CODE (templ
) != TEMPLATE_DECL
9530 /* Make sure it's a user visible template, if it was named by
9532 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9533 && !PRIMARY_TEMPLATE_P (templ
)))
9535 if (complain
& tf_error
)
9537 error ("non-template type %qT used as a template", d1
);
9539 error ("for template declaration %q+D", in_decl
);
9541 return error_mark_node
;
9544 complain
&= ~tf_user
;
9546 /* An alias that just changes the name of a template is equivalent to the
9547 other template, so if any of the arguments are pack expansions, strip
9548 the alias to avoid problems with a pack expansion passed to a non-pack
9549 alias template parameter (DR 1430). */
9550 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9551 templ
= get_underlying_template (templ
);
9553 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9556 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9557 if (arglist2
== error_mark_node
9558 || (!uses_template_parms (arglist2
)
9559 && check_instantiated_args (templ
, arglist2
, complain
)))
9560 return error_mark_node
;
9562 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9567 tree template_type
= TREE_TYPE (templ
);
9570 tree found
= NULL_TREE
;
9573 int is_dependent_type
;
9574 int use_partial_inst_tmpl
= false;
9576 if (template_type
== error_mark_node
)
9577 /* An error occurred while building the template TEMPL, and a
9578 diagnostic has most certainly been emitted for that
9579 already. Let's propagate that error. */
9580 return error_mark_node
;
9582 gen_tmpl
= most_general_template (templ
);
9583 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9584 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9585 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9587 if (arg_depth
== 1 && parm_depth
> 1)
9589 /* We've been given an incomplete set of template arguments.
9592 template <class T> struct S1 {
9593 template <class U> struct S2 {};
9594 template <class U> struct S2<U*> {};
9597 we will be called with an ARGLIST of `U*', but the
9598 TEMPLATE will be `template <class T> template
9599 <class U> struct S1<T>::S2'. We must fill in the missing
9601 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9602 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9603 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9606 /* Now we should have enough arguments. */
9607 gcc_assert (parm_depth
== arg_depth
);
9609 /* From here on, we're only interested in the most general
9612 /* Calculate the BOUND_ARGS. These will be the args that are
9613 actually tsubst'd into the definition to create the
9615 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9617 /*require_all_args=*/true,
9618 /*use_default_args=*/true);
9620 if (arglist
== error_mark_node
)
9621 /* We were unable to bind the arguments. */
9622 return error_mark_node
;
9624 /* In the scope of a template class, explicit references to the
9625 template class refer to the type of the template, not any
9626 instantiation of it. For example, in:
9628 template <class T> class C { void f(C<T>); }
9630 the `C<T>' is just the same as `C'. Outside of the
9631 class, however, such a reference is an instantiation. */
9633 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9634 || currently_open_class (template_type
))
9636 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9638 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9639 return template_type
;
9642 /* If we already have this specialization, return it. */
9643 elt
.tmpl
= gen_tmpl
;
9645 elt
.spec
= NULL_TREE
;
9646 hash
= spec_hasher::hash (&elt
);
9647 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9652 /* If the the template's constraints are not satisfied,
9653 then we cannot form a valid type.
9655 Note that the check is deferred until after the hash
9656 lookup. This prevents redundant checks on previously
9657 instantiated specializations. */
9658 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9660 if (complain
& tf_error
)
9662 auto_diagnostic_group d
;
9663 error ("template constraint failure for %qD", gen_tmpl
);
9664 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9666 return error_mark_node
;
9669 is_dependent_type
= uses_template_parms (arglist
);
9671 /* If the deduced arguments are invalid, then the binding
9673 if (!is_dependent_type
9674 && check_instantiated_args (gen_tmpl
,
9675 INNERMOST_TEMPLATE_ARGS (arglist
),
9677 return error_mark_node
;
9679 if (!is_dependent_type
9680 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9681 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9682 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9684 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9685 DECL_NAME (gen_tmpl
),
9686 /*tag_scope=*/ts_global
);
9690 context
= DECL_CONTEXT (gen_tmpl
);
9691 if (context
&& TYPE_P (context
))
9693 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9694 context
= complete_type (context
);
9697 context
= tsubst (context
, arglist
, complain
, in_decl
);
9699 if (context
== error_mark_node
)
9700 return error_mark_node
;
9703 context
= global_namespace
;
9705 /* Create the type. */
9706 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9708 /* The user referred to a specialization of an alias
9709 template represented by GEN_TMPL.
9711 [temp.alias]/2 says:
9713 When a template-id refers to the specialization of an
9714 alias template, it is equivalent to the associated
9715 type obtained by substitution of its
9716 template-arguments for the template-parameters in the
9717 type-id of the alias template. */
9719 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9720 /* Note that the call above (by indirectly calling
9721 register_specialization in tsubst_decl) registers the
9722 TYPE_DECL representing the specialization of the alias
9723 template. So next time someone substitutes ARGLIST for
9724 the template parms into the alias template (GEN_TMPL),
9725 she'll get that TYPE_DECL back. */
9727 if (t
== error_mark_node
)
9730 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9732 if (!is_dependent_type
)
9734 set_current_access_from_decl (TYPE_NAME (template_type
));
9735 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9736 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9737 arglist
, complain
, in_decl
),
9738 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9739 arglist
, complain
, in_decl
),
9740 SCOPED_ENUM_P (template_type
), NULL
);
9742 if (t
== error_mark_node
)
9747 /* We don't want to call start_enum for this type, since
9748 the values for the enumeration constants may involve
9749 template parameters. And, no one should be interested
9750 in the enumeration constants for such a type. */
9751 t
= cxx_make_type (ENUMERAL_TYPE
);
9752 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9754 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9755 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9756 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9758 else if (CLASS_TYPE_P (template_type
))
9760 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9761 instantiated here. */
9762 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9764 t
= make_class_type (TREE_CODE (template_type
));
9765 CLASSTYPE_DECLARED_CLASS (t
)
9766 = CLASSTYPE_DECLARED_CLASS (template_type
);
9767 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9769 /* A local class. Make sure the decl gets registered properly. */
9770 if (context
== current_function_decl
)
9771 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9773 return error_mark_node
;
9775 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9776 /* This instantiation is another name for the primary
9777 template type. Set the TYPE_CANONICAL field
9779 TYPE_CANONICAL (t
) = template_type
;
9780 else if (any_template_arguments_need_structural_equality_p (arglist
))
9781 /* Some of the template arguments require structural
9782 equality testing, so this template class requires
9783 structural equality testing. */
9784 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9789 /* If we called start_enum or pushtag above, this information
9790 will already be set up. */
9793 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9795 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9796 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9797 DECL_SOURCE_LOCATION (type_decl
)
9798 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9801 type_decl
= TYPE_NAME (t
);
9803 if (CLASS_TYPE_P (template_type
))
9805 TREE_PRIVATE (type_decl
)
9806 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9807 TREE_PROTECTED (type_decl
)
9808 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9809 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9811 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9812 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9816 if (OVERLOAD_TYPE_P (t
)
9817 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9819 static const char *tags
[] = {"abi_tag", "may_alias"};
9821 for (unsigned ix
= 0; ix
!= 2; ix
++)
9824 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9828 = tree_cons (TREE_PURPOSE (attributes
),
9829 TREE_VALUE (attributes
),
9830 TYPE_ATTRIBUTES (t
));
9834 /* Let's consider the explicit specialization of a member
9835 of a class template specialization that is implicitly instantiated,
9840 template<class U> struct M {}; //#0
9845 struct S<int>::M<char> //#1
9849 [temp.expl.spec]/4 says this is valid.
9851 In this case, when we write:
9854 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9857 When we encounter #1, we want to store the partial instantiation
9858 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9860 For all cases other than this "explicit specialization of member of a
9861 class template", we just want to store the most general template into
9862 the CLASSTYPE_TI_TEMPLATE of M.
9864 This case of "explicit specialization of member of a class template"
9866 1/ the enclosing class is an instantiation of, and therefore not
9867 the same as, the context of the most general template, and
9868 2/ we aren't looking at the partial instantiation itself, i.e.
9869 the innermost arguments are not the same as the innermost parms of
9870 the most general template.
9872 So it's only when 1/ and 2/ happens that we want to use the partial
9873 instantiation of the member template in lieu of its most general
9876 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9877 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9878 /* the enclosing class must be an instantiation... */
9879 && CLASS_TYPE_P (context
)
9880 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9882 TREE_VEC_LENGTH (arglist
)--;
9883 ++processing_template_decl
;
9884 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9885 tree partial_inst_args
=
9886 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9887 arglist
, complain
, NULL_TREE
);
9888 --processing_template_decl
;
9889 TREE_VEC_LENGTH (arglist
)++;
9890 if (partial_inst_args
== error_mark_node
)
9891 return error_mark_node
;
9892 use_partial_inst_tmpl
=
9893 /*...and we must not be looking at the partial instantiation
9895 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9899 if (!use_partial_inst_tmpl
)
9900 /* This case is easy; there are no member templates involved. */
9904 /* This is a full instantiation of a member template. Find
9905 the partial instantiation of which this is an instance. */
9907 /* Temporarily reduce by one the number of levels in the ARGLIST
9908 so as to avoid comparing the last set of arguments. */
9909 TREE_VEC_LENGTH (arglist
)--;
9910 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9911 TREE_VEC_LENGTH (arglist
)++;
9912 /* FOUND is either a proper class type, or an alias
9913 template specialization. In the later case, it's a
9914 TYPE_DECL, resulting from the substituting of arguments
9915 for parameters in the TYPE_DECL of the alias template
9916 done earlier. So be careful while getting the template
9918 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9920 : (TREE_CODE (found
) == TYPE_DECL
9921 ? DECL_TI_TEMPLATE (found
)
9922 : CLASSTYPE_TI_TEMPLATE (found
)));
9924 if (DECL_CLASS_TEMPLATE_P (found
)
9925 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
9927 /* If this partial instantiation is specialized, we want to
9928 use it for hash table lookup. */
9930 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
9931 hash
= spec_hasher::hash (&elt
);
9935 // Build template info for the new specialization.
9936 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9939 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9940 gcc_checking_assert (*slot
== NULL
);
9941 entry
= ggc_alloc
<spec_entry
> ();
9945 /* Note this use of the partial instantiation so we can check it
9946 later in maybe_process_partial_specialization. */
9947 DECL_TEMPLATE_INSTANTIATIONS (found
)
9948 = tree_cons (arglist
, t
,
9949 DECL_TEMPLATE_INSTANTIATIONS (found
));
9951 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9952 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9953 /* Now that the type has been registered on the instantiations
9954 list, we set up the enumerators. Because the enumeration
9955 constants may involve the enumeration type itself, we make
9956 sure to register the type first, and then create the
9957 constants. That way, doing tsubst_expr for the enumeration
9958 constants won't result in recursive calls here; we'll find
9959 the instantiation and exit above. */
9960 tsubst_enum (template_type
, t
, arglist
);
9962 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9963 /* If the type makes use of template parameters, the
9964 code that generates debugging information will crash. */
9965 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9967 /* Possibly limit visibility based on template args. */
9968 TREE_PUBLIC (type_decl
) = 1;
9969 determine_visibility (type_decl
);
9971 inherit_targ_abi_tags (t
);
9977 /* Wrapper for lookup_template_class_1. */
9980 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9981 int entering_scope
, tsubst_flags_t complain
)
9984 timevar_push (TV_TEMPLATE_INST
);
9985 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9986 entering_scope
, complain
);
9987 timevar_pop (TV_TEMPLATE_INST
);
9991 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9994 lookup_template_variable (tree templ
, tree arglist
)
9996 if (flag_concepts
&& variable_concept_p (templ
))
9997 return build_concept_check (templ
, arglist
, tf_none
);
9999 /* The type of the expression is NULL_TREE since the template-id could refer
10000 to an explicit or partial specialization. */
10001 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10004 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10007 finish_template_variable (tree var
, tsubst_flags_t complain
)
10009 tree templ
= TREE_OPERAND (var
, 0);
10010 tree arglist
= TREE_OPERAND (var
, 1);
10012 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10013 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10015 templ
= most_general_template (templ
);
10016 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10017 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10019 /*use_default*/true);
10021 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10023 if (complain
& tf_error
)
10025 auto_diagnostic_group d
;
10026 error ("use of invalid variable template %qE", var
);
10027 diagnose_constraints (location_of (var
), templ
, arglist
);
10029 return error_mark_node
;
10032 return instantiate_template (templ
, arglist
, complain
);
10035 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10036 TARGS template args, and instantiate it if it's not dependent. */
10039 lookup_and_finish_template_variable (tree templ
, tree targs
,
10040 tsubst_flags_t complain
)
10042 templ
= lookup_template_variable (templ
, targs
);
10043 if (!any_dependent_template_arguments_p (targs
))
10045 templ
= finish_template_variable (templ
, complain
);
10049 return convert_from_reference (templ
);
10053 struct pair_fn_data
10058 /* True when we should also visit template parameters that occur in
10059 non-deduced contexts. */
10060 bool include_nondeduced_p
;
10061 hash_set
<tree
> *visited
;
10064 /* Called from for_each_template_parm via walk_tree. */
10067 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10070 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10071 tree_fn_t fn
= pfd
->fn
;
10072 void *data
= pfd
->data
;
10073 tree result
= NULL_TREE
;
10075 #define WALK_SUBTREE(NODE) \
10078 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10079 pfd->include_nondeduced_p, \
10081 if (result) goto out; \
10085 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10089 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10090 WALK_SUBTREE (TYPE_CONTEXT (t
));
10092 switch (TREE_CODE (t
))
10095 if (TYPE_PTRMEMFUNC_P (t
))
10097 /* Fall through. */
10100 case ENUMERAL_TYPE
:
10101 if (!TYPE_TEMPLATE_INFO (t
))
10102 *walk_subtrees
= 0;
10104 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10108 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10109 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10113 /* Since we're not going to walk subtrees, we have to do this
10114 explicitly here. */
10115 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10116 /* Fall through. */
10118 case FUNCTION_TYPE
:
10119 /* Check the return type. */
10120 WALK_SUBTREE (TREE_TYPE (t
));
10122 /* Check the parameter types. Since default arguments are not
10123 instantiated until they are needed, the TYPE_ARG_TYPES may
10124 contain expressions that involve template parameters. But,
10125 no-one should be looking at them yet. And, once they're
10126 instantiated, they don't contain template parameters, so
10127 there's no point in looking at them then, either. */
10131 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10132 WALK_SUBTREE (TREE_VALUE (parm
));
10134 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10135 want walk_tree walking into them itself. */
10136 *walk_subtrees
= 0;
10139 if (flag_noexcept_type
)
10141 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10143 WALK_SUBTREE (TREE_PURPOSE (spec
));
10148 case DECLTYPE_TYPE
:
10149 case UNDERLYING_TYPE
:
10150 if (pfd
->include_nondeduced_p
10151 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10153 pfd
->include_nondeduced_p
,
10155 return error_mark_node
;
10156 *walk_subtrees
= false;
10159 case FUNCTION_DECL
:
10161 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10162 WALK_SUBTREE (DECL_TI_ARGS (t
));
10163 /* Fall through. */
10167 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10168 WALK_SUBTREE (DECL_INITIAL (t
));
10169 if (DECL_CONTEXT (t
)
10170 && pfd
->include_nondeduced_p
)
10171 WALK_SUBTREE (DECL_CONTEXT (t
));
10174 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10175 /* Record template parameters such as `T' inside `TT<T>'. */
10176 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10177 /* Fall through. */
10179 case TEMPLATE_TEMPLATE_PARM
:
10180 case TEMPLATE_TYPE_PARM
:
10181 case TEMPLATE_PARM_INDEX
:
10182 if (fn
&& (*fn
)(t
, data
))
10188 case TEMPLATE_DECL
:
10189 /* A template template parameter is encountered. */
10190 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10191 WALK_SUBTREE (TREE_TYPE (t
));
10193 /* Already substituted template template parameter */
10194 *walk_subtrees
= 0;
10197 case TYPENAME_TYPE
:
10198 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10199 partial instantiation. */
10200 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10204 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10205 && pfd
->include_nondeduced_p
)
10206 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10210 case COMPONENT_REF
:
10211 /* If there's no type, then this thing must be some expression
10212 involving template parameters. */
10213 if (!fn
&& !TREE_TYPE (t
))
10214 return error_mark_node
;
10219 case IMPLICIT_CONV_EXPR
:
10220 case REINTERPRET_CAST_EXPR
:
10221 case CONST_CAST_EXPR
:
10222 case STATIC_CAST_EXPR
:
10223 case DYNAMIC_CAST_EXPR
:
10227 case PSEUDO_DTOR_EXPR
:
10229 return error_mark_node
;
10233 if (pfd
->include_nondeduced_p
)
10234 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10237 case REQUIRES_EXPR
:
10240 return error_mark_node
;
10242 /* Recursively walk the type of each constraint variable. */
10243 tree p
= TREE_OPERAND (t
, 0);
10246 WALK_SUBTREE (TREE_TYPE (p
));
10247 p
= TREE_CHAIN (p
);
10256 #undef WALK_SUBTREE
10258 /* We didn't find any template parameters we liked. */
10263 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10264 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10265 call FN with the parameter and the DATA.
10266 If FN returns nonzero, the iteration is terminated, and
10267 for_each_template_parm returns 1. Otherwise, the iteration
10268 continues. If FN never returns a nonzero value, the value
10269 returned by for_each_template_parm is 0. If FN is NULL, it is
10270 considered to be the function which always returns 1.
10272 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10273 parameters that occur in non-deduced contexts. When false, only
10274 visits those template parameters that can be deduced. */
10277 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10278 hash_set
<tree
> *visited
,
10279 bool include_nondeduced_p
,
10282 struct pair_fn_data pfd
;
10287 pfd
.any_fn
= any_fn
;
10289 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10291 /* Walk the tree. (Conceptually, we would like to walk without
10292 duplicates, but for_each_template_parm_r recursively calls
10293 for_each_template_parm, so we would need to reorganize a fair
10294 bit to use walk_tree_without_duplicates, so we keep our own
10297 pfd
.visited
= visited
;
10299 pfd
.visited
= new hash_set
<tree
>;
10300 result
= cp_walk_tree (&t
,
10301 for_each_template_parm_r
,
10308 delete pfd
.visited
;
10315 struct find_template_parameter_info
10317 explicit find_template_parameter_info (int d
)
10321 hash_set
<tree
> visited
;
10322 hash_set
<tree
> parms
;
10326 /* Appends the declaration of T to the list in DATA. */
10329 keep_template_parm (tree t
, void* data
)
10331 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10333 /* Template parameters declared within the expression are not part of
10334 the parameter mapping. For example, in this concept:
10336 template<typename T>
10337 concept C = requires { <expr> } -> same_as<int>;
10339 the return specifier same_as<int> declares a new decltype parameter
10340 that must not be part of the parameter mapping. The same is true
10341 for generic lambda parameters, lambda template parameters, etc. */
10344 template_parm_level_and_index (t
, &level
, &index
);
10345 if (level
> ftpi
->max_depth
)
10348 /* Arguments like const T yield parameters like const T. This means that
10349 a template-id like X<T, const T> would yield two distinct parameters:
10350 T and const T. Adjust types to their unqualified versions. */
10352 t
= TYPE_MAIN_VARIANT (t
);
10353 ftpi
->parms
.add (t
);
10358 /* Ensure that we recursively examine certain terms that are not normally
10359 visited in for_each_template_parm_r. */
10362 any_template_parm_r (tree t
, void *data
)
10364 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10366 #define WALK_SUBTREE(NODE) \
10369 for_each_template_parm (NODE, keep_template_parm, data, \
10370 &ftpi->visited, true, \
10371 any_template_parm_r); \
10375 switch (TREE_CODE (t
))
10379 case ENUMERAL_TYPE
:
10380 /* Search for template parameters in type aliases. */
10381 if (alias_template_specialization_p (t
))
10383 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
10384 WALK_SUBTREE (TI_ARGS (tinfo
));
10388 case TEMPLATE_TYPE_PARM
:
10389 /* Type constraints of a placeholder type may contain parameters. */
10391 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10392 WALK_SUBTREE (constr
);
10395 case TEMPLATE_ID_EXPR
:
10396 /* Search through references to variable templates. */
10397 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10398 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10403 WALK_SUBTREE (TREE_TYPE (t
));
10407 /* A parameter or constraint variable may also depend on a template
10408 parameter without explicitly naming it. */
10409 WALK_SUBTREE (TREE_TYPE (t
));
10416 /* Keep walking. */
10420 /* Returns a list of unique template parameters found within T. */
10423 find_template_parameters (tree t
, int depth
)
10425 find_template_parameter_info
ftpi (depth
);
10426 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10427 /*include_nondeduced*/true, any_template_parm_r
);
10428 tree list
= NULL_TREE
;
10429 for (hash_set
<tree
>::iterator iter
= ftpi
.parms
.begin();
10430 iter
!= ftpi
.parms
.end(); ++iter
)
10431 list
= tree_cons (NULL_TREE
, *iter
, list
);
10435 /* Returns true if T depends on any template parameter. */
10438 uses_template_parms (tree t
)
10440 if (t
== NULL_TREE
)
10444 int saved_processing_template_decl
;
10446 saved_processing_template_decl
= processing_template_decl
;
10447 if (!saved_processing_template_decl
)
10448 processing_template_decl
= 1;
10450 dependent_p
= dependent_type_p (t
);
10451 else if (TREE_CODE (t
) == TREE_VEC
)
10452 dependent_p
= any_dependent_template_arguments_p (t
);
10453 else if (TREE_CODE (t
) == TREE_LIST
)
10454 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10455 || uses_template_parms (TREE_CHAIN (t
)));
10456 else if (TREE_CODE (t
) == TYPE_DECL
)
10457 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10458 else if (DECL_P (t
)
10460 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10461 || TREE_CODE (t
) == OVERLOAD
10463 || identifier_p (t
)
10464 || TREE_CODE (t
) == TRAIT_EXPR
10465 || TREE_CODE (t
) == CONSTRUCTOR
10466 || CONSTANT_CLASS_P (t
))
10467 dependent_p
= (type_dependent_expression_p (t
)
10468 || value_dependent_expression_p (t
));
10471 gcc_assert (t
== error_mark_node
);
10472 dependent_p
= false;
10475 processing_template_decl
= saved_processing_template_decl
;
10477 return dependent_p
;
10480 /* Returns true iff current_function_decl is an incompletely instantiated
10481 template. Useful instead of processing_template_decl because the latter
10482 is set to 0 during instantiate_non_dependent_expr. */
10485 in_template_function (void)
10487 tree fn
= current_function_decl
;
10489 ++processing_template_decl
;
10490 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10491 && DECL_TEMPLATE_INFO (fn
)
10492 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10493 --processing_template_decl
;
10497 /* Returns true if T depends on any template parameter with level LEVEL. */
10500 uses_template_parms_level (tree t
, int level
)
10502 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10503 /*include_nondeduced_p=*/true);
10506 /* Returns true if the signature of DECL depends on any template parameter from
10507 its enclosing class. */
10510 uses_outer_template_parms (tree decl
)
10512 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10515 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10516 &depth
, NULL
, /*include_nondeduced_p=*/true))
10518 if (PRIMARY_TEMPLATE_P (decl
)
10519 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10520 (DECL_TEMPLATE_PARMS (decl
)),
10521 template_parm_outer_level
,
10522 &depth
, NULL
, /*include_nondeduced_p=*/true))
10524 tree ci
= get_constraints (decl
);
10526 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10527 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10528 &depth
, NULL
, /*nondeduced*/true))
10533 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10534 ill-formed translation unit, i.e. a variable or function that isn't
10535 usable in a constant expression. */
10538 neglectable_inst_p (tree d
)
10540 return (d
&& DECL_P (d
)
10541 && !undeduced_auto_decl (d
)
10542 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10543 : decl_maybe_constant_var_p (d
)));
10546 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10547 neglectable and instantiated from within an erroneous instantiation. */
10550 limit_bad_template_recursion (tree decl
)
10552 struct tinst_level
*lev
= current_tinst_level
;
10553 int errs
= errorcount
+ sorrycount
;
10554 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10557 for (; lev
; lev
= lev
->next
)
10558 if (neglectable_inst_p (lev
->maybe_get_node ()))
10561 return (lev
&& errs
> lev
->errors
);
10564 static int tinst_depth
;
10565 extern int max_tinst_depth
;
10568 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10570 /* We're starting to instantiate D; record the template instantiation context
10571 at LOC for diagnostics and to restore it later. */
10574 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10576 struct tinst_level
*new_level
;
10578 if (tinst_depth
>= max_tinst_depth
)
10580 /* Tell error.c not to try to instantiate any templates. */
10582 fatal_error (input_location
,
10583 "template instantiation depth exceeds maximum of %d"
10584 " (use %<-ftemplate-depth=%> to increase the maximum)",
10589 /* If the current instantiation caused problems, don't let it instantiate
10590 anything else. Do allow deduction substitution and decls usable in
10591 constant expressions. */
10592 if (!targs
&& limit_bad_template_recursion (tldcl
))
10595 /* When not -quiet, dump template instantiations other than functions, since
10596 announce_function will take care of those. */
10597 if (!quiet_flag
&& !targs
10598 && TREE_CODE (tldcl
) != TREE_LIST
10599 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10600 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10602 new_level
= tinst_level_freelist ().alloc ();
10603 new_level
->tldcl
= tldcl
;
10604 new_level
->targs
= targs
;
10605 new_level
->locus
= loc
;
10606 new_level
->errors
= errorcount
+ sorrycount
;
10607 new_level
->next
= NULL
;
10608 new_level
->refcount
= 0;
10609 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10610 set_refcount_ptr (current_tinst_level
, new_level
);
10613 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10614 depth_reached
= tinst_depth
;
10619 /* We're starting substitution of TMPL<ARGS>; record the template
10620 substitution context for diagnostics and to restore it later. */
10623 push_tinst_level (tree tmpl
, tree args
)
10625 return push_tinst_level_loc (tmpl
, args
, input_location
);
10628 /* We're starting to instantiate D; record INPUT_LOCATION and the
10629 template instantiation context for diagnostics and to restore it
10633 push_tinst_level (tree d
)
10635 return push_tinst_level_loc (d
, input_location
);
10638 /* Likewise, but record LOC as the program location. */
10641 push_tinst_level_loc (tree d
, location_t loc
)
10643 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10644 return push_tinst_level_loc (d
, NULL
, loc
);
10647 /* We're done instantiating this template; return to the instantiation
10651 pop_tinst_level (void)
10653 /* Restore the filename and line number stashed away when we started
10654 this instantiation. */
10655 input_location
= current_tinst_level
->locus
;
10656 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10660 /* We're instantiating a deferred template; restore the template
10661 instantiation context in which the instantiation was requested, which
10662 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10665 reopen_tinst_level (struct tinst_level
*level
)
10667 struct tinst_level
*t
;
10670 for (t
= level
; t
; t
= t
->next
)
10673 set_refcount_ptr (current_tinst_level
, level
);
10674 pop_tinst_level ();
10675 if (current_tinst_level
)
10676 current_tinst_level
->errors
= errorcount
+sorrycount
;
10677 return level
->maybe_get_node ();
10680 /* Returns the TINST_LEVEL which gives the original instantiation
10683 struct tinst_level
*
10684 outermost_tinst_level (void)
10686 struct tinst_level
*level
= current_tinst_level
;
10688 while (level
->next
)
10689 level
= level
->next
;
10693 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10694 vector of template arguments, as for tsubst.
10696 Returns an appropriate tsubst'd friend declaration. */
10699 tsubst_friend_function (tree decl
, tree args
)
10703 if (TREE_CODE (decl
) == FUNCTION_DECL
10704 && DECL_TEMPLATE_INSTANTIATION (decl
)
10705 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10706 /* This was a friend declared with an explicit template
10707 argument list, e.g.:
10709 friend void f<>(T);
10711 to indicate that f was a template instantiation, not a new
10712 function declaration. Now, we have to figure out what
10713 instantiation of what template. */
10715 tree template_id
, arglist
, fns
;
10718 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10720 /* Friend functions are looked up in the containing namespace scope.
10721 We must enter that scope, to avoid finding member functions of the
10722 current class with same name. */
10723 push_nested_namespace (ns
);
10724 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10725 tf_warning_or_error
, NULL_TREE
,
10726 /*integral_constant_expression_p=*/false);
10727 pop_nested_namespace (ns
);
10728 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10729 tf_warning_or_error
, NULL_TREE
);
10730 template_id
= lookup_template_function (fns
, arglist
);
10732 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10733 tmpl
= determine_specialization (template_id
, new_friend
,
10735 /*need_member_template=*/0,
10736 TREE_VEC_LENGTH (args
),
10738 return instantiate_template (tmpl
, new_args
, tf_error
);
10741 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10743 /* The NEW_FRIEND will look like an instantiation, to the
10744 compiler, but is not an instantiation from the point of view of
10745 the language. For example, we might have had:
10747 template <class T> struct S {
10748 template <class U> friend void f(T, U);
10751 Then, in S<int>, template <class U> void f(int, U) is not an
10752 instantiation of anything. */
10753 if (new_friend
== error_mark_node
)
10754 return error_mark_node
;
10756 DECL_USE_TEMPLATE (new_friend
) = 0;
10757 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10759 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10760 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10761 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10763 /* Attach the template requirements to the new declaration
10764 for declaration matching. We need to rebuild the requirements
10765 so that parameter levels match. */
10766 if (tree ci
= get_constraints (decl
))
10768 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10769 tree args
= generic_targs_for (new_friend
);
10770 tree treqs
= tsubst_constraint (CI_TEMPLATE_REQS (ci
), args
,
10771 tf_warning_or_error
, NULL_TREE
);
10772 tree freqs
= tsubst_constraint (CI_DECLARATOR_REQS (ci
), args
,
10773 tf_warning_or_error
, NULL_TREE
);
10775 /* Update the constraints -- these won't really be valid for
10776 checking, but that's not what we need them for. These ensure
10777 that the declared function can find the friend during
10778 declaration matching. */
10779 tree new_ci
= get_constraints (new_friend
);
10780 CI_TEMPLATE_REQS (new_ci
) = treqs
;
10781 CI_DECLARATOR_REQS (new_ci
) = freqs
;
10783 /* Also update the template parameter list. */
10784 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10788 /* The mangled name for the NEW_FRIEND is incorrect. The function
10789 is not a template instantiation and should not be mangled like
10790 one. Therefore, we forget the mangling here; we'll recompute it
10791 later if we need it. */
10792 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10794 SET_DECL_RTL (new_friend
, NULL
);
10795 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10798 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10801 tree new_friend_template_info
;
10802 tree new_friend_result_template_info
;
10804 int new_friend_is_defn
;
10806 /* We must save some information from NEW_FRIEND before calling
10807 duplicate decls since that function will free NEW_FRIEND if
10809 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10810 new_friend_is_defn
=
10811 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10812 (template_for_substitution (new_friend
)))
10814 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10816 /* This declaration is a `primary' template. */
10817 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10819 new_friend_result_template_info
10820 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10823 new_friend_result_template_info
= NULL_TREE
;
10825 /* Inside pushdecl_namespace_level, we will push into the
10826 current namespace. However, the friend function should go
10827 into the namespace of the template. */
10828 ns
= decl_namespace_context (new_friend
);
10829 push_nested_namespace (ns
);
10830 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10831 pop_nested_namespace (ns
);
10833 if (old_decl
== error_mark_node
)
10834 return error_mark_node
;
10836 if (old_decl
!= new_friend
)
10838 /* This new friend declaration matched an existing
10839 declaration. For example, given:
10841 template <class T> void f(T);
10842 template <class U> class C {
10843 template <class T> friend void f(T) {}
10846 the friend declaration actually provides the definition
10847 of `f', once C has been instantiated for some type. So,
10848 old_decl will be the out-of-class template declaration,
10849 while new_friend is the in-class definition.
10851 But, if `f' was called before this point, the
10852 instantiation of `f' will have DECL_TI_ARGS corresponding
10853 to `T' but not to `U', references to which might appear
10854 in the definition of `f'. Previously, the most general
10855 template for an instantiation of `f' was the out-of-class
10856 version; now it is the in-class version. Therefore, we
10857 run through all specialization of `f', adding to their
10858 DECL_TI_ARGS appropriately. In particular, they need a
10859 new set of outer arguments, corresponding to the
10860 arguments for this class instantiation.
10862 The same situation can arise with something like this:
10864 friend void f(int);
10865 template <class T> class C {
10866 friend void f(T) {}
10869 when `C<int>' is instantiated. Now, `f(int)' is defined
10872 if (!new_friend_is_defn
)
10873 /* On the other hand, if the in-class declaration does
10874 *not* provide a definition, then we don't want to alter
10875 existing definitions. We can just leave everything
10880 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10881 tree new_args
= TI_ARGS (new_friend_template_info
);
10883 /* Overwrite whatever template info was there before, if
10884 any, with the new template information pertaining to
10885 the declaration. */
10886 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10888 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10890 /* We should have called reregister_specialization in
10891 duplicate_decls. */
10892 gcc_assert (retrieve_specialization (new_template
,
10896 /* Instantiate it if the global has already been used. */
10897 if (DECL_ODR_USED (old_decl
))
10898 instantiate_decl (old_decl
, /*defer_ok=*/true,
10899 /*expl_inst_class_mem_p=*/false);
10905 /* Indicate that the old function template is a partial
10907 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10908 = new_friend_result_template_info
;
10910 gcc_assert (new_template
10911 == most_general_template (new_template
));
10912 gcc_assert (new_template
!= old_decl
);
10914 /* Reassign any specializations already in the hash table
10915 to the new more general template, and add the
10916 additional template args. */
10917 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10919 t
= TREE_CHAIN (t
))
10921 tree spec
= TREE_VALUE (t
);
10924 elt
.tmpl
= old_decl
;
10925 elt
.args
= DECL_TI_ARGS (spec
);
10926 elt
.spec
= NULL_TREE
;
10928 decl_specializations
->remove_elt (&elt
);
10930 DECL_TI_ARGS (spec
)
10931 = add_outermost_template_args (new_args
,
10932 DECL_TI_ARGS (spec
));
10934 register_specialization
10935 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10938 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10942 /* The information from NEW_FRIEND has been merged into OLD_DECL
10943 by duplicate_decls. */
10944 new_friend
= old_decl
;
10949 tree context
= DECL_CONTEXT (new_friend
);
10953 template <class T> class C {
10954 template <class U> friend void C1<U>::f (); // case 1
10955 friend void C2<T>::f (); // case 2
10957 we only need to make sure CONTEXT is a complete type for
10958 case 2. To distinguish between the two cases, we note that
10959 CONTEXT of case 1 remains dependent type after tsubst while
10960 this isn't true for case 2. */
10961 ++processing_template_decl
;
10962 dependent_p
= dependent_type_p (context
);
10963 --processing_template_decl
;
10966 && !complete_type_or_else (context
, NULL_TREE
))
10967 return error_mark_node
;
10969 if (COMPLETE_TYPE_P (context
))
10971 tree fn
= new_friend
;
10972 /* do_friend adds the TEMPLATE_DECL for any member friend
10973 template even if it isn't a member template, i.e.
10974 template <class T> friend A<T>::f();
10975 Look through it in that case. */
10976 if (TREE_CODE (fn
) == TEMPLATE_DECL
10977 && !PRIMARY_TEMPLATE_P (fn
))
10978 fn
= DECL_TEMPLATE_RESULT (fn
);
10979 /* Check to see that the declaration is really present, and,
10980 possibly obtain an improved declaration. */
10981 fn
= check_classfn (context
, fn
, NULL_TREE
);
10991 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10992 template arguments, as for tsubst.
10994 Returns an appropriate tsubst'd friend type or error_mark_node on
10998 tsubst_friend_class (tree friend_tmpl
, tree args
)
11002 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11004 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11005 return TREE_TYPE (tmpl
);
11008 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11009 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11010 push_nested_namespace (context
);
11013 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11014 push_nested_class (context
);
11017 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11018 /*non_class=*/false, /*block_p=*/false,
11019 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11021 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11023 /* The friend template has already been declared. Just
11024 check to see that the declarations match, and install any new
11025 default parameters. We must tsubst the default parameters,
11026 of course. We only need the innermost template parameters
11027 because that is all that redeclare_class_template will look
11029 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11030 > TMPL_ARGS_DEPTH (args
))
11032 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11033 args
, tf_warning_or_error
);
11034 location_t saved_input_location
= input_location
;
11035 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11036 tree cons
= get_constraints (tmpl
);
11037 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11038 input_location
= saved_input_location
;
11043 /* The friend template has not already been declared. In this
11044 case, the instantiation of the template class will cause the
11045 injection of this template into the namespace scope. */
11046 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11048 if (tmpl
!= error_mark_node
)
11050 /* The new TMPL is not an instantiation of anything, so we
11051 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11052 for the new type because that is supposed to be the
11053 corresponding template decl, i.e., TMPL. */
11054 DECL_USE_TEMPLATE (tmpl
) = 0;
11055 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11056 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11057 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11058 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11060 /* It is hidden. */
11061 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11062 DECL_ANTICIPATED (tmpl
)
11063 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11065 /* Inject this template into the enclosing namspace scope. */
11066 tmpl
= pushdecl_namespace_level (tmpl
, true);
11070 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11071 pop_nested_namespace (context
);
11073 pop_nested_class ();
11075 return TREE_TYPE (tmpl
);
11078 /* Returns zero if TYPE cannot be completed later due to circularity.
11079 Otherwise returns one. */
11082 can_complete_type_without_circularity (tree type
)
11084 if (type
== NULL_TREE
|| type
== error_mark_node
)
11086 else if (COMPLETE_TYPE_P (type
))
11088 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11089 return can_complete_type_without_circularity (TREE_TYPE (type
));
11090 else if (CLASS_TYPE_P (type
)
11091 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11097 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11098 tsubst_flags_t
, tree
);
11100 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11101 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11104 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11105 tsubst_flags_t complain
, tree in_decl
)
11107 gcc_assert (ATTR_IS_DEPENDENT (t
));
11109 tree val
= TREE_VALUE (t
);
11110 if (val
== NULL_TREE
)
11111 /* Nothing to do. */;
11112 else if ((flag_openmp
|| flag_openmp_simd
)
11113 && is_attribute_p ("omp declare simd",
11114 get_attribute_name (t
)))
11116 tree clauses
= TREE_VALUE (val
);
11117 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11118 complain
, in_decl
);
11119 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11120 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11121 tree parms
= DECL_ARGUMENTS (*decl_p
);
11123 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11125 val
= build_tree_list (NULL_TREE
, clauses
);
11129 else if (flag_openmp
11130 && is_attribute_p ("omp declare variant base",
11131 get_attribute_name (t
)))
11133 ++cp_unevaluated_operand
;
11135 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11136 in_decl
, /*integral_constant_expression_p=*/false);
11137 --cp_unevaluated_operand
;
11138 tree chain
= TREE_CHAIN (val
);
11139 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11140 tree ctx
= copy_list (TREE_VALUE (val
));
11141 tree simd
= get_identifier ("simd");
11142 tree score
= get_identifier (" score");
11143 tree condition
= get_identifier ("condition");
11144 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11146 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11147 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11148 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11150 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11152 tree clauses
= TREE_VALUE (t2
);
11153 clauses
= tsubst_omp_clauses (clauses
,
11154 C_ORT_OMP_DECLARE_SIMD
, args
,
11155 complain
, in_decl
);
11156 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11157 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11158 TREE_VALUE (t2
) = clauses
;
11162 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11163 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11164 if (TREE_VALUE (t3
))
11167 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11168 && TREE_PURPOSE (t3
) != score
);
11169 tree v
= TREE_VALUE (t3
);
11170 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11172 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11173 v
= fold_non_dependent_expr (v
);
11174 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11175 || (TREE_PURPOSE (t3
) == score
11176 ? TREE_CODE (v
) != INTEGER_CST
11177 : !tree_fits_shwi_p (v
)))
11180 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11182 if (TREE_PURPOSE (t3
) == score
)
11183 error_at (loc
, "score argument must be "
11184 "constant integer expression");
11185 else if (allow_string
)
11186 error_at (loc
, "property must be constant "
11187 "integer expression or string "
11190 error_at (loc
, "property must be constant "
11191 "integer expression");
11194 else if (TREE_PURPOSE (t3
) == score
11195 && tree_int_cst_sgn (v
) < 0)
11198 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11200 error_at (loc
, "score argument must be "
11204 TREE_VALUE (t3
) = v
;
11209 val
= tree_cons (varid
, ctx
, chain
);
11211 /* If the first attribute argument is an identifier, don't
11212 pass it through tsubst. Attributes like mode, format,
11213 cleanup and several target specific attributes expect it
11215 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11218 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11219 /*integral_constant_expression_p=*/false);
11220 if (chain
!= TREE_CHAIN (val
))
11221 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11223 else if (PACK_EXPANSION_P (val
))
11225 /* An attribute pack expansion. */
11226 tree purp
= TREE_PURPOSE (t
);
11227 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11228 if (pack
== error_mark_node
)
11229 return error_mark_node
;
11230 int len
= TREE_VEC_LENGTH (pack
);
11231 tree list
= NULL_TREE
;
11233 for (int i
= 0; i
< len
; ++i
)
11235 tree elt
= TREE_VEC_ELT (pack
, i
);
11236 *q
= build_tree_list (purp
, elt
);
11237 q
= &TREE_CHAIN (*q
);
11242 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11243 /*integral_constant_expression_p=*/false);
11245 if (val
!= TREE_VALUE (t
))
11246 return build_tree_list (TREE_PURPOSE (t
), val
);
11250 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11251 unchanged or a new TREE_LIST chain. */
11254 tsubst_attributes (tree attributes
, tree args
,
11255 tsubst_flags_t complain
, tree in_decl
)
11257 tree last_dep
= NULL_TREE
;
11259 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11260 if (ATTR_IS_DEPENDENT (t
))
11263 attributes
= copy_list (attributes
);
11268 for (tree
*p
= &attributes
; *p
; )
11271 if (ATTR_IS_DEPENDENT (t
))
11273 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11278 p
= &TREE_CHAIN (*p
);
11279 *p
= TREE_CHAIN (t
);
11283 p
= &TREE_CHAIN (*p
);
11289 /* Apply any attributes which had to be deferred until instantiation
11290 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11291 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11294 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11295 tree args
, tsubst_flags_t complain
, tree in_decl
)
11297 tree last_dep
= NULL_TREE
;
11301 if (attributes
== NULL_TREE
)
11304 if (DECL_P (*decl_p
))
11306 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11308 p
= &DECL_ATTRIBUTES (*decl_p
);
11309 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11310 to our attributes parameter. */
11311 gcc_assert (*p
== attributes
);
11315 p
= &TYPE_ATTRIBUTES (*decl_p
);
11316 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11317 lookup_template_class_1, and should be preserved. */
11318 gcc_assert (*p
!= attributes
);
11320 p
= &TREE_CHAIN (*p
);
11323 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11324 if (ATTR_IS_DEPENDENT (t
))
11327 attributes
= copy_list (attributes
);
11334 tree late_attrs
= NULL_TREE
;
11335 tree
*q
= &late_attrs
;
11340 if (ATTR_IS_DEPENDENT (t
))
11342 *p
= TREE_CHAIN (t
);
11343 TREE_CHAIN (t
) = NULL_TREE
;
11344 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11346 q
= &TREE_CHAIN (*q
);
11349 p
= &TREE_CHAIN (t
);
11352 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11356 /* Perform (or defer) access check for typedefs that were referenced
11357 from within the template TMPL code.
11358 This is a subroutine of instantiate_decl and instantiate_class_template.
11359 TMPL is the template to consider and TARGS is the list of arguments of
11363 perform_typedefs_access_check (tree tmpl
, tree targs
)
11366 qualified_typedef_usage_t
*iter
;
11369 || (!CLASS_TYPE_P (tmpl
)
11370 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
11373 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
11375 tree type_decl
= iter
->typedef_decl
;
11376 tree type_scope
= iter
->context
;
11378 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
11381 if (uses_template_parms (type_decl
))
11382 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
11383 if (uses_template_parms (type_scope
))
11384 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11386 /* Make access check error messages point to the location
11387 of the use of the typedef. */
11388 iloc_sentinel
ils (iter
->locus
);
11389 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11390 type_decl
, type_decl
,
11391 tf_warning_or_error
);
11396 instantiate_class_template_1 (tree type
)
11398 tree templ
, args
, pattern
, t
, member
;
11402 unsigned int saved_maximum_field_alignment
;
11405 if (type
== error_mark_node
)
11406 return error_mark_node
;
11408 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11409 || uses_template_parms (type
))
11412 /* Figure out which template is being instantiated. */
11413 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11414 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11416 /* Mark the type as in the process of being defined. */
11417 TYPE_BEING_DEFINED (type
) = 1;
11419 /* We may be in the middle of deferred access check. Disable
11421 deferring_access_check_sentinel
acs (dk_no_deferred
);
11423 /* Determine what specialization of the original template to
11425 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11426 if (t
== error_mark_node
)
11427 return error_mark_node
;
11430 /* This TYPE is actually an instantiation of a partial
11431 specialization. We replace the innermost set of ARGS with
11432 the arguments appropriate for substitution. For example,
11435 template <class T> struct S {};
11436 template <class T> struct S<T*> {};
11438 and supposing that we are instantiating S<int*>, ARGS will
11439 presently be {int*} -- but we need {int}. */
11440 pattern
= TREE_TYPE (t
);
11441 args
= TREE_PURPOSE (t
);
11445 pattern
= TREE_TYPE (templ
);
11446 args
= CLASSTYPE_TI_ARGS (type
);
11449 /* If the template we're instantiating is incomplete, then clearly
11450 there's nothing we can do. */
11451 if (!COMPLETE_TYPE_P (pattern
))
11453 /* We can try again later. */
11454 TYPE_BEING_DEFINED (type
) = 0;
11458 /* If we've recursively instantiated too many templates, stop. */
11459 if (! push_tinst_level (type
))
11462 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11463 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11465 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11466 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11467 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11468 fn_context
= error_mark_node
;
11470 push_to_top_level ();
11473 cp_unevaluated_operand
= 0;
11474 c_inhibit_evaluation_warnings
= 0;
11476 /* Use #pragma pack from the template context. */
11477 saved_maximum_field_alignment
= maximum_field_alignment
;
11478 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11480 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11482 /* Set the input location to the most specialized template definition.
11483 This is needed if tsubsting causes an error. */
11484 typedecl
= TYPE_MAIN_DECL (pattern
);
11485 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11486 DECL_SOURCE_LOCATION (typedecl
);
11488 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11489 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11490 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11491 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11492 if (ANON_AGGR_TYPE_P (pattern
))
11493 SET_ANON_AGGR_TYPE_P (type
);
11494 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11496 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11497 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11498 /* Adjust visibility for template arguments. */
11499 determine_visibility (TYPE_MAIN_DECL (type
));
11501 if (CLASS_TYPE_P (type
))
11502 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11504 pbinfo
= TYPE_BINFO (pattern
);
11506 /* We should never instantiate a nested class before its enclosing
11507 class; we need to look up the nested class by name before we can
11508 instantiate it, and that lookup should instantiate the enclosing
11510 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11511 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11513 base_list
= NULL_TREE
;
11514 if (BINFO_N_BASE_BINFOS (pbinfo
))
11520 /* We must enter the scope containing the type, as that is where
11521 the accessibility of types named in dependent bases are
11523 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11525 /* Substitute into each of the bases to determine the actual
11527 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11530 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11531 tree expanded_bases
= NULL_TREE
;
11534 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11537 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11538 args
, tf_error
, NULL_TREE
);
11539 if (expanded_bases
== error_mark_node
)
11542 len
= TREE_VEC_LENGTH (expanded_bases
);
11545 for (idx
= 0; idx
< len
; idx
++)
11547 if (expanded_bases
)
11548 /* Extract the already-expanded base class. */
11549 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11551 /* Substitute to figure out the base class. */
11552 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11555 if (base
== error_mark_node
)
11558 base_list
= tree_cons (access
, base
, base_list
);
11559 if (BINFO_VIRTUAL_P (pbase_binfo
))
11560 TREE_TYPE (base_list
) = integer_type_node
;
11564 /* The list is now in reverse order; correct that. */
11565 base_list
= nreverse (base_list
);
11568 pop_scope (pushed_scope
);
11570 /* Now call xref_basetypes to set up all the base-class
11572 xref_basetypes (type
, base_list
);
11574 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11575 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11576 args
, tf_error
, NULL_TREE
);
11577 fixup_attribute_variants (type
);
11579 /* Now that our base classes are set up, enter the scope of the
11580 class, so that name lookups into base classes, etc. will work
11581 correctly. This is precisely analogous to what we do in
11582 begin_class_definition when defining an ordinary non-template
11583 class, except we also need to push the enclosing classes. */
11584 push_nested_class (type
);
11586 /* Now members are processed in the order of declaration. */
11587 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11588 member
; member
= TREE_CHAIN (member
))
11590 tree t
= TREE_VALUE (member
);
11592 if (TREE_PURPOSE (member
))
11596 if (LAMBDA_TYPE_P (t
))
11597 /* A closure type for a lambda in an NSDMI or default argument.
11598 Ignore it; it will be regenerated when needed. */
11601 /* Build new CLASSTYPE_NESTED_UTDS. */
11604 bool class_template_p
;
11606 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11607 && TYPE_LANG_SPECIFIC (t
)
11608 && CLASSTYPE_IS_TEMPLATE (t
));
11609 /* If the member is a class template, then -- even after
11610 substitution -- there may be dependent types in the
11611 template argument list for the class. We increment
11612 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11613 that function will assume that no types are dependent
11614 when outside of a template. */
11615 if (class_template_p
)
11616 ++processing_template_decl
;
11617 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11618 if (class_template_p
)
11619 --processing_template_decl
;
11620 if (newtag
== error_mark_node
)
11623 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11625 tree name
= TYPE_IDENTIFIER (t
);
11627 if (class_template_p
)
11628 /* Unfortunately, lookup_template_class sets
11629 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11630 instantiation (i.e., for the type of a member
11631 template class nested within a template class.)
11632 This behavior is required for
11633 maybe_process_partial_specialization to work
11634 correctly, but is not accurate in this case;
11635 the TAG is not an instantiation of anything.
11636 (The corresponding TEMPLATE_DECL is an
11637 instantiation, but the TYPE is not.) */
11638 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11640 /* Now, we call pushtag to put this NEWTAG into the scope of
11641 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11642 pushtag calling push_template_decl. We don't have to do
11643 this for enums because it will already have been done in
11646 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11647 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11650 else if (DECL_DECLARES_FUNCTION_P (t
))
11654 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11655 ++processing_template_decl
;
11656 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11657 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11658 --processing_template_decl
;
11659 set_current_access_from_decl (r
);
11660 finish_member_declaration (r
);
11661 /* Instantiate members marked with attribute used. */
11662 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11664 if (TREE_CODE (r
) == FUNCTION_DECL
11665 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11666 cp_check_omp_declare_reduction (r
);
11668 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11669 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11670 /* A closure type for a lambda in an NSDMI or default argument.
11671 Ignore it; it will be regenerated when needed. */;
11674 /* Build new TYPE_FIELDS. */
11675 if (TREE_CODE (t
) == STATIC_ASSERT
)
11679 ++c_inhibit_evaluation_warnings
;
11681 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11682 tf_warning_or_error
, NULL_TREE
,
11683 /*integral_constant_expression_p=*/true);
11684 --c_inhibit_evaluation_warnings
;
11686 finish_static_assert (condition
,
11687 STATIC_ASSERT_MESSAGE (t
),
11688 STATIC_ASSERT_SOURCE_LOCATION (t
),
11689 /*member_p=*/true);
11691 else if (TREE_CODE (t
) != CONST_DECL
)
11694 tree vec
= NULL_TREE
;
11697 /* The file and line for this declaration, to
11698 assist in error message reporting. Since we
11699 called push_tinst_level above, we don't need to
11701 input_location
= DECL_SOURCE_LOCATION (t
);
11703 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11704 ++processing_template_decl
;
11705 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11706 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11707 --processing_template_decl
;
11709 if (TREE_CODE (r
) == TREE_VEC
)
11711 /* A capture pack became multiple fields. */
11713 len
= TREE_VEC_LENGTH (vec
);
11716 for (int i
= 0; i
< len
; ++i
)
11719 r
= TREE_VEC_ELT (vec
, i
);
11724 [t]he initialization (and any associated
11725 side-effects) of a static data member does
11726 not occur unless the static data member is
11727 itself used in a way that requires the
11728 definition of the static data member to
11731 Therefore, we do not substitute into the
11732 initialized for the static data member here. */
11733 finish_static_data_member_decl
11735 /*init=*/NULL_TREE
,
11736 /*init_const_expr_p=*/false,
11737 /*asmspec_tree=*/NULL_TREE
,
11739 /* Instantiate members marked with attribute used. */
11740 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11743 else if (TREE_CODE (r
) == FIELD_DECL
)
11745 /* Determine whether R has a valid type and can be
11746 completed later. If R is invalid, then its type
11747 is replaced by error_mark_node. */
11748 tree rtype
= TREE_TYPE (r
);
11749 if (can_complete_type_without_circularity (rtype
))
11750 complete_type (rtype
);
11752 if (!complete_or_array_type_p (rtype
))
11754 /* If R's type couldn't be completed and
11755 it isn't a flexible array member (whose
11756 type is incomplete by definition) give
11758 cxx_incomplete_type_error (r
, rtype
);
11759 TREE_TYPE (r
) = error_mark_node
;
11761 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11762 && TYPE_DOMAIN (rtype
) == NULL_TREE
11763 && (TREE_CODE (type
) == UNION_TYPE
11764 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11766 error ("flexible array member %qD in union", r
);
11767 TREE_TYPE (r
) = error_mark_node
;
11771 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11772 such a thing will already have been added to the field
11773 list by tsubst_enum in finish_member_declaration in the
11774 CLASSTYPE_NESTED_UTDS case above. */
11775 if (!(TREE_CODE (r
) == TYPE_DECL
11776 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11777 && DECL_ARTIFICIAL (r
)))
11779 set_current_access_from_decl (r
);
11780 finish_member_declaration (r
);
11788 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11789 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11791 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11793 tree friend_type
= t
;
11794 bool adjust_processing_template_decl
= false;
11796 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11798 /* template <class T> friend class C; */
11799 friend_type
= tsubst_friend_class (friend_type
, args
);
11800 adjust_processing_template_decl
= true;
11802 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11804 /* template <class T> friend class C::D; */
11805 friend_type
= tsubst (friend_type
, args
,
11806 tf_warning_or_error
, NULL_TREE
);
11807 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11808 friend_type
= TREE_TYPE (friend_type
);
11809 adjust_processing_template_decl
= true;
11811 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11812 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11814 /* This could be either
11818 when dependent_type_p is false or
11820 template <class U> friend class T::C;
11823 /* Bump processing_template_decl in case this is something like
11824 template <class T> friend struct A<T>::B. */
11825 ++processing_template_decl
;
11826 friend_type
= tsubst (friend_type
, args
,
11827 tf_warning_or_error
, NULL_TREE
);
11828 if (dependent_type_p (friend_type
))
11829 adjust_processing_template_decl
= true;
11830 --processing_template_decl
;
11832 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11833 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11834 && TYPE_HIDDEN_P (friend_type
))
11838 where C hasn't been declared yet. Let's lookup name
11839 from namespace scope directly, bypassing any name that
11840 come from dependent base class. */
11841 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11843 /* The call to xref_tag_from_type does injection for friend
11845 push_nested_namespace (ns
);
11847 xref_tag_from_type (friend_type
, NULL_TREE
,
11848 /*tag_scope=*/ts_current
);
11849 pop_nested_namespace (ns
);
11851 else if (uses_template_parms (friend_type
))
11852 /* friend class C<T>; */
11853 friend_type
= tsubst (friend_type
, args
,
11854 tf_warning_or_error
, NULL_TREE
);
11859 where C is already declared or
11861 friend class C<int>;
11863 We don't have to do anything in these cases. */
11865 if (adjust_processing_template_decl
)
11866 /* Trick make_friend_class into realizing that the friend
11867 we're adding is a template, not an ordinary class. It's
11868 important that we use make_friend_class since it will
11869 perform some error-checking and output cross-reference
11871 ++processing_template_decl
;
11873 if (friend_type
!= error_mark_node
)
11874 make_friend_class (type
, friend_type
, /*complain=*/false);
11876 if (adjust_processing_template_decl
)
11877 --processing_template_decl
;
11881 /* Build new DECL_FRIENDLIST. */
11884 /* The file and line for this declaration, to
11885 assist in error message reporting. Since we
11886 called push_tinst_level above, we don't need to
11888 input_location
= DECL_SOURCE_LOCATION (t
);
11890 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11892 ++processing_template_decl
;
11893 push_deferring_access_checks (dk_no_check
);
11896 r
= tsubst_friend_function (t
, args
);
11897 add_friend (type
, r
, /*complain=*/false);
11898 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11900 pop_deferring_access_checks ();
11901 --processing_template_decl
;
11909 /* Restore these before substituting into the lambda capture
11911 cp_unevaluated_operand
= saved_unevaluated_operand
;
11912 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11915 /* Set the file and line number information to whatever is given for
11916 the class itself. This puts error messages involving generated
11917 implicit functions at a predictable point, and the same point
11918 that would be used for non-template classes. */
11919 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11921 unreverse_member_declarations (type
);
11922 finish_struct_1 (type
);
11923 TYPE_BEING_DEFINED (type
) = 0;
11925 /* We don't instantiate default arguments for member functions. 14.7.1:
11927 The implicit instantiation of a class template specialization causes
11928 the implicit instantiation of the declarations, but not of the
11929 definitions or default arguments, of the class member functions,
11930 member classes, static data members and member templates.... */
11932 /* Some typedefs referenced from within the template code need to be access
11933 checked at template instantiation time, i.e now. These types were
11934 added to the template at parsing time. Let's get those and perform
11935 the access checks then. */
11936 perform_typedefs_access_check (pattern
, args
);
11937 perform_deferred_access_checks (tf_warning_or_error
);
11938 pop_nested_class ();
11939 maximum_field_alignment
= saved_maximum_field_alignment
;
11941 pop_from_top_level ();
11942 pop_tinst_level ();
11944 /* The vtable for a template class can be emitted in any translation
11945 unit in which the class is instantiated. When there is no key
11946 method, however, finish_struct_1 will already have added TYPE to
11947 the keyed_classes. */
11948 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11949 vec_safe_push (keyed_classes
, type
);
11954 /* Wrapper for instantiate_class_template_1. */
11957 instantiate_class_template (tree type
)
11960 timevar_push (TV_TEMPLATE_INST
);
11961 ret
= instantiate_class_template_1 (type
);
11962 timevar_pop (TV_TEMPLATE_INST
);
11967 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11973 else if (TYPE_P (t
))
11974 r
= tsubst (t
, args
, complain
, in_decl
);
11977 if (!(complain
& tf_warning
))
11978 ++c_inhibit_evaluation_warnings
;
11979 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11980 /*integral_constant_expression_p=*/true);
11981 if (!(complain
& tf_warning
))
11982 --c_inhibit_evaluation_warnings
;
11988 /* Given a function parameter pack TMPL_PARM and some function parameters
11989 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11990 and set *SPEC_P to point at the next point in the list. */
11993 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11995 /* Collect all of the extra "packed" parameters into an
11998 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11999 tree spec_parm
= *spec_p
;
12002 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12004 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12007 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12008 parmvec
= make_tree_vec (len
);
12009 spec_parm
= *spec_p
;
12010 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12012 tree elt
= spec_parm
;
12013 if (DECL_PACK_P (elt
))
12014 elt
= make_pack_expansion (elt
);
12015 TREE_VEC_ELT (parmvec
, i
) = elt
;
12018 /* Build the argument packs. */
12019 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12020 *spec_p
= spec_parm
;
12025 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12026 NONTYPE_ARGUMENT_PACK. */
12029 make_fnparm_pack (tree spec_parm
)
12031 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12034 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12035 pack expansion with no extra args, 2 if it has extra args, or 0
12036 if it is not a pack expansion. */
12039 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12041 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12042 /* We're being called before this happens in tsubst_pack_expansion. */
12043 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12044 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12045 if (i
>= TREE_VEC_LENGTH (vec
))
12047 tree elt
= TREE_VEC_ELT (vec
, i
);
12049 /* A decl pack is itself an expansion. */
12050 elt
= TREE_TYPE (elt
);
12051 if (!PACK_EXPANSION_P (elt
))
12053 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12059 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12062 make_argument_pack_select (tree arg_pack
, unsigned index
)
12064 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12066 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12067 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12072 /* This is a subroutine of tsubst_pack_expansion.
12074 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12075 mechanism to store the (non complete list of) arguments of the
12076 substitution and return a non substituted pack expansion, in order
12077 to wait for when we have enough arguments to really perform the
12081 use_pack_expansion_extra_args_p (tree parm_packs
,
12083 bool has_empty_arg
)
12085 /* If one pack has an expansion and another pack has a normal
12086 argument or if one pack has an empty argument and an another
12087 one hasn't then tsubst_pack_expansion cannot perform the
12088 substitution and need to fall back on the
12089 PACK_EXPANSION_EXTRA mechanism. */
12090 if (parm_packs
== NULL_TREE
)
12092 else if (has_empty_arg
)
12094 /* If all the actual packs are pack expansions, we can still
12095 subsitute directly. */
12096 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12098 tree a
= TREE_VALUE (p
);
12099 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12100 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12101 a
= ARGUMENT_PACK_ARGS (a
);
12102 if (TREE_VEC_LENGTH (a
) == 1)
12103 a
= TREE_VEC_ELT (a
, 0);
12104 if (PACK_EXPANSION_P (a
))
12111 bool has_expansion_arg
= false;
12112 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12114 bool has_non_expansion_arg
= false;
12115 for (tree parm_pack
= parm_packs
;
12117 parm_pack
= TREE_CHAIN (parm_pack
))
12119 tree arg
= TREE_VALUE (parm_pack
);
12121 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12123 /* We can't substitute a pack expansion with extra args into
12127 has_expansion_arg
= true;
12129 has_non_expansion_arg
= true;
12132 if (has_expansion_arg
&& has_non_expansion_arg
)
12138 /* [temp.variadic]/6 says that:
12140 The instantiation of a pack expansion [...]
12141 produces a list E1,E2, ..., En, where N is the number of elements
12142 in the pack expansion parameters.
12144 This subroutine of tsubst_pack_expansion produces one of these Ei.
12146 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12147 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12148 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12149 INDEX is the index 'i' of the element Ei to produce. ARGS,
12150 COMPLAIN, and IN_DECL are the same parameters as for the
12151 tsubst_pack_expansion function.
12153 The function returns the resulting Ei upon successful completion,
12154 or error_mark_node.
12156 Note that this function possibly modifies the ARGS parameter, so
12157 it's the responsibility of the caller to restore it. */
12160 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12163 tree args
/* This parm gets
12165 tsubst_flags_t complain
,
12169 bool ith_elem_is_expansion
= false;
12171 /* For each parameter pack, change the substitution of the parameter
12172 pack to the ith argument in its argument pack, then expand the
12174 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12176 tree parm
= TREE_PURPOSE (pack
);
12177 tree arg_pack
= TREE_VALUE (pack
);
12178 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12180 ith_elem_is_expansion
|=
12181 argument_pack_element_is_expansion_p (arg_pack
, index
);
12183 /* Select the Ith argument from the pack. */
12184 if (TREE_CODE (parm
) == PARM_DECL
12186 || TREE_CODE (parm
) == FIELD_DECL
)
12190 aps
= make_argument_pack_select (arg_pack
, index
);
12191 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12192 return error_mark_node
;
12193 register_local_specialization (aps
, parm
);
12196 aps
= retrieve_local_specialization (parm
);
12201 template_parm_level_and_index (parm
, &level
, &idx
);
12205 aps
= make_argument_pack_select (arg_pack
, index
);
12206 /* Update the corresponding argument. */
12207 TMPL_ARG (args
, level
, idx
) = aps
;
12210 /* Re-use the ARGUMENT_PACK_SELECT. */
12211 aps
= TMPL_ARG (args
, level
, idx
);
12213 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12216 /* Substitute into the PATTERN with the (possibly altered)
12218 if (pattern
== in_decl
)
12219 /* Expanding a fixed parameter pack from
12220 coerce_template_parameter_pack. */
12221 t
= tsubst_decl (pattern
, args
, complain
);
12222 else if (pattern
== error_mark_node
)
12223 t
= error_mark_node
;
12224 else if (!TYPE_P (pattern
))
12225 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12226 /*integral_constant_expression_p=*/false);
12228 t
= tsubst (pattern
, args
, complain
, in_decl
);
12230 /* If the Ith argument pack element is a pack expansion, then
12231 the Ith element resulting from the substituting is going to
12232 be a pack expansion as well. */
12233 if (ith_elem_is_expansion
)
12234 t
= make_pack_expansion (t
, complain
);
12239 /* When the unexpanded parameter pack in a fold expression expands to an empty
12240 sequence, the value of the expression is as follows; the program is
12241 ill-formed if the operator is not listed in this table.
12248 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12250 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12251 if (!FOLD_EXPR_MODIFY_P (t
))
12254 case TRUTH_ANDIF_EXPR
:
12255 return boolean_true_node
;
12256 case TRUTH_ORIF_EXPR
:
12257 return boolean_false_node
;
12258 case COMPOUND_EXPR
:
12264 if (complain
& tf_error
)
12265 error_at (location_of (t
),
12266 "fold of empty expansion over %O", code
);
12267 return error_mark_node
;
12270 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12271 form an expression that combines the two terms using the
12275 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12277 tree op
= FOLD_EXPR_OP (t
);
12278 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12280 // Handle compound assignment operators.
12281 if (FOLD_EXPR_MODIFY_P (t
))
12282 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12286 case COMPOUND_EXPR
:
12287 return build_x_compound_expr (input_location
, left
, right
, complain
);
12289 return build_x_binary_op (input_location
, code
,
12290 left
, TREE_CODE (left
),
12291 right
, TREE_CODE (right
),
12297 /* Substitute ARGS into the pack of a fold expression T. */
12300 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12302 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12305 /* Substitute ARGS into the pack of a fold expression T. */
12308 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12310 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12313 /* Expand a PACK of arguments into a grouped as left fold.
12314 Given a pack containing elements A0, A1, ..., An and an
12315 operator @, this builds the expression:
12317 ((A0 @ A1) @ A2) ... @ An
12319 Note that PACK must not be empty.
12321 The operator is defined by the original fold expression T. */
12324 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12326 tree left
= TREE_VEC_ELT (pack
, 0);
12327 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12329 tree right
= TREE_VEC_ELT (pack
, i
);
12330 left
= fold_expression (t
, left
, right
, complain
);
12335 /* Substitute into a unary left fold expression. */
12338 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12341 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12342 if (pack
== error_mark_node
)
12343 return error_mark_node
;
12344 if (PACK_EXPANSION_P (pack
))
12346 tree r
= copy_node (t
);
12347 FOLD_EXPR_PACK (r
) = pack
;
12350 if (TREE_VEC_LENGTH (pack
) == 0)
12351 return expand_empty_fold (t
, complain
);
12353 return expand_left_fold (t
, pack
, complain
);
12356 /* Substitute into a binary left fold expression.
12358 Do ths by building a single (non-empty) vector of argumnts and
12359 building the expression from those elements. */
12362 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12365 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12366 if (pack
== error_mark_node
)
12367 return error_mark_node
;
12368 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12369 if (init
== error_mark_node
)
12370 return error_mark_node
;
12372 if (PACK_EXPANSION_P (pack
))
12374 tree r
= copy_node (t
);
12375 FOLD_EXPR_PACK (r
) = pack
;
12376 FOLD_EXPR_INIT (r
) = init
;
12380 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12381 TREE_VEC_ELT (vec
, 0) = init
;
12382 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12383 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12385 return expand_left_fold (t
, vec
, complain
);
12388 /* Expand a PACK of arguments into a grouped as right fold.
12389 Given a pack containing elementns A0, A1, ..., and an
12390 operator @, this builds the expression:
12392 A0@ ... (An-2 @ (An-1 @ An))
12394 Note that PACK must not be empty.
12396 The operator is defined by the original fold expression T. */
12399 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12401 // Build the expression.
12402 int n
= TREE_VEC_LENGTH (pack
);
12403 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12404 for (--n
; n
!= 0; --n
)
12406 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12407 right
= fold_expression (t
, left
, right
, complain
);
12412 /* Substitute into a unary right fold expression. */
12415 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12418 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12419 if (pack
== error_mark_node
)
12420 return error_mark_node
;
12421 if (PACK_EXPANSION_P (pack
))
12423 tree r
= copy_node (t
);
12424 FOLD_EXPR_PACK (r
) = pack
;
12427 if (TREE_VEC_LENGTH (pack
) == 0)
12428 return expand_empty_fold (t
, complain
);
12430 return expand_right_fold (t
, pack
, complain
);
12433 /* Substitute into a binary right fold expression.
12435 Do ths by building a single (non-empty) vector of arguments and
12436 building the expression from those elements. */
12439 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12442 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12443 if (pack
== error_mark_node
)
12444 return error_mark_node
;
12445 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12446 if (init
== error_mark_node
)
12447 return error_mark_node
;
12449 if (PACK_EXPANSION_P (pack
))
12451 tree r
= copy_node (t
);
12452 FOLD_EXPR_PACK (r
) = pack
;
12453 FOLD_EXPR_INIT (r
) = init
;
12457 int n
= TREE_VEC_LENGTH (pack
);
12458 tree vec
= make_tree_vec (n
+ 1);
12459 for (int i
= 0; i
< n
; ++i
)
12460 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12461 TREE_VEC_ELT (vec
, n
) = init
;
12463 return expand_right_fold (t
, vec
, complain
);
12466 /* Walk through the pattern of a pack expansion, adding everything in
12467 local_specializations to a list. */
12472 hash_set
<tree
> internal
;
12474 tsubst_flags_t complain
;
12476 el_data (tsubst_flags_t c
)
12477 : extra (NULL_TREE
), complain (c
) {}
12480 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12482 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12483 tree
*extra
= &data
.extra
;
12484 tsubst_flags_t complain
= data
.complain
;
12486 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12487 /* Remember local typedefs (85214). */
12488 tp
= &TYPE_NAME (*tp
);
12490 if (TREE_CODE (*tp
) == DECL_EXPR
)
12491 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12492 else if (tree spec
= retrieve_local_specialization (*tp
))
12494 if (data
.internal
.contains (*tp
))
12495 /* Don't mess with variables declared within the pattern. */
12497 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12499 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12500 tree args
= ARGUMENT_PACK_ARGS (spec
);
12501 if (TREE_VEC_LENGTH (args
) == 1)
12503 tree elt
= TREE_VEC_ELT (args
, 0);
12504 if (PACK_EXPANSION_P (elt
))
12505 elt
= PACK_EXPANSION_PATTERN (elt
);
12506 if (DECL_PACK_P (elt
))
12509 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12511 /* Handle lambda capture here, since we aren't doing any
12512 substitution now, and so tsubst_copy won't call
12513 process_outer_var_ref. */
12514 tree args
= ARGUMENT_PACK_ARGS (spec
);
12515 int len
= TREE_VEC_LENGTH (args
);
12516 for (int i
= 0; i
< len
; ++i
)
12518 tree arg
= TREE_VEC_ELT (args
, i
);
12520 if (outer_automatic_var_p (arg
))
12521 carg
= process_outer_var_ref (arg
, complain
);
12524 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12528 spec
= copy_node (spec
);
12529 args
= copy_node (args
);
12530 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12531 register_local_specialization (spec
, *tp
);
12533 TREE_VEC_ELT (args
, i
) = carg
;
12538 if (outer_automatic_var_p (spec
))
12539 spec
= process_outer_var_ref (spec
, complain
);
12540 *extra
= tree_cons (*tp
, spec
, *extra
);
12545 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12547 el_data
data (complain
);
12548 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12552 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12553 for use in PACK_EXPANSION_EXTRA_ARGS. */
12556 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12559 if (local_specializations
)
12560 if (tree locals
= extract_local_specs (pattern
, complain
))
12561 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12565 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12566 normal template args to ARGS. */
12569 add_extra_args (tree extra
, tree args
)
12571 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12573 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12575 /* The partial instantiation involved local declarations collected in
12576 extract_local_specs; map from the general template to our local
12578 tree gen
= TREE_PURPOSE (elt
);
12579 tree inst
= TREE_VALUE (elt
);
12581 if (tree local
= retrieve_local_specialization (inst
))
12583 /* else inst is already a full instantiation of the pack. */
12584 register_local_specialization (inst
, gen
);
12586 gcc_assert (!TREE_PURPOSE (extra
));
12587 extra
= TREE_VALUE (extra
);
12590 /* I think we should always be able to substitute dependent args into the
12591 pattern. If that turns out to be incorrect in some cases, enable the
12592 alternate code (and add complain/in_decl parms to this function). */
12593 gcc_checking_assert (!uses_template_parms (extra
));
12595 if (!uses_template_parms (extra
))
12597 gcc_unreachable ();
12598 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12599 args
= add_outermost_template_args (args
, extra
);
12603 args
= add_to_template_args (extra
, args
);
12607 /* Substitute ARGS into T, which is an pack expansion
12608 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12609 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12610 (if only a partial substitution could be performed) or
12611 ERROR_MARK_NODE if there was an error. */
12613 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12617 tree pack
, packs
= NULL_TREE
;
12618 bool unsubstituted_packs
= false;
12619 bool unsubstituted_fn_pack
= false;
12622 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12623 bool need_local_specializations
= false;
12626 gcc_assert (PACK_EXPANSION_P (t
));
12627 pattern
= PACK_EXPANSION_PATTERN (t
);
12629 /* Add in any args remembered from an earlier partial instantiation. */
12630 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12632 levels
= TMPL_ARGS_DEPTH (args
);
12634 /* Determine the argument packs that will instantiate the parameter
12635 packs used in the expansion expression. While we're at it,
12636 compute the number of arguments to be expanded and make sure it
12638 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12639 pack
= TREE_CHAIN (pack
))
12641 tree parm_pack
= TREE_VALUE (pack
);
12642 tree arg_pack
= NULL_TREE
;
12643 tree orig_arg
= NULL_TREE
;
12646 if (TREE_CODE (parm_pack
) == BASES
)
12648 gcc_assert (parm_pack
== pattern
);
12649 if (BASES_DIRECT (parm_pack
))
12650 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12655 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12656 args
, complain
, in_decl
,
12659 else if (builtin_pack_call_p (parm_pack
))
12661 if (parm_pack
!= pattern
)
12663 if (complain
& tf_error
)
12664 sorry ("%qE is not the entire pattern of the pack expansion",
12666 return error_mark_node
;
12668 return expand_builtin_pack_call (parm_pack
, args
,
12669 complain
, in_decl
);
12671 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12673 /* We know we have correct local_specializations if this
12674 expansion is at function scope, or if we're dealing with a
12675 local parameter in a requires expression; for the latter,
12676 tsubst_requires_expr set it up appropriately. */
12677 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12678 arg_pack
= retrieve_local_specialization (parm_pack
);
12680 /* We can't rely on local_specializations for a parameter
12681 name used later in a function declaration (such as in a
12682 late-specified return type). Even if it exists, it might
12683 have the wrong value for a recursive call. */
12684 need_local_specializations
= true;
12688 /* This parameter pack was used in an unevaluated context. Just
12689 make a dummy decl, since it's only used for its type. */
12690 ++cp_unevaluated_operand
;
12691 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12692 --cp_unevaluated_operand
;
12693 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12694 /* Partial instantiation of the parm_pack, we can't build
12695 up an argument pack yet. */
12696 arg_pack
= NULL_TREE
;
12698 arg_pack
= make_fnparm_pack (arg_pack
);
12700 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12701 /* This argument pack isn't fully instantiated yet. We set this
12702 flag rather than clear arg_pack because we do want to do the
12703 optimization below, and we don't want to substitute directly
12704 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12705 where it isn't expected). */
12706 unsubstituted_fn_pack
= true;
12708 else if (is_capture_proxy (parm_pack
))
12710 arg_pack
= retrieve_local_specialization (parm_pack
);
12711 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12712 unsubstituted_fn_pack
= true;
12717 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12718 if (level
<= levels
)
12719 arg_pack
= TMPL_ARG (args
, level
, idx
);
12722 orig_arg
= arg_pack
;
12723 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12724 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12726 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12727 /* This can only happen if we forget to expand an argument
12728 pack somewhere else. Just return an error, silently. */
12730 result
= make_tree_vec (1);
12731 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12738 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12740 /* Don't bother trying to do a partial substitution with
12741 incomplete packs; we'll try again after deduction. */
12742 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12747 else if (len
!= my_len
12748 && !unsubstituted_fn_pack
)
12750 if (!(complain
& tf_error
))
12751 /* Fail quietly. */;
12752 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12753 error ("mismatched argument pack lengths while expanding %qT",
12756 error ("mismatched argument pack lengths while expanding %qE",
12758 return error_mark_node
;
12761 /* Keep track of the parameter packs and their corresponding
12763 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12764 TREE_TYPE (packs
) = orig_arg
;
12768 /* We can't substitute for this parameter pack. We use a flag as
12769 well as the missing_level counter because function parameter
12770 packs don't have a level. */
12771 if (!(processing_template_decl
|| is_auto (parm_pack
)))
12773 gcc_unreachable ();
12775 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12776 unsubstituted_packs
= true;
12780 /* If the expansion is just T..., return the matching argument pack, unless
12781 we need to call convert_from_reference on all the elements. This is an
12782 important optimization; see c++/68422. */
12783 if (!unsubstituted_packs
12784 && TREE_PURPOSE (packs
) == pattern
)
12786 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12788 /* If the argument pack is a single pack expansion, pull it out. */
12789 if (TREE_VEC_LENGTH (args
) == 1
12790 && pack_expansion_args_count (args
))
12791 return TREE_VEC_ELT (args
, 0);
12793 /* Types need no adjustment, nor does sizeof..., and if we still have
12794 some pack expansion args we won't do anything yet. */
12795 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12796 || PACK_EXPANSION_SIZEOF_P (t
)
12797 || pack_expansion_args_count (args
))
12799 /* Also optimize expression pack expansions if we can tell that the
12800 elements won't have reference type. */
12801 tree type
= TREE_TYPE (pattern
);
12802 if (type
&& !TYPE_REF_P (type
)
12803 && !PACK_EXPANSION_P (type
)
12804 && !WILDCARD_TYPE_P (type
))
12806 /* Otherwise use the normal path so we get convert_from_reference. */
12809 /* We cannot expand this expansion expression, because we don't have
12810 all of the argument packs we need. */
12811 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12813 /* We got some full packs, but we can't substitute them in until we
12814 have values for all the packs. So remember these until then. */
12816 t
= make_pack_expansion (pattern
, complain
);
12817 PACK_EXPANSION_EXTRA_ARGS (t
)
12818 = build_extra_args (pattern
, args
, complain
);
12821 else if (unsubstituted_packs
)
12823 /* There were no real arguments, we're just replacing a parameter
12824 pack with another version of itself. Substitute into the
12825 pattern and return a PACK_EXPANSION_*. The caller will need to
12827 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12828 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12829 /*integral_constant_expression_p=*/false);
12831 t
= tsubst (pattern
, args
, complain
, in_decl
);
12832 t
= make_pack_expansion (t
, complain
);
12836 gcc_assert (len
>= 0);
12838 if (need_local_specializations
)
12840 /* We're in a late-specified return type, so create our own local
12841 specializations map; the current map is either NULL or (in the
12842 case of recursive unification) might have bindings that we don't
12843 want to use or alter. */
12844 saved_local_specializations
= local_specializations
;
12845 local_specializations
= new hash_map
<tree
, tree
>;
12848 /* For each argument in each argument pack, substitute into the
12850 result
= make_tree_vec (len
);
12851 tree elem_args
= copy_template_args (args
);
12852 for (i
= 0; i
< len
; ++i
)
12854 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12856 elem_args
, complain
,
12858 TREE_VEC_ELT (result
, i
) = t
;
12859 if (t
== error_mark_node
)
12861 result
= error_mark_node
;
12866 /* Update ARGS to restore the substitution from parameter packs to
12867 their argument packs. */
12868 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12870 tree parm
= TREE_PURPOSE (pack
);
12872 if (TREE_CODE (parm
) == PARM_DECL
12874 || TREE_CODE (parm
) == FIELD_DECL
)
12875 register_local_specialization (TREE_TYPE (pack
), parm
);
12880 if (TREE_VALUE (pack
) == NULL_TREE
)
12883 template_parm_level_and_index (parm
, &level
, &idx
);
12885 /* Update the corresponding argument. */
12886 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12887 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12890 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12894 if (need_local_specializations
)
12896 delete local_specializations
;
12897 local_specializations
= saved_local_specializations
;
12900 /* If the dependent pack arguments were such that we end up with only a
12901 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12902 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12903 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12904 return TREE_VEC_ELT (result
, 0);
12909 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12910 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12911 parameter packs; all parms generated from a function parameter pack will
12912 have the same DECL_PARM_INDEX. */
12915 get_pattern_parm (tree parm
, tree tmpl
)
12917 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12920 if (DECL_ARTIFICIAL (parm
))
12922 for (patparm
= DECL_ARGUMENTS (pattern
);
12923 patparm
; patparm
= DECL_CHAIN (patparm
))
12924 if (DECL_ARTIFICIAL (patparm
)
12925 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12930 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12931 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12932 gcc_assert (DECL_PARM_INDEX (patparm
)
12933 == DECL_PARM_INDEX (parm
));
12939 /* Make an argument pack out of the TREE_VEC VEC. */
12942 make_argument_pack (tree vec
)
12945 tree elt
= TREE_VEC_ELT (vec
, 0);
12947 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12950 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12951 TREE_CONSTANT (pack
) = 1;
12953 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12957 /* Return an exact copy of template args T that can be modified
12961 copy_template_args (tree t
)
12963 if (t
== error_mark_node
)
12966 int len
= TREE_VEC_LENGTH (t
);
12967 tree new_vec
= make_tree_vec (len
);
12969 for (int i
= 0; i
< len
; ++i
)
12971 tree elt
= TREE_VEC_ELT (t
, i
);
12972 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12973 elt
= copy_template_args (elt
);
12974 TREE_VEC_ELT (new_vec
, i
) = elt
;
12977 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12978 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12983 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
12986 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
12989 /* Substitute into each of the arguments. */
12990 tree new_arg
= TYPE_P (orig_arg
)
12991 ? cxx_make_type (TREE_CODE (orig_arg
))
12992 : make_node (TREE_CODE (orig_arg
));
12994 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12995 args
, complain
, in_decl
);
12996 if (pack_args
== error_mark_node
)
12997 new_arg
= error_mark_node
;
12999 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13001 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13002 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13007 /* Substitute ARGS into the vector or list of template arguments T. */
13010 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13013 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13016 if (t
== error_mark_node
)
13017 return error_mark_node
;
13019 len
= TREE_VEC_LENGTH (t
);
13020 elts
= XALLOCAVEC (tree
, len
);
13022 for (i
= 0; i
< len
; i
++)
13024 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13027 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13028 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13029 else if (PACK_EXPANSION_P (orig_arg
))
13031 /* Substitute into an expansion expression. */
13032 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13034 if (TREE_CODE (new_arg
) == TREE_VEC
)
13035 /* Add to the expanded length adjustment the number of
13036 expanded arguments. We subtract one from this
13037 measurement, because the argument pack expression
13038 itself is already counted as 1 in
13039 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13040 the argument pack is empty. */
13041 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13043 else if (ARGUMENT_PACK_P (orig_arg
))
13044 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13046 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13048 if (new_arg
== error_mark_node
)
13049 return error_mark_node
;
13052 if (new_arg
!= orig_arg
)
13059 /* Make space for the expanded arguments coming from template
13061 t
= make_tree_vec (len
+ expanded_len_adjust
);
13062 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13063 arguments for a member template.
13064 In that case each TREE_VEC in ORIG_T represents a level of template
13065 arguments, and ORIG_T won't carry any non defaulted argument count.
13066 It will rather be the nested TREE_VECs that will carry one.
13067 In other words, ORIG_T carries a non defaulted argument count only
13068 if it doesn't contain any nested TREE_VEC. */
13069 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13071 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13072 count
+= expanded_len_adjust
;
13073 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13075 for (i
= 0, out
= 0; i
< len
; i
++)
13077 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13078 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13079 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13083 /* Now expand the template argument pack "in place". */
13084 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13085 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13089 TREE_VEC_ELT (t
, out
) = elts
[i
];
13097 /* Substitute ARGS into one level PARMS of template parameters. */
13100 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13102 if (parms
== error_mark_node
)
13103 return error_mark_node
;
13105 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13107 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13109 tree tuple
= TREE_VEC_ELT (parms
, i
);
13111 if (tuple
== error_mark_node
)
13114 TREE_VEC_ELT (new_vec
, i
) =
13115 tsubst_template_parm (tuple
, args
, complain
);
13121 /* Return the result of substituting ARGS into the template parameters
13122 given by PARMS. If there are m levels of ARGS and m + n levels of
13123 PARMS, then the result will contain n levels of PARMS. For
13124 example, if PARMS is `template <class T> template <class U>
13125 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13126 result will be `template <int*, double, class V>'. */
13129 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13131 tree r
= NULL_TREE
;
13134 /* When substituting into a template, we must set
13135 PROCESSING_TEMPLATE_DECL as the template parameters may be
13136 dependent if they are based on one-another, and the dependency
13137 predicates are short-circuit outside of templates. */
13138 ++processing_template_decl
;
13140 for (new_parms
= &r
;
13141 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13142 new_parms
= &(TREE_CHAIN (*new_parms
)),
13143 parms
= TREE_CHAIN (parms
))
13145 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13148 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13149 - TMPL_ARGS_DEPTH (args
)),
13150 new_vec
, NULL_TREE
);
13153 --processing_template_decl
;
13158 /* Return the result of substituting ARGS into one template parameter
13159 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13160 parameter and which TREE_PURPOSE is the default argument of the
13161 template parameter. */
13164 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13166 tree default_value
, parm_decl
;
13168 if (args
== NULL_TREE
13170 || t
== error_mark_node
)
13173 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13175 default_value
= TREE_PURPOSE (t
);
13176 parm_decl
= TREE_VALUE (t
);
13177 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13179 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13180 if (TREE_CODE (parm_decl
) == PARM_DECL
13181 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13182 parm_decl
= error_mark_node
;
13183 default_value
= tsubst_template_arg (default_value
, args
,
13184 complain
, NULL_TREE
);
13185 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13187 tree r
= build_tree_list (default_value
, parm_decl
);
13188 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13192 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13193 type T. If T is not an aggregate or enumeration type, it is
13194 handled as if by tsubst. IN_DECL is as for tsubst. If
13195 ENTERING_SCOPE is nonzero, T is the context for a template which
13196 we are presently tsubst'ing. Return the substituted value. */
13199 tsubst_aggr_type (tree t
,
13201 tsubst_flags_t complain
,
13203 int entering_scope
)
13205 if (t
== NULL_TREE
)
13208 switch (TREE_CODE (t
))
13211 if (TYPE_PTRMEMFUNC_P (t
))
13212 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13214 /* Fall through. */
13215 case ENUMERAL_TYPE
:
13217 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13223 /* In "sizeof(X<I>)" we need to evaluate "I". */
13226 /* First, determine the context for the type we are looking
13228 context
= TYPE_CONTEXT (t
);
13229 if (context
&& TYPE_P (context
))
13231 context
= tsubst_aggr_type (context
, args
, complain
,
13232 in_decl
, /*entering_scope=*/1);
13233 /* If context is a nested class inside a class template,
13234 it may still need to be instantiated (c++/33959). */
13235 context
= complete_type (context
);
13238 /* Then, figure out what arguments are appropriate for the
13239 type we are trying to find. For example, given:
13241 template <class T> struct S;
13242 template <class T, class U> void f(T, U) { S<U> su; }
13244 and supposing that we are instantiating f<int, double>,
13245 then our ARGS will be {int, double}, but, when looking up
13246 S we only want {double}. */
13247 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13248 complain
, in_decl
);
13249 if (argvec
== error_mark_node
)
13250 r
= error_mark_node
;
13253 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13254 entering_scope
, complain
);
13255 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13261 /* This is not a template type, so there's nothing to do. */
13265 return tsubst (t
, args
, complain
, in_decl
);
13269 static GTY((cache
)) tree_cache_map
*defarg_inst
;
13271 /* Substitute into the default argument ARG (a default argument for
13272 FN), which has the indicated TYPE. */
13275 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13276 tsubst_flags_t complain
)
13278 int errs
= errorcount
+ sorrycount
;
13280 /* This can happen in invalid code. */
13281 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13284 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13285 parm
= chain_index (parmnum
, parm
);
13286 tree parmtype
= TREE_TYPE (parm
);
13287 if (DECL_BY_REFERENCE (parm
))
13288 parmtype
= TREE_TYPE (parmtype
);
13289 if (parmtype
== error_mark_node
)
13290 return error_mark_node
;
13292 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13295 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13298 /* This default argument came from a template. Instantiate the
13299 default argument here, not in tsubst. In the case of
13308 we must be careful to do name lookup in the scope of S<T>,
13309 rather than in the current class. */
13310 push_to_top_level ();
13311 push_access_scope (fn
);
13312 push_deferring_access_checks (dk_no_deferred
);
13313 start_lambda_scope (parm
);
13315 /* The default argument expression may cause implicitly defined
13316 member functions to be synthesized, which will result in garbage
13317 collection. We must treat this situation as if we were within
13318 the body of function so as to avoid collecting live data on the
13321 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13322 complain
, NULL_TREE
,
13323 /*integral_constant_expression_p=*/false);
13326 finish_lambda_scope ();
13328 /* Make sure the default argument is reasonable. */
13329 arg
= check_default_argument (type
, arg
, complain
);
13331 if (errorcount
+sorrycount
> errs
13332 && (complain
& tf_warning_or_error
))
13333 inform (input_location
,
13334 " when instantiating default argument for call to %qD", fn
);
13336 pop_deferring_access_checks ();
13337 pop_access_scope (fn
);
13338 pop_from_top_level ();
13340 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13343 defarg_inst
= tree_cache_map::create_ggc (37);
13344 defarg_inst
->put (parm
, arg
);
13350 /* Substitute into all the default arguments for FN. */
13353 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13358 tmpl_args
= DECL_TI_ARGS (fn
);
13360 /* If this function is not yet instantiated, we certainly don't need
13361 its default arguments. */
13362 if (uses_template_parms (tmpl_args
))
13364 /* Don't do this again for clones. */
13365 if (DECL_CLONED_FUNCTION_P (fn
))
13369 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13371 arg
= TREE_CHAIN (arg
), ++i
)
13372 if (TREE_PURPOSE (arg
))
13373 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13375 TREE_PURPOSE (arg
),
13379 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13380 static GTY((cache
)) tree_cache_map
*explicit_specifier_map
;
13382 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13385 store_explicit_specifier (tree v
, tree t
)
13387 if (!explicit_specifier_map
)
13388 explicit_specifier_map
= tree_cache_map::create_ggc (37);
13389 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13390 explicit_specifier_map
->put (v
, t
);
13393 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13396 lookup_explicit_specifier (tree v
)
13398 return *explicit_specifier_map
->get (v
);
13401 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13404 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13405 tree lambda_fntype
)
13407 tree gen_tmpl
, argvec
;
13408 hashval_t hash
= 0;
13411 /* Nobody should be tsubst'ing into non-template functions. */
13412 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13414 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13416 /* If T is not dependent, just return it. */
13417 if (!uses_template_parms (DECL_TI_ARGS (t
))
13418 && !LAMBDA_FUNCTION_P (t
))
13421 /* Calculate the most general template of which R is a
13423 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13425 /* We're substituting a lambda function under tsubst_lambda_expr but not
13426 directly from it; find the matching function we're already inside.
13427 But don't do this if T is a generic lambda with a single level of
13428 template parms, as in that case we're doing a normal instantiation. */
13429 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13430 && (!generic_lambda_fn_p (t
)
13431 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13432 return enclosing_instantiation_of (t
);
13434 /* Calculate the complete set of arguments used to
13436 argvec
= tsubst_template_args (DECL_TI_ARGS
13437 (DECL_TEMPLATE_RESULT
13438 (DECL_TI_TEMPLATE (t
))),
13439 args
, complain
, in_decl
);
13440 if (argvec
== error_mark_node
)
13441 return error_mark_node
;
13443 /* Check to see if we already have this specialization. */
13444 if (!lambda_fntype
)
13446 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13447 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13451 /* We can see more levels of arguments than parameters if
13452 there was a specialization of a member template, like
13455 template <class T> struct S { template <class U> void f(); }
13456 template <> template <class U> void S<int>::f(U);
13458 Here, we'll be substituting into the specialization,
13459 because that's where we can find the code we actually
13460 want to generate, but we'll have enough arguments for
13461 the most general template.
13463 We also deal with the peculiar case:
13465 template <class T> struct S {
13466 template <class U> friend void f();
13468 template <class U> void f() {}
13470 template void f<double>();
13472 Here, the ARGS for the instantiation of will be {int,
13473 double}. But, we only need as many ARGS as there are
13474 levels of template parameters in CODE_PATTERN. We are
13475 careful not to get fooled into reducing the ARGS in
13478 template <class T> struct S { template <class U> void f(U); }
13479 template <class T> template <> void S<T>::f(int) {}
13481 which we can spot because the pattern will be a
13482 specialization in this case. */
13483 int args_depth
= TMPL_ARGS_DEPTH (args
);
13485 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13487 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13488 args
= get_innermost_template_args (args
, parms_depth
);
13492 /* This special case arises when we have something like this:
13494 template <class T> struct S {
13495 friend void f<int>(int, double);
13498 Here, the DECL_TI_TEMPLATE for the friend declaration
13499 will be an IDENTIFIER_NODE. We are being called from
13500 tsubst_friend_function, and we want only to create a
13501 new decl (R) with appropriate types so that we can call
13502 determine_specialization. */
13503 gen_tmpl
= NULL_TREE
;
13504 argvec
= NULL_TREE
;
13507 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13509 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13510 bool member
= ctx
&& TYPE_P (ctx
);
13512 if (member
&& !closure
)
13513 ctx
= tsubst_aggr_type (ctx
, args
,
13514 complain
, t
, /*entering_scope=*/1);
13516 tree type
= (lambda_fntype
? lambda_fntype
13517 : tsubst (TREE_TYPE (t
), args
,
13518 complain
| tf_fndecl_type
, in_decl
));
13519 if (type
== error_mark_node
)
13520 return error_mark_node
;
13522 /* If we hit excessive deduction depth, the type is bogus even if
13523 it isn't error_mark_node, so don't build a decl. */
13524 if (excessive_deduction_depth
)
13525 return error_mark_node
;
13527 /* We do NOT check for matching decls pushed separately at this
13528 point, as they may not represent instantiations of this
13529 template, and in any case are considered separate under the
13531 tree r
= copy_decl (t
);
13532 DECL_USE_TEMPLATE (r
) = 0;
13533 TREE_TYPE (r
) = type
;
13534 /* Clear out the mangled name and RTL for the instantiation. */
13535 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13536 SET_DECL_RTL (r
, NULL
);
13537 /* Leave DECL_INITIAL set on deleted instantiations. */
13538 if (!DECL_DELETED_FN (r
))
13539 DECL_INITIAL (r
) = NULL_TREE
;
13540 DECL_CONTEXT (r
) = ctx
;
13542 /* Handle explicit(dependent-expr). */
13543 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13545 tree spec
= lookup_explicit_specifier (t
);
13546 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13547 /*function_p=*/false,
13549 spec
= build_explicit_specifier (spec
, complain
);
13550 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13553 /* OpenMP UDRs have the only argument a reference to the declared
13554 type. We want to diagnose if the declared type is a reference,
13555 which is invalid, but as references to references are usually
13556 quietly merged, diagnose it here. */
13557 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13560 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13561 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13562 if (TYPE_REF_P (argtype
))
13563 error_at (DECL_SOURCE_LOCATION (t
),
13564 "reference type %qT in "
13565 "%<#pragma omp declare reduction%>", argtype
);
13566 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13567 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13571 if (member
&& DECL_CONV_FN_P (r
))
13572 /* Type-conversion operator. Reconstruct the name, in
13573 case it's the name of one of the template's parameters. */
13574 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13576 tree parms
= DECL_ARGUMENTS (t
);
13578 parms
= DECL_CHAIN (parms
);
13579 parms
= tsubst (parms
, args
, complain
, t
);
13580 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13581 DECL_CONTEXT (parm
) = r
;
13584 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13585 DECL_CHAIN (tparm
) = parms
;
13588 DECL_ARGUMENTS (r
) = parms
;
13589 DECL_RESULT (r
) = NULL_TREE
;
13591 TREE_STATIC (r
) = 0;
13592 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13593 DECL_EXTERNAL (r
) = 1;
13594 /* If this is an instantiation of a function with internal
13595 linkage, we already know what object file linkage will be
13596 assigned to the instantiation. */
13597 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13598 DECL_DEFER_OUTPUT (r
) = 0;
13599 DECL_CHAIN (r
) = NULL_TREE
;
13600 DECL_PENDING_INLINE_INFO (r
) = 0;
13601 DECL_PENDING_INLINE_P (r
) = 0;
13602 DECL_SAVED_TREE (r
) = NULL_TREE
;
13603 DECL_STRUCT_FUNCTION (r
) = NULL
;
13605 /* We'll re-clone as appropriate in instantiate_template. */
13606 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13608 /* If we aren't complaining now, return on error before we register
13609 the specialization so that we'll complain eventually. */
13610 if ((complain
& tf_error
) == 0
13611 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13612 && !grok_op_properties (r
, /*complain=*/false))
13613 return error_mark_node
;
13615 /* Associate the constraints directly with the instantiation. We
13616 don't substitute through the constraints; that's only done when
13617 they are checked. */
13618 if (tree ci
= get_constraints (t
))
13619 set_constraints (r
, ci
);
13621 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13622 SET_DECL_FRIEND_CONTEXT (r
,
13623 tsubst (DECL_FRIEND_CONTEXT (t
),
13624 args
, complain
, in_decl
));
13626 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13627 this in the special friend case mentioned above where
13628 GEN_TMPL is NULL. */
13629 if (gen_tmpl
&& !closure
)
13631 DECL_TEMPLATE_INFO (r
)
13632 = build_template_info (gen_tmpl
, argvec
);
13633 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13636 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13638 /* We instantiated this while substituting into
13639 the type earlier (template/friend54.C). */
13642 /* We're not supposed to instantiate default arguments
13643 until they are called, for a template. But, for a
13646 template <class T> void f ()
13647 { extern void g(int i = T()); }
13649 we should do the substitution when the template is
13650 instantiated. We handle the member function case in
13651 instantiate_class_template since the default arguments
13652 might refer to other members of the class. */
13654 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13655 && !uses_template_parms (argvec
))
13656 tsubst_default_arguments (r
, complain
);
13659 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13661 /* Copy the list of befriending classes. */
13662 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13664 friends
= &TREE_CHAIN (*friends
))
13666 *friends
= copy_node (*friends
);
13667 TREE_VALUE (*friends
)
13668 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13671 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13673 maybe_retrofit_in_chrg (r
);
13674 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13675 return error_mark_node
;
13676 /* If this is an instantiation of a member template, clone it.
13677 If it isn't, that'll be handled by
13678 clone_constructors_and_destructors. */
13679 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13680 clone_function_decl (r
, /*update_methods=*/false);
13682 else if ((complain
& tf_error
) != 0
13683 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13684 && !grok_op_properties (r
, /*complain=*/true))
13685 return error_mark_node
;
13687 /* Possibly limit visibility based on template args. */
13688 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13689 if (DECL_VISIBILITY_SPECIFIED (t
))
13691 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13692 DECL_ATTRIBUTES (r
)
13693 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13695 determine_visibility (r
);
13696 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13697 && !processing_template_decl
)
13698 defaulted_late_check (r
);
13700 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13701 args
, complain
, in_decl
);
13703 if (tree attr
= lookup_attribute ("omp declare variant base",
13704 DECL_ATTRIBUTES (r
)))
13705 omp_declare_variant_finalize (r
, attr
);
13710 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13713 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13714 tree lambda_fntype
)
13716 /* We can get here when processing a member function template,
13717 member class template, or template template parameter. */
13718 tree decl
= DECL_TEMPLATE_RESULT (t
);
13724 hashval_t hash
= 0;
13726 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13728 /* Template template parameter is treated here. */
13729 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13730 if (new_type
== error_mark_node
)
13731 r
= error_mark_node
;
13732 /* If we get a real template back, return it. This can happen in
13733 the context of most_specialized_partial_spec. */
13734 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13737 /* The new TEMPLATE_DECL was built in
13738 reduce_template_parm_level. */
13739 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13743 if (!lambda_fntype
)
13745 /* We might already have an instance of this template.
13746 The ARGS are for the surrounding class type, so the
13747 full args contain the tsubst'd args for the context,
13748 plus the innermost args from the template decl. */
13749 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13750 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13751 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13752 /* Because this is a template, the arguments will still be
13753 dependent, even after substitution. If
13754 PROCESSING_TEMPLATE_DECL is not set, the dependency
13755 predicates will short-circuit. */
13756 ++processing_template_decl
;
13757 full_args
= tsubst_template_args (tmpl_args
, args
,
13758 complain
, in_decl
);
13759 --processing_template_decl
;
13760 if (full_args
== error_mark_node
)
13761 return error_mark_node
;
13763 /* If this is a default template template argument,
13764 tsubst might not have changed anything. */
13765 if (full_args
== tmpl_args
)
13768 hash
= hash_tmpl_and_args (t
, full_args
);
13769 spec
= retrieve_specialization (t
, full_args
, hash
);
13770 if (spec
!= NULL_TREE
)
13773 /* Type partial instantiations are stored as the type by
13774 lookup_template_class_1, not here as the template. */
13775 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
13780 /* Make a new template decl. It will be similar to the
13781 original, but will record the current template arguments.
13782 We also create a new function declaration, which is just
13783 like the old one, but points to this new template, rather
13784 than the old one. */
13786 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13787 DECL_CHAIN (r
) = NULL_TREE
;
13789 // Build new template info linking to the original template decl.
13790 if (!lambda_fntype
)
13792 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13793 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13796 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13798 /* The template parameters for this new template are all the
13799 template parameters for the old template, except the
13800 outermost level of parameters. */
13801 DECL_TEMPLATE_PARMS (r
)
13802 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13805 if (TREE_CODE (decl
) == TYPE_DECL
13806 && !TYPE_DECL_ALIAS_P (decl
))
13809 ++processing_template_decl
;
13810 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13811 --processing_template_decl
;
13812 if (new_type
== error_mark_node
)
13813 return error_mark_node
;
13815 TREE_TYPE (r
) = new_type
;
13816 /* For a partial specialization, we need to keep pointing to
13817 the primary template. */
13818 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13819 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13820 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13821 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13822 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13827 ++processing_template_decl
;
13828 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13829 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13831 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13832 --processing_template_decl
;
13833 if (new_decl
== error_mark_node
)
13834 return error_mark_node
;
13836 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13837 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13838 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13841 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13842 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13846 DECL_TI_TEMPLATE (new_decl
) = r
;
13847 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13851 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13852 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13854 if (PRIMARY_TEMPLATE_P (t
))
13855 DECL_PRIMARY_TEMPLATE (r
) = r
;
13857 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13859 /* Record this non-type partial instantiation. */
13860 register_specialization (r
, t
,
13861 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13867 /* True if FN is the op() for a lambda in an uninstantiated template. */
13870 lambda_fn_in_template_p (tree fn
)
13872 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13874 tree closure
= DECL_CONTEXT (fn
);
13875 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13878 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13879 which the above is true. */
13882 instantiated_lambda_fn_p (tree fn
)
13884 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13886 tree closure
= DECL_CONTEXT (fn
);
13887 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13888 return LAMBDA_EXPR_INSTANTIATED (lam
);
13891 /* We're instantiating a variable from template function TCTX. Return the
13892 corresponding current enclosing scope. This gets complicated because lambda
13893 functions in templates are regenerated rather than instantiated, but generic
13894 lambda functions are subsequently instantiated. */
13897 enclosing_instantiation_of (tree otctx
)
13900 tree fn
= current_function_decl
;
13901 int lambda_count
= 0;
13903 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
13904 || instantiated_lambda_fn_p (tctx
));
13905 tctx
= decl_function_context (tctx
))
13907 for (; fn
; fn
= decl_function_context (fn
))
13910 int flambda_count
= 0;
13911 for (; fn
&& instantiated_lambda_fn_p (fn
);
13912 fn
= decl_function_context (fn
))
13914 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13915 ? most_general_template (fn
) != most_general_template (tctx
)
13918 if (flambda_count
!= lambda_count
)
13920 gcc_assert (flambda_count
> lambda_count
);
13921 for (; flambda_count
> lambda_count
; --flambda_count
)
13922 ofn
= decl_function_context (ofn
);
13924 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13925 || DECL_CONV_FN_P (ofn
));
13928 gcc_unreachable ();
13931 /* Substitute the ARGS into the T, which is a _DECL. Return the
13932 result of the substitution. Issue error and warning messages under
13933 control of COMPLAIN. */
13936 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13938 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13939 location_t saved_loc
;
13940 tree r
= NULL_TREE
;
13942 hashval_t hash
= 0;
13944 /* Set the filename and linenumber to improve error-reporting. */
13945 saved_loc
= input_location
;
13946 input_location
= DECL_SOURCE_LOCATION (t
);
13948 switch (TREE_CODE (t
))
13950 case TEMPLATE_DECL
:
13951 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13954 case FUNCTION_DECL
:
13955 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13960 tree type
= NULL_TREE
;
13962 tree expanded_types
= NULL_TREE
;
13963 tree prev_r
= NULL_TREE
;
13964 tree first_r
= NULL_TREE
;
13966 if (DECL_PACK_P (t
))
13968 /* If there is a local specialization that isn't a
13969 parameter pack, it means that we're doing a "simple"
13970 substitution from inside tsubst_pack_expansion. Just
13971 return the local specialization (which will be a single
13973 tree spec
= retrieve_local_specialization (t
);
13975 && TREE_CODE (spec
) == PARM_DECL
13976 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13979 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13980 the parameters in this function parameter pack. */
13981 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13982 complain
, in_decl
);
13983 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13985 len
= TREE_VEC_LENGTH (expanded_types
);
13987 /* Zero-length parameter packs are boring. Just substitute
13990 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13995 /* All we did was update the type. Make a note of that. */
13996 type
= expanded_types
;
13997 expanded_types
= NULL_TREE
;
14001 /* Loop through all of the parameters we'll build. When T is
14002 a function parameter pack, LEN is the number of expanded
14003 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14005 for (i
= 0; i
< len
; ++i
)
14009 if (DECL_TEMPLATE_PARM_P (t
))
14010 SET_DECL_TEMPLATE_PARM_P (r
);
14012 if (expanded_types
)
14013 /* We're on the Ith parameter of the function parameter
14016 /* Get the Ith type. */
14017 type
= TREE_VEC_ELT (expanded_types
, i
);
14019 /* Rename the parameter to include the index. */
14021 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14024 /* We're dealing with a normal parameter. */
14025 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14027 type
= type_decays_to (type
);
14028 TREE_TYPE (r
) = type
;
14029 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14031 if (DECL_INITIAL (r
))
14033 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14034 DECL_INITIAL (r
) = TREE_TYPE (r
);
14036 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14037 complain
, in_decl
);
14040 DECL_CONTEXT (r
) = NULL_TREE
;
14042 if (!DECL_TEMPLATE_PARM_P (r
))
14043 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14045 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14046 args
, complain
, in_decl
);
14048 /* Keep track of the first new parameter we
14049 generate. That's what will be returned to the
14054 /* Build a proper chain of parameters when substituting
14055 into a function parameter pack. */
14057 DECL_CHAIN (prev_r
) = r
;
14060 /* If cp_unevaluated_operand is set, we're just looking for a
14061 single dummy parameter, so don't keep going. */
14062 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14063 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14064 complain
, DECL_CHAIN (t
));
14066 /* FIRST_R contains the start of the chain we've built. */
14073 tree type
= NULL_TREE
;
14074 tree vec
= NULL_TREE
;
14075 tree expanded_types
= NULL_TREE
;
14078 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14080 /* This field is a lambda capture pack. Return a TREE_VEC of
14081 the expanded fields to instantiate_class_template_1. */
14082 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14083 complain
, in_decl
);
14084 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14086 len
= TREE_VEC_LENGTH (expanded_types
);
14087 vec
= make_tree_vec (len
);
14091 /* All we did was update the type. Make a note of that. */
14092 type
= expanded_types
;
14093 expanded_types
= NULL_TREE
;
14097 for (int i
= 0; i
< len
; ++i
)
14100 if (expanded_types
)
14102 type
= TREE_VEC_ELT (expanded_types
, i
);
14104 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14107 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14109 if (type
== error_mark_node
)
14110 RETURN (error_mark_node
);
14111 TREE_TYPE (r
) = type
;
14112 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14114 if (DECL_C_BIT_FIELD (r
))
14115 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14117 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14118 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14120 /*integral_constant_expression_p=*/true);
14121 if (DECL_INITIAL (t
))
14123 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14124 NSDMI in perform_member_init. Still set DECL_INITIAL
14125 so that we know there is one. */
14126 DECL_INITIAL (r
) = void_node
;
14127 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14128 retrofit_lang_decl (r
);
14129 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14131 /* We don't have to set DECL_CONTEXT here; it is set by
14132 finish_member_declaration. */
14133 DECL_CHAIN (r
) = NULL_TREE
;
14135 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14136 args
, complain
, in_decl
);
14139 TREE_VEC_ELT (vec
, i
) = r
;
14148 /* We reach here only for member using decls. We also need to check
14149 uses_template_parms because DECL_DEPENDENT_P is not set for a
14150 using-declaration that designates a member of the current
14151 instantiation (c++/53549). */
14152 if (DECL_DEPENDENT_P (t
)
14153 || uses_template_parms (USING_DECL_SCOPE (t
)))
14155 tree scope
= USING_DECL_SCOPE (t
);
14156 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14157 if (PACK_EXPANSION_P (scope
))
14159 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14160 int len
= TREE_VEC_LENGTH (vec
);
14161 r
= make_tree_vec (len
);
14162 for (int i
= 0; i
< len
; ++i
)
14164 tree escope
= TREE_VEC_ELT (vec
, i
);
14165 tree elt
= do_class_using_decl (escope
, name
);
14168 r
= error_mark_node
;
14173 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14174 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14176 TREE_VEC_ELT (r
, i
) = elt
;
14181 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14182 complain
, in_decl
);
14183 r
= do_class_using_decl (inst_scope
, name
);
14185 r
= error_mark_node
;
14188 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14189 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14196 DECL_CHAIN (r
) = NULL_TREE
;
14203 tree argvec
= NULL_TREE
;
14204 tree gen_tmpl
= NULL_TREE
;
14206 tree tmpl
= NULL_TREE
;
14208 tree type
= NULL_TREE
;
14211 if (TREE_TYPE (t
) == error_mark_node
)
14212 RETURN (error_mark_node
);
14214 if (TREE_CODE (t
) == TYPE_DECL
14215 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14217 /* If this is the canonical decl, we don't have to
14218 mess with instantiations, and often we can't (for
14219 typename, template type parms and such). Note that
14220 TYPE_NAME is not correct for the above test if
14221 we've copied the type for a typedef. */
14222 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14223 if (type
== error_mark_node
)
14224 RETURN (error_mark_node
);
14225 r
= TYPE_NAME (type
);
14229 /* Check to see if we already have the specialization we
14232 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14234 /* T is a static data member or namespace-scope entity.
14235 We have to substitute into namespace-scope variables
14236 (not just variable templates) because of cases like:
14238 template <class T> void f() { extern T t; }
14240 where the entity referenced is not known until
14241 instantiation time. */
14243 ctx
= DECL_CONTEXT (t
);
14244 if (DECL_CLASS_SCOPE_P (t
))
14246 ctx
= tsubst_aggr_type (ctx
, args
,
14248 in_decl
, /*entering_scope=*/1);
14249 /* If CTX is unchanged, then T is in fact the
14250 specialization we want. That situation occurs when
14251 referencing a static data member within in its own
14252 class. We can use pointer equality, rather than
14253 same_type_p, because DECL_CONTEXT is always
14255 if (ctx
== DECL_CONTEXT (t
)
14256 /* ... unless T is a member template; in which
14257 case our caller can be willing to create a
14258 specialization of that template represented
14260 && !(DECL_TI_TEMPLATE (t
)
14261 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14267 tmpl
= DECL_TI_TEMPLATE (t
);
14268 gen_tmpl
= most_general_template (tmpl
);
14269 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14270 if (argvec
!= error_mark_node
)
14271 argvec
= (coerce_innermost_template_parms
14272 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14273 argvec
, t
, complain
,
14274 /*all*/true, /*defarg*/true));
14275 if (argvec
== error_mark_node
)
14276 RETURN (error_mark_node
);
14277 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14278 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14283 /* A local variable. */
14285 /* Subsequent calls to pushdecl will fill this in. */
14287 /* Unless this is a reference to a static variable from an
14288 enclosing function, in which case we need to fill it in now. */
14289 if (TREE_STATIC (t
))
14291 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14292 if (fn
!= current_function_decl
)
14295 spec
= retrieve_local_specialization (t
);
14297 /* If we already have the specialization we need, there is
14298 nothing more to do. */
14305 /* Create a new node for the specialization we need. */
14306 if (type
== NULL_TREE
)
14308 if (is_typedef_decl (t
))
14309 type
= DECL_ORIGINAL_TYPE (t
);
14311 type
= TREE_TYPE (t
);
14313 && VAR_HAD_UNKNOWN_BOUND (t
)
14314 && type
!= error_mark_node
)
14315 type
= strip_array_domain (type
);
14316 tree sub_args
= args
;
14317 if (tree auto_node
= type_uses_auto (type
))
14319 /* Mask off any template args past the variable's context so we
14320 don't replace the auto with an unrelated argument. */
14321 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14322 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14324 /* This should never happen with the new lambda instantiation
14325 model, but keep the handling just in case. */
14326 gcc_assert (!CHECKING_P
),
14327 sub_args
= strip_innermost_template_args (args
, extra
);
14329 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14330 /* Substituting the type might have recursively instantiated this
14331 same alias (c++/86171). */
14332 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14333 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14342 DECL_INITIALIZED_P (r
) = 0;
14343 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14344 if (type
== error_mark_node
)
14345 RETURN (error_mark_node
);
14346 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14348 /* It may seem that this case cannot occur, since:
14353 declares a function, not a variable. However:
14356 template <typename T> void g() { T t; }
14357 template void g<f>();
14359 is an attempt to declare a variable with function
14361 error ("variable %qD has function type",
14362 /* R is not yet sufficiently initialized, so we
14363 just use its name. */
14365 RETURN (error_mark_node
);
14367 type
= complete_type (type
);
14368 /* Wait until cp_finish_decl to set this again, to handle
14369 circular dependency (template/instantiate6.C). */
14370 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14371 type
= check_var_type (DECL_NAME (r
), type
,
14372 DECL_SOURCE_LOCATION (r
));
14373 if (DECL_HAS_VALUE_EXPR_P (t
))
14375 tree ve
= DECL_VALUE_EXPR (t
);
14376 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14377 /*constant_expression_p=*/false);
14378 if (REFERENCE_REF_P (ve
))
14380 gcc_assert (TYPE_REF_P (type
));
14381 ve
= TREE_OPERAND (ve
, 0);
14383 SET_DECL_VALUE_EXPR (r
, ve
);
14385 if (CP_DECL_THREAD_LOCAL_P (r
)
14386 && !processing_template_decl
)
14387 set_decl_tls_model (r
, decl_default_tls_model (r
));
14389 else if (DECL_SELF_REFERENCE_P (t
))
14390 SET_DECL_SELF_REFERENCE_P (r
);
14391 TREE_TYPE (r
) = type
;
14392 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14393 DECL_CONTEXT (r
) = ctx
;
14394 /* Clear out the mangled name and RTL for the instantiation. */
14395 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14396 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14397 SET_DECL_RTL (r
, NULL
);
14398 /* The initializer must not be expanded until it is required;
14399 see [temp.inst]. */
14400 DECL_INITIAL (r
) = NULL_TREE
;
14401 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14404 if (DECL_LANG_SPECIFIC (r
))
14405 SET_DECL_DEPENDENT_INIT_P (r
, false);
14407 SET_DECL_MODE (r
, VOIDmode
);
14409 /* Possibly limit visibility based on template args. */
14410 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14411 if (DECL_VISIBILITY_SPECIFIED (t
))
14413 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14414 DECL_ATTRIBUTES (r
)
14415 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14417 determine_visibility (r
);
14422 /* A static data member declaration is always marked
14423 external when it is declared in-class, even if an
14424 initializer is present. We mimic the non-template
14425 processing here. */
14426 DECL_EXTERNAL (r
) = 1;
14427 if (DECL_NAMESPACE_SCOPE_P (t
))
14428 DECL_NOT_REALLY_EXTERN (r
) = 1;
14430 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14431 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14432 /* Remember whether we require constant initialization of
14433 a non-constant template variable. */
14434 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14435 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14436 if (!error_operand_p (r
) || (complain
& tf_error
))
14437 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14441 if (DECL_LANG_SPECIFIC (r
))
14442 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14443 if (!cp_unevaluated_operand
)
14444 register_local_specialization (r
, t
);
14447 DECL_CHAIN (r
) = NULL_TREE
;
14449 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14451 args
, complain
, in_decl
);
14453 /* Preserve a typedef that names a type. */
14454 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14456 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14457 set_underlying_type (r
);
14458 if (TYPE_DECL_ALIAS_P (r
))
14459 /* An alias template specialization can be dependent
14460 even if its underlying type is not. */
14461 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14464 layout_decl (r
, 0);
14469 gcc_unreachable ();
14474 /* Restore the file and line information. */
14475 input_location
= saved_loc
;
14480 /* Substitute into the complete parameter type list PARMS. */
14483 tsubst_function_parms (tree parms
,
14485 tsubst_flags_t complain
,
14488 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14491 /* Substitute into the ARG_TYPES of a function type.
14492 If END is a TREE_CHAIN, leave it and any following types
14496 tsubst_arg_types (tree arg_types
,
14499 tsubst_flags_t complain
,
14502 tree remaining_arg_types
;
14503 tree type
= NULL_TREE
;
14505 tree expanded_args
= NULL_TREE
;
14508 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14511 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14512 args
, end
, complain
, in_decl
);
14513 if (remaining_arg_types
== error_mark_node
)
14514 return error_mark_node
;
14516 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14518 /* For a pack expansion, perform substitution on the
14519 entire expression. Later on, we'll handle the arguments
14521 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14522 args
, complain
, in_decl
);
14524 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14525 /* So that we'll spin through the parameters, one by one. */
14526 i
= TREE_VEC_LENGTH (expanded_args
);
14529 /* We only partially substituted into the parameter
14530 pack. Our type is TYPE_PACK_EXPANSION. */
14531 type
= expanded_args
;
14532 expanded_args
= NULL_TREE
;
14540 type
= TREE_VEC_ELT (expanded_args
, i
);
14542 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14544 if (type
== error_mark_node
)
14545 return error_mark_node
;
14546 if (VOID_TYPE_P (type
))
14548 if (complain
& tf_error
)
14550 error ("invalid parameter type %qT", type
);
14552 error ("in declaration %q+D", in_decl
);
14554 return error_mark_node
;
14557 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14558 return error_mark_node
;
14560 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14561 top-level qualifiers as required. */
14562 type
= cv_unqualified (type_decays_to (type
));
14564 /* We do not substitute into default arguments here. The standard
14565 mandates that they be instantiated only when needed, which is
14566 done in build_over_call. */
14567 default_arg
= TREE_PURPOSE (arg_types
);
14569 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14570 since the new op() won't have any associated template arguments for us
14571 to refer to later. */
14572 if (lambda_fn_in_template_p (in_decl
))
14573 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14574 false/*fn*/, false/*constexpr*/);
14576 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14578 /* We've instantiated a template before its default arguments
14579 have been parsed. This can happen for a nested template
14580 class, and is not an error unless we require the default
14581 argument in a call of this function. */
14582 remaining_arg_types
=
14583 tree_cons (default_arg
, type
, remaining_arg_types
);
14584 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14585 remaining_arg_types
);
14588 remaining_arg_types
=
14589 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14592 return remaining_arg_types
;
14595 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14596 *not* handle the exception-specification for FNTYPE, because the
14597 initial substitution of explicitly provided template parameters
14598 during argument deduction forbids substitution into the
14599 exception-specification:
14603 All references in the function type of the function template to the
14604 corresponding template parameters are replaced by the specified tem-
14605 plate argument values. If a substitution in a template parameter or
14606 in the function type of the function template results in an invalid
14607 type, type deduction fails. [Note: The equivalent substitution in
14608 exception specifications is done only when the function is instanti-
14609 ated, at which point a program is ill-formed if the substitution
14610 results in an invalid type.] */
14613 tsubst_function_type (tree t
,
14615 tsubst_flags_t complain
,
14619 tree arg_types
= NULL_TREE
;
14622 /* The TYPE_CONTEXT is not used for function/method types. */
14623 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14625 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14627 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14629 if (late_return_type_p
)
14631 /* Substitute the argument types. */
14632 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14633 complain
, in_decl
);
14634 if (arg_types
== error_mark_node
)
14635 return error_mark_node
;
14637 tree save_ccp
= current_class_ptr
;
14638 tree save_ccr
= current_class_ref
;
14639 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14640 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14641 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14644 /* DR 1207: 'this' is in scope in the trailing return type. */
14645 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14648 /* Substitute the return type. */
14649 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14653 current_class_ptr
= save_ccp
;
14654 current_class_ref
= save_ccr
;
14658 /* Substitute the return type. */
14659 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14661 if (return_type
== error_mark_node
)
14662 return error_mark_node
;
14663 /* DR 486 clarifies that creation of a function type with an
14664 invalid return type is a deduction failure. */
14665 if (TREE_CODE (return_type
) == ARRAY_TYPE
14666 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14668 if (complain
& tf_error
)
14670 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14671 error ("function returning an array");
14673 error ("function returning a function");
14675 return error_mark_node
;
14678 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14679 return error_mark_node
;
14681 if (!late_return_type_p
)
14683 /* Substitute the argument types. */
14684 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14685 complain
, in_decl
);
14686 if (arg_types
== error_mark_node
)
14687 return error_mark_node
;
14690 /* Construct a new type node and return it. */
14691 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14693 fntype
= build_function_type (return_type
, arg_types
);
14694 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14698 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14699 /* Don't pick up extra function qualifiers from the basetype. */
14700 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14701 if (! MAYBE_CLASS_TYPE_P (r
))
14705 Type deduction may fail for any of the following
14708 -- Attempting to create "pointer to member of T" when T
14709 is not a class type. */
14710 if (complain
& tf_error
)
14711 error ("creating pointer to member function of non-class type %qT",
14713 return error_mark_node
;
14716 fntype
= build_method_type_directly (r
, return_type
,
14717 TREE_CHAIN (arg_types
));
14719 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14721 /* See comment above. */
14722 tree raises
= NULL_TREE
;
14723 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14724 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14729 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14730 ARGS into that specification, and return the substituted
14731 specification. If there is no specification, return NULL_TREE. */
14734 tsubst_exception_specification (tree fntype
,
14736 tsubst_flags_t complain
,
14743 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14744 new_specs
= NULL_TREE
;
14745 if (specs
&& TREE_PURPOSE (specs
))
14747 /* A noexcept-specifier. */
14748 tree expr
= TREE_PURPOSE (specs
);
14749 if (TREE_CODE (expr
) == INTEGER_CST
)
14753 /* Defer instantiation of noexcept-specifiers to avoid
14754 excessive instantiations (c++/49107). */
14755 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14756 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14758 /* We already partially instantiated this member template,
14759 so combine the new args with the old. */
14760 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14761 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14762 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14763 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14767 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14768 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14773 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14775 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14777 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14779 new_specs
= tsubst_copy_and_build
14780 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14781 /*integral_constant_expression_p=*/true);
14783 new_specs
= build_noexcept_spec (new_specs
, complain
);
14787 if (! TREE_VALUE (specs
))
14794 tree expanded_specs
= NULL_TREE
;
14796 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14798 /* Expand the pack expansion type. */
14799 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14803 if (expanded_specs
== error_mark_node
)
14804 return error_mark_node
;
14805 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14806 len
= TREE_VEC_LENGTH (expanded_specs
);
14809 /* We're substituting into a member template, so
14810 we got a TYPE_PACK_EXPANSION back. Add that
14811 expansion and move on. */
14812 gcc_assert (TREE_CODE (expanded_specs
)
14813 == TYPE_PACK_EXPANSION
);
14814 new_specs
= add_exception_specifier (new_specs
,
14817 specs
= TREE_CHAIN (specs
);
14822 for (i
= 0; i
< len
; ++i
)
14824 if (expanded_specs
)
14825 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14827 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14828 if (spec
== error_mark_node
)
14830 new_specs
= add_exception_specifier (new_specs
, spec
,
14834 specs
= TREE_CHAIN (specs
);
14840 /* Take the tree structure T and replace template parameters used
14841 therein with the argument vector ARGS. IN_DECL is an associated
14842 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14843 Issue error and warning messages under control of COMPLAIN. Note
14844 that we must be relatively non-tolerant of extensions here, in
14845 order to preserve conformance; if we allow substitutions that
14846 should not be allowed, we may allow argument deductions that should
14847 not succeed, and therefore report ambiguous overload situations
14848 where there are none. In theory, we could allow the substitution,
14849 but indicate that it should have failed, and allow our caller to
14850 make sure that the right thing happens, but we don't try to do this
14853 This function is used for dealing with types, decls and the like;
14854 for expressions, use tsubst_expr or tsubst_copy. */
14857 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14859 enum tree_code code
;
14860 tree type
, r
= NULL_TREE
;
14862 if (t
== NULL_TREE
|| t
== error_mark_node
14863 || t
== integer_type_node
14864 || t
== void_type_node
14865 || t
== char_type_node
14866 || t
== unknown_type_node
14867 || TREE_CODE (t
) == NAMESPACE_DECL
14868 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14872 return tsubst_decl (t
, args
, complain
);
14874 if (args
== NULL_TREE
)
14877 code
= TREE_CODE (t
);
14879 if (code
== IDENTIFIER_NODE
)
14880 type
= IDENTIFIER_TYPE_VALUE (t
);
14882 type
= TREE_TYPE (t
);
14884 gcc_assert (type
!= unknown_type_node
);
14886 /* Reuse typedefs. We need to do this to handle dependent attributes,
14887 such as attribute aligned. */
14889 && typedef_variant_p (t
))
14891 tree decl
= TYPE_NAME (t
);
14893 if (alias_template_specialization_p (t
))
14895 /* DECL represents an alias template and we want to
14897 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14898 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14899 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14901 else if (DECL_CLASS_SCOPE_P (decl
)
14902 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14903 && uses_template_parms (DECL_CONTEXT (decl
)))
14905 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14906 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14907 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14909 else if (DECL_FUNCTION_SCOPE_P (decl
)
14910 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14911 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14912 r
= retrieve_local_specialization (decl
);
14914 /* The typedef is from a non-template context. */
14920 r
= cp_build_qualified_type_real
14921 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14922 complain
| tf_ignore_bad_quals
);
14927 /* We don't have an instantiation yet, so drop the typedef. */
14928 int quals
= cp_type_quals (t
);
14929 t
= DECL_ORIGINAL_TYPE (decl
);
14930 t
= cp_build_qualified_type_real (t
, quals
,
14931 complain
| tf_ignore_bad_quals
);
14935 bool fndecl_type
= (complain
& tf_fndecl_type
);
14936 complain
&= ~tf_fndecl_type
;
14939 && code
!= TYPENAME_TYPE
14940 && code
!= TEMPLATE_TYPE_PARM
14941 && code
!= TEMPLATE_PARM_INDEX
14942 && code
!= IDENTIFIER_NODE
14943 && code
!= FUNCTION_TYPE
14944 && code
!= METHOD_TYPE
)
14945 type
= tsubst (type
, args
, complain
, in_decl
);
14946 if (type
== error_mark_node
)
14947 return error_mark_node
;
14953 case ENUMERAL_TYPE
:
14954 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14955 /*entering_scope=*/0);
14958 case IDENTIFIER_NODE
:
14969 if (t
== integer_type_node
)
14972 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14973 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14977 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14979 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14980 /*integral_constant_expression_p=*/false);
14982 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14984 if (TREE_CODE (max
) == NOP_EXPR
14985 && TREE_SIDE_EFFECTS (omax
)
14986 && !TREE_TYPE (max
))
14987 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14989 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14990 with TREE_SIDE_EFFECTS that indicates this is not an integral
14991 constant expression. */
14992 if (processing_template_decl
14993 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14995 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14996 TREE_SIDE_EFFECTS (max
) = 1;
14999 return compute_array_index_type (NULL_TREE
, max
, complain
);
15002 case TEMPLATE_TYPE_PARM
:
15003 case TEMPLATE_TEMPLATE_PARM
:
15004 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15005 case TEMPLATE_PARM_INDEX
:
15010 tree arg
= NULL_TREE
;
15012 /* Early in template argument deduction substitution, we don't
15013 want to reduce the level of 'auto', or it will be confused
15014 with a normal template parm in subsequent deduction. */
15015 if (is_auto (t
) && (complain
& tf_partial
))
15020 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15021 template_parm_level_and_index (t
, &level
, &idx
);
15023 levels
= TMPL_ARGS_DEPTH (args
);
15024 if (level
<= levels
15025 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15027 arg
= TMPL_ARG (args
, level
, idx
);
15029 /* See through ARGUMENT_PACK_SELECT arguments. */
15030 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15031 arg
= argument_pack_select_arg (arg
);
15034 if (arg
== error_mark_node
)
15035 return error_mark_node
;
15036 else if (arg
!= NULL_TREE
)
15038 if (ARGUMENT_PACK_P (arg
))
15039 /* If ARG is an argument pack, we don't actually want to
15040 perform a substitution here, because substitutions
15041 for argument packs are only done
15042 element-by-element. We can get to this point when
15043 substituting the type of a non-type template
15044 parameter pack, when that type actually contains
15045 template parameter packs from an outer template, e.g.,
15047 template<typename... Types> struct A {
15048 template<Types... Values> struct B { };
15052 if (code
== TEMPLATE_TYPE_PARM
)
15056 /* When building concept checks for the purpose of
15057 deducing placeholders, we can end up with wildcards
15058 where types are expected. Adjust this to the deduced
15060 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15061 arg
= TREE_TYPE (TREE_TYPE (arg
));
15063 gcc_assert (TYPE_P (arg
));
15065 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15067 return cp_build_qualified_type_real
15068 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15070 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15072 /* We are processing a type constructed from a
15073 template template parameter. */
15074 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15075 args
, complain
, in_decl
);
15076 if (argvec
== error_mark_node
)
15077 return error_mark_node
;
15079 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15080 || TREE_CODE (arg
) == TEMPLATE_DECL
15081 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15083 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15084 /* Consider this code:
15086 template <template <class> class Template>
15088 template <class Arg> using Bind = Template<Arg>;
15091 template <template <class> class Template, class Arg>
15092 using Instantiate = Template<Arg>; //#0
15094 template <template <class> class Template,
15097 Instantiate<Internal<Template>::template Bind,
15100 When #1 is parsed, the
15101 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15102 parameter `Template' in #0 matches the
15103 UNBOUND_CLASS_TEMPLATE representing the argument
15104 `Internal<Template>::template Bind'; We then want
15105 to assemble the type `Bind<Argument>' that can't
15106 be fully created right now, because
15107 `Internal<Template>' not being complete, the Bind
15108 template cannot be looked up in that context. So
15109 we need to "store" `Bind<Argument>' for later
15110 when the context of Bind becomes complete. Let's
15111 store that in a TYPENAME_TYPE. */
15112 return make_typename_type (TYPE_CONTEXT (arg
),
15113 build_nt (TEMPLATE_ID_EXPR
,
15114 TYPE_IDENTIFIER (arg
),
15119 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15120 are resolving nested-types in the signature of a
15121 member function templates. Otherwise ARG is a
15122 TEMPLATE_DECL and is the real template to be
15124 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15125 arg
= TYPE_NAME (arg
);
15127 r
= lookup_template_class (arg
,
15129 DECL_CONTEXT (arg
),
15130 /*entering_scope=*/0,
15132 return cp_build_qualified_type_real
15133 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15135 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15138 /* TEMPLATE_PARM_INDEX. */
15139 return convert_from_reference (unshare_expr (arg
));
15143 /* This can happen during the attempted tsubst'ing in
15144 unify. This means that we don't yet have any information
15145 about the template parameter in question. */
15148 /* If we get here, we must have been looking at a parm for a
15149 more deeply nested template. Make a new version of this
15150 template parameter, but with a lower level. */
15153 case TEMPLATE_TYPE_PARM
:
15154 case TEMPLATE_TEMPLATE_PARM
:
15155 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15156 if (cp_type_quals (t
))
15158 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15159 r
= cp_build_qualified_type_real
15160 (r
, cp_type_quals (t
),
15161 complain
| (code
== TEMPLATE_TYPE_PARM
15162 ? tf_ignore_bad_quals
: 0));
15164 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15165 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15166 && (r
= (TEMPLATE_PARM_DESCENDANTS
15167 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15168 && (r
= TREE_TYPE (r
))
15169 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15170 /* Break infinite recursion when substituting the constraints
15171 of a constrained placeholder. */;
15172 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15173 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15174 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15175 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15176 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15177 && (TEMPLATE_PARM_LEVEL (r
)
15178 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15179 /* Cache the simple case of lowering a type parameter. */
15184 TEMPLATE_TYPE_PARM_INDEX (r
)
15185 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15186 r
, levels
, args
, complain
);
15187 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15188 TYPE_MAIN_VARIANT (r
) = r
;
15189 TYPE_POINTER_TO (r
) = NULL_TREE
;
15190 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15192 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15194 /* Propagate constraints on placeholders since they are
15195 only instantiated during satisfaction. */
15196 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15197 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15198 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15200 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15201 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15205 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15206 /* We have reduced the level of the template
15207 template parameter, but not the levels of its
15208 template parameters, so canonical_type_parameter
15209 will not be able to find the canonical template
15210 template parameter for this level. Thus, we
15211 require structural equality checking to compare
15212 TEMPLATE_TEMPLATE_PARMs. */
15213 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15214 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15215 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15217 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15219 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15221 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15222 /* We might need to substitute into the types of non-type
15223 template parameters. */
15224 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15225 complain
, in_decl
);
15226 if (tmpl
== error_mark_node
)
15227 return error_mark_node
;
15228 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15229 complain
, in_decl
);
15230 if (argvec
== error_mark_node
)
15231 return error_mark_node
;
15233 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15234 = build_template_info (tmpl
, argvec
);
15239 case TEMPLATE_PARM_INDEX
:
15240 /* OK, now substitute the type of the non-type parameter. We
15241 couldn't do it earlier because it might be an auto parameter,
15242 and we wouldn't need to if we had an argument. */
15243 type
= tsubst (type
, args
, complain
, in_decl
);
15244 if (type
== error_mark_node
)
15245 return error_mark_node
;
15246 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15250 gcc_unreachable ();
15258 tree purpose
, value
, chain
;
15260 if (t
== void_list_node
)
15263 purpose
= TREE_PURPOSE (t
);
15266 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15267 if (purpose
== error_mark_node
)
15268 return error_mark_node
;
15270 value
= TREE_VALUE (t
);
15273 value
= tsubst (value
, args
, complain
, in_decl
);
15274 if (value
== error_mark_node
)
15275 return error_mark_node
;
15277 chain
= TREE_CHAIN (t
);
15278 if (chain
&& chain
!= void_type_node
)
15280 chain
= tsubst (chain
, args
, complain
, in_decl
);
15281 if (chain
== error_mark_node
)
15282 return error_mark_node
;
15284 if (purpose
== TREE_PURPOSE (t
)
15285 && value
== TREE_VALUE (t
)
15286 && chain
== TREE_CHAIN (t
))
15288 return hash_tree_cons (purpose
, value
, chain
);
15292 /* We should never be tsubsting a binfo. */
15293 gcc_unreachable ();
15296 /* A vector of template arguments. */
15297 gcc_assert (!type
);
15298 return tsubst_template_args (t
, args
, complain
, in_decl
);
15301 case REFERENCE_TYPE
:
15303 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15308 Type deduction may fail for any of the following
15311 -- Attempting to create a pointer to reference type.
15312 -- Attempting to create a reference to a reference type or
15313 a reference to void.
15315 Core issue 106 says that creating a reference to a reference
15316 during instantiation is no longer a cause for failure. We
15317 only enforce this check in strict C++98 mode. */
15318 if ((TYPE_REF_P (type
)
15319 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15320 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15322 static location_t last_loc
;
15324 /* We keep track of the last time we issued this error
15325 message to avoid spewing a ton of messages during a
15326 single bad template instantiation. */
15327 if (complain
& tf_error
15328 && last_loc
!= input_location
)
15330 if (VOID_TYPE_P (type
))
15331 error ("forming reference to void");
15332 else if (code
== POINTER_TYPE
)
15333 error ("forming pointer to reference type %qT", type
);
15335 error ("forming reference to reference type %qT", type
);
15336 last_loc
= input_location
;
15339 return error_mark_node
;
15341 else if (TREE_CODE (type
) == FUNCTION_TYPE
15342 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15343 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15345 if (complain
& tf_error
)
15347 if (code
== POINTER_TYPE
)
15348 error ("forming pointer to qualified function type %qT",
15351 error ("forming reference to qualified function type %qT",
15354 return error_mark_node
;
15356 else if (code
== POINTER_TYPE
)
15358 r
= build_pointer_type (type
);
15359 if (TREE_CODE (type
) == METHOD_TYPE
)
15360 r
= build_ptrmemfunc_type (r
);
15362 else if (TYPE_REF_P (type
))
15363 /* In C++0x, during template argument substitution, when there is an
15364 attempt to create a reference to a reference type, reference
15365 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15367 "If a template-argument for a template-parameter T names a type
15368 that is a reference to a type A, an attempt to create the type
15369 'lvalue reference to cv T' creates the type 'lvalue reference to
15370 A,' while an attempt to create the type type rvalue reference to
15371 cv T' creates the type T"
15373 r
= cp_build_reference_type
15375 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15377 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15378 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15380 if (r
!= error_mark_node
)
15381 /* Will this ever be needed for TYPE_..._TO values? */
15388 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15389 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15393 Type deduction may fail for any of the following
15396 -- Attempting to create "pointer to member of T" when T
15397 is not a class type. */
15398 if (complain
& tf_error
)
15399 error ("creating pointer to member of non-class type %qT", r
);
15400 return error_mark_node
;
15402 if (TYPE_REF_P (type
))
15404 if (complain
& tf_error
)
15405 error ("creating pointer to member reference type %qT", type
);
15406 return error_mark_node
;
15408 if (VOID_TYPE_P (type
))
15410 if (complain
& tf_error
)
15411 error ("creating pointer to member of type void");
15412 return error_mark_node
;
15414 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15415 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15417 /* The type of the implicit object parameter gets its
15418 cv-qualifiers from the FUNCTION_TYPE. */
15421 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15422 type_memfn_rqual (type
));
15423 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15424 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15428 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15432 case FUNCTION_TYPE
:
15437 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15438 if (fntype
== error_mark_node
)
15439 return error_mark_node
;
15441 /* Substitute the exception specification. */
15442 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15443 /*defer_ok*/fndecl_type
);
15444 if (specs
== error_mark_node
)
15445 return error_mark_node
;
15447 fntype
= build_exception_variant (fntype
, specs
);
15452 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15453 if (domain
== error_mark_node
)
15454 return error_mark_node
;
15456 /* As an optimization, we avoid regenerating the array type if
15457 it will obviously be the same as T. */
15458 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15461 /* These checks should match the ones in create_array_type_for_decl.
15465 The deduction may fail for any of the following reasons:
15467 -- Attempting to create an array with an element type that
15468 is void, a function type, or a reference type, or [DR337]
15469 an abstract class type. */
15470 if (VOID_TYPE_P (type
)
15471 || TREE_CODE (type
) == FUNCTION_TYPE
15472 || (TREE_CODE (type
) == ARRAY_TYPE
15473 && TYPE_DOMAIN (type
) == NULL_TREE
)
15474 || TYPE_REF_P (type
))
15476 if (complain
& tf_error
)
15477 error ("creating array of %qT", type
);
15478 return error_mark_node
;
15481 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15482 return error_mark_node
;
15484 r
= build_cplus_array_type (type
, domain
);
15486 if (!valid_array_size_p (input_location
, r
, in_decl
,
15487 (complain
& tf_error
)))
15488 return error_mark_node
;
15490 if (TYPE_USER_ALIGN (t
))
15492 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15493 TYPE_USER_ALIGN (r
) = 1;
15499 case TYPENAME_TYPE
:
15501 tree ctx
= TYPE_CONTEXT (t
);
15502 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15504 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15505 if (ctx
== error_mark_node
15506 || TREE_VEC_LENGTH (ctx
) > 1)
15507 return error_mark_node
;
15508 if (TREE_VEC_LENGTH (ctx
) == 0)
15510 if (complain
& tf_error
)
15511 error ("%qD is instantiated for an empty pack",
15512 TYPENAME_TYPE_FULLNAME (t
));
15513 return error_mark_node
;
15515 ctx
= TREE_VEC_ELT (ctx
, 0);
15518 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15519 /*entering_scope=*/1);
15520 if (ctx
== error_mark_node
)
15521 return error_mark_node
;
15523 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15524 complain
, in_decl
);
15525 if (f
== error_mark_node
)
15526 return error_mark_node
;
15528 if (!MAYBE_CLASS_TYPE_P (ctx
))
15530 if (complain
& tf_error
)
15531 error ("%qT is not a class, struct, or union type", ctx
);
15532 return error_mark_node
;
15534 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15536 /* Normally, make_typename_type does not require that the CTX
15537 have complete type in order to allow things like:
15539 template <class T> struct S { typename S<T>::X Y; };
15541 But, such constructs have already been resolved by this
15542 point, so here CTX really should have complete type, unless
15543 it's a partial instantiation. */
15544 ctx
= complete_type (ctx
);
15545 if (!COMPLETE_TYPE_P (ctx
))
15547 if (complain
& tf_error
)
15548 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15549 return error_mark_node
;
15553 f
= make_typename_type (ctx
, f
, typename_type
,
15554 complain
| tf_keep_type_decl
);
15555 if (f
== error_mark_node
)
15557 if (TREE_CODE (f
) == TYPE_DECL
)
15559 complain
|= tf_ignore_bad_quals
;
15563 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15565 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15567 if (complain
& tf_error
)
15568 error ("%qT resolves to %qT, which is not an enumeration type",
15571 return error_mark_node
;
15573 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15575 if (complain
& tf_error
)
15576 error ("%qT resolves to %qT, which is is not a class type",
15579 return error_mark_node
;
15583 return cp_build_qualified_type_real
15584 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15587 case UNBOUND_CLASS_TEMPLATE
:
15589 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15590 in_decl
, /*entering_scope=*/1);
15591 tree name
= TYPE_IDENTIFIER (t
);
15592 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15594 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15595 return error_mark_node
;
15598 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15599 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15606 ++cp_unevaluated_operand
;
15607 ++c_inhibit_evaluation_warnings
;
15609 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15611 /*integral_constant_expression_p=*/false);
15613 --cp_unevaluated_operand
;
15614 --c_inhibit_evaluation_warnings
;
15616 type
= finish_typeof (type
);
15617 return cp_build_qualified_type_real (type
,
15619 | cp_type_quals (type
),
15623 case DECLTYPE_TYPE
:
15627 ++cp_unevaluated_operand
;
15628 ++c_inhibit_evaluation_warnings
;
15630 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15631 complain
|tf_decltype
, in_decl
,
15632 /*function_p*/false,
15633 /*integral_constant_expression*/false);
15635 --cp_unevaluated_operand
;
15636 --c_inhibit_evaluation_warnings
;
15638 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15639 type
= lambda_capture_field_type (type
,
15640 false /*explicit_init*/,
15641 DECLTYPE_FOR_REF_CAPTURE (t
));
15642 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15643 type
= lambda_proxy_type (type
);
15646 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15647 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15649 /* In a template ~id could be either a complement expression
15650 or an unqualified-id naming a destructor; if instantiating
15651 it produces an expression, it's not an id-expression or
15654 type
= finish_decltype_type (type
, id
, complain
);
15656 return cp_build_qualified_type_real (type
,
15658 | cp_type_quals (type
),
15659 complain
| tf_ignore_bad_quals
);
15662 case UNDERLYING_TYPE
:
15664 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15665 complain
, in_decl
);
15666 return finish_underlying_type (type
);
15669 case TYPE_ARGUMENT_PACK
:
15670 case NONTYPE_ARGUMENT_PACK
:
15674 if (code
== NONTYPE_ARGUMENT_PACK
)
15675 r
= make_node (code
);
15677 r
= cxx_make_type (code
);
15679 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15680 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15681 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15699 /* We should use one of the expression tsubsts for these codes. */
15700 gcc_unreachable ();
15703 sorry ("use of %qs in template", get_tree_code_name (code
));
15704 return error_mark_node
;
15708 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15709 expression on the left-hand side of the "." or "->" operator. We
15710 only do the lookup if we had a dependent BASELINK. Otherwise we
15711 adjust it onto the instantiated heirarchy. */
15714 tsubst_baselink (tree baselink
, tree object_type
,
15715 tree args
, tsubst_flags_t complain
, tree in_decl
)
15717 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15718 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15719 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15721 tree optype
= BASELINK_OPTYPE (baselink
);
15722 optype
= tsubst (optype
, args
, complain
, in_decl
);
15724 tree template_args
= NULL_TREE
;
15725 bool template_id_p
= false;
15726 tree fns
= BASELINK_FUNCTIONS (baselink
);
15727 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15729 template_id_p
= true;
15730 template_args
= TREE_OPERAND (fns
, 1);
15731 fns
= TREE_OPERAND (fns
, 0);
15733 template_args
= tsubst_template_args (template_args
, args
,
15734 complain
, in_decl
);
15737 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15738 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15739 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15743 tree name
= OVL_NAME (fns
);
15744 if (IDENTIFIER_CONV_OP_P (name
))
15745 name
= make_conv_op_name (optype
);
15747 if (name
== complete_dtor_identifier
)
15748 /* Treat as-if non-dependent below. */
15749 dependent_p
= false;
15751 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15754 if ((complain
& tf_error
)
15755 && constructor_name_p (name
, qualifying_scope
))
15756 error ("cannot call constructor %<%T::%D%> directly",
15757 qualifying_scope
, name
);
15758 return error_mark_node
;
15761 if (BASELINK_P (baselink
))
15762 fns
= BASELINK_FUNCTIONS (baselink
);
15765 /* We're going to overwrite pieces below, make a duplicate. */
15766 baselink
= copy_node (baselink
);
15768 /* If lookup found a single function, mark it as used at this point.
15769 (If lookup found multiple functions the one selected later by
15770 overload resolution will be marked as used at that point.) */
15771 if (!template_id_p
&& !really_overloaded_fn (fns
))
15773 tree fn
= OVL_FIRST (fns
);
15774 bool ok
= mark_used (fn
, complain
);
15775 if (!ok
&& !(complain
& tf_error
))
15776 return error_mark_node
;
15777 if (ok
&& BASELINK_P (baselink
))
15778 /* We might have instantiated an auto function. */
15779 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15782 if (BASELINK_P (baselink
))
15784 /* Add back the template arguments, if present. */
15786 BASELINK_FUNCTIONS (baselink
)
15787 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15789 /* Update the conversion operator type. */
15790 BASELINK_OPTYPE (baselink
) = optype
;
15794 object_type
= current_class_type
;
15796 if (qualified_p
|| !dependent_p
)
15798 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15802 /* We need to call adjust_result_of_qualified_name_lookup in case the
15803 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15804 so that we still get virtual function binding. */
15805 BASELINK_QUALIFIED_P (baselink
) = false;
15811 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15812 true if the qualified-id will be a postfix-expression in-and-of
15813 itself; false if more of the postfix-expression follows the
15814 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15818 tsubst_qualified_id (tree qualified_id
, tree args
,
15819 tsubst_flags_t complain
, tree in_decl
,
15820 bool done
, bool address_p
)
15826 tree template_args
;
15827 location_t loc
= UNKNOWN_LOCATION
;
15829 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15831 /* Figure out what name to look up. */
15832 name
= TREE_OPERAND (qualified_id
, 1);
15833 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15835 is_template
= true;
15836 loc
= EXPR_LOCATION (name
);
15837 template_args
= TREE_OPERAND (name
, 1);
15839 template_args
= tsubst_template_args (template_args
, args
,
15840 complain
, in_decl
);
15841 if (template_args
== error_mark_node
)
15842 return error_mark_node
;
15843 name
= TREE_OPERAND (name
, 0);
15847 is_template
= false;
15848 template_args
= NULL_TREE
;
15851 /* Substitute into the qualifying scope. When there are no ARGS, we
15852 are just trying to simplify a non-dependent expression. In that
15853 case the qualifying scope may be dependent, and, in any case,
15854 substituting will not help. */
15855 scope
= TREE_OPERAND (qualified_id
, 0);
15858 scope
= tsubst (scope
, args
, complain
, in_decl
);
15859 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15864 if (dependent_scope_p (scope
))
15867 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15868 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15869 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15870 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15874 if (!BASELINK_P (name
) && !DECL_P (expr
))
15876 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15878 /* A BIT_NOT_EXPR is used to represent a destructor. */
15879 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15881 error ("qualifying type %qT does not match destructor name ~%qT",
15882 scope
, TREE_OPERAND (expr
, 0));
15883 expr
= error_mark_node
;
15886 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15887 /*is_type_p=*/0, false);
15890 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15891 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15892 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15894 if (complain
& tf_error
)
15896 error ("dependent-name %qE is parsed as a non-type, but "
15897 "instantiation yields a type", qualified_id
);
15898 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15900 return error_mark_node
;
15906 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15908 /* Remember that there was a reference to this entity. */
15909 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15910 return error_mark_node
;
15913 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15915 if (complain
& tf_error
)
15916 qualified_name_lookup_error (scope
,
15917 TREE_OPERAND (qualified_id
, 1),
15918 expr
, input_location
);
15919 return error_mark_node
;
15924 /* We may be repeating a check already done during parsing, but
15925 if it was well-formed and passed then, it will pass again
15926 now, and if it didn't, we wouldn't have got here. The case
15927 we want to catch is when we couldn't tell then, and can now,
15928 namely when templ prior to substitution was an
15930 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15931 return error_mark_node
;
15933 if (variable_template_p (expr
))
15934 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15937 expr
= lookup_template_function (expr
, template_args
);
15940 if (expr
== error_mark_node
&& complain
& tf_error
)
15941 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15942 expr
, input_location
);
15943 else if (TYPE_P (scope
))
15945 expr
= (adjust_result_of_qualified_name_lookup
15946 (expr
, scope
, current_nonlambda_class_type ()));
15947 expr
= (finish_qualified_id_expr
15948 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15949 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15950 /*template_arg_p=*/false, complain
));
15953 /* Expressions do not generally have reference type. */
15954 if (TREE_CODE (expr
) != SCOPE_REF
15955 /* However, if we're about to form a pointer-to-member, we just
15956 want the referenced member referenced. */
15957 && TREE_CODE (expr
) != OFFSET_REF
)
15958 expr
= convert_from_reference (expr
);
15960 if (REF_PARENTHESIZED_P (qualified_id
))
15961 expr
= force_paren_expr (expr
);
15966 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15967 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15971 tsubst_init (tree init
, tree decl
, tree args
,
15972 tsubst_flags_t complain
, tree in_decl
)
15977 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15979 tree type
= TREE_TYPE (decl
);
15981 if (!init
&& type
!= error_mark_node
)
15983 if (tree auto_node
= type_uses_auto (type
))
15985 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
15987 if (complain
& tf_error
)
15988 error ("initializer for %q#D expands to an empty list "
15989 "of expressions", decl
);
15990 return error_mark_node
;
15993 else if (!dependent_type_p (type
))
15995 /* If we had an initializer but it
15996 instantiated to nothing,
15997 value-initialize the object. This will
15998 only occur when the initializer was a
15999 pack expansion where the parameter packs
16000 used in that expansion were of length
16002 init
= build_value_init (type
, complain
);
16003 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16004 init
= get_target_expr_sfinae (init
, complain
);
16005 if (TREE_CODE (init
) == TARGET_EXPR
)
16006 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16013 /* Like tsubst, but deals with expressions. This function just replaces
16014 template parms; to finish processing the resultant expression, use
16015 tsubst_copy_and_build or tsubst_expr. */
16018 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16020 enum tree_code code
;
16023 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16026 code
= TREE_CODE (t
);
16031 r
= retrieve_local_specialization (t
);
16033 if (r
== NULL_TREE
)
16035 /* We get here for a use of 'this' in an NSDMI. */
16036 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16037 return current_class_ptr
;
16039 /* This can happen for a parameter name used later in a function
16040 declaration (such as in a late-specified return type). Just
16041 make a dummy decl, since it's only used for its type. */
16042 gcc_assert (cp_unevaluated_operand
!= 0);
16043 r
= tsubst_decl (t
, args
, complain
);
16044 /* Give it the template pattern as its context; its true context
16045 hasn't been instantiated yet and this is good enough for
16047 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16050 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16051 r
= argument_pack_select_arg (r
);
16052 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16053 return error_mark_node
;
16061 if (DECL_TEMPLATE_PARM_P (t
))
16062 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16063 /* There is no need to substitute into namespace-scope
16065 if (DECL_NAMESPACE_SCOPE_P (t
))
16067 /* If ARGS is NULL, then T is known to be non-dependent. */
16068 if (args
== NULL_TREE
)
16069 return scalar_constant_value (t
);
16071 /* Unfortunately, we cannot just call lookup_name here.
16074 template <int I> int f() {
16076 struct S { void g() { E e = a; } };
16079 When we instantiate f<7>::S::g(), say, lookup_name is not
16080 clever enough to find f<7>::a. */
16082 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16083 /*entering_scope=*/0);
16085 for (v
= TYPE_VALUES (enum_type
);
16087 v
= TREE_CHAIN (v
))
16088 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16089 return TREE_VALUE (v
);
16091 /* We didn't find the name. That should never happen; if
16092 name-lookup found it during preliminary parsing, we
16093 should find it again here during instantiation. */
16094 gcc_unreachable ();
16099 if (DECL_CONTEXT (t
))
16103 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16104 /*entering_scope=*/1);
16105 if (ctx
!= DECL_CONTEXT (t
))
16107 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16110 if (complain
& tf_error
)
16111 error ("using invalid field %qD", t
);
16112 return error_mark_node
;
16121 case FUNCTION_DECL
:
16122 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16123 r
= tsubst (t
, args
, complain
, in_decl
);
16124 else if (local_variable_p (t
)
16125 && uses_template_parms (DECL_CONTEXT (t
)))
16127 r
= retrieve_local_specialization (t
);
16128 if (r
== NULL_TREE
)
16130 /* First try name lookup to find the instantiation. */
16131 r
= lookup_name (DECL_NAME (t
));
16136 /* During error-recovery we may find a non-variable,
16137 even an OVERLOAD: just bail out and avoid ICEs and
16138 duplicate diagnostics (c++/62207). */
16139 gcc_assert (seen_error ());
16140 return error_mark_node
;
16142 if (!is_capture_proxy (r
))
16144 /* Make sure the one we found is the one we want. */
16145 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16146 if (ctx
!= DECL_CONTEXT (r
))
16155 /* This can happen for a variable used in a
16156 late-specified return type of a local lambda, or for a
16157 local static or constant. Building a new VAR_DECL
16158 should be OK in all those cases. */
16159 r
= tsubst_decl (t
, args
, complain
);
16160 if (local_specializations
)
16161 /* Avoid infinite recursion (79640). */
16162 register_local_specialization (r
, t
);
16163 if (decl_maybe_constant_var_p (r
))
16165 /* We can't call cp_finish_decl, so handle the
16166 initializer by hand. */
16167 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16168 complain
, in_decl
);
16169 if (!processing_template_decl
)
16170 init
= maybe_constant_init (init
);
16171 if (processing_template_decl
16172 ? potential_constant_expression (init
)
16173 : reduced_constant_expression_p (init
))
16174 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16175 = TREE_CONSTANT (r
) = true;
16176 DECL_INITIAL (r
) = init
;
16177 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16179 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16180 complain
, adc_variable_type
);
16182 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16183 || decl_constant_var_p (r
)
16185 if (!processing_template_decl
16186 && !TREE_STATIC (r
))
16187 r
= process_outer_var_ref (r
, complain
);
16189 /* Remember this for subsequent uses. */
16190 if (local_specializations
)
16191 register_local_specialization (r
, t
);
16193 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16194 r
= argument_pack_select_arg (r
);
16198 if (!mark_used (r
, complain
))
16199 return error_mark_node
;
16202 case NAMESPACE_DECL
:
16209 return tsubst_baselink (t
, current_nonlambda_class_type (),
16210 args
, complain
, in_decl
);
16212 case TEMPLATE_DECL
:
16213 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16214 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16215 args
, complain
, in_decl
);
16216 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16217 return tsubst (t
, args
, complain
, in_decl
);
16218 else if (DECL_CLASS_SCOPE_P (t
)
16219 && uses_template_parms (DECL_CONTEXT (t
)))
16221 /* Template template argument like the following example need
16224 template <template <class> class TT> struct C {};
16225 template <class T> struct D {
16226 template <class U> struct E {};
16231 We are processing the template argument `E' in #1 for
16232 the template instantiation #2. Originally, `E' is a
16233 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16234 have to substitute this with one having context `D<int>'. */
16236 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16237 if (dependent_scope_p (context
))
16239 /* When rewriting a constructor into a deduction guide, a
16240 non-dependent name can become dependent, so memtmpl<args>
16241 becomes context::template memtmpl<args>. */
16242 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16243 return build_qualified_name (type
, context
, DECL_NAME (t
),
16246 return lookup_field (context
, DECL_NAME(t
), 0, false);
16249 /* Ordinary template template argument. */
16252 case NON_LVALUE_EXPR
:
16253 case VIEW_CONVERT_EXPR
:
16255 /* Handle location wrappers by substituting the wrapped node
16256 first, *then* reusing the resulting type. Doing the type
16257 first ensures that we handle template parameters and
16258 parameter pack expansions. */
16259 if (location_wrapper_p (t
))
16261 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16262 complain
, in_decl
);
16263 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16265 tree op
= TREE_OPERAND (t
, 0);
16266 if (code
== VIEW_CONVERT_EXPR
16267 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16269 /* Wrapper to make a C++20 template parameter object const. */
16270 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16271 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16273 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16274 return build1 (code
, type
, op
);
16278 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
16282 /* We shouldn't see any other uses of these in templates. */
16283 gcc_unreachable ();
16287 case REINTERPRET_CAST_EXPR
:
16288 case CONST_CAST_EXPR
:
16289 case STATIC_CAST_EXPR
:
16290 case DYNAMIC_CAST_EXPR
:
16291 case IMPLICIT_CONV_EXPR
:
16295 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16296 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16297 return build1 (code
, type
, op0
);
16301 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16302 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16304 tree expanded
, op
= TREE_OPERAND (t
, 0);
16307 if (SIZEOF_EXPR_TYPE_P (t
))
16308 op
= TREE_TYPE (op
);
16310 ++cp_unevaluated_operand
;
16311 ++c_inhibit_evaluation_warnings
;
16312 /* We only want to compute the number of arguments. */
16313 if (PACK_EXPANSION_P (op
))
16314 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16316 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16317 args
, complain
, in_decl
);
16318 --cp_unevaluated_operand
;
16319 --c_inhibit_evaluation_warnings
;
16321 if (TREE_CODE (expanded
) == TREE_VEC
)
16323 len
= TREE_VEC_LENGTH (expanded
);
16324 /* Set TREE_USED for the benefit of -Wunused. */
16325 for (int i
= 0; i
< len
; i
++)
16326 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16327 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16330 if (expanded
== error_mark_node
)
16331 return error_mark_node
;
16332 else if (PACK_EXPANSION_P (expanded
)
16333 || (TREE_CODE (expanded
) == TREE_VEC
16334 && pack_expansion_args_count (expanded
)))
16337 if (PACK_EXPANSION_P (expanded
))
16339 else if (TREE_VEC_LENGTH (expanded
) == 1)
16340 expanded
= TREE_VEC_ELT (expanded
, 0);
16342 expanded
= make_argument_pack (expanded
);
16344 if (TYPE_P (expanded
))
16345 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
16347 complain
& tf_error
);
16349 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
16350 complain
& tf_error
);
16353 return build_int_cst (size_type_node
, len
);
16355 if (SIZEOF_EXPR_TYPE_P (t
))
16357 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16358 args
, complain
, in_decl
);
16359 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16360 r
= build1 (SIZEOF_EXPR
,
16361 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16362 SIZEOF_EXPR_TYPE_P (r
) = 1;
16369 case TRUTH_NOT_EXPR
:
16372 case UNARY_PLUS_EXPR
: /* Unary + */
16374 case AT_ENCODE_EXPR
:
16378 case REALPART_EXPR
:
16379 case IMAGPART_EXPR
:
16382 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16383 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16384 r
= build1 (code
, type
, op0
);
16385 if (code
== ALIGNOF_EXPR
)
16386 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16390 case COMPONENT_REF
:
16395 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16396 name
= TREE_OPERAND (t
, 1);
16397 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16399 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16400 complain
, in_decl
);
16401 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16403 else if (TREE_CODE (name
) == SCOPE_REF
16404 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16406 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16407 complain
, in_decl
);
16408 name
= TREE_OPERAND (name
, 1);
16409 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16410 complain
, in_decl
);
16411 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16412 name
= build_qualified_name (/*type=*/NULL_TREE
,
16414 /*template_p=*/false);
16416 else if (BASELINK_P (name
))
16417 name
= tsubst_baselink (name
,
16418 non_reference (TREE_TYPE (object
)),
16422 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16423 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16429 case TRUNC_DIV_EXPR
:
16430 case CEIL_DIV_EXPR
:
16431 case FLOOR_DIV_EXPR
:
16432 case ROUND_DIV_EXPR
:
16433 case EXACT_DIV_EXPR
:
16437 case TRUNC_MOD_EXPR
:
16438 case FLOOR_MOD_EXPR
:
16439 case TRUTH_ANDIF_EXPR
:
16440 case TRUTH_ORIF_EXPR
:
16441 case TRUTH_AND_EXPR
:
16442 case TRUTH_OR_EXPR
:
16453 case COMPOUND_EXPR
:
16456 case PREDECREMENT_EXPR
:
16457 case PREINCREMENT_EXPR
:
16458 case POSTDECREMENT_EXPR
:
16459 case POSTINCREMENT_EXPR
:
16461 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16462 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16463 return build_nt (code
, op0
, op1
);
16468 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16469 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16470 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16471 QUALIFIED_NAME_IS_TEMPLATE (t
));
16476 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16477 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16478 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16483 int n
= VL_EXP_OPERAND_LENGTH (t
);
16484 tree result
= build_vl_exp (CALL_EXPR
, n
);
16486 for (i
= 0; i
< n
; i
++)
16487 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16488 complain
, in_decl
);
16494 case PSEUDO_DTOR_EXPR
:
16495 case VEC_PERM_EXPR
:
16497 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16498 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16499 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16500 r
= build_nt (code
, op0
, op1
, op2
);
16501 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16507 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16508 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16509 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16510 r
= build_nt (code
, op0
, op1
, op2
);
16511 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16517 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16518 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16519 r
= build_nt (code
, op0
, op1
);
16520 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16521 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16525 case TEMPLATE_ID_EXPR
:
16527 /* Substituted template arguments */
16528 tree fn
= TREE_OPERAND (t
, 0);
16529 tree targs
= TREE_OPERAND (t
, 1);
16531 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16533 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16535 return lookup_template_function (fn
, targs
);
16540 tree purpose
, value
, chain
;
16542 if (t
== void_list_node
)
16545 purpose
= TREE_PURPOSE (t
);
16547 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16548 value
= TREE_VALUE (t
);
16550 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16551 chain
= TREE_CHAIN (t
);
16552 if (chain
&& chain
!= void_type_node
)
16553 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16554 if (purpose
== TREE_PURPOSE (t
)
16555 && value
== TREE_VALUE (t
)
16556 && chain
== TREE_CHAIN (t
))
16558 return tree_cons (purpose
, value
, chain
);
16563 case ENUMERAL_TYPE
:
16565 case TEMPLATE_TYPE_PARM
:
16566 case TEMPLATE_TEMPLATE_PARM
:
16567 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16568 case TEMPLATE_PARM_INDEX
:
16570 case REFERENCE_TYPE
:
16572 case FUNCTION_TYPE
:
16575 case TYPENAME_TYPE
:
16576 case UNBOUND_CLASS_TEMPLATE
:
16578 case DECLTYPE_TYPE
:
16580 return tsubst (t
, args
, complain
, in_decl
);
16584 /* Fall through. */
16585 case IDENTIFIER_NODE
:
16586 if (IDENTIFIER_CONV_OP_P (t
))
16588 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16589 return make_conv_op_name (new_type
);
16595 /* This is handled by tsubst_copy_and_build. */
16596 gcc_unreachable ();
16600 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16601 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16602 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16605 case CLEANUP_POINT_EXPR
:
16606 /* We shouldn't have built any of these during initial template
16607 generation. Instead, they should be built during instantiation
16608 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16609 gcc_unreachable ();
16613 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16614 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16615 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16616 r
= build2 (code
, type
, op0
, op1
);
16617 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16618 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16619 && !(complain
& tf_error
))
16620 return error_mark_node
;
16624 case EXPR_PACK_EXPANSION
:
16625 error ("invalid use of pack expansion expression");
16626 return error_mark_node
;
16628 case NONTYPE_ARGUMENT_PACK
:
16629 error ("use %<...%> to expand argument pack");
16630 return error_mark_node
;
16633 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16641 /* Instantiate any typedefs in the type. */
16642 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16643 r
= fold_convert (type
, t
);
16644 gcc_assert (TREE_CODE (r
) == code
);
16649 /* These can sometimes show up in a partial instantiation, but never
16650 involve template parms. */
16651 gcc_assert (!uses_template_parms (t
));
16654 case UNARY_LEFT_FOLD_EXPR
:
16655 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16656 case UNARY_RIGHT_FOLD_EXPR
:
16657 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16658 case BINARY_LEFT_FOLD_EXPR
:
16659 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16660 case BINARY_RIGHT_FOLD_EXPR
:
16661 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16665 case DEBUG_BEGIN_STMT
:
16666 /* ??? There's no point in copying it for now, but maybe some
16667 day it will contain more information, such as a pointer back
16668 to the containing function, inlined copy or so. */
16672 /* We shouldn't get here, but keep going if !flag_checking. */
16674 gcc_unreachable ();
16679 /* Helper function for tsubst_omp_clauses, used for instantiation of
16680 OMP_CLAUSE_DECL of clauses. */
16683 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16684 tree in_decl
, tree
*iterator_cache
)
16686 if (decl
== NULL_TREE
)
16689 /* Handle OpenMP iterators. */
16690 if (TREE_CODE (decl
) == TREE_LIST
16691 && TREE_PURPOSE (decl
)
16692 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16695 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16696 ret
= iterator_cache
[1];
16700 begin_scope (sk_omp
, NULL
);
16701 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16703 *tp
= copy_node (it
);
16704 TREE_VEC_ELT (*tp
, 0)
16705 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16706 TREE_VEC_ELT (*tp
, 1)
16707 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16708 /*integral_constant_expression_p=*/false);
16709 TREE_VEC_ELT (*tp
, 2)
16710 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16711 /*integral_constant_expression_p=*/false);
16712 TREE_VEC_ELT (*tp
, 3)
16713 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16714 /*integral_constant_expression_p=*/false);
16715 TREE_CHAIN (*tp
) = NULL_TREE
;
16716 tp
= &TREE_CHAIN (*tp
);
16718 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16719 iterator_cache
[0] = TREE_PURPOSE (decl
);
16720 iterator_cache
[1] = ret
;
16722 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16727 /* Handle an OpenMP array section represented as a TREE_LIST (or
16728 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16729 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16730 TREE_LIST. We can handle it exactly the same as an array section
16731 (purpose, value, and a chain), even though the nomenclature
16732 (low_bound, length, etc) is different. */
16733 if (TREE_CODE (decl
) == TREE_LIST
)
16736 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16737 /*integral_constant_expression_p=*/false);
16738 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16739 /*integral_constant_expression_p=*/false);
16740 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16742 if (TREE_PURPOSE (decl
) == low_bound
16743 && TREE_VALUE (decl
) == length
16744 && TREE_CHAIN (decl
) == chain
)
16746 tree ret
= tree_cons (low_bound
, length
, chain
);
16747 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16748 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16751 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16752 /*integral_constant_expression_p=*/false);
16753 /* Undo convert_from_reference tsubst_expr could have called. */
16755 && REFERENCE_REF_P (ret
)
16756 && !REFERENCE_REF_P (decl
))
16757 ret
= TREE_OPERAND (ret
, 0);
16761 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16764 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16765 tree args
, tsubst_flags_t complain
, tree in_decl
)
16767 tree new_clauses
= NULL_TREE
, nc
, oc
;
16768 tree linear_no_step
= NULL_TREE
;
16769 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16771 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16773 nc
= copy_node (oc
);
16774 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16777 switch (OMP_CLAUSE_CODE (nc
))
16779 case OMP_CLAUSE_LASTPRIVATE
:
16780 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16782 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16783 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16784 in_decl
, /*integral_constant_expression_p=*/false);
16785 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16786 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16789 case OMP_CLAUSE_PRIVATE
:
16790 case OMP_CLAUSE_SHARED
:
16791 case OMP_CLAUSE_FIRSTPRIVATE
:
16792 case OMP_CLAUSE_COPYIN
:
16793 case OMP_CLAUSE_COPYPRIVATE
:
16794 case OMP_CLAUSE_UNIFORM
:
16795 case OMP_CLAUSE_DEPEND
:
16796 case OMP_CLAUSE_FROM
:
16797 case OMP_CLAUSE_TO
:
16798 case OMP_CLAUSE_MAP
:
16799 case OMP_CLAUSE_NONTEMPORAL
:
16800 case OMP_CLAUSE_USE_DEVICE_PTR
:
16801 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16802 case OMP_CLAUSE_IS_DEVICE_PTR
:
16803 case OMP_CLAUSE_INCLUSIVE
:
16804 case OMP_CLAUSE_EXCLUSIVE
:
16805 OMP_CLAUSE_DECL (nc
)
16806 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16807 in_decl
, iterator_cache
);
16809 case OMP_CLAUSE_TILE
:
16810 case OMP_CLAUSE_IF
:
16811 case OMP_CLAUSE_NUM_THREADS
:
16812 case OMP_CLAUSE_SCHEDULE
:
16813 case OMP_CLAUSE_COLLAPSE
:
16814 case OMP_CLAUSE_FINAL
:
16815 case OMP_CLAUSE_DEVICE
:
16816 case OMP_CLAUSE_DIST_SCHEDULE
:
16817 case OMP_CLAUSE_NUM_TEAMS
:
16818 case OMP_CLAUSE_THREAD_LIMIT
:
16819 case OMP_CLAUSE_SAFELEN
:
16820 case OMP_CLAUSE_SIMDLEN
:
16821 case OMP_CLAUSE_NUM_TASKS
:
16822 case OMP_CLAUSE_GRAINSIZE
:
16823 case OMP_CLAUSE_PRIORITY
:
16824 case OMP_CLAUSE_ORDERED
:
16825 case OMP_CLAUSE_HINT
:
16826 case OMP_CLAUSE_NUM_GANGS
:
16827 case OMP_CLAUSE_NUM_WORKERS
:
16828 case OMP_CLAUSE_VECTOR_LENGTH
:
16829 case OMP_CLAUSE_WORKER
:
16830 case OMP_CLAUSE_VECTOR
:
16831 case OMP_CLAUSE_ASYNC
:
16832 case OMP_CLAUSE_WAIT
:
16833 OMP_CLAUSE_OPERAND (nc
, 0)
16834 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16835 in_decl
, /*integral_constant_expression_p=*/false);
16837 case OMP_CLAUSE_REDUCTION
:
16838 case OMP_CLAUSE_IN_REDUCTION
:
16839 case OMP_CLAUSE_TASK_REDUCTION
:
16840 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16842 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16843 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16845 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16846 complain
, in_decl
);
16847 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16848 = build_qualified_name (NULL_TREE
, scope
,
16849 TREE_OPERAND (placeholder
, 1),
16853 gcc_assert (identifier_p (placeholder
));
16855 OMP_CLAUSE_DECL (nc
)
16856 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16859 case OMP_CLAUSE_GANG
:
16860 case OMP_CLAUSE_ALIGNED
:
16861 OMP_CLAUSE_DECL (nc
)
16862 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16864 OMP_CLAUSE_OPERAND (nc
, 1)
16865 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16866 in_decl
, /*integral_constant_expression_p=*/false);
16868 case OMP_CLAUSE_LINEAR
:
16869 OMP_CLAUSE_DECL (nc
)
16870 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16872 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16874 gcc_assert (!linear_no_step
);
16875 linear_no_step
= nc
;
16877 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16878 OMP_CLAUSE_LINEAR_STEP (nc
)
16879 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16880 complain
, in_decl
, NULL
);
16882 OMP_CLAUSE_LINEAR_STEP (nc
)
16883 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16885 /*integral_constant_expression_p=*/false);
16887 case OMP_CLAUSE_NOWAIT
:
16888 case OMP_CLAUSE_DEFAULT
:
16889 case OMP_CLAUSE_UNTIED
:
16890 case OMP_CLAUSE_MERGEABLE
:
16891 case OMP_CLAUSE_INBRANCH
:
16892 case OMP_CLAUSE_NOTINBRANCH
:
16893 case OMP_CLAUSE_PROC_BIND
:
16894 case OMP_CLAUSE_FOR
:
16895 case OMP_CLAUSE_PARALLEL
:
16896 case OMP_CLAUSE_SECTIONS
:
16897 case OMP_CLAUSE_TASKGROUP
:
16898 case OMP_CLAUSE_NOGROUP
:
16899 case OMP_CLAUSE_THREADS
:
16900 case OMP_CLAUSE_SIMD
:
16901 case OMP_CLAUSE_DEFAULTMAP
:
16902 case OMP_CLAUSE_ORDER
:
16903 case OMP_CLAUSE_BIND
:
16904 case OMP_CLAUSE_INDEPENDENT
:
16905 case OMP_CLAUSE_AUTO
:
16906 case OMP_CLAUSE_SEQ
:
16907 case OMP_CLAUSE_IF_PRESENT
:
16908 case OMP_CLAUSE_FINALIZE
:
16911 gcc_unreachable ();
16913 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16914 switch (OMP_CLAUSE_CODE (nc
))
16916 case OMP_CLAUSE_SHARED
:
16917 case OMP_CLAUSE_PRIVATE
:
16918 case OMP_CLAUSE_FIRSTPRIVATE
:
16919 case OMP_CLAUSE_LASTPRIVATE
:
16920 case OMP_CLAUSE_COPYPRIVATE
:
16921 case OMP_CLAUSE_LINEAR
:
16922 case OMP_CLAUSE_REDUCTION
:
16923 case OMP_CLAUSE_IN_REDUCTION
:
16924 case OMP_CLAUSE_TASK_REDUCTION
:
16925 case OMP_CLAUSE_USE_DEVICE_PTR
:
16926 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16927 case OMP_CLAUSE_IS_DEVICE_PTR
:
16928 case OMP_CLAUSE_INCLUSIVE
:
16929 case OMP_CLAUSE_EXCLUSIVE
:
16930 /* tsubst_expr on SCOPE_REF results in returning
16931 finish_non_static_data_member result. Undo that here. */
16932 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16933 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16934 == IDENTIFIER_NODE
))
16936 tree t
= OMP_CLAUSE_DECL (nc
);
16939 switch (TREE_CODE (v
))
16941 case COMPONENT_REF
:
16945 case POINTER_PLUS_EXPR
:
16946 v
= TREE_OPERAND (v
, 0);
16949 if (DECL_CONTEXT (v
) == current_function_decl
16950 && DECL_ARTIFICIAL (v
)
16951 && DECL_NAME (v
) == this_identifier
)
16952 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16959 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16960 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16961 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16962 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16963 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16965 tree decl
= OMP_CLAUSE_DECL (nc
);
16968 retrofit_lang_decl (decl
);
16969 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16978 new_clauses
= nreverse (new_clauses
);
16979 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16981 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16982 if (linear_no_step
)
16983 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16984 if (nc
== linear_no_step
)
16986 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16990 return new_clauses
;
16993 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16996 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16999 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17001 tree purpose
, value
, chain
;
17006 if (TREE_CODE (t
) != TREE_LIST
)
17007 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17008 /*function_p=*/false,
17009 /*integral_constant_expression_p=*/false);
17011 if (t
== void_list_node
)
17014 purpose
= TREE_PURPOSE (t
);
17016 purpose
= RECUR (purpose
);
17017 value
= TREE_VALUE (t
);
17020 if (TREE_CODE (value
) != LABEL_DECL
)
17021 value
= RECUR (value
);
17024 value
= lookup_label (DECL_NAME (value
));
17025 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17026 TREE_USED (value
) = 1;
17029 chain
= TREE_CHAIN (t
);
17030 if (chain
&& chain
!= void_type_node
)
17031 chain
= RECUR (chain
);
17032 return tree_cons (purpose
, value
, chain
);
17036 /* Used to temporarily communicate the list of #pragma omp parallel
17037 clauses to #pragma omp for instantiation if they are combined
17040 static tree
*omp_parallel_combined_clauses
;
17042 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17043 tree
*, unsigned int *);
17045 /* Substitute one OMP_FOR iterator. */
17048 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17049 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17050 tree args
, tsubst_flags_t complain
, tree in_decl
,
17051 bool integral_constant_expression_p
)
17053 #define RECUR(NODE) \
17054 tsubst_expr ((NODE), args, complain, in_decl, \
17055 integral_constant_expression_p)
17056 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17059 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17060 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17062 decl
= TREE_OPERAND (init
, 0);
17063 init
= TREE_OPERAND (init
, 1);
17064 tree decl_expr
= NULL_TREE
;
17065 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17068 bool decomp
= false;
17069 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17071 tree v
= DECL_VALUE_EXPR (decl
);
17072 if (TREE_CODE (v
) == ARRAY_REF
17073 && VAR_P (TREE_OPERAND (v
, 0))
17074 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17076 tree decomp_first
= NULL_TREE
;
17077 unsigned decomp_cnt
= 0;
17078 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17079 maybe_push_decl (d
);
17080 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17081 in_decl
, &decomp_first
, &decomp_cnt
);
17083 if (d
== error_mark_node
)
17084 decl
= error_mark_node
;
17086 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17088 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17090 tree v
= build_nt (ARRAY_REF
, d
,
17091 size_int (decomp_cnt
- i
- 1),
17092 NULL_TREE
, NULL_TREE
);
17093 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17094 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17096 fit_decomposition_lang_decl (decomp_first
, d
);
17097 decomp_first
= DECL_CHAIN (decomp_first
);
17101 decl
= tsubst_decl (decl
, args
, complain
);
17103 maybe_push_decl (decl
);
17105 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17107 /* We need to jump through some hoops to handle declarations in the
17108 init-statement, since we might need to handle auto deduction,
17109 but we need to keep control of initialization. */
17111 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17112 decl
= tsubst_decl (decl
, args
, complain
);
17116 if (TREE_CODE (decl
) == SCOPE_REF
)
17118 decl
= RECUR (decl
);
17119 if (TREE_CODE (decl
) == COMPONENT_REF
)
17123 switch (TREE_CODE (v
))
17125 case COMPONENT_REF
:
17129 case POINTER_PLUS_EXPR
:
17130 v
= TREE_OPERAND (v
, 0);
17133 if (DECL_CONTEXT (v
) == current_function_decl
17134 && DECL_ARTIFICIAL (v
)
17135 && DECL_NAME (v
) == this_identifier
)
17137 decl
= TREE_OPERAND (decl
, 1);
17138 decl
= omp_privatize_field (decl
, false);
17148 decl
= RECUR (decl
);
17150 init
= RECUR (init
);
17152 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17154 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17155 if (TREE_CODE (o
) == TREE_LIST
)
17156 TREE_VEC_ELT (orig_declv
, i
)
17157 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17158 RECUR (TREE_VALUE (o
)),
17161 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17166 tree this_pre_body
= NULL_TREE
;
17167 tree orig_init
= NULL_TREE
;
17168 tree orig_decl
= NULL_TREE
;
17169 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17170 orig_init
, cond
, incr
);
17173 if (orig_declv
== NULL_TREE
)
17174 orig_declv
= copy_node (declv
);
17175 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17178 else if (orig_declv
)
17179 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17182 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17183 if (!range_for
&& auto_node
&& init
)
17185 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17187 gcc_assert (!type_dependent_expression_p (decl
));
17189 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17193 /* Declare the variable, but don't let that initialize it. */
17194 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17195 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17197 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17202 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
17203 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17204 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17206 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17207 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17208 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17209 NOP_EXPR
, rhs
, complain
);
17212 incr
= RECUR (incr
);
17213 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17214 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17216 TREE_VEC_ELT (declv
, i
) = decl
;
17217 TREE_VEC_ELT (initv
, i
) = init
;
17218 TREE_VEC_ELT (condv
, i
) = cond
;
17219 TREE_VEC_ELT (incrv
, i
) = incr
;
17225 /* Declare and initialize the variable. */
17233 for (j
= ((omp_parallel_combined_clauses
== NULL
17234 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17236 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17238 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17239 && OMP_CLAUSE_DECL (*pc
) == decl
)
17241 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17242 && OMP_CLAUSE_DECL (*pc
) == decl
)
17246 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17248 *pc
= OMP_CLAUSE_CHAIN (c
);
17249 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17252 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17253 && OMP_CLAUSE_DECL (*pc
) == decl
)
17255 error ("iteration variable %qD should not be firstprivate",
17257 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17259 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17260 && OMP_CLAUSE_DECL (*pc
) == decl
)
17262 error ("iteration variable %qD should not be reduction",
17264 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17267 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17272 if (*pc
== NULL_TREE
)
17274 tree c
= build_omp_clause (input_location
,
17275 TREE_CODE (t
) == OMP_LOOP
17276 ? OMP_CLAUSE_LASTPRIVATE
17277 : OMP_CLAUSE_PRIVATE
);
17278 OMP_CLAUSE_DECL (c
) = decl
;
17279 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17282 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17287 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17288 if (COMPARISON_CLASS_P (cond
))
17290 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17291 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17292 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17295 cond
= RECUR (cond
);
17296 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17297 switch (TREE_CODE (incr
))
17299 case PREINCREMENT_EXPR
:
17300 case PREDECREMENT_EXPR
:
17301 case POSTINCREMENT_EXPR
:
17302 case POSTDECREMENT_EXPR
:
17303 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17304 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17307 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17308 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17310 tree rhs
= TREE_OPERAND (incr
, 1);
17311 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17312 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17313 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17314 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17315 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17319 incr
= RECUR (incr
);
17322 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17323 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17325 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17326 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17327 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17328 TREE_TYPE (decl
), lhs
,
17329 RECUR (TREE_OPERAND (incr
, 2))));
17331 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17332 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17333 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17335 tree rhs
= TREE_OPERAND (incr
, 2);
17336 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17337 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17338 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17339 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17340 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17344 incr
= RECUR (incr
);
17347 incr
= RECUR (incr
);
17351 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17352 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17353 TREE_VEC_ELT (declv
, i
) = decl
;
17354 TREE_VEC_ELT (initv
, i
) = init
;
17355 TREE_VEC_ELT (condv
, i
) = cond
;
17356 TREE_VEC_ELT (incrv
, i
) = incr
;
17361 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17362 of OMP_TARGET's body. */
17365 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17367 *walk_subtrees
= 0;
17368 switch (TREE_CODE (*tp
))
17373 case STATEMENT_LIST
:
17374 *walk_subtrees
= 1;
17382 /* Helper function for tsubst_expr. For decomposition declaration
17383 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17384 also the corresponding decls representing the identifiers
17385 of the decomposition declaration. Return DECL if successful
17386 or error_mark_node otherwise, set *FIRST to the first decl
17387 in the list chained through DECL_CHAIN and *CNT to the number
17391 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17392 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17395 tree decl2
, decl3
, prev
= decl
;
17397 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17398 for (decl2
= DECL_CHAIN (pattern_decl
);
17401 && DECL_DECOMPOSITION_P (decl2
)
17402 && DECL_NAME (decl2
);
17403 decl2
= DECL_CHAIN (decl2
))
17405 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17407 gcc_assert (errorcount
);
17408 return error_mark_node
;
17411 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17412 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17413 tree v
= DECL_VALUE_EXPR (decl2
);
17414 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17415 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17416 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17417 SET_DECL_VALUE_EXPR (decl2
, v
);
17418 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17420 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17423 gcc_assert (errorcount
);
17424 decl
= error_mark_node
;
17427 maybe_push_decl (decl3
);
17428 if (error_operand_p (decl3
))
17429 decl
= error_mark_node
;
17430 else if (decl
!= error_mark_node
17431 && DECL_CHAIN (decl3
) != prev
17434 gcc_assert (errorcount
);
17435 decl
= error_mark_node
;
17444 /* Return the proper local_specialization for init-capture pack DECL. */
17447 lookup_init_capture_pack (tree decl
)
17449 /* We handle normal pack captures by forwarding to the specialization of the
17450 captured parameter. We can't do that for pack init-captures; we need them
17451 to have their own local_specialization. We created the individual
17452 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17453 when we process the DECL_EXPR for the pack init-capture in the template.
17454 So, how do we find them? We don't know the capture proxy pack when
17455 building the individual resulting proxies, and we don't know the
17456 individual proxies when instantiating the pack. What we have in common is
17459 So...when we instantiate the FIELD_DECL, we stick the result in
17460 local_specializations. Then at the DECL_EXPR we look up that result, see
17461 how many elements it has, synthesize the names, and look them up. */
17463 tree cname
= DECL_NAME (decl
);
17464 tree val
= DECL_VALUE_EXPR (decl
);
17465 tree field
= TREE_OPERAND (val
, 1);
17466 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17467 tree fpack
= retrieve_local_specialization (field
);
17468 if (fpack
== error_mark_node
)
17469 return error_mark_node
;
17472 tree vec
= NULL_TREE
;
17473 tree r
= NULL_TREE
;
17474 if (TREE_CODE (fpack
) == TREE_VEC
)
17476 len
= TREE_VEC_LENGTH (fpack
);
17477 vec
= make_tree_vec (len
);
17478 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17479 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17481 for (int i
= 0; i
< len
; ++i
)
17483 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17484 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17486 TREE_VEC_ELT (vec
, i
) = elt
;
17493 /* Like tsubst_copy for expressions, etc. but also does semantic
17497 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17498 bool integral_constant_expression_p
)
17500 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17501 #define RECUR(NODE) \
17502 tsubst_expr ((NODE), args, complain, in_decl, \
17503 integral_constant_expression_p)
17509 if (t
== NULL_TREE
|| t
== error_mark_node
)
17512 loc
= input_location
;
17513 if (location_t eloc
= cp_expr_location (t
))
17514 input_location
= eloc
;
17515 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17516 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17518 switch (TREE_CODE (t
))
17520 case STATEMENT_LIST
:
17522 tree_stmt_iterator i
;
17523 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17524 RECUR (tsi_stmt (i
));
17528 case CTOR_INITIALIZER
:
17529 finish_mem_initializers (tsubst_initializer_list
17530 (TREE_OPERAND (t
, 0), args
));
17534 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17538 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17539 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17540 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17542 finish_expr_stmt (tmp
);
17546 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17551 tree decl
, pattern_decl
;
17554 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17555 if (TREE_CODE (decl
) == LABEL_DECL
)
17556 finish_label_decl (DECL_NAME (decl
));
17557 else if (TREE_CODE (decl
) == USING_DECL
)
17559 tree scope
= USING_DECL_SCOPE (decl
);
17560 tree name
= DECL_NAME (decl
);
17562 scope
= tsubst (scope
, args
, complain
, in_decl
);
17563 finish_nonmember_using_decl (scope
, name
);
17565 else if (is_capture_proxy (decl
)
17566 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17568 /* We're in tsubst_lambda_expr, we've already inserted a new
17569 capture proxy, so look it up and register it. */
17571 if (!DECL_PACK_P (decl
))
17573 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17574 /*nonclass*/1, /*block_p=*/true,
17575 /*ns_only*/0, LOOKUP_HIDDEN
);
17576 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17578 else if (is_normal_capture_proxy (decl
))
17580 inst
= (retrieve_local_specialization
17581 (DECL_CAPTURED_VARIABLE (decl
)));
17582 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
17585 inst
= lookup_init_capture_pack (decl
);
17587 register_local_specialization (inst
, decl
);
17590 else if (DECL_PRETTY_FUNCTION_P (decl
))
17591 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17593 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17594 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17595 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17596 /* Don't copy the old closure; we'll create a new one in
17597 tsubst_lambda_expr. */
17601 init
= DECL_INITIAL (decl
);
17602 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17603 for local variables, so save if DECL was declared constinit. */
17604 const bool constinit_p
17606 && DECL_LANG_SPECIFIC (decl
)
17607 && DECL_TEMPLATE_INFO (decl
)
17608 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17609 decl
= tsubst (decl
, args
, complain
, in_decl
);
17610 if (decl
!= error_mark_node
)
17612 /* By marking the declaration as instantiated, we avoid
17613 trying to instantiate it. Since instantiate_decl can't
17614 handle local variables, and since we've already done
17615 all that needs to be done, that's the right thing to
17618 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17619 if (VAR_P (decl
) && !DECL_NAME (decl
)
17620 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17621 /* Anonymous aggregates are a special case. */
17622 finish_anon_union (decl
);
17623 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17625 DECL_CONTEXT (decl
) = current_function_decl
;
17626 if (DECL_NAME (decl
) == this_identifier
)
17628 tree lam
= DECL_CONTEXT (current_function_decl
);
17629 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17630 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17632 insert_capture_proxy (decl
);
17634 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17635 /* We already did a pushtag. */;
17636 else if (TREE_CODE (decl
) == FUNCTION_DECL
17637 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17638 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17640 DECL_CONTEXT (decl
) = NULL_TREE
;
17642 DECL_CONTEXT (decl
) = current_function_decl
;
17643 cp_check_omp_declare_reduction (decl
);
17647 bool const_init
= false;
17648 unsigned int cnt
= 0;
17649 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17650 maybe_push_decl (decl
);
17653 && DECL_DECOMPOSITION_P (decl
)
17654 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17655 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17656 complain
, in_decl
, &first
,
17659 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17662 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17665 if (ndecl
!= error_mark_node
)
17666 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17668 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17669 constinit_p
? LOOKUP_CONSTINIT
: 0);
17671 if (ndecl
!= error_mark_node
)
17672 cp_finish_decomp (ndecl
, first
, cnt
);
17681 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17682 RECUR (FOR_INIT_STMT (t
));
17683 finish_init_stmt (stmt
);
17684 tmp
= RECUR (FOR_COND (t
));
17685 finish_for_cond (tmp
, stmt
, false, 0);
17686 tmp
= RECUR (FOR_EXPR (t
));
17687 finish_for_expr (tmp
, stmt
);
17689 bool prev
= note_iteration_stmt_body_start ();
17690 RECUR (FOR_BODY (t
));
17691 note_iteration_stmt_body_end (prev
);
17693 finish_for_stmt (stmt
);
17696 case RANGE_FOR_STMT
:
17698 /* Construct another range_for, if this is not a final
17699 substitution (for inside inside a generic lambda of a
17700 template). Otherwise convert to a regular for. */
17702 stmt
= (processing_template_decl
17703 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17704 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17705 RECUR (RANGE_FOR_INIT_STMT (t
));
17706 decl
= RANGE_FOR_DECL (t
);
17707 decl
= tsubst (decl
, args
, complain
, in_decl
);
17708 maybe_push_decl (decl
);
17709 expr
= RECUR (RANGE_FOR_EXPR (t
));
17711 tree decomp_first
= NULL_TREE
;
17712 unsigned decomp_cnt
= 0;
17713 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17714 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17716 &decomp_first
, &decomp_cnt
);
17718 if (processing_template_decl
)
17720 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17721 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17722 finish_range_for_decl (stmt
, decl
, expr
);
17723 if (decomp_first
&& decl
!= error_mark_node
)
17724 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17728 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17729 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17730 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17731 decomp_first
, decomp_cnt
,
17732 RANGE_FOR_IVDEP (t
), unroll
);
17735 bool prev
= note_iteration_stmt_body_start ();
17736 RECUR (RANGE_FOR_BODY (t
));
17737 note_iteration_stmt_body_end (prev
);
17738 finish_for_stmt (stmt
);
17743 stmt
= begin_while_stmt ();
17744 tmp
= RECUR (WHILE_COND (t
));
17745 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17747 bool prev
= note_iteration_stmt_body_start ();
17748 RECUR (WHILE_BODY (t
));
17749 note_iteration_stmt_body_end (prev
);
17751 finish_while_stmt (stmt
);
17755 stmt
= begin_do_stmt ();
17757 bool prev
= note_iteration_stmt_body_start ();
17758 RECUR (DO_BODY (t
));
17759 note_iteration_stmt_body_end (prev
);
17761 finish_do_body (stmt
);
17762 tmp
= RECUR (DO_COND (t
));
17763 finish_do_stmt (tmp
, stmt
, false, 0);
17767 stmt
= begin_if_stmt ();
17768 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17769 if (IF_STMT_CONSTEXPR_P (t
))
17770 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17771 tmp
= RECUR (IF_COND (t
));
17772 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17773 if (IF_STMT_CONSTEXPR_P (t
)
17774 && instantiation_dependent_expression_p (tmp
))
17776 /* We're partially instantiating a generic lambda, but the condition
17777 of the constexpr if is still dependent. Don't substitute into the
17778 branches now, just remember the template arguments. */
17779 do_poplevel (IF_SCOPE (stmt
));
17780 IF_COND (stmt
) = IF_COND (t
);
17781 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17782 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17783 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17787 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17788 /* Don't instantiate the THEN_CLAUSE. */;
17791 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17792 bool inhibit
= integer_zerop (folded
);
17794 ++c_inhibit_evaluation_warnings
;
17795 RECUR (THEN_CLAUSE (t
));
17797 --c_inhibit_evaluation_warnings
;
17799 finish_then_clause (stmt
);
17801 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17802 /* Don't instantiate the ELSE_CLAUSE. */;
17803 else if (ELSE_CLAUSE (t
))
17805 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17806 bool inhibit
= integer_nonzerop (folded
);
17807 begin_else_clause (stmt
);
17809 ++c_inhibit_evaluation_warnings
;
17810 RECUR (ELSE_CLAUSE (t
));
17812 --c_inhibit_evaluation_warnings
;
17813 finish_else_clause (stmt
);
17816 finish_if_stmt (stmt
);
17820 if (BIND_EXPR_BODY_BLOCK (t
))
17821 stmt
= begin_function_body ();
17823 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17824 ? BCS_TRY_BLOCK
: 0);
17826 RECUR (BIND_EXPR_BODY (t
));
17828 if (BIND_EXPR_BODY_BLOCK (t
))
17829 finish_function_body (stmt
);
17831 finish_compound_stmt (stmt
);
17835 finish_break_stmt ();
17838 case CONTINUE_STMT
:
17839 finish_continue_stmt ();
17843 stmt
= begin_switch_stmt ();
17844 tmp
= RECUR (SWITCH_STMT_COND (t
));
17845 finish_switch_cond (tmp
, stmt
);
17846 RECUR (SWITCH_STMT_BODY (t
));
17847 finish_switch_stmt (stmt
);
17850 case CASE_LABEL_EXPR
:
17852 tree decl
= CASE_LABEL (t
);
17853 tree low
= RECUR (CASE_LOW (t
));
17854 tree high
= RECUR (CASE_HIGH (t
));
17855 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17856 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17858 tree label
= CASE_LABEL (l
);
17859 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17860 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17861 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17868 tree decl
= LABEL_EXPR_LABEL (t
);
17871 label
= finish_label_stmt (DECL_NAME (decl
));
17872 if (TREE_CODE (label
) == LABEL_DECL
)
17873 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17874 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17875 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17880 tmp
= GOTO_DESTINATION (t
);
17881 if (TREE_CODE (tmp
) != LABEL_DECL
)
17882 /* Computed goto's must be tsubst'd into. On the other hand,
17883 non-computed gotos must not be; the identifier in question
17884 will have no binding. */
17887 tmp
= DECL_NAME (tmp
);
17888 finish_goto_stmt (tmp
);
17893 tree string
= RECUR (ASM_STRING (t
));
17894 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17895 complain
, in_decl
);
17896 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17897 complain
, in_decl
);
17898 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17899 complain
, in_decl
);
17900 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17901 complain
, in_decl
);
17902 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
17903 outputs
, inputs
, clobbers
, labels
,
17905 tree asm_expr
= tmp
;
17906 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17907 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17908 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17915 stmt
= begin_try_block ();
17916 RECUR (TRY_STMTS (t
));
17917 finish_cleanup_try_block (stmt
);
17918 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17922 tree compound_stmt
= NULL_TREE
;
17924 if (FN_TRY_BLOCK_P (t
))
17925 stmt
= begin_function_try_block (&compound_stmt
);
17927 stmt
= begin_try_block ();
17929 RECUR (TRY_STMTS (t
));
17931 if (FN_TRY_BLOCK_P (t
))
17932 finish_function_try_block (stmt
);
17934 finish_try_block (stmt
);
17936 RECUR (TRY_HANDLERS (t
));
17937 if (FN_TRY_BLOCK_P (t
))
17938 finish_function_handler_sequence (stmt
, compound_stmt
);
17940 finish_handler_sequence (stmt
);
17946 tree decl
= HANDLER_PARMS (t
);
17950 decl
= tsubst (decl
, args
, complain
, in_decl
);
17951 /* Prevent instantiate_decl from trying to instantiate
17952 this variable. We've already done all that needs to be
17954 if (decl
!= error_mark_node
)
17955 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17957 stmt
= begin_handler ();
17958 finish_handler_parms (decl
, stmt
);
17959 RECUR (HANDLER_BODY (t
));
17960 finish_handler (stmt
);
17965 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17966 if (CLASS_TYPE_P (tmp
))
17968 /* Local classes are not independent templates; they are
17969 instantiated along with their containing function. And this
17970 way we don't have to deal with pushing out of one local class
17971 to instantiate a member of another local class. */
17972 /* Closures are handled by the LAMBDA_EXPR. */
17973 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17974 complete_type (tmp
);
17975 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17977 || (TREE_CODE (fld
) == FUNCTION_DECL
17978 && !DECL_ARTIFICIAL (fld
)))
17979 && DECL_TEMPLATE_INSTANTIATION (fld
))
17980 instantiate_decl (fld
, /*defer_ok=*/false,
17981 /*expl_inst_class=*/false);
17985 case STATIC_ASSERT
:
17989 ++c_inhibit_evaluation_warnings
;
17991 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17994 /*integral_constant_expression_p=*/true);
17995 --c_inhibit_evaluation_warnings
;
17997 finish_static_assert (condition
,
17998 STATIC_ASSERT_MESSAGE (t
),
17999 STATIC_ASSERT_SOURCE_LOCATION (t
),
18000 /*member_p=*/false);
18005 case OACC_PARALLEL
:
18007 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18009 stmt
= begin_omp_parallel ();
18010 RECUR (OMP_BODY (t
));
18011 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18015 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18016 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18017 complain
, in_decl
);
18018 if (OMP_PARALLEL_COMBINED (t
))
18019 omp_parallel_combined_clauses
= &tmp
;
18020 stmt
= begin_omp_parallel ();
18021 RECUR (OMP_PARALLEL_BODY (t
));
18022 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18023 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18024 = OMP_PARALLEL_COMBINED (t
);
18025 pop_omp_privatization_clauses (r
);
18029 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18031 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18032 complain
, in_decl
);
18034 OMP_TASK_CLAUSES (t
) = tmp
;
18038 r
= push_omp_privatization_clauses (false);
18039 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18040 complain
, in_decl
);
18041 stmt
= begin_omp_task ();
18042 RECUR (OMP_TASK_BODY (t
));
18043 finish_omp_task (tmp
, stmt
);
18044 pop_omp_privatization_clauses (r
);
18050 case OMP_DISTRIBUTE
:
18054 tree clauses
, body
, pre_body
;
18055 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18056 tree orig_declv
= NULL_TREE
;
18057 tree incrv
= NULL_TREE
;
18058 enum c_omp_region_type ort
= C_ORT_OMP
;
18059 bool any_range_for
= false;
18062 if (TREE_CODE (t
) == OACC_LOOP
)
18065 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18066 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18068 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18070 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18071 if (OMP_FOR_ORIG_DECLS (t
))
18072 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18073 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18074 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18075 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18078 keep_next_level (true);
18079 stmt
= begin_omp_structured_block ();
18081 pre_body
= push_stmt_list ();
18082 RECUR (OMP_FOR_PRE_BODY (t
));
18083 pre_body
= pop_stmt_list (pre_body
);
18085 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18086 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18088 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18089 condv
, incrv
, &clauses
, args
,
18091 integral_constant_expression_p
);
18092 omp_parallel_combined_clauses
= NULL
;
18096 gcc_assert (orig_declv
);
18097 body
= begin_omp_structured_block ();
18098 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18099 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18100 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18101 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18102 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18103 TREE_VEC_ELT (declv
, i
));
18106 body
= push_stmt_list ();
18107 RECUR (OMP_FOR_BODY (t
));
18109 body
= finish_omp_structured_block (body
);
18111 body
= pop_stmt_list (body
);
18113 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18114 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18115 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18119 t
= make_node (TREE_CODE (t
));
18120 TREE_TYPE (t
) = void_type_node
;
18121 OMP_FOR_BODY (t
) = body
;
18122 OMP_FOR_PRE_BODY (t
) = pre_body
;
18123 OMP_FOR_CLAUSES (t
) = clauses
;
18124 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18128 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18130 pop_omp_privatization_clauses (r
);
18135 omp_parallel_combined_clauses
= NULL
;
18140 case OMP_TASKGROUP
:
18142 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18143 && OMP_TEAMS_COMBINED (t
));
18144 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18146 if (TREE_CODE (t
) == OMP_TEAMS
)
18148 keep_next_level (true);
18149 stmt
= begin_omp_structured_block ();
18150 RECUR (OMP_BODY (t
));
18151 stmt
= finish_omp_structured_block (stmt
);
18155 stmt
= push_stmt_list ();
18156 RECUR (OMP_BODY (t
));
18157 stmt
= pop_stmt_list (stmt
);
18161 OMP_BODY (t
) = stmt
;
18162 OMP_CLAUSES (t
) = tmp
;
18164 pop_omp_privatization_clauses (r
);
18168 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18169 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18171 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18172 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18174 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18175 args
, complain
, in_decl
);
18176 if (tmp
== NULL_TREE
)
18177 tmp
= error_mark_node
;
18181 kind
= (enum omp_clause_depend_kind
)
18182 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18185 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18188 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18189 OMP_CLAUSE_DEPEND_SOURCE
,
18190 OMP_DEPOBJ_CLAUSES (t
));
18194 case OMP_TARGET_DATA
:
18196 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18197 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18199 keep_next_level (true);
18200 stmt
= begin_omp_structured_block ();
18202 RECUR (OMP_BODY (t
));
18203 stmt
= finish_omp_structured_block (stmt
);
18206 OMP_BODY (t
) = stmt
;
18207 OMP_CLAUSES (t
) = tmp
;
18208 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18210 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18213 /* For combined target teams, ensure the num_teams and
18214 thread_limit clause expressions are evaluated on the host,
18215 before entering the target construct. */
18217 for (c
= OMP_TEAMS_CLAUSES (teams
);
18218 c
; c
= OMP_CLAUSE_CHAIN (c
))
18219 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18220 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18221 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18223 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18224 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18225 if (expr
== error_mark_node
)
18227 tmp
= TARGET_EXPR_SLOT (expr
);
18229 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18230 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18231 OMP_CLAUSE_FIRSTPRIVATE
);
18232 OMP_CLAUSE_DECL (tc
) = tmp
;
18233 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18234 OMP_TARGET_CLAUSES (t
) = tc
;
18243 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18244 complain
, in_decl
);
18245 OACC_DECLARE_CLAUSES (t
) = tmp
;
18249 case OMP_TARGET_UPDATE
:
18250 case OMP_TARGET_ENTER_DATA
:
18251 case OMP_TARGET_EXIT_DATA
:
18252 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18253 complain
, in_decl
);
18255 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18259 case OACC_ENTER_DATA
:
18260 case OACC_EXIT_DATA
:
18262 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18263 complain
, in_decl
);
18265 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18270 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18271 complain
, in_decl
);
18272 stmt
= push_stmt_list ();
18273 RECUR (OMP_BODY (t
));
18274 stmt
= pop_stmt_list (stmt
);
18277 OMP_BODY (t
) = stmt
;
18278 OMP_ORDERED_CLAUSES (t
) = tmp
;
18284 stmt
= push_stmt_list ();
18285 RECUR (OMP_BODY (t
));
18286 stmt
= pop_stmt_list (stmt
);
18289 OMP_BODY (t
) = stmt
;
18294 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18296 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18297 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18298 complain
, in_decl
);
18299 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18301 tree op1
= TREE_OPERAND (t
, 1);
18302 tree rhs1
= NULL_TREE
;
18304 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18306 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18307 op1
= TREE_OPERAND (op1
, 1);
18309 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18310 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18311 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18312 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18313 OMP_ATOMIC_MEMORY_ORDER (t
));
18317 tree op1
= TREE_OPERAND (t
, 1);
18318 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18319 tree rhs1
= NULL_TREE
;
18320 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18321 enum tree_code opcode
= NOP_EXPR
;
18322 if (code
== OMP_ATOMIC_READ
)
18324 v
= RECUR (TREE_OPERAND (op1
, 0));
18325 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18327 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18328 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18330 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18331 v
= RECUR (TREE_OPERAND (op1
, 0));
18332 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18333 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18335 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18336 op11
= TREE_OPERAND (op11
, 1);
18338 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18339 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18340 opcode
= TREE_CODE (op11
);
18341 if (opcode
== MODIFY_EXPR
)
18347 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18348 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18350 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18351 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18355 case TRANSACTION_EXPR
:
18358 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18359 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18361 if (TRANSACTION_EXPR_IS_STMT (t
))
18363 tree body
= TRANSACTION_EXPR_BODY (t
);
18364 tree noex
= NULL_TREE
;
18365 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18367 noex
= MUST_NOT_THROW_COND (body
);
18368 if (noex
== NULL_TREE
)
18369 noex
= boolean_true_node
;
18370 body
= TREE_OPERAND (body
, 0);
18372 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18374 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18378 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18379 RECUR (TRANSACTION_EXPR_BODY (t
)),
18386 case MUST_NOT_THROW_EXPR
:
18388 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18389 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18390 RETURN (build_must_not_throw_expr (op0
, cond
));
18393 case EXPR_PACK_EXPANSION
:
18394 error ("invalid use of pack expansion expression");
18395 RETURN (error_mark_node
);
18397 case NONTYPE_ARGUMENT_PACK
:
18398 error ("use %<...%> to expand argument pack");
18399 RETURN (error_mark_node
);
18401 case COMPOUND_EXPR
:
18402 tmp
= RECUR (TREE_OPERAND (t
, 0));
18403 if (tmp
== NULL_TREE
)
18404 /* If the first operand was a statement, we're done with it. */
18405 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18406 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18407 RECUR (TREE_OPERAND (t
, 1)),
18410 case ANNOTATE_EXPR
:
18411 tmp
= RECUR (TREE_OPERAND (t
, 0));
18412 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18413 TREE_TYPE (tmp
), tmp
,
18414 RECUR (TREE_OPERAND (t
, 1)),
18415 RECUR (TREE_OPERAND (t
, 2))));
18418 RETURN (add_stmt (copy_node (t
)));
18421 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18423 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18424 /*function_p=*/false,
18425 integral_constant_expression_p
));
18428 RETURN (NULL_TREE
);
18430 input_location
= loc
;
18436 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18437 function. For description of the body see comment above
18438 cp_parser_omp_declare_reduction_exprs. */
18441 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18443 if (t
== NULL_TREE
|| t
== error_mark_node
)
18446 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18448 tree_stmt_iterator tsi
;
18451 memset (stmts
, 0, sizeof stmts
);
18452 for (i
= 0, tsi
= tsi_start (t
);
18453 i
< 7 && !tsi_end_p (tsi
);
18454 i
++, tsi_next (&tsi
))
18455 stmts
[i
] = tsi_stmt (tsi
);
18456 gcc_assert (tsi_end_p (tsi
));
18460 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18461 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18462 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18463 args
, complain
, in_decl
);
18464 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18465 args
, complain
, in_decl
);
18466 DECL_CONTEXT (omp_out
) = current_function_decl
;
18467 DECL_CONTEXT (omp_in
) = current_function_decl
;
18468 keep_next_level (true);
18469 tree block
= begin_omp_structured_block ();
18470 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18471 block
= finish_omp_structured_block (block
);
18472 block
= maybe_cleanup_point_expr_void (block
);
18473 add_decl_expr (omp_out
);
18474 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18475 TREE_NO_WARNING (omp_out
) = 1;
18476 add_decl_expr (omp_in
);
18477 finish_expr_stmt (block
);
18481 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18482 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18483 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18484 args
, complain
, in_decl
);
18485 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18486 args
, complain
, in_decl
);
18487 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18488 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18489 keep_next_level (true);
18490 tree block
= begin_omp_structured_block ();
18491 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18492 block
= finish_omp_structured_block (block
);
18493 block
= maybe_cleanup_point_expr_void (block
);
18494 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18495 add_decl_expr (omp_priv
);
18496 add_decl_expr (omp_orig
);
18497 finish_expr_stmt (block
);
18499 add_decl_expr (omp_orig
);
18503 /* T is a postfix-expression that is not being used in a function
18504 call. Return the substituted version of T. */
18507 tsubst_non_call_postfix_expression (tree t
, tree args
,
18508 tsubst_flags_t complain
,
18511 if (TREE_CODE (t
) == SCOPE_REF
)
18512 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18513 /*done=*/false, /*address_p=*/false);
18515 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18516 /*function_p=*/false,
18517 /*integral_constant_expression_p=*/false);
18522 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18523 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18524 dependent init-capture. */
18527 prepend_one_capture (tree field
, tree init
, tree
&list
,
18528 tsubst_flags_t complain
)
18530 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18532 tree type
= NULL_TREE
;
18535 if (complain
& tf_error
)
18536 error ("empty initializer in lambda init-capture");
18537 init
= error_mark_node
;
18539 else if (TREE_CODE (init
) == TREE_LIST
)
18540 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18542 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18543 TREE_TYPE (field
) = type
;
18544 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18546 list
= tree_cons (field
, init
, list
);
18549 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18550 instantiation context. Instantiating a pack expansion containing a lambda
18551 might result in multiple lambdas all based on the same lambda in the
18555 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18557 tree oldfn
= lambda_function (t
);
18560 tree r
= build_lambda_expr ();
18562 LAMBDA_EXPR_LOCATION (r
)
18563 = LAMBDA_EXPR_LOCATION (t
);
18564 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18565 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18566 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18567 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18569 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18570 /* A lambda in a default argument outside a class gets no
18571 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18572 tsubst_default_argument calls start_lambda_scope, so we need to
18573 specifically ignore it here, and use the global scope. */
18574 record_null_lambda_scope (r
);
18576 record_lambda_scope (r
);
18578 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18579 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18581 vec
<tree
,va_gc
>* field_packs
= NULL
;
18583 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18584 cap
= TREE_CHAIN (cap
))
18586 tree ofield
= TREE_PURPOSE (cap
);
18587 if (PACK_EXPANSION_P (ofield
))
18588 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18589 tree field
= tsubst_decl (ofield
, args
, complain
);
18591 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18593 /* Remember these for when we've pushed local_specializations. */
18594 vec_safe_push (field_packs
, ofield
);
18595 vec_safe_push (field_packs
, field
);
18598 if (field
== error_mark_node
)
18599 return error_mark_node
;
18601 tree init
= TREE_VALUE (cap
);
18602 if (PACK_EXPANSION_P (init
))
18603 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18605 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18606 /*fn*/false, /*constexpr*/false);
18608 if (TREE_CODE (field
) == TREE_VEC
)
18610 int len
= TREE_VEC_LENGTH (field
);
18611 gcc_assert (TREE_CODE (init
) == TREE_VEC
18612 && TREE_VEC_LENGTH (init
) == len
);
18613 for (int i
= 0; i
< len
; ++i
)
18614 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18615 TREE_VEC_ELT (init
, i
),
18616 LAMBDA_EXPR_CAPTURE_LIST (r
),
18621 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18624 if (id_equal (DECL_NAME (field
), "__this"))
18625 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18629 tree type
= begin_lambda_type (r
);
18630 if (type
== error_mark_node
)
18631 return error_mark_node
;
18633 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18634 determine_visibility (TYPE_NAME (type
));
18636 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18638 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18639 ? DECL_TI_TEMPLATE (oldfn
)
18642 tree fntype
= static_fn_type (oldfn
);
18644 ++processing_template_decl
;
18645 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18647 --processing_template_decl
;
18649 if (fntype
== error_mark_node
)
18650 r
= error_mark_node
;
18653 /* The body of a lambda-expression is not a subexpression of the
18654 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18655 which would be skipped if cp_unevaluated_operand. */
18658 /* Fix the type of 'this'. */
18659 fntype
= build_memfn_type (fntype
, type
,
18660 type_memfn_quals (fntype
),
18661 type_memfn_rqual (fntype
));
18665 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18666 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18667 finish_member_declaration (tmpl
);
18672 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18673 finish_member_declaration (fn
);
18676 /* Let finish_function set this. */
18677 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18679 bool nested
= cfun
;
18681 push_function_context ();
18683 /* Still increment function_depth so that we don't GC in the
18684 middle of an expression. */
18687 local_specialization_stack
s (lss_copy
);
18689 tree body
= start_lambda_function (fn
, r
);
18691 /* Now record them for lookup_init_capture_pack. */
18692 int fplen
= vec_safe_length (field_packs
);
18693 for (int i
= 0; i
< fplen
; )
18695 tree pack
= (*field_packs
)[i
++];
18696 tree inst
= (*field_packs
)[i
++];
18697 register_local_specialization (inst
, pack
);
18699 release_tree_vector (field_packs
);
18701 register_parameter_specializations (oldfn
, fn
);
18705 /* We might not partially instantiate some parts of the function, so
18706 copy these flags from the original template. */
18707 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18708 current_function_returns_value
= ol
->returns_value
;
18709 current_function_returns_null
= ol
->returns_null
;
18710 current_function_returns_abnormally
= ol
->returns_abnormally
;
18711 current_function_infinite_loop
= ol
->infinite_loop
;
18714 /* [temp.deduct] A lambda-expression appearing in a function type or a
18715 template parameter is not considered part of the immediate context for
18716 the purposes of template argument deduction. */
18717 complain
= tf_warning_or_error
;
18719 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18720 /*constexpr*/false);
18722 finish_lambda_function (body
);
18725 pop_function_context ();
18729 /* The capture list was built up in reverse order; fix that now. */
18730 LAMBDA_EXPR_CAPTURE_LIST (r
)
18731 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18733 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18735 maybe_add_lambda_conv_op (type
);
18738 finish_struct (type
, /*attr*/NULL_TREE
);
18740 insert_pending_capture_proxies ();
18745 /* Like tsubst but deals with expressions and performs semantic
18746 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18749 tsubst_copy_and_build (tree t
,
18751 tsubst_flags_t complain
,
18754 bool integral_constant_expression_p
)
18756 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18757 #define RECUR(NODE) \
18758 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18759 /*function_p=*/false, \
18760 integral_constant_expression_p)
18765 if (t
== NULL_TREE
|| t
== error_mark_node
)
18768 loc
= input_location
;
18769 if (location_t eloc
= cp_expr_location (t
))
18770 input_location
= eloc
;
18772 /* N3276 decltype magic only applies to calls at the top level or on the
18773 right side of a comma. */
18774 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18775 complain
&= ~tf_decltype
;
18777 switch (TREE_CODE (t
))
18781 /* Fall through. */
18782 case IDENTIFIER_NODE
:
18786 bool non_integral_constant_expression_p
;
18787 const char *error_msg
;
18789 if (IDENTIFIER_CONV_OP_P (t
))
18791 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18792 t
= make_conv_op_name (new_type
);
18795 /* Look up the name. */
18796 decl
= lookup_name (t
);
18798 /* By convention, expressions use ERROR_MARK_NODE to indicate
18799 failure, not NULL_TREE. */
18800 if (decl
== NULL_TREE
)
18801 decl
= error_mark_node
;
18803 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18805 integral_constant_expression_p
,
18806 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18807 &non_integral_constant_expression_p
,
18808 /*template_p=*/false,
18810 /*address_p=*/false,
18811 /*template_arg_p=*/false,
18816 if (!function_p
&& identifier_p (decl
))
18818 if (complain
& tf_error
)
18819 unqualified_name_lookup_error (decl
);
18820 decl
= error_mark_node
;
18825 case TEMPLATE_ID_EXPR
:
18828 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18829 tree targs
= TREE_OPERAND (t
, 1);
18832 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18833 if (targs
== error_mark_node
)
18834 RETURN (error_mark_node
);
18836 if (TREE_CODE (templ
) == SCOPE_REF
)
18838 tree name
= TREE_OPERAND (templ
, 1);
18839 tree tid
= lookup_template_function (name
, targs
);
18840 TREE_OPERAND (templ
, 1) = tid
;
18844 if (concept_definition_p (templ
))
18846 tree check
= build_concept_check (templ
, targs
, complain
);
18847 if (check
== error_mark_node
)
18848 RETURN (error_mark_node
);
18850 tree id
= unpack_concept_check (check
);
18852 /* If we built a function concept check, return the underlying
18853 template-id. So we can evaluate it as a function call. */
18854 if (function_concept_p (TREE_OPERAND (id
, 0)))
18857 /* Evaluate the concept, if needed. */
18858 tree args
= TREE_OPERAND (id
, 1);
18859 if (!uses_template_parms (args
)
18860 && !processing_constraint_expression_p ())
18861 RETURN (evaluate_concept_check (check
, complain
));
18866 if (variable_template_p (templ
))
18867 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
18869 if (TREE_CODE (templ
) == COMPONENT_REF
)
18871 object
= TREE_OPERAND (templ
, 0);
18872 templ
= TREE_OPERAND (templ
, 1);
18875 object
= NULL_TREE
;
18876 templ
= lookup_template_function (templ
, targs
);
18879 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18880 object
, templ
, NULL_TREE
));
18882 RETURN (baselink_for_fns (templ
));
18887 tree r
= RECUR (TREE_OPERAND (t
, 0));
18889 if (REFERENCE_REF_P (t
))
18891 /* A type conversion to reference type will be enclosed in
18892 such an indirect ref, but the substitution of the cast
18893 will have also added such an indirect ref. */
18894 r
= convert_from_reference (r
);
18897 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18898 complain
|decltype_flag
);
18900 if (REF_PARENTHESIZED_P (t
))
18901 r
= force_paren_expr (r
);
18908 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18909 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18910 RETURN (build_nop (type
, op0
));
18913 case IMPLICIT_CONV_EXPR
:
18915 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18916 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18917 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18919 retval
= copy_node (t
);
18920 TREE_TYPE (retval
) = type
;
18921 TREE_OPERAND (retval
, 0) = expr
;
18924 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18925 /* We'll pass this to convert_nontype_argument again, we don't need
18926 to actually perform any conversion here. */
18928 int flags
= LOOKUP_IMPLICIT
;
18929 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18930 flags
= LOOKUP_NORMAL
;
18931 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
18932 flags
|= LOOKUP_NO_NARROWING
;
18933 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18939 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18940 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18941 if (op0
== error_mark_node
)
18942 RETURN (error_mark_node
);
18943 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
18947 case REINTERPRET_CAST_EXPR
:
18948 case CONST_CAST_EXPR
:
18949 case DYNAMIC_CAST_EXPR
:
18950 case STATIC_CAST_EXPR
:
18953 tree op
, r
= NULL_TREE
;
18955 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18956 if (integral_constant_expression_p
18957 && !cast_valid_in_integral_constant_expression_p (type
))
18959 if (complain
& tf_error
)
18960 error ("a cast to a type other than an integral or "
18961 "enumeration type cannot appear in a constant-expression");
18962 RETURN (error_mark_node
);
18965 op
= RECUR (TREE_OPERAND (t
, 0));
18967 warning_sentinel
s(warn_useless_cast
);
18968 warning_sentinel
s2(warn_ignored_qualifiers
);
18969 switch (TREE_CODE (t
))
18972 r
= build_functional_cast (type
, op
, complain
);
18974 case REINTERPRET_CAST_EXPR
:
18975 r
= build_reinterpret_cast (type
, op
, complain
);
18977 case CONST_CAST_EXPR
:
18978 r
= build_const_cast (type
, op
, complain
);
18980 case DYNAMIC_CAST_EXPR
:
18981 r
= build_dynamic_cast (type
, op
, complain
);
18983 case STATIC_CAST_EXPR
:
18984 r
= build_static_cast (type
, op
, complain
);
18987 gcc_unreachable ();
18993 case POSTDECREMENT_EXPR
:
18994 case POSTINCREMENT_EXPR
:
18995 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18996 args
, complain
, in_decl
);
18997 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
18998 complain
|decltype_flag
));
19000 case PREDECREMENT_EXPR
:
19001 case PREINCREMENT_EXPR
:
19005 case TRUTH_NOT_EXPR
:
19006 case UNARY_PLUS_EXPR
: /* Unary + */
19007 case REALPART_EXPR
:
19008 case IMAGPART_EXPR
:
19009 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19010 RECUR (TREE_OPERAND (t
, 0)),
19011 complain
|decltype_flag
));
19013 case FIX_TRUNC_EXPR
:
19014 gcc_unreachable ();
19017 op1
= TREE_OPERAND (t
, 0);
19018 if (TREE_CODE (op1
) == LABEL_DECL
)
19019 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19020 EXPR_LOCATION (op1
)));
19021 if (TREE_CODE (op1
) == SCOPE_REF
)
19022 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19023 /*done=*/true, /*address_p=*/true);
19025 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19027 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19028 complain
|decltype_flag
));
19033 case TRUNC_DIV_EXPR
:
19034 case CEIL_DIV_EXPR
:
19035 case FLOOR_DIV_EXPR
:
19036 case ROUND_DIV_EXPR
:
19037 case EXACT_DIV_EXPR
:
19041 case TRUNC_MOD_EXPR
:
19042 case FLOOR_MOD_EXPR
:
19043 case TRUTH_ANDIF_EXPR
:
19044 case TRUTH_ORIF_EXPR
:
19045 case TRUTH_AND_EXPR
:
19046 case TRUTH_OR_EXPR
:
19057 case SPACESHIP_EXPR
:
19061 warning_sentinel
s1(warn_type_limits
);
19062 warning_sentinel
s2(warn_div_by_zero
);
19063 warning_sentinel
s3(warn_logical_op
);
19064 warning_sentinel
s4(warn_tautological_compare
);
19065 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19066 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19067 tree r
= build_x_binary_op
19068 (input_location
, TREE_CODE (t
),
19070 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19072 : TREE_CODE (TREE_OPERAND (t
, 0))),
19074 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19076 : TREE_CODE (TREE_OPERAND (t
, 1))),
19078 complain
|decltype_flag
);
19079 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19080 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19085 case POINTER_PLUS_EXPR
:
19087 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19088 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19089 RETURN (fold_build_pointer_plus (op0
, op1
));
19093 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19094 /*address_p=*/false));
19096 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19097 args
, complain
, in_decl
);
19098 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19099 RECUR (TREE_OPERAND (t
, 1)),
19100 complain
|decltype_flag
));
19103 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19104 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19105 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19112 op1
= TREE_OPERAND (t
, 0);
19113 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19114 op1
= TREE_TYPE (op1
);
19115 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19116 && ALIGNOF_EXPR_STD_P (t
));
19119 /* When there are no ARGS, we are trying to evaluate a
19120 non-dependent expression from the parser. Trying to do
19121 the substitutions may not work. */
19123 op1
= TREE_TYPE (op1
);
19127 ++cp_unevaluated_operand
;
19128 ++c_inhibit_evaluation_warnings
;
19130 op1
= tsubst (op1
, args
, complain
, in_decl
);
19132 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19133 /*function_p=*/false,
19134 /*integral_constant_expression_p=*/
19136 --cp_unevaluated_operand
;
19137 --c_inhibit_evaluation_warnings
;
19140 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
19141 complain
& tf_error
);
19143 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
19144 complain
& tf_error
);
19145 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19147 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19149 if (!processing_template_decl
&& TYPE_P (op1
))
19151 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19152 build1 (NOP_EXPR
, op1
, error_mark_node
));
19153 SIZEOF_EXPR_TYPE_P (r
) = 1;
19156 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19157 TREE_SIDE_EFFECTS (r
) = 0;
19158 TREE_READONLY (r
) = 1;
19160 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19165 case AT_ENCODE_EXPR
:
19167 op1
= TREE_OPERAND (t
, 0);
19168 ++cp_unevaluated_operand
;
19169 ++c_inhibit_evaluation_warnings
;
19170 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19171 /*function_p=*/false,
19172 /*integral_constant_expression_p=*/false);
19173 --cp_unevaluated_operand
;
19174 --c_inhibit_evaluation_warnings
;
19175 RETURN (objc_build_encode_expr (op1
));
19178 case NOEXCEPT_EXPR
:
19179 op1
= TREE_OPERAND (t
, 0);
19180 ++cp_unevaluated_operand
;
19181 ++c_inhibit_evaluation_warnings
;
19182 ++cp_noexcept_operand
;
19183 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19184 /*function_p=*/false,
19185 /*integral_constant_expression_p=*/false);
19186 --cp_unevaluated_operand
;
19187 --c_inhibit_evaluation_warnings
;
19188 --cp_noexcept_operand
;
19189 RETURN (finish_noexcept_expr (op1
, complain
));
19193 warning_sentinel
s(warn_div_by_zero
);
19194 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19195 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19196 tree r
= build_x_modify_expr
19197 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19198 complain
|decltype_flag
);
19199 /* TREE_NO_WARNING must be set if either the expression was
19200 parenthesized or it uses an operator such as >>= rather
19201 than plain assignment. In the former case, it was already
19202 set and must be copied. In the latter case,
19203 build_x_modify_expr sets it and it must not be reset
19205 if (TREE_NO_WARNING (t
))
19206 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19212 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19213 args
, complain
, in_decl
);
19214 /* Remember that there was a reference to this entity. */
19216 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19217 RETURN (error_mark_node
);
19218 RETURN (build_x_arrow (input_location
, op1
, complain
));
19222 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19223 tree init
= RECUR (TREE_OPERAND (t
, 3));
19224 vec
<tree
, va_gc
> *placement_vec
;
19225 vec
<tree
, va_gc
> *init_vec
;
19228 if (placement
== NULL_TREE
)
19229 placement_vec
= NULL
;
19232 placement_vec
= make_tree_vector ();
19233 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19234 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19237 /* If there was an initializer in the original tree, but it
19238 instantiated to an empty list, then we should pass a
19239 non-NULL empty vector to tell build_new that it was an
19240 empty initializer() rather than no initializer. This can
19241 only happen when the initializer is a pack expansion whose
19242 parameter packs are of length zero. */
19243 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19247 init_vec
= make_tree_vector ();
19248 if (init
== void_node
)
19249 gcc_assert (init_vec
!= NULL
);
19252 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19253 vec_safe_push (init_vec
, TREE_VALUE (init
));
19257 /* Avoid passing an enclosing decl to valid_array_size_p. */
19258 in_decl
= NULL_TREE
;
19260 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19261 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19262 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
19263 NEW_EXPR_USE_GLOBAL (t
),
19266 if (placement_vec
!= NULL
)
19267 release_tree_vector (placement_vec
);
19268 if (init_vec
!= NULL
)
19269 release_tree_vector (init_vec
);
19276 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19277 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19278 RETURN (delete_sanity (op0
, op1
,
19279 DELETE_EXPR_USE_VEC (t
),
19280 DELETE_EXPR_USE_GLOBAL (t
),
19284 case COMPOUND_EXPR
:
19286 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19287 complain
& ~tf_decltype
, in_decl
,
19288 /*function_p=*/false,
19289 integral_constant_expression_p
);
19290 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19292 RECUR (TREE_OPERAND (t
, 1)),
19293 complain
|decltype_flag
));
19299 unsigned int nargs
, i
;
19304 function
= CALL_EXPR_FN (t
);
19305 /* Internal function with no arguments. */
19306 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19309 /* When we parsed the expression, we determined whether or
19310 not Koenig lookup should be performed. */
19311 koenig_p
= KOENIG_LOOKUP_P (t
);
19312 if (function
== NULL_TREE
)
19315 qualified_p
= false;
19317 else if (TREE_CODE (function
) == SCOPE_REF
)
19319 qualified_p
= true;
19320 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19322 /*address_p=*/false);
19324 else if (koenig_p
&& identifier_p (function
))
19326 /* Do nothing; calling tsubst_copy_and_build on an identifier
19327 would incorrectly perform unqualified lookup again.
19329 Note that we can also have an IDENTIFIER_NODE if the earlier
19330 unqualified lookup found a member function; in that case
19331 koenig_p will be false and we do want to do the lookup
19332 again to find the instantiated member function.
19334 FIXME but doing that causes c++/15272, so we need to stop
19335 using IDENTIFIER_NODE in that situation. */
19336 qualified_p
= false;
19340 if (TREE_CODE (function
) == COMPONENT_REF
)
19342 tree op
= TREE_OPERAND (function
, 1);
19344 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19345 || (BASELINK_P (op
)
19346 && BASELINK_QUALIFIED_P (op
)));
19349 qualified_p
= false;
19351 if (TREE_CODE (function
) == ADDR_EXPR
19352 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19353 /* Avoid error about taking the address of a constructor. */
19354 function
= TREE_OPERAND (function
, 0);
19356 function
= tsubst_copy_and_build (function
, args
, complain
,
19359 integral_constant_expression_p
);
19361 if (BASELINK_P (function
))
19362 qualified_p
= true;
19365 nargs
= call_expr_nargs (t
);
19366 releasing_vec call_args
;
19367 for (i
= 0; i
< nargs
; ++i
)
19369 tree arg
= CALL_EXPR_ARG (t
, i
);
19371 if (!PACK_EXPANSION_P (arg
))
19372 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19375 /* Expand the pack expansion and push each entry onto
19377 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19378 if (TREE_CODE (arg
) == TREE_VEC
)
19380 unsigned int len
, j
;
19382 len
= TREE_VEC_LENGTH (arg
);
19383 for (j
= 0; j
< len
; ++j
)
19385 tree value
= TREE_VEC_ELT (arg
, j
);
19386 if (value
!= NULL_TREE
)
19387 value
= convert_from_reference (value
);
19388 vec_safe_push (call_args
, value
);
19393 /* A partial substitution. Add one entry. */
19394 vec_safe_push (call_args
, arg
);
19399 /* Stripped-down processing for a call in a thunk. Specifically, in
19400 the thunk template for a generic lambda. */
19401 if (CALL_FROM_THUNK_P (t
))
19403 /* Now that we've expanded any packs, the number of call args
19404 might be different. */
19405 unsigned int cargs
= call_args
->length ();
19406 tree thisarg
= NULL_TREE
;
19407 if (TREE_CODE (function
) == COMPONENT_REF
)
19409 thisarg
= TREE_OPERAND (function
, 0);
19410 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19411 thisarg
= TREE_OPERAND (thisarg
, 0);
19412 function
= TREE_OPERAND (function
, 1);
19413 if (TREE_CODE (function
) == BASELINK
)
19414 function
= BASELINK_FUNCTIONS (function
);
19416 /* We aren't going to do normal overload resolution, so force the
19417 template-id to resolve. */
19418 function
= resolve_nondeduced_context (function
, complain
);
19419 for (unsigned i
= 0; i
< cargs
; ++i
)
19421 /* In a thunk, pass through args directly, without any
19423 tree arg
= (*call_args
)[i
];
19424 while (TREE_CODE (arg
) != PARM_DECL
)
19425 arg
= TREE_OPERAND (arg
, 0);
19426 (*call_args
)[i
] = arg
;
19430 /* If there are no other args, just push 'this'. */
19432 vec_safe_push (call_args
, thisarg
);
19435 /* Otherwise, shift the other args over to make room. */
19436 tree last
= (*call_args
)[cargs
- 1];
19437 vec_safe_push (call_args
, last
);
19438 for (int i
= cargs
- 1; i
> 0; --i
)
19439 (*call_args
)[i
] = (*call_args
)[i
- 1];
19440 (*call_args
)[0] = thisarg
;
19443 ret
= build_call_a (function
, call_args
->length (),
19444 call_args
->address ());
19445 /* The thunk location is not interesting. */
19446 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19447 CALL_FROM_THUNK_P (ret
) = true;
19448 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19449 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19454 /* We do not perform argument-dependent lookup if normal
19455 lookup finds a non-function, in accordance with the
19456 expected resolution of DR 218. */
19458 && ((is_overloaded_fn (function
)
19459 /* If lookup found a member function, the Koenig lookup is
19460 not appropriate, even if an unqualified-name was used
19461 to denote the function. */
19462 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19463 || identifier_p (function
))
19464 /* Only do this when substitution turns a dependent call
19465 into a non-dependent call. */
19466 && type_dependent_expression_p_push (t
)
19467 && !any_type_dependent_arguments_p (call_args
))
19468 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19470 if (function
!= NULL_TREE
19471 && identifier_p (function
)
19472 && !any_type_dependent_arguments_p (call_args
))
19474 if (koenig_p
&& (complain
& tf_warning_or_error
))
19476 /* For backwards compatibility and good diagnostics, try
19477 the unqualified lookup again if we aren't in SFINAE
19479 tree unq
= (tsubst_copy_and_build
19480 (function
, args
, complain
, in_decl
, true,
19481 integral_constant_expression_p
));
19482 if (unq
== error_mark_node
)
19483 RETURN (error_mark_node
);
19485 if (unq
!= function
)
19487 /* In a lambda fn, we have to be careful to not
19488 introduce new this captures. Legacy code can't
19489 be using lambdas anyway, so it's ok to be
19491 bool in_lambda
= (current_class_type
19492 && LAMBDA_TYPE_P (current_class_type
));
19493 char const *const msg
19494 = G_("%qD was not declared in this scope, "
19495 "and no declarations were found by "
19496 "argument-dependent lookup at the point "
19497 "of instantiation");
19501 error_at (cp_expr_loc_or_input_loc (t
),
19504 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19510 if (INDIRECT_REF_P (fn
))
19511 fn
= TREE_OPERAND (fn
, 0);
19512 if (is_overloaded_fn (fn
))
19513 fn
= get_first_fn (fn
);
19516 /* Can't say anything more. */;
19517 else if (DECL_CLASS_SCOPE_P (fn
))
19519 location_t loc
= cp_expr_loc_or_input_loc (t
);
19521 "declarations in dependent base %qT are "
19522 "not found by unqualified lookup",
19523 DECL_CLASS_CONTEXT (fn
));
19524 if (current_class_ptr
)
19526 "use %<this->%D%> instead", function
);
19529 "use %<%T::%D%> instead",
19530 current_class_name
, function
);
19533 inform (DECL_SOURCE_LOCATION (fn
),
19534 "%qD declared here, later in the "
19535 "translation unit", fn
);
19537 RETURN (error_mark_node
);
19543 if (identifier_p (function
))
19545 if (complain
& tf_error
)
19546 unqualified_name_lookup_error (function
);
19547 RETURN (error_mark_node
);
19551 /* Remember that there was a reference to this entity. */
19552 if (function
!= NULL_TREE
19553 && DECL_P (function
)
19554 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19555 RETURN (error_mark_node
);
19557 /* Put back tf_decltype for the actual call. */
19558 complain
|= decltype_flag
;
19560 if (function
== NULL_TREE
)
19561 switch (CALL_EXPR_IFN (t
))
19564 gcc_assert (nargs
== 1);
19565 if (vec_safe_length (call_args
) != 1)
19567 error_at (cp_expr_loc_or_input_loc (t
),
19568 "wrong number of arguments to "
19569 "%<__builtin_launder%>");
19570 ret
= error_mark_node
;
19573 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19574 (*call_args
)[0], complain
);
19577 case IFN_VEC_CONVERT
:
19578 gcc_assert (nargs
== 1);
19579 if (vec_safe_length (call_args
) != 1)
19581 error_at (cp_expr_loc_or_input_loc (t
),
19582 "wrong number of arguments to "
19583 "%<__builtin_convertvector%>");
19584 ret
= error_mark_node
;
19587 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19588 tsubst (TREE_TYPE (t
), args
,
19589 complain
, in_decl
),
19591 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19596 /* Unsupported internal function with arguments. */
19597 gcc_unreachable ();
19599 else if (TREE_CODE (function
) == OFFSET_REF
19600 || TREE_CODE (function
) == DOTSTAR_EXPR
19601 || TREE_CODE (function
) == MEMBER_REF
)
19602 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19604 else if (TREE_CODE (function
) == COMPONENT_REF
)
19606 tree instance
= TREE_OPERAND (function
, 0);
19607 tree fn
= TREE_OPERAND (function
, 1);
19609 if (processing_template_decl
19610 && (type_dependent_expression_p (instance
)
19611 || (!BASELINK_P (fn
)
19612 && TREE_CODE (fn
) != FIELD_DECL
)
19613 || type_dependent_expression_p (fn
)
19614 || any_type_dependent_arguments_p (call_args
)))
19615 ret
= build_min_nt_call_vec (function
, call_args
);
19616 else if (!BASELINK_P (fn
))
19617 ret
= finish_call_expr (function
, &call_args
,
19618 /*disallow_virtual=*/false,
19619 /*koenig_p=*/false,
19622 ret
= (build_new_method_call
19624 &call_args
, NULL_TREE
,
19625 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19629 else if (concept_check_p (function
))
19631 /* FUNCTION is a template-id referring to a concept definition. */
19632 tree id
= unpack_concept_check (function
);
19633 tree tmpl
= TREE_OPERAND (id
, 0);
19634 tree args
= TREE_OPERAND (id
, 1);
19636 /* Calls to standard and variable concepts should have been
19637 previously diagnosed. */
19638 gcc_assert (function_concept_p (tmpl
));
19640 /* Ensure the result is wrapped as a call expression. */
19641 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
19643 /* Possibly evaluate the check if it is non-dependent. */
19644 if (!uses_template_parms (args
)
19645 && !processing_constraint_expression_p ())
19646 ret
= evaluate_concept_check (ret
, complain
);
19649 ret
= finish_call_expr (function
, &call_args
,
19650 /*disallow_virtual=*/qualified_p
,
19654 if (ret
!= error_mark_node
)
19656 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
19657 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
19658 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
19659 if (op
|| ord
|| rev
)
19661 function
= extract_call_expr (ret
);
19662 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
19663 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
19664 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
19673 tree cond
= RECUR (TREE_OPERAND (t
, 0));
19674 cond
= mark_rvalue_use (cond
);
19675 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19678 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19680 if (integer_zerop (folded_cond
))
19682 ++c_inhibit_evaluation_warnings
;
19683 exp1
= RECUR (TREE_OPERAND (t
, 1));
19684 --c_inhibit_evaluation_warnings
;
19685 exp2
= RECUR (TREE_OPERAND (t
, 2));
19689 exp1
= RECUR (TREE_OPERAND (t
, 1));
19690 ++c_inhibit_evaluation_warnings
;
19691 exp2
= RECUR (TREE_OPERAND (t
, 2));
19692 --c_inhibit_evaluation_warnings
;
19694 cond
= folded_cond
;
19698 exp1
= RECUR (TREE_OPERAND (t
, 1));
19699 exp2
= RECUR (TREE_OPERAND (t
, 2));
19702 warning_sentinel
s(warn_duplicated_branches
);
19703 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19704 cond
, exp1
, exp2
, complain
));
19707 case PSEUDO_DTOR_EXPR
:
19709 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19710 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19711 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19712 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19718 tree purpose
, value
, chain
;
19720 if (t
== void_list_node
)
19723 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19724 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19726 /* We have pack expansions, so expand those and
19727 create a new list out of it. */
19728 tree purposevec
= NULL_TREE
;
19729 tree valuevec
= NULL_TREE
;
19733 /* Expand the argument expressions. */
19734 if (TREE_PURPOSE (t
))
19735 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19736 complain
, in_decl
);
19737 if (TREE_VALUE (t
))
19738 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19739 complain
, in_decl
);
19741 /* Build the rest of the list. */
19742 chain
= TREE_CHAIN (t
);
19743 if (chain
&& chain
!= void_type_node
)
19744 chain
= RECUR (chain
);
19746 /* Determine the number of arguments. */
19747 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19749 len
= TREE_VEC_LENGTH (purposevec
);
19750 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19752 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19753 len
= TREE_VEC_LENGTH (valuevec
);
19756 /* Since we only performed a partial substitution into
19757 the argument pack, we only RETURN (a single list
19759 if (purposevec
== TREE_PURPOSE (t
)
19760 && valuevec
== TREE_VALUE (t
)
19761 && chain
== TREE_CHAIN (t
))
19764 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19767 /* Convert the argument vectors into a TREE_LIST */
19771 /* Grab the Ith values. */
19773 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19776 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19779 /* Build the list (backwards). */
19780 chain
= tree_cons (purpose
, value
, chain
);
19786 purpose
= TREE_PURPOSE (t
);
19788 purpose
= RECUR (purpose
);
19789 value
= TREE_VALUE (t
);
19791 value
= RECUR (value
);
19792 chain
= TREE_CHAIN (t
);
19793 if (chain
&& chain
!= void_type_node
)
19794 chain
= RECUR (chain
);
19795 if (purpose
== TREE_PURPOSE (t
)
19796 && value
== TREE_VALUE (t
)
19797 && chain
== TREE_CHAIN (t
))
19799 RETURN (tree_cons (purpose
, value
, chain
));
19802 case COMPONENT_REF
:
19809 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19810 args
, complain
, in_decl
);
19811 /* Remember that there was a reference to this entity. */
19812 if (DECL_P (object
)
19813 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19814 RETURN (error_mark_node
);
19815 object_type
= TREE_TYPE (object
);
19817 member
= TREE_OPERAND (t
, 1);
19818 if (BASELINK_P (member
))
19819 member
= tsubst_baselink (member
,
19820 non_reference (TREE_TYPE (object
)),
19821 args
, complain
, in_decl
);
19823 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19824 if (member
== error_mark_node
)
19825 RETURN (error_mark_node
);
19827 if (TREE_CODE (member
) == FIELD_DECL
)
19829 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19830 if (TREE_CODE (r
) == COMPONENT_REF
)
19831 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19834 else if (type_dependent_expression_p (object
))
19835 /* We can't do much here. */;
19836 else if (!CLASS_TYPE_P (object_type
))
19838 if (scalarish_type_p (object_type
))
19840 tree s
= NULL_TREE
;
19841 tree dtor
= member
;
19843 if (TREE_CODE (dtor
) == SCOPE_REF
)
19845 s
= TREE_OPERAND (dtor
, 0);
19846 dtor
= TREE_OPERAND (dtor
, 1);
19848 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19850 dtor
= TREE_OPERAND (dtor
, 0);
19852 RETURN (finish_pseudo_destructor_expr
19853 (object
, s
, dtor
, input_location
));
19857 else if (TREE_CODE (member
) == SCOPE_REF
19858 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19860 /* Lookup the template functions now that we know what the
19862 tree scope
= TREE_OPERAND (member
, 0);
19863 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19864 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19865 member
= lookup_qualified_name (scope
, tmpl
,
19866 /*is_type_p=*/false,
19867 /*complain=*/false);
19868 if (BASELINK_P (member
))
19870 BASELINK_FUNCTIONS (member
)
19871 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19873 member
= (adjust_result_of_qualified_name_lookup
19874 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19879 qualified_name_lookup_error (scope
, tmpl
, member
,
19881 RETURN (error_mark_node
);
19884 else if (TREE_CODE (member
) == SCOPE_REF
19885 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19886 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19888 if (complain
& tf_error
)
19890 if (TYPE_P (TREE_OPERAND (member
, 0)))
19891 error ("%qT is not a class or namespace",
19892 TREE_OPERAND (member
, 0));
19894 error ("%qD is not a class or namespace",
19895 TREE_OPERAND (member
, 0));
19897 RETURN (error_mark_node
);
19900 r
= finish_class_member_access_expr (object
, member
,
19901 /*template_p=*/false,
19903 if (TREE_CODE (r
) == COMPONENT_REF
)
19904 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19909 RETURN (build_throw
19910 (RECUR (TREE_OPERAND (t
, 0))));
19914 vec
<constructor_elt
, va_gc
> *n
;
19915 constructor_elt
*ce
;
19916 unsigned HOST_WIDE_INT idx
;
19917 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19918 bool process_index_p
;
19920 bool need_copy_p
= false;
19923 if (type
== error_mark_node
)
19924 RETURN (error_mark_node
);
19926 /* We do not want to process the index of aggregate
19927 initializers as they are identifier nodes which will be
19928 looked up by digest_init. */
19929 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19931 if (null_member_pointer_value_p (t
))
19933 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
19937 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19938 newlen
= vec_safe_length (n
);
19939 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19941 if (ce
->index
&& process_index_p
19942 /* An identifier index is looked up in the type
19943 being initialized, not the current scope. */
19944 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
19945 ce
->index
= RECUR (ce
->index
);
19947 if (PACK_EXPANSION_P (ce
->value
))
19949 /* Substitute into the pack expansion. */
19950 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
19953 if (ce
->value
== error_mark_node
19954 || PACK_EXPANSION_P (ce
->value
))
19956 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
19957 /* Just move the argument into place. */
19958 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
19961 /* Update the length of the final CONSTRUCTOR
19962 arguments vector, and note that we will need to
19964 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
19965 need_copy_p
= true;
19969 ce
->value
= RECUR (ce
->value
);
19974 vec
<constructor_elt
, va_gc
> *old_n
= n
;
19976 vec_alloc (n
, newlen
);
19977 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
19979 if (TREE_CODE (ce
->value
) == TREE_VEC
)
19981 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
19982 for (i
= 0; i
< len
; ++i
)
19983 CONSTRUCTOR_APPEND_ELT (n
, 0,
19984 TREE_VEC_ELT (ce
->value
, i
));
19987 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
19991 r
= build_constructor (init_list_type_node
, n
);
19992 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
19993 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
19994 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
19996 if (TREE_HAS_CONSTRUCTOR (t
))
19998 fcl_t cl
= fcl_functional
;
19999 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20001 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20004 TREE_TYPE (r
) = type
;
20010 tree operand_0
= TREE_OPERAND (t
, 0);
20011 if (TYPE_P (operand_0
))
20013 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20014 RETURN (get_typeid (operand_0
, complain
));
20018 operand_0
= RECUR (operand_0
);
20019 RETURN (build_typeid (operand_0
, complain
));
20030 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20031 /* ??? We're doing a subset of finish_id_expression here. */
20032 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20033 /* Replace an evaluated use of the thread_local variable with
20034 a call to its wrapper. */
20036 else if (outer_automatic_var_p (r
))
20037 r
= process_outer_var_ref (r
, complain
);
20039 if (!TYPE_REF_P (TREE_TYPE (t
)))
20040 /* If the original type was a reference, we'll be wrapped in
20041 the appropriate INDIRECT_REF. */
20042 r
= convert_from_reference (r
);
20048 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20049 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20050 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20053 case OFFSETOF_EXPR
:
20056 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20057 in_decl
, /*function_p=*/false,
20058 /*integral_constant_expression_p=*/false);
20059 RETURN (finish_offsetof (object_ptr
,
20060 RECUR (TREE_OPERAND (t
, 0)),
20061 EXPR_LOCATION (t
)));
20064 case ADDRESSOF_EXPR
:
20065 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20066 RECUR (TREE_OPERAND (t
, 0)), complain
));
20070 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20071 complain
, in_decl
);
20073 tree type2
= TRAIT_EXPR_TYPE2 (t
);
20074 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
20075 type2
= RECUR (type2
);
20077 type2
= tsubst (type2
, args
, complain
, in_decl
);
20079 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20080 TRAIT_EXPR_KIND (t
), type1
, type2
));
20085 tree old_stmt_expr
= cur_stmt_expr
;
20086 tree stmt_expr
= begin_stmt_expr ();
20088 cur_stmt_expr
= stmt_expr
;
20089 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20090 integral_constant_expression_p
);
20091 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20092 cur_stmt_expr
= old_stmt_expr
;
20094 /* If the resulting list of expression statement is empty,
20095 fold it further into void_node. */
20096 if (empty_expr_stmt_p (stmt_expr
))
20097 stmt_expr
= void_node
;
20099 RETURN (stmt_expr
);
20104 if (complain
& tf_partial
)
20106 /* We don't have a full set of template arguments yet; don't touch
20107 the lambda at all. */
20108 gcc_assert (processing_template_decl
);
20111 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20113 RETURN (build_lambda_object (r
));
20117 /* We can get here for a constant initializer of non-dependent type.
20118 FIXME stop folding in cp_parser_initializer_clause. */
20120 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20125 case TRANSACTION_EXPR
:
20126 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20127 integral_constant_expression_p
));
20130 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20132 case VEC_PERM_EXPR
:
20134 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20135 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20136 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20137 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20141 case REQUIRES_EXPR
:
20142 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
20145 /* No need to substitute further, a RANGE_EXPR will always be built
20146 with constant operands. */
20149 case NON_LVALUE_EXPR
:
20150 case VIEW_CONVERT_EXPR
:
20151 if (location_wrapper_p (t
))
20152 /* We need to do this here as well as in tsubst_copy so we get the
20153 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20154 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20155 EXPR_LOCATION (t
)));
20159 /* Handle Objective-C++ constructs, if appropriate. */
20162 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20163 in_decl
, /*function_p=*/false);
20167 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20173 input_location
= loc
;
20177 /* Verify that the instantiated ARGS are valid. For type arguments,
20178 make sure that the type's linkage is ok. For non-type arguments,
20179 make sure they are constants if they are integral or enumerations.
20180 Emit an error under control of COMPLAIN, and return TRUE on error. */
20183 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20185 if (dependent_template_arg_p (t
))
20187 if (ARGUMENT_PACK_P (t
))
20189 tree vec
= ARGUMENT_PACK_ARGS (t
);
20190 int len
= TREE_VEC_LENGTH (vec
);
20191 bool result
= false;
20194 for (i
= 0; i
< len
; ++i
)
20195 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20199 else if (TYPE_P (t
))
20201 /* [basic.link]: A name with no linkage (notably, the name
20202 of a class or enumeration declared in a local scope)
20203 shall not be used to declare an entity with linkage.
20204 This implies that names with no linkage cannot be used as
20207 DR 757 relaxes this restriction for C++0x. */
20208 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20209 : no_linkage_check (t
, /*relaxed_p=*/false));
20213 /* DR 488 makes use of a type with no linkage cause
20214 type deduction to fail. */
20215 if (complain
& tf_error
)
20217 if (TYPE_UNNAMED_P (nt
))
20218 error ("%qT is/uses unnamed type", t
);
20220 error ("template argument for %qD uses local type %qT",
20225 /* In order to avoid all sorts of complications, we do not
20226 allow variably-modified types as template arguments. */
20227 else if (variably_modified_type_p (t
, NULL_TREE
))
20229 if (complain
& tf_error
)
20230 error ("%qT is a variably modified type", t
);
20234 /* Class template and alias template arguments should be OK. */
20235 else if (DECL_TYPE_TEMPLATE_P (t
))
20237 /* A non-type argument of integral or enumerated type must be a
20239 else if (TREE_TYPE (t
)
20240 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20241 && !REFERENCE_REF_P (t
)
20242 && !TREE_CONSTANT (t
))
20244 if (complain
& tf_error
)
20245 error ("integral expression %qE is not constant", t
);
20252 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20254 int ix
, len
= DECL_NTPARMS (tmpl
);
20255 bool result
= false;
20257 for (ix
= 0; ix
!= len
; ix
++)
20259 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20262 if (result
&& (complain
& tf_error
))
20263 error (" trying to instantiate %qD", tmpl
);
20267 /* We're out of SFINAE context now, so generate diagnostics for the access
20268 errors we saw earlier when instantiating D from TMPL and ARGS. */
20271 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20273 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20274 tree type
= TREE_TYPE (pattern
);
20275 location_t loc
= input_location
;
20277 push_access_scope (d
);
20278 push_deferring_access_checks (dk_no_deferred
);
20279 input_location
= DECL_SOURCE_LOCATION (pattern
);
20280 tsubst (type
, args
, tf_warning_or_error
, d
);
20281 input_location
= loc
;
20282 pop_deferring_access_checks ();
20283 pop_access_scope (d
);
20286 /* Instantiate the indicated variable, function, or alias template TMPL with
20287 the template arguments in TARG_PTR. */
20290 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20292 tree targ_ptr
= orig_args
;
20296 bool access_ok
= true;
20298 if (tmpl
== error_mark_node
)
20299 return error_mark_node
;
20301 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20303 /* If this function is a clone, handle it specially. */
20304 if (DECL_CLONED_FUNCTION_P (tmpl
))
20309 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20310 DECL_CLONED_FUNCTION. */
20311 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20312 targ_ptr
, complain
);
20313 if (spec
== error_mark_node
)
20314 return error_mark_node
;
20316 /* Look for the clone. */
20317 FOR_EACH_CLONE (clone
, spec
)
20318 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20320 /* We should always have found the clone by now. */
20321 gcc_unreachable ();
20325 if (targ_ptr
== error_mark_node
)
20326 return error_mark_node
;
20328 /* Check to see if we already have this specialization. */
20329 gen_tmpl
= most_general_template (tmpl
);
20330 if (TMPL_ARGS_DEPTH (targ_ptr
)
20331 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20332 /* targ_ptr only has the innermost template args, so add the outer ones
20333 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20334 the case of a non-dependent call within a template definition). */
20335 targ_ptr
= (add_outermost_template_args
20336 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20339 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20340 but it doesn't seem to be on the hot path. */
20341 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20343 gcc_assert (tmpl
== gen_tmpl
20344 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
20346 || fndecl
== NULL_TREE
);
20348 if (spec
!= NULL_TREE
)
20350 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20352 if (complain
& tf_error
)
20353 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20354 return error_mark_node
;
20359 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20361 return error_mark_node
;
20363 /* We are building a FUNCTION_DECL, during which the access of its
20364 parameters and return types have to be checked. However this
20365 FUNCTION_DECL which is the desired context for access checking
20366 is not built yet. We solve this chicken-and-egg problem by
20367 deferring all checks until we have the FUNCTION_DECL. */
20368 push_deferring_access_checks (dk_deferred
);
20370 /* Instantiation of the function happens in the context of the function
20371 template, not the context of the overload resolution we're doing. */
20372 push_to_top_level ();
20373 /* If there are dependent arguments, e.g. because we're doing partial
20374 ordering, make sure processing_template_decl stays set. */
20375 if (uses_template_parms (targ_ptr
))
20376 ++processing_template_decl
;
20377 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20379 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20380 complain
, gen_tmpl
, true);
20381 push_nested_class (ctx
);
20384 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20386 fndecl
= NULL_TREE
;
20387 if (VAR_P (pattern
))
20389 /* We need to determine if we're using a partial or explicit
20390 specialization now, because the type of the variable could be
20392 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20393 tree elt
= most_specialized_partial_spec (tid
, complain
);
20394 if (elt
== error_mark_node
)
20395 pattern
= error_mark_node
;
20398 tree partial_tmpl
= TREE_VALUE (elt
);
20399 tree partial_args
= TREE_PURPOSE (elt
);
20400 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20401 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20405 /* Substitute template parameters to obtain the specialization. */
20406 if (fndecl
== NULL_TREE
)
20407 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20408 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20409 pop_nested_class ();
20410 pop_from_top_level ();
20412 if (fndecl
== error_mark_node
)
20414 pop_deferring_access_checks ();
20415 return error_mark_node
;
20418 /* The DECL_TI_TEMPLATE should always be the immediate parent
20419 template, not the most general template. */
20420 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20421 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20423 /* Now we know the specialization, compute access previously
20424 deferred. Do no access control for inheriting constructors,
20425 as we already checked access for the inherited constructor. */
20426 if (!(flag_new_inheriting_ctors
20427 && DECL_INHERITED_CTOR (fndecl
)))
20429 push_access_scope (fndecl
);
20430 if (!perform_deferred_access_checks (complain
))
20432 pop_access_scope (fndecl
);
20434 pop_deferring_access_checks ();
20436 /* If we've just instantiated the main entry point for a function,
20437 instantiate all the alternate entry points as well. We do this
20438 by cloning the instantiation of the main entry point, not by
20439 instantiating the template clones. */
20440 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20441 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20442 clone_function_decl (fndecl
, /*update_methods=*/false);
20446 if (!(complain
& tf_error
))
20448 /* Remember to reinstantiate when we're out of SFINAE so the user
20449 can see the errors. */
20450 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20452 return error_mark_node
;
20457 /* Wrapper for instantiate_template_1. */
20460 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20463 timevar_push (TV_TEMPLATE_INST
);
20464 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20465 timevar_pop (TV_TEMPLATE_INST
);
20469 /* Instantiate the alias template TMPL with ARGS. Also push a template
20470 instantiation level, which instantiate_template doesn't do because
20471 functions and variables have sufficient context established by the
20475 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20477 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20478 return error_mark_node
;
20479 if (!push_tinst_level (tmpl
, args
))
20480 return error_mark_node
;
20483 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20484 args
, tmpl
, complain
,
20485 /*require_all_args=*/true,
20486 /*use_default_args=*/true);
20488 tree r
= instantiate_template (tmpl
, args
, complain
);
20489 pop_tinst_level ();
20494 /* PARM is a template parameter pack for FN. Returns true iff
20495 PARM is used in a deducible way in the argument list of FN. */
20498 pack_deducible_p (tree parm
, tree fn
)
20500 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20501 for (; t
; t
= TREE_CHAIN (t
))
20503 tree type
= TREE_VALUE (t
);
20505 if (!PACK_EXPANSION_P (type
))
20507 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20508 packs
; packs
= TREE_CHAIN (packs
))
20509 if (template_args_equal (TREE_VALUE (packs
), parm
))
20511 /* The template parameter pack is used in a function parameter
20512 pack. If this is the end of the parameter list, the
20513 template parameter pack is deducible. */
20514 if (TREE_CHAIN (t
) == void_list_node
)
20517 /* Otherwise, not. Well, it could be deduced from
20518 a non-pack parameter, but doing so would end up with
20519 a deduction mismatch, so don't bother. */
20523 /* The template parameter pack isn't used in any function parameter
20524 packs, but it might be used deeper, e.g. tuple<Args...>. */
20528 /* Subroutine of fn_type_unification: check non-dependent parms for
20532 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20533 tree fn
, unification_kind_t strict
, int flags
,
20534 struct conversion
**convs
, bool explain_p
)
20536 /* Non-constructor methods need to leave a conversion for 'this', which
20537 isn't included in nargs here. */
20538 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20539 && !DECL_CONSTRUCTOR_P (fn
));
20541 for (unsigned ia
= 0;
20542 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20544 tree parm
= TREE_VALUE (parms
);
20546 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20547 && (!TREE_CHAIN (parms
)
20548 || TREE_CHAIN (parms
) == void_list_node
))
20549 /* For a function parameter pack that occurs at the end of the
20550 parameter-declaration-list, the type A of each remaining
20551 argument of the call is compared with the type P of the
20552 declarator-id of the function parameter pack. */
20555 parms
= TREE_CHAIN (parms
);
20557 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20558 /* For a function parameter pack that does not occur at the
20559 end of the parameter-declaration-list, the type of the
20560 parameter pack is a non-deduced context. */
20563 if (!uses_template_parms (parm
))
20565 tree arg
= args
[ia
];
20566 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20567 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20569 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20570 conv_p
, explain_p
))
20580 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20581 NARGS elements of the arguments that are being used when calling
20582 it. TARGS is a vector into which the deduced template arguments
20585 Returns either a FUNCTION_DECL for the matching specialization of FN or
20586 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20587 true, diagnostics will be printed to explain why it failed.
20589 If FN is a conversion operator, or we are trying to produce a specific
20590 specialization, RETURN_TYPE is the return type desired.
20592 The EXPLICIT_TARGS are explicit template arguments provided via a
20595 The parameter STRICT is one of:
20598 We are deducing arguments for a function call, as in
20599 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20600 deducing arguments for a call to the result of a conversion
20601 function template, as in [over.call.object].
20604 We are deducing arguments for a conversion function, as in
20605 [temp.deduct.conv].
20608 We are deducing arguments when doing an explicit instantiation
20609 as in [temp.explicit], when determining an explicit specialization
20610 as in [temp.expl.spec], or when taking the address of a function
20611 template, as in [temp.deduct.funcaddr]. */
20614 fn_type_unification (tree fn
,
20615 tree explicit_targs
,
20618 unsigned int nargs
,
20620 unification_kind_t strict
,
20622 struct conversion
**convs
,
20628 tree decl
= NULL_TREE
;
20629 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20631 static int deduction_depth
;
20632 /* type_unification_real will pass back any access checks from default
20633 template argument substitution. */
20634 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20635 /* We don't have all the template args yet. */
20636 bool incomplete
= true;
20639 if (flag_new_inheriting_ctors
)
20640 fn
= strip_inheriting_ctors (fn
);
20642 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20643 tree r
= error_mark_node
;
20645 tree full_targs
= targs
;
20646 if (TMPL_ARGS_DEPTH (targs
)
20647 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20648 full_targs
= (add_outermost_template_args
20649 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20653 complain
|= tf_decltype
;
20655 /* In C++0x, it's possible to have a function template whose type depends
20656 on itself recursively. This is most obvious with decltype, but can also
20657 occur with enumeration scope (c++/48969). So we need to catch infinite
20658 recursion and reject the substitution at deduction time; this function
20659 will return error_mark_node for any repeated substitution.
20661 This also catches excessive recursion such as when f<N> depends on
20662 f<N-1> across all integers, and returns error_mark_node for all the
20663 substitutions back up to the initial one.
20665 This is, of course, not reentrant. */
20666 if (excessive_deduction_depth
)
20667 return error_mark_node
;
20670 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20672 fntype
= TREE_TYPE (fn
);
20673 if (explicit_targs
)
20677 The specified template arguments must match the template
20678 parameters in kind (i.e., type, nontype, template), and there
20679 must not be more arguments than there are parameters;
20680 otherwise type deduction fails.
20682 Nontype arguments must match the types of the corresponding
20683 nontype template parameters, or must be convertible to the
20684 types of the corresponding nontype parameters as specified in
20685 _temp.arg.nontype_, otherwise type deduction fails.
20687 All references in the function type of the function template
20688 to the corresponding template parameters are replaced by the
20689 specified template argument values. If a substitution in a
20690 template parameter or in the function type of the function
20691 template results in an invalid type, type deduction fails. */
20692 int i
, len
= TREE_VEC_LENGTH (tparms
);
20693 location_t loc
= input_location
;
20694 incomplete
= false;
20696 if (explicit_targs
== error_mark_node
)
20699 if (TMPL_ARGS_DEPTH (explicit_targs
)
20700 < TMPL_ARGS_DEPTH (full_targs
))
20701 explicit_targs
= add_outermost_template_args (full_targs
,
20704 /* Adjust any explicit template arguments before entering the
20705 substitution context. */
20707 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20708 complain
|tf_partial
,
20709 /*require_all_args=*/false,
20710 /*use_default_args=*/false));
20711 if (explicit_targs
== error_mark_node
)
20714 /* Substitute the explicit args into the function type. This is
20715 necessary so that, for instance, explicitly declared function
20716 arguments can match null pointed constants. If we were given
20717 an incomplete set of explicit args, we must not do semantic
20718 processing during substitution as we could create partial
20720 for (i
= 0; i
< len
; i
++)
20722 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20723 bool parameter_pack
= false;
20724 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20726 /* Dig out the actual parm. */
20727 if (TREE_CODE (parm
) == TYPE_DECL
20728 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20730 parm
= TREE_TYPE (parm
);
20731 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20733 else if (TREE_CODE (parm
) == PARM_DECL
)
20735 parm
= DECL_INITIAL (parm
);
20736 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20739 if (targ
== NULL_TREE
)
20740 /* No explicit argument for this template parameter. */
20742 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20744 /* Mark the argument pack as "incomplete". We could
20745 still deduce more arguments during unification.
20746 We remove this mark in type_unification_real. */
20747 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20748 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20749 = ARGUMENT_PACK_ARGS (targ
);
20751 /* We have some incomplete argument packs. */
20758 if (!push_tinst_level (fn
, explicit_targs
))
20760 excessive_deduction_depth
= true;
20763 ++processing_template_decl
;
20764 input_location
= DECL_SOURCE_LOCATION (fn
);
20765 /* Ignore any access checks; we'll see them again in
20766 instantiate_template and they might have the wrong
20767 access path at this point. */
20768 push_deferring_access_checks (dk_deferred
);
20769 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20770 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20771 pop_deferring_access_checks ();
20772 input_location
= loc
;
20773 --processing_template_decl
;
20774 pop_tinst_level ();
20776 if (fntype
== error_mark_node
)
20780 /* Place the explicitly specified arguments in TARGS. */
20781 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20782 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20783 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20784 if (!incomplete
&& CHECKING_P
20785 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20786 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20787 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20790 if (return_type
&& strict
!= DEDUCE_CALL
)
20792 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20793 new_args
[0] = return_type
;
20794 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20802 /* Never do unification on the 'this' parameter. */
20803 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20805 if (return_type
&& strict
== DEDUCE_CALL
)
20807 /* We're deducing for a call to the result of a template conversion
20808 function. The parms we really want are in return_type. */
20809 if (INDIRECT_TYPE_P (return_type
))
20810 return_type
= TREE_TYPE (return_type
);
20811 parms
= TYPE_ARG_TYPES (return_type
);
20813 else if (return_type
)
20815 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20818 /* We allow incomplete unification without an error message here
20819 because the standard doesn't seem to explicitly prohibit it. Our
20820 callers must be ready to deal with unification failures in any
20823 /* If we aren't explaining yet, push tinst context so we can see where
20824 any errors (e.g. from class instantiations triggered by instantiation
20825 of default template arguments) come from. If we are explaining, this
20826 context is redundant. */
20827 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20829 excessive_deduction_depth
= true;
20833 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20834 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20835 strict
, &checks
, explain_p
);
20837 pop_tinst_level ();
20841 /* Now that we have bindings for all of the template arguments,
20842 ensure that the arguments deduced for the template template
20843 parameters have compatible template parameter lists. We cannot
20844 check this property before we have deduced all template
20845 arguments, because the template parameter types of a template
20846 template parameter might depend on prior template parameters
20847 deduced after the template template parameter. The following
20848 ill-formed example illustrates this issue:
20850 template<typename T, template<T> class C> void f(C<5>, T);
20852 template<int N> struct X {};
20855 f(X<5>(), 5l); // error: template argument deduction fails
20858 The template parameter list of 'C' depends on the template type
20859 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20860 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20861 time that we deduce 'C'. */
20862 if (!template_template_parm_bindings_ok_p
20863 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20865 unify_inconsistent_template_template_parameters (explain_p
);
20869 /* DR 1391: All parameters have args, now check non-dependent parms for
20871 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20876 /* All is well so far. Now, check:
20880 When all template arguments have been deduced, all uses of
20881 template parameters in nondeduced contexts are replaced with
20882 the corresponding deduced argument values. If the
20883 substitution results in an invalid type, as described above,
20884 type deduction fails. */
20885 if (!push_tinst_level (fn
, targs
))
20887 excessive_deduction_depth
= true;
20891 /* Also collect access checks from the instantiation. */
20892 reopen_deferring_access_checks (checks
);
20894 decl
= instantiate_template (fn
, targs
, complain
);
20896 checks
= get_deferred_access_checks ();
20897 pop_deferring_access_checks ();
20899 pop_tinst_level ();
20901 if (decl
== error_mark_node
)
20904 /* Now perform any access checks encountered during substitution. */
20905 push_access_scope (decl
);
20906 ok
= perform_access_checks (checks
, complain
);
20907 pop_access_scope (decl
);
20911 /* If we're looking for an exact match, check that what we got
20912 is indeed an exact match. It might not be if some template
20913 parameters are used in non-deduced contexts. But don't check
20914 for an exact match if we have dependent template arguments;
20915 in that case we're doing partial ordering, and we already know
20916 that we have two candidates that will provide the actual type. */
20917 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20919 tree substed
= TREE_TYPE (decl
);
20923 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
20925 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
20926 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
20927 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
20929 unify_type_mismatch (explain_p
, args
[i
],
20930 TREE_VALUE (sarg
));
20935 /* After doing deduction with the inherited constructor, actually return an
20936 instantiation of the inheriting constructor. */
20938 decl
= instantiate_template (orig_fn
, targs
, complain
);
20944 if (excessive_deduction_depth
)
20946 if (deduction_depth
== 0)
20947 /* Reset once we're all the way out. */
20948 excessive_deduction_depth
= false;
20954 /* Adjust types before performing type deduction, as described in
20955 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20956 sections are symmetric. PARM is the type of a function parameter
20957 or the return type of the conversion function. ARG is the type of
20958 the argument passed to the call, or the type of the value
20959 initialized with the result of the conversion function.
20960 ARG_EXPR is the original argument expression, which may be null. */
20963 maybe_adjust_types_for_deduction (unification_kind_t strict
,
20976 /* Swap PARM and ARG throughout the remainder of this
20977 function; the handling is precisely symmetric since PARM
20978 will initialize ARG rather than vice versa. */
20979 std::swap (parm
, arg
);
20983 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20984 too, but here handle it by stripping the reference from PARM
20985 rather than by adding it to ARG. */
20986 if (TYPE_REF_P (*parm
)
20987 && TYPE_REF_IS_RVALUE (*parm
)
20988 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20989 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20990 && TYPE_REF_P (*arg
)
20991 && !TYPE_REF_IS_RVALUE (*arg
))
20992 *parm
= TREE_TYPE (*parm
);
20993 /* Nothing else to do in this case. */
20997 gcc_unreachable ();
21000 if (!TYPE_REF_P (*parm
))
21002 /* [temp.deduct.call]
21004 If P is not a reference type:
21006 --If A is an array type, the pointer type produced by the
21007 array-to-pointer standard conversion (_conv.array_) is
21008 used in place of A for type deduction; otherwise,
21010 --If A is a function type, the pointer type produced by
21011 the function-to-pointer standard conversion
21012 (_conv.func_) is used in place of A for type deduction;
21015 --If A is a cv-qualified type, the top level
21016 cv-qualifiers of A's type are ignored for type
21018 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21019 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21020 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21021 *arg
= build_pointer_type (*arg
);
21023 *arg
= TYPE_MAIN_VARIANT (*arg
);
21026 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21027 reference to a cv-unqualified template parameter that does not represent a
21028 template parameter of a class template (during class template argument
21029 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21030 an lvalue, the type "lvalue reference to A" is used in place of A for type
21032 if (TYPE_REF_P (*parm
)
21033 && TYPE_REF_IS_RVALUE (*parm
)
21034 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21035 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21036 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21037 && (arg_expr
? lvalue_p (arg_expr
)
21038 /* try_one_overload doesn't provide an arg_expr, but
21039 functions are always lvalues. */
21040 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21041 *arg
= build_reference_type (*arg
);
21043 /* [temp.deduct.call]
21045 If P is a cv-qualified type, the top level cv-qualifiers
21046 of P's type are ignored for type deduction. If P is a
21047 reference type, the type referred to by P is used for
21049 *parm
= TYPE_MAIN_VARIANT (*parm
);
21050 if (TYPE_REF_P (*parm
))
21052 *parm
= TREE_TYPE (*parm
);
21053 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21056 /* DR 322. For conversion deduction, remove a reference type on parm
21057 too (which has been swapped into ARG). */
21058 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21059 *arg
= TREE_TYPE (*arg
);
21064 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21065 template which doesn't contain any deducible template parameters; check if
21066 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21067 unify_one_argument. */
21070 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21071 int flags
, struct conversion
**conv_p
,
21077 type
= TREE_TYPE (arg
);
21081 if (same_type_p (parm
, type
))
21082 return unify_success (explain_p
);
21084 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21085 if (strict
== DEDUCE_CONV
)
21087 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21088 return unify_success (explain_p
);
21090 else if (strict
!= DEDUCE_EXACT
)
21093 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21095 /* Avoid recalculating this in add_function_candidate. */
21097 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21099 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21101 return unify_success (explain_p
);
21104 if (strict
== DEDUCE_EXACT
)
21105 return unify_type_mismatch (explain_p
, parm
, arg
);
21107 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21110 static bool uses_deducible_template_parms (tree type
);
21112 /* Returns true iff the expression EXPR is one from which a template
21113 argument can be deduced. In other words, if it's an undecorated
21114 use of a template non-type parameter. */
21117 deducible_expression (tree expr
)
21119 /* Strip implicit conversions. */
21120 while (CONVERT_EXPR_P (expr
))
21121 expr
= TREE_OPERAND (expr
, 0);
21122 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21125 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21126 deducible way; that is, if it has a max value of <PARM> - 1. */
21129 deducible_array_bound (tree domain
)
21131 if (domain
== NULL_TREE
)
21134 tree max
= TYPE_MAX_VALUE (domain
);
21135 if (TREE_CODE (max
) != MINUS_EXPR
)
21138 return deducible_expression (TREE_OPERAND (max
, 0));
21141 /* Returns true iff the template arguments ARGS use a template parameter
21142 in a deducible way. */
21145 deducible_template_args (tree args
)
21147 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21150 tree elt
= TREE_VEC_ELT (args
, i
);
21151 if (ARGUMENT_PACK_P (elt
))
21152 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21155 if (PACK_EXPANSION_P (elt
))
21156 elt
= PACK_EXPANSION_PATTERN (elt
);
21157 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21159 else if (TYPE_P (elt
))
21160 deducible
= uses_deducible_template_parms (elt
);
21162 deducible
= deducible_expression (elt
);
21170 /* Returns true iff TYPE contains any deducible references to template
21171 parameters, as per 14.8.2.5. */
21174 uses_deducible_template_parms (tree type
)
21176 if (PACK_EXPANSION_P (type
))
21177 type
= PACK_EXPANSION_PATTERN (type
);
21184 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21185 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21191 if (INDIRECT_TYPE_P (type
))
21192 return uses_deducible_template_parms (TREE_TYPE (type
));
21194 /* T[integer-constant ]
21196 if (TREE_CODE (type
) == ARRAY_TYPE
)
21197 return (uses_deducible_template_parms (TREE_TYPE (type
))
21198 || deducible_array_bound (TYPE_DOMAIN (type
)));
21210 if (TYPE_PTRMEM_P (type
))
21211 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21212 || (uses_deducible_template_parms
21213 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21215 /* template-name <T> (where template-name refers to a class template)
21216 template-name <i> (where template-name refers to a class template) */
21217 if (CLASS_TYPE_P (type
)
21218 && CLASSTYPE_TEMPLATE_INFO (type
)
21219 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21220 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21221 (CLASSTYPE_TI_ARGS (type
)));
21226 if (FUNC_OR_METHOD_TYPE_P (type
))
21228 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21230 tree parm
= TYPE_ARG_TYPES (type
);
21231 if (TREE_CODE (type
) == METHOD_TYPE
)
21232 parm
= TREE_CHAIN (parm
);
21233 for (; parm
; parm
= TREE_CHAIN (parm
))
21234 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21241 /* Subroutine of type_unification_real and unify_pack_expansion to
21242 handle unification of a single P/A pair. Parameters are as
21243 for those functions. */
21246 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21247 int subr
, unification_kind_t strict
,
21250 tree arg_expr
= NULL_TREE
;
21253 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21254 return unify_invalid (explain_p
);
21255 if (arg
== unknown_type_node
)
21256 /* We can't deduce anything from this, but we might get all the
21257 template args from other function args. */
21258 return unify_success (explain_p
);
21260 /* Implicit conversions (Clause 4) will be performed on a function
21261 argument to convert it to the type of the corresponding function
21262 parameter if the parameter type contains no template-parameters that
21263 participate in template argument deduction. */
21264 if (strict
!= DEDUCE_EXACT
21265 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21266 /* For function parameters with no deducible template parameters,
21267 just return. We'll check non-dependent conversions later. */
21268 return unify_success (explain_p
);
21273 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21274 | UNIFY_ALLOW_MORE_CV_QUAL
21275 | UNIFY_ALLOW_DERIVED
);
21279 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21283 arg_strict
= UNIFY_ALLOW_NONE
;
21287 gcc_unreachable ();
21290 /* We only do these transformations if this is the top-level
21291 parameter_type_list in a call or declaration matching; in other
21292 situations (nested function declarators, template argument lists) we
21293 won't be comparing a type to an expression, and we don't do any type
21299 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21300 if (type_unknown_p (arg
))
21302 /* [temp.deduct.type] A template-argument can be
21303 deduced from a pointer to function or pointer
21304 to member function argument if the set of
21305 overloaded functions does not contain function
21306 templates and at most one of a set of
21307 overloaded functions provides a unique
21309 resolve_overloaded_unification (tparms
, targs
, parm
,
21311 arg_strict
, explain_p
);
21312 /* If a unique match was not found, this is a
21313 non-deduced context, so we still succeed. */
21314 return unify_success (explain_p
);
21318 arg
= unlowered_expr_type (arg
);
21319 if (arg
== error_mark_node
)
21320 return unify_invalid (explain_p
);
21324 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21327 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21328 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21329 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21331 /* For deduction from an init-list we need the actual list. */
21332 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21334 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21337 /* for_each_template_parm callback that always returns 0. */
21340 zero_r (tree
, void *)
21345 /* for_each_template_parm any_fn callback to handle deduction of a template
21346 type argument from the type of an array bound. */
21349 array_deduction_r (tree t
, void *data
)
21351 tree_pair_p d
= (tree_pair_p
)data
;
21352 tree
&tparms
= d
->purpose
;
21353 tree
&targs
= d
->value
;
21355 if (TREE_CODE (t
) == ARRAY_TYPE
)
21356 if (tree dom
= TYPE_DOMAIN (t
))
21357 if (tree max
= TYPE_MAX_VALUE (dom
))
21359 if (TREE_CODE (max
) == MINUS_EXPR
)
21360 max
= TREE_OPERAND (max
, 0);
21361 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21362 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21363 UNIFY_ALLOW_NONE
, /*explain*/false);
21366 /* Keep walking. */
21370 /* Try to deduce any not-yet-deduced template type arguments from the type of
21371 an array bound. This is handled separately from unify because 14.8.2.5 says
21372 "The type of a type parameter is only deduced from an array bound if it is
21373 not otherwise deduced." */
21376 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21378 tree_pair_s data
= { tparms
, targs
};
21379 hash_set
<tree
> visited
;
21380 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21381 /*nondeduced*/false, array_deduction_r
);
21384 /* Most parms like fn_type_unification.
21386 If SUBR is 1, we're being called recursively (to unify the
21387 arguments of a function or method parameter of a function
21390 CHECKS is a pointer to a vector of access checks encountered while
21391 substituting default template arguments. */
21394 type_unification_real (tree tparms
,
21398 unsigned int xnargs
,
21400 unification_kind_t strict
,
21401 vec
<deferred_access_check
, va_gc
> **checks
,
21406 int ntparms
= TREE_VEC_LENGTH (tparms
);
21407 int saw_undeduced
= 0;
21410 unsigned int nargs
;
21413 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21414 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21415 gcc_assert (ntparms
> 0);
21417 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21419 /* Reset the number of non-defaulted template arguments contained
21421 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21429 while (parms
&& parms
!= void_list_node
21432 parm
= TREE_VALUE (parms
);
21434 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21435 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21436 /* For a function parameter pack that occurs at the end of the
21437 parameter-declaration-list, the type A of each remaining
21438 argument of the call is compared with the type P of the
21439 declarator-id of the function parameter pack. */
21442 parms
= TREE_CHAIN (parms
);
21444 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21445 /* For a function parameter pack that does not occur at the
21446 end of the parameter-declaration-list, the type of the
21447 parameter pack is a non-deduced context. */
21453 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21459 && parms
!= void_list_node
21460 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21462 /* Unify the remaining arguments with the pack expansion type. */
21464 tree parmvec
= make_tree_vec (1);
21466 /* Allocate a TREE_VEC and copy in all of the arguments */
21467 argvec
= make_tree_vec (nargs
- ia
);
21468 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21469 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21471 /* Copy the parameter into parmvec. */
21472 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21473 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21474 /*subr=*/subr
, explain_p
))
21477 /* Advance to the end of the list of parameters. */
21478 parms
= TREE_CHAIN (parms
);
21481 /* Fail if we've reached the end of the parm list, and more args
21482 are present, and the parm list isn't variadic. */
21483 if (ia
< nargs
&& parms
== void_list_node
)
21484 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21485 /* Fail if parms are left and they don't have default values and
21486 they aren't all deduced as empty packs (c++/57397). This is
21487 consistent with sufficient_parms_p. */
21488 if (parms
&& parms
!= void_list_node
21489 && TREE_PURPOSE (parms
) == NULL_TREE
)
21491 unsigned int count
= nargs
;
21496 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21499 p
= TREE_CHAIN (p
);
21501 while (p
&& p
!= void_list_node
);
21502 if (count
!= nargs
)
21503 return unify_too_few_arguments (explain_p
, ia
, count
,
21509 tsubst_flags_t complain
= (explain_p
21510 ? tf_warning_or_error
21512 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21514 for (i
= 0; i
< ntparms
; i
++)
21516 tree targ
= TREE_VEC_ELT (targs
, i
);
21517 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21519 /* Clear the "incomplete" flags on all argument packs now so that
21520 substituting them into later default arguments works. */
21521 if (targ
&& ARGUMENT_PACK_P (targ
))
21523 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21524 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21527 if (targ
|| tparm
== error_mark_node
)
21529 tparm
= TREE_VALUE (tparm
);
21531 if (TREE_CODE (tparm
) == TYPE_DECL
21532 && !tried_array_deduction
)
21534 try_array_deduction (tparms
, targs
, xparms
);
21535 tried_array_deduction
= true;
21536 if (TREE_VEC_ELT (targs
, i
))
21540 /* If this is an undeduced nontype parameter that depends on
21541 a type parameter, try another pass; its type may have been
21542 deduced from a later argument than the one from which
21543 this parameter can be deduced. */
21544 if (TREE_CODE (tparm
) == PARM_DECL
21545 && uses_template_parms (TREE_TYPE (tparm
))
21546 && saw_undeduced
< 2)
21552 /* Core issue #226 (C++0x) [temp.deduct]:
21554 If a template argument has not been deduced, its
21555 default template argument, if any, is used.
21557 When we are in C++98 mode, TREE_PURPOSE will either
21558 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21559 to explicitly check cxx_dialect here. */
21560 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21561 /* OK, there is a default argument. Wait until after the
21562 conversion check to do substitution. */
21565 /* If the type parameter is a parameter pack, then it will
21566 be deduced to an empty parameter pack. */
21567 if (template_parameter_pack_p (tparm
))
21571 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21573 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21574 TREE_CONSTANT (arg
) = 1;
21577 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21579 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21581 TREE_VEC_ELT (targs
, i
) = arg
;
21585 return unify_parameter_deduction_failure (explain_p
, tparm
);
21588 /* Now substitute into the default template arguments. */
21589 for (i
= 0; i
< ntparms
; i
++)
21591 tree targ
= TREE_VEC_ELT (targs
, i
);
21592 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21594 if (targ
|| tparm
== error_mark_node
)
21596 tree parm
= TREE_VALUE (tparm
);
21597 tree arg
= TREE_PURPOSE (tparm
);
21598 reopen_deferring_access_checks (*checks
);
21599 location_t save_loc
= input_location
;
21601 input_location
= DECL_SOURCE_LOCATION (parm
);
21603 if (saw_undeduced
== 1
21604 && TREE_CODE (parm
) == PARM_DECL
21605 && uses_template_parms (TREE_TYPE (parm
)))
21607 /* The type of this non-type parameter depends on undeduced
21608 parameters. Don't try to use its default argument yet,
21609 since we might deduce an argument for it on the next pass,
21610 but do check whether the arguments we already have cause
21611 substitution failure, so that that happens before we try
21612 later default arguments (78489). */
21613 ++processing_template_decl
;
21614 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21616 --processing_template_decl
;
21617 if (type
== error_mark_node
)
21618 arg
= error_mark_node
;
21624 /* Even if the call is happening in template context, getting
21625 here means it's non-dependent, and a default argument is
21626 considered a separate definition under [temp.decls], so we can
21627 do this substitution without processing_template_decl. This
21628 is important if the default argument contains something that
21629 might be instantiation-dependent like access (87480). */
21630 processing_template_decl_sentinel s
;
21631 tree substed
= NULL_TREE
;
21632 if (saw_undeduced
== 1)
21634 /* First instatiate in template context, in case we still
21635 depend on undeduced template parameters. */
21636 ++processing_template_decl
;
21637 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21639 --processing_template_decl
;
21640 if (substed
!= error_mark_node
21641 && !uses_template_parms (substed
))
21642 /* We replaced all the tparms, substitute again out of
21643 template context. */
21644 substed
= NULL_TREE
;
21647 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21650 if (!uses_template_parms (substed
))
21651 arg
= convert_template_argument (parm
, substed
, full_targs
,
21652 complain
, i
, NULL_TREE
);
21653 else if (saw_undeduced
== 1)
21656 arg
= error_mark_node
;
21659 input_location
= save_loc
;
21660 *checks
= get_deferred_access_checks ();
21661 pop_deferring_access_checks ();
21663 if (arg
== error_mark_node
)
21667 TREE_VEC_ELT (targs
, i
) = arg
;
21668 /* The position of the first default template argument,
21669 is also the number of non-defaulted arguments in TARGS.
21671 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21672 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21676 if (saw_undeduced
++ == 1)
21680 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21681 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21683 return unify_success (explain_p
);
21686 /* Subroutine of type_unification_real. Args are like the variables
21687 at the call site. ARG is an overloaded function (or template-id);
21688 we try deducing template args from each of the overloads, and if
21689 only one succeeds, we go with that. Modifies TARGS and returns
21690 true on success. */
21693 resolve_overloaded_unification (tree tparms
,
21697 unification_kind_t strict
,
21701 tree tempargs
= copy_node (targs
);
21703 tree goodfn
= NULL_TREE
;
21706 if (TREE_CODE (arg
) == ADDR_EXPR
)
21708 arg
= TREE_OPERAND (arg
, 0);
21714 if (TREE_CODE (arg
) == COMPONENT_REF
)
21715 /* Handle `&x' where `x' is some static or non-static member
21717 arg
= TREE_OPERAND (arg
, 1);
21719 if (TREE_CODE (arg
) == OFFSET_REF
)
21720 arg
= TREE_OPERAND (arg
, 1);
21722 /* Strip baselink information. */
21723 if (BASELINK_P (arg
))
21724 arg
= BASELINK_FUNCTIONS (arg
);
21726 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21728 /* If we got some explicit template args, we need to plug them into
21729 the affected templates before we try to unify, in case the
21730 explicit args will completely resolve the templates in question. */
21733 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21734 arg
= TREE_OPERAND (arg
, 0);
21736 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21739 tree subargs
, elem
;
21741 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21744 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21745 expl_subargs
, NULL_TREE
, tf_none
,
21746 /*require_all_args=*/true,
21747 /*use_default_args=*/true);
21748 if (subargs
!= error_mark_node
21749 && !any_dependent_template_arguments_p (subargs
))
21751 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21752 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21753 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21754 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21763 /* If no templates (or more than one) are fully resolved by the
21764 explicit arguments, this template-id is a non-deduced context; it
21765 could still be OK if we deduce all template arguments for the
21766 enclosing call through other arguments. */
21770 else if (!OVL_P (arg
))
21771 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21772 -- but the deduction does not succeed because the expression is
21773 not just the function on its own. */
21776 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21779 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21780 strict
, sub_strict
, addr_p
, explain_p
)
21781 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21788 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21789 to function or pointer to member function argument if the set of
21790 overloaded functions does not contain function templates and at most
21791 one of a set of overloaded functions provides a unique match.
21793 So if we found multiple possibilities, we return success but don't
21794 deduce anything. */
21798 int i
= TREE_VEC_LENGTH (targs
);
21800 if (TREE_VEC_ELT (tempargs
, i
))
21802 tree old
= TREE_VEC_ELT (targs
, i
);
21803 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21804 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21805 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21806 /* Don't forget explicit template arguments in a pack. */
21807 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21808 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21809 TREE_VEC_ELT (targs
, i
) = new_
;
21818 /* Core DR 115: In contexts where deduction is done and fails, or in
21819 contexts where deduction is not done, if a template argument list is
21820 specified and it, along with any default template arguments, identifies
21821 a single function template specialization, then the template-id is an
21822 lvalue for the function template specialization. */
21825 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21827 tree expr
, offset
, baselink
;
21830 if (!type_unknown_p (orig_expr
))
21835 offset
= NULL_TREE
;
21836 baselink
= NULL_TREE
;
21838 if (TREE_CODE (expr
) == ADDR_EXPR
)
21840 expr
= TREE_OPERAND (expr
, 0);
21843 if (TREE_CODE (expr
) == OFFSET_REF
)
21846 expr
= TREE_OPERAND (expr
, 1);
21848 if (BASELINK_P (expr
))
21851 expr
= BASELINK_FUNCTIONS (expr
);
21854 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21857 tree goodfn
= NULL_TREE
;
21859 /* If we got some explicit template args, we need to plug them into
21860 the affected templates before we try to unify, in case the
21861 explicit args will completely resolve the templates in question. */
21863 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21864 tree arg
= TREE_OPERAND (expr
, 0);
21865 tree badfn
= NULL_TREE
;
21866 tree badargs
= NULL_TREE
;
21868 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21871 tree subargs
, elem
;
21873 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21876 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21877 expl_subargs
, NULL_TREE
, tf_none
,
21878 /*require_all_args=*/true,
21879 /*use_default_args=*/true);
21880 if (subargs
!= error_mark_node
21881 && !any_dependent_template_arguments_p (subargs
))
21883 elem
= instantiate_template (fn
, subargs
, tf_none
);
21884 if (elem
== error_mark_node
)
21889 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21898 mark_used (goodfn
);
21901 expr
= build_baselink (BASELINK_BINFO (baselink
),
21902 BASELINK_ACCESS_BINFO (baselink
),
21903 expr
, BASELINK_OPTYPE (baselink
));
21907 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21908 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21911 expr
= cp_build_addr_expr (expr
, complain
);
21914 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21915 /* There were no good options and at least one bad one, so let the
21916 user know what the problem is. */
21917 instantiate_template (badfn
, badargs
, complain
);
21922 /* As above, but error out if the expression remains overloaded. */
21925 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
21927 exp
= resolve_nondeduced_context (exp
, complain
);
21928 if (type_unknown_p (exp
))
21930 if (complain
& tf_error
)
21931 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
21932 return error_mark_node
;
21937 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21938 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21939 different overloads deduce different arguments for a given parm.
21940 ADDR_P is true if the expression for which deduction is being
21941 performed was of the form "& fn" rather than simply "fn".
21943 Returns 1 on success. */
21946 try_one_overload (tree tparms
,
21951 unification_kind_t strict
,
21960 if (arg
== error_mark_node
)
21963 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21964 to function or pointer to member function argument if the set of
21965 overloaded functions does not contain function templates and at most
21966 one of a set of overloaded functions provides a unique match.
21968 So if this is a template, just return success. */
21970 if (uses_template_parms (arg
))
21973 if (TREE_CODE (arg
) == METHOD_TYPE
)
21974 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
21976 arg
= build_pointer_type (arg
);
21978 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
21980 /* We don't copy orig_targs for this because if we have already deduced
21981 some template args from previous args, unify would complain when we
21982 try to deduce a template parameter for the same argument, even though
21983 there isn't really a conflict. */
21984 nargs
= TREE_VEC_LENGTH (targs
);
21985 tempargs
= make_tree_vec (nargs
);
21987 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
21990 /* First make sure we didn't deduce anything that conflicts with
21991 explicitly specified args. */
21992 for (i
= nargs
; i
--; )
21994 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21995 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
21999 else if (uses_template_parms (elt
))
22000 /* Since we're unifying against ourselves, we will fill in
22001 template args used in the function parm list with our own
22002 template parms. Discard them. */
22003 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22004 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22006 /* Check that the argument at each index of the deduced argument pack
22007 is equivalent to the corresponding explicitly specified argument.
22008 We may have deduced more arguments than were explicitly specified,
22011 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22012 that's wrong if we deduce the same argument pack from multiple
22013 function arguments: it's only incomplete the first time. */
22015 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22016 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22018 if (TREE_VEC_LENGTH (deduced_pack
)
22019 < TREE_VEC_LENGTH (explicit_pack
))
22022 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22023 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22024 TREE_VEC_ELT (deduced_pack
, j
)))
22027 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22031 for (i
= nargs
; i
--; )
22033 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22036 TREE_VEC_ELT (targs
, i
) = elt
;
22042 /* PARM is a template class (perhaps with unbound template
22043 parameters). ARG is a fully instantiated type. If ARG can be
22044 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22045 TARGS are as for unify. */
22048 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22051 tree copy_of_targs
;
22053 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22055 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22056 /* Matches anything. */;
22057 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22058 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22061 /* We need to make a new template argument vector for the call to
22062 unify. If we used TARGS, we'd clutter it up with the result of
22063 the attempted unification, even if this class didn't work out.
22064 We also don't want to commit ourselves to all the unifications
22065 we've already done, since unification is supposed to be done on
22066 an argument-by-argument basis. In other words, consider the
22067 following pathological case:
22069 template <int I, int J, int K>
22072 template <int I, int J>
22073 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22075 template <int I, int J, int K>
22076 void f(S<I, J, K>, S<I, I, I>);
22085 Now, by the time we consider the unification involving `s2', we
22086 already know that we must have `f<0, 0, 0>'. But, even though
22087 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22088 because there are two ways to unify base classes of S<0, 1, 2>
22089 with S<I, I, I>. If we kept the already deduced knowledge, we
22090 would reject the possibility I=1. */
22091 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22093 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22095 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22100 /* If unification failed, we're done. */
22101 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22102 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22108 /* Given a template type PARM and a class type ARG, find the unique
22109 base type in ARG that is an instance of PARM. We do not examine
22110 ARG itself; only its base-classes. If there is not exactly one
22111 appropriate base class, return NULL_TREE. PARM may be the type of
22112 a partial specialization, as well as a plain template type. Used
22115 static enum template_base_result
22116 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22117 bool explain_p
, tree
*result
)
22119 tree rval
= NULL_TREE
;
22122 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22124 binfo
= TYPE_BINFO (complete_type (arg
));
22127 /* The type could not be completed. */
22128 *result
= NULL_TREE
;
22129 return tbr_incomplete_type
;
22132 /* Walk in inheritance graph order. The search order is not
22133 important, and this avoids multiple walks of virtual bases. */
22134 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22136 tree r
= try_class_unification (tparms
, targs
, parm
,
22137 BINFO_TYPE (binfo
), explain_p
);
22141 /* If there is more than one satisfactory baseclass, then:
22145 If they yield more than one possible deduced A, the type
22149 if (rval
&& !same_type_p (r
, rval
))
22151 *result
= NULL_TREE
;
22152 return tbr_ambiguous_baseclass
;
22160 return tbr_success
;
22163 /* Returns the level of DECL, which declares a template parameter. */
22166 template_decl_level (tree decl
)
22168 switch (TREE_CODE (decl
))
22171 case TEMPLATE_DECL
:
22172 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22175 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22178 gcc_unreachable ();
22183 /* Decide whether ARG can be unified with PARM, considering only the
22184 cv-qualifiers of each type, given STRICT as documented for unify.
22185 Returns nonzero iff the unification is OK on that basis. */
22188 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22190 int arg_quals
= cp_type_quals (arg
);
22191 int parm_quals
= cp_type_quals (parm
);
22193 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22194 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22196 /* Although a CVR qualifier is ignored when being applied to a
22197 substituted template parameter ([8.3.2]/1 for example), that
22198 does not allow us to unify "const T" with "int&" because both
22199 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22200 It is ok when we're allowing additional CV qualifiers
22201 at the outer level [14.8.2.1]/3,1st bullet. */
22202 if ((TYPE_REF_P (arg
)
22203 || FUNC_OR_METHOD_TYPE_P (arg
))
22204 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22207 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22208 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22212 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22213 && (arg_quals
& parm_quals
) != parm_quals
)
22216 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22217 && (parm_quals
& arg_quals
) != arg_quals
)
22223 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22225 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22227 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22228 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22229 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22231 *index
= TEMPLATE_TYPE_IDX (parm
);
22232 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22236 *index
= TEMPLATE_PARM_IDX (parm
);
22237 *level
= TEMPLATE_PARM_LEVEL (parm
);
22241 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22243 if (unify (TP, TA, P, A, S, EP)) \
22247 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22248 expansion at the end of PACKED_PARMS. Returns 0 if the type
22249 deduction succeeds, 1 otherwise. STRICT is the same as in
22250 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22251 function call argument list. We'll need to adjust the arguments to make them
22252 types. SUBR tells us if this is from a recursive call to
22253 type_unification_real, or for comparing two template argument
22257 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22258 tree packed_args
, unification_kind_t strict
,
22259 bool subr
, bool explain_p
)
22262 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22263 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22264 tree pack
, packs
= NULL_TREE
;
22265 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22267 /* Add in any args remembered from an earlier partial instantiation. */
22268 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22269 int levels
= TMPL_ARGS_DEPTH (targs
);
22271 packed_args
= expand_template_argument_pack (packed_args
);
22273 int len
= TREE_VEC_LENGTH (packed_args
);
22275 /* Determine the parameter packs we will be deducing from the
22276 pattern, and record their current deductions. */
22277 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22278 pack
; pack
= TREE_CHAIN (pack
))
22280 tree parm_pack
= TREE_VALUE (pack
);
22283 /* Only template parameter packs can be deduced, not e.g. function
22284 parameter packs or __bases or __integer_pack. */
22285 if (!TEMPLATE_PARM_P (parm_pack
))
22288 /* Determine the index and level of this parameter pack. */
22289 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22290 if (level
< levels
)
22293 /* Keep track of the parameter packs and their corresponding
22295 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22296 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22299 /* Loop through all of the arguments that have not yet been
22300 unified and unify each with the pattern. */
22301 for (i
= start
; i
< len
; i
++)
22304 bool any_explicit
= false;
22305 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22307 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22308 or the element of its argument pack at the current index if
22309 this argument was explicitly specified. */
22310 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22314 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22317 if (TREE_VALUE (pack
)
22318 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22319 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22321 any_explicit
= true;
22322 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22324 TMPL_ARG (targs
, level
, idx
) = arg
;
22327 /* If we had explicit template arguments, substitute them into the
22328 pattern before deduction. */
22331 /* Some arguments might still be unspecified or dependent. */
22333 ++processing_template_decl
;
22334 dependent
= any_dependent_template_arguments_p (targs
);
22336 --processing_template_decl
;
22337 parm
= tsubst (pattern
, targs
,
22338 explain_p
? tf_warning_or_error
: tf_none
,
22341 --processing_template_decl
;
22342 if (parm
== error_mark_node
)
22348 /* Unify the pattern with the current argument. */
22349 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22353 /* For each parameter pack, collect the deduced value. */
22354 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22357 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22359 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22360 TMPL_ARG (targs
, level
, idx
);
22364 /* Verify that the results of unification with the parameter packs
22365 produce results consistent with what we've seen before, and make
22366 the deduced argument packs available. */
22367 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22369 tree old_pack
= TREE_VALUE (pack
);
22370 tree new_args
= TREE_TYPE (pack
);
22371 int i
, len
= TREE_VEC_LENGTH (new_args
);
22373 bool nondeduced_p
= false;
22375 /* By default keep the original deduced argument pack.
22376 If necessary, more specific code is going to update the
22377 resulting deduced argument later down in this function. */
22378 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22379 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22381 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22382 actually deduce anything. */
22383 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22384 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22385 nondeduced_p
= true;
22389 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22391 /* If we had fewer function args than explicit template args,
22392 just use the explicits. */
22393 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22394 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22395 if (len
< explicit_len
)
22396 new_args
= explicit_args
;
22402 /* Build the deduced *_ARGUMENT_PACK. */
22403 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22405 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22406 TREE_CONSTANT (result
) = 1;
22409 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22411 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22413 /* Note the deduced argument packs for this parameter
22415 TMPL_ARG (targs
, level
, idx
) = result
;
22417 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22418 && (ARGUMENT_PACK_ARGS (old_pack
)
22419 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22421 /* We only had the explicitly-provided arguments before, but
22422 now we have a complete set of arguments. */
22423 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22425 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22426 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22427 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22431 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22432 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22434 if (!comp_template_args (old_args
, new_args
,
22435 &bad_old_arg
, &bad_new_arg
))
22436 /* Inconsistent unification of this parameter pack. */
22437 return unify_parameter_pack_inconsistent (explain_p
,
22443 return unify_success (explain_p
);
22446 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22447 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22448 parameters and return value are as for unify. */
22451 unify_array_domain (tree tparms
, tree targs
,
22452 tree parm_dom
, tree arg_dom
,
22460 /* Our representation of array types uses "N - 1" as the
22461 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22462 not an integer constant. We cannot unify arbitrarily
22463 complex expressions, so we eliminate the MINUS_EXPRs
22465 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22466 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22469 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22470 parm_max
= TREE_OPERAND (parm_max
, 0);
22472 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22473 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22476 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22477 trying to unify the type of a variable with the type
22478 of a template parameter. For example:
22480 template <unsigned int N>
22481 void f (char (&) [N]);
22488 Here, the type of the ARG will be "int [g(i)]", and
22489 may be a SAVE_EXPR, etc. */
22490 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22491 return unify_vla_arg (explain_p
, arg_dom
);
22492 arg_max
= TREE_OPERAND (arg_max
, 0);
22495 /* If only one of the bounds used a MINUS_EXPR, compensate
22496 by adding one to the other bound. */
22497 if (parm_cst
&& !arg_cst
)
22498 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22502 else if (arg_cst
&& !parm_cst
)
22503 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22508 return unify (tparms
, targs
, parm_max
, arg_max
,
22509 UNIFY_ALLOW_INTEGER
, explain_p
);
22512 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22514 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22519 if (PACK_EXPANSION_P (t
))
22520 t
= PACK_EXPANSION_PATTERN (t
);
22521 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22522 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22523 || DECL_TYPE_TEMPLATE_P (t
))
22525 else if (TYPE_P (t
))
22531 /* Deduce the value of template parameters. TPARMS is the (innermost)
22532 set of template parameters to a template. TARGS is the bindings
22533 for those template parameters, as determined thus far; TARGS may
22534 include template arguments for outer levels of template parameters
22535 as well. PARM is a parameter to a template function, or a
22536 subcomponent of that parameter; ARG is the corresponding argument.
22537 This function attempts to match PARM with ARG in a manner
22538 consistent with the existing assignments in TARGS. If more values
22539 are deduced, then TARGS is updated.
22541 Returns 0 if the type deduction succeeds, 1 otherwise. The
22542 parameter STRICT is a bitwise or of the following flags:
22545 Require an exact match between PARM and ARG.
22546 UNIFY_ALLOW_MORE_CV_QUAL:
22547 Allow the deduced ARG to be more cv-qualified (by qualification
22548 conversion) than ARG.
22549 UNIFY_ALLOW_LESS_CV_QUAL:
22550 Allow the deduced ARG to be less cv-qualified than ARG.
22551 UNIFY_ALLOW_DERIVED:
22552 Allow the deduced ARG to be a template base class of ARG,
22553 or a pointer to a template base class of the type pointed to by
22555 UNIFY_ALLOW_INTEGER:
22556 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22557 case for more information.
22558 UNIFY_ALLOW_OUTER_LEVEL:
22559 This is the outermost level of a deduction. Used to determine validity
22560 of qualification conversions. A valid qualification conversion must
22561 have const qualified pointers leading up to the inner type which
22562 requires additional CV quals, except at the outer level, where const
22563 is not required [conv.qual]. It would be normal to set this flag in
22564 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22565 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22566 This is the outermost level of a deduction, and PARM can be more CV
22567 qualified at this point.
22568 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22569 This is the outermost level of a deduction, and PARM can be less CV
22570 qualified at this point. */
22573 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22579 int strict_in
= strict
;
22580 tsubst_flags_t complain
= (explain_p
22581 ? tf_warning_or_error
22584 /* I don't think this will do the right thing with respect to types.
22585 But the only case I've seen it in so far has been array bounds, where
22586 signedness is the only information lost, and I think that will be
22587 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22588 finish_id_expression_1, and are also OK. */
22589 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22590 parm
= TREE_OPERAND (parm
, 0);
22592 if (arg
== error_mark_node
)
22593 return unify_invalid (explain_p
);
22594 if (arg
== unknown_type_node
22595 || arg
== init_list_type_node
)
22596 /* We can't deduce anything from this, but we might get all the
22597 template args from other function args. */
22598 return unify_success (explain_p
);
22600 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22601 return unify_success (explain_p
);
22603 /* If PARM uses template parameters, then we can't bail out here,
22604 even if ARG == PARM, since we won't record unifications for the
22605 template parameters. We might need them if we're trying to
22606 figure out which of two things is more specialized. */
22607 if (arg
== parm
&& !uses_template_parms (parm
))
22608 return unify_success (explain_p
);
22610 /* Handle init lists early, so the rest of the function can assume
22611 we're dealing with a type. */
22612 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22616 tree orig_parm
= parm
;
22618 if (!is_std_init_list (parm
)
22619 && TREE_CODE (parm
) != ARRAY_TYPE
)
22620 /* We can only deduce from an initializer list argument if the
22621 parameter is std::initializer_list or an array; otherwise this
22622 is a non-deduced context. */
22623 return unify_success (explain_p
);
22625 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22626 elttype
= TREE_TYPE (parm
);
22629 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22630 /* Deduction is defined in terms of a single type, so just punt
22631 on the (bizarre) std::initializer_list<T...>. */
22632 if (PACK_EXPANSION_P (elttype
))
22633 return unify_success (explain_p
);
22636 if (strict
!= DEDUCE_EXACT
22637 && TYPE_P (elttype
)
22638 && !uses_deducible_template_parms (elttype
))
22639 /* If ELTTYPE has no deducible template parms, skip deduction from
22640 the list elements. */;
22642 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22644 int elt_strict
= strict
;
22646 if (elt
== error_mark_node
)
22647 return unify_invalid (explain_p
);
22649 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22651 tree type
= TREE_TYPE (elt
);
22652 if (type
== error_mark_node
)
22653 return unify_invalid (explain_p
);
22654 /* It should only be possible to get here for a call. */
22655 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22656 elt_strict
|= maybe_adjust_types_for_deduction
22657 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22661 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22665 if (TREE_CODE (parm
) == ARRAY_TYPE
22666 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22668 /* Also deduce from the length of the initializer list. */
22669 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22670 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22671 if (idx
== error_mark_node
)
22672 return unify_invalid (explain_p
);
22673 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22677 /* If the std::initializer_list<T> deduction worked, replace the
22678 deduced A with std::initializer_list<A>. */
22679 if (orig_parm
!= parm
)
22681 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22682 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22683 targ
= listify (targ
);
22684 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22686 return unify_success (explain_p
);
22689 /* If parm and arg aren't the same kind of thing (template, type, or
22690 expression), fail early. */
22691 if (pa_kind (parm
) != pa_kind (arg
))
22692 return unify_invalid (explain_p
);
22694 /* Immediately reject some pairs that won't unify because of
22695 cv-qualification mismatches. */
22696 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22698 /* It is the elements of the array which hold the cv quals of an array
22699 type, and the elements might be template type parms. We'll check
22700 when we recurse. */
22701 && TREE_CODE (arg
) != ARRAY_TYPE
22702 /* We check the cv-qualifiers when unifying with template type
22703 parameters below. We want to allow ARG `const T' to unify with
22704 PARM `T' for example, when computing which of two templates
22705 is more specialized, for example. */
22706 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22707 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22708 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22710 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22711 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22712 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22713 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22714 strict
&= ~UNIFY_ALLOW_DERIVED
;
22715 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22716 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22718 switch (TREE_CODE (parm
))
22720 case TYPENAME_TYPE
:
22722 case UNBOUND_CLASS_TEMPLATE
:
22723 /* In a type which contains a nested-name-specifier, template
22724 argument values cannot be deduced for template parameters used
22725 within the nested-name-specifier. */
22726 return unify_success (explain_p
);
22728 case TEMPLATE_TYPE_PARM
:
22729 case TEMPLATE_TEMPLATE_PARM
:
22730 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22731 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22732 if (error_operand_p (tparm
))
22733 return unify_invalid (explain_p
);
22735 if (TEMPLATE_TYPE_LEVEL (parm
)
22736 != template_decl_level (tparm
))
22737 /* The PARM is not one we're trying to unify. Just check
22738 to see if it matches ARG. */
22740 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22741 && (is_auto (parm
) ? is_auto (arg
)
22742 : same_type_p (parm
, arg
)))
22743 return unify_success (explain_p
);
22745 return unify_type_mismatch (explain_p
, parm
, arg
);
22747 idx
= TEMPLATE_TYPE_IDX (parm
);
22748 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22749 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22750 if (error_operand_p (tparm
))
22751 return unify_invalid (explain_p
);
22753 /* Check for mixed types and values. */
22754 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22755 && TREE_CODE (tparm
) != TYPE_DECL
)
22756 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22757 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22758 gcc_unreachable ();
22760 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22762 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22763 && CLASS_TYPE_P (arg
))
22765 /* First try to match ARG directly. */
22766 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22770 /* Otherwise, look for a suitable base of ARG, as below. */
22771 enum template_base_result r
;
22772 r
= get_template_base (tparms
, targs
, parm
, arg
,
22775 return unify_no_common_base (explain_p
, r
, parm
, arg
);
22779 /* ARG must be constructed from a template class or a template
22780 template parameter. */
22781 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
22782 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22783 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22785 /* Deduce arguments T, i from TT<T> or TT<i>. */
22786 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22789 arg
= TYPE_TI_TEMPLATE (arg
);
22791 /* Fall through to deduce template name. */
22794 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22795 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22797 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22799 /* Simple cases: Value already set, does match or doesn't. */
22800 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22801 return unify_success (explain_p
);
22803 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22807 /* If PARM is `const T' and ARG is only `int', we don't have
22808 a match unless we are allowing additional qualification.
22809 If ARG is `const int' and PARM is just `T' that's OK;
22810 that binds `const int' to `T'. */
22811 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22813 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22815 /* Consider the case where ARG is `const volatile int' and
22816 PARM is `const T'. Then, T should be `volatile int'. */
22817 arg
= cp_build_qualified_type_real
22818 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22819 if (arg
== error_mark_node
)
22820 return unify_invalid (explain_p
);
22822 /* Simple cases: Value already set, does match or doesn't. */
22823 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22824 return unify_success (explain_p
);
22826 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22828 /* Make sure that ARG is not a variable-sized array. (Note
22829 that were talking about variable-sized arrays (like
22830 `int[n]'), rather than arrays of unknown size (like
22831 `int[]').) We'll get very confused by such a type since
22832 the bound of the array is not constant, and therefore
22833 not mangleable. Besides, such types are not allowed in
22834 ISO C++, so we can do as we please here. We do allow
22835 them for 'auto' deduction, since that isn't ABI-exposed. */
22836 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22837 return unify_vla_arg (explain_p
, arg
);
22839 /* Strip typedefs as in convert_template_argument. */
22840 arg
= canonicalize_type_argument (arg
, tf_none
);
22843 /* If ARG is a parameter pack or an expansion, we cannot unify
22844 against it unless PARM is also a parameter pack. */
22845 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22846 && !template_parameter_pack_p (parm
))
22847 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22849 /* If the argument deduction results is a METHOD_TYPE,
22850 then there is a problem.
22851 METHOD_TYPE doesn't map to any real C++ type the result of
22852 the deduction cannot be of that type. */
22853 if (TREE_CODE (arg
) == METHOD_TYPE
)
22854 return unify_method_type_error (explain_p
, arg
);
22856 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22857 return unify_success (explain_p
);
22859 case TEMPLATE_PARM_INDEX
:
22860 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22861 if (error_operand_p (tparm
))
22862 return unify_invalid (explain_p
);
22864 if (TEMPLATE_PARM_LEVEL (parm
)
22865 != template_decl_level (tparm
))
22867 /* The PARM is not one we're trying to unify. Just check
22868 to see if it matches ARG. */
22869 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22870 && cp_tree_equal (parm
, arg
));
22872 unify_expression_unequal (explain_p
, parm
, arg
);
22876 idx
= TEMPLATE_PARM_IDX (parm
);
22877 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22881 if ((strict
& UNIFY_ALLOW_INTEGER
)
22882 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22883 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22884 /* We're deducing from an array bound, the type doesn't matter. */
22885 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22886 int x
= !cp_tree_equal (targ
, arg
);
22888 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22892 /* [temp.deduct.type] If, in the declaration of a function template
22893 with a non-type template-parameter, the non-type
22894 template-parameter is used in an expression in the function
22895 parameter-list and, if the corresponding template-argument is
22896 deduced, the template-argument type shall match the type of the
22897 template-parameter exactly, except that a template-argument
22898 deduced from an array bound may be of any integral type.
22899 The non-type parameter might use already deduced type parameters. */
22900 tparm
= TREE_TYPE (parm
);
22901 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22902 /* We don't have enough levels of args to do any substitution. This
22903 can happen in the context of -fnew-ttp-matching. */;
22906 ++processing_template_decl
;
22907 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22908 --processing_template_decl
;
22910 if (tree a
= type_uses_auto (tparm
))
22912 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22913 if (tparm
== error_mark_node
)
22918 if (!TREE_TYPE (arg
))
22919 /* Template-parameter dependent expression. Just accept it for now.
22920 It will later be processed in convert_template_argument. */
22922 else if (same_type_ignoring_top_level_qualifiers_p
22923 (non_reference (TREE_TYPE (arg
)),
22924 non_reference (tparm
)))
22925 /* OK. Ignore top-level quals here because a class-type template
22926 parameter object is const. */;
22927 else if ((strict
& UNIFY_ALLOW_INTEGER
)
22928 && CP_INTEGRAL_TYPE_P (tparm
))
22929 /* Convert the ARG to the type of PARM; the deduced non-type
22930 template argument must exactly match the types of the
22931 corresponding parameter. */
22932 arg
= fold (build_nop (tparm
, arg
));
22933 else if (uses_template_parms (tparm
))
22935 /* We haven't deduced the type of this parameter yet. */
22936 if (cxx_dialect
>= cxx17
22937 /* We deduce from array bounds in try_array_deduction. */
22938 && !(strict
& UNIFY_ALLOW_INTEGER
))
22940 /* Deduce it from the non-type argument. */
22941 tree atype
= TREE_TYPE (arg
);
22942 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22944 UNIFY_ALLOW_NONE
, explain_p
);
22947 /* Try again later. */
22948 return unify_success (explain_p
);
22951 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
22953 /* If ARG is a parameter pack or an expansion, we cannot unify
22954 against it unless PARM is also a parameter pack. */
22955 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22956 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
22957 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22960 bool removed_attr
= false;
22961 arg
= strip_typedefs_expr (arg
, &removed_attr
);
22963 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22964 return unify_success (explain_p
);
22968 /* A pointer-to-member constant can be unified only with
22969 another constant. */
22970 if (TREE_CODE (arg
) != PTRMEM_CST
)
22971 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
22973 /* Just unify the class member. It would be useless (and possibly
22974 wrong, depending on the strict flags) to unify also
22975 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22976 arg refer to the same variable, even if through different
22977 classes. For instance:
22979 struct A { int x; };
22982 Unification of &A::x and &B::x must succeed. */
22983 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
22984 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
22989 if (!TYPE_PTR_P (arg
))
22990 return unify_type_mismatch (explain_p
, parm
, arg
);
22992 /* [temp.deduct.call]
22994 A can be another pointer or pointer to member type that can
22995 be converted to the deduced A via a qualification
22996 conversion (_conv.qual_).
22998 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22999 This will allow for additional cv-qualification of the
23000 pointed-to types if appropriate. */
23002 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23003 /* The derived-to-base conversion only persists through one
23004 level of pointers. */
23005 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23007 return unify (tparms
, targs
, TREE_TYPE (parm
),
23008 TREE_TYPE (arg
), strict
, explain_p
);
23011 case REFERENCE_TYPE
:
23012 if (!TYPE_REF_P (arg
))
23013 return unify_type_mismatch (explain_p
, parm
, arg
);
23014 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23015 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23018 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23019 return unify_type_mismatch (explain_p
, parm
, arg
);
23020 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23021 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23022 return unify_type_mismatch (explain_p
, parm
, arg
);
23023 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23024 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23025 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23026 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23027 TYPE_DOMAIN (arg
), explain_p
);
23028 return unify_success (explain_p
);
23035 case ENUMERAL_TYPE
:
23038 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23039 return unify_type_mismatch (explain_p
, parm
, arg
);
23041 /* We have already checked cv-qualification at the top of the
23043 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23044 return unify_type_mismatch (explain_p
, parm
, arg
);
23046 /* As far as unification is concerned, this wins. Later checks
23047 will invalidate it if necessary. */
23048 return unify_success (explain_p
);
23050 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23051 /* Type INTEGER_CST can come from ordinary constant template args. */
23053 while (CONVERT_EXPR_P (arg
))
23054 arg
= TREE_OPERAND (arg
, 0);
23056 if (TREE_CODE (arg
) != INTEGER_CST
)
23057 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23058 return (tree_int_cst_equal (parm
, arg
)
23059 ? unify_success (explain_p
)
23060 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23064 int i
, len
, argslen
;
23065 int parm_variadic_p
= 0;
23067 if (TREE_CODE (arg
) != TREE_VEC
)
23068 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23070 len
= TREE_VEC_LENGTH (parm
);
23071 argslen
= TREE_VEC_LENGTH (arg
);
23073 /* Check for pack expansions in the parameters. */
23074 for (i
= 0; i
< len
; ++i
)
23076 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23079 /* We can unify against something with a trailing
23081 parm_variadic_p
= 1;
23083 /* [temp.deduct.type]/9: If the template argument list of
23084 P contains a pack expansion that is not the last
23085 template argument, the entire template argument list
23086 is a non-deduced context. */
23087 return unify_success (explain_p
);
23091 /* If we don't have enough arguments to satisfy the parameters
23092 (not counting the pack expression at the end), or we have
23093 too many arguments for a parameter list that doesn't end in
23094 a pack expression, we can't unify. */
23095 if (parm_variadic_p
23096 ? argslen
< len
- parm_variadic_p
23098 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23100 /* Unify all of the parameters that precede the (optional)
23101 pack expression. */
23102 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23104 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23105 TREE_VEC_ELT (parm
, i
),
23106 TREE_VEC_ELT (arg
, i
),
23107 UNIFY_ALLOW_NONE
, explain_p
);
23109 if (parm_variadic_p
)
23110 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23112 /*subr=*/true, explain_p
);
23113 return unify_success (explain_p
);
23118 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23119 return unify_type_mismatch (explain_p
, parm
, arg
);
23121 if (TYPE_PTRMEMFUNC_P (parm
))
23123 if (!TYPE_PTRMEMFUNC_P (arg
))
23124 return unify_type_mismatch (explain_p
, parm
, arg
);
23126 return unify (tparms
, targs
,
23127 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23128 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23129 strict
, explain_p
);
23131 else if (TYPE_PTRMEMFUNC_P (arg
))
23132 return unify_type_mismatch (explain_p
, parm
, arg
);
23134 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23136 tree t
= NULL_TREE
;
23138 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23140 /* First, we try to unify the PARM and ARG directly. */
23141 t
= try_class_unification (tparms
, targs
,
23142 parm
, arg
, explain_p
);
23146 /* Fallback to the special case allowed in
23147 [temp.deduct.call]:
23149 If P is a class, and P has the form
23150 template-id, then A can be a derived class of
23151 the deduced A. Likewise, if P is a pointer to
23152 a class of the form template-id, A can be a
23153 pointer to a derived class pointed to by the
23155 enum template_base_result r
;
23156 r
= get_template_base (tparms
, targs
, parm
, arg
,
23161 /* Don't give the derived diagnostic if we're
23162 already dealing with the same template. */
23164 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23165 && (CLASSTYPE_TI_TEMPLATE (parm
)
23166 == CLASSTYPE_TI_TEMPLATE (arg
)));
23167 return unify_no_common_base (explain_p
&& !same_template
,
23172 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23173 && (CLASSTYPE_TI_TEMPLATE (parm
)
23174 == CLASSTYPE_TI_TEMPLATE (arg
)))
23175 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23176 Then, we should unify `int' and `U'. */
23179 /* There's no chance of unification succeeding. */
23180 return unify_type_mismatch (explain_p
, parm
, arg
);
23182 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23183 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23185 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23186 return unify_type_mismatch (explain_p
, parm
, arg
);
23187 return unify_success (explain_p
);
23190 case FUNCTION_TYPE
:
23192 unsigned int nargs
;
23197 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23198 return unify_type_mismatch (explain_p
, parm
, arg
);
23200 /* CV qualifications for methods can never be deduced, they must
23201 match exactly. We need to check them explicitly here,
23202 because type_unification_real treats them as any other
23203 cv-qualified parameter. */
23204 if (TREE_CODE (parm
) == METHOD_TYPE
23205 && (!check_cv_quals_for_unify
23207 class_of_this_parm (arg
),
23208 class_of_this_parm (parm
))))
23209 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23210 if (TREE_CODE (arg
) == FUNCTION_TYPE
23211 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23212 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23213 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23214 return unify_type_mismatch (explain_p
, parm
, arg
);
23216 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23217 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23219 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23220 args
= XALLOCAVEC (tree
, nargs
);
23221 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23222 a
!= NULL_TREE
&& a
!= void_list_node
;
23223 a
= TREE_CHAIN (a
), ++i
)
23224 args
[i
] = TREE_VALUE (a
);
23227 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23228 args
, nargs
, 1, DEDUCE_EXACT
,
23232 if (flag_noexcept_type
)
23234 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23235 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23236 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23237 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23238 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23239 && uses_template_parms (TREE_PURPOSE (pspec
)))
23240 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23241 TREE_PURPOSE (aspec
),
23242 UNIFY_ALLOW_NONE
, explain_p
);
23243 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23244 return unify_type_mismatch (explain_p
, parm
, arg
);
23251 /* Unify a pointer to member with a pointer to member function, which
23252 deduces the type of the member as a function type. */
23253 if (TYPE_PTRMEMFUNC_P (arg
))
23255 /* Check top-level cv qualifiers */
23256 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23257 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23259 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23260 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23261 UNIFY_ALLOW_NONE
, explain_p
);
23263 /* Determine the type of the function we are unifying against. */
23264 tree fntype
= static_fn_type (arg
);
23266 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23269 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23270 return unify_type_mismatch (explain_p
, parm
, arg
);
23271 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23272 TYPE_OFFSET_BASETYPE (arg
),
23273 UNIFY_ALLOW_NONE
, explain_p
);
23274 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23275 strict
, explain_p
);
23278 if (DECL_TEMPLATE_PARM_P (parm
))
23279 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23280 if (arg
!= scalar_constant_value (parm
))
23281 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23282 return unify_success (explain_p
);
23285 case TEMPLATE_DECL
:
23286 /* Matched cases are handled by the ARG == PARM test above. */
23287 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23290 /* We might get a variable as a non-type template argument in parm if the
23291 corresponding parameter is type-dependent. Make any necessary
23292 adjustments based on whether arg is a reference. */
23293 if (CONSTANT_CLASS_P (arg
))
23294 parm
= fold_non_dependent_expr (parm
, complain
);
23295 else if (REFERENCE_REF_P (arg
))
23297 tree sub
= TREE_OPERAND (arg
, 0);
23299 if (TREE_CODE (sub
) == ADDR_EXPR
)
23300 arg
= TREE_OPERAND (sub
, 0);
23302 /* Now use the normal expression code to check whether they match. */
23305 case TYPE_ARGUMENT_PACK
:
23306 case NONTYPE_ARGUMENT_PACK
:
23307 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23308 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23311 case DECLTYPE_TYPE
:
23312 case UNDERLYING_TYPE
:
23313 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23314 or UNDERLYING_TYPE nodes. */
23315 return unify_success (explain_p
);
23318 /* Unification fails if we hit an error node. */
23319 return unify_invalid (explain_p
);
23322 if (REFERENCE_REF_P (parm
))
23324 bool pexp
= PACK_EXPANSION_P (arg
);
23326 arg
= PACK_EXPANSION_PATTERN (arg
);
23327 if (REFERENCE_REF_P (arg
))
23328 arg
= TREE_OPERAND (arg
, 0);
23330 arg
= make_pack_expansion (arg
, complain
);
23331 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23332 strict
, explain_p
);
23337 /* An unresolved overload is a nondeduced context. */
23338 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23339 return unify_success (explain_p
);
23340 gcc_assert (EXPR_P (parm
)
23341 || COMPOUND_LITERAL_P (parm
)
23342 || TREE_CODE (parm
) == TRAIT_EXPR
);
23344 /* We must be looking at an expression. This can happen with
23348 void foo(S<I>, S<I + 2>);
23352 template<typename T>
23353 void foo(A<T, T{}>);
23355 This is a "non-deduced context":
23359 The non-deduced contexts are:
23361 --A non-type template argument or an array bound in which
23362 a subexpression references a template parameter.
23364 In these cases, we assume deduction succeeded, but don't
23365 actually infer any unifications. */
23367 if (!uses_template_parms (parm
)
23368 && !template_args_equal (parm
, arg
))
23369 return unify_expression_unequal (explain_p
, parm
, arg
);
23371 return unify_success (explain_p
);
23374 #undef RECUR_AND_CHECK_FAILURE
23376 /* Note that DECL can be defined in this translation unit, if
23380 mark_definable (tree decl
)
23383 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23384 FOR_EACH_CLONE (clone
, decl
)
23385 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23388 /* Called if RESULT is explicitly instantiated, or is a member of an
23389 explicitly instantiated class. */
23392 mark_decl_instantiated (tree result
, int extern_p
)
23394 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23396 /* If this entity has already been written out, it's too late to
23397 make any modifications. */
23398 if (TREE_ASM_WRITTEN (result
))
23401 /* For anonymous namespace we don't need to do anything. */
23402 if (decl_anon_ns_mem_p (result
))
23404 gcc_assert (!TREE_PUBLIC (result
));
23408 if (TREE_CODE (result
) != FUNCTION_DECL
)
23409 /* The TREE_PUBLIC flag for function declarations will have been
23410 set correctly by tsubst. */
23411 TREE_PUBLIC (result
) = 1;
23413 /* This might have been set by an earlier implicit instantiation. */
23414 DECL_COMDAT (result
) = 0;
23417 DECL_NOT_REALLY_EXTERN (result
) = 0;
23420 mark_definable (result
);
23421 mark_needed (result
);
23422 /* Always make artificials weak. */
23423 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23424 comdat_linkage (result
);
23425 /* For WIN32 we also want to put explicit instantiations in
23426 linkonce sections. */
23427 else if (TREE_PUBLIC (result
))
23428 maybe_make_one_only (result
);
23429 if (TREE_CODE (result
) == FUNCTION_DECL
23430 && DECL_TEMPLATE_INSTANTIATED (result
))
23431 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23432 since start_preparsed_function wouldn't have if we had an earlier
23433 extern explicit instantiation. */
23434 DECL_EXTERNAL (result
) = 0;
23437 /* If EXTERN_P, then this function will not be emitted -- unless
23438 followed by an explicit instantiation, at which point its linkage
23439 will be adjusted. If !EXTERN_P, then this function will be
23440 emitted here. In neither circumstance do we want
23441 import_export_decl to adjust the linkage. */
23442 DECL_INTERFACE_KNOWN (result
) = 1;
23445 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23446 important template arguments. If any are missing, we check whether
23447 they're important by using error_mark_node for substituting into any
23448 args that were used for partial ordering (the ones between ARGS and END)
23449 and seeing if it bubbles up. */
23452 check_undeduced_parms (tree targs
, tree args
, tree end
)
23454 bool found
= false;
23456 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23457 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23460 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23464 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23465 if (substed
== error_mark_node
)
23471 /* Given two function templates PAT1 and PAT2, return:
23473 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23474 -1 if PAT2 is more specialized than PAT1.
23475 0 if neither is more specialized.
23477 LEN indicates the number of parameters we should consider
23478 (defaulted parameters should not be considered).
23480 The 1998 std underspecified function template partial ordering, and
23481 DR214 addresses the issue. We take pairs of arguments, one from
23482 each of the templates, and deduce them against each other. One of
23483 the templates will be more specialized if all the *other*
23484 template's arguments deduce against its arguments and at least one
23485 of its arguments *does* *not* deduce against the other template's
23486 corresponding argument. Deduction is done as for class templates.
23487 The arguments used in deduction have reference and top level cv
23488 qualifiers removed. Iff both arguments were originally reference
23489 types *and* deduction succeeds in both directions, an lvalue reference
23490 wins against an rvalue reference and otherwise the template
23491 with the more cv-qualified argument wins for that pairing (if
23492 neither is more cv-qualified, they both are equal). Unlike regular
23493 deduction, after all the arguments have been deduced in this way,
23494 we do *not* verify the deduced template argument values can be
23495 substituted into non-deduced contexts.
23497 The logic can be a bit confusing here, because we look at deduce1 and
23498 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23499 can find template arguments for pat1 to make arg1 look like arg2, that
23500 means that arg2 is at least as specialized as arg1. */
23503 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23505 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23506 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23507 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23508 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23509 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23510 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23511 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23512 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23513 tree origs1
, origs2
;
23514 bool lose1
= false;
23515 bool lose2
= false;
23517 /* Remove the this parameter from non-static member functions. If
23518 one is a non-static member function and the other is not a static
23519 member function, remove the first parameter from that function
23520 also. This situation occurs for operator functions where we
23521 locate both a member function (with this pointer) and non-member
23522 operator (with explicit first operand). */
23523 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23525 len
--; /* LEN is the number of significant arguments for DECL1 */
23526 args1
= TREE_CHAIN (args1
);
23527 if (!DECL_STATIC_FUNCTION_P (decl2
))
23528 args2
= TREE_CHAIN (args2
);
23530 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23532 args2
= TREE_CHAIN (args2
);
23533 if (!DECL_STATIC_FUNCTION_P (decl1
))
23536 args1
= TREE_CHAIN (args1
);
23540 /* If only one is a conversion operator, they are unordered. */
23541 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23544 /* Consider the return type for a conversion function */
23545 if (DECL_CONV_FN_P (decl1
))
23547 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23548 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23552 processing_template_decl
++;
23558 /* Stop when an ellipsis is seen. */
23559 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23561 tree arg1
= TREE_VALUE (args1
);
23562 tree arg2
= TREE_VALUE (args2
);
23563 int deduce1
, deduce2
;
23569 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23570 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23572 /* When both arguments are pack expansions, we need only
23573 unify the patterns themselves. */
23574 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23575 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23577 /* This is the last comparison we need to do. */
23581 /* DR 1847: If a particular P contains no template-parameters that
23582 participate in template argument deduction, that P is not used to
23583 determine the ordering. */
23584 if (!uses_deducible_template_parms (arg1
)
23585 && !uses_deducible_template_parms (arg2
))
23588 if (TYPE_REF_P (arg1
))
23590 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23591 arg1
= TREE_TYPE (arg1
);
23592 quals1
= cp_type_quals (arg1
);
23595 if (TYPE_REF_P (arg2
))
23597 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23598 arg2
= TREE_TYPE (arg2
);
23599 quals2
= cp_type_quals (arg2
);
23602 arg1
= TYPE_MAIN_VARIANT (arg1
);
23603 arg2
= TYPE_MAIN_VARIANT (arg2
);
23605 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23607 int i
, len2
= remaining_arguments (args2
);
23608 tree parmvec
= make_tree_vec (1);
23609 tree argvec
= make_tree_vec (len2
);
23612 /* Setup the parameter vector, which contains only ARG1. */
23613 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23615 /* Setup the argument vector, which contains the remaining
23617 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23618 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23620 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23621 argvec
, DEDUCE_EXACT
,
23622 /*subr=*/true, /*explain_p=*/false)
23625 /* We cannot deduce in the other direction, because ARG1 is
23626 a pack expansion but ARG2 is not. */
23629 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23631 int i
, len1
= remaining_arguments (args1
);
23632 tree parmvec
= make_tree_vec (1);
23633 tree argvec
= make_tree_vec (len1
);
23636 /* Setup the parameter vector, which contains only ARG1. */
23637 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23639 /* Setup the argument vector, which contains the remaining
23641 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23642 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23644 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23645 argvec
, DEDUCE_EXACT
,
23646 /*subr=*/true, /*explain_p=*/false)
23649 /* We cannot deduce in the other direction, because ARG2 is
23650 a pack expansion but ARG1 is not.*/
23656 /* The normal case, where neither argument is a pack
23658 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23659 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23661 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23662 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23666 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23667 arg2, then arg2 is not as specialized as arg1. */
23673 /* "If, for a given type, deduction succeeds in both directions
23674 (i.e., the types are identical after the transformations above)
23675 and both P and A were reference types (before being replaced with
23676 the type referred to above):
23677 - if the type from the argument template was an lvalue reference and
23678 the type from the parameter template was not, the argument type is
23679 considered to be more specialized than the other; otherwise,
23680 - if the type from the argument template is more cv-qualified
23681 than the type from the parameter template (as described above),
23682 the argument type is considered to be more specialized than the other;
23684 - neither type is more specialized than the other." */
23686 if (deduce1
&& deduce2
)
23688 if (ref1
&& ref2
&& ref1
!= ref2
)
23695 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23697 if ((quals1
& quals2
) == quals2
)
23699 if ((quals1
& quals2
) == quals1
)
23704 if (lose1
&& lose2
)
23705 /* We've failed to deduce something in either direction.
23706 These must be unordered. */
23711 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23712 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23713 /* We have already processed all of the arguments in our
23714 handing of the pack expansion type. */
23717 args1
= TREE_CHAIN (args1
);
23718 args2
= TREE_CHAIN (args2
);
23721 /* "In most cases, all template parameters must have values in order for
23722 deduction to succeed, but for partial ordering purposes a template
23723 parameter may remain without a value provided it is not used in the
23724 types being used for partial ordering."
23726 Thus, if we are missing any of the targs1 we need to substitute into
23727 origs1, then pat2 is not as specialized as pat1. This can happen when
23728 there is a nondeduced context. */
23729 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23731 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23734 processing_template_decl
--;
23736 /* If both deductions succeed, the partial ordering selects the more
23737 constrained template. */
23738 if (!lose1
&& !lose2
)
23740 int winner
= more_constrained (decl1
, decl2
);
23743 else if (winner
< 0)
23747 /* All things being equal, if the next argument is a pack expansion
23748 for one function but not for the other, prefer the
23749 non-variadic function. FIXME this is bogus; see c++/41958. */
23751 && args1
&& TREE_VALUE (args1
)
23752 && args2
&& TREE_VALUE (args2
))
23754 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23755 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23758 if (lose1
== lose2
)
23766 /* Determine which of two partial specializations of TMPL is more
23769 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23770 to the first partial specialization. The TREE_PURPOSE is the
23771 innermost set of template parameters for the partial
23772 specialization. PAT2 is similar, but for the second template.
23774 Return 1 if the first partial specialization is more specialized;
23775 -1 if the second is more specialized; 0 if neither is more
23778 See [temp.class.order] for information about determining which of
23779 two templates is more specialized. */
23782 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
23786 bool any_deductions
= false;
23788 tree tmpl1
= TREE_VALUE (pat1
);
23789 tree tmpl2
= TREE_VALUE (pat2
);
23790 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
23791 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23793 /* Just like what happens for functions, if we are ordering between
23794 different template specializations, we may encounter dependent
23795 types in the arguments, and we need our dependency check functions
23796 to behave correctly. */
23797 ++processing_template_decl
;
23798 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23802 any_deductions
= true;
23805 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23809 any_deductions
= true;
23811 --processing_template_decl
;
23813 /* If both deductions succeed, the partial ordering selects the more
23814 constrained template. */
23815 if (!winner
&& any_deductions
)
23816 winner
= more_constrained (tmpl1
, tmpl2
);
23818 /* In the case of a tie where at least one of the templates
23819 has a parameter pack at the end, the template with the most
23820 non-packed parameters wins. */
23823 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23824 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23826 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23827 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23828 int len1
= TREE_VEC_LENGTH (args1
);
23829 int len2
= TREE_VEC_LENGTH (args2
);
23831 /* We don't count the pack expansion at the end. */
23832 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23834 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23839 else if (len1
< len2
)
23846 /* Return the template arguments that will produce the function signature
23847 DECL from the function template FN, with the explicit template
23848 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23849 also match. Return NULL_TREE if no satisfactory arguments could be
23853 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23855 int ntparms
= DECL_NTPARMS (fn
);
23856 tree targs
= make_tree_vec (ntparms
);
23857 tree decl_type
= TREE_TYPE (decl
);
23858 tree decl_arg_types
;
23860 unsigned int nargs
, ix
;
23863 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23865 /* Never do unification on the 'this' parameter. */
23866 decl_arg_types
= skip_artificial_parms_for (decl
,
23867 TYPE_ARG_TYPES (decl_type
));
23869 nargs
= list_length (decl_arg_types
);
23870 args
= XALLOCAVEC (tree
, nargs
);
23871 for (arg
= decl_arg_types
, ix
= 0;
23872 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23873 arg
= TREE_CHAIN (arg
), ++ix
)
23874 args
[ix
] = TREE_VALUE (arg
);
23876 if (fn_type_unification (fn
, explicit_args
, targs
,
23878 (check_rettype
|| DECL_CONV_FN_P (fn
)
23879 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23880 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23881 /*explain_p=*/false,
23883 == error_mark_node
)
23889 /* Return the innermost template arguments that, when applied to a partial
23890 specialization SPEC_TMPL of TMPL, yield the ARGS.
23892 For example, suppose we have:
23894 template <class T, class U> struct S {};
23895 template <class T> struct S<T*, int> {};
23897 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23898 partial specialization and the ARGS will be {double*, int}. The resulting
23899 vector will be {double}, indicating that `T' is bound to `double'. */
23902 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23904 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23906 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23907 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23909 tree innermost_deduced_args
;
23911 innermost_deduced_args
= make_tree_vec (ntparms
);
23912 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23914 deduced_args
= copy_node (args
);
23915 SET_TMPL_ARGS_LEVEL (deduced_args
,
23916 TMPL_ARGS_DEPTH (deduced_args
),
23917 innermost_deduced_args
);
23920 deduced_args
= innermost_deduced_args
;
23922 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23924 if (unify (tparms
, deduced_args
,
23925 INNERMOST_TEMPLATE_ARGS (spec_args
),
23926 INNERMOST_TEMPLATE_ARGS (args
),
23927 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
23930 for (i
= 0; i
< ntparms
; ++i
)
23931 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
23933 if (!tried_array_deduction
)
23935 try_array_deduction (tparms
, innermost_deduced_args
,
23936 INNERMOST_TEMPLATE_ARGS (spec_args
));
23937 tried_array_deduction
= true;
23938 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
23944 if (!push_tinst_level (spec_tmpl
, deduced_args
))
23946 excessive_deduction_depth
= true;
23950 /* Verify that nondeduced template arguments agree with the type
23951 obtained from argument deduction.
23955 struct A { typedef int X; };
23956 template <class T, class U> struct C {};
23957 template <class T> struct C<T, typename T::X> {};
23959 Then with the instantiation `C<A, int>', we can deduce that
23960 `T' is `A' but unify () does not check whether `typename T::X'
23962 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
23964 if (spec_args
!= error_mark_node
)
23965 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
23966 INNERMOST_TEMPLATE_ARGS (spec_args
),
23967 tmpl
, tf_none
, false, false);
23969 pop_tinst_level ();
23971 if (spec_args
== error_mark_node
23972 /* We only need to check the innermost arguments; the other
23973 arguments will always agree. */
23974 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
23975 INNERMOST_TEMPLATE_ARGS (args
)))
23978 /* Now that we have bindings for all of the template arguments,
23979 ensure that the arguments deduced for the template template
23980 parameters have compatible template parameter lists. See the use
23981 of template_template_parm_bindings_ok_p in fn_type_unification
23982 for more information. */
23983 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
23986 return deduced_args
;
23989 // Compare two function templates T1 and T2 by deducing bindings
23990 // from one against the other. If both deductions succeed, compare
23991 // constraints to see which is more constrained.
23993 more_specialized_inst (tree t1
, tree t2
)
23998 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24004 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24010 // If both deductions succeed, then one may be more constrained.
24011 if (count
== 2 && fate
== 0)
24012 fate
= more_constrained (t1
, t2
);
24017 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24018 Return the TREE_LIST node with the most specialized template, if
24019 any. If there is no most specialized template, the error_mark_node
24022 Note that this function does not look at, or modify, the
24023 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24024 returned is one of the elements of INSTANTIATIONS, callers may
24025 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24026 and retrieve it from the value returned. */
24029 most_specialized_instantiation (tree templates
)
24033 ++processing_template_decl
;
24036 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24038 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24039 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24044 /* Equally specialized, move to next function. If there
24045 is no next function, nothing's most specialized. */
24046 fn
= TREE_CHAIN (fn
);
24054 /* Now verify that champ is better than everything earlier in the
24055 instantiation list. */
24056 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24057 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24064 processing_template_decl
--;
24067 return error_mark_node
;
24072 /* If DECL is a specialization of some template, return the most
24073 general such template. Otherwise, returns NULL_TREE.
24075 For example, given:
24077 template <class T> struct S { template <class U> void f(U); };
24079 if TMPL is `template <class U> void S<int>::f(U)' this will return
24080 the full template. This function will not trace past partial
24081 specializations, however. For example, given in addition:
24083 template <class T> struct S<T*> { template <class U> void f(U); };
24085 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24086 `template <class T> template <class U> S<T*>::f(U)'. */
24089 most_general_template (tree decl
)
24091 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24093 if (tree tinfo
= get_template_info (decl
))
24094 decl
= TI_TEMPLATE (tinfo
);
24095 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24096 template friend, or a FIELD_DECL for a capture pack. */
24097 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24101 /* Look for more and more general templates. */
24102 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24104 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24105 (See cp-tree.h for details.) */
24106 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24109 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24110 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24111 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24114 /* Stop if we run into an explicitly specialized class template. */
24115 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24116 && DECL_CONTEXT (decl
)
24117 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24120 decl
= DECL_TI_TEMPLATE (decl
);
24126 /* Return the most specialized of the template partial specializations
24127 which can produce TARGET, a specialization of some class or variable
24128 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24129 a TEMPLATE_DECL node corresponding to the partial specialization, while
24130 the TREE_PURPOSE is the set of template arguments that must be
24131 substituted into the template pattern in order to generate TARGET.
24133 If the choice of partial specialization is ambiguous, a diagnostic
24134 is issued, and the error_mark_node is returned. If there are no
24135 partial specializations matching TARGET, then NULL_TREE is
24136 returned, indicating that the primary template should be used. */
24139 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24141 tree list
= NULL_TREE
;
24146 tree outer_args
= NULL_TREE
;
24149 if (TYPE_P (target
))
24151 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24152 tmpl
= TI_TEMPLATE (tinfo
);
24153 args
= TI_ARGS (tinfo
);
24155 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24157 tmpl
= TREE_OPERAND (target
, 0);
24158 args
= TREE_OPERAND (target
, 1);
24160 else if (VAR_P (target
))
24162 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24163 tmpl
= TI_TEMPLATE (tinfo
);
24164 args
= TI_ARGS (tinfo
);
24167 gcc_unreachable ();
24169 tree main_tmpl
= most_general_template (tmpl
);
24171 /* For determining which partial specialization to use, only the
24172 innermost args are interesting. */
24173 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24175 outer_args
= strip_innermost_template_args (args
, 1);
24176 args
= INNERMOST_TEMPLATE_ARGS (args
);
24179 /* The caller hasn't called push_to_top_level yet, but we need
24180 get_partial_spec_bindings to be done in non-template context so that we'll
24181 fully resolve everything. */
24182 processing_template_decl_sentinel ptds
;
24184 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24187 tree spec_tmpl
= TREE_VALUE (t
);
24191 /* Substitute in the template args from the enclosing class. */
24192 ++processing_template_decl
;
24193 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24194 --processing_template_decl
;
24197 if (spec_tmpl
== error_mark_node
)
24198 return error_mark_node
;
24200 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24204 spec_args
= add_to_template_args (outer_args
, spec_args
);
24206 /* Keep the candidate only if the constraints are satisfied,
24207 or if we're not compiling with concepts. */
24209 || constraints_satisfied_p (spec_tmpl
, spec_args
))
24211 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
24212 TREE_TYPE (list
) = TREE_TYPE (t
);
24220 ambiguous_p
= false;
24223 t
= TREE_CHAIN (t
);
24224 for (; t
; t
= TREE_CHAIN (t
))
24226 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24233 t
= TREE_CHAIN (t
);
24236 ambiguous_p
= true;
24245 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24247 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24250 ambiguous_p
= true;
24258 char *spaces
= NULL
;
24259 if (!(complain
& tf_error
))
24260 return error_mark_node
;
24261 if (TYPE_P (target
))
24262 error ("ambiguous template instantiation for %q#T", target
);
24264 error ("ambiguous template instantiation for %q#D", target
);
24265 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24266 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24268 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24269 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24270 "%s %#qS", spaces
? spaces
: str
, subst
);
24271 spaces
= spaces
? spaces
: get_spaces (str
);
24274 return error_mark_node
;
24280 /* Explicitly instantiate DECL. */
24283 do_decl_instantiation (tree decl
, tree storage
)
24285 tree result
= NULL_TREE
;
24288 if (!decl
|| decl
== error_mark_node
)
24289 /* An error occurred, for which grokdeclarator has already issued
24290 an appropriate message. */
24292 else if (! DECL_LANG_SPECIFIC (decl
))
24294 error ("explicit instantiation of non-template %q#D", decl
);
24297 else if (DECL_DECLARED_CONCEPT_P (decl
))
24300 error ("explicit instantiation of variable concept %q#D", decl
);
24302 error ("explicit instantiation of function concept %q#D", decl
);
24306 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24307 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24309 if (VAR_P (decl
) && !var_templ
)
24311 /* There is an asymmetry here in the way VAR_DECLs and
24312 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24313 the latter, the DECL we get back will be marked as a
24314 template instantiation, and the appropriate
24315 DECL_TEMPLATE_INFO will be set up. This does not happen for
24316 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24317 should handle VAR_DECLs as it currently handles
24319 if (!DECL_CLASS_SCOPE_P (decl
))
24321 error ("%qD is not a static data member of a class template", decl
);
24324 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24325 if (!result
|| !VAR_P (result
))
24327 error ("no matching template for %qD found", decl
);
24330 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24332 error ("type %qT for explicit instantiation %qD does not match "
24333 "declared type %qT", TREE_TYPE (result
), decl
,
24338 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24340 error ("explicit instantiation of %q#D", decl
);
24346 /* Check for various error cases. Note that if the explicit
24347 instantiation is valid the RESULT will currently be marked as an
24348 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24349 until we get here. */
24351 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24353 /* DR 259 [temp.spec].
24355 Both an explicit instantiation and a declaration of an explicit
24356 specialization shall not appear in a program unless the explicit
24357 instantiation follows a declaration of the explicit specialization.
24359 For a given set of template parameters, if an explicit
24360 instantiation of a template appears after a declaration of an
24361 explicit specialization for that template, the explicit
24362 instantiation has no effect. */
24365 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24369 No program shall explicitly instantiate any template more
24372 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24373 the first instantiation was `extern' and the second is not,
24374 and EXTERN_P for the opposite case. */
24375 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24376 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24377 /* If an "extern" explicit instantiation follows an ordinary
24378 explicit instantiation, the template is instantiated. */
24382 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24384 error ("no matching template for %qD found", result
);
24387 else if (!DECL_TEMPLATE_INFO (result
))
24389 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24393 if (storage
== NULL_TREE
)
24395 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24397 if (cxx_dialect
== cxx98
)
24398 pedwarn (input_location
, OPT_Wpedantic
,
24399 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24404 error ("storage class %qD applied to template instantiation", storage
);
24406 check_explicit_instantiation_namespace (result
);
24407 mark_decl_instantiated (result
, extern_p
);
24409 instantiate_decl (result
, /*defer_ok=*/true,
24410 /*expl_inst_class_mem_p=*/false);
24414 mark_class_instantiated (tree t
, int extern_p
)
24416 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24417 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24418 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24419 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24422 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24423 rest_of_type_compilation (t
, 1);
24427 /* Called from do_type_instantiation through binding_table_foreach to
24428 do recursive instantiation for the type bound in ENTRY. */
24430 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24432 tree storage
= *(tree
*) data
;
24434 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24435 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24436 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24437 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24440 /* Perform an explicit instantiation of template class T. STORAGE, if
24441 non-null, is the RID for extern, inline or static. COMPLAIN is
24442 nonzero if this is called from the parser, zero if called recursively,
24443 since the standard is unclear (as detailed below). */
24446 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24451 int previous_instantiation_extern_p
= 0;
24453 if (TREE_CODE (t
) == TYPE_DECL
)
24456 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24459 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24461 error ("explicit instantiation of non-class template %qD", tmpl
);
24463 error ("explicit instantiation of non-template type %qT", t
);
24469 if (!COMPLETE_TYPE_P (t
))
24471 if (complain
& tf_error
)
24472 error ("explicit instantiation of %q#T before definition of template",
24477 if (storage
!= NULL_TREE
)
24479 if (storage
== ridpointers
[(int) RID_EXTERN
])
24481 if (cxx_dialect
== cxx98
)
24482 pedwarn (input_location
, OPT_Wpedantic
,
24483 "ISO C++ 1998 forbids the use of %<extern%> on "
24484 "explicit instantiations");
24487 pedwarn (input_location
, OPT_Wpedantic
,
24488 "ISO C++ forbids the use of %qE"
24489 " on explicit instantiations", storage
);
24491 if (storage
== ridpointers
[(int) RID_INLINE
])
24493 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24495 else if (storage
== ridpointers
[(int) RID_STATIC
])
24499 error ("storage class %qD applied to template instantiation",
24505 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24507 /* DR 259 [temp.spec].
24509 Both an explicit instantiation and a declaration of an explicit
24510 specialization shall not appear in a program unless the explicit
24511 instantiation follows a declaration of the explicit specialization.
24513 For a given set of template parameters, if an explicit
24514 instantiation of a template appears after a declaration of an
24515 explicit specialization for that template, the explicit
24516 instantiation has no effect. */
24519 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24523 No program shall explicitly instantiate any template more
24526 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24527 instantiation was `extern'. If EXTERN_P then the second is.
24528 These cases are OK. */
24529 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24531 if (!previous_instantiation_extern_p
&& !extern_p
24532 && (complain
& tf_error
))
24533 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24535 /* If we've already instantiated the template, just return now. */
24536 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24540 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24541 mark_class_instantiated (t
, extern_p
);
24546 /* In contrast to implicit instantiation, where only the
24547 declarations, and not the definitions, of members are
24548 instantiated, we have here:
24552 The explicit instantiation of a class template specialization
24553 implies the instantiation of all of its members not
24554 previously explicitly specialized in the translation unit
24555 containing the explicit instantiation.
24557 Of course, we can't instantiate member template classes, since we
24558 don't have any arguments for them. Note that the standard is
24559 unclear on whether the instantiation of the members are
24560 *explicit* instantiations or not. However, the most natural
24561 interpretation is that it should be an explicit
24563 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24565 || (TREE_CODE (fld
) == FUNCTION_DECL
24567 && user_provided_p (fld
)))
24568 && DECL_TEMPLATE_INSTANTIATION (fld
))
24570 mark_decl_instantiated (fld
, extern_p
);
24572 instantiate_decl (fld
, /*defer_ok=*/true,
24573 /*expl_inst_class_mem_p=*/true);
24576 if (CLASSTYPE_NESTED_UTDS (t
))
24577 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24578 bt_instantiate_type_proc
, &storage
);
24581 /* Given a function DECL, which is a specialization of TMPL, modify
24582 DECL to be a re-instantiation of TMPL with the same template
24583 arguments. TMPL should be the template into which tsubst'ing
24584 should occur for DECL, not the most general template.
24586 One reason for doing this is a scenario like this:
24589 void f(const T&, int i);
24591 void g() { f(3, 7); }
24594 void f(const T& t, const int i) { }
24596 Note that when the template is first instantiated, with
24597 instantiate_template, the resulting DECL will have no name for the
24598 first parameter, and the wrong type for the second. So, when we go
24599 to instantiate the DECL, we regenerate it. */
24602 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24604 /* The arguments used to instantiate DECL, from the most general
24608 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24610 /* Make sure that we can see identifiers, and compute access
24612 push_access_scope (decl
);
24614 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24622 args_depth
= TMPL_ARGS_DEPTH (args
);
24623 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24624 if (args_depth
> parms_depth
)
24625 args
= get_innermost_template_args (args
, parms_depth
);
24627 /* Instantiate a dynamic exception-specification. noexcept will be
24629 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24630 if (TREE_VALUE (raises
))
24632 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24633 args
, tf_error
, NULL_TREE
,
24634 /*defer_ok*/false);
24635 if (specs
&& specs
!= error_mark_node
)
24636 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24640 /* Merge parameter declarations. */
24641 decl_parm
= skip_artificial_parms_for (decl
,
24642 DECL_ARGUMENTS (decl
));
24644 = skip_artificial_parms_for (code_pattern
,
24645 DECL_ARGUMENTS (code_pattern
));
24646 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24651 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24652 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24653 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24655 parm_type
= type_decays_to (parm_type
);
24656 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24657 TREE_TYPE (decl_parm
) = parm_type
;
24658 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24659 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24661 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24662 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24664 decl_parm
= DECL_CHAIN (decl_parm
);
24665 pattern_parm
= DECL_CHAIN (pattern_parm
);
24667 /* Merge any parameters that match with the function parameter
24669 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24672 tree expanded_types
;
24673 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24674 the parameters in this function parameter pack. */
24675 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24676 args
, tf_error
, NULL_TREE
);
24677 len
= TREE_VEC_LENGTH (expanded_types
);
24678 for (i
= 0; i
< len
; i
++)
24683 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24684 /* Rename the parameter to include the index. */
24685 DECL_NAME (decl_parm
) =
24686 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24687 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24688 parm_type
= type_decays_to (parm_type
);
24689 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24690 TREE_TYPE (decl_parm
) = parm_type
;
24691 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24692 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24694 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24695 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24697 decl_parm
= DECL_CHAIN (decl_parm
);
24700 /* Merge additional specifiers from the CODE_PATTERN. */
24701 if (DECL_DECLARED_INLINE_P (code_pattern
)
24702 && !DECL_DECLARED_INLINE_P (decl
))
24703 DECL_DECLARED_INLINE_P (decl
) = 1;
24705 maybe_instantiate_noexcept (decl
, tf_error
);
24707 else if (VAR_P (decl
))
24709 start_lambda_scope (decl
);
24710 DECL_INITIAL (decl
) =
24711 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
24712 tf_error
, DECL_TI_TEMPLATE (decl
));
24713 finish_lambda_scope ();
24714 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24715 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24716 tf_error
, DECL_TI_TEMPLATE (decl
));
24719 gcc_unreachable ();
24721 pop_access_scope (decl
);
24724 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24725 substituted to get DECL. */
24728 template_for_substitution (tree decl
)
24730 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24732 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24733 for the instantiation. This is not always the most general
24734 template. Consider, for example:
24737 struct S { template <class U> void f();
24738 template <> void f<int>(); };
24740 and an instantiation of S<double>::f<int>. We want TD to be the
24741 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24742 while (/* An instantiation cannot have a definition, so we need a
24743 more general template. */
24744 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24745 /* We must also deal with friend templates. Given:
24747 template <class T> struct S {
24748 template <class U> friend void f() {};
24751 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24752 so far as the language is concerned, but that's still
24753 where we get the pattern for the instantiation from. On
24754 other hand, if the definition comes outside the class, say:
24756 template <class T> struct S {
24757 template <class U> friend void f();
24759 template <class U> friend void f() {}
24761 we don't need to look any further. That's what the check for
24762 DECL_INITIAL is for. */
24763 || (TREE_CODE (decl
) == FUNCTION_DECL
24764 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24765 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24767 /* The present template, TD, should not be a definition. If it
24768 were a definition, we should be using it! Note that we
24769 cannot restructure the loop to just keep going until we find
24770 a template with a definition, since that might go too far if
24771 a specialization was declared, but not defined. */
24773 /* Fetch the more general template. */
24774 tmpl
= DECL_TI_TEMPLATE (tmpl
);
24780 /* Returns true if we need to instantiate this template instance even if we
24781 know we aren't going to emit it. */
24784 always_instantiate_p (tree decl
)
24786 /* We always instantiate inline functions so that we can inline them. An
24787 explicit instantiation declaration prohibits implicit instantiation of
24788 non-inline functions. With high levels of optimization, we would
24789 normally inline non-inline functions -- but we're not allowed to do
24790 that for "extern template" functions. Therefore, we check
24791 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24792 return ((TREE_CODE (decl
) == FUNCTION_DECL
24793 && (DECL_DECLARED_INLINE_P (decl
)
24794 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
24795 /* And we need to instantiate static data members so that
24796 their initializers are available in integral constant
24799 && decl_maybe_constant_var_p (decl
)));
24802 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24803 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24804 error, true otherwise. */
24807 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24809 tree fntype
, spec
, noex
, clone
;
24811 /* Don't instantiate a noexcept-specification from template context. */
24812 if (processing_template_decl
24813 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24816 if (DECL_CLONED_FUNCTION_P (fn
))
24817 fn
= DECL_CLONED_FUNCTION (fn
);
24819 tree orig_fn
= NULL_TREE
;
24820 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24821 its FUNCTION_DECL for the rest of this function -- push_access_scope
24822 doesn't accept TEMPLATE_DECLs. */
24823 if (DECL_FUNCTION_TEMPLATE_P (fn
))
24826 fn
= DECL_TEMPLATE_RESULT (fn
);
24829 fntype
= TREE_TYPE (fn
);
24830 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24832 if (!spec
|| !TREE_PURPOSE (spec
))
24835 noex
= TREE_PURPOSE (spec
);
24837 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24839 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24840 bool added
= false;
24841 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24843 spec
= get_defaulted_eh_spec (fn
, complain
);
24844 if (spec
== error_mark_node
)
24845 /* This might have failed because of an unparsed DMI, so
24846 let's try again later. */
24849 else if (!(added
= !fns
->add (fn
)))
24851 /* If hash_set::add returns true, the element was already there. */
24852 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24853 DECL_SOURCE_LOCATION (fn
));
24855 "exception specification of %qD depends on itself",
24857 spec
= noexcept_false_spec
;
24859 else if (push_tinst_level (fn
))
24861 push_to_top_level ();
24862 push_access_scope (fn
);
24863 push_deferring_access_checks (dk_no_deferred
);
24864 input_location
= DECL_SOURCE_LOCATION (fn
);
24866 /* If needed, set current_class_ptr for the benefit of
24867 tsubst_copy/PARM_DECL. */
24868 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
24869 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
24871 tree this_parm
= DECL_ARGUMENTS (tdecl
);
24872 current_class_ptr
= NULL_TREE
;
24873 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
24874 current_class_ptr
= this_parm
;
24877 /* If this function is represented by a TEMPLATE_DECL, then
24878 the deferred noexcept-specification might still contain
24879 dependent types, even after substitution. And we need the
24880 dependency check functions to work in build_noexcept_spec. */
24882 ++processing_template_decl
;
24884 /* Do deferred instantiation of the noexcept-specifier. */
24885 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24886 DEFERRED_NOEXCEPT_ARGS (noex
),
24887 tf_warning_or_error
, fn
,
24888 /*function_p=*/false,
24891 /* Build up the noexcept-specification. */
24892 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24895 --processing_template_decl
;
24897 pop_deferring_access_checks ();
24898 pop_access_scope (fn
);
24899 pop_tinst_level ();
24900 pop_from_top_level ();
24903 spec
= noexcept_false_spec
;
24908 if (spec
== error_mark_node
)
24910 /* This failed with a hard error, so let's go with false. */
24911 gcc_assert (seen_error ());
24912 spec
= noexcept_false_spec
;
24915 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24917 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
24919 FOR_EACH_CLONE (clone
, fn
)
24921 if (TREE_TYPE (clone
) == fntype
)
24922 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24924 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
24931 /* We're starting to process the function INST, an instantiation of PATTERN;
24932 add their parameters to local_specializations. */
24935 register_parameter_specializations (tree pattern
, tree inst
)
24937 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
24938 tree spec_parm
= DECL_ARGUMENTS (inst
);
24939 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
24941 register_local_specialization (spec_parm
, tmpl_parm
);
24942 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
24943 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
24945 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
24947 if (!DECL_PACK_P (tmpl_parm
))
24949 register_local_specialization (spec_parm
, tmpl_parm
);
24950 spec_parm
= DECL_CHAIN (spec_parm
);
24954 /* Register the (value) argument pack as a specialization of
24955 TMPL_PARM, then move on. */
24956 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
24957 register_local_specialization (argpack
, tmpl_parm
);
24960 gcc_assert (!spec_parm
);
24963 /* Produce the definition of D, a _DECL generated from a template. If
24964 DEFER_OK is true, then we don't have to actually do the
24965 instantiation now; we just have to do it sometime. Normally it is
24966 an error if this is an explicit instantiation but D is undefined.
24967 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24968 instantiated class template. */
24971 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
24973 tree tmpl
= DECL_TI_TEMPLATE (d
);
24980 bool pattern_defined
;
24981 location_t saved_loc
= input_location
;
24982 int saved_unevaluated_operand
= cp_unevaluated_operand
;
24983 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24987 /* This function should only be used to instantiate templates for
24988 functions and static member variables. */
24989 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
24991 /* A concept is never instantiated. */
24992 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
24994 /* Variables are never deferred; if instantiation is required, they
24995 are instantiated right away. That allows for better code in the
24996 case that an expression refers to the value of the variable --
24997 if the variable has a constant value the referring expression can
24998 take advantage of that fact. */
25002 /* Don't instantiate cloned functions. Instead, instantiate the
25003 functions they cloned. */
25004 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25005 d
= DECL_CLONED_FUNCTION (d
);
25007 if (DECL_TEMPLATE_INSTANTIATED (d
)
25008 || (TREE_CODE (d
) == FUNCTION_DECL
25009 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25010 || DECL_TEMPLATE_SPECIALIZATION (d
))
25011 /* D has already been instantiated or explicitly specialized, so
25012 there's nothing for us to do here.
25014 It might seem reasonable to check whether or not D is an explicit
25015 instantiation, and, if so, stop here. But when an explicit
25016 instantiation is deferred until the end of the compilation,
25017 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25018 the instantiation. */
25021 /* Check to see whether we know that this template will be
25022 instantiated in some other file, as with "extern template"
25024 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25026 /* In general, we do not instantiate such templates. */
25027 if (external_p
&& !always_instantiate_p (d
))
25030 gen_tmpl
= most_general_template (tmpl
);
25031 gen_args
= DECL_TI_ARGS (d
);
25033 if (tmpl
!= gen_tmpl
)
25034 /* We should already have the extra args. */
25035 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25036 == TMPL_ARGS_DEPTH (gen_args
));
25037 /* And what's in the hash table should match D. */
25038 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
25039 || spec
== NULL_TREE
);
25041 /* This needs to happen before any tsubsting. */
25042 if (! push_tinst_level (d
))
25045 timevar_push (TV_TEMPLATE_INST
);
25047 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25048 for the instantiation. */
25049 td
= template_for_substitution (d
);
25054 /* Look up an explicit specialization, if any. */
25055 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25056 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25057 if (elt
&& elt
!= error_mark_node
)
25059 td
= TREE_VALUE (elt
);
25060 args
= TREE_PURPOSE (elt
);
25064 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25066 /* We should never be trying to instantiate a member of a class
25067 template or partial specialization. */
25068 gcc_assert (d
!= code_pattern
);
25070 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25071 || DECL_TEMPLATE_SPECIALIZATION (td
))
25072 /* In the case of a friend template whose definition is provided
25073 outside the class, we may have too many arguments. Drop the
25074 ones we don't need. The same is true for specializations. */
25075 args
= get_innermost_template_args
25076 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25078 if (TREE_CODE (d
) == FUNCTION_DECL
)
25080 deleted_p
= DECL_DELETED_FN (code_pattern
);
25081 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25082 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25083 || DECL_DEFAULTED_FN (code_pattern
)
25089 if (DECL_CLASS_SCOPE_P (code_pattern
))
25090 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25092 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25095 /* We may be in the middle of deferred access check. Disable it now. */
25096 push_deferring_access_checks (dk_no_deferred
);
25098 /* Unless an explicit instantiation directive has already determined
25099 the linkage of D, remember that a definition is available for
25101 if (pattern_defined
25102 && !DECL_INTERFACE_KNOWN (d
)
25103 && !DECL_NOT_REALLY_EXTERN (d
))
25104 mark_definable (d
);
25106 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25107 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25108 input_location
= DECL_SOURCE_LOCATION (d
);
25110 /* If D is a member of an explicitly instantiated class template,
25111 and no definition is available, treat it like an implicit
25113 if (!pattern_defined
&& expl_inst_class_mem_p
25114 && DECL_EXPLICIT_INSTANTIATION (d
))
25116 /* Leave linkage flags alone on instantiations with anonymous
25118 if (TREE_PUBLIC (d
))
25120 DECL_NOT_REALLY_EXTERN (d
) = 0;
25121 DECL_INTERFACE_KNOWN (d
) = 0;
25123 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25126 /* Defer all other templates, unless we have been explicitly
25127 forbidden from doing so. */
25128 if (/* If there is no definition, we cannot instantiate the
25131 /* If it's OK to postpone instantiation, do so. */
25133 /* If this is a static data member that will be defined
25134 elsewhere, we don't want to instantiate the entire data
25135 member, but we do want to instantiate the initializer so that
25136 we can substitute that elsewhere. */
25137 || (external_p
&& VAR_P (d
))
25138 /* Handle here a deleted function too, avoid generating
25139 its body (c++/61080). */
25142 /* The definition of the static data member is now required so
25143 we must substitute the initializer. */
25145 && !DECL_INITIAL (d
)
25146 && DECL_INITIAL (code_pattern
))
25150 bool const_init
= false;
25151 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25153 ns
= decl_namespace_context (d
);
25154 push_nested_namespace (ns
);
25156 push_nested_class (DECL_CONTEXT (d
));
25157 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25159 tf_warning_or_error
, NULL_TREE
,
25160 /*integral_constant_expression_p=*/false);
25161 /* If instantiating the initializer involved instantiating this
25162 again, don't call cp_finish_decl twice. */
25163 if (!DECL_INITIAL (d
))
25165 /* Make sure the initializer is still constant, in case of
25166 circular dependency (template/instantiate6.C). */
25168 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25169 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25170 /*asmspec_tree=*/NULL_TREE
,
25171 LOOKUP_ONLYCONVERTING
);
25174 pop_nested_class ();
25175 pop_nested_namespace (ns
);
25178 /* We restore the source position here because it's used by
25179 add_pending_template. */
25180 input_location
= saved_loc
;
25182 if (at_eof
&& !pattern_defined
25183 && DECL_EXPLICIT_INSTANTIATION (d
)
25184 && DECL_NOT_REALLY_EXTERN (d
))
25187 The definition of a non-exported function template, a
25188 non-exported member function template, or a non-exported
25189 member function or static data member of a class template
25190 shall be present in every translation unit in which it is
25191 explicitly instantiated. */
25192 permerror (input_location
, "explicit instantiation of %qD "
25193 "but no definition available", d
);
25195 /* If we're in unevaluated context, we just wanted to get the
25196 constant value; this isn't an odr use, so don't queue
25197 a full instantiation. */
25198 if (cp_unevaluated_operand
!= 0)
25200 /* ??? Historically, we have instantiated inline functions, even
25201 when marked as "extern template". */
25202 if (!(external_p
&& VAR_P (d
)))
25203 add_pending_template (d
);
25207 bool push_to_top
, nested
;
25209 fn_context
= decl_function_context (d
);
25210 if (LAMBDA_FUNCTION_P (d
))
25211 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25212 fn_context
= NULL_TREE
;
25213 nested
= current_function_decl
!= NULL_TREE
;
25214 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25216 vec
<tree
> omp_privatization_save
;
25218 save_omp_privatization_clauses (omp_privatization_save
);
25221 push_to_top_level ();
25224 gcc_assert (!processing_template_decl
);
25225 push_function_context ();
25226 cp_unevaluated_operand
= 0;
25227 c_inhibit_evaluation_warnings
= 0;
25230 /* Mark D as instantiated so that recursive calls to
25231 instantiate_decl do not try to instantiate it again. */
25232 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25234 /* Regenerate the declaration in case the template has been modified
25235 by a subsequent redeclaration. */
25236 regenerate_decl_from_template (d
, td
, args
);
25238 /* We already set the file and line above. Reset them now in case
25239 they changed as a result of calling regenerate_decl_from_template. */
25240 input_location
= DECL_SOURCE_LOCATION (d
);
25245 bool const_init
= false;
25247 /* Clear out DECL_RTL; whatever was there before may not be right
25248 since we've reset the type of the declaration. */
25249 SET_DECL_RTL (d
, NULL
);
25250 DECL_IN_AGGR_P (d
) = 0;
25252 /* The initializer is placed in DECL_INITIAL by
25253 regenerate_decl_from_template so we don't need to
25254 push/pop_access_scope again here. Pull it out so that
25255 cp_finish_decl can process it. */
25256 init
= DECL_INITIAL (d
);
25257 DECL_INITIAL (d
) = NULL_TREE
;
25258 DECL_INITIALIZED_P (d
) = 0;
25260 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25261 initializer. That function will defer actual emission until
25262 we have a chance to determine linkage. */
25263 DECL_EXTERNAL (d
) = 0;
25265 /* Enter the scope of D so that access-checking works correctly. */
25266 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25268 push_nested_class (DECL_CONTEXT (d
));
25270 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25271 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25272 ? LOOKUP_CONSTINIT
: 0);
25273 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25276 pop_nested_class ();
25278 if (variable_template_p (gen_tmpl
))
25279 note_variable_template_instantiation (d
);
25281 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25282 synthesize_method (d
);
25283 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25285 /* Set up the list of local specializations. */
25286 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25287 tree block
= NULL_TREE
;
25289 /* Set up context. */
25290 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25291 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25292 block
= push_stmt_list ();
25294 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25296 /* Some typedefs referenced from within the template code need to be
25297 access checked at template instantiation time, i.e now. These
25298 types were added to the template at parsing time. Let's get those
25299 and perform the access checks then. */
25300 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
25303 /* Create substitution entries for the parameters. */
25304 register_parameter_specializations (code_pattern
, d
);
25306 /* Substitute into the body of the function. */
25307 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25308 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25309 tf_warning_or_error
, tmpl
);
25312 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25313 tf_warning_or_error
, tmpl
,
25314 /*integral_constant_expression_p=*/false);
25316 /* Set the current input_location to the end of the function
25317 so that finish_function knows where we are. */
25319 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25321 /* Remember if we saw an infinite loop in the template. */
25322 current_function_infinite_loop
25323 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25326 /* Finish the function. */
25327 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25328 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25329 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25332 d
= finish_function (/*inline_p=*/false);
25333 expand_or_defer_fn (d
);
25336 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25337 cp_check_omp_declare_reduction (d
);
25340 /* We're not deferring instantiation any more. */
25341 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25344 pop_from_top_level ();
25346 pop_function_context ();
25349 restore_omp_privatization_clauses (omp_privatization_save
);
25352 pop_deferring_access_checks ();
25353 timevar_pop (TV_TEMPLATE_INST
);
25354 pop_tinst_level ();
25355 input_location
= saved_loc
;
25356 cp_unevaluated_operand
= saved_unevaluated_operand
;
25357 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25362 /* Run through the list of templates that we wish we could
25363 instantiate, and instantiate any we can. RETRIES is the
25364 number of times we retry pending template instantiation. */
25367 instantiate_pending_templates (int retries
)
25370 location_t saved_loc
= input_location
;
25372 /* Instantiating templates may trigger vtable generation. This in turn
25373 may require further template instantiations. We place a limit here
25374 to avoid infinite loop. */
25375 if (pending_templates
&& retries
>= max_tinst_depth
)
25377 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25379 fatal_error (input_location
,
25380 "template instantiation depth exceeds maximum of %d"
25381 " instantiating %q+D, possibly from virtual table generation"
25382 " (use %<-ftemplate-depth=%> to increase the maximum)",
25383 max_tinst_depth
, decl
);
25384 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25385 /* Pretend that we defined it. */
25386 DECL_INITIAL (decl
) = error_mark_node
;
25392 struct pending_template
**t
= &pending_templates
;
25393 struct pending_template
*last
= NULL
;
25397 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25398 bool complete
= false;
25400 if (TYPE_P (instantiation
))
25402 if (!COMPLETE_TYPE_P (instantiation
))
25404 instantiate_class_template (instantiation
);
25405 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25406 for (tree fld
= TYPE_FIELDS (instantiation
);
25407 fld
; fld
= TREE_CHAIN (fld
))
25409 || (TREE_CODE (fld
) == FUNCTION_DECL
25410 && !DECL_ARTIFICIAL (fld
)))
25411 && DECL_TEMPLATE_INSTANTIATION (fld
))
25412 instantiate_decl (fld
,
25413 /*defer_ok=*/false,
25414 /*expl_inst_class_mem_p=*/false);
25416 if (COMPLETE_TYPE_P (instantiation
))
25420 complete
= COMPLETE_TYPE_P (instantiation
);
25424 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25425 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25428 = instantiate_decl (instantiation
,
25429 /*defer_ok=*/false,
25430 /*expl_inst_class_mem_p=*/false);
25431 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25435 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25436 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25441 /* If INSTANTIATION has been instantiated, then we don't
25442 need to consider it again in the future. */
25443 struct pending_template
*drop
= *t
;
25445 set_refcount_ptr (drop
->tinst
);
25446 pending_template_freelist ().free (drop
);
25454 set_refcount_ptr (current_tinst_level
);
25456 last_pending_template
= last
;
25458 while (reconsider
);
25460 input_location
= saved_loc
;
25463 /* Substitute ARGVEC into T, which is a list of initializers for
25464 either base class or a non-static data member. The TREE_PURPOSEs
25465 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25466 instantiate_decl. */
25469 tsubst_initializer_list (tree t
, tree argvec
)
25471 tree inits
= NULL_TREE
;
25472 tree target_ctor
= error_mark_node
;
25474 for (; t
; t
= TREE_CHAIN (t
))
25478 tree expanded_bases
= NULL_TREE
;
25479 tree expanded_arguments
= NULL_TREE
;
25482 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25487 /* Expand the base class expansion type into separate base
25489 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25490 tf_warning_or_error
,
25492 if (expanded_bases
== error_mark_node
)
25495 /* We'll be building separate TREE_LISTs of arguments for
25497 len
= TREE_VEC_LENGTH (expanded_bases
);
25498 expanded_arguments
= make_tree_vec (len
);
25499 for (i
= 0; i
< len
; i
++)
25500 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25502 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25503 expand each argument in the TREE_VALUE of t. */
25504 expr
= make_node (EXPR_PACK_EXPANSION
);
25505 PACK_EXPANSION_LOCAL_P (expr
) = true;
25506 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25507 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25509 if (TREE_VALUE (t
) == void_type_node
)
25510 /* VOID_TYPE_NODE is used to indicate
25511 value-initialization. */
25513 for (i
= 0; i
< len
; i
++)
25514 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25518 /* Substitute parameter packs into each argument in the
25520 in_base_initializer
= 1;
25521 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25523 tree expanded_exprs
;
25525 /* Expand the argument. */
25526 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25528 = tsubst_pack_expansion (expr
, argvec
,
25529 tf_warning_or_error
,
25531 if (expanded_exprs
== error_mark_node
)
25534 /* Prepend each of the expanded expressions to the
25535 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25536 for (i
= 0; i
< len
; i
++)
25538 TREE_VEC_ELT (expanded_arguments
, i
) =
25539 tree_cons (NULL_TREE
,
25540 TREE_VEC_ELT (expanded_exprs
, i
),
25541 TREE_VEC_ELT (expanded_arguments
, i
));
25544 in_base_initializer
= 0;
25546 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25547 since we built them backwards. */
25548 for (i
= 0; i
< len
; i
++)
25550 TREE_VEC_ELT (expanded_arguments
, i
) =
25551 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25556 for (i
= 0; i
< len
; ++i
)
25558 if (expanded_bases
)
25560 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25561 decl
= expand_member_init (decl
);
25562 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25567 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25568 tf_warning_or_error
, NULL_TREE
);
25570 decl
= expand_member_init (decl
);
25571 if (decl
&& !DECL_P (decl
))
25572 in_base_initializer
= 1;
25574 init
= TREE_VALUE (t
);
25576 if (init
!= void_type_node
)
25577 init
= tsubst_expr (init
, argvec
,
25578 tf_warning_or_error
, NULL_TREE
,
25579 /*integral_constant_expression_p=*/false);
25580 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25581 /* If we had an initializer but it instantiated to nothing,
25582 value-initialize the object. This will only occur when
25583 the initializer was a pack expansion where the parameter
25584 packs used in that expansion were of length zero. */
25585 init
= void_type_node
;
25586 in_base_initializer
= 0;
25589 if (target_ctor
!= error_mark_node
25590 && init
!= error_mark_node
)
25592 error ("mem-initializer for %qD follows constructor delegation",
25596 /* Look for a target constructor. */
25597 if (init
!= error_mark_node
25598 && decl
&& CLASS_TYPE_P (decl
)
25599 && same_type_p (decl
, current_class_type
))
25601 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25604 error ("constructor delegation follows mem-initializer for %qD",
25605 TREE_PURPOSE (inits
));
25608 target_ctor
= init
;
25613 init
= build_tree_list (decl
, init
);
25614 TREE_CHAIN (init
) = inits
;
25622 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25625 set_current_access_from_decl (tree decl
)
25627 if (TREE_PRIVATE (decl
))
25628 current_access_specifier
= access_private_node
;
25629 else if (TREE_PROTECTED (decl
))
25630 current_access_specifier
= access_protected_node
;
25632 current_access_specifier
= access_public_node
;
25635 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25636 is the instantiation (which should have been created with
25637 start_enum) and ARGS are the template arguments to use. */
25640 tsubst_enum (tree tag
, tree newtag
, tree args
)
25644 if (SCOPED_ENUM_P (newtag
))
25645 begin_scope (sk_scoped_enum
, newtag
);
25647 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25652 decl
= TREE_VALUE (e
);
25653 /* Note that in a template enum, the TREE_VALUE is the
25654 CONST_DECL, not the corresponding INTEGER_CST. */
25655 value
= tsubst_expr (DECL_INITIAL (decl
),
25656 args
, tf_warning_or_error
, NULL_TREE
,
25657 /*integral_constant_expression_p=*/true);
25659 /* Give this enumeration constant the correct access. */
25660 set_current_access_from_decl (decl
);
25662 /* Actually build the enumerator itself. Here we're assuming that
25663 enumerators can't have dependent attributes. */
25664 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25665 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25668 if (SCOPED_ENUM_P (newtag
))
25671 finish_enum_value_list (newtag
);
25672 finish_enum (newtag
);
25674 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25675 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25678 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25679 its type -- but without substituting the innermost set of template
25680 arguments. So, innermost set of template parameters will appear in
25684 get_mostly_instantiated_function_type (tree decl
)
25686 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25687 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
25690 /* Return truthvalue if we're processing a template different from
25691 the last one involved in diagnostics. */
25693 problematic_instantiation_changed (void)
25695 return current_tinst_level
!= last_error_tinst_level
;
25698 /* Remember current template involved in diagnostics. */
25700 record_last_problematic_instantiation (void)
25702 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
25705 struct tinst_level
*
25706 current_instantiation (void)
25708 return current_tinst_level
;
25711 /* Return TRUE if current_function_decl is being instantiated, false
25715 instantiating_current_function_p (void)
25717 return (current_instantiation ()
25718 && (current_instantiation ()->maybe_get_node ()
25719 == current_function_decl
));
25722 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25723 type. Return false for ok, true for disallowed. Issue error and
25724 inform messages under control of COMPLAIN. */
25727 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25729 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25731 else if (TYPE_PTR_P (type
))
25733 else if (TYPE_REF_P (type
)
25734 && !TYPE_REF_IS_RVALUE (type
))
25736 else if (TYPE_PTRMEM_P (type
))
25738 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25740 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25742 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25744 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25746 /* A bound template template parm could later be instantiated to have a valid
25747 nontype parm type via an alias template. */
25748 else if (cxx_dialect
>= cxx11
25749 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25751 else if (CLASS_TYPE_P (type
))
25753 if (cxx_dialect
< cxx2a
)
25755 if (complain
& tf_error
)
25756 error ("non-type template parameters of class type only available "
25757 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25760 if (dependent_type_p (type
))
25762 if (!complete_type_or_else (type
, NULL_TREE
))
25764 if (!structural_type_p (type
))
25766 auto_diagnostic_group d
;
25767 if (complain
& tf_error
)
25768 error ("%qT is not a valid type for a template non-type parameter "
25769 "because it is not structural", type
);
25770 structural_type_p (type
, true);
25776 if (complain
& tf_error
)
25778 if (type
== error_mark_node
)
25779 inform (input_location
, "invalid template non-type parameter");
25781 error ("%q#T is not a valid type for a template non-type parameter",
25787 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25788 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25791 dependent_type_p_r (tree type
)
25797 A type is dependent if it is:
25799 -- a template parameter. Template template parameters are types
25800 for us (since TYPE_P holds true for them) so we handle
25802 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25803 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
25805 /* -- a qualified-id with a nested-name-specifier which contains a
25806 class-name that names a dependent type or whose unqualified-id
25807 names a dependent type. */
25808 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25811 /* An alias template specialization can be dependent even if the
25812 resulting type is not. */
25813 if (dependent_alias_template_spec_p (type
))
25816 /* -- a cv-qualified type where the cv-unqualified type is
25818 No code is necessary for this bullet; the code below handles
25819 cv-qualified types, and we don't want to strip aliases with
25820 TYPE_MAIN_VARIANT because of DR 1558. */
25821 /* -- a compound type constructed from any dependent type. */
25822 if (TYPE_PTRMEM_P (type
))
25823 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
25824 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25826 else if (INDIRECT_TYPE_P (type
))
25827 return dependent_type_p (TREE_TYPE (type
));
25828 else if (FUNC_OR_METHOD_TYPE_P (type
))
25832 if (dependent_type_p (TREE_TYPE (type
)))
25834 for (arg_type
= TYPE_ARG_TYPES (type
);
25836 arg_type
= TREE_CHAIN (arg_type
))
25837 if (dependent_type_p (TREE_VALUE (arg_type
)))
25839 if (cxx_dialect
>= cxx17
)
25840 /* A value-dependent noexcept-specifier makes the type dependent. */
25841 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25842 if (tree noex
= TREE_PURPOSE (spec
))
25843 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25844 affect overload resolution and treating it as dependent breaks
25845 things. Same for an unparsed noexcept expression. */
25846 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25847 && TREE_CODE (noex
) != DEFERRED_PARSE
25848 && value_dependent_expression_p (noex
))
25852 /* -- an array type constructed from any dependent type or whose
25853 size is specified by a constant expression that is
25856 We checked for type- and value-dependence of the bounds in
25857 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25858 if (TREE_CODE (type
) == ARRAY_TYPE
)
25860 if (TYPE_DOMAIN (type
)
25861 && dependent_type_p (TYPE_DOMAIN (type
)))
25863 return dependent_type_p (TREE_TYPE (type
));
25866 /* -- a template-id in which either the template name is a template
25868 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25870 /* ... or any of the template arguments is a dependent type or
25871 an expression that is type-dependent or value-dependent. */
25872 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25873 && (any_dependent_template_arguments_p
25874 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25877 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25878 dependent; if the argument of the `typeof' expression is not
25879 type-dependent, then it should already been have resolved. */
25880 if (TREE_CODE (type
) == TYPEOF_TYPE
25881 || TREE_CODE (type
) == DECLTYPE_TYPE
25882 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25885 /* A template argument pack is dependent if any of its packed
25887 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25889 tree args
= ARGUMENT_PACK_ARGS (type
);
25890 int i
, len
= TREE_VEC_LENGTH (args
);
25891 for (i
= 0; i
< len
; ++i
)
25892 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25896 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25897 be template parameters. */
25898 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25901 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25904 /* The standard does not specifically mention types that are local
25905 to template functions or local classes, but they should be
25906 considered dependent too. For example:
25908 template <int I> void f() {
25913 The size of `E' cannot be known until the value of `I' has been
25914 determined. Therefore, `E' must be considered dependent. */
25915 scope
= TYPE_CONTEXT (type
);
25916 if (scope
&& TYPE_P (scope
))
25917 return dependent_type_p (scope
);
25918 /* Don't use type_dependent_expression_p here, as it can lead
25919 to infinite recursion trying to determine whether a lambda
25920 nested in a lambda is dependent (c++/47687). */
25921 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25922 && DECL_LANG_SPECIFIC (scope
)
25923 && DECL_TEMPLATE_INFO (scope
)
25924 && (any_dependent_template_arguments_p
25925 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
25928 /* Other types are non-dependent. */
25932 /* Returns TRUE if TYPE is dependent, in the sense of
25933 [temp.dep.type]. Note that a NULL type is considered dependent. */
25936 dependent_type_p (tree type
)
25938 /* If there are no template parameters in scope, then there can't be
25939 any dependent types. */
25940 if (!processing_template_decl
)
25942 /* If we are not processing a template, then nobody should be
25943 providing us with a dependent type. */
25945 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
25949 /* If the type is NULL, we have not computed a type for the entity
25950 in question; in that case, the type is dependent. */
25954 /* Erroneous types can be considered non-dependent. */
25955 if (type
== error_mark_node
)
25958 /* Getting here with global_type_node means we improperly called this
25959 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25960 gcc_checking_assert (type
!= global_type_node
);
25962 /* If we have not already computed the appropriate value for TYPE,
25964 if (!TYPE_DEPENDENT_P_VALID (type
))
25966 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
25967 TYPE_DEPENDENT_P_VALID (type
) = 1;
25970 return TYPE_DEPENDENT_P (type
);
25973 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25974 lookup. In other words, a dependent type that is not the current
25978 dependent_scope_p (tree scope
)
25980 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
25981 && !currently_open_class (scope
));
25984 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25985 an unknown base of 'this' (and is therefore instantiation-dependent). */
25988 unknown_base_ref_p (tree t
)
25990 if (!current_class_ptr
)
25993 tree mem
= TREE_OPERAND (t
, 1);
25994 if (shared_member_p (mem
))
25997 tree cur
= current_nonlambda_class_type ();
25998 if (!any_dependent_bases_p (cur
))
26001 tree ctx
= TREE_OPERAND (t
, 0);
26002 if (DERIVED_FROM_P (ctx
, cur
))
26008 /* T is a SCOPE_REF; return whether we need to consider it
26009 instantiation-dependent so that we can check access at instantiation
26010 time even though we know which member it resolves to. */
26013 instantiation_dependent_scope_ref_p (tree t
)
26015 if (DECL_P (TREE_OPERAND (t
, 1))
26016 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26017 && !unknown_base_ref_p (t
)
26018 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26019 TREE_OPERAND (t
, 1)))
26025 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26026 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26029 /* Note that this predicate is not appropriate for general expressions;
26030 only constant expressions (that satisfy potential_constant_expression)
26031 can be tested for value dependence. */
26034 value_dependent_expression_p (tree expression
)
26036 if (!processing_template_decl
|| expression
== NULL_TREE
)
26039 /* A type-dependent expression is also value-dependent. */
26040 if (type_dependent_expression_p (expression
))
26043 switch (TREE_CODE (expression
))
26046 /* A dependent member function of the current instantiation. */
26047 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26049 case FUNCTION_DECL
:
26050 /* A dependent member function of the current instantiation. */
26051 if (DECL_CLASS_SCOPE_P (expression
)
26052 && dependent_type_p (DECL_CONTEXT (expression
)))
26056 case IDENTIFIER_NODE
:
26057 /* A name that has not been looked up -- must be dependent. */
26060 case TEMPLATE_PARM_INDEX
:
26061 /* A non-type template parm. */
26065 /* A non-type template parm. */
26066 if (DECL_TEMPLATE_PARM_P (expression
))
26068 return value_dependent_expression_p (DECL_INITIAL (expression
));
26071 /* A constant with literal type and is initialized
26072 with an expression that is value-dependent. */
26073 if (DECL_DEPENDENT_INIT_P (expression
)
26074 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26075 || TYPE_REF_P (TREE_TYPE (expression
)))
26077 if (DECL_HAS_VALUE_EXPR_P (expression
))
26079 tree value_expr
= DECL_VALUE_EXPR (expression
);
26080 if (value_dependent_expression_p (value_expr
)
26081 /* __PRETTY_FUNCTION__ inside a template function is dependent
26082 on the name of the function. */
26083 || (DECL_PRETTY_FUNCTION_P (expression
)
26084 /* It might be used in a template, but not a template
26085 function, in which case its DECL_VALUE_EXPR will be
26087 && value_expr
== error_mark_node
))
26092 case DYNAMIC_CAST_EXPR
:
26093 case STATIC_CAST_EXPR
:
26094 case CONST_CAST_EXPR
:
26095 case REINTERPRET_CAST_EXPR
:
26097 case IMPLICIT_CONV_EXPR
:
26098 /* These expressions are value-dependent if the type to which
26099 the cast occurs is dependent or the expression being casted
26100 is value-dependent. */
26102 tree type
= TREE_TYPE (expression
);
26104 if (dependent_type_p (type
))
26107 /* A functional cast has a list of operands. */
26108 expression
= TREE_OPERAND (expression
, 0);
26111 /* If there are no operands, it must be an expression such
26112 as "int()". This should not happen for aggregate types
26113 because it would form non-constant expressions. */
26114 gcc_assert (cxx_dialect
>= cxx11
26115 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26120 if (TREE_CODE (expression
) == TREE_LIST
)
26121 return any_value_dependent_elements_p (expression
);
26123 return value_dependent_expression_p (expression
);
26127 if (SIZEOF_EXPR_TYPE_P (expression
))
26128 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26132 /* A `sizeof' expression is value-dependent if the operand is
26133 type-dependent or is a pack expansion. */
26134 expression
= TREE_OPERAND (expression
, 0);
26135 if (PACK_EXPANSION_P (expression
))
26137 else if (TYPE_P (expression
))
26138 return dependent_type_p (expression
);
26139 return instantiation_dependent_uneval_expression_p (expression
);
26141 case AT_ENCODE_EXPR
:
26142 /* An 'encode' expression is value-dependent if the operand is
26144 expression
= TREE_OPERAND (expression
, 0);
26145 return dependent_type_p (expression
);
26147 case NOEXCEPT_EXPR
:
26148 expression
= TREE_OPERAND (expression
, 0);
26149 return instantiation_dependent_uneval_expression_p (expression
);
26152 /* All instantiation-dependent expressions should also be considered
26153 value-dependent. */
26154 return instantiation_dependent_scope_ref_p (expression
);
26156 case COMPONENT_REF
:
26157 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26158 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26160 case NONTYPE_ARGUMENT_PACK
:
26161 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26162 is value-dependent. */
26164 tree values
= ARGUMENT_PACK_ARGS (expression
);
26165 int i
, len
= TREE_VEC_LENGTH (values
);
26167 for (i
= 0; i
< len
; ++i
)
26168 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26176 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26178 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26184 if (TREE_CODE (type2
) != TREE_LIST
)
26185 return dependent_type_p (type2
);
26187 for (; type2
; type2
= TREE_CHAIN (type2
))
26188 if (dependent_type_p (TREE_VALUE (type2
)))
26195 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26196 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26199 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26200 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26204 tree op
= TREE_OPERAND (expression
, 0);
26205 return (value_dependent_expression_p (op
)
26206 || has_value_dependent_address (op
));
26209 case REQUIRES_EXPR
:
26210 /* Treat all requires-expressions as value-dependent so
26211 we don't try to fold them. */
26215 return dependent_type_p (TREE_OPERAND (expression
, 0));
26219 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26221 tree fn
= get_callee_fndecl (expression
);
26223 nargs
= call_expr_nargs (expression
);
26224 for (i
= 0; i
< nargs
; ++i
)
26226 tree op
= CALL_EXPR_ARG (expression
, i
);
26227 /* In a call to a constexpr member function, look through the
26228 implicit ADDR_EXPR on the object argument so that it doesn't
26229 cause the call to be considered value-dependent. We also
26230 look through it in potential_constant_expression. */
26231 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26232 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26233 && TREE_CODE (op
) == ADDR_EXPR
)
26234 op
= TREE_OPERAND (op
, 0);
26235 if (value_dependent_expression_p (op
))
26241 case TEMPLATE_ID_EXPR
:
26242 return concept_definition_p (TREE_OPERAND (expression
, 0));
26248 if (dependent_type_p (TREE_TYPE (expression
)))
26250 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26251 if (value_dependent_expression_p (val
))
26257 /* Treat a GNU statement expression as dependent to avoid crashing
26258 under instantiate_non_dependent_expr; it can't be constant. */
26262 /* A constant expression is value-dependent if any subexpression is
26263 value-dependent. */
26264 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26266 case tcc_reference
:
26268 case tcc_comparison
:
26270 case tcc_expression
:
26273 int i
, len
= cp_tree_operand_length (expression
);
26275 for (i
= 0; i
< len
; i
++)
26277 tree t
= TREE_OPERAND (expression
, i
);
26279 /* In some cases, some of the operands may be missing.
26280 (For example, in the case of PREDECREMENT_EXPR, the
26281 amount to increment by may be missing.) That doesn't
26282 make the expression dependent. */
26283 if (t
&& value_dependent_expression_p (t
))
26294 /* The expression is not value-dependent. */
26298 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26299 [temp.dep.expr]. Note that an expression with no type is
26300 considered dependent. Other parts of the compiler arrange for an
26301 expression with type-dependent subexpressions to have no type, so
26302 this function doesn't have to be fully recursive. */
26305 type_dependent_expression_p (tree expression
)
26307 if (!processing_template_decl
)
26310 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26313 STRIP_ANY_LOCATION_WRAPPER (expression
);
26315 /* An unresolved name is always dependent. */
26316 if (identifier_p (expression
)
26317 || TREE_CODE (expression
) == USING_DECL
26318 || TREE_CODE (expression
) == WILDCARD_DECL
)
26321 /* A lambda-expression in template context is dependent. dependent_type_p is
26322 true for a lambda in the scope of a class or function template, but that
26323 doesn't cover all template contexts, like a default template argument. */
26324 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26327 /* A fold expression is type-dependent. */
26328 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26329 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26330 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26331 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26334 /* Some expression forms are never type-dependent. */
26335 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
26336 || TREE_CODE (expression
) == SIZEOF_EXPR
26337 || TREE_CODE (expression
) == ALIGNOF_EXPR
26338 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26339 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26340 || TREE_CODE (expression
) == TRAIT_EXPR
26341 || TREE_CODE (expression
) == TYPEID_EXPR
26342 || TREE_CODE (expression
) == DELETE_EXPR
26343 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26344 || TREE_CODE (expression
) == THROW_EXPR
26345 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26348 /* The types of these expressions depends only on the type to which
26349 the cast occurs. */
26350 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26351 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26352 || TREE_CODE (expression
) == CONST_CAST_EXPR
26353 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26354 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26355 || TREE_CODE (expression
) == CAST_EXPR
)
26356 return dependent_type_p (TREE_TYPE (expression
));
26358 /* The types of these expressions depends only on the type created
26359 by the expression. */
26360 if (TREE_CODE (expression
) == NEW_EXPR
26361 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26363 /* For NEW_EXPR tree nodes created inside a template, either
26364 the object type itself or a TREE_LIST may appear as the
26366 tree type
= TREE_OPERAND (expression
, 1);
26367 if (TREE_CODE (type
) == TREE_LIST
)
26368 /* This is an array type. We need to check array dimensions
26370 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26371 || value_dependent_expression_p
26372 (TREE_OPERAND (TREE_VALUE (type
), 1));
26374 return dependent_type_p (type
);
26377 if (TREE_CODE (expression
) == SCOPE_REF
)
26379 tree scope
= TREE_OPERAND (expression
, 0);
26380 tree name
= TREE_OPERAND (expression
, 1);
26382 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26383 contains an identifier associated by name lookup with one or more
26384 declarations declared with a dependent type, or...a
26385 nested-name-specifier or qualified-id that names a member of an
26386 unknown specialization. */
26387 return (type_dependent_expression_p (name
)
26388 || dependent_scope_p (scope
));
26391 if (TREE_CODE (expression
) == TEMPLATE_DECL
26392 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26393 return uses_outer_template_parms (expression
);
26395 if (TREE_CODE (expression
) == STMT_EXPR
)
26396 expression
= stmt_expr_value_expr (expression
);
26398 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26403 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26405 if (type_dependent_expression_p (elt
))
26411 /* A static data member of the current instantiation with incomplete
26412 array type is type-dependent, as the definition and specializations
26413 can have different bounds. */
26414 if (VAR_P (expression
)
26415 && DECL_CLASS_SCOPE_P (expression
)
26416 && dependent_type_p (DECL_CONTEXT (expression
))
26417 && VAR_HAD_UNKNOWN_BOUND (expression
))
26420 /* An array of unknown bound depending on a variadic parameter, eg:
26422 template<typename... Args>
26423 void foo (Args... args)
26425 int arr[] = { args... };
26428 template<int... vals>
26431 int arr[] = { vals... };
26434 If the array has no length and has an initializer, it must be that
26435 we couldn't determine its length in cp_complete_array_type because
26436 it is dependent. */
26437 if (VAR_P (expression
)
26438 && TREE_TYPE (expression
) != NULL_TREE
26439 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26440 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26441 && DECL_INITIAL (expression
))
26444 /* A function or variable template-id is type-dependent if it has any
26445 dependent template arguments. */
26446 if (VAR_OR_FUNCTION_DECL_P (expression
)
26447 && DECL_LANG_SPECIFIC (expression
)
26448 && DECL_TEMPLATE_INFO (expression
))
26450 /* Consider the innermost template arguments, since those are the ones
26451 that come from the template-id; the template arguments for the
26452 enclosing class do not make it type-dependent unless they are used in
26453 the type of the decl. */
26454 if (instantiates_primary_template_p (expression
)
26455 && (any_dependent_template_arguments_p
26456 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26460 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26461 type-dependent. Checking this is important for functions with auto return
26462 type, which looks like a dependent type. */
26463 if (TREE_CODE (expression
) == FUNCTION_DECL
26464 && !(DECL_CLASS_SCOPE_P (expression
)
26465 && dependent_type_p (DECL_CONTEXT (expression
)))
26466 && !(DECL_LANG_SPECIFIC (expression
)
26467 && DECL_FRIEND_P (expression
)
26468 && (!DECL_FRIEND_CONTEXT (expression
)
26469 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26470 && !DECL_LOCAL_FUNCTION_P (expression
))
26472 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26473 || undeduced_auto_decl (expression
));
26477 /* Always dependent, on the number of arguments if nothing else. */
26478 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26481 if (TREE_TYPE (expression
) == unknown_type_node
)
26483 if (TREE_CODE (expression
) == ADDR_EXPR
)
26484 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26485 if (TREE_CODE (expression
) == COMPONENT_REF
26486 || TREE_CODE (expression
) == OFFSET_REF
)
26488 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26490 expression
= TREE_OPERAND (expression
, 1);
26491 if (identifier_p (expression
))
26494 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26495 if (TREE_CODE (expression
) == SCOPE_REF
)
26498 if (BASELINK_P (expression
))
26500 if (BASELINK_OPTYPE (expression
)
26501 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26503 expression
= BASELINK_FUNCTIONS (expression
);
26506 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26508 if (any_dependent_template_arguments_p
26509 (TREE_OPERAND (expression
, 1)))
26511 expression
= TREE_OPERAND (expression
, 0);
26512 if (identifier_p (expression
))
26516 gcc_assert (OVL_P (expression
));
26518 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26519 if (type_dependent_expression_p (*iter
))
26525 /* The type of a non-type template parm declared with a placeholder type
26526 depends on the corresponding template argument, even though
26527 placeholders are not normally considered dependent. */
26528 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26529 && is_auto (TREE_TYPE (expression
)))
26532 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26534 /* Dependent type attributes might not have made it from the decl to
26536 if (DECL_P (expression
)
26537 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26540 return (dependent_type_p (TREE_TYPE (expression
)));
26543 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26544 type-dependent if the expression refers to a member of the current
26545 instantiation and the type of the referenced member is dependent, or the
26546 class member access expression refers to a member of an unknown
26549 This function returns true if the OBJECT in such a class member access
26550 expression is of an unknown specialization. */
26553 type_dependent_object_expression_p (tree object
)
26555 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26557 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26559 tree scope
= TREE_TYPE (object
);
26560 return (!scope
|| dependent_scope_p (scope
));
26563 /* walk_tree callback function for instantiation_dependent_expression_p,
26564 below. Returns non-zero if a dependent subexpression is found. */
26567 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26572 /* We don't have to worry about decltype currently because decltype
26573 of an instantiation-dependent expr is a dependent type. This
26574 might change depending on the resolution of DR 1172. */
26575 *walk_subtrees
= false;
26578 enum tree_code code
= TREE_CODE (*tp
);
26581 /* Don't treat an argument list as dependent just because it has no
26585 case NONTYPE_ARGUMENT_PACK
:
26588 case TEMPLATE_PARM_INDEX
:
26589 if (dependent_type_p (TREE_TYPE (*tp
)))
26591 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26593 /* We'll check value-dependence separately. */
26596 /* Handle expressions with type operands. */
26600 case AT_ENCODE_EXPR
:
26602 tree op
= TREE_OPERAND (*tp
, 0);
26603 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26604 op
= TREE_TYPE (op
);
26607 if (dependent_type_p (op
))
26611 *walk_subtrees
= false;
26618 case COMPONENT_REF
:
26619 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26620 /* In a template, finish_class_member_access_expr creates a
26621 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26622 type-dependent, so that we can check access control at
26623 instantiation time (PR 42277). See also Core issue 1273. */
26628 if (instantiation_dependent_scope_ref_p (*tp
))
26633 /* Treat statement-expressions as dependent. */
26637 /* Treat requires-expressions as dependent. */
26638 case REQUIRES_EXPR
:
26642 /* Treat concept checks as dependent. */
26643 if (concept_check_p (*tp
))
26647 case TEMPLATE_ID_EXPR
:
26648 /* Treat concept checks as dependent. */
26649 if (concept_check_p (*tp
))
26654 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26662 if (type_dependent_expression_p (*tp
))
26668 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26669 sense defined by the ABI:
26671 "An expression is instantiation-dependent if it is type-dependent
26672 or value-dependent, or it has a subexpression that is type-dependent
26673 or value-dependent."
26675 Except don't actually check value-dependence for unevaluated expressions,
26676 because in sizeof(i) we don't care about the value of i. Checking
26677 type-dependence will in turn check value-dependence of array bounds/template
26678 arguments as needed. */
26681 instantiation_dependent_uneval_expression_p (tree expression
)
26685 if (!processing_template_decl
)
26688 if (expression
== error_mark_node
)
26691 result
= cp_walk_tree_without_duplicates (&expression
,
26692 instantiation_dependent_r
, NULL
);
26693 return result
!= NULL_TREE
;
26696 /* As above, but also check value-dependence of the expression as a whole. */
26699 instantiation_dependent_expression_p (tree expression
)
26701 return (instantiation_dependent_uneval_expression_p (expression
)
26702 || value_dependent_expression_p (expression
));
26705 /* Like type_dependent_expression_p, but it also works while not processing
26706 a template definition, i.e. during substitution or mangling. */
26709 type_dependent_expression_p_push (tree expr
)
26712 ++processing_template_decl
;
26713 b
= type_dependent_expression_p (expr
);
26714 --processing_template_decl
;
26718 /* Returns TRUE if ARGS contains a type-dependent expression. */
26721 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
26726 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
26728 if (type_dependent_expression_p (arg
))
26734 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26735 expressions) contains any type-dependent expressions. */
26738 any_type_dependent_elements_p (const_tree list
)
26740 for (; list
; list
= TREE_CHAIN (list
))
26741 if (type_dependent_expression_p (TREE_VALUE (list
)))
26747 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26748 expressions) contains any value-dependent expressions. */
26751 any_value_dependent_elements_p (const_tree list
)
26753 for (; list
; list
= TREE_CHAIN (list
))
26754 if (value_dependent_expression_p (TREE_VALUE (list
)))
26760 /* Returns TRUE if the ARG (a template argument) is dependent. */
26763 dependent_template_arg_p (tree arg
)
26765 if (!processing_template_decl
)
26768 /* Assume a template argument that was wrongly written by the user
26769 is dependent. This is consistent with what
26770 any_dependent_template_arguments_p [that calls this function]
26772 if (!arg
|| arg
== error_mark_node
)
26775 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
26776 arg
= argument_pack_select_arg (arg
);
26778 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
26780 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26782 if (DECL_TEMPLATE_PARM_P (arg
))
26784 /* A member template of a dependent class is not necessarily
26785 type-dependent, but it is a dependent template argument because it
26786 will be a member of an unknown specialization to that template. */
26787 tree scope
= CP_DECL_CONTEXT (arg
);
26788 return TYPE_P (scope
) && dependent_type_p (scope
);
26790 else if (ARGUMENT_PACK_P (arg
))
26792 tree args
= ARGUMENT_PACK_ARGS (arg
);
26793 int i
, len
= TREE_VEC_LENGTH (args
);
26794 for (i
= 0; i
< len
; ++i
)
26796 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26802 else if (TYPE_P (arg
))
26803 return dependent_type_p (arg
);
26805 return (type_dependent_expression_p (arg
)
26806 || value_dependent_expression_p (arg
));
26809 /* Returns true if ARGS (a collection of template arguments) contains
26810 any types that require structural equality testing. */
26813 any_template_arguments_need_structural_equality_p (tree args
)
26820 if (args
== error_mark_node
)
26823 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26825 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26826 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26828 tree arg
= TREE_VEC_ELT (level
, j
);
26829 tree packed_args
= NULL_TREE
;
26832 if (ARGUMENT_PACK_P (arg
))
26834 /* Look inside the argument pack. */
26835 packed_args
= ARGUMENT_PACK_ARGS (arg
);
26836 len
= TREE_VEC_LENGTH (packed_args
);
26839 for (k
= 0; k
< len
; ++k
)
26842 arg
= TREE_VEC_ELT (packed_args
, k
);
26844 if (error_operand_p (arg
))
26846 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26848 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
26850 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
26851 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26860 /* Returns true if ARGS (a collection of template arguments) contains
26861 any dependent arguments. */
26864 any_dependent_template_arguments_p (const_tree args
)
26871 if (args
== error_mark_node
)
26874 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26876 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26877 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26878 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26885 /* Returns true if ARGS contains any errors. */
26888 any_erroneous_template_args_p (const_tree args
)
26893 if (args
== error_mark_node
)
26896 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26898 if (tree ti
= get_template_info (args
))
26899 args
= TI_ARGS (ti
);
26907 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26909 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26910 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26911 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26918 /* Returns TRUE if the template TMPL is type-dependent. */
26921 dependent_template_p (tree tmpl
)
26923 if (TREE_CODE (tmpl
) == OVERLOAD
)
26925 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
26926 if (dependent_template_p (*iter
))
26931 /* Template template parameters are dependent. */
26932 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
26933 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
26935 /* So are names that have not been looked up. */
26936 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
26941 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26944 dependent_template_id_p (tree tmpl
, tree args
)
26946 return (dependent_template_p (tmpl
)
26947 || any_dependent_template_arguments_p (args
));
26950 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26954 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
26958 if (!processing_template_decl
)
26961 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
26963 tree decl
= TREE_VEC_ELT (declv
, i
);
26964 tree init
= TREE_VEC_ELT (initv
, i
);
26965 tree cond
= TREE_VEC_ELT (condv
, i
);
26966 tree incr
= TREE_VEC_ELT (incrv
, i
);
26968 if (type_dependent_expression_p (decl
)
26969 || TREE_CODE (decl
) == SCOPE_REF
)
26972 if (init
&& type_dependent_expression_p (init
))
26975 if (cond
== global_namespace
)
26978 if (type_dependent_expression_p (cond
))
26981 if (COMPARISON_CLASS_P (cond
)
26982 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
26983 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
26986 if (TREE_CODE (incr
) == MODOP_EXPR
)
26988 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
26989 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
26992 else if (type_dependent_expression_p (incr
))
26994 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
26996 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
26998 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27000 tree t
= TREE_OPERAND (incr
, 1);
27001 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27002 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27005 /* If this loop has a class iterator with != comparison
27006 with increment other than i++/++i/i--/--i, make sure the
27007 increment is constant. */
27008 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27009 && TREE_CODE (cond
) == NE_EXPR
)
27011 if (TREE_OPERAND (t
, 0) == decl
)
27012 t
= TREE_OPERAND (t
, 1);
27014 t
= TREE_OPERAND (t
, 0);
27015 if (TREE_CODE (t
) != INTEGER_CST
)
27025 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27026 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27027 no such TYPE can be found. Note that this function peers inside
27028 uninstantiated templates and therefore should be used only in
27029 extremely limited situations. ONLY_CURRENT_P restricts this
27030 peering to the currently open classes hierarchy (which is required
27031 when comparing types). */
27034 resolve_typename_type (tree type
, bool only_current_p
)
27043 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27045 scope
= TYPE_CONTEXT (type
);
27046 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27047 gcc_checking_assert (uses_template_parms (scope
));
27049 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27050 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27051 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27052 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27053 identifier of the TYPENAME_TYPE anymore.
27054 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27055 TYPENAME_TYPE instead, we avoid messing up with a possible
27056 typedef variant case. */
27057 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27059 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27060 it first before we can figure out what NAME refers to. */
27061 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27063 if (TYPENAME_IS_RESOLVING_P (scope
))
27064 /* Given a class template A with a dependent base with nested type C,
27065 typedef typename A::C::C C will land us here, as trying to resolve
27066 the initial A::C leads to the local C typedef, which leads back to
27067 A::C::C. So we break the recursion now. */
27070 scope
= resolve_typename_type (scope
, only_current_p
);
27072 /* If we don't know what SCOPE refers to, then we cannot resolve the
27074 if (!CLASS_TYPE_P (scope
))
27076 /* If this is a typedef, we don't want to look inside (c++/11987). */
27077 if (typedef_variant_p (type
))
27079 /* If SCOPE isn't the template itself, it will not have a valid
27080 TYPE_FIELDS list. */
27081 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27082 /* scope is either the template itself or a compatible instantiation
27083 like X<T>, so look up the name in the original template. */
27084 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27085 /* If scope has no fields, it can't be a current instantiation. Check this
27086 before currently_open_class to avoid infinite recursion (71515). */
27087 if (!TYPE_FIELDS (scope
))
27089 /* If the SCOPE is not the current instantiation, there's no reason
27090 to look inside it. */
27091 if (only_current_p
&& !currently_open_class (scope
))
27093 /* Enter the SCOPE so that name lookup will be resolved as if we
27094 were in the class definition. In particular, SCOPE will no
27095 longer be considered a dependent type. */
27096 pushed_scope
= push_scope (scope
);
27097 /* Look up the declaration. */
27098 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27099 tf_warning_or_error
);
27101 result
= NULL_TREE
;
27103 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27104 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27105 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27108 else if (identifier_p (fullname
)
27109 && TREE_CODE (decl
) == TYPE_DECL
)
27111 result
= TREE_TYPE (decl
);
27112 if (result
== error_mark_node
)
27113 result
= NULL_TREE
;
27115 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27116 && DECL_CLASS_TEMPLATE_P (decl
))
27118 /* Obtain the template and the arguments. */
27119 tree tmpl
= TREE_OPERAND (fullname
, 0);
27120 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27122 /* We get here with a plain identifier because a previous tentative
27123 parse of the nested-name-specifier as part of a ptr-operator saw
27124 ::template X<A>. The use of ::template is necessary in a
27125 ptr-operator, but wrong in a declarator-id.
27127 [temp.names]: In a qualified-id of a declarator-id, the keyword
27128 template shall not appear at the top level. */
27129 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27130 "keyword %<template%> not allowed in declarator-id");
27133 tree args
= TREE_OPERAND (fullname
, 1);
27134 /* Instantiate the template. */
27135 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27136 /*entering_scope=*/true,
27137 tf_error
| tf_user
);
27138 if (result
== error_mark_node
)
27139 result
= NULL_TREE
;
27142 /* Leave the SCOPE. */
27144 pop_scope (pushed_scope
);
27146 /* If we failed to resolve it, return the original typename. */
27150 /* If lookup found a typename type, resolve that too. */
27151 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27153 /* Ill-formed programs can cause infinite recursion here, so we
27154 must catch that. */
27155 TYPENAME_IS_RESOLVING_P (result
) = 1;
27156 result
= resolve_typename_type (result
, only_current_p
);
27157 TYPENAME_IS_RESOLVING_P (result
) = 0;
27160 /* Qualify the resulting type. */
27161 quals
= cp_type_quals (type
);
27163 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27168 /* EXPR is an expression which is not type-dependent. Return a proxy
27169 for EXPR that can be used to compute the types of larger
27170 expressions containing EXPR. */
27173 build_non_dependent_expr (tree expr
)
27175 tree orig_expr
= expr
;
27178 /* When checking, try to get a constant value for all non-dependent
27179 expressions in order to expose bugs in *_dependent_expression_p
27180 and constexpr. This can affect code generation, see PR70704, so
27181 only do this for -fchecking=2. */
27182 if (flag_checking
> 1
27183 && cxx_dialect
>= cxx11
27184 /* Don't do this during nsdmi parsing as it can lead to
27185 unexpected recursive instantiations. */
27186 && !parsing_nsdmi ()
27187 /* Don't do this during concept processing either and for
27188 the same reason. */
27189 && !processing_constraint_expression_p ())
27190 fold_non_dependent_expr (expr
, tf_none
);
27192 STRIP_ANY_LOCATION_WRAPPER (expr
);
27194 /* Preserve OVERLOADs; the functions must be available to resolve
27197 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27198 inner_expr
= stmt_expr_value_expr (inner_expr
);
27199 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27200 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27201 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27202 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27203 if (is_overloaded_fn (inner_expr
)
27204 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27206 /* There is no need to return a proxy for a variable or enumerator. */
27207 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27209 /* Preserve string constants; conversions from string constants to
27210 "char *" are allowed, even though normally a "const char *"
27211 cannot be used to initialize a "char *". */
27212 if (TREE_CODE (expr
) == STRING_CST
)
27214 /* Preserve void and arithmetic constants, as an optimization -- there is no
27215 reason to create a new node. */
27216 if (TREE_CODE (expr
) == VOID_CST
27217 || TREE_CODE (expr
) == INTEGER_CST
27218 || TREE_CODE (expr
) == REAL_CST
)
27220 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27221 There is at least one place where we want to know that a
27222 particular expression is a throw-expression: when checking a ?:
27223 expression, there are special rules if the second or third
27224 argument is a throw-expression. */
27225 if (TREE_CODE (expr
) == THROW_EXPR
)
27228 /* Don't wrap an initializer list, we need to be able to look inside. */
27229 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27232 /* Don't wrap a dummy object, we need to be able to test for it. */
27233 if (is_dummy_object (expr
))
27236 if (TREE_CODE (expr
) == COND_EXPR
)
27237 return build3 (COND_EXPR
,
27239 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27240 (TREE_OPERAND (expr
, 1)
27241 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27242 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27243 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27244 if (TREE_CODE (expr
) == COMPOUND_EXPR
27245 && !COMPOUND_EXPR_OVERLOADED (expr
))
27246 return build2 (COMPOUND_EXPR
,
27248 TREE_OPERAND (expr
, 0),
27249 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27251 /* If the type is unknown, it can't really be non-dependent */
27252 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27254 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27255 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27256 TREE_TYPE (expr
), expr
);
27259 /* ARGS is a vector of expressions as arguments to a function call.
27260 Replace the arguments with equivalent non-dependent expressions.
27261 This modifies ARGS in place. */
27264 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27269 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27271 tree newarg
= build_non_dependent_expr (arg
);
27273 (*args
)[ix
] = newarg
;
27277 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27278 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27279 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27282 make_auto_1 (tree name
, bool set_canonical
)
27284 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27285 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27286 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27287 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27288 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27289 TYPE_NAME (au
), NULL_TREE
);
27291 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27292 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27293 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27299 make_decltype_auto (void)
27301 return make_auto_1 (decltype_auto_identifier
, true);
27307 return make_auto_1 (auto_identifier
, true);
27310 /* Return a C++17 deduction placeholder for class template TMPL. */
27313 make_template_placeholder (tree tmpl
)
27315 tree t
= make_auto_1 (auto_identifier
, false);
27316 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27317 /* Our canonical type depends on the placeholder. */
27318 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27322 /* True iff T is a C++17 class template deduction placeholder. */
27325 template_placeholder_p (tree t
)
27327 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27330 /* Make a "constrained auto" type-specifier. This is an auto or
27331 decltype(auto) type with constraints that must be associated after
27332 deduction. The constraint is formed from the given concept CON
27333 and its optional sequence of template arguments ARGS.
27335 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27338 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27340 /* Build the constraint. */
27341 tree tmpl
= DECL_TI_TEMPLATE (con
);
27343 if (TREE_CODE (con
) == FUNCTION_DECL
)
27344 expr
= ovl_make (tmpl
);
27345 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27347 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27349 /* Our canonical type depends on the constraint. */
27350 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27352 /* Attach the constraint to the type declaration. */
27353 return TYPE_NAME (type
);
27356 /* Make a "constrained auto" type-specifier. */
27359 make_constrained_auto (tree con
, tree args
)
27361 tree type
= make_auto_1 (auto_identifier
, false);
27362 return make_constrained_placeholder_type (type
, con
, args
);
27365 /* Make a "constrained decltype(auto)" type-specifier. */
27368 make_constrained_decltype_auto (tree con
, tree args
)
27370 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27371 /* FIXME: I don't know why this isn't done in make_auto_1. */
27372 AUTO_IS_DECLTYPE (type
) = true;
27373 return make_constrained_placeholder_type (type
, con
, args
);
27376 /* Build and return a concept definition. Like other templates, the
27377 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27378 the TEMPLATE_DECL. */
27381 finish_concept_definition (cp_expr id
, tree init
)
27383 gcc_assert (identifier_p (id
));
27384 gcc_assert (processing_template_decl
);
27386 location_t loc
= id
.get_location();
27388 /* A concept-definition shall not have associated constraints. */
27389 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27391 error_at (loc
, "a concept cannot be constrained");
27392 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27395 /* A concept-definition shall appear in namespace scope. Templates
27396 aren't allowed in block scope, so we only need to check for class
27398 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27400 error_at (loc
, "concept %qE not in namespace scope", *id
);
27401 return error_mark_node
;
27404 /* Initially build the concept declaration; it's type is bool. */
27405 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27406 DECL_CONTEXT (decl
) = current_scope ();
27407 DECL_INITIAL (decl
) = init
;
27409 /* Push the enclosing template. */
27410 return push_template_decl (decl
);
27413 /* Given type ARG, return std::initializer_list<ARG>. */
27418 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27420 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27422 gcc_rich_location
richloc (input_location
);
27423 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27424 error_at (&richloc
,
27425 "deducing from brace-enclosed initializer list"
27426 " requires %<#include <initializer_list>%>");
27428 return error_mark_node
;
27430 tree argvec
= make_tree_vec (1);
27431 TREE_VEC_ELT (argvec
, 0) = arg
;
27433 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27434 NULL_TREE
, 0, tf_warning_or_error
);
27437 /* Replace auto in TYPE with std::initializer_list<auto>. */
27440 listify_autos (tree type
, tree auto_node
)
27442 tree init_auto
= listify (strip_top_quals (auto_node
));
27443 tree argvec
= make_tree_vec (1);
27444 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27445 if (processing_template_decl
)
27446 argvec
= add_to_template_args (current_template_args (), argvec
);
27447 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27450 /* Hash traits for hashing possibly constrained 'auto'
27451 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27453 struct auto_hash
: default_hash_traits
<tree
>
27455 static inline hashval_t
hash (tree
);
27456 static inline bool equal (tree
, tree
);
27459 /* Hash the 'auto' T. */
27462 auto_hash::hash (tree t
)
27464 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27465 /* Matching constrained-type-specifiers denote the same template
27466 parameter, so hash the constraint. */
27467 return hash_placeholder_constraint (c
);
27469 /* But unconstrained autos are all separate, so just hash the pointer. */
27470 return iterative_hash_object (t
, 0);
27473 /* Compare two 'auto's. */
27476 auto_hash::equal (tree t1
, tree t2
)
27481 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27482 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27484 /* Two unconstrained autos are distinct. */
27488 return equivalent_placeholder_constraints (c1
, c2
);
27491 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27492 constrained) auto, add it to the vector. */
27495 extract_autos_r (tree t
, void *data
)
27497 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27500 /* All the autos were built with index 0; fix that up now. */
27501 tree
*p
= hash
.find_slot (t
, INSERT
);
27504 /* If this is a repeated constrained-type-specifier, use the index we
27506 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27509 /* Otherwise this is new, so use the current count. */
27511 idx
= hash
.elements () - 1;
27513 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27516 /* Always keep walking. */
27520 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27521 says they can appear anywhere in the type. */
27524 extract_autos (tree type
)
27526 hash_set
<tree
> visited
;
27527 hash_table
<auto_hash
> hash (2);
27529 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27531 tree tree_vec
= make_tree_vec (hash
.elements());
27532 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27533 iter
!= hash
.end(); ++iter
)
27536 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27537 TREE_VEC_ELT (tree_vec
, i
)
27538 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27544 /* The stem for deduction guide names. */
27545 const char *const dguide_base
= "__dguide_";
27547 /* Return the name for a deduction guide for class template TMPL. */
27550 dguide_name (tree tmpl
)
27552 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27553 tree tname
= TYPE_IDENTIFIER (type
);
27554 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27555 + IDENTIFIER_LENGTH (tname
));
27556 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27557 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27558 IDENTIFIER_LENGTH (tname
) + 1);
27559 tree dname
= get_identifier (buf
);
27560 TREE_TYPE (dname
) = type
;
27564 /* True if NAME is the name of a deduction guide. */
27567 dguide_name_p (tree name
)
27569 return (TREE_CODE (name
) == IDENTIFIER_NODE
27570 && TREE_TYPE (name
)
27571 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27572 strlen (dguide_base
)));
27575 /* True if FN is a deduction guide. */
27578 deduction_guide_p (const_tree fn
)
27581 if (tree name
= DECL_NAME (fn
))
27582 return dguide_name_p (name
);
27586 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27589 copy_guide_p (const_tree fn
)
27591 gcc_assert (deduction_guide_p (fn
));
27592 if (!DECL_ARTIFICIAL (fn
))
27594 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27595 return (TREE_CHAIN (parms
) == void_list_node
27596 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27599 /* True if FN is a guide generated from a constructor template. */
27602 template_guide_p (const_tree fn
)
27604 gcc_assert (deduction_guide_p (fn
));
27605 if (!DECL_ARTIFICIAL (fn
))
27607 tree tmpl
= DECL_TI_TEMPLATE (fn
);
27608 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
27609 return PRIMARY_TEMPLATE_P (org
);
27613 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27614 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27615 template parameter types. Note that the handling of template template
27616 parameters relies on current_template_parms being set appropriately for the
27620 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
27621 tree tsubst_args
, tsubst_flags_t complain
)
27623 if (olddecl
== error_mark_node
)
27624 return error_mark_node
;
27626 tree oldidx
= get_template_parm_index (olddecl
);
27629 if (TREE_CODE (olddecl
) == TYPE_DECL
27630 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27632 tree oldtype
= TREE_TYPE (olddecl
);
27633 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27634 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27635 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27636 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27637 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27641 newtype
= TREE_TYPE (olddecl
);
27642 if (type_uses_auto (newtype
))
27644 // Substitute once to fix references to other template parameters.
27645 newtype
= tsubst (newtype
, tsubst_args
,
27646 complain
|tf_partial
, NULL_TREE
);
27647 // Now substitute again to reduce the level of the auto.
27648 newtype
= tsubst (newtype
, current_template_args (),
27649 complain
, NULL_TREE
);
27652 newtype
= tsubst (newtype
, tsubst_args
,
27653 complain
, NULL_TREE
);
27657 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27658 DECL_NAME (olddecl
), newtype
);
27659 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27662 if (TREE_CODE (olddecl
) == TYPE_DECL
27663 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27665 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27666 = build_template_parm_index (index
, level
, level
,
27668 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27669 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27670 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27671 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
27673 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27675 DECL_TEMPLATE_RESULT (newdecl
)
27676 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
27677 DECL_NAME (olddecl
), newtype
);
27678 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
27679 // First create a copy (ttargs) of tsubst_args with an
27680 // additional level for the template template parameter's own
27681 // template parameters (ttparms).
27682 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
27683 (DECL_TEMPLATE_PARMS (olddecl
)));
27684 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
27685 tree ttargs
= make_tree_vec (depth
+ 1);
27686 for (int i
= 0; i
< depth
; ++i
)
27687 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
27688 TREE_VEC_ELT (ttargs
, depth
)
27689 = template_parms_level_to_args (ttparms
);
27690 // Substitute ttargs into ttparms to fix references to
27691 // other template parameters.
27692 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27693 complain
|tf_partial
);
27694 // Now substitute again with args based on tparms, to reduce
27695 // the level of the ttparms.
27696 ttargs
= current_template_args ();
27697 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27699 // Finally, tack the adjusted parms onto tparms.
27700 ttparms
= tree_cons (size_int (depth
), ttparms
,
27701 current_template_parms
);
27702 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
27707 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
27709 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
27710 TREE_CODE (oldconst
),
27711 DECL_NAME (oldconst
), newtype
);
27712 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
27713 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
27714 SET_DECL_TEMPLATE_PARM_P (newconst
);
27715 newidx
= build_template_parm_index (index
, level
, level
,
27716 newconst
, newtype
);
27717 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27718 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27719 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
27725 /* Returns a C++17 class deduction guide template based on the constructor
27726 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27727 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27730 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
27732 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
27733 bool memtmpl
= false;
27736 tree fn_tmpl
= NULL_TREE
;
27741 bool copy_p
= TYPE_REF_P (type
);
27744 type
= TREE_TYPE (type
);
27745 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
27748 fparms
= void_list_node
;
27750 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
27751 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
27752 targs
= CLASSTYPE_TI_ARGS (type
);
27755 loc
= DECL_SOURCE_LOCATION (ctmpl
);
27756 explicit_p
= false;
27760 ++processing_template_decl
;
27764 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
27765 : DECL_TI_TEMPLATE (ctor
));
27767 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
27768 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
27770 type
= DECL_CONTEXT (ctor
);
27772 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
27773 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27774 fully specialized args for the enclosing class. Strip those off, as
27775 the deduction guide won't have those template parameters. */
27776 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
27777 TMPL_PARMS_DEPTH (tparms
));
27778 /* Discard the 'this' parameter. */
27779 fparms
= FUNCTION_ARG_CHAIN (ctor
);
27780 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
27781 ci
= get_constraints (ctor
);
27782 loc
= DECL_SOURCE_LOCATION (ctor
);
27783 explicit_p
= DECL_NONCONVERTING_P (ctor
);
27785 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
27789 /* For a member template constructor, we need to flatten the two
27790 template parameter lists into one, and then adjust the function
27791 signature accordingly. This gets...complicated. */
27792 tree save_parms
= current_template_parms
;
27794 /* For a member template we should have two levels of parms/args, one
27795 for the class and one for the constructor. We stripped
27796 specialized args for further enclosing classes above. */
27797 const int depth
= 2;
27798 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
27800 /* Template args for translating references to the two-level template
27801 parameters into references to the one-level template parameters we
27803 tree tsubst_args
= copy_node (targs
);
27804 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
27805 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
27807 /* Template parms for the constructor template. */
27808 tree ftparms
= TREE_VALUE (tparms
);
27809 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
27810 /* Template parms for the class template. */
27811 tparms
= TREE_CHAIN (tparms
);
27812 tree ctparms
= TREE_VALUE (tparms
);
27813 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
27814 /* Template parms for the deduction guide start as a copy of the
27815 template parms for the class. We set current_template_parms for
27816 lookup_template_class_1. */
27817 current_template_parms
= tparms
= copy_node (tparms
);
27818 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
27819 for (unsigned i
= 0; i
< clen
; ++i
)
27820 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
27822 /* Now we need to rewrite the constructor parms to append them to the
27824 for (unsigned i
= 0; i
< flen
; ++i
)
27826 unsigned index
= i
+ clen
;
27827 unsigned level
= 1;
27828 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
27829 tree olddecl
= TREE_VALUE (oldelt
);
27830 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27831 tsubst_args
, complain
);
27832 if (newdecl
== error_mark_node
)
27834 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27835 tsubst_args
, complain
, ctor
);
27836 tree list
= build_tree_list (newdef
, newdecl
);
27837 TEMPLATE_PARM_CONSTRAINTS (list
)
27838 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27839 tsubst_args
, complain
, ctor
);
27840 TREE_VEC_ELT (new_vec
, index
) = list
;
27841 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
27844 /* Now we have a final set of template parms to substitute into the
27845 function signature. */
27846 targs
= template_parms_to_args (tparms
);
27847 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
27849 if (fparms
== error_mark_node
)
27851 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
27853 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
27855 current_template_parms
= save_parms
;
27858 --processing_template_decl
;
27860 return error_mark_node
;
27865 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27866 tparms
= copy_node (tparms
);
27867 INNERMOST_TEMPLATE_PARMS (tparms
)
27868 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
27871 tree fntype
= build_function_type (type
, fparms
);
27872 tree ded_fn
= build_lang_decl_loc (loc
,
27874 dguide_name (type
), fntype
);
27875 DECL_ARGUMENTS (ded_fn
) = fargs
;
27876 DECL_ARTIFICIAL (ded_fn
) = true;
27877 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
27878 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
27879 DECL_ARTIFICIAL (ded_tmpl
) = true;
27880 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
27881 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
27882 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
27883 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
27885 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
27887 set_constraints (ded_tmpl
, ci
);
27892 /* Deduce template arguments for the class template placeholder PTYPE for
27893 template TMPL based on the initializer INIT, and return the resulting
27897 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
27898 tsubst_flags_t complain
)
27900 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
27902 /* We should have handled this in the caller. */
27903 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27905 if (complain
& tf_error
)
27906 error ("non-class template %qT used without template arguments", tmpl
);
27907 return error_mark_node
;
27909 if (init
&& TREE_TYPE (init
) == ptype
)
27910 /* Using the template parm as its own argument. */
27913 tree type
= TREE_TYPE (tmpl
);
27915 bool try_list_ctor
= false;
27917 releasing_vec rv_args
= NULL
;
27918 vec
<tree
,va_gc
> *&args
= *&rv_args
;
27919 if (init
== NULL_TREE
27920 || TREE_CODE (init
) == TREE_LIST
)
27921 args
= make_tree_vector_from_list (init
);
27922 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27924 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
27925 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
27927 /* As an exception, the first phase in 16.3.1.7 (considering the
27928 initializer list as a single argument) is omitted if the
27929 initializer list consists of a single expression of type cv U,
27930 where U is a specialization of C or a class derived from a
27931 specialization of C. */
27932 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
27933 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
27935 tree etype
= TREE_TYPE (elt
);
27936 tree tparms
= (INNERMOST_TEMPLATE_PARMS
27937 (DECL_TEMPLATE_PARMS (tmpl
)));
27938 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27939 int err
= unify (tparms
, targs
, type
, etype
,
27940 UNIFY_ALLOW_DERIVED
, /*explain*/false);
27942 try_list_ctor
= false;
27946 if (try_list_ctor
|| is_std_init_list (type
))
27947 args
= make_tree_vector_single (init
);
27949 args
= make_tree_vector_from_ctor (init
);
27952 args
= make_tree_vector_single (init
);
27954 tree dname
= dguide_name (tmpl
);
27955 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
27956 /*type*/false, /*complain*/false,
27958 bool elided
= false;
27959 if (cands
== error_mark_node
)
27962 /* Prune explicit deduction guides in copy-initialization context. */
27963 if (flags
& LOOKUP_ONLYCONVERTING
)
27965 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
27966 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27971 /* Found a nonconverting guide, prune the candidates. */
27972 tree pruned
= NULL_TREE
;
27973 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27974 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27975 pruned
= lookup_add (*iter
, pruned
);
27981 tree outer_args
= NULL_TREE
;
27982 if (DECL_CLASS_SCOPE_P (tmpl
)
27983 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
27985 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
27986 type
= TREE_TYPE (most_general_template (tmpl
));
27989 bool saw_ctor
= false;
27990 // FIXME cache artificial deduction guides
27991 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
27993 /* Skip inherited constructors. */
27994 if (iter
.using_p ())
27997 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
27998 if (guide
== error_mark_node
)
27999 return error_mark_node
;
28000 if ((flags
& LOOKUP_ONLYCONVERTING
)
28001 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
28004 cands
= lookup_add (guide
, cands
);
28009 tree call
= error_mark_node
;
28011 /* If this is list-initialization and the class has a list constructor, first
28012 try deducing from the list as a single argument, as [over.match.list]. */
28013 tree list_cands
= NULL_TREE
;
28014 if (try_list_ctor
&& cands
)
28015 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28018 if (is_list_ctor (dg
))
28019 list_cands
= lookup_add (dg
, list_cands
);
28023 ++cp_unevaluated_operand
;
28024 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28025 --cp_unevaluated_operand
;
28027 if (call
== error_mark_node
)
28029 /* That didn't work, now try treating the list as a sequence of
28031 release_tree_vector (args
);
28032 args
= make_tree_vector_from_ctor (init
);
28036 /* Maybe generate an implicit deduction guide. */
28037 if (call
== error_mark_node
&& args
->length () < 2)
28039 tree gtype
= NULL_TREE
;
28041 if (args
->length () == 1)
28042 /* Generate a copy guide. */
28043 gtype
= build_reference_type (type
);
28044 else if (!saw_ctor
)
28045 /* Generate a default guide. */
28050 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
28051 if (guide
== error_mark_node
)
28052 return error_mark_node
;
28053 cands
= lookup_add (guide
, cands
);
28057 if (elided
&& !cands
)
28059 error ("cannot deduce template arguments for copy-initialization"
28060 " of %qT, as it has no non-explicit deduction guides or "
28061 "user-declared constructors", type
);
28062 return error_mark_node
;
28064 else if (!cands
&& call
== error_mark_node
)
28066 error ("cannot deduce template arguments of %qT, as it has no viable "
28067 "deduction guides", type
);
28068 return error_mark_node
;
28071 if (call
== error_mark_node
)
28073 ++cp_unevaluated_operand
;
28074 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28075 --cp_unevaluated_operand
;
28078 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
28080 error ("class template argument deduction failed:");
28082 ++cp_unevaluated_operand
;
28083 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28084 --cp_unevaluated_operand
;
28087 inform (input_location
, "explicit deduction guides not considered "
28088 "for copy-initialization");
28091 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28094 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28095 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28096 The CONTEXT determines the context in which auto deduction is performed
28097 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28098 OUTER_TARGS are used during template argument deduction
28099 (context == adc_unify) to properly substitute the result, and is ignored
28102 For partial-concept-ids, extra args may be appended to the list of deduced
28103 template arguments prior to determining constraint satisfaction. */
28106 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28107 tsubst_flags_t complain
, auto_deduction_context context
,
28108 tree outer_targs
, int flags
)
28112 if (init
== error_mark_node
)
28113 return error_mark_node
;
28115 if (init
&& type_dependent_expression_p (init
)
28116 && context
!= adc_unify
)
28117 /* Defining a subset of type-dependent expressions that we can deduce
28118 from ahead of time isn't worth the trouble. */
28121 /* Similarly, we can't deduce from another undeduced decl. */
28122 if (init
&& undeduced_auto_decl (init
))
28125 /* We may be doing a partial substitution, but we still want to replace
28127 complain
&= ~tf_partial
;
28129 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28130 /* C++17 class template argument deduction. */
28131 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28133 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28134 /* Nothing we can do with this, even in deduction context. */
28137 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28138 with either a new invented type template parameter U or, if the
28139 initializer is a braced-init-list (8.5.4), with
28140 std::initializer_list<U>. */
28141 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28143 if (!DIRECT_LIST_INIT_P (init
))
28144 type
= listify_autos (type
, auto_node
);
28145 else if (CONSTRUCTOR_NELTS (init
) == 1)
28146 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28149 if (complain
& tf_warning_or_error
)
28151 if (permerror (input_location
, "direct-list-initialization of "
28152 "%<auto%> requires exactly one element"))
28153 inform (input_location
,
28154 "for deduction to %<std::initializer_list%>, use copy-"
28155 "list-initialization (i.e. add %<=%> before the %<{%>)");
28157 type
= listify_autos (type
, auto_node
);
28161 if (type
== error_mark_node
)
28162 return error_mark_node
;
28164 init
= resolve_nondeduced_context (init
, complain
);
28166 if (context
== adc_decomp_type
28167 && auto_node
== type
28168 && init
!= error_mark_node
28169 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
28170 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28171 and initializer has array type, deduce cv-qualified array type. */
28172 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
28174 else if (AUTO_IS_DECLTYPE (auto_node
))
28176 tree stripped_init
= tree_strip_any_location_wrapper (init
);
28177 bool id
= (DECL_P (stripped_init
)
28178 || ((TREE_CODE (init
) == COMPONENT_REF
28179 || TREE_CODE (init
) == SCOPE_REF
)
28180 && !REF_PARENTHESIZED_P (init
)));
28181 targs
= make_tree_vec (1);
28182 TREE_VEC_ELT (targs
, 0)
28183 = finish_decltype_type (init
, id
, tf_warning_or_error
);
28184 if (type
!= auto_node
)
28186 if (complain
& tf_error
)
28187 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
28188 return error_mark_node
;
28193 if (error_operand_p (init
))
28194 return error_mark_node
;
28196 tree parms
= build_tree_list (NULL_TREE
, type
);
28200 tparms
= extract_autos (type
);
28203 tparms
= make_tree_vec (1);
28204 TREE_VEC_ELT (tparms
, 0)
28205 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
28208 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28209 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28211 NULL
, /*explain_p=*/false);
28214 if (processing_template_decl
)
28215 /* Try again at instantiation time. */
28217 if (type
&& type
!= error_mark_node
28218 && (complain
& tf_error
))
28219 /* If type is error_mark_node a diagnostic must have been
28220 emitted by now. Also, having a mention to '<type error>'
28221 in the diagnostic is not really useful to the user. */
28224 && FNDECL_USED_AUTO (current_function_decl
)
28226 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
28227 && LAMBDA_FUNCTION_P (current_function_decl
))
28228 error ("unable to deduce lambda return type from %qE", init
);
28230 error ("unable to deduce %qT from %qE", type
, init
);
28231 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28233 NULL
, /*explain_p=*/true);
28235 return error_mark_node
;
28239 /* Check any placeholder constraints against the deduced type. */
28240 if (flag_concepts
&& !processing_template_decl
)
28241 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
28243 /* Use the deduced type to check the associated constraints. If we
28244 have a partial-concept-id, rebuild the argument list so that
28245 we check using the extra arguments. */
28246 check
= unpack_concept_check (check
);
28247 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
28248 tree
cdecl = TREE_OPERAND (check
, 0);
28250 cdecl = OVL_FIRST (cdecl);
28251 tree cargs
= TREE_OPERAND (check
, 1);
28252 if (TREE_VEC_LENGTH (cargs
) > 1)
28254 cargs
= copy_node (cargs
);
28255 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
28260 /* Rebuild the check using the deduced arguments. */
28261 check
= build_concept_check (cdecl, cargs
, tf_none
);
28263 if (!constraints_satisfied_p (check
))
28265 if (complain
& tf_warning_or_error
)
28267 auto_diagnostic_group d
;
28270 case adc_unspecified
:
28272 error("placeholder constraints not satisfied");
28274 case adc_variable_type
:
28275 case adc_decomp_type
:
28276 error ("deduced initializer does not satisfy "
28277 "placeholder constraints");
28279 case adc_return_type
:
28280 error ("deduced return type does not satisfy "
28281 "placeholder constraints");
28283 case adc_requirement
:
28284 error ("deduced expression type does not satisfy "
28285 "placeholder constraints");
28288 diagnose_constraints (input_location
, check
, targs
);
28290 return error_mark_node
;
28294 if (processing_template_decl
&& context
!= adc_unify
)
28295 outer_targs
= current_template_args ();
28296 targs
= add_to_template_args (outer_targs
, targs
);
28297 return tsubst (type
, targs
, complain
, NULL_TREE
);
28300 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28304 splice_late_return_type (tree type
, tree late_return_type
)
28306 if (is_auto (type
))
28308 if (late_return_type
)
28309 return late_return_type
;
28311 tree idx
= get_template_parm_index (type
);
28312 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
28313 /* In an abbreviated function template we didn't know we were dealing
28314 with a function template when we saw the auto return type, so update
28315 it to have the correct level. */
28316 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
28321 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28322 'decltype(auto)' or a deduced class template. */
28325 is_auto (const_tree type
)
28327 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
28328 && (TYPE_IDENTIFIER (type
) == auto_identifier
28329 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
28335 /* for_each_template_parm callback for type_uses_auto. */
28338 is_auto_r (tree tp
, void */
*data*/
)
28340 return is_auto (tp
);
28343 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28344 a use of `auto'. Returns NULL_TREE otherwise. */
28347 type_uses_auto (tree type
)
28349 if (type
== NULL_TREE
)
28351 else if (flag_concepts
)
28353 /* The Concepts TS allows multiple autos in one type-specifier; just
28354 return the first one we find, do_auto_deduction will collect all of
28356 if (uses_template_parms (type
))
28357 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
28358 /*visited*/NULL
, /*nondeduced*/false);
28363 return find_type_usage (type
, is_auto
);
28366 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28367 concepts are enabled, auto is acceptable in template arguments, but
28368 only when TEMPL identifies a template class. Return TRUE if any
28369 such errors were reported. */
28372 check_auto_in_tmpl_args (tree tmpl
, tree args
)
28374 /* If there were previous errors, nevermind. */
28375 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
28378 /* If TMPL is an identifier, we're parsing and we can't tell yet
28379 whether TMPL is supposed to be a type, a function or a variable.
28380 We'll only be able to tell during template substitution, so we
28381 expect to be called again then. If concepts are enabled and we
28382 know we have a type, we're ok. */
28384 && (identifier_p (tmpl
)
28386 && (DECL_TYPE_TEMPLATE_P (tmpl
)
28387 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
28390 /* Quickly search for any occurrences of auto; usually there won't
28391 be any, and then we'll avoid allocating the vector. */
28392 if (!type_uses_auto (args
))
28395 bool errors
= false;
28397 tree vec
= extract_autos (args
);
28398 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
28400 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
28401 error_at (DECL_SOURCE_LOCATION (xauto
),
28402 "invalid use of %qT in template argument", xauto
);
28409 /* For a given template T, return the vector of typedefs referenced
28410 in T for which access check is needed at T instantiation time.
28411 T is either a FUNCTION_DECL or a RECORD_TYPE.
28412 Those typedefs were added to T by the function
28413 append_type_to_template_for_access_check. */
28415 vec
<qualified_typedef_usage_t
, va_gc
> *
28416 get_types_needing_access_check (tree t
)
28419 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
28421 if (!t
|| t
== error_mark_node
)
28424 if (!(ti
= get_template_info (t
)))
28427 if (CLASS_TYPE_P (t
)
28428 || TREE_CODE (t
) == FUNCTION_DECL
)
28430 if (!TI_TEMPLATE (ti
))
28433 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
28439 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28440 tied to T. That list of typedefs will be access checked at
28441 T instantiation time.
28442 T is either a FUNCTION_DECL or a RECORD_TYPE.
28443 TYPE_DECL is a TYPE_DECL node representing a typedef.
28444 SCOPE is the scope through which TYPE_DECL is accessed.
28445 LOCATION is the location of the usage point of TYPE_DECL.
28447 This function is a subroutine of
28448 append_type_to_template_for_access_check. */
28451 append_type_to_template_for_access_check_1 (tree t
,
28454 location_t location
)
28456 qualified_typedef_usage_t typedef_usage
;
28459 if (!t
|| t
== error_mark_node
)
28462 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
28463 || CLASS_TYPE_P (t
))
28465 && TREE_CODE (type_decl
) == TYPE_DECL
28468 if (!(ti
= get_template_info (t
)))
28471 gcc_assert (TI_TEMPLATE (ti
));
28473 typedef_usage
.typedef_decl
= type_decl
;
28474 typedef_usage
.context
= scope
;
28475 typedef_usage
.locus
= location
;
28477 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
28480 /* Append TYPE_DECL to the template TEMPL.
28481 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28482 At TEMPL instanciation time, TYPE_DECL will be checked to see
28483 if it can be accessed through SCOPE.
28484 LOCATION is the location of the usage point of TYPE_DECL.
28486 e.g. consider the following code snippet:
28493 template<class U> struct S
28495 C::myint mi; // <-- usage point of the typedef C::myint
28500 At S<char> instantiation time, we need to check the access of C::myint
28501 In other words, we need to check the access of the myint typedef through
28502 the C scope. For that purpose, this function will add the myint typedef
28503 and the scope C through which its being accessed to a list of typedefs
28504 tied to the template S. That list will be walked at template instantiation
28505 time and access check performed on each typedefs it contains.
28506 Note that this particular code snippet should yield an error because
28507 myint is private to C. */
28510 append_type_to_template_for_access_check (tree templ
,
28513 location_t location
)
28515 qualified_typedef_usage_t
*iter
;
28518 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
28520 /* Make sure we don't append the type to the template twice. */
28521 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
28522 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
28525 append_type_to_template_for_access_check_1 (templ
, type_decl
,
28529 /* Recursively walk over && expressions searching for EXPR. Return a reference
28530 to that expression. */
28532 static tree
*find_template_requirement (tree
*t
, tree key
)
28536 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
28538 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
28540 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
28546 /* Convert the generic type parameters in PARM that match the types given in the
28547 range [START_IDX, END_IDX) from the current_template_parms into generic type
28551 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
28553 tree current
= current_template_parms
;
28554 int depth
= TMPL_PARMS_DEPTH (current
);
28555 current
= INNERMOST_TEMPLATE_PARMS (current
);
28556 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
28558 for (int i
= 0; i
< start_idx
; ++i
)
28559 TREE_VEC_ELT (replacement
, i
)
28560 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
28562 for (int i
= start_idx
; i
< end_idx
; ++i
)
28564 /* Create a distinct parameter pack type from the current parm and add it
28565 to the replacement args to tsubst below into the generic function
28567 tree node
= TREE_VEC_ELT (current
, i
);
28568 tree o
= TREE_TYPE (TREE_VALUE (node
));
28569 tree t
= copy_type (o
);
28570 TEMPLATE_TYPE_PARM_INDEX (t
)
28571 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
28573 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
28574 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
28575 TYPE_MAIN_VARIANT (t
) = t
;
28576 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
28577 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
28578 TREE_VEC_ELT (replacement
, i
) = t
;
28580 /* Replace the current template parameter with new pack. */
28581 TREE_VALUE (node
) = TREE_CHAIN (t
);
28583 /* Surgically adjust the associated constraint of adjusted parameter
28584 and it's corresponding contribution to the current template
28586 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
28588 tree id
= unpack_concept_check (constr
);
28589 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
28590 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
28591 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
28593 /* If there was a constraint, we also need to replace that in
28594 the template requirements, which we've already built. */
28595 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
28596 reqs
= find_template_requirement (reqs
, constr
);
28601 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
28602 TREE_VEC_ELT (replacement
, i
)
28603 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
28605 /* If there are more levels then build up the replacement with the outer
28608 replacement
= add_to_template_args (template_parms_to_args
28609 (TREE_CHAIN (current_template_parms
)),
28612 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
28615 GTY(()) tree current_failed_constraint
;
28617 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28621 declare_integer_pack (void)
28623 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
28624 build_function_type_list (integer_type_node
,
28627 NULL_TREE
, ECF_CONST
);
28628 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
28629 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
28630 CP_BUILT_IN_INTEGER_PACK
);
28633 /* Set up the hash tables for template instantiations. */
28636 init_template_processing (void)
28638 /* FIXME: enable sanitization (PR87847) */
28639 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
28640 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
28642 if (cxx_dialect
>= cxx11
)
28643 declare_integer_pack ();
28646 /* Print stats about the template hash tables for -fstats. */
28649 print_template_statistics (void)
28651 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
28652 "%f collisions\n", (long) decl_specializations
->size (),
28653 (long) decl_specializations
->elements (),
28654 decl_specializations
->collisions ());
28655 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
28656 "%f collisions\n", (long) type_specializations
->size (),
28657 (long) type_specializations
->elements (),
28658 type_specializations
->collisions ());
28663 namespace selftest
{
28665 /* Verify that build_non_dependent_expr () works, for various expressions,
28666 and that location wrappers don't affect the results. */
28669 test_build_non_dependent_expr ()
28671 location_t loc
= BUILTINS_LOCATION
;
28673 /* Verify constants, without and with location wrappers. */
28674 tree int_cst
= build_int_cst (integer_type_node
, 42);
28675 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
28677 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
28678 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
28679 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
28681 tree string_lit
= build_string (4, "foo");
28682 TREE_TYPE (string_lit
) = char_array_type_node
;
28683 string_lit
= fix_string_type (string_lit
);
28684 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
28686 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
28687 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
28688 ASSERT_EQ (wrapped_string_lit
,
28689 build_non_dependent_expr (wrapped_string_lit
));
28692 /* Verify that type_dependent_expression_p () works correctly, even
28693 in the presence of location wrapper nodes. */
28696 test_type_dependent_expression_p ()
28698 location_t loc
= BUILTINS_LOCATION
;
28700 tree name
= get_identifier ("foo");
28702 /* If no templates are involved, nothing is type-dependent. */
28703 gcc_assert (!processing_template_decl
);
28704 ASSERT_FALSE (type_dependent_expression_p (name
));
28706 ++processing_template_decl
;
28708 /* Within a template, an unresolved name is always type-dependent. */
28709 ASSERT_TRUE (type_dependent_expression_p (name
));
28711 /* Ensure it copes with NULL_TREE and errors. */
28712 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
28713 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
28715 /* A USING_DECL in a template should be type-dependent, even if wrapped
28716 with a location wrapper (PR c++/83799). */
28717 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
28718 TREE_TYPE (using_decl
) = integer_type_node
;
28719 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
28720 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
28721 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
28722 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
28724 --processing_template_decl
;
28727 /* Run all of the selftests within this file. */
28732 test_build_non_dependent_expr ();
28733 test_type_dependent_expression_p ();
28736 } // namespace selftest
28738 #endif /* #if CHECKING_P */
28740 #include "gt-cp-pt.h"