1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_nop
)
88 else if (policy
== lss_blank
|| !saved
)
89 local_specializations
= new hash_map
<tree
, tree
>;
91 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
94 local_specialization_stack::~local_specialization_stack ()
96 if (local_specializations
!= saved
)
98 delete local_specializations
;
99 local_specializations
= saved
;
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth
;
106 struct GTY((for_user
)) spec_entry
113 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
115 static hashval_t
hash (spec_entry
*);
116 static bool equal (spec_entry
*, spec_entry
*);
119 /* The general template is not in these tables. */
120 typedef hash_table
<spec_hasher
> spec_hash_table
;
121 static GTY (()) spec_hash_table
*decl_specializations
;
122 static GTY (()) spec_hash_table
*type_specializations
;
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
139 enum template_base_result
{
141 tbr_ambiguous_baseclass
,
145 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
146 unification_kind_t
, int,
148 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
149 unification_kind_t
, int, bool, bool);
150 static int unify (tree
, tree
, tree
, tree
, int, bool);
151 static void add_pending_template (tree
);
152 static tree
reopen_tinst_level (struct tinst_level
*);
153 static tree
tsubst_initializer_list (tree
, tree
);
154 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
155 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
157 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
159 static void tsubst_enum (tree
, tree
, tree
);
160 static tree
add_to_template_args (tree
, tree
);
161 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
162 static int check_non_deducible_conversion (tree
, tree
, int, int,
163 struct conversion
**, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
166 static int type_unification_real (tree
, tree
, tree
, const tree
*,
167 unsigned int, int, unification_kind_t
,
168 vec
<deferred_access_check
, va_gc
> **,
170 static void note_template_header (int);
171 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
172 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
173 static tree
convert_template_argument (tree
, tree
, tree
,
174 tsubst_flags_t
, int, tree
);
175 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
176 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
177 static tree
expand_template_argument_pack (tree
);
178 static tree
build_template_parm_index (int, int, int, tree
, tree
);
179 static bool inline_needs_template_parms (tree
, bool);
180 static void push_inline_template_parms_recursive (tree
, int);
181 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
182 static int mark_template_parm (tree
, void *);
183 static int template_parm_this_level_p (tree
, void *);
184 static tree
tsubst_friend_function (tree
, tree
);
185 static tree
tsubst_friend_class (tree
, tree
);
186 static int can_complete_type_without_circularity (tree
);
187 static tree
get_bindings (tree
, tree
, tree
, bool);
188 static int template_decl_level (tree
);
189 static int check_cv_quals_for_unify (int, tree
, tree
);
190 static int unify_pack_expansion (tree
, tree
, tree
,
191 tree
, unification_kind_t
, bool, bool);
192 static tree
copy_template_args (tree
);
193 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
194 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
195 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
196 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
198 static bool check_specialization_scope (void);
199 static tree
process_partial_specialization (tree
);
200 static void set_current_access_from_decl (tree
);
201 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
203 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
204 static bool class_nttp_const_wrapper_p (tree t
);
205 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
207 static bool template_template_parm_bindings_ok_p (tree
, tree
);
208 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
209 static tree
for_each_template_parm_r (tree
*, int *, void *);
210 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
211 static void copy_default_args_to_explicit_spec (tree
);
212 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
213 static bool dependent_template_arg_p (tree
);
214 static bool any_template_arguments_need_structural_equality_p (tree
);
215 static bool dependent_type_p_r (tree
);
216 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
218 static void perform_instantiation_time_access_checks (tree
, tree
);
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
get_underlying_template (tree
);
225 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
226 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
227 static tree
make_argument_pack (tree
);
228 static void register_parameter_specializations (tree
, tree
);
229 static tree
enclosing_instantiation_of (tree tctx
);
230 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
232 /* Make the current scope suitable for access checking when we are
233 processing T. T can be FUNCTION_DECL for instantiated function
234 template, VAR_DECL for static member variable, or TYPE_DECL for
235 alias template (needed by instantiate_decl). */
238 push_access_scope (tree t
)
240 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
241 || TREE_CODE (t
) == TYPE_DECL
);
243 if (DECL_FRIEND_CONTEXT (t
))
244 push_nested_class (DECL_FRIEND_CONTEXT (t
));
245 else if (DECL_CLASS_SCOPE_P (t
))
246 push_nested_class (DECL_CONTEXT (t
));
248 push_to_top_level ();
250 if (TREE_CODE (t
) == FUNCTION_DECL
)
252 vec_safe_push (saved_access_scope
, current_function_decl
);
253 current_function_decl
= t
;
257 /* Restore the scope set up by push_access_scope. T is the node we
261 pop_access_scope (tree t
)
263 if (TREE_CODE (t
) == FUNCTION_DECL
)
264 current_function_decl
= saved_access_scope
->pop();
266 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
269 pop_from_top_level ();
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
278 finish_member_template_decl (tree decl
)
280 if (decl
== error_mark_node
)
281 return error_mark_node
;
283 gcc_assert (DECL_P (decl
));
285 if (TREE_CODE (decl
) == TYPE_DECL
)
289 type
= TREE_TYPE (decl
);
290 if (type
== error_mark_node
)
291 return error_mark_node
;
292 if (MAYBE_CLASS_TYPE_P (type
)
293 && CLASSTYPE_TEMPLATE_INFO (type
)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
296 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
297 check_member_template (tmpl
);
302 else if (TREE_CODE (decl
) == FIELD_DECL
)
303 error_at (DECL_SOURCE_LOCATION (decl
),
304 "data member %qD cannot be a member template", decl
);
305 else if (DECL_TEMPLATE_INFO (decl
))
307 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
309 check_member_template (DECL_TI_TEMPLATE (decl
));
310 return DECL_TI_TEMPLATE (decl
);
316 error_at (DECL_SOURCE_LOCATION (decl
),
317 "invalid member template declaration %qD", decl
);
319 return error_mark_node
;
322 /* Create a template info node. */
325 build_template_info (tree template_decl
, tree template_args
)
327 tree result
= make_node (TEMPLATE_INFO
);
328 TI_TEMPLATE (result
) = template_decl
;
329 TI_ARGS (result
) = template_args
;
333 /* Return the template info node corresponding to T, whatever T is. */
336 get_template_info (const_tree t
)
338 tree tinfo
= NULL_TREE
;
340 if (!t
|| t
== error_mark_node
)
343 if (TREE_CODE (t
) == NAMESPACE_DECL
344 || TREE_CODE (t
) == PARM_DECL
)
347 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
348 tinfo
= DECL_TEMPLATE_INFO (t
);
350 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
353 if (OVERLOAD_TYPE_P (t
))
354 tinfo
= TYPE_TEMPLATE_INFO (t
);
355 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
356 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
361 /* Returns the template nesting level of the indicated class TYPE.
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
380 template_class_depth (tree type
)
384 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
386 tree tinfo
= get_template_info (type
);
388 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
394 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
397 type
= CP_DECL_CONTEXT (type
);
399 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
400 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
402 type
= CP_TYPE_CONTEXT (type
);
408 /* Return TRUE if NODE instantiates a template that has arguments of
409 its own, be it directly a primary template or indirectly through a
410 partial specializations. */
412 instantiates_primary_template_p (tree node
)
414 tree tinfo
= get_template_info (node
);
418 tree tmpl
= TI_TEMPLATE (tinfo
);
419 if (PRIMARY_TEMPLATE_P (tmpl
))
422 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
425 /* So now we know we have a specialization, but it could be a full
426 or a partial specialization. To tell which, compare the depth of
427 its template arguments with those of its context. */
429 tree ctxt
= DECL_CONTEXT (tmpl
);
430 tree ctinfo
= get_template_info (ctxt
);
434 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
435 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
438 /* Subroutine of maybe_begin_member_template_processing.
439 Returns true if processing DECL needs us to push template parms. */
442 inline_needs_template_parms (tree decl
, bool nsdmi
)
444 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
447 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
448 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
451 /* Subroutine of maybe_begin_member_template_processing.
452 Push the template parms in PARMS, starting from LEVELS steps into the
453 chain, and ending at the beginning, since template parms are listed
457 push_inline_template_parms_recursive (tree parmlist
, int levels
)
459 tree parms
= TREE_VALUE (parmlist
);
463 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
465 ++processing_template_decl
;
466 current_template_parms
467 = tree_cons (size_int (processing_template_decl
),
468 parms
, current_template_parms
);
469 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
471 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
473 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
475 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
477 if (error_operand_p (parm
))
480 gcc_assert (DECL_P (parm
));
482 switch (TREE_CODE (parm
))
490 /* Push the CONST_DECL. */
491 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
500 /* Restore the template parameter context for a member template, a
501 friend template defined in a class definition, or a non-template
502 member of template class. */
505 maybe_begin_member_template_processing (tree decl
)
509 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
513 tree ctx
= DECL_CONTEXT (decl
);
514 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
515 /* Disregard full specializations (c++/60999). */
516 && uses_template_parms (ctx
)
517 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
520 if (inline_needs_template_parms (decl
, nsdmi
))
522 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
523 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
525 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
528 parms
= TREE_CHAIN (parms
);
531 push_inline_template_parms_recursive (parms
, levels
);
534 /* Remember how many levels of template parameters we pushed so that
535 we can pop them later. */
536 inline_parm_levels
.safe_push (levels
);
539 /* Undo the effects of maybe_begin_member_template_processing. */
542 maybe_end_member_template_processing (void)
547 if (inline_parm_levels
.length () == 0)
550 last
= inline_parm_levels
.pop ();
551 for (i
= 0; i
< last
; ++i
)
553 --processing_template_decl
;
554 current_template_parms
= TREE_CHAIN (current_template_parms
);
559 /* Return a new template argument vector which contains all of ARGS,
560 but has as its innermost set of arguments the EXTRA_ARGS. */
563 add_to_template_args (tree args
, tree extra_args
)
570 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
573 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
574 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
576 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
577 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
579 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
580 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
585 /* Like add_to_template_args, but only the outermost ARGS are added to
586 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
587 (EXTRA_ARGS) levels are added. This function is used to combine
588 the template arguments from a partial instantiation with the
589 template arguments used to attain the full instantiation from the
590 partial instantiation.
592 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
595 add_outermost_template_args (tree args
, tree extra_args
)
601 if (TREE_CODE (args
) == TEMPLATE_DECL
)
603 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
607 /* If there are more levels of EXTRA_ARGS than there are ARGS,
608 something very fishy is going on. */
609 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
611 /* If *all* the new arguments will be the EXTRA_ARGS, just return
613 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
616 /* For the moment, we make ARGS look like it contains fewer levels. */
617 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
619 new_args
= add_to_template_args (args
, extra_args
);
621 /* Now, we restore ARGS to its full dimensions. */
622 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
627 /* Return the N levels of innermost template arguments from the ARGS. */
630 get_innermost_template_args (tree args
, int n
)
638 /* If N is 1, just return the innermost set of template arguments. */
640 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
642 /* If we're not removing anything, just return the arguments we were
644 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
645 gcc_assert (extra_levels
>= 0);
646 if (extra_levels
== 0)
649 /* Make a new set of arguments, not containing the outer arguments. */
650 new_args
= make_tree_vec (n
);
651 for (i
= 1; i
<= n
; ++i
)
652 SET_TMPL_ARGS_LEVEL (new_args
, i
,
653 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
658 /* The inverse of get_innermost_template_args: Return all but the innermost
659 EXTRA_LEVELS levels of template arguments from the ARGS. */
662 strip_innermost_template_args (tree args
, int extra_levels
)
665 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
670 /* If N is 1, just return the outermost set of template arguments. */
672 return TMPL_ARGS_LEVEL (args
, 1);
674 /* If we're not removing anything, just return the arguments we were
676 gcc_assert (extra_levels
>= 0);
677 if (extra_levels
== 0)
680 /* Make a new set of arguments, not containing the inner arguments. */
681 new_args
= make_tree_vec (n
);
682 for (i
= 1; i
<= n
; ++i
)
683 SET_TMPL_ARGS_LEVEL (new_args
, i
,
684 TMPL_ARGS_LEVEL (args
, i
));
689 /* We've got a template header coming up; push to a new level for storing
693 begin_template_parm_list (void)
695 /* We use a non-tag-transparent scope here, which causes pushtag to
696 put tags in this scope, rather than in the enclosing class or
697 namespace scope. This is the right thing, since we want
698 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
699 global template class, push_template_decl handles putting the
700 TEMPLATE_DECL into top-level scope. For a nested template class,
703 template <class T> struct S1 {
704 template <class T> struct S2 {};
707 pushtag contains special code to insert the TEMPLATE_DECL for S2
708 at the right scope. */
709 begin_scope (sk_template_parms
, NULL
);
710 ++processing_template_decl
;
711 ++processing_template_parmlist
;
712 note_template_header (0);
714 /* Add a dummy parameter level while we process the parameter list. */
715 current_template_parms
716 = tree_cons (size_int (processing_template_decl
),
718 current_template_parms
);
721 /* This routine is called when a specialization is declared. If it is
722 invalid to declare a specialization here, an error is reported and
723 false is returned, otherwise this routine will return true. */
726 check_specialization_scope (void)
728 tree scope
= current_scope ();
732 An explicit specialization shall be declared in the namespace of
733 which the template is a member, or, for member templates, in the
734 namespace of which the enclosing class or enclosing class
735 template is a member. An explicit specialization of a member
736 function, member class or static data member of a class template
737 shall be declared in the namespace of which the class template
739 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
741 error ("explicit specialization in non-namespace scope %qD", scope
);
747 In an explicit specialization declaration for a member of a class
748 template or a member template that appears in namespace scope,
749 the member template and some of its enclosing class templates may
750 remain unspecialized, except that the declaration shall not
751 explicitly specialize a class member template if its enclosing
752 class templates are not explicitly specialized as well. */
753 if (current_template_parms
)
755 error ("enclosing class templates are not explicitly specialized");
762 /* We've just seen template <>. */
765 begin_specialization (void)
767 begin_scope (sk_template_spec
, NULL
);
768 note_template_header (1);
769 return check_specialization_scope ();
772 /* Called at then end of processing a declaration preceded by
776 end_specialization (void)
779 reset_specialization ();
782 /* Any template <>'s that we have seen thus far are not referring to a
783 function specialization. */
786 reset_specialization (void)
788 processing_specialization
= 0;
789 template_header_count
= 0;
792 /* We've just seen a template header. If SPECIALIZATION is nonzero,
793 it was of the form template <>. */
796 note_template_header (int specialization
)
798 processing_specialization
= specialization
;
799 template_header_count
++;
802 /* We're beginning an explicit instantiation. */
805 begin_explicit_instantiation (void)
807 gcc_assert (!processing_explicit_instantiation
);
808 processing_explicit_instantiation
= true;
813 end_explicit_instantiation (void)
815 gcc_assert (processing_explicit_instantiation
);
816 processing_explicit_instantiation
= false;
819 /* An explicit specialization or partial specialization of TMPL is being
820 declared. Check that the namespace in which the specialization is
821 occurring is permissible. Returns false iff it is invalid to
822 specialize TMPL in the current namespace. */
825 check_specialization_namespace (tree tmpl
)
827 tree tpl_ns
= decl_namespace_context (tmpl
);
831 An explicit specialization shall be declared in a namespace enclosing the
832 specialized template. An explicit specialization whose declarator-id is
833 not qualified shall be declared in the nearest enclosing namespace of the
834 template, or, if the namespace is inline (7.3.1), any namespace from its
835 enclosing namespace set. */
836 if (current_scope() != DECL_CONTEXT (tmpl
)
837 && !at_namespace_scope_p ())
839 error ("specialization of %qD must appear at namespace scope", tmpl
);
843 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
844 /* Same or enclosing namespace. */
848 auto_diagnostic_group d
;
849 if (permerror (input_location
,
850 "specialization of %qD in different namespace", tmpl
))
851 inform (DECL_SOURCE_LOCATION (tmpl
),
852 " from definition of %q#D", tmpl
);
857 /* SPEC is an explicit instantiation. Check that it is valid to
858 perform this explicit instantiation in the current namespace. */
861 check_explicit_instantiation_namespace (tree spec
)
865 /* DR 275: An explicit instantiation shall appear in an enclosing
866 namespace of its template. */
867 ns
= decl_namespace_context (spec
);
868 if (!is_nested_namespace (current_namespace
, ns
))
869 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
870 "(which does not enclose namespace %qD)",
871 spec
, current_namespace
, ns
);
874 /* Returns the type of a template specialization only if that
875 specialization needs to be defined. Otherwise (e.g., if the type has
876 already been defined), the function returns NULL_TREE. */
879 maybe_new_partial_specialization (tree type
)
881 /* An implicit instantiation of an incomplete type implies
882 the definition of a new class template.
890 Here, S<T*> is an implicit instantiation of S whose type
892 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
895 /* It can also be the case that TYPE is a completed specialization.
896 Continuing the previous example, suppose we also declare:
902 Here, S<T*> refers to the specialization S<T*> defined
903 above. However, we need to differentiate definitions because
904 we intend to define a new partial specialization. In this case,
905 we rely on the fact that the constraints are different for
906 this declaration than that above.
908 Note that we also get here for injected class names and
909 late-parsed template definitions. We must ensure that we
910 do not create new type declarations for those cases. */
911 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
913 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
914 tree args
= CLASSTYPE_TI_ARGS (type
);
916 /* If there are no template parameters, this cannot be a new
917 partial template specialization? */
918 if (!current_template_parms
)
921 /* The injected-class-name is not a new partial specialization. */
922 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
925 /* If the constraints are not the same as those of the primary
926 then, we can probably create a new specialization. */
927 tree type_constr
= current_template_constraints ();
929 if (type
== TREE_TYPE (tmpl
))
931 tree main_constr
= get_constraints (tmpl
);
932 if (equivalent_constraints (type_constr
, main_constr
))
936 /* Also, if there's a pre-existing specialization with matching
937 constraints, then this also isn't new. */
938 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
941 tree spec_tmpl
= TREE_VALUE (specs
);
942 tree spec_args
= TREE_PURPOSE (specs
);
943 tree spec_constr
= get_constraints (spec_tmpl
);
944 if (comp_template_args (args
, spec_args
)
945 && equivalent_constraints (type_constr
, spec_constr
))
947 specs
= TREE_CHAIN (specs
);
950 /* Create a new type node (and corresponding type decl)
951 for the newly declared specialization. */
952 tree t
= make_class_type (TREE_CODE (type
));
953 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
954 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
956 /* We only need a separate type node for storing the definition of this
957 partial specialization; uses of S<T*> are unconstrained, so all are
958 equivalent. So keep TYPE_CANONICAL the same. */
959 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
961 /* Build the corresponding type decl. */
962 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
963 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
964 DECL_SOURCE_LOCATION (d
) = input_location
;
965 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
966 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
974 /* The TYPE is being declared. If it is a template type, that means it
975 is a partial specialization. Do appropriate error-checking. */
978 maybe_process_partial_specialization (tree type
)
982 if (type
== error_mark_node
)
983 return error_mark_node
;
985 /* A lambda that appears in specialization context is not itself a
987 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
990 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
992 error ("name of class shadows template template parameter %qD",
994 return error_mark_node
;
997 context
= TYPE_CONTEXT (type
);
999 if (TYPE_ALIAS_P (type
))
1001 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1003 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1004 error ("specialization of alias template %qD",
1005 TI_TEMPLATE (tinfo
));
1007 error ("explicit specialization of non-template %qT", type
);
1008 return error_mark_node
;
1010 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1012 /* This is for ordinary explicit specialization and partial
1013 specialization of a template class such as:
1015 template <> class C<int>;
1019 template <class T> class C<T*>;
1021 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1023 if (tree t
= maybe_new_partial_specialization (type
))
1025 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1026 && !at_namespace_scope_p ())
1027 return error_mark_node
;
1028 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1029 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1030 if (processing_template_decl
)
1032 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1033 if (decl
== error_mark_node
)
1034 return error_mark_node
;
1035 return TREE_TYPE (decl
);
1038 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1039 error ("specialization of %qT after instantiation", type
);
1040 else if (errorcount
&& !processing_specialization
1041 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1042 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1043 /* Trying to define a specialization either without a template<> header
1044 or in an inappropriate place. We've already given an error, so just
1045 bail now so we don't actually define the specialization. */
1046 return error_mark_node
;
1048 else if (CLASS_TYPE_P (type
)
1049 && !CLASSTYPE_USE_TEMPLATE (type
)
1050 && CLASSTYPE_TEMPLATE_INFO (type
)
1051 && context
&& CLASS_TYPE_P (context
)
1052 && CLASSTYPE_TEMPLATE_INFO (context
))
1054 /* This is for an explicit specialization of member class
1055 template according to [temp.expl.spec/18]:
1057 template <> template <class U> class C<int>::D;
1059 The context `C<int>' must be an implicit instantiation.
1060 Otherwise this is just a member class template declared
1063 template <> class C<int> { template <class U> class D; };
1064 template <> template <class U> class C<int>::D;
1066 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1067 while in the second case, `C<int>::D' is a primary template
1068 and `C<T>::D' may not exist. */
1070 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1071 && !COMPLETE_TYPE_P (type
))
1074 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1076 if (current_namespace
1077 != decl_namespace_context (tmpl
))
1079 if (permerror (input_location
,
1080 "specialization of %qD in different namespace",
1082 inform (DECL_SOURCE_LOCATION (tmpl
),
1083 "from definition of %q#D", tmpl
);
1086 /* Check for invalid specialization after instantiation:
1088 template <> template <> class C<int>::D<int>;
1089 template <> template <class U> class C<int>::D; */
1091 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1092 t
; t
= TREE_CHAIN (t
))
1094 tree inst
= TREE_VALUE (t
);
1095 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1096 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1098 /* We already have a full specialization of this partial
1099 instantiation, or a full specialization has been
1100 looked up but not instantiated. Reassign it to the
1101 new member specialization template. */
1105 elt
.tmpl
= most_general_template (tmpl
);
1106 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1109 type_specializations
->remove_elt (&elt
);
1112 CLASSTYPE_TI_ARGS (inst
)
1113 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1116 = type_specializations
->find_slot (&elt
, INSERT
);
1117 entry
= ggc_alloc
<spec_entry
> ();
1122 /* But if we've had an implicit instantiation, that's a
1123 problem ([temp.expl.spec]/6). */
1124 error ("specialization %qT after instantiation %qT",
1128 /* Mark TYPE as a specialization. And as a result, we only
1129 have one level of template argument for the innermost
1131 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1132 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1133 CLASSTYPE_TI_ARGS (type
)
1134 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1137 else if (processing_specialization
)
1139 /* Someday C++0x may allow for enum template specialization. */
1140 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1141 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1142 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1143 "of %qD not allowed by ISO C++", type
);
1146 error ("explicit specialization of non-template %qT", type
);
1147 return error_mark_node
;
1154 /* Returns nonzero if we can optimize the retrieval of specializations
1155 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1156 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1159 optimize_specialization_lookup_p (tree tmpl
)
1161 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1162 && DECL_CLASS_SCOPE_P (tmpl
)
1163 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1165 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1166 /* The optimized lookup depends on the fact that the
1167 template arguments for the member function template apply
1168 purely to the containing class, which is not true if the
1169 containing class is an explicit or partial
1171 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1172 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1173 && !DECL_CONV_FN_P (tmpl
)
1174 /* It is possible to have a template that is not a member
1175 template and is not a member of a template class:
1177 template <typename T>
1178 struct S { friend A::f(); };
1180 Here, the friend function is a template, but the context does
1181 not have template information. The optimized lookup relies
1182 on having ARGS be the template arguments for both the class
1183 and the function template. */
1184 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1187 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1188 gone through coerce_template_parms by now. */
1191 verify_unstripped_args_1 (tree inner
)
1193 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1195 tree arg
= TREE_VEC_ELT (inner
, i
);
1196 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1198 else if (TYPE_P (arg
))
1199 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1200 else if (ARGUMENT_PACK_P (arg
))
1201 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1202 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1203 /* Allow typedefs on the type of a non-type argument, since a
1204 parameter can have them. */;
1206 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1211 verify_unstripped_args (tree args
)
1213 ++processing_template_decl
;
1214 if (!any_dependent_template_arguments_p (args
))
1215 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1216 --processing_template_decl
;
1219 /* Retrieve the specialization (in the sense of [temp.spec] - a
1220 specialization is either an instantiation or an explicit
1221 specialization) of TMPL for the given template ARGS. If there is
1222 no such specialization, return NULL_TREE. The ARGS are a vector of
1223 arguments, or a vector of vectors of arguments, in the case of
1224 templates with more than one level of parameters.
1226 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1227 then we search for a partial specialization matching ARGS. This
1228 parameter is ignored if TMPL is not a class template.
1230 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1231 result is a NONTYPE_ARGUMENT_PACK. */
1234 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1236 if (tmpl
== NULL_TREE
)
1239 if (args
== error_mark_node
)
1242 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1243 || TREE_CODE (tmpl
) == FIELD_DECL
);
1245 /* There should be as many levels of arguments as there are
1246 levels of parameters. */
1247 gcc_assert (TMPL_ARGS_DEPTH (args
)
1248 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1249 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1250 : template_class_depth (DECL_CONTEXT (tmpl
))));
1253 verify_unstripped_args (args
);
1255 /* Lambda functions in templates aren't instantiated normally, but through
1256 tsubst_lambda_expr. */
1257 if (lambda_fn_in_template_p (tmpl
))
1260 if (optimize_specialization_lookup_p (tmpl
))
1262 /* The template arguments actually apply to the containing
1263 class. Find the class specialization with those
1265 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1266 tree class_specialization
1267 = retrieve_specialization (class_template
, args
, 0);
1268 if (!class_specialization
)
1271 /* Find the instance of TMPL. */
1272 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1273 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1276 if (tree ti
= get_template_info (fn
))
1277 if (TI_TEMPLATE (ti
) == tmpl
1278 /* using-declarations can bring in a different
1279 instantiation of tmpl as a member of a different
1280 instantiation of tmpl's class. We don't want those
1282 && DECL_CONTEXT (fn
) == class_specialization
)
1291 spec_hash_table
*specializations
;
1295 elt
.spec
= NULL_TREE
;
1297 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1298 specializations
= type_specializations
;
1300 specializations
= decl_specializations
;
1303 hash
= spec_hasher::hash (&elt
);
1304 found
= specializations
->find_with_hash (&elt
, hash
);
1312 /* Like retrieve_specialization, but for local declarations. */
1315 retrieve_local_specialization (tree tmpl
)
1317 if (local_specializations
== NULL
)
1320 tree
*slot
= local_specializations
->get (tmpl
);
1321 return slot
? *slot
: NULL_TREE
;
1324 /* Returns nonzero iff DECL is a specialization of TMPL. */
1327 is_specialization_of (tree decl
, tree tmpl
)
1331 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1335 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1341 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1343 for (t
= TREE_TYPE (decl
);
1345 t
= CLASSTYPE_USE_TEMPLATE (t
)
1346 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1347 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1354 /* Returns nonzero iff DECL is a specialization of friend declaration
1355 FRIEND_DECL according to [temp.friend]. */
1358 is_specialization_of_friend (tree decl
, tree friend_decl
)
1360 bool need_template
= true;
1363 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1364 || TREE_CODE (decl
) == TYPE_DECL
);
1366 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1367 of a template class, we want to check if DECL is a specialization
1369 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1370 && DECL_TEMPLATE_INFO (friend_decl
)
1371 && !DECL_USE_TEMPLATE (friend_decl
))
1373 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1374 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1375 need_template
= false;
1377 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1378 && !PRIMARY_TEMPLATE_P (friend_decl
))
1379 need_template
= false;
1381 /* There is nothing to do if this is not a template friend. */
1382 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1385 if (is_specialization_of (decl
, friend_decl
))
1389 A member of a class template may be declared to be a friend of a
1390 non-template class. In this case, the corresponding member of
1391 every specialization of the class template is a friend of the
1392 class granting friendship.
1394 For example, given a template friend declaration
1396 template <class T> friend void A<T>::f();
1398 the member function below is considered a friend
1400 template <> struct A<int> {
1404 For this type of template friend, TEMPLATE_DEPTH below will be
1405 nonzero. To determine if DECL is a friend of FRIEND, we first
1406 check if the enclosing class is a specialization of another. */
1408 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1410 && DECL_CLASS_SCOPE_P (decl
)
1411 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1412 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1414 /* Next, we check the members themselves. In order to handle
1415 a few tricky cases, such as when FRIEND_DECL's are
1417 template <class T> friend void A<T>::g(T t);
1418 template <class T> template <T t> friend void A<T>::h();
1422 void A<int>::g(int);
1423 template <int> void A<int>::h();
1425 we need to figure out ARGS, the template arguments from
1426 the context of DECL. This is required for template substitution
1427 of `T' in the function parameter of `g' and template parameter
1428 of `h' in the above examples. Here ARGS corresponds to `int'. */
1430 tree context
= DECL_CONTEXT (decl
);
1431 tree args
= NULL_TREE
;
1432 int current_depth
= 0;
1434 while (current_depth
< template_depth
)
1436 if (CLASSTYPE_TEMPLATE_INFO (context
))
1438 if (current_depth
== 0)
1439 args
= TYPE_TI_ARGS (context
);
1441 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1444 context
= TYPE_CONTEXT (context
);
1447 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1452 tree friend_args_type
;
1453 tree decl_args_type
;
1455 /* Make sure that both DECL and FRIEND_DECL are templates or
1457 is_template
= DECL_TEMPLATE_INFO (decl
)
1458 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1459 if (need_template
^ is_template
)
1461 else if (is_template
)
1463 /* If both are templates, check template parameter list. */
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1467 if (!comp_template_parms
1468 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1472 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1475 decl_type
= TREE_TYPE (decl
);
1477 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1478 tf_none
, NULL_TREE
);
1479 if (friend_type
== error_mark_node
)
1482 /* Check if return types match. */
1483 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1486 /* Check if function parameter types match, ignoring the
1487 `this' parameter. */
1488 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1489 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1491 friend_args_type
= TREE_CHAIN (friend_args_type
);
1492 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1493 decl_args_type
= TREE_CHAIN (decl_args_type
);
1495 return compparms (decl_args_type
, friend_args_type
);
1499 /* DECL is a TYPE_DECL */
1501 tree decl_type
= TREE_TYPE (decl
);
1503 /* Make sure that both DECL and FRIEND_DECL are templates or
1506 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1507 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1509 if (need_template
^ is_template
)
1511 else if (is_template
)
1514 /* If both are templates, check the name of the two
1515 TEMPLATE_DECL's first because is_friend didn't. */
1516 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1517 != DECL_NAME (friend_decl
))
1520 /* Now check template parameter list. */
1522 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1524 return comp_template_parms
1525 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1529 return (DECL_NAME (decl
)
1530 == DECL_NAME (friend_decl
));
1536 /* Register the specialization SPEC as a specialization of TMPL with
1537 the indicated ARGS. IS_FRIEND indicates whether the specialization
1538 is actually just a friend declaration. ATTRLIST is the list of
1539 attributes that the specialization is declared with or NULL when
1540 it isn't. Returns SPEC, or an equivalent prior declaration, if
1543 We also store instantiations of field packs in the hash table, even
1544 though they are not themselves templates, to make lookup easier. */
1547 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1551 spec_entry
**slot
= NULL
;
1554 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1555 || (TREE_CODE (tmpl
) == FIELD_DECL
1556 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1558 if (TREE_CODE (spec
) == FUNCTION_DECL
1559 && uses_template_parms (DECL_TI_ARGS (spec
)))
1560 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1561 register it; we want the corresponding TEMPLATE_DECL instead.
1562 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1563 the more obvious `uses_template_parms (spec)' to avoid problems
1564 with default function arguments. In particular, given
1565 something like this:
1567 template <class T> void f(T t1, T t = T())
1569 the default argument expression is not substituted for in an
1570 instantiation unless and until it is actually needed. */
1573 if (optimize_specialization_lookup_p (tmpl
))
1574 /* We don't put these specializations in the hash table, but we might
1575 want to give an error about a mismatch. */
1576 fn
= retrieve_specialization (tmpl
, args
, 0);
1584 hash
= spec_hasher::hash (&elt
);
1586 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1593 /* We can sometimes try to re-register a specialization that we've
1594 already got. In particular, regenerate_decl_from_template calls
1595 duplicate_decls which will update the specialization list. But,
1596 we'll still get called again here anyhow. It's more convenient
1597 to simply allow this than to try to prevent it. */
1600 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1602 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1604 if (DECL_ODR_USED (fn
)
1605 || DECL_EXPLICIT_INSTANTIATION (fn
))
1607 error ("specialization of %qD after instantiation",
1609 return error_mark_node
;
1614 /* This situation should occur only if the first
1615 specialization is an implicit instantiation, the
1616 second is an explicit specialization, and the
1617 implicit instantiation has not yet been used. That
1618 situation can occur if we have implicitly
1619 instantiated a member function and then specialized
1622 We can also wind up here if a friend declaration that
1623 looked like an instantiation turns out to be a
1626 template <class T> void foo(T);
1627 class S { friend void foo<>(int) };
1628 template <> void foo(int);
1630 We transform the existing DECL in place so that any
1631 pointers to it become pointers to the updated
1634 If there was a definition for the template, but not
1635 for the specialization, we want this to look as if
1636 there were no definition, and vice versa. */
1637 DECL_INITIAL (fn
) = NULL_TREE
;
1638 duplicate_decls (spec
, fn
, is_friend
);
1639 /* The call to duplicate_decls will have applied
1642 An explicit specialization of a function template
1643 is inline only if it is explicitly declared to be,
1644 and independently of whether its function template
1647 to the primary function; now copy the inline bits to
1648 the various clones. */
1649 FOR_EACH_CLONE (clone
, fn
)
1651 DECL_DECLARED_INLINE_P (clone
)
1652 = DECL_DECLARED_INLINE_P (fn
);
1653 DECL_SOURCE_LOCATION (clone
)
1654 = DECL_SOURCE_LOCATION (fn
);
1655 DECL_DELETED_FN (clone
)
1656 = DECL_DELETED_FN (fn
);
1658 check_specialization_namespace (tmpl
);
1663 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1665 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1666 if (dd
== error_mark_node
)
1667 /* We've already complained in duplicate_decls. */
1668 return error_mark_node
;
1670 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1671 /* Dup decl failed, but this is a new definition. Set the
1672 line number so any errors match this new
1674 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1680 return duplicate_decls (spec
, fn
, is_friend
);
1682 /* A specialization must be declared in the same namespace as the
1683 template it is specializing. */
1684 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1685 && !check_specialization_namespace (tmpl
))
1686 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1688 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1690 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1691 gcc_assert (tmpl
&& args
&& spec
);
1694 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1695 && PRIMARY_TEMPLATE_P (tmpl
)
1696 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1697 || variable_template_p (tmpl
))
1698 /* If TMPL is a forward declaration of a template function, keep a list
1699 of all specializations in case we need to reassign them to a friend
1700 template later in tsubst_friend_function.
1702 Also keep a list of all variable template instantiations so that
1703 process_partial_specialization can check whether a later partial
1704 specialization would have used it. */
1705 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1706 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1712 /* Returns true iff two spec_entry nodes are equivalent. */
1714 int comparing_specializations
;
1717 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1721 ++comparing_specializations
;
1722 equal
= (e1
->tmpl
== e2
->tmpl
1723 && comp_template_args (e1
->args
, e2
->args
));
1724 if (equal
&& flag_concepts
1725 /* tmpl could be a FIELD_DECL for a capture pack. */
1726 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1727 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1728 && uses_template_parms (e1
->args
))
1730 /* Partial specializations of a variable template can be distinguished by
1732 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1733 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1734 equal
= equivalent_constraints (c1
, c2
);
1736 --comparing_specializations
;
1741 /* Returns a hash for a template TMPL and template arguments ARGS. */
1744 hash_tmpl_and_args (tree tmpl
, tree args
)
1746 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1747 return iterative_hash_template_arg (args
, val
);
1750 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1754 spec_hasher::hash (spec_entry
*e
)
1756 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1759 /* Recursively calculate a hash value for a template argument ARG, for use
1760 in the hash tables of template specializations. We must be
1761 careful to (at least) skip the same entities template_args_equal
1765 iterative_hash_template_arg (tree arg
, hashval_t val
)
1767 if (arg
== NULL_TREE
)
1768 return iterative_hash_object (arg
, val
);
1771 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1772 while (CONVERT_EXPR_P (arg
)
1773 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1774 || class_nttp_const_wrapper_p (arg
))
1775 arg
= TREE_OPERAND (arg
, 0);
1777 enum tree_code code
= TREE_CODE (arg
);
1779 val
= iterative_hash_object (code
, val
);
1783 case ARGUMENT_PACK_SELECT
:
1789 case IDENTIFIER_NODE
:
1790 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1793 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1794 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1797 case TYPE_PACK_EXPANSION
:
1798 case EXPR_PACK_EXPANSION
:
1799 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1800 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1802 case TYPE_ARGUMENT_PACK
:
1803 case NONTYPE_ARGUMENT_PACK
:
1804 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1807 for (; arg
; arg
= TREE_CHAIN (arg
))
1808 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1812 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1813 val
= iterative_hash_template_arg (*iter
, val
);
1820 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1821 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1823 val
= iterative_hash_template_arg (field
, val
);
1824 val
= iterative_hash_template_arg (value
, val
);
1830 if (!DECL_ARTIFICIAL (arg
))
1832 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1833 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1835 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1838 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1841 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1842 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1844 case TEMPLATE_PARM_INDEX
:
1845 val
= iterative_hash_template_arg
1846 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1847 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1848 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1851 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1852 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1853 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1856 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1858 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1862 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1863 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1864 val
= iterative_hash_object (code
, val
);
1865 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1868 /* [temp.over.link] Two lambda-expressions are never considered
1871 So just hash the closure type. */
1872 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1875 case IMPLICIT_CONV_EXPR
:
1876 case STATIC_CAST_EXPR
:
1877 case REINTERPRET_CAST_EXPR
:
1878 case CONST_CAST_EXPR
:
1879 case DYNAMIC_CAST_EXPR
:
1881 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1882 /* Now hash operands as usual. */
1887 tree fn
= CALL_EXPR_FN (arg
);
1888 if (tree name
= dependent_name (fn
))
1890 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1891 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1894 val
= iterative_hash_template_arg (fn
, val
);
1895 call_expr_arg_iterator ai
;
1896 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1897 x
= next_call_expr_arg (&ai
))
1898 val
= iterative_hash_template_arg (x
, val
);
1906 char tclass
= TREE_CODE_CLASS (code
);
1910 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1912 // We want an alias specialization that survived strip_typedefs
1913 // to hash differently from its TYPE_CANONICAL, to avoid hash
1914 // collisions that compare as different in template_args_equal.
1915 // These could be dependent specializations that strip_typedefs
1916 // left alone, or untouched specializations because
1917 // coerce_template_parms returns the unconverted template
1918 // arguments if it sees incomplete argument packs.
1919 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1920 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1923 switch (TREE_CODE (arg
))
1925 case TEMPLATE_TEMPLATE_PARM
:
1927 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1929 /* Do not recurse with TPI directly, as that is unbounded
1931 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1932 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1937 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1941 if (tree canonical
= TYPE_CANONICAL (arg
))
1942 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1948 case tcc_declaration
:
1950 return iterative_hash_expr (arg
, val
);
1953 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1954 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1955 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1963 /* Unregister the specialization SPEC as a specialization of TMPL.
1964 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1965 if the SPEC was listed as a specialization of TMPL.
1967 Note that SPEC has been ggc_freed, so we can't look inside it. */
1970 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1975 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1976 elt
.args
= TI_ARGS (tinfo
);
1977 elt
.spec
= NULL_TREE
;
1979 entry
= decl_specializations
->find (&elt
);
1982 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1983 gcc_assert (new_spec
!= NULL_TREE
);
1984 entry
->spec
= new_spec
;
1991 /* Like register_specialization, but for local declarations. We are
1992 registering SPEC, an instantiation of TMPL. */
1995 register_local_specialization (tree spec
, tree tmpl
)
1997 gcc_assert (tmpl
!= spec
);
1998 local_specializations
->put (tmpl
, spec
);
2001 /* TYPE is a class type. Returns true if TYPE is an explicitly
2002 specialized class. */
2005 explicit_class_specialization_p (tree type
)
2007 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2009 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2012 /* Print the list of functions at FNS, going through all the overloads
2013 for each element of the list. Alternatively, FNS cannot be a
2014 TREE_LIST, in which case it will be printed together with all the
2017 MORE and *STR should respectively be FALSE and NULL when the function
2018 is called from the outside. They are used internally on recursive
2019 calls. print_candidates manages the two parameters and leaves NULL
2020 in *STR when it ends. */
2023 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2025 if (TREE_CODE (fns
) == TREE_LIST
)
2026 for (; fns
; fns
= TREE_CHAIN (fns
))
2027 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2029 for (lkp_iterator
iter (fns
); iter
;)
2034 const char *pfx
= *str
;
2038 pfx
= _("candidates are:");
2040 pfx
= _("candidate is:");
2041 *str
= get_spaces (pfx
);
2043 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2047 /* Print the list of candidate FNS in an error message. FNS can also
2048 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2051 print_candidates (tree fns
)
2054 print_candidates_1 (fns
, &str
);
2058 /* Get a (possibly) constrained template declaration for the
2059 purpose of ordering candidates. */
2061 get_template_for_ordering (tree list
)
2063 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2064 tree f
= TREE_VALUE (list
);
2065 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2066 return TI_TEMPLATE (ti
);
2070 /* Among candidates having the same signature, return the
2071 most constrained or NULL_TREE if there is no best candidate.
2072 If the signatures of candidates vary (e.g., template
2073 specialization vs. member function), then there can be no
2076 Note that we don't compare constraints on the functions
2077 themselves, but rather those of their templates. */
2079 most_constrained_function (tree candidates
)
2081 // Try to find the best candidate in a first pass.
2082 tree champ
= candidates
;
2083 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2085 int winner
= more_constrained (get_template_for_ordering (champ
),
2086 get_template_for_ordering (c
));
2088 champ
= c
; // The candidate is more constrained
2089 else if (winner
== 0)
2090 return NULL_TREE
; // Neither is more constrained
2093 // Verify that the champ is better than previous candidates.
2094 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2095 if (!more_constrained (get_template_for_ordering (champ
),
2096 get_template_for_ordering (c
)))
2104 /* Returns the template (one of the functions given by TEMPLATE_ID)
2105 which can be specialized to match the indicated DECL with the
2106 explicit template args given in TEMPLATE_ID. The DECL may be
2107 NULL_TREE if none is available. In that case, the functions in
2108 TEMPLATE_ID are non-members.
2110 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2111 specialization of a member template.
2113 The TEMPLATE_COUNT is the number of references to qualifying
2114 template classes that appeared in the name of the function. See
2115 check_explicit_specialization for a more accurate description.
2117 TSK indicates what kind of template declaration (if any) is being
2118 declared. TSK_TEMPLATE indicates that the declaration given by
2119 DECL, though a FUNCTION_DECL, has template parameters, and is
2120 therefore a template function.
2122 The template args (those explicitly specified and those deduced)
2123 are output in a newly created vector *TARGS_OUT.
2125 If it is impossible to determine the result, an error message is
2126 issued. The error_mark_node is returned to indicate failure. */
2129 determine_specialization (tree template_id
,
2132 int need_member_template
,
2138 tree explicit_targs
;
2139 tree candidates
= NULL_TREE
;
2141 /* A TREE_LIST of templates of which DECL may be a specialization.
2142 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2143 corresponding TREE_PURPOSE is the set of template arguments that,
2144 when used to instantiate the template, would produce a function
2145 with the signature of DECL. */
2146 tree templates
= NULL_TREE
;
2148 cp_binding_level
*b
;
2150 *targs_out
= NULL_TREE
;
2152 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2153 return error_mark_node
;
2155 /* We shouldn't be specializing a member template of an
2156 unspecialized class template; we already gave an error in
2157 check_specialization_scope, now avoid crashing. */
2159 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2160 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2162 gcc_assert (errorcount
);
2163 return error_mark_node
;
2166 fns
= TREE_OPERAND (template_id
, 0);
2167 explicit_targs
= TREE_OPERAND (template_id
, 1);
2169 if (fns
== error_mark_node
)
2170 return error_mark_node
;
2172 /* Check for baselinks. */
2173 if (BASELINK_P (fns
))
2174 fns
= BASELINK_FUNCTIONS (fns
);
2176 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2178 error_at (DECL_SOURCE_LOCATION (decl
),
2179 "%qD is not a function template", fns
);
2180 return error_mark_node
;
2182 else if (VAR_P (decl
) && !variable_template_p (fns
))
2184 error ("%qD is not a variable template", fns
);
2185 return error_mark_node
;
2188 /* Count the number of template headers specified for this
2191 for (b
= current_binding_level
;
2192 b
->kind
== sk_template_parms
;
2196 tree orig_fns
= fns
;
2198 if (variable_template_p (fns
))
2200 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2201 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2202 tf_warning_or_error
,
2203 /*req_all*/true, /*use_defarg*/true);
2204 if (targs
!= error_mark_node
)
2205 templates
= tree_cons (targs
, fns
, templates
);
2207 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2211 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2213 tree decl_arg_types
;
2217 /* In case of explicit specialization, we need to check if
2218 the number of template headers appearing in the specialization
2219 is correct. This is usually done in check_explicit_specialization,
2220 but the check done there cannot be exhaustive when specializing
2221 member functions. Consider the following code:
2223 template <> void A<int>::f(int);
2224 template <> template <> void A<int>::f(int);
2226 Assuming that A<int> is not itself an explicit specialization
2227 already, the first line specializes "f" which is a non-template
2228 member function, whilst the second line specializes "f" which
2229 is a template member function. So both lines are syntactically
2230 correct, and check_explicit_specialization does not reject
2233 Here, we can do better, as we are matching the specialization
2234 against the declarations. We count the number of template
2235 headers, and we check if they match TEMPLATE_COUNT + 1
2236 (TEMPLATE_COUNT is the number of qualifying template classes,
2237 plus there must be another header for the member template
2240 Notice that if header_count is zero, this is not a
2241 specialization but rather a template instantiation, so there
2242 is no check we can perform here. */
2243 if (header_count
&& header_count
!= template_count
+ 1)
2246 /* Check that the number of template arguments at the
2247 innermost level for DECL is the same as for FN. */
2248 if (current_binding_level
->kind
== sk_template_parms
2249 && !current_binding_level
->explicit_spec_p
2250 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2251 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2252 (current_template_parms
))))
2255 /* DECL might be a specialization of FN. */
2256 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2257 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2259 /* For a non-static member function, we need to make sure
2260 that the const qualification is the same. Since
2261 get_bindings does not try to merge the "this" parameter,
2262 we must do the comparison explicitly. */
2263 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2265 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2266 TREE_VALUE (decl_arg_types
)))
2269 /* And the ref-qualification. */
2270 if (type_memfn_rqual (TREE_TYPE (decl
))
2271 != type_memfn_rqual (TREE_TYPE (fn
)))
2275 /* Skip the "this" parameter and, for constructors of
2276 classes with virtual bases, the VTT parameter. A
2277 full specialization of a constructor will have a VTT
2278 parameter, but a template never will. */
2280 = skip_artificial_parms_for (decl
, decl_arg_types
);
2282 = skip_artificial_parms_for (fn
, fn_arg_types
);
2284 /* Function templates cannot be specializations; there are
2285 no partial specializations of functions. Therefore, if
2286 the type of DECL does not match FN, there is no
2289 Note that it should never be the case that we have both
2290 candidates added here, and for regular member functions
2292 if (tsk
== tsk_template
)
2294 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2295 current_template_parms
))
2297 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2298 TREE_TYPE (TREE_TYPE (fn
))))
2300 if (!compparms (fn_arg_types
, decl_arg_types
))
2303 tree freq
= get_trailing_function_requirements (fn
);
2304 tree dreq
= get_trailing_function_requirements (decl
);
2309 tree fargs
= DECL_TI_ARGS (fn
);
2310 tsubst_flags_t complain
= tf_none
;
2311 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2312 if (!cp_tree_equal (freq
, dreq
))
2316 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2320 /* See whether this function might be a specialization of this
2321 template. Suppress access control because we might be trying
2322 to make this specialization a friend, and we have already done
2323 access control for the declaration of the specialization. */
2324 push_deferring_access_checks (dk_no_check
);
2325 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2326 pop_deferring_access_checks ();
2329 /* We cannot deduce template arguments that when used to
2330 specialize TMPL will produce DECL. */
2333 if (uses_template_parms (targs
))
2334 /* We deduced something involving 'auto', which isn't a valid
2335 template argument. */
2338 /* Remove, from the set of candidates, all those functions
2339 whose constraints are not satisfied. */
2340 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2343 // Then, try to form the new function type.
2344 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2345 if (insttype
== error_mark_node
)
2348 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2349 if (!compparms (fn_arg_types
, decl_arg_types
))
2352 /* Save this template, and the arguments deduced. */
2353 templates
= tree_cons (targs
, fn
, templates
);
2355 else if (need_member_template
)
2356 /* FN is an ordinary member function, and we need a
2357 specialization of a member template. */
2359 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2360 /* We can get IDENTIFIER_NODEs here in certain erroneous
2363 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2364 /* This is just an ordinary non-member function. Nothing can
2365 be a specialization of that. */
2367 else if (DECL_ARTIFICIAL (fn
))
2368 /* Cannot specialize functions that are created implicitly. */
2372 tree decl_arg_types
;
2374 /* This is an ordinary member function. However, since
2375 we're here, we can assume its enclosing class is a
2376 template class. For example,
2378 template <typename T> struct S { void f(); };
2379 template <> void S<int>::f() {}
2381 Here, S<int>::f is a non-template, but S<int> is a
2382 template class. If FN has the same type as DECL, we
2383 might be in business. */
2385 if (!DECL_TEMPLATE_INFO (fn
))
2386 /* Its enclosing class is an explicit specialization
2387 of a template class. This is not a candidate. */
2390 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2391 TREE_TYPE (TREE_TYPE (fn
))))
2392 /* The return types differ. */
2395 /* Adjust the type of DECL in case FN is a static member. */
2396 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2397 if (DECL_STATIC_FUNCTION_P (fn
)
2398 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2399 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2401 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2405 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2406 && (type_memfn_rqual (TREE_TYPE (decl
))
2407 != type_memfn_rqual (TREE_TYPE (fn
))))
2410 // If the deduced arguments do not satisfy the constraints,
2411 // this is not a candidate.
2412 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2415 // Add the candidate.
2416 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2420 if (templates
&& TREE_CHAIN (templates
))
2426 It is possible for a specialization with a given function
2427 signature to be instantiated from more than one function
2428 template. In such cases, explicit specification of the
2429 template arguments must be used to uniquely identify the
2430 function template specialization being specialized.
2432 Note that here, there's no suggestion that we're supposed to
2433 determine which of the candidate templates is most
2434 specialized. However, we, also have:
2438 Partial ordering of overloaded function template
2439 declarations is used in the following contexts to select
2440 the function template to which a function template
2441 specialization refers:
2443 -- when an explicit specialization refers to a function
2446 So, we do use the partial ordering rules, at least for now.
2447 This extension can only serve to make invalid programs valid,
2448 so it's safe. And, there is strong anecdotal evidence that
2449 the committee intended the partial ordering rules to apply;
2450 the EDG front end has that behavior, and John Spicer claims
2451 that the committee simply forgot to delete the wording in
2452 [temp.expl.spec]. */
2453 tree tmpl
= most_specialized_instantiation (templates
);
2454 if (tmpl
!= error_mark_node
)
2457 TREE_CHAIN (templates
) = NULL_TREE
;
2461 // Concepts allows multiple declarations of member functions
2462 // with the same signature. Like above, we need to rely on
2463 // on the partial ordering of those candidates to determine which
2465 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2467 if (tree cand
= most_constrained_function (candidates
))
2470 TREE_CHAIN (cand
) = NULL_TREE
;
2474 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2476 error ("template-id %qD for %q+D does not match any template "
2477 "declaration", template_id
, decl
);
2478 if (header_count
&& header_count
!= template_count
+ 1)
2479 inform (DECL_SOURCE_LOCATION (decl
),
2480 "saw %d %<template<>%>, need %d for "
2481 "specializing a member function template",
2482 header_count
, template_count
+ 1);
2484 print_candidates (orig_fns
);
2485 return error_mark_node
;
2487 else if ((templates
&& TREE_CHAIN (templates
))
2488 || (candidates
&& TREE_CHAIN (candidates
))
2489 || (templates
&& candidates
))
2491 error ("ambiguous template specialization %qD for %q+D",
2493 candidates
= chainon (candidates
, templates
);
2494 print_candidates (candidates
);
2495 return error_mark_node
;
2498 /* We have one, and exactly one, match. */
2501 tree fn
= TREE_VALUE (candidates
);
2502 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2504 /* Propagate the candidate's constraints to the declaration. */
2505 if (tsk
!= tsk_template
)
2506 set_constraints (decl
, get_constraints (fn
));
2508 /* DECL is a re-declaration or partial instantiation of a template
2510 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2512 /* It was a specialization of an ordinary member function in a
2514 return DECL_TI_TEMPLATE (fn
);
2517 /* It was a specialization of a template. */
2518 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2519 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2521 *targs_out
= copy_node (targs
);
2522 SET_TMPL_ARGS_LEVEL (*targs_out
,
2523 TMPL_ARGS_DEPTH (*targs_out
),
2524 TREE_PURPOSE (templates
));
2527 *targs_out
= TREE_PURPOSE (templates
);
2528 return TREE_VALUE (templates
);
2531 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2532 but with the default argument values filled in from those in the
2536 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2539 tree new_spec_types
;
2544 if (spec_types
== void_list_node
)
2545 return void_list_node
;
2547 /* Substitute into the rest of the list. */
2549 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2550 TREE_CHAIN (tmpl_types
));
2552 /* Add the default argument for this parameter. */
2553 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2554 TREE_VALUE (spec_types
),
2558 /* DECL is an explicit specialization. Replicate default arguments
2559 from the template it specializes. (That way, code like:
2561 template <class T> void f(T = 3);
2562 template <> void f(double);
2565 works, as required.) An alternative approach would be to look up
2566 the correct default arguments at the call-site, but this approach
2567 is consistent with how implicit instantiations are handled. */
2570 copy_default_args_to_explicit_spec (tree decl
)
2575 tree new_spec_types
;
2579 tree object_type
= NULL_TREE
;
2580 tree in_charge
= NULL_TREE
;
2581 tree vtt
= NULL_TREE
;
2583 /* See if there's anything we need to do. */
2584 tmpl
= DECL_TI_TEMPLATE (decl
);
2585 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2586 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2587 if (TREE_PURPOSE (t
))
2592 old_type
= TREE_TYPE (decl
);
2593 spec_types
= TYPE_ARG_TYPES (old_type
);
2595 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2597 /* Remove the this pointer, but remember the object's type for
2599 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2600 spec_types
= TREE_CHAIN (spec_types
);
2601 tmpl_types
= TREE_CHAIN (tmpl_types
);
2603 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2605 /* DECL may contain more parameters than TMPL due to the extra
2606 in-charge parameter in constructors and destructors. */
2607 in_charge
= spec_types
;
2608 spec_types
= TREE_CHAIN (spec_types
);
2610 if (DECL_HAS_VTT_PARM_P (decl
))
2613 spec_types
= TREE_CHAIN (spec_types
);
2617 /* Compute the merged default arguments. */
2619 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2621 /* Compute the new FUNCTION_TYPE. */
2625 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2630 /* Put the in-charge parameter back. */
2631 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2632 TREE_VALUE (in_charge
),
2635 new_type
= build_method_type_directly (object_type
,
2636 TREE_TYPE (old_type
),
2640 new_type
= build_function_type (TREE_TYPE (old_type
),
2642 new_type
= cp_build_type_attribute_variant (new_type
,
2643 TYPE_ATTRIBUTES (old_type
));
2644 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2646 TREE_TYPE (decl
) = new_type
;
2649 /* Return the number of template headers we expect to see for a definition
2650 or specialization of CTYPE or one of its non-template members. */
2653 num_template_headers_for_class (tree ctype
)
2655 int num_templates
= 0;
2657 while (ctype
&& CLASS_TYPE_P (ctype
))
2659 /* You're supposed to have one `template <...>' for every
2660 template class, but you don't need one for a full
2661 specialization. For example:
2663 template <class T> struct S{};
2664 template <> struct S<int> { void f(); };
2665 void S<int>::f () {}
2667 is correct; there shouldn't be a `template <>' for the
2668 definition of `S<int>::f'. */
2669 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2670 /* If CTYPE does not have template information of any
2671 kind, then it is not a template, nor is it nested
2672 within a template. */
2674 if (explicit_class_specialization_p (ctype
))
2676 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2679 ctype
= TYPE_CONTEXT (ctype
);
2682 return num_templates
;
2685 /* Do a simple sanity check on the template headers that precede the
2686 variable declaration DECL. */
2689 check_template_variable (tree decl
)
2691 tree ctx
= CP_DECL_CONTEXT (decl
);
2692 int wanted
= num_template_headers_for_class (ctx
);
2693 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2694 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2696 if (cxx_dialect
< cxx14
)
2697 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2698 "variable templates only available with "
2699 "%<-std=c++14%> or %<-std=gnu++14%>");
2701 // Namespace-scope variable templates should have a template header.
2704 if (template_header_count
> wanted
)
2706 auto_diagnostic_group d
;
2707 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2708 "too many template headers for %qD "
2711 if (warned
&& CLASS_TYPE_P (ctx
)
2712 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2713 inform (DECL_SOURCE_LOCATION (decl
),
2714 "members of an explicitly specialized class are defined "
2715 "without a template header");
2719 /* An explicit specialization whose declarator-id or class-head-name is not
2720 qualified shall be declared in the nearest enclosing namespace of the
2721 template, or, if the namespace is inline (7.3.1), any namespace from its
2722 enclosing namespace set.
2724 If the name declared in the explicit instantiation is an unqualified name,
2725 the explicit instantiation shall appear in the namespace where its template
2726 is declared or, if that namespace is inline (7.3.1), any namespace from its
2727 enclosing namespace set. */
2730 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2732 tree tmpl
= most_general_template (t
);
2733 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2734 && !is_nested_namespace (current_namespace
,
2735 CP_DECL_CONTEXT (tmpl
), true))
2737 if (processing_specialization
)
2738 permerror (loc
, "explicit specialization of %qD outside its "
2739 "namespace must use a nested-name-specifier", tmpl
);
2740 else if (processing_explicit_instantiation
2741 && cxx_dialect
>= cxx11
)
2742 /* This was allowed in C++98, so only pedwarn. */
2743 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2744 "outside its namespace must use a nested-name-"
2749 /* Warn for a template specialization SPEC that is missing some of a set
2750 of function or type attributes that the template TEMPL is declared with.
2751 ATTRLIST is a list of additional attributes that SPEC should be taken
2752 to ultimately be declared with. */
2755 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2757 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2758 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2760 /* Avoid warning if the difference between the primary and
2761 the specialization is not in one of the attributes below. */
2762 const char* const blacklist
[] = {
2763 "alloc_align", "alloc_size", "assume_aligned", "format",
2764 "format_arg", "malloc", "nonnull", NULL
2767 /* Put together a list of the black listed attributes that the primary
2768 template is declared with that the specialization is not, in case
2769 it's not apparent from the most recent declaration of the primary. */
2771 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2777 auto_diagnostic_group d
;
2778 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2779 "explicit specialization %q#D may be missing attributes",
2781 inform (DECL_SOURCE_LOCATION (tmpl
),
2783 ? G_("missing primary template attributes %s")
2784 : G_("missing primary template attribute %s"),
2785 pp_formatted_text (&str
));
2788 /* Check to see if the function just declared, as indicated in
2789 DECLARATOR, and in DECL, is a specialization of a function
2790 template. We may also discover that the declaration is an explicit
2791 instantiation at this point.
2793 Returns DECL, or an equivalent declaration that should be used
2794 instead if all goes well. Issues an error message if something is
2795 amiss. Returns error_mark_node if the error is not easily
2798 FLAGS is a bitmask consisting of the following flags:
2800 2: The function has a definition.
2801 4: The function is a friend.
2803 The TEMPLATE_COUNT is the number of references to qualifying
2804 template classes that appeared in the name of the function. For
2807 template <class T> struct S { void f(); };
2810 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2811 classes are not counted in the TEMPLATE_COUNT, so that in
2813 template <class T> struct S {};
2814 template <> struct S<int> { void f(); }
2815 template <> void S<int>::f();
2817 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2818 invalid; there should be no template <>.)
2820 If the function is a specialization, it is marked as such via
2821 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2822 is set up correctly, and it is added to the list of specializations
2823 for that template. */
2826 check_explicit_specialization (tree declarator
,
2832 int have_def
= flags
& 2;
2833 int is_friend
= flags
& 4;
2834 bool is_concept
= flags
& 8;
2835 int specialization
= 0;
2836 int explicit_instantiation
= 0;
2837 int member_specialization
= 0;
2838 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2839 tree dname
= DECL_NAME (decl
);
2844 if (!processing_specialization
)
2847 tsk
= tsk_excessive_parms
;
2850 tsk
= current_tmpl_spec_kind (template_count
);
2855 if (processing_specialization
&& !VAR_P (decl
))
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2860 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2863 /* This could be something like:
2865 template <class T> void f(T);
2866 class S { friend void f<>(int); } */
2870 /* This case handles bogus declarations like template <>
2871 template <class T> void f<int>(); */
2873 error_at (cp_expr_loc_or_input_loc (declarator
),
2874 "template-id %qE in declaration of primary template",
2881 case tsk_invalid_member_spec
:
2882 /* The error has already been reported in
2883 check_specialization_scope. */
2884 return error_mark_node
;
2886 case tsk_invalid_expl_inst
:
2887 error ("template parameter list used in explicit instantiation");
2893 error ("definition provided for explicit instantiation");
2895 explicit_instantiation
= 1;
2898 case tsk_excessive_parms
:
2899 case tsk_insufficient_parms
:
2900 if (tsk
== tsk_excessive_parms
)
2901 error ("too many template parameter lists in declaration of %qD",
2903 else if (template_header_count
)
2904 error("too few template parameter lists in declaration of %qD", decl
);
2906 error("explicit specialization of %qD must be introduced by "
2907 "%<template <>%>", decl
);
2912 error ("explicit specialization declared %<concept%>");
2914 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2915 /* In cases like template<> constexpr bool v = true;
2916 We'll give an error in check_template_variable. */
2919 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2921 member_specialization
= 1;
2927 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2929 /* This case handles bogus declarations like template <>
2930 template <class T> void f<int>(); */
2932 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2933 error_at (cp_expr_loc_or_input_loc (declarator
),
2934 "template-id %qE in declaration of primary template",
2936 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2938 /* Partial specialization of variable template. */
2939 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2943 else if (cxx_dialect
< cxx14
)
2944 error_at (cp_expr_loc_or_input_loc (declarator
),
2945 "non-type partial specialization %qE "
2946 "is not allowed", declarator
);
2948 error_at (cp_expr_loc_or_input_loc (declarator
),
2949 "non-class, non-variable partial specialization %qE "
2950 "is not allowed", declarator
);
2955 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2956 /* This is a specialization of a member template, without
2957 specialization the containing class. Something like:
2959 template <class T> struct S {
2960 template <class U> void f (U);
2962 template <> template <class U> void S<int>::f(U) {}
2964 That's a specialization -- but of the entire template. */
2972 if ((specialization
|| member_specialization
)
2973 /* This doesn't apply to variable templates. */
2974 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2976 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2977 for (; t
; t
= TREE_CHAIN (t
))
2978 if (TREE_PURPOSE (t
))
2980 permerror (input_location
,
2981 "default argument specified in explicit specialization");
2986 if (specialization
|| member_specialization
|| explicit_instantiation
)
2988 tree tmpl
= NULL_TREE
;
2989 tree targs
= NULL_TREE
;
2990 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2992 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2993 if (!was_template_id
)
2997 gcc_assert (identifier_p (declarator
));
3002 /* If there is no class context, the explicit instantiation
3003 must be at namespace scope. */
3004 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3006 /* Find the namespace binding, using the declaration
3008 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3009 LOOK_want::NORMAL
, true);
3010 if (fns
== error_mark_node
)
3011 /* If lookup fails, look for a friend declaration so we can
3012 give a better diagnostic. */
3013 fns
= (lookup_qualified_name
3014 (CP_DECL_CONTEXT (decl
), dname
,
3015 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3018 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3020 error ("%qD is not a template function", dname
);
3021 fns
= error_mark_node
;
3025 declarator
= lookup_template_function (fns
, NULL_TREE
);
3028 if (declarator
== error_mark_node
)
3029 return error_mark_node
;
3031 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3033 if (!explicit_instantiation
)
3034 /* A specialization in class scope. This is invalid,
3035 but the error will already have been flagged by
3036 check_specialization_scope. */
3037 return error_mark_node
;
3040 /* It's not valid to write an explicit instantiation in
3043 class C { template void f(); }
3045 This case is caught by the parser. However, on
3048 template class C { void f(); };
3050 (which is invalid) we can get here. The error will be
3057 else if (ctype
!= NULL_TREE
3058 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3060 // We'll match variable templates in start_decl.
3064 /* Find the list of functions in ctype that have the same
3065 name as the declared function. */
3066 tree name
= TREE_OPERAND (declarator
, 0);
3068 if (constructor_name_p (name
, ctype
))
3070 if (DECL_CONSTRUCTOR_P (decl
)
3071 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3072 : !CLASSTYPE_DESTRUCTOR (ctype
))
3074 /* From [temp.expl.spec]:
3076 If such an explicit specialization for the member
3077 of a class template names an implicitly-declared
3078 special member function (clause _special_), the
3079 program is ill-formed.
3081 Similar language is found in [temp.explicit]. */
3082 error ("specialization of implicitly-declared special member function");
3083 return error_mark_node
;
3086 name
= DECL_NAME (decl
);
3089 /* For a type-conversion operator, We might be looking for
3090 `operator int' which will be a specialization of
3091 `operator T'. Grab all the conversion operators, and
3092 then select from them. */
3093 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3094 ? conv_op_identifier
: name
);
3096 if (fns
== NULL_TREE
)
3098 error ("no member function %qD declared in %qT", name
, ctype
);
3099 return error_mark_node
;
3102 TREE_OPERAND (declarator
, 0) = fns
;
3105 /* Figure out what exactly is being specialized at this point.
3106 Note that for an explicit instantiation, even one for a
3107 member function, we cannot tell a priori whether the
3108 instantiation is for a member template, or just a member
3109 function of a template class. Even if a member template is
3110 being instantiated, the member template arguments may be
3111 elided if they can be deduced from the rest of the
3113 tmpl
= determine_specialization (declarator
, decl
,
3115 member_specialization
,
3119 if (!tmpl
|| tmpl
== error_mark_node
)
3120 /* We couldn't figure out what this declaration was
3122 return error_mark_node
;
3125 if (TREE_CODE (decl
) == FUNCTION_DECL
3126 && DECL_HIDDEN_FRIEND_P (tmpl
))
3128 auto_diagnostic_group d
;
3129 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3130 "friend declaration %qD is not visible to "
3131 "explicit specialization", tmpl
))
3132 inform (DECL_SOURCE_LOCATION (tmpl
),
3133 "friend declaration here");
3135 else if (!ctype
&& !is_friend
3136 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3137 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3139 tree gen_tmpl
= most_general_template (tmpl
);
3141 if (explicit_instantiation
)
3143 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3144 is done by do_decl_instantiation later. */
3146 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3147 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3149 if (arg_depth
> parm_depth
)
3151 /* If TMPL is not the most general template (for
3152 example, if TMPL is a friend template that is
3153 injected into namespace scope), then there will
3154 be too many levels of TARGS. Remove some of them
3159 new_targs
= make_tree_vec (parm_depth
);
3160 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3161 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3162 = TREE_VEC_ELT (targs
, i
);
3166 return instantiate_template (tmpl
, targs
, tf_error
);
3169 /* If we thought that the DECL was a member function, but it
3170 turns out to be specializing a static member function,
3171 make DECL a static member function as well. */
3172 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3173 && DECL_STATIC_FUNCTION_P (tmpl
)
3174 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3175 revert_static_member_fn (decl
);
3177 /* If this is a specialization of a member template of a
3178 template class, we want to return the TEMPLATE_DECL, not
3179 the specialization of it. */
3180 if (tsk
== tsk_template
&& !was_template_id
)
3182 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3183 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3184 DECL_INITIAL (result
) = NULL_TREE
;
3188 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3189 DECL_SOURCE_LOCATION (result
)
3190 = DECL_SOURCE_LOCATION (decl
);
3191 /* We want to use the argument list specified in the
3192 definition, not in the original declaration. */
3193 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3194 for (parm
= DECL_ARGUMENTS (result
); parm
;
3195 parm
= DECL_CHAIN (parm
))
3196 DECL_CONTEXT (parm
) = result
;
3198 return register_specialization (tmpl
, gen_tmpl
, targs
,
3202 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3203 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3205 if (was_template_id
)
3206 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3208 /* Inherit default function arguments from the template
3209 DECL is specializing. */
3210 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3211 copy_default_args_to_explicit_spec (decl
);
3213 /* This specialization has the same protection as the
3214 template it specializes. */
3215 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3216 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3218 /* 7.1.1-1 [dcl.stc]
3220 A storage-class-specifier shall not be specified in an
3221 explicit specialization...
3223 The parser rejects these, so unless action is taken here,
3224 explicit function specializations will always appear with
3227 The action recommended by the C++ CWG in response to C++
3228 defect report 605 is to make the storage class and linkage
3229 of the explicit specialization match the templated function:
3231 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3233 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3235 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3236 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3238 /* A concept cannot be specialized. */
3239 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3241 error ("explicit specialization of function concept %qD",
3243 return error_mark_node
;
3246 /* This specialization has the same linkage and visibility as
3247 the function template it specializes. */
3248 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3249 if (! TREE_PUBLIC (decl
))
3251 DECL_INTERFACE_KNOWN (decl
) = 1;
3252 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3254 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3255 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3257 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3258 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3262 /* If DECL is a friend declaration, declared using an
3263 unqualified name, the namespace associated with DECL may
3264 have been set incorrectly. For example, in:
3266 template <typename T> void f(T);
3268 struct S { friend void f<int>(int); }
3271 we will have set the DECL_CONTEXT for the friend
3272 declaration to N, rather than to the global namespace. */
3273 if (DECL_NAMESPACE_SCOPE_P (decl
))
3274 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3276 if (is_friend
&& !have_def
)
3277 /* This is not really a declaration of a specialization.
3278 It's just the name of an instantiation. But, it's not
3279 a request for an instantiation, either. */
3280 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3281 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3282 /* A specialization is not necessarily COMDAT. */
3283 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3284 && DECL_DECLARED_INLINE_P (decl
));
3285 else if (VAR_P (decl
))
3286 DECL_COMDAT (decl
) = false;
3288 /* If this is a full specialization, register it so that we can find
3289 it again. Partial specializations will be registered in
3290 process_partial_specialization. */
3291 if (!processing_template_decl
)
3293 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3295 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3300 /* A 'structor should already have clones. */
3301 gcc_assert (decl
== error_mark_node
3302 || variable_template_p (tmpl
)
3303 || !(DECL_CONSTRUCTOR_P (decl
)
3304 || DECL_DESTRUCTOR_P (decl
))
3305 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3312 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3313 parameters. These are represented in the same format used for
3314 DECL_TEMPLATE_PARMS. */
3317 comp_template_parms (const_tree parms1
, const_tree parms2
)
3322 if (parms1
== parms2
)
3325 for (p1
= parms1
, p2
= parms2
;
3326 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3327 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3329 tree t1
= TREE_VALUE (p1
);
3330 tree t2
= TREE_VALUE (p2
);
3333 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3334 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3336 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3339 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3341 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3342 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3344 /* If either of the template parameters are invalid, assume
3345 they match for the sake of error recovery. */
3346 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3349 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3352 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3353 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3354 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3356 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3361 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3362 /* One set of parameters has more parameters lists than the
3369 /* Returns true if two template parameters are declared with
3370 equivalent constraints. */
3373 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3375 tree req1
= TREE_TYPE (parm1
);
3376 tree req2
= TREE_TYPE (parm2
);
3380 return cp_tree_equal (req1
, req2
);
3384 /* Returns true when two template parameters are equivalent. */
3387 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3389 tree decl1
= TREE_VALUE (parm1
);
3390 tree decl2
= TREE_VALUE (parm2
);
3392 /* If either of the template parameters are invalid, assume
3393 they match for the sake of error recovery. */
3394 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3397 /* ... they declare parameters of the same kind. */
3398 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3401 /* ... one parameter was introduced by a parameter declaration, then
3402 both are. This case arises as a result of eagerly rewriting declarations
3404 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3407 /* ... if either declares a pack, they both do. */
3408 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3411 if (TREE_CODE (decl1
) == PARM_DECL
)
3413 /* ... if they declare non-type parameters, the types are equivalent. */
3414 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3417 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3419 /* ... if they declare template template parameters, their template
3420 parameter lists are equivalent. */
3421 if (!template_heads_equivalent_p (decl1
, decl2
))
3425 /* ... if they are declared with a qualified-concept name, they both
3426 are, and those names are equivalent. */
3427 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3430 /* Returns true if two template parameters lists are equivalent.
3431 Two template parameter lists are equivalent if they have the
3432 same length and their corresponding parameters are equivalent.
3434 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3435 data structure returned by DECL_TEMPLATE_PARMS.
3437 This is generally the same implementation as comp_template_parms
3438 except that it also the concept names and arguments used to
3439 introduce parameters. */
3442 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3444 if (parms1
== parms2
)
3447 const_tree p1
= parms1
;
3448 const_tree p2
= parms2
;
3449 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3451 tree list1
= TREE_VALUE (p1
);
3452 tree list2
= TREE_VALUE (p2
);
3454 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3457 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3459 tree parm1
= TREE_VEC_ELT (list1
, i
);
3460 tree parm2
= TREE_VEC_ELT (list2
, i
);
3461 if (!template_parameters_equivalent_p (parm1
, parm2
))
3465 p1
= TREE_CHAIN (p1
);
3466 p2
= TREE_CHAIN (p2
);
3469 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3475 /* Return true if the requires-clause of the template parameter lists are
3476 equivalent and false otherwise. */
3478 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3480 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3481 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3482 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3484 if (!cp_tree_equal (req1
, req2
))
3489 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3490 Two template heads are equivalent if their template parameter
3491 lists are equivalent and their requires clauses are equivalent.
3493 In pre-C++20, this is equivalent to calling comp_template_parms
3494 for the template parameters of TMPL1 and TMPL2. */
3497 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3499 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3500 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3502 /* Don't change the matching rules for pre-C++20. */
3503 if (cxx_dialect
< cxx20
)
3504 return comp_template_parms (parms1
, parms2
);
3506 /* ... have the same number of template parameters, and their
3507 corresponding parameters are equivalent. */
3508 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3511 /* ... if either has a requires-clause, they both do and their
3512 corresponding constraint-expressions are equivalent. */
3513 return template_requirements_equivalent_p (parms1
, parms2
);
3516 /* Determine whether PARM is a parameter pack. */
3519 template_parameter_pack_p (const_tree parm
)
3521 /* Determine if we have a non-type template parameter pack. */
3522 if (TREE_CODE (parm
) == PARM_DECL
)
3523 return (DECL_TEMPLATE_PARM_P (parm
)
3524 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3525 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3526 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3528 /* If this is a list of template parameters, we could get a
3529 TYPE_DECL or a TEMPLATE_DECL. */
3530 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3531 parm
= TREE_TYPE (parm
);
3533 /* Otherwise it must be a type template parameter. */
3534 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3535 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3536 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3539 /* Determine if T is a function parameter pack. */
3542 function_parameter_pack_p (const_tree t
)
3544 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3545 return DECL_PACK_P (t
);
3549 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3550 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3553 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3555 if (! primary_func_tmpl_inst
3556 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3557 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3560 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3563 /* Return true iff the function parameter PARAM_DECL was expanded
3564 from the function parameter pack PACK. */
3567 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3569 if (DECL_ARTIFICIAL (param_decl
)
3570 || !function_parameter_pack_p (pack
))
3573 /* The parameter pack and its pack arguments have the same
3575 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3578 /* Determine whether ARGS describes a variadic template args list,
3579 i.e., one that is terminated by a template argument pack. */
3582 template_args_variadic_p (tree args
)
3587 if (args
== NULL_TREE
)
3590 args
= INNERMOST_TEMPLATE_ARGS (args
);
3591 nargs
= TREE_VEC_LENGTH (args
);
3596 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3598 return ARGUMENT_PACK_P (last_parm
);
3601 /* Generate a new name for the parameter pack name NAME (an
3602 IDENTIFIER_NODE) that incorporates its */
3605 make_ith_pack_parameter_name (tree name
, int i
)
3607 /* Munge the name to include the parameter index. */
3608 #define NUMBUF_LEN 128
3609 char numbuf
[NUMBUF_LEN
];
3613 if (name
== NULL_TREE
)
3615 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3616 newname_len
= IDENTIFIER_LENGTH (name
)
3617 + strlen (numbuf
) + 2;
3618 newname
= (char*)alloca (newname_len
);
3619 snprintf (newname
, newname_len
,
3620 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3621 return get_identifier (newname
);
3624 /* Return true if T is a primary function, class or alias template
3625 specialization, not including the template pattern. */
3628 primary_template_specialization_p (const_tree t
)
3633 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3634 return (DECL_LANG_SPECIFIC (t
)
3635 && DECL_USE_TEMPLATE (t
)
3636 && DECL_TEMPLATE_INFO (t
)
3637 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3638 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3639 return (CLASSTYPE_TEMPLATE_INFO (t
)
3640 && CLASSTYPE_USE_TEMPLATE (t
)
3641 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3642 else if (alias_template_specialization_p (t
, nt_transparent
))
3647 /* Return true if PARM is a template template parameter. */
3650 template_template_parameter_p (const_tree parm
)
3652 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3655 /* Return true iff PARM is a DECL representing a type template
3659 template_type_parameter_p (const_tree parm
)
3662 && (TREE_CODE (parm
) == TYPE_DECL
3663 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3664 && DECL_TEMPLATE_PARM_P (parm
));
3667 /* Return the template parameters of T if T is a
3668 primary template instantiation, NULL otherwise. */
3671 get_primary_template_innermost_parameters (const_tree t
)
3673 tree parms
= NULL
, template_info
= NULL
;
3675 if ((template_info
= get_template_info (t
))
3676 && primary_template_specialization_p (t
))
3677 parms
= INNERMOST_TEMPLATE_PARMS
3678 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3683 /* Return the template parameters of the LEVELth level from the full list
3684 of template parameters PARMS. */
3687 get_template_parms_at_level (tree parms
, int level
)
3691 || TREE_CODE (parms
) != TREE_LIST
3692 || level
> TMPL_PARMS_DEPTH (parms
))
3695 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3696 if (TMPL_PARMS_DEPTH (p
) == level
)
3702 /* Returns the template arguments of T if T is a template instantiation,
3706 get_template_innermost_arguments (const_tree t
)
3708 tree args
= NULL
, template_info
= NULL
;
3710 if ((template_info
= get_template_info (t
))
3711 && TI_ARGS (template_info
))
3712 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3717 /* Return the argument pack elements of T if T is a template argument pack,
3721 get_template_argument_pack_elems (const_tree t
)
3723 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3724 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3727 return ARGUMENT_PACK_ARGS (t
);
3730 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3731 ARGUMENT_PACK_SELECT represents. */
3734 argument_pack_select_arg (tree t
)
3736 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3737 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3739 /* If the selected argument is an expansion E, that most likely means we were
3740 called from gen_elem_of_pack_expansion_instantiation during the
3741 substituting of an argument pack (of which the Ith element is a pack
3742 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3743 In this case, the Ith element resulting from this substituting is going to
3744 be a pack expansion, which pattern is the pattern of E. Let's return the
3745 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3746 resulting pack expansion from it. */
3747 if (PACK_EXPANSION_P (arg
))
3749 /* Make sure we aren't throwing away arg info. */
3750 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3751 arg
= PACK_EXPANSION_PATTERN (arg
);
3758 /* True iff FN is a function representing a built-in variadic parameter
3762 builtin_pack_fn_p (tree fn
)
3765 || TREE_CODE (fn
) != FUNCTION_DECL
3766 || !DECL_IS_BUILTIN (fn
))
3769 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3775 /* True iff CALL is a call to a function representing a built-in variadic
3779 builtin_pack_call_p (tree call
)
3781 if (TREE_CODE (call
) != CALL_EXPR
)
3783 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3786 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3789 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3792 tree ohi
= CALL_EXPR_ARG (call
, 0);
3793 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3794 false/*fn*/, true/*int_cst*/);
3796 if (value_dependent_expression_p (hi
))
3800 call
= copy_node (call
);
3801 CALL_EXPR_ARG (call
, 0) = hi
;
3803 tree ex
= make_pack_expansion (call
, complain
);
3804 tree vec
= make_tree_vec (1);
3805 TREE_VEC_ELT (vec
, 0) = ex
;
3810 hi
= cxx_constant_value (hi
);
3811 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3813 /* Calculate the largest value of len that won't make the size of the vec
3814 overflow an int. The compiler will exceed resource limits long before
3815 this, but it seems a decent place to diagnose. */
3816 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3818 if (len
< 0 || len
> max
)
3820 if ((complain
& tf_error
)
3821 && hi
!= error_mark_node
)
3822 error ("argument to %<__integer_pack%> must be between 0 and %d",
3824 return error_mark_node
;
3827 tree vec
= make_tree_vec (len
);
3829 for (int i
= 0; i
< len
; ++i
)
3830 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3836 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3840 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3843 if (!builtin_pack_call_p (call
))
3846 tree fn
= CALL_EXPR_FN (call
);
3848 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3849 return expand_integer_pack (call
, args
, complain
, in_decl
);
3854 /* Structure used to track the progress of find_parameter_packs_r. */
3855 struct find_parameter_pack_data
3857 /* TREE_LIST that will contain all of the parameter packs found by
3859 tree
* parameter_packs
;
3861 /* Set of AST nodes that have been visited by the traversal. */
3862 hash_set
<tree
> *visited
;
3864 /* True iff we're making a type pack expansion. */
3865 bool type_pack_expansion_p
;
3868 /* Identifies all of the argument packs that occur in a template
3869 argument and appends them to the TREE_LIST inside DATA, which is a
3870 find_parameter_pack_data structure. This is a subroutine of
3871 make_pack_expansion and uses_parameter_packs. */
3873 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3876 struct find_parameter_pack_data
* ppd
=
3877 (struct find_parameter_pack_data
*)data
;
3878 bool parameter_pack_p
= false;
3880 /* Don't look through typedefs; we are interested in whether a
3881 parameter pack is actually written in the expression/type we're
3882 looking at, not the target type. */
3883 if (TYPE_P (t
) && typedef_variant_p (t
))
3885 /* But do look at arguments for an alias template. */
3886 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3887 cp_walk_tree (&TI_ARGS (tinfo
),
3888 &find_parameter_packs_r
,
3894 /* Identify whether this is a parameter pack or not. */
3895 switch (TREE_CODE (t
))
3897 case TEMPLATE_PARM_INDEX
:
3898 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3899 parameter_pack_p
= true;
3902 case TEMPLATE_TYPE_PARM
:
3903 t
= TYPE_MAIN_VARIANT (t
);
3905 case TEMPLATE_TEMPLATE_PARM
:
3906 /* If the placeholder appears in the decl-specifier-seq of a function
3907 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3908 is a pack expansion, the invented template parameter is a template
3910 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3911 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3912 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3913 parameter_pack_p
= true;
3918 if (DECL_PACK_P (t
))
3920 /* We don't want to walk into the type of a PARM_DECL,
3921 because we don't want to see the type parameter pack. */
3923 parameter_pack_p
= true;
3928 if (DECL_PACK_P (t
))
3930 /* We don't want to walk into the type of a variadic capture proxy,
3931 because we don't want to see the type parameter pack. */
3933 parameter_pack_p
= true;
3935 else if (variable_template_specialization_p (t
))
3937 cp_walk_tree (&DECL_TI_ARGS (t
),
3938 find_parameter_packs_r
,
3945 if (builtin_pack_call_p (t
))
3946 parameter_pack_p
= true;
3950 parameter_pack_p
= true;
3953 /* Not a parameter pack. */
3957 if (parameter_pack_p
)
3959 /* Add this parameter pack to the list. */
3960 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3964 cp_walk_tree (&TYPE_CONTEXT (t
),
3965 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3967 /* This switch statement will return immediately if we don't find a
3968 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3969 switch (TREE_CODE (t
))
3971 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3972 /* Check the template itself. */
3973 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3974 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3979 tree decl
= DECL_EXPR_DECL (t
);
3980 /* Ignore the declaration of a capture proxy for a parameter pack. */
3981 if (is_capture_proxy (decl
))
3983 if (is_typedef_decl (decl
))
3984 /* Since we stop at typedefs above, we need to look through them at
3985 the point of the DECL_EXPR. */
3986 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3987 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3992 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3994 cp_walk_tree (&TREE_TYPE (t
),
3995 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3998 case TYPE_PACK_EXPANSION
:
3999 case EXPR_PACK_EXPANSION
:
4004 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4009 case IDENTIFIER_NODE
:
4010 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4017 /* Since we defer implicit capture, look in the parms and body. */
4018 tree fn
= lambda_function (t
);
4019 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4021 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4028 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4029 type_pack_expansion_p to false so that any placeholders
4030 within the expression don't get marked as parameter packs. */
4031 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4032 ppd
->type_pack_expansion_p
= false;
4033 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4035 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4041 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4043 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4045 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4047 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4058 /* Determines if the expression or type T uses any parameter packs. */
4060 uses_parameter_packs (tree t
)
4062 tree parameter_packs
= NULL_TREE
;
4063 struct find_parameter_pack_data ppd
;
4064 ppd
.parameter_packs
= ¶meter_packs
;
4065 ppd
.visited
= new hash_set
<tree
>;
4066 ppd
.type_pack_expansion_p
= false;
4067 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4069 return parameter_packs
;
4072 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4073 representation a base-class initializer into a parameter pack
4074 expansion. If all goes well, the resulting node will be an
4075 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4078 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4081 tree parameter_packs
= NULL_TREE
;
4082 bool for_types
= false;
4083 struct find_parameter_pack_data ppd
;
4085 if (!arg
|| arg
== error_mark_node
)
4088 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4090 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4091 class initializer. In this case, the TREE_PURPOSE will be a
4092 _TYPE node (representing the base class expansion we're
4093 initializing) and the TREE_VALUE will be a TREE_LIST
4094 containing the initialization arguments.
4096 The resulting expansion looks somewhat different from most
4097 expansions. Rather than returning just one _EXPANSION, we
4098 return a TREE_LIST whose TREE_PURPOSE is a
4099 TYPE_PACK_EXPANSION containing the bases that will be
4100 initialized. The TREE_VALUE will be identical to the
4101 original TREE_VALUE, which is a list of arguments that will
4102 be passed to each base. We do not introduce any new pack
4103 expansion nodes into the TREE_VALUE (although it is possible
4104 that some already exist), because the TREE_PURPOSE and
4105 TREE_VALUE all need to be expanded together with the same
4106 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4107 resulting TREE_PURPOSE will mention the parameter packs in
4108 both the bases and the arguments to the bases. */
4111 tree parameter_packs
= NULL_TREE
;
4113 /* Determine which parameter packs will be used by the base
4115 ppd
.visited
= new hash_set
<tree
>;
4116 ppd
.parameter_packs
= ¶meter_packs
;
4117 ppd
.type_pack_expansion_p
= false;
4118 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4119 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4122 if (parameter_packs
== NULL_TREE
)
4124 if (complain
& tf_error
)
4125 error ("base initializer expansion %qT contains no parameter packs",
4128 return error_mark_node
;
4131 if (TREE_VALUE (arg
) != void_type_node
)
4133 /* Collect the sets of parameter packs used in each of the
4134 initialization arguments. */
4135 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4137 /* Determine which parameter packs will be expanded in this
4139 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4146 /* Create the pack expansion type for the base type. */
4147 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4148 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4149 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4150 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4153 they will rarely be compared to anything. */
4154 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4156 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4159 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4162 /* Build the PACK_EXPANSION_* node. */
4164 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4165 : make_node (EXPR_PACK_EXPANSION
);
4166 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4167 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4169 /* Propagate type and const-expression information. */
4170 TREE_TYPE (result
) = TREE_TYPE (arg
);
4171 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4172 /* Mark this read now, since the expansion might be length 0. */
4173 mark_exp_read (arg
);
4176 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4177 they will rarely be compared to anything. */
4178 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4180 /* Determine which parameter packs will be expanded. */
4181 ppd
.parameter_packs
= ¶meter_packs
;
4182 ppd
.visited
= new hash_set
<tree
>;
4183 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4184 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4187 /* Make sure we found some parameter packs. */
4188 if (parameter_packs
== NULL_TREE
)
4190 if (complain
& tf_error
)
4193 error ("expansion pattern %qT contains no parameter packs", arg
);
4195 error ("expansion pattern %qE contains no parameter packs", arg
);
4197 return error_mark_node
;
4199 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4201 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4206 /* Checks T for any "bare" parameter packs, which have not yet been
4207 expanded, and issues an error if any are found. This operation can
4208 only be done on full expressions or types (e.g., an expression
4209 statement, "if" condition, etc.), because we could have expressions like:
4211 foo(f(g(h(args)))...)
4213 where "args" is a parameter pack. check_for_bare_parameter_packs
4214 should not be called for the subexpressions args, h(args),
4215 g(h(args)), or f(g(h(args))), because we would produce erroneous
4218 Returns TRUE and emits an error if there were bare parameter packs,
4219 returns FALSE otherwise. */
4221 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4223 tree parameter_packs
= NULL_TREE
;
4224 struct find_parameter_pack_data ppd
;
4226 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4229 /* A lambda might use a parameter pack from the containing context. */
4230 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4231 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4234 if (TREE_CODE (t
) == TYPE_DECL
)
4237 ppd
.parameter_packs
= ¶meter_packs
;
4238 ppd
.visited
= new hash_set
<tree
>;
4239 ppd
.type_pack_expansion_p
= false;
4240 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4243 if (parameter_packs
)
4245 if (loc
== UNKNOWN_LOCATION
)
4246 loc
= cp_expr_loc_or_input_loc (t
);
4247 error_at (loc
, "parameter packs not expanded with %<...%>:");
4248 while (parameter_packs
)
4250 tree pack
= TREE_VALUE (parameter_packs
);
4251 tree name
= NULL_TREE
;
4253 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4254 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4255 name
= TYPE_NAME (pack
);
4256 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4257 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4258 else if (TREE_CODE (pack
) == CALL_EXPR
)
4259 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4261 name
= DECL_NAME (pack
);
4264 inform (loc
, " %qD", name
);
4266 inform (loc
, " %s", "<anonymous>");
4268 parameter_packs
= TREE_CHAIN (parameter_packs
);
4277 /* Expand any parameter packs that occur in the template arguments in
4280 expand_template_argument_pack (tree args
)
4282 if (args
== error_mark_node
)
4283 return error_mark_node
;
4285 tree result_args
= NULL_TREE
;
4286 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4287 int num_result_args
= -1;
4288 int non_default_args_count
= -1;
4290 /* First, determine if we need to expand anything, and the number of
4291 slots we'll need. */
4292 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4294 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4295 if (arg
== NULL_TREE
)
4297 if (ARGUMENT_PACK_P (arg
))
4299 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4300 if (num_result_args
< 0)
4301 num_result_args
= in_arg
+ num_packed
;
4303 num_result_args
+= num_packed
;
4307 if (num_result_args
>= 0)
4312 /* If no expansion is necessary, we're done. */
4313 if (num_result_args
< 0)
4316 /* Expand arguments. */
4317 result_args
= make_tree_vec (num_result_args
);
4318 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4319 non_default_args_count
=
4320 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4321 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4323 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4324 if (ARGUMENT_PACK_P (arg
))
4326 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4327 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4328 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4329 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4330 if (non_default_args_count
> 0)
4331 non_default_args_count
+= num_packed
- 1;
4335 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4339 if (non_default_args_count
>= 0)
4340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4344 /* Checks if DECL shadows a template parameter.
4346 [temp.local]: A template-parameter shall not be redeclared within its
4347 scope (including nested scopes).
4349 Emits an error and returns TRUE if the DECL shadows a parameter,
4350 returns FALSE otherwise. */
4353 check_template_shadow (tree decl
)
4357 /* If we're not in a template, we can't possibly shadow a template
4359 if (!current_template_parms
)
4362 /* Figure out what we're shadowing. */
4363 decl
= OVL_FIRST (decl
);
4364 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4366 /* If there's no previous binding for this name, we're not shadowing
4367 anything, let alone a template parameter. */
4371 /* If we're not shadowing a template parameter, we're done. Note
4372 that OLDDECL might be an OVERLOAD (or perhaps even an
4373 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4375 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4378 /* We check for decl != olddecl to avoid bogus errors for using a
4379 name inside a class. We check TPFI to avoid duplicate errors for
4380 inline member templates. */
4382 || (DECL_TEMPLATE_PARM_P (decl
)
4383 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4386 /* Don't complain about the injected class name, as we've already
4387 complained about the class itself. */
4388 if (DECL_SELF_REFERENCE_P (decl
))
4391 if (DECL_TEMPLATE_PARM_P (decl
))
4392 error ("declaration of template parameter %q+D shadows "
4393 "template parameter", decl
);
4395 error ("declaration of %q+#D shadows template parameter", decl
);
4396 inform (DECL_SOURCE_LOCATION (olddecl
),
4397 "template parameter %qD declared here", olddecl
);
4401 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4402 ORIG_LEVEL, DECL, and TYPE. */
4405 build_template_parm_index (int index
,
4411 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4412 TEMPLATE_PARM_IDX (t
) = index
;
4413 TEMPLATE_PARM_LEVEL (t
) = level
;
4414 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4415 TEMPLATE_PARM_DECL (t
) = decl
;
4416 TREE_TYPE (t
) = type
;
4417 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4418 TREE_READONLY (t
) = TREE_READONLY (decl
);
4423 /* Find the canonical type parameter for the given template type
4424 parameter. Returns the canonical type parameter, which may be TYPE
4425 if no such parameter existed. */
4428 canonical_type_parameter (tree type
)
4430 int idx
= TEMPLATE_TYPE_IDX (type
);
4432 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4434 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4435 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4437 for (tree list
= (*canonical_template_parms
)[idx
];
4438 list
; list
= TREE_CHAIN (list
))
4439 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4440 return TREE_VALUE (list
);
4442 (*canonical_template_parms
)[idx
]
4443 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4447 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4448 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4449 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4450 new one is created. */
4453 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4454 tsubst_flags_t complain
)
4456 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4457 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4458 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4459 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4461 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4463 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4464 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4466 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4467 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4468 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4469 DECL_ARTIFICIAL (decl
) = 1;
4470 SET_DECL_TEMPLATE_PARM_P (decl
);
4472 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4473 TEMPLATE_PARM_LEVEL (index
) - levels
,
4474 TEMPLATE_PARM_ORIG_LEVEL (index
),
4476 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4477 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4478 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4480 /* Template template parameters need this. */
4482 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4484 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4485 TYPE_DECL
, DECL_NAME (decl
), type
);
4486 DECL_TEMPLATE_RESULT (decl
) = inner
;
4487 DECL_ARTIFICIAL (inner
) = true;
4488 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4489 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4492 /* Attach the TPI to the decl. */
4493 if (TREE_CODE (inner
) == TYPE_DECL
)
4494 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4496 DECL_INITIAL (decl
) = tpi
;
4499 return TEMPLATE_PARM_DESCENDANTS (index
);
4502 /* Process information from new template parameter PARM and append it
4503 to the LIST being built. This new parameter is a non-type
4504 parameter iff IS_NON_TYPE is true. This new parameter is a
4505 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4509 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4510 bool is_non_type
, bool is_parameter_pack
)
4512 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4513 tree prev
= NULL_TREE
;
4518 prev
= tree_last (list
);
4520 tree p
= TREE_VALUE (prev
);
4521 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4522 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4523 else if (TREE_CODE (p
) == PARM_DECL
)
4524 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4529 tree decl
= NULL_TREE
;
4530 tree defval
= TREE_PURPOSE (parm
);
4531 tree constr
= TREE_TYPE (parm
);
4535 parm
= TREE_VALUE (parm
);
4537 SET_DECL_TEMPLATE_PARM_P (parm
);
4539 if (TREE_TYPE (parm
) != error_mark_node
)
4543 The top-level cv-qualifiers on the template-parameter are
4544 ignored when determining its type. */
4545 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4546 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4547 TREE_TYPE (parm
) = error_mark_node
;
4548 else if (uses_parameter_packs (TREE_TYPE (parm
))
4549 && !is_parameter_pack
4550 /* If we're in a nested template parameter list, the template
4551 template parameter could be a parameter pack. */
4552 && processing_template_parmlist
== 1)
4554 /* This template parameter is not a parameter pack, but it
4555 should be. Complain about "bare" parameter packs. */
4556 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4558 /* Recover by calling this a parameter pack. */
4559 is_parameter_pack
= true;
4563 /* A template parameter is not modifiable. */
4564 TREE_CONSTANT (parm
) = 1;
4565 TREE_READONLY (parm
) = 1;
4566 decl
= build_decl (parm_loc
,
4567 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4568 TREE_CONSTANT (decl
) = 1;
4569 TREE_READONLY (decl
) = 1;
4570 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4571 = build_template_parm_index (idx
, processing_template_decl
,
4572 processing_template_decl
,
4573 decl
, TREE_TYPE (parm
));
4575 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4576 = is_parameter_pack
;
4581 parm
= TREE_VALUE (TREE_VALUE (parm
));
4583 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4585 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4586 /* This is for distinguishing between real templates and template
4587 template parameters */
4588 TREE_TYPE (parm
) = t
;
4590 /* any_template_parm_r expects to be able to get the targs of a
4591 DECL_TEMPLATE_RESULT. */
4592 tree result
= DECL_TEMPLATE_RESULT (parm
);
4593 TREE_TYPE (result
) = t
;
4594 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4595 tree tinfo
= build_template_info (parm
, args
);
4596 retrofit_lang_decl (result
);
4597 DECL_TEMPLATE_INFO (result
) = tinfo
;
4603 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4604 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4605 decl
= build_decl (parm_loc
,
4606 TYPE_DECL
, parm
, t
);
4609 TYPE_NAME (t
) = decl
;
4610 TYPE_STUB_DECL (t
) = decl
;
4612 TEMPLATE_TYPE_PARM_INDEX (t
)
4613 = build_template_parm_index (idx
, processing_template_decl
,
4614 processing_template_decl
,
4615 decl
, TREE_TYPE (parm
));
4616 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4617 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4618 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4620 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4622 DECL_ARTIFICIAL (decl
) = 1;
4623 SET_DECL_TEMPLATE_PARM_P (decl
);
4625 /* Build requirements for the type/template parameter.
4626 This must be done after SET_DECL_TEMPLATE_PARM_P or
4627 process_template_parm could fail. */
4628 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4630 decl
= pushdecl (decl
);
4634 /* Build the parameter node linking the parameter declaration,
4635 its default argument (if any), and its constraints (if any). */
4636 parm
= build_tree_list (defval
, parm
);
4637 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4640 TREE_CHAIN (prev
) = parm
;
4647 /* The end of a template parameter list has been reached. Process the
4648 tree list into a parameter vector, converting each parameter into a more
4649 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4653 end_template_parm_list (tree parms
)
4655 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4657 /* Pop the dummy parameter level and add the real one. We do not
4658 morph the dummy parameter in place, as it might have been
4659 captured by a (nested) template-template-parm. */
4660 current_template_parms
= TREE_CHAIN (current_template_parms
);
4662 current_template_parms
4663 = tree_cons (size_int (processing_template_decl
),
4664 saved_parmlist
, current_template_parms
);
4666 for (unsigned ix
= 0; parms
; ix
++)
4669 parms
= TREE_CHAIN (parms
);
4670 TREE_CHAIN (parm
) = NULL_TREE
;
4672 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4675 --processing_template_parmlist
;
4677 return saved_parmlist
;
4680 // Explicitly indicate the end of the template parameter list. We assume
4681 // that the current template parameters have been constructed and/or
4682 // managed explicitly, as when creating new template template parameters
4683 // from a shorthand constraint.
4685 end_template_parm_list ()
4687 --processing_template_parmlist
;
4690 /* end_template_decl is called after a template declaration is seen. */
4693 end_template_decl (void)
4695 reset_specialization ();
4697 if (! processing_template_decl
)
4700 /* This matches the pushlevel in begin_template_parm_list. */
4703 --processing_template_decl
;
4704 current_template_parms
= TREE_CHAIN (current_template_parms
);
4707 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4708 thereof, and converts it into an argument suitable to be passed to
4709 the type substitution functions. Note that if the TREE_LIST contains
4710 an error_mark node, the returned argument is error_mark_node. */
4713 template_parm_to_arg (tree t
)
4718 if (TREE_CODE (t
) == TREE_LIST
)
4721 if (error_operand_p (t
))
4722 return error_mark_node
;
4724 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4726 if (TREE_CODE (t
) == TYPE_DECL
4727 || TREE_CODE (t
) == TEMPLATE_DECL
)
4730 t
= DECL_INITIAL (t
);
4733 gcc_assert (TEMPLATE_PARM_P (t
));
4735 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4736 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4738 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4740 /* Turn this argument into a TYPE_ARGUMENT_PACK
4741 with a single element, which expands T. */
4742 tree vec
= make_tree_vec (1);
4744 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4746 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4748 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4749 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4754 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4756 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4757 with a single element, which expands T. */
4758 tree vec
= make_tree_vec (1);
4760 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4762 t
= convert_from_reference (t
);
4763 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4765 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4766 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4769 t
= convert_from_reference (t
);
4774 /* Given a single level of template parameters (a TREE_VEC), return it
4775 as a set of template arguments. */
4778 template_parms_level_to_args (tree parms
)
4780 tree a
= copy_node (parms
);
4781 TREE_TYPE (a
) = NULL_TREE
;
4782 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4783 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4786 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4791 /* Given a set of template parameters, return them as a set of template
4792 arguments. The template parameters are represented as a TREE_VEC, in
4793 the form documented in cp-tree.h for template arguments. */
4796 template_parms_to_args (tree parms
)
4799 tree args
= NULL_TREE
;
4800 int length
= TMPL_PARMS_DEPTH (parms
);
4803 /* If there is only one level of template parameters, we do not
4804 create a TREE_VEC of TREE_VECs. Instead, we return a single
4805 TREE_VEC containing the arguments. */
4807 args
= make_tree_vec (length
);
4809 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4811 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4814 TREE_VEC_ELT (args
, --l
) = a
;
4822 /* Within the declaration of a template, return the currently active
4823 template parameters as an argument TREE_VEC. */
4826 current_template_args (void)
4828 return template_parms_to_args (current_template_parms
);
4831 /* Return the fully generic arguments for of TMPL, i.e. what
4832 current_template_args would be while parsing it. */
4835 generic_targs_for (tree tmpl
)
4837 if (tmpl
== NULL_TREE
)
4839 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4840 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4841 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4842 template parameter, it has no TEMPLATE_INFO; for a partial
4843 specialization, it has the arguments for the primary template, and we
4844 want the arguments for the partial specialization. */;
4845 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4846 if (tree ti
= get_template_info (result
))
4847 return TI_ARGS (ti
);
4848 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4851 /* Update the declared TYPE by doing any lookups which were thought to be
4852 dependent, but are not now that we know the SCOPE of the declarator. */
4855 maybe_update_decl_type (tree orig_type
, tree scope
)
4857 tree type
= orig_type
;
4859 if (type
== NULL_TREE
)
4862 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4863 type
= TREE_TYPE (type
);
4865 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4866 && dependent_type_p (type
)
4867 /* Don't bother building up the args in this case. */
4868 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4870 /* tsubst in the args corresponding to the template parameters,
4871 including auto if present. Most things will be unchanged, but
4872 make_typename_type and tsubst_qualified_id will resolve
4873 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4874 tree args
= current_template_args ();
4875 tree auto_node
= type_uses_auto (type
);
4879 tree auto_vec
= make_tree_vec (1);
4880 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4881 args
= add_to_template_args (args
, auto_vec
);
4883 pushed
= push_scope (scope
);
4884 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4889 if (type
== error_mark_node
)
4892 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4894 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4897 type
= TYPE_NAME (type
);
4902 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4903 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4904 the new template is a member template. */
4907 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4909 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4910 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4911 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4912 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4913 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4914 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4915 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4916 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4921 struct template_parm_data
4923 /* The level of the template parameters we are currently
4927 /* The index of the specialization argument we are currently
4931 /* An array whose size is the number of template parameters. The
4932 elements are nonzero if the parameter has been used in any one
4933 of the arguments processed so far. */
4936 /* An array whose size is the number of template arguments. The
4937 elements are nonzero if the argument makes use of template
4938 parameters of this level. */
4939 int* arg_uses_template_parms
;
4942 /* Subroutine of push_template_decl used to see if each template
4943 parameter in a partial specialization is used in the explicit
4944 argument list. If T is of the LEVEL given in DATA (which is
4945 treated as a template_parm_data*), then DATA->PARMS is marked
4949 mark_template_parm (tree t
, void* data
)
4953 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4955 template_parm_level_and_index (t
, &level
, &idx
);
4957 if (level
== tpd
->level
)
4959 tpd
->parms
[idx
] = 1;
4960 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4963 /* In C++17 the type of a non-type argument is a deduced context. */
4964 if (cxx_dialect
>= cxx17
4965 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4966 for_each_template_parm (TREE_TYPE (t
),
4967 &mark_template_parm
,
4970 /*include_nondeduced_p=*/false);
4972 /* Return zero so that for_each_template_parm will continue the
4973 traversal of the tree; we want to mark *every* template parm. */
4977 /* Process the partial specialization DECL. */
4980 process_partial_specialization (tree decl
)
4982 tree type
= TREE_TYPE (decl
);
4983 tree tinfo
= get_template_info (decl
);
4984 tree maintmpl
= TI_TEMPLATE (tinfo
);
4985 tree specargs
= TI_ARGS (tinfo
);
4986 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4987 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4990 int nargs
= TREE_VEC_LENGTH (inner_args
);
4993 bool did_error_intro
= false;
4994 struct template_parm_data tpd
;
4995 struct template_parm_data tpd2
;
4997 gcc_assert (current_template_parms
);
4999 /* A concept cannot be specialized. */
5000 if (flag_concepts
&& variable_concept_p (maintmpl
))
5002 error ("specialization of variable concept %q#D", maintmpl
);
5003 return error_mark_node
;
5006 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5007 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5009 /* We check that each of the template parameters given in the
5010 partial specialization is used in the argument list to the
5011 specialization. For example:
5013 template <class T> struct S;
5014 template <class T> struct S<T*>;
5016 The second declaration is OK because `T*' uses the template
5017 parameter T, whereas
5019 template <class T> struct S<int>;
5021 is no good. Even trickier is:
5032 The S2<T> declaration is actually invalid; it is a
5033 full-specialization. Of course,
5036 struct S2<T (*)(U)>;
5038 or some such would have been OK. */
5039 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5040 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5041 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5043 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5044 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5045 for (i
= 0; i
< nargs
; ++i
)
5047 tpd
.current_arg
= i
;
5048 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5049 &mark_template_parm
,
5052 /*include_nondeduced_p=*/false);
5054 for (i
= 0; i
< ntparms
; ++i
)
5055 if (tpd
.parms
[i
] == 0)
5057 /* One of the template parms was not used in a deduced context in the
5059 if (!did_error_intro
)
5061 error ("template parameters not deducible in "
5062 "partial specialization:");
5063 did_error_intro
= true;
5066 inform (input_location
, " %qD",
5067 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5070 if (did_error_intro
)
5071 return error_mark_node
;
5073 /* [temp.class.spec]
5075 The argument list of the specialization shall not be identical to
5076 the implicit argument list of the primary template. */
5078 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5079 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5081 || !strictly_subsumes (current_template_constraints (),
5082 main_args
, maintmpl
)))
5085 error ("partial specialization %q+D does not specialize "
5086 "any template arguments; to define the primary template, "
5087 "remove the template argument list", decl
);
5089 error ("partial specialization %q+D does not specialize any "
5090 "template arguments and is not more constrained than "
5091 "the primary template; to define the primary template, "
5092 "remove the template argument list", decl
);
5093 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5096 /* A partial specialization that replaces multiple parameters of the
5097 primary template with a pack expansion is less specialized for those
5099 if (nargs
< DECL_NTPARMS (maintmpl
))
5101 error ("partial specialization is not more specialized than the "
5102 "primary template because it replaces multiple parameters "
5103 "with a pack expansion");
5104 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5105 /* Avoid crash in process_partial_specialization. */
5109 else if (nargs
> DECL_NTPARMS (maintmpl
))
5111 error ("too many arguments for partial specialization %qT", type
);
5112 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5113 /* Avoid crash below. */
5117 /* If we aren't in a dependent class, we can actually try deduction. */
5118 else if (tpd
.level
== 1
5119 /* FIXME we should be able to handle a partial specialization of a
5120 partial instantiation, but currently we can't (c++/41727). */
5121 && TMPL_ARGS_DEPTH (specargs
) == 1
5122 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5124 auto_diagnostic_group d
;
5125 if (permerror (input_location
, "partial specialization %qD is not "
5126 "more specialized than", decl
))
5127 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5131 /* [temp.class.spec]
5133 A partially specialized non-type argument expression shall not
5134 involve template parameters of the partial specialization except
5135 when the argument expression is a simple identifier.
5137 The type of a template parameter corresponding to a specialized
5138 non-type argument shall not be dependent on a parameter of the
5141 Also, we verify that pack expansions only occur at the
5142 end of the argument list. */
5144 for (i
= 0; i
< nargs
; ++i
)
5146 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5147 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5148 tree packed_args
= NULL_TREE
;
5151 if (ARGUMENT_PACK_P (arg
))
5153 /* Extract the arguments from the argument pack. We'll be
5154 iterating over these in the following loop. */
5155 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5156 len
= TREE_VEC_LENGTH (packed_args
);
5159 for (j
= 0; j
< len
; j
++)
5162 /* Get the Jth argument in the parameter pack. */
5163 arg
= TREE_VEC_ELT (packed_args
, j
);
5165 if (PACK_EXPANSION_P (arg
))
5167 /* Pack expansions must come at the end of the
5169 if ((packed_args
&& j
< len
- 1)
5170 || (!packed_args
&& i
< nargs
- 1))
5172 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5173 error ("parameter pack argument %qE must be at the "
5174 "end of the template argument list", arg
);
5176 error ("parameter pack argument %qT must be at the "
5177 "end of the template argument list", arg
);
5181 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5182 /* We only care about the pattern. */
5183 arg
= PACK_EXPANSION_PATTERN (arg
);
5185 if (/* These first two lines are the `non-type' bit. */
5187 && TREE_CODE (arg
) != TEMPLATE_DECL
5188 /* This next two lines are the `argument expression is not just a
5189 simple identifier' condition and also the `specialized
5190 non-type argument' bit. */
5191 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5192 && !((REFERENCE_REF_P (arg
)
5193 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5194 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5196 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5197 || (packed_args
&& uses_template_parms (arg
)))
5198 error_at (cp_expr_loc_or_input_loc (arg
),
5199 "template argument %qE involves template "
5200 "parameter(s)", arg
);
5203 /* Look at the corresponding template parameter,
5204 marking which template parameters its type depends
5206 tree type
= TREE_TYPE (parm
);
5210 /* We haven't yet initialized TPD2. Do so now. */
5211 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5212 /* The number of parameters here is the number in the
5213 main template, which, as checked in the assertion
5215 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5217 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5220 /* Mark the template parameters. But this time, we're
5221 looking for the template parameters of the main
5222 template, not in the specialization. */
5223 tpd2
.current_arg
= i
;
5224 tpd2
.arg_uses_template_parms
[i
] = 0;
5225 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5226 for_each_template_parm (type
,
5227 &mark_template_parm
,
5230 /*include_nondeduced_p=*/false);
5232 if (tpd2
.arg_uses_template_parms
[i
])
5234 /* The type depended on some template parameters.
5235 If they are fully specialized in the
5236 specialization, that's OK. */
5239 for (j
= 0; j
< nargs
; ++j
)
5240 if (tpd2
.parms
[j
] != 0
5241 && tpd
.arg_uses_template_parms
[j
])
5244 error_n (input_location
, count
,
5245 "type %qT of template argument %qE depends "
5246 "on a template parameter",
5247 "type %qT of template argument %qE depends "
5248 "on template parameters",
5257 /* We should only get here once. */
5258 if (TREE_CODE (decl
) == TYPE_DECL
)
5259 gcc_assert (!COMPLETE_TYPE_P (type
));
5261 // Build the template decl.
5262 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5263 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5264 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5265 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5266 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5268 /* Give template template parms a DECL_CONTEXT of the template
5269 for which they are a parameter. */
5270 for (i
= 0; i
< ntparms
; ++i
)
5272 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5273 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5274 DECL_CONTEXT (parm
) = tmpl
;
5278 /* We didn't register this in check_explicit_specialization so we could
5279 wait until the constraints were set. */
5280 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5282 associate_classtype_constraints (type
);
5284 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5285 = tree_cons (specargs
, tmpl
,
5286 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5287 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5289 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5290 inst
= TREE_CHAIN (inst
))
5292 tree instance
= TREE_VALUE (inst
);
5293 if (TYPE_P (instance
)
5294 ? (COMPLETE_TYPE_P (instance
)
5295 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5296 : DECL_TEMPLATE_INSTANTIATION (instance
))
5298 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5299 tree inst_decl
= (DECL_P (instance
)
5300 ? instance
: TYPE_NAME (instance
));
5303 else if (spec
== error_mark_node
)
5304 permerror (input_location
,
5305 "declaration of %qD ambiguates earlier template "
5306 "instantiation for %qD", decl
, inst_decl
);
5307 else if (TREE_VALUE (spec
) == tmpl
)
5308 permerror (input_location
,
5309 "partial specialization of %qD after instantiation "
5310 "of %qD", decl
, inst_decl
);
5317 /* PARM is a template parameter of some form; return the corresponding
5318 TEMPLATE_PARM_INDEX. */
5321 get_template_parm_index (tree parm
)
5323 if (TREE_CODE (parm
) == PARM_DECL
5324 || TREE_CODE (parm
) == CONST_DECL
)
5325 parm
= DECL_INITIAL (parm
);
5326 else if (TREE_CODE (parm
) == TYPE_DECL
5327 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5328 parm
= TREE_TYPE (parm
);
5329 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5330 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5331 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5332 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5333 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5337 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5338 parameter packs used by the template parameter PARM. */
5341 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5343 /* A type parm can't refer to another parm. */
5344 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5346 else if (TREE_CODE (parm
) == PARM_DECL
)
5348 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5353 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5355 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5356 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5358 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5359 if (template_parameter_pack_p (p
))
5360 /* Any packs in the type are expanded by this parameter. */;
5362 fixed_parameter_pack_p_1 (p
, ppd
);
5366 /* PARM is a template parameter pack. Return any parameter packs used in
5367 its type or the type of any of its template parameters. If there are
5368 any such packs, it will be instantiated into a fixed template parameter
5369 list by partial instantiation rather than be fully deduced. */
5372 fixed_parameter_pack_p (tree parm
)
5374 /* This can only be true in a member template. */
5375 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5377 /* This can only be true for a parameter pack. */
5378 if (!template_parameter_pack_p (parm
))
5380 /* A type parm can't refer to another parm. */
5381 if (TREE_CODE (parm
) == TYPE_DECL
)
5384 tree parameter_packs
= NULL_TREE
;
5385 struct find_parameter_pack_data ppd
;
5386 ppd
.parameter_packs
= ¶meter_packs
;
5387 ppd
.visited
= new hash_set
<tree
>;
5388 ppd
.type_pack_expansion_p
= false;
5390 fixed_parameter_pack_p_1 (parm
, &ppd
);
5393 return parameter_packs
;
5396 /* Check that a template declaration's use of default arguments and
5397 parameter packs is not invalid. Here, PARMS are the template
5398 parameters. IS_PRIMARY is true if DECL is the thing declared by
5399 a primary template. IS_PARTIAL is true if DECL is a partial
5402 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5403 function template declaration or a friend class template
5404 declaration. In the function case, 1 indicates a declaration, 2
5405 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5406 emitted for extraneous default arguments.
5408 Returns TRUE if there were no errors found, FALSE otherwise. */
5411 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5412 bool is_partial
, int is_friend_decl
)
5415 int last_level_to_check
;
5417 bool no_errors
= true;
5421 A default template-argument shall not be specified in a
5422 function template declaration or a function template definition, nor
5423 in the template-parameter-list of the definition of a member of a
5426 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5427 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5428 /* You can't have a function template declaration in a local
5429 scope, nor you can you define a member of a class template in a
5433 if ((TREE_CODE (decl
) == TYPE_DECL
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5436 || (TREE_CODE (decl
) == FUNCTION_DECL
5437 && LAMBDA_FUNCTION_P (decl
)))
5438 /* A lambda doesn't have an explicit declaration; don't complain
5439 about the parms of the enclosing class. */
5442 if (current_class_type
5443 && !TYPE_BEING_DEFINED (current_class_type
)
5444 && DECL_LANG_SPECIFIC (decl
)
5445 && DECL_DECLARES_FUNCTION_P (decl
)
5446 /* If this is either a friend defined in the scope of the class
5447 or a member function. */
5448 && (DECL_FUNCTION_MEMBER_P (decl
)
5449 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5450 : DECL_FRIEND_CONTEXT (decl
)
5451 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5453 /* And, if it was a member function, it really was defined in
5454 the scope of the class. */
5455 && (!DECL_FUNCTION_MEMBER_P (decl
)
5456 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5457 /* We already checked these parameters when the template was
5458 declared, so there's no need to do it again now. This function
5459 was defined in class scope, but we're processing its body now
5460 that the class is complete. */
5463 /* Core issue 226 (C++0x only): the following only applies to class
5466 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5470 If a template-parameter has a default template-argument, all
5471 subsequent template-parameters shall have a default
5472 template-argument supplied. */
5473 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5475 tree inner_parms
= TREE_VALUE (parm_level
);
5476 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5477 int seen_def_arg_p
= 0;
5480 for (i
= 0; i
< ntparms
; ++i
)
5482 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5484 if (parm
== error_mark_node
)
5487 if (TREE_PURPOSE (parm
))
5489 else if (seen_def_arg_p
5490 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5492 error ("no default argument for %qD", TREE_VALUE (parm
));
5493 /* For better subsequent error-recovery, we indicate that
5494 there should have been a default argument. */
5495 TREE_PURPOSE (parm
) = error_mark_node
;
5498 else if (!is_partial
5500 /* Don't complain about an enclosing partial
5502 && parm_level
== parms
5503 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5505 && template_parameter_pack_p (TREE_VALUE (parm
))
5506 /* A fixed parameter pack will be partially
5507 instantiated into a fixed length list. */
5508 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5510 /* A primary class template, primary variable template
5511 (DR 2032), or alias template can only have one
5512 parameter pack, at the end of the template
5515 error ("parameter pack %q+D must be at the end of the"
5516 " template parameter list", TREE_VALUE (parm
));
5518 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5526 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5530 /* For an ordinary class template, default template arguments are
5531 allowed at the innermost level, e.g.:
5532 template <class T = int>
5534 but, in a partial specialization, they're not allowed even
5535 there, as we have in [temp.class.spec]:
5537 The template parameter list of a specialization shall not
5538 contain default template argument values.
5540 So, for a partial specialization, or for a function template
5541 (in C++98/C++03), we look at all of them. */
5544 /* But, for a primary class template that is not a partial
5545 specialization we look at all template parameters except the
5547 parms
= TREE_CHAIN (parms
);
5549 /* Figure out what error message to issue. */
5550 if (is_friend_decl
== 2)
5551 msg
= G_("default template arguments may not be used in function template "
5552 "friend re-declaration");
5553 else if (is_friend_decl
)
5554 msg
= G_("default template arguments may not be used in template "
5555 "friend declarations");
5556 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5557 msg
= G_("default template arguments may not be used in function templates "
5558 "without %<-std=c++11%> or %<-std=gnu++11%>");
5559 else if (is_partial
)
5560 msg
= G_("default template arguments may not be used in "
5561 "partial specializations");
5562 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5563 msg
= G_("default argument for template parameter for class enclosing %qD");
5565 /* Per [temp.param]/9, "A default template-argument shall not be
5566 specified in the template-parameter-lists of the definition of
5567 a member of a class template that appears outside of the member's
5568 class.", thus if we aren't handling a member of a class template
5569 there is no need to examine the parameters. */
5572 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5573 /* If we're inside a class definition, there's no need to
5574 examine the parameters to the class itself. On the one
5575 hand, they will be checked when the class is defined, and,
5576 on the other, default arguments are valid in things like:
5577 template <class T = double>
5578 struct S { template <class U> void f(U); };
5579 Here the default argument for `S' has no bearing on the
5580 declaration of `f'. */
5581 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5583 /* Check everything. */
5584 last_level_to_check
= 0;
5586 for (parm_level
= parms
;
5587 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5588 parm_level
= TREE_CHAIN (parm_level
))
5590 tree inner_parms
= TREE_VALUE (parm_level
);
5594 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5595 for (i
= 0; i
< ntparms
; ++i
)
5597 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5605 if (is_friend_decl
== 2)
5612 /* Clear out the default argument so that we are not
5614 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5618 /* At this point, if we're still interested in issuing messages,
5619 they must apply to classes surrounding the object declared. */
5621 msg
= G_("default argument for template parameter for class "
5628 /* Worker for push_template_decl_real, called via
5629 for_each_template_parm. DATA is really an int, indicating the
5630 level of the parameters we are interested in. If T is a template
5631 parameter of that level, return nonzero. */
5634 template_parm_this_level_p (tree t
, void* data
)
5636 int this_level
= *(int *)data
;
5639 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5640 level
= TEMPLATE_PARM_LEVEL (t
);
5642 level
= TEMPLATE_TYPE_LEVEL (t
);
5643 return level
== this_level
;
5646 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5647 DATA is really an int, indicating the innermost outer level of parameters.
5648 If T is a template parameter of that level or further out, return
5652 template_parm_outer_level (tree t
, void *data
)
5654 int this_level
= *(int *)data
;
5657 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5658 level
= TEMPLATE_PARM_LEVEL (t
);
5660 level
= TEMPLATE_TYPE_LEVEL (t
);
5661 return level
<= this_level
;
5664 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5665 parameters given by current_template_args, or reuses a
5666 previously existing one, if appropriate. Returns the DECL, or an
5667 equivalent one, if it is replaced via a call to duplicate_decls.
5669 If IS_FRIEND is true, DECL is a friend declaration. */
5672 push_template_decl_real (tree decl
, bool is_friend
)
5680 int new_template_p
= 0;
5681 /* True if the template is a member template, in the sense of
5683 bool member_template_p
= false;
5685 if (decl
== error_mark_node
|| !current_template_parms
)
5686 return error_mark_node
;
5688 /* See if this is a partial specialization. */
5689 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5690 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5691 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5693 && DECL_LANG_SPECIFIC (decl
)
5694 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5695 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5697 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5701 /* For a friend, we want the context of the friend, not
5702 the type of which it is a friend. */
5703 ctx
= CP_DECL_CONTEXT (decl
);
5704 else if (CP_DECL_CONTEXT (decl
)
5705 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5706 /* In the case of a virtual function, we want the class in which
5708 ctx
= CP_DECL_CONTEXT (decl
);
5710 /* Otherwise, if we're currently defining some class, the DECL
5711 is assumed to be a member of the class. */
5712 ctx
= current_scope ();
5714 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5717 if (!DECL_CONTEXT (decl
))
5718 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5720 /* See if this is a primary template. */
5721 if (is_friend
&& ctx
5722 && uses_template_parms_level (ctx
, processing_template_decl
))
5723 /* A friend template that specifies a class context, i.e.
5724 template <typename T> friend void A<T>::f();
5727 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5730 is_primary
= template_parm_scope_p ();
5734 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5736 if (DECL_CLASS_SCOPE_P (decl
))
5737 member_template_p
= true;
5738 if (TREE_CODE (decl
) == TYPE_DECL
5739 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5741 error ("template class without a name");
5742 return error_mark_node
;
5744 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5746 if (member_template_p
)
5748 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5749 error ("member template %qD may not have virt-specifiers", decl
);
5751 if (DECL_DESTRUCTOR_P (decl
))
5755 A destructor shall not be a member template. */
5756 error_at (DECL_SOURCE_LOCATION (decl
),
5757 "destructor %qD declared as member template", decl
);
5758 return error_mark_node
;
5760 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5761 && (!prototype_p (TREE_TYPE (decl
))
5762 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5763 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5764 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5765 == void_list_node
)))
5767 /* [basic.stc.dynamic.allocation]
5769 An allocation function can be a function
5770 template. ... Template allocation functions shall
5771 have two or more parameters. */
5772 error ("invalid template declaration of %qD", decl
);
5773 return error_mark_node
;
5776 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5777 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5779 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5780 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5781 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5783 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5784 if (TREE_CODE (t
) == TYPE_DECL
)
5786 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5787 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5790 else if (TREE_CODE (decl
) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl
))
5792 /* alias-declaration */
5793 gcc_assert (!DECL_ARTIFICIAL (decl
));
5794 else if (VAR_P (decl
))
5795 /* C++14 variable template. */;
5796 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5797 /* C++20 concept definitions. */;
5800 error ("template declaration of %q#D", decl
);
5801 return error_mark_node
;
5805 /* Check to see that the rules regarding the use of default
5806 arguments are not being violated. We check args for a friend
5807 functions when we know whether it's a definition, introducing
5808 declaration or re-declaration. */
5809 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5810 check_default_tmpl_args (decl
, current_template_parms
,
5811 is_primary
, is_partial
, is_friend
);
5813 /* Ensure that there are no parameter packs in the type of this
5814 declaration that have not been expanded. */
5815 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5817 /* Check each of the arguments individually to see if there are
5818 any bare parameter packs. */
5819 tree type
= TREE_TYPE (decl
);
5820 tree arg
= DECL_ARGUMENTS (decl
);
5821 tree argtype
= TYPE_ARG_TYPES (type
);
5823 while (arg
&& argtype
)
5825 if (!DECL_PACK_P (arg
)
5826 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5828 /* This is a PARM_DECL that contains unexpanded parameter
5829 packs. We have already complained about this in the
5830 check_for_bare_parameter_packs call, so just replace
5831 these types with ERROR_MARK_NODE. */
5832 TREE_TYPE (arg
) = error_mark_node
;
5833 TREE_VALUE (argtype
) = error_mark_node
;
5836 arg
= DECL_CHAIN (arg
);
5837 argtype
= TREE_CHAIN (argtype
);
5840 /* Check for bare parameter packs in the return type and the
5841 exception specifiers. */
5842 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5843 /* Errors were already issued, set return type to int
5844 as the frontend doesn't expect error_mark_node as
5846 TREE_TYPE (type
) = integer_type_node
;
5847 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5848 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5850 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5851 ? DECL_ORIGINAL_TYPE (decl
)
5852 : TREE_TYPE (decl
)))
5854 TREE_TYPE (decl
) = error_mark_node
;
5855 return error_mark_node
;
5859 return process_partial_specialization (decl
);
5861 args
= current_template_args ();
5864 || TREE_CODE (ctx
) == FUNCTION_DECL
5865 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5866 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5867 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5869 if (DECL_LANG_SPECIFIC (decl
)
5870 && DECL_TEMPLATE_INFO (decl
)
5871 && DECL_TI_TEMPLATE (decl
))
5872 tmpl
= DECL_TI_TEMPLATE (decl
);
5873 /* If DECL is a TYPE_DECL for a class-template, then there won't
5874 be DECL_LANG_SPECIFIC. The information equivalent to
5875 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5876 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5877 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5878 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5880 /* Since a template declaration already existed for this
5881 class-type, we must be redeclaring it here. Make sure
5882 that the redeclaration is valid. */
5883 redeclare_class_template (TREE_TYPE (decl
),
5884 current_template_parms
,
5885 current_template_constraints ());
5886 /* We don't need to create a new TEMPLATE_DECL; just use the
5887 one we already had. */
5888 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5892 tmpl
= build_template_decl (decl
, current_template_parms
,
5896 if (DECL_LANG_SPECIFIC (decl
)
5897 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5899 /* A specialization of a member template of a template
5901 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5902 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5903 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5909 tree a
, t
, current
, parms
;
5911 tree tinfo
= get_template_info (decl
);
5915 error ("template definition of non-template %q#D", decl
);
5916 return error_mark_node
;
5919 tmpl
= TI_TEMPLATE (tinfo
);
5921 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5922 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5923 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5924 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5928 /* The declaration is a specialization of a member
5929 template, declared outside the class. Therefore, the
5930 innermost template arguments will be NULL, so we
5931 replace them with the arguments determined by the
5932 earlier call to check_explicit_specialization. */
5933 args
= DECL_TI_ARGS (decl
);
5936 = build_template_decl (decl
, current_template_parms
,
5938 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5939 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5940 DECL_TEMPLATE_INFO (new_tmpl
)
5941 = build_template_info (tmpl
, args
);
5943 register_specialization (new_tmpl
,
5944 most_general_template (tmpl
),
5950 /* Make sure the template headers we got make sense. */
5952 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5953 i
= TMPL_PARMS_DEPTH (parms
);
5954 if (TMPL_ARGS_DEPTH (args
) != i
)
5956 error ("expected %d levels of template parms for %q#D, got %d",
5957 i
, decl
, TMPL_ARGS_DEPTH (args
));
5958 DECL_INTERFACE_KNOWN (decl
) = 1;
5959 return error_mark_node
;
5962 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5964 a
= TMPL_ARGS_LEVEL (args
, i
);
5965 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5967 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5969 if (current
== decl
)
5970 error ("got %d template parameters for %q#D",
5971 TREE_VEC_LENGTH (a
), decl
);
5973 error ("got %d template parameters for %q#T",
5974 TREE_VEC_LENGTH (a
), current
);
5975 error (" but %d required", TREE_VEC_LENGTH (t
));
5976 /* Avoid crash in import_export_decl. */
5977 DECL_INTERFACE_KNOWN (decl
) = 1;
5978 return error_mark_node
;
5981 if (current
== decl
)
5983 else if (current
== NULL_TREE
)
5984 /* Can happen in erroneous input. */
5987 current
= get_containing_scope (current
);
5990 /* Check that the parms are used in the appropriate qualifying scopes
5991 in the declarator. */
5992 if (!comp_template_args
5994 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5996 error ("template arguments to %qD do not match original "
5997 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5998 if (!uses_template_parms (TI_ARGS (tinfo
)))
5999 inform (input_location
, "use %<template<>%> for"
6000 " an explicit specialization");
6001 /* Avoid crash in import_export_decl. */
6002 DECL_INTERFACE_KNOWN (decl
) = 1;
6003 return error_mark_node
;
6007 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6011 /* Push template declarations for global functions and types.
6012 Note that we do not try to push a global template friend
6013 declared in a template class; such a thing may well depend on
6014 the template parameters of the class and we'll push it when
6015 instantiating the befriending class. */
6017 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6019 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
6020 if (tmpl
== error_mark_node
)
6021 return error_mark_node
;
6023 /* Hide template friend classes that haven't been declared yet. */
6024 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
6026 DECL_ANTICIPATED (tmpl
) = 1;
6027 DECL_FRIEND_P (tmpl
) = 1;
6032 /* The type may have been completed, or (erroneously) changed. */
6033 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6037 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6039 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6041 /* Give template template parms a DECL_CONTEXT of the template
6042 for which they are a parameter. */
6043 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6044 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6046 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6047 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6048 DECL_CONTEXT (parm
) = tmpl
;
6051 if (TREE_CODE (decl
) == TYPE_DECL
6052 && TYPE_DECL_ALIAS_P (decl
))
6055 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6057 /* ??? Why don't we do this here for all templates? */
6058 constr
= build_constraints (constr
, NULL_TREE
);
6059 set_constraints (decl
, constr
);
6061 if (complex_alias_template_p (tmpl
))
6062 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6066 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6067 back to its most general template. If TMPL is a specialization,
6068 ARGS may only have the innermost set of arguments. Add the missing
6069 argument levels if necessary. */
6070 if (DECL_TEMPLATE_INFO (tmpl
))
6071 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6073 info
= build_template_info (tmpl
, args
);
6075 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6076 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6080 retrofit_lang_decl (decl
);
6081 if (DECL_LANG_SPECIFIC (decl
)
6082 && !(VAR_OR_FUNCTION_DECL_P (decl
)
6083 && DECL_LOCAL_DECL_P (decl
)))
6084 DECL_TEMPLATE_INFO (decl
) = info
;
6087 if (flag_implicit_templates
6089 && TREE_PUBLIC (decl
)
6090 && VAR_OR_FUNCTION_DECL_P (decl
))
6091 /* Set DECL_COMDAT on template instantiations; if we force
6092 them to be emitted by explicit instantiation,
6093 mark_needed will tell cgraph to do the right thing. */
6094 DECL_COMDAT (decl
) = true;
6096 return DECL_TEMPLATE_RESULT (tmpl
);
6100 push_template_decl (tree decl
)
6102 return push_template_decl_real (decl
, false);
6105 /* FN is an inheriting constructor that inherits from the constructor
6106 template INHERITED; turn FN into a constructor template with a matching
6110 add_inherited_template_parms (tree fn
, tree inherited
)
6113 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6114 inner_parms
= copy_node (inner_parms
);
6116 = tree_cons (size_int (processing_template_decl
+ 1),
6117 inner_parms
, current_template_parms
);
6118 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6119 tree args
= template_parms_to_args (parms
);
6120 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6121 DECL_ARTIFICIAL (tmpl
) = true;
6122 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6126 /* Called when a class template TYPE is redeclared with the indicated
6127 template PARMS, e.g.:
6129 template <class T> struct S;
6130 template <class T> struct S {}; */
6133 redeclare_class_template (tree type
, tree parms
, tree cons
)
6139 if (!TYPE_TEMPLATE_INFO (type
))
6141 error ("%qT is not a template type", type
);
6145 tmpl
= TYPE_TI_TEMPLATE (type
);
6146 if (!PRIMARY_TEMPLATE_P (tmpl
))
6147 /* The type is nested in some template class. Nothing to worry
6148 about here; there are no new template parameters for the nested
6154 error ("template specifiers not specified in declaration of %qD",
6159 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6160 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6162 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6164 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6165 "redeclared with %d template parameter",
6166 "redeclared with %d template parameters",
6167 TREE_VEC_LENGTH (parms
));
6168 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6169 "previous declaration %qD used %d template parameter",
6170 "previous declaration %qD used %d template parameters",
6171 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6175 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6182 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6183 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6186 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6187 if (error_operand_p (tmpl_parm
))
6190 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6191 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6192 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6194 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6196 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6197 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6198 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6199 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6200 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6201 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6202 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6203 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6204 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6206 auto_diagnostic_group d
;
6207 error ("template parameter %q+#D", tmpl_parm
);
6208 inform (input_location
, "redeclared here as %q#D", parm
);
6212 /* The parameters can be declared to introduce different
6214 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6215 tree p2
= TREE_VEC_ELT (parms
, i
);
6216 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6218 auto_diagnostic_group d
;
6219 error ("declaration of template parameter %q+#D with different "
6220 "constraints", parm
);
6221 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6222 "original declaration appeared here");
6226 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6228 /* We have in [temp.param]:
6230 A template-parameter may not be given default arguments
6231 by two different declarations in the same scope. */
6232 auto_diagnostic_group d
;
6233 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6234 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6235 "original definition appeared here");
6239 if (parm_default
!= NULL_TREE
)
6240 /* Update the previous template parameters (which are the ones
6241 that will really count) with the new default value. */
6242 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6243 else if (tmpl_default
!= NULL_TREE
)
6244 /* Update the new parameters, too; they'll be used as the
6245 parameters for any members. */
6246 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6248 /* Give each template template parm in this redeclaration a
6249 DECL_CONTEXT of the template for which they are a parameter. */
6250 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6252 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6253 DECL_CONTEXT (parm
) = tmpl
;
6256 if (TREE_CODE (parm
) == TYPE_DECL
)
6257 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6260 tree ci
= get_constraints (tmpl
);
6261 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6262 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6264 /* Two classes with different constraints declare different entities. */
6265 if (!cp_tree_equal (req1
, req2
))
6267 auto_diagnostic_group d
;
6268 error_at (input_location
, "redeclaration %q#D with different "
6269 "constraints", tmpl
);
6270 inform (DECL_SOURCE_LOCATION (tmpl
),
6271 "original declaration appeared here");
6278 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6279 to be used when the caller has already checked
6280 (processing_template_decl
6281 && !instantiation_dependent_expression_p (expr)
6282 && potential_constant_expression (expr))
6283 and cleared processing_template_decl. */
6286 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6288 return tsubst_copy_and_build (expr
,
6291 /*in_decl=*/NULL_TREE
,
6292 /*function_p=*/false,
6293 /*integral_constant_expression_p=*/true);
6296 /* Simplify EXPR if it is a non-dependent expression. Returns the
6297 (possibly simplified) expression. */
6300 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6302 if (expr
== NULL_TREE
)
6305 /* If we're in a template, but EXPR isn't value dependent, simplify
6306 it. We're supposed to treat:
6308 template <typename T> void f(T[1 + 1]);
6309 template <typename T> void f(T[2]);
6311 as two declarations of the same function, for example. */
6312 if (processing_template_decl
6313 && is_nondependent_constant_expression (expr
))
6315 processing_template_decl_sentinel s
;
6316 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6322 instantiate_non_dependent_expr (tree expr
)
6324 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6327 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6328 an uninstantiated expression. */
6331 instantiate_non_dependent_or_null (tree expr
)
6333 if (expr
== NULL_TREE
)
6335 if (processing_template_decl
)
6337 if (!is_nondependent_constant_expression (expr
))
6341 processing_template_decl_sentinel s
;
6342 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6348 /* True iff T is a specialization of a variable template. */
6351 variable_template_specialization_p (tree t
)
6353 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6355 tree tmpl
= DECL_TI_TEMPLATE (t
);
6356 return variable_template_p (tmpl
);
6359 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6360 template declaration, or a TYPE_DECL for an alias declaration. */
6363 alias_type_or_template_p (tree t
)
6367 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6370 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6371 || DECL_ALIAS_TEMPLATE_P (t
));
6374 /* If T is a specialization of an alias template, return it; otherwise return
6375 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6378 alias_template_specialization_p (const_tree t
,
6379 bool transparent_typedefs
)
6384 /* It's an alias template specialization if it's an alias and its
6385 TYPE_NAME is a specialization of a primary template. */
6386 if (typedef_variant_p (t
))
6388 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6389 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6390 return CONST_CAST_TREE (t
);
6391 if (transparent_typedefs
)
6392 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6394 transparent_typedefs
);
6400 /* An alias template is complex from a SFINAE perspective if a template-id
6401 using that alias can be ill-formed when the expansion is not, as with
6402 the void_t template. We determine this by checking whether the
6403 expansion for the alias template uses all its template parameters. */
6405 struct uses_all_template_parms_data
6412 uses_all_template_parms_r (tree t
, void *data_
)
6414 struct uses_all_template_parms_data
&data
6415 = *(struct uses_all_template_parms_data
*)data_
;
6416 tree idx
= get_template_parm_index (t
);
6418 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6419 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6423 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6426 complex_pack_expansion_r (tree t
, void *data_
)
6428 /* An alias template with a pack expansion that expands a pack from the
6429 enclosing class needs to be considered complex, to avoid confusion with
6430 the same pack being used as an argument to the alias's own template
6431 parameter (91966). */
6432 if (!PACK_EXPANSION_P (t
))
6434 struct uses_all_template_parms_data
&data
6435 = *(struct uses_all_template_parms_data
*)data_
;
6436 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6437 pack
= TREE_CHAIN (pack
))
6439 tree parm_pack
= TREE_VALUE (pack
);
6440 if (!TEMPLATE_PARM_P (parm_pack
))
6443 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6444 if (level
< data
.level
)
6451 complex_alias_template_p (const_tree tmpl
)
6453 /* A renaming alias isn't complex. */
6454 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6457 /* Any other constrained alias is complex. */
6458 if (get_constraints (tmpl
))
6461 struct uses_all_template_parms_data data
;
6462 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6463 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6464 data
.level
= TMPL_PARMS_DEPTH (parms
);
6465 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6466 data
.seen
= XALLOCAVEC (bool, len
);
6467 for (int i
= 0; i
< len
; ++i
)
6468 data
.seen
[i
] = false;
6470 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6471 NULL
, true, complex_pack_expansion_r
))
6473 for (int i
= 0; i
< len
; ++i
)
6479 /* If T is a specialization of a complex alias template with dependent
6480 template-arguments, return it; otherwise return NULL_TREE. If T is a
6481 typedef to such a specialization, return the specialization. */
6484 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6486 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6489 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6491 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6492 && (any_dependent_template_arguments_p
6493 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6494 return CONST_CAST_TREE (t
);
6496 if (transparent_typedefs
)
6498 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6499 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6505 /* Return the number of innermost template parameters in TMPL. */
6508 num_innermost_template_parms (const_tree tmpl
)
6510 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6511 return TREE_VEC_LENGTH (parms
);
6514 /* Return either TMPL or another template that it is equivalent to under DR
6515 1286: An alias that just changes the name of a template is equivalent to
6516 the other template. */
6519 get_underlying_template (tree tmpl
)
6521 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6522 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6524 /* Determine if the alias is equivalent to an underlying template. */
6525 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6526 /* The underlying type may have been ill-formed. Don't proceed. */
6529 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6533 tree underlying
= TI_TEMPLATE (tinfo
);
6534 if (!PRIMARY_TEMPLATE_P (underlying
)
6535 || (num_innermost_template_parms (tmpl
)
6536 != num_innermost_template_parms (underlying
)))
6539 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6540 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6543 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6544 it's appropriate to treat a less-constrained alias as equivalent. */
6545 if (!at_least_as_constrained (underlying
, tmpl
))
6548 /* Alias is equivalent. Strip it and repeat. */
6555 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6556 must be a reference-to-function or a pointer-to-function type, as specified
6557 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6558 and check that the resulting function has external linkage. */
6561 convert_nontype_argument_function (tree type
, tree expr
,
6562 tsubst_flags_t complain
)
6566 linkage_kind linkage
;
6568 fn
= instantiate_type (type
, fns
, tf_none
);
6569 if (fn
== error_mark_node
)
6570 return error_mark_node
;
6572 if (value_dependent_expression_p (fn
))
6575 fn_no_ptr
= strip_fnptr_conv (fn
);
6576 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6577 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6578 if (BASELINK_P (fn_no_ptr
))
6579 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6581 /* [temp.arg.nontype]/1
6583 A template-argument for a non-type, non-template template-parameter
6586 -- the address of an object or function with external [C++11: or
6587 internal] linkage. */
6589 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6590 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6592 if (complain
& tf_error
)
6594 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6595 error_at (loc
, "%qE is not a valid template argument for type %qT",
6597 if (TYPE_PTR_P (type
))
6598 inform (loc
, "it must be the address of a function "
6599 "with external linkage");
6601 inform (loc
, "it must be the name of a function with "
6602 "external linkage");
6607 linkage
= decl_linkage (fn_no_ptr
);
6608 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6610 if (complain
& tf_error
)
6612 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6613 if (cxx_dialect
>= cxx11
)
6614 error_at (loc
, "%qE is not a valid template argument for type "
6615 "%qT because %qD has no linkage",
6616 expr
, type
, fn_no_ptr
);
6618 error_at (loc
, "%qE is not a valid template argument for type "
6619 "%qT because %qD does not have external linkage",
6620 expr
, type
, fn_no_ptr
);
6626 if (TYPE_REF_P (type
))
6628 if (REFERENCE_REF_P (fn
))
6629 fn
= TREE_OPERAND (fn
, 0);
6631 fn
= build_address (fn
);
6633 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6634 fn
= build_nop (type
, fn
);
6639 /* Subroutine of convert_nontype_argument.
6640 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6641 Emit an error otherwise. */
6644 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6645 tsubst_flags_t complain
)
6647 tree orig_expr
= expr
;
6649 if (null_ptr_cst_p (expr
))
6651 if (TREE_CODE (expr
) == PTRMEM_CST
6652 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6653 PTRMEM_CST_CLASS (expr
)))
6655 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6657 if (processing_template_decl
6658 && TREE_CODE (expr
) == ADDR_EXPR
6659 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6661 if (complain
& tf_error
)
6663 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6664 error_at (loc
, "%qE is not a valid template argument for type %qT",
6666 if (TREE_CODE (expr
) != PTRMEM_CST
)
6667 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6669 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6674 /* Returns TRUE iff the address of OP is value-dependent.
6676 14.6.2.4 [temp.dep.temp]:
6677 A non-integral non-type template-argument is dependent if its type is
6678 dependent or it has either of the following forms
6681 and contains a nested-name-specifier which specifies a class-name that
6682 names a dependent type.
6684 We generalize this to just say that the address of a member of a
6685 dependent class is value-dependent; the above doesn't cover the
6686 address of a static data member named with an unqualified-id. */
6689 has_value_dependent_address (tree op
)
6691 STRIP_ANY_LOCATION_WRAPPER (op
);
6693 /* We could use get_inner_reference here, but there's no need;
6694 this is only relevant for template non-type arguments, which
6695 can only be expressed as &id-expression. */
6698 tree ctx
= CP_DECL_CONTEXT (op
);
6699 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6706 /* The next set of functions are used for providing helpful explanatory
6707 diagnostics for failed overload resolution. Their messages should be
6708 indented by two spaces for consistency with the messages in
6712 unify_success (bool /*explain_p*/)
6717 /* Other failure functions should call this one, to provide a single function
6718 for setting a breakpoint on. */
6721 unify_invalid (bool /*explain_p*/)
6727 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6730 inform (input_location
,
6731 " couldn%'t deduce template parameter %qD", parm
);
6732 return unify_invalid (explain_p
);
6736 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6739 inform (input_location
,
6740 " types %qT and %qT have incompatible cv-qualifiers",
6742 return unify_invalid (explain_p
);
6746 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6749 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6750 return unify_invalid (explain_p
);
6754 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6757 inform (input_location
,
6758 " template parameter %qD is not a parameter pack, but "
6761 return unify_invalid (explain_p
);
6765 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6768 inform (input_location
,
6769 " template argument %qE does not match "
6770 "pointer-to-member constant %qE",
6772 return unify_invalid (explain_p
);
6776 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6779 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6780 return unify_invalid (explain_p
);
6784 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6787 inform (input_location
,
6788 " inconsistent parameter pack deduction with %qT and %qT",
6790 return unify_invalid (explain_p
);
6794 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6799 inform (input_location
,
6800 " deduced conflicting types for parameter %qT (%qT and %qT)",
6801 parm
, first
, second
);
6803 inform (input_location
,
6804 " deduced conflicting values for non-type parameter "
6805 "%qE (%qE and %qE)", parm
, first
, second
);
6807 return unify_invalid (explain_p
);
6811 unify_vla_arg (bool explain_p
, tree arg
)
6814 inform (input_location
,
6815 " variable-sized array type %qT is not "
6816 "a valid template argument",
6818 return unify_invalid (explain_p
);
6822 unify_method_type_error (bool explain_p
, tree arg
)
6825 inform (input_location
,
6826 " member function type %qT is not a valid template argument",
6828 return unify_invalid (explain_p
);
6832 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6837 inform_n (input_location
, wanted
,
6838 " candidate expects at least %d argument, %d provided",
6839 " candidate expects at least %d arguments, %d provided",
6842 inform_n (input_location
, wanted
,
6843 " candidate expects %d argument, %d provided",
6844 " candidate expects %d arguments, %d provided",
6847 return unify_invalid (explain_p
);
6851 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6853 return unify_arity (explain_p
, have
, wanted
);
6857 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6858 bool least_p
= false)
6860 return unify_arity (explain_p
, have
, wanted
, least_p
);
6864 unify_arg_conversion (bool explain_p
, tree to_type
,
6865 tree from_type
, tree arg
)
6868 inform (cp_expr_loc_or_input_loc (arg
),
6869 " cannot convert %qE (type %qT) to type %qT",
6870 arg
, from_type
, to_type
);
6871 return unify_invalid (explain_p
);
6875 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6876 tree parm
, tree arg
)
6881 case tbr_ambiguous_baseclass
:
6882 inform (input_location
, " %qT is an ambiguous base class of %qT",
6886 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6889 return unify_invalid (explain_p
);
6893 unify_inconsistent_template_template_parameters (bool explain_p
)
6896 inform (input_location
,
6897 " template parameters of a template template argument are "
6898 "inconsistent with other deduced template arguments");
6899 return unify_invalid (explain_p
);
6903 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6906 inform (input_location
,
6907 " cannot deduce a template for %qT from non-template type %qT",
6909 return unify_invalid (explain_p
);
6913 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6916 inform (input_location
,
6917 " template argument %qE does not match %qE", arg
, parm
);
6918 return unify_invalid (explain_p
);
6921 /* True if T is a C++20 template parameter object to store the argument for a
6922 template parameter of class type. */
6925 template_parm_object_p (const_tree t
)
6927 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6928 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6931 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6932 argument for TYPE, points to an unsuitable object.
6934 Also adjust the type of the index in C++20 array subobject references. */
6937 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6939 switch (TREE_CODE (expr
))
6942 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6946 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6951 unsigned i
; tree elt
;
6952 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6953 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6960 tree decl
= TREE_OPERAND (expr
, 0);
6962 if (cxx_dialect
>= cxx20
)
6963 while (TREE_CODE (decl
) == COMPONENT_REF
6964 || TREE_CODE (decl
) == ARRAY_REF
)
6966 tree
&op
= TREE_OPERAND (decl
, 1);
6967 if (TREE_CODE (decl
) == ARRAY_REF
6968 && TREE_CODE (op
) == INTEGER_CST
)
6969 /* Canonicalize array offsets to ptrdiff_t; how they were
6970 written doesn't matter for subobject identity. */
6971 op
= fold_convert (ptrdiff_type_node
, op
);
6972 decl
= TREE_OPERAND (decl
, 0);
6977 if (complain
& tf_error
)
6978 error_at (cp_expr_loc_or_input_loc (expr
),
6979 "%qE is not a valid template argument of type %qT "
6980 "because %qE is not a variable", expr
, type
, decl
);
6983 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6985 if (complain
& tf_error
)
6986 error_at (cp_expr_loc_or_input_loc (expr
),
6987 "%qE is not a valid template argument of type %qT "
6988 "in C++98 because %qD does not have external linkage",
6992 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6993 && decl_linkage (decl
) == lk_none
)
6995 if (complain
& tf_error
)
6996 error_at (cp_expr_loc_or_input_loc (expr
),
6997 "%qE is not a valid template argument of type %qT "
6998 "because %qD has no linkage", expr
, type
, decl
);
7001 /* C++17: For a non-type template-parameter of reference or pointer
7002 type, the value of the constant expression shall not refer to (or
7003 for a pointer type, shall not be the address of):
7004 * a subobject (4.5),
7005 * a temporary object (15.2),
7006 * a string literal (5.13.5),
7007 * the result of a typeid expression (8.2.8), or
7008 * a predefined __func__ variable (11.4.1). */
7009 else if (DECL_ARTIFICIAL (decl
))
7011 if (complain
& tf_error
)
7012 error ("the address of %qD is not a valid template argument",
7016 else if (cxx_dialect
< cxx20
7017 && !(same_type_ignoring_top_level_qualifiers_p
7018 (strip_array_types (TREE_TYPE (type
)),
7019 strip_array_types (TREE_TYPE (decl
)))))
7021 if (complain
& tf_error
)
7022 error ("the address of the %qT subobject of %qD is not a "
7023 "valid template argument", TREE_TYPE (type
), decl
);
7026 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7028 if (complain
& tf_error
)
7029 error ("the address of %qD is not a valid template argument "
7030 "because it does not have static storage duration",
7038 if (!INDIRECT_TYPE_P (type
))
7039 /* We're only concerned about pointers and references here. */;
7040 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7041 /* Null pointer values are OK in C++11. */;
7046 if (complain
& tf_error
)
7047 error ("%qD is not a valid template argument "
7048 "because %qD is a variable, not the address of "
7049 "a variable", expr
, expr
);
7054 if (complain
& tf_error
)
7055 error ("%qE is not a valid template argument for %qT "
7056 "because it is not the address of a variable",
7066 /* The template arguments corresponding to template parameter objects of types
7067 that contain pointers to members. */
7069 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7071 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7072 template argument EXPR. */
7075 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7077 if (TREE_CODE (expr
) == TARGET_EXPR
)
7078 expr
= TARGET_EXPR_INITIAL (expr
);
7080 if (!TREE_CONSTANT (expr
))
7082 if ((complain
& tf_error
)
7083 && require_rvalue_constant_expression (expr
))
7084 cxx_constant_value (expr
);
7085 return error_mark_node
;
7087 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7088 return error_mark_node
;
7090 tree name
= mangle_template_parm_object (expr
);
7091 tree decl
= get_global_binding (name
);
7095 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7096 decl
= create_temporary_var (type
);
7097 TREE_STATIC (decl
) = true;
7098 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7099 TREE_READONLY (decl
) = true;
7100 DECL_NAME (decl
) = name
;
7101 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7102 DECL_CONTEXT (decl
) = global_namespace
;
7103 comdat_linkage (decl
);
7105 if (!zero_init_p (type
))
7107 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7108 lower_var_init before we're done mangling. So store the original
7110 tree copy
= unshare_constructor (expr
);
7111 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7114 pushdecl_top_level_and_finish (decl
, expr
);
7119 /* Return the actual template argument corresponding to template parameter
7123 tparm_object_argument (tree var
)
7125 if (zero_init_p (TREE_TYPE (var
)))
7126 return DECL_INITIAL (var
);
7127 return *(tparm_obj_values
->get (var
));
7130 /* Attempt to convert the non-type template parameter EXPR to the
7131 indicated TYPE. If the conversion is successful, return the
7132 converted value. If the conversion is unsuccessful, return
7133 NULL_TREE if we issued an error message, or error_mark_node if we
7134 did not. We issue error messages for out-and-out bad template
7135 parameters, but not simply because the conversion failed, since we
7136 might be just trying to do argument deduction. Both TYPE and EXPR
7137 must be non-dependent.
7139 The conversion follows the special rules described in
7140 [temp.arg.nontype], and it is much more strict than an implicit
7143 This function is called twice for each template argument (see
7144 lookup_template_class for a more accurate description of this
7145 problem). This means that we need to handle expressions which
7146 are not valid in a C++ source, but can be created from the
7147 first call (for instance, casts to perform conversions). These
7148 hacks can go away after we fix the double coercion problem. */
7151 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7154 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7156 /* Detect immediately string literals as invalid non-type argument.
7157 This special-case is not needed for correctness (we would easily
7158 catch this later), but only to provide better diagnostic for this
7159 common user mistake. As suggested by DR 100, we do not mention
7160 linkage issues in the diagnostic as this is not the point. */
7161 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7163 if (complain
& tf_error
)
7164 error ("%qE is not a valid template argument for type %qT "
7165 "because string literals can never be used in this context",
7170 /* Add the ADDR_EXPR now for the benefit of
7171 value_dependent_expression_p. */
7172 if (TYPE_PTROBV_P (type
)
7173 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7175 expr
= decay_conversion (expr
, complain
);
7176 if (expr
== error_mark_node
)
7177 return error_mark_node
;
7180 /* If we are in a template, EXPR may be non-dependent, but still
7181 have a syntactic, rather than semantic, form. For example, EXPR
7182 might be a SCOPE_REF, rather than the VAR_DECL to which the
7183 SCOPE_REF refers. Preserving the qualifying scope is necessary
7184 so that access checking can be performed when the template is
7185 instantiated -- but here we need the resolved form so that we can
7186 convert the argument. */
7187 bool non_dep
= false;
7188 if (TYPE_REF_OBJ_P (type
)
7189 && has_value_dependent_address (expr
))
7190 /* If we want the address and it's value-dependent, don't fold. */;
7191 else if (processing_template_decl
7192 && is_nondependent_constant_expression (expr
))
7194 if (error_operand_p (expr
))
7195 return error_mark_node
;
7196 expr_type
= TREE_TYPE (expr
);
7198 /* If the argument is non-dependent, perform any conversions in
7199 non-dependent context as well. */
7200 processing_template_decl_sentinel
s (non_dep
);
7202 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7204 const bool val_dep_p
= value_dependent_expression_p (expr
);
7206 expr
= canonicalize_expr_argument (expr
, complain
);
7208 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7209 to a non-type argument of "nullptr". */
7210 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7211 expr
= fold_simple (convert (type
, expr
));
7213 /* In C++11, integral or enumeration non-type template arguments can be
7214 arbitrary constant expressions. Pointer and pointer to
7215 member arguments can be general constant expressions that evaluate
7216 to a null value, but otherwise still need to be of a specific form. */
7217 if (cxx_dialect
>= cxx11
)
7219 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7220 /* A PTRMEM_CST is already constant, and a valid template
7221 argument for a parameter of pointer to member type, we just want
7222 to leave it in that form rather than lower it to a
7224 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7225 || cxx_dialect
>= cxx17
)
7227 /* C++17: A template-argument for a non-type template-parameter shall
7228 be a converted constant expression (8.20) of the type of the
7229 template-parameter. */
7230 expr
= build_converted_constant_expr (type
, expr
, complain
);
7231 if (expr
== error_mark_node
)
7232 /* Make sure we return NULL_TREE only if we have really issued
7233 an error, as described above. */
7234 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7235 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7237 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7240 expr
= maybe_constant_value (expr
, NULL_TREE
,
7241 /*manifestly_const_eval=*/true);
7242 expr
= convert_from_reference (expr
);
7244 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7246 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7247 /*manifestly_const_eval=*/true);
7248 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7249 : null_member_pointer_value_p (folded
))
7254 if (TYPE_REF_P (type
))
7255 expr
= mark_lvalue_use (expr
);
7257 expr
= mark_rvalue_use (expr
);
7259 /* HACK: Due to double coercion, we can get a
7260 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7261 which is the tree that we built on the first call (see
7262 below when coercing to reference to object or to reference to
7263 function). We just strip everything and get to the arg.
7264 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7266 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7268 tree probe_type
, probe
= expr
;
7269 if (REFERENCE_REF_P (probe
))
7270 probe
= TREE_OPERAND (probe
, 0);
7271 probe_type
= TREE_TYPE (probe
);
7272 if (TREE_CODE (probe
) == NOP_EXPR
)
7274 /* ??? Maybe we could use convert_from_reference here, but we
7275 would need to relax its constraints because the NOP_EXPR
7276 could actually change the type to something more cv-qualified,
7277 and this is not folded by convert_from_reference. */
7278 tree addr
= TREE_OPERAND (probe
, 0);
7279 if (TYPE_REF_P (probe_type
)
7280 && TREE_CODE (addr
) == ADDR_EXPR
7281 && TYPE_PTR_P (TREE_TYPE (addr
))
7282 && (same_type_ignoring_top_level_qualifiers_p
7283 (TREE_TYPE (probe_type
),
7284 TREE_TYPE (TREE_TYPE (addr
)))))
7286 expr
= TREE_OPERAND (addr
, 0);
7287 expr_type
= TREE_TYPE (probe_type
);
7292 /* [temp.arg.nontype]/5, bullet 1
7294 For a non-type template-parameter of integral or enumeration type,
7295 integral promotions (_conv.prom_) and integral conversions
7296 (_conv.integral_) are applied. */
7297 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7298 || TREE_CODE (type
) == REAL_TYPE
)
7300 if (cxx_dialect
< cxx11
)
7302 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7303 t
= maybe_constant_value (t
);
7304 if (t
!= error_mark_node
)
7308 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7309 return error_mark_node
;
7311 /* Notice that there are constant expressions like '4 % 0' which
7312 do not fold into integer constants. */
7313 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7315 if (complain
& tf_error
)
7317 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7318 if (!require_potential_constant_expression (expr
))
7319 expr
= error_mark_node
;
7321 expr
= cxx_constant_value (expr
);
7322 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7323 inform (loc
, "in template argument for type %qT", type
);
7324 if (expr
== error_mark_node
)
7326 /* else cxx_constant_value complained but gave us
7327 a real constant, so go ahead. */
7328 if (!CONSTANT_CLASS_P (expr
))
7330 /* Some assemble time constant expressions like
7331 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7332 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7333 as we can emit them into .rodata initializers of
7334 variables, yet they can't fold into an INTEGER_CST at
7335 compile time. Refuse them here. */
7336 gcc_checking_assert (reduced_constant_expression_p (expr
));
7337 error_at (loc
, "template argument %qE for type %qT not "
7338 "a compile-time constant", expr
, type
);
7346 /* Avoid typedef problems. */
7347 if (TREE_TYPE (expr
) != type
)
7348 expr
= fold_convert (type
, expr
);
7350 /* [temp.arg.nontype]/5, bullet 2
7352 For a non-type template-parameter of type pointer to object,
7353 qualification conversions (_conv.qual_) and the array-to-pointer
7354 conversion (_conv.array_) are applied. */
7355 else if (TYPE_PTROBV_P (type
))
7357 tree decayed
= expr
;
7359 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7360 decay_conversion or an explicit cast. If it's a problematic cast,
7361 we'll complain about it below. */
7362 if (TREE_CODE (expr
) == NOP_EXPR
)
7366 if (TREE_CODE (probe
) == ADDR_EXPR
7367 && TYPE_PTR_P (TREE_TYPE (probe
)))
7370 expr_type
= TREE_TYPE (expr
);
7374 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7376 A template-argument for a non-type, non-template template-parameter
7377 shall be one of: [...]
7379 -- the name of a non-type template-parameter;
7380 -- the address of an object or function with external linkage, [...]
7381 expressed as "& id-expression" where the & is optional if the name
7382 refers to a function or array, or if the corresponding
7383 template-parameter is a reference.
7385 Here, we do not care about functions, as they are invalid anyway
7386 for a parameter of type pointer-to-object. */
7389 /* Non-type template parameters are OK. */
7391 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7392 /* Null pointer values are OK in C++11. */;
7393 else if (TREE_CODE (expr
) != ADDR_EXPR
7394 && !INDIRECT_TYPE_P (expr_type
))
7395 /* Other values, like integer constants, might be valid
7396 non-type arguments of some other type. */
7397 return error_mark_node
;
7398 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7403 expr
= perform_qualification_conversions (type
, expr
);
7404 if (expr
== error_mark_node
)
7405 return error_mark_node
;
7407 /* [temp.arg.nontype]/5, bullet 3
7409 For a non-type template-parameter of type reference to object, no
7410 conversions apply. The type referred to by the reference may be more
7411 cv-qualified than the (otherwise identical) type of the
7412 template-argument. The template-parameter is bound directly to the
7413 template-argument, which must be an lvalue. */
7414 else if (TYPE_REF_OBJ_P (type
))
7416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7418 return error_mark_node
;
7420 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7422 if (complain
& tf_error
)
7423 error ("%qE is not a valid template argument for type %qT "
7424 "because of conflicts in cv-qualification", expr
, type
);
7428 if (!lvalue_p (expr
))
7430 if (complain
& tf_error
)
7431 error ("%qE is not a valid template argument for type %qT "
7432 "because it is not an lvalue", expr
, type
);
7436 /* [temp.arg.nontype]/1
7438 A template-argument for a non-type, non-template template-parameter
7439 shall be one of: [...]
7441 -- the address of an object or function with external linkage. */
7442 if (INDIRECT_REF_P (expr
)
7443 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7445 expr
= TREE_OPERAND (expr
, 0);
7448 if (complain
& tf_error
)
7449 error ("%q#D is not a valid template argument for type %qT "
7450 "because a reference variable does not have a constant "
7451 "address", expr
, type
);
7456 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7457 /* OK, dependent reference. We don't want to ask whether a DECL is
7458 itself value-dependent, since what we want here is its address. */;
7461 expr
= build_address (expr
);
7463 if (invalid_tparm_referent_p (type
, expr
, complain
))
7467 if (!same_type_p (type
, TREE_TYPE (expr
)))
7468 expr
= build_nop (type
, expr
);
7470 /* [temp.arg.nontype]/5, bullet 4
7472 For a non-type template-parameter of type pointer to function, only
7473 the function-to-pointer conversion (_conv.func_) is applied. If the
7474 template-argument represents a set of overloaded functions (or a
7475 pointer to such), the matching function is selected from the set
7477 else if (TYPE_PTRFN_P (type
))
7479 /* If the argument is a template-id, we might not have enough
7480 context information to decay the pointer. */
7481 if (!type_unknown_p (expr_type
))
7483 expr
= decay_conversion (expr
, complain
);
7484 if (expr
== error_mark_node
)
7485 return error_mark_node
;
7488 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7489 /* Null pointer values are OK in C++11. */
7490 return perform_qualification_conversions (type
, expr
);
7492 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7493 if (!expr
|| expr
== error_mark_node
)
7496 /* [temp.arg.nontype]/5, bullet 5
7498 For a non-type template-parameter of type reference to function, no
7499 conversions apply. If the template-argument represents a set of
7500 overloaded functions, the matching function is selected from the set
7502 else if (TYPE_REFFN_P (type
))
7504 if (TREE_CODE (expr
) == ADDR_EXPR
)
7506 if (complain
& tf_error
)
7508 error ("%qE is not a valid template argument for type %qT "
7509 "because it is a pointer", expr
, type
);
7510 inform (input_location
, "try using %qE instead",
7511 TREE_OPERAND (expr
, 0));
7516 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7517 if (!expr
|| expr
== error_mark_node
)
7520 /* [temp.arg.nontype]/5, bullet 6
7522 For a non-type template-parameter of type pointer to member function,
7523 no conversions apply. If the template-argument represents a set of
7524 overloaded member functions, the matching member function is selected
7525 from the set (_over.over_). */
7526 else if (TYPE_PTRMEMFUNC_P (type
))
7528 expr
= instantiate_type (type
, expr
, tf_none
);
7529 if (expr
== error_mark_node
)
7530 return error_mark_node
;
7532 /* [temp.arg.nontype] bullet 1 says the pointer to member
7533 expression must be a pointer-to-member constant. */
7535 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7538 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7539 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7540 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7541 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7543 /* [temp.arg.nontype]/5, bullet 7
7545 For a non-type template-parameter of type pointer to data member,
7546 qualification conversions (_conv.qual_) are applied. */
7547 else if (TYPE_PTRDATAMEM_P (type
))
7549 /* [temp.arg.nontype] bullet 1 says the pointer to member
7550 expression must be a pointer-to-member constant. */
7552 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7555 expr
= perform_qualification_conversions (type
, expr
);
7556 if (expr
== error_mark_node
)
7559 else if (NULLPTR_TYPE_P (type
))
7561 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7563 if (complain
& tf_error
)
7564 error ("%qE is not a valid template argument for type %qT "
7565 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7570 else if (CLASS_TYPE_P (type
))
7572 /* Replace the argument with a reference to the corresponding template
7573 parameter object. */
7575 expr
= get_template_parm_object (expr
, complain
);
7576 if (expr
== error_mark_node
)
7579 /* A template non-type parameter must be one of the above. */
7583 /* Sanity check: did we actually convert the argument to the
7585 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7586 (type
, TREE_TYPE (expr
)));
7587 return convert_from_reference (expr
);
7590 /* Subroutine of coerce_template_template_parms, which returns 1 if
7591 PARM_PARM and ARG_PARM match using the rule for the template
7592 parameters of template template parameters. Both PARM and ARG are
7593 template parameters; the rest of the arguments are the same as for
7594 coerce_template_template_parms.
7597 coerce_template_template_parm (tree parm
,
7599 tsubst_flags_t complain
,
7603 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7604 || parm
== NULL_TREE
|| error_operand_p (parm
))
7607 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7610 switch (TREE_CODE (parm
))
7613 /* We encounter instantiations of templates like
7614 template <template <template <class> class> class TT>
7617 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7618 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7620 if (!coerce_template_template_parms
7621 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7627 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7628 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7629 /* Argument is a parameter pack but parameter is not. */
7634 /* The tsubst call is used to handle cases such as
7636 template <int> class C {};
7637 template <class T, template <T> class TT> class D {};
7640 i.e. the parameter list of TT depends on earlier parameters. */
7641 if (!uses_template_parms (TREE_TYPE (arg
)))
7643 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7644 if (!uses_template_parms (t
)
7645 && !same_type_p (t
, TREE_TYPE (arg
)))
7649 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7650 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7651 /* Argument is a parameter pack but parameter is not. */
7663 /* Coerce template argument list ARGLIST for use with template
7664 template-parameter TEMPL. */
7667 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7668 tsubst_flags_t complain
)
7670 /* Consider an example where a template template parameter declared as
7672 template <class T, class U = std::allocator<T> > class TT
7674 The template parameter level of T and U are one level larger than
7675 of TT. To proper process the default argument of U, say when an
7676 instantiation `TT<int>' is seen, we need to build the full
7677 arguments containing {int} as the innermost level. Outer levels,
7678 available when not appearing as default template argument, can be
7679 obtained from the arguments of the enclosing template.
7681 Suppose that TT is later substituted with std::vector. The above
7682 instantiation is `TT<int, std::allocator<T> >' with TT at
7683 level 1, and T at level 2, while the template arguments at level 1
7684 becomes {std::vector} and the inner level 2 is {int}. */
7686 tree outer
= DECL_CONTEXT (templ
);
7688 outer
= generic_targs_for (outer
);
7689 else if (current_template_parms
)
7691 /* This is an argument of the current template, so we haven't set
7692 DECL_CONTEXT yet. */
7693 tree relevant_template_parms
;
7695 /* Parameter levels that are greater than the level of the given
7696 template template parm are irrelevant. */
7697 relevant_template_parms
= current_template_parms
;
7698 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7699 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7700 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7702 outer
= template_parms_to_args (relevant_template_parms
);
7706 arglist
= add_to_template_args (outer
, arglist
);
7708 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7709 return coerce_template_parms (parmlist
, arglist
, templ
,
7711 /*require_all_args=*/true,
7712 /*use_default_args=*/true);
7715 /* A cache of template template parameters with match-all default
7717 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7719 /* T is a bound template template-parameter. Copy its arguments into default
7720 arguments of the template template-parameter's template parameters. */
7723 add_defaults_to_ttp (tree otmpl
)
7725 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7728 tree ntmpl
= copy_node (otmpl
);
7730 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7731 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7732 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7733 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7734 TYPE_NAME (ntype
) = ntmpl
;
7735 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7737 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7738 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7739 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7740 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7742 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7743 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7744 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7745 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7746 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7748 tree o
= TREE_VEC_ELT (vec
, i
);
7749 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7751 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7752 TREE_PURPOSE (n
) = any_targ_node
;
7756 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7760 /* ARG is a bound potential template template-argument, and PARGS is a list
7761 of arguments for the corresponding template template-parameter. Adjust
7762 PARGS as appropriate for application to ARG's template, and if ARG is a
7763 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7764 arguments to the template template parameter. */
7767 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7769 ++processing_template_decl
;
7770 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7771 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7773 /* When comparing two template template-parameters in partial ordering,
7774 rewrite the one currently being used as an argument to have default
7775 arguments for all parameters. */
7776 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7777 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7778 if (pargs
!= error_mark_node
)
7779 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7780 TYPE_TI_ARGS (arg
));
7785 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7786 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7787 /*require_all*/true,
7788 /*use_default*/true);
7790 --processing_template_decl
;
7794 /* Subroutine of unify for the case when PARM is a
7795 BOUND_TEMPLATE_TEMPLATE_PARM. */
7798 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7801 tree parmvec
= TYPE_TI_ARGS (parm
);
7802 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7804 /* The template template parm might be variadic and the argument
7805 not, so flatten both argument lists. */
7806 parmvec
= expand_template_argument_pack (parmvec
);
7807 argvec
= expand_template_argument_pack (argvec
);
7811 /* In keeping with P0522R0, adjust P's template arguments
7812 to apply to A's template; then flatten it again. */
7813 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7814 nparmvec
= expand_template_argument_pack (nparmvec
);
7816 if (unify (tparms
, targs
, nparmvec
, argvec
,
7817 UNIFY_ALLOW_NONE
, explain_p
))
7820 /* If the P0522 adjustment eliminated a pack expansion, deduce
7823 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7824 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7825 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7830 /* Deduce arguments T, i from TT<T> or TT<i>.
7831 We check each element of PARMVEC and ARGVEC individually
7832 rather than the whole TREE_VEC since they can have
7833 different number of elements, which is allowed under N2555. */
7835 int len
= TREE_VEC_LENGTH (parmvec
);
7837 /* Check if the parameters end in a pack, making them
7839 int parm_variadic_p
= 0;
7841 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7842 parm_variadic_p
= 1;
7844 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7845 /* If the template argument list of P contains a pack
7846 expansion that is not the last template argument, the
7847 entire template argument list is a non-deduced
7849 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7850 return unify_success (explain_p
);
7852 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7853 return unify_too_few_arguments (explain_p
,
7854 TREE_VEC_LENGTH (argvec
), len
);
7856 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7857 if (unify (tparms
, targs
,
7858 TREE_VEC_ELT (parmvec
, i
),
7859 TREE_VEC_ELT (argvec
, i
),
7860 UNIFY_ALLOW_NONE
, explain_p
))
7864 && unify_pack_expansion (tparms
, targs
,
7867 /*subr=*/true, explain_p
))
7874 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7875 template template parameters. Both PARM_PARMS and ARG_PARMS are
7876 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7879 Consider the example:
7880 template <class T> class A;
7881 template<template <class U> class TT> class B;
7883 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7884 the parameters to A, and OUTER_ARGS contains A. */
7887 coerce_template_template_parms (tree parm_parms
,
7889 tsubst_flags_t complain
,
7893 int nparms
, nargs
, i
;
7897 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7898 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7900 nparms
= TREE_VEC_LENGTH (parm_parms
);
7901 nargs
= TREE_VEC_LENGTH (arg_parms
);
7905 /* P0522R0: A template template-parameter P is at least as specialized as
7906 a template template-argument A if, given the following rewrite to two
7907 function templates, the function template corresponding to P is at
7908 least as specialized as the function template corresponding to A
7909 according to the partial ordering rules for function templates
7910 ([temp.func.order]). Given an invented class template X with the
7911 template parameter list of A (including default arguments):
7913 * Each of the two function templates has the same template parameters,
7914 respectively, as P or A.
7916 * Each function template has a single function parameter whose type is
7917 a specialization of X with template arguments corresponding to the
7918 template parameters from the respective function template where, for
7919 each template parameter PP in the template parameter list of the
7920 function template, a corresponding template argument AA is formed. If
7921 PP declares a parameter pack, then AA is the pack expansion
7922 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7924 If the rewrite produces an invalid type, then P is not at least as
7925 specialized as A. */
7927 /* So coerce P's args to apply to A's parms, and then deduce between A's
7928 args and the converted args. If that succeeds, A is at least as
7929 specialized as P, so they match.*/
7930 tree pargs
= template_parms_level_to_args (parm_parms
);
7931 pargs
= add_outermost_template_args (outer_args
, pargs
);
7932 ++processing_template_decl
;
7933 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7934 /*require_all*/true, /*use_default*/true);
7935 --processing_template_decl
;
7936 if (pargs
!= error_mark_node
)
7938 tree targs
= make_tree_vec (nargs
);
7939 tree aargs
= template_parms_level_to_args (arg_parms
);
7940 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7946 /* Determine whether we have a parameter pack at the end of the
7947 template template parameter's template parameter list. */
7948 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7950 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7952 if (error_operand_p (parm
))
7955 switch (TREE_CODE (parm
))
7959 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7964 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7974 && !(variadic_p
&& nargs
>= nparms
- 1))
7977 /* Check all of the template parameters except the parameter pack at
7978 the end (if any). */
7979 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7981 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7982 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7985 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7986 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7988 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7996 /* Check each of the template parameters in the template
7997 argument against the template parameter pack at the end of
7998 the template template parameter. */
7999 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8002 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8004 for (; i
< nargs
; ++i
)
8006 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8009 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8011 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8020 /* Verifies that the deduced template arguments (in TARGS) for the
8021 template template parameters (in TPARMS) represent valid bindings,
8022 by comparing the template parameter list of each template argument
8023 to the template parameter list of its corresponding template
8024 template parameter, in accordance with DR150. This
8025 routine can only be called after all template arguments have been
8026 deduced. It will return TRUE if all of the template template
8027 parameter bindings are okay, FALSE otherwise. */
8029 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8031 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8034 /* We're dealing with template parms in this process. */
8035 ++processing_template_decl
;
8037 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8039 for (i
= 0; i
< ntparms
; ++i
)
8041 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8042 tree targ
= TREE_VEC_ELT (targs
, i
);
8044 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8046 tree packed_args
= NULL_TREE
;
8049 if (ARGUMENT_PACK_P (targ
))
8051 /* Look inside the argument pack. */
8052 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8053 len
= TREE_VEC_LENGTH (packed_args
);
8056 for (idx
= 0; idx
< len
; ++idx
)
8058 tree targ_parms
= NULL_TREE
;
8061 /* Extract the next argument from the argument
8063 targ
= TREE_VEC_ELT (packed_args
, idx
);
8065 if (PACK_EXPANSION_P (targ
))
8066 /* Look at the pattern of the pack expansion. */
8067 targ
= PACK_EXPANSION_PATTERN (targ
);
8069 /* Extract the template parameters from the template
8071 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8072 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8073 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8074 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8076 /* Verify that we can coerce the template template
8077 parameters from the template argument to the template
8078 parameter. This requires an exact match. */
8080 && !coerce_template_template_parms
8081 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8096 --processing_template_decl
;
8100 /* Since type attributes aren't mangled, we need to strip them from
8101 template type arguments. */
8104 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8106 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8108 bool removed_attributes
= false;
8109 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8110 if (removed_attributes
8111 && (complain
& tf_warning
))
8112 warning (OPT_Wignored_attributes
,
8113 "ignoring attributes on template argument %qT", arg
);
8117 /* And from inside dependent non-type arguments like sizeof(Type). */
8120 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8122 if (!arg
|| arg
== error_mark_node
)
8124 bool removed_attributes
= false;
8125 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8126 if (removed_attributes
8127 && (complain
& tf_warning
))
8128 warning (OPT_Wignored_attributes
,
8129 "ignoring attributes in template argument %qE", arg
);
8133 // A template declaration can be substituted for a constrained
8134 // template template parameter only when the argument is more
8135 // constrained than the parameter.
8137 is_compatible_template_arg (tree parm
, tree arg
)
8139 tree parm_cons
= get_constraints (parm
);
8141 /* For now, allow constrained template template arguments
8142 and unconstrained template template parameters. */
8143 if (parm_cons
== NULL_TREE
)
8146 /* If the template parameter is constrained, we need to rewrite its
8147 constraints in terms of the ARG's template parameters. This ensures
8148 that all of the template parameter types will have the same depth.
8150 Note that this is only valid when coerce_template_template_parm is
8151 true for the innermost template parameters of PARM and ARG. In other
8152 words, because coercion is successful, this conversion will be valid. */
8153 tree new_args
= NULL_TREE
;
8156 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8157 new_args
= template_parms_level_to_args (aparms
);
8158 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8159 tf_none
, NULL_TREE
);
8160 if (parm_cons
== error_mark_node
)
8164 return weakly_subsumes (parm_cons
, new_args
, arg
);
8167 // Convert a placeholder argument into a binding to the original
8168 // parameter. The original parameter is saved as the TREE_TYPE of
8171 convert_wildcard_argument (tree parm
, tree arg
)
8173 TREE_TYPE (arg
) = parm
;
8177 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8178 because one of them is dependent. But we need to represent the
8179 conversion for the benefit of cp_tree_equal. */
8182 maybe_convert_nontype_argument (tree type
, tree arg
)
8184 /* Auto parms get no conversion. */
8185 if (type_uses_auto (type
))
8187 /* We don't need or want to add this conversion now if we're going to use the
8188 argument for deduction. */
8189 if (value_dependent_expression_p (arg
))
8192 type
= cv_unqualified (type
);
8193 tree argtype
= TREE_TYPE (arg
);
8194 if (same_type_p (type
, argtype
))
8197 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8198 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8202 /* Convert the indicated template ARG as necessary to match the
8203 indicated template PARM. Returns the converted ARG, or
8204 error_mark_node if the conversion was unsuccessful. Error and
8205 warning messages are issued under control of COMPLAIN. This
8206 conversion is for the Ith parameter in the parameter list. ARGS is
8207 the full set of template arguments deduced so far. */
8210 convert_template_argument (tree parm
,
8213 tsubst_flags_t complain
,
8219 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8221 if (parm
== error_mark_node
|| error_operand_p (arg
))
8222 return error_mark_node
;
8224 /* Trivially convert placeholders. */
8225 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8226 return convert_wildcard_argument (parm
, arg
);
8228 if (arg
== any_targ_node
)
8231 if (TREE_CODE (arg
) == TREE_LIST
8232 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8234 /* The template argument was the name of some
8235 member function. That's usually
8236 invalid, but static members are OK. In any
8237 case, grab the underlying fields/functions
8238 and issue an error later if required. */
8239 TREE_TYPE (arg
) = unknown_type_node
;
8244 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8245 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8246 || requires_tmpl_type
);
8248 /* When determining whether an argument pack expansion is a template,
8249 look at the pattern. */
8250 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8251 arg
= PACK_EXPANSION_PATTERN (arg
);
8253 /* Deal with an injected-class-name used as a template template arg. */
8254 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8256 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8257 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8259 if (cxx_dialect
>= cxx11
)
8260 /* OK under DR 1004. */;
8261 else if (complain
& tf_warning_or_error
)
8262 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8263 " used as template template argument", TYPE_NAME (arg
));
8264 else if (flag_pedantic_errors
)
8272 ((TREE_CODE (arg
) == TEMPLATE_DECL
8273 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8274 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8275 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8276 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8279 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8280 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8281 arg
= TYPE_STUB_DECL (arg
);
8283 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8285 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8286 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8288 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8290 if (complain
& tf_error
)
8291 error ("invalid use of destructor %qE as a type", orig_arg
);
8292 return error_mark_node
;
8295 permerror (input_location
,
8296 "to refer to a type member of a template parameter, "
8297 "use %<typename %E%>", orig_arg
);
8299 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8300 TREE_OPERAND (arg
, 1),
8306 if (is_type
!= requires_type
)
8310 if (complain
& tf_error
)
8312 error ("type/value mismatch at argument %d in template "
8313 "parameter list for %qD",
8317 /* The template argument is a type, but we're expecting
8319 inform (input_location
,
8320 " expected a constant of type %qT, got %qT",
8322 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8323 /* [temp.arg]/2: "In a template-argument, an ambiguity
8324 between a type-id and an expression is resolved to a
8325 type-id, regardless of the form of the corresponding
8326 template-parameter." So give the user a clue. */
8327 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8328 inform (input_location
, " ambiguous template argument "
8329 "for non-type template parameter is treated as "
8332 else if (requires_tmpl_type
)
8333 inform (input_location
,
8334 " expected a class template, got %qE", orig_arg
);
8336 inform (input_location
,
8337 " expected a type, got %qE", orig_arg
);
8340 return error_mark_node
;
8342 if (is_tmpl_type
^ requires_tmpl_type
)
8344 if (in_decl
&& (complain
& tf_error
))
8346 error ("type/value mismatch at argument %d in template "
8347 "parameter list for %qD",
8350 inform (input_location
,
8351 " expected a type, got %qT", DECL_NAME (arg
));
8353 inform (input_location
,
8354 " expected a class template, got %qT", orig_arg
);
8356 return error_mark_node
;
8359 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8360 /* We already did the appropriate conversion when packing args. */
8364 if (requires_tmpl_type
)
8366 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8367 /* The number of argument required is not known yet.
8368 Just accept it for now. */
8372 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8375 /* Strip alias templates that are equivalent to another
8377 arg
= get_underlying_template (arg
);
8378 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8380 if (coerce_template_template_parms (parmparm
, argparm
,
8386 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8388 if (val
!= error_mark_node
)
8390 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8391 val
= TREE_TYPE (val
);
8392 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8393 val
= make_pack_expansion (val
, complain
);
8398 if (in_decl
&& (complain
& tf_error
))
8400 error ("type/value mismatch at argument %d in "
8401 "template parameter list for %qD",
8403 inform (input_location
,
8404 " expected a template of type %qD, got %qT",
8408 val
= error_mark_node
;
8411 // Check that the constraints are compatible before allowing the
8413 if (val
!= error_mark_node
)
8414 if (!is_compatible_template_arg (parm
, arg
))
8416 if (in_decl
&& (complain
& tf_error
))
8418 error ("constraint mismatch at argument %d in "
8419 "template parameter list for %qD",
8421 inform (input_location
, " expected %qD but got %qD",
8424 val
= error_mark_node
;
8430 /* We only form one instance of each template specialization.
8431 Therefore, if we use a non-canonical variant (i.e., a
8432 typedef), any future messages referring to the type will use
8433 the typedef, which is confusing if those future uses do not
8434 themselves also use the typedef. */
8436 val
= canonicalize_type_argument (val
, complain
);
8440 tree t
= TREE_TYPE (parm
);
8442 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8443 > TMPL_ARGS_DEPTH (args
))
8444 /* We don't have enough levels of args to do any substitution. This
8445 can happen in the context of -fnew-ttp-matching. */;
8446 else if (tree a
= type_uses_auto (t
))
8448 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8449 if (t
== error_mark_node
)
8450 return error_mark_node
;
8453 t
= tsubst (t
, args
, complain
, in_decl
);
8455 if (invalid_nontype_parm_type_p (t
, complain
))
8456 return error_mark_node
;
8458 if (t
!= TREE_TYPE (parm
))
8459 t
= canonicalize_type_argument (t
, complain
);
8461 if (!type_dependent_expression_p (orig_arg
)
8462 && !uses_template_parms (t
))
8463 /* We used to call digest_init here. However, digest_init
8464 will report errors, which we don't want when complain
8465 is zero. More importantly, digest_init will try too
8466 hard to convert things: for example, `0' should not be
8467 converted to pointer type at this point according to
8468 the standard. Accepting this is not merely an
8469 extension, since deciding whether or not these
8470 conversions can occur is part of determining which
8471 function template to call, or whether a given explicit
8472 argument specification is valid. */
8473 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8476 val
= canonicalize_expr_argument (orig_arg
, complain
);
8477 val
= maybe_convert_nontype_argument (t
, val
);
8481 if (val
== NULL_TREE
)
8482 val
= error_mark_node
;
8483 else if (val
== error_mark_node
&& (complain
& tf_error
))
8484 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8485 "could not convert template argument %qE from %qT to %qT",
8486 orig_arg
, TREE_TYPE (orig_arg
), t
);
8488 if (INDIRECT_REF_P (val
))
8490 /* Reject template arguments that are references to built-in
8491 functions with no library fallbacks. */
8492 const_tree inner
= TREE_OPERAND (val
, 0);
8493 const_tree innertype
= TREE_TYPE (inner
);
8495 && TYPE_REF_P (innertype
)
8496 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8497 && TREE_OPERAND_LENGTH (inner
) > 0
8498 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8499 return error_mark_node
;
8502 if (TREE_CODE (val
) == SCOPE_REF
)
8504 /* Strip typedefs from the SCOPE_REF. */
8505 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8506 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8508 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8509 QUALIFIED_NAME_IS_TEMPLATE (val
));
8516 /* Coerces the remaining template arguments in INNER_ARGS (from
8517 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8518 Returns the coerced argument pack. PARM_IDX is the position of this
8519 parameter in the template parameter list. ARGS is the original
8520 template argument list. */
8522 coerce_template_parameter_pack (tree parms
,
8530 tsubst_flags_t complain
)
8532 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8533 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8536 tree packed_parms
= NULL_TREE
;
8538 if (arg_idx
> nargs
)
8541 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8543 /* When the template parameter is a non-type template parameter pack
8544 or template template parameter pack whose type or template
8545 parameters use parameter packs, we know exactly how many arguments
8546 we are looking for. Build a vector of the instantiated decls for
8547 these template parameters in PACKED_PARMS. */
8548 /* We can't use make_pack_expansion here because it would interpret a
8549 _DECL as a use rather than a declaration. */
8550 tree decl
= TREE_VALUE (parm
);
8551 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8552 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8553 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8554 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8556 TREE_VEC_LENGTH (args
)--;
8557 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8558 TREE_VEC_LENGTH (args
)++;
8560 if (packed_parms
== error_mark_node
)
8561 return error_mark_node
;
8563 /* If we're doing a partial instantiation of a member template,
8564 verify that all of the types used for the non-type
8565 template parameter pack are, in fact, valid for non-type
8566 template parameters. */
8568 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8570 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8571 for (j
= 0; j
< len
; ++j
)
8573 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8574 if (TREE_CODE (t
) == PARM_DECL
8575 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8576 return error_mark_node
;
8578 /* We don't know how many args we have yet, just
8579 use the unconverted ones for now. */
8583 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8585 /* Check if we have a placeholder pack, which indicates we're
8586 in the context of a introduction list. In that case we want
8587 to match this pack to the single placeholder. */
8588 else if (arg_idx
< nargs
8589 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8590 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8592 nargs
= arg_idx
+ 1;
8593 packed_args
= make_tree_vec (1);
8596 packed_args
= make_tree_vec (nargs
- arg_idx
);
8598 /* Convert the remaining arguments, which will be a part of the
8599 parameter pack "parm". */
8600 int first_pack_arg
= arg_idx
;
8601 for (; arg_idx
< nargs
; ++arg_idx
)
8603 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8604 tree actual_parm
= TREE_VALUE (parm
);
8605 int pack_idx
= arg_idx
- first_pack_arg
;
8609 /* Once we've packed as many args as we have types, stop. */
8610 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8612 else if (PACK_EXPANSION_P (arg
))
8613 /* We don't know how many args we have yet, just
8614 use the unconverted ones for now. */
8617 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8620 if (arg
== error_mark_node
)
8622 if (complain
& tf_error
)
8623 error ("template argument %d is invalid", arg_idx
+ 1);
8626 arg
= convert_template_argument (actual_parm
,
8627 arg
, new_args
, complain
, parm_idx
,
8629 if (arg
== error_mark_node
)
8631 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8634 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8635 && TREE_VEC_LENGTH (packed_args
) > 0)
8637 if (complain
& tf_error
)
8638 error ("wrong number of template arguments (%d, should be %d)",
8639 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8640 return error_mark_node
;
8643 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8644 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8645 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8648 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8649 TREE_CONSTANT (argument_pack
) = 1;
8652 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8654 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8655 TREE_VEC_LENGTH (packed_args
));
8656 return argument_pack
;
8659 /* Returns the number of pack expansions in the template argument vector
8663 pack_expansion_args_count (tree args
)
8668 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8670 tree elt
= TREE_VEC_ELT (args
, i
);
8671 if (elt
&& PACK_EXPANSION_P (elt
))
8677 /* Convert all template arguments to their appropriate types, and
8678 return a vector containing the innermost resulting template
8679 arguments. If any error occurs, return error_mark_node. Error and
8680 warning messages are issued under control of COMPLAIN.
8682 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8683 for arguments not specified in ARGS. Otherwise, if
8684 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8685 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8686 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8690 coerce_template_parms (tree parms
,
8693 tsubst_flags_t complain
,
8694 bool require_all_args
,
8695 bool use_default_args
)
8697 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8698 tree orig_inner_args
;
8701 tree new_inner_args
;
8703 /* When used as a boolean value, indicates whether this is a
8704 variadic template parameter list. Since it's an int, we can also
8705 subtract it from nparms to get the number of non-variadic
8708 int variadic_args_p
= 0;
8709 int post_variadic_parms
= 0;
8711 /* Adjustment to nparms for fixed parameter packs. */
8712 int fixed_pack_adjust
= 0;
8713 int fixed_packs
= 0;
8716 /* Likewise for parameters with default arguments. */
8719 if (args
== error_mark_node
)
8720 return error_mark_node
;
8722 nparms
= TREE_VEC_LENGTH (parms
);
8724 /* Determine if there are any parameter packs or default arguments. */
8725 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8727 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8729 ++post_variadic_parms
;
8730 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8732 if (TREE_PURPOSE (parm
))
8736 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8737 /* If there are no parameters that follow a parameter pack, we need to
8738 expand any argument packs so that we can deduce a parameter pack from
8739 some non-packed args followed by an argument pack, as in variadic85.C.
8740 If there are such parameters, we need to leave argument packs intact
8741 so the arguments are assigned properly. This can happen when dealing
8742 with a nested class inside a partial specialization of a class
8743 template, as in variadic92.C, or when deducing a template parameter pack
8744 from a sub-declarator, as in variadic114.C. */
8745 if (!post_variadic_parms
)
8746 inner_args
= expand_template_argument_pack (inner_args
);
8748 /* Count any pack expansion args. */
8749 variadic_args_p
= pack_expansion_args_count (inner_args
);
8751 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8752 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8753 || (nargs
< nparms
- variadic_p
8756 && (!use_default_args
8757 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8758 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8761 if (complain
& tf_error
)
8763 if (variadic_p
|| default_p
)
8765 nparms
-= variadic_p
+ default_p
;
8766 error ("wrong number of template arguments "
8767 "(%d, should be at least %d)", nargs
, nparms
);
8770 error ("wrong number of template arguments "
8771 "(%d, should be %d)", nargs
, nparms
);
8774 inform (DECL_SOURCE_LOCATION (in_decl
),
8775 "provided for %qD", in_decl
);
8778 return error_mark_node
;
8780 /* We can't pass a pack expansion to a non-pack parameter of an alias
8781 template (DR 1430). */
8783 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8784 || concept_definition_p (in_decl
))
8786 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8788 if (complain
& tf_error
)
8790 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8792 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8793 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8795 if (PACK_EXPANSION_P (arg
)
8796 && !template_parameter_pack_p (parm
))
8798 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8799 error_at (location_of (arg
),
8800 "pack expansion argument for non-pack parameter "
8801 "%qD of alias template %qD", parm
, in_decl
);
8803 error_at (location_of (arg
),
8804 "pack expansion argument for non-pack parameter "
8805 "%qD of concept %qD", parm
, in_decl
);
8806 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8813 return error_mark_node
;
8816 /* We need to evaluate the template arguments, even though this
8817 template-id may be nested within a "sizeof". */
8820 new_inner_args
= make_tree_vec (nparms
);
8821 new_args
= add_outermost_template_args (args
, new_inner_args
);
8822 int pack_adjust
= 0;
8823 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8828 /* Get the Ith template parameter. */
8829 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8831 if (parm
== error_mark_node
)
8833 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8837 /* Calculate the next argument. */
8838 if (arg_idx
< nargs
)
8839 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8843 if (template_parameter_pack_p (TREE_VALUE (parm
))
8844 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8845 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8847 /* Some arguments will be placed in the
8848 template parameter pack PARM. */
8849 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8850 inner_args
, arg_idx
,
8854 if (arg
== NULL_TREE
)
8856 /* We don't know how many args we have yet, just use the
8857 unconverted (and still packed) ones for now. */
8858 new_inner_args
= orig_inner_args
;
8863 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8865 /* Store this argument. */
8866 if (arg
== error_mark_node
)
8869 /* We are done with all of the arguments. */
8875 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8876 arg_idx
+= pack_adjust
;
8877 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8880 fixed_pack_adjust
+= pack_adjust
;
8888 if (PACK_EXPANSION_P (arg
))
8890 /* "If every valid specialization of a variadic template
8891 requires an empty template parameter pack, the template is
8892 ill-formed, no diagnostic required." So check that the
8893 pattern works with this parameter. */
8894 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8895 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8899 if (conv
== error_mark_node
)
8901 if (complain
& tf_error
)
8902 inform (input_location
, "so any instantiation with a "
8903 "non-empty parameter pack would be ill-formed");
8906 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8907 /* Recover from missing typename. */
8908 TREE_VEC_ELT (inner_args
, arg_idx
)
8909 = make_pack_expansion (conv
, complain
);
8911 /* We don't know how many args we have yet, just
8912 use the unconverted ones for now. */
8913 new_inner_args
= inner_args
;
8918 else if (require_all_args
)
8920 /* There must be a default arg in this case. */
8921 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8923 /* The position of the first default template argument,
8924 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8926 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8927 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8928 arg_idx
- pack_adjust
);
8933 if (arg
== error_mark_node
)
8935 if (complain
& tf_error
)
8936 error ("template argument %d is invalid", arg_idx
+ 1);
8940 /* This can occur if there was an error in the template
8941 parameter list itself (which we would already have
8942 reported) that we are trying to recover from, e.g., a class
8943 template with a parameter list such as
8944 template<typename..., typename> (cpp0x/variadic150.C). */
8947 /* This can also happen with a fixed parameter pack (71834). */
8948 if (arg_idx
>= nargs
)
8952 arg
= convert_template_argument (TREE_VALUE (parm
),
8953 arg
, new_args
, complain
,
8956 if (arg
== error_mark_node
)
8959 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8962 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8964 /* If we had fixed parameter packs, we didn't know how many arguments we
8965 actually needed earlier; now we do. */
8966 nparms
+= fixed_pack_adjust
;
8967 variadic_p
-= fixed_packs
;
8971 if (arg_idx
< nargs
)
8973 /* We had some pack expansion arguments that will only work if the packs
8974 are empty, but wait until instantiation time to complain.
8975 See variadic-ttp3.C. */
8977 /* Except that we can't provide empty packs to alias templates or
8978 concepts when there are no corresponding parameters. Basically,
8979 we can get here with this:
8981 template<typename T> concept C = true;
8983 template<typename... Args>
8987 When parsing C<Args...>, we try to form a concept check of
8988 C<?, Args...>. Without the extra check for substituting an empty
8989 pack past the last parameter, we can accept the check as valid.
8991 FIXME: This may be valid for alias templates (but I doubt it).
8993 FIXME: The error could be better also. */
8994 if (in_decl
&& concept_definition_p (in_decl
))
8996 if (complain
& tf_error
)
8997 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8998 "too many arguments");
8999 return error_mark_node
;
9002 int len
= nparms
+ (nargs
- arg_idx
);
9003 tree args
= make_tree_vec (len
);
9005 for (; i
< nparms
; ++i
)
9006 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9007 for (; i
< len
; ++i
, ++arg_idx
)
9008 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9009 arg_idx
- pack_adjust
);
9010 new_inner_args
= args
;
9015 gcc_assert (!(complain
& tf_error
) || seen_error ());
9016 return error_mark_node
;
9019 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9020 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9021 TREE_VEC_LENGTH (new_inner_args
));
9023 return new_inner_args
;
9026 /* Convert all template arguments to their appropriate types, and
9027 return a vector containing the innermost resulting template
9028 arguments. If any error occurs, return error_mark_node. Error and
9029 warning messages are not issued.
9031 Note that no function argument deduction is performed, and default
9032 arguments are used to fill in unspecified arguments. */
9034 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9036 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9039 /* Convert all template arguments to their appropriate type, and
9040 instantiate default arguments as needed. This returns a vector
9041 containing the innermost resulting template arguments, or
9042 error_mark_node if unsuccessful. */
9044 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9045 tsubst_flags_t complain
)
9047 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9050 /* Like coerce_template_parms. If PARMS represents all template
9051 parameters levels, this function returns a vector of vectors
9052 representing all the resulting argument levels. Note that in this
9053 case, only the innermost arguments are coerced because the
9054 outermost ones are supposed to have been coerced already.
9056 Otherwise, if PARMS represents only (the innermost) vector of
9057 parameters, this function returns a vector containing just the
9058 innermost resulting arguments. */
9061 coerce_innermost_template_parms (tree parms
,
9064 tsubst_flags_t complain
,
9065 bool require_all_args
,
9066 bool use_default_args
)
9068 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9069 int args_depth
= TMPL_ARGS_DEPTH (args
);
9072 if (parms_depth
> 1)
9074 coerced_args
= make_tree_vec (parms_depth
);
9078 for (level
= parms
, cur_depth
= parms_depth
;
9079 parms_depth
> 0 && level
!= NULL_TREE
;
9080 level
= TREE_CHAIN (level
), --cur_depth
)
9083 if (cur_depth
== args_depth
)
9084 l
= coerce_template_parms (TREE_VALUE (level
),
9085 args
, in_decl
, complain
,
9089 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9091 if (l
== error_mark_node
)
9092 return error_mark_node
;
9094 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9098 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9099 args
, in_decl
, complain
,
9102 return coerced_args
;
9105 /* Returns true if T is a wrapper to make a C++20 template parameter
9109 class_nttp_const_wrapper_p (tree t
)
9111 if (cxx_dialect
< cxx20
)
9113 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9114 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9115 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9118 /* Returns 1 if template args OT and NT are equivalent. */
9121 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9125 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9127 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9130 if (class_nttp_const_wrapper_p (nt
))
9131 nt
= TREE_OPERAND (nt
, 0);
9132 if (class_nttp_const_wrapper_p (ot
))
9133 ot
= TREE_OPERAND (ot
, 0);
9135 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9136 /* For member templates */
9137 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9138 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9139 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9140 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9141 PACK_EXPANSION_PATTERN (nt
))
9142 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9143 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9144 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9145 return cp_tree_equal (ot
, nt
);
9146 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9148 else if (TYPE_P (nt
) || TYPE_P (ot
))
9150 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9152 /* Don't treat an alias template specialization with dependent
9153 arguments as equivalent to its underlying type when used as a
9154 template argument; we need them to be distinct so that we
9155 substitute into the specialization arguments at instantiation
9156 time. And aliases can't be equivalent without being ==, so
9157 we don't need to look any deeper.
9159 During partial ordering, however, we need to treat them normally so
9160 that we can order uses of the same alias with different
9161 cv-qualification (79960). */
9163 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9166 return same_type_p (ot
, nt
);
9170 /* Try to treat a template non-type argument that has been converted
9171 to the parameter type as equivalent to one that hasn't yet. */
9172 for (enum tree_code code1
= TREE_CODE (ot
);
9173 CONVERT_EXPR_CODE_P (code1
)
9174 || code1
== NON_LVALUE_EXPR
;
9175 code1
= TREE_CODE (ot
))
9176 ot
= TREE_OPERAND (ot
, 0);
9178 for (enum tree_code code2
= TREE_CODE (nt
);
9179 CONVERT_EXPR_CODE_P (code2
)
9180 || code2
== NON_LVALUE_EXPR
;
9181 code2
= TREE_CODE (nt
))
9182 nt
= TREE_OPERAND (nt
, 0);
9184 return cp_tree_equal (ot
, nt
);
9188 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9189 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9190 NEWARG_PTR with the offending arguments if they are non-NULL. */
9193 comp_template_args (tree oldargs
, tree newargs
,
9194 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9199 if (oldargs
== newargs
)
9202 if (!oldargs
|| !newargs
)
9205 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9208 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9210 tree nt
= TREE_VEC_ELT (newargs
, i
);
9211 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9213 if (! template_args_equal (ot
, nt
, partial_order
))
9215 if (oldarg_ptr
!= NULL
)
9217 if (newarg_ptr
!= NULL
)
9226 comp_template_args_porder (tree oargs
, tree nargs
)
9228 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9231 /* Implement a freelist interface for objects of type T.
9233 Head is a separate object, rather than a regular member, so that we
9234 can define it as a GTY deletable pointer, which is highly
9235 desirable. A data member could be declared that way, but then the
9236 containing object would implicitly get GTY((user)), which would
9237 prevent us from instantiating freelists as global objects.
9238 Although this way we can create freelist global objects, they're
9239 such thin wrappers that instantiating temporaries at every use
9240 loses nothing and saves permanent storage for the freelist object.
9242 Member functions next, anew, poison and reinit have default
9243 implementations that work for most of the types we're interested
9244 in, but if they don't work for some type, they should be explicitly
9245 specialized. See the comments before them for requirements, and
9246 the example specializations for the tree_list_freelist. */
9247 template <typename T
>
9250 /* Return the next object in a chain. We could just do type
9251 punning, but if we access the object with its underlying type, we
9252 avoid strict-aliasing trouble. This needs only work between
9253 poison and reinit. */
9254 static T
*&next (T
*obj
) { return obj
->next
; }
9256 /* Return a newly allocated, uninitialized or minimally-initialized
9257 object of type T. Any initialization performed by anew should
9258 either remain across the life of the object and the execution of
9259 poison, or be redone by reinit. */
9260 static T
*anew () { return ggc_alloc
<T
> (); }
9262 /* Optionally scribble all over the bits holding the object, so that
9263 they become (mostly?) uninitialized memory. This is called while
9264 preparing to make the object part of the free list. */
9265 static void poison (T
*obj
) {
9266 T
*p ATTRIBUTE_UNUSED
= obj
;
9267 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9269 #ifdef ENABLE_GC_CHECKING
9270 /* Poison the data, to indicate the data is garbage. */
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9272 memset (p
, 0xa5, sizeof (*p
));
9274 /* Let valgrind know the object is free. */
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9277 /* Let valgrind know the next portion of the object is available,
9278 but uninitialized. */
9279 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9282 /* Bring an object that underwent at least one lifecycle after anew
9283 and before the most recent free and poison, back to a usable
9284 state, reinitializing whatever is needed for it to be
9285 functionally equivalent to an object just allocated and returned
9286 by anew. This may poison or clear the next field, used by
9287 freelist housekeeping after poison was called. */
9288 static void reinit (T
*obj
) {
9289 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9291 #ifdef ENABLE_GC_CHECKING
9292 memset (q
, 0xa5, sizeof (*q
));
9294 /* Let valgrind know the entire object is available, but
9296 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9299 /* Reference a GTY-deletable pointer that points to the first object
9300 in the free list proper. */
9303 /* Construct a freelist object chaining objects off of HEAD. */
9304 freelist (T
*&head
) : head(head
) {}
9306 /* Add OBJ to the free object list. The former head becomes OBJ's
9315 /* Take an object from the free list, if one is available, or
9316 allocate a new one. Objects taken from the free list should be
9317 regarded as filled with garbage, except for bits that are
9318 configured to be preserved across free and alloc. */
9333 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9334 want to allocate a TREE_LIST using the usual interface, and ensure
9335 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9336 build_tree_list logic in reinit, so this could go out of sync. */
9339 freelist
<tree_node
>::next (tree obj
)
9341 return TREE_CHAIN (obj
);
9345 freelist
<tree_node
>::anew ()
9347 return build_tree_list (NULL
, NULL
);
9351 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9353 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9354 tree p ATTRIBUTE_UNUSED
= obj
;
9355 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9356 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9358 #ifdef ENABLE_GC_CHECKING
9359 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9361 /* Poison the data, to indicate the data is garbage. */
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9363 memset (p
, 0xa5, size
);
9365 /* Let valgrind know the object is free. */
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9367 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9368 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9371 #ifdef ENABLE_GC_CHECKING
9372 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9373 /* Keep TREE_CHAIN functional. */
9374 TREE_SET_CODE (obj
, TREE_LIST
);
9376 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9381 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9383 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9385 #ifdef ENABLE_GC_CHECKING
9386 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9388 memset (obj
, 0, sizeof (tree_list
));
9391 /* Let valgrind know the entire object is available, but
9393 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9395 #ifdef ENABLE_GC_CHECKING
9396 TREE_SET_CODE (obj
, TREE_LIST
);
9398 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9402 /* Point to the first object in the TREE_LIST freelist. */
9403 static GTY((deletable
)) tree tree_list_freelist_head
;
9404 /* Return the/an actual TREE_LIST freelist. */
9405 static inline freelist
<tree_node
>
9406 tree_list_freelist ()
9408 return tree_list_freelist_head
;
9411 /* Point to the first object in the tinst_level freelist. */
9412 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9413 /* Return the/an actual tinst_level freelist. */
9414 static inline freelist
<tinst_level
>
9415 tinst_level_freelist ()
9417 return tinst_level_freelist_head
;
9420 /* Point to the first object in the pending_template freelist. */
9421 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9422 /* Return the/an actual pending_template freelist. */
9423 static inline freelist
<pending_template
>
9424 pending_template_freelist ()
9426 return pending_template_freelist_head
;
9429 /* Build the TREE_LIST object out of a split list, store it
9430 permanently, and return it. */
9432 tinst_level::to_list ()
9434 gcc_assert (split_list_p ());
9435 tree ret
= tree_list_freelist ().alloc ();
9436 TREE_PURPOSE (ret
) = tldcl
;
9437 TREE_VALUE (ret
) = targs
;
9440 gcc_assert (tree_list_p ());
9444 const unsigned short tinst_level::refcount_infinity
;
9446 /* Increment OBJ's refcount unless it is already infinite. */
9447 static tinst_level
*
9448 inc_refcount_use (tinst_level
*obj
)
9450 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9455 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9457 tinst_level::free (tinst_level
*obj
)
9459 if (obj
->tree_list_p ())
9460 tree_list_freelist ().free (obj
->get_node ());
9461 tinst_level_freelist ().free (obj
);
9464 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9465 OBJ's DECL and OBJ, and start over with the tinst_level object that
9466 used to be referenced by OBJ's NEXT. */
9468 dec_refcount_use (tinst_level
*obj
)
9471 && obj
->refcount
!= tinst_level::refcount_infinity
9472 && !--obj
->refcount
)
9474 tinst_level
*next
= obj
->next
;
9475 tinst_level::free (obj
);
9480 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9481 and of the former PTR. Omitting the second argument is equivalent
9482 to passing (T*)NULL; this is allowed because passing the
9483 zero-valued integral constant NULL confuses type deduction and/or
9484 overload resolution. */
9485 template <typename T
>
9487 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9490 ptr
= inc_refcount_use (obj
);
9491 dec_refcount_use (save
);
9495 add_pending_template (tree d
)
9497 tree ti
= (TYPE_P (d
)
9498 ? CLASSTYPE_TEMPLATE_INFO (d
)
9499 : DECL_TEMPLATE_INFO (d
));
9500 struct pending_template
*pt
;
9503 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9506 /* We are called both from instantiate_decl, where we've already had a
9507 tinst_level pushed, and instantiate_template, where we haven't.
9509 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9510 level
= !current_tinst_level
9511 || current_tinst_level
->maybe_get_node () != d
;
9514 push_tinst_level (d
);
9516 pt
= pending_template_freelist ().alloc ();
9519 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9520 if (last_pending_template
)
9521 last_pending_template
->next
= pt
;
9523 pending_templates
= pt
;
9525 last_pending_template
= pt
;
9527 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9534 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9535 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9536 documentation for TEMPLATE_ID_EXPR. */
9539 lookup_template_function (tree fns
, tree arglist
)
9541 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9542 return error_mark_node
;
9544 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9546 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9548 error ("%q#D is not a function template", fns
);
9549 return error_mark_node
;
9552 if (BASELINK_P (fns
))
9554 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9556 BASELINK_FUNCTIONS (fns
),
9561 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9564 /* Within the scope of a template class S<T>, the name S gets bound
9565 (in build_self_reference) to a TYPE_DECL for the class, not a
9566 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9567 or one of its enclosing classes, and that type is a template,
9568 return the associated TEMPLATE_DECL. Otherwise, the original
9571 Also handle the case when DECL is a TREE_LIST of ambiguous
9572 injected-class-names from different bases. */
9575 maybe_get_template_decl_from_type_decl (tree decl
)
9577 if (decl
== NULL_TREE
)
9580 /* DR 176: A lookup that finds an injected-class-name (10.2
9581 [class.member.lookup]) can result in an ambiguity in certain cases
9582 (for example, if it is found in more than one base class). If all of
9583 the injected-class-names that are found refer to specializations of
9584 the same class template, and if the name is followed by a
9585 template-argument-list, the reference refers to the class template
9586 itself and not a specialization thereof, and is not ambiguous. */
9587 if (TREE_CODE (decl
) == TREE_LIST
)
9589 tree t
, tmpl
= NULL_TREE
;
9590 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9592 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9595 else if (tmpl
!= elt
)
9598 if (tmpl
&& t
== NULL_TREE
)
9604 return (decl
!= NULL_TREE
9605 && DECL_SELF_REFERENCE_P (decl
)
9606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9607 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9610 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9611 parameters, find the desired type.
9613 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9615 IN_DECL, if non-NULL, is the template declaration we are trying to
9618 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9619 the class we are looking up.
9621 Issue error and warning messages under control of COMPLAIN.
9623 If the template class is really a local class in a template
9624 function, then the FUNCTION_CONTEXT is the function in which it is
9627 ??? Note that this function is currently called *twice* for each
9628 template-id: the first time from the parser, while creating the
9629 incomplete type (finish_template_type), and the second type during the
9630 real instantiation (instantiate_template_class). This is surely something
9631 that we want to avoid. It also causes some problems with argument
9632 coercion (see convert_nontype_argument for more information on this). */
9635 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9636 int entering_scope
, tsubst_flags_t complain
)
9638 tree templ
= NULL_TREE
, parmlist
;
9645 if (identifier_p (d1
))
9647 tree value
= innermost_non_namespace_value (d1
);
9648 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9653 push_decl_namespace (context
);
9654 templ
= lookup_name (d1
);
9655 templ
= maybe_get_template_decl_from_type_decl (templ
);
9657 pop_decl_namespace ();
9660 context
= DECL_CONTEXT (templ
);
9662 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9664 tree type
= TREE_TYPE (d1
);
9666 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9667 an implicit typename for the second A. Deal with it. */
9668 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9669 type
= TREE_TYPE (type
);
9671 if (CLASSTYPE_TEMPLATE_INFO (type
))
9673 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9674 d1
= DECL_NAME (templ
);
9677 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9678 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9680 templ
= TYPE_TI_TEMPLATE (d1
);
9681 d1
= DECL_NAME (templ
);
9683 else if (DECL_TYPE_TEMPLATE_P (d1
))
9686 d1
= DECL_NAME (templ
);
9687 context
= DECL_CONTEXT (templ
);
9689 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9692 d1
= DECL_NAME (templ
);
9695 /* Issue an error message if we didn't find a template. */
9698 if (complain
& tf_error
)
9699 error ("%qT is not a template", d1
);
9700 return error_mark_node
;
9703 if (TREE_CODE (templ
) != TEMPLATE_DECL
9704 /* Make sure it's a user visible template, if it was named by
9706 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9707 && !PRIMARY_TEMPLATE_P (templ
)))
9709 if (complain
& tf_error
)
9711 error ("non-template type %qT used as a template", d1
);
9713 error ("for template declaration %q+D", in_decl
);
9715 return error_mark_node
;
9718 complain
&= ~tf_user
;
9720 /* An alias that just changes the name of a template is equivalent to the
9721 other template, so if any of the arguments are pack expansions, strip
9722 the alias to avoid problems with a pack expansion passed to a non-pack
9723 alias template parameter (DR 1430). */
9724 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9725 templ
= get_underlying_template (templ
);
9727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9730 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9731 if (arglist2
== error_mark_node
9732 || (!uses_template_parms (arglist2
)
9733 && check_instantiated_args (templ
, arglist2
, complain
)))
9734 return error_mark_node
;
9736 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9741 tree template_type
= TREE_TYPE (templ
);
9744 tree found
= NULL_TREE
;
9747 int is_dependent_type
;
9748 int use_partial_inst_tmpl
= false;
9750 if (template_type
== error_mark_node
)
9751 /* An error occurred while building the template TEMPL, and a
9752 diagnostic has most certainly been emitted for that
9753 already. Let's propagate that error. */
9754 return error_mark_node
;
9756 gen_tmpl
= most_general_template (templ
);
9757 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9758 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9759 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9761 if (arg_depth
== 1 && parm_depth
> 1)
9763 /* We've been given an incomplete set of template arguments.
9766 template <class T> struct S1 {
9767 template <class U> struct S2 {};
9768 template <class U> struct S2<U*> {};
9771 we will be called with an ARGLIST of `U*', but the
9772 TEMPLATE will be `template <class T> template
9773 <class U> struct S1<T>::S2'. We must fill in the missing
9775 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9776 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9777 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9780 /* Now we should have enough arguments. */
9781 gcc_assert (parm_depth
== arg_depth
);
9783 /* From here on, we're only interested in the most general
9786 /* Calculate the BOUND_ARGS. These will be the args that are
9787 actually tsubst'd into the definition to create the
9789 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9791 /*require_all_args=*/true,
9792 /*use_default_args=*/true);
9794 if (arglist
== error_mark_node
)
9795 /* We were unable to bind the arguments. */
9796 return error_mark_node
;
9798 /* In the scope of a template class, explicit references to the
9799 template class refer to the type of the template, not any
9800 instantiation of it. For example, in:
9802 template <class T> class C { void f(C<T>); }
9804 the `C<T>' is just the same as `C'. Outside of the
9805 class, however, such a reference is an instantiation. */
9807 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9808 || currently_open_class (template_type
))
9810 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9812 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9813 return template_type
;
9816 /* If we already have this specialization, return it. */
9817 elt
.tmpl
= gen_tmpl
;
9819 elt
.spec
= NULL_TREE
;
9820 hash
= spec_hasher::hash (&elt
);
9821 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9826 /* If the template's constraints are not satisfied,
9827 then we cannot form a valid type.
9829 Note that the check is deferred until after the hash
9830 lookup. This prevents redundant checks on previously
9831 instantiated specializations. */
9833 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9834 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9836 if (complain
& tf_error
)
9838 auto_diagnostic_group d
;
9839 error ("template constraint failure for %qD", gen_tmpl
);
9840 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9842 return error_mark_node
;
9845 is_dependent_type
= uses_template_parms (arglist
);
9847 /* If the deduced arguments are invalid, then the binding
9849 if (!is_dependent_type
9850 && check_instantiated_args (gen_tmpl
,
9851 INNERMOST_TEMPLATE_ARGS (arglist
),
9853 return error_mark_node
;
9855 if (!is_dependent_type
9856 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9857 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9858 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9860 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9861 DECL_NAME (gen_tmpl
),
9862 /*tag_scope=*/ts_global
);
9866 context
= DECL_CONTEXT (gen_tmpl
);
9867 if (context
&& TYPE_P (context
))
9869 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9870 context
= complete_type (context
);
9873 context
= tsubst (context
, arglist
, complain
, in_decl
);
9875 if (context
== error_mark_node
)
9876 return error_mark_node
;
9879 context
= global_namespace
;
9881 /* Create the type. */
9882 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9884 /* The user referred to a specialization of an alias
9885 template represented by GEN_TMPL.
9887 [temp.alias]/2 says:
9889 When a template-id refers to the specialization of an
9890 alias template, it is equivalent to the associated
9891 type obtained by substitution of its
9892 template-arguments for the template-parameters in the
9893 type-id of the alias template. */
9895 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9896 /* Note that the call above (by indirectly calling
9897 register_specialization in tsubst_decl) registers the
9898 TYPE_DECL representing the specialization of the alias
9899 template. So next time someone substitutes ARGLIST for
9900 the template parms into the alias template (GEN_TMPL),
9901 she'll get that TYPE_DECL back. */
9903 if (t
== error_mark_node
)
9906 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9908 if (!is_dependent_type
)
9910 set_current_access_from_decl (TYPE_NAME (template_type
));
9911 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9912 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9913 arglist
, complain
, in_decl
),
9914 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9915 arglist
, complain
, in_decl
),
9916 SCOPED_ENUM_P (template_type
), NULL
);
9918 if (t
== error_mark_node
)
9923 /* We don't want to call start_enum for this type, since
9924 the values for the enumeration constants may involve
9925 template parameters. And, no one should be interested
9926 in the enumeration constants for such a type. */
9927 t
= cxx_make_type (ENUMERAL_TYPE
);
9928 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9930 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9931 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9932 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9934 else if (CLASS_TYPE_P (template_type
))
9936 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9937 instantiated here. */
9938 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9940 t
= make_class_type (TREE_CODE (template_type
));
9941 CLASSTYPE_DECLARED_CLASS (t
)
9942 = CLASSTYPE_DECLARED_CLASS (template_type
);
9943 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9945 /* A local class. Make sure the decl gets registered properly. */
9946 if (context
== current_function_decl
)
9947 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9949 return error_mark_node
;
9951 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9952 /* This instantiation is another name for the primary
9953 template type. Set the TYPE_CANONICAL field
9955 TYPE_CANONICAL (t
) = template_type
;
9956 else if (any_template_arguments_need_structural_equality_p (arglist
))
9957 /* Some of the template arguments require structural
9958 equality testing, so this template class requires
9959 structural equality testing. */
9960 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9965 /* If we called start_enum or pushtag above, this information
9966 will already be set up. */
9967 type_decl
= TYPE_NAME (t
);
9970 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9972 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9973 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9974 DECL_SOURCE_LOCATION (type_decl
)
9975 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9978 if (CLASS_TYPE_P (template_type
))
9980 TREE_PRIVATE (type_decl
)
9981 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9982 TREE_PROTECTED (type_decl
)
9983 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9984 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9986 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9987 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9991 if (OVERLOAD_TYPE_P (t
)
9992 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9994 static const char *tags
[] = {"abi_tag", "may_alias"};
9996 for (unsigned ix
= 0; ix
!= 2; ix
++)
9999 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10002 TYPE_ATTRIBUTES (t
)
10003 = tree_cons (TREE_PURPOSE (attributes
),
10004 TREE_VALUE (attributes
),
10005 TYPE_ATTRIBUTES (t
));
10009 /* Let's consider the explicit specialization of a member
10010 of a class template specialization that is implicitly instantiated,
10015 template<class U> struct M {}; //#0
10020 struct S<int>::M<char> //#1
10024 [temp.expl.spec]/4 says this is valid.
10026 In this case, when we write:
10029 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10032 When we encounter #1, we want to store the partial instantiation
10033 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10035 For all cases other than this "explicit specialization of member of a
10036 class template", we just want to store the most general template into
10037 the CLASSTYPE_TI_TEMPLATE of M.
10039 This case of "explicit specialization of member of a class template"
10041 1/ the enclosing class is an instantiation of, and therefore not
10042 the same as, the context of the most general template, and
10043 2/ we aren't looking at the partial instantiation itself, i.e.
10044 the innermost arguments are not the same as the innermost parms of
10045 the most general template.
10047 So it's only when 1/ and 2/ happens that we want to use the partial
10048 instantiation of the member template in lieu of its most general
10051 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10052 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10053 /* the enclosing class must be an instantiation... */
10054 && CLASS_TYPE_P (context
)
10055 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10057 TREE_VEC_LENGTH (arglist
)--;
10058 ++processing_template_decl
;
10059 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10060 tree partial_inst_args
=
10061 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10062 arglist
, complain
, NULL_TREE
);
10063 --processing_template_decl
;
10064 TREE_VEC_LENGTH (arglist
)++;
10065 if (partial_inst_args
== error_mark_node
)
10066 return error_mark_node
;
10067 use_partial_inst_tmpl
=
10068 /*...and we must not be looking at the partial instantiation
10070 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10071 partial_inst_args
);
10074 if (!use_partial_inst_tmpl
)
10075 /* This case is easy; there are no member templates involved. */
10079 /* This is a full instantiation of a member template. Find
10080 the partial instantiation of which this is an instance. */
10082 /* Temporarily reduce by one the number of levels in the ARGLIST
10083 so as to avoid comparing the last set of arguments. */
10084 TREE_VEC_LENGTH (arglist
)--;
10085 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10086 TREE_VEC_LENGTH (arglist
)++;
10087 /* FOUND is either a proper class type, or an alias
10088 template specialization. In the later case, it's a
10089 TYPE_DECL, resulting from the substituting of arguments
10090 for parameters in the TYPE_DECL of the alias template
10091 done earlier. So be careful while getting the template
10093 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10095 : (TREE_CODE (found
) == TYPE_DECL
10096 ? DECL_TI_TEMPLATE (found
)
10097 : CLASSTYPE_TI_TEMPLATE (found
)));
10099 if (DECL_CLASS_TEMPLATE_P (found
)
10100 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10102 /* If this partial instantiation is specialized, we want to
10103 use it for hash table lookup. */
10105 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10106 hash
= spec_hasher::hash (&elt
);
10110 /* Build template info for the new specialization. */
10111 if (TYPE_ALIAS_P (t
))
10113 /* This is constructed during instantiation of the alias
10114 decl. But for member templates of template classes, that
10115 is not correct as we need to refer to the partially
10116 instantiated template, not the most general template.
10117 The incorrect knowledge will not have escaped this
10118 instantiation process, so we're good just updating the
10119 template_info we made then. */
10120 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10121 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
));
10122 if (TI_TEMPLATE (ti
) != found
)
10124 gcc_checking_assert (DECL_TI_TEMPLATE (found
) == TI_TEMPLATE (ti
));
10125 TI_TEMPLATE (ti
) = found
;
10129 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10132 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10133 gcc_checking_assert (*slot
== NULL
);
10134 entry
= ggc_alloc
<spec_entry
> ();
10138 /* Note this use of the partial instantiation so we can check it
10139 later in maybe_process_partial_specialization. */
10140 DECL_TEMPLATE_INSTANTIATIONS (found
)
10141 = tree_cons (arglist
, t
,
10142 DECL_TEMPLATE_INSTANTIATIONS (found
));
10144 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10145 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10146 /* Now that the type has been registered on the instantiations
10147 list, we set up the enumerators. Because the enumeration
10148 constants may involve the enumeration type itself, we make
10149 sure to register the type first, and then create the
10150 constants. That way, doing tsubst_expr for the enumeration
10151 constants won't result in recursive calls here; we'll find
10152 the instantiation and exit above. */
10153 tsubst_enum (template_type
, t
, arglist
);
10155 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10156 /* If the type makes use of template parameters, the
10157 code that generates debugging information will crash. */
10158 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10160 /* Possibly limit visibility based on template args. */
10161 TREE_PUBLIC (type_decl
) = 1;
10162 determine_visibility (type_decl
);
10164 inherit_targ_abi_tags (t
);
10170 /* Wrapper for lookup_template_class_1. */
10173 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10174 int entering_scope
, tsubst_flags_t complain
)
10177 timevar_push (TV_TEMPLATE_INST
);
10178 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10179 entering_scope
, complain
);
10180 timevar_pop (TV_TEMPLATE_INST
);
10184 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10187 lookup_template_variable (tree templ
, tree arglist
)
10189 if (flag_concepts
&& variable_concept_p (templ
))
10190 return build_concept_check (templ
, arglist
, tf_none
);
10192 /* The type of the expression is NULL_TREE since the template-id could refer
10193 to an explicit or partial specialization. */
10194 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10197 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10200 finish_template_variable (tree var
, tsubst_flags_t complain
)
10202 tree templ
= TREE_OPERAND (var
, 0);
10203 tree arglist
= TREE_OPERAND (var
, 1);
10205 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10206 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10208 templ
= most_general_template (templ
);
10209 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10210 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10212 /*use_default*/true);
10213 if (arglist
== error_mark_node
)
10214 return error_mark_node
;
10216 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10218 if (complain
& tf_error
)
10220 auto_diagnostic_group d
;
10221 error ("use of invalid variable template %qE", var
);
10222 diagnose_constraints (location_of (var
), templ
, arglist
);
10224 return error_mark_node
;
10227 return instantiate_template (templ
, arglist
, complain
);
10230 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10231 TARGS template args, and instantiate it if it's not dependent. */
10234 lookup_and_finish_template_variable (tree templ
, tree targs
,
10235 tsubst_flags_t complain
)
10237 templ
= lookup_template_variable (templ
, targs
);
10238 if (!any_dependent_template_arguments_p (targs
))
10240 templ
= finish_template_variable (templ
, complain
);
10244 return convert_from_reference (templ
);
10248 struct pair_fn_data
10253 /* True when we should also visit template parameters that occur in
10254 non-deduced contexts. */
10255 bool include_nondeduced_p
;
10256 hash_set
<tree
> *visited
;
10259 /* Called from for_each_template_parm via walk_tree. */
10262 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10265 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10266 tree_fn_t fn
= pfd
->fn
;
10267 void *data
= pfd
->data
;
10268 tree result
= NULL_TREE
;
10270 #define WALK_SUBTREE(NODE) \
10273 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10274 pfd->include_nondeduced_p, \
10276 if (result) goto out; \
10280 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10284 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10285 WALK_SUBTREE (TYPE_CONTEXT (t
));
10287 switch (TREE_CODE (t
))
10290 if (TYPE_PTRMEMFUNC_P (t
))
10292 /* Fall through. */
10295 case ENUMERAL_TYPE
:
10296 if (!TYPE_TEMPLATE_INFO (t
))
10297 *walk_subtrees
= 0;
10299 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10303 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10304 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10308 /* Since we're not going to walk subtrees, we have to do this
10309 explicitly here. */
10310 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10311 /* Fall through. */
10313 case FUNCTION_TYPE
:
10314 /* Check the return type. */
10315 WALK_SUBTREE (TREE_TYPE (t
));
10317 /* Check the parameter types. Since default arguments are not
10318 instantiated until they are needed, the TYPE_ARG_TYPES may
10319 contain expressions that involve template parameters. But,
10320 no-one should be looking at them yet. And, once they're
10321 instantiated, they don't contain template parameters, so
10322 there's no point in looking at them then, either. */
10326 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10327 WALK_SUBTREE (TREE_VALUE (parm
));
10329 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10330 want walk_tree walking into them itself. */
10331 *walk_subtrees
= 0;
10334 if (flag_noexcept_type
)
10336 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10338 WALK_SUBTREE (TREE_PURPOSE (spec
));
10343 case DECLTYPE_TYPE
:
10344 case UNDERLYING_TYPE
:
10345 if (pfd
->include_nondeduced_p
10346 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10348 pfd
->include_nondeduced_p
,
10350 return error_mark_node
;
10351 *walk_subtrees
= false;
10354 case FUNCTION_DECL
:
10356 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10357 WALK_SUBTREE (DECL_TI_ARGS (t
));
10358 /* Fall through. */
10362 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10363 WALK_SUBTREE (DECL_INITIAL (t
));
10364 if (DECL_CONTEXT (t
)
10365 && pfd
->include_nondeduced_p
)
10366 WALK_SUBTREE (DECL_CONTEXT (t
));
10369 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10370 /* Record template parameters such as `T' inside `TT<T>'. */
10371 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10372 /* Fall through. */
10374 case TEMPLATE_TEMPLATE_PARM
:
10375 case TEMPLATE_TYPE_PARM
:
10376 case TEMPLATE_PARM_INDEX
:
10377 if (fn
&& (*fn
)(t
, data
))
10383 case TEMPLATE_DECL
:
10384 /* A template template parameter is encountered. */
10385 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10386 WALK_SUBTREE (TREE_TYPE (t
));
10388 /* Already substituted template template parameter */
10389 *walk_subtrees
= 0;
10392 case TYPENAME_TYPE
:
10393 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10394 partial instantiation. */
10395 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10396 *walk_subtrees
= 0;
10400 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10401 && pfd
->include_nondeduced_p
)
10402 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10406 case COMPONENT_REF
:
10407 /* If there's no type, then this thing must be some expression
10408 involving template parameters. */
10409 if (!fn
&& !TREE_TYPE (t
))
10410 return error_mark_node
;
10415 case IMPLICIT_CONV_EXPR
:
10416 case REINTERPRET_CAST_EXPR
:
10417 case CONST_CAST_EXPR
:
10418 case STATIC_CAST_EXPR
:
10419 case DYNAMIC_CAST_EXPR
:
10423 case PSEUDO_DTOR_EXPR
:
10425 return error_mark_node
;
10429 if (pfd
->include_nondeduced_p
)
10430 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10433 case REQUIRES_EXPR
:
10436 return error_mark_node
;
10438 /* Recursively walk the type of each constraint variable. */
10439 tree p
= TREE_OPERAND (t
, 0);
10442 WALK_SUBTREE (TREE_TYPE (p
));
10443 p
= TREE_CHAIN (p
);
10452 #undef WALK_SUBTREE
10454 /* We didn't find any template parameters we liked. */
10459 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10460 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10461 call FN with the parameter and the DATA.
10462 If FN returns nonzero, the iteration is terminated, and
10463 for_each_template_parm returns 1. Otherwise, the iteration
10464 continues. If FN never returns a nonzero value, the value
10465 returned by for_each_template_parm is 0. If FN is NULL, it is
10466 considered to be the function which always returns 1.
10468 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10469 parameters that occur in non-deduced contexts. When false, only
10470 visits those template parameters that can be deduced. */
10473 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10474 hash_set
<tree
> *visited
,
10475 bool include_nondeduced_p
,
10478 struct pair_fn_data pfd
;
10483 pfd
.any_fn
= any_fn
;
10485 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10487 /* Walk the tree. (Conceptually, we would like to walk without
10488 duplicates, but for_each_template_parm_r recursively calls
10489 for_each_template_parm, so we would need to reorganize a fair
10490 bit to use walk_tree_without_duplicates, so we keep our own
10493 pfd
.visited
= visited
;
10495 pfd
.visited
= new hash_set
<tree
>;
10496 result
= cp_walk_tree (&t
,
10497 for_each_template_parm_r
,
10504 delete pfd
.visited
;
10511 struct find_template_parameter_info
10513 explicit find_template_parameter_info (tree ctx_parms
)
10514 : parm_list (NULL_TREE
),
10515 ctx_parms (ctx_parms
),
10516 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10519 hash_set
<tree
> visited
;
10520 hash_set
<tree
> parms
;
10526 /* Appends the declaration of T to the list in DATA. */
10529 keep_template_parm (tree t
, void* data
)
10531 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10533 /* Template parameters declared within the expression are not part of
10534 the parameter mapping. For example, in this concept:
10536 template<typename T>
10537 concept C = requires { <expr> } -> same_as<int>;
10539 the return specifier same_as<int> declares a new decltype parameter
10540 that must not be part of the parameter mapping. The same is true
10541 for generic lambda parameters, lambda template parameters, etc. */
10544 template_parm_level_and_index (t
, &level
, &index
);
10545 if (level
> ftpi
->max_depth
)
10548 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10549 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10550 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10551 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10553 /* Arguments like const T yield parameters like const T. This means that
10554 a template-id like X<T, const T> would yield two distinct parameters:
10555 T and const T. Adjust types to their unqualified versions. */
10557 t
= TYPE_MAIN_VARIANT (t
);
10558 if (!ftpi
->parms
.add (t
))
10559 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10564 /* Ensure that we recursively examine certain terms that are not normally
10565 visited in for_each_template_parm_r. */
10568 any_template_parm_r (tree t
, void *data
)
10570 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10572 #define WALK_SUBTREE(NODE) \
10575 for_each_template_parm (NODE, keep_template_parm, data, \
10576 &ftpi->visited, true, \
10577 any_template_parm_r); \
10581 /* A mention of a member alias/typedef is a use of all of its template
10582 arguments, including those from the enclosing class, so we don't use
10583 alias_template_specialization_p here. */
10584 if (TYPE_P (t
) && typedef_variant_p (t
))
10585 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10586 WALK_SUBTREE (TI_ARGS (tinfo
));
10588 switch (TREE_CODE (t
))
10590 case TEMPLATE_TYPE_PARM
:
10591 /* Type constraints of a placeholder type may contain parameters. */
10593 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10594 WALK_SUBTREE (constr
);
10597 case TEMPLATE_ID_EXPR
:
10598 /* Search through references to variable templates. */
10599 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10600 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10603 case TEMPLATE_PARM_INDEX
:
10605 /* A parameter or constraint variable may also depend on a template
10606 parameter without explicitly naming it. */
10607 WALK_SUBTREE (TREE_TYPE (t
));
10610 case TEMPLATE_DECL
:
10612 /* If T is a member template that shares template parameters with
10613 ctx_parms, we need to mark all those parameters for mapping. */
10614 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10615 tree cparms
= ftpi
->ctx_parms
;
10616 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10617 dparms
= TREE_CHAIN (dparms
);
10618 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10619 cparms
= TREE_CHAIN (cparms
);
10621 && (TREE_TYPE (TREE_VALUE (dparms
))
10622 != TREE_TYPE (TREE_VALUE (cparms
))))
10623 dparms
= TREE_CHAIN (dparms
),
10624 cparms
= TREE_CHAIN (cparms
);
10627 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10628 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10629 for (int i
= 0; i
< ddepth
; ++i
)
10630 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10637 /* Look in the parms and body. */
10638 tree fn
= lambda_function (t
);
10639 WALK_SUBTREE (TREE_TYPE (fn
));
10640 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10644 case IDENTIFIER_NODE
:
10645 if (IDENTIFIER_CONV_OP_P (t
))
10646 /* The conversion-type-id of a conversion operator may be dependent. */
10647 WALK_SUBTREE (TREE_TYPE (t
));
10654 /* Keep walking. */
10658 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10659 are the template parameters in scope. */
10662 find_template_parameters (tree t
, tree ctx_parms
)
10667 find_template_parameter_info
ftpi (ctx_parms
);
10668 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10669 /*include_nondeduced*/true, any_template_parm_r
);
10670 return ftpi
.parm_list
;
10673 /* Returns true if T depends on any template parameter. */
10676 uses_template_parms (tree t
)
10678 if (t
== NULL_TREE
)
10682 int saved_processing_template_decl
;
10684 saved_processing_template_decl
= processing_template_decl
;
10685 if (!saved_processing_template_decl
)
10686 processing_template_decl
= 1;
10688 dependent_p
= dependent_type_p (t
);
10689 else if (TREE_CODE (t
) == TREE_VEC
)
10690 dependent_p
= any_dependent_template_arguments_p (t
);
10691 else if (TREE_CODE (t
) == TREE_LIST
)
10692 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10693 || uses_template_parms (TREE_CHAIN (t
)));
10694 else if (TREE_CODE (t
) == TYPE_DECL
)
10695 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10696 else if (t
== error_mark_node
)
10697 dependent_p
= false;
10699 dependent_p
= value_dependent_expression_p (t
);
10701 processing_template_decl
= saved_processing_template_decl
;
10703 return dependent_p
;
10706 /* Returns true iff current_function_decl is an incompletely instantiated
10707 template. Useful instead of processing_template_decl because the latter
10708 is set to 0 during instantiate_non_dependent_expr. */
10711 in_template_function (void)
10713 tree fn
= current_function_decl
;
10715 ++processing_template_decl
;
10716 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10717 && DECL_TEMPLATE_INFO (fn
)
10718 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10719 --processing_template_decl
;
10723 /* Returns true if T depends on any template parameter with level LEVEL. */
10726 uses_template_parms_level (tree t
, int level
)
10728 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10729 /*include_nondeduced_p=*/true);
10732 /* Returns true if the signature of DECL depends on any template parameter from
10733 its enclosing class. */
10736 uses_outer_template_parms (tree decl
)
10738 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10741 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10742 &depth
, NULL
, /*include_nondeduced_p=*/true))
10744 if (PRIMARY_TEMPLATE_P (decl
)
10745 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10746 (DECL_TEMPLATE_PARMS (decl
)),
10747 template_parm_outer_level
,
10748 &depth
, NULL
, /*include_nondeduced_p=*/true))
10750 tree ci
= get_constraints (decl
);
10752 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10753 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10754 &depth
, NULL
, /*nondeduced*/true))
10759 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10760 ill-formed translation unit, i.e. a variable or function that isn't
10761 usable in a constant expression. */
10764 neglectable_inst_p (tree d
)
10766 return (d
&& DECL_P (d
)
10767 && !undeduced_auto_decl (d
)
10768 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10769 : decl_maybe_constant_var_p (d
)));
10772 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10773 neglectable and instantiated from within an erroneous instantiation. */
10776 limit_bad_template_recursion (tree decl
)
10778 struct tinst_level
*lev
= current_tinst_level
;
10779 int errs
= errorcount
+ sorrycount
;
10780 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10783 for (; lev
; lev
= lev
->next
)
10784 if (neglectable_inst_p (lev
->maybe_get_node ()))
10787 return (lev
&& errs
> lev
->errors
);
10790 static int tinst_depth
;
10791 extern int max_tinst_depth
;
10794 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10796 /* We're starting to instantiate D; record the template instantiation context
10797 at LOC for diagnostics and to restore it later. */
10800 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10802 struct tinst_level
*new_level
;
10804 if (tinst_depth
>= max_tinst_depth
)
10806 /* Tell error.c not to try to instantiate any templates. */
10808 fatal_error (input_location
,
10809 "template instantiation depth exceeds maximum of %d"
10810 " (use %<-ftemplate-depth=%> to increase the maximum)",
10815 /* If the current instantiation caused problems, don't let it instantiate
10816 anything else. Do allow deduction substitution and decls usable in
10817 constant expressions. */
10818 if (!targs
&& limit_bad_template_recursion (tldcl
))
10820 /* Avoid no_linkage_errors and unused function warnings for this
10822 TREE_NO_WARNING (tldcl
) = 1;
10826 /* When not -quiet, dump template instantiations other than functions, since
10827 announce_function will take care of those. */
10828 if (!quiet_flag
&& !targs
10829 && TREE_CODE (tldcl
) != TREE_LIST
10830 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10831 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10833 new_level
= tinst_level_freelist ().alloc ();
10834 new_level
->tldcl
= tldcl
;
10835 new_level
->targs
= targs
;
10836 new_level
->locus
= loc
;
10837 new_level
->errors
= errorcount
+ sorrycount
;
10838 new_level
->next
= NULL
;
10839 new_level
->refcount
= 0;
10840 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10841 set_refcount_ptr (current_tinst_level
, new_level
);
10844 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10845 depth_reached
= tinst_depth
;
10850 /* We're starting substitution of TMPL<ARGS>; record the template
10851 substitution context for diagnostics and to restore it later. */
10854 push_tinst_level (tree tmpl
, tree args
)
10856 return push_tinst_level_loc (tmpl
, args
, input_location
);
10859 /* We're starting to instantiate D; record INPUT_LOCATION and the
10860 template instantiation context for diagnostics and to restore it
10864 push_tinst_level (tree d
)
10866 return push_tinst_level_loc (d
, input_location
);
10869 /* Likewise, but record LOC as the program location. */
10872 push_tinst_level_loc (tree d
, location_t loc
)
10874 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10875 return push_tinst_level_loc (d
, NULL
, loc
);
10878 /* We're done instantiating this template; return to the instantiation
10882 pop_tinst_level (void)
10884 /* Restore the filename and line number stashed away when we started
10885 this instantiation. */
10886 input_location
= current_tinst_level
->locus
;
10887 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10891 /* We're instantiating a deferred template; restore the template
10892 instantiation context in which the instantiation was requested, which
10893 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10896 reopen_tinst_level (struct tinst_level
*level
)
10898 struct tinst_level
*t
;
10901 for (t
= level
; t
; t
= t
->next
)
10904 set_refcount_ptr (current_tinst_level
, level
);
10905 pop_tinst_level ();
10906 if (current_tinst_level
)
10907 current_tinst_level
->errors
= errorcount
+sorrycount
;
10908 return level
->maybe_get_node ();
10911 /* Returns the TINST_LEVEL which gives the original instantiation
10914 struct tinst_level
*
10915 outermost_tinst_level (void)
10917 struct tinst_level
*level
= current_tinst_level
;
10919 while (level
->next
)
10920 level
= level
->next
;
10924 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10925 vector of template arguments, as for tsubst.
10927 Returns an appropriate tsubst'd friend declaration. */
10930 tsubst_friend_function (tree decl
, tree args
)
10934 if (TREE_CODE (decl
) == FUNCTION_DECL
10935 && DECL_TEMPLATE_INSTANTIATION (decl
)
10936 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10937 /* This was a friend declared with an explicit template
10938 argument list, e.g.:
10940 friend void f<>(T);
10942 to indicate that f was a template instantiation, not a new
10943 function declaration. Now, we have to figure out what
10944 instantiation of what template. */
10946 tree template_id
, arglist
, fns
;
10949 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10951 /* Friend functions are looked up in the containing namespace scope.
10952 We must enter that scope, to avoid finding member functions of the
10953 current class with same name. */
10954 push_nested_namespace (ns
);
10955 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10956 tf_warning_or_error
, NULL_TREE
,
10957 /*integral_constant_expression_p=*/false);
10958 pop_nested_namespace (ns
);
10959 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10960 tf_warning_or_error
, NULL_TREE
);
10961 template_id
= lookup_template_function (fns
, arglist
);
10963 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10964 tmpl
= determine_specialization (template_id
, new_friend
,
10966 /*need_member_template=*/0,
10967 TREE_VEC_LENGTH (args
),
10969 return instantiate_template (tmpl
, new_args
, tf_error
);
10972 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10973 if (new_friend
== error_mark_node
)
10974 return error_mark_node
;
10976 /* The NEW_FRIEND will look like an instantiation, to the
10977 compiler, but is not an instantiation from the point of view of
10978 the language. For example, we might have had:
10980 template <class T> struct S {
10981 template <class U> friend void f(T, U);
10984 Then, in S<int>, template <class U> void f(int, U) is not an
10985 instantiation of anything. */
10987 DECL_USE_TEMPLATE (new_friend
) = 0;
10988 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10990 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10991 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10992 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10994 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10995 match in decls_match. */
10996 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10997 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
10998 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
10999 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11002 /* The mangled name for the NEW_FRIEND is incorrect. The function
11003 is not a template instantiation and should not be mangled like
11004 one. Therefore, we forget the mangling here; we'll recompute it
11005 later if we need it. */
11006 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11008 SET_DECL_RTL (new_friend
, NULL
);
11009 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11012 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11017 /* We must save some information from NEW_FRIEND before calling
11018 duplicate decls since that function will free NEW_FRIEND if
11020 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11021 tree new_friend_result_template_info
= NULL_TREE
;
11022 bool new_friend_is_defn
=
11023 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11024 (template_for_substitution (new_friend
)))
11026 tree not_tmpl
= new_friend
;
11028 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11030 /* This declaration is a `primary' template. */
11031 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11033 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11034 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11037 /* Inside pushdecl_namespace_level, we will push into the
11038 current namespace. However, the friend function should go
11039 into the namespace of the template. */
11040 ns
= decl_namespace_context (new_friend
);
11041 push_nested_namespace (ns
);
11042 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
11043 pop_nested_namespace (ns
);
11045 if (old_decl
== error_mark_node
)
11046 return error_mark_node
;
11048 if (old_decl
!= new_friend
)
11050 /* This new friend declaration matched an existing
11051 declaration. For example, given:
11053 template <class T> void f(T);
11054 template <class U> class C {
11055 template <class T> friend void f(T) {}
11058 the friend declaration actually provides the definition
11059 of `f', once C has been instantiated for some type. So,
11060 old_decl will be the out-of-class template declaration,
11061 while new_friend is the in-class definition.
11063 But, if `f' was called before this point, the
11064 instantiation of `f' will have DECL_TI_ARGS corresponding
11065 to `T' but not to `U', references to which might appear
11066 in the definition of `f'. Previously, the most general
11067 template for an instantiation of `f' was the out-of-class
11068 version; now it is the in-class version. Therefore, we
11069 run through all specialization of `f', adding to their
11070 DECL_TI_ARGS appropriately. In particular, they need a
11071 new set of outer arguments, corresponding to the
11072 arguments for this class instantiation.
11074 The same situation can arise with something like this:
11076 friend void f(int);
11077 template <class T> class C {
11078 friend void f(T) {}
11081 when `C<int>' is instantiated. Now, `f(int)' is defined
11084 if (!new_friend_is_defn
)
11085 /* On the other hand, if the in-class declaration does
11086 *not* provide a definition, then we don't want to alter
11087 existing definitions. We can just leave everything
11092 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11093 tree new_args
= TI_ARGS (new_friend_template_info
);
11095 /* Overwrite whatever template info was there before, if
11096 any, with the new template information pertaining to
11097 the declaration. */
11098 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11100 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11102 /* We should have called reregister_specialization in
11103 duplicate_decls. */
11104 gcc_assert (retrieve_specialization (new_template
,
11108 /* Instantiate it if the global has already been used. */
11109 if (DECL_ODR_USED (old_decl
))
11110 instantiate_decl (old_decl
, /*defer_ok=*/true,
11111 /*expl_inst_class_mem_p=*/false);
11117 /* Indicate that the old function template is a partial
11119 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11120 = new_friend_result_template_info
;
11122 gcc_assert (new_template
11123 == most_general_template (new_template
));
11124 gcc_assert (new_template
!= old_decl
);
11126 /* Reassign any specializations already in the hash table
11127 to the new more general template, and add the
11128 additional template args. */
11129 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11131 t
= TREE_CHAIN (t
))
11133 tree spec
= TREE_VALUE (t
);
11136 elt
.tmpl
= old_decl
;
11137 elt
.args
= DECL_TI_ARGS (spec
);
11138 elt
.spec
= NULL_TREE
;
11140 decl_specializations
->remove_elt (&elt
);
11142 DECL_TI_ARGS (spec
)
11143 = add_outermost_template_args (new_args
,
11144 DECL_TI_ARGS (spec
));
11146 register_specialization
11147 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11150 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11154 /* The information from NEW_FRIEND has been merged into OLD_DECL
11155 by duplicate_decls. */
11156 new_friend
= old_decl
;
11161 tree context
= DECL_CONTEXT (new_friend
);
11165 template <class T> class C {
11166 template <class U> friend void C1<U>::f (); // case 1
11167 friend void C2<T>::f (); // case 2
11169 we only need to make sure CONTEXT is a complete type for
11170 case 2. To distinguish between the two cases, we note that
11171 CONTEXT of case 1 remains dependent type after tsubst while
11172 this isn't true for case 2. */
11173 ++processing_template_decl
;
11174 dependent_p
= dependent_type_p (context
);
11175 --processing_template_decl
;
11178 && !complete_type_or_else (context
, NULL_TREE
))
11179 return error_mark_node
;
11181 if (COMPLETE_TYPE_P (context
))
11183 tree fn
= new_friend
;
11184 /* do_friend adds the TEMPLATE_DECL for any member friend
11185 template even if it isn't a member template, i.e.
11186 template <class T> friend A<T>::f();
11187 Look through it in that case. */
11188 if (TREE_CODE (fn
) == TEMPLATE_DECL
11189 && !PRIMARY_TEMPLATE_P (fn
))
11190 fn
= DECL_TEMPLATE_RESULT (fn
);
11191 /* Check to see that the declaration is really present, and,
11192 possibly obtain an improved declaration. */
11193 fn
= check_classfn (context
, fn
, NULL_TREE
);
11203 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11204 template arguments, as for tsubst.
11206 Returns an appropriate tsubst'd friend type or error_mark_node on
11210 tsubst_friend_class (tree friend_tmpl
, tree args
)
11214 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11216 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11217 return TREE_TYPE (tmpl
);
11220 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11221 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11222 push_nested_namespace (context
);
11225 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11226 push_nested_class (context
);
11229 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11230 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11232 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11234 /* The friend template has already been declared. Just
11235 check to see that the declarations match, and install any new
11236 default parameters. We must tsubst the default parameters,
11237 of course. We only need the innermost template parameters
11238 because that is all that redeclare_class_template will look
11240 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11241 > TMPL_ARGS_DEPTH (args
))
11243 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11244 args
, tf_warning_or_error
);
11245 location_t saved_input_location
= input_location
;
11246 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11247 tree cons
= get_constraints (tmpl
);
11248 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11249 input_location
= saved_input_location
;
11254 /* The friend template has not already been declared. In this
11255 case, the instantiation of the template class will cause the
11256 injection of this template into the namespace scope. */
11257 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11259 if (tmpl
!= error_mark_node
)
11261 /* The new TMPL is not an instantiation of anything, so we
11262 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11263 for the new type because that is supposed to be the
11264 corresponding template decl, i.e., TMPL. */
11265 DECL_USE_TEMPLATE (tmpl
) = 0;
11266 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11267 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11268 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11269 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11271 /* It is hidden. */
11272 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11273 DECL_ANTICIPATED (tmpl
)
11274 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11276 /* Substitute into and set the constraints on the new declaration. */
11277 if (tree ci
= get_constraints (friend_tmpl
))
11279 ++processing_template_decl
;
11280 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11281 DECL_FRIEND_CONTEXT (friend_tmpl
));
11282 --processing_template_decl
;
11283 set_constraints (tmpl
, ci
);
11286 /* Inject this template into the enclosing namspace scope. */
11287 tmpl
= pushdecl_namespace_level (tmpl
, true);
11291 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11292 pop_nested_namespace (context
);
11294 pop_nested_class ();
11296 return TREE_TYPE (tmpl
);
11299 /* Returns zero if TYPE cannot be completed later due to circularity.
11300 Otherwise returns one. */
11303 can_complete_type_without_circularity (tree type
)
11305 if (type
== NULL_TREE
|| type
== error_mark_node
)
11307 else if (COMPLETE_TYPE_P (type
))
11309 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11310 return can_complete_type_without_circularity (TREE_TYPE (type
));
11311 else if (CLASS_TYPE_P (type
)
11312 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11318 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11319 tsubst_flags_t
, tree
);
11321 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11322 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11325 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11326 tsubst_flags_t complain
, tree in_decl
)
11328 gcc_assert (ATTR_IS_DEPENDENT (t
));
11330 tree val
= TREE_VALUE (t
);
11331 if (val
== NULL_TREE
)
11332 /* Nothing to do. */;
11333 else if ((flag_openmp
|| flag_openmp_simd
)
11334 && is_attribute_p ("omp declare simd",
11335 get_attribute_name (t
)))
11337 tree clauses
= TREE_VALUE (val
);
11338 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11339 complain
, in_decl
);
11340 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11341 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11342 tree parms
= DECL_ARGUMENTS (*decl_p
);
11344 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11346 val
= build_tree_list (NULL_TREE
, clauses
);
11350 else if (flag_openmp
11351 && is_attribute_p ("omp declare variant base",
11352 get_attribute_name (t
)))
11354 ++cp_unevaluated_operand
;
11356 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11357 in_decl
, /*integral_constant_expression_p=*/false);
11358 --cp_unevaluated_operand
;
11359 tree chain
= TREE_CHAIN (val
);
11360 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11361 tree ctx
= copy_list (TREE_VALUE (val
));
11362 tree simd
= get_identifier ("simd");
11363 tree score
= get_identifier (" score");
11364 tree condition
= get_identifier ("condition");
11365 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11367 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11368 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11369 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11371 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11373 tree clauses
= TREE_VALUE (t2
);
11374 clauses
= tsubst_omp_clauses (clauses
,
11375 C_ORT_OMP_DECLARE_SIMD
, args
,
11376 complain
, in_decl
);
11377 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11378 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11379 TREE_VALUE (t2
) = clauses
;
11383 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11384 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11385 if (TREE_VALUE (t3
))
11388 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11389 && TREE_PURPOSE (t3
) != score
);
11390 tree v
= TREE_VALUE (t3
);
11391 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11393 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11394 v
= fold_non_dependent_expr (v
);
11395 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11396 || (TREE_PURPOSE (t3
) == score
11397 ? TREE_CODE (v
) != INTEGER_CST
11398 : !tree_fits_shwi_p (v
)))
11401 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11403 if (TREE_PURPOSE (t3
) == score
)
11404 error_at (loc
, "score argument must be "
11405 "constant integer expression");
11406 else if (allow_string
)
11407 error_at (loc
, "property must be constant "
11408 "integer expression or string "
11411 error_at (loc
, "property must be constant "
11412 "integer expression");
11415 else if (TREE_PURPOSE (t3
) == score
11416 && tree_int_cst_sgn (v
) < 0)
11419 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11421 error_at (loc
, "score argument must be "
11425 TREE_VALUE (t3
) = v
;
11430 val
= tree_cons (varid
, ctx
, chain
);
11432 /* If the first attribute argument is an identifier, don't
11433 pass it through tsubst. Attributes like mode, format,
11434 cleanup and several target specific attributes expect it
11436 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11439 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11440 /*integral_constant_expression_p=*/false);
11441 if (chain
!= TREE_CHAIN (val
))
11442 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11444 else if (PACK_EXPANSION_P (val
))
11446 /* An attribute pack expansion. */
11447 tree purp
= TREE_PURPOSE (t
);
11448 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11449 if (pack
== error_mark_node
)
11450 return error_mark_node
;
11451 int len
= TREE_VEC_LENGTH (pack
);
11452 tree list
= NULL_TREE
;
11454 for (int i
= 0; i
< len
; ++i
)
11456 tree elt
= TREE_VEC_ELT (pack
, i
);
11457 *q
= build_tree_list (purp
, elt
);
11458 q
= &TREE_CHAIN (*q
);
11463 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11464 /*integral_constant_expression_p=*/false);
11466 if (val
!= TREE_VALUE (t
))
11467 return build_tree_list (TREE_PURPOSE (t
), val
);
11471 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11472 unchanged or a new TREE_LIST chain. */
11475 tsubst_attributes (tree attributes
, tree args
,
11476 tsubst_flags_t complain
, tree in_decl
)
11478 tree last_dep
= NULL_TREE
;
11480 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11481 if (ATTR_IS_DEPENDENT (t
))
11484 attributes
= copy_list (attributes
);
11489 for (tree
*p
= &attributes
; *p
; )
11492 if (ATTR_IS_DEPENDENT (t
))
11494 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11499 p
= &TREE_CHAIN (*p
);
11500 *p
= TREE_CHAIN (t
);
11504 p
= &TREE_CHAIN (*p
);
11510 /* Apply any attributes which had to be deferred until instantiation
11511 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11512 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11515 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11516 tree args
, tsubst_flags_t complain
, tree in_decl
)
11518 tree last_dep
= NULL_TREE
;
11522 if (attributes
== NULL_TREE
)
11525 if (DECL_P (*decl_p
))
11527 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11529 p
= &DECL_ATTRIBUTES (*decl_p
);
11530 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11531 to our attributes parameter. */
11532 gcc_assert (*p
== attributes
);
11536 p
= &TYPE_ATTRIBUTES (*decl_p
);
11537 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11538 lookup_template_class_1, and should be preserved. */
11539 gcc_assert (*p
!= attributes
);
11541 p
= &TREE_CHAIN (*p
);
11544 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11545 if (ATTR_IS_DEPENDENT (t
))
11548 attributes
= copy_list (attributes
);
11555 tree late_attrs
= NULL_TREE
;
11556 tree
*q
= &late_attrs
;
11561 if (ATTR_IS_DEPENDENT (t
))
11563 *p
= TREE_CHAIN (t
);
11564 TREE_CHAIN (t
) = NULL_TREE
;
11565 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11567 q
= &TREE_CHAIN (*q
);
11570 p
= &TREE_CHAIN (t
);
11573 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11577 /* The template TMPL is being instantiated with the template arguments TARGS.
11578 Perform the access checks that we deferred when parsing the template. */
11581 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11584 deferred_access_check
*chk
;
11586 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11589 if (vec
<deferred_access_check
, va_gc
> *access_checks
11590 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11591 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11593 tree decl
= chk
->decl
;
11594 tree diag_decl
= chk
->diag_decl
;
11595 tree type_scope
= TREE_TYPE (chk
->binfo
);
11597 if (uses_template_parms (type_scope
))
11598 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11600 /* Make access check error messages point to the location
11601 of the use of the typedef. */
11602 iloc_sentinel
ils (chk
->loc
);
11603 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11604 decl
, diag_decl
, tf_warning_or_error
);
11609 instantiate_class_template_1 (tree type
)
11611 tree templ
, args
, pattern
, t
, member
;
11615 unsigned int saved_maximum_field_alignment
;
11618 if (type
== error_mark_node
)
11619 return error_mark_node
;
11621 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11622 || uses_template_parms (type
))
11625 /* Figure out which template is being instantiated. */
11626 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11627 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11629 /* Mark the type as in the process of being defined. */
11630 TYPE_BEING_DEFINED (type
) = 1;
11632 /* We may be in the middle of deferred access check. Disable
11634 deferring_access_check_sentinel
acs (dk_no_deferred
);
11636 /* Determine what specialization of the original template to
11638 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11639 if (t
== error_mark_node
)
11640 return error_mark_node
;
11643 /* This TYPE is actually an instantiation of a partial
11644 specialization. We replace the innermost set of ARGS with
11645 the arguments appropriate for substitution. For example,
11648 template <class T> struct S {};
11649 template <class T> struct S<T*> {};
11651 and supposing that we are instantiating S<int*>, ARGS will
11652 presently be {int*} -- but we need {int}. */
11653 pattern
= TREE_TYPE (t
);
11654 args
= TREE_PURPOSE (t
);
11658 pattern
= TREE_TYPE (templ
);
11659 args
= CLASSTYPE_TI_ARGS (type
);
11662 /* If the template we're instantiating is incomplete, then clearly
11663 there's nothing we can do. */
11664 if (!COMPLETE_TYPE_P (pattern
))
11666 /* We can try again later. */
11667 TYPE_BEING_DEFINED (type
) = 0;
11671 /* If we've recursively instantiated too many templates, stop. */
11672 if (! push_tinst_level (type
))
11675 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11676 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11678 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11679 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11680 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11681 fn_context
= error_mark_node
;
11683 push_to_top_level ();
11686 cp_unevaluated_operand
= 0;
11687 c_inhibit_evaluation_warnings
= 0;
11689 /* Use #pragma pack from the template context. */
11690 saved_maximum_field_alignment
= maximum_field_alignment
;
11691 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11693 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11695 /* Set the input location to the most specialized template definition.
11696 This is needed if tsubsting causes an error. */
11697 typedecl
= TYPE_MAIN_DECL (pattern
);
11698 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11699 DECL_SOURCE_LOCATION (typedecl
);
11701 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11702 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11703 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11704 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11705 if (ANON_AGGR_TYPE_P (pattern
))
11706 SET_ANON_AGGR_TYPE_P (type
);
11707 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11709 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11710 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11711 /* Adjust visibility for template arguments. */
11712 determine_visibility (TYPE_MAIN_DECL (type
));
11714 if (CLASS_TYPE_P (type
))
11715 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11717 pbinfo
= TYPE_BINFO (pattern
);
11719 /* We should never instantiate a nested class before its enclosing
11720 class; we need to look up the nested class by name before we can
11721 instantiate it, and that lookup should instantiate the enclosing
11723 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11724 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11726 base_list
= NULL_TREE
;
11727 if (BINFO_N_BASE_BINFOS (pbinfo
))
11733 /* We must enter the scope containing the type, as that is where
11734 the accessibility of types named in dependent bases are
11736 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11738 /* Substitute into each of the bases to determine the actual
11740 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11743 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11744 tree expanded_bases
= NULL_TREE
;
11747 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11750 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11751 args
, tf_error
, NULL_TREE
);
11752 if (expanded_bases
== error_mark_node
)
11755 len
= TREE_VEC_LENGTH (expanded_bases
);
11758 for (idx
= 0; idx
< len
; idx
++)
11760 if (expanded_bases
)
11761 /* Extract the already-expanded base class. */
11762 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11764 /* Substitute to figure out the base class. */
11765 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11768 if (base
== error_mark_node
)
11771 base_list
= tree_cons (access
, base
, base_list
);
11772 if (BINFO_VIRTUAL_P (pbase_binfo
))
11773 TREE_TYPE (base_list
) = integer_type_node
;
11777 /* The list is now in reverse order; correct that. */
11778 base_list
= nreverse (base_list
);
11781 pop_scope (pushed_scope
);
11783 /* Now call xref_basetypes to set up all the base-class
11785 xref_basetypes (type
, base_list
);
11787 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11788 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11789 args
, tf_error
, NULL_TREE
);
11790 fixup_attribute_variants (type
);
11792 /* Now that our base classes are set up, enter the scope of the
11793 class, so that name lookups into base classes, etc. will work
11794 correctly. This is precisely analogous to what we do in
11795 begin_class_definition when defining an ordinary non-template
11796 class, except we also need to push the enclosing classes. */
11797 push_nested_class (type
);
11799 /* Now members are processed in the order of declaration. */
11800 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11801 member
; member
= TREE_CHAIN (member
))
11803 tree t
= TREE_VALUE (member
);
11805 if (TREE_PURPOSE (member
))
11809 if (LAMBDA_TYPE_P (t
))
11810 /* A closure type for a lambda in an NSDMI or default argument.
11811 Ignore it; it will be regenerated when needed. */
11814 /* Build new CLASSTYPE_NESTED_UTDS. */
11815 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11816 && TYPE_LANG_SPECIFIC (t
)
11817 && CLASSTYPE_IS_TEMPLATE (t
));
11819 /* If the member is a class template, then -- even after
11820 substitution -- there may be dependent types in the
11821 template argument list for the class. We increment
11822 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11823 that function will assume that no types are dependent
11824 when outside of a template. */
11825 if (class_template_p
)
11826 ++processing_template_decl
;
11827 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11828 if (class_template_p
)
11829 --processing_template_decl
;
11830 if (newtag
== error_mark_node
)
11833 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11835 tree name
= TYPE_IDENTIFIER (t
);
11837 if (class_template_p
)
11838 /* Unfortunately, lookup_template_class sets
11839 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11840 instantiation (i.e., for the type of a member
11841 template class nested within a template class.)
11842 This behavior is required for
11843 maybe_process_partial_specialization to work
11844 correctly, but is not accurate in this case;
11845 the TAG is not an instantiation of anything.
11846 (The corresponding TEMPLATE_DECL is an
11847 instantiation, but the TYPE is not.) */
11848 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11850 /* Now, we call pushtag to put this NEWTAG into the scope of
11851 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11852 pushtag calling push_template_decl. We don't have to do
11853 this for enums because it will already have been done in
11856 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11857 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11860 else if (DECL_DECLARES_FUNCTION_P (t
))
11864 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11865 ++processing_template_decl
;
11866 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11867 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11868 --processing_template_decl
;
11869 set_current_access_from_decl (r
);
11870 finish_member_declaration (r
);
11871 /* Instantiate members marked with attribute used. */
11872 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11874 if (TREE_CODE (r
) == FUNCTION_DECL
11875 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11876 cp_check_omp_declare_reduction (r
);
11878 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11879 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11880 /* A closure type for a lambda in an NSDMI or default argument.
11881 Ignore it; it will be regenerated when needed. */;
11884 /* Build new TYPE_FIELDS. */
11885 if (TREE_CODE (t
) == STATIC_ASSERT
)
11886 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11887 /*integral_constant_expression_p=*/true);
11888 else if (TREE_CODE (t
) != CONST_DECL
)
11891 tree vec
= NULL_TREE
;
11894 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11895 /* The file and line for this declaration, to
11896 assist in error message reporting. Since we
11897 called push_tinst_level above, we don't need to
11899 input_location
= DECL_SOURCE_LOCATION (t
);
11901 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11902 ++processing_template_decl
;
11903 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11904 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11905 --processing_template_decl
;
11907 if (TREE_CODE (r
) == TREE_VEC
)
11909 /* A capture pack became multiple fields. */
11911 len
= TREE_VEC_LENGTH (vec
);
11914 for (int i
= 0; i
< len
; ++i
)
11917 r
= TREE_VEC_ELT (vec
, i
);
11922 [t]he initialization (and any associated
11923 side-effects) of a static data member does
11924 not occur unless the static data member is
11925 itself used in a way that requires the
11926 definition of the static data member to
11929 Therefore, we do not substitute into the
11930 initialized for the static data member here. */
11931 finish_static_data_member_decl
11933 /*init=*/NULL_TREE
,
11934 /*init_const_expr_p=*/false,
11935 /*asmspec_tree=*/NULL_TREE
,
11937 /* Instantiate members marked with attribute used. */
11938 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11941 else if (TREE_CODE (r
) == FIELD_DECL
)
11943 /* Determine whether R has a valid type and can be
11944 completed later. If R is invalid, then its type
11945 is replaced by error_mark_node. */
11946 tree rtype
= TREE_TYPE (r
);
11947 if (can_complete_type_without_circularity (rtype
))
11948 complete_type (rtype
);
11950 if (!complete_or_array_type_p (rtype
))
11952 /* If R's type couldn't be completed and
11953 it isn't a flexible array member (whose
11954 type is incomplete by definition) give
11956 cxx_incomplete_type_error (r
, rtype
);
11957 TREE_TYPE (r
) = error_mark_node
;
11959 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11960 && TYPE_DOMAIN (rtype
) == NULL_TREE
11961 && (TREE_CODE (type
) == UNION_TYPE
11962 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11964 error ("flexible array member %qD in union", r
);
11965 TREE_TYPE (r
) = error_mark_node
;
11967 else if (!verify_type_context (input_location
,
11968 TCTX_FIELD
, rtype
))
11969 TREE_TYPE (r
) = error_mark_node
;
11972 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11973 such a thing will already have been added to the field
11974 list by tsubst_enum in finish_member_declaration in the
11975 CLASSTYPE_NESTED_UTDS case above. */
11976 if (!(TREE_CODE (r
) == TYPE_DECL
11977 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11978 && DECL_ARTIFICIAL (r
)))
11980 set_current_access_from_decl (r
);
11981 finish_member_declaration (r
);
11989 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11990 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11992 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11994 tree friend_type
= t
;
11995 bool adjust_processing_template_decl
= false;
11997 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11999 /* template <class T> friend class C; */
12000 friend_type
= tsubst_friend_class (friend_type
, args
);
12001 adjust_processing_template_decl
= true;
12003 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12005 /* template <class T> friend class C::D; */
12006 friend_type
= tsubst (friend_type
, args
,
12007 tf_warning_or_error
, NULL_TREE
);
12008 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12009 friend_type
= TREE_TYPE (friend_type
);
12010 adjust_processing_template_decl
= true;
12012 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12013 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12015 /* This could be either
12019 when dependent_type_p is false or
12021 template <class U> friend class T::C;
12024 /* Bump processing_template_decl in case this is something like
12025 template <class T> friend struct A<T>::B. */
12026 ++processing_template_decl
;
12027 friend_type
= tsubst (friend_type
, args
,
12028 tf_warning_or_error
, NULL_TREE
);
12029 if (dependent_type_p (friend_type
))
12030 adjust_processing_template_decl
= true;
12031 --processing_template_decl
;
12033 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
12034 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
12035 && TYPE_HIDDEN_P (friend_type
))
12039 where C hasn't been declared yet. Let's lookup name
12040 from namespace scope directly, bypassing any name that
12041 come from dependent base class. */
12042 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
12044 /* The call to xref_tag_from_type does injection for friend
12046 push_nested_namespace (ns
);
12048 xref_tag_from_type (friend_type
, NULL_TREE
,
12049 /*tag_scope=*/ts_current
);
12050 pop_nested_namespace (ns
);
12052 else if (uses_template_parms (friend_type
))
12053 /* friend class C<T>; */
12054 friend_type
= tsubst (friend_type
, args
,
12055 tf_warning_or_error
, NULL_TREE
);
12060 where C is already declared or
12062 friend class C<int>;
12064 We don't have to do anything in these cases. */
12066 if (adjust_processing_template_decl
)
12067 /* Trick make_friend_class into realizing that the friend
12068 we're adding is a template, not an ordinary class. It's
12069 important that we use make_friend_class since it will
12070 perform some error-checking and output cross-reference
12072 ++processing_template_decl
;
12074 if (friend_type
!= error_mark_node
)
12075 make_friend_class (type
, friend_type
, /*complain=*/false);
12077 if (adjust_processing_template_decl
)
12078 --processing_template_decl
;
12082 /* Build new DECL_FRIENDLIST. */
12085 /* The file and line for this declaration, to
12086 assist in error message reporting. Since we
12087 called push_tinst_level above, we don't need to
12089 input_location
= DECL_SOURCE_LOCATION (t
);
12091 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12093 ++processing_template_decl
;
12094 push_deferring_access_checks (dk_no_check
);
12097 r
= tsubst_friend_function (t
, args
);
12098 add_friend (type
, r
, /*complain=*/false);
12099 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12101 pop_deferring_access_checks ();
12102 --processing_template_decl
;
12110 /* Restore these before substituting into the lambda capture
12112 cp_unevaluated_operand
= saved_unevaluated_operand
;
12113 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12116 /* Set the file and line number information to whatever is given for
12117 the class itself. This puts error messages involving generated
12118 implicit functions at a predictable point, and the same point
12119 that would be used for non-template classes. */
12120 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12122 unreverse_member_declarations (type
);
12123 finish_struct_1 (type
);
12124 TYPE_BEING_DEFINED (type
) = 0;
12126 /* We don't instantiate default arguments for member functions. 14.7.1:
12128 The implicit instantiation of a class template specialization causes
12129 the implicit instantiation of the declarations, but not of the
12130 definitions or default arguments, of the class member functions,
12131 member classes, static data members and member templates.... */
12133 perform_instantiation_time_access_checks (pattern
, args
);
12134 perform_deferred_access_checks (tf_warning_or_error
);
12135 pop_nested_class ();
12136 maximum_field_alignment
= saved_maximum_field_alignment
;
12138 pop_from_top_level ();
12139 pop_tinst_level ();
12141 /* The vtable for a template class can be emitted in any translation
12142 unit in which the class is instantiated. When there is no key
12143 method, however, finish_struct_1 will already have added TYPE to
12144 the keyed_classes. */
12145 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12146 vec_safe_push (keyed_classes
, type
);
12151 /* Wrapper for instantiate_class_template_1. */
12154 instantiate_class_template (tree type
)
12157 timevar_push (TV_TEMPLATE_INST
);
12158 ret
= instantiate_class_template_1 (type
);
12159 timevar_pop (TV_TEMPLATE_INST
);
12164 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12170 else if (TYPE_P (t
))
12171 r
= tsubst (t
, args
, complain
, in_decl
);
12174 if (!(complain
& tf_warning
))
12175 ++c_inhibit_evaluation_warnings
;
12176 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12177 /*integral_constant_expression_p=*/true);
12178 if (!(complain
& tf_warning
))
12179 --c_inhibit_evaluation_warnings
;
12185 /* Given a function parameter pack TMPL_PARM and some function parameters
12186 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12187 and set *SPEC_P to point at the next point in the list. */
12190 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12192 /* Collect all of the extra "packed" parameters into an
12195 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12196 tree spec_parm
= *spec_p
;
12199 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12201 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12204 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12205 parmvec
= make_tree_vec (len
);
12206 spec_parm
= *spec_p
;
12207 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12209 tree elt
= spec_parm
;
12210 if (DECL_PACK_P (elt
))
12211 elt
= make_pack_expansion (elt
);
12212 TREE_VEC_ELT (parmvec
, i
) = elt
;
12215 /* Build the argument packs. */
12216 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12217 *spec_p
= spec_parm
;
12222 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12223 NONTYPE_ARGUMENT_PACK. */
12226 make_fnparm_pack (tree spec_parm
)
12228 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12231 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12232 pack expansion with no extra args, 2 if it has extra args, or 0
12233 if it is not a pack expansion. */
12236 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12238 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12239 /* We're being called before this happens in tsubst_pack_expansion. */
12240 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12241 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12242 if (i
>= TREE_VEC_LENGTH (vec
))
12244 tree elt
= TREE_VEC_ELT (vec
, i
);
12246 /* A decl pack is itself an expansion. */
12247 elt
= TREE_TYPE (elt
);
12248 if (!PACK_EXPANSION_P (elt
))
12250 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12256 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12259 make_argument_pack_select (tree arg_pack
, unsigned index
)
12261 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12263 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12264 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12269 /* This is a subroutine of tsubst_pack_expansion.
12271 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12272 mechanism to store the (non complete list of) arguments of the
12273 substitution and return a non substituted pack expansion, in order
12274 to wait for when we have enough arguments to really perform the
12278 use_pack_expansion_extra_args_p (tree parm_packs
,
12280 bool has_empty_arg
)
12282 /* If one pack has an expansion and another pack has a normal
12283 argument or if one pack has an empty argument and an another
12284 one hasn't then tsubst_pack_expansion cannot perform the
12285 substitution and need to fall back on the
12286 PACK_EXPANSION_EXTRA mechanism. */
12287 if (parm_packs
== NULL_TREE
)
12289 else if (has_empty_arg
)
12291 /* If all the actual packs are pack expansions, we can still
12292 subsitute directly. */
12293 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12295 tree a
= TREE_VALUE (p
);
12296 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12297 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12298 a
= ARGUMENT_PACK_ARGS (a
);
12299 if (TREE_VEC_LENGTH (a
) == 1)
12300 a
= TREE_VEC_ELT (a
, 0);
12301 if (PACK_EXPANSION_P (a
))
12308 bool has_expansion_arg
= false;
12309 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12311 bool has_non_expansion_arg
= false;
12312 for (tree parm_pack
= parm_packs
;
12314 parm_pack
= TREE_CHAIN (parm_pack
))
12316 tree arg
= TREE_VALUE (parm_pack
);
12318 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12320 /* We can't substitute a pack expansion with extra args into
12324 has_expansion_arg
= true;
12326 has_non_expansion_arg
= true;
12329 if (has_expansion_arg
&& has_non_expansion_arg
)
12335 /* [temp.variadic]/6 says that:
12337 The instantiation of a pack expansion [...]
12338 produces a list E1,E2, ..., En, where N is the number of elements
12339 in the pack expansion parameters.
12341 This subroutine of tsubst_pack_expansion produces one of these Ei.
12343 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12344 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12345 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12346 INDEX is the index 'i' of the element Ei to produce. ARGS,
12347 COMPLAIN, and IN_DECL are the same parameters as for the
12348 tsubst_pack_expansion function.
12350 The function returns the resulting Ei upon successful completion,
12351 or error_mark_node.
12353 Note that this function possibly modifies the ARGS parameter, so
12354 it's the responsibility of the caller to restore it. */
12357 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12360 tree args
/* This parm gets
12362 tsubst_flags_t complain
,
12366 bool ith_elem_is_expansion
= false;
12368 /* For each parameter pack, change the substitution of the parameter
12369 pack to the ith argument in its argument pack, then expand the
12371 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12373 tree parm
= TREE_PURPOSE (pack
);
12374 tree arg_pack
= TREE_VALUE (pack
);
12375 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12377 ith_elem_is_expansion
|=
12378 argument_pack_element_is_expansion_p (arg_pack
, index
);
12380 /* Select the Ith argument from the pack. */
12381 if (TREE_CODE (parm
) == PARM_DECL
12383 || TREE_CODE (parm
) == FIELD_DECL
)
12387 aps
= make_argument_pack_select (arg_pack
, index
);
12388 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12389 return error_mark_node
;
12390 register_local_specialization (aps
, parm
);
12393 aps
= retrieve_local_specialization (parm
);
12398 template_parm_level_and_index (parm
, &level
, &idx
);
12402 aps
= make_argument_pack_select (arg_pack
, index
);
12403 /* Update the corresponding argument. */
12404 TMPL_ARG (args
, level
, idx
) = aps
;
12407 /* Re-use the ARGUMENT_PACK_SELECT. */
12408 aps
= TMPL_ARG (args
, level
, idx
);
12410 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12413 /* Substitute into the PATTERN with the (possibly altered)
12415 if (pattern
== in_decl
)
12416 /* Expanding a fixed parameter pack from
12417 coerce_template_parameter_pack. */
12418 t
= tsubst_decl (pattern
, args
, complain
);
12419 else if (pattern
== error_mark_node
)
12420 t
= error_mark_node
;
12421 else if (!TYPE_P (pattern
))
12422 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12423 /*integral_constant_expression_p=*/false);
12425 t
= tsubst (pattern
, args
, complain
, in_decl
);
12427 /* If the Ith argument pack element is a pack expansion, then
12428 the Ith element resulting from the substituting is going to
12429 be a pack expansion as well. */
12430 if (ith_elem_is_expansion
)
12431 t
= make_pack_expansion (t
, complain
);
12436 /* When the unexpanded parameter pack in a fold expression expands to an empty
12437 sequence, the value of the expression is as follows; the program is
12438 ill-formed if the operator is not listed in this table.
12445 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12447 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12448 if (!FOLD_EXPR_MODIFY_P (t
))
12451 case TRUTH_ANDIF_EXPR
:
12452 return boolean_true_node
;
12453 case TRUTH_ORIF_EXPR
:
12454 return boolean_false_node
;
12455 case COMPOUND_EXPR
:
12461 if (complain
& tf_error
)
12462 error_at (location_of (t
),
12463 "fold of empty expansion over %O", code
);
12464 return error_mark_node
;
12467 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12468 form an expression that combines the two terms using the
12472 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12474 tree op
= FOLD_EXPR_OP (t
);
12475 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12477 // Handle compound assignment operators.
12478 if (FOLD_EXPR_MODIFY_P (t
))
12479 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12481 warning_sentinel
s(warn_parentheses
);
12484 case COMPOUND_EXPR
:
12485 return build_x_compound_expr (input_location
, left
, right
, complain
);
12487 return build_x_binary_op (input_location
, code
,
12488 left
, TREE_CODE (left
),
12489 right
, TREE_CODE (right
),
12495 /* Substitute ARGS into the pack of a fold expression T. */
12498 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12500 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12503 /* Substitute ARGS into the pack of a fold expression T. */
12506 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12508 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12511 /* Expand a PACK of arguments into a grouped as left fold.
12512 Given a pack containing elements A0, A1, ..., An and an
12513 operator @, this builds the expression:
12515 ((A0 @ A1) @ A2) ... @ An
12517 Note that PACK must not be empty.
12519 The operator is defined by the original fold expression T. */
12522 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12524 tree left
= TREE_VEC_ELT (pack
, 0);
12525 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12527 tree right
= TREE_VEC_ELT (pack
, i
);
12528 left
= fold_expression (t
, left
, right
, complain
);
12533 /* Substitute into a unary left fold expression. */
12536 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12539 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12540 if (pack
== error_mark_node
)
12541 return error_mark_node
;
12542 if (PACK_EXPANSION_P (pack
))
12544 tree r
= copy_node (t
);
12545 FOLD_EXPR_PACK (r
) = pack
;
12548 if (TREE_VEC_LENGTH (pack
) == 0)
12549 return expand_empty_fold (t
, complain
);
12551 return expand_left_fold (t
, pack
, complain
);
12554 /* Substitute into a binary left fold expression.
12556 Do ths by building a single (non-empty) vector of argumnts and
12557 building the expression from those elements. */
12560 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12563 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12564 if (pack
== error_mark_node
)
12565 return error_mark_node
;
12566 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12567 if (init
== error_mark_node
)
12568 return error_mark_node
;
12570 if (PACK_EXPANSION_P (pack
))
12572 tree r
= copy_node (t
);
12573 FOLD_EXPR_PACK (r
) = pack
;
12574 FOLD_EXPR_INIT (r
) = init
;
12578 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12579 TREE_VEC_ELT (vec
, 0) = init
;
12580 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12581 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12583 return expand_left_fold (t
, vec
, complain
);
12586 /* Expand a PACK of arguments into a grouped as right fold.
12587 Given a pack containing elementns A0, A1, ..., and an
12588 operator @, this builds the expression:
12590 A0@ ... (An-2 @ (An-1 @ An))
12592 Note that PACK must not be empty.
12594 The operator is defined by the original fold expression T. */
12597 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12599 // Build the expression.
12600 int n
= TREE_VEC_LENGTH (pack
);
12601 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12602 for (--n
; n
!= 0; --n
)
12604 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12605 right
= fold_expression (t
, left
, right
, complain
);
12610 /* Substitute into a unary right fold expression. */
12613 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12616 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12617 if (pack
== error_mark_node
)
12618 return error_mark_node
;
12619 if (PACK_EXPANSION_P (pack
))
12621 tree r
= copy_node (t
);
12622 FOLD_EXPR_PACK (r
) = pack
;
12625 if (TREE_VEC_LENGTH (pack
) == 0)
12626 return expand_empty_fold (t
, complain
);
12628 return expand_right_fold (t
, pack
, complain
);
12631 /* Substitute into a binary right fold expression.
12633 Do ths by building a single (non-empty) vector of arguments and
12634 building the expression from those elements. */
12637 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12640 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12641 if (pack
== error_mark_node
)
12642 return error_mark_node
;
12643 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12644 if (init
== error_mark_node
)
12645 return error_mark_node
;
12647 if (PACK_EXPANSION_P (pack
))
12649 tree r
= copy_node (t
);
12650 FOLD_EXPR_PACK (r
) = pack
;
12651 FOLD_EXPR_INIT (r
) = init
;
12655 int n
= TREE_VEC_LENGTH (pack
);
12656 tree vec
= make_tree_vec (n
+ 1);
12657 for (int i
= 0; i
< n
; ++i
)
12658 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12659 TREE_VEC_ELT (vec
, n
) = init
;
12661 return expand_right_fold (t
, vec
, complain
);
12664 /* Walk through the pattern of a pack expansion, adding everything in
12665 local_specializations to a list. */
12670 hash_set
<tree
> internal
;
12672 tsubst_flags_t complain
;
12674 el_data (tsubst_flags_t c
)
12675 : extra (NULL_TREE
), complain (c
) {}
12678 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12680 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12681 tree
*extra
= &data
.extra
;
12682 tsubst_flags_t complain
= data
.complain
;
12684 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12685 /* Remember local typedefs (85214). */
12686 tp
= &TYPE_NAME (*tp
);
12688 if (TREE_CODE (*tp
) == DECL_EXPR
)
12689 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12690 else if (tree spec
= retrieve_local_specialization (*tp
))
12692 if (data
.internal
.contains (*tp
))
12693 /* Don't mess with variables declared within the pattern. */
12695 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12697 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12698 tree args
= ARGUMENT_PACK_ARGS (spec
);
12699 if (TREE_VEC_LENGTH (args
) == 1)
12701 tree elt
= TREE_VEC_ELT (args
, 0);
12702 if (PACK_EXPANSION_P (elt
))
12703 elt
= PACK_EXPANSION_PATTERN (elt
);
12704 if (DECL_PACK_P (elt
))
12707 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12709 /* Handle lambda capture here, since we aren't doing any
12710 substitution now, and so tsubst_copy won't call
12711 process_outer_var_ref. */
12712 tree args
= ARGUMENT_PACK_ARGS (spec
);
12713 int len
= TREE_VEC_LENGTH (args
);
12714 for (int i
= 0; i
< len
; ++i
)
12716 tree arg
= TREE_VEC_ELT (args
, i
);
12718 if (outer_automatic_var_p (arg
))
12719 carg
= process_outer_var_ref (arg
, complain
);
12722 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12726 spec
= copy_node (spec
);
12727 args
= copy_node (args
);
12728 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12729 register_local_specialization (spec
, *tp
);
12731 TREE_VEC_ELT (args
, i
) = carg
;
12736 if (outer_automatic_var_p (spec
))
12737 spec
= process_outer_var_ref (spec
, complain
);
12738 *extra
= tree_cons (*tp
, spec
, *extra
);
12743 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12745 el_data
data (complain
);
12746 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12750 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12751 for use in PACK_EXPANSION_EXTRA_ARGS. */
12754 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12757 if (local_specializations
)
12758 if (tree locals
= extract_local_specs (pattern
, complain
))
12759 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12763 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12764 normal template args to ARGS. */
12767 add_extra_args (tree extra
, tree args
)
12769 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12771 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12773 /* The partial instantiation involved local declarations collected in
12774 extract_local_specs; map from the general template to our local
12776 tree gen
= TREE_PURPOSE (elt
);
12777 tree inst
= TREE_VALUE (elt
);
12779 if (tree local
= retrieve_local_specialization (inst
))
12781 /* else inst is already a full instantiation of the pack. */
12782 register_local_specialization (inst
, gen
);
12784 gcc_assert (!TREE_PURPOSE (extra
));
12785 extra
= TREE_VALUE (extra
);
12788 /* I think we should always be able to substitute dependent args into the
12789 pattern. If that turns out to be incorrect in some cases, enable the
12790 alternate code (and add complain/in_decl parms to this function). */
12791 gcc_checking_assert (!uses_template_parms (extra
));
12793 if (!uses_template_parms (extra
))
12795 gcc_unreachable ();
12796 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12797 args
= add_outermost_template_args (args
, extra
);
12801 args
= add_to_template_args (extra
, args
);
12805 /* Substitute ARGS into T, which is an pack expansion
12806 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12807 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12808 (if only a partial substitution could be performed) or
12809 ERROR_MARK_NODE if there was an error. */
12811 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12815 tree pack
, packs
= NULL_TREE
;
12816 bool unsubstituted_packs
= false;
12819 bool need_local_specializations
= false;
12822 gcc_assert (PACK_EXPANSION_P (t
));
12823 pattern
= PACK_EXPANSION_PATTERN (t
);
12825 /* Add in any args remembered from an earlier partial instantiation. */
12826 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12828 levels
= TMPL_ARGS_DEPTH (args
);
12830 /* Determine the argument packs that will instantiate the parameter
12831 packs used in the expansion expression. While we're at it,
12832 compute the number of arguments to be expanded and make sure it
12834 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12835 pack
= TREE_CHAIN (pack
))
12837 tree parm_pack
= TREE_VALUE (pack
);
12838 tree arg_pack
= NULL_TREE
;
12839 tree orig_arg
= NULL_TREE
;
12842 if (TREE_CODE (parm_pack
) == BASES
)
12844 gcc_assert (parm_pack
== pattern
);
12845 if (BASES_DIRECT (parm_pack
))
12846 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12851 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12852 args
, complain
, in_decl
,
12855 else if (builtin_pack_call_p (parm_pack
))
12857 if (parm_pack
!= pattern
)
12859 if (complain
& tf_error
)
12860 sorry ("%qE is not the entire pattern of the pack expansion",
12862 return error_mark_node
;
12864 return expand_builtin_pack_call (parm_pack
, args
,
12865 complain
, in_decl
);
12867 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12869 /* We know we have correct local_specializations if this
12870 expansion is at function scope, or if we're dealing with a
12871 local parameter in a requires expression; for the latter,
12872 tsubst_requires_expr set it up appropriately. */
12873 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12874 arg_pack
= retrieve_local_specialization (parm_pack
);
12876 /* We can't rely on local_specializations for a parameter
12877 name used later in a function declaration (such as in a
12878 late-specified return type). Even if it exists, it might
12879 have the wrong value for a recursive call. */
12880 need_local_specializations
= true;
12884 /* This parameter pack was used in an unevaluated context. Just
12885 make a dummy decl, since it's only used for its type. */
12886 ++cp_unevaluated_operand
;
12887 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12888 --cp_unevaluated_operand
;
12889 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12890 /* Partial instantiation of the parm_pack, we can't build
12891 up an argument pack yet. */
12892 arg_pack
= NULL_TREE
;
12894 arg_pack
= make_fnparm_pack (arg_pack
);
12896 else if (DECL_PACK_P (arg_pack
))
12897 /* This argument pack isn't fully instantiated yet. */
12898 arg_pack
= NULL_TREE
;
12900 else if (is_capture_proxy (parm_pack
))
12902 arg_pack
= retrieve_local_specialization (parm_pack
);
12903 if (DECL_PACK_P (arg_pack
))
12904 arg_pack
= NULL_TREE
;
12909 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12910 if (level
<= levels
)
12911 arg_pack
= TMPL_ARG (args
, level
, idx
);
12913 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12914 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12915 arg_pack
= NULL_TREE
;
12918 orig_arg
= arg_pack
;
12919 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12920 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12922 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12923 /* This can only happen if we forget to expand an argument
12924 pack somewhere else. Just return an error, silently. */
12926 result
= make_tree_vec (1);
12927 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12934 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12936 /* Don't bother trying to do a partial substitution with
12937 incomplete packs; we'll try again after deduction. */
12938 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12943 else if (len
!= my_len
)
12945 if (!(complain
& tf_error
))
12946 /* Fail quietly. */;
12947 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12948 error ("mismatched argument pack lengths while expanding %qT",
12951 error ("mismatched argument pack lengths while expanding %qE",
12953 return error_mark_node
;
12956 /* Keep track of the parameter packs and their corresponding
12958 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12959 TREE_TYPE (packs
) = orig_arg
;
12963 /* We can't substitute for this parameter pack. We use a flag as
12964 well as the missing_level counter because function parameter
12965 packs don't have a level. */
12966 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12967 unsubstituted_packs
= true;
12971 /* If the expansion is just T..., return the matching argument pack, unless
12972 we need to call convert_from_reference on all the elements. This is an
12973 important optimization; see c++/68422. */
12974 if (!unsubstituted_packs
12975 && TREE_PURPOSE (packs
) == pattern
)
12977 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12979 /* If the argument pack is a single pack expansion, pull it out. */
12980 if (TREE_VEC_LENGTH (args
) == 1
12981 && pack_expansion_args_count (args
))
12982 return TREE_VEC_ELT (args
, 0);
12984 /* Types need no adjustment, nor does sizeof..., and if we still have
12985 some pack expansion args we won't do anything yet. */
12986 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12987 || PACK_EXPANSION_SIZEOF_P (t
)
12988 || pack_expansion_args_count (args
))
12990 /* Also optimize expression pack expansions if we can tell that the
12991 elements won't have reference type. */
12992 tree type
= TREE_TYPE (pattern
);
12993 if (type
&& !TYPE_REF_P (type
)
12994 && !PACK_EXPANSION_P (type
)
12995 && !WILDCARD_TYPE_P (type
))
12997 /* Otherwise use the normal path so we get convert_from_reference. */
13000 /* We cannot expand this expansion expression, because we don't have
13001 all of the argument packs we need. */
13002 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
13004 /* We got some full packs, but we can't substitute them in until we
13005 have values for all the packs. So remember these until then. */
13007 t
= make_pack_expansion (pattern
, complain
);
13008 PACK_EXPANSION_EXTRA_ARGS (t
)
13009 = build_extra_args (pattern
, args
, complain
);
13013 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13014 type, so create our own local specializations map; the current map is
13015 either NULL or (in the case of recursive unification) might have
13016 bindings that we don't want to use or alter. */
13017 local_specialization_stack
lss (need_local_specializations
13018 ? lss_blank
: lss_nop
);
13020 if (unsubstituted_packs
)
13022 /* There were no real arguments, we're just replacing a parameter
13023 pack with another version of itself. Substitute into the
13024 pattern and return a PACK_EXPANSION_*. The caller will need to
13026 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13027 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13028 /*integral_constant_expression_p=*/false);
13030 t
= tsubst (pattern
, args
, complain
, in_decl
);
13031 t
= make_pack_expansion (t
, complain
);
13035 gcc_assert (len
>= 0);
13037 /* For each argument in each argument pack, substitute into the
13039 result
= make_tree_vec (len
);
13040 tree elem_args
= copy_template_args (args
);
13041 for (i
= 0; i
< len
; ++i
)
13043 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13045 elem_args
, complain
,
13047 TREE_VEC_ELT (result
, i
) = t
;
13048 if (t
== error_mark_node
)
13050 result
= error_mark_node
;
13055 /* Update ARGS to restore the substitution from parameter packs to
13056 their argument packs. */
13057 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13059 tree parm
= TREE_PURPOSE (pack
);
13061 if (TREE_CODE (parm
) == PARM_DECL
13063 || TREE_CODE (parm
) == FIELD_DECL
)
13064 register_local_specialization (TREE_TYPE (pack
), parm
);
13069 if (TREE_VALUE (pack
) == NULL_TREE
)
13072 template_parm_level_and_index (parm
, &level
, &idx
);
13074 /* Update the corresponding argument. */
13075 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13076 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13079 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13083 /* If the dependent pack arguments were such that we end up with only a
13084 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13085 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13086 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13087 return TREE_VEC_ELT (result
, 0);
13092 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13093 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13094 parameter packs; all parms generated from a function parameter pack will
13095 have the same DECL_PARM_INDEX. */
13098 get_pattern_parm (tree parm
, tree tmpl
)
13100 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13103 if (DECL_ARTIFICIAL (parm
))
13105 for (patparm
= DECL_ARGUMENTS (pattern
);
13106 patparm
; patparm
= DECL_CHAIN (patparm
))
13107 if (DECL_ARTIFICIAL (patparm
)
13108 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13113 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13114 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13115 gcc_assert (DECL_PARM_INDEX (patparm
)
13116 == DECL_PARM_INDEX (parm
));
13122 /* Make an argument pack out of the TREE_VEC VEC. */
13125 make_argument_pack (tree vec
)
13129 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13130 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13133 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13134 TREE_CONSTANT (pack
) = 1;
13136 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13140 /* Return an exact copy of template args T that can be modified
13144 copy_template_args (tree t
)
13146 if (t
== error_mark_node
)
13149 int len
= TREE_VEC_LENGTH (t
);
13150 tree new_vec
= make_tree_vec (len
);
13152 for (int i
= 0; i
< len
; ++i
)
13154 tree elt
= TREE_VEC_ELT (t
, i
);
13155 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13156 elt
= copy_template_args (elt
);
13157 TREE_VEC_ELT (new_vec
, i
) = elt
;
13160 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13161 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13166 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13169 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13172 /* Substitute into each of the arguments. */
13173 tree new_arg
= TYPE_P (orig_arg
)
13174 ? cxx_make_type (TREE_CODE (orig_arg
))
13175 : make_node (TREE_CODE (orig_arg
));
13177 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13178 args
, complain
, in_decl
);
13179 if (pack_args
== error_mark_node
)
13180 new_arg
= error_mark_node
;
13182 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13184 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13185 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13190 /* Substitute ARGS into the vector or list of template arguments T. */
13193 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13196 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13199 if (t
== error_mark_node
)
13200 return error_mark_node
;
13202 len
= TREE_VEC_LENGTH (t
);
13203 elts
= XALLOCAVEC (tree
, len
);
13205 for (i
= 0; i
< len
; i
++)
13207 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13210 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13211 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13212 else if (PACK_EXPANSION_P (orig_arg
))
13214 /* Substitute into an expansion expression. */
13215 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13217 if (TREE_CODE (new_arg
) == TREE_VEC
)
13218 /* Add to the expanded length adjustment the number of
13219 expanded arguments. We subtract one from this
13220 measurement, because the argument pack expression
13221 itself is already counted as 1 in
13222 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13223 the argument pack is empty. */
13224 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13226 else if (ARGUMENT_PACK_P (orig_arg
))
13227 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13229 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13231 if (new_arg
== error_mark_node
)
13232 return error_mark_node
;
13235 if (new_arg
!= orig_arg
)
13242 /* Make space for the expanded arguments coming from template
13244 t
= make_tree_vec (len
+ expanded_len_adjust
);
13245 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13246 arguments for a member template.
13247 In that case each TREE_VEC in ORIG_T represents a level of template
13248 arguments, and ORIG_T won't carry any non defaulted argument count.
13249 It will rather be the nested TREE_VECs that will carry one.
13250 In other words, ORIG_T carries a non defaulted argument count only
13251 if it doesn't contain any nested TREE_VEC. */
13252 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13254 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13255 count
+= expanded_len_adjust
;
13256 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13258 for (i
= 0, out
= 0; i
< len
; i
++)
13260 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13261 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13262 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13266 /* Now expand the template argument pack "in place". */
13267 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13268 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13272 TREE_VEC_ELT (t
, out
) = elts
[i
];
13280 /* Substitute ARGS into one level PARMS of template parameters. */
13283 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13285 if (parms
== error_mark_node
)
13286 return error_mark_node
;
13288 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13290 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13292 tree tuple
= TREE_VEC_ELT (parms
, i
);
13294 if (tuple
== error_mark_node
)
13297 TREE_VEC_ELT (new_vec
, i
) =
13298 tsubst_template_parm (tuple
, args
, complain
);
13304 /* Return the result of substituting ARGS into the template parameters
13305 given by PARMS. If there are m levels of ARGS and m + n levels of
13306 PARMS, then the result will contain n levels of PARMS. For
13307 example, if PARMS is `template <class T> template <class U>
13308 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13309 result will be `template <int*, double, class V>'. */
13312 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13314 tree r
= NULL_TREE
;
13317 /* When substituting into a template, we must set
13318 PROCESSING_TEMPLATE_DECL as the template parameters may be
13319 dependent if they are based on one-another, and the dependency
13320 predicates are short-circuit outside of templates. */
13321 ++processing_template_decl
;
13323 for (new_parms
= &r
;
13324 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13325 new_parms
= &(TREE_CHAIN (*new_parms
)),
13326 parms
= TREE_CHAIN (parms
))
13328 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13331 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13332 - TMPL_ARGS_DEPTH (args
)),
13333 new_vec
, NULL_TREE
);
13334 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13335 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13338 --processing_template_decl
;
13343 /* Return the result of substituting ARGS into one template parameter
13344 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13345 parameter and which TREE_PURPOSE is the default argument of the
13346 template parameter. */
13349 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13351 tree default_value
, parm_decl
;
13353 if (args
== NULL_TREE
13355 || t
== error_mark_node
)
13358 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13360 default_value
= TREE_PURPOSE (t
);
13361 parm_decl
= TREE_VALUE (t
);
13362 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13364 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13365 if (TREE_CODE (parm_decl
) == PARM_DECL
13366 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13367 parm_decl
= error_mark_node
;
13368 default_value
= tsubst_template_arg (default_value
, args
,
13369 complain
, NULL_TREE
);
13370 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13372 tree r
= build_tree_list (default_value
, parm_decl
);
13373 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13377 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13378 type T. If T is not an aggregate or enumeration type, it is
13379 handled as if by tsubst. IN_DECL is as for tsubst. If
13380 ENTERING_SCOPE is nonzero, T is the context for a template which
13381 we are presently tsubst'ing. Return the substituted value. */
13384 tsubst_aggr_type (tree t
,
13386 tsubst_flags_t complain
,
13388 int entering_scope
)
13390 if (t
== NULL_TREE
)
13393 switch (TREE_CODE (t
))
13396 if (TYPE_PTRMEMFUNC_P (t
))
13397 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13399 /* Fall through. */
13400 case ENUMERAL_TYPE
:
13402 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13408 /* In "sizeof(X<I>)" we need to evaluate "I". */
13411 /* First, determine the context for the type we are looking
13413 context
= TYPE_CONTEXT (t
);
13414 if (context
&& TYPE_P (context
))
13416 context
= tsubst_aggr_type (context
, args
, complain
,
13417 in_decl
, /*entering_scope=*/1);
13418 /* If context is a nested class inside a class template,
13419 it may still need to be instantiated (c++/33959). */
13420 context
= complete_type (context
);
13423 /* Then, figure out what arguments are appropriate for the
13424 type we are trying to find. For example, given:
13426 template <class T> struct S;
13427 template <class T, class U> void f(T, U) { S<U> su; }
13429 and supposing that we are instantiating f<int, double>,
13430 then our ARGS will be {int, double}, but, when looking up
13431 S we only want {double}. */
13432 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13433 complain
, in_decl
);
13434 if (argvec
== error_mark_node
)
13435 r
= error_mark_node
;
13436 else if (cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13438 /* See maybe_dependent_member_ref. */
13439 tree name
= TYPE_IDENTIFIER (t
);
13440 tree fullname
= name
;
13441 if (instantiates_primary_template_p (t
))
13442 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13443 INNERMOST_TEMPLATE_ARGS (argvec
));
13444 return build_typename_type (context
, name
, fullname
,
13449 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13450 entering_scope
, complain
);
13451 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13457 /* This is not a template type, so there's nothing to do. */
13461 return tsubst (t
, args
, complain
, in_decl
);
13465 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13467 /* Substitute into the default argument ARG (a default argument for
13468 FN), which has the indicated TYPE. */
13471 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13472 tsubst_flags_t complain
)
13474 int errs
= errorcount
+ sorrycount
;
13476 /* This can happen in invalid code. */
13477 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13481 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13482 && CONSTRUCTOR_NELTS (arg
) == 0)
13485 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13486 parm
= chain_index (parmnum
, parm
);
13487 tree parmtype
= TREE_TYPE (parm
);
13488 if (DECL_BY_REFERENCE (parm
))
13489 parmtype
= TREE_TYPE (parmtype
);
13490 if (parmtype
== error_mark_node
)
13491 return error_mark_node
;
13493 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13496 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13499 /* This default argument came from a template. Instantiate the
13500 default argument here, not in tsubst. In the case of
13509 we must be careful to do name lookup in the scope of S<T>,
13510 rather than in the current class. */
13511 push_to_top_level ();
13512 push_access_scope (fn
);
13513 push_deferring_access_checks (dk_no_deferred
);
13514 start_lambda_scope (parm
);
13516 /* The default argument expression may cause implicitly defined
13517 member functions to be synthesized, which will result in garbage
13518 collection. We must treat this situation as if we were within
13519 the body of function so as to avoid collecting live data on the
13522 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13523 complain
, NULL_TREE
,
13524 /*integral_constant_expression_p=*/false);
13527 finish_lambda_scope ();
13529 /* Make sure the default argument is reasonable. */
13530 arg
= check_default_argument (type
, arg
, complain
);
13532 if (errorcount
+sorrycount
> errs
13533 && (complain
& tf_warning_or_error
))
13534 inform (input_location
,
13535 " when instantiating default argument for call to %qD", fn
);
13537 pop_deferring_access_checks ();
13538 pop_access_scope (fn
);
13539 pop_from_top_level ();
13541 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13544 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13545 defarg_inst
->put (parm
, arg
);
13551 /* Substitute into all the default arguments for FN. */
13554 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13559 tmpl_args
= DECL_TI_ARGS (fn
);
13561 /* If this function is not yet instantiated, we certainly don't need
13562 its default arguments. */
13563 if (uses_template_parms (tmpl_args
))
13565 /* Don't do this again for clones. */
13566 if (DECL_CLONED_FUNCTION_P (fn
))
13570 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13572 arg
= TREE_CHAIN (arg
), ++i
)
13573 if (TREE_PURPOSE (arg
))
13574 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13576 TREE_PURPOSE (arg
),
13580 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13581 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13583 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13586 store_explicit_specifier (tree v
, tree t
)
13588 if (!explicit_specifier_map
)
13589 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13590 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13591 explicit_specifier_map
->put (v
, t
);
13594 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13597 lookup_explicit_specifier (tree v
)
13599 return *explicit_specifier_map
->get (v
);
13602 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13603 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13604 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13608 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13609 tree raises
, tsubst_flags_t complain
)
13611 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13614 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13616 new_type
= build_function_type (return_type
, arg_types
);
13617 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13621 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13622 /* Don't pick up extra function qualifiers from the basetype. */
13623 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13624 if (! MAYBE_CLASS_TYPE_P (r
))
13628 Type deduction may fail for any of the following
13631 -- Attempting to create "pointer to member of T" when T
13632 is not a class type. */
13633 if (complain
& tf_error
)
13634 error ("creating pointer to member function of non-class type %qT",
13636 return error_mark_node
;
13639 new_type
= build_method_type_directly (r
, return_type
,
13640 TREE_CHAIN (arg_types
));
13642 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13644 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13645 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13646 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13649 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13650 each of its formal parameters. If there is a disagreement then rebuild
13651 DECL's function type according to its formal parameter types, as part of a
13652 resolution for Core issues 1001/1322. */
13655 maybe_rebuild_function_decl_type (tree decl
)
13657 bool function_type_needs_rebuilding
= false;
13658 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13660 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13661 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13663 tree parm_type
= TREE_VALUE (parm_type_list
);
13664 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13665 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13667 function_type_needs_rebuilding
= true;
13671 parm_list
= DECL_CHAIN (parm_list
);
13672 parm_type_list
= TREE_CHAIN (parm_type_list
);
13676 if (!function_type_needs_rebuilding
)
13679 const tree fntype
= TREE_TYPE (decl
);
13680 tree parm_list
= DECL_ARGUMENTS (decl
);
13681 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13682 tree new_parm_type_list
= NULL_TREE
;
13683 tree
*q
= &new_parm_type_list
;
13684 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13686 *q
= copy_node (old_parm_type_list
);
13687 parm_list
= DECL_CHAIN (parm_list
);
13688 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13689 q
= &TREE_CHAIN (*q
);
13691 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13693 *q
= copy_node (old_parm_type_list
);
13694 tree
*new_parm_type
= &TREE_VALUE (*q
);
13695 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13696 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13697 *new_parm_type
= formal_parm_type_unqual
;
13699 parm_list
= DECL_CHAIN (parm_list
);
13700 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13701 q
= &TREE_CHAIN (*q
);
13703 if (old_parm_type_list
== void_list_node
)
13704 *q
= void_list_node
;
13707 = rebuild_function_or_method_type (fntype
,
13708 TREE_TYPE (fntype
), new_parm_type_list
,
13709 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13712 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13715 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13716 tree lambda_fntype
)
13718 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
13719 hashval_t hash
= 0;
13722 /* Nobody should be tsubst'ing into non-template functions. */
13723 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
13724 || DECL_LOCAL_DECL_P (t
));
13726 if (DECL_LOCAL_DECL_P (t
))
13728 if (tree spec
= retrieve_local_specialization (t
))
13731 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13733 /* If T is not dependent, just return it. */
13734 if (!uses_template_parms (DECL_TI_ARGS (t
))
13735 && !LAMBDA_FUNCTION_P (t
))
13738 /* Calculate the most general template of which R is a
13740 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13742 /* We're substituting a lambda function under tsubst_lambda_expr but not
13743 directly from it; find the matching function we're already inside.
13744 But don't do this if T is a generic lambda with a single level of
13745 template parms, as in that case we're doing a normal instantiation. */
13746 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13747 && (!generic_lambda_fn_p (t
)
13748 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13749 return enclosing_instantiation_of (t
);
13751 /* Calculate the complete set of arguments used to
13753 argvec
= tsubst_template_args (DECL_TI_ARGS
13754 (DECL_TEMPLATE_RESULT
13755 (DECL_TI_TEMPLATE (t
))),
13756 args
, complain
, in_decl
);
13757 if (argvec
== error_mark_node
)
13758 return error_mark_node
;
13760 /* Check to see if we already have this specialization. */
13761 if (!lambda_fntype
)
13763 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13764 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13768 /* We can see more levels of arguments than parameters if
13769 there was a specialization of a member template, like
13772 template <class T> struct S { template <class U> void f(); }
13773 template <> template <class U> void S<int>::f(U);
13775 Here, we'll be substituting into the specialization,
13776 because that's where we can find the code we actually
13777 want to generate, but we'll have enough arguments for
13778 the most general template.
13780 We also deal with the peculiar case:
13782 template <class T> struct S {
13783 template <class U> friend void f();
13785 template <class U> void f() {}
13787 template void f<double>();
13789 Here, the ARGS for the instantiation of will be {int,
13790 double}. But, we only need as many ARGS as there are
13791 levels of template parameters in CODE_PATTERN. We are
13792 careful not to get fooled into reducing the ARGS in
13795 template <class T> struct S { template <class U> void f(U); }
13796 template <class T> template <> void S<T>::f(int) {}
13798 which we can spot because the pattern will be a
13799 specialization in this case. */
13800 int args_depth
= TMPL_ARGS_DEPTH (args
);
13802 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13804 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13805 args
= get_innermost_template_args (args
, parms_depth
);
13809 /* This special case arises when we have something like this:
13811 template <class T> struct S {
13812 friend void f<int>(int, double);
13815 Here, the DECL_TI_TEMPLATE for the friend declaration
13816 will be an IDENTIFIER_NODE. We are being called from
13817 tsubst_friend_function, and we want only to create a
13818 new decl (R) with appropriate types so that we can call
13819 determine_specialization. */
13820 gen_tmpl
= NULL_TREE
;
13821 argvec
= NULL_TREE
;
13824 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13826 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13827 bool member
= ctx
&& TYPE_P (ctx
);
13829 if (member
&& !closure
)
13830 ctx
= tsubst_aggr_type (ctx
, args
,
13831 complain
, t
, /*entering_scope=*/1);
13833 tree type
= (lambda_fntype
? lambda_fntype
13834 : tsubst (TREE_TYPE (t
), args
,
13835 complain
| tf_fndecl_type
, in_decl
));
13836 if (type
== error_mark_node
)
13837 return error_mark_node
;
13839 /* If we hit excessive deduction depth, the type is bogus even if
13840 it isn't error_mark_node, so don't build a decl. */
13841 if (excessive_deduction_depth
)
13842 return error_mark_node
;
13844 /* We do NOT check for matching decls pushed separately at this
13845 point, as they may not represent instantiations of this
13846 template, and in any case are considered separate under the
13848 tree r
= copy_decl (t
);
13849 DECL_USE_TEMPLATE (r
) = 0;
13850 TREE_TYPE (r
) = type
;
13851 /* Clear out the mangled name and RTL for the instantiation. */
13852 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13853 SET_DECL_RTL (r
, NULL
);
13854 /* Leave DECL_INITIAL set on deleted instantiations. */
13855 if (!DECL_DELETED_FN (r
))
13856 DECL_INITIAL (r
) = NULL_TREE
;
13857 DECL_CONTEXT (r
) = ctx
;
13859 /* Handle explicit(dependent-expr). */
13860 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13862 tree spec
= lookup_explicit_specifier (t
);
13863 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13864 /*function_p=*/false,
13866 spec
= build_explicit_specifier (spec
, complain
);
13867 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13870 /* OpenMP UDRs have the only argument a reference to the declared
13871 type. We want to diagnose if the declared type is a reference,
13872 which is invalid, but as references to references are usually
13873 quietly merged, diagnose it here. */
13874 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13877 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13878 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13879 if (TYPE_REF_P (argtype
))
13880 error_at (DECL_SOURCE_LOCATION (t
),
13881 "reference type %qT in "
13882 "%<#pragma omp declare reduction%>", argtype
);
13883 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13884 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13888 if (member
&& DECL_CONV_FN_P (r
))
13889 /* Type-conversion operator. Reconstruct the name, in
13890 case it's the name of one of the template's parameters. */
13891 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13893 tree parms
= DECL_ARGUMENTS (t
);
13895 parms
= DECL_CHAIN (parms
);
13896 parms
= tsubst (parms
, args
, complain
, t
);
13897 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13898 DECL_CONTEXT (parm
) = r
;
13901 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13902 DECL_NAME (tparm
) = closure_identifier
;
13903 DECL_CHAIN (tparm
) = parms
;
13906 DECL_ARGUMENTS (r
) = parms
;
13907 DECL_RESULT (r
) = NULL_TREE
;
13909 maybe_rebuild_function_decl_type (r
);
13911 TREE_STATIC (r
) = 0;
13912 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13913 DECL_EXTERNAL (r
) = 1;
13914 /* If this is an instantiation of a function with internal
13915 linkage, we already know what object file linkage will be
13916 assigned to the instantiation. */
13917 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13918 DECL_DEFER_OUTPUT (r
) = 0;
13919 DECL_CHAIN (r
) = NULL_TREE
;
13920 DECL_PENDING_INLINE_INFO (r
) = 0;
13921 DECL_PENDING_INLINE_P (r
) = 0;
13922 DECL_SAVED_TREE (r
) = NULL_TREE
;
13923 DECL_STRUCT_FUNCTION (r
) = NULL
;
13925 /* We'll re-clone as appropriate in instantiate_template. */
13926 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13928 /* If we aren't complaining now, return on error before we register
13929 the specialization so that we'll complain eventually. */
13930 if ((complain
& tf_error
) == 0
13931 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13932 && !grok_op_properties (r
, /*complain=*/false))
13933 return error_mark_node
;
13935 /* Associate the constraints directly with the instantiation. We
13936 don't substitute through the constraints; that's only done when
13937 they are checked. */
13938 if (tree ci
= get_constraints (t
))
13939 /* Unless we're regenerating a lambda, in which case we'll set the
13940 lambda's constraints in tsubst_lambda_expr. */
13941 if (!lambda_fntype
)
13942 set_constraints (r
, ci
);
13944 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13945 SET_DECL_FRIEND_CONTEXT (r
,
13946 tsubst (DECL_FRIEND_CONTEXT (t
),
13947 args
, complain
, in_decl
));
13949 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13950 this in the special friend case mentioned above where
13951 GEN_TMPL is NULL. */
13952 if (gen_tmpl
&& !closure
)
13954 DECL_TEMPLATE_INFO (r
)
13955 = build_template_info (gen_tmpl
, argvec
);
13956 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13959 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13961 /* We instantiated this while substituting into
13962 the type earlier (template/friend54.C). */
13965 /* We're not supposed to instantiate default arguments
13966 until they are called, for a template. But, for a
13969 template <class T> void f ()
13970 { extern void g(int i = T()); }
13972 we should do the substitution when the template is
13973 instantiated. We handle the member function case in
13974 instantiate_class_template since the default arguments
13975 might refer to other members of the class. */
13977 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13978 && !uses_template_parms (argvec
))
13979 tsubst_default_arguments (r
, complain
);
13981 else if (DECL_LOCAL_DECL_P (r
))
13983 if (!cp_unevaluated_operand
)
13984 register_local_specialization (r
, t
);
13987 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13989 /* Copy the list of befriending classes. */
13990 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13992 friends
= &TREE_CHAIN (*friends
))
13994 *friends
= copy_node (*friends
);
13995 TREE_VALUE (*friends
)
13996 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13999 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14001 maybe_retrofit_in_chrg (r
);
14002 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14003 return error_mark_node
;
14004 /* If this is an instantiation of a member template, clone it.
14005 If it isn't, that'll be handled by
14006 clone_constructors_and_destructors. */
14007 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14008 clone_cdtor (r
, /*update_methods=*/false);
14010 else if ((complain
& tf_error
) != 0
14011 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14012 && !grok_op_properties (r
, /*complain=*/true))
14013 return error_mark_node
;
14015 /* Possibly limit visibility based on template args. */
14016 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14017 if (DECL_VISIBILITY_SPECIFIED (t
))
14019 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14020 DECL_ATTRIBUTES (r
)
14021 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14023 determine_visibility (r
);
14024 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14025 && !processing_template_decl
)
14026 defaulted_late_check (r
);
14028 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14029 args
, complain
, in_decl
);
14031 if (tree attr
= lookup_attribute ("omp declare variant base",
14032 DECL_ATTRIBUTES (r
)))
14033 omp_declare_variant_finalize (r
, attr
);
14038 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14041 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14042 tree lambda_fntype
)
14044 /* We can get here when processing a member function template,
14045 member class template, or template template parameter. */
14046 tree decl
= DECL_TEMPLATE_RESULT (t
);
14052 hashval_t hash
= 0;
14054 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14056 /* Template template parameter is treated here. */
14057 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14058 if (new_type
== error_mark_node
)
14059 r
= error_mark_node
;
14060 /* If we get a real template back, return it. This can happen in
14061 the context of most_specialized_partial_spec. */
14062 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14065 /* The new TEMPLATE_DECL was built in
14066 reduce_template_parm_level. */
14067 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14071 if (!lambda_fntype
)
14073 /* We might already have an instance of this template.
14074 The ARGS are for the surrounding class type, so the
14075 full args contain the tsubst'd args for the context,
14076 plus the innermost args from the template decl. */
14077 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14078 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14079 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14080 /* Because this is a template, the arguments will still be
14081 dependent, even after substitution. If
14082 PROCESSING_TEMPLATE_DECL is not set, the dependency
14083 predicates will short-circuit. */
14084 ++processing_template_decl
;
14085 full_args
= tsubst_template_args (tmpl_args
, args
,
14086 complain
, in_decl
);
14087 --processing_template_decl
;
14088 if (full_args
== error_mark_node
)
14089 return error_mark_node
;
14091 /* If this is a default template template argument,
14092 tsubst might not have changed anything. */
14093 if (full_args
== tmpl_args
)
14096 hash
= hash_tmpl_and_args (t
, full_args
);
14097 spec
= retrieve_specialization (t
, full_args
, hash
);
14098 if (spec
!= NULL_TREE
)
14101 /* Type partial instantiations are stored as the type by
14102 lookup_template_class_1, not here as the template. */
14103 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14108 /* Make a new template decl. It will be similar to the
14109 original, but will record the current template arguments.
14110 We also create a new function declaration, which is just
14111 like the old one, but points to this new template, rather
14112 than the old one. */
14114 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14115 DECL_CHAIN (r
) = NULL_TREE
;
14117 // Build new template info linking to the original template decl.
14118 if (!lambda_fntype
)
14120 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14121 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14124 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14126 /* The template parameters for this new template are all the
14127 template parameters for the old template, except the
14128 outermost level of parameters. */
14129 DECL_TEMPLATE_PARMS (r
)
14130 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14133 bool class_p
= false;
14135 ++processing_template_decl
;
14136 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14137 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14140 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14143 inner
= TREE_TYPE (inner
);
14145 inner
= tsubst (inner
, args
, complain
, in_decl
);
14147 --processing_template_decl
;
14148 if (inner
== error_mark_node
)
14149 return error_mark_node
;
14153 /* For a partial specialization, we need to keep pointing to
14154 the primary template. */
14155 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14156 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14158 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14159 inner
= TYPE_MAIN_DECL (inner
);
14161 else if (lambda_fntype
)
14163 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14164 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14168 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14169 DECL_TI_TEMPLATE (inner
) = r
;
14170 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14173 DECL_TEMPLATE_RESULT (r
) = inner
;
14174 TREE_TYPE (r
) = TREE_TYPE (inner
);
14175 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14177 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14178 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14180 if (PRIMARY_TEMPLATE_P (t
))
14181 DECL_PRIMARY_TEMPLATE (r
) = r
;
14183 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14185 /* Record this non-type partial instantiation. */
14186 register_specialization (r
, t
,
14187 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14193 /* True if FN is the op() for a lambda in an uninstantiated template. */
14196 lambda_fn_in_template_p (tree fn
)
14198 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14200 tree closure
= DECL_CONTEXT (fn
);
14201 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14204 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14205 which the above is true. */
14208 instantiated_lambda_fn_p (tree fn
)
14210 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14212 tree closure
= DECL_CONTEXT (fn
);
14213 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14214 return LAMBDA_EXPR_INSTANTIATED (lam
);
14217 /* We're instantiating a variable from template function TCTX. Return the
14218 corresponding current enclosing scope. This gets complicated because lambda
14219 functions in templates are regenerated rather than instantiated, but generic
14220 lambda functions are subsequently instantiated. */
14223 enclosing_instantiation_of (tree otctx
)
14226 tree fn
= current_function_decl
;
14227 int lambda_count
= 0;
14229 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14230 || instantiated_lambda_fn_p (tctx
));
14231 tctx
= decl_function_context (tctx
))
14233 for (; fn
; fn
= decl_function_context (fn
))
14236 int flambda_count
= 0;
14237 for (; fn
&& instantiated_lambda_fn_p (fn
);
14238 fn
= decl_function_context (fn
))
14240 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14241 ? most_general_template (fn
) != most_general_template (tctx
)
14244 if (flambda_count
!= lambda_count
)
14246 gcc_assert (flambda_count
> lambda_count
);
14247 for (; flambda_count
> lambda_count
; --flambda_count
)
14248 ofn
= decl_function_context (ofn
);
14250 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14251 || DECL_CONV_FN_P (ofn
));
14254 gcc_unreachable ();
14257 /* Substitute the ARGS into the T, which is a _DECL. Return the
14258 result of the substitution. Issue error and warning messages under
14259 control of COMPLAIN. */
14262 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14264 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14265 location_t saved_loc
;
14266 tree r
= NULL_TREE
;
14268 hashval_t hash
= 0;
14270 /* Set the filename and linenumber to improve error-reporting. */
14271 saved_loc
= input_location
;
14272 input_location
= DECL_SOURCE_LOCATION (t
);
14274 switch (TREE_CODE (t
))
14276 case TEMPLATE_DECL
:
14277 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14280 case FUNCTION_DECL
:
14281 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14286 tree type
= NULL_TREE
;
14288 tree expanded_types
= NULL_TREE
;
14289 tree prev_r
= NULL_TREE
;
14290 tree first_r
= NULL_TREE
;
14292 if (DECL_PACK_P (t
))
14294 /* If there is a local specialization that isn't a
14295 parameter pack, it means that we're doing a "simple"
14296 substitution from inside tsubst_pack_expansion. Just
14297 return the local specialization (which will be a single
14299 tree spec
= retrieve_local_specialization (t
);
14301 && TREE_CODE (spec
) == PARM_DECL
14302 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14305 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14306 the parameters in this function parameter pack. */
14307 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14308 complain
, in_decl
);
14309 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14311 len
= TREE_VEC_LENGTH (expanded_types
);
14313 /* Zero-length parameter packs are boring. Just substitute
14315 if (len
== 0 && !cp_unevaluated_operand
)
14316 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14321 /* All we did was update the type. Make a note of that. */
14322 type
= expanded_types
;
14323 expanded_types
= NULL_TREE
;
14327 /* Loop through all of the parameters we'll build. When T is
14328 a function parameter pack, LEN is the number of expanded
14329 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14331 for (i
= 0; i
< len
; ++i
)
14335 if (DECL_TEMPLATE_PARM_P (t
))
14336 SET_DECL_TEMPLATE_PARM_P (r
);
14338 if (expanded_types
)
14339 /* We're on the Ith parameter of the function parameter
14342 /* Get the Ith type. */
14343 type
= TREE_VEC_ELT (expanded_types
, i
);
14345 /* Rename the parameter to include the index. */
14347 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14350 /* We're dealing with a normal parameter. */
14351 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14353 type
= type_decays_to (type
);
14354 TREE_TYPE (r
) = type
;
14355 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14357 if (DECL_INITIAL (r
))
14359 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14360 DECL_INITIAL (r
) = TREE_TYPE (r
);
14362 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14363 complain
, in_decl
);
14366 DECL_CONTEXT (r
) = NULL_TREE
;
14368 if (!DECL_TEMPLATE_PARM_P (r
))
14369 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14371 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14372 args
, complain
, in_decl
);
14374 /* Keep track of the first new parameter we
14375 generate. That's what will be returned to the
14380 /* Build a proper chain of parameters when substituting
14381 into a function parameter pack. */
14383 DECL_CHAIN (prev_r
) = r
;
14386 /* If cp_unevaluated_operand is set, we're just looking for a
14387 single dummy parameter, so don't keep going. */
14388 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14389 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14390 complain
, DECL_CHAIN (t
));
14392 /* FIRST_R contains the start of the chain we've built. */
14399 tree type
= NULL_TREE
;
14400 tree vec
= NULL_TREE
;
14401 tree expanded_types
= NULL_TREE
;
14404 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14406 /* This field is a lambda capture pack. Return a TREE_VEC of
14407 the expanded fields to instantiate_class_template_1. */
14408 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14409 complain
, in_decl
);
14410 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14412 len
= TREE_VEC_LENGTH (expanded_types
);
14413 vec
= make_tree_vec (len
);
14417 /* All we did was update the type. Make a note of that. */
14418 type
= expanded_types
;
14419 expanded_types
= NULL_TREE
;
14423 for (int i
= 0; i
< len
; ++i
)
14426 if (expanded_types
)
14428 type
= TREE_VEC_ELT (expanded_types
, i
);
14430 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14433 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14435 if (type
== error_mark_node
)
14436 RETURN (error_mark_node
);
14437 TREE_TYPE (r
) = type
;
14438 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14440 if (DECL_C_BIT_FIELD (r
))
14441 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14443 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14444 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14446 /*integral_constant_expression_p=*/true);
14447 if (DECL_INITIAL (t
))
14449 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14450 NSDMI in perform_member_init. Still set DECL_INITIAL
14451 so that we know there is one. */
14452 DECL_INITIAL (r
) = void_node
;
14453 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14454 retrofit_lang_decl (r
);
14455 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14457 /* We don't have to set DECL_CONTEXT here; it is set by
14458 finish_member_declaration. */
14459 DECL_CHAIN (r
) = NULL_TREE
;
14461 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14462 args
, complain
, in_decl
);
14465 TREE_VEC_ELT (vec
, i
) = r
;
14474 /* We reach here only for member using decls. We also need to check
14475 uses_template_parms because DECL_DEPENDENT_P is not set for a
14476 using-declaration that designates a member of the current
14477 instantiation (c++/53549). */
14478 if (DECL_DEPENDENT_P (t
)
14479 || uses_template_parms (USING_DECL_SCOPE (t
)))
14481 tree scope
= USING_DECL_SCOPE (t
);
14482 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14483 if (PACK_EXPANSION_P (scope
))
14485 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14486 int len
= TREE_VEC_LENGTH (vec
);
14487 r
= make_tree_vec (len
);
14488 for (int i
= 0; i
< len
; ++i
)
14490 tree escope
= TREE_VEC_ELT (vec
, i
);
14491 tree elt
= do_class_using_decl (escope
, name
);
14494 r
= error_mark_node
;
14499 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14500 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14502 TREE_VEC_ELT (r
, i
) = elt
;
14507 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14508 complain
, in_decl
);
14509 r
= do_class_using_decl (inst_scope
, name
);
14511 r
= error_mark_node
;
14514 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14515 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14522 DECL_CHAIN (r
) = NULL_TREE
;
14529 tree argvec
= NULL_TREE
;
14530 tree gen_tmpl
= NULL_TREE
;
14531 tree tmpl
= NULL_TREE
;
14532 tree type
= NULL_TREE
;
14534 if (TREE_TYPE (t
) == error_mark_node
)
14535 RETURN (error_mark_node
);
14537 if (TREE_CODE (t
) == TYPE_DECL
14538 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14540 /* If this is the canonical decl, we don't have to
14541 mess with instantiations, and often we can't (for
14542 typename, template type parms and such). Note that
14543 TYPE_NAME is not correct for the above test if
14544 we've copied the type for a typedef. */
14545 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14546 if (type
== error_mark_node
)
14547 RETURN (error_mark_node
);
14548 r
= TYPE_NAME (type
);
14552 /* Check to see if we already have the specialization we
14554 tree spec
= NULL_TREE
;
14555 bool local_p
= false;
14556 tree ctx
= DECL_CONTEXT (t
);
14557 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14558 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14561 if (DECL_CLASS_SCOPE_P (t
))
14563 ctx
= tsubst_aggr_type (ctx
, args
,
14565 in_decl
, /*entering_scope=*/1);
14566 /* If CTX is unchanged, then T is in fact the
14567 specialization we want. That situation occurs when
14568 referencing a static data member within in its own
14569 class. We can use pointer equality, rather than
14570 same_type_p, because DECL_CONTEXT is always
14572 if (ctx
== DECL_CONTEXT (t
)
14573 /* ... unless T is a member template; in which
14574 case our caller can be willing to create a
14575 specialization of that template represented
14577 && !(DECL_TI_TEMPLATE (t
)
14578 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14584 tmpl
= DECL_TI_TEMPLATE (t
);
14585 gen_tmpl
= most_general_template (tmpl
);
14586 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14587 if (argvec
!= error_mark_node
)
14588 argvec
= (coerce_innermost_template_parms
14589 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14590 argvec
, t
, complain
,
14591 /*all*/true, /*defarg*/true));
14592 if (argvec
== error_mark_node
)
14593 RETURN (error_mark_node
);
14594 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14595 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14600 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14601 /* Subsequent calls to pushdecl will fill this in. */
14603 /* A local variable. */
14605 /* Unless this is a reference to a static variable from an
14606 enclosing function, in which case we need to fill it in now. */
14607 if (TREE_STATIC (t
))
14609 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14610 if (fn
!= current_function_decl
)
14613 spec
= retrieve_local_specialization (t
);
14615 /* If we already have the specialization we need, there is
14616 nothing more to do. */
14623 /* Create a new node for the specialization we need. */
14624 if (type
== NULL_TREE
)
14626 if (is_typedef_decl (t
))
14627 type
= DECL_ORIGINAL_TYPE (t
);
14629 type
= TREE_TYPE (t
);
14631 && VAR_HAD_UNKNOWN_BOUND (t
)
14632 && type
!= error_mark_node
)
14633 type
= strip_array_domain (type
);
14634 tree sub_args
= args
;
14635 if (tree auto_node
= type_uses_auto (type
))
14637 /* Mask off any template args past the variable's context so we
14638 don't replace the auto with an unrelated argument. */
14639 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14640 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14642 /* This should never happen with the new lambda instantiation
14643 model, but keep the handling just in case. */
14644 gcc_assert (!CHECKING_P
),
14645 sub_args
= strip_innermost_template_args (args
, extra
);
14647 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14648 /* Substituting the type might have recursively instantiated this
14649 same alias (c++/86171). */
14650 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14651 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14660 DECL_INITIALIZED_P (r
) = 0;
14661 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14662 if (type
== error_mark_node
)
14663 RETURN (error_mark_node
);
14664 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14666 /* It may seem that this case cannot occur, since:
14671 declares a function, not a variable. However:
14674 template <typename T> void g() { T t; }
14675 template void g<f>();
14677 is an attempt to declare a variable with function
14679 error ("variable %qD has function type",
14680 /* R is not yet sufficiently initialized, so we
14681 just use its name. */
14683 RETURN (error_mark_node
);
14685 type
= complete_type (type
);
14686 /* Wait until cp_finish_decl to set this again, to handle
14687 circular dependency (template/instantiate6.C). */
14688 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14689 type
= check_var_type (DECL_NAME (r
), type
,
14690 DECL_SOURCE_LOCATION (r
));
14691 if (DECL_HAS_VALUE_EXPR_P (t
))
14693 tree ve
= DECL_VALUE_EXPR (t
);
14694 /* If the DECL_VALUE_EXPR is converted to the declared type,
14695 preserve the identity so that gimplify_type_sizes works. */
14696 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14698 ve
= TREE_OPERAND (ve
, 0);
14699 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14700 /*constant_expression_p=*/false);
14701 if (REFERENCE_REF_P (ve
))
14703 gcc_assert (TYPE_REF_P (type
));
14704 ve
= TREE_OPERAND (ve
, 0);
14707 ve
= build_nop (type
, ve
);
14708 else if (DECL_LANG_SPECIFIC (t
)
14709 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14710 && TREE_CODE (ve
) == COMPONENT_REF
14711 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14712 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14713 type
= TREE_TYPE (ve
);
14715 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14716 == TYPE_MAIN_VARIANT (type
));
14717 SET_DECL_VALUE_EXPR (r
, ve
);
14719 if (CP_DECL_THREAD_LOCAL_P (r
)
14720 && !processing_template_decl
)
14721 set_decl_tls_model (r
, decl_default_tls_model (r
));
14723 else if (DECL_SELF_REFERENCE_P (t
))
14724 SET_DECL_SELF_REFERENCE_P (r
);
14725 TREE_TYPE (r
) = type
;
14726 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14727 DECL_CONTEXT (r
) = ctx
;
14728 /* Clear out the mangled name and RTL for the instantiation. */
14729 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14730 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14731 SET_DECL_RTL (r
, NULL
);
14732 /* The initializer must not be expanded until it is required;
14733 see [temp.inst]. */
14734 DECL_INITIAL (r
) = NULL_TREE
;
14735 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14738 if (DECL_LANG_SPECIFIC (r
))
14739 SET_DECL_DEPENDENT_INIT_P (r
, false);
14741 SET_DECL_MODE (r
, VOIDmode
);
14743 /* Possibly limit visibility based on template args. */
14744 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14745 if (DECL_VISIBILITY_SPECIFIED (t
))
14747 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14748 DECL_ATTRIBUTES (r
)
14749 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14751 determine_visibility (r
);
14756 /* A static data member declaration is always marked
14757 external when it is declared in-class, even if an
14758 initializer is present. We mimic the non-template
14759 processing here. */
14760 DECL_EXTERNAL (r
) = 1;
14761 if (DECL_NAMESPACE_SCOPE_P (t
))
14762 DECL_NOT_REALLY_EXTERN (r
) = 1;
14764 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14765 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14766 if (!error_operand_p (r
) || (complain
& tf_error
))
14767 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14771 if (DECL_LANG_SPECIFIC (r
))
14772 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14773 if (!cp_unevaluated_operand
)
14774 register_local_specialization (r
, t
);
14777 DECL_CHAIN (r
) = NULL_TREE
;
14779 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14781 args
, complain
, in_decl
);
14783 /* Preserve a typedef that names a type. */
14784 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14786 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14787 set_underlying_type (r
);
14788 if (TYPE_DECL_ALIAS_P (r
))
14789 /* An alias template specialization can be dependent
14790 even if its underlying type is not. */
14791 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14794 layout_decl (r
, 0);
14799 gcc_unreachable ();
14804 /* Restore the file and line information. */
14805 input_location
= saved_loc
;
14810 /* Substitute into the complete parameter type list PARMS. */
14813 tsubst_function_parms (tree parms
,
14815 tsubst_flags_t complain
,
14818 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14821 /* Substitute into the ARG_TYPES of a function type.
14822 If END is a TREE_CHAIN, leave it and any following types
14826 tsubst_arg_types (tree arg_types
,
14829 tsubst_flags_t complain
,
14832 tree remaining_arg_types
;
14833 tree type
= NULL_TREE
;
14835 tree expanded_args
= NULL_TREE
;
14838 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14841 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14842 args
, end
, complain
, in_decl
);
14843 if (remaining_arg_types
== error_mark_node
)
14844 return error_mark_node
;
14846 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14848 /* For a pack expansion, perform substitution on the
14849 entire expression. Later on, we'll handle the arguments
14851 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14852 args
, complain
, in_decl
);
14854 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14855 /* So that we'll spin through the parameters, one by one. */
14856 i
= TREE_VEC_LENGTH (expanded_args
);
14859 /* We only partially substituted into the parameter
14860 pack. Our type is TYPE_PACK_EXPANSION. */
14861 type
= expanded_args
;
14862 expanded_args
= NULL_TREE
;
14870 type
= TREE_VEC_ELT (expanded_args
, i
);
14872 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14874 if (type
== error_mark_node
)
14875 return error_mark_node
;
14876 if (VOID_TYPE_P (type
))
14878 if (complain
& tf_error
)
14880 error ("invalid parameter type %qT", type
);
14882 error ("in declaration %q+D", in_decl
);
14884 return error_mark_node
;
14887 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14888 return error_mark_node
;
14890 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14891 top-level qualifiers as required. */
14892 type
= cv_unqualified (type_decays_to (type
));
14894 /* We do not substitute into default arguments here. The standard
14895 mandates that they be instantiated only when needed, which is
14896 done in build_over_call. */
14897 default_arg
= TREE_PURPOSE (arg_types
);
14899 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14900 since the new op() won't have any associated template arguments for us
14901 to refer to later. */
14902 if (lambda_fn_in_template_p (in_decl
))
14903 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14904 false/*fn*/, false/*constexpr*/);
14906 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14908 /* We've instantiated a template before its default arguments
14909 have been parsed. This can happen for a nested template
14910 class, and is not an error unless we require the default
14911 argument in a call of this function. */
14912 remaining_arg_types
=
14913 tree_cons (default_arg
, type
, remaining_arg_types
);
14914 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14915 remaining_arg_types
);
14918 remaining_arg_types
=
14919 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14922 return remaining_arg_types
;
14925 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14926 *not* handle the exception-specification for FNTYPE, because the
14927 initial substitution of explicitly provided template parameters
14928 during argument deduction forbids substitution into the
14929 exception-specification:
14933 All references in the function type of the function template to the
14934 corresponding template parameters are replaced by the specified tem-
14935 plate argument values. If a substitution in a template parameter or
14936 in the function type of the function template results in an invalid
14937 type, type deduction fails. [Note: The equivalent substitution in
14938 exception specifications is done only when the function is instanti-
14939 ated, at which point a program is ill-formed if the substitution
14940 results in an invalid type.] */
14943 tsubst_function_type (tree t
,
14945 tsubst_flags_t complain
,
14949 tree arg_types
= NULL_TREE
;
14951 /* The TYPE_CONTEXT is not used for function/method types. */
14952 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14954 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14956 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14958 if (late_return_type_p
)
14960 /* Substitute the argument types. */
14961 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14962 complain
, in_decl
);
14963 if (arg_types
== error_mark_node
)
14964 return error_mark_node
;
14966 tree save_ccp
= current_class_ptr
;
14967 tree save_ccr
= current_class_ref
;
14968 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14969 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14970 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14973 /* DR 1207: 'this' is in scope in the trailing return type. */
14974 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14977 /* Substitute the return type. */
14978 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14982 current_class_ptr
= save_ccp
;
14983 current_class_ref
= save_ccr
;
14987 /* Substitute the return type. */
14988 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14990 if (return_type
== error_mark_node
)
14991 return error_mark_node
;
14992 /* DR 486 clarifies that creation of a function type with an
14993 invalid return type is a deduction failure. */
14994 if (TREE_CODE (return_type
) == ARRAY_TYPE
14995 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14997 if (complain
& tf_error
)
14999 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15000 error ("function returning an array");
15002 error ("function returning a function");
15004 return error_mark_node
;
15007 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
15008 return error_mark_node
;
15010 if (!late_return_type_p
)
15012 /* Substitute the argument types. */
15013 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15014 complain
, in_decl
);
15015 if (arg_types
== error_mark_node
)
15016 return error_mark_node
;
15019 /* Construct a new type node and return it. */
15020 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15021 /*raises=*/NULL_TREE
, complain
);
15024 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15025 ARGS into that specification, and return the substituted
15026 specification. If there is no specification, return NULL_TREE. */
15029 tsubst_exception_specification (tree fntype
,
15031 tsubst_flags_t complain
,
15038 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15039 new_specs
= NULL_TREE
;
15040 if (specs
&& TREE_PURPOSE (specs
))
15042 /* A noexcept-specifier. */
15043 tree expr
= TREE_PURPOSE (specs
);
15044 if (TREE_CODE (expr
) == INTEGER_CST
)
15048 /* Defer instantiation of noexcept-specifiers to avoid
15049 excessive instantiations (c++/49107). */
15050 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15051 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15053 /* We already partially instantiated this member template,
15054 so combine the new args with the old. */
15055 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15056 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15057 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15058 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15062 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15063 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15068 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15070 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15072 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15074 new_specs
= tsubst_copy_and_build
15075 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15076 /*integral_constant_expression_p=*/true);
15078 new_specs
= build_noexcept_spec (new_specs
, complain
);
15082 if (! TREE_VALUE (specs
))
15089 tree expanded_specs
= NULL_TREE
;
15091 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15093 /* Expand the pack expansion type. */
15094 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15098 if (expanded_specs
== error_mark_node
)
15099 return error_mark_node
;
15100 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15101 len
= TREE_VEC_LENGTH (expanded_specs
);
15104 /* We're substituting into a member template, so
15105 we got a TYPE_PACK_EXPANSION back. Add that
15106 expansion and move on. */
15107 gcc_assert (TREE_CODE (expanded_specs
)
15108 == TYPE_PACK_EXPANSION
);
15109 new_specs
= add_exception_specifier (new_specs
,
15112 specs
= TREE_CHAIN (specs
);
15117 for (i
= 0; i
< len
; ++i
)
15119 if (expanded_specs
)
15120 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15122 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15123 if (spec
== error_mark_node
)
15125 new_specs
= add_exception_specifier (new_specs
, spec
,
15129 specs
= TREE_CHAIN (specs
);
15135 /* Substitute through a TREE_LIST of types or expressions, handling pack
15139 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15141 if (t
== void_list_node
)
15144 tree purpose
= TREE_PURPOSE (t
);
15145 tree purposevec
= NULL_TREE
;
15148 else if (PACK_EXPANSION_P (purpose
))
15150 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15151 if (TREE_CODE (purpose
) == TREE_VEC
)
15152 purposevec
= purpose
;
15154 else if (TYPE_P (purpose
))
15155 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15157 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15158 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15159 return error_mark_node
;
15161 tree value
= TREE_VALUE (t
);
15162 tree valuevec
= NULL_TREE
;
15165 else if (PACK_EXPANSION_P (value
))
15167 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15168 if (TREE_CODE (value
) == TREE_VEC
)
15171 else if (TYPE_P (value
))
15172 value
= tsubst (value
, args
, complain
, in_decl
);
15174 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15175 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15176 return error_mark_node
;
15178 tree chain
= TREE_CHAIN (t
);
15181 else if (TREE_CODE (chain
) == TREE_LIST
)
15182 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15183 else if (TYPE_P (chain
))
15184 chain
= tsubst (chain
, args
, complain
, in_decl
);
15186 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15187 if (chain
== error_mark_node
)
15188 return error_mark_node
;
15190 if (purpose
== TREE_PURPOSE (t
)
15191 && value
== TREE_VALUE (t
)
15192 && chain
== TREE_CHAIN (t
))
15196 /* Determine the number of arguments. */
15199 len
= TREE_VEC_LENGTH (purposevec
);
15200 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15203 len
= TREE_VEC_LENGTH (valuevec
);
15207 for (int i
= len
; i
-- > 0; )
15210 purpose
= TREE_VEC_ELT (purposevec
, i
);
15212 value
= TREE_VEC_ELT (valuevec
, i
);
15214 if (value
&& TYPE_P (value
))
15215 chain
= hash_tree_cons (purpose
, value
, chain
);
15217 chain
= tree_cons (purpose
, value
, chain
);
15223 /* Take the tree structure T and replace template parameters used
15224 therein with the argument vector ARGS. IN_DECL is an associated
15225 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15226 Issue error and warning messages under control of COMPLAIN. Note
15227 that we must be relatively non-tolerant of extensions here, in
15228 order to preserve conformance; if we allow substitutions that
15229 should not be allowed, we may allow argument deductions that should
15230 not succeed, and therefore report ambiguous overload situations
15231 where there are none. In theory, we could allow the substitution,
15232 but indicate that it should have failed, and allow our caller to
15233 make sure that the right thing happens, but we don't try to do this
15236 This function is used for dealing with types, decls and the like;
15237 for expressions, use tsubst_expr or tsubst_copy. */
15240 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15242 enum tree_code code
;
15243 tree type
, r
= NULL_TREE
;
15245 if (t
== NULL_TREE
|| t
== error_mark_node
15246 || t
== integer_type_node
15247 || t
== void_type_node
15248 || t
== char_type_node
15249 || t
== unknown_type_node
15250 || TREE_CODE (t
) == NAMESPACE_DECL
15251 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15255 return tsubst_decl (t
, args
, complain
);
15257 if (args
== NULL_TREE
)
15260 code
= TREE_CODE (t
);
15262 if (code
== IDENTIFIER_NODE
)
15263 type
= IDENTIFIER_TYPE_VALUE (t
);
15265 type
= TREE_TYPE (t
);
15267 gcc_assert (type
!= unknown_type_node
);
15269 /* Reuse typedefs. We need to do this to handle dependent attributes,
15270 such as attribute aligned. */
15272 && typedef_variant_p (t
))
15274 tree decl
= TYPE_NAME (t
);
15276 if (alias_template_specialization_p (t
, nt_opaque
))
15278 /* DECL represents an alias template and we want to
15280 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15281 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15282 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15284 else if (DECL_CLASS_SCOPE_P (decl
)
15285 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15286 && uses_template_parms (DECL_CONTEXT (decl
)))
15288 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15289 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15290 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15292 else if (DECL_FUNCTION_SCOPE_P (decl
)
15293 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15294 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15295 r
= retrieve_local_specialization (decl
);
15297 /* The typedef is from a non-template context. */
15303 r
= cp_build_qualified_type_real
15304 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15305 complain
| tf_ignore_bad_quals
);
15310 /* We don't have an instantiation yet, so drop the typedef. */
15311 int quals
= cp_type_quals (t
);
15312 t
= DECL_ORIGINAL_TYPE (decl
);
15313 t
= cp_build_qualified_type_real (t
, quals
,
15314 complain
| tf_ignore_bad_quals
);
15318 bool fndecl_type
= (complain
& tf_fndecl_type
);
15319 complain
&= ~tf_fndecl_type
;
15322 && code
!= TYPENAME_TYPE
15323 && code
!= TEMPLATE_TYPE_PARM
15324 && code
!= TEMPLATE_PARM_INDEX
15325 && code
!= IDENTIFIER_NODE
15326 && code
!= FUNCTION_TYPE
15327 && code
!= METHOD_TYPE
)
15328 type
= tsubst (type
, args
, complain
, in_decl
);
15329 if (type
== error_mark_node
)
15330 return error_mark_node
;
15336 case ENUMERAL_TYPE
:
15337 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15338 /*entering_scope=*/0);
15341 case IDENTIFIER_NODE
:
15352 if (t
== integer_type_node
)
15355 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15356 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15360 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15362 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15363 /*integral_constant_expression_p=*/false);
15365 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15367 if (TREE_CODE (max
) == NOP_EXPR
15368 && TREE_SIDE_EFFECTS (omax
)
15369 && !TREE_TYPE (max
))
15370 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15372 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15373 with TREE_SIDE_EFFECTS that indicates this is not an integral
15374 constant expression. */
15375 if (processing_template_decl
15376 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15378 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15379 TREE_SIDE_EFFECTS (max
) = 1;
15382 return compute_array_index_type (NULL_TREE
, max
, complain
);
15385 case TEMPLATE_TYPE_PARM
:
15386 case TEMPLATE_TEMPLATE_PARM
:
15387 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15388 case TEMPLATE_PARM_INDEX
:
15393 tree arg
= NULL_TREE
;
15397 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15398 template_parm_level_and_index (t
, &level
, &idx
);
15400 levels
= TMPL_ARGS_DEPTH (args
);
15401 if (level
<= levels
15402 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15404 arg
= TMPL_ARG (args
, level
, idx
);
15406 /* See through ARGUMENT_PACK_SELECT arguments. */
15407 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15408 arg
= argument_pack_select_arg (arg
);
15411 if (arg
== error_mark_node
)
15412 return error_mark_node
;
15413 else if (arg
!= NULL_TREE
)
15415 if (ARGUMENT_PACK_P (arg
))
15416 /* If ARG is an argument pack, we don't actually want to
15417 perform a substitution here, because substitutions
15418 for argument packs are only done
15419 element-by-element. We can get to this point when
15420 substituting the type of a non-type template
15421 parameter pack, when that type actually contains
15422 template parameter packs from an outer template, e.g.,
15424 template<typename... Types> struct A {
15425 template<Types... Values> struct B { };
15429 if (code
== TEMPLATE_TYPE_PARM
)
15433 /* When building concept checks for the purpose of
15434 deducing placeholders, we can end up with wildcards
15435 where types are expected. Adjust this to the deduced
15437 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15438 arg
= TREE_TYPE (TREE_TYPE (arg
));
15440 gcc_assert (TYPE_P (arg
));
15442 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15444 return cp_build_qualified_type_real
15445 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15447 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15449 /* We are processing a type constructed from a
15450 template template parameter. */
15451 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15452 args
, complain
, in_decl
);
15453 if (argvec
== error_mark_node
)
15454 return error_mark_node
;
15456 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15457 || TREE_CODE (arg
) == TEMPLATE_DECL
15458 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15460 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15461 /* Consider this code:
15463 template <template <class> class Template>
15465 template <class Arg> using Bind = Template<Arg>;
15468 template <template <class> class Template, class Arg>
15469 using Instantiate = Template<Arg>; //#0
15471 template <template <class> class Template,
15474 Instantiate<Internal<Template>::template Bind,
15477 When #1 is parsed, the
15478 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15479 parameter `Template' in #0 matches the
15480 UNBOUND_CLASS_TEMPLATE representing the argument
15481 `Internal<Template>::template Bind'; We then want
15482 to assemble the type `Bind<Argument>' that can't
15483 be fully created right now, because
15484 `Internal<Template>' not being complete, the Bind
15485 template cannot be looked up in that context. So
15486 we need to "store" `Bind<Argument>' for later
15487 when the context of Bind becomes complete. Let's
15488 store that in a TYPENAME_TYPE. */
15489 return make_typename_type (TYPE_CONTEXT (arg
),
15490 build_nt (TEMPLATE_ID_EXPR
,
15491 TYPE_IDENTIFIER (arg
),
15496 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15497 are resolving nested-types in the signature of a
15498 member function templates. Otherwise ARG is a
15499 TEMPLATE_DECL and is the real template to be
15501 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15502 arg
= TYPE_NAME (arg
);
15504 r
= lookup_template_class (arg
,
15506 DECL_CONTEXT (arg
),
15507 /*entering_scope=*/0,
15509 return cp_build_qualified_type_real
15510 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15512 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15515 /* TEMPLATE_PARM_INDEX. */
15516 return convert_from_reference (unshare_expr (arg
));
15520 /* This can happen during the attempted tsubst'ing in
15521 unify. This means that we don't yet have any information
15522 about the template parameter in question. */
15525 /* Early in template argument deduction substitution, we don't
15526 want to reduce the level of 'auto', or it will be confused
15527 with a normal template parm in subsequent deduction.
15528 Similarly, don't reduce the level of template parameters to
15529 avoid mismatches when deducing their types. */
15530 if (complain
& tf_partial
)
15533 /* If we get here, we must have been looking at a parm for a
15534 more deeply nested template. Make a new version of this
15535 template parameter, but with a lower level. */
15538 case TEMPLATE_TYPE_PARM
:
15539 case TEMPLATE_TEMPLATE_PARM
:
15540 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15541 if (cp_type_quals (t
))
15543 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15544 r
= cp_build_qualified_type_real
15545 (r
, cp_type_quals (t
),
15546 complain
| (code
== TEMPLATE_TYPE_PARM
15547 ? tf_ignore_bad_quals
: 0));
15549 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15550 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15551 && (r
= (TEMPLATE_PARM_DESCENDANTS
15552 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15553 && (r
= TREE_TYPE (r
))
15554 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15555 /* Break infinite recursion when substituting the constraints
15556 of a constrained placeholder. */;
15557 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15558 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15559 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15560 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15561 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15562 && (TEMPLATE_PARM_LEVEL (r
)
15563 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15564 /* Cache the simple case of lowering a type parameter. */
15569 TEMPLATE_TYPE_PARM_INDEX (r
)
15570 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15571 r
, levels
, args
, complain
);
15572 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15573 TYPE_MAIN_VARIANT (r
) = r
;
15574 TYPE_POINTER_TO (r
) = NULL_TREE
;
15575 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15577 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15579 /* Propagate constraints on placeholders since they are
15580 only instantiated during satisfaction. */
15581 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15582 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15583 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15585 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15586 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15590 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15591 /* We have reduced the level of the template
15592 template parameter, but not the levels of its
15593 template parameters, so canonical_type_parameter
15594 will not be able to find the canonical template
15595 template parameter for this level. Thus, we
15596 require structural equality checking to compare
15597 TEMPLATE_TEMPLATE_PARMs. */
15598 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15599 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15600 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15602 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15604 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15606 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15607 /* We might need to substitute into the types of non-type
15608 template parameters. */
15609 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15610 complain
, in_decl
);
15611 if (tmpl
== error_mark_node
)
15612 return error_mark_node
;
15613 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15614 complain
, in_decl
);
15615 if (argvec
== error_mark_node
)
15616 return error_mark_node
;
15618 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15619 = build_template_info (tmpl
, argvec
);
15624 case TEMPLATE_PARM_INDEX
:
15625 /* OK, now substitute the type of the non-type parameter. We
15626 couldn't do it earlier because it might be an auto parameter,
15627 and we wouldn't need to if we had an argument. */
15628 type
= tsubst (type
, args
, complain
, in_decl
);
15629 if (type
== error_mark_node
)
15630 return error_mark_node
;
15631 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15635 gcc_unreachable ();
15642 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15645 /* We should never be tsubsting a binfo. */
15646 gcc_unreachable ();
15649 /* A vector of template arguments. */
15650 gcc_assert (!type
);
15651 return tsubst_template_args (t
, args
, complain
, in_decl
);
15654 case REFERENCE_TYPE
:
15656 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15661 Type deduction may fail for any of the following
15664 -- Attempting to create a pointer to reference type.
15665 -- Attempting to create a reference to a reference type or
15666 a reference to void.
15668 Core issue 106 says that creating a reference to a reference
15669 during instantiation is no longer a cause for failure. We
15670 only enforce this check in strict C++98 mode. */
15671 if ((TYPE_REF_P (type
)
15672 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15673 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15675 static location_t last_loc
;
15677 /* We keep track of the last time we issued this error
15678 message to avoid spewing a ton of messages during a
15679 single bad template instantiation. */
15680 if (complain
& tf_error
15681 && last_loc
!= input_location
)
15683 if (VOID_TYPE_P (type
))
15684 error ("forming reference to void");
15685 else if (code
== POINTER_TYPE
)
15686 error ("forming pointer to reference type %qT", type
);
15688 error ("forming reference to reference type %qT", type
);
15689 last_loc
= input_location
;
15692 return error_mark_node
;
15694 else if (TREE_CODE (type
) == FUNCTION_TYPE
15695 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15696 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15698 if (complain
& tf_error
)
15700 if (code
== POINTER_TYPE
)
15701 error ("forming pointer to qualified function type %qT",
15704 error ("forming reference to qualified function type %qT",
15707 return error_mark_node
;
15709 else if (code
== POINTER_TYPE
)
15711 r
= build_pointer_type (type
);
15712 if (TREE_CODE (type
) == METHOD_TYPE
)
15713 r
= build_ptrmemfunc_type (r
);
15715 else if (TYPE_REF_P (type
))
15716 /* In C++0x, during template argument substitution, when there is an
15717 attempt to create a reference to a reference type, reference
15718 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15720 "If a template-argument for a template-parameter T names a type
15721 that is a reference to a type A, an attempt to create the type
15722 'lvalue reference to cv T' creates the type 'lvalue reference to
15723 A,' while an attempt to create the type type rvalue reference to
15724 cv T' creates the type T"
15726 r
= cp_build_reference_type
15728 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15730 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15731 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15733 if (r
!= error_mark_node
)
15734 /* Will this ever be needed for TYPE_..._TO values? */
15741 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15742 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15746 Type deduction may fail for any of the following
15749 -- Attempting to create "pointer to member of T" when T
15750 is not a class type. */
15751 if (complain
& tf_error
)
15752 error ("creating pointer to member of non-class type %qT", r
);
15753 return error_mark_node
;
15755 if (TYPE_REF_P (type
))
15757 if (complain
& tf_error
)
15758 error ("creating pointer to member reference type %qT", type
);
15759 return error_mark_node
;
15761 if (VOID_TYPE_P (type
))
15763 if (complain
& tf_error
)
15764 error ("creating pointer to member of type void");
15765 return error_mark_node
;
15767 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15768 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15770 /* The type of the implicit object parameter gets its
15771 cv-qualifiers from the FUNCTION_TYPE. */
15774 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15775 type_memfn_rqual (type
));
15776 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15777 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15781 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15785 case FUNCTION_TYPE
:
15790 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15791 if (fntype
== error_mark_node
)
15792 return error_mark_node
;
15794 /* Substitute the exception specification. */
15795 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15796 /*defer_ok*/fndecl_type
);
15797 if (specs
== error_mark_node
)
15798 return error_mark_node
;
15800 fntype
= build_exception_variant (fntype
, specs
);
15805 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15806 if (domain
== error_mark_node
)
15807 return error_mark_node
;
15809 /* As an optimization, we avoid regenerating the array type if
15810 it will obviously be the same as T. */
15811 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15814 /* These checks should match the ones in create_array_type_for_decl.
15818 The deduction may fail for any of the following reasons:
15820 -- Attempting to create an array with an element type that
15821 is void, a function type, or a reference type, or [DR337]
15822 an abstract class type. */
15823 if (VOID_TYPE_P (type
)
15824 || TREE_CODE (type
) == FUNCTION_TYPE
15825 || (TREE_CODE (type
) == ARRAY_TYPE
15826 && TYPE_DOMAIN (type
) == NULL_TREE
)
15827 || TYPE_REF_P (type
))
15829 if (complain
& tf_error
)
15830 error ("creating array of %qT", type
);
15831 return error_mark_node
;
15834 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15835 return error_mark_node
;
15837 r
= build_cplus_array_type (type
, domain
);
15839 if (!valid_array_size_p (input_location
, r
, in_decl
,
15840 (complain
& tf_error
)))
15841 return error_mark_node
;
15843 if (TYPE_USER_ALIGN (t
))
15845 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15846 TYPE_USER_ALIGN (r
) = 1;
15852 case TYPENAME_TYPE
:
15854 tree ctx
= TYPE_CONTEXT (t
);
15855 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15857 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15858 if (ctx
== error_mark_node
15859 || TREE_VEC_LENGTH (ctx
) > 1)
15860 return error_mark_node
;
15861 if (TREE_VEC_LENGTH (ctx
) == 0)
15863 if (complain
& tf_error
)
15864 error ("%qD is instantiated for an empty pack",
15865 TYPENAME_TYPE_FULLNAME (t
));
15866 return error_mark_node
;
15868 ctx
= TREE_VEC_ELT (ctx
, 0);
15871 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15872 /*entering_scope=*/1);
15873 if (ctx
== error_mark_node
)
15874 return error_mark_node
;
15876 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15877 complain
, in_decl
);
15878 if (f
== error_mark_node
)
15879 return error_mark_node
;
15881 if (!MAYBE_CLASS_TYPE_P (ctx
))
15883 if (complain
& tf_error
)
15884 error ("%qT is not a class, struct, or union type", ctx
);
15885 return error_mark_node
;
15887 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15889 /* Normally, make_typename_type does not require that the CTX
15890 have complete type in order to allow things like:
15892 template <class T> struct S { typename S<T>::X Y; };
15894 But, such constructs have already been resolved by this
15895 point, so here CTX really should have complete type, unless
15896 it's a partial instantiation. */
15897 ctx
= complete_type (ctx
);
15898 if (!COMPLETE_TYPE_P (ctx
))
15900 if (complain
& tf_error
)
15901 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15902 return error_mark_node
;
15906 f
= make_typename_type (ctx
, f
, typename_type
,
15907 complain
| tf_keep_type_decl
);
15908 if (f
== error_mark_node
)
15910 if (TREE_CODE (f
) == TYPE_DECL
)
15912 complain
|= tf_ignore_bad_quals
;
15916 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15918 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15920 if (complain
& tf_error
)
15921 error ("%qT resolves to %qT, which is not an enumeration type",
15924 return error_mark_node
;
15926 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15928 if (complain
& tf_error
)
15929 error ("%qT resolves to %qT, which is not a class type",
15932 return error_mark_node
;
15936 return cp_build_qualified_type_real
15937 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15940 case UNBOUND_CLASS_TEMPLATE
:
15942 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15943 in_decl
, /*entering_scope=*/1);
15944 tree name
= TYPE_IDENTIFIER (t
);
15945 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15947 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15948 return error_mark_node
;
15951 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15952 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15959 ++cp_unevaluated_operand
;
15960 ++c_inhibit_evaluation_warnings
;
15962 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15964 /*integral_constant_expression_p=*/false);
15966 --cp_unevaluated_operand
;
15967 --c_inhibit_evaluation_warnings
;
15969 type
= finish_typeof (type
);
15970 return cp_build_qualified_type_real (type
,
15972 | cp_type_quals (type
),
15976 case DECLTYPE_TYPE
:
15980 ++cp_unevaluated_operand
;
15981 ++c_inhibit_evaluation_warnings
;
15983 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15984 complain
|tf_decltype
, in_decl
,
15985 /*function_p*/false,
15986 /*integral_constant_expression*/false);
15988 --cp_unevaluated_operand
;
15989 --c_inhibit_evaluation_warnings
;
15991 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15992 type
= lambda_capture_field_type (type
,
15993 false /*explicit_init*/,
15994 DECLTYPE_FOR_REF_CAPTURE (t
));
15995 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15996 type
= lambda_proxy_type (type
);
15999 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16000 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16002 /* In a template ~id could be either a complement expression
16003 or an unqualified-id naming a destructor; if instantiating
16004 it produces an expression, it's not an id-expression or
16007 type
= finish_decltype_type (type
, id
, complain
);
16009 return cp_build_qualified_type_real (type
,
16011 | cp_type_quals (type
),
16012 complain
| tf_ignore_bad_quals
);
16015 case UNDERLYING_TYPE
:
16017 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16018 complain
, in_decl
);
16019 return finish_underlying_type (type
);
16022 case TYPE_ARGUMENT_PACK
:
16023 case NONTYPE_ARGUMENT_PACK
:
16027 if (code
== NONTYPE_ARGUMENT_PACK
)
16028 r
= make_node (code
);
16030 r
= cxx_make_type (code
);
16032 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
16033 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
16034 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
16052 /* We should use one of the expression tsubsts for these codes. */
16053 gcc_unreachable ();
16056 sorry ("use of %qs in template", get_tree_code_name (code
));
16057 return error_mark_node
;
16061 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16062 expression on the left-hand side of the "." or "->" operator. We
16063 only do the lookup if we had a dependent BASELINK. Otherwise we
16064 adjust it onto the instantiated heirarchy. */
16067 tsubst_baselink (tree baselink
, tree object_type
,
16068 tree args
, tsubst_flags_t complain
, tree in_decl
)
16070 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16071 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16072 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16074 tree optype
= BASELINK_OPTYPE (baselink
);
16075 optype
= tsubst (optype
, args
, complain
, in_decl
);
16077 tree template_args
= NULL_TREE
;
16078 bool template_id_p
= false;
16079 tree fns
= BASELINK_FUNCTIONS (baselink
);
16080 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16082 template_id_p
= true;
16083 template_args
= TREE_OPERAND (fns
, 1);
16084 fns
= TREE_OPERAND (fns
, 0);
16086 template_args
= tsubst_template_args (template_args
, args
,
16087 complain
, in_decl
);
16090 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16091 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16092 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16096 tree name
= OVL_NAME (fns
);
16097 if (IDENTIFIER_CONV_OP_P (name
))
16098 name
= make_conv_op_name (optype
);
16100 if (name
== complete_dtor_identifier
)
16101 /* Treat as-if non-dependent below. */
16102 dependent_p
= false;
16104 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16108 if ((complain
& tf_error
)
16109 && constructor_name_p (name
, qualifying_scope
))
16110 error ("cannot call constructor %<%T::%D%> directly",
16111 qualifying_scope
, name
);
16112 return error_mark_node
;
16115 if (BASELINK_P (baselink
))
16116 fns
= BASELINK_FUNCTIONS (baselink
);
16119 /* We're going to overwrite pieces below, make a duplicate. */
16120 baselink
= copy_node (baselink
);
16122 /* If lookup found a single function, mark it as used at this point.
16123 (If lookup found multiple functions the one selected later by
16124 overload resolution will be marked as used at that point.) */
16125 if (!template_id_p
&& !really_overloaded_fn (fns
))
16127 tree fn
= OVL_FIRST (fns
);
16128 bool ok
= mark_used (fn
, complain
);
16129 if (!ok
&& !(complain
& tf_error
))
16130 return error_mark_node
;
16131 if (ok
&& BASELINK_P (baselink
))
16132 /* We might have instantiated an auto function. */
16133 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16136 if (BASELINK_P (baselink
))
16138 /* Add back the template arguments, if present. */
16140 BASELINK_FUNCTIONS (baselink
)
16141 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16143 /* Update the conversion operator type. */
16144 BASELINK_OPTYPE (baselink
) = optype
;
16148 object_type
= current_class_type
;
16150 if (qualified_p
|| !dependent_p
)
16152 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16156 /* We need to call adjust_result_of_qualified_name_lookup in case the
16157 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16158 so that we still get virtual function binding. */
16159 BASELINK_QUALIFIED_P (baselink
) = false;
16165 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16166 true if the qualified-id will be a postfix-expression in-and-of
16167 itself; false if more of the postfix-expression follows the
16168 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16172 tsubst_qualified_id (tree qualified_id
, tree args
,
16173 tsubst_flags_t complain
, tree in_decl
,
16174 bool done
, bool address_p
)
16180 tree template_args
;
16181 location_t loc
= UNKNOWN_LOCATION
;
16183 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16185 /* Figure out what name to look up. */
16186 name
= TREE_OPERAND (qualified_id
, 1);
16187 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16189 is_template
= true;
16190 loc
= EXPR_LOCATION (name
);
16191 template_args
= TREE_OPERAND (name
, 1);
16193 template_args
= tsubst_template_args (template_args
, args
,
16194 complain
, in_decl
);
16195 if (template_args
== error_mark_node
)
16196 return error_mark_node
;
16197 name
= TREE_OPERAND (name
, 0);
16201 is_template
= false;
16202 template_args
= NULL_TREE
;
16205 /* Substitute into the qualifying scope. When there are no ARGS, we
16206 are just trying to simplify a non-dependent expression. In that
16207 case the qualifying scope may be dependent, and, in any case,
16208 substituting will not help. */
16209 scope
= TREE_OPERAND (qualified_id
, 0);
16212 scope
= tsubst (scope
, args
, complain
, in_decl
);
16213 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16218 if (dependent_scope_p (scope
))
16221 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16222 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16223 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16224 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16228 if (!BASELINK_P (name
) && !DECL_P (expr
))
16230 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16232 /* A BIT_NOT_EXPR is used to represent a destructor. */
16233 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16235 error ("qualifying type %qT does not match destructor name ~%qT",
16236 scope
, TREE_OPERAND (expr
, 0));
16237 expr
= error_mark_node
;
16240 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16241 LOOK_want::NORMAL
, false);
16244 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16245 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16246 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16248 if (complain
& tf_error
)
16250 error ("dependent-name %qE is parsed as a non-type, but "
16251 "instantiation yields a type", qualified_id
);
16252 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16254 return error_mark_node
;
16260 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16262 return error_mark_node
;
16263 /* Remember that there was a reference to this entity. */
16264 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16265 return error_mark_node
;
16268 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16270 if (complain
& tf_error
)
16271 qualified_name_lookup_error (scope
,
16272 TREE_OPERAND (qualified_id
, 1),
16273 expr
, input_location
);
16274 return error_mark_node
;
16279 /* We may be repeating a check already done during parsing, but
16280 if it was well-formed and passed then, it will pass again
16281 now, and if it didn't, we wouldn't have got here. The case
16282 we want to catch is when we couldn't tell then, and can now,
16283 namely when templ prior to substitution was an
16285 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16286 return error_mark_node
;
16288 if (variable_template_p (expr
))
16289 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16292 expr
= lookup_template_function (expr
, template_args
);
16295 if (expr
== error_mark_node
&& complain
& tf_error
)
16296 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16297 expr
, input_location
);
16298 else if (TYPE_P (scope
))
16300 expr
= (adjust_result_of_qualified_name_lookup
16301 (expr
, scope
, current_nonlambda_class_type ()));
16302 expr
= (finish_qualified_id_expr
16303 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16304 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16305 /*template_arg_p=*/false, complain
));
16308 /* Expressions do not generally have reference type. */
16309 if (TREE_CODE (expr
) != SCOPE_REF
16310 /* However, if we're about to form a pointer-to-member, we just
16311 want the referenced member referenced. */
16312 && TREE_CODE (expr
) != OFFSET_REF
)
16313 expr
= convert_from_reference (expr
);
16315 if (REF_PARENTHESIZED_P (qualified_id
))
16316 expr
= force_paren_expr (expr
);
16321 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16322 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16326 tsubst_init (tree init
, tree decl
, tree args
,
16327 tsubst_flags_t complain
, tree in_decl
)
16332 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16334 tree type
= TREE_TYPE (decl
);
16336 if (!init
&& type
!= error_mark_node
)
16338 if (tree auto_node
= type_uses_auto (type
))
16340 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16342 if (complain
& tf_error
)
16343 error ("initializer for %q#D expands to an empty list "
16344 "of expressions", decl
);
16345 return error_mark_node
;
16348 else if (!dependent_type_p (type
))
16350 /* If we had an initializer but it
16351 instantiated to nothing,
16352 value-initialize the object. This will
16353 only occur when the initializer was a
16354 pack expansion where the parameter packs
16355 used in that expansion were of length
16357 init
= build_value_init (type
, complain
);
16358 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16359 init
= get_target_expr_sfinae (init
, complain
);
16360 if (TREE_CODE (init
) == TARGET_EXPR
)
16361 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16368 /* If T is a reference to a dependent member of the current instantiation C and
16369 we are trying to refer to that member in a partial instantiation of C,
16370 return a SCOPE_REF; otherwise, return NULL_TREE.
16372 This can happen when forming a C++17 deduction guide, as in PR96199. */
16375 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16378 if (cxx_dialect
< cxx17
)
16381 tree ctx
= context_for_name_lookup (t
);
16382 if (!CLASS_TYPE_P (ctx
))
16385 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16386 if (dependent_scope_p (ctx
))
16387 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16388 /*template_p=*/false);
16393 /* Like tsubst, but deals with expressions. This function just replaces
16394 template parms; to finish processing the resultant expression, use
16395 tsubst_copy_and_build or tsubst_expr. */
16398 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16400 enum tree_code code
;
16403 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16406 code
= TREE_CODE (t
);
16411 r
= retrieve_local_specialization (t
);
16413 if (r
== NULL_TREE
)
16415 /* We get here for a use of 'this' in an NSDMI. */
16416 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16417 return current_class_ptr
;
16419 /* This can happen for a parameter name used later in a function
16420 declaration (such as in a late-specified return type). Just
16421 make a dummy decl, since it's only used for its type. */
16422 gcc_assert (cp_unevaluated_operand
!= 0);
16423 r
= tsubst_decl (t
, args
, complain
);
16424 /* Give it the template pattern as its context; its true context
16425 hasn't been instantiated yet and this is good enough for
16427 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16430 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16431 r
= argument_pack_select_arg (r
);
16432 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16433 return error_mark_node
;
16441 if (DECL_TEMPLATE_PARM_P (t
))
16442 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16443 /* There is no need to substitute into namespace-scope
16445 if (DECL_NAMESPACE_SCOPE_P (t
))
16447 /* If ARGS is NULL, then T is known to be non-dependent. */
16448 if (args
== NULL_TREE
)
16449 return scalar_constant_value (t
);
16451 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16454 /* Unfortunately, we cannot just call lookup_name here.
16457 template <int I> int f() {
16459 struct S { void g() { E e = a; } };
16462 When we instantiate f<7>::S::g(), say, lookup_name is not
16463 clever enough to find f<7>::a. */
16465 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16466 /*entering_scope=*/0);
16468 for (v
= TYPE_VALUES (enum_type
);
16470 v
= TREE_CHAIN (v
))
16471 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16472 return TREE_VALUE (v
);
16474 /* We didn't find the name. That should never happen; if
16475 name-lookup found it during preliminary parsing, we
16476 should find it again here during instantiation. */
16477 gcc_unreachable ();
16482 if (DECL_CONTEXT (t
))
16486 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16487 /*entering_scope=*/1);
16488 if (ctx
!= DECL_CONTEXT (t
))
16490 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16493 if (complain
& tf_error
)
16494 error ("using invalid field %qD", t
);
16495 return error_mark_node
;
16504 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16507 case FUNCTION_DECL
:
16508 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16509 r
= tsubst (t
, args
, complain
, in_decl
);
16510 else if (local_variable_p (t
)
16511 && uses_template_parms (DECL_CONTEXT (t
)))
16513 r
= retrieve_local_specialization (t
);
16514 if (r
== NULL_TREE
)
16516 /* First try name lookup to find the instantiation. */
16517 r
= lookup_name (DECL_NAME (t
));
16522 /* During error-recovery we may find a non-variable,
16523 even an OVERLOAD: just bail out and avoid ICEs and
16524 duplicate diagnostics (c++/62207). */
16525 gcc_assert (seen_error ());
16526 return error_mark_node
;
16528 if (!is_capture_proxy (r
))
16530 /* Make sure the one we found is the one we want. */
16531 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16532 if (ctx
!= DECL_CONTEXT (r
))
16541 /* This can happen for a variable used in a
16542 late-specified return type of a local lambda, or for a
16543 local static or constant. Building a new VAR_DECL
16544 should be OK in all those cases. */
16545 r
= tsubst_decl (t
, args
, complain
);
16546 if (local_specializations
)
16547 /* Avoid infinite recursion (79640). */
16548 register_local_specialization (r
, t
);
16549 if (decl_maybe_constant_var_p (r
))
16551 /* We can't call cp_finish_decl, so handle the
16552 initializer by hand. */
16553 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16554 complain
, in_decl
);
16555 if (!processing_template_decl
)
16556 init
= maybe_constant_init (init
);
16557 if (processing_template_decl
16558 ? potential_constant_expression (init
)
16559 : reduced_constant_expression_p (init
))
16560 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16561 = TREE_CONSTANT (r
) = true;
16562 DECL_INITIAL (r
) = init
;
16563 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16565 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16566 complain
, adc_variable_type
);
16568 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16569 || decl_constant_var_p (r
)
16571 if (!processing_template_decl
16572 && !TREE_STATIC (r
))
16573 r
= process_outer_var_ref (r
, complain
);
16575 /* Remember this for subsequent uses. */
16576 if (local_specializations
)
16577 register_local_specialization (r
, t
);
16579 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16580 r
= argument_pack_select_arg (r
);
16584 if (!mark_used (r
, complain
))
16585 return error_mark_node
;
16588 case NAMESPACE_DECL
:
16595 return tsubst_baselink (t
, current_nonlambda_class_type (),
16596 args
, complain
, in_decl
);
16598 case TEMPLATE_DECL
:
16599 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16600 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16601 args
, complain
, in_decl
);
16602 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16603 return tsubst (t
, args
, complain
, in_decl
);
16604 else if (DECL_CLASS_SCOPE_P (t
)
16605 && uses_template_parms (DECL_CONTEXT (t
)))
16607 /* Template template argument like the following example need
16610 template <template <class> class TT> struct C {};
16611 template <class T> struct D {
16612 template <class U> struct E {};
16617 We are processing the template argument `E' in #1 for
16618 the template instantiation #2. Originally, `E' is a
16619 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16620 have to substitute this with one having context `D<int>'. */
16622 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16623 if (dependent_scope_p (context
))
16625 /* When rewriting a constructor into a deduction guide, a
16626 non-dependent name can become dependent, so memtmpl<args>
16627 becomes context::template memtmpl<args>. */
16628 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16629 return build_qualified_name (type
, context
, DECL_NAME (t
),
16632 return lookup_field (context
, DECL_NAME(t
), 0, false);
16635 /* Ordinary template template argument. */
16638 case NON_LVALUE_EXPR
:
16639 case VIEW_CONVERT_EXPR
:
16641 /* Handle location wrappers by substituting the wrapped node
16642 first, *then* reusing the resulting type. Doing the type
16643 first ensures that we handle template parameters and
16644 parameter pack expansions. */
16645 if (location_wrapper_p (t
))
16647 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16648 complain
, in_decl
);
16649 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16651 tree op
= TREE_OPERAND (t
, 0);
16652 if (code
== VIEW_CONVERT_EXPR
16653 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16655 /* Wrapper to make a C++20 template parameter object const. */
16656 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16657 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16659 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16660 return build1 (code
, type
, op
);
16664 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16665 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16666 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16670 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16671 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16673 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16674 op
= build1 (code
, TREE_TYPE (op
), op
);
16675 REF_PARENTHESIZED_P (op
) = true;
16678 /* We shouldn't see any other uses of these in templates. */
16679 gcc_unreachable ();
16683 case REINTERPRET_CAST_EXPR
:
16684 case CONST_CAST_EXPR
:
16685 case STATIC_CAST_EXPR
:
16686 case DYNAMIC_CAST_EXPR
:
16687 case IMPLICIT_CONV_EXPR
:
16691 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16692 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16693 return build1 (code
, type
, op0
);
16697 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16698 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16700 tree expanded
, op
= TREE_OPERAND (t
, 0);
16703 if (SIZEOF_EXPR_TYPE_P (t
))
16704 op
= TREE_TYPE (op
);
16706 ++cp_unevaluated_operand
;
16707 ++c_inhibit_evaluation_warnings
;
16708 /* We only want to compute the number of arguments. */
16709 if (PACK_EXPANSION_P (op
))
16710 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16712 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16713 args
, complain
, in_decl
);
16714 --cp_unevaluated_operand
;
16715 --c_inhibit_evaluation_warnings
;
16717 if (TREE_CODE (expanded
) == TREE_VEC
)
16719 len
= TREE_VEC_LENGTH (expanded
);
16720 /* Set TREE_USED for the benefit of -Wunused. */
16721 for (int i
= 0; i
< len
; i
++)
16722 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16723 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16726 if (expanded
== error_mark_node
)
16727 return error_mark_node
;
16728 else if (PACK_EXPANSION_P (expanded
)
16729 || (TREE_CODE (expanded
) == TREE_VEC
16730 && pack_expansion_args_count (expanded
)))
16733 if (PACK_EXPANSION_P (expanded
))
16735 else if (TREE_VEC_LENGTH (expanded
) == 1)
16736 expanded
= TREE_VEC_ELT (expanded
, 0);
16738 expanded
= make_argument_pack (expanded
);
16740 if (TYPE_P (expanded
))
16741 return cxx_sizeof_or_alignof_type (input_location
,
16742 expanded
, SIZEOF_EXPR
,
16744 complain
& tf_error
);
16746 return cxx_sizeof_or_alignof_expr (input_location
,
16747 expanded
, SIZEOF_EXPR
,
16748 complain
& tf_error
);
16751 return build_int_cst (size_type_node
, len
);
16753 if (SIZEOF_EXPR_TYPE_P (t
))
16755 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16756 args
, complain
, in_decl
);
16757 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16758 r
= build1 (SIZEOF_EXPR
,
16759 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16760 SIZEOF_EXPR_TYPE_P (r
) = 1;
16767 case TRUTH_NOT_EXPR
:
16770 case UNARY_PLUS_EXPR
: /* Unary + */
16772 case AT_ENCODE_EXPR
:
16776 case REALPART_EXPR
:
16777 case IMAGPART_EXPR
:
16780 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16781 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16782 r
= build1 (code
, type
, op0
);
16783 if (code
== ALIGNOF_EXPR
)
16784 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16788 case COMPONENT_REF
:
16793 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16794 name
= TREE_OPERAND (t
, 1);
16795 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16797 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16798 complain
, in_decl
);
16799 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16801 else if (TREE_CODE (name
) == SCOPE_REF
16802 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16804 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16805 complain
, in_decl
);
16806 name
= TREE_OPERAND (name
, 1);
16807 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16808 complain
, in_decl
);
16809 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16810 name
= build_qualified_name (/*type=*/NULL_TREE
,
16812 /*template_p=*/false);
16814 else if (BASELINK_P (name
))
16815 name
= tsubst_baselink (name
,
16816 non_reference (TREE_TYPE (object
)),
16820 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16821 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16827 case TRUNC_DIV_EXPR
:
16828 case CEIL_DIV_EXPR
:
16829 case FLOOR_DIV_EXPR
:
16830 case ROUND_DIV_EXPR
:
16831 case EXACT_DIV_EXPR
:
16835 case TRUNC_MOD_EXPR
:
16836 case FLOOR_MOD_EXPR
:
16837 case TRUTH_ANDIF_EXPR
:
16838 case TRUTH_ORIF_EXPR
:
16839 case TRUTH_AND_EXPR
:
16840 case TRUTH_OR_EXPR
:
16851 case COMPOUND_EXPR
:
16854 case PREDECREMENT_EXPR
:
16855 case PREINCREMENT_EXPR
:
16856 case POSTDECREMENT_EXPR
:
16857 case POSTINCREMENT_EXPR
:
16859 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16860 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16861 return build_nt (code
, op0
, op1
);
16866 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16867 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16868 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16869 QUALIFIED_NAME_IS_TEMPLATE (t
));
16874 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16875 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16876 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16881 int n
= VL_EXP_OPERAND_LENGTH (t
);
16882 tree result
= build_vl_exp (CALL_EXPR
, n
);
16884 for (i
= 0; i
< n
; i
++)
16885 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16886 complain
, in_decl
);
16892 case PSEUDO_DTOR_EXPR
:
16893 case VEC_PERM_EXPR
:
16895 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16896 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16897 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16898 r
= build_nt (code
, op0
, op1
, op2
);
16899 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16905 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16906 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16907 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16908 r
= build_nt (code
, op0
, op1
, op2
);
16909 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16915 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16916 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16917 r
= build_nt (code
, op0
, op1
);
16918 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16919 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16923 case TEMPLATE_ID_EXPR
:
16925 /* Substituted template arguments */
16926 tree fn
= TREE_OPERAND (t
, 0);
16927 tree targs
= TREE_OPERAND (t
, 1);
16929 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16931 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16933 return lookup_template_function (fn
, targs
);
16938 tree purpose
, value
, chain
;
16940 if (t
== void_list_node
)
16943 purpose
= TREE_PURPOSE (t
);
16945 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16946 value
= TREE_VALUE (t
);
16948 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16949 chain
= TREE_CHAIN (t
);
16950 if (chain
&& chain
!= void_type_node
)
16951 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16952 if (purpose
== TREE_PURPOSE (t
)
16953 && value
== TREE_VALUE (t
)
16954 && chain
== TREE_CHAIN (t
))
16956 return tree_cons (purpose
, value
, chain
);
16961 case ENUMERAL_TYPE
:
16963 case TEMPLATE_TYPE_PARM
:
16964 case TEMPLATE_TEMPLATE_PARM
:
16965 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16966 case TEMPLATE_PARM_INDEX
:
16968 case REFERENCE_TYPE
:
16970 case FUNCTION_TYPE
:
16973 case TYPENAME_TYPE
:
16974 case UNBOUND_CLASS_TEMPLATE
:
16976 case DECLTYPE_TYPE
:
16978 return tsubst (t
, args
, complain
, in_decl
);
16982 /* Fall through. */
16983 case IDENTIFIER_NODE
:
16984 if (IDENTIFIER_CONV_OP_P (t
))
16986 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16987 return make_conv_op_name (new_type
);
16993 /* This is handled by tsubst_copy_and_build. */
16994 gcc_unreachable ();
16998 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16999 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17000 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17003 case CLEANUP_POINT_EXPR
:
17004 /* We shouldn't have built any of these during initial template
17005 generation. Instead, they should be built during instantiation
17006 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17007 gcc_unreachable ();
17011 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17012 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17013 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17014 r
= build2 (code
, type
, op0
, op1
);
17015 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17016 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17017 && !(complain
& tf_error
))
17018 return error_mark_node
;
17022 case EXPR_PACK_EXPANSION
:
17023 error ("invalid use of pack expansion expression");
17024 return error_mark_node
;
17026 case NONTYPE_ARGUMENT_PACK
:
17027 error ("use %<...%> to expand argument pack");
17028 return error_mark_node
;
17031 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17038 /* Instantiate any typedefs in the type. */
17039 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17040 r
= fold_convert (type
, t
);
17041 gcc_assert (TREE_CODE (r
) == code
);
17047 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17049 if (type
!= TREE_TYPE (t
))
17052 TREE_TYPE (r
) = type
;
17058 /* These can sometimes show up in a partial instantiation, but never
17059 involve template parms. */
17060 gcc_assert (!uses_template_parms (t
));
17063 case UNARY_LEFT_FOLD_EXPR
:
17064 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17065 case UNARY_RIGHT_FOLD_EXPR
:
17066 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17067 case BINARY_LEFT_FOLD_EXPR
:
17068 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17069 case BINARY_RIGHT_FOLD_EXPR
:
17070 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17074 case DEBUG_BEGIN_STMT
:
17075 /* ??? There's no point in copying it for now, but maybe some
17076 day it will contain more information, such as a pointer back
17077 to the containing function, inlined copy or so. */
17080 case CO_AWAIT_EXPR
:
17081 return tsubst_expr (t
, args
, complain
, in_decl
,
17082 /*integral_constant_expression_p=*/false);
17086 /* We shouldn't get here, but keep going if !flag_checking. */
17088 gcc_unreachable ();
17093 /* Helper function for tsubst_omp_clauses, used for instantiation of
17094 OMP_CLAUSE_DECL of clauses. */
17097 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17098 tree in_decl
, tree
*iterator_cache
)
17100 if (decl
== NULL_TREE
)
17103 /* Handle OpenMP iterators. */
17104 if (TREE_CODE (decl
) == TREE_LIST
17105 && TREE_PURPOSE (decl
)
17106 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17109 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17110 ret
= iterator_cache
[1];
17114 begin_scope (sk_omp
, NULL
);
17115 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17117 *tp
= copy_node (it
);
17118 TREE_VEC_ELT (*tp
, 0)
17119 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17120 TREE_VEC_ELT (*tp
, 1)
17121 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17122 /*integral_constant_expression_p=*/false);
17123 TREE_VEC_ELT (*tp
, 2)
17124 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17125 /*integral_constant_expression_p=*/false);
17126 TREE_VEC_ELT (*tp
, 3)
17127 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17128 /*integral_constant_expression_p=*/false);
17129 TREE_CHAIN (*tp
) = NULL_TREE
;
17130 tp
= &TREE_CHAIN (*tp
);
17132 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17133 iterator_cache
[0] = TREE_PURPOSE (decl
);
17134 iterator_cache
[1] = ret
;
17136 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17141 /* Handle an OpenMP array section represented as a TREE_LIST (or
17142 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17143 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17144 TREE_LIST. We can handle it exactly the same as an array section
17145 (purpose, value, and a chain), even though the nomenclature
17146 (low_bound, length, etc) is different. */
17147 if (TREE_CODE (decl
) == TREE_LIST
)
17150 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17151 /*integral_constant_expression_p=*/false);
17152 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17153 /*integral_constant_expression_p=*/false);
17154 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17156 if (TREE_PURPOSE (decl
) == low_bound
17157 && TREE_VALUE (decl
) == length
17158 && TREE_CHAIN (decl
) == chain
)
17160 tree ret
= tree_cons (low_bound
, length
, chain
);
17161 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17162 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17165 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17166 /*integral_constant_expression_p=*/false);
17167 /* Undo convert_from_reference tsubst_expr could have called. */
17169 && REFERENCE_REF_P (ret
)
17170 && !REFERENCE_REF_P (decl
))
17171 ret
= TREE_OPERAND (ret
, 0);
17175 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17178 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17179 tree args
, tsubst_flags_t complain
, tree in_decl
)
17181 tree new_clauses
= NULL_TREE
, nc
, oc
;
17182 tree linear_no_step
= NULL_TREE
;
17183 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17185 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17187 nc
= copy_node (oc
);
17188 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17191 switch (OMP_CLAUSE_CODE (nc
))
17193 case OMP_CLAUSE_LASTPRIVATE
:
17194 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17196 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17197 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17198 in_decl
, /*integral_constant_expression_p=*/false);
17199 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17200 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17203 case OMP_CLAUSE_PRIVATE
:
17204 case OMP_CLAUSE_SHARED
:
17205 case OMP_CLAUSE_FIRSTPRIVATE
:
17206 case OMP_CLAUSE_COPYIN
:
17207 case OMP_CLAUSE_COPYPRIVATE
:
17208 case OMP_CLAUSE_UNIFORM
:
17209 case OMP_CLAUSE_DEPEND
:
17210 case OMP_CLAUSE_FROM
:
17211 case OMP_CLAUSE_TO
:
17212 case OMP_CLAUSE_MAP
:
17213 case OMP_CLAUSE_NONTEMPORAL
:
17214 case OMP_CLAUSE_USE_DEVICE_PTR
:
17215 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17216 case OMP_CLAUSE_IS_DEVICE_PTR
:
17217 case OMP_CLAUSE_INCLUSIVE
:
17218 case OMP_CLAUSE_EXCLUSIVE
:
17219 OMP_CLAUSE_DECL (nc
)
17220 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17221 in_decl
, iterator_cache
);
17223 case OMP_CLAUSE_TILE
:
17224 case OMP_CLAUSE_IF
:
17225 case OMP_CLAUSE_NUM_THREADS
:
17226 case OMP_CLAUSE_SCHEDULE
:
17227 case OMP_CLAUSE_COLLAPSE
:
17228 case OMP_CLAUSE_FINAL
:
17229 case OMP_CLAUSE_DEVICE
:
17230 case OMP_CLAUSE_DIST_SCHEDULE
:
17231 case OMP_CLAUSE_NUM_TEAMS
:
17232 case OMP_CLAUSE_THREAD_LIMIT
:
17233 case OMP_CLAUSE_SAFELEN
:
17234 case OMP_CLAUSE_SIMDLEN
:
17235 case OMP_CLAUSE_NUM_TASKS
:
17236 case OMP_CLAUSE_GRAINSIZE
:
17237 case OMP_CLAUSE_PRIORITY
:
17238 case OMP_CLAUSE_ORDERED
:
17239 case OMP_CLAUSE_HINT
:
17240 case OMP_CLAUSE_NUM_GANGS
:
17241 case OMP_CLAUSE_NUM_WORKERS
:
17242 case OMP_CLAUSE_VECTOR_LENGTH
:
17243 case OMP_CLAUSE_WORKER
:
17244 case OMP_CLAUSE_VECTOR
:
17245 case OMP_CLAUSE_ASYNC
:
17246 case OMP_CLAUSE_WAIT
:
17247 OMP_CLAUSE_OPERAND (nc
, 0)
17248 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17249 in_decl
, /*integral_constant_expression_p=*/false);
17251 case OMP_CLAUSE_REDUCTION
:
17252 case OMP_CLAUSE_IN_REDUCTION
:
17253 case OMP_CLAUSE_TASK_REDUCTION
:
17254 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17256 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17257 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17259 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17260 complain
, in_decl
);
17261 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17262 = build_qualified_name (NULL_TREE
, scope
,
17263 TREE_OPERAND (placeholder
, 1),
17267 gcc_assert (identifier_p (placeholder
));
17269 OMP_CLAUSE_DECL (nc
)
17270 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17273 case OMP_CLAUSE_GANG
:
17274 case OMP_CLAUSE_ALIGNED
:
17275 OMP_CLAUSE_DECL (nc
)
17276 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17278 OMP_CLAUSE_OPERAND (nc
, 1)
17279 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17280 in_decl
, /*integral_constant_expression_p=*/false);
17282 case OMP_CLAUSE_LINEAR
:
17283 OMP_CLAUSE_DECL (nc
)
17284 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17286 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17288 gcc_assert (!linear_no_step
);
17289 linear_no_step
= nc
;
17291 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17292 OMP_CLAUSE_LINEAR_STEP (nc
)
17293 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17294 complain
, in_decl
, NULL
);
17296 OMP_CLAUSE_LINEAR_STEP (nc
)
17297 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17299 /*integral_constant_expression_p=*/false);
17301 case OMP_CLAUSE_NOWAIT
:
17302 case OMP_CLAUSE_DEFAULT
:
17303 case OMP_CLAUSE_UNTIED
:
17304 case OMP_CLAUSE_MERGEABLE
:
17305 case OMP_CLAUSE_INBRANCH
:
17306 case OMP_CLAUSE_NOTINBRANCH
:
17307 case OMP_CLAUSE_PROC_BIND
:
17308 case OMP_CLAUSE_FOR
:
17309 case OMP_CLAUSE_PARALLEL
:
17310 case OMP_CLAUSE_SECTIONS
:
17311 case OMP_CLAUSE_TASKGROUP
:
17312 case OMP_CLAUSE_NOGROUP
:
17313 case OMP_CLAUSE_THREADS
:
17314 case OMP_CLAUSE_SIMD
:
17315 case OMP_CLAUSE_DEFAULTMAP
:
17316 case OMP_CLAUSE_ORDER
:
17317 case OMP_CLAUSE_BIND
:
17318 case OMP_CLAUSE_INDEPENDENT
:
17319 case OMP_CLAUSE_AUTO
:
17320 case OMP_CLAUSE_SEQ
:
17321 case OMP_CLAUSE_IF_PRESENT
:
17322 case OMP_CLAUSE_FINALIZE
:
17325 gcc_unreachable ();
17327 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17328 switch (OMP_CLAUSE_CODE (nc
))
17330 case OMP_CLAUSE_SHARED
:
17331 case OMP_CLAUSE_PRIVATE
:
17332 case OMP_CLAUSE_FIRSTPRIVATE
:
17333 case OMP_CLAUSE_LASTPRIVATE
:
17334 case OMP_CLAUSE_COPYPRIVATE
:
17335 case OMP_CLAUSE_LINEAR
:
17336 case OMP_CLAUSE_REDUCTION
:
17337 case OMP_CLAUSE_IN_REDUCTION
:
17338 case OMP_CLAUSE_TASK_REDUCTION
:
17339 case OMP_CLAUSE_USE_DEVICE_PTR
:
17340 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17341 case OMP_CLAUSE_IS_DEVICE_PTR
:
17342 case OMP_CLAUSE_INCLUSIVE
:
17343 case OMP_CLAUSE_EXCLUSIVE
:
17344 /* tsubst_expr on SCOPE_REF results in returning
17345 finish_non_static_data_member result. Undo that here. */
17346 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17347 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17348 == IDENTIFIER_NODE
))
17350 tree t
= OMP_CLAUSE_DECL (nc
);
17353 switch (TREE_CODE (v
))
17355 case COMPONENT_REF
:
17359 case POINTER_PLUS_EXPR
:
17360 v
= TREE_OPERAND (v
, 0);
17363 if (DECL_CONTEXT (v
) == current_function_decl
17364 && DECL_ARTIFICIAL (v
)
17365 && DECL_NAME (v
) == this_identifier
)
17366 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17373 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17374 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17375 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17376 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17377 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17379 tree decl
= OMP_CLAUSE_DECL (nc
);
17382 retrofit_lang_decl (decl
);
17383 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17392 new_clauses
= nreverse (new_clauses
);
17393 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17395 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17396 if (linear_no_step
)
17397 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17398 if (nc
== linear_no_step
)
17400 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17404 return new_clauses
;
17407 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17410 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17413 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17415 tree purpose
, value
, chain
;
17420 if (TREE_CODE (t
) != TREE_LIST
)
17421 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17422 /*function_p=*/false,
17423 /*integral_constant_expression_p=*/false);
17425 if (t
== void_list_node
)
17428 purpose
= TREE_PURPOSE (t
);
17430 purpose
= RECUR (purpose
);
17431 value
= TREE_VALUE (t
);
17434 if (TREE_CODE (value
) != LABEL_DECL
)
17435 value
= RECUR (value
);
17438 value
= lookup_label (DECL_NAME (value
));
17439 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17440 TREE_USED (value
) = 1;
17443 chain
= TREE_CHAIN (t
);
17444 if (chain
&& chain
!= void_type_node
)
17445 chain
= RECUR (chain
);
17446 return tree_cons (purpose
, value
, chain
);
17450 /* Used to temporarily communicate the list of #pragma omp parallel
17451 clauses to #pragma omp for instantiation if they are combined
17454 static tree
*omp_parallel_combined_clauses
;
17456 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17457 tree
*, unsigned int *);
17459 /* Substitute one OMP_FOR iterator. */
17462 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17463 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17464 tree args
, tsubst_flags_t complain
, tree in_decl
,
17465 bool integral_constant_expression_p
)
17467 #define RECUR(NODE) \
17468 tsubst_expr ((NODE), args, complain, in_decl, \
17469 integral_constant_expression_p)
17470 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17473 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17474 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17476 decl
= TREE_OPERAND (init
, 0);
17477 init
= TREE_OPERAND (init
, 1);
17478 tree decl_expr
= NULL_TREE
;
17479 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17482 bool decomp
= false;
17483 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17485 tree v
= DECL_VALUE_EXPR (decl
);
17486 if (TREE_CODE (v
) == ARRAY_REF
17487 && VAR_P (TREE_OPERAND (v
, 0))
17488 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17490 tree decomp_first
= NULL_TREE
;
17491 unsigned decomp_cnt
= 0;
17492 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17493 maybe_push_decl (d
);
17494 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17495 in_decl
, &decomp_first
, &decomp_cnt
);
17497 if (d
== error_mark_node
)
17498 decl
= error_mark_node
;
17500 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17502 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17504 tree v
= build_nt (ARRAY_REF
, d
,
17505 size_int (decomp_cnt
- i
- 1),
17506 NULL_TREE
, NULL_TREE
);
17507 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17508 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17510 fit_decomposition_lang_decl (decomp_first
, d
);
17511 decomp_first
= DECL_CHAIN (decomp_first
);
17515 decl
= tsubst_decl (decl
, args
, complain
);
17517 maybe_push_decl (decl
);
17519 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17521 /* We need to jump through some hoops to handle declarations in the
17522 init-statement, since we might need to handle auto deduction,
17523 but we need to keep control of initialization. */
17525 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17526 decl
= tsubst_decl (decl
, args
, complain
);
17530 if (TREE_CODE (decl
) == SCOPE_REF
)
17532 decl
= RECUR (decl
);
17533 if (TREE_CODE (decl
) == COMPONENT_REF
)
17537 switch (TREE_CODE (v
))
17539 case COMPONENT_REF
:
17543 case POINTER_PLUS_EXPR
:
17544 v
= TREE_OPERAND (v
, 0);
17547 if (DECL_CONTEXT (v
) == current_function_decl
17548 && DECL_ARTIFICIAL (v
)
17549 && DECL_NAME (v
) == this_identifier
)
17551 decl
= TREE_OPERAND (decl
, 1);
17552 decl
= omp_privatize_field (decl
, false);
17562 decl
= RECUR (decl
);
17564 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17566 init
= copy_node (init
);
17567 TREE_VEC_ELT (init
, 0)
17568 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17569 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17570 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17573 init
= RECUR (init
);
17575 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17577 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17578 if (TREE_CODE (o
) == TREE_LIST
)
17579 TREE_VEC_ELT (orig_declv
, i
)
17580 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17581 RECUR (TREE_VALUE (o
)),
17584 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17589 tree this_pre_body
= NULL_TREE
;
17590 tree orig_init
= NULL_TREE
;
17591 tree orig_decl
= NULL_TREE
;
17592 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17593 orig_init
, cond
, incr
);
17596 if (orig_declv
== NULL_TREE
)
17597 orig_declv
= copy_node (declv
);
17598 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17601 else if (orig_declv
)
17602 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17605 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17606 if (!range_for
&& auto_node
&& init
)
17608 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17610 gcc_assert (!type_dependent_expression_p (decl
));
17612 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17616 /* Declare the variable, but don't let that initialize it. */
17617 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17618 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17620 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17625 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17626 if (COMPARISON_CLASS_P (cond
)
17627 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17629 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17630 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17631 TREE_VEC_ELT (rhs
, 0)
17632 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17633 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17634 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17635 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17639 cond
= RECUR (cond
);
17640 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17641 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17643 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17644 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17645 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17646 NOP_EXPR
, rhs
, complain
);
17649 incr
= RECUR (incr
);
17650 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17651 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17653 TREE_VEC_ELT (declv
, i
) = decl
;
17654 TREE_VEC_ELT (initv
, i
) = init
;
17655 TREE_VEC_ELT (condv
, i
) = cond
;
17656 TREE_VEC_ELT (incrv
, i
) = incr
;
17662 /* Declare and initialize the variable. */
17670 for (j
= ((omp_parallel_combined_clauses
== NULL
17671 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17673 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17675 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17676 && OMP_CLAUSE_DECL (*pc
) == decl
)
17678 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17679 && OMP_CLAUSE_DECL (*pc
) == decl
)
17683 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17685 *pc
= OMP_CLAUSE_CHAIN (c
);
17686 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17689 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17690 && OMP_CLAUSE_DECL (*pc
) == decl
)
17692 error ("iteration variable %qD should not be firstprivate",
17694 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17696 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17697 && OMP_CLAUSE_DECL (*pc
) == decl
)
17699 error ("iteration variable %qD should not be reduction",
17701 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17704 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17709 if (*pc
== NULL_TREE
)
17711 tree c
= build_omp_clause (input_location
,
17712 TREE_CODE (t
) == OMP_LOOP
17713 ? OMP_CLAUSE_LASTPRIVATE
17714 : OMP_CLAUSE_PRIVATE
);
17715 OMP_CLAUSE_DECL (c
) = decl
;
17716 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17719 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17724 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17725 if (COMPARISON_CLASS_P (cond
))
17727 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17728 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17729 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17732 cond
= RECUR (cond
);
17733 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17734 switch (TREE_CODE (incr
))
17736 case PREINCREMENT_EXPR
:
17737 case PREDECREMENT_EXPR
:
17738 case POSTINCREMENT_EXPR
:
17739 case POSTDECREMENT_EXPR
:
17740 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17741 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17744 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17745 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17747 tree rhs
= TREE_OPERAND (incr
, 1);
17748 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17749 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17750 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17751 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17752 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17756 incr
= RECUR (incr
);
17759 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17760 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17762 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17763 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17764 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17765 TREE_TYPE (decl
), lhs
,
17766 RECUR (TREE_OPERAND (incr
, 2))));
17768 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17769 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17770 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17772 tree rhs
= TREE_OPERAND (incr
, 2);
17773 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17774 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17775 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17776 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17777 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17781 incr
= RECUR (incr
);
17784 incr
= RECUR (incr
);
17788 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17789 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17790 TREE_VEC_ELT (declv
, i
) = decl
;
17791 TREE_VEC_ELT (initv
, i
) = init
;
17792 TREE_VEC_ELT (condv
, i
) = cond
;
17793 TREE_VEC_ELT (incrv
, i
) = incr
;
17798 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17799 of OMP_TARGET's body. */
17802 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17804 *walk_subtrees
= 0;
17805 switch (TREE_CODE (*tp
))
17810 case STATEMENT_LIST
:
17811 *walk_subtrees
= 1;
17819 /* Helper function for tsubst_expr. For decomposition declaration
17820 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17821 also the corresponding decls representing the identifiers
17822 of the decomposition declaration. Return DECL if successful
17823 or error_mark_node otherwise, set *FIRST to the first decl
17824 in the list chained through DECL_CHAIN and *CNT to the number
17828 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17829 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17832 tree decl2
, decl3
, prev
= decl
;
17834 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17835 for (decl2
= DECL_CHAIN (pattern_decl
);
17838 && DECL_DECOMPOSITION_P (decl2
)
17839 && DECL_NAME (decl2
);
17840 decl2
= DECL_CHAIN (decl2
))
17842 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17844 gcc_assert (errorcount
);
17845 return error_mark_node
;
17848 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17849 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17850 tree v
= DECL_VALUE_EXPR (decl2
);
17851 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17852 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17853 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17854 SET_DECL_VALUE_EXPR (decl2
, v
);
17855 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17857 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17860 gcc_assert (errorcount
);
17861 decl
= error_mark_node
;
17864 maybe_push_decl (decl3
);
17865 if (error_operand_p (decl3
))
17866 decl
= error_mark_node
;
17867 else if (decl
!= error_mark_node
17868 && DECL_CHAIN (decl3
) != prev
17871 gcc_assert (errorcount
);
17872 decl
= error_mark_node
;
17881 /* Return the proper local_specialization for init-capture pack DECL. */
17884 lookup_init_capture_pack (tree decl
)
17886 /* We handle normal pack captures by forwarding to the specialization of the
17887 captured parameter. We can't do that for pack init-captures; we need them
17888 to have their own local_specialization. We created the individual
17889 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17890 when we process the DECL_EXPR for the pack init-capture in the template.
17891 So, how do we find them? We don't know the capture proxy pack when
17892 building the individual resulting proxies, and we don't know the
17893 individual proxies when instantiating the pack. What we have in common is
17896 So...when we instantiate the FIELD_DECL, we stick the result in
17897 local_specializations. Then at the DECL_EXPR we look up that result, see
17898 how many elements it has, synthesize the names, and look them up. */
17900 tree cname
= DECL_NAME (decl
);
17901 tree val
= DECL_VALUE_EXPR (decl
);
17902 tree field
= TREE_OPERAND (val
, 1);
17903 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17904 tree fpack
= retrieve_local_specialization (field
);
17905 if (fpack
== error_mark_node
)
17906 return error_mark_node
;
17909 tree vec
= NULL_TREE
;
17910 tree r
= NULL_TREE
;
17911 if (TREE_CODE (fpack
) == TREE_VEC
)
17913 len
= TREE_VEC_LENGTH (fpack
);
17914 vec
= make_tree_vec (len
);
17915 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17916 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17918 for (int i
= 0; i
< len
; ++i
)
17920 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17921 tree elt
= lookup_name (ename
);
17923 TREE_VEC_ELT (vec
, i
) = elt
;
17930 /* Like tsubst_copy for expressions, etc. but also does semantic
17934 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17935 bool integral_constant_expression_p
)
17937 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17938 #define RECUR(NODE) \
17939 tsubst_expr ((NODE), args, complain, in_decl, \
17940 integral_constant_expression_p)
17946 if (t
== NULL_TREE
|| t
== error_mark_node
)
17949 loc
= input_location
;
17950 if (location_t eloc
= cp_expr_location (t
))
17951 input_location
= eloc
;
17952 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17953 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17955 switch (TREE_CODE (t
))
17957 case STATEMENT_LIST
:
17959 tree_stmt_iterator i
;
17960 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17961 RECUR (tsi_stmt (i
));
17965 case CTOR_INITIALIZER
:
17966 finish_mem_initializers (tsubst_initializer_list
17967 (TREE_OPERAND (t
, 0), args
));
17971 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17974 case CO_RETURN_EXPR
:
17975 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
17978 case CO_YIELD_EXPR
:
17979 stmt
= finish_co_yield_expr (input_location
,
17980 RECUR (TREE_OPERAND (t
, 0)));
17984 case CO_AWAIT_EXPR
:
17985 stmt
= finish_co_await_expr (input_location
,
17986 RECUR (TREE_OPERAND (t
, 0)));
17991 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17992 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17993 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17995 finish_expr_stmt (tmp
);
17999 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18004 tree decl
, pattern_decl
;
18007 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18008 if (TREE_CODE (decl
) == LABEL_DECL
)
18009 finish_label_decl (DECL_NAME (decl
));
18010 else if (TREE_CODE (decl
) == USING_DECL
)
18012 tree scope
= USING_DECL_SCOPE (decl
);
18013 tree name
= DECL_NAME (decl
);
18015 scope
= tsubst (scope
, args
, complain
, in_decl
);
18016 finish_nonmember_using_decl (scope
, name
);
18018 else if (is_capture_proxy (decl
)
18019 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18021 /* We're in tsubst_lambda_expr, we've already inserted a new
18022 capture proxy, so look it up and register it. */
18024 if (!DECL_PACK_P (decl
))
18026 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18027 LOOK_want::HIDDEN_LAMBDA
);
18028 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18030 else if (is_normal_capture_proxy (decl
))
18032 inst
= (retrieve_local_specialization
18033 (DECL_CAPTURED_VARIABLE (decl
)));
18034 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18035 || DECL_PACK_P (inst
));
18038 inst
= lookup_init_capture_pack (decl
);
18040 register_local_specialization (inst
, decl
);
18043 else if (DECL_PRETTY_FUNCTION_P (decl
))
18044 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18046 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18047 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18048 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18049 /* Don't copy the old closure; we'll create a new one in
18050 tsubst_lambda_expr. */
18054 init
= DECL_INITIAL (decl
);
18055 decl
= tsubst (decl
, args
, complain
, in_decl
);
18056 if (decl
!= error_mark_node
)
18058 /* By marking the declaration as instantiated, we avoid
18059 trying to instantiate it. Since instantiate_decl can't
18060 handle local variables, and since we've already done
18061 all that needs to be done, that's the right thing to
18064 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18065 if (VAR_P (decl
) && !DECL_NAME (decl
)
18066 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18067 /* Anonymous aggregates are a special case. */
18068 finish_anon_union (decl
);
18069 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18071 DECL_CONTEXT (decl
) = current_function_decl
;
18072 if (DECL_NAME (decl
) == this_identifier
)
18074 tree lam
= DECL_CONTEXT (current_function_decl
);
18075 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18076 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18078 insert_capture_proxy (decl
);
18080 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18081 /* We already did a pushtag. */;
18082 else if (TREE_CODE (decl
) == FUNCTION_DECL
18083 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18084 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
18086 /* We pretend this is regular local extern decl of
18087 a namespace-scope fn. Then we make it really
18088 local, it is a nested function. */
18089 gcc_checking_assert (DECL_LOCAL_DECL_P (decl
));
18090 DECL_CONTEXT (decl
) = global_namespace
;
18092 DECL_CONTEXT (decl
) = current_function_decl
;
18093 if (cp_check_omp_declare_reduction (decl
))
18094 instantiate_body (pattern_decl
, args
, decl
, true);
18098 bool const_init
= false;
18099 unsigned int cnt
= 0;
18100 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18101 maybe_push_decl (decl
);
18104 && DECL_DECOMPOSITION_P (decl
)
18105 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18106 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18107 complain
, in_decl
, &first
,
18110 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18113 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18116 if (ndecl
!= error_mark_node
)
18117 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18119 /* In a non-template function, VLA type declarations are
18120 handled in grokdeclarator; for templates, handle them
18122 predeclare_vla (decl
);
18124 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
18126 if (ndecl
!= error_mark_node
)
18127 cp_finish_decomp (ndecl
, first
, cnt
);
18136 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18137 RECUR (FOR_INIT_STMT (t
));
18138 finish_init_stmt (stmt
);
18139 tmp
= RECUR (FOR_COND (t
));
18140 finish_for_cond (tmp
, stmt
, false, 0);
18141 tmp
= RECUR (FOR_EXPR (t
));
18142 finish_for_expr (tmp
, stmt
);
18144 bool prev
= note_iteration_stmt_body_start ();
18145 RECUR (FOR_BODY (t
));
18146 note_iteration_stmt_body_end (prev
);
18148 finish_for_stmt (stmt
);
18151 case RANGE_FOR_STMT
:
18153 /* Construct another range_for, if this is not a final
18154 substitution (for inside a generic lambda of a
18155 template). Otherwise convert to a regular for. */
18157 stmt
= (processing_template_decl
18158 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18159 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18160 RECUR (RANGE_FOR_INIT_STMT (t
));
18161 decl
= RANGE_FOR_DECL (t
);
18162 decl
= tsubst (decl
, args
, complain
, in_decl
);
18163 maybe_push_decl (decl
);
18164 expr
= RECUR (RANGE_FOR_EXPR (t
));
18166 tree decomp_first
= NULL_TREE
;
18167 unsigned decomp_cnt
= 0;
18168 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18169 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18171 &decomp_first
, &decomp_cnt
);
18173 if (processing_template_decl
)
18175 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18176 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18177 finish_range_for_decl (stmt
, decl
, expr
);
18178 if (decomp_first
&& decl
!= error_mark_node
)
18179 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18183 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18184 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18185 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18186 decomp_first
, decomp_cnt
,
18187 RANGE_FOR_IVDEP (t
), unroll
);
18190 bool prev
= note_iteration_stmt_body_start ();
18191 RECUR (RANGE_FOR_BODY (t
));
18192 note_iteration_stmt_body_end (prev
);
18193 finish_for_stmt (stmt
);
18198 stmt
= begin_while_stmt ();
18199 tmp
= RECUR (WHILE_COND (t
));
18200 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18202 bool prev
= note_iteration_stmt_body_start ();
18203 RECUR (WHILE_BODY (t
));
18204 note_iteration_stmt_body_end (prev
);
18206 finish_while_stmt (stmt
);
18210 stmt
= begin_do_stmt ();
18212 bool prev
= note_iteration_stmt_body_start ();
18213 RECUR (DO_BODY (t
));
18214 note_iteration_stmt_body_end (prev
);
18216 finish_do_body (stmt
);
18217 tmp
= RECUR (DO_COND (t
));
18218 finish_do_stmt (tmp
, stmt
, false, 0);
18222 stmt
= begin_if_stmt ();
18223 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18224 if (IF_STMT_CONSTEXPR_P (t
))
18225 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18226 tmp
= RECUR (IF_COND (t
));
18227 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18228 if (IF_STMT_CONSTEXPR_P (t
)
18229 && instantiation_dependent_expression_p (tmp
))
18231 /* We're partially instantiating a generic lambda, but the condition
18232 of the constexpr if is still dependent. Don't substitute into the
18233 branches now, just remember the template arguments. */
18234 do_poplevel (IF_SCOPE (stmt
));
18235 IF_COND (stmt
) = IF_COND (t
);
18236 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18237 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18238 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18242 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18243 /* Don't instantiate the THEN_CLAUSE. */;
18246 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18247 bool inhibit
= integer_zerop (folded
);
18249 ++c_inhibit_evaluation_warnings
;
18250 RECUR (THEN_CLAUSE (t
));
18252 --c_inhibit_evaluation_warnings
;
18254 finish_then_clause (stmt
);
18256 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18257 /* Don't instantiate the ELSE_CLAUSE. */;
18258 else if (ELSE_CLAUSE (t
))
18260 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18261 bool inhibit
= integer_nonzerop (folded
);
18262 begin_else_clause (stmt
);
18264 ++c_inhibit_evaluation_warnings
;
18265 RECUR (ELSE_CLAUSE (t
));
18267 --c_inhibit_evaluation_warnings
;
18268 finish_else_clause (stmt
);
18271 finish_if_stmt (stmt
);
18275 if (BIND_EXPR_BODY_BLOCK (t
))
18276 stmt
= begin_function_body ();
18278 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18279 ? BCS_TRY_BLOCK
: 0);
18281 RECUR (BIND_EXPR_BODY (t
));
18283 if (BIND_EXPR_BODY_BLOCK (t
))
18284 finish_function_body (stmt
);
18286 finish_compound_stmt (stmt
);
18290 finish_break_stmt ();
18293 case CONTINUE_STMT
:
18294 finish_continue_stmt ();
18298 stmt
= begin_switch_stmt ();
18299 tmp
= RECUR (SWITCH_STMT_COND (t
));
18300 finish_switch_cond (tmp
, stmt
);
18301 RECUR (SWITCH_STMT_BODY (t
));
18302 finish_switch_stmt (stmt
);
18305 case CASE_LABEL_EXPR
:
18307 tree decl
= CASE_LABEL (t
);
18308 tree low
= RECUR (CASE_LOW (t
));
18309 tree high
= RECUR (CASE_HIGH (t
));
18310 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18311 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18313 tree label
= CASE_LABEL (l
);
18314 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18315 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18316 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18323 tree decl
= LABEL_EXPR_LABEL (t
);
18326 label
= finish_label_stmt (DECL_NAME (decl
));
18327 if (TREE_CODE (label
) == LABEL_DECL
)
18328 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18329 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18330 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18335 tmp
= GOTO_DESTINATION (t
);
18336 if (TREE_CODE (tmp
) != LABEL_DECL
)
18337 /* Computed goto's must be tsubst'd into. On the other hand,
18338 non-computed gotos must not be; the identifier in question
18339 will have no binding. */
18342 tmp
= DECL_NAME (tmp
);
18343 finish_goto_stmt (tmp
);
18348 tree string
= RECUR (ASM_STRING (t
));
18349 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18350 complain
, in_decl
);
18351 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18352 complain
, in_decl
);
18353 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18354 complain
, in_decl
);
18355 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18356 complain
, in_decl
);
18357 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18358 outputs
, inputs
, clobbers
, labels
,
18360 tree asm_expr
= tmp
;
18361 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18362 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18363 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18370 stmt
= begin_try_block ();
18371 RECUR (TRY_STMTS (t
));
18372 finish_cleanup_try_block (stmt
);
18373 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18377 tree compound_stmt
= NULL_TREE
;
18379 if (FN_TRY_BLOCK_P (t
))
18380 stmt
= begin_function_try_block (&compound_stmt
);
18382 stmt
= begin_try_block ();
18384 RECUR (TRY_STMTS (t
));
18386 if (FN_TRY_BLOCK_P (t
))
18387 finish_function_try_block (stmt
);
18389 finish_try_block (stmt
);
18391 RECUR (TRY_HANDLERS (t
));
18392 if (FN_TRY_BLOCK_P (t
))
18393 finish_function_handler_sequence (stmt
, compound_stmt
);
18395 finish_handler_sequence (stmt
);
18401 tree decl
= HANDLER_PARMS (t
);
18405 decl
= tsubst (decl
, args
, complain
, in_decl
);
18406 /* Prevent instantiate_decl from trying to instantiate
18407 this variable. We've already done all that needs to be
18409 if (decl
!= error_mark_node
)
18410 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18412 stmt
= begin_handler ();
18413 finish_handler_parms (decl
, stmt
);
18414 RECUR (HANDLER_BODY (t
));
18415 finish_handler (stmt
);
18420 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18421 if (CLASS_TYPE_P (tmp
))
18423 /* Local classes are not independent templates; they are
18424 instantiated along with their containing function. And this
18425 way we don't have to deal with pushing out of one local class
18426 to instantiate a member of another local class. */
18427 /* Closures are handled by the LAMBDA_EXPR. */
18428 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18429 complete_type (tmp
);
18430 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18432 || (TREE_CODE (fld
) == FUNCTION_DECL
18433 && !DECL_ARTIFICIAL (fld
)))
18434 && DECL_TEMPLATE_INSTANTIATION (fld
))
18435 instantiate_decl (fld
, /*defer_ok=*/false,
18436 /*expl_inst_class=*/false);
18440 case STATIC_ASSERT
:
18444 ++c_inhibit_evaluation_warnings
;
18446 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18449 /*integral_constant_expression_p=*/true);
18450 --c_inhibit_evaluation_warnings
;
18452 finish_static_assert (condition
,
18453 STATIC_ASSERT_MESSAGE (t
),
18454 STATIC_ASSERT_SOURCE_LOCATION (t
),
18455 /*member_p=*/false);
18460 case OACC_PARALLEL
:
18462 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18464 stmt
= begin_omp_parallel ();
18465 RECUR (OMP_BODY (t
));
18466 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18470 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18471 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18472 complain
, in_decl
);
18473 if (OMP_PARALLEL_COMBINED (t
))
18474 omp_parallel_combined_clauses
= &tmp
;
18475 stmt
= begin_omp_parallel ();
18476 RECUR (OMP_PARALLEL_BODY (t
));
18477 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18478 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18479 = OMP_PARALLEL_COMBINED (t
);
18480 pop_omp_privatization_clauses (r
);
18484 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18486 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18487 complain
, in_decl
);
18489 OMP_TASK_CLAUSES (t
) = tmp
;
18493 r
= push_omp_privatization_clauses (false);
18494 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18495 complain
, in_decl
);
18496 stmt
= begin_omp_task ();
18497 RECUR (OMP_TASK_BODY (t
));
18498 finish_omp_task (tmp
, stmt
);
18499 pop_omp_privatization_clauses (r
);
18505 case OMP_DISTRIBUTE
:
18509 tree clauses
, body
, pre_body
;
18510 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18511 tree orig_declv
= NULL_TREE
;
18512 tree incrv
= NULL_TREE
;
18513 enum c_omp_region_type ort
= C_ORT_OMP
;
18514 bool any_range_for
= false;
18517 if (TREE_CODE (t
) == OACC_LOOP
)
18520 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18521 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18523 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18525 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18526 if (OMP_FOR_ORIG_DECLS (t
))
18527 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18528 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18529 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18530 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18533 keep_next_level (true);
18534 stmt
= begin_omp_structured_block ();
18536 pre_body
= push_stmt_list ();
18537 RECUR (OMP_FOR_PRE_BODY (t
));
18538 pre_body
= pop_stmt_list (pre_body
);
18540 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18541 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18543 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18544 condv
, incrv
, &clauses
, args
,
18546 integral_constant_expression_p
);
18547 omp_parallel_combined_clauses
= NULL
;
18551 gcc_assert (orig_declv
);
18552 body
= begin_omp_structured_block ();
18553 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18554 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18555 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18556 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18557 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18558 TREE_VEC_ELT (declv
, i
));
18561 body
= push_stmt_list ();
18562 RECUR (OMP_FOR_BODY (t
));
18564 body
= finish_omp_structured_block (body
);
18566 body
= pop_stmt_list (body
);
18568 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18569 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18570 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18574 t
= make_node (TREE_CODE (t
));
18575 TREE_TYPE (t
) = void_type_node
;
18576 OMP_FOR_BODY (t
) = body
;
18577 OMP_FOR_PRE_BODY (t
) = pre_body
;
18578 OMP_FOR_CLAUSES (t
) = clauses
;
18579 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18583 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18585 pop_omp_privatization_clauses (r
);
18590 omp_parallel_combined_clauses
= NULL
;
18595 case OMP_TASKGROUP
:
18597 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18598 && OMP_TEAMS_COMBINED (t
));
18599 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18601 if (TREE_CODE (t
) == OMP_TEAMS
)
18603 keep_next_level (true);
18604 stmt
= begin_omp_structured_block ();
18605 RECUR (OMP_BODY (t
));
18606 stmt
= finish_omp_structured_block (stmt
);
18610 stmt
= push_stmt_list ();
18611 RECUR (OMP_BODY (t
));
18612 stmt
= pop_stmt_list (stmt
);
18615 if (TREE_CODE (t
) == OMP_CRITICAL
18616 && tmp
!= NULL_TREE
18617 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18619 error_at (OMP_CLAUSE_LOCATION (tmp
),
18620 "%<#pragma omp critical%> with %<hint%> clause requires "
18621 "a name, except when %<omp_sync_hint_none%> is used");
18622 RETURN (error_mark_node
);
18625 OMP_BODY (t
) = stmt
;
18626 OMP_CLAUSES (t
) = tmp
;
18628 pop_omp_privatization_clauses (r
);
18632 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18633 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18635 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18636 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18638 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18639 args
, complain
, in_decl
);
18640 if (tmp
== NULL_TREE
)
18641 tmp
= error_mark_node
;
18645 kind
= (enum omp_clause_depend_kind
)
18646 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18649 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18652 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18653 OMP_CLAUSE_DEPEND_SOURCE
,
18654 OMP_DEPOBJ_CLAUSES (t
));
18658 case OMP_TARGET_DATA
:
18660 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18661 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18663 keep_next_level (true);
18664 stmt
= begin_omp_structured_block ();
18666 RECUR (OMP_BODY (t
));
18667 stmt
= finish_omp_structured_block (stmt
);
18670 OMP_BODY (t
) = stmt
;
18671 OMP_CLAUSES (t
) = tmp
;
18672 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18674 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18677 /* For combined target teams, ensure the num_teams and
18678 thread_limit clause expressions are evaluated on the host,
18679 before entering the target construct. */
18681 for (c
= OMP_TEAMS_CLAUSES (teams
);
18682 c
; c
= OMP_CLAUSE_CHAIN (c
))
18683 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18684 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18685 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18687 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18688 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18689 if (expr
== error_mark_node
)
18691 tmp
= TARGET_EXPR_SLOT (expr
);
18693 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18694 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18695 OMP_CLAUSE_FIRSTPRIVATE
);
18696 OMP_CLAUSE_DECL (tc
) = tmp
;
18697 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18698 OMP_TARGET_CLAUSES (t
) = tc
;
18707 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18708 complain
, in_decl
);
18709 OACC_DECLARE_CLAUSES (t
) = tmp
;
18713 case OMP_TARGET_UPDATE
:
18714 case OMP_TARGET_ENTER_DATA
:
18715 case OMP_TARGET_EXIT_DATA
:
18716 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18717 complain
, in_decl
);
18719 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18723 case OACC_ENTER_DATA
:
18724 case OACC_EXIT_DATA
:
18726 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18727 complain
, in_decl
);
18729 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18734 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18735 complain
, in_decl
);
18736 stmt
= push_stmt_list ();
18737 RECUR (OMP_BODY (t
));
18738 stmt
= pop_stmt_list (stmt
);
18741 OMP_BODY (t
) = stmt
;
18742 OMP_ORDERED_CLAUSES (t
) = tmp
;
18747 omp_parallel_combined_clauses
= NULL
;
18750 stmt
= push_stmt_list ();
18751 RECUR (OMP_BODY (t
));
18752 stmt
= pop_stmt_list (stmt
);
18755 OMP_BODY (t
) = stmt
;
18760 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18762 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18763 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18764 complain
, in_decl
);
18765 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18767 tree op1
= TREE_OPERAND (t
, 1);
18768 tree rhs1
= NULL_TREE
;
18770 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18772 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18773 op1
= TREE_OPERAND (op1
, 1);
18775 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18776 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18777 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18778 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18779 OMP_ATOMIC_MEMORY_ORDER (t
));
18783 tree op1
= TREE_OPERAND (t
, 1);
18784 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18785 tree rhs1
= NULL_TREE
;
18786 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18787 enum tree_code opcode
= NOP_EXPR
;
18788 if (code
== OMP_ATOMIC_READ
)
18790 v
= RECUR (TREE_OPERAND (op1
, 0));
18791 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18793 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18794 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18796 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18797 v
= RECUR (TREE_OPERAND (op1
, 0));
18798 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18799 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18801 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18802 op11
= TREE_OPERAND (op11
, 1);
18804 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18805 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18806 opcode
= TREE_CODE (op11
);
18807 if (opcode
== MODIFY_EXPR
)
18813 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18814 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18816 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18817 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18821 case TRANSACTION_EXPR
:
18824 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18825 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18827 if (TRANSACTION_EXPR_IS_STMT (t
))
18829 tree body
= TRANSACTION_EXPR_BODY (t
);
18830 tree noex
= NULL_TREE
;
18831 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18833 noex
= MUST_NOT_THROW_COND (body
);
18834 if (noex
== NULL_TREE
)
18835 noex
= boolean_true_node
;
18836 body
= TREE_OPERAND (body
, 0);
18838 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18840 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18844 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18845 RECUR (TRANSACTION_EXPR_BODY (t
)),
18852 case MUST_NOT_THROW_EXPR
:
18854 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18855 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18856 RETURN (build_must_not_throw_expr (op0
, cond
));
18859 case EXPR_PACK_EXPANSION
:
18860 error ("invalid use of pack expansion expression");
18861 RETURN (error_mark_node
);
18863 case NONTYPE_ARGUMENT_PACK
:
18864 error ("use %<...%> to expand argument pack");
18865 RETURN (error_mark_node
);
18867 case COMPOUND_EXPR
:
18868 tmp
= RECUR (TREE_OPERAND (t
, 0));
18869 if (tmp
== NULL_TREE
)
18870 /* If the first operand was a statement, we're done with it. */
18871 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18872 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18873 RECUR (TREE_OPERAND (t
, 1)),
18876 case ANNOTATE_EXPR
:
18877 tmp
= RECUR (TREE_OPERAND (t
, 0));
18878 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18879 TREE_TYPE (tmp
), tmp
,
18880 RECUR (TREE_OPERAND (t
, 1)),
18881 RECUR (TREE_OPERAND (t
, 2))));
18884 RETURN (add_stmt (copy_node (t
)));
18887 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18889 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18890 /*function_p=*/false,
18891 integral_constant_expression_p
));
18894 RETURN (NULL_TREE
);
18896 input_location
= loc
;
18902 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18903 function. For description of the body see comment above
18904 cp_parser_omp_declare_reduction_exprs. */
18907 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18909 if (t
== NULL_TREE
|| t
== error_mark_node
)
18912 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
18914 tree_stmt_iterator tsi
;
18917 memset (stmts
, 0, sizeof stmts
);
18918 for (i
= 0, tsi
= tsi_start (t
);
18919 i
< 7 && !tsi_end_p (tsi
);
18920 i
++, tsi_next (&tsi
))
18921 stmts
[i
] = tsi_stmt (tsi
);
18922 gcc_assert (tsi_end_p (tsi
));
18926 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18927 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18928 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18929 args
, complain
, in_decl
);
18930 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18931 args
, complain
, in_decl
);
18932 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18933 expect to be pushing it. */
18934 DECL_CONTEXT (omp_out
) = current_function_decl
;
18935 DECL_CONTEXT (omp_in
) = current_function_decl
;
18936 keep_next_level (true);
18937 tree block
= begin_omp_structured_block ();
18938 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18939 block
= finish_omp_structured_block (block
);
18940 block
= maybe_cleanup_point_expr_void (block
);
18941 add_decl_expr (omp_out
);
18942 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18943 TREE_NO_WARNING (omp_out
) = 1;
18944 add_decl_expr (omp_in
);
18945 finish_expr_stmt (block
);
18949 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18950 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18951 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18952 args
, complain
, in_decl
);
18953 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18954 args
, complain
, in_decl
);
18955 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18956 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18957 keep_next_level (true);
18958 tree block
= begin_omp_structured_block ();
18959 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18960 block
= finish_omp_structured_block (block
);
18961 block
= maybe_cleanup_point_expr_void (block
);
18962 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18963 add_decl_expr (omp_priv
);
18964 add_decl_expr (omp_orig
);
18965 finish_expr_stmt (block
);
18967 add_decl_expr (omp_orig
);
18971 /* T is a postfix-expression that is not being used in a function
18972 call. Return the substituted version of T. */
18975 tsubst_non_call_postfix_expression (tree t
, tree args
,
18976 tsubst_flags_t complain
,
18979 if (TREE_CODE (t
) == SCOPE_REF
)
18980 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18981 /*done=*/false, /*address_p=*/false);
18983 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18984 /*function_p=*/false,
18985 /*integral_constant_expression_p=*/false);
18990 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18991 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18992 dependent init-capture. */
18995 prepend_one_capture (tree field
, tree init
, tree
&list
,
18996 tsubst_flags_t complain
)
18998 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19000 tree type
= NULL_TREE
;
19003 if (complain
& tf_error
)
19004 error ("empty initializer in lambda init-capture");
19005 init
= error_mark_node
;
19007 else if (TREE_CODE (init
) == TREE_LIST
)
19008 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19010 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19011 TREE_TYPE (field
) = type
;
19012 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19014 list
= tree_cons (field
, init
, list
);
19017 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19018 instantiation context. Instantiating a pack expansion containing a lambda
19019 might result in multiple lambdas all based on the same lambda in the
19023 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19025 tree oldfn
= lambda_function (t
);
19028 tree r
= build_lambda_expr ();
19030 LAMBDA_EXPR_LOCATION (r
)
19031 = LAMBDA_EXPR_LOCATION (t
);
19032 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19033 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19034 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19035 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19037 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19038 /* A lambda in a default argument outside a class gets no
19039 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19040 tsubst_default_argument calls start_lambda_scope, so we need to
19041 specifically ignore it here, and use the global scope. */
19042 record_null_lambda_scope (r
);
19044 record_lambda_scope (r
);
19046 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19047 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19049 vec
<tree
,va_gc
>* field_packs
= NULL
;
19051 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19052 cap
= TREE_CHAIN (cap
))
19054 tree ofield
= TREE_PURPOSE (cap
);
19055 tree init
= TREE_VALUE (cap
);
19056 if (PACK_EXPANSION_P (init
))
19057 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19059 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19060 /*fn*/false, /*constexpr*/false);
19062 if (init
== error_mark_node
)
19063 return error_mark_node
;
19065 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19066 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19068 if (!processing_template_decl
19069 && init
&& TREE_CODE (init
) != TREE_VEC
19070 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19072 /* For a VLA, simply tsubsting the field type won't work, we need to
19073 go through add_capture again. XXX do we want to do this for all
19075 tree name
= (get_identifier
19076 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19077 tree ftype
= TREE_TYPE (ofield
);
19078 bool by_ref
= (TYPE_REF_P (ftype
)
19079 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19080 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19081 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19085 if (PACK_EXPANSION_P (ofield
))
19086 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19087 tree field
= tsubst_decl (ofield
, args
, complain
);
19089 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19091 /* Remember these for when we've pushed local_specializations. */
19092 vec_safe_push (field_packs
, ofield
);
19093 vec_safe_push (field_packs
, field
);
19096 if (field
== error_mark_node
)
19097 return error_mark_node
;
19099 if (TREE_CODE (field
) == TREE_VEC
)
19101 int len
= TREE_VEC_LENGTH (field
);
19102 gcc_assert (TREE_CODE (init
) == TREE_VEC
19103 && TREE_VEC_LENGTH (init
) == len
);
19104 for (int i
= 0; i
< len
; ++i
)
19105 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19106 TREE_VEC_ELT (init
, i
),
19107 LAMBDA_EXPR_CAPTURE_LIST (r
),
19112 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19115 if (id_equal (DECL_NAME (field
), "__this"))
19116 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19120 tree type
= begin_lambda_type (r
);
19121 if (type
== error_mark_node
)
19122 return error_mark_node
;
19124 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19125 determine_visibility (TYPE_NAME (type
));
19127 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19129 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19130 ? DECL_TI_TEMPLATE (oldfn
)
19133 tree fntype
= static_fn_type (oldfn
);
19135 ++processing_template_decl
;
19136 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19138 --processing_template_decl
;
19140 if (fntype
== error_mark_node
)
19141 r
= error_mark_node
;
19144 /* The body of a lambda-expression is not a subexpression of the
19145 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19146 which would be skipped if cp_unevaluated_operand. */
19149 /* Fix the type of 'this'. */
19150 fntype
= build_memfn_type (fntype
, type
,
19151 type_memfn_quals (fntype
),
19152 type_memfn_rqual (fntype
));
19156 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19157 if (tmpl
== error_mark_node
)
19159 r
= error_mark_node
;
19162 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19163 finish_member_declaration (tmpl
);
19168 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19169 if (fn
== error_mark_node
)
19171 r
= error_mark_node
;
19174 finish_member_declaration (fn
);
19177 if (tree ci
= get_constraints (oldfn
))
19179 /* Substitute into the lambda's constraints. */
19181 ++processing_template_decl
;
19182 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19184 --processing_template_decl
;
19185 set_constraints (fn
, ci
);
19188 /* Let finish_function set this. */
19189 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19191 bool nested
= cfun
;
19193 push_function_context ();
19195 /* Still increment function_depth so that we don't GC in the
19196 middle of an expression. */
19199 local_specialization_stack
s (lss_copy
);
19201 tree body
= start_lambda_function (fn
, r
);
19203 /* Now record them for lookup_init_capture_pack. */
19204 int fplen
= vec_safe_length (field_packs
);
19205 for (int i
= 0; i
< fplen
; )
19207 tree pack
= (*field_packs
)[i
++];
19208 tree inst
= (*field_packs
)[i
++];
19209 register_local_specialization (inst
, pack
);
19211 release_tree_vector (field_packs
);
19213 register_parameter_specializations (oldfn
, fn
);
19217 /* We might not partially instantiate some parts of the function, so
19218 copy these flags from the original template. */
19219 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19220 current_function_returns_value
= ol
->returns_value
;
19221 current_function_returns_null
= ol
->returns_null
;
19222 current_function_returns_abnormally
= ol
->returns_abnormally
;
19223 current_function_infinite_loop
= ol
->infinite_loop
;
19226 /* [temp.deduct] A lambda-expression appearing in a function type or a
19227 template parameter is not considered part of the immediate context for
19228 the purposes of template argument deduction. */
19229 complain
= tf_warning_or_error
;
19231 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19232 /*constexpr*/false);
19234 finish_lambda_function (body
);
19237 pop_function_context ();
19241 /* The capture list was built up in reverse order; fix that now. */
19242 LAMBDA_EXPR_CAPTURE_LIST (r
)
19243 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19245 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19247 maybe_add_lambda_conv_op (type
);
19251 finish_struct (type
, /*attr*/NULL_TREE
);
19253 insert_pending_capture_proxies ();
19258 /* Like tsubst but deals with expressions and performs semantic
19259 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19262 tsubst_copy_and_build (tree t
,
19264 tsubst_flags_t complain
,
19267 bool integral_constant_expression_p
)
19269 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19270 #define RECUR(NODE) \
19271 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19272 /*function_p=*/false, \
19273 integral_constant_expression_p)
19276 location_t save_loc
;
19278 if (t
== NULL_TREE
|| t
== error_mark_node
)
19281 save_loc
= input_location
;
19282 if (location_t eloc
= cp_expr_location (t
))
19283 input_location
= eloc
;
19285 /* N3276 decltype magic only applies to calls at the top level or on the
19286 right side of a comma. */
19287 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19288 complain
&= ~tf_decltype
;
19290 switch (TREE_CODE (t
))
19294 /* Fall through. */
19295 case IDENTIFIER_NODE
:
19299 bool non_integral_constant_expression_p
;
19300 const char *error_msg
;
19302 if (IDENTIFIER_CONV_OP_P (t
))
19304 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19305 t
= make_conv_op_name (new_type
);
19308 /* Look up the name. */
19309 decl
= lookup_name (t
);
19311 /* By convention, expressions use ERROR_MARK_NODE to indicate
19312 failure, not NULL_TREE. */
19313 if (decl
== NULL_TREE
)
19314 decl
= error_mark_node
;
19316 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19318 integral_constant_expression_p
,
19319 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19320 &non_integral_constant_expression_p
,
19321 /*template_p=*/false,
19323 /*address_p=*/false,
19324 /*template_arg_p=*/false,
19329 if (!function_p
&& identifier_p (decl
))
19331 if (complain
& tf_error
)
19332 unqualified_name_lookup_error (decl
);
19333 decl
= error_mark_node
;
19338 case TEMPLATE_ID_EXPR
:
19341 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19342 tree targs
= TREE_OPERAND (t
, 1);
19345 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19346 if (targs
== error_mark_node
)
19347 RETURN (error_mark_node
);
19349 if (TREE_CODE (templ
) == SCOPE_REF
)
19351 tree name
= TREE_OPERAND (templ
, 1);
19352 tree tid
= lookup_template_function (name
, targs
);
19353 TREE_OPERAND (templ
, 1) = tid
;
19357 if (concept_definition_p (templ
))
19359 tree check
= build_concept_check (templ
, targs
, complain
);
19360 if (check
== error_mark_node
)
19361 RETURN (error_mark_node
);
19363 tree id
= unpack_concept_check (check
);
19365 /* If we built a function concept check, return the underlying
19366 template-id. So we can evaluate it as a function call. */
19367 if (function_concept_p (TREE_OPERAND (id
, 0)))
19373 if (variable_template_p (templ
))
19375 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19377 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19381 if (TREE_CODE (templ
) == COMPONENT_REF
)
19383 object
= TREE_OPERAND (templ
, 0);
19384 templ
= TREE_OPERAND (templ
, 1);
19387 object
= NULL_TREE
;
19388 templ
= lookup_template_function (templ
, targs
);
19391 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19392 object
, templ
, NULL_TREE
));
19394 RETURN (baselink_for_fns (templ
));
19399 tree r
= RECUR (TREE_OPERAND (t
, 0));
19401 if (REFERENCE_REF_P (t
))
19403 /* A type conversion to reference type will be enclosed in
19404 such an indirect ref, but the substitution of the cast
19405 will have also added such an indirect ref. */
19406 r
= convert_from_reference (r
);
19409 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19410 complain
|decltype_flag
);
19412 if (REF_PARENTHESIZED_P (t
))
19413 r
= force_paren_expr (r
);
19420 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19421 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19422 RETURN (build_nop (type
, op0
));
19425 case IMPLICIT_CONV_EXPR
:
19427 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19428 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19429 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19431 retval
= copy_node (t
);
19432 TREE_TYPE (retval
) = type
;
19433 TREE_OPERAND (retval
, 0) = expr
;
19436 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19437 /* We'll pass this to convert_nontype_argument again, we don't need
19438 to actually perform any conversion here. */
19440 int flags
= LOOKUP_IMPLICIT
;
19441 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19442 flags
= LOOKUP_NORMAL
;
19443 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19444 flags
|= LOOKUP_NO_NARROWING
;
19445 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19451 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19452 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19453 if (op0
== error_mark_node
)
19454 RETURN (error_mark_node
);
19455 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19459 case REINTERPRET_CAST_EXPR
:
19460 case CONST_CAST_EXPR
:
19461 case DYNAMIC_CAST_EXPR
:
19462 case STATIC_CAST_EXPR
:
19465 tree op
, r
= NULL_TREE
;
19467 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19468 if (integral_constant_expression_p
19469 && !cast_valid_in_integral_constant_expression_p (type
))
19471 if (complain
& tf_error
)
19472 error ("a cast to a type other than an integral or "
19473 "enumeration type cannot appear in a constant-expression");
19474 RETURN (error_mark_node
);
19477 op
= RECUR (TREE_OPERAND (t
, 0));
19479 warning_sentinel
s(warn_useless_cast
);
19480 warning_sentinel
s2(warn_ignored_qualifiers
);
19481 switch (TREE_CODE (t
))
19484 r
= build_functional_cast (input_location
, type
, op
, complain
);
19486 case REINTERPRET_CAST_EXPR
:
19487 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19489 case CONST_CAST_EXPR
:
19490 r
= build_const_cast (input_location
, type
, op
, complain
);
19492 case DYNAMIC_CAST_EXPR
:
19493 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19495 case STATIC_CAST_EXPR
:
19496 r
= build_static_cast (input_location
, type
, op
, complain
);
19497 if (IMPLICIT_RVALUE_P (t
))
19498 set_implicit_rvalue_p (r
);
19501 gcc_unreachable ();
19507 case POSTDECREMENT_EXPR
:
19508 case POSTINCREMENT_EXPR
:
19509 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19510 args
, complain
, in_decl
);
19511 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19512 complain
|decltype_flag
));
19514 case PREDECREMENT_EXPR
:
19515 case PREINCREMENT_EXPR
:
19519 case TRUTH_NOT_EXPR
:
19520 case UNARY_PLUS_EXPR
: /* Unary + */
19521 case REALPART_EXPR
:
19522 case IMAGPART_EXPR
:
19523 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19524 RECUR (TREE_OPERAND (t
, 0)),
19525 complain
|decltype_flag
));
19527 case FIX_TRUNC_EXPR
:
19528 gcc_unreachable ();
19531 op1
= TREE_OPERAND (t
, 0);
19532 if (TREE_CODE (op1
) == LABEL_DECL
)
19533 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19534 EXPR_LOCATION (op1
)));
19535 if (TREE_CODE (op1
) == SCOPE_REF
)
19536 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19537 /*done=*/true, /*address_p=*/true);
19539 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19541 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19542 complain
|decltype_flag
));
19547 case TRUNC_DIV_EXPR
:
19548 case CEIL_DIV_EXPR
:
19549 case FLOOR_DIV_EXPR
:
19550 case ROUND_DIV_EXPR
:
19551 case EXACT_DIV_EXPR
:
19555 case TRUNC_MOD_EXPR
:
19556 case FLOOR_MOD_EXPR
:
19557 case TRUTH_ANDIF_EXPR
:
19558 case TRUTH_ORIF_EXPR
:
19559 case TRUTH_AND_EXPR
:
19560 case TRUTH_OR_EXPR
:
19571 case SPACESHIP_EXPR
:
19575 /* If T was type-dependent, suppress warnings that depend on the range
19576 of the types involved. */
19577 bool was_dep
= type_dependent_expression_p_push (t
);
19579 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19580 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19582 warning_sentinel
s1(warn_type_limits
, was_dep
);
19583 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19584 warning_sentinel
s3(warn_logical_op
, was_dep
);
19585 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19587 tree r
= build_x_binary_op
19588 (input_location
, TREE_CODE (t
),
19590 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19592 : TREE_CODE (TREE_OPERAND (t
, 0))),
19594 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19596 : TREE_CODE (TREE_OPERAND (t
, 1))),
19598 complain
|decltype_flag
);
19599 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19600 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19605 case POINTER_PLUS_EXPR
:
19607 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19608 if (op0
== error_mark_node
)
19609 RETURN (error_mark_node
);
19610 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19611 if (op1
== error_mark_node
)
19612 RETURN (error_mark_node
);
19613 RETURN (fold_build_pointer_plus (op0
, op1
));
19617 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19618 /*address_p=*/false));
19620 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19621 args
, complain
, in_decl
);
19622 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19623 RECUR (TREE_OPERAND (t
, 1)),
19624 complain
|decltype_flag
));
19627 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19628 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19629 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19636 op1
= TREE_OPERAND (t
, 0);
19637 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19638 op1
= TREE_TYPE (op1
);
19639 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19640 && ALIGNOF_EXPR_STD_P (t
));
19643 /* When there are no ARGS, we are trying to evaluate a
19644 non-dependent expression from the parser. Trying to do
19645 the substitutions may not work. */
19647 op1
= TREE_TYPE (op1
);
19651 ++cp_unevaluated_operand
;
19652 ++c_inhibit_evaluation_warnings
;
19654 op1
= tsubst (op1
, args
, complain
, in_decl
);
19656 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19657 /*function_p=*/false,
19658 /*integral_constant_expression_p=*/
19660 --cp_unevaluated_operand
;
19661 --c_inhibit_evaluation_warnings
;
19664 r
= cxx_sizeof_or_alignof_type (input_location
,
19665 op1
, TREE_CODE (t
), std_alignof
,
19666 complain
& tf_error
);
19668 r
= cxx_sizeof_or_alignof_expr (input_location
,
19669 op1
, TREE_CODE (t
),
19670 complain
& tf_error
);
19671 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19673 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19675 if (!processing_template_decl
&& TYPE_P (op1
))
19677 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19678 build1 (NOP_EXPR
, op1
, error_mark_node
));
19679 SIZEOF_EXPR_TYPE_P (r
) = 1;
19682 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19683 TREE_SIDE_EFFECTS (r
) = 0;
19684 TREE_READONLY (r
) = 1;
19686 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19691 case AT_ENCODE_EXPR
:
19693 op1
= TREE_OPERAND (t
, 0);
19694 ++cp_unevaluated_operand
;
19695 ++c_inhibit_evaluation_warnings
;
19696 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19697 /*function_p=*/false,
19698 /*integral_constant_expression_p=*/false);
19699 --cp_unevaluated_operand
;
19700 --c_inhibit_evaluation_warnings
;
19701 RETURN (objc_build_encode_expr (op1
));
19704 case NOEXCEPT_EXPR
:
19705 op1
= TREE_OPERAND (t
, 0);
19706 ++cp_unevaluated_operand
;
19707 ++c_inhibit_evaluation_warnings
;
19708 ++cp_noexcept_operand
;
19709 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19710 /*function_p=*/false,
19711 /*integral_constant_expression_p=*/false);
19712 --cp_unevaluated_operand
;
19713 --c_inhibit_evaluation_warnings
;
19714 --cp_noexcept_operand
;
19715 RETURN (finish_noexcept_expr (op1
, complain
));
19719 warning_sentinel
s(warn_div_by_zero
);
19720 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19721 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19722 tree r
= build_x_modify_expr
19723 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19724 complain
|decltype_flag
);
19725 /* TREE_NO_WARNING must be set if either the expression was
19726 parenthesized or it uses an operator such as >>= rather
19727 than plain assignment. In the former case, it was already
19728 set and must be copied. In the latter case,
19729 build_x_modify_expr sets it and it must not be reset
19731 if (TREE_NO_WARNING (t
))
19732 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19738 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19739 args
, complain
, in_decl
);
19740 /* Remember that there was a reference to this entity. */
19742 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19743 RETURN (error_mark_node
);
19744 RETURN (build_x_arrow (input_location
, op1
, complain
));
19748 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19749 tree init
= RECUR (TREE_OPERAND (t
, 3));
19750 vec
<tree
, va_gc
> *placement_vec
;
19751 vec
<tree
, va_gc
> *init_vec
;
19753 location_t loc
= EXPR_LOCATION (t
);
19755 if (placement
== NULL_TREE
)
19756 placement_vec
= NULL
;
19757 else if (placement
== error_mark_node
)
19758 RETURN (error_mark_node
);
19761 placement_vec
= make_tree_vector ();
19762 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19763 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19766 /* If there was an initializer in the original tree, but it
19767 instantiated to an empty list, then we should pass a
19768 non-NULL empty vector to tell build_new that it was an
19769 empty initializer() rather than no initializer. This can
19770 only happen when the initializer is a pack expansion whose
19771 parameter packs are of length zero. */
19772 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19776 init_vec
= make_tree_vector ();
19777 if (init
== void_node
)
19778 gcc_assert (init_vec
!= NULL
);
19781 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19782 vec_safe_push (init_vec
, TREE_VALUE (init
));
19786 /* Avoid passing an enclosing decl to valid_array_size_p. */
19787 in_decl
= NULL_TREE
;
19789 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19790 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19791 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19792 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19795 if (placement_vec
!= NULL
)
19796 release_tree_vector (placement_vec
);
19797 if (init_vec
!= NULL
)
19798 release_tree_vector (init_vec
);
19805 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19806 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19807 RETURN (delete_sanity (input_location
, op0
, op1
,
19808 DELETE_EXPR_USE_VEC (t
),
19809 DELETE_EXPR_USE_GLOBAL (t
),
19813 case COMPOUND_EXPR
:
19815 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19816 complain
& ~tf_decltype
, in_decl
,
19817 /*function_p=*/false,
19818 integral_constant_expression_p
);
19819 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19821 RECUR (TREE_OPERAND (t
, 1)),
19822 complain
|decltype_flag
));
19828 unsigned int nargs
, i
;
19833 function
= CALL_EXPR_FN (t
);
19834 /* Internal function with no arguments. */
19835 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19838 /* When we parsed the expression, we determined whether or
19839 not Koenig lookup should be performed. */
19840 koenig_p
= KOENIG_LOOKUP_P (t
);
19841 if (function
== NULL_TREE
)
19844 qualified_p
= false;
19846 else if (TREE_CODE (function
) == SCOPE_REF
)
19848 qualified_p
= true;
19849 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19851 /*address_p=*/false);
19853 else if (koenig_p
&& identifier_p (function
))
19855 /* Do nothing; calling tsubst_copy_and_build on an identifier
19856 would incorrectly perform unqualified lookup again.
19858 Note that we can also have an IDENTIFIER_NODE if the earlier
19859 unqualified lookup found a member function; in that case
19860 koenig_p will be false and we do want to do the lookup
19861 again to find the instantiated member function.
19863 FIXME but doing that causes c++/15272, so we need to stop
19864 using IDENTIFIER_NODE in that situation. */
19865 qualified_p
= false;
19869 if (TREE_CODE (function
) == COMPONENT_REF
)
19871 tree op
= TREE_OPERAND (function
, 1);
19873 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19874 || (BASELINK_P (op
)
19875 && BASELINK_QUALIFIED_P (op
)));
19878 qualified_p
= false;
19880 if (TREE_CODE (function
) == ADDR_EXPR
19881 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19882 /* Avoid error about taking the address of a constructor. */
19883 function
= TREE_OPERAND (function
, 0);
19885 function
= tsubst_copy_and_build (function
, args
, complain
,
19888 integral_constant_expression_p
);
19890 if (BASELINK_P (function
))
19891 qualified_p
= true;
19894 nargs
= call_expr_nargs (t
);
19895 releasing_vec call_args
;
19896 for (i
= 0; i
< nargs
; ++i
)
19898 tree arg
= CALL_EXPR_ARG (t
, i
);
19900 if (!PACK_EXPANSION_P (arg
))
19901 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19904 /* Expand the pack expansion and push each entry onto
19906 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19907 if (TREE_CODE (arg
) == TREE_VEC
)
19909 unsigned int len
, j
;
19911 len
= TREE_VEC_LENGTH (arg
);
19912 for (j
= 0; j
< len
; ++j
)
19914 tree value
= TREE_VEC_ELT (arg
, j
);
19915 if (value
!= NULL_TREE
)
19916 value
= convert_from_reference (value
);
19917 vec_safe_push (call_args
, value
);
19922 /* A partial substitution. Add one entry. */
19923 vec_safe_push (call_args
, arg
);
19928 /* Stripped-down processing for a call in a thunk. Specifically, in
19929 the thunk template for a generic lambda. */
19930 if (CALL_FROM_THUNK_P (t
))
19932 /* Now that we've expanded any packs, the number of call args
19933 might be different. */
19934 unsigned int cargs
= call_args
->length ();
19935 tree thisarg
= NULL_TREE
;
19936 if (TREE_CODE (function
) == COMPONENT_REF
)
19938 thisarg
= TREE_OPERAND (function
, 0);
19939 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19940 thisarg
= TREE_OPERAND (thisarg
, 0);
19941 function
= TREE_OPERAND (function
, 1);
19942 if (TREE_CODE (function
) == BASELINK
)
19943 function
= BASELINK_FUNCTIONS (function
);
19945 /* We aren't going to do normal overload resolution, so force the
19946 template-id to resolve. */
19947 function
= resolve_nondeduced_context (function
, complain
);
19948 for (unsigned i
= 0; i
< cargs
; ++i
)
19950 /* In a thunk, pass through args directly, without any
19952 tree arg
= (*call_args
)[i
];
19953 while (TREE_CODE (arg
) != PARM_DECL
)
19954 arg
= TREE_OPERAND (arg
, 0);
19955 (*call_args
)[i
] = arg
;
19959 /* If there are no other args, just push 'this'. */
19961 vec_safe_push (call_args
, thisarg
);
19964 /* Otherwise, shift the other args over to make room. */
19965 tree last
= (*call_args
)[cargs
- 1];
19966 vec_safe_push (call_args
, last
);
19967 for (int i
= cargs
- 1; i
> 0; --i
)
19968 (*call_args
)[i
] = (*call_args
)[i
- 1];
19969 (*call_args
)[0] = thisarg
;
19972 ret
= build_call_a (function
, call_args
->length (),
19973 call_args
->address ());
19974 /* The thunk location is not interesting. */
19975 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19976 CALL_FROM_THUNK_P (ret
) = true;
19977 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19978 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19983 /* We do not perform argument-dependent lookup if normal
19984 lookup finds a non-function, in accordance with the
19985 expected resolution of DR 218. */
19987 && ((is_overloaded_fn (function
)
19988 /* If lookup found a member function, the Koenig lookup is
19989 not appropriate, even if an unqualified-name was used
19990 to denote the function. */
19991 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19992 || identifier_p (function
))
19993 /* Only do this when substitution turns a dependent call
19994 into a non-dependent call. */
19995 && type_dependent_expression_p_push (t
)
19996 && !any_type_dependent_arguments_p (call_args
))
19997 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19999 if (function
!= NULL_TREE
20000 && identifier_p (function
)
20001 && !any_type_dependent_arguments_p (call_args
))
20003 if (koenig_p
&& (complain
& tf_warning_or_error
))
20005 /* For backwards compatibility and good diagnostics, try
20006 the unqualified lookup again if we aren't in SFINAE
20008 tree unq
= (tsubst_copy_and_build
20009 (function
, args
, complain
, in_decl
, true,
20010 integral_constant_expression_p
));
20011 if (unq
== error_mark_node
)
20012 RETURN (error_mark_node
);
20014 if (unq
!= function
)
20016 /* In a lambda fn, we have to be careful to not
20017 introduce new this captures. Legacy code can't
20018 be using lambdas anyway, so it's ok to be
20020 bool in_lambda
= (current_class_type
20021 && LAMBDA_TYPE_P (current_class_type
));
20022 char const *const msg
20023 = G_("%qD was not declared in this scope, "
20024 "and no declarations were found by "
20025 "argument-dependent lookup at the point "
20026 "of instantiation");
20030 error_at (cp_expr_loc_or_input_loc (t
),
20033 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20039 if (INDIRECT_REF_P (fn
))
20040 fn
= TREE_OPERAND (fn
, 0);
20041 if (is_overloaded_fn (fn
))
20042 fn
= get_first_fn (fn
);
20045 /* Can't say anything more. */;
20046 else if (DECL_CLASS_SCOPE_P (fn
))
20048 location_t loc
= cp_expr_loc_or_input_loc (t
);
20050 "declarations in dependent base %qT are "
20051 "not found by unqualified lookup",
20052 DECL_CLASS_CONTEXT (fn
));
20053 if (current_class_ptr
)
20055 "use %<this->%D%> instead", function
);
20058 "use %<%T::%D%> instead",
20059 current_class_name
, function
);
20062 inform (DECL_SOURCE_LOCATION (fn
),
20063 "%qD declared here, later in the "
20064 "translation unit", fn
);
20066 RETURN (error_mark_node
);
20072 if (identifier_p (function
))
20074 if (complain
& tf_error
)
20075 unqualified_name_lookup_error (function
);
20076 RETURN (error_mark_node
);
20080 /* Remember that there was a reference to this entity. */
20081 if (function
!= NULL_TREE
20082 && DECL_P (function
)
20083 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20084 RETURN (error_mark_node
);
20086 /* Put back tf_decltype for the actual call. */
20087 complain
|= decltype_flag
;
20089 if (function
== NULL_TREE
)
20090 switch (CALL_EXPR_IFN (t
))
20093 gcc_assert (nargs
== 1);
20094 if (vec_safe_length (call_args
) != 1)
20096 error_at (cp_expr_loc_or_input_loc (t
),
20097 "wrong number of arguments to "
20098 "%<__builtin_launder%>");
20099 ret
= error_mark_node
;
20102 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20103 (*call_args
)[0], complain
);
20106 case IFN_VEC_CONVERT
:
20107 gcc_assert (nargs
== 1);
20108 if (vec_safe_length (call_args
) != 1)
20110 error_at (cp_expr_loc_or_input_loc (t
),
20111 "wrong number of arguments to "
20112 "%<__builtin_convertvector%>");
20113 ret
= error_mark_node
;
20116 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20117 tsubst (TREE_TYPE (t
), args
,
20118 complain
, in_decl
),
20120 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20125 /* Unsupported internal function with arguments. */
20126 gcc_unreachable ();
20128 else if (TREE_CODE (function
) == OFFSET_REF
20129 || TREE_CODE (function
) == DOTSTAR_EXPR
20130 || TREE_CODE (function
) == MEMBER_REF
)
20131 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20133 else if (TREE_CODE (function
) == COMPONENT_REF
)
20135 tree instance
= TREE_OPERAND (function
, 0);
20136 tree fn
= TREE_OPERAND (function
, 1);
20138 if (processing_template_decl
20139 && (type_dependent_expression_p (instance
)
20140 || (!BASELINK_P (fn
)
20141 && TREE_CODE (fn
) != FIELD_DECL
)
20142 || type_dependent_expression_p (fn
)
20143 || any_type_dependent_arguments_p (call_args
)))
20144 ret
= build_min_nt_call_vec (function
, call_args
);
20145 else if (!BASELINK_P (fn
))
20146 ret
= finish_call_expr (function
, &call_args
,
20147 /*disallow_virtual=*/false,
20148 /*koenig_p=*/false,
20151 ret
= (build_new_method_call
20153 &call_args
, NULL_TREE
,
20154 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20158 else if (concept_check_p (function
))
20160 /* FUNCTION is a template-id referring to a concept definition. */
20161 tree id
= unpack_concept_check (function
);
20162 tree tmpl
= TREE_OPERAND (id
, 0);
20163 tree args
= TREE_OPERAND (id
, 1);
20165 /* Calls to standard and variable concepts should have been
20166 previously diagnosed. */
20167 gcc_assert (function_concept_p (tmpl
));
20169 /* Ensure the result is wrapped as a call expression. */
20170 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20173 ret
= finish_call_expr (function
, &call_args
,
20174 /*disallow_virtual=*/qualified_p
,
20178 if (ret
!= error_mark_node
)
20180 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20181 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20182 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20183 if (op
|| ord
|| rev
)
20185 function
= extract_call_expr (ret
);
20186 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20187 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20188 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20197 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20198 cond
= mark_rvalue_use (cond
);
20199 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20202 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20204 if (integer_zerop (folded_cond
))
20206 ++c_inhibit_evaluation_warnings
;
20207 exp1
= RECUR (TREE_OPERAND (t
, 1));
20208 --c_inhibit_evaluation_warnings
;
20209 exp2
= RECUR (TREE_OPERAND (t
, 2));
20213 exp1
= RECUR (TREE_OPERAND (t
, 1));
20214 ++c_inhibit_evaluation_warnings
;
20215 exp2
= RECUR (TREE_OPERAND (t
, 2));
20216 --c_inhibit_evaluation_warnings
;
20218 cond
= folded_cond
;
20222 exp1
= RECUR (TREE_OPERAND (t
, 1));
20223 exp2
= RECUR (TREE_OPERAND (t
, 2));
20226 warning_sentinel
s(warn_duplicated_branches
);
20227 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20228 cond
, exp1
, exp2
, complain
));
20231 case PSEUDO_DTOR_EXPR
:
20233 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20234 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20235 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20236 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20241 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20243 case COMPONENT_REF
:
20250 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20251 args
, complain
, in_decl
);
20252 /* Remember that there was a reference to this entity. */
20253 if (DECL_P (object
)
20254 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20255 RETURN (error_mark_node
);
20256 object_type
= TREE_TYPE (object
);
20258 member
= TREE_OPERAND (t
, 1);
20259 if (BASELINK_P (member
))
20260 member
= tsubst_baselink (member
,
20261 non_reference (TREE_TYPE (object
)),
20262 args
, complain
, in_decl
);
20264 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20265 if (member
== error_mark_node
)
20266 RETURN (error_mark_node
);
20268 if (TREE_CODE (member
) == FIELD_DECL
)
20270 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20271 if (TREE_CODE (r
) == COMPONENT_REF
)
20272 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20275 else if (type_dependent_expression_p (object
))
20276 /* We can't do much here. */;
20277 else if (!CLASS_TYPE_P (object_type
))
20279 if (scalarish_type_p (object_type
))
20281 tree s
= NULL_TREE
;
20282 tree dtor
= member
;
20284 if (TREE_CODE (dtor
) == SCOPE_REF
)
20286 s
= TREE_OPERAND (dtor
, 0);
20287 dtor
= TREE_OPERAND (dtor
, 1);
20289 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20291 dtor
= TREE_OPERAND (dtor
, 0);
20293 RETURN (finish_pseudo_destructor_expr
20294 (object
, s
, dtor
, input_location
));
20298 else if (TREE_CODE (member
) == SCOPE_REF
20299 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20301 /* Lookup the template functions now that we know what the
20303 tree scope
= TREE_OPERAND (member
, 0);
20304 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20305 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20306 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20307 /*complain=*/false);
20308 if (BASELINK_P (member
))
20310 BASELINK_FUNCTIONS (member
)
20311 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20313 member
= (adjust_result_of_qualified_name_lookup
20314 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20319 qualified_name_lookup_error (scope
, tmpl
, member
,
20321 RETURN (error_mark_node
);
20324 else if (TREE_CODE (member
) == SCOPE_REF
20325 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20326 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20328 if (complain
& tf_error
)
20330 if (TYPE_P (TREE_OPERAND (member
, 0)))
20331 error ("%qT is not a class or namespace",
20332 TREE_OPERAND (member
, 0));
20334 error ("%qD is not a class or namespace",
20335 TREE_OPERAND (member
, 0));
20337 RETURN (error_mark_node
);
20340 r
= finish_class_member_access_expr (object
, member
,
20341 /*template_p=*/false,
20343 if (TREE_CODE (r
) == COMPONENT_REF
)
20344 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20349 RETURN (build_throw
20350 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20354 vec
<constructor_elt
, va_gc
> *n
;
20355 constructor_elt
*ce
;
20356 unsigned HOST_WIDE_INT idx
;
20357 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20358 bool process_index_p
;
20360 bool need_copy_p
= false;
20363 if (type
== error_mark_node
)
20364 RETURN (error_mark_node
);
20366 /* We do not want to process the index of aggregate
20367 initializers as they are identifier nodes which will be
20368 looked up by digest_init. */
20369 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20371 if (null_member_pointer_value_p (t
))
20373 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20377 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20378 newlen
= vec_safe_length (n
);
20379 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20381 if (ce
->index
&& process_index_p
20382 /* An identifier index is looked up in the type
20383 being initialized, not the current scope. */
20384 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20385 ce
->index
= RECUR (ce
->index
);
20387 if (PACK_EXPANSION_P (ce
->value
))
20389 /* Substitute into the pack expansion. */
20390 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20393 if (ce
->value
== error_mark_node
20394 || PACK_EXPANSION_P (ce
->value
))
20396 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20397 /* Just move the argument into place. */
20398 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20401 /* Update the length of the final CONSTRUCTOR
20402 arguments vector, and note that we will need to
20404 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20405 need_copy_p
= true;
20409 ce
->value
= RECUR (ce
->value
);
20414 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20416 vec_alloc (n
, newlen
);
20417 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20419 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20421 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20422 for (i
= 0; i
< len
; ++i
)
20423 CONSTRUCTOR_APPEND_ELT (n
, 0,
20424 TREE_VEC_ELT (ce
->value
, i
));
20427 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20431 r
= build_constructor (init_list_type_node
, n
);
20432 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20433 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20434 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20436 if (TREE_HAS_CONSTRUCTOR (t
))
20438 fcl_t cl
= fcl_functional
;
20439 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20441 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20444 TREE_TYPE (r
) = type
;
20450 tree operand_0
= TREE_OPERAND (t
, 0);
20451 if (TYPE_P (operand_0
))
20453 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20454 RETURN (get_typeid (operand_0
, complain
));
20458 operand_0
= RECUR (operand_0
);
20459 RETURN (build_typeid (operand_0
, complain
));
20470 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20471 /* ??? We're doing a subset of finish_id_expression here. */
20472 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20473 /* Replace an evaluated use of the thread_local variable with
20474 a call to its wrapper. */
20476 else if (outer_automatic_var_p (r
))
20477 r
= process_outer_var_ref (r
, complain
);
20479 if (!TYPE_REF_P (TREE_TYPE (t
)))
20480 /* If the original type was a reference, we'll be wrapped in
20481 the appropriate INDIRECT_REF. */
20482 r
= convert_from_reference (r
);
20488 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20489 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20490 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20493 case OFFSETOF_EXPR
:
20496 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20497 in_decl
, /*function_p=*/false,
20498 /*integral_constant_expression_p=*/false);
20499 RETURN (finish_offsetof (object_ptr
,
20500 RECUR (TREE_OPERAND (t
, 0)),
20501 EXPR_LOCATION (t
)));
20504 case ADDRESSOF_EXPR
:
20505 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20506 RECUR (TREE_OPERAND (t
, 0)), complain
));
20510 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20511 complain
, in_decl
);
20512 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20513 complain
, in_decl
);
20514 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20515 TRAIT_EXPR_KIND (t
), type1
, type2
));
20520 tree old_stmt_expr
= cur_stmt_expr
;
20521 tree stmt_expr
= begin_stmt_expr ();
20523 cur_stmt_expr
= stmt_expr
;
20524 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20525 integral_constant_expression_p
);
20526 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20527 cur_stmt_expr
= old_stmt_expr
;
20529 /* If the resulting list of expression statement is empty,
20530 fold it further into void_node. */
20531 if (empty_expr_stmt_p (stmt_expr
))
20532 stmt_expr
= void_node
;
20534 RETURN (stmt_expr
);
20539 if (complain
& tf_partial
)
20541 /* We don't have a full set of template arguments yet; don't touch
20542 the lambda at all. */
20543 gcc_assert (processing_template_decl
);
20546 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20548 RETURN (build_lambda_object (r
));
20552 /* We can get here for a constant initializer of non-dependent type.
20553 FIXME stop folding in cp_parser_initializer_clause. */
20555 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20560 case TRANSACTION_EXPR
:
20561 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20562 integral_constant_expression_p
));
20565 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20567 case VEC_PERM_EXPR
:
20569 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20570 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20571 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20572 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20576 case REQUIRES_EXPR
:
20578 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20583 /* No need to substitute further, a RANGE_EXPR will always be built
20584 with constant operands. */
20587 case NON_LVALUE_EXPR
:
20588 case VIEW_CONVERT_EXPR
:
20589 if (location_wrapper_p (t
))
20590 /* We need to do this here as well as in tsubst_copy so we get the
20591 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20592 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20593 EXPR_LOCATION (t
)));
20597 /* Handle Objective-C++ constructs, if appropriate. */
20600 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20601 in_decl
, /*function_p=*/false);
20605 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20611 input_location
= save_loc
;
20615 /* Verify that the instantiated ARGS are valid. For type arguments,
20616 make sure that the type's linkage is ok. For non-type arguments,
20617 make sure they are constants if they are integral or enumerations.
20618 Emit an error under control of COMPLAIN, and return TRUE on error. */
20621 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20623 if (dependent_template_arg_p (t
))
20625 if (ARGUMENT_PACK_P (t
))
20627 tree vec
= ARGUMENT_PACK_ARGS (t
);
20628 int len
= TREE_VEC_LENGTH (vec
);
20629 bool result
= false;
20632 for (i
= 0; i
< len
; ++i
)
20633 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20637 else if (TYPE_P (t
))
20639 /* [basic.link]: A name with no linkage (notably, the name
20640 of a class or enumeration declared in a local scope)
20641 shall not be used to declare an entity with linkage.
20642 This implies that names with no linkage cannot be used as
20645 DR 757 relaxes this restriction for C++0x. */
20646 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20647 : no_linkage_check (t
, /*relaxed_p=*/false));
20651 /* DR 488 makes use of a type with no linkage cause
20652 type deduction to fail. */
20653 if (complain
& tf_error
)
20655 if (TYPE_UNNAMED_P (nt
))
20656 error ("%qT is/uses unnamed type", t
);
20658 error ("template argument for %qD uses local type %qT",
20663 /* In order to avoid all sorts of complications, we do not
20664 allow variably-modified types as template arguments. */
20665 else if (variably_modified_type_p (t
, NULL_TREE
))
20667 if (complain
& tf_error
)
20668 error ("%qT is a variably modified type", t
);
20672 /* Class template and alias template arguments should be OK. */
20673 else if (DECL_TYPE_TEMPLATE_P (t
))
20675 /* A non-type argument of integral or enumerated type must be a
20677 else if (TREE_TYPE (t
)
20678 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20679 && !REFERENCE_REF_P (t
)
20680 && !TREE_CONSTANT (t
))
20682 if (complain
& tf_error
)
20683 error ("integral expression %qE is not constant", t
);
20690 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20692 int ix
, len
= DECL_NTPARMS (tmpl
);
20693 bool result
= false;
20695 for (ix
= 0; ix
!= len
; ix
++)
20697 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20700 if (result
&& (complain
& tf_error
))
20701 error (" trying to instantiate %qD", tmpl
);
20705 /* We're out of SFINAE context now, so generate diagnostics for the access
20706 errors we saw earlier when instantiating D from TMPL and ARGS. */
20709 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20711 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20712 tree type
= TREE_TYPE (pattern
);
20713 location_t loc
= input_location
;
20715 push_access_scope (d
);
20716 push_deferring_access_checks (dk_no_deferred
);
20717 input_location
= DECL_SOURCE_LOCATION (pattern
);
20718 tsubst (type
, args
, tf_warning_or_error
, d
);
20719 input_location
= loc
;
20720 pop_deferring_access_checks ();
20721 pop_access_scope (d
);
20724 /* Instantiate the indicated variable, function, or alias template TMPL with
20725 the template arguments in TARG_PTR. */
20728 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20730 tree targ_ptr
= orig_args
;
20734 bool access_ok
= true;
20736 if (tmpl
== error_mark_node
)
20737 return error_mark_node
;
20739 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20741 /* If this function is a clone, handle it specially. */
20742 if (DECL_CLONED_FUNCTION_P (tmpl
))
20747 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20748 DECL_CLONED_FUNCTION. */
20749 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20750 targ_ptr
, complain
);
20751 if (spec
== error_mark_node
)
20752 return error_mark_node
;
20754 /* Look for the clone. */
20755 FOR_EACH_CLONE (clone
, spec
)
20756 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20758 /* We should always have found the clone by now. */
20759 gcc_unreachable ();
20763 if (targ_ptr
== error_mark_node
)
20764 return error_mark_node
;
20766 /* Check to see if we already have this specialization. */
20767 gen_tmpl
= most_general_template (tmpl
);
20768 if (TMPL_ARGS_DEPTH (targ_ptr
)
20769 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20770 /* targ_ptr only has the innermost template args, so add the outer ones
20771 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20772 the case of a non-dependent call within a template definition). */
20773 targ_ptr
= (add_outermost_template_args
20774 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20777 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20778 but it doesn't seem to be on the hot path. */
20779 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20781 gcc_checking_assert (tmpl
== gen_tmpl
20783 = retrieve_specialization (tmpl
, orig_args
, 0))
20785 || fndecl
== NULL_TREE
);
20787 if (spec
!= NULL_TREE
)
20789 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20791 if (complain
& tf_error
)
20792 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20793 return error_mark_node
;
20798 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20800 return error_mark_node
;
20802 /* We are building a FUNCTION_DECL, during which the access of its
20803 parameters and return types have to be checked. However this
20804 FUNCTION_DECL which is the desired context for access checking
20805 is not built yet. We solve this chicken-and-egg problem by
20806 deferring all checks until we have the FUNCTION_DECL. */
20807 push_deferring_access_checks (dk_deferred
);
20809 /* Instantiation of the function happens in the context of the function
20810 template, not the context of the overload resolution we're doing. */
20811 push_to_top_level ();
20812 /* If there are dependent arguments, e.g. because we're doing partial
20813 ordering, make sure processing_template_decl stays set. */
20814 if (uses_template_parms (targ_ptr
))
20815 ++processing_template_decl
;
20816 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20818 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20819 complain
, gen_tmpl
, true);
20820 push_nested_class (ctx
);
20823 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20825 fndecl
= NULL_TREE
;
20826 if (VAR_P (pattern
))
20828 /* We need to determine if we're using a partial or explicit
20829 specialization now, because the type of the variable could be
20831 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20832 tree elt
= most_specialized_partial_spec (tid
, complain
);
20833 if (elt
== error_mark_node
)
20834 pattern
= error_mark_node
;
20837 tree partial_tmpl
= TREE_VALUE (elt
);
20838 tree partial_args
= TREE_PURPOSE (elt
);
20839 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20840 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20844 /* Substitute template parameters to obtain the specialization. */
20845 if (fndecl
== NULL_TREE
)
20846 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20847 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20848 pop_nested_class ();
20849 pop_from_top_level ();
20851 if (fndecl
== error_mark_node
)
20853 pop_deferring_access_checks ();
20854 return error_mark_node
;
20857 /* The DECL_TI_TEMPLATE should always be the immediate parent
20858 template, not the most general template. */
20859 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20860 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20862 /* Now we know the specialization, compute access previously
20863 deferred. Do no access control for inheriting constructors,
20864 as we already checked access for the inherited constructor. */
20865 if (!(flag_new_inheriting_ctors
20866 && DECL_INHERITED_CTOR (fndecl
)))
20868 push_access_scope (fndecl
);
20869 if (!perform_deferred_access_checks (complain
))
20871 pop_access_scope (fndecl
);
20873 pop_deferring_access_checks ();
20875 /* If we've just instantiated the main entry point for a function,
20876 instantiate all the alternate entry points as well. We do this
20877 by cloning the instantiation of the main entry point, not by
20878 instantiating the template clones. */
20879 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20880 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20881 clone_cdtor (fndecl
, /*update_methods=*/false);
20885 if (!(complain
& tf_error
))
20887 /* Remember to reinstantiate when we're out of SFINAE so the user
20888 can see the errors. */
20889 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20891 return error_mark_node
;
20896 /* Wrapper for instantiate_template_1. */
20899 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20902 timevar_push (TV_TEMPLATE_INST
);
20903 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20904 timevar_pop (TV_TEMPLATE_INST
);
20908 /* Instantiate the alias template TMPL with ARGS. Also push a template
20909 instantiation level, which instantiate_template doesn't do because
20910 functions and variables have sufficient context established by the
20914 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20916 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20917 return error_mark_node
;
20920 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20921 args
, tmpl
, complain
,
20922 /*require_all_args=*/true,
20923 /*use_default_args=*/true);
20925 /* FIXME check for satisfaction in check_instantiated_args. */
20927 && !any_dependent_template_arguments_p (args
)
20928 && !constraints_satisfied_p (tmpl
, args
))
20930 if (complain
& tf_error
)
20932 auto_diagnostic_group d
;
20933 error ("template constraint failure for %qD", tmpl
);
20934 diagnose_constraints (input_location
, tmpl
, args
);
20936 return error_mark_node
;
20939 if (!push_tinst_level (tmpl
, args
))
20940 return error_mark_node
;
20941 tree r
= instantiate_template (tmpl
, args
, complain
);
20942 pop_tinst_level ();
20947 /* PARM is a template parameter pack for FN. Returns true iff
20948 PARM is used in a deducible way in the argument list of FN. */
20951 pack_deducible_p (tree parm
, tree fn
)
20953 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20954 for (; t
; t
= TREE_CHAIN (t
))
20956 tree type
= TREE_VALUE (t
);
20958 if (!PACK_EXPANSION_P (type
))
20960 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20961 packs
; packs
= TREE_CHAIN (packs
))
20962 if (template_args_equal (TREE_VALUE (packs
), parm
))
20964 /* The template parameter pack is used in a function parameter
20965 pack. If this is the end of the parameter list, the
20966 template parameter pack is deducible. */
20967 if (TREE_CHAIN (t
) == void_list_node
)
20970 /* Otherwise, not. Well, it could be deduced from
20971 a non-pack parameter, but doing so would end up with
20972 a deduction mismatch, so don't bother. */
20976 /* The template parameter pack isn't used in any function parameter
20977 packs, but it might be used deeper, e.g. tuple<Args...>. */
20981 /* Subroutine of fn_type_unification: check non-dependent parms for
20985 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20986 tree fn
, unification_kind_t strict
, int flags
,
20987 struct conversion
**convs
, bool explain_p
)
20989 /* Non-constructor methods need to leave a conversion for 'this', which
20990 isn't included in nargs here. */
20991 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20992 && !DECL_CONSTRUCTOR_P (fn
));
20994 for (unsigned ia
= 0;
20995 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20997 tree parm
= TREE_VALUE (parms
);
20999 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21000 && (!TREE_CHAIN (parms
)
21001 || TREE_CHAIN (parms
) == void_list_node
))
21002 /* For a function parameter pack that occurs at the end of the
21003 parameter-declaration-list, the type A of each remaining
21004 argument of the call is compared with the type P of the
21005 declarator-id of the function parameter pack. */
21008 parms
= TREE_CHAIN (parms
);
21010 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21011 /* For a function parameter pack that does not occur at the
21012 end of the parameter-declaration-list, the type of the
21013 parameter pack is a non-deduced context. */
21016 if (!uses_template_parms (parm
))
21018 tree arg
= args
[ia
];
21019 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21020 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21022 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21023 conv_p
, explain_p
))
21033 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21034 NARGS elements of the arguments that are being used when calling
21035 it. TARGS is a vector into which the deduced template arguments
21038 Returns either a FUNCTION_DECL for the matching specialization of FN or
21039 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21040 true, diagnostics will be printed to explain why it failed.
21042 If FN is a conversion operator, or we are trying to produce a specific
21043 specialization, RETURN_TYPE is the return type desired.
21045 The EXPLICIT_TARGS are explicit template arguments provided via a
21048 The parameter STRICT is one of:
21051 We are deducing arguments for a function call, as in
21052 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21053 deducing arguments for a call to the result of a conversion
21054 function template, as in [over.call.object].
21057 We are deducing arguments for a conversion function, as in
21058 [temp.deduct.conv].
21061 We are deducing arguments when doing an explicit instantiation
21062 as in [temp.explicit], when determining an explicit specialization
21063 as in [temp.expl.spec], or when taking the address of a function
21064 template, as in [temp.deduct.funcaddr]. */
21067 fn_type_unification (tree fn
,
21068 tree explicit_targs
,
21071 unsigned int nargs
,
21073 unification_kind_t strict
,
21075 struct conversion
**convs
,
21081 tree decl
= NULL_TREE
;
21082 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21084 static int deduction_depth
;
21085 /* type_unification_real will pass back any access checks from default
21086 template argument substitution. */
21087 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21088 /* We don't have all the template args yet. */
21089 bool incomplete
= true;
21092 if (flag_new_inheriting_ctors
)
21093 fn
= strip_inheriting_ctors (fn
);
21095 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21096 tree r
= error_mark_node
;
21098 tree full_targs
= targs
;
21099 if (TMPL_ARGS_DEPTH (targs
)
21100 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21101 full_targs
= (add_outermost_template_args
21102 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21106 complain
|= tf_decltype
;
21108 /* In C++0x, it's possible to have a function template whose type depends
21109 on itself recursively. This is most obvious with decltype, but can also
21110 occur with enumeration scope (c++/48969). So we need to catch infinite
21111 recursion and reject the substitution at deduction time; this function
21112 will return error_mark_node for any repeated substitution.
21114 This also catches excessive recursion such as when f<N> depends on
21115 f<N-1> across all integers, and returns error_mark_node for all the
21116 substitutions back up to the initial one.
21118 This is, of course, not reentrant. */
21119 if (excessive_deduction_depth
)
21120 return error_mark_node
;
21123 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21125 fntype
= TREE_TYPE (fn
);
21126 if (explicit_targs
)
21130 The specified template arguments must match the template
21131 parameters in kind (i.e., type, nontype, template), and there
21132 must not be more arguments than there are parameters;
21133 otherwise type deduction fails.
21135 Nontype arguments must match the types of the corresponding
21136 nontype template parameters, or must be convertible to the
21137 types of the corresponding nontype parameters as specified in
21138 _temp.arg.nontype_, otherwise type deduction fails.
21140 All references in the function type of the function template
21141 to the corresponding template parameters are replaced by the
21142 specified template argument values. If a substitution in a
21143 template parameter or in the function type of the function
21144 template results in an invalid type, type deduction fails. */
21145 int i
, len
= TREE_VEC_LENGTH (tparms
);
21146 location_t loc
= input_location
;
21147 incomplete
= false;
21149 if (explicit_targs
== error_mark_node
)
21152 if (TMPL_ARGS_DEPTH (explicit_targs
)
21153 < TMPL_ARGS_DEPTH (full_targs
))
21154 explicit_targs
= add_outermost_template_args (full_targs
,
21157 /* Adjust any explicit template arguments before entering the
21158 substitution context. */
21160 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21161 complain
|tf_partial
,
21162 /*require_all_args=*/false,
21163 /*use_default_args=*/false));
21164 if (explicit_targs
== error_mark_node
)
21167 /* Substitute the explicit args into the function type. This is
21168 necessary so that, for instance, explicitly declared function
21169 arguments can match null pointed constants. If we were given
21170 an incomplete set of explicit args, we must not do semantic
21171 processing during substitution as we could create partial
21173 for (i
= 0; i
< len
; i
++)
21175 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21176 bool parameter_pack
= false;
21177 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21179 /* Dig out the actual parm. */
21180 if (TREE_CODE (parm
) == TYPE_DECL
21181 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21183 parm
= TREE_TYPE (parm
);
21184 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21186 else if (TREE_CODE (parm
) == PARM_DECL
)
21188 parm
= DECL_INITIAL (parm
);
21189 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21192 if (targ
== NULL_TREE
)
21193 /* No explicit argument for this template parameter. */
21195 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21197 /* Mark the argument pack as "incomplete". We could
21198 still deduce more arguments during unification.
21199 We remove this mark in type_unification_real. */
21200 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21201 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21202 = ARGUMENT_PACK_ARGS (targ
);
21204 /* We have some incomplete argument packs. */
21211 if (!push_tinst_level (fn
, explicit_targs
))
21213 excessive_deduction_depth
= true;
21216 ++processing_template_decl
;
21217 input_location
= DECL_SOURCE_LOCATION (fn
);
21218 /* Ignore any access checks; we'll see them again in
21219 instantiate_template and they might have the wrong
21220 access path at this point. */
21221 push_deferring_access_checks (dk_deferred
);
21222 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21223 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21224 pop_deferring_access_checks ();
21225 input_location
= loc
;
21226 --processing_template_decl
;
21227 pop_tinst_level ();
21229 if (fntype
== error_mark_node
)
21233 /* Place the explicitly specified arguments in TARGS. */
21234 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21235 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21236 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21237 if (!incomplete
&& CHECKING_P
21238 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21239 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21240 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21243 if (return_type
&& strict
!= DEDUCE_CALL
)
21245 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21246 new_args
[0] = return_type
;
21247 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21255 /* Never do unification on the 'this' parameter. */
21256 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21258 if (return_type
&& strict
== DEDUCE_CALL
)
21260 /* We're deducing for a call to the result of a template conversion
21261 function. The parms we really want are in return_type. */
21262 if (INDIRECT_TYPE_P (return_type
))
21263 return_type
= TREE_TYPE (return_type
);
21264 parms
= TYPE_ARG_TYPES (return_type
);
21266 else if (return_type
)
21268 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21271 /* We allow incomplete unification without an error message here
21272 because the standard doesn't seem to explicitly prohibit it. Our
21273 callers must be ready to deal with unification failures in any
21276 /* If we aren't explaining yet, push tinst context so we can see where
21277 any errors (e.g. from class instantiations triggered by instantiation
21278 of default template arguments) come from. If we are explaining, this
21279 context is redundant. */
21280 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21282 excessive_deduction_depth
= true;
21286 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21287 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21288 strict
, &checks
, explain_p
);
21290 pop_tinst_level ();
21294 /* Now that we have bindings for all of the template arguments,
21295 ensure that the arguments deduced for the template template
21296 parameters have compatible template parameter lists. We cannot
21297 check this property before we have deduced all template
21298 arguments, because the template parameter types of a template
21299 template parameter might depend on prior template parameters
21300 deduced after the template template parameter. The following
21301 ill-formed example illustrates this issue:
21303 template<typename T, template<T> class C> void f(C<5>, T);
21305 template<int N> struct X {};
21308 f(X<5>(), 5l); // error: template argument deduction fails
21311 The template parameter list of 'C' depends on the template type
21312 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21313 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21314 time that we deduce 'C'. */
21315 if (!template_template_parm_bindings_ok_p
21316 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21318 unify_inconsistent_template_template_parameters (explain_p
);
21324 /* CWG2369: Check satisfaction before non-deducible conversions. */
21325 if (!constraints_satisfied_p (fn
, targs
))
21328 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21332 /* DR 1391: All parameters have args, now check non-dependent parms for
21333 convertibility. We don't do this if all args were explicitly specified,
21334 as the standard says that we substitute explicit args immediately. */
21336 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21340 /* All is well so far. Now, check:
21344 When all template arguments have been deduced, all uses of
21345 template parameters in nondeduced contexts are replaced with
21346 the corresponding deduced argument values. If the
21347 substitution results in an invalid type, as described above,
21348 type deduction fails. */
21349 if (!push_tinst_level (fn
, targs
))
21351 excessive_deduction_depth
= true;
21355 /* Also collect access checks from the instantiation. */
21356 reopen_deferring_access_checks (checks
);
21358 decl
= instantiate_template (fn
, targs
, complain
);
21360 checks
= get_deferred_access_checks ();
21361 pop_deferring_access_checks ();
21363 pop_tinst_level ();
21365 if (decl
== error_mark_node
)
21368 /* Now perform any access checks encountered during substitution. */
21369 push_access_scope (decl
);
21370 ok
= perform_access_checks (checks
, complain
);
21371 pop_access_scope (decl
);
21375 /* If we're looking for an exact match, check that what we got
21376 is indeed an exact match. It might not be if some template
21377 parameters are used in non-deduced contexts. But don't check
21378 for an exact match if we have dependent template arguments;
21379 in that case we're doing partial ordering, and we already know
21380 that we have two candidates that will provide the actual type. */
21381 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21383 tree substed
= TREE_TYPE (decl
);
21387 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21389 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21390 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21391 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21393 unify_type_mismatch (explain_p
, args
[i
],
21394 TREE_VALUE (sarg
));
21399 /* After doing deduction with the inherited constructor, actually return an
21400 instantiation of the inheriting constructor. */
21402 decl
= instantiate_template (orig_fn
, targs
, complain
);
21408 if (excessive_deduction_depth
)
21410 if (deduction_depth
== 0)
21411 /* Reset once we're all the way out. */
21412 excessive_deduction_depth
= false;
21418 /* Adjust types before performing type deduction, as described in
21419 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21420 sections are symmetric. PARM is the type of a function parameter
21421 or the return type of the conversion function. ARG is the type of
21422 the argument passed to the call, or the type of the value
21423 initialized with the result of the conversion function.
21424 ARG_EXPR is the original argument expression, which may be null. */
21427 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21440 /* Swap PARM and ARG throughout the remainder of this
21441 function; the handling is precisely symmetric since PARM
21442 will initialize ARG rather than vice versa. */
21443 std::swap (parm
, arg
);
21447 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21448 too, but here handle it by stripping the reference from PARM
21449 rather than by adding it to ARG. */
21450 if (TYPE_REF_P (*parm
)
21451 && TYPE_REF_IS_RVALUE (*parm
)
21452 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21453 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21454 && TYPE_REF_P (*arg
)
21455 && !TYPE_REF_IS_RVALUE (*arg
))
21456 *parm
= TREE_TYPE (*parm
);
21457 /* Nothing else to do in this case. */
21461 gcc_unreachable ();
21464 if (!TYPE_REF_P (*parm
))
21466 /* [temp.deduct.call]
21468 If P is not a reference type:
21470 --If A is an array type, the pointer type produced by the
21471 array-to-pointer standard conversion (_conv.array_) is
21472 used in place of A for type deduction; otherwise,
21474 --If A is a function type, the pointer type produced by
21475 the function-to-pointer standard conversion
21476 (_conv.func_) is used in place of A for type deduction;
21479 --If A is a cv-qualified type, the top level
21480 cv-qualifiers of A's type are ignored for type
21482 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21483 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21484 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21485 *arg
= build_pointer_type (*arg
);
21487 *arg
= TYPE_MAIN_VARIANT (*arg
);
21490 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21491 reference to a cv-unqualified template parameter that does not represent a
21492 template parameter of a class template (during class template argument
21493 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21494 an lvalue, the type "lvalue reference to A" is used in place of A for type
21496 if (TYPE_REF_P (*parm
)
21497 && TYPE_REF_IS_RVALUE (*parm
)
21498 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21499 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21500 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21501 && (arg_expr
? lvalue_p (arg_expr
)
21502 /* try_one_overload doesn't provide an arg_expr, but
21503 functions are always lvalues. */
21504 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21505 *arg
= build_reference_type (*arg
);
21507 /* [temp.deduct.call]
21509 If P is a cv-qualified type, the top level cv-qualifiers
21510 of P's type are ignored for type deduction. If P is a
21511 reference type, the type referred to by P is used for
21513 *parm
= TYPE_MAIN_VARIANT (*parm
);
21514 if (TYPE_REF_P (*parm
))
21516 *parm
= TREE_TYPE (*parm
);
21517 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21520 /* DR 322. For conversion deduction, remove a reference type on parm
21521 too (which has been swapped into ARG). */
21522 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21523 *arg
= TREE_TYPE (*arg
);
21528 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21529 template which doesn't contain any deducible template parameters; check if
21530 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21531 unify_one_argument. */
21534 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21535 int flags
, struct conversion
**conv_p
,
21541 type
= TREE_TYPE (arg
);
21545 if (same_type_p (parm
, type
))
21546 return unify_success (explain_p
);
21548 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21549 if (strict
== DEDUCE_CONV
)
21551 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21552 return unify_success (explain_p
);
21554 else if (strict
!= DEDUCE_EXACT
)
21557 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21559 /* Avoid recalculating this in add_function_candidate. */
21561 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21563 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21565 return unify_success (explain_p
);
21568 if (strict
== DEDUCE_EXACT
)
21569 return unify_type_mismatch (explain_p
, parm
, arg
);
21571 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21574 static bool uses_deducible_template_parms (tree type
);
21576 /* Returns true iff the expression EXPR is one from which a template
21577 argument can be deduced. In other words, if it's an undecorated
21578 use of a template non-type parameter. */
21581 deducible_expression (tree expr
)
21583 /* Strip implicit conversions. */
21584 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21585 expr
= TREE_OPERAND (expr
, 0);
21586 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21589 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21590 deducible way; that is, if it has a max value of <PARM> - 1. */
21593 deducible_array_bound (tree domain
)
21595 if (domain
== NULL_TREE
)
21598 tree max
= TYPE_MAX_VALUE (domain
);
21599 if (TREE_CODE (max
) != MINUS_EXPR
)
21602 return deducible_expression (TREE_OPERAND (max
, 0));
21605 /* Returns true iff the template arguments ARGS use a template parameter
21606 in a deducible way. */
21609 deducible_template_args (tree args
)
21611 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21614 tree elt
= TREE_VEC_ELT (args
, i
);
21615 if (ARGUMENT_PACK_P (elt
))
21616 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21619 if (PACK_EXPANSION_P (elt
))
21620 elt
= PACK_EXPANSION_PATTERN (elt
);
21621 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21623 else if (TYPE_P (elt
))
21624 deducible
= uses_deducible_template_parms (elt
);
21626 deducible
= deducible_expression (elt
);
21634 /* Returns true iff TYPE contains any deducible references to template
21635 parameters, as per 14.8.2.5. */
21638 uses_deducible_template_parms (tree type
)
21640 if (PACK_EXPANSION_P (type
))
21641 type
= PACK_EXPANSION_PATTERN (type
);
21648 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21649 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21655 if (INDIRECT_TYPE_P (type
))
21656 return uses_deducible_template_parms (TREE_TYPE (type
));
21658 /* T[integer-constant ]
21660 if (TREE_CODE (type
) == ARRAY_TYPE
)
21661 return (uses_deducible_template_parms (TREE_TYPE (type
))
21662 || deducible_array_bound (TYPE_DOMAIN (type
)));
21674 if (TYPE_PTRMEM_P (type
))
21675 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21676 || (uses_deducible_template_parms
21677 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21679 /* template-name <T> (where template-name refers to a class template)
21680 template-name <i> (where template-name refers to a class template) */
21681 if (CLASS_TYPE_P (type
)
21682 && CLASSTYPE_TEMPLATE_INFO (type
)
21683 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21684 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21685 (CLASSTYPE_TI_ARGS (type
)));
21690 if (FUNC_OR_METHOD_TYPE_P (type
))
21692 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21694 tree parm
= TYPE_ARG_TYPES (type
);
21695 if (TREE_CODE (type
) == METHOD_TYPE
)
21696 parm
= TREE_CHAIN (parm
);
21697 for (; parm
; parm
= TREE_CHAIN (parm
))
21698 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21705 /* Subroutine of type_unification_real and unify_pack_expansion to
21706 handle unification of a single P/A pair. Parameters are as
21707 for those functions. */
21710 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21711 int subr
, unification_kind_t strict
,
21714 tree arg_expr
= NULL_TREE
;
21717 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21718 return unify_invalid (explain_p
);
21719 if (arg
== unknown_type_node
)
21720 /* We can't deduce anything from this, but we might get all the
21721 template args from other function args. */
21722 return unify_success (explain_p
);
21724 /* Implicit conversions (Clause 4) will be performed on a function
21725 argument to convert it to the type of the corresponding function
21726 parameter if the parameter type contains no template-parameters that
21727 participate in template argument deduction. */
21728 if (strict
!= DEDUCE_EXACT
21729 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21730 /* For function parameters with no deducible template parameters,
21731 just return. We'll check non-dependent conversions later. */
21732 return unify_success (explain_p
);
21737 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21738 | UNIFY_ALLOW_MORE_CV_QUAL
21739 | UNIFY_ALLOW_DERIVED
);
21743 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21747 arg_strict
= UNIFY_ALLOW_NONE
;
21751 gcc_unreachable ();
21754 /* We only do these transformations if this is the top-level
21755 parameter_type_list in a call or declaration matching; in other
21756 situations (nested function declarators, template argument lists) we
21757 won't be comparing a type to an expression, and we don't do any type
21763 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21764 if (type_unknown_p (arg
))
21766 /* [temp.deduct.type] A template-argument can be
21767 deduced from a pointer to function or pointer
21768 to member function argument if the set of
21769 overloaded functions does not contain function
21770 templates and at most one of a set of
21771 overloaded functions provides a unique
21773 resolve_overloaded_unification (tparms
, targs
, parm
,
21775 arg_strict
, explain_p
);
21776 /* If a unique match was not found, this is a
21777 non-deduced context, so we still succeed. */
21778 return unify_success (explain_p
);
21782 arg
= unlowered_expr_type (arg
);
21783 if (arg
== error_mark_node
)
21784 return unify_invalid (explain_p
);
21788 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21791 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21792 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21793 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21795 /* For deduction from an init-list we need the actual list. */
21796 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21798 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21801 /* for_each_template_parm callback that always returns 0. */
21804 zero_r (tree
, void *)
21809 /* for_each_template_parm any_fn callback to handle deduction of a template
21810 type argument from the type of an array bound. */
21813 array_deduction_r (tree t
, void *data
)
21815 tree_pair_p d
= (tree_pair_p
)data
;
21816 tree
&tparms
= d
->purpose
;
21817 tree
&targs
= d
->value
;
21819 if (TREE_CODE (t
) == ARRAY_TYPE
)
21820 if (tree dom
= TYPE_DOMAIN (t
))
21821 if (tree max
= TYPE_MAX_VALUE (dom
))
21823 if (TREE_CODE (max
) == MINUS_EXPR
)
21824 max
= TREE_OPERAND (max
, 0);
21825 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21826 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21827 UNIFY_ALLOW_NONE
, /*explain*/false);
21830 /* Keep walking. */
21834 /* Try to deduce any not-yet-deduced template type arguments from the type of
21835 an array bound. This is handled separately from unify because 14.8.2.5 says
21836 "The type of a type parameter is only deduced from an array bound if it is
21837 not otherwise deduced." */
21840 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21842 tree_pair_s data
= { tparms
, targs
};
21843 hash_set
<tree
> visited
;
21844 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21845 /*nondeduced*/false, array_deduction_r
);
21848 /* Most parms like fn_type_unification.
21850 If SUBR is 1, we're being called recursively (to unify the
21851 arguments of a function or method parameter of a function
21854 CHECKS is a pointer to a vector of access checks encountered while
21855 substituting default template arguments. */
21858 type_unification_real (tree tparms
,
21862 unsigned int xnargs
,
21864 unification_kind_t strict
,
21865 vec
<deferred_access_check
, va_gc
> **checks
,
21870 int ntparms
= TREE_VEC_LENGTH (tparms
);
21871 int saw_undeduced
= 0;
21874 unsigned int nargs
;
21877 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21878 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21879 gcc_assert (ntparms
> 0);
21881 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21883 /* Reset the number of non-defaulted template arguments contained
21885 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21893 while (parms
&& parms
!= void_list_node
21896 parm
= TREE_VALUE (parms
);
21898 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21899 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21900 /* For a function parameter pack that occurs at the end of the
21901 parameter-declaration-list, the type A of each remaining
21902 argument of the call is compared with the type P of the
21903 declarator-id of the function parameter pack. */
21906 parms
= TREE_CHAIN (parms
);
21908 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21909 /* For a function parameter pack that does not occur at the
21910 end of the parameter-declaration-list, the type of the
21911 parameter pack is a non-deduced context. */
21917 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21923 && parms
!= void_list_node
21924 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21926 /* Unify the remaining arguments with the pack expansion type. */
21928 tree parmvec
= make_tree_vec (1);
21930 /* Allocate a TREE_VEC and copy in all of the arguments */
21931 argvec
= make_tree_vec (nargs
- ia
);
21932 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21933 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21935 /* Copy the parameter into parmvec. */
21936 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21937 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21938 /*subr=*/subr
, explain_p
))
21941 /* Advance to the end of the list of parameters. */
21942 parms
= TREE_CHAIN (parms
);
21945 /* Fail if we've reached the end of the parm list, and more args
21946 are present, and the parm list isn't variadic. */
21947 if (ia
< nargs
&& parms
== void_list_node
)
21948 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21949 /* Fail if parms are left and they don't have default values and
21950 they aren't all deduced as empty packs (c++/57397). This is
21951 consistent with sufficient_parms_p. */
21952 if (parms
&& parms
!= void_list_node
21953 && TREE_PURPOSE (parms
) == NULL_TREE
)
21955 unsigned int count
= nargs
;
21960 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21963 p
= TREE_CHAIN (p
);
21965 while (p
&& p
!= void_list_node
);
21966 if (count
!= nargs
)
21967 return unify_too_few_arguments (explain_p
, ia
, count
,
21973 tsubst_flags_t complain
= (explain_p
21974 ? tf_warning_or_error
21976 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21978 for (i
= 0; i
< ntparms
; i
++)
21980 tree targ
= TREE_VEC_ELT (targs
, i
);
21981 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21983 /* Clear the "incomplete" flags on all argument packs now so that
21984 substituting them into later default arguments works. */
21985 if (targ
&& ARGUMENT_PACK_P (targ
))
21987 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21988 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21991 if (targ
|| tparm
== error_mark_node
)
21993 tparm
= TREE_VALUE (tparm
);
21995 if (TREE_CODE (tparm
) == TYPE_DECL
21996 && !tried_array_deduction
)
21998 try_array_deduction (tparms
, targs
, xparms
);
21999 tried_array_deduction
= true;
22000 if (TREE_VEC_ELT (targs
, i
))
22004 /* If this is an undeduced nontype parameter that depends on
22005 a type parameter, try another pass; its type may have been
22006 deduced from a later argument than the one from which
22007 this parameter can be deduced. */
22008 if (TREE_CODE (tparm
) == PARM_DECL
22009 && uses_template_parms (TREE_TYPE (tparm
))
22010 && saw_undeduced
< 2)
22016 /* Core issue #226 (C++0x) [temp.deduct]:
22018 If a template argument has not been deduced, its
22019 default template argument, if any, is used.
22021 When we are in C++98 mode, TREE_PURPOSE will either
22022 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22023 to explicitly check cxx_dialect here. */
22024 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22025 /* OK, there is a default argument. Wait until after the
22026 conversion check to do substitution. */
22029 /* If the type parameter is a parameter pack, then it will
22030 be deduced to an empty parameter pack. */
22031 if (template_parameter_pack_p (tparm
))
22035 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22037 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22038 TREE_CONSTANT (arg
) = 1;
22041 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22043 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22045 TREE_VEC_ELT (targs
, i
) = arg
;
22049 return unify_parameter_deduction_failure (explain_p
, tparm
);
22052 /* Now substitute into the default template arguments. */
22053 for (i
= 0; i
< ntparms
; i
++)
22055 tree targ
= TREE_VEC_ELT (targs
, i
);
22056 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22058 if (targ
|| tparm
== error_mark_node
)
22060 tree parm
= TREE_VALUE (tparm
);
22061 tree arg
= TREE_PURPOSE (tparm
);
22062 reopen_deferring_access_checks (*checks
);
22063 location_t save_loc
= input_location
;
22065 input_location
= DECL_SOURCE_LOCATION (parm
);
22067 if (saw_undeduced
== 1
22068 && TREE_CODE (parm
) == PARM_DECL
22069 && uses_template_parms (TREE_TYPE (parm
)))
22071 /* The type of this non-type parameter depends on undeduced
22072 parameters. Don't try to use its default argument yet,
22073 since we might deduce an argument for it on the next pass,
22074 but do check whether the arguments we already have cause
22075 substitution failure, so that that happens before we try
22076 later default arguments (78489). */
22077 ++processing_template_decl
;
22078 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22080 --processing_template_decl
;
22081 if (type
== error_mark_node
)
22082 arg
= error_mark_node
;
22088 /* Even if the call is happening in template context, getting
22089 here means it's non-dependent, and a default argument is
22090 considered a separate definition under [temp.decls], so we can
22091 do this substitution without processing_template_decl. This
22092 is important if the default argument contains something that
22093 might be instantiation-dependent like access (87480). */
22094 processing_template_decl_sentinel s
;
22095 tree substed
= NULL_TREE
;
22096 if (saw_undeduced
== 1)
22098 /* First instatiate in template context, in case we still
22099 depend on undeduced template parameters. */
22100 ++processing_template_decl
;
22101 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22103 --processing_template_decl
;
22104 if (substed
!= error_mark_node
22105 && !uses_template_parms (substed
))
22106 /* We replaced all the tparms, substitute again out of
22107 template context. */
22108 substed
= NULL_TREE
;
22111 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22114 if (!uses_template_parms (substed
))
22115 arg
= convert_template_argument (parm
, substed
, full_targs
,
22116 complain
, i
, NULL_TREE
);
22117 else if (saw_undeduced
== 1)
22120 arg
= error_mark_node
;
22123 input_location
= save_loc
;
22124 *checks
= get_deferred_access_checks ();
22125 pop_deferring_access_checks ();
22127 if (arg
== error_mark_node
)
22131 TREE_VEC_ELT (targs
, i
) = arg
;
22132 /* The position of the first default template argument,
22133 is also the number of non-defaulted arguments in TARGS.
22135 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22136 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22140 if (saw_undeduced
++ == 1)
22144 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22145 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22147 return unify_success (explain_p
);
22150 /* Subroutine of type_unification_real. Args are like the variables
22151 at the call site. ARG is an overloaded function (or template-id);
22152 we try deducing template args from each of the overloads, and if
22153 only one succeeds, we go with that. Modifies TARGS and returns
22154 true on success. */
22157 resolve_overloaded_unification (tree tparms
,
22161 unification_kind_t strict
,
22165 tree tempargs
= copy_node (targs
);
22167 tree goodfn
= NULL_TREE
;
22170 if (TREE_CODE (arg
) == ADDR_EXPR
)
22172 arg
= TREE_OPERAND (arg
, 0);
22178 if (TREE_CODE (arg
) == COMPONENT_REF
)
22179 /* Handle `&x' where `x' is some static or non-static member
22181 arg
= TREE_OPERAND (arg
, 1);
22183 if (TREE_CODE (arg
) == OFFSET_REF
)
22184 arg
= TREE_OPERAND (arg
, 1);
22186 /* Strip baselink information. */
22187 if (BASELINK_P (arg
))
22188 arg
= BASELINK_FUNCTIONS (arg
);
22190 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22192 /* If we got some explicit template args, we need to plug them into
22193 the affected templates before we try to unify, in case the
22194 explicit args will completely resolve the templates in question. */
22197 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22198 arg
= TREE_OPERAND (arg
, 0);
22200 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22203 tree subargs
, elem
;
22205 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22208 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22209 expl_subargs
, NULL_TREE
, tf_none
,
22210 /*require_all_args=*/true,
22211 /*use_default_args=*/true);
22212 if (subargs
!= error_mark_node
22213 && !any_dependent_template_arguments_p (subargs
))
22215 fn
= instantiate_template (fn
, subargs
, tf_none
);
22216 if (!constraints_satisfied_p (fn
))
22218 if (undeduced_auto_decl (fn
))
22220 /* Instantiate the function to deduce its return type. */
22222 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22226 elem
= TREE_TYPE (fn
);
22227 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22228 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22229 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22238 /* If no templates (or more than one) are fully resolved by the
22239 explicit arguments, this template-id is a non-deduced context; it
22240 could still be OK if we deduce all template arguments for the
22241 enclosing call through other arguments. */
22245 else if (!OVL_P (arg
))
22246 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22247 -- but the deduction does not succeed because the expression is
22248 not just the function on its own. */
22251 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22254 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22255 strict
, sub_strict
, addr_p
, explain_p
)
22256 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22263 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22264 to function or pointer to member function argument if the set of
22265 overloaded functions does not contain function templates and at most
22266 one of a set of overloaded functions provides a unique match.
22268 So if we found multiple possibilities, we return success but don't
22269 deduce anything. */
22273 int i
= TREE_VEC_LENGTH (targs
);
22275 if (TREE_VEC_ELT (tempargs
, i
))
22277 tree old
= TREE_VEC_ELT (targs
, i
);
22278 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22279 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22280 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22281 /* Don't forget explicit template arguments in a pack. */
22282 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22283 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22284 TREE_VEC_ELT (targs
, i
) = new_
;
22293 /* Core DR 115: In contexts where deduction is done and fails, or in
22294 contexts where deduction is not done, if a template argument list is
22295 specified and it, along with any default template arguments, identifies
22296 a single function template specialization, then the template-id is an
22297 lvalue for the function template specialization. */
22300 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22302 tree expr
, offset
, baselink
;
22305 if (!type_unknown_p (orig_expr
))
22310 offset
= NULL_TREE
;
22311 baselink
= NULL_TREE
;
22313 if (TREE_CODE (expr
) == ADDR_EXPR
)
22315 expr
= TREE_OPERAND (expr
, 0);
22318 if (TREE_CODE (expr
) == OFFSET_REF
)
22321 expr
= TREE_OPERAND (expr
, 1);
22323 if (BASELINK_P (expr
))
22326 expr
= BASELINK_FUNCTIONS (expr
);
22329 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22332 tree goodfn
= NULL_TREE
;
22334 /* If we got some explicit template args, we need to plug them into
22335 the affected templates before we try to unify, in case the
22336 explicit args will completely resolve the templates in question. */
22338 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22339 tree arg
= TREE_OPERAND (expr
, 0);
22340 tree badfn
= NULL_TREE
;
22341 tree badargs
= NULL_TREE
;
22343 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22346 tree subargs
, elem
;
22348 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22351 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22352 expl_subargs
, NULL_TREE
, tf_none
,
22353 /*require_all_args=*/true,
22354 /*use_default_args=*/true);
22355 if (subargs
!= error_mark_node
22356 && !any_dependent_template_arguments_p (subargs
))
22358 elem
= instantiate_template (fn
, subargs
, tf_none
);
22359 if (elem
== error_mark_node
)
22364 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22365 && constraints_satisfied_p (elem
))
22374 mark_used (goodfn
);
22377 expr
= build_baselink (BASELINK_BINFO (baselink
),
22378 BASELINK_ACCESS_BINFO (baselink
),
22379 expr
, BASELINK_OPTYPE (baselink
));
22383 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22384 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22387 expr
= cp_build_addr_expr (expr
, complain
);
22390 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22391 /* There were no good options and at least one bad one, so let the
22392 user know what the problem is. */
22393 instantiate_template (badfn
, badargs
, complain
);
22398 /* As above, but error out if the expression remains overloaded. */
22401 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22403 exp
= resolve_nondeduced_context (exp
, complain
);
22404 if (type_unknown_p (exp
))
22406 if (complain
& tf_error
)
22407 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22408 return error_mark_node
;
22413 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22414 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22415 different overloads deduce different arguments for a given parm.
22416 ADDR_P is true if the expression for which deduction is being
22417 performed was of the form "& fn" rather than simply "fn".
22419 Returns 1 on success. */
22422 try_one_overload (tree tparms
,
22427 unification_kind_t strict
,
22436 if (arg
== error_mark_node
)
22439 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22440 to function or pointer to member function argument if the set of
22441 overloaded functions does not contain function templates and at most
22442 one of a set of overloaded functions provides a unique match.
22444 So if this is a template, just return success. */
22446 if (uses_template_parms (arg
))
22449 if (TREE_CODE (arg
) == METHOD_TYPE
)
22450 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22452 arg
= build_pointer_type (arg
);
22454 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22456 /* We don't copy orig_targs for this because if we have already deduced
22457 some template args from previous args, unify would complain when we
22458 try to deduce a template parameter for the same argument, even though
22459 there isn't really a conflict. */
22460 nargs
= TREE_VEC_LENGTH (targs
);
22461 tempargs
= make_tree_vec (nargs
);
22463 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22466 /* First make sure we didn't deduce anything that conflicts with
22467 explicitly specified args. */
22468 for (i
= nargs
; i
--; )
22470 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22471 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22475 else if (uses_template_parms (elt
))
22476 /* Since we're unifying against ourselves, we will fill in
22477 template args used in the function parm list with our own
22478 template parms. Discard them. */
22479 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22480 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22482 /* Check that the argument at each index of the deduced argument pack
22483 is equivalent to the corresponding explicitly specified argument.
22484 We may have deduced more arguments than were explicitly specified,
22487 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22488 that's wrong if we deduce the same argument pack from multiple
22489 function arguments: it's only incomplete the first time. */
22491 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22492 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22494 if (TREE_VEC_LENGTH (deduced_pack
)
22495 < TREE_VEC_LENGTH (explicit_pack
))
22498 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22499 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22500 TREE_VEC_ELT (deduced_pack
, j
)))
22503 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22507 for (i
= nargs
; i
--; )
22509 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22512 TREE_VEC_ELT (targs
, i
) = elt
;
22518 /* PARM is a template class (perhaps with unbound template
22519 parameters). ARG is a fully instantiated type. If ARG can be
22520 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22521 TARGS are as for unify. */
22524 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22527 tree copy_of_targs
;
22529 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22531 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22532 /* Matches anything. */;
22533 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22534 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22537 /* We need to make a new template argument vector for the call to
22538 unify. If we used TARGS, we'd clutter it up with the result of
22539 the attempted unification, even if this class didn't work out.
22540 We also don't want to commit ourselves to all the unifications
22541 we've already done, since unification is supposed to be done on
22542 an argument-by-argument basis. In other words, consider the
22543 following pathological case:
22545 template <int I, int J, int K>
22548 template <int I, int J>
22549 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22551 template <int I, int J, int K>
22552 void f(S<I, J, K>, S<I, I, I>);
22561 Now, by the time we consider the unification involving `s2', we
22562 already know that we must have `f<0, 0, 0>'. But, even though
22563 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22564 because there are two ways to unify base classes of S<0, 1, 2>
22565 with S<I, I, I>. If we kept the already deduced knowledge, we
22566 would reject the possibility I=1. */
22567 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22569 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22571 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22576 /* If unification failed, we're done. */
22577 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22578 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22584 /* Given a template type PARM and a class type ARG, find the unique
22585 base type in ARG that is an instance of PARM. We do not examine
22586 ARG itself; only its base-classes. If there is not exactly one
22587 appropriate base class, return NULL_TREE. PARM may be the type of
22588 a partial specialization, as well as a plain template type. Used
22591 static enum template_base_result
22592 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22593 bool explain_p
, tree
*result
)
22595 tree rval
= NULL_TREE
;
22598 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22600 binfo
= TYPE_BINFO (complete_type (arg
));
22603 /* The type could not be completed. */
22604 *result
= NULL_TREE
;
22605 return tbr_incomplete_type
;
22608 /* Walk in inheritance graph order. The search order is not
22609 important, and this avoids multiple walks of virtual bases. */
22610 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22612 tree r
= try_class_unification (tparms
, targs
, parm
,
22613 BINFO_TYPE (binfo
), explain_p
);
22617 /* If there is more than one satisfactory baseclass, then:
22621 If they yield more than one possible deduced A, the type
22625 if (rval
&& !same_type_p (r
, rval
))
22627 *result
= NULL_TREE
;
22628 return tbr_ambiguous_baseclass
;
22636 return tbr_success
;
22639 /* Returns the level of DECL, which declares a template parameter. */
22642 template_decl_level (tree decl
)
22644 switch (TREE_CODE (decl
))
22647 case TEMPLATE_DECL
:
22648 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22651 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22654 gcc_unreachable ();
22659 /* Decide whether ARG can be unified with PARM, considering only the
22660 cv-qualifiers of each type, given STRICT as documented for unify.
22661 Returns nonzero iff the unification is OK on that basis. */
22664 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22666 int arg_quals
= cp_type_quals (arg
);
22667 int parm_quals
= cp_type_quals (parm
);
22669 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22670 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22672 /* Although a CVR qualifier is ignored when being applied to a
22673 substituted template parameter ([8.3.2]/1 for example), that
22674 does not allow us to unify "const T" with "int&" because both
22675 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22676 It is ok when we're allowing additional CV qualifiers
22677 at the outer level [14.8.2.1]/3,1st bullet. */
22678 if ((TYPE_REF_P (arg
)
22679 || FUNC_OR_METHOD_TYPE_P (arg
))
22680 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22683 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22684 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22688 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22689 && (arg_quals
& parm_quals
) != parm_quals
)
22692 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22693 && (parm_quals
& arg_quals
) != arg_quals
)
22699 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22701 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22703 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22704 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22705 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22707 *index
= TEMPLATE_TYPE_IDX (parm
);
22708 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22712 *index
= TEMPLATE_PARM_IDX (parm
);
22713 *level
= TEMPLATE_PARM_LEVEL (parm
);
22717 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22719 if (unify (TP, TA, P, A, S, EP)) \
22723 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22724 expansion at the end of PACKED_PARMS. Returns 0 if the type
22725 deduction succeeds, 1 otherwise. STRICT is the same as in
22726 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22727 function call argument list. We'll need to adjust the arguments to make them
22728 types. SUBR tells us if this is from a recursive call to
22729 type_unification_real, or for comparing two template argument
22733 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22734 tree packed_args
, unification_kind_t strict
,
22735 bool subr
, bool explain_p
)
22738 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22739 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22740 tree pack
, packs
= NULL_TREE
;
22741 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22743 /* Add in any args remembered from an earlier partial instantiation. */
22744 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22745 int levels
= TMPL_ARGS_DEPTH (targs
);
22747 packed_args
= expand_template_argument_pack (packed_args
);
22749 int len
= TREE_VEC_LENGTH (packed_args
);
22751 /* Determine the parameter packs we will be deducing from the
22752 pattern, and record their current deductions. */
22753 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22754 pack
; pack
= TREE_CHAIN (pack
))
22756 tree parm_pack
= TREE_VALUE (pack
);
22759 /* Only template parameter packs can be deduced, not e.g. function
22760 parameter packs or __bases or __integer_pack. */
22761 if (!TEMPLATE_PARM_P (parm_pack
))
22764 /* Determine the index and level of this parameter pack. */
22765 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22766 if (level
< levels
)
22769 /* Keep track of the parameter packs and their corresponding
22771 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22772 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22775 /* Loop through all of the arguments that have not yet been
22776 unified and unify each with the pattern. */
22777 for (i
= start
; i
< len
; i
++)
22780 bool any_explicit
= false;
22781 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22783 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22784 or the element of its argument pack at the current index if
22785 this argument was explicitly specified. */
22786 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22790 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22793 if (TREE_VALUE (pack
)
22794 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22795 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22797 any_explicit
= true;
22798 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22800 TMPL_ARG (targs
, level
, idx
) = arg
;
22803 /* If we had explicit template arguments, substitute them into the
22804 pattern before deduction. */
22807 /* Some arguments might still be unspecified or dependent. */
22809 ++processing_template_decl
;
22810 dependent
= any_dependent_template_arguments_p (targs
);
22812 --processing_template_decl
;
22813 parm
= tsubst (pattern
, targs
,
22814 explain_p
? tf_warning_or_error
: tf_none
,
22817 --processing_template_decl
;
22818 if (parm
== error_mark_node
)
22824 /* Unify the pattern with the current argument. */
22825 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22829 /* For each parameter pack, collect the deduced value. */
22830 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22833 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22835 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22836 TMPL_ARG (targs
, level
, idx
);
22840 /* Verify that the results of unification with the parameter packs
22841 produce results consistent with what we've seen before, and make
22842 the deduced argument packs available. */
22843 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22845 tree old_pack
= TREE_VALUE (pack
);
22846 tree new_args
= TREE_TYPE (pack
);
22847 int i
, len
= TREE_VEC_LENGTH (new_args
);
22849 bool nondeduced_p
= false;
22851 /* By default keep the original deduced argument pack.
22852 If necessary, more specific code is going to update the
22853 resulting deduced argument later down in this function. */
22854 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22855 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22857 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22858 actually deduce anything. */
22859 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22860 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22861 nondeduced_p
= true;
22865 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22867 /* If we had fewer function args than explicit template args,
22868 just use the explicits. */
22869 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22870 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22871 if (len
< explicit_len
)
22872 new_args
= explicit_args
;
22878 /* Build the deduced *_ARGUMENT_PACK. */
22879 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22881 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22882 TREE_CONSTANT (result
) = 1;
22885 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22887 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22889 /* Note the deduced argument packs for this parameter
22891 TMPL_ARG (targs
, level
, idx
) = result
;
22893 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22894 && (ARGUMENT_PACK_ARGS (old_pack
)
22895 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22897 /* We only had the explicitly-provided arguments before, but
22898 now we have a complete set of arguments. */
22899 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22901 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22902 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22903 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22907 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22908 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22909 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22910 /* During template argument deduction for the aggregate deduction
22911 candidate, the number of elements in a trailing parameter pack
22912 is only deduced from the number of remaining function
22913 arguments if it is not otherwise deduced. */
22914 if (cxx_dialect
>= cxx20
22915 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
22916 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
22917 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
22918 if (!comp_template_args (old_args
, new_args
,
22919 &bad_old_arg
, &bad_new_arg
))
22920 /* Inconsistent unification of this parameter pack. */
22921 return unify_parameter_pack_inconsistent (explain_p
,
22927 return unify_success (explain_p
);
22930 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22931 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22932 parameters and return value are as for unify. */
22935 unify_array_domain (tree tparms
, tree targs
,
22936 tree parm_dom
, tree arg_dom
,
22944 /* Our representation of array types uses "N - 1" as the
22945 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22946 not an integer constant. We cannot unify arbitrarily
22947 complex expressions, so we eliminate the MINUS_EXPRs
22949 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22950 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22953 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22954 parm_max
= TREE_OPERAND (parm_max
, 0);
22956 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22957 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22960 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22961 trying to unify the type of a variable with the type
22962 of a template parameter. For example:
22964 template <unsigned int N>
22965 void f (char (&) [N]);
22972 Here, the type of the ARG will be "int [g(i)]", and
22973 may be a SAVE_EXPR, etc. */
22974 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22975 return unify_vla_arg (explain_p
, arg_dom
);
22976 arg_max
= TREE_OPERAND (arg_max
, 0);
22979 /* If only one of the bounds used a MINUS_EXPR, compensate
22980 by adding one to the other bound. */
22981 if (parm_cst
&& !arg_cst
)
22982 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22986 else if (arg_cst
&& !parm_cst
)
22987 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22992 return unify (tparms
, targs
, parm_max
, arg_max
,
22993 UNIFY_ALLOW_INTEGER
, explain_p
);
22996 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22998 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23003 if (PACK_EXPANSION_P (t
))
23004 t
= PACK_EXPANSION_PATTERN (t
);
23005 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23006 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23007 || DECL_TYPE_TEMPLATE_P (t
))
23009 else if (TYPE_P (t
))
23015 /* Deduce the value of template parameters. TPARMS is the (innermost)
23016 set of template parameters to a template. TARGS is the bindings
23017 for those template parameters, as determined thus far; TARGS may
23018 include template arguments for outer levels of template parameters
23019 as well. PARM is a parameter to a template function, or a
23020 subcomponent of that parameter; ARG is the corresponding argument.
23021 This function attempts to match PARM with ARG in a manner
23022 consistent with the existing assignments in TARGS. If more values
23023 are deduced, then TARGS is updated.
23025 Returns 0 if the type deduction succeeds, 1 otherwise. The
23026 parameter STRICT is a bitwise or of the following flags:
23029 Require an exact match between PARM and ARG.
23030 UNIFY_ALLOW_MORE_CV_QUAL:
23031 Allow the deduced ARG to be more cv-qualified (by qualification
23032 conversion) than ARG.
23033 UNIFY_ALLOW_LESS_CV_QUAL:
23034 Allow the deduced ARG to be less cv-qualified than ARG.
23035 UNIFY_ALLOW_DERIVED:
23036 Allow the deduced ARG to be a template base class of ARG,
23037 or a pointer to a template base class of the type pointed to by
23039 UNIFY_ALLOW_INTEGER:
23040 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23041 case for more information.
23042 UNIFY_ALLOW_OUTER_LEVEL:
23043 This is the outermost level of a deduction. Used to determine validity
23044 of qualification conversions. A valid qualification conversion must
23045 have const qualified pointers leading up to the inner type which
23046 requires additional CV quals, except at the outer level, where const
23047 is not required [conv.qual]. It would be normal to set this flag in
23048 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23049 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23050 This is the outermost level of a deduction, and PARM can be more CV
23051 qualified at this point.
23052 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23053 This is the outermost level of a deduction, and PARM can be less CV
23054 qualified at this point. */
23057 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23063 int strict_in
= strict
;
23064 tsubst_flags_t complain
= (explain_p
23065 ? tf_warning_or_error
23068 /* I don't think this will do the right thing with respect to types.
23069 But the only case I've seen it in so far has been array bounds, where
23070 signedness is the only information lost, and I think that will be
23071 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23072 finish_id_expression_1, and are also OK. */
23073 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23074 parm
= TREE_OPERAND (parm
, 0);
23076 if (arg
== error_mark_node
)
23077 return unify_invalid (explain_p
);
23078 if (arg
== unknown_type_node
23079 || arg
== init_list_type_node
)
23080 /* We can't deduce anything from this, but we might get all the
23081 template args from other function args. */
23082 return unify_success (explain_p
);
23084 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23085 return unify_success (explain_p
);
23087 /* If PARM uses template parameters, then we can't bail out here,
23088 even if ARG == PARM, since we won't record unifications for the
23089 template parameters. We might need them if we're trying to
23090 figure out which of two things is more specialized. */
23091 if (arg
== parm
&& !uses_template_parms (parm
))
23092 return unify_success (explain_p
);
23094 /* Handle init lists early, so the rest of the function can assume
23095 we're dealing with a type. */
23096 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23100 tree orig_parm
= parm
;
23102 if (!is_std_init_list (parm
)
23103 && TREE_CODE (parm
) != ARRAY_TYPE
)
23104 /* We can only deduce from an initializer list argument if the
23105 parameter is std::initializer_list or an array; otherwise this
23106 is a non-deduced context. */
23107 return unify_success (explain_p
);
23109 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23110 elttype
= TREE_TYPE (parm
);
23113 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23114 /* Deduction is defined in terms of a single type, so just punt
23115 on the (bizarre) std::initializer_list<T...>. */
23116 if (PACK_EXPANSION_P (elttype
))
23117 return unify_success (explain_p
);
23120 if (strict
!= DEDUCE_EXACT
23121 && TYPE_P (elttype
)
23122 && !uses_deducible_template_parms (elttype
))
23123 /* If ELTTYPE has no deducible template parms, skip deduction from
23124 the list elements. */;
23126 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23128 int elt_strict
= strict
;
23130 if (elt
== error_mark_node
)
23131 return unify_invalid (explain_p
);
23133 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23135 tree type
= TREE_TYPE (elt
);
23136 if (type
== error_mark_node
)
23137 return unify_invalid (explain_p
);
23138 /* It should only be possible to get here for a call. */
23139 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23140 elt_strict
|= maybe_adjust_types_for_deduction
23141 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23145 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23149 if (TREE_CODE (parm
) == ARRAY_TYPE
23150 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23152 /* Also deduce from the length of the initializer list. */
23153 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23154 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23155 if (idx
== error_mark_node
)
23156 return unify_invalid (explain_p
);
23157 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23161 /* If the std::initializer_list<T> deduction worked, replace the
23162 deduced A with std::initializer_list<A>. */
23163 if (orig_parm
!= parm
)
23165 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23166 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23167 targ
= listify (targ
);
23168 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23170 return unify_success (explain_p
);
23173 /* If parm and arg aren't the same kind of thing (template, type, or
23174 expression), fail early. */
23175 if (pa_kind (parm
) != pa_kind (arg
))
23176 return unify_invalid (explain_p
);
23178 /* Immediately reject some pairs that won't unify because of
23179 cv-qualification mismatches. */
23180 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23182 /* It is the elements of the array which hold the cv quals of an array
23183 type, and the elements might be template type parms. We'll check
23184 when we recurse. */
23185 && TREE_CODE (arg
) != ARRAY_TYPE
23186 /* We check the cv-qualifiers when unifying with template type
23187 parameters below. We want to allow ARG `const T' to unify with
23188 PARM `T' for example, when computing which of two templates
23189 is more specialized, for example. */
23190 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23191 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23192 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23194 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23195 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23196 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23197 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23198 strict
&= ~UNIFY_ALLOW_DERIVED
;
23199 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23200 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23202 switch (TREE_CODE (parm
))
23204 case TYPENAME_TYPE
:
23206 case UNBOUND_CLASS_TEMPLATE
:
23207 /* In a type which contains a nested-name-specifier, template
23208 argument values cannot be deduced for template parameters used
23209 within the nested-name-specifier. */
23210 return unify_success (explain_p
);
23212 case TEMPLATE_TYPE_PARM
:
23213 case TEMPLATE_TEMPLATE_PARM
:
23214 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23215 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23216 if (error_operand_p (tparm
))
23217 return unify_invalid (explain_p
);
23219 if (TEMPLATE_TYPE_LEVEL (parm
)
23220 != template_decl_level (tparm
))
23221 /* The PARM is not one we're trying to unify. Just check
23222 to see if it matches ARG. */
23224 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23225 && (is_auto (parm
) ? is_auto (arg
)
23226 : same_type_p (parm
, arg
)))
23227 return unify_success (explain_p
);
23229 return unify_type_mismatch (explain_p
, parm
, arg
);
23231 idx
= TEMPLATE_TYPE_IDX (parm
);
23232 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23233 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23234 if (error_operand_p (tparm
))
23235 return unify_invalid (explain_p
);
23237 /* Check for mixed types and values. */
23238 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23239 && TREE_CODE (tparm
) != TYPE_DECL
)
23240 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23241 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23242 gcc_unreachable ();
23244 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23246 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23247 && CLASS_TYPE_P (arg
))
23249 /* First try to match ARG directly. */
23250 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23254 /* Otherwise, look for a suitable base of ARG, as below. */
23255 enum template_base_result r
;
23256 r
= get_template_base (tparms
, targs
, parm
, arg
,
23259 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23263 /* ARG must be constructed from a template class or a template
23264 template parameter. */
23265 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23266 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23267 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23269 /* Deduce arguments T, i from TT<T> or TT<i>. */
23270 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23273 arg
= TYPE_TI_TEMPLATE (arg
);
23275 /* Fall through to deduce template name. */
23278 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23279 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23281 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23283 /* Simple cases: Value already set, does match or doesn't. */
23284 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23285 return unify_success (explain_p
);
23287 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23291 /* If PARM is `const T' and ARG is only `int', we don't have
23292 a match unless we are allowing additional qualification.
23293 If ARG is `const int' and PARM is just `T' that's OK;
23294 that binds `const int' to `T'. */
23295 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23297 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23299 /* Consider the case where ARG is `const volatile int' and
23300 PARM is `const T'. Then, T should be `volatile int'. */
23301 arg
= cp_build_qualified_type_real
23302 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23303 if (arg
== error_mark_node
)
23304 return unify_invalid (explain_p
);
23306 /* Simple cases: Value already set, does match or doesn't. */
23307 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23308 return unify_success (explain_p
);
23310 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23312 /* Make sure that ARG is not a variable-sized array. (Note
23313 that were talking about variable-sized arrays (like
23314 `int[n]'), rather than arrays of unknown size (like
23315 `int[]').) We'll get very confused by such a type since
23316 the bound of the array is not constant, and therefore
23317 not mangleable. Besides, such types are not allowed in
23318 ISO C++, so we can do as we please here. We do allow
23319 them for 'auto' deduction, since that isn't ABI-exposed. */
23320 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23321 return unify_vla_arg (explain_p
, arg
);
23323 /* Strip typedefs as in convert_template_argument. */
23324 arg
= canonicalize_type_argument (arg
, tf_none
);
23327 /* If ARG is a parameter pack or an expansion, we cannot unify
23328 against it unless PARM is also a parameter pack. */
23329 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23330 && !template_parameter_pack_p (parm
))
23331 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23333 /* If the argument deduction results is a METHOD_TYPE,
23334 then there is a problem.
23335 METHOD_TYPE doesn't map to any real C++ type the result of
23336 the deduction cannot be of that type. */
23337 if (TREE_CODE (arg
) == METHOD_TYPE
)
23338 return unify_method_type_error (explain_p
, arg
);
23340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23341 return unify_success (explain_p
);
23343 case TEMPLATE_PARM_INDEX
:
23344 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23345 if (error_operand_p (tparm
))
23346 return unify_invalid (explain_p
);
23348 if (TEMPLATE_PARM_LEVEL (parm
)
23349 != template_decl_level (tparm
))
23351 /* The PARM is not one we're trying to unify. Just check
23352 to see if it matches ARG. */
23353 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23354 && cp_tree_equal (parm
, arg
));
23356 unify_expression_unequal (explain_p
, parm
, arg
);
23360 idx
= TEMPLATE_PARM_IDX (parm
);
23361 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23365 if ((strict
& UNIFY_ALLOW_INTEGER
)
23366 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23367 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23368 /* We're deducing from an array bound, the type doesn't matter. */
23369 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23370 int x
= !cp_tree_equal (targ
, arg
);
23372 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23376 /* [temp.deduct.type] If, in the declaration of a function template
23377 with a non-type template-parameter, the non-type
23378 template-parameter is used in an expression in the function
23379 parameter-list and, if the corresponding template-argument is
23380 deduced, the template-argument type shall match the type of the
23381 template-parameter exactly, except that a template-argument
23382 deduced from an array bound may be of any integral type.
23383 The non-type parameter might use already deduced type parameters. */
23384 tparm
= TREE_TYPE (parm
);
23385 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23386 /* We don't have enough levels of args to do any substitution. This
23387 can happen in the context of -fnew-ttp-matching. */;
23390 ++processing_template_decl
;
23391 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23392 --processing_template_decl
;
23394 if (tree a
= type_uses_auto (tparm
))
23396 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23397 if (tparm
== error_mark_node
)
23402 if (!TREE_TYPE (arg
))
23403 /* Template-parameter dependent expression. Just accept it for now.
23404 It will later be processed in convert_template_argument. */
23406 else if (same_type_ignoring_top_level_qualifiers_p
23407 (non_reference (TREE_TYPE (arg
)),
23408 non_reference (tparm
)))
23409 /* OK. Ignore top-level quals here because a class-type template
23410 parameter object is const. */;
23411 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23412 && CP_INTEGRAL_TYPE_P (tparm
))
23413 /* Convert the ARG to the type of PARM; the deduced non-type
23414 template argument must exactly match the types of the
23415 corresponding parameter. */
23416 arg
= fold (build_nop (tparm
, arg
));
23417 else if (uses_template_parms (tparm
))
23419 /* We haven't deduced the type of this parameter yet. */
23420 if (cxx_dialect
>= cxx17
23421 /* We deduce from array bounds in try_array_deduction. */
23422 && !(strict
& UNIFY_ALLOW_INTEGER
))
23424 /* Deduce it from the non-type argument. */
23425 tree atype
= TREE_TYPE (arg
);
23426 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23428 UNIFY_ALLOW_NONE
, explain_p
);
23431 /* Try again later. */
23432 return unify_success (explain_p
);
23435 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23437 /* If ARG is a parameter pack or an expansion, we cannot unify
23438 against it unless PARM is also a parameter pack. */
23439 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23440 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23441 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23444 bool removed_attr
= false;
23445 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23447 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23448 return unify_success (explain_p
);
23452 /* A pointer-to-member constant can be unified only with
23453 another constant. */
23454 if (TREE_CODE (arg
) != PTRMEM_CST
)
23455 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23457 /* Just unify the class member. It would be useless (and possibly
23458 wrong, depending on the strict flags) to unify also
23459 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23460 arg refer to the same variable, even if through different
23461 classes. For instance:
23463 struct A { int x; };
23466 Unification of &A::x and &B::x must succeed. */
23467 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23468 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23473 if (!TYPE_PTR_P (arg
))
23474 return unify_type_mismatch (explain_p
, parm
, arg
);
23476 /* [temp.deduct.call]
23478 A can be another pointer or pointer to member type that can
23479 be converted to the deduced A via a qualification
23480 conversion (_conv.qual_).
23482 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23483 This will allow for additional cv-qualification of the
23484 pointed-to types if appropriate. */
23486 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23487 /* The derived-to-base conversion only persists through one
23488 level of pointers. */
23489 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23491 return unify (tparms
, targs
, TREE_TYPE (parm
),
23492 TREE_TYPE (arg
), strict
, explain_p
);
23495 case REFERENCE_TYPE
:
23496 if (!TYPE_REF_P (arg
))
23497 return unify_type_mismatch (explain_p
, parm
, arg
);
23498 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23499 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23502 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23503 return unify_type_mismatch (explain_p
, parm
, arg
);
23504 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23505 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23506 return unify_type_mismatch (explain_p
, parm
, arg
);
23507 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23508 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23509 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23510 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23511 TYPE_DOMAIN (arg
), explain_p
);
23512 return unify_success (explain_p
);
23519 case ENUMERAL_TYPE
:
23522 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23523 return unify_type_mismatch (explain_p
, parm
, arg
);
23525 /* We have already checked cv-qualification at the top of the
23527 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23528 return unify_type_mismatch (explain_p
, parm
, arg
);
23530 /* As far as unification is concerned, this wins. Later checks
23531 will invalidate it if necessary. */
23532 return unify_success (explain_p
);
23534 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23535 /* Type INTEGER_CST can come from ordinary constant template args. */
23537 while (CONVERT_EXPR_P (arg
))
23538 arg
= TREE_OPERAND (arg
, 0);
23540 if (TREE_CODE (arg
) != INTEGER_CST
)
23541 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23542 return (tree_int_cst_equal (parm
, arg
)
23543 ? unify_success (explain_p
)
23544 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23548 int i
, len
, argslen
;
23549 int parm_variadic_p
= 0;
23551 if (TREE_CODE (arg
) != TREE_VEC
)
23552 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23554 len
= TREE_VEC_LENGTH (parm
);
23555 argslen
= TREE_VEC_LENGTH (arg
);
23557 /* Check for pack expansions in the parameters. */
23558 for (i
= 0; i
< len
; ++i
)
23560 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23563 /* We can unify against something with a trailing
23565 parm_variadic_p
= 1;
23567 /* [temp.deduct.type]/9: If the template argument list of
23568 P contains a pack expansion that is not the last
23569 template argument, the entire template argument list
23570 is a non-deduced context. */
23571 return unify_success (explain_p
);
23575 /* If we don't have enough arguments to satisfy the parameters
23576 (not counting the pack expression at the end), or we have
23577 too many arguments for a parameter list that doesn't end in
23578 a pack expression, we can't unify. */
23579 if (parm_variadic_p
23580 ? argslen
< len
- parm_variadic_p
23582 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23584 /* Unify all of the parameters that precede the (optional)
23585 pack expression. */
23586 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23588 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23589 TREE_VEC_ELT (parm
, i
),
23590 TREE_VEC_ELT (arg
, i
),
23591 UNIFY_ALLOW_NONE
, explain_p
);
23593 if (parm_variadic_p
)
23594 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23596 /*subr=*/true, explain_p
);
23597 return unify_success (explain_p
);
23602 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23603 return unify_type_mismatch (explain_p
, parm
, arg
);
23605 if (TYPE_PTRMEMFUNC_P (parm
))
23607 if (!TYPE_PTRMEMFUNC_P (arg
))
23608 return unify_type_mismatch (explain_p
, parm
, arg
);
23610 return unify (tparms
, targs
,
23611 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23612 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23613 strict
, explain_p
);
23615 else if (TYPE_PTRMEMFUNC_P (arg
))
23616 return unify_type_mismatch (explain_p
, parm
, arg
);
23618 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23620 tree t
= NULL_TREE
;
23622 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23624 /* First, we try to unify the PARM and ARG directly. */
23625 t
= try_class_unification (tparms
, targs
,
23626 parm
, arg
, explain_p
);
23630 /* Fallback to the special case allowed in
23631 [temp.deduct.call]:
23633 If P is a class, and P has the form
23634 template-id, then A can be a derived class of
23635 the deduced A. Likewise, if P is a pointer to
23636 a class of the form template-id, A can be a
23637 pointer to a derived class pointed to by the
23639 enum template_base_result r
;
23640 r
= get_template_base (tparms
, targs
, parm
, arg
,
23645 /* Don't give the derived diagnostic if we're
23646 already dealing with the same template. */
23648 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23649 && (CLASSTYPE_TI_TEMPLATE (parm
)
23650 == CLASSTYPE_TI_TEMPLATE (arg
)));
23651 return unify_no_common_base (explain_p
&& !same_template
,
23656 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23657 && (CLASSTYPE_TI_TEMPLATE (parm
)
23658 == CLASSTYPE_TI_TEMPLATE (arg
)))
23659 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23660 Then, we should unify `int' and `U'. */
23663 /* There's no chance of unification succeeding. */
23664 return unify_type_mismatch (explain_p
, parm
, arg
);
23666 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23667 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23669 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23670 return unify_type_mismatch (explain_p
, parm
, arg
);
23671 return unify_success (explain_p
);
23674 case FUNCTION_TYPE
:
23676 unsigned int nargs
;
23681 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23682 return unify_type_mismatch (explain_p
, parm
, arg
);
23684 /* CV qualifications for methods can never be deduced, they must
23685 match exactly. We need to check them explicitly here,
23686 because type_unification_real treats them as any other
23687 cv-qualified parameter. */
23688 if (TREE_CODE (parm
) == METHOD_TYPE
23689 && (!check_cv_quals_for_unify
23691 class_of_this_parm (arg
),
23692 class_of_this_parm (parm
))))
23693 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23694 if (TREE_CODE (arg
) == FUNCTION_TYPE
23695 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23696 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23697 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23698 return unify_type_mismatch (explain_p
, parm
, arg
);
23700 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23701 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23703 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23704 args
= XALLOCAVEC (tree
, nargs
);
23705 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23706 a
!= NULL_TREE
&& a
!= void_list_node
;
23707 a
= TREE_CHAIN (a
), ++i
)
23708 args
[i
] = TREE_VALUE (a
);
23711 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23712 args
, nargs
, 1, DEDUCE_EXACT
,
23716 if (flag_noexcept_type
)
23718 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23719 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23720 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23721 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23722 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23723 && uses_template_parms (TREE_PURPOSE (pspec
)))
23724 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23725 TREE_PURPOSE (aspec
),
23726 UNIFY_ALLOW_NONE
, explain_p
);
23727 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23728 return unify_type_mismatch (explain_p
, parm
, arg
);
23735 /* Unify a pointer to member with a pointer to member function, which
23736 deduces the type of the member as a function type. */
23737 if (TYPE_PTRMEMFUNC_P (arg
))
23739 /* Check top-level cv qualifiers */
23740 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23741 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23743 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23744 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23745 UNIFY_ALLOW_NONE
, explain_p
);
23747 /* Determine the type of the function we are unifying against. */
23748 tree fntype
= static_fn_type (arg
);
23750 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23753 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23754 return unify_type_mismatch (explain_p
, parm
, arg
);
23755 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23756 TYPE_OFFSET_BASETYPE (arg
),
23757 UNIFY_ALLOW_NONE
, explain_p
);
23758 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23759 strict
, explain_p
);
23762 if (DECL_TEMPLATE_PARM_P (parm
))
23763 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23764 if (arg
!= scalar_constant_value (parm
))
23765 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23766 return unify_success (explain_p
);
23769 case TEMPLATE_DECL
:
23770 /* Matched cases are handled by the ARG == PARM test above. */
23771 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23774 /* We might get a variable as a non-type template argument in parm if the
23775 corresponding parameter is type-dependent. Make any necessary
23776 adjustments based on whether arg is a reference. */
23777 if (CONSTANT_CLASS_P (arg
))
23778 parm
= fold_non_dependent_expr (parm
, complain
);
23779 else if (REFERENCE_REF_P (arg
))
23781 tree sub
= TREE_OPERAND (arg
, 0);
23783 if (TREE_CODE (sub
) == ADDR_EXPR
)
23784 arg
= TREE_OPERAND (sub
, 0);
23786 /* Now use the normal expression code to check whether they match. */
23789 case TYPE_ARGUMENT_PACK
:
23790 case NONTYPE_ARGUMENT_PACK
:
23791 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23792 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23795 case DECLTYPE_TYPE
:
23796 case UNDERLYING_TYPE
:
23797 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23798 or UNDERLYING_TYPE nodes. */
23799 return unify_success (explain_p
);
23802 /* Unification fails if we hit an error node. */
23803 return unify_invalid (explain_p
);
23806 if (REFERENCE_REF_P (parm
))
23808 bool pexp
= PACK_EXPANSION_P (arg
);
23810 arg
= PACK_EXPANSION_PATTERN (arg
);
23811 if (REFERENCE_REF_P (arg
))
23812 arg
= TREE_OPERAND (arg
, 0);
23814 arg
= make_pack_expansion (arg
, complain
);
23815 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23816 strict
, explain_p
);
23821 /* An unresolved overload is a nondeduced context. */
23822 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23823 return unify_success (explain_p
);
23824 gcc_assert (EXPR_P (parm
)
23825 || COMPOUND_LITERAL_P (parm
)
23826 || TREE_CODE (parm
) == TRAIT_EXPR
);
23828 /* We must be looking at an expression. This can happen with
23832 void foo(S<I>, S<I + 2>);
23836 template<typename T>
23837 void foo(A<T, T{}>);
23839 This is a "non-deduced context":
23843 The non-deduced contexts are:
23845 --A non-type template argument or an array bound in which
23846 a subexpression references a template parameter.
23848 In these cases, we assume deduction succeeded, but don't
23849 actually infer any unifications. */
23851 if (!uses_template_parms (parm
)
23852 && !template_args_equal (parm
, arg
))
23853 return unify_expression_unequal (explain_p
, parm
, arg
);
23855 return unify_success (explain_p
);
23858 #undef RECUR_AND_CHECK_FAILURE
23860 /* Note that DECL can be defined in this translation unit, if
23864 mark_definable (tree decl
)
23867 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23868 FOR_EACH_CLONE (clone
, decl
)
23869 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23872 /* Called if RESULT is explicitly instantiated, or is a member of an
23873 explicitly instantiated class. */
23876 mark_decl_instantiated (tree result
, int extern_p
)
23878 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23880 /* If this entity has already been written out, it's too late to
23881 make any modifications. */
23882 if (TREE_ASM_WRITTEN (result
))
23885 /* For anonymous namespace we don't need to do anything. */
23886 if (decl_anon_ns_mem_p (result
))
23888 gcc_assert (!TREE_PUBLIC (result
));
23892 if (TREE_CODE (result
) != FUNCTION_DECL
)
23893 /* The TREE_PUBLIC flag for function declarations will have been
23894 set correctly by tsubst. */
23895 TREE_PUBLIC (result
) = 1;
23897 /* This might have been set by an earlier implicit instantiation. */
23898 DECL_COMDAT (result
) = 0;
23901 DECL_NOT_REALLY_EXTERN (result
) = 0;
23904 mark_definable (result
);
23905 mark_needed (result
);
23906 /* Always make artificials weak. */
23907 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23908 comdat_linkage (result
);
23909 /* For WIN32 we also want to put explicit instantiations in
23910 linkonce sections. */
23911 else if (TREE_PUBLIC (result
))
23912 maybe_make_one_only (result
);
23913 if (TREE_CODE (result
) == FUNCTION_DECL
23914 && DECL_TEMPLATE_INSTANTIATED (result
))
23915 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23916 since start_preparsed_function wouldn't have if we had an earlier
23917 extern explicit instantiation. */
23918 DECL_EXTERNAL (result
) = 0;
23921 /* If EXTERN_P, then this function will not be emitted -- unless
23922 followed by an explicit instantiation, at which point its linkage
23923 will be adjusted. If !EXTERN_P, then this function will be
23924 emitted here. In neither circumstance do we want
23925 import_export_decl to adjust the linkage. */
23926 DECL_INTERFACE_KNOWN (result
) = 1;
23929 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23930 important template arguments. If any are missing, we check whether
23931 they're important by using error_mark_node for substituting into any
23932 args that were used for partial ordering (the ones between ARGS and END)
23933 and seeing if it bubbles up. */
23936 check_undeduced_parms (tree targs
, tree args
, tree end
)
23938 bool found
= false;
23940 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23941 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23944 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23948 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23949 if (substed
== error_mark_node
)
23955 /* Given two function templates PAT1 and PAT2, return:
23957 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23958 -1 if PAT2 is more specialized than PAT1.
23959 0 if neither is more specialized.
23961 LEN indicates the number of parameters we should consider
23962 (defaulted parameters should not be considered).
23964 The 1998 std underspecified function template partial ordering, and
23965 DR214 addresses the issue. We take pairs of arguments, one from
23966 each of the templates, and deduce them against each other. One of
23967 the templates will be more specialized if all the *other*
23968 template's arguments deduce against its arguments and at least one
23969 of its arguments *does* *not* deduce against the other template's
23970 corresponding argument. Deduction is done as for class templates.
23971 The arguments used in deduction have reference and top level cv
23972 qualifiers removed. Iff both arguments were originally reference
23973 types *and* deduction succeeds in both directions, an lvalue reference
23974 wins against an rvalue reference and otherwise the template
23975 with the more cv-qualified argument wins for that pairing (if
23976 neither is more cv-qualified, they both are equal). Unlike regular
23977 deduction, after all the arguments have been deduced in this way,
23978 we do *not* verify the deduced template argument values can be
23979 substituted into non-deduced contexts.
23981 The logic can be a bit confusing here, because we look at deduce1 and
23982 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23983 can find template arguments for pat1 to make arg1 look like arg2, that
23984 means that arg2 is at least as specialized as arg1. */
23987 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23989 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23990 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23991 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23992 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23993 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23994 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23995 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23996 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23997 tree origs1
, origs2
;
23998 bool lose1
= false;
23999 bool lose2
= false;
24001 /* Remove the this parameter from non-static member functions. If
24002 one is a non-static member function and the other is not a static
24003 member function, remove the first parameter from that function
24004 also. This situation occurs for operator functions where we
24005 locate both a member function (with this pointer) and non-member
24006 operator (with explicit first operand). */
24007 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24009 len
--; /* LEN is the number of significant arguments for DECL1 */
24010 args1
= TREE_CHAIN (args1
);
24011 if (!DECL_STATIC_FUNCTION_P (decl2
))
24012 args2
= TREE_CHAIN (args2
);
24014 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24016 args2
= TREE_CHAIN (args2
);
24017 if (!DECL_STATIC_FUNCTION_P (decl1
))
24020 args1
= TREE_CHAIN (args1
);
24024 /* If only one is a conversion operator, they are unordered. */
24025 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24028 /* Consider the return type for a conversion function */
24029 if (DECL_CONV_FN_P (decl1
))
24031 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24032 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24036 processing_template_decl
++;
24042 /* Stop when an ellipsis is seen. */
24043 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24045 tree arg1
= TREE_VALUE (args1
);
24046 tree arg2
= TREE_VALUE (args2
);
24047 int deduce1
, deduce2
;
24053 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24054 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24056 /* When both arguments are pack expansions, we need only
24057 unify the patterns themselves. */
24058 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24059 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24061 /* This is the last comparison we need to do. */
24065 if (TYPE_REF_P (arg1
))
24067 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24068 arg1
= TREE_TYPE (arg1
);
24069 quals1
= cp_type_quals (arg1
);
24072 if (TYPE_REF_P (arg2
))
24074 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24075 arg2
= TREE_TYPE (arg2
);
24076 quals2
= cp_type_quals (arg2
);
24079 arg1
= TYPE_MAIN_VARIANT (arg1
);
24080 arg2
= TYPE_MAIN_VARIANT (arg2
);
24082 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24084 int i
, len2
= remaining_arguments (args2
);
24085 tree parmvec
= make_tree_vec (1);
24086 tree argvec
= make_tree_vec (len2
);
24089 /* Setup the parameter vector, which contains only ARG1. */
24090 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24092 /* Setup the argument vector, which contains the remaining
24094 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24095 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24097 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24098 argvec
, DEDUCE_EXACT
,
24099 /*subr=*/true, /*explain_p=*/false)
24102 /* We cannot deduce in the other direction, because ARG1 is
24103 a pack expansion but ARG2 is not. */
24106 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24108 int i
, len1
= remaining_arguments (args1
);
24109 tree parmvec
= make_tree_vec (1);
24110 tree argvec
= make_tree_vec (len1
);
24113 /* Setup the parameter vector, which contains only ARG1. */
24114 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24116 /* Setup the argument vector, which contains the remaining
24118 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24119 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24121 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24122 argvec
, DEDUCE_EXACT
,
24123 /*subr=*/true, /*explain_p=*/false)
24126 /* We cannot deduce in the other direction, because ARG2 is
24127 a pack expansion but ARG1 is not.*/
24133 /* The normal case, where neither argument is a pack
24135 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24136 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24138 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24139 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24143 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24144 arg2, then arg2 is not as specialized as arg1. */
24150 /* "If, for a given type, deduction succeeds in both directions
24151 (i.e., the types are identical after the transformations above)
24152 and both P and A were reference types (before being replaced with
24153 the type referred to above):
24154 - if the type from the argument template was an lvalue reference and
24155 the type from the parameter template was not, the argument type is
24156 considered to be more specialized than the other; otherwise,
24157 - if the type from the argument template is more cv-qualified
24158 than the type from the parameter template (as described above),
24159 the argument type is considered to be more specialized than the other;
24161 - neither type is more specialized than the other." */
24163 if (deduce1
&& deduce2
)
24165 if (ref1
&& ref2
&& ref1
!= ref2
)
24172 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24174 if ((quals1
& quals2
) == quals2
)
24176 if ((quals1
& quals2
) == quals1
)
24181 if (lose1
&& lose2
)
24182 /* We've failed to deduce something in either direction.
24183 These must be unordered. */
24186 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24187 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24188 /* We have already processed all of the arguments in our
24189 handing of the pack expansion type. */
24192 args1
= TREE_CHAIN (args1
);
24193 args2
= TREE_CHAIN (args2
);
24196 /* "In most cases, all template parameters must have values in order for
24197 deduction to succeed, but for partial ordering purposes a template
24198 parameter may remain without a value provided it is not used in the
24199 types being used for partial ordering."
24201 Thus, if we are missing any of the targs1 we need to substitute into
24202 origs1, then pat2 is not as specialized as pat1. This can happen when
24203 there is a nondeduced context. */
24204 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24206 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24209 processing_template_decl
--;
24211 /* If both deductions succeed, the partial ordering selects the more
24212 constrained template. */
24213 /* P2113: If the corresponding template-parameters of the
24214 template-parameter-lists are not equivalent ([temp.over.link]) or if
24215 the function parameters that positionally correspond between the two
24216 templates are not of the same type, neither template is more
24217 specialized than the other. */
24218 if (!lose1
&& !lose2
24219 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24220 DECL_TEMPLATE_PARMS (pat2
))
24221 && compparms (origs1
, origs2
))
24223 int winner
= more_constrained (decl1
, decl2
);
24226 else if (winner
< 0)
24230 /* All things being equal, if the next argument is a pack expansion
24231 for one function but not for the other, prefer the
24232 non-variadic function. FIXME this is bogus; see c++/41958. */
24234 && args1
&& TREE_VALUE (args1
)
24235 && args2
&& TREE_VALUE (args2
))
24237 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24238 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24241 if (lose1
== lose2
)
24249 /* Determine which of two partial specializations of TMPL is more
24252 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24253 to the first partial specialization. The TREE_PURPOSE is the
24254 innermost set of template parameters for the partial
24255 specialization. PAT2 is similar, but for the second template.
24257 Return 1 if the first partial specialization is more specialized;
24258 -1 if the second is more specialized; 0 if neither is more
24261 See [temp.class.order] for information about determining which of
24262 two templates is more specialized. */
24265 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24269 bool any_deductions
= false;
24271 tree tmpl1
= TREE_VALUE (pat1
);
24272 tree tmpl2
= TREE_VALUE (pat2
);
24273 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24274 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24276 /* Just like what happens for functions, if we are ordering between
24277 different template specializations, we may encounter dependent
24278 types in the arguments, and we need our dependency check functions
24279 to behave correctly. */
24280 ++processing_template_decl
;
24281 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24285 any_deductions
= true;
24288 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24292 any_deductions
= true;
24294 --processing_template_decl
;
24296 /* If both deductions succeed, the partial ordering selects the more
24297 constrained template. */
24298 if (!winner
&& any_deductions
)
24299 winner
= more_constrained (tmpl1
, tmpl2
);
24301 /* In the case of a tie where at least one of the templates
24302 has a parameter pack at the end, the template with the most
24303 non-packed parameters wins. */
24306 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24307 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24309 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24310 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24311 int len1
= TREE_VEC_LENGTH (args1
);
24312 int len2
= TREE_VEC_LENGTH (args2
);
24314 /* We don't count the pack expansion at the end. */
24315 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24317 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24322 else if (len1
< len2
)
24329 /* Return the template arguments that will produce the function signature
24330 DECL from the function template FN, with the explicit template
24331 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24332 also match. Return NULL_TREE if no satisfactory arguments could be
24336 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24338 int ntparms
= DECL_NTPARMS (fn
);
24339 tree targs
= make_tree_vec (ntparms
);
24340 tree decl_type
= TREE_TYPE (decl
);
24341 tree decl_arg_types
;
24343 unsigned int nargs
, ix
;
24346 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24348 /* Never do unification on the 'this' parameter. */
24349 decl_arg_types
= skip_artificial_parms_for (decl
,
24350 TYPE_ARG_TYPES (decl_type
));
24352 nargs
= list_length (decl_arg_types
);
24353 args
= XALLOCAVEC (tree
, nargs
);
24354 for (arg
= decl_arg_types
, ix
= 0;
24355 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24356 arg
= TREE_CHAIN (arg
), ++ix
)
24357 args
[ix
] = TREE_VALUE (arg
);
24359 if (fn_type_unification (fn
, explicit_args
, targs
,
24361 (check_rettype
|| DECL_CONV_FN_P (fn
)
24362 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24363 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24364 /*explain_p=*/false,
24366 == error_mark_node
)
24372 /* Return the innermost template arguments that, when applied to a partial
24373 specialization SPEC_TMPL of TMPL, yield the ARGS.
24375 For example, suppose we have:
24377 template <class T, class U> struct S {};
24378 template <class T> struct S<T*, int> {};
24380 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24381 partial specialization and the ARGS will be {double*, int}. The resulting
24382 vector will be {double}, indicating that `T' is bound to `double'. */
24385 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24387 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24389 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24390 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24392 tree innermost_deduced_args
;
24394 innermost_deduced_args
= make_tree_vec (ntparms
);
24395 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24397 deduced_args
= copy_node (args
);
24398 SET_TMPL_ARGS_LEVEL (deduced_args
,
24399 TMPL_ARGS_DEPTH (deduced_args
),
24400 innermost_deduced_args
);
24403 deduced_args
= innermost_deduced_args
;
24405 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24407 if (unify (tparms
, deduced_args
,
24408 INNERMOST_TEMPLATE_ARGS (spec_args
),
24409 INNERMOST_TEMPLATE_ARGS (args
),
24410 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24413 for (i
= 0; i
< ntparms
; ++i
)
24414 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24416 if (!tried_array_deduction
)
24418 try_array_deduction (tparms
, innermost_deduced_args
,
24419 INNERMOST_TEMPLATE_ARGS (spec_args
));
24420 tried_array_deduction
= true;
24421 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24427 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24429 excessive_deduction_depth
= true;
24433 /* Verify that nondeduced template arguments agree with the type
24434 obtained from argument deduction.
24438 struct A { typedef int X; };
24439 template <class T, class U> struct C {};
24440 template <class T> struct C<T, typename T::X> {};
24442 Then with the instantiation `C<A, int>', we can deduce that
24443 `T' is `A' but unify () does not check whether `typename T::X'
24445 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24447 if (spec_args
!= error_mark_node
)
24448 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24449 INNERMOST_TEMPLATE_ARGS (spec_args
),
24450 tmpl
, tf_none
, false, false);
24452 pop_tinst_level ();
24454 if (spec_args
== error_mark_node
24455 /* We only need to check the innermost arguments; the other
24456 arguments will always agree. */
24457 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24458 INNERMOST_TEMPLATE_ARGS (args
)))
24461 /* Now that we have bindings for all of the template arguments,
24462 ensure that the arguments deduced for the template template
24463 parameters have compatible template parameter lists. See the use
24464 of template_template_parm_bindings_ok_p in fn_type_unification
24465 for more information. */
24466 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24469 return deduced_args
;
24472 // Compare two function templates T1 and T2 by deducing bindings
24473 // from one against the other. If both deductions succeed, compare
24474 // constraints to see which is more constrained.
24476 more_specialized_inst (tree t1
, tree t2
)
24481 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24487 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24493 // If both deductions succeed, then one may be more constrained.
24494 if (count
== 2 && fate
== 0)
24495 fate
= more_constrained (t1
, t2
);
24500 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24501 Return the TREE_LIST node with the most specialized template, if
24502 any. If there is no most specialized template, the error_mark_node
24505 Note that this function does not look at, or modify, the
24506 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24507 returned is one of the elements of INSTANTIATIONS, callers may
24508 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24509 and retrieve it from the value returned. */
24512 most_specialized_instantiation (tree templates
)
24516 ++processing_template_decl
;
24519 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24521 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24522 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24527 /* Equally specialized, move to next function. If there
24528 is no next function, nothing's most specialized. */
24529 fn
= TREE_CHAIN (fn
);
24537 /* Now verify that champ is better than everything earlier in the
24538 instantiation list. */
24539 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24540 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24547 processing_template_decl
--;
24550 return error_mark_node
;
24555 /* If DECL is a specialization of some template, return the most
24556 general such template. Otherwise, returns NULL_TREE.
24558 For example, given:
24560 template <class T> struct S { template <class U> void f(U); };
24562 if TMPL is `template <class U> void S<int>::f(U)' this will return
24563 the full template. This function will not trace past partial
24564 specializations, however. For example, given in addition:
24566 template <class T> struct S<T*> { template <class U> void f(U); };
24568 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24569 `template <class T> template <class U> S<T*>::f(U)'. */
24572 most_general_template (tree decl
)
24574 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24576 if (tree tinfo
= get_template_info (decl
))
24577 decl
= TI_TEMPLATE (tinfo
);
24578 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24579 template friend, or a FIELD_DECL for a capture pack. */
24580 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24584 /* Look for more and more general templates. */
24585 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24587 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24588 (See cp-tree.h for details.) */
24589 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24592 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24593 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24594 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24597 /* Stop if we run into an explicitly specialized class template. */
24598 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24599 && DECL_CONTEXT (decl
)
24600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24603 decl
= DECL_TI_TEMPLATE (decl
);
24609 /* Return the most specialized of the template partial specializations
24610 which can produce TARGET, a specialization of some class or variable
24611 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24612 a TEMPLATE_DECL node corresponding to the partial specialization, while
24613 the TREE_PURPOSE is the set of template arguments that must be
24614 substituted into the template pattern in order to generate TARGET.
24616 If the choice of partial specialization is ambiguous, a diagnostic
24617 is issued, and the error_mark_node is returned. If there are no
24618 partial specializations matching TARGET, then NULL_TREE is
24619 returned, indicating that the primary template should be used. */
24622 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24624 tree list
= NULL_TREE
;
24629 tree outer_args
= NULL_TREE
;
24632 if (TYPE_P (target
))
24634 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24635 tmpl
= TI_TEMPLATE (tinfo
);
24636 args
= TI_ARGS (tinfo
);
24638 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24640 tmpl
= TREE_OPERAND (target
, 0);
24641 args
= TREE_OPERAND (target
, 1);
24643 else if (VAR_P (target
))
24645 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24646 tmpl
= TI_TEMPLATE (tinfo
);
24647 args
= TI_ARGS (tinfo
);
24650 gcc_unreachable ();
24652 tree main_tmpl
= most_general_template (tmpl
);
24654 /* For determining which partial specialization to use, only the
24655 innermost args are interesting. */
24656 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24658 outer_args
= strip_innermost_template_args (args
, 1);
24659 args
= INNERMOST_TEMPLATE_ARGS (args
);
24662 /* The caller hasn't called push_to_top_level yet, but we need
24663 get_partial_spec_bindings to be done in non-template context so that we'll
24664 fully resolve everything. */
24665 processing_template_decl_sentinel ptds
;
24667 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24669 const tree ospec_tmpl
= TREE_VALUE (t
);
24674 /* Substitute in the template args from the enclosing class. */
24675 ++processing_template_decl
;
24676 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24677 --processing_template_decl
;
24678 if (spec_tmpl
== error_mark_node
)
24679 return error_mark_node
;
24682 spec_tmpl
= ospec_tmpl
;
24684 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24688 spec_args
= add_to_template_args (outer_args
, spec_args
);
24690 /* Keep the candidate only if the constraints are satisfied,
24691 or if we're not compiling with concepts. */
24693 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24695 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24696 TREE_TYPE (list
) = TREE_TYPE (t
);
24704 ambiguous_p
= false;
24707 t
= TREE_CHAIN (t
);
24708 for (; t
; t
= TREE_CHAIN (t
))
24710 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24717 t
= TREE_CHAIN (t
);
24720 ambiguous_p
= true;
24729 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24731 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24734 ambiguous_p
= true;
24742 char *spaces
= NULL
;
24743 if (!(complain
& tf_error
))
24744 return error_mark_node
;
24745 if (TYPE_P (target
))
24746 error ("ambiguous template instantiation for %q#T", target
);
24748 error ("ambiguous template instantiation for %q#D", target
);
24749 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24750 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24752 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24753 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24754 "%s %#qS", spaces
? spaces
: str
, subst
);
24755 spaces
= spaces
? spaces
: get_spaces (str
);
24758 return error_mark_node
;
24764 /* Explicitly instantiate DECL. */
24767 do_decl_instantiation (tree decl
, tree storage
)
24769 tree result
= NULL_TREE
;
24772 if (!decl
|| decl
== error_mark_node
)
24773 /* An error occurred, for which grokdeclarator has already issued
24774 an appropriate message. */
24776 else if (! DECL_LANG_SPECIFIC (decl
))
24778 error ("explicit instantiation of non-template %q#D", decl
);
24781 else if (DECL_DECLARED_CONCEPT_P (decl
))
24784 error ("explicit instantiation of variable concept %q#D", decl
);
24786 error ("explicit instantiation of function concept %q#D", decl
);
24790 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24791 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24793 if (VAR_P (decl
) && !var_templ
)
24795 /* There is an asymmetry here in the way VAR_DECLs and
24796 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24797 the latter, the DECL we get back will be marked as a
24798 template instantiation, and the appropriate
24799 DECL_TEMPLATE_INFO will be set up. This does not happen for
24800 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24801 should handle VAR_DECLs as it currently handles
24803 if (!DECL_CLASS_SCOPE_P (decl
))
24805 error ("%qD is not a static data member of a class template", decl
);
24808 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24809 if (!result
|| !VAR_P (result
))
24811 error ("no matching template for %qD found", decl
);
24814 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24816 error ("type %qT for explicit instantiation %qD does not match "
24817 "declared type %qT", TREE_TYPE (result
), decl
,
24822 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24824 error ("explicit instantiation of %q#D", decl
);
24830 /* Check for various error cases. Note that if the explicit
24831 instantiation is valid the RESULT will currently be marked as an
24832 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24833 until we get here. */
24835 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24837 /* DR 259 [temp.spec].
24839 Both an explicit instantiation and a declaration of an explicit
24840 specialization shall not appear in a program unless the explicit
24841 instantiation follows a declaration of the explicit specialization.
24843 For a given set of template parameters, if an explicit
24844 instantiation of a template appears after a declaration of an
24845 explicit specialization for that template, the explicit
24846 instantiation has no effect. */
24849 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24853 No program shall explicitly instantiate any template more
24856 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24857 the first instantiation was `extern' and the second is not,
24858 and EXTERN_P for the opposite case. */
24859 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24860 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24861 /* If an "extern" explicit instantiation follows an ordinary
24862 explicit instantiation, the template is instantiated. */
24866 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24868 error ("no matching template for %qD found", result
);
24871 else if (!DECL_TEMPLATE_INFO (result
))
24873 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24877 if (storage
== NULL_TREE
)
24879 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24881 if (cxx_dialect
== cxx98
)
24882 pedwarn (input_location
, OPT_Wpedantic
,
24883 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24888 error ("storage class %qD applied to template instantiation", storage
);
24890 check_explicit_instantiation_namespace (result
);
24891 mark_decl_instantiated (result
, extern_p
);
24893 instantiate_decl (result
, /*defer_ok=*/true,
24894 /*expl_inst_class_mem_p=*/false);
24898 mark_class_instantiated (tree t
, int extern_p
)
24900 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24901 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24902 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24903 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24906 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24907 rest_of_type_compilation (t
, 1);
24911 /* Called from do_type_instantiation through binding_table_foreach to
24912 do recursive instantiation for the type bound in ENTRY. */
24914 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24916 tree storage
= *(tree
*) data
;
24918 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24919 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24920 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24921 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24924 /* Perform an explicit instantiation of template class T. STORAGE, if
24925 non-null, is the RID for extern, inline or static. COMPLAIN is
24926 nonzero if this is called from the parser, zero if called recursively,
24927 since the standard is unclear (as detailed below). */
24930 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24935 int previous_instantiation_extern_p
= 0;
24937 if (TREE_CODE (t
) == TYPE_DECL
)
24940 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24943 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24945 error ("explicit instantiation of non-class template %qD", tmpl
);
24947 error ("explicit instantiation of non-template type %qT", t
);
24953 if (!COMPLETE_TYPE_P (t
))
24955 if (complain
& tf_error
)
24956 error ("explicit instantiation of %q#T before definition of template",
24961 if (storage
!= NULL_TREE
)
24963 if (storage
== ridpointers
[(int) RID_EXTERN
])
24965 if (cxx_dialect
== cxx98
)
24966 pedwarn (input_location
, OPT_Wpedantic
,
24967 "ISO C++ 1998 forbids the use of %<extern%> on "
24968 "explicit instantiations");
24971 pedwarn (input_location
, OPT_Wpedantic
,
24972 "ISO C++ forbids the use of %qE"
24973 " on explicit instantiations", storage
);
24975 if (storage
== ridpointers
[(int) RID_INLINE
])
24977 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24979 else if (storage
== ridpointers
[(int) RID_STATIC
])
24983 error ("storage class %qD applied to template instantiation",
24989 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24991 /* DR 259 [temp.spec].
24993 Both an explicit instantiation and a declaration of an explicit
24994 specialization shall not appear in a program unless the explicit
24995 instantiation follows a declaration of the explicit specialization.
24997 For a given set of template parameters, if an explicit
24998 instantiation of a template appears after a declaration of an
24999 explicit specialization for that template, the explicit
25000 instantiation has no effect. */
25003 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
25007 No program shall explicitly instantiate any template more
25010 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25011 instantiation was `extern'. If EXTERN_P then the second is.
25012 These cases are OK. */
25013 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
25015 if (!previous_instantiation_extern_p
&& !extern_p
25016 && (complain
& tf_error
))
25017 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
25019 /* If we've already instantiated the template, just return now. */
25020 if (!CLASSTYPE_INTERFACE_ONLY (t
))
25024 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25025 mark_class_instantiated (t
, extern_p
);
25030 /* In contrast to implicit instantiation, where only the
25031 declarations, and not the definitions, of members are
25032 instantiated, we have here:
25036 An explicit instantiation that names a class template
25037 specialization is also an explicit instantiation of the same
25038 kind (declaration or definition) of each of its members (not
25039 including members inherited from base classes and members
25040 that are templates) that has not been previously explicitly
25041 specialized in the translation unit containing the explicit
25042 instantiation, provided that the associated constraints, if
25043 any, of that member are satisfied by the template arguments
25044 of the explicit instantiation. */
25045 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25047 || (TREE_CODE (fld
) == FUNCTION_DECL
25049 && user_provided_p (fld
)))
25050 && DECL_TEMPLATE_INSTANTIATION (fld
)
25051 && constraints_satisfied_p (fld
))
25053 mark_decl_instantiated (fld
, extern_p
);
25055 instantiate_decl (fld
, /*defer_ok=*/true,
25056 /*expl_inst_class_mem_p=*/true);
25059 if (CLASSTYPE_NESTED_UTDS (t
))
25060 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
25061 bt_instantiate_type_proc
, &storage
);
25064 /* Given a function DECL, which is a specialization of TMPL, modify
25065 DECL to be a re-instantiation of TMPL with the same template
25066 arguments. TMPL should be the template into which tsubst'ing
25067 should occur for DECL, not the most general template.
25069 One reason for doing this is a scenario like this:
25072 void f(const T&, int i);
25074 void g() { f(3, 7); }
25077 void f(const T& t, const int i) { }
25079 Note that when the template is first instantiated, with
25080 instantiate_template, the resulting DECL will have no name for the
25081 first parameter, and the wrong type for the second. So, when we go
25082 to instantiate the DECL, we regenerate it. */
25085 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25087 /* The arguments used to instantiate DECL, from the most general
25091 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25093 /* Make sure that we can see identifiers, and compute access
25095 push_access_scope (decl
);
25097 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25105 args_depth
= TMPL_ARGS_DEPTH (args
);
25106 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25107 if (args_depth
> parms_depth
)
25108 args
= get_innermost_template_args (args
, parms_depth
);
25110 /* Instantiate a dynamic exception-specification. noexcept will be
25112 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25113 if (TREE_VALUE (raises
))
25115 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25116 args
, tf_error
, NULL_TREE
,
25117 /*defer_ok*/false);
25118 if (specs
&& specs
!= error_mark_node
)
25119 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25123 /* Merge parameter declarations. */
25124 decl_parm
= skip_artificial_parms_for (decl
,
25125 DECL_ARGUMENTS (decl
));
25127 = skip_artificial_parms_for (code_pattern
,
25128 DECL_ARGUMENTS (code_pattern
));
25129 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25134 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25135 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25136 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25138 parm_type
= type_decays_to (parm_type
);
25139 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25140 TREE_TYPE (decl_parm
) = parm_type
;
25141 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25142 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25144 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25145 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25147 decl_parm
= DECL_CHAIN (decl_parm
);
25148 pattern_parm
= DECL_CHAIN (pattern_parm
);
25150 /* Merge any parameters that match with the function parameter
25152 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25155 tree expanded_types
;
25156 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25157 the parameters in this function parameter pack. */
25158 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25159 args
, tf_error
, NULL_TREE
);
25160 len
= TREE_VEC_LENGTH (expanded_types
);
25161 for (i
= 0; i
< len
; i
++)
25166 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25167 /* Rename the parameter to include the index. */
25168 DECL_NAME (decl_parm
) =
25169 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25170 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25171 parm_type
= type_decays_to (parm_type
);
25172 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25173 TREE_TYPE (decl_parm
) = parm_type
;
25174 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25175 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25177 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25178 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25180 decl_parm
= DECL_CHAIN (decl_parm
);
25183 /* Merge additional specifiers from the CODE_PATTERN. */
25184 if (DECL_DECLARED_INLINE_P (code_pattern
)
25185 && !DECL_DECLARED_INLINE_P (decl
))
25186 DECL_DECLARED_INLINE_P (decl
) = 1;
25188 maybe_instantiate_noexcept (decl
, tf_error
);
25190 else if (VAR_P (decl
))
25192 start_lambda_scope (decl
);
25193 DECL_INITIAL (decl
) =
25194 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25195 tf_error
, DECL_TI_TEMPLATE (decl
));
25196 finish_lambda_scope ();
25197 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25198 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25199 tf_error
, DECL_TI_TEMPLATE (decl
));
25202 gcc_unreachable ();
25204 pop_access_scope (decl
);
25207 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25208 substituted to get DECL. */
25211 template_for_substitution (tree decl
)
25213 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25215 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25216 for the instantiation. This is not always the most general
25217 template. Consider, for example:
25220 struct S { template <class U> void f();
25221 template <> void f<int>(); };
25223 and an instantiation of S<double>::f<int>. We want TD to be the
25224 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25225 while (/* An instantiation cannot have a definition, so we need a
25226 more general template. */
25227 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25228 /* We must also deal with friend templates. Given:
25230 template <class T> struct S {
25231 template <class U> friend void f() {};
25234 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25235 so far as the language is concerned, but that's still
25236 where we get the pattern for the instantiation from. On
25237 other hand, if the definition comes outside the class, say:
25239 template <class T> struct S {
25240 template <class U> friend void f();
25242 template <class U> friend void f() {}
25244 we don't need to look any further. That's what the check for
25245 DECL_INITIAL is for. */
25246 || (TREE_CODE (decl
) == FUNCTION_DECL
25247 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25248 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25250 /* The present template, TD, should not be a definition. If it
25251 were a definition, we should be using it! Note that we
25252 cannot restructure the loop to just keep going until we find
25253 a template with a definition, since that might go too far if
25254 a specialization was declared, but not defined. */
25256 /* Fetch the more general template. */
25257 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25263 /* Returns true if we need to instantiate this template instance even if we
25264 know we aren't going to emit it. */
25267 always_instantiate_p (tree decl
)
25269 /* We always instantiate inline functions so that we can inline them. An
25270 explicit instantiation declaration prohibits implicit instantiation of
25271 non-inline functions. With high levels of optimization, we would
25272 normally inline non-inline functions -- but we're not allowed to do
25273 that for "extern template" functions. Therefore, we check
25274 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25275 return ((TREE_CODE (decl
) == FUNCTION_DECL
25276 && (DECL_DECLARED_INLINE_P (decl
)
25277 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25278 /* And we need to instantiate static data members so that
25279 their initializers are available in integral constant
25282 && decl_maybe_constant_var_p (decl
)));
25285 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25286 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25287 error, true otherwise. */
25290 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25292 tree fntype
, spec
, noex
;
25294 /* Don't instantiate a noexcept-specification from template context. */
25295 if (processing_template_decl
25296 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25299 if (DECL_MAYBE_DELETED (fn
))
25301 if (fn
== current_function_decl
)
25302 /* We're in start_preparsed_function, keep going. */
25306 synthesize_method (fn
);
25308 return !DECL_MAYBE_DELETED (fn
);
25311 fntype
= TREE_TYPE (fn
);
25312 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25314 if (!spec
|| !TREE_PURPOSE (spec
))
25317 noex
= TREE_PURPOSE (spec
);
25318 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25319 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25322 tree orig_fn
= NULL_TREE
;
25323 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25324 its FUNCTION_DECL for the rest of this function -- push_access_scope
25325 doesn't accept TEMPLATE_DECLs. */
25326 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25329 fn
= DECL_TEMPLATE_RESULT (fn
);
25332 if (DECL_CLONED_FUNCTION_P (fn
))
25334 tree prime
= DECL_CLONED_FUNCTION (fn
);
25335 if (!maybe_instantiate_noexcept (prime
, complain
))
25337 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25339 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25341 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25342 bool added
= false;
25343 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25345 spec
= get_defaulted_eh_spec (fn
, complain
);
25346 if (spec
== error_mark_node
)
25347 /* This might have failed because of an unparsed DMI, so
25348 let's try again later. */
25351 else if (!(added
= !fns
->add (fn
)))
25353 /* If hash_set::add returns true, the element was already there. */
25354 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25355 DECL_SOURCE_LOCATION (fn
));
25357 "exception specification of %qD depends on itself",
25359 spec
= noexcept_false_spec
;
25361 else if (push_tinst_level (fn
))
25363 push_to_top_level ();
25364 push_access_scope (fn
);
25365 push_deferring_access_checks (dk_no_deferred
);
25366 input_location
= DECL_SOURCE_LOCATION (fn
);
25368 /* If needed, set current_class_ptr for the benefit of
25369 tsubst_copy/PARM_DECL. */
25370 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25371 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25373 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25374 current_class_ptr
= NULL_TREE
;
25375 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25376 current_class_ptr
= this_parm
;
25379 /* If this function is represented by a TEMPLATE_DECL, then
25380 the deferred noexcept-specification might still contain
25381 dependent types, even after substitution. And we need the
25382 dependency check functions to work in build_noexcept_spec. */
25384 ++processing_template_decl
;
25386 /* Do deferred instantiation of the noexcept-specifier. */
25387 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25388 DEFERRED_NOEXCEPT_ARGS (noex
),
25389 tf_warning_or_error
, fn
,
25390 /*function_p=*/false,
25393 /* Build up the noexcept-specification. */
25394 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25397 --processing_template_decl
;
25399 pop_deferring_access_checks ();
25400 pop_access_scope (fn
);
25401 pop_tinst_level ();
25402 pop_from_top_level ();
25405 spec
= noexcept_false_spec
;
25411 if (spec
== error_mark_node
)
25413 /* This failed with a hard error, so let's go with false. */
25414 gcc_assert (seen_error ());
25415 spec
= noexcept_false_spec
;
25418 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25420 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25425 /* We're starting to process the function INST, an instantiation of PATTERN;
25426 add their parameters to local_specializations. */
25429 register_parameter_specializations (tree pattern
, tree inst
)
25431 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25432 tree spec_parm
= DECL_ARGUMENTS (inst
);
25433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25435 register_local_specialization (spec_parm
, tmpl_parm
);
25436 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25437 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25439 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25441 if (!DECL_PACK_P (tmpl_parm
)
25442 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25444 register_local_specialization (spec_parm
, tmpl_parm
);
25445 spec_parm
= DECL_CHAIN (spec_parm
);
25449 /* Register the (value) argument pack as a specialization of
25450 TMPL_PARM, then move on. */
25451 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25452 register_local_specialization (argpack
, tmpl_parm
);
25455 gcc_assert (!spec_parm
);
25458 /* Instantiate the body of D using PATTERN with ARGS. We have
25459 already determined PATTERN is the correct template to use.
25460 NESTED_P is true if this is a nested function, in which case
25461 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25464 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25466 tree td
= NULL_TREE
;
25467 tree code_pattern
= pattern
;
25472 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25475 /* Only OMP reductions are nested. */
25476 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25478 vec
<tree
> omp_privatization_save
;
25479 if (current_function_decl
)
25480 save_omp_privatization_clauses (omp_privatization_save
);
25483 = !(current_function_decl
25484 && !LAMBDA_FUNCTION_P (d
)
25485 && decl_function_context (d
) == current_function_decl
);
25488 push_to_top_level ();
25491 gcc_assert (!processing_template_decl
);
25492 push_function_context ();
25493 cp_unevaluated_operand
= 0;
25494 c_inhibit_evaluation_warnings
= 0;
25499 /* The variable might be a lambda's extra scope, and that
25500 lambda's visibility depends on D's. */
25501 maybe_commonize_var (d
);
25502 determine_visibility (d
);
25505 /* Mark D as instantiated so that recursive calls to
25506 instantiate_decl do not try to instantiate it again. */
25507 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25510 /* Regenerate the declaration in case the template has been modified
25511 by a subsequent redeclaration. */
25512 regenerate_decl_from_template (d
, td
, args
);
25514 /* We already set the file and line above. Reset them now in case
25515 they changed as a result of calling regenerate_decl_from_template. */
25516 input_location
= DECL_SOURCE_LOCATION (d
);
25521 bool const_init
= false;
25523 /* Clear out DECL_RTL; whatever was there before may not be right
25524 since we've reset the type of the declaration. */
25525 SET_DECL_RTL (d
, NULL
);
25526 DECL_IN_AGGR_P (d
) = 0;
25528 /* The initializer is placed in DECL_INITIAL by
25529 regenerate_decl_from_template so we don't need to
25530 push/pop_access_scope again here. Pull it out so that
25531 cp_finish_decl can process it. */
25532 init
= DECL_INITIAL (d
);
25533 DECL_INITIAL (d
) = NULL_TREE
;
25534 DECL_INITIALIZED_P (d
) = 0;
25536 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25537 initializer. That function will defer actual emission until
25538 we have a chance to determine linkage. */
25539 DECL_EXTERNAL (d
) = 0;
25541 /* Enter the scope of D so that access-checking works correctly. */
25542 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25544 push_nested_class (DECL_CONTEXT (d
));
25546 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25547 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25550 pop_nested_class ();
25552 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25553 synthesize_method (d
);
25554 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25556 /* Set up the list of local specializations. */
25557 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25558 tree block
= NULL_TREE
;
25560 /* Set up context. */
25562 block
= push_stmt_list ();
25564 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25566 perform_instantiation_time_access_checks (code_pattern
, args
);
25568 /* Create substitution entries for the parameters. */
25569 register_parameter_specializations (code_pattern
, d
);
25571 /* Substitute into the body of the function. */
25572 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25573 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25574 tf_warning_or_error
, d
);
25577 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25578 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25579 /*integral_constant_expression_p=*/false);
25581 /* Set the current input_location to the end of the function
25582 so that finish_function knows where we are. */
25584 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25586 /* Remember if we saw an infinite loop in the template. */
25587 current_function_infinite_loop
25588 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25591 /* Finish the function. */
25593 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25596 d
= finish_function (/*inline_p=*/false);
25597 expand_or_defer_fn (d
);
25600 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25601 cp_check_omp_declare_reduction (d
);
25604 /* We're not deferring instantiation any more. */
25605 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25608 pop_from_top_level ();
25610 pop_function_context ();
25612 if (current_function_decl
)
25613 restore_omp_privatization_clauses (omp_privatization_save
);
25616 /* Produce the definition of D, a _DECL generated from a template. If
25617 DEFER_OK is true, then we don't have to actually do the
25618 instantiation now; we just have to do it sometime. Normally it is
25619 an error if this is an explicit instantiation but D is undefined.
25620 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25621 instantiated class template. */
25624 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25626 tree tmpl
= DECL_TI_TEMPLATE (d
);
25633 bool pattern_defined
;
25634 location_t saved_loc
= input_location
;
25635 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25636 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25640 /* This function should only be used to instantiate templates for
25641 functions and static member variables. */
25642 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25644 /* A concept is never instantiated. */
25645 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25647 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
25649 /* Variables are never deferred; if instantiation is required, they
25650 are instantiated right away. That allows for better code in the
25651 case that an expression refers to the value of the variable --
25652 if the variable has a constant value the referring expression can
25653 take advantage of that fact. */
25657 /* Don't instantiate cloned functions. Instead, instantiate the
25658 functions they cloned. */
25659 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25660 d
= DECL_CLONED_FUNCTION (d
);
25662 if (DECL_TEMPLATE_INSTANTIATED (d
)
25663 || TREE_TYPE (d
) == error_mark_node
25664 || (TREE_CODE (d
) == FUNCTION_DECL
25665 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25666 || DECL_TEMPLATE_SPECIALIZATION (d
))
25667 /* D has already been instantiated or explicitly specialized, so
25668 there's nothing for us to do here.
25670 It might seem reasonable to check whether or not D is an explicit
25671 instantiation, and, if so, stop here. But when an explicit
25672 instantiation is deferred until the end of the compilation,
25673 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25674 the instantiation. */
25677 /* Check to see whether we know that this template will be
25678 instantiated in some other file, as with "extern template"
25680 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25682 /* In general, we do not instantiate such templates. */
25683 if (external_p
&& !always_instantiate_p (d
))
25686 gen_tmpl
= most_general_template (tmpl
);
25687 gen_args
= DECL_TI_ARGS (d
);
25689 /* We should already have the extra args. */
25690 gcc_checking_assert (tmpl
== gen_tmpl
25691 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25692 == TMPL_ARGS_DEPTH (gen_args
)));
25693 /* And what's in the hash table should match D. */
25694 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25696 || spec
== NULL_TREE
);
25698 /* This needs to happen before any tsubsting. */
25699 if (! push_tinst_level (d
))
25702 timevar_push (TV_TEMPLATE_INST
);
25704 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25705 for the instantiation. */
25706 td
= template_for_substitution (d
);
25711 /* Look up an explicit specialization, if any. */
25712 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25713 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25714 if (elt
&& elt
!= error_mark_node
)
25716 td
= TREE_VALUE (elt
);
25717 args
= TREE_PURPOSE (elt
);
25721 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25723 /* We should never be trying to instantiate a member of a class
25724 template or partial specialization. */
25725 gcc_assert (d
!= code_pattern
);
25727 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25728 || DECL_TEMPLATE_SPECIALIZATION (td
))
25729 /* In the case of a friend template whose definition is provided
25730 outside the class, we may have too many arguments. Drop the
25731 ones we don't need. The same is true for specializations. */
25732 args
= get_innermost_template_args
25733 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25735 if (TREE_CODE (d
) == FUNCTION_DECL
)
25737 deleted_p
= DECL_DELETED_FN (code_pattern
);
25738 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25739 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25740 || DECL_DEFAULTED_FN (code_pattern
)
25746 if (DECL_CLASS_SCOPE_P (code_pattern
))
25747 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25749 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25752 /* We may be in the middle of deferred access check. Disable it now. */
25753 push_deferring_access_checks (dk_no_deferred
);
25755 /* Unless an explicit instantiation directive has already determined
25756 the linkage of D, remember that a definition is available for
25758 if (pattern_defined
25759 && !DECL_INTERFACE_KNOWN (d
)
25760 && !DECL_NOT_REALLY_EXTERN (d
))
25761 mark_definable (d
);
25763 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25764 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25765 input_location
= DECL_SOURCE_LOCATION (d
);
25767 /* If D is a member of an explicitly instantiated class template,
25768 and no definition is available, treat it like an implicit
25770 if (!pattern_defined
&& expl_inst_class_mem_p
25771 && DECL_EXPLICIT_INSTANTIATION (d
))
25773 /* Leave linkage flags alone on instantiations with anonymous
25775 if (TREE_PUBLIC (d
))
25777 DECL_NOT_REALLY_EXTERN (d
) = 0;
25778 DECL_INTERFACE_KNOWN (d
) = 0;
25780 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25783 /* Defer all other templates, unless we have been explicitly
25784 forbidden from doing so. */
25785 if (/* If there is no definition, we cannot instantiate the
25788 /* If it's OK to postpone instantiation, do so. */
25790 /* If this is a static data member that will be defined
25791 elsewhere, we don't want to instantiate the entire data
25792 member, but we do want to instantiate the initializer so that
25793 we can substitute that elsewhere. */
25794 || (external_p
&& VAR_P (d
))
25795 /* Handle here a deleted function too, avoid generating
25796 its body (c++/61080). */
25799 /* The definition of the static data member is now required so
25800 we must substitute the initializer. */
25802 && !DECL_INITIAL (d
)
25803 && DECL_INITIAL (code_pattern
))
25807 bool const_init
= false;
25808 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25810 ns
= decl_namespace_context (d
);
25811 push_nested_namespace (ns
);
25813 push_nested_class (DECL_CONTEXT (d
));
25814 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25816 tf_warning_or_error
, NULL_TREE
,
25817 /*integral_constant_expression_p=*/false);
25818 /* If instantiating the initializer involved instantiating this
25819 again, don't call cp_finish_decl twice. */
25820 if (!DECL_INITIAL (d
))
25822 /* Make sure the initializer is still constant, in case of
25823 circular dependency (template/instantiate6.C). */
25825 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25826 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25827 /*asmspec_tree=*/NULL_TREE
,
25828 LOOKUP_ONLYCONVERTING
);
25831 pop_nested_class ();
25832 pop_nested_namespace (ns
);
25835 /* We restore the source position here because it's used by
25836 add_pending_template. */
25837 input_location
= saved_loc
;
25839 if (at_eof
&& !pattern_defined
25840 && DECL_EXPLICIT_INSTANTIATION (d
)
25841 && DECL_NOT_REALLY_EXTERN (d
))
25844 The definition of a non-exported function template, a
25845 non-exported member function template, or a non-exported
25846 member function or static data member of a class template
25847 shall be present in every translation unit in which it is
25848 explicitly instantiated. */
25849 permerror (input_location
, "explicit instantiation of %qD "
25850 "but no definition available", d
);
25852 /* If we're in unevaluated context, we just wanted to get the
25853 constant value; this isn't an odr use, so don't queue
25854 a full instantiation. */
25855 if (!cp_unevaluated_operand
25856 /* ??? Historically, we have instantiated inline functions, even
25857 when marked as "extern template". */
25858 && !(external_p
&& VAR_P (d
)))
25859 add_pending_template (d
);
25863 if (variable_template_p (gen_tmpl
))
25864 note_variable_template_instantiation (d
);
25865 instantiate_body (td
, args
, d
, false);
25868 pop_deferring_access_checks ();
25869 timevar_pop (TV_TEMPLATE_INST
);
25870 pop_tinst_level ();
25871 input_location
= saved_loc
;
25872 cp_unevaluated_operand
= saved_unevaluated_operand
;
25873 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25878 /* Run through the list of templates that we wish we could
25879 instantiate, and instantiate any we can. RETRIES is the
25880 number of times we retry pending template instantiation. */
25883 instantiate_pending_templates (int retries
)
25886 location_t saved_loc
= input_location
;
25888 /* Instantiating templates may trigger vtable generation. This in turn
25889 may require further template instantiations. We place a limit here
25890 to avoid infinite loop. */
25891 if (pending_templates
&& retries
>= max_tinst_depth
)
25893 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25895 fatal_error (input_location
,
25896 "template instantiation depth exceeds maximum of %d"
25897 " instantiating %q+D, possibly from virtual table generation"
25898 " (use %<-ftemplate-depth=%> to increase the maximum)",
25899 max_tinst_depth
, decl
);
25900 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25901 /* Pretend that we defined it. */
25902 DECL_INITIAL (decl
) = error_mark_node
;
25908 struct pending_template
**t
= &pending_templates
;
25909 struct pending_template
*last
= NULL
;
25913 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25914 bool complete
= false;
25916 if (TYPE_P (instantiation
))
25918 if (!COMPLETE_TYPE_P (instantiation
))
25920 instantiate_class_template (instantiation
);
25921 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25922 for (tree fld
= TYPE_FIELDS (instantiation
);
25923 fld
; fld
= TREE_CHAIN (fld
))
25925 || (TREE_CODE (fld
) == FUNCTION_DECL
25926 && !DECL_ARTIFICIAL (fld
)))
25927 && DECL_TEMPLATE_INSTANTIATION (fld
))
25928 instantiate_decl (fld
,
25929 /*defer_ok=*/false,
25930 /*expl_inst_class_mem_p=*/false);
25932 if (COMPLETE_TYPE_P (instantiation
))
25936 complete
= COMPLETE_TYPE_P (instantiation
);
25940 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25941 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25944 = instantiate_decl (instantiation
,
25945 /*defer_ok=*/false,
25946 /*expl_inst_class_mem_p=*/false);
25947 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25951 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25952 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25957 /* If INSTANTIATION has been instantiated, then we don't
25958 need to consider it again in the future. */
25959 struct pending_template
*drop
= *t
;
25961 set_refcount_ptr (drop
->tinst
);
25962 pending_template_freelist ().free (drop
);
25970 set_refcount_ptr (current_tinst_level
);
25972 last_pending_template
= last
;
25974 while (reconsider
);
25976 input_location
= saved_loc
;
25979 /* Substitute ARGVEC into T, which is a list of initializers for
25980 either base class or a non-static data member. The TREE_PURPOSEs
25981 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25982 instantiate_decl. */
25985 tsubst_initializer_list (tree t
, tree argvec
)
25987 tree inits
= NULL_TREE
;
25988 tree target_ctor
= error_mark_node
;
25990 for (; t
; t
= TREE_CHAIN (t
))
25994 tree expanded_bases
= NULL_TREE
;
25995 tree expanded_arguments
= NULL_TREE
;
25998 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26003 /* Expand the base class expansion type into separate base
26005 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26006 tf_warning_or_error
,
26008 if (expanded_bases
== error_mark_node
)
26011 /* We'll be building separate TREE_LISTs of arguments for
26013 len
= TREE_VEC_LENGTH (expanded_bases
);
26014 expanded_arguments
= make_tree_vec (len
);
26015 for (i
= 0; i
< len
; i
++)
26016 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26018 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26019 expand each argument in the TREE_VALUE of t. */
26020 expr
= make_node (EXPR_PACK_EXPANSION
);
26021 PACK_EXPANSION_LOCAL_P (expr
) = true;
26022 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26023 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26025 if (TREE_VALUE (t
) == void_type_node
)
26026 /* VOID_TYPE_NODE is used to indicate
26027 value-initialization. */
26029 for (i
= 0; i
< len
; i
++)
26030 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26034 /* Substitute parameter packs into each argument in the
26036 in_base_initializer
= 1;
26037 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26039 tree expanded_exprs
;
26041 /* Expand the argument. */
26042 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26044 = tsubst_pack_expansion (expr
, argvec
,
26045 tf_warning_or_error
,
26047 if (expanded_exprs
== error_mark_node
)
26050 /* Prepend each of the expanded expressions to the
26051 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26052 for (i
= 0; i
< len
; i
++)
26054 TREE_VEC_ELT (expanded_arguments
, i
) =
26055 tree_cons (NULL_TREE
,
26056 TREE_VEC_ELT (expanded_exprs
, i
),
26057 TREE_VEC_ELT (expanded_arguments
, i
));
26060 in_base_initializer
= 0;
26062 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26063 since we built them backwards. */
26064 for (i
= 0; i
< len
; i
++)
26066 TREE_VEC_ELT (expanded_arguments
, i
) =
26067 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26072 for (i
= 0; i
< len
; ++i
)
26074 if (expanded_bases
)
26076 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26077 decl
= expand_member_init (decl
);
26078 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26083 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26084 tf_warning_or_error
, NULL_TREE
);
26086 decl
= expand_member_init (decl
);
26087 if (decl
&& !DECL_P (decl
))
26088 in_base_initializer
= 1;
26090 init
= TREE_VALUE (t
);
26092 if (init
!= void_type_node
)
26093 init
= tsubst_expr (init
, argvec
,
26094 tf_warning_or_error
, NULL_TREE
,
26095 /*integral_constant_expression_p=*/false);
26096 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26097 /* If we had an initializer but it instantiated to nothing,
26098 value-initialize the object. This will only occur when
26099 the initializer was a pack expansion where the parameter
26100 packs used in that expansion were of length zero. */
26101 init
= void_type_node
;
26102 in_base_initializer
= 0;
26105 if (target_ctor
!= error_mark_node
26106 && init
!= error_mark_node
)
26108 error ("mem-initializer for %qD follows constructor delegation",
26112 /* Look for a target constructor. */
26113 if (init
!= error_mark_node
26114 && decl
&& CLASS_TYPE_P (decl
)
26115 && same_type_p (decl
, current_class_type
))
26117 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26120 error ("constructor delegation follows mem-initializer for %qD",
26121 TREE_PURPOSE (inits
));
26124 target_ctor
= init
;
26129 init
= build_tree_list (decl
, init
);
26130 /* Carry over the dummy TREE_TYPE node containing the source
26132 TREE_TYPE (init
) = TREE_TYPE (t
);
26133 TREE_CHAIN (init
) = inits
;
26141 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26144 set_current_access_from_decl (tree decl
)
26146 if (TREE_PRIVATE (decl
))
26147 current_access_specifier
= access_private_node
;
26148 else if (TREE_PROTECTED (decl
))
26149 current_access_specifier
= access_protected_node
;
26151 current_access_specifier
= access_public_node
;
26154 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26155 is the instantiation (which should have been created with
26156 start_enum) and ARGS are the template arguments to use. */
26159 tsubst_enum (tree tag
, tree newtag
, tree args
)
26163 if (SCOPED_ENUM_P (newtag
))
26164 begin_scope (sk_scoped_enum
, newtag
);
26166 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26171 decl
= TREE_VALUE (e
);
26172 /* Note that in a template enum, the TREE_VALUE is the
26173 CONST_DECL, not the corresponding INTEGER_CST. */
26174 value
= tsubst_expr (DECL_INITIAL (decl
),
26175 args
, tf_warning_or_error
, NULL_TREE
,
26176 /*integral_constant_expression_p=*/true);
26178 /* Give this enumeration constant the correct access. */
26179 set_current_access_from_decl (decl
);
26181 /* Actually build the enumerator itself. Here we're assuming that
26182 enumerators can't have dependent attributes. */
26183 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26184 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26187 if (SCOPED_ENUM_P (newtag
))
26190 finish_enum_value_list (newtag
);
26191 finish_enum (newtag
);
26193 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26194 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26197 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26198 its type -- but without substituting the innermost set of template
26199 arguments. So, innermost set of template parameters will appear in
26203 get_mostly_instantiated_function_type (tree decl
)
26205 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26206 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26209 /* Return truthvalue if we're processing a template different from
26210 the last one involved in diagnostics. */
26212 problematic_instantiation_changed (void)
26214 return current_tinst_level
!= last_error_tinst_level
;
26217 /* Remember current template involved in diagnostics. */
26219 record_last_problematic_instantiation (void)
26221 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26224 struct tinst_level
*
26225 current_instantiation (void)
26227 return current_tinst_level
;
26230 /* Return TRUE if current_function_decl is being instantiated, false
26234 instantiating_current_function_p (void)
26236 return (current_instantiation ()
26237 && (current_instantiation ()->maybe_get_node ()
26238 == current_function_decl
));
26241 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26242 type. Return false for ok, true for disallowed. Issue error and
26243 inform messages under control of COMPLAIN. */
26246 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26248 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26250 else if (TYPE_PTR_P (type
))
26252 else if (TYPE_REF_P (type
)
26253 && !TYPE_REF_IS_RVALUE (type
))
26255 else if (TYPE_PTRMEM_P (type
))
26257 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26259 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26261 if (complain
& tf_error
)
26262 error ("non-type template parameters of deduced class type only "
26263 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26268 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26270 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26272 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26274 /* A bound template template parm could later be instantiated to have a valid
26275 nontype parm type via an alias template. */
26276 else if (cxx_dialect
>= cxx11
26277 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26279 else if (VOID_TYPE_P (type
))
26280 /* Fall through. */;
26281 else if (cxx_dialect
>= cxx20
)
26283 if (dependent_type_p (type
))
26285 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26287 if (structural_type_p (type
))
26289 if (complain
& tf_error
)
26291 auto_diagnostic_group d
;
26292 error ("%qT is not a valid type for a template non-type "
26293 "parameter because it is not structural", type
);
26294 structural_type_p (type
, true);
26298 else if (CLASS_TYPE_P (type
))
26300 if (complain
& tf_error
)
26301 error ("non-type template parameters of class type only available "
26302 "with %<-std=c++20%> or %<-std=gnu++20%>");
26306 if (complain
& tf_error
)
26308 if (type
== error_mark_node
)
26309 inform (input_location
, "invalid template non-type parameter");
26311 error ("%q#T is not a valid type for a template non-type parameter",
26317 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26318 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26321 dependent_type_p_r (tree type
)
26327 A type is dependent if it is:
26329 -- a template parameter. Template template parameters are types
26330 for us (since TYPE_P holds true for them) so we handle
26332 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26333 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26335 /* -- a qualified-id with a nested-name-specifier which contains a
26336 class-name that names a dependent type or whose unqualified-id
26337 names a dependent type. */
26338 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26341 /* An alias template specialization can be dependent even if the
26342 resulting type is not. */
26343 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26346 /* -- a cv-qualified type where the cv-unqualified type is
26348 No code is necessary for this bullet; the code below handles
26349 cv-qualified types, and we don't want to strip aliases with
26350 TYPE_MAIN_VARIANT because of DR 1558. */
26351 /* -- a compound type constructed from any dependent type. */
26352 if (TYPE_PTRMEM_P (type
))
26353 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26354 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26356 else if (INDIRECT_TYPE_P (type
))
26357 return dependent_type_p (TREE_TYPE (type
));
26358 else if (FUNC_OR_METHOD_TYPE_P (type
))
26362 if (dependent_type_p (TREE_TYPE (type
)))
26364 for (arg_type
= TYPE_ARG_TYPES (type
);
26366 arg_type
= TREE_CHAIN (arg_type
))
26367 if (dependent_type_p (TREE_VALUE (arg_type
)))
26369 if (cxx_dialect
>= cxx17
)
26370 /* A value-dependent noexcept-specifier makes the type dependent. */
26371 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26372 if (tree noex
= TREE_PURPOSE (spec
))
26373 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26374 affect overload resolution and treating it as dependent breaks
26375 things. Same for an unparsed noexcept expression. */
26376 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26377 && TREE_CODE (noex
) != DEFERRED_PARSE
26378 && value_dependent_expression_p (noex
))
26382 /* -- an array type constructed from any dependent type or whose
26383 size is specified by a constant expression that is
26386 We checked for type- and value-dependence of the bounds in
26387 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26388 if (TREE_CODE (type
) == ARRAY_TYPE
)
26390 if (TYPE_DOMAIN (type
)
26391 && dependent_type_p (TYPE_DOMAIN (type
)))
26393 return dependent_type_p (TREE_TYPE (type
));
26396 /* -- a template-id in which either the template name is a template
26398 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26400 /* ... or any of the template arguments is a dependent type or
26401 an expression that is type-dependent or value-dependent. */
26402 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26403 && (any_dependent_template_arguments_p
26404 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26407 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26408 dependent; if the argument of the `typeof' expression is not
26409 type-dependent, then it should already been have resolved. */
26410 if (TREE_CODE (type
) == TYPEOF_TYPE
26411 || TREE_CODE (type
) == DECLTYPE_TYPE
26412 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26415 /* A template argument pack is dependent if any of its packed
26417 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26419 tree args
= ARGUMENT_PACK_ARGS (type
);
26420 int i
, len
= TREE_VEC_LENGTH (args
);
26421 for (i
= 0; i
< len
; ++i
)
26422 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26426 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26427 be template parameters. */
26428 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26431 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26434 /* The standard does not specifically mention types that are local
26435 to template functions or local classes, but they should be
26436 considered dependent too. For example:
26438 template <int I> void f() {
26443 The size of `E' cannot be known until the value of `I' has been
26444 determined. Therefore, `E' must be considered dependent. */
26445 scope
= TYPE_CONTEXT (type
);
26446 if (scope
&& TYPE_P (scope
))
26447 return dependent_type_p (scope
);
26448 /* Don't use type_dependent_expression_p here, as it can lead
26449 to infinite recursion trying to determine whether a lambda
26450 nested in a lambda is dependent (c++/47687). */
26451 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26452 && DECL_LANG_SPECIFIC (scope
)
26453 && DECL_TEMPLATE_INFO (scope
)
26454 && (any_dependent_template_arguments_p
26455 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26458 /* Other types are non-dependent. */
26462 /* Returns TRUE if TYPE is dependent, in the sense of
26463 [temp.dep.type]. Note that a NULL type is considered dependent. */
26466 dependent_type_p (tree type
)
26468 /* If there are no template parameters in scope, then there can't be
26469 any dependent types. */
26470 if (!processing_template_decl
)
26472 /* If we are not processing a template, then nobody should be
26473 providing us with a dependent type. */
26475 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26479 /* If the type is NULL, we have not computed a type for the entity
26480 in question; in that case, the type is dependent. */
26484 /* Erroneous types can be considered non-dependent. */
26485 if (type
== error_mark_node
)
26488 /* Getting here with global_type_node means we improperly called this
26489 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26490 gcc_checking_assert (type
!= global_type_node
);
26492 /* If we have not already computed the appropriate value for TYPE,
26494 if (!TYPE_DEPENDENT_P_VALID (type
))
26496 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26497 TYPE_DEPENDENT_P_VALID (type
) = 1;
26500 return TYPE_DEPENDENT_P (type
);
26503 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26504 lookup. In other words, a dependent type that is not the current
26508 dependent_scope_p (tree scope
)
26510 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26511 && !currently_open_class (scope
));
26514 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26515 an unknown base of 'this' (and is therefore instantiation-dependent). */
26518 unknown_base_ref_p (tree t
)
26520 if (!current_class_ptr
)
26523 tree mem
= TREE_OPERAND (t
, 1);
26524 if (shared_member_p (mem
))
26527 tree cur
= current_nonlambda_class_type ();
26528 if (!any_dependent_bases_p (cur
))
26531 tree ctx
= TREE_OPERAND (t
, 0);
26532 if (DERIVED_FROM_P (ctx
, cur
))
26538 /* T is a SCOPE_REF; return whether we need to consider it
26539 instantiation-dependent so that we can check access at instantiation
26540 time even though we know which member it resolves to. */
26543 instantiation_dependent_scope_ref_p (tree t
)
26545 if (DECL_P (TREE_OPERAND (t
, 1))
26546 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26547 && !unknown_base_ref_p (t
)
26548 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26549 TREE_OPERAND (t
, 1)))
26555 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26556 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26559 /* Note that this predicate is not appropriate for general expressions;
26560 only constant expressions (that satisfy potential_constant_expression)
26561 can be tested for value dependence. */
26564 value_dependent_expression_p (tree expression
)
26566 if (!processing_template_decl
|| expression
== NULL_TREE
)
26569 /* A type-dependent expression is also value-dependent. */
26570 if (type_dependent_expression_p (expression
))
26573 switch (TREE_CODE (expression
))
26576 /* A dependent member function of the current instantiation. */
26577 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26579 case FUNCTION_DECL
:
26580 /* A dependent member function of the current instantiation. */
26581 if (DECL_CLASS_SCOPE_P (expression
)
26582 && dependent_type_p (DECL_CONTEXT (expression
)))
26586 case IDENTIFIER_NODE
:
26587 /* A name that has not been looked up -- must be dependent. */
26590 case TEMPLATE_PARM_INDEX
:
26591 /* A non-type template parm. */
26595 /* A non-type template parm. */
26596 if (DECL_TEMPLATE_PARM_P (expression
))
26598 return value_dependent_expression_p (DECL_INITIAL (expression
));
26601 /* A constant with literal type and is initialized
26602 with an expression that is value-dependent. */
26603 if (DECL_DEPENDENT_INIT_P (expression
)
26604 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26605 || TYPE_REF_P (TREE_TYPE (expression
)))
26607 if (DECL_HAS_VALUE_EXPR_P (expression
))
26609 tree value_expr
= DECL_VALUE_EXPR (expression
);
26610 if (value_dependent_expression_p (value_expr
)
26611 /* __PRETTY_FUNCTION__ inside a template function is dependent
26612 on the name of the function. */
26613 || (DECL_PRETTY_FUNCTION_P (expression
)
26614 /* It might be used in a template, but not a template
26615 function, in which case its DECL_VALUE_EXPR will be
26617 && value_expr
== error_mark_node
))
26622 case DYNAMIC_CAST_EXPR
:
26623 case STATIC_CAST_EXPR
:
26624 case CONST_CAST_EXPR
:
26625 case REINTERPRET_CAST_EXPR
:
26627 case IMPLICIT_CONV_EXPR
:
26628 /* These expressions are value-dependent if the type to which
26629 the cast occurs is dependent or the expression being casted
26630 is value-dependent. */
26632 tree type
= TREE_TYPE (expression
);
26634 if (dependent_type_p (type
))
26637 /* A functional cast has a list of operands. */
26638 expression
= TREE_OPERAND (expression
, 0);
26641 /* If there are no operands, it must be an expression such
26642 as "int()". This should not happen for aggregate types
26643 because it would form non-constant expressions. */
26644 gcc_assert (cxx_dialect
>= cxx11
26645 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26650 if (TREE_CODE (expression
) == TREE_LIST
)
26651 return any_value_dependent_elements_p (expression
);
26653 return value_dependent_expression_p (expression
);
26657 if (SIZEOF_EXPR_TYPE_P (expression
))
26658 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26662 /* A `sizeof' expression is value-dependent if the operand is
26663 type-dependent or is a pack expansion. */
26664 expression
= TREE_OPERAND (expression
, 0);
26665 if (PACK_EXPANSION_P (expression
))
26667 else if (TYPE_P (expression
))
26668 return dependent_type_p (expression
);
26669 return instantiation_dependent_uneval_expression_p (expression
);
26671 case AT_ENCODE_EXPR
:
26672 /* An 'encode' expression is value-dependent if the operand is
26674 expression
= TREE_OPERAND (expression
, 0);
26675 return dependent_type_p (expression
);
26677 case NOEXCEPT_EXPR
:
26678 expression
= TREE_OPERAND (expression
, 0);
26679 return instantiation_dependent_uneval_expression_p (expression
);
26682 /* All instantiation-dependent expressions should also be considered
26683 value-dependent. */
26684 return instantiation_dependent_scope_ref_p (expression
);
26686 case COMPONENT_REF
:
26687 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26688 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26690 case NONTYPE_ARGUMENT_PACK
:
26691 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26692 is value-dependent. */
26694 tree values
= ARGUMENT_PACK_ARGS (expression
);
26695 int i
, len
= TREE_VEC_LENGTH (values
);
26697 for (i
= 0; i
< len
; ++i
)
26698 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26706 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26708 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26714 if (TREE_CODE (type2
) != TREE_LIST
)
26715 return dependent_type_p (type2
);
26717 for (; type2
; type2
= TREE_CHAIN (type2
))
26718 if (dependent_type_p (TREE_VALUE (type2
)))
26725 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26726 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26729 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26730 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26734 tree op
= TREE_OPERAND (expression
, 0);
26735 return (value_dependent_expression_p (op
)
26736 || has_value_dependent_address (op
));
26739 case REQUIRES_EXPR
:
26740 /* Treat all requires-expressions as value-dependent so
26741 we don't try to fold them. */
26745 return dependent_type_p (TREE_OPERAND (expression
, 0));
26749 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26751 tree fn
= get_callee_fndecl (expression
);
26753 nargs
= call_expr_nargs (expression
);
26754 for (i
= 0; i
< nargs
; ++i
)
26756 tree op
= CALL_EXPR_ARG (expression
, i
);
26757 /* In a call to a constexpr member function, look through the
26758 implicit ADDR_EXPR on the object argument so that it doesn't
26759 cause the call to be considered value-dependent. We also
26760 look through it in potential_constant_expression. */
26761 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26762 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26763 && TREE_CODE (op
) == ADDR_EXPR
)
26764 op
= TREE_OPERAND (op
, 0);
26765 if (value_dependent_expression_p (op
))
26771 case TEMPLATE_ID_EXPR
:
26772 return concept_definition_p (TREE_OPERAND (expression
, 0));
26778 if (dependent_type_p (TREE_TYPE (expression
)))
26780 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26781 if (value_dependent_expression_p (val
))
26787 /* Treat a GNU statement expression as dependent to avoid crashing
26788 under instantiate_non_dependent_expr; it can't be constant. */
26792 /* A constant expression is value-dependent if any subexpression is
26793 value-dependent. */
26794 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26796 case tcc_reference
:
26798 case tcc_comparison
:
26800 case tcc_expression
:
26803 int i
, len
= cp_tree_operand_length (expression
);
26805 for (i
= 0; i
< len
; i
++)
26807 tree t
= TREE_OPERAND (expression
, i
);
26809 /* In some cases, some of the operands may be missing.
26810 (For example, in the case of PREDECREMENT_EXPR, the
26811 amount to increment by may be missing.) That doesn't
26812 make the expression dependent. */
26813 if (t
&& value_dependent_expression_p (t
))
26824 /* The expression is not value-dependent. */
26828 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26829 [temp.dep.expr]. Note that an expression with no type is
26830 considered dependent. Other parts of the compiler arrange for an
26831 expression with type-dependent subexpressions to have no type, so
26832 this function doesn't have to be fully recursive. */
26835 type_dependent_expression_p (tree expression
)
26837 if (!processing_template_decl
)
26840 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26843 STRIP_ANY_LOCATION_WRAPPER (expression
);
26845 /* An unresolved name is always dependent. */
26846 if (identifier_p (expression
)
26847 || TREE_CODE (expression
) == USING_DECL
26848 || TREE_CODE (expression
) == WILDCARD_DECL
)
26851 /* A lambda-expression in template context is dependent. dependent_type_p is
26852 true for a lambda in the scope of a class or function template, but that
26853 doesn't cover all template contexts, like a default template argument. */
26854 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26857 /* A fold expression is type-dependent. */
26858 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26859 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26860 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26861 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26864 /* Some expression forms are never type-dependent. */
26865 if (TREE_CODE (expression
) == SIZEOF_EXPR
26866 || TREE_CODE (expression
) == ALIGNOF_EXPR
26867 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26868 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26869 || TREE_CODE (expression
) == TRAIT_EXPR
26870 || TREE_CODE (expression
) == TYPEID_EXPR
26871 || TREE_CODE (expression
) == DELETE_EXPR
26872 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26873 || TREE_CODE (expression
) == THROW_EXPR
26874 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26877 /* The types of these expressions depends only on the type to which
26878 the cast occurs. */
26879 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26880 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26881 || TREE_CODE (expression
) == CONST_CAST_EXPR
26882 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26883 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26884 || TREE_CODE (expression
) == CAST_EXPR
)
26885 return dependent_type_p (TREE_TYPE (expression
));
26887 /* The types of these expressions depends only on the type created
26888 by the expression. */
26889 if (TREE_CODE (expression
) == NEW_EXPR
26890 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26892 /* For NEW_EXPR tree nodes created inside a template, either
26893 the object type itself or a TREE_LIST may appear as the
26895 tree type
= TREE_OPERAND (expression
, 1);
26896 if (TREE_CODE (type
) == TREE_LIST
)
26897 /* This is an array type. We need to check array dimensions
26899 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26900 || value_dependent_expression_p
26901 (TREE_OPERAND (TREE_VALUE (type
), 1));
26902 /* Array type whose dimension has to be deduced. */
26903 else if (TREE_CODE (type
) == ARRAY_TYPE
26904 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
26907 return dependent_type_p (type
);
26910 if (TREE_CODE (expression
) == SCOPE_REF
)
26912 tree scope
= TREE_OPERAND (expression
, 0);
26913 tree name
= TREE_OPERAND (expression
, 1);
26915 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26916 contains an identifier associated by name lookup with one or more
26917 declarations declared with a dependent type, or...a
26918 nested-name-specifier or qualified-id that names a member of an
26919 unknown specialization. */
26920 return (type_dependent_expression_p (name
)
26921 || dependent_scope_p (scope
));
26924 if (TREE_CODE (expression
) == TEMPLATE_DECL
26925 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26926 return uses_outer_template_parms (expression
);
26928 if (TREE_CODE (expression
) == STMT_EXPR
)
26929 expression
= stmt_expr_value_expr (expression
);
26931 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26938 if (type_dependent_expression_p (elt
))
26944 /* A static data member of the current instantiation with incomplete
26945 array type is type-dependent, as the definition and specializations
26946 can have different bounds. */
26947 if (VAR_P (expression
)
26948 && DECL_CLASS_SCOPE_P (expression
)
26949 && dependent_type_p (DECL_CONTEXT (expression
))
26950 && VAR_HAD_UNKNOWN_BOUND (expression
))
26953 /* An array of unknown bound depending on a variadic parameter, eg:
26955 template<typename... Args>
26956 void foo (Args... args)
26958 int arr[] = { args... };
26961 template<int... vals>
26964 int arr[] = { vals... };
26967 If the array has no length and has an initializer, it must be that
26968 we couldn't determine its length in cp_complete_array_type because
26969 it is dependent. */
26970 if (VAR_P (expression
)
26971 && TREE_TYPE (expression
) != NULL_TREE
26972 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26973 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26974 && DECL_INITIAL (expression
))
26977 /* A function or variable template-id is type-dependent if it has any
26978 dependent template arguments. */
26979 if (VAR_OR_FUNCTION_DECL_P (expression
)
26980 && DECL_LANG_SPECIFIC (expression
)
26981 && DECL_TEMPLATE_INFO (expression
))
26983 /* Consider the innermost template arguments, since those are the ones
26984 that come from the template-id; the template arguments for the
26985 enclosing class do not make it type-dependent unless they are used in
26986 the type of the decl. */
26987 if (instantiates_primary_template_p (expression
)
26988 && (any_dependent_template_arguments_p
26989 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26993 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26994 type-dependent. Checking this is important for functions with auto return
26995 type, which looks like a dependent type. */
26996 if (TREE_CODE (expression
) == FUNCTION_DECL
26997 && !(DECL_CLASS_SCOPE_P (expression
)
26998 && dependent_type_p (DECL_CONTEXT (expression
)))
26999 && !(DECL_LANG_SPECIFIC (expression
)
27000 && DECL_FRIEND_P (expression
)
27001 && (!DECL_FRIEND_CONTEXT (expression
)
27002 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27003 && !DECL_LOCAL_DECL_P (expression
))
27005 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27006 || undeduced_auto_decl (expression
));
27010 /* Always dependent, on the number of arguments if nothing else. */
27011 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27014 if (TREE_TYPE (expression
) == unknown_type_node
)
27016 if (TREE_CODE (expression
) == ADDR_EXPR
)
27017 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27018 if (TREE_CODE (expression
) == COMPONENT_REF
27019 || TREE_CODE (expression
) == OFFSET_REF
)
27021 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27023 expression
= TREE_OPERAND (expression
, 1);
27024 if (identifier_p (expression
))
27027 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27028 if (TREE_CODE (expression
) == SCOPE_REF
)
27031 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27032 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27033 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27036 if (BASELINK_P (expression
))
27038 if (BASELINK_OPTYPE (expression
)
27039 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27041 expression
= BASELINK_FUNCTIONS (expression
);
27044 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27046 if (any_dependent_template_arguments_p
27047 (TREE_OPERAND (expression
, 1)))
27049 expression
= TREE_OPERAND (expression
, 0);
27050 if (identifier_p (expression
))
27054 gcc_assert (OVL_P (expression
));
27056 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27057 if (type_dependent_expression_p (*iter
))
27063 /* The type of a non-type template parm declared with a placeholder type
27064 depends on the corresponding template argument, even though
27065 placeholders are not normally considered dependent. */
27066 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27067 && is_auto (TREE_TYPE (expression
)))
27070 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27072 /* Dependent type attributes might not have made it from the decl to
27074 if (DECL_P (expression
)
27075 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27078 return (dependent_type_p (TREE_TYPE (expression
)));
27081 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27082 type-dependent if the expression refers to a member of the current
27083 instantiation and the type of the referenced member is dependent, or the
27084 class member access expression refers to a member of an unknown
27087 This function returns true if the OBJECT in such a class member access
27088 expression is of an unknown specialization. */
27091 type_dependent_object_expression_p (tree object
)
27093 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27095 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27097 tree scope
= TREE_TYPE (object
);
27098 return (!scope
|| dependent_scope_p (scope
));
27101 /* walk_tree callback function for instantiation_dependent_expression_p,
27102 below. Returns non-zero if a dependent subexpression is found. */
27105 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27110 /* We don't have to worry about decltype currently because decltype
27111 of an instantiation-dependent expr is a dependent type. This
27112 might change depending on the resolution of DR 1172. */
27113 *walk_subtrees
= false;
27116 enum tree_code code
= TREE_CODE (*tp
);
27119 /* Don't treat an argument list as dependent just because it has no
27123 case NONTYPE_ARGUMENT_PACK
:
27126 case TEMPLATE_PARM_INDEX
:
27127 if (dependent_type_p (TREE_TYPE (*tp
)))
27129 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27131 /* We'll check value-dependence separately. */
27134 /* Handle expressions with type operands. */
27138 case AT_ENCODE_EXPR
:
27140 tree op
= TREE_OPERAND (*tp
, 0);
27141 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27142 op
= TREE_TYPE (op
);
27145 if (dependent_type_p (op
))
27149 *walk_subtrees
= false;
27156 case COMPONENT_REF
:
27157 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27158 /* In a template, finish_class_member_access_expr creates a
27159 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27160 type-dependent, so that we can check access control at
27161 instantiation time (PR 42277). See also Core issue 1273. */
27166 if (instantiation_dependent_scope_ref_p (*tp
))
27171 /* Treat statement-expressions as dependent. */
27175 /* Treat requires-expressions as dependent. */
27176 case REQUIRES_EXPR
:
27180 /* Treat concept checks as dependent. */
27181 if (concept_check_p (*tp
))
27185 case TEMPLATE_ID_EXPR
:
27186 /* Treat concept checks as dependent. */
27187 if (concept_check_p (*tp
))
27192 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27200 if (type_dependent_expression_p (*tp
))
27206 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27207 sense defined by the ABI:
27209 "An expression is instantiation-dependent if it is type-dependent
27210 or value-dependent, or it has a subexpression that is type-dependent
27211 or value-dependent."
27213 Except don't actually check value-dependence for unevaluated expressions,
27214 because in sizeof(i) we don't care about the value of i. Checking
27215 type-dependence will in turn check value-dependence of array bounds/template
27216 arguments as needed. */
27219 instantiation_dependent_uneval_expression_p (tree expression
)
27223 if (!processing_template_decl
)
27226 if (expression
== error_mark_node
)
27229 result
= cp_walk_tree_without_duplicates (&expression
,
27230 instantiation_dependent_r
, NULL
);
27231 return result
!= NULL_TREE
;
27234 /* As above, but also check value-dependence of the expression as a whole. */
27237 instantiation_dependent_expression_p (tree expression
)
27239 return (instantiation_dependent_uneval_expression_p (expression
)
27240 || value_dependent_expression_p (expression
));
27243 /* Like type_dependent_expression_p, but it also works while not processing
27244 a template definition, i.e. during substitution or mangling. */
27247 type_dependent_expression_p_push (tree expr
)
27250 ++processing_template_decl
;
27251 b
= type_dependent_expression_p (expr
);
27252 --processing_template_decl
;
27256 /* Returns TRUE if ARGS contains a type-dependent expression. */
27259 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27264 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27266 if (type_dependent_expression_p (arg
))
27272 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27273 expressions) contains any type-dependent expressions. */
27276 any_type_dependent_elements_p (const_tree list
)
27278 for (; list
; list
= TREE_CHAIN (list
))
27279 if (type_dependent_expression_p (TREE_VALUE (list
)))
27285 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27286 expressions) contains any value-dependent expressions. */
27289 any_value_dependent_elements_p (const_tree list
)
27291 for (; list
; list
= TREE_CHAIN (list
))
27292 if (value_dependent_expression_p (TREE_VALUE (list
)))
27298 /* Returns TRUE if the ARG (a template argument) is dependent. */
27301 dependent_template_arg_p (tree arg
)
27303 if (!processing_template_decl
)
27306 /* Assume a template argument that was wrongly written by the user
27307 is dependent. This is consistent with what
27308 any_dependent_template_arguments_p [that calls this function]
27310 if (!arg
|| arg
== error_mark_node
)
27313 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27314 arg
= argument_pack_select_arg (arg
);
27316 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27318 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27320 if (DECL_TEMPLATE_PARM_P (arg
))
27322 /* A member template of a dependent class is not necessarily
27323 type-dependent, but it is a dependent template argument because it
27324 will be a member of an unknown specialization to that template. */
27325 tree scope
= CP_DECL_CONTEXT (arg
);
27326 return TYPE_P (scope
) && dependent_type_p (scope
);
27328 else if (ARGUMENT_PACK_P (arg
))
27330 tree args
= ARGUMENT_PACK_ARGS (arg
);
27331 int i
, len
= TREE_VEC_LENGTH (args
);
27332 for (i
= 0; i
< len
; ++i
)
27334 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27340 else if (TYPE_P (arg
))
27341 return dependent_type_p (arg
);
27343 return value_dependent_expression_p (arg
);
27346 /* Returns true if ARGS (a collection of template arguments) contains
27347 any types that require structural equality testing. */
27350 any_template_arguments_need_structural_equality_p (tree args
)
27357 if (args
== error_mark_node
)
27360 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27362 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27363 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27365 tree arg
= TREE_VEC_ELT (level
, j
);
27366 tree packed_args
= NULL_TREE
;
27369 if (ARGUMENT_PACK_P (arg
))
27371 /* Look inside the argument pack. */
27372 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27373 len
= TREE_VEC_LENGTH (packed_args
);
27376 for (k
= 0; k
< len
; ++k
)
27379 arg
= TREE_VEC_ELT (packed_args
, k
);
27381 if (error_operand_p (arg
))
27383 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27385 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27387 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27388 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27397 /* Returns true if ARGS (a collection of template arguments) contains
27398 any dependent arguments. */
27401 any_dependent_template_arguments_p (const_tree args
)
27408 if (args
== error_mark_node
)
27411 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27413 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27414 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27415 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27422 /* Returns true if ARGS contains any errors. */
27425 any_erroneous_template_args_p (const_tree args
)
27430 if (args
== error_mark_node
)
27433 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27435 if (tree ti
= get_template_info (args
))
27436 args
= TI_ARGS (ti
);
27444 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27446 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27447 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27448 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27455 /* Returns TRUE if the template TMPL is type-dependent. */
27458 dependent_template_p (tree tmpl
)
27460 if (TREE_CODE (tmpl
) == OVERLOAD
)
27462 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27463 if (dependent_template_p (*iter
))
27468 /* Template template parameters are dependent. */
27469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27470 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27472 /* So are names that have not been looked up. */
27473 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27478 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27481 dependent_template_id_p (tree tmpl
, tree args
)
27483 return (dependent_template_p (tmpl
)
27484 || any_dependent_template_arguments_p (args
));
27487 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27491 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27495 if (!processing_template_decl
)
27498 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27500 tree decl
= TREE_VEC_ELT (declv
, i
);
27501 tree init
= TREE_VEC_ELT (initv
, i
);
27502 tree cond
= TREE_VEC_ELT (condv
, i
);
27503 tree incr
= TREE_VEC_ELT (incrv
, i
);
27505 if (type_dependent_expression_p (decl
)
27506 || TREE_CODE (decl
) == SCOPE_REF
)
27509 if (init
&& type_dependent_expression_p (init
))
27512 if (cond
== global_namespace
)
27515 if (type_dependent_expression_p (cond
))
27518 if (COMPARISON_CLASS_P (cond
)
27519 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27520 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27523 if (TREE_CODE (incr
) == MODOP_EXPR
)
27525 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27526 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27529 else if (type_dependent_expression_p (incr
))
27531 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27533 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27535 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27537 tree t
= TREE_OPERAND (incr
, 1);
27538 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27539 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27542 /* If this loop has a class iterator with != comparison
27543 with increment other than i++/++i/i--/--i, make sure the
27544 increment is constant. */
27545 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27546 && TREE_CODE (cond
) == NE_EXPR
)
27548 if (TREE_OPERAND (t
, 0) == decl
)
27549 t
= TREE_OPERAND (t
, 1);
27551 t
= TREE_OPERAND (t
, 0);
27552 if (TREE_CODE (t
) != INTEGER_CST
)
27562 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27563 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27564 no such TYPE can be found. Note that this function peers inside
27565 uninstantiated templates and therefore should be used only in
27566 extremely limited situations. ONLY_CURRENT_P restricts this
27567 peering to the currently open classes hierarchy (which is required
27568 when comparing types). */
27571 resolve_typename_type (tree type
, bool only_current_p
)
27580 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27582 scope
= TYPE_CONTEXT (type
);
27583 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27584 gcc_checking_assert (uses_template_parms (scope
));
27586 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27587 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27588 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27589 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27590 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27591 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27592 the TYPENAME_TYPE instead, we avoid messing up with a possible
27593 typedef variant case. */
27594 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27596 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27597 it first before we can figure out what NAME refers to. */
27598 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27600 if (TYPENAME_IS_RESOLVING_P (scope
))
27601 /* Given a class template A with a dependent base with nested type C,
27602 typedef typename A::C::C C will land us here, as trying to resolve
27603 the initial A::C leads to the local C typedef, which leads back to
27604 A::C::C. So we break the recursion now. */
27607 scope
= resolve_typename_type (scope
, only_current_p
);
27609 /* If we don't know what SCOPE refers to, then we cannot resolve the
27611 if (!CLASS_TYPE_P (scope
))
27613 /* If this is a typedef, we don't want to look inside (c++/11987). */
27614 if (typedef_variant_p (type
))
27616 /* If SCOPE isn't the template itself, it will not have a valid
27617 TYPE_FIELDS list. */
27618 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27619 /* scope is either the template itself or a compatible instantiation
27620 like X<T>, so look up the name in the original template. */
27621 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27622 /* If scope has no fields, it can't be a current instantiation. Check this
27623 before currently_open_class to avoid infinite recursion (71515). */
27624 if (!TYPE_FIELDS (scope
))
27626 /* If the SCOPE is not the current instantiation, there's no reason
27627 to look inside it. */
27628 if (only_current_p
&& !currently_open_class (scope
))
27630 /* Enter the SCOPE so that name lookup will be resolved as if we
27631 were in the class definition. In particular, SCOPE will no
27632 longer be considered a dependent type. */
27633 pushed_scope
= push_scope (scope
);
27634 /* Look up the declaration. */
27635 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27636 tf_warning_or_error
);
27638 result
= NULL_TREE
;
27640 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27641 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27642 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27645 else if (identifier_p (fullname
)
27646 && TREE_CODE (decl
) == TYPE_DECL
)
27648 result
= TREE_TYPE (decl
);
27649 if (result
== error_mark_node
)
27650 result
= NULL_TREE
;
27652 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27653 && DECL_CLASS_TEMPLATE_P (decl
))
27655 /* Obtain the template and the arguments. */
27656 tree tmpl
= TREE_OPERAND (fullname
, 0);
27657 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27659 /* We get here with a plain identifier because a previous tentative
27660 parse of the nested-name-specifier as part of a ptr-operator saw
27661 ::template X<A>. The use of ::template is necessary in a
27662 ptr-operator, but wrong in a declarator-id.
27664 [temp.names]: In a qualified-id of a declarator-id, the keyword
27665 template shall not appear at the top level. */
27666 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27667 "keyword %<template%> not allowed in declarator-id");
27670 tree args
= TREE_OPERAND (fullname
, 1);
27671 /* Instantiate the template. */
27672 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27673 /*entering_scope=*/true,
27674 tf_error
| tf_user
);
27675 if (result
== error_mark_node
)
27676 result
= NULL_TREE
;
27679 /* Leave the SCOPE. */
27681 pop_scope (pushed_scope
);
27683 /* If we failed to resolve it, return the original typename. */
27687 /* If lookup found a typename type, resolve that too. */
27688 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27690 /* Ill-formed programs can cause infinite recursion here, so we
27691 must catch that. */
27692 TYPENAME_IS_RESOLVING_P (result
) = 1;
27693 result
= resolve_typename_type (result
, only_current_p
);
27694 TYPENAME_IS_RESOLVING_P (result
) = 0;
27697 /* Qualify the resulting type. */
27698 quals
= cp_type_quals (type
);
27700 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27705 /* EXPR is an expression which is not type-dependent. Return a proxy
27706 for EXPR that can be used to compute the types of larger
27707 expressions containing EXPR. */
27710 build_non_dependent_expr (tree expr
)
27712 tree orig_expr
= expr
;
27715 /* When checking, try to get a constant value for all non-dependent
27716 expressions in order to expose bugs in *_dependent_expression_p
27717 and constexpr. This can affect code generation, see PR70704, so
27718 only do this for -fchecking=2. */
27719 if (flag_checking
> 1
27720 && cxx_dialect
>= cxx11
27721 /* Don't do this during nsdmi parsing as it can lead to
27722 unexpected recursive instantiations. */
27723 && !parsing_nsdmi ()
27724 /* Don't do this during concept processing either and for
27725 the same reason. */
27726 && !processing_constraint_expression_p ())
27727 fold_non_dependent_expr (expr
, tf_none
);
27729 STRIP_ANY_LOCATION_WRAPPER (expr
);
27731 /* Preserve OVERLOADs; the functions must be available to resolve
27734 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27735 inner_expr
= stmt_expr_value_expr (inner_expr
);
27736 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27737 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27738 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27739 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27740 if (is_overloaded_fn (inner_expr
)
27741 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27743 /* There is no need to return a proxy for a variable or enumerator. */
27744 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27746 /* Preserve string constants; conversions from string constants to
27747 "char *" are allowed, even though normally a "const char *"
27748 cannot be used to initialize a "char *". */
27749 if (TREE_CODE (expr
) == STRING_CST
)
27751 /* Preserve void and arithmetic constants, as an optimization -- there is no
27752 reason to create a new node. */
27753 if (TREE_CODE (expr
) == VOID_CST
27754 || TREE_CODE (expr
) == INTEGER_CST
27755 || TREE_CODE (expr
) == REAL_CST
)
27757 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27758 There is at least one place where we want to know that a
27759 particular expression is a throw-expression: when checking a ?:
27760 expression, there are special rules if the second or third
27761 argument is a throw-expression. */
27762 if (TREE_CODE (expr
) == THROW_EXPR
)
27765 /* Don't wrap an initializer list, we need to be able to look inside. */
27766 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27769 /* Don't wrap a dummy object, we need to be able to test for it. */
27770 if (is_dummy_object (expr
))
27773 if (TREE_CODE (expr
) == COND_EXPR
)
27774 return build3 (COND_EXPR
,
27776 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27777 (TREE_OPERAND (expr
, 1)
27778 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27779 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27780 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27781 if (TREE_CODE (expr
) == COMPOUND_EXPR
27782 && !COMPOUND_EXPR_OVERLOADED (expr
))
27783 return build2 (COMPOUND_EXPR
,
27785 TREE_OPERAND (expr
, 0),
27786 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27788 /* If the type is unknown, it can't really be non-dependent */
27789 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27791 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27792 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27793 TREE_TYPE (expr
), expr
);
27796 /* ARGS is a vector of expressions as arguments to a function call.
27797 Replace the arguments with equivalent non-dependent expressions.
27798 This modifies ARGS in place. */
27801 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27806 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27808 tree newarg
= build_non_dependent_expr (arg
);
27810 (*args
)[ix
] = newarg
;
27814 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27815 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27816 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27819 make_auto_1 (tree name
, bool set_canonical
)
27821 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27822 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27823 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27824 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27825 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27826 TYPE_NAME (au
), NULL_TREE
);
27828 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27829 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27830 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27831 if (name
== decltype_auto_identifier
)
27832 AUTO_IS_DECLTYPE (au
) = true;
27838 make_decltype_auto (void)
27840 return make_auto_1 (decltype_auto_identifier
, true);
27846 return make_auto_1 (auto_identifier
, true);
27849 /* Return a C++17 deduction placeholder for class template TMPL. */
27852 make_template_placeholder (tree tmpl
)
27854 tree t
= make_auto_1 (auto_identifier
, false);
27855 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27856 /* Our canonical type depends on the placeholder. */
27857 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27861 /* True iff T is a C++17 class template deduction placeholder. */
27864 template_placeholder_p (tree t
)
27866 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27869 /* Make a "constrained auto" type-specifier. This is an auto or
27870 decltype(auto) type with constraints that must be associated after
27871 deduction. The constraint is formed from the given concept CON
27872 and its optional sequence of template arguments ARGS.
27874 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27877 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27879 /* Build the constraint. */
27880 tree tmpl
= DECL_TI_TEMPLATE (con
);
27882 if (TREE_CODE (con
) == FUNCTION_DECL
)
27883 expr
= ovl_make (tmpl
);
27884 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27886 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27888 /* Our canonical type depends on the constraint. */
27889 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27891 /* Attach the constraint to the type declaration. */
27892 return TYPE_NAME (type
);
27895 /* Make a "constrained auto" type-specifier. */
27898 make_constrained_auto (tree con
, tree args
)
27900 tree type
= make_auto_1 (auto_identifier
, false);
27901 return make_constrained_placeholder_type (type
, con
, args
);
27904 /* Make a "constrained decltype(auto)" type-specifier. */
27907 make_constrained_decltype_auto (tree con
, tree args
)
27909 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27910 return make_constrained_placeholder_type (type
, con
, args
);
27913 /* Build and return a concept definition. Like other templates, the
27914 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27915 the TEMPLATE_DECL. */
27918 finish_concept_definition (cp_expr id
, tree init
)
27920 gcc_assert (identifier_p (id
));
27921 gcc_assert (processing_template_decl
);
27923 location_t loc
= id
.get_location();
27925 /* A concept-definition shall not have associated constraints. */
27926 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27928 error_at (loc
, "a concept cannot be constrained");
27929 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27932 /* A concept-definition shall appear in namespace scope. Templates
27933 aren't allowed in block scope, so we only need to check for class
27935 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27937 error_at (loc
, "concept %qE not in namespace scope", *id
);
27938 return error_mark_node
;
27941 /* Initially build the concept declaration; its type is bool. */
27942 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27943 DECL_CONTEXT (decl
) = current_scope ();
27944 DECL_INITIAL (decl
) = init
;
27946 /* Push the enclosing template. */
27947 return push_template_decl (decl
);
27950 /* Given type ARG, return std::initializer_list<ARG>. */
27955 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27957 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27959 gcc_rich_location
richloc (input_location
);
27960 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27961 error_at (&richloc
,
27962 "deducing from brace-enclosed initializer list"
27963 " requires %<#include <initializer_list>%>");
27965 return error_mark_node
;
27967 tree argvec
= make_tree_vec (1);
27968 TREE_VEC_ELT (argvec
, 0) = arg
;
27970 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27971 NULL_TREE
, 0, tf_warning_or_error
);
27974 /* Replace auto in TYPE with std::initializer_list<auto>. */
27977 listify_autos (tree type
, tree auto_node
)
27979 tree init_auto
= listify (strip_top_quals (auto_node
));
27980 tree argvec
= make_tree_vec (1);
27981 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27982 if (processing_template_decl
)
27983 argvec
= add_to_template_args (current_template_args (), argvec
);
27984 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27987 /* Hash traits for hashing possibly constrained 'auto'
27988 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27990 struct auto_hash
: default_hash_traits
<tree
>
27992 static inline hashval_t
hash (tree
);
27993 static inline bool equal (tree
, tree
);
27996 /* Hash the 'auto' T. */
27999 auto_hash::hash (tree t
)
28001 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28002 /* Matching constrained-type-specifiers denote the same template
28003 parameter, so hash the constraint. */
28004 return hash_placeholder_constraint (c
);
28006 /* But unconstrained autos are all separate, so just hash the pointer. */
28007 return iterative_hash_object (t
, 0);
28010 /* Compare two 'auto's. */
28013 auto_hash::equal (tree t1
, tree t2
)
28018 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28019 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28021 /* Two unconstrained autos are distinct. */
28025 return equivalent_placeholder_constraints (c1
, c2
);
28028 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28029 constrained) auto, add it to the vector. */
28032 extract_autos_r (tree t
, void *data
)
28034 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28037 /* All the autos were built with index 0; fix that up now. */
28038 tree
*p
= hash
.find_slot (t
, INSERT
);
28041 /* If this is a repeated constrained-type-specifier, use the index we
28043 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28046 /* Otherwise this is new, so use the current count. */
28048 idx
= hash
.elements () - 1;
28050 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28053 /* Always keep walking. */
28057 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28058 says they can appear anywhere in the type. */
28061 extract_autos (tree type
)
28063 hash_set
<tree
> visited
;
28064 hash_table
<auto_hash
> hash (2);
28066 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28068 tree tree_vec
= make_tree_vec (hash
.elements());
28069 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28070 iter
!= hash
.end(); ++iter
)
28073 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28074 TREE_VEC_ELT (tree_vec
, i
)
28075 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28081 /* The stem for deduction guide names. */
28082 const char *const dguide_base
= "__dguide_";
28084 /* Return the name for a deduction guide for class template TMPL. */
28087 dguide_name (tree tmpl
)
28089 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28090 tree tname
= TYPE_IDENTIFIER (type
);
28091 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28092 + IDENTIFIER_LENGTH (tname
));
28093 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28094 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28095 IDENTIFIER_LENGTH (tname
) + 1);
28096 tree dname
= get_identifier (buf
);
28097 TREE_TYPE (dname
) = type
;
28101 /* True if NAME is the name of a deduction guide. */
28104 dguide_name_p (tree name
)
28106 return (TREE_CODE (name
) == IDENTIFIER_NODE
28107 && TREE_TYPE (name
)
28108 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28109 strlen (dguide_base
)));
28112 /* True if FN is a deduction guide. */
28115 deduction_guide_p (const_tree fn
)
28118 if (tree name
= DECL_NAME (fn
))
28119 return dguide_name_p (name
);
28123 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28126 copy_guide_p (const_tree fn
)
28128 gcc_assert (deduction_guide_p (fn
));
28129 if (!DECL_ARTIFICIAL (fn
))
28131 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28132 return (TREE_CHAIN (parms
) == void_list_node
28133 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28136 /* True if FN is a guide generated from a constructor template. */
28139 template_guide_p (const_tree fn
)
28141 gcc_assert (deduction_guide_p (fn
));
28142 if (!DECL_ARTIFICIAL (fn
))
28144 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28145 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28146 return PRIMARY_TEMPLATE_P (org
);
28150 /* True if FN is an aggregate initialization guide or the copy deduction
28154 builtin_guide_p (const_tree fn
)
28156 if (!deduction_guide_p (fn
))
28158 if (!DECL_ARTIFICIAL (fn
))
28159 /* Explicitly declared. */
28161 if (DECL_ABSTRACT_ORIGIN (fn
))
28162 /* Derived from a constructor. */
28167 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28168 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28169 template parameter types. Note that the handling of template template
28170 parameters relies on current_template_parms being set appropriately for the
28174 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28175 tree tsubst_args
, tsubst_flags_t complain
)
28177 if (olddecl
== error_mark_node
)
28178 return error_mark_node
;
28180 tree oldidx
= get_template_parm_index (olddecl
);
28183 if (TREE_CODE (olddecl
) == TYPE_DECL
28184 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28186 tree oldtype
= TREE_TYPE (olddecl
);
28187 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28188 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28189 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28190 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28191 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28195 newtype
= TREE_TYPE (olddecl
);
28196 if (type_uses_auto (newtype
))
28198 // Substitute once to fix references to other template parameters.
28199 newtype
= tsubst (newtype
, tsubst_args
,
28200 complain
|tf_partial
, NULL_TREE
);
28201 // Now substitute again to reduce the level of the auto.
28202 newtype
= tsubst (newtype
, current_template_args (),
28203 complain
, NULL_TREE
);
28206 newtype
= tsubst (newtype
, tsubst_args
,
28207 complain
, NULL_TREE
);
28211 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28212 DECL_NAME (olddecl
), newtype
);
28213 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28216 if (TREE_CODE (olddecl
) == TYPE_DECL
28217 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28219 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28220 = build_template_parm_index (index
, level
, level
,
28222 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28223 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28224 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28225 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28226 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28228 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28230 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28232 DECL_TEMPLATE_RESULT (newdecl
)
28233 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28234 DECL_NAME (olddecl
), newtype
);
28235 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28236 // First create a copy (ttargs) of tsubst_args with an
28237 // additional level for the template template parameter's own
28238 // template parameters (ttparms).
28239 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28240 (DECL_TEMPLATE_PARMS (olddecl
)));
28241 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28242 tree ttargs
= make_tree_vec (depth
+ 1);
28243 for (int i
= 0; i
< depth
; ++i
)
28244 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28245 TREE_VEC_ELT (ttargs
, depth
)
28246 = template_parms_level_to_args (ttparms
);
28247 // Substitute ttargs into ttparms to fix references to
28248 // other template parameters.
28249 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28250 complain
|tf_partial
);
28251 // Now substitute again with args based on tparms, to reduce
28252 // the level of the ttparms.
28253 ttargs
= current_template_args ();
28254 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28256 // Finally, tack the adjusted parms onto tparms.
28257 ttparms
= tree_cons (size_int (depth
), ttparms
,
28258 current_template_parms
);
28259 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28264 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28266 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28267 TREE_CODE (oldconst
),
28268 DECL_NAME (oldconst
), newtype
);
28269 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28270 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28271 SET_DECL_TEMPLATE_PARM_P (newconst
);
28272 newidx
= build_template_parm_index (index
, level
, level
,
28273 newconst
, newtype
);
28274 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28275 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28276 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28282 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28283 template parameter. */
28286 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28287 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28289 tree olddecl
= TREE_VALUE (oldelt
);
28290 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28292 if (newdecl
== error_mark_node
)
28293 return error_mark_node
;
28294 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28295 targs
, complain
, NULL_TREE
);
28296 tree list
= build_tree_list (newdef
, newdecl
);
28297 TEMPLATE_PARM_CONSTRAINTS (list
)
28298 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28299 targs
, complain
, NULL_TREE
);
28300 int depth
= TMPL_ARGS_DEPTH (targs
);
28301 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28305 /* Returns a C++17 class deduction guide template based on the constructor
28306 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28307 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28308 aggregate initialization guide. */
28311 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28313 tree tparms
, targs
, fparms
, fargs
, ci
;
28314 bool memtmpl
= false;
28317 tree fn_tmpl
= NULL_TREE
;
28321 ++processing_template_decl
;
28322 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28323 --processing_template_decl
;
28326 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28330 bool copy_p
= TYPE_REF_P (ctor
);
28332 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28334 fparms
= void_list_node
;
28336 else if (TREE_CODE (ctor
) == TREE_LIST
)
28339 gcc_unreachable ();
28341 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28342 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28343 targs
= CLASSTYPE_TI_ARGS (type
);
28346 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28347 explicit_p
= false;
28351 ++processing_template_decl
;
28355 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28356 : DECL_TI_TEMPLATE (ctor
));
28358 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28359 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28361 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28362 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28363 fully specialized args for the enclosing class. Strip those off, as
28364 the deduction guide won't have those template parameters. */
28365 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28366 TMPL_PARMS_DEPTH (tparms
));
28367 /* Discard the 'this' parameter. */
28368 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28369 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28370 ci
= get_constraints (ctor
);
28371 loc
= DECL_SOURCE_LOCATION (ctor
);
28372 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28374 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28378 /* For a member template constructor, we need to flatten the two
28379 template parameter lists into one, and then adjust the function
28380 signature accordingly. This gets...complicated. */
28381 tree save_parms
= current_template_parms
;
28383 /* For a member template we should have two levels of parms/args, one
28384 for the class and one for the constructor. We stripped
28385 specialized args for further enclosing classes above. */
28386 const int depth
= 2;
28387 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28389 /* Template args for translating references to the two-level template
28390 parameters into references to the one-level template parameters we
28392 tree tsubst_args
= copy_node (targs
);
28393 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28394 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28396 /* Template parms for the constructor template. */
28397 tree ftparms
= TREE_VALUE (tparms
);
28398 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28399 /* Template parms for the class template. */
28400 tparms
= TREE_CHAIN (tparms
);
28401 tree ctparms
= TREE_VALUE (tparms
);
28402 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28403 /* Template parms for the deduction guide start as a copy of the
28404 template parms for the class. We set current_template_parms for
28405 lookup_template_class_1. */
28406 current_template_parms
= tparms
= copy_node (tparms
);
28407 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28408 for (unsigned i
= 0; i
< clen
; ++i
)
28409 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28411 /* Now we need to rewrite the constructor parms to append them to the
28413 for (unsigned i
= 0; i
< flen
; ++i
)
28415 unsigned index
= i
+ clen
;
28416 unsigned level
= 1;
28417 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28419 = rewrite_tparm_list (oldelt
, index
, level
,
28420 tsubst_args
, i
, complain
);
28421 if (newelt
== error_mark_node
)
28423 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28426 /* Now we have a final set of template parms to substitute into the
28427 function signature. */
28428 targs
= template_parms_to_args (tparms
);
28429 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28431 if (fparms
== error_mark_node
)
28434 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28436 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28437 cp_unevaluated_operand. */
28439 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28440 current_template_parms
= save_parms
;
28444 /* Substitute in the same arguments to rewrite class members into
28445 references to members of an unknown specialization. */
28447 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28448 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28450 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28453 --processing_template_decl
;
28455 return error_mark_node
;
28460 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28461 tparms
= copy_node (tparms
);
28462 INNERMOST_TEMPLATE_PARMS (tparms
)
28463 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28466 tree fntype
= build_function_type (type
, fparms
);
28467 tree ded_fn
= build_lang_decl_loc (loc
,
28469 dguide_name (type
), fntype
);
28470 DECL_ARGUMENTS (ded_fn
) = fargs
;
28471 DECL_ARTIFICIAL (ded_fn
) = true;
28472 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28473 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28474 DECL_ARTIFICIAL (ded_tmpl
) = true;
28475 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28476 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28478 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28480 set_constraints (ded_tmpl
, ci
);
28485 /* Add to LIST the member types for the reshaped initializer CTOR. */
28488 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28490 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28491 tree idx
, val
; unsigned i
;
28492 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28494 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28495 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28496 && CONSTRUCTOR_NELTS (val
)
28497 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28498 type gets a single initializer. */
28499 && CP_AGGREGATE_TYPE_P (ftype
)
28500 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28501 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28503 tree subelt
= NULL_TREE
;
28504 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28505 subelt
= TREE_TYPE (ftype
);
28506 list
= collect_ctor_idx_types (val
, list
, subelt
);
28509 tree arg
= NULL_TREE
;
28510 if (i
== v
->length() - 1
28511 && PACK_EXPANSION_P (ftype
))
28512 /* Give the trailing pack expansion parameter a default argument to
28513 match aggregate initialization behavior, even if we deduce the
28514 length of the pack separately to more than we have initializers. */
28515 arg
= build_constructor (init_list_type_node
, NULL
);
28516 /* if ei is of array type and xi is a braced-init-list or string literal,
28517 Ti is an rvalue reference to the declared type of ei */
28518 STRIP_ANY_LOCATION_WRAPPER (val
);
28519 if (TREE_CODE (ftype
) == ARRAY_TYPE
28520 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28521 || TREE_CODE (val
) == STRING_CST
))
28523 if (TREE_CODE (val
) == STRING_CST
)
28524 ftype
= cp_build_qualified_type
28525 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28526 ftype
= (cp_build_reference_type
28527 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28529 list
= tree_cons (arg
, ftype
, list
);
28535 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28538 is_spec_or_derived (tree etype
, tree tmpl
)
28540 if (!etype
|| !CLASS_TYPE_P (etype
))
28543 tree type
= TREE_TYPE (tmpl
);
28544 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28545 (DECL_TEMPLATE_PARMS (tmpl
)));
28546 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28547 int err
= unify (tparms
, targs
, type
, etype
,
28548 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28553 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28557 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28559 if (cxx_dialect
< cxx20
)
28562 if (init
== NULL_TREE
)
28565 tree type
= TREE_TYPE (tmpl
);
28566 if (!CP_AGGREGATE_TYPE_P (type
))
28569 /* No aggregate candidate for copy-initialization. */
28570 if (args
->length() == 1)
28572 tree val
= (*args
)[0];
28573 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28577 /* If we encounter a problem, we just won't add the candidate. */
28578 tsubst_flags_t complain
= tf_none
;
28580 tree parms
= NULL_TREE
;
28581 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28583 init
= reshape_init (type
, init
, complain
);
28584 if (init
== error_mark_node
)
28586 parms
= collect_ctor_idx_types (init
, parms
);
28588 else if (TREE_CODE (init
) == TREE_LIST
)
28590 int len
= list_length (init
);
28591 for (tree field
= TYPE_FIELDS (type
);
28593 --len
, field
= DECL_CHAIN (field
))
28595 field
= next_initializable_field (field
);
28598 tree ftype
= finish_decltype_type (field
, true, complain
);
28599 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28603 /* Aggregate initialization doesn't apply to an initializer expression. */
28609 parms
= nreverse (parms
);
28610 TREE_CHAIN (last
) = void_list_node
;
28611 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28618 /* UGUIDES are the deduction guides for the underlying template of alias
28619 template TMPL; adjust them to be deduction guides for TMPL. */
28622 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28624 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28625 class type (9.2.8.2) where the template-name names an alias template A,
28626 the defining-type-id of A must be of the form
28628 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28630 as specified in 9.2.8.2. The guides of A are the set of functions or
28631 function templates formed as follows. For each function or function
28632 template f in the guides of the template named by the simple-template-id
28633 of the defining-type-id, the template arguments of the return type of f
28634 are deduced from the defining-type-id of A according to the process in
28635 13.10.2.5 with the exception that deduction does not fail if not all
28636 template arguments are deduced. Let g denote the result of substituting
28637 these deductions into f. If substitution succeeds, form a function or
28638 function template f' with the following properties and add it to the set
28641 * The function type of f' is the function type of g.
28643 * If f is a function template, f' is a function template whose template
28644 parameter list consists of all the template parameters of A (including
28645 their default template arguments) that appear in the above deductions or
28646 (recursively) in their default template arguments, followed by the
28647 template parameters of f that were not deduced (including their default
28648 template arguments), otherwise f' is not a function template.
28650 * The associated constraints (13.5.2) are the conjunction of the
28651 associated constraints of g and a constraint that is satisfied if and only
28652 if the arguments of A are deducible (see below) from the return type.
28654 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28657 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28658 considered to be so as well.
28660 * The explicit-specifier of f' is the explicit-specifier of g (if
28663 /* This implementation differs from the above in two significant ways:
28665 1) We include all template parameters of A, not just some.
28666 2) The added constraint is same_type instead of deducible.
28668 I believe that while it's probably possible to construct a testcase that
28669 behaves differently with this simplification, it should have the same
28670 effect for real uses. Including all template parameters means that we
28671 deduce all parameters of A when resolving the call, so when we're in the
28672 constraint we don't need to deduce them again, we can just check whether
28673 the deduction produced the desired result. */
28675 tsubst_flags_t complain
= tf_warning_or_error
;
28676 tree atype
= TREE_TYPE (tmpl
);
28677 tree aguides
= NULL_TREE
;
28678 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28679 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28680 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28681 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28685 location_t loc
= DECL_SOURCE_LOCATION (f
);
28686 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28688 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28690 processing_template_decl_sentinel
ptds (/*reset*/false);
28691 ++processing_template_decl
;
28693 /* Deduce template arguments for f from the type-id of A. */
28694 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28695 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28696 tree targs
= make_tree_vec (len
);
28697 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28700 /* The number of parms for f' is the number of parms for A plus
28701 non-deduced parms of f. */
28702 unsigned ndlen
= 0;
28704 for (unsigned i
= 0; i
< len
; ++i
)
28705 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28707 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28709 /* First copy over the parms of A. */
28710 for (j
= 0; j
< natparms
; ++j
)
28711 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28712 /* Now rewrite the non-deduced parms of f. */
28713 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28714 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28717 unsigned index
= j
++;
28718 unsigned level
= 1;
28719 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28720 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28721 targs
, i
, complain
);
28722 TREE_VEC_ELT (gtparms
, index
) = list
;
28724 gtparms
= build_tree_list (size_one_node
, gtparms
);
28726 /* Substitute the deduced arguments plus the rewritten template
28727 parameters into f to get g. This covers the type, copyness,
28728 guideness, and explicit-specifier. */
28729 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28730 if (g
== error_mark_node
)
28731 return error_mark_node
;
28732 DECL_USE_TEMPLATE (g
) = 0;
28733 fprime
= build_template_decl (g
, gtparms
, false);
28734 DECL_TEMPLATE_RESULT (fprime
) = g
;
28735 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28736 tree gtargs
= template_parms_to_args (gtparms
);
28737 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28738 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28740 /* Substitute the associated constraints. */
28741 tree ci
= get_constraints (f
);
28743 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28744 if (ci
== error_mark_node
)
28745 return error_mark_node
;
28747 /* Add a constraint that the return type matches the instantiation of
28748 A with the same template arguments. */
28749 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28750 if (!same_type_p (atype
, ret
)
28751 /* FIXME this should mean they don't compare as equivalent. */
28752 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28754 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28755 ci
= append_constraint (ci
, same
);
28760 remove_constraints (fprime
);
28761 set_constraints (fprime
, ci
);
28766 /* For a non-template deduction guide, if the arguments of A aren't
28767 deducible from the return type, don't add the candidate. */
28768 tree targs
= make_tree_vec (natparms
);
28769 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28770 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28771 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28777 aguides
= lookup_add (fprime
, aguides
);
28783 /* Return artificial deduction guides built from the constructors of class
28787 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28789 tree type
= TREE_TYPE (tmpl
);
28790 tree outer_args
= NULL_TREE
;
28791 if (DECL_CLASS_SCOPE_P (tmpl
)
28792 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28794 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28795 type
= TREE_TYPE (most_general_template (tmpl
));
28798 tree cands
= NULL_TREE
;
28800 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28802 /* Skip inherited constructors. */
28803 if (iter
.using_p ())
28806 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28807 cands
= lookup_add (guide
, cands
);
28810 /* Add implicit default constructor deduction guide. */
28811 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28813 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28815 cands
= lookup_add (guide
, cands
);
28818 /* Add copy guide. */
28820 tree gtype
= build_reference_type (type
);
28821 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28823 cands
= lookup_add (guide
, cands
);
28829 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28831 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28832 aggregate candidate is added separately because it depends on the
28836 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28838 tree guides
= NULL_TREE
;
28839 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28841 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28842 tree tinfo
= get_template_info (under
);
28843 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28847 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28848 dguide_name (tmpl
),
28849 LOOK_want::NORMAL
, /*complain*/false);
28850 if (guides
== error_mark_node
)
28851 guides
= NULL_TREE
;
28854 /* Cache the deduction guides for a template. We also remember the result of
28855 lookup, and rebuild everything if it changes; should be very rare. */
28856 tree_pair_p cache
= NULL
;
28858 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28861 if (cache
->purpose
== guides
)
28862 return cache
->value
;
28866 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28867 cache
->purpose
= guides
;
28870 tree cands
= NULL_TREE
;
28871 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28872 cands
= alias_ctad_tweaks (tmpl
, guides
);
28875 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28876 for (ovl_iterator
it (guides
); it
; ++it
)
28877 cands
= lookup_add (*it
, cands
);
28880 cache
->value
= cands
;
28884 /* Return whether TMPL is a (class template argument-) deducible template. */
28887 ctad_template_p (tree tmpl
)
28889 /* A deducible template is either a class template or is an alias template
28890 whose defining-type-id is of the form
28892 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28894 where the nested-name-specifier (if any) is non-dependent and the
28895 template-name of the simple-template-id names a deducible template. */
28897 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28898 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28900 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28902 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28903 if (tree tinfo
= get_template_info (orig
))
28904 return ctad_template_p (TI_TEMPLATE (tinfo
));
28908 /* Deduce template arguments for the class template placeholder PTYPE for
28909 template TMPL based on the initializer INIT, and return the resulting
28913 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28914 int flags
, tsubst_flags_t complain
)
28916 /* We should have handled this in the caller. */
28917 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28920 /* Look through alias templates that just rename another template. */
28921 tmpl
= get_underlying_template (tmpl
);
28922 if (!ctad_template_p (tmpl
))
28924 if (complain
& tf_error
)
28925 error ("non-deducible template %qT used without template arguments", tmpl
);
28926 return error_mark_node
;
28928 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28930 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28931 if (complain
& tf_warning_or_error
)
28932 pedwarn (input_location
, 0, "alias template deduction only available "
28933 "with %<-std=c++20%> or %<-std=gnu++20%>");
28936 if (init
&& TREE_TYPE (init
) == ptype
)
28937 /* Using the template parm as its own argument. */
28940 tree type
= TREE_TYPE (tmpl
);
28942 bool try_list_ctor
= false;
28944 releasing_vec rv_args
= NULL
;
28945 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28946 if (init
== NULL_TREE
)
28947 args
= make_tree_vector ();
28948 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28950 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28951 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28953 /* As an exception, the first phase in 16.3.1.7 (considering the
28954 initializer list as a single argument) is omitted if the
28955 initializer list consists of a single expression of type cv U,
28956 where U is a specialization of C or a class derived from a
28957 specialization of C. */
28958 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28959 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28960 try_list_ctor
= false;
28962 if (try_list_ctor
|| is_std_init_list (type
))
28963 args
= make_tree_vector_single (init
);
28965 args
= make_tree_vector_from_ctor (init
);
28967 else if (TREE_CODE (init
) == TREE_LIST
)
28968 args
= make_tree_vector_from_list (init
);
28970 args
= make_tree_vector_single (init
);
28972 /* Do this now to avoid problems with erroneous args later on. */
28973 args
= resolve_args (args
, complain
);
28975 return error_mark_node
;
28977 tree cands
= deduction_guides_for (tmpl
, complain
);
28978 if (cands
== error_mark_node
)
28979 return error_mark_node
;
28981 /* Prune explicit deduction guides in copy-initialization context. */
28982 bool elided
= false;
28983 if (flags
& LOOKUP_ONLYCONVERTING
)
28985 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28986 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28991 /* Found a nonconverting guide, prune the candidates. */
28992 tree pruned
= NULL_TREE
;
28993 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28994 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28995 pruned
= lookup_add (*iter
, pruned
);
29001 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29002 cands
= lookup_add (guide
, cands
);
29004 tree call
= error_mark_node
;
29006 /* If this is list-initialization and the class has a list constructor, first
29007 try deducing from the list as a single argument, as [over.match.list]. */
29008 tree list_cands
= NULL_TREE
;
29009 if (try_list_ctor
&& cands
)
29010 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29013 if (is_list_ctor (dg
))
29014 list_cands
= lookup_add (dg
, list_cands
);
29018 ++cp_unevaluated_operand
;
29019 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29020 --cp_unevaluated_operand
;
29022 if (call
== error_mark_node
)
29024 /* That didn't work, now try treating the list as a sequence of
29026 release_tree_vector (args
);
29027 args
= make_tree_vector_from_ctor (init
);
29031 if (elided
&& !cands
)
29033 error ("cannot deduce template arguments for copy-initialization"
29034 " of %qT, as it has no non-explicit deduction guides or "
29035 "user-declared constructors", type
);
29036 return error_mark_node
;
29038 else if (!cands
&& call
== error_mark_node
)
29040 error ("cannot deduce template arguments of %qT, as it has no viable "
29041 "deduction guides", type
);
29042 return error_mark_node
;
29045 if (call
== error_mark_node
)
29047 ++cp_unevaluated_operand
;
29048 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29049 --cp_unevaluated_operand
;
29052 if (call
== error_mark_node
29053 && (complain
& tf_warning_or_error
))
29055 error ("class template argument deduction failed:");
29057 ++cp_unevaluated_operand
;
29058 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
29059 --cp_unevaluated_operand
;
29062 inform (input_location
, "explicit deduction guides not considered "
29063 "for copy-initialization");
29066 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29069 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29070 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29071 The CONTEXT determines the context in which auto deduction is performed
29072 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29073 OUTER_TARGS are used during template argument deduction
29074 (context == adc_unify) to properly substitute the result, and is ignored
29077 For partial-concept-ids, extra args may be appended to the list of deduced
29078 template arguments prior to determining constraint satisfaction. */
29081 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29082 tsubst_flags_t complain
, auto_deduction_context context
,
29083 tree outer_targs
, int flags
)
29087 if (init
== error_mark_node
)
29088 return error_mark_node
;
29090 if (init
&& type_dependent_expression_p (init
)
29091 && context
!= adc_unify
)
29092 /* Defining a subset of type-dependent expressions that we can deduce
29093 from ahead of time isn't worth the trouble. */
29096 /* Similarly, we can't deduce from another undeduced decl. */
29097 if (init
&& undeduced_auto_decl (init
))
29100 /* We may be doing a partial substitution, but we still want to replace
29102 complain
&= ~tf_partial
;
29104 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29105 /* C++17 class template argument deduction. */
29106 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29108 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29109 /* Nothing we can do with this, even in deduction context. */
29112 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29113 with either a new invented type template parameter U or, if the
29114 initializer is a braced-init-list (8.5.4), with
29115 std::initializer_list<U>. */
29116 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29118 if (!DIRECT_LIST_INIT_P (init
))
29119 type
= listify_autos (type
, auto_node
);
29120 else if (CONSTRUCTOR_NELTS (init
) == 1)
29121 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29124 if (complain
& tf_warning_or_error
)
29126 if (permerror (input_location
, "direct-list-initialization of "
29127 "%<auto%> requires exactly one element"))
29128 inform (input_location
,
29129 "for deduction to %<std::initializer_list%>, use copy-"
29130 "list-initialization (i.e. add %<=%> before the %<{%>)");
29132 type
= listify_autos (type
, auto_node
);
29136 if (type
== error_mark_node
)
29137 return error_mark_node
;
29139 init
= resolve_nondeduced_context (init
, complain
);
29141 if (context
== adc_decomp_type
29142 && auto_node
== type
29143 && init
!= error_mark_node
29144 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29145 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29146 and initializer has array type, deduce cv-qualified array type. */
29147 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29149 else if (AUTO_IS_DECLTYPE (auto_node
))
29151 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29152 bool id
= (DECL_P (stripped_init
)
29153 || ((TREE_CODE (init
) == COMPONENT_REF
29154 || TREE_CODE (init
) == SCOPE_REF
)
29155 && !REF_PARENTHESIZED_P (init
)));
29156 targs
= make_tree_vec (1);
29157 TREE_VEC_ELT (targs
, 0)
29158 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29159 if (type
!= auto_node
)
29161 if (complain
& tf_error
)
29162 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29163 return error_mark_node
;
29165 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29167 if (complain
& tf_error
)
29168 error ("%<decltype(auto)%> cannot be cv-qualified");
29169 return error_mark_node
;
29174 if (error_operand_p (init
))
29175 return error_mark_node
;
29177 tree parms
= build_tree_list (NULL_TREE
, type
);
29181 tparms
= extract_autos (type
);
29184 tparms
= make_tree_vec (1);
29185 TREE_VEC_ELT (tparms
, 0)
29186 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29189 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29190 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29192 NULL
, /*explain_p=*/false);
29195 if (processing_template_decl
)
29196 /* Try again at instantiation time. */
29198 if (type
&& type
!= error_mark_node
29199 && (complain
& tf_error
))
29200 /* If type is error_mark_node a diagnostic must have been
29201 emitted by now. Also, having a mention to '<type error>'
29202 in the diagnostic is not really useful to the user. */
29205 && FNDECL_USED_AUTO (current_function_decl
)
29207 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29208 && LAMBDA_FUNCTION_P (current_function_decl
))
29209 error ("unable to deduce lambda return type from %qE", init
);
29211 error ("unable to deduce %qT from %qE", type
, init
);
29212 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29214 NULL
, /*explain_p=*/true);
29216 return error_mark_node
;
29220 /* Check any placeholder constraints against the deduced type. */
29221 if (flag_concepts
&& !processing_template_decl
)
29222 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29224 /* Use the deduced type to check the associated constraints. If we
29225 have a partial-concept-id, rebuild the argument list so that
29226 we check using the extra arguments. */
29227 check
= unpack_concept_check (check
);
29228 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29229 tree
cdecl = TREE_OPERAND (check
, 0);
29231 cdecl = OVL_FIRST (cdecl);
29232 tree cargs
= TREE_OPERAND (check
, 1);
29233 if (TREE_VEC_LENGTH (cargs
) > 1)
29235 cargs
= copy_node (cargs
);
29236 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29241 /* Rebuild the check using the deduced arguments. */
29242 check
= build_concept_check (cdecl, cargs
, tf_none
);
29244 if (!constraints_satisfied_p (check
))
29246 if (complain
& tf_warning_or_error
)
29248 auto_diagnostic_group d
;
29251 case adc_unspecified
:
29253 error("placeholder constraints not satisfied");
29255 case adc_variable_type
:
29256 case adc_decomp_type
:
29257 error ("deduced initializer does not satisfy "
29258 "placeholder constraints");
29260 case adc_return_type
:
29261 error ("deduced return type does not satisfy "
29262 "placeholder constraints");
29264 case adc_requirement
:
29265 error ("deduced expression type does not satisfy "
29266 "placeholder constraints");
29269 diagnose_constraints (input_location
, check
, targs
);
29271 return error_mark_node
;
29275 if (processing_template_decl
&& context
!= adc_unify
)
29276 outer_targs
= current_template_args ();
29277 targs
= add_to_template_args (outer_targs
, targs
);
29278 return tsubst (type
, targs
, complain
, NULL_TREE
);
29281 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29285 splice_late_return_type (tree type
, tree late_return_type
)
29287 if (late_return_type
)
29289 gcc_assert (is_auto (type
) || seen_error ());
29290 return late_return_type
;
29293 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29295 tree idx
= get_template_parm_index (*auto_node
);
29296 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29298 /* In an abbreviated function template we didn't know we were dealing
29299 with a function template when we saw the auto return type, so update
29300 it to have the correct level. */
29301 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29302 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29303 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29304 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29305 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29306 *auto_node
= new_auto
;
29312 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29313 'decltype(auto)' or a deduced class template. */
29316 is_auto (const_tree type
)
29318 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29319 && (TYPE_IDENTIFIER (type
) == auto_identifier
29320 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29326 /* for_each_template_parm callback for type_uses_auto. */
29329 is_auto_r (tree tp
, void */
*data*/
)
29331 return is_auto (tp
);
29334 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29335 a use of `auto'. Returns NULL_TREE otherwise. */
29338 type_uses_auto (tree type
)
29340 if (type
== NULL_TREE
)
29342 else if (flag_concepts
)
29344 /* The Concepts TS allows multiple autos in one type-specifier; just
29345 return the first one we find, do_auto_deduction will collect all of
29347 if (uses_template_parms (type
))
29348 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29349 /*visited*/NULL
, /*nondeduced*/false);
29353 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29359 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29360 concepts are enabled, auto is acceptable in template arguments, but
29361 only when TEMPL identifies a template class. Return TRUE if any
29362 such errors were reported. */
29365 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29367 /* If there were previous errors, nevermind. */
29368 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29371 /* If TMPL is an identifier, we're parsing and we can't tell yet
29372 whether TMPL is supposed to be a type, a function or a variable.
29373 We'll only be able to tell during template substitution, so we
29374 expect to be called again then. If concepts are enabled and we
29375 know we have a type, we're ok. */
29377 && (identifier_p (tmpl
)
29379 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29383 /* Quickly search for any occurrences of auto; usually there won't
29384 be any, and then we'll avoid allocating the vector. */
29385 if (!type_uses_auto (args
))
29388 bool errors
= false;
29390 tree vec
= extract_autos (args
);
29391 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29393 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29394 error_at (DECL_SOURCE_LOCATION (xauto
),
29395 "invalid use of %qT in template argument", xauto
);
29402 /* Recursively walk over && expressions searching for EXPR. Return a reference
29403 to that expression. */
29405 static tree
*find_template_requirement (tree
*t
, tree key
)
29409 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29411 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29413 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29419 /* Convert the generic type parameters in PARM that match the types given in the
29420 range [START_IDX, END_IDX) from the current_template_parms into generic type
29424 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29426 tree current
= current_template_parms
;
29427 int depth
= TMPL_PARMS_DEPTH (current
);
29428 current
= INNERMOST_TEMPLATE_PARMS (current
);
29429 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29431 for (int i
= 0; i
< start_idx
; ++i
)
29432 TREE_VEC_ELT (replacement
, i
)
29433 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29435 for (int i
= start_idx
; i
< end_idx
; ++i
)
29437 /* Create a distinct parameter pack type from the current parm and add it
29438 to the replacement args to tsubst below into the generic function
29440 tree node
= TREE_VEC_ELT (current
, i
);
29441 tree o
= TREE_TYPE (TREE_VALUE (node
));
29442 tree t
= copy_type (o
);
29443 TEMPLATE_TYPE_PARM_INDEX (t
)
29444 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29446 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29447 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29448 TYPE_MAIN_VARIANT (t
) = t
;
29449 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29450 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29451 TREE_VEC_ELT (replacement
, i
) = t
;
29453 /* Replace the current template parameter with new pack. */
29454 TREE_VALUE (node
) = TREE_CHAIN (t
);
29456 /* Surgically adjust the associated constraint of adjusted parameter
29457 and it's corresponding contribution to the current template
29459 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29461 tree id
= unpack_concept_check (constr
);
29462 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
29463 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29464 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29466 /* If there was a constraint, we also need to replace that in
29467 the template requirements, which we've already built. */
29468 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29469 reqs
= find_template_requirement (reqs
, constr
);
29474 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29475 TREE_VEC_ELT (replacement
, i
)
29476 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29478 /* If there are more levels then build up the replacement with the outer
29481 replacement
= add_to_template_args (template_parms_to_args
29482 (TREE_CHAIN (current_template_parms
)),
29485 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29488 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29492 declare_integer_pack (void)
29494 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29495 build_function_type_list (integer_type_node
,
29498 NULL_TREE
, ECF_CONST
);
29499 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29500 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29501 CP_BUILT_IN_INTEGER_PACK
);
29504 /* Set up the hash tables for template instantiations. */
29507 init_template_processing (void)
29509 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29510 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29512 if (cxx_dialect
>= cxx11
)
29513 declare_integer_pack ();
29516 /* Print stats about the template hash tables for -fstats. */
29519 print_template_statistics (void)
29521 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29522 "%f collisions\n", (long) decl_specializations
->size (),
29523 (long) decl_specializations
->elements (),
29524 decl_specializations
->collisions ());
29525 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29526 "%f collisions\n", (long) type_specializations
->size (),
29527 (long) type_specializations
->elements (),
29528 type_specializations
->collisions ());
29533 namespace selftest
{
29535 /* Verify that build_non_dependent_expr () works, for various expressions,
29536 and that location wrappers don't affect the results. */
29539 test_build_non_dependent_expr ()
29541 location_t loc
= BUILTINS_LOCATION
;
29543 /* Verify constants, without and with location wrappers. */
29544 tree int_cst
= build_int_cst (integer_type_node
, 42);
29545 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29547 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29548 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29549 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29551 tree string_lit
= build_string (4, "foo");
29552 TREE_TYPE (string_lit
) = char_array_type_node
;
29553 string_lit
= fix_string_type (string_lit
);
29554 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29556 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29557 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29558 ASSERT_EQ (wrapped_string_lit
,
29559 build_non_dependent_expr (wrapped_string_lit
));
29562 /* Verify that type_dependent_expression_p () works correctly, even
29563 in the presence of location wrapper nodes. */
29566 test_type_dependent_expression_p ()
29568 location_t loc
= BUILTINS_LOCATION
;
29570 tree name
= get_identifier ("foo");
29572 /* If no templates are involved, nothing is type-dependent. */
29573 gcc_assert (!processing_template_decl
);
29574 ASSERT_FALSE (type_dependent_expression_p (name
));
29576 ++processing_template_decl
;
29578 /* Within a template, an unresolved name is always type-dependent. */
29579 ASSERT_TRUE (type_dependent_expression_p (name
));
29581 /* Ensure it copes with NULL_TREE and errors. */
29582 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29583 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29585 /* A USING_DECL in a template should be type-dependent, even if wrapped
29586 with a location wrapper (PR c++/83799). */
29587 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29588 TREE_TYPE (using_decl
) = integer_type_node
;
29589 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29590 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29591 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29592 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29594 --processing_template_decl
;
29597 /* Run all of the selftests within this file. */
29602 test_build_non_dependent_expr ();
29603 test_type_dependent_expression_p ();
29606 } // namespace selftest
29608 #endif /* #if CHECKING_P */
29610 #include "gt-cp-pt.h"