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
, /*hiding=*/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
, /*hiding=*/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
, /*hiding=*/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
);
2991 bool found_hidden
= false;
2993 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2994 if (!was_template_id
)
2998 gcc_assert (identifier_p (declarator
));
3003 /* If there is no class context, the explicit instantiation
3004 must be at namespace scope. */
3005 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3007 /* Find the namespace binding, using the declaration
3009 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3010 LOOK_want::NORMAL
, true);
3011 if (fns
== error_mark_node
)
3013 /* If lookup fails, look for a friend declaration so we can
3014 give a better diagnostic. */
3015 fns
= (lookup_qualified_name
3016 (CP_DECL_CONTEXT (decl
), dname
,
3017 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3019 found_hidden
= true;
3022 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3024 error ("%qD is not a template function", dname
);
3025 fns
= error_mark_node
;
3029 declarator
= lookup_template_function (fns
, NULL_TREE
);
3032 if (declarator
== error_mark_node
)
3033 return error_mark_node
;
3035 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3037 if (!explicit_instantiation
)
3038 /* A specialization in class scope. This is invalid,
3039 but the error will already have been flagged by
3040 check_specialization_scope. */
3041 return error_mark_node
;
3044 /* It's not valid to write an explicit instantiation in
3047 class C { template void f(); }
3049 This case is caught by the parser. However, on
3052 template class C { void f(); };
3054 (which is invalid) we can get here. The error will be
3061 else if (ctype
!= NULL_TREE
3062 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3064 // We'll match variable templates in start_decl.
3068 /* Find the list of functions in ctype that have the same
3069 name as the declared function. */
3070 tree name
= TREE_OPERAND (declarator
, 0);
3072 if (constructor_name_p (name
, ctype
))
3074 if (DECL_CONSTRUCTOR_P (decl
)
3075 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3076 : !CLASSTYPE_DESTRUCTOR (ctype
))
3078 /* From [temp.expl.spec]:
3080 If such an explicit specialization for the member
3081 of a class template names an implicitly-declared
3082 special member function (clause _special_), the
3083 program is ill-formed.
3085 Similar language is found in [temp.explicit]. */
3086 error ("specialization of implicitly-declared special member function");
3087 return error_mark_node
;
3090 name
= DECL_NAME (decl
);
3093 /* For a type-conversion operator, We might be looking for
3094 `operator int' which will be a specialization of
3095 `operator T'. Grab all the conversion operators, and
3096 then select from them. */
3097 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3098 ? conv_op_identifier
: name
);
3100 if (fns
== NULL_TREE
)
3102 error ("no member function %qD declared in %qT", name
, ctype
);
3103 return error_mark_node
;
3106 TREE_OPERAND (declarator
, 0) = fns
;
3109 /* Figure out what exactly is being specialized at this point.
3110 Note that for an explicit instantiation, even one for a
3111 member function, we cannot tell a priori whether the
3112 instantiation is for a member template, or just a member
3113 function of a template class. Even if a member template is
3114 being instantiated, the member template arguments may be
3115 elided if they can be deduced from the rest of the
3117 tmpl
= determine_specialization (declarator
, decl
,
3119 member_specialization
,
3123 if (!tmpl
|| tmpl
== error_mark_node
)
3124 /* We couldn't figure out what this declaration was
3126 return error_mark_node
;
3129 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3131 auto_diagnostic_group d
;
3132 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3133 "friend declaration %qD is not visible to "
3134 "explicit specialization", tmpl
))
3135 inform (DECL_SOURCE_LOCATION (tmpl
),
3136 "friend declaration here");
3139 if (!ctype
&& !is_friend
3140 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3141 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3143 tree gen_tmpl
= most_general_template (tmpl
);
3145 if (explicit_instantiation
)
3147 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3148 is done by do_decl_instantiation later. */
3150 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3151 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3153 if (arg_depth
> parm_depth
)
3155 /* If TMPL is not the most general template (for
3156 example, if TMPL is a friend template that is
3157 injected into namespace scope), then there will
3158 be too many levels of TARGS. Remove some of them
3163 new_targs
= make_tree_vec (parm_depth
);
3164 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3165 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3166 = TREE_VEC_ELT (targs
, i
);
3170 return instantiate_template (tmpl
, targs
, tf_error
);
3173 /* If we thought that the DECL was a member function, but it
3174 turns out to be specializing a static member function,
3175 make DECL a static member function as well. */
3176 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3177 && DECL_STATIC_FUNCTION_P (tmpl
)
3178 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3179 revert_static_member_fn (decl
);
3181 /* If this is a specialization of a member template of a
3182 template class, we want to return the TEMPLATE_DECL, not
3183 the specialization of it. */
3184 if (tsk
== tsk_template
&& !was_template_id
)
3186 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3187 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3188 DECL_INITIAL (result
) = NULL_TREE
;
3192 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3193 DECL_SOURCE_LOCATION (result
)
3194 = DECL_SOURCE_LOCATION (decl
);
3195 /* We want to use the argument list specified in the
3196 definition, not in the original declaration. */
3197 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3198 for (parm
= DECL_ARGUMENTS (result
); parm
;
3199 parm
= DECL_CHAIN (parm
))
3200 DECL_CONTEXT (parm
) = result
;
3202 return register_specialization (tmpl
, gen_tmpl
, targs
,
3206 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3207 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3209 if (was_template_id
)
3210 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3212 /* Inherit default function arguments from the template
3213 DECL is specializing. */
3214 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3215 copy_default_args_to_explicit_spec (decl
);
3217 /* This specialization has the same protection as the
3218 template it specializes. */
3219 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3220 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3222 /* 7.1.1-1 [dcl.stc]
3224 A storage-class-specifier shall not be specified in an
3225 explicit specialization...
3227 The parser rejects these, so unless action is taken here,
3228 explicit function specializations will always appear with
3231 The action recommended by the C++ CWG in response to C++
3232 defect report 605 is to make the storage class and linkage
3233 of the explicit specialization match the templated function:
3235 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3237 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3239 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3240 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3242 /* A concept cannot be specialized. */
3243 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3245 error ("explicit specialization of function concept %qD",
3247 return error_mark_node
;
3250 /* This specialization has the same linkage and visibility as
3251 the function template it specializes. */
3252 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3253 if (! TREE_PUBLIC (decl
))
3255 DECL_INTERFACE_KNOWN (decl
) = 1;
3256 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3258 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3259 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3261 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3262 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3266 /* If DECL is a friend declaration, declared using an
3267 unqualified name, the namespace associated with DECL may
3268 have been set incorrectly. For example, in:
3270 template <typename T> void f(T);
3272 struct S { friend void f<int>(int); }
3275 we will have set the DECL_CONTEXT for the friend
3276 declaration to N, rather than to the global namespace. */
3277 if (DECL_NAMESPACE_SCOPE_P (decl
))
3278 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3280 if (is_friend
&& !have_def
)
3281 /* This is not really a declaration of a specialization.
3282 It's just the name of an instantiation. But, it's not
3283 a request for an instantiation, either. */
3284 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3285 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3286 /* A specialization is not necessarily COMDAT. */
3287 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3288 && DECL_DECLARED_INLINE_P (decl
));
3289 else if (VAR_P (decl
))
3290 DECL_COMDAT (decl
) = false;
3292 /* If this is a full specialization, register it so that we can find
3293 it again. Partial specializations will be registered in
3294 process_partial_specialization. */
3295 if (!processing_template_decl
)
3297 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3299 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3304 /* A 'structor should already have clones. */
3305 gcc_assert (decl
== error_mark_node
3306 || variable_template_p (tmpl
)
3307 || !(DECL_CONSTRUCTOR_P (decl
)
3308 || DECL_DESTRUCTOR_P (decl
))
3309 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3316 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3317 parameters. These are represented in the same format used for
3318 DECL_TEMPLATE_PARMS. */
3321 comp_template_parms (const_tree parms1
, const_tree parms2
)
3326 if (parms1
== parms2
)
3329 for (p1
= parms1
, p2
= parms2
;
3330 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3331 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3333 tree t1
= TREE_VALUE (p1
);
3334 tree t2
= TREE_VALUE (p2
);
3337 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3338 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3340 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3343 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3345 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3346 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3348 /* If either of the template parameters are invalid, assume
3349 they match for the sake of error recovery. */
3350 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3353 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3356 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3357 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3358 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3360 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3365 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3366 /* One set of parameters has more parameters lists than the
3373 /* Returns true if two template parameters are declared with
3374 equivalent constraints. */
3377 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3379 tree req1
= TREE_TYPE (parm1
);
3380 tree req2
= TREE_TYPE (parm2
);
3384 return cp_tree_equal (req1
, req2
);
3388 /* Returns true when two template parameters are equivalent. */
3391 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3393 tree decl1
= TREE_VALUE (parm1
);
3394 tree decl2
= TREE_VALUE (parm2
);
3396 /* If either of the template parameters are invalid, assume
3397 they match for the sake of error recovery. */
3398 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3401 /* ... they declare parameters of the same kind. */
3402 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3405 /* ... one parameter was introduced by a parameter declaration, then
3406 both are. This case arises as a result of eagerly rewriting declarations
3408 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3411 /* ... if either declares a pack, they both do. */
3412 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3415 if (TREE_CODE (decl1
) == PARM_DECL
)
3417 /* ... if they declare non-type parameters, the types are equivalent. */
3418 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3421 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3423 /* ... if they declare template template parameters, their template
3424 parameter lists are equivalent. */
3425 if (!template_heads_equivalent_p (decl1
, decl2
))
3429 /* ... if they are declared with a qualified-concept name, they both
3430 are, and those names are equivalent. */
3431 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3434 /* Returns true if two template parameters lists are equivalent.
3435 Two template parameter lists are equivalent if they have the
3436 same length and their corresponding parameters are equivalent.
3438 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3439 data structure returned by DECL_TEMPLATE_PARMS.
3441 This is generally the same implementation as comp_template_parms
3442 except that it also the concept names and arguments used to
3443 introduce parameters. */
3446 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3448 if (parms1
== parms2
)
3451 const_tree p1
= parms1
;
3452 const_tree p2
= parms2
;
3453 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3455 tree list1
= TREE_VALUE (p1
);
3456 tree list2
= TREE_VALUE (p2
);
3458 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3461 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3463 tree parm1
= TREE_VEC_ELT (list1
, i
);
3464 tree parm2
= TREE_VEC_ELT (list2
, i
);
3465 if (!template_parameters_equivalent_p (parm1
, parm2
))
3469 p1
= TREE_CHAIN (p1
);
3470 p2
= TREE_CHAIN (p2
);
3473 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3479 /* Return true if the requires-clause of the template parameter lists are
3480 equivalent and false otherwise. */
3482 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3484 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3485 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3486 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3488 if (!cp_tree_equal (req1
, req2
))
3493 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3494 Two template heads are equivalent if their template parameter
3495 lists are equivalent and their requires clauses are equivalent.
3497 In pre-C++20, this is equivalent to calling comp_template_parms
3498 for the template parameters of TMPL1 and TMPL2. */
3501 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3503 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3504 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3506 /* Don't change the matching rules for pre-C++20. */
3507 if (cxx_dialect
< cxx20
)
3508 return comp_template_parms (parms1
, parms2
);
3510 /* ... have the same number of template parameters, and their
3511 corresponding parameters are equivalent. */
3512 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3515 /* ... if either has a requires-clause, they both do and their
3516 corresponding constraint-expressions are equivalent. */
3517 return template_requirements_equivalent_p (parms1
, parms2
);
3520 /* Determine whether PARM is a parameter pack. */
3523 template_parameter_pack_p (const_tree parm
)
3525 /* Determine if we have a non-type template parameter pack. */
3526 if (TREE_CODE (parm
) == PARM_DECL
)
3527 return (DECL_TEMPLATE_PARM_P (parm
)
3528 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3529 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3530 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3532 /* If this is a list of template parameters, we could get a
3533 TYPE_DECL or a TEMPLATE_DECL. */
3534 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3535 parm
= TREE_TYPE (parm
);
3537 /* Otherwise it must be a type template parameter. */
3538 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3539 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3540 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3543 /* Determine if T is a function parameter pack. */
3546 function_parameter_pack_p (const_tree t
)
3548 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3549 return DECL_PACK_P (t
);
3553 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3554 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3557 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3559 if (! primary_func_tmpl_inst
3560 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3561 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3564 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3567 /* Return true iff the function parameter PARAM_DECL was expanded
3568 from the function parameter pack PACK. */
3571 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3573 if (DECL_ARTIFICIAL (param_decl
)
3574 || !function_parameter_pack_p (pack
))
3577 /* The parameter pack and its pack arguments have the same
3579 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3582 /* Determine whether ARGS describes a variadic template args list,
3583 i.e., one that is terminated by a template argument pack. */
3586 template_args_variadic_p (tree args
)
3591 if (args
== NULL_TREE
)
3594 args
= INNERMOST_TEMPLATE_ARGS (args
);
3595 nargs
= TREE_VEC_LENGTH (args
);
3600 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3602 return ARGUMENT_PACK_P (last_parm
);
3605 /* Generate a new name for the parameter pack name NAME (an
3606 IDENTIFIER_NODE) that incorporates its */
3609 make_ith_pack_parameter_name (tree name
, int i
)
3611 /* Munge the name to include the parameter index. */
3612 #define NUMBUF_LEN 128
3613 char numbuf
[NUMBUF_LEN
];
3617 if (name
== NULL_TREE
)
3619 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3620 newname_len
= IDENTIFIER_LENGTH (name
)
3621 + strlen (numbuf
) + 2;
3622 newname
= (char*)alloca (newname_len
);
3623 snprintf (newname
, newname_len
,
3624 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3625 return get_identifier (newname
);
3628 /* Return true if T is a primary function, class or alias template
3629 specialization, not including the template pattern. */
3632 primary_template_specialization_p (const_tree t
)
3637 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3638 return (DECL_LANG_SPECIFIC (t
)
3639 && DECL_USE_TEMPLATE (t
)
3640 && DECL_TEMPLATE_INFO (t
)
3641 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3642 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3643 return (CLASSTYPE_TEMPLATE_INFO (t
)
3644 && CLASSTYPE_USE_TEMPLATE (t
)
3645 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3646 else if (alias_template_specialization_p (t
, nt_transparent
))
3651 /* Return true if PARM is a template template parameter. */
3654 template_template_parameter_p (const_tree parm
)
3656 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3659 /* Return true iff PARM is a DECL representing a type template
3663 template_type_parameter_p (const_tree parm
)
3666 && (TREE_CODE (parm
) == TYPE_DECL
3667 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3668 && DECL_TEMPLATE_PARM_P (parm
));
3671 /* Return the template parameters of T if T is a
3672 primary template instantiation, NULL otherwise. */
3675 get_primary_template_innermost_parameters (const_tree t
)
3677 tree parms
= NULL
, template_info
= NULL
;
3679 if ((template_info
= get_template_info (t
))
3680 && primary_template_specialization_p (t
))
3681 parms
= INNERMOST_TEMPLATE_PARMS
3682 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3687 /* Return the template parameters of the LEVELth level from the full list
3688 of template parameters PARMS. */
3691 get_template_parms_at_level (tree parms
, int level
)
3695 || TREE_CODE (parms
) != TREE_LIST
3696 || level
> TMPL_PARMS_DEPTH (parms
))
3699 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3700 if (TMPL_PARMS_DEPTH (p
) == level
)
3706 /* Returns the template arguments of T if T is a template instantiation,
3710 get_template_innermost_arguments (const_tree t
)
3712 tree args
= NULL
, template_info
= NULL
;
3714 if ((template_info
= get_template_info (t
))
3715 && TI_ARGS (template_info
))
3716 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3721 /* Return the argument pack elements of T if T is a template argument pack,
3725 get_template_argument_pack_elems (const_tree t
)
3727 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3728 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3731 return ARGUMENT_PACK_ARGS (t
);
3734 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3735 ARGUMENT_PACK_SELECT represents. */
3738 argument_pack_select_arg (tree t
)
3740 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3741 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3743 /* If the selected argument is an expansion E, that most likely means we were
3744 called from gen_elem_of_pack_expansion_instantiation during the
3745 substituting of an argument pack (of which the Ith element is a pack
3746 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3747 In this case, the Ith element resulting from this substituting is going to
3748 be a pack expansion, which pattern is the pattern of E. Let's return the
3749 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3750 resulting pack expansion from it. */
3751 if (PACK_EXPANSION_P (arg
))
3753 /* Make sure we aren't throwing away arg info. */
3754 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3755 arg
= PACK_EXPANSION_PATTERN (arg
);
3762 /* True iff FN is a function representing a built-in variadic parameter
3766 builtin_pack_fn_p (tree fn
)
3769 || TREE_CODE (fn
) != FUNCTION_DECL
3770 || !DECL_IS_BUILTIN (fn
))
3773 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3779 /* True iff CALL is a call to a function representing a built-in variadic
3783 builtin_pack_call_p (tree call
)
3785 if (TREE_CODE (call
) != CALL_EXPR
)
3787 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3790 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3793 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3796 tree ohi
= CALL_EXPR_ARG (call
, 0);
3797 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3798 false/*fn*/, true/*int_cst*/);
3800 if (value_dependent_expression_p (hi
))
3804 call
= copy_node (call
);
3805 CALL_EXPR_ARG (call
, 0) = hi
;
3807 tree ex
= make_pack_expansion (call
, complain
);
3808 tree vec
= make_tree_vec (1);
3809 TREE_VEC_ELT (vec
, 0) = ex
;
3814 hi
= cxx_constant_value (hi
);
3815 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3817 /* Calculate the largest value of len that won't make the size of the vec
3818 overflow an int. The compiler will exceed resource limits long before
3819 this, but it seems a decent place to diagnose. */
3820 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3822 if (len
< 0 || len
> max
)
3824 if ((complain
& tf_error
)
3825 && hi
!= error_mark_node
)
3826 error ("argument to %<__integer_pack%> must be between 0 and %d",
3828 return error_mark_node
;
3831 tree vec
= make_tree_vec (len
);
3833 for (int i
= 0; i
< len
; ++i
)
3834 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3840 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3844 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3847 if (!builtin_pack_call_p (call
))
3850 tree fn
= CALL_EXPR_FN (call
);
3852 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3853 return expand_integer_pack (call
, args
, complain
, in_decl
);
3858 /* Structure used to track the progress of find_parameter_packs_r. */
3859 struct find_parameter_pack_data
3861 /* TREE_LIST that will contain all of the parameter packs found by
3863 tree
* parameter_packs
;
3865 /* Set of AST nodes that have been visited by the traversal. */
3866 hash_set
<tree
> *visited
;
3868 /* True iff we're making a type pack expansion. */
3869 bool type_pack_expansion_p
;
3872 /* Identifies all of the argument packs that occur in a template
3873 argument and appends them to the TREE_LIST inside DATA, which is a
3874 find_parameter_pack_data structure. This is a subroutine of
3875 make_pack_expansion and uses_parameter_packs. */
3877 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3880 struct find_parameter_pack_data
* ppd
=
3881 (struct find_parameter_pack_data
*)data
;
3882 bool parameter_pack_p
= false;
3884 /* Don't look through typedefs; we are interested in whether a
3885 parameter pack is actually written in the expression/type we're
3886 looking at, not the target type. */
3887 if (TYPE_P (t
) && typedef_variant_p (t
))
3889 /* But do look at arguments for an alias template. */
3890 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3891 cp_walk_tree (&TI_ARGS (tinfo
),
3892 &find_parameter_packs_r
,
3898 /* Identify whether this is a parameter pack or not. */
3899 switch (TREE_CODE (t
))
3901 case TEMPLATE_PARM_INDEX
:
3902 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3903 parameter_pack_p
= true;
3906 case TEMPLATE_TYPE_PARM
:
3907 t
= TYPE_MAIN_VARIANT (t
);
3909 case TEMPLATE_TEMPLATE_PARM
:
3910 /* If the placeholder appears in the decl-specifier-seq of a function
3911 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3912 is a pack expansion, the invented template parameter is a template
3914 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3915 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3916 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3917 parameter_pack_p
= true;
3922 if (DECL_PACK_P (t
))
3924 /* We don't want to walk into the type of a PARM_DECL,
3925 because we don't want to see the type parameter pack. */
3927 parameter_pack_p
= true;
3932 if (DECL_PACK_P (t
))
3934 /* We don't want to walk into the type of a variadic capture proxy,
3935 because we don't want to see the type parameter pack. */
3937 parameter_pack_p
= true;
3939 else if (variable_template_specialization_p (t
))
3941 cp_walk_tree (&DECL_TI_ARGS (t
),
3942 find_parameter_packs_r
,
3949 if (builtin_pack_call_p (t
))
3950 parameter_pack_p
= true;
3954 parameter_pack_p
= true;
3957 /* Not a parameter pack. */
3961 if (parameter_pack_p
)
3963 /* Add this parameter pack to the list. */
3964 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3968 cp_walk_tree (&TYPE_CONTEXT (t
),
3969 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3971 /* This switch statement will return immediately if we don't find a
3972 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3973 switch (TREE_CODE (t
))
3975 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3976 /* Check the template itself. */
3977 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3978 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3983 tree decl
= DECL_EXPR_DECL (t
);
3984 /* Ignore the declaration of a capture proxy for a parameter pack. */
3985 if (is_capture_proxy (decl
))
3987 if (is_typedef_decl (decl
))
3988 /* Since we stop at typedefs above, we need to look through them at
3989 the point of the DECL_EXPR. */
3990 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3991 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3996 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3998 cp_walk_tree (&TREE_TYPE (t
),
3999 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4002 case TYPE_PACK_EXPANSION
:
4003 case EXPR_PACK_EXPANSION
:
4008 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4013 case IDENTIFIER_NODE
:
4014 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4021 /* Since we defer implicit capture, look in the parms and body. */
4022 tree fn
= lambda_function (t
);
4023 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4025 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4032 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4033 type_pack_expansion_p to false so that any placeholders
4034 within the expression don't get marked as parameter packs. */
4035 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4036 ppd
->type_pack_expansion_p
= false;
4037 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4039 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4045 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4047 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4049 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4051 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4062 /* Determines if the expression or type T uses any parameter packs. */
4064 uses_parameter_packs (tree t
)
4066 tree parameter_packs
= NULL_TREE
;
4067 struct find_parameter_pack_data ppd
;
4068 ppd
.parameter_packs
= ¶meter_packs
;
4069 ppd
.visited
= new hash_set
<tree
>;
4070 ppd
.type_pack_expansion_p
= false;
4071 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4073 return parameter_packs
;
4076 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4077 representation a base-class initializer into a parameter pack
4078 expansion. If all goes well, the resulting node will be an
4079 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4082 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4085 tree parameter_packs
= NULL_TREE
;
4086 bool for_types
= false;
4087 struct find_parameter_pack_data ppd
;
4089 if (!arg
|| arg
== error_mark_node
)
4092 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4094 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4095 class initializer. In this case, the TREE_PURPOSE will be a
4096 _TYPE node (representing the base class expansion we're
4097 initializing) and the TREE_VALUE will be a TREE_LIST
4098 containing the initialization arguments.
4100 The resulting expansion looks somewhat different from most
4101 expansions. Rather than returning just one _EXPANSION, we
4102 return a TREE_LIST whose TREE_PURPOSE is a
4103 TYPE_PACK_EXPANSION containing the bases that will be
4104 initialized. The TREE_VALUE will be identical to the
4105 original TREE_VALUE, which is a list of arguments that will
4106 be passed to each base. We do not introduce any new pack
4107 expansion nodes into the TREE_VALUE (although it is possible
4108 that some already exist), because the TREE_PURPOSE and
4109 TREE_VALUE all need to be expanded together with the same
4110 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4111 resulting TREE_PURPOSE will mention the parameter packs in
4112 both the bases and the arguments to the bases. */
4115 tree parameter_packs
= NULL_TREE
;
4117 /* Determine which parameter packs will be used by the base
4119 ppd
.visited
= new hash_set
<tree
>;
4120 ppd
.parameter_packs
= ¶meter_packs
;
4121 ppd
.type_pack_expansion_p
= false;
4122 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4123 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4126 if (parameter_packs
== NULL_TREE
)
4128 if (complain
& tf_error
)
4129 error ("base initializer expansion %qT contains no parameter packs",
4132 return error_mark_node
;
4135 if (TREE_VALUE (arg
) != void_type_node
)
4137 /* Collect the sets of parameter packs used in each of the
4138 initialization arguments. */
4139 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4141 /* Determine which parameter packs will be expanded in this
4143 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4150 /* Create the pack expansion type for the base type. */
4151 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4152 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4153 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4154 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4156 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4157 they will rarely be compared to anything. */
4158 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4160 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4163 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4166 /* Build the PACK_EXPANSION_* node. */
4168 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4169 : make_node (EXPR_PACK_EXPANSION
);
4170 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4171 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4173 /* Propagate type and const-expression information. */
4174 TREE_TYPE (result
) = TREE_TYPE (arg
);
4175 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4176 /* Mark this read now, since the expansion might be length 0. */
4177 mark_exp_read (arg
);
4180 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4181 they will rarely be compared to anything. */
4182 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4184 /* Determine which parameter packs will be expanded. */
4185 ppd
.parameter_packs
= ¶meter_packs
;
4186 ppd
.visited
= new hash_set
<tree
>;
4187 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4188 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4191 /* Make sure we found some parameter packs. */
4192 if (parameter_packs
== NULL_TREE
)
4194 if (complain
& tf_error
)
4197 error ("expansion pattern %qT contains no parameter packs", arg
);
4199 error ("expansion pattern %qE contains no parameter packs", arg
);
4201 return error_mark_node
;
4203 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4205 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4210 /* Checks T for any "bare" parameter packs, which have not yet been
4211 expanded, and issues an error if any are found. This operation can
4212 only be done on full expressions or types (e.g., an expression
4213 statement, "if" condition, etc.), because we could have expressions like:
4215 foo(f(g(h(args)))...)
4217 where "args" is a parameter pack. check_for_bare_parameter_packs
4218 should not be called for the subexpressions args, h(args),
4219 g(h(args)), or f(g(h(args))), because we would produce erroneous
4222 Returns TRUE and emits an error if there were bare parameter packs,
4223 returns FALSE otherwise. */
4225 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4227 tree parameter_packs
= NULL_TREE
;
4228 struct find_parameter_pack_data ppd
;
4230 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4233 /* A lambda might use a parameter pack from the containing context. */
4234 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4235 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4238 if (TREE_CODE (t
) == TYPE_DECL
)
4241 ppd
.parameter_packs
= ¶meter_packs
;
4242 ppd
.visited
= new hash_set
<tree
>;
4243 ppd
.type_pack_expansion_p
= false;
4244 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4247 if (parameter_packs
)
4249 if (loc
== UNKNOWN_LOCATION
)
4250 loc
= cp_expr_loc_or_input_loc (t
);
4251 error_at (loc
, "parameter packs not expanded with %<...%>:");
4252 while (parameter_packs
)
4254 tree pack
= TREE_VALUE (parameter_packs
);
4255 tree name
= NULL_TREE
;
4257 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4258 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4259 name
= TYPE_NAME (pack
);
4260 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4261 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4262 else if (TREE_CODE (pack
) == CALL_EXPR
)
4263 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4265 name
= DECL_NAME (pack
);
4268 inform (loc
, " %qD", name
);
4270 inform (loc
, " %s", "<anonymous>");
4272 parameter_packs
= TREE_CHAIN (parameter_packs
);
4281 /* Expand any parameter packs that occur in the template arguments in
4284 expand_template_argument_pack (tree args
)
4286 if (args
== error_mark_node
)
4287 return error_mark_node
;
4289 tree result_args
= NULL_TREE
;
4290 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4291 int num_result_args
= -1;
4292 int non_default_args_count
= -1;
4294 /* First, determine if we need to expand anything, and the number of
4295 slots we'll need. */
4296 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4298 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4299 if (arg
== NULL_TREE
)
4301 if (ARGUMENT_PACK_P (arg
))
4303 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4304 if (num_result_args
< 0)
4305 num_result_args
= in_arg
+ num_packed
;
4307 num_result_args
+= num_packed
;
4311 if (num_result_args
>= 0)
4316 /* If no expansion is necessary, we're done. */
4317 if (num_result_args
< 0)
4320 /* Expand arguments. */
4321 result_args
= make_tree_vec (num_result_args
);
4322 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4323 non_default_args_count
=
4324 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4325 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4327 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4328 if (ARGUMENT_PACK_P (arg
))
4330 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4331 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4332 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4333 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4334 if (non_default_args_count
> 0)
4335 non_default_args_count
+= num_packed
- 1;
4339 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4343 if (non_default_args_count
>= 0)
4344 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4348 /* Checks if DECL shadows a template parameter.
4350 [temp.local]: A template-parameter shall not be redeclared within its
4351 scope (including nested scopes).
4353 Emits an error and returns TRUE if the DECL shadows a parameter,
4354 returns FALSE otherwise. */
4357 check_template_shadow (tree decl
)
4361 /* If we're not in a template, we can't possibly shadow a template
4363 if (!current_template_parms
)
4366 /* Figure out what we're shadowing. */
4367 decl
= OVL_FIRST (decl
);
4368 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4370 /* If there's no previous binding for this name, we're not shadowing
4371 anything, let alone a template parameter. */
4375 /* If we're not shadowing a template parameter, we're done. Note
4376 that OLDDECL might be an OVERLOAD (or perhaps even an
4377 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4379 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4382 /* We check for decl != olddecl to avoid bogus errors for using a
4383 name inside a class. We check TPFI to avoid duplicate errors for
4384 inline member templates. */
4386 || (DECL_TEMPLATE_PARM_P (decl
)
4387 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4390 /* Don't complain about the injected class name, as we've already
4391 complained about the class itself. */
4392 if (DECL_SELF_REFERENCE_P (decl
))
4395 if (DECL_TEMPLATE_PARM_P (decl
))
4396 error ("declaration of template parameter %q+D shadows "
4397 "template parameter", decl
);
4399 error ("declaration of %q+#D shadows template parameter", decl
);
4400 inform (DECL_SOURCE_LOCATION (olddecl
),
4401 "template parameter %qD declared here", olddecl
);
4405 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4406 ORIG_LEVEL, DECL, and TYPE. */
4409 build_template_parm_index (int index
,
4415 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4416 TEMPLATE_PARM_IDX (t
) = index
;
4417 TEMPLATE_PARM_LEVEL (t
) = level
;
4418 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4419 TEMPLATE_PARM_DECL (t
) = decl
;
4420 TREE_TYPE (t
) = type
;
4421 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4422 TREE_READONLY (t
) = TREE_READONLY (decl
);
4427 /* Find the canonical type parameter for the given template type
4428 parameter. Returns the canonical type parameter, which may be TYPE
4429 if no such parameter existed. */
4432 canonical_type_parameter (tree type
)
4434 int idx
= TEMPLATE_TYPE_IDX (type
);
4436 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4438 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4439 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4441 for (tree list
= (*canonical_template_parms
)[idx
];
4442 list
; list
= TREE_CHAIN (list
))
4443 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4444 return TREE_VALUE (list
);
4446 (*canonical_template_parms
)[idx
]
4447 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4451 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4452 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4453 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4454 new one is created. */
4457 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4458 tsubst_flags_t complain
)
4460 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4461 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4462 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4463 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4465 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4467 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4468 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4470 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4471 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4472 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4473 DECL_ARTIFICIAL (decl
) = 1;
4474 SET_DECL_TEMPLATE_PARM_P (decl
);
4476 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4477 TEMPLATE_PARM_LEVEL (index
) - levels
,
4478 TEMPLATE_PARM_ORIG_LEVEL (index
),
4480 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4481 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4482 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4484 /* Template template parameters need this. */
4486 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4488 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4489 TYPE_DECL
, DECL_NAME (decl
), type
);
4490 DECL_TEMPLATE_RESULT (decl
) = inner
;
4491 DECL_ARTIFICIAL (inner
) = true;
4492 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4493 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4496 /* Attach the TPI to the decl. */
4497 if (TREE_CODE (inner
) == TYPE_DECL
)
4498 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4500 DECL_INITIAL (decl
) = tpi
;
4503 return TEMPLATE_PARM_DESCENDANTS (index
);
4506 /* Process information from new template parameter PARM and append it
4507 to the LIST being built. This new parameter is a non-type
4508 parameter iff IS_NON_TYPE is true. This new parameter is a
4509 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4513 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4514 bool is_non_type
, bool is_parameter_pack
)
4516 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4517 tree prev
= NULL_TREE
;
4522 prev
= tree_last (list
);
4524 tree p
= TREE_VALUE (prev
);
4525 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4526 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4527 else if (TREE_CODE (p
) == PARM_DECL
)
4528 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4533 tree decl
= NULL_TREE
;
4534 tree defval
= TREE_PURPOSE (parm
);
4535 tree constr
= TREE_TYPE (parm
);
4539 parm
= TREE_VALUE (parm
);
4541 SET_DECL_TEMPLATE_PARM_P (parm
);
4543 if (TREE_TYPE (parm
) != error_mark_node
)
4547 The top-level cv-qualifiers on the template-parameter are
4548 ignored when determining its type. */
4549 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4550 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4551 TREE_TYPE (parm
) = error_mark_node
;
4552 else if (uses_parameter_packs (TREE_TYPE (parm
))
4553 && !is_parameter_pack
4554 /* If we're in a nested template parameter list, the template
4555 template parameter could be a parameter pack. */
4556 && processing_template_parmlist
== 1)
4558 /* This template parameter is not a parameter pack, but it
4559 should be. Complain about "bare" parameter packs. */
4560 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4562 /* Recover by calling this a parameter pack. */
4563 is_parameter_pack
= true;
4567 /* A template parameter is not modifiable. */
4568 TREE_CONSTANT (parm
) = 1;
4569 TREE_READONLY (parm
) = 1;
4570 decl
= build_decl (parm_loc
,
4571 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4572 TREE_CONSTANT (decl
) = 1;
4573 TREE_READONLY (decl
) = 1;
4574 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4575 = build_template_parm_index (idx
, processing_template_decl
,
4576 processing_template_decl
,
4577 decl
, TREE_TYPE (parm
));
4579 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4580 = is_parameter_pack
;
4585 parm
= TREE_VALUE (TREE_VALUE (parm
));
4587 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4589 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4590 /* This is for distinguishing between real templates and template
4591 template parameters */
4592 TREE_TYPE (parm
) = t
;
4594 /* any_template_parm_r expects to be able to get the targs of a
4595 DECL_TEMPLATE_RESULT. */
4596 tree result
= DECL_TEMPLATE_RESULT (parm
);
4597 TREE_TYPE (result
) = t
;
4598 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4599 tree tinfo
= build_template_info (parm
, args
);
4600 retrofit_lang_decl (result
);
4601 DECL_TEMPLATE_INFO (result
) = tinfo
;
4607 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4608 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4609 decl
= build_decl (parm_loc
,
4610 TYPE_DECL
, parm
, t
);
4613 TYPE_NAME (t
) = decl
;
4614 TYPE_STUB_DECL (t
) = decl
;
4616 TEMPLATE_TYPE_PARM_INDEX (t
)
4617 = build_template_parm_index (idx
, processing_template_decl
,
4618 processing_template_decl
,
4619 decl
, TREE_TYPE (parm
));
4620 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4621 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4622 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4624 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4626 DECL_ARTIFICIAL (decl
) = 1;
4627 SET_DECL_TEMPLATE_PARM_P (decl
);
4629 /* Build requirements for the type/template parameter.
4630 This must be done after SET_DECL_TEMPLATE_PARM_P or
4631 process_template_parm could fail. */
4632 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4634 decl
= pushdecl (decl
);
4638 /* Build the parameter node linking the parameter declaration,
4639 its default argument (if any), and its constraints (if any). */
4640 parm
= build_tree_list (defval
, parm
);
4641 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4644 TREE_CHAIN (prev
) = parm
;
4651 /* The end of a template parameter list has been reached. Process the
4652 tree list into a parameter vector, converting each parameter into a more
4653 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4657 end_template_parm_list (tree parms
)
4659 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4661 /* Pop the dummy parameter level and add the real one. We do not
4662 morph the dummy parameter in place, as it might have been
4663 captured by a (nested) template-template-parm. */
4664 current_template_parms
= TREE_CHAIN (current_template_parms
);
4666 current_template_parms
4667 = tree_cons (size_int (processing_template_decl
),
4668 saved_parmlist
, current_template_parms
);
4670 for (unsigned ix
= 0; parms
; ix
++)
4673 parms
= TREE_CHAIN (parms
);
4674 TREE_CHAIN (parm
) = NULL_TREE
;
4676 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4679 --processing_template_parmlist
;
4681 return saved_parmlist
;
4684 // Explicitly indicate the end of the template parameter list. We assume
4685 // that the current template parameters have been constructed and/or
4686 // managed explicitly, as when creating new template template parameters
4687 // from a shorthand constraint.
4689 end_template_parm_list ()
4691 --processing_template_parmlist
;
4694 /* end_template_decl is called after a template declaration is seen. */
4697 end_template_decl (void)
4699 reset_specialization ();
4701 if (! processing_template_decl
)
4704 /* This matches the pushlevel in begin_template_parm_list. */
4707 --processing_template_decl
;
4708 current_template_parms
= TREE_CHAIN (current_template_parms
);
4711 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4712 thereof, and converts it into an argument suitable to be passed to
4713 the type substitution functions. Note that if the TREE_LIST contains
4714 an error_mark node, the returned argument is error_mark_node. */
4717 template_parm_to_arg (tree t
)
4722 if (TREE_CODE (t
) == TREE_LIST
)
4725 if (error_operand_p (t
))
4726 return error_mark_node
;
4728 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4730 if (TREE_CODE (t
) == TYPE_DECL
4731 || TREE_CODE (t
) == TEMPLATE_DECL
)
4734 t
= DECL_INITIAL (t
);
4737 gcc_assert (TEMPLATE_PARM_P (t
));
4739 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4740 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4742 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4744 /* Turn this argument into a TYPE_ARGUMENT_PACK
4745 with a single element, which expands T. */
4746 tree vec
= make_tree_vec (1);
4748 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4750 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4752 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4753 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4758 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4760 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4761 with a single element, which expands T. */
4762 tree vec
= make_tree_vec (1);
4764 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4766 t
= convert_from_reference (t
);
4767 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4769 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4770 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4773 t
= convert_from_reference (t
);
4778 /* Given a single level of template parameters (a TREE_VEC), return it
4779 as a set of template arguments. */
4782 template_parms_level_to_args (tree parms
)
4784 tree a
= copy_node (parms
);
4785 TREE_TYPE (a
) = NULL_TREE
;
4786 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4787 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4790 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4795 /* Given a set of template parameters, return them as a set of template
4796 arguments. The template parameters are represented as a TREE_VEC, in
4797 the form documented in cp-tree.h for template arguments. */
4800 template_parms_to_args (tree parms
)
4803 tree args
= NULL_TREE
;
4804 int length
= TMPL_PARMS_DEPTH (parms
);
4807 /* If there is only one level of template parameters, we do not
4808 create a TREE_VEC of TREE_VECs. Instead, we return a single
4809 TREE_VEC containing the arguments. */
4811 args
= make_tree_vec (length
);
4813 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4815 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4818 TREE_VEC_ELT (args
, --l
) = a
;
4826 /* Within the declaration of a template, return the currently active
4827 template parameters as an argument TREE_VEC. */
4830 current_template_args (void)
4832 return template_parms_to_args (current_template_parms
);
4835 /* Return the fully generic arguments for of TMPL, i.e. what
4836 current_template_args would be while parsing it. */
4839 generic_targs_for (tree tmpl
)
4841 if (tmpl
== NULL_TREE
)
4843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4844 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4845 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4846 template parameter, it has no TEMPLATE_INFO; for a partial
4847 specialization, it has the arguments for the primary template, and we
4848 want the arguments for the partial specialization. */;
4849 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4850 if (tree ti
= get_template_info (result
))
4851 return TI_ARGS (ti
);
4852 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4855 /* Update the declared TYPE by doing any lookups which were thought to be
4856 dependent, but are not now that we know the SCOPE of the declarator. */
4859 maybe_update_decl_type (tree orig_type
, tree scope
)
4861 tree type
= orig_type
;
4863 if (type
== NULL_TREE
)
4866 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4867 type
= TREE_TYPE (type
);
4869 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4870 && dependent_type_p (type
)
4871 /* Don't bother building up the args in this case. */
4872 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4874 /* tsubst in the args corresponding to the template parameters,
4875 including auto if present. Most things will be unchanged, but
4876 make_typename_type and tsubst_qualified_id will resolve
4877 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4878 tree args
= current_template_args ();
4879 tree auto_node
= type_uses_auto (type
);
4883 tree auto_vec
= make_tree_vec (1);
4884 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4885 args
= add_to_template_args (args
, auto_vec
);
4887 pushed
= push_scope (scope
);
4888 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4893 if (type
== error_mark_node
)
4896 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4898 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4901 type
= TYPE_NAME (type
);
4906 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4907 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4908 the new template is a member template. */
4911 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4913 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4914 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4915 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4916 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4917 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4918 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4919 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4920 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4925 struct template_parm_data
4927 /* The level of the template parameters we are currently
4931 /* The index of the specialization argument we are currently
4935 /* An array whose size is the number of template parameters. The
4936 elements are nonzero if the parameter has been used in any one
4937 of the arguments processed so far. */
4940 /* An array whose size is the number of template arguments. The
4941 elements are nonzero if the argument makes use of template
4942 parameters of this level. */
4943 int* arg_uses_template_parms
;
4946 /* Subroutine of push_template_decl used to see if each template
4947 parameter in a partial specialization is used in the explicit
4948 argument list. If T is of the LEVEL given in DATA (which is
4949 treated as a template_parm_data*), then DATA->PARMS is marked
4953 mark_template_parm (tree t
, void* data
)
4957 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4959 template_parm_level_and_index (t
, &level
, &idx
);
4961 if (level
== tpd
->level
)
4963 tpd
->parms
[idx
] = 1;
4964 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4967 /* In C++17 the type of a non-type argument is a deduced context. */
4968 if (cxx_dialect
>= cxx17
4969 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4970 for_each_template_parm (TREE_TYPE (t
),
4971 &mark_template_parm
,
4974 /*include_nondeduced_p=*/false);
4976 /* Return zero so that for_each_template_parm will continue the
4977 traversal of the tree; we want to mark *every* template parm. */
4981 /* Process the partial specialization DECL. */
4984 process_partial_specialization (tree decl
)
4986 tree type
= TREE_TYPE (decl
);
4987 tree tinfo
= get_template_info (decl
);
4988 tree maintmpl
= TI_TEMPLATE (tinfo
);
4989 tree specargs
= TI_ARGS (tinfo
);
4990 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4991 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4994 int nargs
= TREE_VEC_LENGTH (inner_args
);
4997 bool did_error_intro
= false;
4998 struct template_parm_data tpd
;
4999 struct template_parm_data tpd2
;
5001 gcc_assert (current_template_parms
);
5003 /* A concept cannot be specialized. */
5004 if (flag_concepts
&& variable_concept_p (maintmpl
))
5006 error ("specialization of variable concept %q#D", maintmpl
);
5007 return error_mark_node
;
5010 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5011 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5013 /* We check that each of the template parameters given in the
5014 partial specialization is used in the argument list to the
5015 specialization. For example:
5017 template <class T> struct S;
5018 template <class T> struct S<T*>;
5020 The second declaration is OK because `T*' uses the template
5021 parameter T, whereas
5023 template <class T> struct S<int>;
5025 is no good. Even trickier is:
5036 The S2<T> declaration is actually invalid; it is a
5037 full-specialization. Of course,
5040 struct S2<T (*)(U)>;
5042 or some such would have been OK. */
5043 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5044 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5045 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5047 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5048 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5049 for (i
= 0; i
< nargs
; ++i
)
5051 tpd
.current_arg
= i
;
5052 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5053 &mark_template_parm
,
5056 /*include_nondeduced_p=*/false);
5058 for (i
= 0; i
< ntparms
; ++i
)
5059 if (tpd
.parms
[i
] == 0)
5061 /* One of the template parms was not used in a deduced context in the
5063 if (!did_error_intro
)
5065 error ("template parameters not deducible in "
5066 "partial specialization:");
5067 did_error_intro
= true;
5070 inform (input_location
, " %qD",
5071 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5074 if (did_error_intro
)
5075 return error_mark_node
;
5077 /* [temp.class.spec]
5079 The argument list of the specialization shall not be identical to
5080 the implicit argument list of the primary template. */
5082 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5083 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5085 || !strictly_subsumes (current_template_constraints (),
5086 main_args
, maintmpl
)))
5089 error ("partial specialization %q+D does not specialize "
5090 "any template arguments; to define the primary template, "
5091 "remove the template argument list", decl
);
5093 error ("partial specialization %q+D does not specialize any "
5094 "template arguments and is not more constrained than "
5095 "the primary template; to define the primary template, "
5096 "remove the template argument list", decl
);
5097 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5100 /* A partial specialization that replaces multiple parameters of the
5101 primary template with a pack expansion is less specialized for those
5103 if (nargs
< DECL_NTPARMS (maintmpl
))
5105 error ("partial specialization is not more specialized than the "
5106 "primary template because it replaces multiple parameters "
5107 "with a pack expansion");
5108 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5109 /* Avoid crash in process_partial_specialization. */
5113 else if (nargs
> DECL_NTPARMS (maintmpl
))
5115 error ("too many arguments for partial specialization %qT", type
);
5116 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5117 /* Avoid crash below. */
5121 /* If we aren't in a dependent class, we can actually try deduction. */
5122 else if (tpd
.level
== 1
5123 /* FIXME we should be able to handle a partial specialization of a
5124 partial instantiation, but currently we can't (c++/41727). */
5125 && TMPL_ARGS_DEPTH (specargs
) == 1
5126 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5128 auto_diagnostic_group d
;
5129 if (permerror (input_location
, "partial specialization %qD is not "
5130 "more specialized than", decl
))
5131 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5135 /* [temp.class.spec]
5137 A partially specialized non-type argument expression shall not
5138 involve template parameters of the partial specialization except
5139 when the argument expression is a simple identifier.
5141 The type of a template parameter corresponding to a specialized
5142 non-type argument shall not be dependent on a parameter of the
5145 Also, we verify that pack expansions only occur at the
5146 end of the argument list. */
5148 for (i
= 0; i
< nargs
; ++i
)
5150 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5151 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5152 tree packed_args
= NULL_TREE
;
5155 if (ARGUMENT_PACK_P (arg
))
5157 /* Extract the arguments from the argument pack. We'll be
5158 iterating over these in the following loop. */
5159 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5160 len
= TREE_VEC_LENGTH (packed_args
);
5163 for (j
= 0; j
< len
; j
++)
5166 /* Get the Jth argument in the parameter pack. */
5167 arg
= TREE_VEC_ELT (packed_args
, j
);
5169 if (PACK_EXPANSION_P (arg
))
5171 /* Pack expansions must come at the end of the
5173 if ((packed_args
&& j
< len
- 1)
5174 || (!packed_args
&& i
< nargs
- 1))
5176 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5177 error ("parameter pack argument %qE must be at the "
5178 "end of the template argument list", arg
);
5180 error ("parameter pack argument %qT must be at the "
5181 "end of the template argument list", arg
);
5185 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5186 /* We only care about the pattern. */
5187 arg
= PACK_EXPANSION_PATTERN (arg
);
5189 if (/* These first two lines are the `non-type' bit. */
5191 && TREE_CODE (arg
) != TEMPLATE_DECL
5192 /* This next two lines are the `argument expression is not just a
5193 simple identifier' condition and also the `specialized
5194 non-type argument' bit. */
5195 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5196 && !((REFERENCE_REF_P (arg
)
5197 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5198 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5200 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5201 || (packed_args
&& uses_template_parms (arg
)))
5202 error_at (cp_expr_loc_or_input_loc (arg
),
5203 "template argument %qE involves template "
5204 "parameter(s)", arg
);
5207 /* Look at the corresponding template parameter,
5208 marking which template parameters its type depends
5210 tree type
= TREE_TYPE (parm
);
5214 /* We haven't yet initialized TPD2. Do so now. */
5215 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5216 /* The number of parameters here is the number in the
5217 main template, which, as checked in the assertion
5219 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5221 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5224 /* Mark the template parameters. But this time, we're
5225 looking for the template parameters of the main
5226 template, not in the specialization. */
5227 tpd2
.current_arg
= i
;
5228 tpd2
.arg_uses_template_parms
[i
] = 0;
5229 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5230 for_each_template_parm (type
,
5231 &mark_template_parm
,
5234 /*include_nondeduced_p=*/false);
5236 if (tpd2
.arg_uses_template_parms
[i
])
5238 /* The type depended on some template parameters.
5239 If they are fully specialized in the
5240 specialization, that's OK. */
5243 for (j
= 0; j
< nargs
; ++j
)
5244 if (tpd2
.parms
[j
] != 0
5245 && tpd
.arg_uses_template_parms
[j
])
5248 error_n (input_location
, count
,
5249 "type %qT of template argument %qE depends "
5250 "on a template parameter",
5251 "type %qT of template argument %qE depends "
5252 "on template parameters",
5261 /* We should only get here once. */
5262 if (TREE_CODE (decl
) == TYPE_DECL
)
5263 gcc_assert (!COMPLETE_TYPE_P (type
));
5265 // Build the template decl.
5266 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5267 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5268 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5269 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5270 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5272 /* Give template template parms a DECL_CONTEXT of the template
5273 for which they are a parameter. */
5274 for (i
= 0; i
< ntparms
; ++i
)
5276 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5277 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5278 DECL_CONTEXT (parm
) = tmpl
;
5282 /* We didn't register this in check_explicit_specialization so we could
5283 wait until the constraints were set. */
5284 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5286 associate_classtype_constraints (type
);
5288 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5289 = tree_cons (specargs
, tmpl
,
5290 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5291 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5293 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5294 inst
= TREE_CHAIN (inst
))
5296 tree instance
= TREE_VALUE (inst
);
5297 if (TYPE_P (instance
)
5298 ? (COMPLETE_TYPE_P (instance
)
5299 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5300 : DECL_TEMPLATE_INSTANTIATION (instance
))
5302 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5303 tree inst_decl
= (DECL_P (instance
)
5304 ? instance
: TYPE_NAME (instance
));
5307 else if (spec
== error_mark_node
)
5308 permerror (input_location
,
5309 "declaration of %qD ambiguates earlier template "
5310 "instantiation for %qD", decl
, inst_decl
);
5311 else if (TREE_VALUE (spec
) == tmpl
)
5312 permerror (input_location
,
5313 "partial specialization of %qD after instantiation "
5314 "of %qD", decl
, inst_decl
);
5321 /* PARM is a template parameter of some form; return the corresponding
5322 TEMPLATE_PARM_INDEX. */
5325 get_template_parm_index (tree parm
)
5327 if (TREE_CODE (parm
) == PARM_DECL
5328 || TREE_CODE (parm
) == CONST_DECL
)
5329 parm
= DECL_INITIAL (parm
);
5330 else if (TREE_CODE (parm
) == TYPE_DECL
5331 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5332 parm
= TREE_TYPE (parm
);
5333 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5334 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5335 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5336 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5337 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5341 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5342 parameter packs used by the template parameter PARM. */
5345 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5347 /* A type parm can't refer to another parm. */
5348 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5350 else if (TREE_CODE (parm
) == PARM_DECL
)
5352 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5357 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5359 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5360 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5362 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5363 if (template_parameter_pack_p (p
))
5364 /* Any packs in the type are expanded by this parameter. */;
5366 fixed_parameter_pack_p_1 (p
, ppd
);
5370 /* PARM is a template parameter pack. Return any parameter packs used in
5371 its type or the type of any of its template parameters. If there are
5372 any such packs, it will be instantiated into a fixed template parameter
5373 list by partial instantiation rather than be fully deduced. */
5376 fixed_parameter_pack_p (tree parm
)
5378 /* This can only be true in a member template. */
5379 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5381 /* This can only be true for a parameter pack. */
5382 if (!template_parameter_pack_p (parm
))
5384 /* A type parm can't refer to another parm. */
5385 if (TREE_CODE (parm
) == TYPE_DECL
)
5388 tree parameter_packs
= NULL_TREE
;
5389 struct find_parameter_pack_data ppd
;
5390 ppd
.parameter_packs
= ¶meter_packs
;
5391 ppd
.visited
= new hash_set
<tree
>;
5392 ppd
.type_pack_expansion_p
= false;
5394 fixed_parameter_pack_p_1 (parm
, &ppd
);
5397 return parameter_packs
;
5400 /* Check that a template declaration's use of default arguments and
5401 parameter packs is not invalid. Here, PARMS are the template
5402 parameters. IS_PRIMARY is true if DECL is the thing declared by
5403 a primary template. IS_PARTIAL is true if DECL is a partial
5406 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5407 function template declaration or a friend class template
5408 declaration. In the function case, 1 indicates a declaration, 2
5409 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5410 emitted for extraneous default arguments.
5412 Returns TRUE if there were no errors found, FALSE otherwise. */
5415 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5416 bool is_partial
, int is_friend_decl
)
5419 int last_level_to_check
;
5421 bool no_errors
= true;
5425 A default template-argument shall not be specified in a
5426 function template declaration or a function template definition, nor
5427 in the template-parameter-list of the definition of a member of a
5430 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5431 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5432 /* You can't have a function template declaration in a local
5433 scope, nor you can you define a member of a class template in a
5437 if ((TREE_CODE (decl
) == TYPE_DECL
5439 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5440 || (TREE_CODE (decl
) == FUNCTION_DECL
5441 && LAMBDA_FUNCTION_P (decl
)))
5442 /* A lambda doesn't have an explicit declaration; don't complain
5443 about the parms of the enclosing class. */
5446 if (current_class_type
5447 && !TYPE_BEING_DEFINED (current_class_type
)
5448 && DECL_LANG_SPECIFIC (decl
)
5449 && DECL_DECLARES_FUNCTION_P (decl
)
5450 /* If this is either a friend defined in the scope of the class
5451 or a member function. */
5452 && (DECL_FUNCTION_MEMBER_P (decl
)
5453 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5454 : DECL_FRIEND_CONTEXT (decl
)
5455 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5457 /* And, if it was a member function, it really was defined in
5458 the scope of the class. */
5459 && (!DECL_FUNCTION_MEMBER_P (decl
)
5460 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5461 /* We already checked these parameters when the template was
5462 declared, so there's no need to do it again now. This function
5463 was defined in class scope, but we're processing its body now
5464 that the class is complete. */
5467 /* Core issue 226 (C++0x only): the following only applies to class
5470 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5474 If a template-parameter has a default template-argument, all
5475 subsequent template-parameters shall have a default
5476 template-argument supplied. */
5477 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5479 tree inner_parms
= TREE_VALUE (parm_level
);
5480 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5481 int seen_def_arg_p
= 0;
5484 for (i
= 0; i
< ntparms
; ++i
)
5486 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5488 if (parm
== error_mark_node
)
5491 if (TREE_PURPOSE (parm
))
5493 else if (seen_def_arg_p
5494 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5496 error ("no default argument for %qD", TREE_VALUE (parm
));
5497 /* For better subsequent error-recovery, we indicate that
5498 there should have been a default argument. */
5499 TREE_PURPOSE (parm
) = error_mark_node
;
5502 else if (!is_partial
5504 /* Don't complain about an enclosing partial
5506 && parm_level
== parms
5507 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5509 && template_parameter_pack_p (TREE_VALUE (parm
))
5510 /* A fixed parameter pack will be partially
5511 instantiated into a fixed length list. */
5512 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5514 /* A primary class template, primary variable template
5515 (DR 2032), or alias template can only have one
5516 parameter pack, at the end of the template
5519 error ("parameter pack %q+D must be at the end of the"
5520 " template parameter list", TREE_VALUE (parm
));
5522 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5530 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5534 /* For an ordinary class template, default template arguments are
5535 allowed at the innermost level, e.g.:
5536 template <class T = int>
5538 but, in a partial specialization, they're not allowed even
5539 there, as we have in [temp.class.spec]:
5541 The template parameter list of a specialization shall not
5542 contain default template argument values.
5544 So, for a partial specialization, or for a function template
5545 (in C++98/C++03), we look at all of them. */
5548 /* But, for a primary class template that is not a partial
5549 specialization we look at all template parameters except the
5551 parms
= TREE_CHAIN (parms
);
5553 /* Figure out what error message to issue. */
5554 if (is_friend_decl
== 2)
5555 msg
= G_("default template arguments may not be used in function template "
5556 "friend re-declaration");
5557 else if (is_friend_decl
)
5558 msg
= G_("default template arguments may not be used in template "
5559 "friend declarations");
5560 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5561 msg
= G_("default template arguments may not be used in function templates "
5562 "without %<-std=c++11%> or %<-std=gnu++11%>");
5563 else if (is_partial
)
5564 msg
= G_("default template arguments may not be used in "
5565 "partial specializations");
5566 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5567 msg
= G_("default argument for template parameter for class enclosing %qD");
5569 /* Per [temp.param]/9, "A default template-argument shall not be
5570 specified in the template-parameter-lists of the definition of
5571 a member of a class template that appears outside of the member's
5572 class.", thus if we aren't handling a member of a class template
5573 there is no need to examine the parameters. */
5576 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5577 /* If we're inside a class definition, there's no need to
5578 examine the parameters to the class itself. On the one
5579 hand, they will be checked when the class is defined, and,
5580 on the other, default arguments are valid in things like:
5581 template <class T = double>
5582 struct S { template <class U> void f(U); };
5583 Here the default argument for `S' has no bearing on the
5584 declaration of `f'. */
5585 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5587 /* Check everything. */
5588 last_level_to_check
= 0;
5590 for (parm_level
= parms
;
5591 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5592 parm_level
= TREE_CHAIN (parm_level
))
5594 tree inner_parms
= TREE_VALUE (parm_level
);
5598 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5599 for (i
= 0; i
< ntparms
; ++i
)
5601 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5604 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5609 if (is_friend_decl
== 2)
5616 /* Clear out the default argument so that we are not
5618 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5622 /* At this point, if we're still interested in issuing messages,
5623 they must apply to classes surrounding the object declared. */
5625 msg
= G_("default argument for template parameter for class "
5632 /* Worker for push_template_decl_real, called via
5633 for_each_template_parm. DATA is really an int, indicating the
5634 level of the parameters we are interested in. If T is a template
5635 parameter of that level, return nonzero. */
5638 template_parm_this_level_p (tree t
, void* data
)
5640 int this_level
= *(int *)data
;
5643 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5644 level
= TEMPLATE_PARM_LEVEL (t
);
5646 level
= TEMPLATE_TYPE_LEVEL (t
);
5647 return level
== this_level
;
5650 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5651 DATA is really an int, indicating the innermost outer level of parameters.
5652 If T is a template parameter of that level or further out, return
5656 template_parm_outer_level (tree t
, void *data
)
5658 int this_level
= *(int *)data
;
5661 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5662 level
= TEMPLATE_PARM_LEVEL (t
);
5664 level
= TEMPLATE_TYPE_LEVEL (t
);
5665 return level
<= this_level
;
5668 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5669 parameters given by current_template_args, or reuses a
5670 previously existing one, if appropriate. Returns the DECL, or an
5671 equivalent one, if it is replaced via a call to duplicate_decls.
5673 If IS_FRIEND is true, DECL is a friend declaration. */
5676 push_template_decl (tree decl
, bool is_friend
)
5684 int new_template_p
= 0;
5685 /* True if the template is a member template, in the sense of
5687 bool member_template_p
= false;
5689 if (decl
== error_mark_node
|| !current_template_parms
)
5690 return error_mark_node
;
5692 /* See if this is a partial specialization. */
5693 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5694 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5695 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5697 && DECL_LANG_SPECIFIC (decl
)
5698 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5699 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5701 /* No surprising friend functions. */
5702 gcc_checking_assert (is_friend
5703 || !(TREE_CODE (decl
) == FUNCTION_DECL
5704 && DECL_FRIEND_P (decl
)));
5707 /* For a friend, we want the context of the friend, not
5708 the type of which it is a friend. */
5709 ctx
= CP_DECL_CONTEXT (decl
);
5710 else if (CP_DECL_CONTEXT (decl
)
5711 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5712 /* In the case of a virtual function, we want the class in which
5714 ctx
= CP_DECL_CONTEXT (decl
);
5716 /* Otherwise, if we're currently defining some class, the DECL
5717 is assumed to be a member of the class. */
5718 ctx
= current_scope ();
5720 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5723 if (!DECL_CONTEXT (decl
))
5724 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5726 /* See if this is a primary template. */
5727 if (is_friend
&& ctx
5728 && uses_template_parms_level (ctx
, processing_template_decl
))
5729 /* A friend template that specifies a class context, i.e.
5730 template <typename T> friend void A<T>::f();
5733 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5736 is_primary
= template_parm_scope_p ();
5740 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5742 if (DECL_CLASS_SCOPE_P (decl
))
5743 member_template_p
= true;
5744 if (TREE_CODE (decl
) == TYPE_DECL
5745 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5747 error ("template class without a name");
5748 return error_mark_node
;
5750 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5752 if (member_template_p
)
5754 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5755 error ("member template %qD may not have virt-specifiers", decl
);
5757 if (DECL_DESTRUCTOR_P (decl
))
5761 A destructor shall not be a member template. */
5762 error_at (DECL_SOURCE_LOCATION (decl
),
5763 "destructor %qD declared as member template", decl
);
5764 return error_mark_node
;
5766 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5767 && (!prototype_p (TREE_TYPE (decl
))
5768 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5769 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5770 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5771 == void_list_node
)))
5773 /* [basic.stc.dynamic.allocation]
5775 An allocation function can be a function
5776 template. ... Template allocation functions shall
5777 have two or more parameters. */
5778 error ("invalid template declaration of %qD", decl
);
5779 return error_mark_node
;
5782 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5783 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5785 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5786 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5787 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5789 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5790 if (TREE_CODE (t
) == TYPE_DECL
)
5792 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5793 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5796 else if (TREE_CODE (decl
) == TYPE_DECL
5797 && TYPE_DECL_ALIAS_P (decl
))
5798 /* alias-declaration */
5799 gcc_assert (!DECL_ARTIFICIAL (decl
));
5800 else if (VAR_P (decl
))
5801 /* C++14 variable template. */;
5802 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5803 /* C++20 concept definitions. */;
5806 error ("template declaration of %q#D", decl
);
5807 return error_mark_node
;
5811 /* Check to see that the rules regarding the use of default
5812 arguments are not being violated. We check args for a friend
5813 functions when we know whether it's a definition, introducing
5814 declaration or re-declaration. */
5815 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5816 check_default_tmpl_args (decl
, current_template_parms
,
5817 is_primary
, is_partial
, is_friend
);
5819 /* Ensure that there are no parameter packs in the type of this
5820 declaration that have not been expanded. */
5821 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5823 /* Check each of the arguments individually to see if there are
5824 any bare parameter packs. */
5825 tree type
= TREE_TYPE (decl
);
5826 tree arg
= DECL_ARGUMENTS (decl
);
5827 tree argtype
= TYPE_ARG_TYPES (type
);
5829 while (arg
&& argtype
)
5831 if (!DECL_PACK_P (arg
)
5832 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5834 /* This is a PARM_DECL that contains unexpanded parameter
5835 packs. We have already complained about this in the
5836 check_for_bare_parameter_packs call, so just replace
5837 these types with ERROR_MARK_NODE. */
5838 TREE_TYPE (arg
) = error_mark_node
;
5839 TREE_VALUE (argtype
) = error_mark_node
;
5842 arg
= DECL_CHAIN (arg
);
5843 argtype
= TREE_CHAIN (argtype
);
5846 /* Check for bare parameter packs in the return type and the
5847 exception specifiers. */
5848 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5849 /* Errors were already issued, set return type to int
5850 as the frontend doesn't expect error_mark_node as
5852 TREE_TYPE (type
) = integer_type_node
;
5853 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5854 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5856 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5857 ? DECL_ORIGINAL_TYPE (decl
)
5858 : TREE_TYPE (decl
)))
5860 TREE_TYPE (decl
) = error_mark_node
;
5861 return error_mark_node
;
5865 return process_partial_specialization (decl
);
5867 args
= current_template_args ();
5870 || TREE_CODE (ctx
) == FUNCTION_DECL
5871 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5872 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5873 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5875 if (DECL_LANG_SPECIFIC (decl
)
5876 && DECL_TEMPLATE_INFO (decl
)
5877 && DECL_TI_TEMPLATE (decl
))
5878 tmpl
= DECL_TI_TEMPLATE (decl
);
5879 /* If DECL is a TYPE_DECL for a class-template, then there won't
5880 be DECL_LANG_SPECIFIC. The information equivalent to
5881 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5882 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5883 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5884 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5886 /* Since a template declaration already existed for this
5887 class-type, we must be redeclaring it here. Make sure
5888 that the redeclaration is valid. */
5889 redeclare_class_template (TREE_TYPE (decl
),
5890 current_template_parms
,
5891 current_template_constraints ());
5892 /* We don't need to create a new TEMPLATE_DECL; just use the
5893 one we already had. */
5894 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5898 tmpl
= build_template_decl (decl
, current_template_parms
,
5902 if (DECL_LANG_SPECIFIC (decl
)
5903 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5905 /* A specialization of a member template of a template
5907 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5908 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5909 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5915 tree a
, t
, current
, parms
;
5917 tree tinfo
= get_template_info (decl
);
5921 error ("template definition of non-template %q#D", decl
);
5922 return error_mark_node
;
5925 tmpl
= TI_TEMPLATE (tinfo
);
5927 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5928 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5929 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5930 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5934 /* The declaration is a specialization of a member
5935 template, declared outside the class. Therefore, the
5936 innermost template arguments will be NULL, so we
5937 replace them with the arguments determined by the
5938 earlier call to check_explicit_specialization. */
5939 args
= DECL_TI_ARGS (decl
);
5942 = build_template_decl (decl
, current_template_parms
,
5944 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5945 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5946 DECL_TEMPLATE_INFO (new_tmpl
)
5947 = build_template_info (tmpl
, args
);
5949 register_specialization (new_tmpl
,
5950 most_general_template (tmpl
),
5956 /* Make sure the template headers we got make sense. */
5958 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5959 i
= TMPL_PARMS_DEPTH (parms
);
5960 if (TMPL_ARGS_DEPTH (args
) != i
)
5962 error ("expected %d levels of template parms for %q#D, got %d",
5963 i
, decl
, TMPL_ARGS_DEPTH (args
));
5964 DECL_INTERFACE_KNOWN (decl
) = 1;
5965 return error_mark_node
;
5968 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5970 a
= TMPL_ARGS_LEVEL (args
, i
);
5971 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5973 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5975 if (current
== decl
)
5976 error ("got %d template parameters for %q#D",
5977 TREE_VEC_LENGTH (a
), decl
);
5979 error ("got %d template parameters for %q#T",
5980 TREE_VEC_LENGTH (a
), current
);
5981 error (" but %d required", TREE_VEC_LENGTH (t
));
5982 /* Avoid crash in import_export_decl. */
5983 DECL_INTERFACE_KNOWN (decl
) = 1;
5984 return error_mark_node
;
5987 if (current
== decl
)
5989 else if (current
== NULL_TREE
)
5990 /* Can happen in erroneous input. */
5993 current
= get_containing_scope (current
);
5996 /* Check that the parms are used in the appropriate qualifying scopes
5997 in the declarator. */
5998 if (!comp_template_args
6000 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6002 error ("template arguments to %qD do not match original "
6003 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6004 if (!uses_template_parms (TI_ARGS (tinfo
)))
6005 inform (input_location
, "use %<template<>%> for"
6006 " an explicit specialization");
6007 /* Avoid crash in import_export_decl. */
6008 DECL_INTERFACE_KNOWN (decl
) = 1;
6009 return error_mark_node
;
6013 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6017 /* Push template declarations for global functions and types.
6018 Note that we do not try to push a global template friend
6019 declared in a template class; such a thing may well depend on
6020 the template parameters of the class and we'll push it when
6021 instantiating the befriending class. */
6023 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6025 /* Hide template friend classes that haven't been declared yet. */
6026 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
6027 DECL_FRIEND_P (tmpl
) = 1;
6029 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6030 if (tmpl
== error_mark_node
)
6031 return error_mark_node
;
6035 /* The type may have been completed, or (erroneously) changed. */
6036 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6040 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6042 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6044 /* Give template template parms a DECL_CONTEXT of the template
6045 for which they are a parameter. */
6046 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6047 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6049 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6050 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6051 DECL_CONTEXT (parm
) = tmpl
;
6054 if (TREE_CODE (decl
) == TYPE_DECL
6055 && TYPE_DECL_ALIAS_P (decl
))
6058 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6060 /* ??? Why don't we do this here for all templates? */
6061 constr
= build_constraints (constr
, NULL_TREE
);
6062 set_constraints (decl
, constr
);
6064 if (complex_alias_template_p (tmpl
))
6065 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6069 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6070 back to its most general template. If TMPL is a specialization,
6071 ARGS may only have the innermost set of arguments. Add the missing
6072 argument levels if necessary. */
6073 if (DECL_TEMPLATE_INFO (tmpl
))
6074 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6076 info
= build_template_info (tmpl
, args
);
6078 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6079 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6083 retrofit_lang_decl (decl
);
6084 if (DECL_LANG_SPECIFIC (decl
)
6085 && !(VAR_OR_FUNCTION_DECL_P (decl
)
6086 && DECL_LOCAL_DECL_P (decl
)))
6087 DECL_TEMPLATE_INFO (decl
) = info
;
6090 if (flag_implicit_templates
6092 && TREE_PUBLIC (decl
)
6093 && VAR_OR_FUNCTION_DECL_P (decl
))
6094 /* Set DECL_COMDAT on template instantiations; if we force
6095 them to be emitted by explicit instantiation,
6096 mark_needed will tell cgraph to do the right thing. */
6097 DECL_COMDAT (decl
) = true;
6099 return DECL_TEMPLATE_RESULT (tmpl
);
6102 /* FN is an inheriting constructor that inherits from the constructor
6103 template INHERITED; turn FN into a constructor template with a matching
6107 add_inherited_template_parms (tree fn
, tree inherited
)
6110 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6111 inner_parms
= copy_node (inner_parms
);
6113 = tree_cons (size_int (processing_template_decl
+ 1),
6114 inner_parms
, current_template_parms
);
6115 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6116 tree args
= template_parms_to_args (parms
);
6117 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6118 DECL_ARTIFICIAL (tmpl
) = true;
6119 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6123 /* Called when a class template TYPE is redeclared with the indicated
6124 template PARMS, e.g.:
6126 template <class T> struct S;
6127 template <class T> struct S {}; */
6130 redeclare_class_template (tree type
, tree parms
, tree cons
)
6136 if (!TYPE_TEMPLATE_INFO (type
))
6138 error ("%qT is not a template type", type
);
6142 tmpl
= TYPE_TI_TEMPLATE (type
);
6143 if (!PRIMARY_TEMPLATE_P (tmpl
))
6144 /* The type is nested in some template class. Nothing to worry
6145 about here; there are no new template parameters for the nested
6151 error ("template specifiers not specified in declaration of %qD",
6156 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6157 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6159 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6161 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6162 "redeclared with %d template parameter",
6163 "redeclared with %d template parameters",
6164 TREE_VEC_LENGTH (parms
));
6165 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6166 "previous declaration %qD used %d template parameter",
6167 "previous declaration %qD used %d template parameters",
6168 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6172 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6179 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6180 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6183 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6184 if (error_operand_p (tmpl_parm
))
6187 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6188 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6189 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6191 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6193 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6194 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6195 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6196 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6197 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6198 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6199 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6200 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6201 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6203 auto_diagnostic_group d
;
6204 error ("template parameter %q+#D", tmpl_parm
);
6205 inform (input_location
, "redeclared here as %q#D", parm
);
6209 /* The parameters can be declared to introduce different
6211 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6212 tree p2
= TREE_VEC_ELT (parms
, i
);
6213 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6215 auto_diagnostic_group d
;
6216 error ("declaration of template parameter %q+#D with different "
6217 "constraints", parm
);
6218 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6219 "original declaration appeared here");
6223 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6225 /* We have in [temp.param]:
6227 A template-parameter may not be given default arguments
6228 by two different declarations in the same scope. */
6229 auto_diagnostic_group d
;
6230 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6231 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6232 "original definition appeared here");
6236 if (parm_default
!= NULL_TREE
)
6237 /* Update the previous template parameters (which are the ones
6238 that will really count) with the new default value. */
6239 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6240 else if (tmpl_default
!= NULL_TREE
)
6241 /* Update the new parameters, too; they'll be used as the
6242 parameters for any members. */
6243 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6245 /* Give each template template parm in this redeclaration a
6246 DECL_CONTEXT of the template for which they are a parameter. */
6247 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6249 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6250 DECL_CONTEXT (parm
) = tmpl
;
6253 if (TREE_CODE (parm
) == TYPE_DECL
)
6254 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6257 tree ci
= get_constraints (tmpl
);
6258 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6259 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6261 /* Two classes with different constraints declare different entities. */
6262 if (!cp_tree_equal (req1
, req2
))
6264 auto_diagnostic_group d
;
6265 error_at (input_location
, "redeclaration %q#D with different "
6266 "constraints", tmpl
);
6267 inform (DECL_SOURCE_LOCATION (tmpl
),
6268 "original declaration appeared here");
6275 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6276 to be used when the caller has already checked
6277 (processing_template_decl
6278 && !instantiation_dependent_expression_p (expr)
6279 && potential_constant_expression (expr))
6280 and cleared processing_template_decl. */
6283 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6285 return tsubst_copy_and_build (expr
,
6288 /*in_decl=*/NULL_TREE
,
6289 /*function_p=*/false,
6290 /*integral_constant_expression_p=*/true);
6293 /* Simplify EXPR if it is a non-dependent expression. Returns the
6294 (possibly simplified) expression. */
6297 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6299 if (expr
== NULL_TREE
)
6302 /* If we're in a template, but EXPR isn't value dependent, simplify
6303 it. We're supposed to treat:
6305 template <typename T> void f(T[1 + 1]);
6306 template <typename T> void f(T[2]);
6308 as two declarations of the same function, for example. */
6309 if (processing_template_decl
6310 && is_nondependent_constant_expression (expr
))
6312 processing_template_decl_sentinel s
;
6313 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6319 instantiate_non_dependent_expr (tree expr
)
6321 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6324 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6325 an uninstantiated expression. */
6328 instantiate_non_dependent_or_null (tree expr
)
6330 if (expr
== NULL_TREE
)
6332 if (processing_template_decl
)
6334 if (!is_nondependent_constant_expression (expr
))
6338 processing_template_decl_sentinel s
;
6339 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6345 /* True iff T is a specialization of a variable template. */
6348 variable_template_specialization_p (tree t
)
6350 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6352 tree tmpl
= DECL_TI_TEMPLATE (t
);
6353 return variable_template_p (tmpl
);
6356 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6357 template declaration, or a TYPE_DECL for an alias declaration. */
6360 alias_type_or_template_p (tree t
)
6364 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6367 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6368 || DECL_ALIAS_TEMPLATE_P (t
));
6371 /* If T is a specialization of an alias template, return it; otherwise return
6372 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6375 alias_template_specialization_p (const_tree t
,
6376 bool transparent_typedefs
)
6381 /* It's an alias template specialization if it's an alias and its
6382 TYPE_NAME is a specialization of a primary template. */
6383 if (typedef_variant_p (t
))
6385 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6386 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6387 return CONST_CAST_TREE (t
);
6388 if (transparent_typedefs
)
6389 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6391 transparent_typedefs
);
6397 /* An alias template is complex from a SFINAE perspective if a template-id
6398 using that alias can be ill-formed when the expansion is not, as with
6399 the void_t template. We determine this by checking whether the
6400 expansion for the alias template uses all its template parameters. */
6402 struct uses_all_template_parms_data
6409 uses_all_template_parms_r (tree t
, void *data_
)
6411 struct uses_all_template_parms_data
&data
6412 = *(struct uses_all_template_parms_data
*)data_
;
6413 tree idx
= get_template_parm_index (t
);
6415 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6416 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6420 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6423 complex_pack_expansion_r (tree t
, void *data_
)
6425 /* An alias template with a pack expansion that expands a pack from the
6426 enclosing class needs to be considered complex, to avoid confusion with
6427 the same pack being used as an argument to the alias's own template
6428 parameter (91966). */
6429 if (!PACK_EXPANSION_P (t
))
6431 struct uses_all_template_parms_data
&data
6432 = *(struct uses_all_template_parms_data
*)data_
;
6433 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6434 pack
= TREE_CHAIN (pack
))
6436 tree parm_pack
= TREE_VALUE (pack
);
6437 if (!TEMPLATE_PARM_P (parm_pack
))
6440 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6441 if (level
< data
.level
)
6448 complex_alias_template_p (const_tree tmpl
)
6450 /* A renaming alias isn't complex. */
6451 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6454 /* Any other constrained alias is complex. */
6455 if (get_constraints (tmpl
))
6458 struct uses_all_template_parms_data data
;
6459 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6460 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6461 data
.level
= TMPL_PARMS_DEPTH (parms
);
6462 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6463 data
.seen
= XALLOCAVEC (bool, len
);
6464 for (int i
= 0; i
< len
; ++i
)
6465 data
.seen
[i
] = false;
6467 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6468 NULL
, true, complex_pack_expansion_r
))
6470 for (int i
= 0; i
< len
; ++i
)
6476 /* If T is a specialization of a complex alias template with dependent
6477 template-arguments, return it; otherwise return NULL_TREE. If T is a
6478 typedef to such a specialization, return the specialization. */
6481 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6483 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6486 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6488 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6489 && (any_dependent_template_arguments_p
6490 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6491 return CONST_CAST_TREE (t
);
6493 if (transparent_typedefs
)
6495 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6496 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6502 /* Return the number of innermost template parameters in TMPL. */
6505 num_innermost_template_parms (const_tree tmpl
)
6507 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6508 return TREE_VEC_LENGTH (parms
);
6511 /* Return either TMPL or another template that it is equivalent to under DR
6512 1286: An alias that just changes the name of a template is equivalent to
6513 the other template. */
6516 get_underlying_template (tree tmpl
)
6518 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6519 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6521 /* Determine if the alias is equivalent to an underlying template. */
6522 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6523 /* The underlying type may have been ill-formed. Don't proceed. */
6526 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6530 tree underlying
= TI_TEMPLATE (tinfo
);
6531 if (!PRIMARY_TEMPLATE_P (underlying
)
6532 || (num_innermost_template_parms (tmpl
)
6533 != num_innermost_template_parms (underlying
)))
6536 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6537 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6540 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6541 it's appropriate to treat a less-constrained alias as equivalent. */
6542 if (!at_least_as_constrained (underlying
, tmpl
))
6545 /* Alias is equivalent. Strip it and repeat. */
6552 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6553 must be a reference-to-function or a pointer-to-function type, as specified
6554 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6555 and check that the resulting function has external linkage. */
6558 convert_nontype_argument_function (tree type
, tree expr
,
6559 tsubst_flags_t complain
)
6563 linkage_kind linkage
;
6565 fn
= instantiate_type (type
, fns
, tf_none
);
6566 if (fn
== error_mark_node
)
6567 return error_mark_node
;
6569 if (value_dependent_expression_p (fn
))
6572 fn_no_ptr
= strip_fnptr_conv (fn
);
6573 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6574 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6575 if (BASELINK_P (fn_no_ptr
))
6576 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6578 /* [temp.arg.nontype]/1
6580 A template-argument for a non-type, non-template template-parameter
6583 -- the address of an object or function with external [C++11: or
6584 internal] linkage. */
6586 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6587 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6589 if (complain
& tf_error
)
6591 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6592 error_at (loc
, "%qE is not a valid template argument for type %qT",
6594 if (TYPE_PTR_P (type
))
6595 inform (loc
, "it must be the address of a function "
6596 "with external linkage");
6598 inform (loc
, "it must be the name of a function with "
6599 "external linkage");
6604 linkage
= decl_linkage (fn_no_ptr
);
6605 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6607 if (complain
& tf_error
)
6609 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6610 if (cxx_dialect
>= cxx11
)
6611 error_at (loc
, "%qE is not a valid template argument for type "
6612 "%qT because %qD has no linkage",
6613 expr
, type
, fn_no_ptr
);
6615 error_at (loc
, "%qE is not a valid template argument for type "
6616 "%qT because %qD does not have external linkage",
6617 expr
, type
, fn_no_ptr
);
6623 if (TYPE_REF_P (type
))
6625 if (REFERENCE_REF_P (fn
))
6626 fn
= TREE_OPERAND (fn
, 0);
6628 fn
= build_address (fn
);
6630 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6631 fn
= build_nop (type
, fn
);
6636 /* Subroutine of convert_nontype_argument.
6637 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6638 Emit an error otherwise. */
6641 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6642 tsubst_flags_t complain
)
6644 tree orig_expr
= expr
;
6646 if (null_ptr_cst_p (expr
))
6648 if (TREE_CODE (expr
) == PTRMEM_CST
6649 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6650 PTRMEM_CST_CLASS (expr
)))
6652 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6654 if (processing_template_decl
6655 && TREE_CODE (expr
) == ADDR_EXPR
6656 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6658 if (complain
& tf_error
)
6660 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6661 error_at (loc
, "%qE is not a valid template argument for type %qT",
6663 if (TREE_CODE (expr
) != PTRMEM_CST
)
6664 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6666 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6671 /* Returns TRUE iff the address of OP is value-dependent.
6673 14.6.2.4 [temp.dep.temp]:
6674 A non-integral non-type template-argument is dependent if its type is
6675 dependent or it has either of the following forms
6678 and contains a nested-name-specifier which specifies a class-name that
6679 names a dependent type.
6681 We generalize this to just say that the address of a member of a
6682 dependent class is value-dependent; the above doesn't cover the
6683 address of a static data member named with an unqualified-id. */
6686 has_value_dependent_address (tree op
)
6688 STRIP_ANY_LOCATION_WRAPPER (op
);
6690 /* We could use get_inner_reference here, but there's no need;
6691 this is only relevant for template non-type arguments, which
6692 can only be expressed as &id-expression. */
6695 tree ctx
= CP_DECL_CONTEXT (op
);
6696 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6703 /* The next set of functions are used for providing helpful explanatory
6704 diagnostics for failed overload resolution. Their messages should be
6705 indented by two spaces for consistency with the messages in
6709 unify_success (bool /*explain_p*/)
6714 /* Other failure functions should call this one, to provide a single function
6715 for setting a breakpoint on. */
6718 unify_invalid (bool /*explain_p*/)
6724 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6727 inform (input_location
,
6728 " couldn%'t deduce template parameter %qD", parm
);
6729 return unify_invalid (explain_p
);
6733 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6736 inform (input_location
,
6737 " types %qT and %qT have incompatible cv-qualifiers",
6739 return unify_invalid (explain_p
);
6743 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6746 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6747 return unify_invalid (explain_p
);
6751 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6754 inform (input_location
,
6755 " template parameter %qD is not a parameter pack, but "
6758 return unify_invalid (explain_p
);
6762 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6765 inform (input_location
,
6766 " template argument %qE does not match "
6767 "pointer-to-member constant %qE",
6769 return unify_invalid (explain_p
);
6773 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6776 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6777 return unify_invalid (explain_p
);
6781 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6784 inform (input_location
,
6785 " inconsistent parameter pack deduction with %qT and %qT",
6787 return unify_invalid (explain_p
);
6791 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6796 inform (input_location
,
6797 " deduced conflicting types for parameter %qT (%qT and %qT)",
6798 parm
, first
, second
);
6800 inform (input_location
,
6801 " deduced conflicting values for non-type parameter "
6802 "%qE (%qE and %qE)", parm
, first
, second
);
6804 return unify_invalid (explain_p
);
6808 unify_vla_arg (bool explain_p
, tree arg
)
6811 inform (input_location
,
6812 " variable-sized array type %qT is not "
6813 "a valid template argument",
6815 return unify_invalid (explain_p
);
6819 unify_method_type_error (bool explain_p
, tree arg
)
6822 inform (input_location
,
6823 " member function type %qT is not a valid template argument",
6825 return unify_invalid (explain_p
);
6829 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6834 inform_n (input_location
, wanted
,
6835 " candidate expects at least %d argument, %d provided",
6836 " candidate expects at least %d arguments, %d provided",
6839 inform_n (input_location
, wanted
,
6840 " candidate expects %d argument, %d provided",
6841 " candidate expects %d arguments, %d provided",
6844 return unify_invalid (explain_p
);
6848 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6850 return unify_arity (explain_p
, have
, wanted
);
6854 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6855 bool least_p
= false)
6857 return unify_arity (explain_p
, have
, wanted
, least_p
);
6861 unify_arg_conversion (bool explain_p
, tree to_type
,
6862 tree from_type
, tree arg
)
6865 inform (cp_expr_loc_or_input_loc (arg
),
6866 " cannot convert %qE (type %qT) to type %qT",
6867 arg
, from_type
, to_type
);
6868 return unify_invalid (explain_p
);
6872 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6873 tree parm
, tree arg
)
6878 case tbr_ambiguous_baseclass
:
6879 inform (input_location
, " %qT is an ambiguous base class of %qT",
6883 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6886 return unify_invalid (explain_p
);
6890 unify_inconsistent_template_template_parameters (bool explain_p
)
6893 inform (input_location
,
6894 " template parameters of a template template argument are "
6895 "inconsistent with other deduced template arguments");
6896 return unify_invalid (explain_p
);
6900 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6903 inform (input_location
,
6904 " cannot deduce a template for %qT from non-template type %qT",
6906 return unify_invalid (explain_p
);
6910 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6913 inform (input_location
,
6914 " template argument %qE does not match %qE", arg
, parm
);
6915 return unify_invalid (explain_p
);
6918 /* True if T is a C++20 template parameter object to store the argument for a
6919 template parameter of class type. */
6922 template_parm_object_p (const_tree t
)
6924 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6925 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6928 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6929 argument for TYPE, points to an unsuitable object.
6931 Also adjust the type of the index in C++20 array subobject references. */
6934 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6936 switch (TREE_CODE (expr
))
6939 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6943 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6948 unsigned i
; tree elt
;
6949 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6950 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6957 tree decl
= TREE_OPERAND (expr
, 0);
6959 if (cxx_dialect
>= cxx20
)
6960 while (TREE_CODE (decl
) == COMPONENT_REF
6961 || TREE_CODE (decl
) == ARRAY_REF
)
6963 tree
&op
= TREE_OPERAND (decl
, 1);
6964 if (TREE_CODE (decl
) == ARRAY_REF
6965 && TREE_CODE (op
) == INTEGER_CST
)
6966 /* Canonicalize array offsets to ptrdiff_t; how they were
6967 written doesn't matter for subobject identity. */
6968 op
= fold_convert (ptrdiff_type_node
, op
);
6969 decl
= TREE_OPERAND (decl
, 0);
6974 if (complain
& tf_error
)
6975 error_at (cp_expr_loc_or_input_loc (expr
),
6976 "%qE is not a valid template argument of type %qT "
6977 "because %qE is not a variable", expr
, type
, decl
);
6980 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6982 if (complain
& tf_error
)
6983 error_at (cp_expr_loc_or_input_loc (expr
),
6984 "%qE is not a valid template argument of type %qT "
6985 "in C++98 because %qD does not have external linkage",
6989 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6990 && decl_linkage (decl
) == lk_none
)
6992 if (complain
& tf_error
)
6993 error_at (cp_expr_loc_or_input_loc (expr
),
6994 "%qE is not a valid template argument of type %qT "
6995 "because %qD has no linkage", expr
, type
, decl
);
6998 /* C++17: For a non-type template-parameter of reference or pointer
6999 type, the value of the constant expression shall not refer to (or
7000 for a pointer type, shall not be the address of):
7001 * a subobject (4.5),
7002 * a temporary object (15.2),
7003 * a string literal (5.13.5),
7004 * the result of a typeid expression (8.2.8), or
7005 * a predefined __func__ variable (11.4.1). */
7006 else if (DECL_ARTIFICIAL (decl
))
7008 if (complain
& tf_error
)
7009 error ("the address of %qD is not a valid template argument",
7013 else if (cxx_dialect
< cxx20
7014 && !(same_type_ignoring_top_level_qualifiers_p
7015 (strip_array_types (TREE_TYPE (type
)),
7016 strip_array_types (TREE_TYPE (decl
)))))
7018 if (complain
& tf_error
)
7019 error ("the address of the %qT subobject of %qD is not a "
7020 "valid template argument", TREE_TYPE (type
), decl
);
7023 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7025 if (complain
& tf_error
)
7026 error ("the address of %qD is not a valid template argument "
7027 "because it does not have static storage duration",
7035 if (!INDIRECT_TYPE_P (type
))
7036 /* We're only concerned about pointers and references here. */;
7037 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7038 /* Null pointer values are OK in C++11. */;
7043 if (complain
& tf_error
)
7044 error ("%qD is not a valid template argument "
7045 "because %qD is a variable, not the address of "
7046 "a variable", expr
, expr
);
7051 if (complain
& tf_error
)
7052 error ("%qE is not a valid template argument for %qT "
7053 "because it is not the address of a variable",
7063 /* The template arguments corresponding to template parameter objects of types
7064 that contain pointers to members. */
7066 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7068 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7069 template argument EXPR. */
7072 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7074 if (TREE_CODE (expr
) == TARGET_EXPR
)
7075 expr
= TARGET_EXPR_INITIAL (expr
);
7077 if (!TREE_CONSTANT (expr
))
7079 if ((complain
& tf_error
)
7080 && require_rvalue_constant_expression (expr
))
7081 cxx_constant_value (expr
);
7082 return error_mark_node
;
7084 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7085 return error_mark_node
;
7087 tree name
= mangle_template_parm_object (expr
);
7088 tree decl
= get_global_binding (name
);
7092 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7093 decl
= create_temporary_var (type
);
7094 DECL_CONTEXT (decl
) = NULL_TREE
;
7095 TREE_STATIC (decl
) = true;
7096 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7097 TREE_READONLY (decl
) = true;
7098 DECL_NAME (decl
) = name
;
7099 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7100 comdat_linkage (decl
);
7102 if (!zero_init_p (type
))
7104 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7105 lower_var_init before we're done mangling. So store the original
7107 tree copy
= unshare_constructor (expr
);
7108 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7111 pushdecl_top_level_and_finish (decl
, expr
);
7116 /* Return the actual template argument corresponding to template parameter
7120 tparm_object_argument (tree var
)
7122 if (zero_init_p (TREE_TYPE (var
)))
7123 return DECL_INITIAL (var
);
7124 return *(tparm_obj_values
->get (var
));
7127 /* Attempt to convert the non-type template parameter EXPR to the
7128 indicated TYPE. If the conversion is successful, return the
7129 converted value. If the conversion is unsuccessful, return
7130 NULL_TREE if we issued an error message, or error_mark_node if we
7131 did not. We issue error messages for out-and-out bad template
7132 parameters, but not simply because the conversion failed, since we
7133 might be just trying to do argument deduction. Both TYPE and EXPR
7134 must be non-dependent.
7136 The conversion follows the special rules described in
7137 [temp.arg.nontype], and it is much more strict than an implicit
7140 This function is called twice for each template argument (see
7141 lookup_template_class for a more accurate description of this
7142 problem). This means that we need to handle expressions which
7143 are not valid in a C++ source, but can be created from the
7144 first call (for instance, casts to perform conversions). These
7145 hacks can go away after we fix the double coercion problem. */
7148 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7151 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7153 /* Detect immediately string literals as invalid non-type argument.
7154 This special-case is not needed for correctness (we would easily
7155 catch this later), but only to provide better diagnostic for this
7156 common user mistake. As suggested by DR 100, we do not mention
7157 linkage issues in the diagnostic as this is not the point. */
7158 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7160 if (complain
& tf_error
)
7161 error ("%qE is not a valid template argument for type %qT "
7162 "because string literals can never be used in this context",
7167 /* Add the ADDR_EXPR now for the benefit of
7168 value_dependent_expression_p. */
7169 if (TYPE_PTROBV_P (type
)
7170 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7172 expr
= decay_conversion (expr
, complain
);
7173 if (expr
== error_mark_node
)
7174 return error_mark_node
;
7177 /* If we are in a template, EXPR may be non-dependent, but still
7178 have a syntactic, rather than semantic, form. For example, EXPR
7179 might be a SCOPE_REF, rather than the VAR_DECL to which the
7180 SCOPE_REF refers. Preserving the qualifying scope is necessary
7181 so that access checking can be performed when the template is
7182 instantiated -- but here we need the resolved form so that we can
7183 convert the argument. */
7184 bool non_dep
= false;
7185 if (TYPE_REF_OBJ_P (type
)
7186 && has_value_dependent_address (expr
))
7187 /* If we want the address and it's value-dependent, don't fold. */;
7188 else if (processing_template_decl
7189 && is_nondependent_constant_expression (expr
))
7191 if (error_operand_p (expr
))
7192 return error_mark_node
;
7193 expr_type
= TREE_TYPE (expr
);
7195 /* If the argument is non-dependent, perform any conversions in
7196 non-dependent context as well. */
7197 processing_template_decl_sentinel
s (non_dep
);
7199 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7201 const bool val_dep_p
= value_dependent_expression_p (expr
);
7203 expr
= canonicalize_expr_argument (expr
, complain
);
7205 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7206 to a non-type argument of "nullptr". */
7207 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7208 expr
= fold_simple (convert (type
, expr
));
7210 /* In C++11, integral or enumeration non-type template arguments can be
7211 arbitrary constant expressions. Pointer and pointer to
7212 member arguments can be general constant expressions that evaluate
7213 to a null value, but otherwise still need to be of a specific form. */
7214 if (cxx_dialect
>= cxx11
)
7216 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7217 /* A PTRMEM_CST is already constant, and a valid template
7218 argument for a parameter of pointer to member type, we just want
7219 to leave it in that form rather than lower it to a
7221 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7222 || cxx_dialect
>= cxx17
)
7224 /* C++17: A template-argument for a non-type template-parameter shall
7225 be a converted constant expression (8.20) of the type of the
7226 template-parameter. */
7227 expr
= build_converted_constant_expr (type
, expr
, complain
);
7228 if (expr
== error_mark_node
)
7229 /* Make sure we return NULL_TREE only if we have really issued
7230 an error, as described above. */
7231 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7232 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7234 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7237 expr
= maybe_constant_value (expr
, NULL_TREE
,
7238 /*manifestly_const_eval=*/true);
7239 expr
= convert_from_reference (expr
);
7241 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7243 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7244 /*manifestly_const_eval=*/true);
7245 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7246 : null_member_pointer_value_p (folded
))
7251 if (TYPE_REF_P (type
))
7252 expr
= mark_lvalue_use (expr
);
7254 expr
= mark_rvalue_use (expr
);
7256 /* HACK: Due to double coercion, we can get a
7257 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7258 which is the tree that we built on the first call (see
7259 below when coercing to reference to object or to reference to
7260 function). We just strip everything and get to the arg.
7261 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7263 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7265 tree probe_type
, probe
= expr
;
7266 if (REFERENCE_REF_P (probe
))
7267 probe
= TREE_OPERAND (probe
, 0);
7268 probe_type
= TREE_TYPE (probe
);
7269 if (TREE_CODE (probe
) == NOP_EXPR
)
7271 /* ??? Maybe we could use convert_from_reference here, but we
7272 would need to relax its constraints because the NOP_EXPR
7273 could actually change the type to something more cv-qualified,
7274 and this is not folded by convert_from_reference. */
7275 tree addr
= TREE_OPERAND (probe
, 0);
7276 if (TYPE_REF_P (probe_type
)
7277 && TREE_CODE (addr
) == ADDR_EXPR
7278 && TYPE_PTR_P (TREE_TYPE (addr
))
7279 && (same_type_ignoring_top_level_qualifiers_p
7280 (TREE_TYPE (probe_type
),
7281 TREE_TYPE (TREE_TYPE (addr
)))))
7283 expr
= TREE_OPERAND (addr
, 0);
7284 expr_type
= TREE_TYPE (probe_type
);
7289 /* [temp.arg.nontype]/5, bullet 1
7291 For a non-type template-parameter of integral or enumeration type,
7292 integral promotions (_conv.prom_) and integral conversions
7293 (_conv.integral_) are applied. */
7294 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7295 || TREE_CODE (type
) == REAL_TYPE
)
7297 if (cxx_dialect
< cxx11
)
7299 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7300 t
= maybe_constant_value (t
);
7301 if (t
!= error_mark_node
)
7305 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7306 return error_mark_node
;
7308 /* Notice that there are constant expressions like '4 % 0' which
7309 do not fold into integer constants. */
7310 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7312 if (complain
& tf_error
)
7314 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7315 if (!require_potential_constant_expression (expr
))
7316 expr
= error_mark_node
;
7318 expr
= cxx_constant_value (expr
);
7319 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7320 inform (loc
, "in template argument for type %qT", type
);
7321 if (expr
== error_mark_node
)
7323 /* else cxx_constant_value complained but gave us
7324 a real constant, so go ahead. */
7325 if (!CONSTANT_CLASS_P (expr
))
7327 /* Some assemble time constant expressions like
7328 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7329 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7330 as we can emit them into .rodata initializers of
7331 variables, yet they can't fold into an INTEGER_CST at
7332 compile time. Refuse them here. */
7333 gcc_checking_assert (reduced_constant_expression_p (expr
));
7334 error_at (loc
, "template argument %qE for type %qT not "
7335 "a compile-time constant", expr
, type
);
7343 /* Avoid typedef problems. */
7344 if (TREE_TYPE (expr
) != type
)
7345 expr
= fold_convert (type
, expr
);
7347 /* [temp.arg.nontype]/5, bullet 2
7349 For a non-type template-parameter of type pointer to object,
7350 qualification conversions (_conv.qual_) and the array-to-pointer
7351 conversion (_conv.array_) are applied. */
7352 else if (TYPE_PTROBV_P (type
))
7354 tree decayed
= expr
;
7356 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7357 decay_conversion or an explicit cast. If it's a problematic cast,
7358 we'll complain about it below. */
7359 if (TREE_CODE (expr
) == NOP_EXPR
)
7363 if (TREE_CODE (probe
) == ADDR_EXPR
7364 && TYPE_PTR_P (TREE_TYPE (probe
)))
7367 expr_type
= TREE_TYPE (expr
);
7371 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7373 A template-argument for a non-type, non-template template-parameter
7374 shall be one of: [...]
7376 -- the name of a non-type template-parameter;
7377 -- the address of an object or function with external linkage, [...]
7378 expressed as "& id-expression" where the & is optional if the name
7379 refers to a function or array, or if the corresponding
7380 template-parameter is a reference.
7382 Here, we do not care about functions, as they are invalid anyway
7383 for a parameter of type pointer-to-object. */
7386 /* Non-type template parameters are OK. */
7388 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7389 /* Null pointer values are OK in C++11. */;
7390 else if (TREE_CODE (expr
) != ADDR_EXPR
7391 && !INDIRECT_TYPE_P (expr_type
))
7392 /* Other values, like integer constants, might be valid
7393 non-type arguments of some other type. */
7394 return error_mark_node
;
7395 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7400 expr
= perform_qualification_conversions (type
, expr
);
7401 if (expr
== error_mark_node
)
7402 return error_mark_node
;
7404 /* [temp.arg.nontype]/5, bullet 3
7406 For a non-type template-parameter of type reference to object, no
7407 conversions apply. The type referred to by the reference may be more
7408 cv-qualified than the (otherwise identical) type of the
7409 template-argument. The template-parameter is bound directly to the
7410 template-argument, which must be an lvalue. */
7411 else if (TYPE_REF_OBJ_P (type
))
7413 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7415 return error_mark_node
;
7417 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7419 if (complain
& tf_error
)
7420 error ("%qE is not a valid template argument for type %qT "
7421 "because of conflicts in cv-qualification", expr
, type
);
7425 if (!lvalue_p (expr
))
7427 if (complain
& tf_error
)
7428 error ("%qE is not a valid template argument for type %qT "
7429 "because it is not an lvalue", expr
, type
);
7433 /* [temp.arg.nontype]/1
7435 A template-argument for a non-type, non-template template-parameter
7436 shall be one of: [...]
7438 -- the address of an object or function with external linkage. */
7439 if (INDIRECT_REF_P (expr
)
7440 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7442 expr
= TREE_OPERAND (expr
, 0);
7445 if (complain
& tf_error
)
7446 error ("%q#D is not a valid template argument for type %qT "
7447 "because a reference variable does not have a constant "
7448 "address", expr
, type
);
7453 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7454 /* OK, dependent reference. We don't want to ask whether a DECL is
7455 itself value-dependent, since what we want here is its address. */;
7458 expr
= build_address (expr
);
7460 if (invalid_tparm_referent_p (type
, expr
, complain
))
7464 if (!same_type_p (type
, TREE_TYPE (expr
)))
7465 expr
= build_nop (type
, expr
);
7467 /* [temp.arg.nontype]/5, bullet 4
7469 For a non-type template-parameter of type pointer to function, only
7470 the function-to-pointer conversion (_conv.func_) is applied. If the
7471 template-argument represents a set of overloaded functions (or a
7472 pointer to such), the matching function is selected from the set
7474 else if (TYPE_PTRFN_P (type
))
7476 /* If the argument is a template-id, we might not have enough
7477 context information to decay the pointer. */
7478 if (!type_unknown_p (expr_type
))
7480 expr
= decay_conversion (expr
, complain
);
7481 if (expr
== error_mark_node
)
7482 return error_mark_node
;
7485 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7486 /* Null pointer values are OK in C++11. */
7487 return perform_qualification_conversions (type
, expr
);
7489 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7490 if (!expr
|| expr
== error_mark_node
)
7493 /* [temp.arg.nontype]/5, bullet 5
7495 For a non-type template-parameter of type reference to function, no
7496 conversions apply. If the template-argument represents a set of
7497 overloaded functions, the matching function is selected from the set
7499 else if (TYPE_REFFN_P (type
))
7501 if (TREE_CODE (expr
) == ADDR_EXPR
)
7503 if (complain
& tf_error
)
7505 error ("%qE is not a valid template argument for type %qT "
7506 "because it is a pointer", expr
, type
);
7507 inform (input_location
, "try using %qE instead",
7508 TREE_OPERAND (expr
, 0));
7513 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7514 if (!expr
|| expr
== error_mark_node
)
7517 /* [temp.arg.nontype]/5, bullet 6
7519 For a non-type template-parameter of type pointer to member function,
7520 no conversions apply. If the template-argument represents a set of
7521 overloaded member functions, the matching member function is selected
7522 from the set (_over.over_). */
7523 else if (TYPE_PTRMEMFUNC_P (type
))
7525 expr
= instantiate_type (type
, expr
, tf_none
);
7526 if (expr
== error_mark_node
)
7527 return error_mark_node
;
7529 /* [temp.arg.nontype] bullet 1 says the pointer to member
7530 expression must be a pointer-to-member constant. */
7532 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7535 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7536 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7537 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7538 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7540 /* [temp.arg.nontype]/5, bullet 7
7542 For a non-type template-parameter of type pointer to data member,
7543 qualification conversions (_conv.qual_) are applied. */
7544 else if (TYPE_PTRDATAMEM_P (type
))
7546 /* [temp.arg.nontype] bullet 1 says the pointer to member
7547 expression must be a pointer-to-member constant. */
7549 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7552 expr
= perform_qualification_conversions (type
, expr
);
7553 if (expr
== error_mark_node
)
7556 else if (NULLPTR_TYPE_P (type
))
7558 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7560 if (complain
& tf_error
)
7561 error ("%qE is not a valid template argument for type %qT "
7562 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7567 else if (CLASS_TYPE_P (type
))
7569 /* Replace the argument with a reference to the corresponding template
7570 parameter object. */
7572 expr
= get_template_parm_object (expr
, complain
);
7573 if (expr
== error_mark_node
)
7576 /* A template non-type parameter must be one of the above. */
7580 /* Sanity check: did we actually convert the argument to the
7582 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7583 (type
, TREE_TYPE (expr
)));
7584 return convert_from_reference (expr
);
7587 /* Subroutine of coerce_template_template_parms, which returns 1 if
7588 PARM_PARM and ARG_PARM match using the rule for the template
7589 parameters of template template parameters. Both PARM and ARG are
7590 template parameters; the rest of the arguments are the same as for
7591 coerce_template_template_parms.
7594 coerce_template_template_parm (tree parm
,
7596 tsubst_flags_t complain
,
7600 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7601 || parm
== NULL_TREE
|| error_operand_p (parm
))
7604 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7607 switch (TREE_CODE (parm
))
7610 /* We encounter instantiations of templates like
7611 template <template <template <class> class> class TT>
7614 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7615 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7617 if (!coerce_template_template_parms
7618 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7624 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7625 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7626 /* Argument is a parameter pack but parameter is not. */
7631 /* The tsubst call is used to handle cases such as
7633 template <int> class C {};
7634 template <class T, template <T> class TT> class D {};
7637 i.e. the parameter list of TT depends on earlier parameters. */
7638 if (!uses_template_parms (TREE_TYPE (arg
)))
7640 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7641 if (!uses_template_parms (t
)
7642 && !same_type_p (t
, TREE_TYPE (arg
)))
7646 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7647 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7648 /* Argument is a parameter pack but parameter is not. */
7660 /* Coerce template argument list ARGLIST for use with template
7661 template-parameter TEMPL. */
7664 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7665 tsubst_flags_t complain
)
7667 /* Consider an example where a template template parameter declared as
7669 template <class T, class U = std::allocator<T> > class TT
7671 The template parameter level of T and U are one level larger than
7672 of TT. To proper process the default argument of U, say when an
7673 instantiation `TT<int>' is seen, we need to build the full
7674 arguments containing {int} as the innermost level. Outer levels,
7675 available when not appearing as default template argument, can be
7676 obtained from the arguments of the enclosing template.
7678 Suppose that TT is later substituted with std::vector. The above
7679 instantiation is `TT<int, std::allocator<T> >' with TT at
7680 level 1, and T at level 2, while the template arguments at level 1
7681 becomes {std::vector} and the inner level 2 is {int}. */
7683 tree outer
= DECL_CONTEXT (templ
);
7685 outer
= generic_targs_for (outer
);
7686 else if (current_template_parms
)
7688 /* This is an argument of the current template, so we haven't set
7689 DECL_CONTEXT yet. */
7690 tree relevant_template_parms
;
7692 /* Parameter levels that are greater than the level of the given
7693 template template parm are irrelevant. */
7694 relevant_template_parms
= current_template_parms
;
7695 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7696 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7697 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7699 outer
= template_parms_to_args (relevant_template_parms
);
7703 arglist
= add_to_template_args (outer
, arglist
);
7705 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7706 return coerce_template_parms (parmlist
, arglist
, templ
,
7708 /*require_all_args=*/true,
7709 /*use_default_args=*/true);
7712 /* A cache of template template parameters with match-all default
7714 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7716 /* T is a bound template template-parameter. Copy its arguments into default
7717 arguments of the template template-parameter's template parameters. */
7720 add_defaults_to_ttp (tree otmpl
)
7722 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7725 tree ntmpl
= copy_node (otmpl
);
7727 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7728 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7729 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7730 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7731 TYPE_NAME (ntype
) = ntmpl
;
7732 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7734 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7735 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7736 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7737 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7739 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7740 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7741 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7742 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7743 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7745 tree o
= TREE_VEC_ELT (vec
, i
);
7746 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7748 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7749 TREE_PURPOSE (n
) = any_targ_node
;
7753 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7757 /* ARG is a bound potential template template-argument, and PARGS is a list
7758 of arguments for the corresponding template template-parameter. Adjust
7759 PARGS as appropriate for application to ARG's template, and if ARG is a
7760 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7761 arguments to the template template parameter. */
7764 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7766 ++processing_template_decl
;
7767 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7768 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7770 /* When comparing two template template-parameters in partial ordering,
7771 rewrite the one currently being used as an argument to have default
7772 arguments for all parameters. */
7773 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7774 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7775 if (pargs
!= error_mark_node
)
7776 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7777 TYPE_TI_ARGS (arg
));
7782 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7783 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7784 /*require_all*/true,
7785 /*use_default*/true);
7787 --processing_template_decl
;
7791 /* Subroutine of unify for the case when PARM is a
7792 BOUND_TEMPLATE_TEMPLATE_PARM. */
7795 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7798 tree parmvec
= TYPE_TI_ARGS (parm
);
7799 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7801 /* The template template parm might be variadic and the argument
7802 not, so flatten both argument lists. */
7803 parmvec
= expand_template_argument_pack (parmvec
);
7804 argvec
= expand_template_argument_pack (argvec
);
7808 /* In keeping with P0522R0, adjust P's template arguments
7809 to apply to A's template; then flatten it again. */
7810 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7811 nparmvec
= expand_template_argument_pack (nparmvec
);
7813 if (unify (tparms
, targs
, nparmvec
, argvec
,
7814 UNIFY_ALLOW_NONE
, explain_p
))
7817 /* If the P0522 adjustment eliminated a pack expansion, deduce
7820 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7821 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7822 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7827 /* Deduce arguments T, i from TT<T> or TT<i>.
7828 We check each element of PARMVEC and ARGVEC individually
7829 rather than the whole TREE_VEC since they can have
7830 different number of elements, which is allowed under N2555. */
7832 int len
= TREE_VEC_LENGTH (parmvec
);
7834 /* Check if the parameters end in a pack, making them
7836 int parm_variadic_p
= 0;
7838 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7839 parm_variadic_p
= 1;
7841 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7842 /* If the template argument list of P contains a pack
7843 expansion that is not the last template argument, the
7844 entire template argument list is a non-deduced
7846 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7847 return unify_success (explain_p
);
7849 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7850 return unify_too_few_arguments (explain_p
,
7851 TREE_VEC_LENGTH (argvec
), len
);
7853 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7854 if (unify (tparms
, targs
,
7855 TREE_VEC_ELT (parmvec
, i
),
7856 TREE_VEC_ELT (argvec
, i
),
7857 UNIFY_ALLOW_NONE
, explain_p
))
7861 && unify_pack_expansion (tparms
, targs
,
7864 /*subr=*/true, explain_p
))
7871 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7872 template template parameters. Both PARM_PARMS and ARG_PARMS are
7873 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7876 Consider the example:
7877 template <class T> class A;
7878 template<template <class U> class TT> class B;
7880 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7881 the parameters to A, and OUTER_ARGS contains A. */
7884 coerce_template_template_parms (tree parm_parms
,
7886 tsubst_flags_t complain
,
7890 int nparms
, nargs
, i
;
7894 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7895 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7897 nparms
= TREE_VEC_LENGTH (parm_parms
);
7898 nargs
= TREE_VEC_LENGTH (arg_parms
);
7902 /* P0522R0: A template template-parameter P is at least as specialized as
7903 a template template-argument A if, given the following rewrite to two
7904 function templates, the function template corresponding to P is at
7905 least as specialized as the function template corresponding to A
7906 according to the partial ordering rules for function templates
7907 ([temp.func.order]). Given an invented class template X with the
7908 template parameter list of A (including default arguments):
7910 * Each of the two function templates has the same template parameters,
7911 respectively, as P or A.
7913 * Each function template has a single function parameter whose type is
7914 a specialization of X with template arguments corresponding to the
7915 template parameters from the respective function template where, for
7916 each template parameter PP in the template parameter list of the
7917 function template, a corresponding template argument AA is formed. If
7918 PP declares a parameter pack, then AA is the pack expansion
7919 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7921 If the rewrite produces an invalid type, then P is not at least as
7922 specialized as A. */
7924 /* So coerce P's args to apply to A's parms, and then deduce between A's
7925 args and the converted args. If that succeeds, A is at least as
7926 specialized as P, so they match.*/
7927 tree pargs
= template_parms_level_to_args (parm_parms
);
7928 pargs
= add_outermost_template_args (outer_args
, pargs
);
7929 ++processing_template_decl
;
7930 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7931 /*require_all*/true, /*use_default*/true);
7932 --processing_template_decl
;
7933 if (pargs
!= error_mark_node
)
7935 tree targs
= make_tree_vec (nargs
);
7936 tree aargs
= template_parms_level_to_args (arg_parms
);
7937 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7943 /* Determine whether we have a parameter pack at the end of the
7944 template template parameter's template parameter list. */
7945 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7947 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7949 if (error_operand_p (parm
))
7952 switch (TREE_CODE (parm
))
7956 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7961 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7971 && !(variadic_p
&& nargs
>= nparms
- 1))
7974 /* Check all of the template parameters except the parameter pack at
7975 the end (if any). */
7976 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7978 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7979 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7982 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7983 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7985 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7993 /* Check each of the template parameters in the template
7994 argument against the template parameter pack at the end of
7995 the template template parameter. */
7996 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7999 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8001 for (; i
< nargs
; ++i
)
8003 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8006 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8008 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8017 /* Verifies that the deduced template arguments (in TARGS) for the
8018 template template parameters (in TPARMS) represent valid bindings,
8019 by comparing the template parameter list of each template argument
8020 to the template parameter list of its corresponding template
8021 template parameter, in accordance with DR150. This
8022 routine can only be called after all template arguments have been
8023 deduced. It will return TRUE if all of the template template
8024 parameter bindings are okay, FALSE otherwise. */
8026 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8028 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8031 /* We're dealing with template parms in this process. */
8032 ++processing_template_decl
;
8034 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8036 for (i
= 0; i
< ntparms
; ++i
)
8038 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8039 tree targ
= TREE_VEC_ELT (targs
, i
);
8041 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8043 tree packed_args
= NULL_TREE
;
8046 if (ARGUMENT_PACK_P (targ
))
8048 /* Look inside the argument pack. */
8049 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8050 len
= TREE_VEC_LENGTH (packed_args
);
8053 for (idx
= 0; idx
< len
; ++idx
)
8055 tree targ_parms
= NULL_TREE
;
8058 /* Extract the next argument from the argument
8060 targ
= TREE_VEC_ELT (packed_args
, idx
);
8062 if (PACK_EXPANSION_P (targ
))
8063 /* Look at the pattern of the pack expansion. */
8064 targ
= PACK_EXPANSION_PATTERN (targ
);
8066 /* Extract the template parameters from the template
8068 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8069 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8070 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8071 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8073 /* Verify that we can coerce the template template
8074 parameters from the template argument to the template
8075 parameter. This requires an exact match. */
8077 && !coerce_template_template_parms
8078 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8093 --processing_template_decl
;
8097 /* Since type attributes aren't mangled, we need to strip them from
8098 template type arguments. */
8101 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8103 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8105 bool removed_attributes
= false;
8106 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8107 if (removed_attributes
8108 && (complain
& tf_warning
))
8109 warning (OPT_Wignored_attributes
,
8110 "ignoring attributes on template argument %qT", arg
);
8114 /* And from inside dependent non-type arguments like sizeof(Type). */
8117 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8119 if (!arg
|| arg
== error_mark_node
)
8121 bool removed_attributes
= false;
8122 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8123 if (removed_attributes
8124 && (complain
& tf_warning
))
8125 warning (OPT_Wignored_attributes
,
8126 "ignoring attributes in template argument %qE", arg
);
8130 // A template declaration can be substituted for a constrained
8131 // template template parameter only when the argument is more
8132 // constrained than the parameter.
8134 is_compatible_template_arg (tree parm
, tree arg
)
8136 tree parm_cons
= get_constraints (parm
);
8138 /* For now, allow constrained template template arguments
8139 and unconstrained template template parameters. */
8140 if (parm_cons
== NULL_TREE
)
8143 /* If the template parameter is constrained, we need to rewrite its
8144 constraints in terms of the ARG's template parameters. This ensures
8145 that all of the template parameter types will have the same depth.
8147 Note that this is only valid when coerce_template_template_parm is
8148 true for the innermost template parameters of PARM and ARG. In other
8149 words, because coercion is successful, this conversion will be valid. */
8150 tree new_args
= NULL_TREE
;
8153 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8154 new_args
= template_parms_level_to_args (aparms
);
8155 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8156 tf_none
, NULL_TREE
);
8157 if (parm_cons
== error_mark_node
)
8161 return weakly_subsumes (parm_cons
, new_args
, arg
);
8164 // Convert a placeholder argument into a binding to the original
8165 // parameter. The original parameter is saved as the TREE_TYPE of
8168 convert_wildcard_argument (tree parm
, tree arg
)
8170 TREE_TYPE (arg
) = parm
;
8174 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8175 because one of them is dependent. But we need to represent the
8176 conversion for the benefit of cp_tree_equal. */
8179 maybe_convert_nontype_argument (tree type
, tree arg
)
8181 /* Auto parms get no conversion. */
8182 if (type_uses_auto (type
))
8184 /* We don't need or want to add this conversion now if we're going to use the
8185 argument for deduction. */
8186 if (value_dependent_expression_p (arg
))
8189 type
= cv_unqualified (type
);
8190 tree argtype
= TREE_TYPE (arg
);
8191 if (same_type_p (type
, argtype
))
8194 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8195 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8199 /* Convert the indicated template ARG as necessary to match the
8200 indicated template PARM. Returns the converted ARG, or
8201 error_mark_node if the conversion was unsuccessful. Error and
8202 warning messages are issued under control of COMPLAIN. This
8203 conversion is for the Ith parameter in the parameter list. ARGS is
8204 the full set of template arguments deduced so far. */
8207 convert_template_argument (tree parm
,
8210 tsubst_flags_t complain
,
8216 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8218 if (parm
== error_mark_node
|| error_operand_p (arg
))
8219 return error_mark_node
;
8221 /* Trivially convert placeholders. */
8222 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8223 return convert_wildcard_argument (parm
, arg
);
8225 if (arg
== any_targ_node
)
8228 if (TREE_CODE (arg
) == TREE_LIST
8229 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8231 /* The template argument was the name of some
8232 member function. That's usually
8233 invalid, but static members are OK. In any
8234 case, grab the underlying fields/functions
8235 and issue an error later if required. */
8236 TREE_TYPE (arg
) = unknown_type_node
;
8241 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8242 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8243 || requires_tmpl_type
);
8245 /* When determining whether an argument pack expansion is a template,
8246 look at the pattern. */
8247 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8248 arg
= PACK_EXPANSION_PATTERN (arg
);
8250 /* Deal with an injected-class-name used as a template template arg. */
8251 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8253 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8254 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8256 if (cxx_dialect
>= cxx11
)
8257 /* OK under DR 1004. */;
8258 else if (complain
& tf_warning_or_error
)
8259 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8260 " used as template template argument", TYPE_NAME (arg
));
8261 else if (flag_pedantic_errors
)
8269 ((TREE_CODE (arg
) == TEMPLATE_DECL
8270 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8271 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8272 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8273 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8276 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8277 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8278 arg
= TYPE_STUB_DECL (arg
);
8280 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8282 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8283 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8285 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8287 if (complain
& tf_error
)
8288 error ("invalid use of destructor %qE as a type", orig_arg
);
8289 return error_mark_node
;
8292 permerror (input_location
,
8293 "to refer to a type member of a template parameter, "
8294 "use %<typename %E%>", orig_arg
);
8296 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8297 TREE_OPERAND (arg
, 1),
8303 if (is_type
!= requires_type
)
8307 if (complain
& tf_error
)
8309 error ("type/value mismatch at argument %d in template "
8310 "parameter list for %qD",
8314 /* The template argument is a type, but we're expecting
8316 inform (input_location
,
8317 " expected a constant of type %qT, got %qT",
8319 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8320 /* [temp.arg]/2: "In a template-argument, an ambiguity
8321 between a type-id and an expression is resolved to a
8322 type-id, regardless of the form of the corresponding
8323 template-parameter." So give the user a clue. */
8324 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8325 inform (input_location
, " ambiguous template argument "
8326 "for non-type template parameter is treated as "
8329 else if (requires_tmpl_type
)
8330 inform (input_location
,
8331 " expected a class template, got %qE", orig_arg
);
8333 inform (input_location
,
8334 " expected a type, got %qE", orig_arg
);
8337 return error_mark_node
;
8339 if (is_tmpl_type
^ requires_tmpl_type
)
8341 if (in_decl
&& (complain
& tf_error
))
8343 error ("type/value mismatch at argument %d in template "
8344 "parameter list for %qD",
8347 inform (input_location
,
8348 " expected a type, got %qT", DECL_NAME (arg
));
8350 inform (input_location
,
8351 " expected a class template, got %qT", orig_arg
);
8353 return error_mark_node
;
8356 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8357 /* We already did the appropriate conversion when packing args. */
8361 if (requires_tmpl_type
)
8363 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8364 /* The number of argument required is not known yet.
8365 Just accept it for now. */
8369 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8372 /* Strip alias templates that are equivalent to another
8374 arg
= get_underlying_template (arg
);
8375 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8377 if (coerce_template_template_parms (parmparm
, argparm
,
8383 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8385 if (val
!= error_mark_node
)
8387 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8388 val
= TREE_TYPE (val
);
8389 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8390 val
= make_pack_expansion (val
, complain
);
8395 if (in_decl
&& (complain
& tf_error
))
8397 error ("type/value mismatch at argument %d in "
8398 "template parameter list for %qD",
8400 inform (input_location
,
8401 " expected a template of type %qD, got %qT",
8405 val
= error_mark_node
;
8408 // Check that the constraints are compatible before allowing the
8410 if (val
!= error_mark_node
)
8411 if (!is_compatible_template_arg (parm
, arg
))
8413 if (in_decl
&& (complain
& tf_error
))
8415 error ("constraint mismatch at argument %d in "
8416 "template parameter list for %qD",
8418 inform (input_location
, " expected %qD but got %qD",
8421 val
= error_mark_node
;
8427 /* We only form one instance of each template specialization.
8428 Therefore, if we use a non-canonical variant (i.e., a
8429 typedef), any future messages referring to the type will use
8430 the typedef, which is confusing if those future uses do not
8431 themselves also use the typedef. */
8433 val
= canonicalize_type_argument (val
, complain
);
8437 tree t
= TREE_TYPE (parm
);
8439 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8440 > TMPL_ARGS_DEPTH (args
))
8441 /* We don't have enough levels of args to do any substitution. This
8442 can happen in the context of -fnew-ttp-matching. */;
8443 else if (tree a
= type_uses_auto (t
))
8445 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8446 if (t
== error_mark_node
)
8447 return error_mark_node
;
8450 t
= tsubst (t
, args
, complain
, in_decl
);
8452 if (invalid_nontype_parm_type_p (t
, complain
))
8453 return error_mark_node
;
8455 if (t
!= TREE_TYPE (parm
))
8456 t
= canonicalize_type_argument (t
, complain
);
8458 if (!type_dependent_expression_p (orig_arg
)
8459 && !uses_template_parms (t
))
8460 /* We used to call digest_init here. However, digest_init
8461 will report errors, which we don't want when complain
8462 is zero. More importantly, digest_init will try too
8463 hard to convert things: for example, `0' should not be
8464 converted to pointer type at this point according to
8465 the standard. Accepting this is not merely an
8466 extension, since deciding whether or not these
8467 conversions can occur is part of determining which
8468 function template to call, or whether a given explicit
8469 argument specification is valid. */
8470 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8473 val
= canonicalize_expr_argument (orig_arg
, complain
);
8474 val
= maybe_convert_nontype_argument (t
, val
);
8478 if (val
== NULL_TREE
)
8479 val
= error_mark_node
;
8480 else if (val
== error_mark_node
&& (complain
& tf_error
))
8481 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8482 "could not convert template argument %qE from %qT to %qT",
8483 orig_arg
, TREE_TYPE (orig_arg
), t
);
8485 if (INDIRECT_REF_P (val
))
8487 /* Reject template arguments that are references to built-in
8488 functions with no library fallbacks. */
8489 const_tree inner
= TREE_OPERAND (val
, 0);
8490 const_tree innertype
= TREE_TYPE (inner
);
8492 && TYPE_REF_P (innertype
)
8493 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8494 && TREE_OPERAND_LENGTH (inner
) > 0
8495 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8496 return error_mark_node
;
8499 if (TREE_CODE (val
) == SCOPE_REF
)
8501 /* Strip typedefs from the SCOPE_REF. */
8502 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8503 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8505 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8506 QUALIFIED_NAME_IS_TEMPLATE (val
));
8513 /* Coerces the remaining template arguments in INNER_ARGS (from
8514 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8515 Returns the coerced argument pack. PARM_IDX is the position of this
8516 parameter in the template parameter list. ARGS is the original
8517 template argument list. */
8519 coerce_template_parameter_pack (tree parms
,
8527 tsubst_flags_t complain
)
8529 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8530 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8533 tree packed_parms
= NULL_TREE
;
8535 if (arg_idx
> nargs
)
8538 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8540 /* When the template parameter is a non-type template parameter pack
8541 or template template parameter pack whose type or template
8542 parameters use parameter packs, we know exactly how many arguments
8543 we are looking for. Build a vector of the instantiated decls for
8544 these template parameters in PACKED_PARMS. */
8545 /* We can't use make_pack_expansion here because it would interpret a
8546 _DECL as a use rather than a declaration. */
8547 tree decl
= TREE_VALUE (parm
);
8548 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8549 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8550 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8551 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8553 TREE_VEC_LENGTH (args
)--;
8554 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8555 TREE_VEC_LENGTH (args
)++;
8557 if (packed_parms
== error_mark_node
)
8558 return error_mark_node
;
8560 /* If we're doing a partial instantiation of a member template,
8561 verify that all of the types used for the non-type
8562 template parameter pack are, in fact, valid for non-type
8563 template parameters. */
8565 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8567 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8568 for (j
= 0; j
< len
; ++j
)
8570 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8571 if (TREE_CODE (t
) == PARM_DECL
8572 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8573 return error_mark_node
;
8575 /* We don't know how many args we have yet, just
8576 use the unconverted ones for now. */
8580 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8582 /* Check if we have a placeholder pack, which indicates we're
8583 in the context of a introduction list. In that case we want
8584 to match this pack to the single placeholder. */
8585 else if (arg_idx
< nargs
8586 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8587 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8589 nargs
= arg_idx
+ 1;
8590 packed_args
= make_tree_vec (1);
8593 packed_args
= make_tree_vec (nargs
- arg_idx
);
8595 /* Convert the remaining arguments, which will be a part of the
8596 parameter pack "parm". */
8597 int first_pack_arg
= arg_idx
;
8598 for (; arg_idx
< nargs
; ++arg_idx
)
8600 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8601 tree actual_parm
= TREE_VALUE (parm
);
8602 int pack_idx
= arg_idx
- first_pack_arg
;
8606 /* Once we've packed as many args as we have types, stop. */
8607 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8609 else if (PACK_EXPANSION_P (arg
))
8610 /* We don't know how many args we have yet, just
8611 use the unconverted ones for now. */
8614 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8617 if (arg
== error_mark_node
)
8619 if (complain
& tf_error
)
8620 error ("template argument %d is invalid", arg_idx
+ 1);
8623 arg
= convert_template_argument (actual_parm
,
8624 arg
, new_args
, complain
, parm_idx
,
8626 if (arg
== error_mark_node
)
8628 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8631 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8632 && TREE_VEC_LENGTH (packed_args
) > 0)
8634 if (complain
& tf_error
)
8635 error ("wrong number of template arguments (%d, should be %d)",
8636 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8637 return error_mark_node
;
8640 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8641 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8642 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8645 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8646 TREE_CONSTANT (argument_pack
) = 1;
8649 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8651 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8652 TREE_VEC_LENGTH (packed_args
));
8653 return argument_pack
;
8656 /* Returns the number of pack expansions in the template argument vector
8660 pack_expansion_args_count (tree args
)
8665 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8667 tree elt
= TREE_VEC_ELT (args
, i
);
8668 if (elt
&& PACK_EXPANSION_P (elt
))
8674 /* Convert all template arguments to their appropriate types, and
8675 return a vector containing the innermost resulting template
8676 arguments. If any error occurs, return error_mark_node. Error and
8677 warning messages are issued under control of COMPLAIN.
8679 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8680 for arguments not specified in ARGS. Otherwise, if
8681 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8682 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8683 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8687 coerce_template_parms (tree parms
,
8690 tsubst_flags_t complain
,
8691 bool require_all_args
,
8692 bool use_default_args
)
8694 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8695 tree orig_inner_args
;
8698 tree new_inner_args
;
8700 /* When used as a boolean value, indicates whether this is a
8701 variadic template parameter list. Since it's an int, we can also
8702 subtract it from nparms to get the number of non-variadic
8705 int variadic_args_p
= 0;
8706 int post_variadic_parms
= 0;
8708 /* Adjustment to nparms for fixed parameter packs. */
8709 int fixed_pack_adjust
= 0;
8710 int fixed_packs
= 0;
8713 /* Likewise for parameters with default arguments. */
8716 if (args
== error_mark_node
)
8717 return error_mark_node
;
8719 nparms
= TREE_VEC_LENGTH (parms
);
8721 /* Determine if there are any parameter packs or default arguments. */
8722 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8724 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8726 ++post_variadic_parms
;
8727 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8729 if (TREE_PURPOSE (parm
))
8733 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8734 /* If there are no parameters that follow a parameter pack, we need to
8735 expand any argument packs so that we can deduce a parameter pack from
8736 some non-packed args followed by an argument pack, as in variadic85.C.
8737 If there are such parameters, we need to leave argument packs intact
8738 so the arguments are assigned properly. This can happen when dealing
8739 with a nested class inside a partial specialization of a class
8740 template, as in variadic92.C, or when deducing a template parameter pack
8741 from a sub-declarator, as in variadic114.C. */
8742 if (!post_variadic_parms
)
8743 inner_args
= expand_template_argument_pack (inner_args
);
8745 /* Count any pack expansion args. */
8746 variadic_args_p
= pack_expansion_args_count (inner_args
);
8748 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8749 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8750 || (nargs
< nparms
- variadic_p
8753 && (!use_default_args
8754 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8755 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8758 if (complain
& tf_error
)
8760 if (variadic_p
|| default_p
)
8762 nparms
-= variadic_p
+ default_p
;
8763 error ("wrong number of template arguments "
8764 "(%d, should be at least %d)", nargs
, nparms
);
8767 error ("wrong number of template arguments "
8768 "(%d, should be %d)", nargs
, nparms
);
8771 inform (DECL_SOURCE_LOCATION (in_decl
),
8772 "provided for %qD", in_decl
);
8775 return error_mark_node
;
8777 /* We can't pass a pack expansion to a non-pack parameter of an alias
8778 template (DR 1430). */
8780 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8781 || concept_definition_p (in_decl
))
8783 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8785 if (complain
& tf_error
)
8787 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8789 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8790 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8792 if (PACK_EXPANSION_P (arg
)
8793 && !template_parameter_pack_p (parm
))
8795 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8796 error_at (location_of (arg
),
8797 "pack expansion argument for non-pack parameter "
8798 "%qD of alias template %qD", parm
, in_decl
);
8800 error_at (location_of (arg
),
8801 "pack expansion argument for non-pack parameter "
8802 "%qD of concept %qD", parm
, in_decl
);
8803 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8810 return error_mark_node
;
8813 /* We need to evaluate the template arguments, even though this
8814 template-id may be nested within a "sizeof". */
8817 new_inner_args
= make_tree_vec (nparms
);
8818 new_args
= add_outermost_template_args (args
, new_inner_args
);
8819 int pack_adjust
= 0;
8820 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8825 /* Get the Ith template parameter. */
8826 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8828 if (parm
== error_mark_node
)
8830 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8834 /* Calculate the next argument. */
8835 if (arg_idx
< nargs
)
8836 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8840 if (template_parameter_pack_p (TREE_VALUE (parm
))
8841 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8842 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8844 /* Some arguments will be placed in the
8845 template parameter pack PARM. */
8846 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8847 inner_args
, arg_idx
,
8851 if (arg
== NULL_TREE
)
8853 /* We don't know how many args we have yet, just use the
8854 unconverted (and still packed) ones for now. */
8855 new_inner_args
= orig_inner_args
;
8860 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8862 /* Store this argument. */
8863 if (arg
== error_mark_node
)
8866 /* We are done with all of the arguments. */
8872 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8873 arg_idx
+= pack_adjust
;
8874 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8877 fixed_pack_adjust
+= pack_adjust
;
8885 if (PACK_EXPANSION_P (arg
))
8887 /* "If every valid specialization of a variadic template
8888 requires an empty template parameter pack, the template is
8889 ill-formed, no diagnostic required." So check that the
8890 pattern works with this parameter. */
8891 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8892 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8896 if (conv
== error_mark_node
)
8898 if (complain
& tf_error
)
8899 inform (input_location
, "so any instantiation with a "
8900 "non-empty parameter pack would be ill-formed");
8903 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8904 /* Recover from missing typename. */
8905 TREE_VEC_ELT (inner_args
, arg_idx
)
8906 = make_pack_expansion (conv
, complain
);
8908 /* We don't know how many args we have yet, just
8909 use the unconverted ones for now. */
8910 new_inner_args
= inner_args
;
8915 else if (require_all_args
)
8917 /* There must be a default arg in this case. */
8918 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8920 /* The position of the first default template argument,
8921 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8923 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8924 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8925 arg_idx
- pack_adjust
);
8930 if (arg
== error_mark_node
)
8932 if (complain
& tf_error
)
8933 error ("template argument %d is invalid", arg_idx
+ 1);
8937 /* This can occur if there was an error in the template
8938 parameter list itself (which we would already have
8939 reported) that we are trying to recover from, e.g., a class
8940 template with a parameter list such as
8941 template<typename..., typename> (cpp0x/variadic150.C). */
8944 /* This can also happen with a fixed parameter pack (71834). */
8945 if (arg_idx
>= nargs
)
8949 arg
= convert_template_argument (TREE_VALUE (parm
),
8950 arg
, new_args
, complain
,
8953 if (arg
== error_mark_node
)
8956 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8959 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8961 /* If we had fixed parameter packs, we didn't know how many arguments we
8962 actually needed earlier; now we do. */
8963 nparms
+= fixed_pack_adjust
;
8964 variadic_p
-= fixed_packs
;
8968 if (arg_idx
< nargs
)
8970 /* We had some pack expansion arguments that will only work if the packs
8971 are empty, but wait until instantiation time to complain.
8972 See variadic-ttp3.C. */
8974 /* Except that we can't provide empty packs to alias templates or
8975 concepts when there are no corresponding parameters. Basically,
8976 we can get here with this:
8978 template<typename T> concept C = true;
8980 template<typename... Args>
8984 When parsing C<Args...>, we try to form a concept check of
8985 C<?, Args...>. Without the extra check for substituting an empty
8986 pack past the last parameter, we can accept the check as valid.
8988 FIXME: This may be valid for alias templates (but I doubt it).
8990 FIXME: The error could be better also. */
8991 if (in_decl
&& concept_definition_p (in_decl
))
8993 if (complain
& tf_error
)
8994 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8995 "too many arguments");
8996 return error_mark_node
;
8999 int len
= nparms
+ (nargs
- arg_idx
);
9000 tree args
= make_tree_vec (len
);
9002 for (; i
< nparms
; ++i
)
9003 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9004 for (; i
< len
; ++i
, ++arg_idx
)
9005 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9006 arg_idx
- pack_adjust
);
9007 new_inner_args
= args
;
9012 gcc_assert (!(complain
& tf_error
) || seen_error ());
9013 return error_mark_node
;
9016 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9017 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9018 TREE_VEC_LENGTH (new_inner_args
));
9020 return new_inner_args
;
9023 /* Convert all template arguments to their appropriate types, and
9024 return a vector containing the innermost resulting template
9025 arguments. If any error occurs, return error_mark_node. Error and
9026 warning messages are not issued.
9028 Note that no function argument deduction is performed, and default
9029 arguments are used to fill in unspecified arguments. */
9031 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9033 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9036 /* Convert all template arguments to their appropriate type, and
9037 instantiate default arguments as needed. This returns a vector
9038 containing the innermost resulting template arguments, or
9039 error_mark_node if unsuccessful. */
9041 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9042 tsubst_flags_t complain
)
9044 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9047 /* Like coerce_template_parms. If PARMS represents all template
9048 parameters levels, this function returns a vector of vectors
9049 representing all the resulting argument levels. Note that in this
9050 case, only the innermost arguments are coerced because the
9051 outermost ones are supposed to have been coerced already.
9053 Otherwise, if PARMS represents only (the innermost) vector of
9054 parameters, this function returns a vector containing just the
9055 innermost resulting arguments. */
9058 coerce_innermost_template_parms (tree parms
,
9061 tsubst_flags_t complain
,
9062 bool require_all_args
,
9063 bool use_default_args
)
9065 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9066 int args_depth
= TMPL_ARGS_DEPTH (args
);
9069 if (parms_depth
> 1)
9071 coerced_args
= make_tree_vec (parms_depth
);
9075 for (level
= parms
, cur_depth
= parms_depth
;
9076 parms_depth
> 0 && level
!= NULL_TREE
;
9077 level
= TREE_CHAIN (level
), --cur_depth
)
9080 if (cur_depth
== args_depth
)
9081 l
= coerce_template_parms (TREE_VALUE (level
),
9082 args
, in_decl
, complain
,
9086 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9088 if (l
== error_mark_node
)
9089 return error_mark_node
;
9091 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9095 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9096 args
, in_decl
, complain
,
9099 return coerced_args
;
9102 /* Returns true if T is a wrapper to make a C++20 template parameter
9106 class_nttp_const_wrapper_p (tree t
)
9108 if (cxx_dialect
< cxx20
)
9110 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9111 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9112 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9115 /* Returns 1 if template args OT and NT are equivalent. */
9118 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9122 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9124 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9127 if (class_nttp_const_wrapper_p (nt
))
9128 nt
= TREE_OPERAND (nt
, 0);
9129 if (class_nttp_const_wrapper_p (ot
))
9130 ot
= TREE_OPERAND (ot
, 0);
9132 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9133 /* For member templates */
9134 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9135 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9136 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9137 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9138 PACK_EXPANSION_PATTERN (nt
))
9139 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9140 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9141 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9142 return cp_tree_equal (ot
, nt
);
9143 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9145 else if (TYPE_P (nt
) || TYPE_P (ot
))
9147 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9149 /* Don't treat an alias template specialization with dependent
9150 arguments as equivalent to its underlying type when used as a
9151 template argument; we need them to be distinct so that we
9152 substitute into the specialization arguments at instantiation
9153 time. And aliases can't be equivalent without being ==, so
9154 we don't need to look any deeper.
9156 During partial ordering, however, we need to treat them normally so
9157 that we can order uses of the same alias with different
9158 cv-qualification (79960). */
9160 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9163 return same_type_p (ot
, nt
);
9167 /* Try to treat a template non-type argument that has been converted
9168 to the parameter type as equivalent to one that hasn't yet. */
9169 for (enum tree_code code1
= TREE_CODE (ot
);
9170 CONVERT_EXPR_CODE_P (code1
)
9171 || code1
== NON_LVALUE_EXPR
;
9172 code1
= TREE_CODE (ot
))
9173 ot
= TREE_OPERAND (ot
, 0);
9175 for (enum tree_code code2
= TREE_CODE (nt
);
9176 CONVERT_EXPR_CODE_P (code2
)
9177 || code2
== NON_LVALUE_EXPR
;
9178 code2
= TREE_CODE (nt
))
9179 nt
= TREE_OPERAND (nt
, 0);
9181 return cp_tree_equal (ot
, nt
);
9185 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9186 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9187 NEWARG_PTR with the offending arguments if they are non-NULL. */
9190 comp_template_args (tree oldargs
, tree newargs
,
9191 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9196 if (oldargs
== newargs
)
9199 if (!oldargs
|| !newargs
)
9202 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9205 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9207 tree nt
= TREE_VEC_ELT (newargs
, i
);
9208 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9210 if (! template_args_equal (ot
, nt
, partial_order
))
9212 if (oldarg_ptr
!= NULL
)
9214 if (newarg_ptr
!= NULL
)
9223 comp_template_args_porder (tree oargs
, tree nargs
)
9225 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9228 /* Implement a freelist interface for objects of type T.
9230 Head is a separate object, rather than a regular member, so that we
9231 can define it as a GTY deletable pointer, which is highly
9232 desirable. A data member could be declared that way, but then the
9233 containing object would implicitly get GTY((user)), which would
9234 prevent us from instantiating freelists as global objects.
9235 Although this way we can create freelist global objects, they're
9236 such thin wrappers that instantiating temporaries at every use
9237 loses nothing and saves permanent storage for the freelist object.
9239 Member functions next, anew, poison and reinit have default
9240 implementations that work for most of the types we're interested
9241 in, but if they don't work for some type, they should be explicitly
9242 specialized. See the comments before them for requirements, and
9243 the example specializations for the tree_list_freelist. */
9244 template <typename T
>
9247 /* Return the next object in a chain. We could just do type
9248 punning, but if we access the object with its underlying type, we
9249 avoid strict-aliasing trouble. This needs only work between
9250 poison and reinit. */
9251 static T
*&next (T
*obj
) { return obj
->next
; }
9253 /* Return a newly allocated, uninitialized or minimally-initialized
9254 object of type T. Any initialization performed by anew should
9255 either remain across the life of the object and the execution of
9256 poison, or be redone by reinit. */
9257 static T
*anew () { return ggc_alloc
<T
> (); }
9259 /* Optionally scribble all over the bits holding the object, so that
9260 they become (mostly?) uninitialized memory. This is called while
9261 preparing to make the object part of the free list. */
9262 static void poison (T
*obj
) {
9263 T
*p ATTRIBUTE_UNUSED
= obj
;
9264 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9266 #ifdef ENABLE_GC_CHECKING
9267 /* Poison the data, to indicate the data is garbage. */
9268 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9269 memset (p
, 0xa5, sizeof (*p
));
9271 /* Let valgrind know the object is free. */
9272 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9274 /* Let valgrind know the next portion of the object is available,
9275 but uninitialized. */
9276 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9279 /* Bring an object that underwent at least one lifecycle after anew
9280 and before the most recent free and poison, back to a usable
9281 state, reinitializing whatever is needed for it to be
9282 functionally equivalent to an object just allocated and returned
9283 by anew. This may poison or clear the next field, used by
9284 freelist housekeeping after poison was called. */
9285 static void reinit (T
*obj
) {
9286 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9288 #ifdef ENABLE_GC_CHECKING
9289 memset (q
, 0xa5, sizeof (*q
));
9291 /* Let valgrind know the entire object is available, but
9293 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9296 /* Reference a GTY-deletable pointer that points to the first object
9297 in the free list proper. */
9300 /* Construct a freelist object chaining objects off of HEAD. */
9301 freelist (T
*&head
) : head(head
) {}
9303 /* Add OBJ to the free object list. The former head becomes OBJ's
9312 /* Take an object from the free list, if one is available, or
9313 allocate a new one. Objects taken from the free list should be
9314 regarded as filled with garbage, except for bits that are
9315 configured to be preserved across free and alloc. */
9330 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9331 want to allocate a TREE_LIST using the usual interface, and ensure
9332 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9333 build_tree_list logic in reinit, so this could go out of sync. */
9336 freelist
<tree_node
>::next (tree obj
)
9338 return TREE_CHAIN (obj
);
9342 freelist
<tree_node
>::anew ()
9344 return build_tree_list (NULL
, NULL
);
9348 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9350 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9351 tree p ATTRIBUTE_UNUSED
= obj
;
9352 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9353 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9355 #ifdef ENABLE_GC_CHECKING
9356 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9358 /* Poison the data, to indicate the data is garbage. */
9359 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9360 memset (p
, 0xa5, size
);
9362 /* Let valgrind know the object is free. */
9363 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9364 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9368 #ifdef ENABLE_GC_CHECKING
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9370 /* Keep TREE_CHAIN functional. */
9371 TREE_SET_CODE (obj
, TREE_LIST
);
9373 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9378 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9380 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9382 #ifdef ENABLE_GC_CHECKING
9383 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9384 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9385 memset (obj
, 0, sizeof (tree_list
));
9388 /* Let valgrind know the entire object is available, but
9390 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9392 #ifdef ENABLE_GC_CHECKING
9393 TREE_SET_CODE (obj
, TREE_LIST
);
9395 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9399 /* Point to the first object in the TREE_LIST freelist. */
9400 static GTY((deletable
)) tree tree_list_freelist_head
;
9401 /* Return the/an actual TREE_LIST freelist. */
9402 static inline freelist
<tree_node
>
9403 tree_list_freelist ()
9405 return tree_list_freelist_head
;
9408 /* Point to the first object in the tinst_level freelist. */
9409 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9410 /* Return the/an actual tinst_level freelist. */
9411 static inline freelist
<tinst_level
>
9412 tinst_level_freelist ()
9414 return tinst_level_freelist_head
;
9417 /* Point to the first object in the pending_template freelist. */
9418 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9419 /* Return the/an actual pending_template freelist. */
9420 static inline freelist
<pending_template
>
9421 pending_template_freelist ()
9423 return pending_template_freelist_head
;
9426 /* Build the TREE_LIST object out of a split list, store it
9427 permanently, and return it. */
9429 tinst_level::to_list ()
9431 gcc_assert (split_list_p ());
9432 tree ret
= tree_list_freelist ().alloc ();
9433 TREE_PURPOSE (ret
) = tldcl
;
9434 TREE_VALUE (ret
) = targs
;
9437 gcc_assert (tree_list_p ());
9441 const unsigned short tinst_level::refcount_infinity
;
9443 /* Increment OBJ's refcount unless it is already infinite. */
9444 static tinst_level
*
9445 inc_refcount_use (tinst_level
*obj
)
9447 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9452 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9454 tinst_level::free (tinst_level
*obj
)
9456 if (obj
->tree_list_p ())
9457 tree_list_freelist ().free (obj
->get_node ());
9458 tinst_level_freelist ().free (obj
);
9461 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9462 OBJ's DECL and OBJ, and start over with the tinst_level object that
9463 used to be referenced by OBJ's NEXT. */
9465 dec_refcount_use (tinst_level
*obj
)
9468 && obj
->refcount
!= tinst_level::refcount_infinity
9469 && !--obj
->refcount
)
9471 tinst_level
*next
= obj
->next
;
9472 tinst_level::free (obj
);
9477 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9478 and of the former PTR. Omitting the second argument is equivalent
9479 to passing (T*)NULL; this is allowed because passing the
9480 zero-valued integral constant NULL confuses type deduction and/or
9481 overload resolution. */
9482 template <typename T
>
9484 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9487 ptr
= inc_refcount_use (obj
);
9488 dec_refcount_use (save
);
9492 add_pending_template (tree d
)
9494 tree ti
= (TYPE_P (d
)
9495 ? CLASSTYPE_TEMPLATE_INFO (d
)
9496 : DECL_TEMPLATE_INFO (d
));
9497 struct pending_template
*pt
;
9500 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9503 /* We are called both from instantiate_decl, where we've already had a
9504 tinst_level pushed, and instantiate_template, where we haven't.
9506 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9507 level
= !current_tinst_level
9508 || current_tinst_level
->maybe_get_node () != d
;
9511 push_tinst_level (d
);
9513 pt
= pending_template_freelist ().alloc ();
9516 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9517 if (last_pending_template
)
9518 last_pending_template
->next
= pt
;
9520 pending_templates
= pt
;
9522 last_pending_template
= pt
;
9524 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9531 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9532 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9533 documentation for TEMPLATE_ID_EXPR. */
9536 lookup_template_function (tree fns
, tree arglist
)
9538 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9539 return error_mark_node
;
9541 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9543 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9545 error ("%q#D is not a function template", fns
);
9546 return error_mark_node
;
9549 if (BASELINK_P (fns
))
9551 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9553 BASELINK_FUNCTIONS (fns
),
9558 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9561 /* Within the scope of a template class S<T>, the name S gets bound
9562 (in build_self_reference) to a TYPE_DECL for the class, not a
9563 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9564 or one of its enclosing classes, and that type is a template,
9565 return the associated TEMPLATE_DECL. Otherwise, the original
9568 Also handle the case when DECL is a TREE_LIST of ambiguous
9569 injected-class-names from different bases. */
9572 maybe_get_template_decl_from_type_decl (tree decl
)
9574 if (decl
== NULL_TREE
)
9577 /* DR 176: A lookup that finds an injected-class-name (10.2
9578 [class.member.lookup]) can result in an ambiguity in certain cases
9579 (for example, if it is found in more than one base class). If all of
9580 the injected-class-names that are found refer to specializations of
9581 the same class template, and if the name is followed by a
9582 template-argument-list, the reference refers to the class template
9583 itself and not a specialization thereof, and is not ambiguous. */
9584 if (TREE_CODE (decl
) == TREE_LIST
)
9586 tree t
, tmpl
= NULL_TREE
;
9587 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9589 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9592 else if (tmpl
!= elt
)
9595 if (tmpl
&& t
== NULL_TREE
)
9601 return (decl
!= NULL_TREE
9602 && DECL_SELF_REFERENCE_P (decl
)
9603 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9604 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9607 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9608 parameters, find the desired type.
9610 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9612 IN_DECL, if non-NULL, is the template declaration we are trying to
9615 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9616 the class we are looking up.
9618 Issue error and warning messages under control of COMPLAIN.
9620 If the template class is really a local class in a template
9621 function, then the FUNCTION_CONTEXT is the function in which it is
9624 ??? Note that this function is currently called *twice* for each
9625 template-id: the first time from the parser, while creating the
9626 incomplete type (finish_template_type), and the second type during the
9627 real instantiation (instantiate_template_class). This is surely something
9628 that we want to avoid. It also causes some problems with argument
9629 coercion (see convert_nontype_argument for more information on this). */
9632 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9633 int entering_scope
, tsubst_flags_t complain
)
9635 tree templ
= NULL_TREE
, parmlist
;
9642 if (identifier_p (d1
))
9644 tree value
= innermost_non_namespace_value (d1
);
9645 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9650 push_decl_namespace (context
);
9651 templ
= lookup_name (d1
);
9652 templ
= maybe_get_template_decl_from_type_decl (templ
);
9654 pop_decl_namespace ();
9657 context
= DECL_CONTEXT (templ
);
9659 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9661 tree type
= TREE_TYPE (d1
);
9663 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9664 an implicit typename for the second A. Deal with it. */
9665 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9666 type
= TREE_TYPE (type
);
9668 if (CLASSTYPE_TEMPLATE_INFO (type
))
9670 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9671 d1
= DECL_NAME (templ
);
9674 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9675 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9677 templ
= TYPE_TI_TEMPLATE (d1
);
9678 d1
= DECL_NAME (templ
);
9680 else if (DECL_TYPE_TEMPLATE_P (d1
))
9683 d1
= DECL_NAME (templ
);
9684 context
= DECL_CONTEXT (templ
);
9686 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9689 d1
= DECL_NAME (templ
);
9692 /* Issue an error message if we didn't find a template. */
9695 if (complain
& tf_error
)
9696 error ("%qT is not a template", d1
);
9697 return error_mark_node
;
9700 if (TREE_CODE (templ
) != TEMPLATE_DECL
9701 /* Make sure it's a user visible template, if it was named by
9703 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9704 && !PRIMARY_TEMPLATE_P (templ
)))
9706 if (complain
& tf_error
)
9708 error ("non-template type %qT used as a template", d1
);
9710 error ("for template declaration %q+D", in_decl
);
9712 return error_mark_node
;
9715 complain
&= ~tf_user
;
9717 /* An alias that just changes the name of a template is equivalent to the
9718 other template, so if any of the arguments are pack expansions, strip
9719 the alias to avoid problems with a pack expansion passed to a non-pack
9720 alias template parameter (DR 1430). */
9721 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9722 templ
= get_underlying_template (templ
);
9724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9727 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9728 if (arglist2
== error_mark_node
9729 || (!uses_template_parms (arglist2
)
9730 && check_instantiated_args (templ
, arglist2
, complain
)))
9731 return error_mark_node
;
9733 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9738 tree template_type
= TREE_TYPE (templ
);
9741 tree found
= NULL_TREE
;
9744 int is_dependent_type
;
9745 int use_partial_inst_tmpl
= false;
9747 if (template_type
== error_mark_node
)
9748 /* An error occurred while building the template TEMPL, and a
9749 diagnostic has most certainly been emitted for that
9750 already. Let's propagate that error. */
9751 return error_mark_node
;
9753 gen_tmpl
= most_general_template (templ
);
9754 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9755 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9756 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9758 if (arg_depth
== 1 && parm_depth
> 1)
9760 /* We've been given an incomplete set of template arguments.
9763 template <class T> struct S1 {
9764 template <class U> struct S2 {};
9765 template <class U> struct S2<U*> {};
9768 we will be called with an ARGLIST of `U*', but the
9769 TEMPLATE will be `template <class T> template
9770 <class U> struct S1<T>::S2'. We must fill in the missing
9772 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9773 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9774 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9777 /* Now we should have enough arguments. */
9778 gcc_assert (parm_depth
== arg_depth
);
9780 /* From here on, we're only interested in the most general
9783 /* Calculate the BOUND_ARGS. These will be the args that are
9784 actually tsubst'd into the definition to create the
9786 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9788 /*require_all_args=*/true,
9789 /*use_default_args=*/true);
9791 if (arglist
== error_mark_node
)
9792 /* We were unable to bind the arguments. */
9793 return error_mark_node
;
9795 /* In the scope of a template class, explicit references to the
9796 template class refer to the type of the template, not any
9797 instantiation of it. For example, in:
9799 template <class T> class C { void f(C<T>); }
9801 the `C<T>' is just the same as `C'. Outside of the
9802 class, however, such a reference is an instantiation. */
9804 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9805 || currently_open_class (template_type
))
9807 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9809 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9810 return template_type
;
9813 /* If we already have this specialization, return it. */
9814 elt
.tmpl
= gen_tmpl
;
9816 elt
.spec
= NULL_TREE
;
9817 hash
= spec_hasher::hash (&elt
);
9818 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9823 /* If the template's constraints are not satisfied,
9824 then we cannot form a valid type.
9826 Note that the check is deferred until after the hash
9827 lookup. This prevents redundant checks on previously
9828 instantiated specializations. */
9830 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9831 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9833 if (complain
& tf_error
)
9835 auto_diagnostic_group d
;
9836 error ("template constraint failure for %qD", gen_tmpl
);
9837 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9839 return error_mark_node
;
9842 is_dependent_type
= uses_template_parms (arglist
);
9844 /* If the deduced arguments are invalid, then the binding
9846 if (!is_dependent_type
9847 && check_instantiated_args (gen_tmpl
,
9848 INNERMOST_TEMPLATE_ARGS (arglist
),
9850 return error_mark_node
;
9852 if (!is_dependent_type
9853 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9854 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9855 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9856 /* This occurs when the user has tried to define a tagged type
9857 in a scope that forbids it. We emitted an error during the
9858 parse. We didn't complete the bail out then, so here we
9860 return error_mark_node
;
9862 context
= DECL_CONTEXT (gen_tmpl
);
9863 if (context
&& TYPE_P (context
))
9865 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9866 context
= complete_type (context
);
9869 context
= tsubst (context
, arglist
, complain
, in_decl
);
9871 if (context
== error_mark_node
)
9872 return error_mark_node
;
9875 context
= global_namespace
;
9877 /* Create the type. */
9878 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9880 /* The user referred to a specialization of an alias
9881 template represented by GEN_TMPL.
9883 [temp.alias]/2 says:
9885 When a template-id refers to the specialization of an
9886 alias template, it is equivalent to the associated
9887 type obtained by substitution of its
9888 template-arguments for the template-parameters in the
9889 type-id of the alias template. */
9891 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9892 /* Note that the call above (by indirectly calling
9893 register_specialization in tsubst_decl) registers the
9894 TYPE_DECL representing the specialization of the alias
9895 template. So next time someone substitutes ARGLIST for
9896 the template parms into the alias template (GEN_TMPL),
9897 she'll get that TYPE_DECL back. */
9899 if (t
== error_mark_node
)
9902 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9904 if (!is_dependent_type
)
9906 set_current_access_from_decl (TYPE_NAME (template_type
));
9907 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9908 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9909 arglist
, complain
, in_decl
),
9910 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9911 arglist
, complain
, in_decl
),
9912 SCOPED_ENUM_P (template_type
), NULL
);
9914 if (t
== error_mark_node
)
9919 /* We don't want to call start_enum for this type, since
9920 the values for the enumeration constants may involve
9921 template parameters. And, no one should be interested
9922 in the enumeration constants for such a type. */
9923 t
= cxx_make_type (ENUMERAL_TYPE
);
9924 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9926 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9927 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9928 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9930 else if (CLASS_TYPE_P (template_type
))
9932 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9933 instantiated here. */
9934 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9936 t
= make_class_type (TREE_CODE (template_type
));
9937 CLASSTYPE_DECLARED_CLASS (t
)
9938 = CLASSTYPE_DECLARED_CLASS (template_type
);
9939 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9941 /* A local class. Make sure the decl gets registered properly. */
9942 if (context
== current_function_decl
)
9943 if (pushtag (DECL_NAME (gen_tmpl
), t
)
9945 return error_mark_node
;
9947 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9948 /* This instantiation is another name for the primary
9949 template type. Set the TYPE_CANONICAL field
9951 TYPE_CANONICAL (t
) = template_type
;
9952 else if (any_template_arguments_need_structural_equality_p (arglist
))
9953 /* Some of the template arguments require structural
9954 equality testing, so this template class requires
9955 structural equality testing. */
9956 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9961 /* If we called start_enum or pushtag above, this information
9962 will already be set up. */
9963 type_decl
= TYPE_NAME (t
);
9966 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9968 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9969 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9970 DECL_SOURCE_LOCATION (type_decl
)
9971 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9974 if (CLASS_TYPE_P (template_type
))
9976 TREE_PRIVATE (type_decl
)
9977 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9978 TREE_PROTECTED (type_decl
)
9979 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9980 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9982 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9983 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9987 if (OVERLOAD_TYPE_P (t
)
9988 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9990 static const char *tags
[] = {"abi_tag", "may_alias"};
9992 for (unsigned ix
= 0; ix
!= 2; ix
++)
9995 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9999 = tree_cons (TREE_PURPOSE (attributes
),
10000 TREE_VALUE (attributes
),
10001 TYPE_ATTRIBUTES (t
));
10005 /* Let's consider the explicit specialization of a member
10006 of a class template specialization that is implicitly instantiated,
10011 template<class U> struct M {}; //#0
10016 struct S<int>::M<char> //#1
10020 [temp.expl.spec]/4 says this is valid.
10022 In this case, when we write:
10025 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10028 When we encounter #1, we want to store the partial instantiation
10029 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10031 For all cases other than this "explicit specialization of member of a
10032 class template", we just want to store the most general template into
10033 the CLASSTYPE_TI_TEMPLATE of M.
10035 This case of "explicit specialization of member of a class template"
10037 1/ the enclosing class is an instantiation of, and therefore not
10038 the same as, the context of the most general template, and
10039 2/ we aren't looking at the partial instantiation itself, i.e.
10040 the innermost arguments are not the same as the innermost parms of
10041 the most general template.
10043 So it's only when 1/ and 2/ happens that we want to use the partial
10044 instantiation of the member template in lieu of its most general
10047 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10048 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10049 /* the enclosing class must be an instantiation... */
10050 && CLASS_TYPE_P (context
)
10051 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10053 TREE_VEC_LENGTH (arglist
)--;
10054 ++processing_template_decl
;
10055 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10056 tree partial_inst_args
=
10057 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10058 arglist
, complain
, NULL_TREE
);
10059 --processing_template_decl
;
10060 TREE_VEC_LENGTH (arglist
)++;
10061 if (partial_inst_args
== error_mark_node
)
10062 return error_mark_node
;
10063 use_partial_inst_tmpl
=
10064 /*...and we must not be looking at the partial instantiation
10066 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10067 partial_inst_args
);
10070 if (!use_partial_inst_tmpl
)
10071 /* This case is easy; there are no member templates involved. */
10075 /* This is a full instantiation of a member template. Find
10076 the partial instantiation of which this is an instance. */
10078 /* Temporarily reduce by one the number of levels in the ARGLIST
10079 so as to avoid comparing the last set of arguments. */
10080 TREE_VEC_LENGTH (arglist
)--;
10081 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10082 TREE_VEC_LENGTH (arglist
)++;
10083 /* FOUND is either a proper class type, or an alias
10084 template specialization. In the later case, it's a
10085 TYPE_DECL, resulting from the substituting of arguments
10086 for parameters in the TYPE_DECL of the alias template
10087 done earlier. So be careful while getting the template
10089 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10091 : (TREE_CODE (found
) == TYPE_DECL
10092 ? DECL_TI_TEMPLATE (found
)
10093 : CLASSTYPE_TI_TEMPLATE (found
)));
10095 if (DECL_CLASS_TEMPLATE_P (found
)
10096 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10098 /* If this partial instantiation is specialized, we want to
10099 use it for hash table lookup. */
10101 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10102 hash
= spec_hasher::hash (&elt
);
10106 /* Build template info for the new specialization. */
10107 if (TYPE_ALIAS_P (t
))
10109 /* This is constructed during instantiation of the alias
10110 decl. But for member templates of template classes, that
10111 is not correct as we need to refer to the partially
10112 instantiated template, not the most general template.
10113 The incorrect knowledge will not have escaped this
10114 instantiation process, so we're good just updating the
10115 template_info we made then. */
10116 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10117 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
));
10118 if (TI_TEMPLATE (ti
) != found
)
10120 gcc_checking_assert (DECL_TI_TEMPLATE (found
) == TI_TEMPLATE (ti
));
10121 TI_TEMPLATE (ti
) = found
;
10125 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10128 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10129 gcc_checking_assert (*slot
== NULL
);
10130 entry
= ggc_alloc
<spec_entry
> ();
10134 /* Note this use of the partial instantiation so we can check it
10135 later in maybe_process_partial_specialization. */
10136 DECL_TEMPLATE_INSTANTIATIONS (found
)
10137 = tree_cons (arglist
, t
,
10138 DECL_TEMPLATE_INSTANTIATIONS (found
));
10140 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10141 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10142 /* Now that the type has been registered on the instantiations
10143 list, we set up the enumerators. Because the enumeration
10144 constants may involve the enumeration type itself, we make
10145 sure to register the type first, and then create the
10146 constants. That way, doing tsubst_expr for the enumeration
10147 constants won't result in recursive calls here; we'll find
10148 the instantiation and exit above. */
10149 tsubst_enum (template_type
, t
, arglist
);
10151 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10152 /* If the type makes use of template parameters, the
10153 code that generates debugging information will crash. */
10154 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10156 /* Possibly limit visibility based on template args. */
10157 TREE_PUBLIC (type_decl
) = 1;
10158 determine_visibility (type_decl
);
10160 inherit_targ_abi_tags (t
);
10166 /* Wrapper for lookup_template_class_1. */
10169 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10170 int entering_scope
, tsubst_flags_t complain
)
10173 timevar_push (TV_TEMPLATE_INST
);
10174 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10175 entering_scope
, complain
);
10176 timevar_pop (TV_TEMPLATE_INST
);
10180 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10183 lookup_template_variable (tree templ
, tree arglist
)
10185 if (flag_concepts
&& variable_concept_p (templ
))
10186 return build_concept_check (templ
, arglist
, tf_none
);
10188 /* The type of the expression is NULL_TREE since the template-id could refer
10189 to an explicit or partial specialization. */
10190 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10193 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10196 finish_template_variable (tree var
, tsubst_flags_t complain
)
10198 tree templ
= TREE_OPERAND (var
, 0);
10199 tree arglist
= TREE_OPERAND (var
, 1);
10201 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10202 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10204 templ
= most_general_template (templ
);
10205 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10206 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10208 /*use_default*/true);
10209 if (arglist
== error_mark_node
)
10210 return error_mark_node
;
10212 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10214 if (complain
& tf_error
)
10216 auto_diagnostic_group d
;
10217 error ("use of invalid variable template %qE", var
);
10218 diagnose_constraints (location_of (var
), templ
, arglist
);
10220 return error_mark_node
;
10223 return instantiate_template (templ
, arglist
, complain
);
10226 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10227 TARGS template args, and instantiate it if it's not dependent. */
10230 lookup_and_finish_template_variable (tree templ
, tree targs
,
10231 tsubst_flags_t complain
)
10233 templ
= lookup_template_variable (templ
, targs
);
10234 if (!any_dependent_template_arguments_p (targs
))
10236 templ
= finish_template_variable (templ
, complain
);
10240 return convert_from_reference (templ
);
10243 /* If the set of template parameters PARMS contains a template parameter
10244 at the given LEVEL and INDEX, then return this parameter. Otherwise
10245 return NULL_TREE. */
10248 corresponding_template_parameter (tree parms
, int level
, int index
)
10250 while (TMPL_PARMS_DEPTH (parms
) > level
)
10251 parms
= TREE_CHAIN (parms
);
10253 if (TMPL_PARMS_DEPTH (parms
) != level
10254 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10257 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10258 /* As in template_parm_to_arg. */
10259 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10262 t
= DECL_INITIAL (t
);
10264 gcc_assert (TEMPLATE_PARM_P (t
));
10268 /* Return the template parameter from PARMS that positionally corresponds
10269 to the template parameter PARM, or else return NULL_TREE. */
10272 corresponding_template_parameter (tree parms
, tree parm
)
10275 template_parm_level_and_index (parm
, &level
, &index
);
10276 return corresponding_template_parameter (parms
, level
, index
);
10280 struct pair_fn_data
10285 /* True when we should also visit template parameters that occur in
10286 non-deduced contexts. */
10287 bool include_nondeduced_p
;
10288 hash_set
<tree
> *visited
;
10291 /* Called from for_each_template_parm via walk_tree. */
10294 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10297 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10298 tree_fn_t fn
= pfd
->fn
;
10299 void *data
= pfd
->data
;
10300 tree result
= NULL_TREE
;
10302 #define WALK_SUBTREE(NODE) \
10305 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10306 pfd->include_nondeduced_p, \
10308 if (result) goto out; \
10312 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10316 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10317 WALK_SUBTREE (TYPE_CONTEXT (t
));
10319 switch (TREE_CODE (t
))
10322 if (TYPE_PTRMEMFUNC_P (t
))
10324 /* Fall through. */
10327 case ENUMERAL_TYPE
:
10328 if (!TYPE_TEMPLATE_INFO (t
))
10329 *walk_subtrees
= 0;
10331 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10335 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10336 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10340 /* Since we're not going to walk subtrees, we have to do this
10341 explicitly here. */
10342 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10343 /* Fall through. */
10345 case FUNCTION_TYPE
:
10346 /* Check the return type. */
10347 WALK_SUBTREE (TREE_TYPE (t
));
10349 /* Check the parameter types. Since default arguments are not
10350 instantiated until they are needed, the TYPE_ARG_TYPES may
10351 contain expressions that involve template parameters. But,
10352 no-one should be looking at them yet. And, once they're
10353 instantiated, they don't contain template parameters, so
10354 there's no point in looking at them then, either. */
10358 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10359 WALK_SUBTREE (TREE_VALUE (parm
));
10361 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10362 want walk_tree walking into them itself. */
10363 *walk_subtrees
= 0;
10366 if (flag_noexcept_type
)
10368 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10370 WALK_SUBTREE (TREE_PURPOSE (spec
));
10375 case DECLTYPE_TYPE
:
10376 case UNDERLYING_TYPE
:
10377 if (pfd
->include_nondeduced_p
10378 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10380 pfd
->include_nondeduced_p
,
10382 return error_mark_node
;
10383 *walk_subtrees
= false;
10386 case FUNCTION_DECL
:
10388 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10389 WALK_SUBTREE (DECL_TI_ARGS (t
));
10390 /* Fall through. */
10394 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10395 WALK_SUBTREE (DECL_INITIAL (t
));
10396 if (DECL_CONTEXT (t
)
10397 && pfd
->include_nondeduced_p
)
10398 WALK_SUBTREE (DECL_CONTEXT (t
));
10401 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10402 /* Record template parameters such as `T' inside `TT<T>'. */
10403 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10404 /* Fall through. */
10406 case TEMPLATE_TEMPLATE_PARM
:
10407 case TEMPLATE_TYPE_PARM
:
10408 case TEMPLATE_PARM_INDEX
:
10409 if (fn
&& (*fn
)(t
, data
))
10415 case TEMPLATE_DECL
:
10416 /* A template template parameter is encountered. */
10417 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10418 WALK_SUBTREE (TREE_TYPE (t
));
10420 /* Already substituted template template parameter */
10421 *walk_subtrees
= 0;
10424 case TYPENAME_TYPE
:
10425 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10426 partial instantiation. */
10427 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10428 *walk_subtrees
= 0;
10432 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10433 && pfd
->include_nondeduced_p
)
10434 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10438 case COMPONENT_REF
:
10439 /* If there's no type, then this thing must be some expression
10440 involving template parameters. */
10441 if (!fn
&& !TREE_TYPE (t
))
10442 return error_mark_node
;
10447 case IMPLICIT_CONV_EXPR
:
10448 case REINTERPRET_CAST_EXPR
:
10449 case CONST_CAST_EXPR
:
10450 case STATIC_CAST_EXPR
:
10451 case DYNAMIC_CAST_EXPR
:
10455 case PSEUDO_DTOR_EXPR
:
10457 return error_mark_node
;
10461 if (pfd
->include_nondeduced_p
)
10462 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10465 case REQUIRES_EXPR
:
10468 return error_mark_node
;
10470 /* Recursively walk the type of each constraint variable. */
10471 tree p
= TREE_OPERAND (t
, 0);
10474 WALK_SUBTREE (TREE_TYPE (p
));
10475 p
= TREE_CHAIN (p
);
10484 #undef WALK_SUBTREE
10486 /* We didn't find any template parameters we liked. */
10491 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10492 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10493 call FN with the parameter and the DATA.
10494 If FN returns nonzero, the iteration is terminated, and
10495 for_each_template_parm returns 1. Otherwise, the iteration
10496 continues. If FN never returns a nonzero value, the value
10497 returned by for_each_template_parm is 0. If FN is NULL, it is
10498 considered to be the function which always returns 1.
10500 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10501 parameters that occur in non-deduced contexts. When false, only
10502 visits those template parameters that can be deduced. */
10505 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10506 hash_set
<tree
> *visited
,
10507 bool include_nondeduced_p
,
10510 struct pair_fn_data pfd
;
10515 pfd
.any_fn
= any_fn
;
10517 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10519 /* Walk the tree. (Conceptually, we would like to walk without
10520 duplicates, but for_each_template_parm_r recursively calls
10521 for_each_template_parm, so we would need to reorganize a fair
10522 bit to use walk_tree_without_duplicates, so we keep our own
10525 pfd
.visited
= visited
;
10527 pfd
.visited
= new hash_set
<tree
>;
10528 result
= cp_walk_tree (&t
,
10529 for_each_template_parm_r
,
10536 delete pfd
.visited
;
10543 struct find_template_parameter_info
10545 explicit find_template_parameter_info (tree ctx_parms
)
10546 : parm_list (NULL_TREE
),
10547 ctx_parms (ctx_parms
),
10548 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10551 hash_set
<tree
> visited
;
10552 hash_set
<tree
> parms
;
10558 /* Appends the declaration of T to the list in DATA. */
10561 keep_template_parm (tree t
, void* data
)
10563 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10565 /* Template parameters declared within the expression are not part of
10566 the parameter mapping. For example, in this concept:
10568 template<typename T>
10569 concept C = requires { <expr> } -> same_as<int>;
10571 the return specifier same_as<int> declares a new decltype parameter
10572 that must not be part of the parameter mapping. The same is true
10573 for generic lambda parameters, lambda template parameters, etc. */
10576 template_parm_level_and_index (t
, &level
, &index
);
10577 if (level
> ftpi
->max_depth
)
10580 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10581 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10582 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10583 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10585 /* This template parameter might be an argument to a cached dependent
10586 specalization that was formed earlier inside some other template, in
10587 which case the parameter is not among the ones that are in-scope.
10588 Look in CTX_PARMS to find the corresponding in-scope template
10589 parameter, and use it instead. */
10590 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10593 /* Arguments like const T yield parameters like const T. This means that
10594 a template-id like X<T, const T> would yield two distinct parameters:
10595 T and const T. Adjust types to their unqualified versions. */
10597 t
= TYPE_MAIN_VARIANT (t
);
10598 if (!ftpi
->parms
.add (t
))
10599 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10604 /* Ensure that we recursively examine certain terms that are not normally
10605 visited in for_each_template_parm_r. */
10608 any_template_parm_r (tree t
, void *data
)
10610 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10612 #define WALK_SUBTREE(NODE) \
10615 for_each_template_parm (NODE, keep_template_parm, data, \
10616 &ftpi->visited, true, \
10617 any_template_parm_r); \
10621 /* A mention of a member alias/typedef is a use of all of its template
10622 arguments, including those from the enclosing class, so we don't use
10623 alias_template_specialization_p here. */
10624 if (TYPE_P (t
) && typedef_variant_p (t
))
10625 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10626 WALK_SUBTREE (TI_ARGS (tinfo
));
10628 switch (TREE_CODE (t
))
10630 case TEMPLATE_TYPE_PARM
:
10631 /* Type constraints of a placeholder type may contain parameters. */
10633 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10634 WALK_SUBTREE (constr
);
10637 case TEMPLATE_ID_EXPR
:
10638 /* Search through references to variable templates. */
10639 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10640 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10643 case TEMPLATE_PARM_INDEX
:
10645 /* A parameter or constraint variable may also depend on a template
10646 parameter without explicitly naming it. */
10647 WALK_SUBTREE (TREE_TYPE (t
));
10650 case TEMPLATE_DECL
:
10652 /* If T is a member template that shares template parameters with
10653 ctx_parms, we need to mark all those parameters for mapping. */
10654 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10655 tree cparms
= ftpi
->ctx_parms
;
10656 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10657 dparms
= TREE_CHAIN (dparms
);
10658 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10659 cparms
= TREE_CHAIN (cparms
);
10661 && (TREE_TYPE (TREE_VALUE (dparms
))
10662 != TREE_TYPE (TREE_VALUE (cparms
))))
10663 dparms
= TREE_CHAIN (dparms
),
10664 cparms
= TREE_CHAIN (cparms
);
10667 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10668 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10669 for (int i
= 0; i
< ddepth
; ++i
)
10670 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10677 /* Look in the parms and body. */
10678 tree fn
= lambda_function (t
);
10679 WALK_SUBTREE (TREE_TYPE (fn
));
10680 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10684 case IDENTIFIER_NODE
:
10685 if (IDENTIFIER_CONV_OP_P (t
))
10686 /* The conversion-type-id of a conversion operator may be dependent. */
10687 WALK_SUBTREE (TREE_TYPE (t
));
10694 /* Keep walking. */
10698 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10699 are the template parameters in scope. */
10702 find_template_parameters (tree t
, tree ctx_parms
)
10707 find_template_parameter_info
ftpi (ctx_parms
);
10708 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10709 /*include_nondeduced*/true, any_template_parm_r
);
10710 return ftpi
.parm_list
;
10713 /* Returns true if T depends on any template parameter. */
10716 uses_template_parms (tree t
)
10718 if (t
== NULL_TREE
)
10722 int saved_processing_template_decl
;
10724 saved_processing_template_decl
= processing_template_decl
;
10725 if (!saved_processing_template_decl
)
10726 processing_template_decl
= 1;
10728 dependent_p
= dependent_type_p (t
);
10729 else if (TREE_CODE (t
) == TREE_VEC
)
10730 dependent_p
= any_dependent_template_arguments_p (t
);
10731 else if (TREE_CODE (t
) == TREE_LIST
)
10732 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10733 || uses_template_parms (TREE_CHAIN (t
)));
10734 else if (TREE_CODE (t
) == TYPE_DECL
)
10735 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10736 else if (t
== error_mark_node
)
10737 dependent_p
= false;
10739 dependent_p
= value_dependent_expression_p (t
);
10741 processing_template_decl
= saved_processing_template_decl
;
10743 return dependent_p
;
10746 /* Returns true iff current_function_decl is an incompletely instantiated
10747 template. Useful instead of processing_template_decl because the latter
10748 is set to 0 during instantiate_non_dependent_expr. */
10751 in_template_function (void)
10753 tree fn
= current_function_decl
;
10755 ++processing_template_decl
;
10756 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10757 && DECL_TEMPLATE_INFO (fn
)
10758 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10759 --processing_template_decl
;
10763 /* Returns true if T depends on any template parameter with level LEVEL. */
10766 uses_template_parms_level (tree t
, int level
)
10768 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10769 /*include_nondeduced_p=*/true);
10772 /* Returns true if the signature of DECL depends on any template parameter from
10773 its enclosing class. */
10776 uses_outer_template_parms (tree decl
)
10778 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10781 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10782 &depth
, NULL
, /*include_nondeduced_p=*/true))
10784 if (PRIMARY_TEMPLATE_P (decl
)
10785 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10786 (DECL_TEMPLATE_PARMS (decl
)),
10787 template_parm_outer_level
,
10788 &depth
, NULL
, /*include_nondeduced_p=*/true))
10790 tree ci
= get_constraints (decl
);
10792 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10793 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10794 &depth
, NULL
, /*nondeduced*/true))
10799 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10800 ill-formed translation unit, i.e. a variable or function that isn't
10801 usable in a constant expression. */
10804 neglectable_inst_p (tree d
)
10806 return (d
&& DECL_P (d
)
10807 && !undeduced_auto_decl (d
)
10808 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10809 : decl_maybe_constant_var_p (d
)));
10812 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10813 neglectable and instantiated from within an erroneous instantiation. */
10816 limit_bad_template_recursion (tree decl
)
10818 struct tinst_level
*lev
= current_tinst_level
;
10819 int errs
= errorcount
+ sorrycount
;
10820 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10823 for (; lev
; lev
= lev
->next
)
10824 if (neglectable_inst_p (lev
->maybe_get_node ()))
10827 return (lev
&& errs
> lev
->errors
);
10830 static int tinst_depth
;
10831 extern int max_tinst_depth
;
10834 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10836 /* We're starting to instantiate D; record the template instantiation context
10837 at LOC for diagnostics and to restore it later. */
10840 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10842 struct tinst_level
*new_level
;
10844 if (tinst_depth
>= max_tinst_depth
)
10846 /* Tell error.c not to try to instantiate any templates. */
10848 fatal_error (input_location
,
10849 "template instantiation depth exceeds maximum of %d"
10850 " (use %<-ftemplate-depth=%> to increase the maximum)",
10855 /* If the current instantiation caused problems, don't let it instantiate
10856 anything else. Do allow deduction substitution and decls usable in
10857 constant expressions. */
10858 if (!targs
&& limit_bad_template_recursion (tldcl
))
10860 /* Avoid no_linkage_errors and unused function warnings for this
10862 TREE_NO_WARNING (tldcl
) = 1;
10866 /* When not -quiet, dump template instantiations other than functions, since
10867 announce_function will take care of those. */
10868 if (!quiet_flag
&& !targs
10869 && TREE_CODE (tldcl
) != TREE_LIST
10870 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10871 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10873 new_level
= tinst_level_freelist ().alloc ();
10874 new_level
->tldcl
= tldcl
;
10875 new_level
->targs
= targs
;
10876 new_level
->locus
= loc
;
10877 new_level
->errors
= errorcount
+ sorrycount
;
10878 new_level
->next
= NULL
;
10879 new_level
->refcount
= 0;
10880 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10881 set_refcount_ptr (current_tinst_level
, new_level
);
10884 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10885 depth_reached
= tinst_depth
;
10890 /* We're starting substitution of TMPL<ARGS>; record the template
10891 substitution context for diagnostics and to restore it later. */
10894 push_tinst_level (tree tmpl
, tree args
)
10896 return push_tinst_level_loc (tmpl
, args
, input_location
);
10899 /* We're starting to instantiate D; record INPUT_LOCATION and the
10900 template instantiation context for diagnostics and to restore it
10904 push_tinst_level (tree d
)
10906 return push_tinst_level_loc (d
, input_location
);
10909 /* Likewise, but record LOC as the program location. */
10912 push_tinst_level_loc (tree d
, location_t loc
)
10914 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10915 return push_tinst_level_loc (d
, NULL
, loc
);
10918 /* We're done instantiating this template; return to the instantiation
10922 pop_tinst_level (void)
10924 /* Restore the filename and line number stashed away when we started
10925 this instantiation. */
10926 input_location
= current_tinst_level
->locus
;
10927 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10931 /* We're instantiating a deferred template; restore the template
10932 instantiation context in which the instantiation was requested, which
10933 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10936 reopen_tinst_level (struct tinst_level
*level
)
10938 struct tinst_level
*t
;
10941 for (t
= level
; t
; t
= t
->next
)
10944 set_refcount_ptr (current_tinst_level
, level
);
10945 pop_tinst_level ();
10946 if (current_tinst_level
)
10947 current_tinst_level
->errors
= errorcount
+sorrycount
;
10948 return level
->maybe_get_node ();
10951 /* Returns the TINST_LEVEL which gives the original instantiation
10954 struct tinst_level
*
10955 outermost_tinst_level (void)
10957 struct tinst_level
*level
= current_tinst_level
;
10959 while (level
->next
)
10960 level
= level
->next
;
10964 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10965 vector of template arguments, as for tsubst.
10967 Returns an appropriate tsubst'd friend declaration. */
10970 tsubst_friend_function (tree decl
, tree args
)
10974 if (TREE_CODE (decl
) == FUNCTION_DECL
10975 && DECL_TEMPLATE_INSTANTIATION (decl
)
10976 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10977 /* This was a friend declared with an explicit template
10978 argument list, e.g.:
10980 friend void f<>(T);
10982 to indicate that f was a template instantiation, not a new
10983 function declaration. Now, we have to figure out what
10984 instantiation of what template. */
10986 tree template_id
, arglist
, fns
;
10989 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10991 /* Friend functions are looked up in the containing namespace scope.
10992 We must enter that scope, to avoid finding member functions of the
10993 current class with same name. */
10994 push_nested_namespace (ns
);
10995 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10996 tf_warning_or_error
, NULL_TREE
,
10997 /*integral_constant_expression_p=*/false);
10998 pop_nested_namespace (ns
);
10999 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11000 tf_warning_or_error
, NULL_TREE
);
11001 template_id
= lookup_template_function (fns
, arglist
);
11003 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11004 tmpl
= determine_specialization (template_id
, new_friend
,
11006 /*need_member_template=*/0,
11007 TREE_VEC_LENGTH (args
),
11009 return instantiate_template (tmpl
, new_args
, tf_error
);
11012 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11013 if (new_friend
== error_mark_node
)
11014 return error_mark_node
;
11016 /* The NEW_FRIEND will look like an instantiation, to the
11017 compiler, but is not an instantiation from the point of view of
11018 the language. For example, we might have had:
11020 template <class T> struct S {
11021 template <class U> friend void f(T, U);
11024 Then, in S<int>, template <class U> void f(int, U) is not an
11025 instantiation of anything. */
11027 DECL_USE_TEMPLATE (new_friend
) = 0;
11028 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11030 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11031 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11032 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11034 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11035 match in decls_match. */
11036 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11037 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11038 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11039 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11042 /* The mangled name for the NEW_FRIEND is incorrect. The function
11043 is not a template instantiation and should not be mangled like
11044 one. Therefore, we forget the mangling here; we'll recompute it
11045 later if we need it. */
11046 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11048 SET_DECL_RTL (new_friend
, NULL
);
11049 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11052 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11057 /* We must save some information from NEW_FRIEND before calling
11058 duplicate decls since that function will free NEW_FRIEND if
11060 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11061 tree new_friend_result_template_info
= NULL_TREE
;
11062 bool new_friend_is_defn
=
11063 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11064 (template_for_substitution (new_friend
)))
11066 tree not_tmpl
= new_friend
;
11068 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11070 /* This declaration is a `primary' template. */
11071 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11073 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11074 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11077 /* Inside pushdecl_namespace_level, we will push into the
11078 current namespace. However, the friend function should go
11079 into the namespace of the template. */
11080 ns
= decl_namespace_context (new_friend
);
11081 push_nested_namespace (ns
);
11082 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11083 pop_nested_namespace (ns
);
11085 if (old_decl
== error_mark_node
)
11086 return error_mark_node
;
11088 if (old_decl
!= new_friend
)
11090 /* This new friend declaration matched an existing
11091 declaration. For example, given:
11093 template <class T> void f(T);
11094 template <class U> class C {
11095 template <class T> friend void f(T) {}
11098 the friend declaration actually provides the definition
11099 of `f', once C has been instantiated for some type. So,
11100 old_decl will be the out-of-class template declaration,
11101 while new_friend is the in-class definition.
11103 But, if `f' was called before this point, the
11104 instantiation of `f' will have DECL_TI_ARGS corresponding
11105 to `T' but not to `U', references to which might appear
11106 in the definition of `f'. Previously, the most general
11107 template for an instantiation of `f' was the out-of-class
11108 version; now it is the in-class version. Therefore, we
11109 run through all specialization of `f', adding to their
11110 DECL_TI_ARGS appropriately. In particular, they need a
11111 new set of outer arguments, corresponding to the
11112 arguments for this class instantiation.
11114 The same situation can arise with something like this:
11116 friend void f(int);
11117 template <class T> class C {
11118 friend void f(T) {}
11121 when `C<int>' is instantiated. Now, `f(int)' is defined
11124 if (!new_friend_is_defn
)
11125 /* On the other hand, if the in-class declaration does
11126 *not* provide a definition, then we don't want to alter
11127 existing definitions. We can just leave everything
11132 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11133 tree new_args
= TI_ARGS (new_friend_template_info
);
11135 /* Overwrite whatever template info was there before, if
11136 any, with the new template information pertaining to
11137 the declaration. */
11138 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11140 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11142 /* We should have called reregister_specialization in
11143 duplicate_decls. */
11144 gcc_assert (retrieve_specialization (new_template
,
11148 /* Instantiate it if the global has already been used. */
11149 if (DECL_ODR_USED (old_decl
))
11150 instantiate_decl (old_decl
, /*defer_ok=*/true,
11151 /*expl_inst_class_mem_p=*/false);
11157 /* Indicate that the old function template is a partial
11159 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11160 = new_friend_result_template_info
;
11162 gcc_assert (new_template
11163 == most_general_template (new_template
));
11164 gcc_assert (new_template
!= old_decl
);
11166 /* Reassign any specializations already in the hash table
11167 to the new more general template, and add the
11168 additional template args. */
11169 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11171 t
= TREE_CHAIN (t
))
11173 tree spec
= TREE_VALUE (t
);
11176 elt
.tmpl
= old_decl
;
11177 elt
.args
= DECL_TI_ARGS (spec
);
11178 elt
.spec
= NULL_TREE
;
11180 decl_specializations
->remove_elt (&elt
);
11182 DECL_TI_ARGS (spec
)
11183 = add_outermost_template_args (new_args
,
11184 DECL_TI_ARGS (spec
));
11186 register_specialization
11187 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11190 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11194 /* The information from NEW_FRIEND has been merged into OLD_DECL
11195 by duplicate_decls. */
11196 new_friend
= old_decl
;
11201 tree context
= DECL_CONTEXT (new_friend
);
11205 template <class T> class C {
11206 template <class U> friend void C1<U>::f (); // case 1
11207 friend void C2<T>::f (); // case 2
11209 we only need to make sure CONTEXT is a complete type for
11210 case 2. To distinguish between the two cases, we note that
11211 CONTEXT of case 1 remains dependent type after tsubst while
11212 this isn't true for case 2. */
11213 ++processing_template_decl
;
11214 dependent_p
= dependent_type_p (context
);
11215 --processing_template_decl
;
11218 && !complete_type_or_else (context
, NULL_TREE
))
11219 return error_mark_node
;
11221 if (COMPLETE_TYPE_P (context
))
11223 tree fn
= new_friend
;
11224 /* do_friend adds the TEMPLATE_DECL for any member friend
11225 template even if it isn't a member template, i.e.
11226 template <class T> friend A<T>::f();
11227 Look through it in that case. */
11228 if (TREE_CODE (fn
) == TEMPLATE_DECL
11229 && !PRIMARY_TEMPLATE_P (fn
))
11230 fn
= DECL_TEMPLATE_RESULT (fn
);
11231 /* Check to see that the declaration is really present, and,
11232 possibly obtain an improved declaration. */
11233 fn
= check_classfn (context
, fn
, NULL_TREE
);
11243 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11244 template arguments, as for tsubst.
11246 Returns an appropriate tsubst'd friend type or error_mark_node on
11250 tsubst_friend_class (tree friend_tmpl
, tree args
)
11254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11256 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11257 return TREE_TYPE (tmpl
);
11260 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11261 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11262 push_nested_namespace (context
);
11265 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11266 push_nested_class (context
);
11269 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11270 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11272 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11274 /* The friend template has already been declared. Just
11275 check to see that the declarations match, and install any new
11276 default parameters. We must tsubst the default parameters,
11277 of course. We only need the innermost template parameters
11278 because that is all that redeclare_class_template will look
11280 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11281 > TMPL_ARGS_DEPTH (args
))
11283 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11284 args
, tf_warning_or_error
);
11285 location_t saved_input_location
= input_location
;
11286 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11287 tree cons
= get_constraints (tmpl
);
11288 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11289 input_location
= saved_input_location
;
11294 /* The friend template has not already been declared. In this
11295 case, the instantiation of the template class will cause the
11296 injection of this template into the namespace scope. */
11297 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11299 if (tmpl
!= error_mark_node
)
11301 /* The new TMPL is not an instantiation of anything, so we
11302 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11303 for the new type because that is supposed to be the
11304 corresponding template decl, i.e., TMPL. */
11305 DECL_USE_TEMPLATE (tmpl
) = 0;
11306 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11307 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11308 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11309 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11311 /* Substitute into and set the constraints on the new declaration. */
11312 if (tree ci
= get_constraints (friend_tmpl
))
11314 ++processing_template_decl
;
11315 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11316 DECL_FRIEND_CONTEXT (friend_tmpl
));
11317 --processing_template_decl
;
11318 set_constraints (tmpl
, ci
);
11321 /* Inject this template into the enclosing namspace scope. */
11322 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11326 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11327 pop_nested_namespace (context
);
11329 pop_nested_class ();
11331 return TREE_TYPE (tmpl
);
11334 /* Returns zero if TYPE cannot be completed later due to circularity.
11335 Otherwise returns one. */
11338 can_complete_type_without_circularity (tree type
)
11340 if (type
== NULL_TREE
|| type
== error_mark_node
)
11342 else if (COMPLETE_TYPE_P (type
))
11344 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11345 return can_complete_type_without_circularity (TREE_TYPE (type
));
11346 else if (CLASS_TYPE_P (type
)
11347 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11353 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11354 tsubst_flags_t
, tree
);
11356 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11357 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11360 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11361 tsubst_flags_t complain
, tree in_decl
)
11363 gcc_assert (ATTR_IS_DEPENDENT (t
));
11365 tree val
= TREE_VALUE (t
);
11366 if (val
== NULL_TREE
)
11367 /* Nothing to do. */;
11368 else if ((flag_openmp
|| flag_openmp_simd
)
11369 && is_attribute_p ("omp declare simd",
11370 get_attribute_name (t
)))
11372 tree clauses
= TREE_VALUE (val
);
11373 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11374 complain
, in_decl
);
11375 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11376 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11377 tree parms
= DECL_ARGUMENTS (*decl_p
);
11379 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11381 val
= build_tree_list (NULL_TREE
, clauses
);
11385 else if (flag_openmp
11386 && is_attribute_p ("omp declare variant base",
11387 get_attribute_name (t
)))
11389 ++cp_unevaluated_operand
;
11391 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11392 in_decl
, /*integral_constant_expression_p=*/false);
11393 --cp_unevaluated_operand
;
11394 tree chain
= TREE_CHAIN (val
);
11395 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11396 tree ctx
= copy_list (TREE_VALUE (val
));
11397 tree simd
= get_identifier ("simd");
11398 tree score
= get_identifier (" score");
11399 tree condition
= get_identifier ("condition");
11400 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11402 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11403 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11404 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11406 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11408 tree clauses
= TREE_VALUE (t2
);
11409 clauses
= tsubst_omp_clauses (clauses
,
11410 C_ORT_OMP_DECLARE_SIMD
, args
,
11411 complain
, in_decl
);
11412 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11413 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11414 TREE_VALUE (t2
) = clauses
;
11418 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11419 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11420 if (TREE_VALUE (t3
))
11423 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11424 && TREE_PURPOSE (t3
) != score
);
11425 tree v
= TREE_VALUE (t3
);
11426 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11428 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11429 v
= fold_non_dependent_expr (v
);
11430 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11431 || (TREE_PURPOSE (t3
) == score
11432 ? TREE_CODE (v
) != INTEGER_CST
11433 : !tree_fits_shwi_p (v
)))
11436 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11438 if (TREE_PURPOSE (t3
) == score
)
11439 error_at (loc
, "score argument must be "
11440 "constant integer expression");
11441 else if (allow_string
)
11442 error_at (loc
, "property must be constant "
11443 "integer expression or string "
11446 error_at (loc
, "property must be constant "
11447 "integer expression");
11450 else if (TREE_PURPOSE (t3
) == score
11451 && tree_int_cst_sgn (v
) < 0)
11454 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11456 error_at (loc
, "score argument must be "
11460 TREE_VALUE (t3
) = v
;
11465 val
= tree_cons (varid
, ctx
, chain
);
11467 /* If the first attribute argument is an identifier, don't
11468 pass it through tsubst. Attributes like mode, format,
11469 cleanup and several target specific attributes expect it
11471 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11474 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11475 /*integral_constant_expression_p=*/false);
11476 if (chain
!= TREE_CHAIN (val
))
11477 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11479 else if (PACK_EXPANSION_P (val
))
11481 /* An attribute pack expansion. */
11482 tree purp
= TREE_PURPOSE (t
);
11483 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11484 if (pack
== error_mark_node
)
11485 return error_mark_node
;
11486 int len
= TREE_VEC_LENGTH (pack
);
11487 tree list
= NULL_TREE
;
11489 for (int i
= 0; i
< len
; ++i
)
11491 tree elt
= TREE_VEC_ELT (pack
, i
);
11492 *q
= build_tree_list (purp
, elt
);
11493 q
= &TREE_CHAIN (*q
);
11498 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11499 /*integral_constant_expression_p=*/false);
11501 if (val
!= TREE_VALUE (t
))
11502 return build_tree_list (TREE_PURPOSE (t
), val
);
11506 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11507 unchanged or a new TREE_LIST chain. */
11510 tsubst_attributes (tree attributes
, tree args
,
11511 tsubst_flags_t complain
, tree in_decl
)
11513 tree last_dep
= NULL_TREE
;
11515 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11516 if (ATTR_IS_DEPENDENT (t
))
11519 attributes
= copy_list (attributes
);
11524 for (tree
*p
= &attributes
; *p
; )
11527 if (ATTR_IS_DEPENDENT (t
))
11529 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11534 p
= &TREE_CHAIN (*p
);
11535 *p
= TREE_CHAIN (t
);
11539 p
= &TREE_CHAIN (*p
);
11545 /* Apply any attributes which had to be deferred until instantiation
11546 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11547 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11550 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11551 tree args
, tsubst_flags_t complain
, tree in_decl
)
11553 tree last_dep
= NULL_TREE
;
11557 if (attributes
== NULL_TREE
)
11560 if (DECL_P (*decl_p
))
11562 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11564 p
= &DECL_ATTRIBUTES (*decl_p
);
11565 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11566 to our attributes parameter. */
11567 gcc_assert (*p
== attributes
);
11571 p
= &TYPE_ATTRIBUTES (*decl_p
);
11572 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11573 lookup_template_class_1, and should be preserved. */
11574 gcc_assert (*p
!= attributes
);
11576 p
= &TREE_CHAIN (*p
);
11579 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11580 if (ATTR_IS_DEPENDENT (t
))
11583 attributes
= copy_list (attributes
);
11590 tree late_attrs
= NULL_TREE
;
11591 tree
*q
= &late_attrs
;
11596 if (ATTR_IS_DEPENDENT (t
))
11598 *p
= TREE_CHAIN (t
);
11599 TREE_CHAIN (t
) = NULL_TREE
;
11600 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11602 q
= &TREE_CHAIN (*q
);
11605 p
= &TREE_CHAIN (t
);
11608 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11612 /* The template TMPL is being instantiated with the template arguments TARGS.
11613 Perform the access checks that we deferred when parsing the template. */
11616 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11619 deferred_access_check
*chk
;
11621 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11624 if (vec
<deferred_access_check
, va_gc
> *access_checks
11625 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11626 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11628 tree decl
= chk
->decl
;
11629 tree diag_decl
= chk
->diag_decl
;
11630 tree type_scope
= TREE_TYPE (chk
->binfo
);
11632 if (uses_template_parms (type_scope
))
11633 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11635 /* Make access check error messages point to the location
11636 of the use of the typedef. */
11637 iloc_sentinel
ils (chk
->loc
);
11638 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11639 decl
, diag_decl
, tf_warning_or_error
);
11644 instantiate_class_template_1 (tree type
)
11646 tree templ
, args
, pattern
, t
, member
;
11650 unsigned int saved_maximum_field_alignment
;
11653 if (type
== error_mark_node
)
11654 return error_mark_node
;
11656 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11657 || uses_template_parms (type
))
11660 /* Figure out which template is being instantiated. */
11661 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11662 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11664 /* Mark the type as in the process of being defined. */
11665 TYPE_BEING_DEFINED (type
) = 1;
11667 /* We may be in the middle of deferred access check. Disable
11669 deferring_access_check_sentinel
acs (dk_no_deferred
);
11671 /* Determine what specialization of the original template to
11673 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11674 if (t
== error_mark_node
)
11675 return error_mark_node
;
11678 /* This TYPE is actually an instantiation of a partial
11679 specialization. We replace the innermost set of ARGS with
11680 the arguments appropriate for substitution. For example,
11683 template <class T> struct S {};
11684 template <class T> struct S<T*> {};
11686 and supposing that we are instantiating S<int*>, ARGS will
11687 presently be {int*} -- but we need {int}. */
11688 pattern
= TREE_TYPE (t
);
11689 args
= TREE_PURPOSE (t
);
11693 pattern
= TREE_TYPE (templ
);
11694 args
= CLASSTYPE_TI_ARGS (type
);
11697 /* If the template we're instantiating is incomplete, then clearly
11698 there's nothing we can do. */
11699 if (!COMPLETE_TYPE_P (pattern
))
11701 /* We can try again later. */
11702 TYPE_BEING_DEFINED (type
) = 0;
11706 /* If we've recursively instantiated too many templates, stop. */
11707 if (! push_tinst_level (type
))
11710 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11711 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11713 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11714 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11715 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11716 fn_context
= error_mark_node
;
11718 push_to_top_level ();
11721 cp_unevaluated_operand
= 0;
11722 c_inhibit_evaluation_warnings
= 0;
11724 /* Use #pragma pack from the template context. */
11725 saved_maximum_field_alignment
= maximum_field_alignment
;
11726 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11728 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11730 /* Set the input location to the most specialized template definition.
11731 This is needed if tsubsting causes an error. */
11732 typedecl
= TYPE_MAIN_DECL (pattern
);
11733 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11734 DECL_SOURCE_LOCATION (typedecl
);
11736 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11737 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11738 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11739 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11740 if (ANON_AGGR_TYPE_P (pattern
))
11741 SET_ANON_AGGR_TYPE_P (type
);
11742 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11744 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11745 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11746 /* Adjust visibility for template arguments. */
11747 determine_visibility (TYPE_MAIN_DECL (type
));
11749 if (CLASS_TYPE_P (type
))
11750 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11752 pbinfo
= TYPE_BINFO (pattern
);
11754 /* We should never instantiate a nested class before its enclosing
11755 class; we need to look up the nested class by name before we can
11756 instantiate it, and that lookup should instantiate the enclosing
11758 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11759 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11761 base_list
= NULL_TREE
;
11762 if (BINFO_N_BASE_BINFOS (pbinfo
))
11768 /* We must enter the scope containing the type, as that is where
11769 the accessibility of types named in dependent bases are
11771 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11773 /* Substitute into each of the bases to determine the actual
11775 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11778 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11779 tree expanded_bases
= NULL_TREE
;
11782 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11785 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11786 args
, tf_error
, NULL_TREE
);
11787 if (expanded_bases
== error_mark_node
)
11790 len
= TREE_VEC_LENGTH (expanded_bases
);
11793 for (idx
= 0; idx
< len
; idx
++)
11795 if (expanded_bases
)
11796 /* Extract the already-expanded base class. */
11797 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11799 /* Substitute to figure out the base class. */
11800 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11803 if (base
== error_mark_node
)
11806 base_list
= tree_cons (access
, base
, base_list
);
11807 if (BINFO_VIRTUAL_P (pbase_binfo
))
11808 TREE_TYPE (base_list
) = integer_type_node
;
11812 /* The list is now in reverse order; correct that. */
11813 base_list
= nreverse (base_list
);
11816 pop_scope (pushed_scope
);
11818 /* Now call xref_basetypes to set up all the base-class
11820 xref_basetypes (type
, base_list
);
11822 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11823 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11824 args
, tf_error
, NULL_TREE
);
11825 fixup_attribute_variants (type
);
11827 /* Now that our base classes are set up, enter the scope of the
11828 class, so that name lookups into base classes, etc. will work
11829 correctly. This is precisely analogous to what we do in
11830 begin_class_definition when defining an ordinary non-template
11831 class, except we also need to push the enclosing classes. */
11832 push_nested_class (type
);
11834 /* Now members are processed in the order of declaration. */
11835 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11836 member
; member
= TREE_CHAIN (member
))
11838 tree t
= TREE_VALUE (member
);
11840 if (TREE_PURPOSE (member
))
11844 if (LAMBDA_TYPE_P (t
))
11845 /* A closure type for a lambda in an NSDMI or default argument.
11846 Ignore it; it will be regenerated when needed. */
11849 /* Build new CLASSTYPE_NESTED_UTDS. */
11850 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11851 && TYPE_LANG_SPECIFIC (t
)
11852 && CLASSTYPE_IS_TEMPLATE (t
));
11854 /* If the member is a class template, then -- even after
11855 substitution -- there may be dependent types in the
11856 template argument list for the class. We increment
11857 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11858 that function will assume that no types are dependent
11859 when outside of a template. */
11860 if (class_template_p
)
11861 ++processing_template_decl
;
11862 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11863 if (class_template_p
)
11864 --processing_template_decl
;
11865 if (newtag
== error_mark_node
)
11868 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11870 tree name
= TYPE_IDENTIFIER (t
);
11872 if (class_template_p
)
11873 /* Unfortunately, lookup_template_class sets
11874 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11875 instantiation (i.e., for the type of a member
11876 template class nested within a template class.)
11877 This behavior is required for
11878 maybe_process_partial_specialization to work
11879 correctly, but is not accurate in this case;
11880 the TAG is not an instantiation of anything.
11881 (The corresponding TEMPLATE_DECL is an
11882 instantiation, but the TYPE is not.) */
11883 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11885 /* Now, we call pushtag to put this NEWTAG into the scope of
11886 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11887 pushtag calling push_template_decl. We don't have to do
11888 this for enums because it will already have been done in
11891 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11892 pushtag (name
, newtag
);
11895 else if (DECL_DECLARES_FUNCTION_P (t
))
11899 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11900 ++processing_template_decl
;
11901 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11902 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11903 --processing_template_decl
;
11904 set_current_access_from_decl (r
);
11905 finish_member_declaration (r
);
11906 /* Instantiate members marked with attribute used. */
11907 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11909 if (TREE_CODE (r
) == FUNCTION_DECL
11910 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11911 cp_check_omp_declare_reduction (r
);
11913 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11914 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11915 /* A closure type for a lambda in an NSDMI or default argument.
11916 Ignore it; it will be regenerated when needed. */;
11919 /* Build new TYPE_FIELDS. */
11920 if (TREE_CODE (t
) == STATIC_ASSERT
)
11921 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11922 /*integral_constant_expression_p=*/true);
11923 else if (TREE_CODE (t
) != CONST_DECL
)
11926 tree vec
= NULL_TREE
;
11929 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11930 /* The file and line for this declaration, to
11931 assist in error message reporting. Since we
11932 called push_tinst_level above, we don't need to
11934 input_location
= DECL_SOURCE_LOCATION (t
);
11936 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11937 ++processing_template_decl
;
11938 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11939 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11940 --processing_template_decl
;
11942 if (TREE_CODE (r
) == TREE_VEC
)
11944 /* A capture pack became multiple fields. */
11946 len
= TREE_VEC_LENGTH (vec
);
11949 for (int i
= 0; i
< len
; ++i
)
11952 r
= TREE_VEC_ELT (vec
, i
);
11957 [t]he initialization (and any associated
11958 side-effects) of a static data member does
11959 not occur unless the static data member is
11960 itself used in a way that requires the
11961 definition of the static data member to
11964 Therefore, we do not substitute into the
11965 initialized for the static data member here. */
11966 finish_static_data_member_decl
11968 /*init=*/NULL_TREE
,
11969 /*init_const_expr_p=*/false,
11970 /*asmspec_tree=*/NULL_TREE
,
11972 /* Instantiate members marked with attribute used. */
11973 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11976 else if (TREE_CODE (r
) == FIELD_DECL
)
11978 /* Determine whether R has a valid type and can be
11979 completed later. If R is invalid, then its type
11980 is replaced by error_mark_node. */
11981 tree rtype
= TREE_TYPE (r
);
11982 if (can_complete_type_without_circularity (rtype
))
11983 complete_type (rtype
);
11985 if (!complete_or_array_type_p (rtype
))
11987 /* If R's type couldn't be completed and
11988 it isn't a flexible array member (whose
11989 type is incomplete by definition) give
11991 cxx_incomplete_type_error (r
, rtype
);
11992 TREE_TYPE (r
) = error_mark_node
;
11994 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11995 && TYPE_DOMAIN (rtype
) == NULL_TREE
11996 && (TREE_CODE (type
) == UNION_TYPE
11997 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11999 error ("flexible array member %qD in union", r
);
12000 TREE_TYPE (r
) = error_mark_node
;
12002 else if (!verify_type_context (input_location
,
12003 TCTX_FIELD
, rtype
))
12004 TREE_TYPE (r
) = error_mark_node
;
12007 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
12008 such a thing will already have been added to the field
12009 list by tsubst_enum in finish_member_declaration in the
12010 CLASSTYPE_NESTED_UTDS case above. */
12011 if (!(TREE_CODE (r
) == TYPE_DECL
12012 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12013 && DECL_ARTIFICIAL (r
)))
12015 set_current_access_from_decl (r
);
12016 finish_member_declaration (r
);
12024 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12025 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12027 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12029 tree friend_type
= t
;
12030 bool adjust_processing_template_decl
= false;
12032 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12034 /* template <class T> friend class C; */
12035 friend_type
= tsubst_friend_class (friend_type
, args
);
12036 adjust_processing_template_decl
= true;
12038 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12040 /* template <class T> friend class C::D; */
12041 friend_type
= tsubst (friend_type
, args
,
12042 tf_warning_or_error
, NULL_TREE
);
12043 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12044 friend_type
= TREE_TYPE (friend_type
);
12045 adjust_processing_template_decl
= true;
12047 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12048 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12050 /* This could be either
12054 when dependent_type_p is false or
12056 template <class U> friend class T::C;
12059 /* Bump processing_template_decl in case this is something like
12060 template <class T> friend struct A<T>::B. */
12061 ++processing_template_decl
;
12062 friend_type
= tsubst (friend_type
, args
,
12063 tf_warning_or_error
, NULL_TREE
);
12064 if (dependent_type_p (friend_type
))
12065 adjust_processing_template_decl
= true;
12066 --processing_template_decl
;
12068 else if (uses_template_parms (friend_type
))
12069 /* friend class C<T>; */
12070 friend_type
= tsubst (friend_type
, args
,
12071 tf_warning_or_error
, NULL_TREE
);
12077 where C is already declared or
12079 friend class C<int>;
12081 We don't have to do anything in these cases. */
12083 if (adjust_processing_template_decl
)
12084 /* Trick make_friend_class into realizing that the friend
12085 we're adding is a template, not an ordinary class. It's
12086 important that we use make_friend_class since it will
12087 perform some error-checking and output cross-reference
12089 ++processing_template_decl
;
12091 if (friend_type
!= error_mark_node
)
12092 make_friend_class (type
, friend_type
, /*complain=*/false);
12094 if (adjust_processing_template_decl
)
12095 --processing_template_decl
;
12099 /* Build new DECL_FRIENDLIST. */
12102 /* The file and line for this declaration, to
12103 assist in error message reporting. Since we
12104 called push_tinst_level above, we don't need to
12106 input_location
= DECL_SOURCE_LOCATION (t
);
12108 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12110 ++processing_template_decl
;
12111 push_deferring_access_checks (dk_no_check
);
12114 r
= tsubst_friend_function (t
, args
);
12115 add_friend (type
, r
, /*complain=*/false);
12116 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12118 pop_deferring_access_checks ();
12119 --processing_template_decl
;
12127 /* Restore these before substituting into the lambda capture
12129 cp_unevaluated_operand
= saved_unevaluated_operand
;
12130 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12133 /* Set the file and line number information to whatever is given for
12134 the class itself. This puts error messages involving generated
12135 implicit functions at a predictable point, and the same point
12136 that would be used for non-template classes. */
12137 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12139 unreverse_member_declarations (type
);
12140 finish_struct_1 (type
);
12141 TYPE_BEING_DEFINED (type
) = 0;
12143 /* We don't instantiate default arguments for member functions. 14.7.1:
12145 The implicit instantiation of a class template specialization causes
12146 the implicit instantiation of the declarations, but not of the
12147 definitions or default arguments, of the class member functions,
12148 member classes, static data members and member templates.... */
12150 perform_instantiation_time_access_checks (pattern
, args
);
12151 perform_deferred_access_checks (tf_warning_or_error
);
12152 pop_nested_class ();
12153 maximum_field_alignment
= saved_maximum_field_alignment
;
12155 pop_from_top_level ();
12156 pop_tinst_level ();
12158 /* The vtable for a template class can be emitted in any translation
12159 unit in which the class is instantiated. When there is no key
12160 method, however, finish_struct_1 will already have added TYPE to
12161 the keyed_classes. */
12162 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12163 vec_safe_push (keyed_classes
, type
);
12168 /* Wrapper for instantiate_class_template_1. */
12171 instantiate_class_template (tree type
)
12174 timevar_push (TV_TEMPLATE_INST
);
12175 ret
= instantiate_class_template_1 (type
);
12176 timevar_pop (TV_TEMPLATE_INST
);
12181 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12187 else if (TYPE_P (t
))
12188 r
= tsubst (t
, args
, complain
, in_decl
);
12191 if (!(complain
& tf_warning
))
12192 ++c_inhibit_evaluation_warnings
;
12193 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12194 /*integral_constant_expression_p=*/true);
12195 if (!(complain
& tf_warning
))
12196 --c_inhibit_evaluation_warnings
;
12202 /* Given a function parameter pack TMPL_PARM and some function parameters
12203 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12204 and set *SPEC_P to point at the next point in the list. */
12207 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12209 /* Collect all of the extra "packed" parameters into an
12212 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12213 tree spec_parm
= *spec_p
;
12216 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12218 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12221 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12222 parmvec
= make_tree_vec (len
);
12223 spec_parm
= *spec_p
;
12224 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12226 tree elt
= spec_parm
;
12227 if (DECL_PACK_P (elt
))
12228 elt
= make_pack_expansion (elt
);
12229 TREE_VEC_ELT (parmvec
, i
) = elt
;
12232 /* Build the argument packs. */
12233 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12234 *spec_p
= spec_parm
;
12239 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12240 NONTYPE_ARGUMENT_PACK. */
12243 make_fnparm_pack (tree spec_parm
)
12245 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12248 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12249 pack expansion with no extra args, 2 if it has extra args, or 0
12250 if it is not a pack expansion. */
12253 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12255 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12256 /* We're being called before this happens in tsubst_pack_expansion. */
12257 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12258 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12259 if (i
>= TREE_VEC_LENGTH (vec
))
12261 tree elt
= TREE_VEC_ELT (vec
, i
);
12263 /* A decl pack is itself an expansion. */
12264 elt
= TREE_TYPE (elt
);
12265 if (!PACK_EXPANSION_P (elt
))
12267 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12273 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12276 make_argument_pack_select (tree arg_pack
, unsigned index
)
12278 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12280 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12281 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12286 /* This is a subroutine of tsubst_pack_expansion.
12288 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12289 mechanism to store the (non complete list of) arguments of the
12290 substitution and return a non substituted pack expansion, in order
12291 to wait for when we have enough arguments to really perform the
12295 use_pack_expansion_extra_args_p (tree parm_packs
,
12297 bool has_empty_arg
)
12299 /* If one pack has an expansion and another pack has a normal
12300 argument or if one pack has an empty argument and an another
12301 one hasn't then tsubst_pack_expansion cannot perform the
12302 substitution and need to fall back on the
12303 PACK_EXPANSION_EXTRA mechanism. */
12304 if (parm_packs
== NULL_TREE
)
12306 else if (has_empty_arg
)
12308 /* If all the actual packs are pack expansions, we can still
12309 subsitute directly. */
12310 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12312 tree a
= TREE_VALUE (p
);
12313 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12314 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12315 a
= ARGUMENT_PACK_ARGS (a
);
12316 if (TREE_VEC_LENGTH (a
) == 1)
12317 a
= TREE_VEC_ELT (a
, 0);
12318 if (PACK_EXPANSION_P (a
))
12325 bool has_expansion_arg
= false;
12326 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12328 bool has_non_expansion_arg
= false;
12329 for (tree parm_pack
= parm_packs
;
12331 parm_pack
= TREE_CHAIN (parm_pack
))
12333 tree arg
= TREE_VALUE (parm_pack
);
12335 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12337 /* We can't substitute a pack expansion with extra args into
12341 has_expansion_arg
= true;
12343 has_non_expansion_arg
= true;
12346 if (has_expansion_arg
&& has_non_expansion_arg
)
12352 /* [temp.variadic]/6 says that:
12354 The instantiation of a pack expansion [...]
12355 produces a list E1,E2, ..., En, where N is the number of elements
12356 in the pack expansion parameters.
12358 This subroutine of tsubst_pack_expansion produces one of these Ei.
12360 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12361 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12362 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12363 INDEX is the index 'i' of the element Ei to produce. ARGS,
12364 COMPLAIN, and IN_DECL are the same parameters as for the
12365 tsubst_pack_expansion function.
12367 The function returns the resulting Ei upon successful completion,
12368 or error_mark_node.
12370 Note that this function possibly modifies the ARGS parameter, so
12371 it's the responsibility of the caller to restore it. */
12374 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12377 tree args
/* This parm gets
12379 tsubst_flags_t complain
,
12383 bool ith_elem_is_expansion
= false;
12385 /* For each parameter pack, change the substitution of the parameter
12386 pack to the ith argument in its argument pack, then expand the
12388 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12390 tree parm
= TREE_PURPOSE (pack
);
12391 tree arg_pack
= TREE_VALUE (pack
);
12392 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12394 ith_elem_is_expansion
|=
12395 argument_pack_element_is_expansion_p (arg_pack
, index
);
12397 /* Select the Ith argument from the pack. */
12398 if (TREE_CODE (parm
) == PARM_DECL
12400 || TREE_CODE (parm
) == FIELD_DECL
)
12404 aps
= make_argument_pack_select (arg_pack
, index
);
12405 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12406 return error_mark_node
;
12407 register_local_specialization (aps
, parm
);
12410 aps
= retrieve_local_specialization (parm
);
12415 template_parm_level_and_index (parm
, &level
, &idx
);
12419 aps
= make_argument_pack_select (arg_pack
, index
);
12420 /* Update the corresponding argument. */
12421 TMPL_ARG (args
, level
, idx
) = aps
;
12424 /* Re-use the ARGUMENT_PACK_SELECT. */
12425 aps
= TMPL_ARG (args
, level
, idx
);
12427 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12430 /* Substitute into the PATTERN with the (possibly altered)
12432 if (pattern
== in_decl
)
12433 /* Expanding a fixed parameter pack from
12434 coerce_template_parameter_pack. */
12435 t
= tsubst_decl (pattern
, args
, complain
);
12436 else if (pattern
== error_mark_node
)
12437 t
= error_mark_node
;
12438 else if (!TYPE_P (pattern
))
12439 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12440 /*integral_constant_expression_p=*/false);
12442 t
= tsubst (pattern
, args
, complain
, in_decl
);
12444 /* If the Ith argument pack element is a pack expansion, then
12445 the Ith element resulting from the substituting is going to
12446 be a pack expansion as well. */
12447 if (ith_elem_is_expansion
)
12448 t
= make_pack_expansion (t
, complain
);
12453 /* When the unexpanded parameter pack in a fold expression expands to an empty
12454 sequence, the value of the expression is as follows; the program is
12455 ill-formed if the operator is not listed in this table.
12462 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12464 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12465 if (!FOLD_EXPR_MODIFY_P (t
))
12468 case TRUTH_ANDIF_EXPR
:
12469 return boolean_true_node
;
12470 case TRUTH_ORIF_EXPR
:
12471 return boolean_false_node
;
12472 case COMPOUND_EXPR
:
12478 if (complain
& tf_error
)
12479 error_at (location_of (t
),
12480 "fold of empty expansion over %O", code
);
12481 return error_mark_node
;
12484 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12485 form an expression that combines the two terms using the
12489 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12491 tree op
= FOLD_EXPR_OP (t
);
12492 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12494 // Handle compound assignment operators.
12495 if (FOLD_EXPR_MODIFY_P (t
))
12496 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12498 warning_sentinel
s(warn_parentheses
);
12501 case COMPOUND_EXPR
:
12502 return build_x_compound_expr (input_location
, left
, right
, complain
);
12504 return build_x_binary_op (input_location
, code
,
12505 left
, TREE_CODE (left
),
12506 right
, TREE_CODE (right
),
12512 /* Substitute ARGS into the pack of a fold expression T. */
12515 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12517 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12520 /* Substitute ARGS into the pack of a fold expression T. */
12523 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12525 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12528 /* Expand a PACK of arguments into a grouped as left fold.
12529 Given a pack containing elements A0, A1, ..., An and an
12530 operator @, this builds the expression:
12532 ((A0 @ A1) @ A2) ... @ An
12534 Note that PACK must not be empty.
12536 The operator is defined by the original fold expression T. */
12539 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12541 tree left
= TREE_VEC_ELT (pack
, 0);
12542 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12544 tree right
= TREE_VEC_ELT (pack
, i
);
12545 left
= fold_expression (t
, left
, right
, complain
);
12550 /* Substitute into a unary left fold expression. */
12553 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12556 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12557 if (pack
== error_mark_node
)
12558 return error_mark_node
;
12559 if (PACK_EXPANSION_P (pack
))
12561 tree r
= copy_node (t
);
12562 FOLD_EXPR_PACK (r
) = pack
;
12565 if (TREE_VEC_LENGTH (pack
) == 0)
12566 return expand_empty_fold (t
, complain
);
12568 return expand_left_fold (t
, pack
, complain
);
12571 /* Substitute into a binary left fold expression.
12573 Do ths by building a single (non-empty) vector of argumnts and
12574 building the expression from those elements. */
12577 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12580 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12581 if (pack
== error_mark_node
)
12582 return error_mark_node
;
12583 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12584 if (init
== error_mark_node
)
12585 return error_mark_node
;
12587 if (PACK_EXPANSION_P (pack
))
12589 tree r
= copy_node (t
);
12590 FOLD_EXPR_PACK (r
) = pack
;
12591 FOLD_EXPR_INIT (r
) = init
;
12595 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12596 TREE_VEC_ELT (vec
, 0) = init
;
12597 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12598 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12600 return expand_left_fold (t
, vec
, complain
);
12603 /* Expand a PACK of arguments into a grouped as right fold.
12604 Given a pack containing elementns A0, A1, ..., and an
12605 operator @, this builds the expression:
12607 A0@ ... (An-2 @ (An-1 @ An))
12609 Note that PACK must not be empty.
12611 The operator is defined by the original fold expression T. */
12614 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12616 // Build the expression.
12617 int n
= TREE_VEC_LENGTH (pack
);
12618 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12619 for (--n
; n
!= 0; --n
)
12621 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12622 right
= fold_expression (t
, left
, right
, complain
);
12627 /* Substitute into a unary right fold expression. */
12630 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12633 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12634 if (pack
== error_mark_node
)
12635 return error_mark_node
;
12636 if (PACK_EXPANSION_P (pack
))
12638 tree r
= copy_node (t
);
12639 FOLD_EXPR_PACK (r
) = pack
;
12642 if (TREE_VEC_LENGTH (pack
) == 0)
12643 return expand_empty_fold (t
, complain
);
12645 return expand_right_fold (t
, pack
, complain
);
12648 /* Substitute into a binary right fold expression.
12650 Do ths by building a single (non-empty) vector of arguments and
12651 building the expression from those elements. */
12654 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12657 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12658 if (pack
== error_mark_node
)
12659 return error_mark_node
;
12660 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12661 if (init
== error_mark_node
)
12662 return error_mark_node
;
12664 if (PACK_EXPANSION_P (pack
))
12666 tree r
= copy_node (t
);
12667 FOLD_EXPR_PACK (r
) = pack
;
12668 FOLD_EXPR_INIT (r
) = init
;
12672 int n
= TREE_VEC_LENGTH (pack
);
12673 tree vec
= make_tree_vec (n
+ 1);
12674 for (int i
= 0; i
< n
; ++i
)
12675 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12676 TREE_VEC_ELT (vec
, n
) = init
;
12678 return expand_right_fold (t
, vec
, complain
);
12681 /* Walk through the pattern of a pack expansion, adding everything in
12682 local_specializations to a list. */
12687 hash_set
<tree
> internal
;
12689 tsubst_flags_t complain
;
12691 el_data (tsubst_flags_t c
)
12692 : extra (NULL_TREE
), complain (c
) {}
12695 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12697 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12698 tree
*extra
= &data
.extra
;
12699 tsubst_flags_t complain
= data
.complain
;
12701 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12702 /* Remember local typedefs (85214). */
12703 tp
= &TYPE_NAME (*tp
);
12705 if (TREE_CODE (*tp
) == DECL_EXPR
)
12706 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12707 else if (tree spec
= retrieve_local_specialization (*tp
))
12709 if (data
.internal
.contains (*tp
))
12710 /* Don't mess with variables declared within the pattern. */
12712 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12714 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12715 tree args
= ARGUMENT_PACK_ARGS (spec
);
12716 if (TREE_VEC_LENGTH (args
) == 1)
12718 tree elt
= TREE_VEC_ELT (args
, 0);
12719 if (PACK_EXPANSION_P (elt
))
12720 elt
= PACK_EXPANSION_PATTERN (elt
);
12721 if (DECL_PACK_P (elt
))
12724 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12726 /* Handle lambda capture here, since we aren't doing any
12727 substitution now, and so tsubst_copy won't call
12728 process_outer_var_ref. */
12729 tree args
= ARGUMENT_PACK_ARGS (spec
);
12730 int len
= TREE_VEC_LENGTH (args
);
12731 for (int i
= 0; i
< len
; ++i
)
12733 tree arg
= TREE_VEC_ELT (args
, i
);
12735 if (outer_automatic_var_p (arg
))
12736 carg
= process_outer_var_ref (arg
, complain
);
12739 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12743 spec
= copy_node (spec
);
12744 args
= copy_node (args
);
12745 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12746 register_local_specialization (spec
, *tp
);
12748 TREE_VEC_ELT (args
, i
) = carg
;
12753 if (outer_automatic_var_p (spec
))
12754 spec
= process_outer_var_ref (spec
, complain
);
12755 *extra
= tree_cons (*tp
, spec
, *extra
);
12760 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12762 el_data
data (complain
);
12763 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12767 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12768 for use in PACK_EXPANSION_EXTRA_ARGS. */
12771 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12774 if (local_specializations
)
12775 if (tree locals
= extract_local_specs (pattern
, complain
))
12776 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12780 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12781 normal template args to ARGS. */
12784 add_extra_args (tree extra
, tree args
)
12786 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12788 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12790 /* The partial instantiation involved local declarations collected in
12791 extract_local_specs; map from the general template to our local
12793 tree gen
= TREE_PURPOSE (elt
);
12794 tree inst
= TREE_VALUE (elt
);
12796 if (tree local
= retrieve_local_specialization (inst
))
12798 /* else inst is already a full instantiation of the pack. */
12799 register_local_specialization (inst
, gen
);
12801 gcc_assert (!TREE_PURPOSE (extra
));
12802 extra
= TREE_VALUE (extra
);
12805 /* I think we should always be able to substitute dependent args into the
12806 pattern. If that turns out to be incorrect in some cases, enable the
12807 alternate code (and add complain/in_decl parms to this function). */
12808 gcc_checking_assert (!uses_template_parms (extra
));
12810 if (!uses_template_parms (extra
))
12812 gcc_unreachable ();
12813 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12814 args
= add_outermost_template_args (args
, extra
);
12818 args
= add_to_template_args (extra
, args
);
12822 /* Substitute ARGS into T, which is an pack expansion
12823 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12824 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12825 (if only a partial substitution could be performed) or
12826 ERROR_MARK_NODE if there was an error. */
12828 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12832 tree pack
, packs
= NULL_TREE
;
12833 bool unsubstituted_packs
= false;
12836 bool need_local_specializations
= false;
12839 gcc_assert (PACK_EXPANSION_P (t
));
12840 pattern
= PACK_EXPANSION_PATTERN (t
);
12842 /* Add in any args remembered from an earlier partial instantiation. */
12843 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12845 levels
= TMPL_ARGS_DEPTH (args
);
12847 /* Determine the argument packs that will instantiate the parameter
12848 packs used in the expansion expression. While we're at it,
12849 compute the number of arguments to be expanded and make sure it
12851 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12852 pack
= TREE_CHAIN (pack
))
12854 tree parm_pack
= TREE_VALUE (pack
);
12855 tree arg_pack
= NULL_TREE
;
12856 tree orig_arg
= NULL_TREE
;
12859 if (TREE_CODE (parm_pack
) == BASES
)
12861 gcc_assert (parm_pack
== pattern
);
12862 if (BASES_DIRECT (parm_pack
))
12863 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12868 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12869 args
, complain
, in_decl
,
12872 else if (builtin_pack_call_p (parm_pack
))
12874 if (parm_pack
!= pattern
)
12876 if (complain
& tf_error
)
12877 sorry ("%qE is not the entire pattern of the pack expansion",
12879 return error_mark_node
;
12881 return expand_builtin_pack_call (parm_pack
, args
,
12882 complain
, in_decl
);
12884 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12886 /* We know we have correct local_specializations if this
12887 expansion is at function scope, or if we're dealing with a
12888 local parameter in a requires expression; for the latter,
12889 tsubst_requires_expr set it up appropriately. */
12890 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12891 arg_pack
= retrieve_local_specialization (parm_pack
);
12893 /* We can't rely on local_specializations for a parameter
12894 name used later in a function declaration (such as in a
12895 late-specified return type). Even if it exists, it might
12896 have the wrong value for a recursive call. */
12897 need_local_specializations
= true;
12901 /* This parameter pack was used in an unevaluated context. Just
12902 make a dummy decl, since it's only used for its type. */
12903 ++cp_unevaluated_operand
;
12904 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12905 --cp_unevaluated_operand
;
12906 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12907 /* Partial instantiation of the parm_pack, we can't build
12908 up an argument pack yet. */
12909 arg_pack
= NULL_TREE
;
12911 arg_pack
= make_fnparm_pack (arg_pack
);
12913 else if (DECL_PACK_P (arg_pack
))
12914 /* This argument pack isn't fully instantiated yet. */
12915 arg_pack
= NULL_TREE
;
12917 else if (is_capture_proxy (parm_pack
))
12919 arg_pack
= retrieve_local_specialization (parm_pack
);
12920 if (DECL_PACK_P (arg_pack
))
12921 arg_pack
= NULL_TREE
;
12926 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12927 if (level
<= levels
)
12928 arg_pack
= TMPL_ARG (args
, level
, idx
);
12930 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12931 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12932 arg_pack
= NULL_TREE
;
12935 orig_arg
= arg_pack
;
12936 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12937 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12939 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12940 /* This can only happen if we forget to expand an argument
12941 pack somewhere else. Just return an error, silently. */
12943 result
= make_tree_vec (1);
12944 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12951 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12953 /* Don't bother trying to do a partial substitution with
12954 incomplete packs; we'll try again after deduction. */
12955 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12960 else if (len
!= my_len
)
12962 if (!(complain
& tf_error
))
12963 /* Fail quietly. */;
12964 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12965 error ("mismatched argument pack lengths while expanding %qT",
12968 error ("mismatched argument pack lengths while expanding %qE",
12970 return error_mark_node
;
12973 /* Keep track of the parameter packs and their corresponding
12975 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12976 TREE_TYPE (packs
) = orig_arg
;
12980 /* We can't substitute for this parameter pack. We use a flag as
12981 well as the missing_level counter because function parameter
12982 packs don't have a level. */
12983 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12984 unsubstituted_packs
= true;
12988 /* If the expansion is just T..., return the matching argument pack, unless
12989 we need to call convert_from_reference on all the elements. This is an
12990 important optimization; see c++/68422. */
12991 if (!unsubstituted_packs
12992 && TREE_PURPOSE (packs
) == pattern
)
12994 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12996 /* If the argument pack is a single pack expansion, pull it out. */
12997 if (TREE_VEC_LENGTH (args
) == 1
12998 && pack_expansion_args_count (args
))
12999 return TREE_VEC_ELT (args
, 0);
13001 /* Types need no adjustment, nor does sizeof..., and if we still have
13002 some pack expansion args we won't do anything yet. */
13003 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13004 || PACK_EXPANSION_SIZEOF_P (t
)
13005 || pack_expansion_args_count (args
))
13007 /* Also optimize expression pack expansions if we can tell that the
13008 elements won't have reference type. */
13009 tree type
= TREE_TYPE (pattern
);
13010 if (type
&& !TYPE_REF_P (type
)
13011 && !PACK_EXPANSION_P (type
)
13012 && !WILDCARD_TYPE_P (type
))
13014 /* Otherwise use the normal path so we get convert_from_reference. */
13017 /* We cannot expand this expansion expression, because we don't have
13018 all of the argument packs we need. */
13019 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
13021 /* We got some full packs, but we can't substitute them in until we
13022 have values for all the packs. So remember these until then. */
13024 t
= make_pack_expansion (pattern
, complain
);
13025 PACK_EXPANSION_EXTRA_ARGS (t
)
13026 = build_extra_args (pattern
, args
, complain
);
13030 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13031 type, so create our own local specializations map; the current map is
13032 either NULL or (in the case of recursive unification) might have
13033 bindings that we don't want to use or alter. */
13034 local_specialization_stack
lss (need_local_specializations
13035 ? lss_blank
: lss_nop
);
13037 if (unsubstituted_packs
)
13039 /* There were no real arguments, we're just replacing a parameter
13040 pack with another version of itself. Substitute into the
13041 pattern and return a PACK_EXPANSION_*. The caller will need to
13043 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13044 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13045 /*integral_constant_expression_p=*/false);
13047 t
= tsubst (pattern
, args
, complain
, in_decl
);
13048 t
= make_pack_expansion (t
, complain
);
13052 gcc_assert (len
>= 0);
13054 /* For each argument in each argument pack, substitute into the
13056 result
= make_tree_vec (len
);
13057 tree elem_args
= copy_template_args (args
);
13058 for (i
= 0; i
< len
; ++i
)
13060 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13062 elem_args
, complain
,
13064 TREE_VEC_ELT (result
, i
) = t
;
13065 if (t
== error_mark_node
)
13067 result
= error_mark_node
;
13072 /* Update ARGS to restore the substitution from parameter packs to
13073 their argument packs. */
13074 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13076 tree parm
= TREE_PURPOSE (pack
);
13078 if (TREE_CODE (parm
) == PARM_DECL
13080 || TREE_CODE (parm
) == FIELD_DECL
)
13081 register_local_specialization (TREE_TYPE (pack
), parm
);
13086 if (TREE_VALUE (pack
) == NULL_TREE
)
13089 template_parm_level_and_index (parm
, &level
, &idx
);
13091 /* Update the corresponding argument. */
13092 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13093 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13096 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13100 /* If the dependent pack arguments were such that we end up with only a
13101 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13102 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13103 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13104 return TREE_VEC_ELT (result
, 0);
13109 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13110 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13111 parameter packs; all parms generated from a function parameter pack will
13112 have the same DECL_PARM_INDEX. */
13115 get_pattern_parm (tree parm
, tree tmpl
)
13117 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13120 if (DECL_ARTIFICIAL (parm
))
13122 for (patparm
= DECL_ARGUMENTS (pattern
);
13123 patparm
; patparm
= DECL_CHAIN (patparm
))
13124 if (DECL_ARTIFICIAL (patparm
)
13125 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13130 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13131 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13132 gcc_assert (DECL_PARM_INDEX (patparm
)
13133 == DECL_PARM_INDEX (parm
));
13139 /* Make an argument pack out of the TREE_VEC VEC. */
13142 make_argument_pack (tree vec
)
13146 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13147 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13150 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13151 TREE_CONSTANT (pack
) = 1;
13153 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13157 /* Return an exact copy of template args T that can be modified
13161 copy_template_args (tree t
)
13163 if (t
== error_mark_node
)
13166 int len
= TREE_VEC_LENGTH (t
);
13167 tree new_vec
= make_tree_vec (len
);
13169 for (int i
= 0; i
< len
; ++i
)
13171 tree elt
= TREE_VEC_ELT (t
, i
);
13172 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13173 elt
= copy_template_args (elt
);
13174 TREE_VEC_ELT (new_vec
, i
) = elt
;
13177 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13178 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13183 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13186 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13189 /* Substitute into each of the arguments. */
13190 tree new_arg
= TYPE_P (orig_arg
)
13191 ? cxx_make_type (TREE_CODE (orig_arg
))
13192 : make_node (TREE_CODE (orig_arg
));
13194 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13195 args
, complain
, in_decl
);
13196 if (pack_args
== error_mark_node
)
13197 new_arg
= error_mark_node
;
13199 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13201 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13202 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13207 /* Substitute ARGS into the vector or list of template arguments T. */
13210 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13213 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13216 if (t
== error_mark_node
)
13217 return error_mark_node
;
13219 len
= TREE_VEC_LENGTH (t
);
13220 elts
= XALLOCAVEC (tree
, len
);
13222 for (i
= 0; i
< len
; i
++)
13224 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13227 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13228 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13229 else if (PACK_EXPANSION_P (orig_arg
))
13231 /* Substitute into an expansion expression. */
13232 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13234 if (TREE_CODE (new_arg
) == TREE_VEC
)
13235 /* Add to the expanded length adjustment the number of
13236 expanded arguments. We subtract one from this
13237 measurement, because the argument pack expression
13238 itself is already counted as 1 in
13239 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13240 the argument pack is empty. */
13241 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13243 else if (ARGUMENT_PACK_P (orig_arg
))
13244 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13246 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13248 if (new_arg
== error_mark_node
)
13249 return error_mark_node
;
13252 if (new_arg
!= orig_arg
)
13259 /* Make space for the expanded arguments coming from template
13261 t
= make_tree_vec (len
+ expanded_len_adjust
);
13262 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13263 arguments for a member template.
13264 In that case each TREE_VEC in ORIG_T represents a level of template
13265 arguments, and ORIG_T won't carry any non defaulted argument count.
13266 It will rather be the nested TREE_VECs that will carry one.
13267 In other words, ORIG_T carries a non defaulted argument count only
13268 if it doesn't contain any nested TREE_VEC. */
13269 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13271 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13272 count
+= expanded_len_adjust
;
13273 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13275 for (i
= 0, out
= 0; i
< len
; i
++)
13277 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13278 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13279 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13283 /* Now expand the template argument pack "in place". */
13284 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13285 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13289 TREE_VEC_ELT (t
, out
) = elts
[i
];
13297 /* Substitute ARGS into one level PARMS of template parameters. */
13300 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13302 if (parms
== error_mark_node
)
13303 return error_mark_node
;
13305 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13307 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13309 tree tuple
= TREE_VEC_ELT (parms
, i
);
13311 if (tuple
== error_mark_node
)
13314 TREE_VEC_ELT (new_vec
, i
) =
13315 tsubst_template_parm (tuple
, args
, complain
);
13321 /* Return the result of substituting ARGS into the template parameters
13322 given by PARMS. If there are m levels of ARGS and m + n levels of
13323 PARMS, then the result will contain n levels of PARMS. For
13324 example, if PARMS is `template <class T> template <class U>
13325 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13326 result will be `template <int*, double, class V>'. */
13329 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13331 tree r
= NULL_TREE
;
13334 /* When substituting into a template, we must set
13335 PROCESSING_TEMPLATE_DECL as the template parameters may be
13336 dependent if they are based on one-another, and the dependency
13337 predicates are short-circuit outside of templates. */
13338 ++processing_template_decl
;
13340 for (new_parms
= &r
;
13341 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13342 new_parms
= &(TREE_CHAIN (*new_parms
)),
13343 parms
= TREE_CHAIN (parms
))
13345 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13348 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13349 - TMPL_ARGS_DEPTH (args
)),
13350 new_vec
, NULL_TREE
);
13351 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13352 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13355 --processing_template_decl
;
13360 /* Return the result of substituting ARGS into one template parameter
13361 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13362 parameter and which TREE_PURPOSE is the default argument of the
13363 template parameter. */
13366 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13368 tree default_value
, parm_decl
;
13370 if (args
== NULL_TREE
13372 || t
== error_mark_node
)
13375 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13377 default_value
= TREE_PURPOSE (t
);
13378 parm_decl
= TREE_VALUE (t
);
13379 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13381 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13382 if (TREE_CODE (parm_decl
) == PARM_DECL
13383 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13384 parm_decl
= error_mark_node
;
13385 default_value
= tsubst_template_arg (default_value
, args
,
13386 complain
, NULL_TREE
);
13387 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13389 tree r
= build_tree_list (default_value
, parm_decl
);
13390 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13394 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13395 type T. If T is not an aggregate or enumeration type, it is
13396 handled as if by tsubst. IN_DECL is as for tsubst. If
13397 ENTERING_SCOPE is nonzero, T is the context for a template which
13398 we are presently tsubst'ing. Return the substituted value. */
13401 tsubst_aggr_type (tree t
,
13403 tsubst_flags_t complain
,
13405 int entering_scope
)
13407 if (t
== NULL_TREE
)
13410 switch (TREE_CODE (t
))
13413 if (TYPE_PTRMEMFUNC_P (t
))
13414 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13416 /* Fall through. */
13417 case ENUMERAL_TYPE
:
13419 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13425 /* In "sizeof(X<I>)" we need to evaluate "I". */
13428 /* First, determine the context for the type we are looking
13430 context
= TYPE_CONTEXT (t
);
13431 if (context
&& TYPE_P (context
))
13433 context
= tsubst_aggr_type (context
, args
, complain
,
13434 in_decl
, /*entering_scope=*/1);
13435 /* If context is a nested class inside a class template,
13436 it may still need to be instantiated (c++/33959). */
13437 context
= complete_type (context
);
13440 /* Then, figure out what arguments are appropriate for the
13441 type we are trying to find. For example, given:
13443 template <class T> struct S;
13444 template <class T, class U> void f(T, U) { S<U> su; }
13446 and supposing that we are instantiating f<int, double>,
13447 then our ARGS will be {int, double}, but, when looking up
13448 S we only want {double}. */
13449 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13450 complain
, in_decl
);
13451 if (argvec
== error_mark_node
)
13452 r
= error_mark_node
;
13453 else if (cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13455 /* See maybe_dependent_member_ref. */
13456 tree name
= TYPE_IDENTIFIER (t
);
13457 tree fullname
= name
;
13458 if (instantiates_primary_template_p (t
))
13459 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13460 INNERMOST_TEMPLATE_ARGS (argvec
));
13461 return build_typename_type (context
, name
, fullname
,
13466 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13467 entering_scope
, complain
);
13468 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13474 /* This is not a template type, so there's nothing to do. */
13478 return tsubst (t
, args
, complain
, in_decl
);
13482 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13484 /* Substitute into the default argument ARG (a default argument for
13485 FN), which has the indicated TYPE. */
13488 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13489 tsubst_flags_t complain
)
13491 int errs
= errorcount
+ sorrycount
;
13493 /* This can happen in invalid code. */
13494 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13498 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13499 && CONSTRUCTOR_NELTS (arg
) == 0)
13502 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13503 parm
= chain_index (parmnum
, parm
);
13504 tree parmtype
= TREE_TYPE (parm
);
13505 if (DECL_BY_REFERENCE (parm
))
13506 parmtype
= TREE_TYPE (parmtype
);
13507 if (parmtype
== error_mark_node
)
13508 return error_mark_node
;
13510 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13513 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13516 /* This default argument came from a template. Instantiate the
13517 default argument here, not in tsubst. In the case of
13526 we must be careful to do name lookup in the scope of S<T>,
13527 rather than in the current class. */
13528 push_to_top_level ();
13529 push_access_scope (fn
);
13530 push_deferring_access_checks (dk_no_deferred
);
13531 start_lambda_scope (parm
);
13533 /* The default argument expression may cause implicitly defined
13534 member functions to be synthesized, which will result in garbage
13535 collection. We must treat this situation as if we were within
13536 the body of function so as to avoid collecting live data on the
13539 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13540 complain
, NULL_TREE
,
13541 /*integral_constant_expression_p=*/false);
13544 finish_lambda_scope ();
13546 /* Make sure the default argument is reasonable. */
13547 arg
= check_default_argument (type
, arg
, complain
);
13549 if (errorcount
+sorrycount
> errs
13550 && (complain
& tf_warning_or_error
))
13551 inform (input_location
,
13552 " when instantiating default argument for call to %qD", fn
);
13554 pop_deferring_access_checks ();
13555 pop_access_scope (fn
);
13556 pop_from_top_level ();
13558 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13561 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13562 defarg_inst
->put (parm
, arg
);
13568 /* Substitute into all the default arguments for FN. */
13571 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13576 tmpl_args
= DECL_TI_ARGS (fn
);
13578 /* If this function is not yet instantiated, we certainly don't need
13579 its default arguments. */
13580 if (uses_template_parms (tmpl_args
))
13582 /* Don't do this again for clones. */
13583 if (DECL_CLONED_FUNCTION_P (fn
))
13587 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13589 arg
= TREE_CHAIN (arg
), ++i
)
13590 if (TREE_PURPOSE (arg
))
13591 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13593 TREE_PURPOSE (arg
),
13597 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13598 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13600 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13603 store_explicit_specifier (tree v
, tree t
)
13605 if (!explicit_specifier_map
)
13606 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13607 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13608 explicit_specifier_map
->put (v
, t
);
13611 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13614 lookup_explicit_specifier (tree v
)
13616 return *explicit_specifier_map
->get (v
);
13619 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13620 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13621 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13625 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13626 tree raises
, tsubst_flags_t complain
)
13628 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13631 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13633 new_type
= build_function_type (return_type
, arg_types
);
13634 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13638 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13639 /* Don't pick up extra function qualifiers from the basetype. */
13640 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13641 if (! MAYBE_CLASS_TYPE_P (r
))
13645 Type deduction may fail for any of the following
13648 -- Attempting to create "pointer to member of T" when T
13649 is not a class type. */
13650 if (complain
& tf_error
)
13651 error ("creating pointer to member function of non-class type %qT",
13653 return error_mark_node
;
13656 new_type
= build_method_type_directly (r
, return_type
,
13657 TREE_CHAIN (arg_types
));
13659 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13661 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13662 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13663 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13666 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13667 each of its formal parameters. If there is a disagreement then rebuild
13668 DECL's function type according to its formal parameter types, as part of a
13669 resolution for Core issues 1001/1322. */
13672 maybe_rebuild_function_decl_type (tree decl
)
13674 bool function_type_needs_rebuilding
= false;
13675 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13677 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13678 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13680 tree parm_type
= TREE_VALUE (parm_type_list
);
13681 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13682 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13684 function_type_needs_rebuilding
= true;
13688 parm_list
= DECL_CHAIN (parm_list
);
13689 parm_type_list
= TREE_CHAIN (parm_type_list
);
13693 if (!function_type_needs_rebuilding
)
13696 const tree fntype
= TREE_TYPE (decl
);
13697 tree parm_list
= DECL_ARGUMENTS (decl
);
13698 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13699 tree new_parm_type_list
= NULL_TREE
;
13700 tree
*q
= &new_parm_type_list
;
13701 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13703 *q
= copy_node (old_parm_type_list
);
13704 parm_list
= DECL_CHAIN (parm_list
);
13705 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13706 q
= &TREE_CHAIN (*q
);
13708 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13710 *q
= copy_node (old_parm_type_list
);
13711 tree
*new_parm_type
= &TREE_VALUE (*q
);
13712 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13713 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13714 *new_parm_type
= formal_parm_type_unqual
;
13716 parm_list
= DECL_CHAIN (parm_list
);
13717 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13718 q
= &TREE_CHAIN (*q
);
13720 if (old_parm_type_list
== void_list_node
)
13721 *q
= void_list_node
;
13724 = rebuild_function_or_method_type (fntype
,
13725 TREE_TYPE (fntype
), new_parm_type_list
,
13726 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13729 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13732 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13733 tree lambda_fntype
)
13735 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
13736 hashval_t hash
= 0;
13739 /* Nobody should be tsubst'ing into non-template functions. */
13740 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
13741 || DECL_LOCAL_DECL_P (t
));
13743 if (DECL_LOCAL_DECL_P (t
))
13745 if (tree spec
= retrieve_local_specialization (t
))
13748 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13750 /* If T is not dependent, just return it. */
13751 if (!uses_template_parms (DECL_TI_ARGS (t
))
13752 && !LAMBDA_FUNCTION_P (t
))
13755 /* Calculate the most general template of which R is a
13757 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13759 /* We're substituting a lambda function under tsubst_lambda_expr but not
13760 directly from it; find the matching function we're already inside.
13761 But don't do this if T is a generic lambda with a single level of
13762 template parms, as in that case we're doing a normal instantiation. */
13763 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13764 && (!generic_lambda_fn_p (t
)
13765 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13766 return enclosing_instantiation_of (t
);
13768 /* Calculate the complete set of arguments used to
13770 argvec
= tsubst_template_args (DECL_TI_ARGS
13771 (DECL_TEMPLATE_RESULT
13772 (DECL_TI_TEMPLATE (t
))),
13773 args
, complain
, in_decl
);
13774 if (argvec
== error_mark_node
)
13775 return error_mark_node
;
13777 /* Check to see if we already have this specialization. */
13778 if (!lambda_fntype
)
13780 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13781 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13785 /* We can see more levels of arguments than parameters if
13786 there was a specialization of a member template, like
13789 template <class T> struct S { template <class U> void f(); }
13790 template <> template <class U> void S<int>::f(U);
13792 Here, we'll be substituting into the specialization,
13793 because that's where we can find the code we actually
13794 want to generate, but we'll have enough arguments for
13795 the most general template.
13797 We also deal with the peculiar case:
13799 template <class T> struct S {
13800 template <class U> friend void f();
13802 template <class U> void f() {}
13804 template void f<double>();
13806 Here, the ARGS for the instantiation of will be {int,
13807 double}. But, we only need as many ARGS as there are
13808 levels of template parameters in CODE_PATTERN. We are
13809 careful not to get fooled into reducing the ARGS in
13812 template <class T> struct S { template <class U> void f(U); }
13813 template <class T> template <> void S<T>::f(int) {}
13815 which we can spot because the pattern will be a
13816 specialization in this case. */
13817 int args_depth
= TMPL_ARGS_DEPTH (args
);
13819 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13821 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13822 args
= get_innermost_template_args (args
, parms_depth
);
13826 /* This special case arises when we have something like this:
13828 template <class T> struct S {
13829 friend void f<int>(int, double);
13832 Here, the DECL_TI_TEMPLATE for the friend declaration
13833 will be an IDENTIFIER_NODE. We are being called from
13834 tsubst_friend_function, and we want only to create a
13835 new decl (R) with appropriate types so that we can call
13836 determine_specialization. */
13837 gen_tmpl
= NULL_TREE
;
13838 argvec
= NULL_TREE
;
13841 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13843 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13844 bool member
= ctx
&& TYPE_P (ctx
);
13846 if (member
&& !closure
)
13847 ctx
= tsubst_aggr_type (ctx
, args
,
13848 complain
, t
, /*entering_scope=*/1);
13850 tree type
= (lambda_fntype
? lambda_fntype
13851 : tsubst (TREE_TYPE (t
), args
,
13852 complain
| tf_fndecl_type
, in_decl
));
13853 if (type
== error_mark_node
)
13854 return error_mark_node
;
13856 /* If we hit excessive deduction depth, the type is bogus even if
13857 it isn't error_mark_node, so don't build a decl. */
13858 if (excessive_deduction_depth
)
13859 return error_mark_node
;
13861 /* We do NOT check for matching decls pushed separately at this
13862 point, as they may not represent instantiations of this
13863 template, and in any case are considered separate under the
13865 tree r
= copy_decl (t
);
13866 DECL_USE_TEMPLATE (r
) = 0;
13867 TREE_TYPE (r
) = type
;
13868 /* Clear out the mangled name and RTL for the instantiation. */
13869 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13870 SET_DECL_RTL (r
, NULL
);
13871 /* Leave DECL_INITIAL set on deleted instantiations. */
13872 if (!DECL_DELETED_FN (r
))
13873 DECL_INITIAL (r
) = NULL_TREE
;
13874 DECL_CONTEXT (r
) = ctx
;
13876 /* Handle explicit(dependent-expr). */
13877 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13879 tree spec
= lookup_explicit_specifier (t
);
13880 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13881 /*function_p=*/false,
13883 spec
= build_explicit_specifier (spec
, complain
);
13884 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13887 /* OpenMP UDRs have the only argument a reference to the declared
13888 type. We want to diagnose if the declared type is a reference,
13889 which is invalid, but as references to references are usually
13890 quietly merged, diagnose it here. */
13891 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13894 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13895 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13896 if (TYPE_REF_P (argtype
))
13897 error_at (DECL_SOURCE_LOCATION (t
),
13898 "reference type %qT in "
13899 "%<#pragma omp declare reduction%>", argtype
);
13900 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13901 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13905 if (member
&& DECL_CONV_FN_P (r
))
13906 /* Type-conversion operator. Reconstruct the name, in
13907 case it's the name of one of the template's parameters. */
13908 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13910 tree parms
= DECL_ARGUMENTS (t
);
13912 parms
= DECL_CHAIN (parms
);
13913 parms
= tsubst (parms
, args
, complain
, t
);
13914 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13915 DECL_CONTEXT (parm
) = r
;
13918 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13919 DECL_NAME (tparm
) = closure_identifier
;
13920 DECL_CHAIN (tparm
) = parms
;
13923 DECL_ARGUMENTS (r
) = parms
;
13924 DECL_RESULT (r
) = NULL_TREE
;
13926 maybe_rebuild_function_decl_type (r
);
13928 TREE_STATIC (r
) = 0;
13929 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13930 DECL_EXTERNAL (r
) = 1;
13931 /* If this is an instantiation of a function with internal
13932 linkage, we already know what object file linkage will be
13933 assigned to the instantiation. */
13934 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13935 DECL_DEFER_OUTPUT (r
) = 0;
13936 DECL_CHAIN (r
) = NULL_TREE
;
13937 DECL_PENDING_INLINE_INFO (r
) = 0;
13938 DECL_PENDING_INLINE_P (r
) = 0;
13939 DECL_SAVED_TREE (r
) = NULL_TREE
;
13940 DECL_STRUCT_FUNCTION (r
) = NULL
;
13942 /* We'll re-clone as appropriate in instantiate_template. */
13943 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13945 /* If we aren't complaining now, return on error before we register
13946 the specialization so that we'll complain eventually. */
13947 if ((complain
& tf_error
) == 0
13948 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13949 && !grok_op_properties (r
, /*complain=*/false))
13950 return error_mark_node
;
13952 /* Associate the constraints directly with the instantiation. We
13953 don't substitute through the constraints; that's only done when
13954 they are checked. */
13955 if (tree ci
= get_constraints (t
))
13956 /* Unless we're regenerating a lambda, in which case we'll set the
13957 lambda's constraints in tsubst_lambda_expr. */
13958 if (!lambda_fntype
)
13959 set_constraints (r
, ci
);
13961 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13962 SET_DECL_FRIEND_CONTEXT (r
,
13963 tsubst (DECL_FRIEND_CONTEXT (t
),
13964 args
, complain
, in_decl
));
13966 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13967 this in the special friend case mentioned above where
13968 GEN_TMPL is NULL. */
13969 if (gen_tmpl
&& !closure
)
13971 DECL_TEMPLATE_INFO (r
)
13972 = build_template_info (gen_tmpl
, argvec
);
13973 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13976 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13978 /* We instantiated this while substituting into
13979 the type earlier (template/friend54.C). */
13982 /* We're not supposed to instantiate default arguments
13983 until they are called, for a template. But, for a
13986 template <class T> void f ()
13987 { extern void g(int i = T()); }
13989 we should do the substitution when the template is
13990 instantiated. We handle the member function case in
13991 instantiate_class_template since the default arguments
13992 might refer to other members of the class. */
13994 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13995 && !uses_template_parms (argvec
))
13996 tsubst_default_arguments (r
, complain
);
13998 else if (DECL_LOCAL_DECL_P (r
))
14000 if (!cp_unevaluated_operand
)
14001 register_local_specialization (r
, t
);
14004 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14006 /* Copy the list of befriending classes. */
14007 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14009 friends
= &TREE_CHAIN (*friends
))
14011 *friends
= copy_node (*friends
);
14012 TREE_VALUE (*friends
)
14013 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14016 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14018 maybe_retrofit_in_chrg (r
);
14019 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14020 return error_mark_node
;
14021 /* If this is an instantiation of a member template, clone it.
14022 If it isn't, that'll be handled by
14023 clone_constructors_and_destructors. */
14024 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14025 clone_cdtor (r
, /*update_methods=*/false);
14027 else if ((complain
& tf_error
) != 0
14028 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14029 && !grok_op_properties (r
, /*complain=*/true))
14030 return error_mark_node
;
14032 /* Possibly limit visibility based on template args. */
14033 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14034 if (DECL_VISIBILITY_SPECIFIED (t
))
14036 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14037 DECL_ATTRIBUTES (r
)
14038 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14040 determine_visibility (r
);
14041 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14042 && !processing_template_decl
)
14043 defaulted_late_check (r
);
14045 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14046 args
, complain
, in_decl
);
14048 if (tree attr
= lookup_attribute ("omp declare variant base",
14049 DECL_ATTRIBUTES (r
)))
14050 omp_declare_variant_finalize (r
, attr
);
14055 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14058 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14059 tree lambda_fntype
)
14061 /* We can get here when processing a member function template,
14062 member class template, or template template parameter. */
14063 tree decl
= DECL_TEMPLATE_RESULT (t
);
14069 hashval_t hash
= 0;
14071 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14073 /* Template template parameter is treated here. */
14074 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14075 if (new_type
== error_mark_node
)
14076 r
= error_mark_node
;
14077 /* If we get a real template back, return it. This can happen in
14078 the context of most_specialized_partial_spec. */
14079 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14082 /* The new TEMPLATE_DECL was built in
14083 reduce_template_parm_level. */
14084 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14088 if (!lambda_fntype
)
14090 /* We might already have an instance of this template.
14091 The ARGS are for the surrounding class type, so the
14092 full args contain the tsubst'd args for the context,
14093 plus the innermost args from the template decl. */
14094 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14095 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14096 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14097 /* Because this is a template, the arguments will still be
14098 dependent, even after substitution. If
14099 PROCESSING_TEMPLATE_DECL is not set, the dependency
14100 predicates will short-circuit. */
14101 ++processing_template_decl
;
14102 full_args
= tsubst_template_args (tmpl_args
, args
,
14103 complain
, in_decl
);
14104 --processing_template_decl
;
14105 if (full_args
== error_mark_node
)
14106 return error_mark_node
;
14108 /* If this is a default template template argument,
14109 tsubst might not have changed anything. */
14110 if (full_args
== tmpl_args
)
14113 hash
= hash_tmpl_and_args (t
, full_args
);
14114 spec
= retrieve_specialization (t
, full_args
, hash
);
14115 if (spec
!= NULL_TREE
)
14118 /* Type partial instantiations are stored as the type by
14119 lookup_template_class_1, not here as the template. */
14120 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14125 /* Make a new template decl. It will be similar to the
14126 original, but will record the current template arguments.
14127 We also create a new function declaration, which is just
14128 like the old one, but points to this new template, rather
14129 than the old one. */
14131 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14132 DECL_CHAIN (r
) = NULL_TREE
;
14134 // Build new template info linking to the original template decl.
14135 if (!lambda_fntype
)
14137 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14138 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14141 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14143 /* The template parameters for this new template are all the
14144 template parameters for the old template, except the
14145 outermost level of parameters. */
14146 DECL_TEMPLATE_PARMS (r
)
14147 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14150 bool class_p
= false;
14152 ++processing_template_decl
;
14153 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14154 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14157 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14160 inner
= TREE_TYPE (inner
);
14162 inner
= tsubst (inner
, args
, complain
, in_decl
);
14164 --processing_template_decl
;
14165 if (inner
== error_mark_node
)
14166 return error_mark_node
;
14170 /* For a partial specialization, we need to keep pointing to
14171 the primary template. */
14172 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14173 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14175 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14176 inner
= TYPE_MAIN_DECL (inner
);
14178 else if (lambda_fntype
)
14180 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14181 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14185 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14186 DECL_TI_TEMPLATE (inner
) = r
;
14187 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14190 DECL_TEMPLATE_RESULT (r
) = inner
;
14191 TREE_TYPE (r
) = TREE_TYPE (inner
);
14192 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14194 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14195 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14197 if (PRIMARY_TEMPLATE_P (t
))
14198 DECL_PRIMARY_TEMPLATE (r
) = r
;
14200 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14202 /* Record this non-type partial instantiation. */
14203 register_specialization (r
, t
,
14204 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14210 /* True if FN is the op() for a lambda in an uninstantiated template. */
14213 lambda_fn_in_template_p (tree fn
)
14215 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14217 tree closure
= DECL_CONTEXT (fn
);
14218 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14221 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14222 which the above is true. */
14225 instantiated_lambda_fn_p (tree fn
)
14227 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14229 tree closure
= DECL_CONTEXT (fn
);
14230 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14231 return LAMBDA_EXPR_INSTANTIATED (lam
);
14234 /* We're instantiating a variable from template function TCTX. Return the
14235 corresponding current enclosing scope. This gets complicated because lambda
14236 functions in templates are regenerated rather than instantiated, but generic
14237 lambda functions are subsequently instantiated. */
14240 enclosing_instantiation_of (tree otctx
)
14243 tree fn
= current_function_decl
;
14244 int lambda_count
= 0;
14246 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14247 || instantiated_lambda_fn_p (tctx
));
14248 tctx
= decl_function_context (tctx
))
14250 for (; fn
; fn
= decl_function_context (fn
))
14253 int flambda_count
= 0;
14254 for (; fn
&& instantiated_lambda_fn_p (fn
);
14255 fn
= decl_function_context (fn
))
14257 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14258 ? most_general_template (fn
) != most_general_template (tctx
)
14261 if (flambda_count
!= lambda_count
)
14263 gcc_assert (flambda_count
> lambda_count
);
14264 for (; flambda_count
> lambda_count
; --flambda_count
)
14265 ofn
= decl_function_context (ofn
);
14267 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14268 || DECL_CONV_FN_P (ofn
));
14271 gcc_unreachable ();
14274 /* Substitute the ARGS into the T, which is a _DECL. Return the
14275 result of the substitution. Issue error and warning messages under
14276 control of COMPLAIN. */
14279 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14281 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14282 location_t saved_loc
;
14283 tree r
= NULL_TREE
;
14285 hashval_t hash
= 0;
14287 /* Set the filename and linenumber to improve error-reporting. */
14288 saved_loc
= input_location
;
14289 input_location
= DECL_SOURCE_LOCATION (t
);
14291 switch (TREE_CODE (t
))
14293 case TEMPLATE_DECL
:
14294 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14297 case FUNCTION_DECL
:
14298 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14303 tree type
= NULL_TREE
;
14305 tree expanded_types
= NULL_TREE
;
14306 tree prev_r
= NULL_TREE
;
14307 tree first_r
= NULL_TREE
;
14309 if (DECL_PACK_P (t
))
14311 /* If there is a local specialization that isn't a
14312 parameter pack, it means that we're doing a "simple"
14313 substitution from inside tsubst_pack_expansion. Just
14314 return the local specialization (which will be a single
14316 tree spec
= retrieve_local_specialization (t
);
14318 && TREE_CODE (spec
) == PARM_DECL
14319 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14322 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14323 the parameters in this function parameter pack. */
14324 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14325 complain
, in_decl
);
14326 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14328 len
= TREE_VEC_LENGTH (expanded_types
);
14330 /* Zero-length parameter packs are boring. Just substitute
14332 if (len
== 0 && !cp_unevaluated_operand
)
14333 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14338 /* All we did was update the type. Make a note of that. */
14339 type
= expanded_types
;
14340 expanded_types
= NULL_TREE
;
14344 /* Loop through all of the parameters we'll build. When T is
14345 a function parameter pack, LEN is the number of expanded
14346 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14348 for (i
= 0; i
< len
; ++i
)
14352 if (DECL_TEMPLATE_PARM_P (t
))
14353 SET_DECL_TEMPLATE_PARM_P (r
);
14355 if (expanded_types
)
14356 /* We're on the Ith parameter of the function parameter
14359 /* Get the Ith type. */
14360 type
= TREE_VEC_ELT (expanded_types
, i
);
14362 /* Rename the parameter to include the index. */
14364 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14367 /* We're dealing with a normal parameter. */
14368 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14370 type
= type_decays_to (type
);
14371 TREE_TYPE (r
) = type
;
14372 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14374 if (DECL_INITIAL (r
))
14376 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14377 DECL_INITIAL (r
) = TREE_TYPE (r
);
14379 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14380 complain
, in_decl
);
14383 DECL_CONTEXT (r
) = NULL_TREE
;
14385 if (!DECL_TEMPLATE_PARM_P (r
))
14386 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14388 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14389 args
, complain
, in_decl
);
14391 /* Keep track of the first new parameter we
14392 generate. That's what will be returned to the
14397 /* Build a proper chain of parameters when substituting
14398 into a function parameter pack. */
14400 DECL_CHAIN (prev_r
) = r
;
14403 /* If cp_unevaluated_operand is set, we're just looking for a
14404 single dummy parameter, so don't keep going. */
14405 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14406 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14407 complain
, DECL_CHAIN (t
));
14409 /* FIRST_R contains the start of the chain we've built. */
14416 tree type
= NULL_TREE
;
14417 tree vec
= NULL_TREE
;
14418 tree expanded_types
= NULL_TREE
;
14421 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14423 /* This field is a lambda capture pack. Return a TREE_VEC of
14424 the expanded fields to instantiate_class_template_1. */
14425 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14426 complain
, in_decl
);
14427 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14429 len
= TREE_VEC_LENGTH (expanded_types
);
14430 vec
= make_tree_vec (len
);
14434 /* All we did was update the type. Make a note of that. */
14435 type
= expanded_types
;
14436 expanded_types
= NULL_TREE
;
14440 for (int i
= 0; i
< len
; ++i
)
14443 if (expanded_types
)
14445 type
= TREE_VEC_ELT (expanded_types
, i
);
14447 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14450 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14452 if (type
== error_mark_node
)
14453 RETURN (error_mark_node
);
14454 TREE_TYPE (r
) = type
;
14455 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14457 if (DECL_C_BIT_FIELD (r
))
14458 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14460 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14461 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14463 /*integral_constant_expression_p=*/true);
14464 if (DECL_INITIAL (t
))
14466 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14467 NSDMI in perform_member_init. Still set DECL_INITIAL
14468 so that we know there is one. */
14469 DECL_INITIAL (r
) = void_node
;
14470 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14471 retrofit_lang_decl (r
);
14472 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14474 /* We don't have to set DECL_CONTEXT here; it is set by
14475 finish_member_declaration. */
14476 DECL_CHAIN (r
) = NULL_TREE
;
14478 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14479 args
, complain
, in_decl
);
14482 TREE_VEC_ELT (vec
, i
) = r
;
14491 /* We reach here only for member using decls. We also need to check
14492 uses_template_parms because DECL_DEPENDENT_P is not set for a
14493 using-declaration that designates a member of the current
14494 instantiation (c++/53549). */
14495 if (DECL_DEPENDENT_P (t
)
14496 || uses_template_parms (USING_DECL_SCOPE (t
)))
14498 tree scope
= USING_DECL_SCOPE (t
);
14499 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14500 if (PACK_EXPANSION_P (scope
))
14502 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14503 int len
= TREE_VEC_LENGTH (vec
);
14504 r
= make_tree_vec (len
);
14505 for (int i
= 0; i
< len
; ++i
)
14507 tree escope
= TREE_VEC_ELT (vec
, i
);
14508 tree elt
= do_class_using_decl (escope
, name
);
14511 r
= error_mark_node
;
14516 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14517 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14519 TREE_VEC_ELT (r
, i
) = elt
;
14524 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14525 complain
, in_decl
);
14526 r
= do_class_using_decl (inst_scope
, name
);
14528 r
= error_mark_node
;
14531 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14532 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14539 DECL_CHAIN (r
) = NULL_TREE
;
14546 tree argvec
= NULL_TREE
;
14547 tree gen_tmpl
= NULL_TREE
;
14548 tree tmpl
= NULL_TREE
;
14549 tree type
= NULL_TREE
;
14551 if (TREE_TYPE (t
) == error_mark_node
)
14552 RETURN (error_mark_node
);
14554 if (TREE_CODE (t
) == TYPE_DECL
14555 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14557 /* If this is the canonical decl, we don't have to
14558 mess with instantiations, and often we can't (for
14559 typename, template type parms and such). Note that
14560 TYPE_NAME is not correct for the above test if
14561 we've copied the type for a typedef. */
14562 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14563 if (type
== error_mark_node
)
14564 RETURN (error_mark_node
);
14565 r
= TYPE_NAME (type
);
14569 /* Check to see if we already have the specialization we
14571 tree spec
= NULL_TREE
;
14572 bool local_p
= false;
14573 tree ctx
= DECL_CONTEXT (t
);
14574 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14575 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14578 if (DECL_CLASS_SCOPE_P (t
))
14580 ctx
= tsubst_aggr_type (ctx
, args
,
14582 in_decl
, /*entering_scope=*/1);
14583 /* If CTX is unchanged, then T is in fact the
14584 specialization we want. That situation occurs when
14585 referencing a static data member within in its own
14586 class. We can use pointer equality, rather than
14587 same_type_p, because DECL_CONTEXT is always
14589 if (ctx
== DECL_CONTEXT (t
)
14590 /* ... unless T is a member template; in which
14591 case our caller can be willing to create a
14592 specialization of that template represented
14594 && !(DECL_TI_TEMPLATE (t
)
14595 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14601 tmpl
= DECL_TI_TEMPLATE (t
);
14602 gen_tmpl
= most_general_template (tmpl
);
14603 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14604 if (argvec
!= error_mark_node
)
14605 argvec
= (coerce_innermost_template_parms
14606 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14607 argvec
, t
, complain
,
14608 /*all*/true, /*defarg*/true));
14609 if (argvec
== error_mark_node
)
14610 RETURN (error_mark_node
);
14611 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14612 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14617 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14618 /* Subsequent calls to pushdecl will fill this in. */
14620 /* A local variable. */
14622 /* Unless this is a reference to a static variable from an
14623 enclosing function, in which case we need to fill it in now. */
14624 if (TREE_STATIC (t
))
14626 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14627 if (fn
!= current_function_decl
)
14630 spec
= retrieve_local_specialization (t
);
14632 /* If we already have the specialization we need, there is
14633 nothing more to do. */
14640 /* Create a new node for the specialization we need. */
14641 if (type
== NULL_TREE
)
14643 if (is_typedef_decl (t
))
14644 type
= DECL_ORIGINAL_TYPE (t
);
14646 type
= TREE_TYPE (t
);
14648 && VAR_HAD_UNKNOWN_BOUND (t
)
14649 && type
!= error_mark_node
)
14650 type
= strip_array_domain (type
);
14651 tree sub_args
= args
;
14652 if (tree auto_node
= type_uses_auto (type
))
14654 /* Mask off any template args past the variable's context so we
14655 don't replace the auto with an unrelated argument. */
14656 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14657 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14659 /* This should never happen with the new lambda instantiation
14660 model, but keep the handling just in case. */
14661 gcc_assert (!CHECKING_P
),
14662 sub_args
= strip_innermost_template_args (args
, extra
);
14664 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14665 /* Substituting the type might have recursively instantiated this
14666 same alias (c++/86171). */
14667 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14668 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14677 DECL_INITIALIZED_P (r
) = 0;
14678 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14679 if (type
== error_mark_node
)
14680 RETURN (error_mark_node
);
14681 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14683 /* It may seem that this case cannot occur, since:
14688 declares a function, not a variable. However:
14691 template <typename T> void g() { T t; }
14692 template void g<f>();
14694 is an attempt to declare a variable with function
14696 error ("variable %qD has function type",
14697 /* R is not yet sufficiently initialized, so we
14698 just use its name. */
14700 RETURN (error_mark_node
);
14702 type
= complete_type (type
);
14703 /* Wait until cp_finish_decl to set this again, to handle
14704 circular dependency (template/instantiate6.C). */
14705 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14706 type
= check_var_type (DECL_NAME (r
), type
,
14707 DECL_SOURCE_LOCATION (r
));
14708 if (DECL_HAS_VALUE_EXPR_P (t
))
14710 tree ve
= DECL_VALUE_EXPR (t
);
14711 /* If the DECL_VALUE_EXPR is converted to the declared type,
14712 preserve the identity so that gimplify_type_sizes works. */
14713 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14715 ve
= TREE_OPERAND (ve
, 0);
14716 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14717 /*constant_expression_p=*/false);
14718 if (REFERENCE_REF_P (ve
))
14720 gcc_assert (TYPE_REF_P (type
));
14721 ve
= TREE_OPERAND (ve
, 0);
14724 ve
= build_nop (type
, ve
);
14725 else if (DECL_LANG_SPECIFIC (t
)
14726 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14727 && TREE_CODE (ve
) == COMPONENT_REF
14728 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14729 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14730 type
= TREE_TYPE (ve
);
14732 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14733 == TYPE_MAIN_VARIANT (type
));
14734 SET_DECL_VALUE_EXPR (r
, ve
);
14736 if (CP_DECL_THREAD_LOCAL_P (r
)
14737 && !processing_template_decl
)
14738 set_decl_tls_model (r
, decl_default_tls_model (r
));
14740 else if (DECL_SELF_REFERENCE_P (t
))
14741 SET_DECL_SELF_REFERENCE_P (r
);
14742 TREE_TYPE (r
) = type
;
14743 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14744 DECL_CONTEXT (r
) = ctx
;
14745 /* Clear out the mangled name and RTL for the instantiation. */
14746 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14747 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14748 SET_DECL_RTL (r
, NULL
);
14749 /* The initializer must not be expanded until it is required;
14750 see [temp.inst]. */
14751 DECL_INITIAL (r
) = NULL_TREE
;
14752 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14755 if (DECL_LANG_SPECIFIC (r
))
14756 SET_DECL_DEPENDENT_INIT_P (r
, false);
14758 SET_DECL_MODE (r
, VOIDmode
);
14760 /* Possibly limit visibility based on template args. */
14761 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14762 if (DECL_VISIBILITY_SPECIFIED (t
))
14764 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14765 DECL_ATTRIBUTES (r
)
14766 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14768 determine_visibility (r
);
14773 /* A static data member declaration is always marked
14774 external when it is declared in-class, even if an
14775 initializer is present. We mimic the non-template
14776 processing here. */
14777 DECL_EXTERNAL (r
) = 1;
14778 if (DECL_NAMESPACE_SCOPE_P (t
))
14779 DECL_NOT_REALLY_EXTERN (r
) = 1;
14781 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14782 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14783 if (!error_operand_p (r
) || (complain
& tf_error
))
14784 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14788 if (DECL_LANG_SPECIFIC (r
))
14789 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14790 if (!cp_unevaluated_operand
)
14791 register_local_specialization (r
, t
);
14794 DECL_CHAIN (r
) = NULL_TREE
;
14796 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14798 args
, complain
, in_decl
);
14800 /* Preserve a typedef that names a type. */
14801 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14803 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14804 set_underlying_type (r
);
14805 if (TYPE_DECL_ALIAS_P (r
))
14806 /* An alias template specialization can be dependent
14807 even if its underlying type is not. */
14808 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14811 layout_decl (r
, 0);
14816 gcc_unreachable ();
14821 /* Restore the file and line information. */
14822 input_location
= saved_loc
;
14827 /* Substitute into the complete parameter type list PARMS. */
14830 tsubst_function_parms (tree parms
,
14832 tsubst_flags_t complain
,
14835 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14838 /* Substitute into the ARG_TYPES of a function type.
14839 If END is a TREE_CHAIN, leave it and any following types
14843 tsubst_arg_types (tree arg_types
,
14846 tsubst_flags_t complain
,
14849 tree remaining_arg_types
;
14850 tree type
= NULL_TREE
;
14852 tree expanded_args
= NULL_TREE
;
14855 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14858 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14859 args
, end
, complain
, in_decl
);
14860 if (remaining_arg_types
== error_mark_node
)
14861 return error_mark_node
;
14863 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14865 /* For a pack expansion, perform substitution on the
14866 entire expression. Later on, we'll handle the arguments
14868 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14869 args
, complain
, in_decl
);
14871 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14872 /* So that we'll spin through the parameters, one by one. */
14873 i
= TREE_VEC_LENGTH (expanded_args
);
14876 /* We only partially substituted into the parameter
14877 pack. Our type is TYPE_PACK_EXPANSION. */
14878 type
= expanded_args
;
14879 expanded_args
= NULL_TREE
;
14887 type
= TREE_VEC_ELT (expanded_args
, i
);
14889 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14891 if (type
== error_mark_node
)
14892 return error_mark_node
;
14893 if (VOID_TYPE_P (type
))
14895 if (complain
& tf_error
)
14897 error ("invalid parameter type %qT", type
);
14899 error ("in declaration %q+D", in_decl
);
14901 return error_mark_node
;
14904 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14905 return error_mark_node
;
14907 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14908 top-level qualifiers as required. */
14909 type
= cv_unqualified (type_decays_to (type
));
14911 /* We do not substitute into default arguments here. The standard
14912 mandates that they be instantiated only when needed, which is
14913 done in build_over_call. */
14914 default_arg
= TREE_PURPOSE (arg_types
);
14916 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14917 since the new op() won't have any associated template arguments for us
14918 to refer to later. */
14919 if (lambda_fn_in_template_p (in_decl
))
14920 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14921 false/*fn*/, false/*constexpr*/);
14923 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14925 /* We've instantiated a template before its default arguments
14926 have been parsed. This can happen for a nested template
14927 class, and is not an error unless we require the default
14928 argument in a call of this function. */
14929 remaining_arg_types
=
14930 tree_cons (default_arg
, type
, remaining_arg_types
);
14931 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14932 remaining_arg_types
);
14935 remaining_arg_types
=
14936 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14939 return remaining_arg_types
;
14942 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14943 *not* handle the exception-specification for FNTYPE, because the
14944 initial substitution of explicitly provided template parameters
14945 during argument deduction forbids substitution into the
14946 exception-specification:
14950 All references in the function type of the function template to the
14951 corresponding template parameters are replaced by the specified tem-
14952 plate argument values. If a substitution in a template parameter or
14953 in the function type of the function template results in an invalid
14954 type, type deduction fails. [Note: The equivalent substitution in
14955 exception specifications is done only when the function is instanti-
14956 ated, at which point a program is ill-formed if the substitution
14957 results in an invalid type.] */
14960 tsubst_function_type (tree t
,
14962 tsubst_flags_t complain
,
14966 tree arg_types
= NULL_TREE
;
14968 /* The TYPE_CONTEXT is not used for function/method types. */
14969 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14971 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14973 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14975 if (late_return_type_p
)
14977 /* Substitute the argument types. */
14978 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14979 complain
, in_decl
);
14980 if (arg_types
== error_mark_node
)
14981 return error_mark_node
;
14983 tree save_ccp
= current_class_ptr
;
14984 tree save_ccr
= current_class_ref
;
14985 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14986 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14987 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14990 /* DR 1207: 'this' is in scope in the trailing return type. */
14991 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14994 /* Substitute the return type. */
14995 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14999 current_class_ptr
= save_ccp
;
15000 current_class_ref
= save_ccr
;
15004 /* Substitute the return type. */
15005 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15007 if (return_type
== error_mark_node
)
15008 return error_mark_node
;
15009 /* DR 486 clarifies that creation of a function type with an
15010 invalid return type is a deduction failure. */
15011 if (TREE_CODE (return_type
) == ARRAY_TYPE
15012 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15014 if (complain
& tf_error
)
15016 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15017 error ("function returning an array");
15019 error ("function returning a function");
15021 return error_mark_node
;
15024 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
15025 return error_mark_node
;
15027 if (!late_return_type_p
)
15029 /* Substitute the argument types. */
15030 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15031 complain
, in_decl
);
15032 if (arg_types
== error_mark_node
)
15033 return error_mark_node
;
15036 /* Construct a new type node and return it. */
15037 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15038 /*raises=*/NULL_TREE
, complain
);
15041 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15042 ARGS into that specification, and return the substituted
15043 specification. If there is no specification, return NULL_TREE. */
15046 tsubst_exception_specification (tree fntype
,
15048 tsubst_flags_t complain
,
15055 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15056 new_specs
= NULL_TREE
;
15057 if (specs
&& TREE_PURPOSE (specs
))
15059 /* A noexcept-specifier. */
15060 tree expr
= TREE_PURPOSE (specs
);
15061 if (TREE_CODE (expr
) == INTEGER_CST
)
15065 /* Defer instantiation of noexcept-specifiers to avoid
15066 excessive instantiations (c++/49107). */
15067 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15068 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15070 /* We already partially instantiated this member template,
15071 so combine the new args with the old. */
15072 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15073 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15074 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15075 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15079 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15080 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15085 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15087 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15089 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15091 new_specs
= tsubst_copy_and_build
15092 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15093 /*integral_constant_expression_p=*/true);
15095 new_specs
= build_noexcept_spec (new_specs
, complain
);
15099 if (! TREE_VALUE (specs
))
15106 tree expanded_specs
= NULL_TREE
;
15108 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15110 /* Expand the pack expansion type. */
15111 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15115 if (expanded_specs
== error_mark_node
)
15116 return error_mark_node
;
15117 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15118 len
= TREE_VEC_LENGTH (expanded_specs
);
15121 /* We're substituting into a member template, so
15122 we got a TYPE_PACK_EXPANSION back. Add that
15123 expansion and move on. */
15124 gcc_assert (TREE_CODE (expanded_specs
)
15125 == TYPE_PACK_EXPANSION
);
15126 new_specs
= add_exception_specifier (new_specs
,
15129 specs
= TREE_CHAIN (specs
);
15134 for (i
= 0; i
< len
; ++i
)
15136 if (expanded_specs
)
15137 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15139 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15140 if (spec
== error_mark_node
)
15142 new_specs
= add_exception_specifier (new_specs
, spec
,
15146 specs
= TREE_CHAIN (specs
);
15152 /* Substitute through a TREE_LIST of types or expressions, handling pack
15156 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15158 if (t
== void_list_node
)
15161 tree purpose
= TREE_PURPOSE (t
);
15162 tree purposevec
= NULL_TREE
;
15165 else if (PACK_EXPANSION_P (purpose
))
15167 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15168 if (TREE_CODE (purpose
) == TREE_VEC
)
15169 purposevec
= purpose
;
15171 else if (TYPE_P (purpose
))
15172 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15174 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15175 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15176 return error_mark_node
;
15178 tree value
= TREE_VALUE (t
);
15179 tree valuevec
= NULL_TREE
;
15182 else if (PACK_EXPANSION_P (value
))
15184 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15185 if (TREE_CODE (value
) == TREE_VEC
)
15188 else if (TYPE_P (value
))
15189 value
= tsubst (value
, args
, complain
, in_decl
);
15191 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15192 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15193 return error_mark_node
;
15195 tree chain
= TREE_CHAIN (t
);
15198 else if (TREE_CODE (chain
) == TREE_LIST
)
15199 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15200 else if (TYPE_P (chain
))
15201 chain
= tsubst (chain
, args
, complain
, in_decl
);
15203 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15204 if (chain
== error_mark_node
)
15205 return error_mark_node
;
15207 if (purpose
== TREE_PURPOSE (t
)
15208 && value
== TREE_VALUE (t
)
15209 && chain
== TREE_CHAIN (t
))
15213 /* Determine the number of arguments. */
15216 len
= TREE_VEC_LENGTH (purposevec
);
15217 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15220 len
= TREE_VEC_LENGTH (valuevec
);
15224 for (int i
= len
; i
-- > 0; )
15227 purpose
= TREE_VEC_ELT (purposevec
, i
);
15229 value
= TREE_VEC_ELT (valuevec
, i
);
15231 if (value
&& TYPE_P (value
))
15232 chain
= hash_tree_cons (purpose
, value
, chain
);
15234 chain
= tree_cons (purpose
, value
, chain
);
15240 /* Take the tree structure T and replace template parameters used
15241 therein with the argument vector ARGS. IN_DECL is an associated
15242 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15243 Issue error and warning messages under control of COMPLAIN. Note
15244 that we must be relatively non-tolerant of extensions here, in
15245 order to preserve conformance; if we allow substitutions that
15246 should not be allowed, we may allow argument deductions that should
15247 not succeed, and therefore report ambiguous overload situations
15248 where there are none. In theory, we could allow the substitution,
15249 but indicate that it should have failed, and allow our caller to
15250 make sure that the right thing happens, but we don't try to do this
15253 This function is used for dealing with types, decls and the like;
15254 for expressions, use tsubst_expr or tsubst_copy. */
15257 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15259 enum tree_code code
;
15260 tree type
, r
= NULL_TREE
;
15262 if (t
== NULL_TREE
|| t
== error_mark_node
15263 || t
== integer_type_node
15264 || t
== void_type_node
15265 || t
== char_type_node
15266 || t
== unknown_type_node
15267 || TREE_CODE (t
) == NAMESPACE_DECL
15268 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15272 return tsubst_decl (t
, args
, complain
);
15274 if (args
== NULL_TREE
)
15277 code
= TREE_CODE (t
);
15279 if (code
== IDENTIFIER_NODE
)
15280 type
= IDENTIFIER_TYPE_VALUE (t
);
15282 type
= TREE_TYPE (t
);
15284 gcc_assert (type
!= unknown_type_node
);
15286 /* Reuse typedefs. We need to do this to handle dependent attributes,
15287 such as attribute aligned. */
15289 && typedef_variant_p (t
))
15291 tree decl
= TYPE_NAME (t
);
15293 if (alias_template_specialization_p (t
, nt_opaque
))
15295 /* DECL represents an alias template and we want to
15297 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15298 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15299 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15301 else if (DECL_CLASS_SCOPE_P (decl
)
15302 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15303 && uses_template_parms (DECL_CONTEXT (decl
)))
15305 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15306 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15307 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15309 else if (DECL_FUNCTION_SCOPE_P (decl
)
15310 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15311 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15312 r
= retrieve_local_specialization (decl
);
15314 /* The typedef is from a non-template context. */
15320 r
= cp_build_qualified_type_real
15321 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15322 complain
| tf_ignore_bad_quals
);
15327 /* We don't have an instantiation yet, so drop the typedef. */
15328 int quals
= cp_type_quals (t
);
15329 t
= DECL_ORIGINAL_TYPE (decl
);
15330 t
= cp_build_qualified_type_real (t
, quals
,
15331 complain
| tf_ignore_bad_quals
);
15335 bool fndecl_type
= (complain
& tf_fndecl_type
);
15336 complain
&= ~tf_fndecl_type
;
15339 && code
!= TYPENAME_TYPE
15340 && code
!= TEMPLATE_TYPE_PARM
15341 && code
!= TEMPLATE_PARM_INDEX
15342 && code
!= IDENTIFIER_NODE
15343 && code
!= FUNCTION_TYPE
15344 && code
!= METHOD_TYPE
)
15345 type
= tsubst (type
, args
, complain
, in_decl
);
15346 if (type
== error_mark_node
)
15347 return error_mark_node
;
15353 case ENUMERAL_TYPE
:
15354 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15355 /*entering_scope=*/0);
15358 case IDENTIFIER_NODE
:
15369 if (t
== integer_type_node
)
15372 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15373 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15377 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15379 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15380 /*integral_constant_expression_p=*/false);
15382 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15384 if (TREE_CODE (max
) == NOP_EXPR
15385 && TREE_SIDE_EFFECTS (omax
)
15386 && !TREE_TYPE (max
))
15387 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15389 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15390 with TREE_SIDE_EFFECTS that indicates this is not an integral
15391 constant expression. */
15392 if (processing_template_decl
15393 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15395 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15396 TREE_SIDE_EFFECTS (max
) = 1;
15399 return compute_array_index_type (NULL_TREE
, max
, complain
);
15402 case TEMPLATE_TYPE_PARM
:
15403 case TEMPLATE_TEMPLATE_PARM
:
15404 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15405 case TEMPLATE_PARM_INDEX
:
15410 tree arg
= NULL_TREE
;
15414 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15415 template_parm_level_and_index (t
, &level
, &idx
);
15417 levels
= TMPL_ARGS_DEPTH (args
);
15418 if (level
<= levels
15419 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15421 arg
= TMPL_ARG (args
, level
, idx
);
15423 /* See through ARGUMENT_PACK_SELECT arguments. */
15424 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15425 arg
= argument_pack_select_arg (arg
);
15428 if (arg
== error_mark_node
)
15429 return error_mark_node
;
15430 else if (arg
!= NULL_TREE
)
15432 if (ARGUMENT_PACK_P (arg
))
15433 /* If ARG is an argument pack, we don't actually want to
15434 perform a substitution here, because substitutions
15435 for argument packs are only done
15436 element-by-element. We can get to this point when
15437 substituting the type of a non-type template
15438 parameter pack, when that type actually contains
15439 template parameter packs from an outer template, e.g.,
15441 template<typename... Types> struct A {
15442 template<Types... Values> struct B { };
15446 if (code
== TEMPLATE_TYPE_PARM
)
15450 /* When building concept checks for the purpose of
15451 deducing placeholders, we can end up with wildcards
15452 where types are expected. Adjust this to the deduced
15454 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15455 arg
= TREE_TYPE (TREE_TYPE (arg
));
15457 gcc_assert (TYPE_P (arg
));
15459 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15461 return cp_build_qualified_type_real
15462 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15464 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15466 /* We are processing a type constructed from a
15467 template template parameter. */
15468 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15469 args
, complain
, in_decl
);
15470 if (argvec
== error_mark_node
)
15471 return error_mark_node
;
15473 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15474 || TREE_CODE (arg
) == TEMPLATE_DECL
15475 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15477 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15478 /* Consider this code:
15480 template <template <class> class Template>
15482 template <class Arg> using Bind = Template<Arg>;
15485 template <template <class> class Template, class Arg>
15486 using Instantiate = Template<Arg>; //#0
15488 template <template <class> class Template,
15491 Instantiate<Internal<Template>::template Bind,
15494 When #1 is parsed, the
15495 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15496 parameter `Template' in #0 matches the
15497 UNBOUND_CLASS_TEMPLATE representing the argument
15498 `Internal<Template>::template Bind'; We then want
15499 to assemble the type `Bind<Argument>' that can't
15500 be fully created right now, because
15501 `Internal<Template>' not being complete, the Bind
15502 template cannot be looked up in that context. So
15503 we need to "store" `Bind<Argument>' for later
15504 when the context of Bind becomes complete. Let's
15505 store that in a TYPENAME_TYPE. */
15506 return make_typename_type (TYPE_CONTEXT (arg
),
15507 build_nt (TEMPLATE_ID_EXPR
,
15508 TYPE_IDENTIFIER (arg
),
15513 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15514 are resolving nested-types in the signature of a
15515 member function templates. Otherwise ARG is a
15516 TEMPLATE_DECL and is the real template to be
15518 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15519 arg
= TYPE_NAME (arg
);
15521 r
= lookup_template_class (arg
,
15523 DECL_CONTEXT (arg
),
15524 /*entering_scope=*/0,
15526 return cp_build_qualified_type_real
15527 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15529 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15532 /* TEMPLATE_PARM_INDEX. */
15533 return convert_from_reference (unshare_expr (arg
));
15537 /* This can happen during the attempted tsubst'ing in
15538 unify. This means that we don't yet have any information
15539 about the template parameter in question. */
15542 /* Early in template argument deduction substitution, we don't
15543 want to reduce the level of 'auto', or it will be confused
15544 with a normal template parm in subsequent deduction.
15545 Similarly, don't reduce the level of template parameters to
15546 avoid mismatches when deducing their types. */
15547 if (complain
& tf_partial
)
15550 /* If we get here, we must have been looking at a parm for a
15551 more deeply nested template. Make a new version of this
15552 template parameter, but with a lower level. */
15555 case TEMPLATE_TYPE_PARM
:
15556 case TEMPLATE_TEMPLATE_PARM
:
15557 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15558 if (cp_type_quals (t
))
15560 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15561 r
= cp_build_qualified_type_real
15562 (r
, cp_type_quals (t
),
15563 complain
| (code
== TEMPLATE_TYPE_PARM
15564 ? tf_ignore_bad_quals
: 0));
15566 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15567 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15568 && (r
= (TEMPLATE_PARM_DESCENDANTS
15569 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15570 && (r
= TREE_TYPE (r
))
15571 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15572 /* Break infinite recursion when substituting the constraints
15573 of a constrained placeholder. */;
15574 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15575 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15576 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15577 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15578 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15579 && (TEMPLATE_PARM_LEVEL (r
)
15580 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15581 /* Cache the simple case of lowering a type parameter. */
15586 TEMPLATE_TYPE_PARM_INDEX (r
)
15587 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15588 r
, levels
, args
, complain
);
15589 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15590 TYPE_MAIN_VARIANT (r
) = r
;
15591 TYPE_POINTER_TO (r
) = NULL_TREE
;
15592 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15594 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15596 /* Propagate constraints on placeholders since they are
15597 only instantiated during satisfaction. */
15598 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15599 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15600 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15602 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15603 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15607 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15608 /* We have reduced the level of the template
15609 template parameter, but not the levels of its
15610 template parameters, so canonical_type_parameter
15611 will not be able to find the canonical template
15612 template parameter for this level. Thus, we
15613 require structural equality checking to compare
15614 TEMPLATE_TEMPLATE_PARMs. */
15615 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15616 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15617 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15619 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15621 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15623 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15624 /* We might need to substitute into the types of non-type
15625 template parameters. */
15626 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15627 complain
, in_decl
);
15628 if (tmpl
== error_mark_node
)
15629 return error_mark_node
;
15630 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15631 complain
, in_decl
);
15632 if (argvec
== error_mark_node
)
15633 return error_mark_node
;
15635 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15636 = build_template_info (tmpl
, argvec
);
15641 case TEMPLATE_PARM_INDEX
:
15642 /* OK, now substitute the type of the non-type parameter. We
15643 couldn't do it earlier because it might be an auto parameter,
15644 and we wouldn't need to if we had an argument. */
15645 type
= tsubst (type
, args
, complain
, in_decl
);
15646 if (type
== error_mark_node
)
15647 return error_mark_node
;
15648 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15652 gcc_unreachable ();
15659 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15662 /* We should never be tsubsting a binfo. */
15663 gcc_unreachable ();
15666 /* A vector of template arguments. */
15667 gcc_assert (!type
);
15668 return tsubst_template_args (t
, args
, complain
, in_decl
);
15671 case REFERENCE_TYPE
:
15673 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15678 Type deduction may fail for any of the following
15681 -- Attempting to create a pointer to reference type.
15682 -- Attempting to create a reference to a reference type or
15683 a reference to void.
15685 Core issue 106 says that creating a reference to a reference
15686 during instantiation is no longer a cause for failure. We
15687 only enforce this check in strict C++98 mode. */
15688 if ((TYPE_REF_P (type
)
15689 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15690 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15692 static location_t last_loc
;
15694 /* We keep track of the last time we issued this error
15695 message to avoid spewing a ton of messages during a
15696 single bad template instantiation. */
15697 if (complain
& tf_error
15698 && last_loc
!= input_location
)
15700 if (VOID_TYPE_P (type
))
15701 error ("forming reference to void");
15702 else if (code
== POINTER_TYPE
)
15703 error ("forming pointer to reference type %qT", type
);
15705 error ("forming reference to reference type %qT", type
);
15706 last_loc
= input_location
;
15709 return error_mark_node
;
15711 else if (TREE_CODE (type
) == FUNCTION_TYPE
15712 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15713 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15715 if (complain
& tf_error
)
15717 if (code
== POINTER_TYPE
)
15718 error ("forming pointer to qualified function type %qT",
15721 error ("forming reference to qualified function type %qT",
15724 return error_mark_node
;
15726 else if (code
== POINTER_TYPE
)
15728 r
= build_pointer_type (type
);
15729 if (TREE_CODE (type
) == METHOD_TYPE
)
15730 r
= build_ptrmemfunc_type (r
);
15732 else if (TYPE_REF_P (type
))
15733 /* In C++0x, during template argument substitution, when there is an
15734 attempt to create a reference to a reference type, reference
15735 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15737 "If a template-argument for a template-parameter T names a type
15738 that is a reference to a type A, an attempt to create the type
15739 'lvalue reference to cv T' creates the type 'lvalue reference to
15740 A,' while an attempt to create the type type rvalue reference to
15741 cv T' creates the type T"
15743 r
= cp_build_reference_type
15745 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15747 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15748 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15750 if (r
!= error_mark_node
)
15751 /* Will this ever be needed for TYPE_..._TO values? */
15758 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15759 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15763 Type deduction may fail for any of the following
15766 -- Attempting to create "pointer to member of T" when T
15767 is not a class type. */
15768 if (complain
& tf_error
)
15769 error ("creating pointer to member of non-class type %qT", r
);
15770 return error_mark_node
;
15772 if (TYPE_REF_P (type
))
15774 if (complain
& tf_error
)
15775 error ("creating pointer to member reference type %qT", type
);
15776 return error_mark_node
;
15778 if (VOID_TYPE_P (type
))
15780 if (complain
& tf_error
)
15781 error ("creating pointer to member of type void");
15782 return error_mark_node
;
15784 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15785 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15787 /* The type of the implicit object parameter gets its
15788 cv-qualifiers from the FUNCTION_TYPE. */
15791 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15792 type_memfn_rqual (type
));
15793 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15794 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15798 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15802 case FUNCTION_TYPE
:
15807 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15808 if (fntype
== error_mark_node
)
15809 return error_mark_node
;
15811 /* Substitute the exception specification. */
15812 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15813 /*defer_ok*/fndecl_type
);
15814 if (specs
== error_mark_node
)
15815 return error_mark_node
;
15817 fntype
= build_exception_variant (fntype
, specs
);
15822 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15823 if (domain
== error_mark_node
)
15824 return error_mark_node
;
15826 /* As an optimization, we avoid regenerating the array type if
15827 it will obviously be the same as T. */
15828 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15831 /* These checks should match the ones in create_array_type_for_decl.
15835 The deduction may fail for any of the following reasons:
15837 -- Attempting to create an array with an element type that
15838 is void, a function type, or a reference type, or [DR337]
15839 an abstract class type. */
15840 if (VOID_TYPE_P (type
)
15841 || TREE_CODE (type
) == FUNCTION_TYPE
15842 || (TREE_CODE (type
) == ARRAY_TYPE
15843 && TYPE_DOMAIN (type
) == NULL_TREE
)
15844 || TYPE_REF_P (type
))
15846 if (complain
& tf_error
)
15847 error ("creating array of %qT", type
);
15848 return error_mark_node
;
15851 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15852 return error_mark_node
;
15854 r
= build_cplus_array_type (type
, domain
);
15856 if (!valid_array_size_p (input_location
, r
, in_decl
,
15857 (complain
& tf_error
)))
15858 return error_mark_node
;
15860 if (TYPE_USER_ALIGN (t
))
15862 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15863 TYPE_USER_ALIGN (r
) = 1;
15869 case TYPENAME_TYPE
:
15871 tree ctx
= TYPE_CONTEXT (t
);
15872 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15874 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15875 if (ctx
== error_mark_node
15876 || TREE_VEC_LENGTH (ctx
) > 1)
15877 return error_mark_node
;
15878 if (TREE_VEC_LENGTH (ctx
) == 0)
15880 if (complain
& tf_error
)
15881 error ("%qD is instantiated for an empty pack",
15882 TYPENAME_TYPE_FULLNAME (t
));
15883 return error_mark_node
;
15885 ctx
= TREE_VEC_ELT (ctx
, 0);
15888 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15889 /*entering_scope=*/1);
15890 if (ctx
== error_mark_node
)
15891 return error_mark_node
;
15893 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15894 complain
, in_decl
);
15895 if (f
== error_mark_node
)
15896 return error_mark_node
;
15898 if (!MAYBE_CLASS_TYPE_P (ctx
))
15900 if (complain
& tf_error
)
15901 error ("%qT is not a class, struct, or union type", ctx
);
15902 return error_mark_node
;
15904 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15906 /* Normally, make_typename_type does not require that the CTX
15907 have complete type in order to allow things like:
15909 template <class T> struct S { typename S<T>::X Y; };
15911 But, such constructs have already been resolved by this
15912 point, so here CTX really should have complete type, unless
15913 it's a partial instantiation. */
15914 ctx
= complete_type (ctx
);
15915 if (!COMPLETE_TYPE_P (ctx
))
15917 if (complain
& tf_error
)
15918 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15919 return error_mark_node
;
15923 f
= make_typename_type (ctx
, f
, typename_type
,
15924 complain
| tf_keep_type_decl
);
15925 if (f
== error_mark_node
)
15927 if (TREE_CODE (f
) == TYPE_DECL
)
15929 complain
|= tf_ignore_bad_quals
;
15933 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15935 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15937 if (complain
& tf_error
)
15938 error ("%qT resolves to %qT, which is not an enumeration type",
15941 return error_mark_node
;
15943 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15945 if (complain
& tf_error
)
15946 error ("%qT resolves to %qT, which is not a class type",
15949 return error_mark_node
;
15953 return cp_build_qualified_type_real
15954 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15957 case UNBOUND_CLASS_TEMPLATE
:
15959 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15960 in_decl
, /*entering_scope=*/1);
15961 tree name
= TYPE_IDENTIFIER (t
);
15962 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15964 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15965 return error_mark_node
;
15968 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15969 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15976 ++cp_unevaluated_operand
;
15977 ++c_inhibit_evaluation_warnings
;
15979 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15981 /*integral_constant_expression_p=*/false);
15983 --cp_unevaluated_operand
;
15984 --c_inhibit_evaluation_warnings
;
15986 type
= finish_typeof (type
);
15987 return cp_build_qualified_type_real (type
,
15989 | cp_type_quals (type
),
15993 case DECLTYPE_TYPE
:
15997 ++cp_unevaluated_operand
;
15998 ++c_inhibit_evaluation_warnings
;
16000 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16001 complain
|tf_decltype
, in_decl
,
16002 /*function_p*/false,
16003 /*integral_constant_expression*/false);
16005 --cp_unevaluated_operand
;
16006 --c_inhibit_evaluation_warnings
;
16008 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16009 type
= lambda_capture_field_type (type
,
16010 false /*explicit_init*/,
16011 DECLTYPE_FOR_REF_CAPTURE (t
));
16012 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16013 type
= lambda_proxy_type (type
);
16016 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16017 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16019 /* In a template ~id could be either a complement expression
16020 or an unqualified-id naming a destructor; if instantiating
16021 it produces an expression, it's not an id-expression or
16024 type
= finish_decltype_type (type
, id
, complain
);
16026 return cp_build_qualified_type_real (type
,
16028 | cp_type_quals (type
),
16029 complain
| tf_ignore_bad_quals
);
16032 case UNDERLYING_TYPE
:
16034 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16035 complain
, in_decl
);
16036 return finish_underlying_type (type
);
16039 case TYPE_ARGUMENT_PACK
:
16040 case NONTYPE_ARGUMENT_PACK
:
16044 if (code
== NONTYPE_ARGUMENT_PACK
)
16045 r
= make_node (code
);
16047 r
= cxx_make_type (code
);
16049 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
16050 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
16051 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
16069 /* We should use one of the expression tsubsts for these codes. */
16070 gcc_unreachable ();
16073 sorry ("use of %qs in template", get_tree_code_name (code
));
16074 return error_mark_node
;
16078 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16079 expression on the left-hand side of the "." or "->" operator. We
16080 only do the lookup if we had a dependent BASELINK. Otherwise we
16081 adjust it onto the instantiated heirarchy. */
16084 tsubst_baselink (tree baselink
, tree object_type
,
16085 tree args
, tsubst_flags_t complain
, tree in_decl
)
16087 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16088 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16089 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16091 tree optype
= BASELINK_OPTYPE (baselink
);
16092 optype
= tsubst (optype
, args
, complain
, in_decl
);
16094 tree template_args
= NULL_TREE
;
16095 bool template_id_p
= false;
16096 tree fns
= BASELINK_FUNCTIONS (baselink
);
16097 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16099 template_id_p
= true;
16100 template_args
= TREE_OPERAND (fns
, 1);
16101 fns
= TREE_OPERAND (fns
, 0);
16103 template_args
= tsubst_template_args (template_args
, args
,
16104 complain
, in_decl
);
16107 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16108 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16109 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16113 tree name
= OVL_NAME (fns
);
16114 if (IDENTIFIER_CONV_OP_P (name
))
16115 name
= make_conv_op_name (optype
);
16117 if (name
== complete_dtor_identifier
)
16118 /* Treat as-if non-dependent below. */
16119 dependent_p
= false;
16121 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16125 if ((complain
& tf_error
)
16126 && constructor_name_p (name
, qualifying_scope
))
16127 error ("cannot call constructor %<%T::%D%> directly",
16128 qualifying_scope
, name
);
16129 return error_mark_node
;
16132 if (BASELINK_P (baselink
))
16133 fns
= BASELINK_FUNCTIONS (baselink
);
16136 /* We're going to overwrite pieces below, make a duplicate. */
16137 baselink
= copy_node (baselink
);
16139 /* If lookup found a single function, mark it as used at this point.
16140 (If lookup found multiple functions the one selected later by
16141 overload resolution will be marked as used at that point.) */
16142 if (!template_id_p
&& !really_overloaded_fn (fns
))
16144 tree fn
= OVL_FIRST (fns
);
16145 bool ok
= mark_used (fn
, complain
);
16146 if (!ok
&& !(complain
& tf_error
))
16147 return error_mark_node
;
16148 if (ok
&& BASELINK_P (baselink
))
16149 /* We might have instantiated an auto function. */
16150 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16153 if (BASELINK_P (baselink
))
16155 /* Add back the template arguments, if present. */
16157 BASELINK_FUNCTIONS (baselink
)
16158 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16160 /* Update the conversion operator type. */
16161 BASELINK_OPTYPE (baselink
) = optype
;
16165 object_type
= current_class_type
;
16167 if (qualified_p
|| !dependent_p
)
16169 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16173 /* We need to call adjust_result_of_qualified_name_lookup in case the
16174 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16175 so that we still get virtual function binding. */
16176 BASELINK_QUALIFIED_P (baselink
) = false;
16182 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16183 true if the qualified-id will be a postfix-expression in-and-of
16184 itself; false if more of the postfix-expression follows the
16185 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16189 tsubst_qualified_id (tree qualified_id
, tree args
,
16190 tsubst_flags_t complain
, tree in_decl
,
16191 bool done
, bool address_p
)
16197 tree template_args
;
16198 location_t loc
= UNKNOWN_LOCATION
;
16200 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16202 /* Figure out what name to look up. */
16203 name
= TREE_OPERAND (qualified_id
, 1);
16204 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16206 is_template
= true;
16207 loc
= EXPR_LOCATION (name
);
16208 template_args
= TREE_OPERAND (name
, 1);
16210 template_args
= tsubst_template_args (template_args
, args
,
16211 complain
, in_decl
);
16212 if (template_args
== error_mark_node
)
16213 return error_mark_node
;
16214 name
= TREE_OPERAND (name
, 0);
16218 is_template
= false;
16219 template_args
= NULL_TREE
;
16222 /* Substitute into the qualifying scope. When there are no ARGS, we
16223 are just trying to simplify a non-dependent expression. In that
16224 case the qualifying scope may be dependent, and, in any case,
16225 substituting will not help. */
16226 scope
= TREE_OPERAND (qualified_id
, 0);
16229 scope
= tsubst (scope
, args
, complain
, in_decl
);
16230 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16235 if (dependent_scope_p (scope
))
16238 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16239 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16240 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16241 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16245 if (!BASELINK_P (name
) && !DECL_P (expr
))
16247 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16249 /* A BIT_NOT_EXPR is used to represent a destructor. */
16250 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16252 error ("qualifying type %qT does not match destructor name ~%qT",
16253 scope
, TREE_OPERAND (expr
, 0));
16254 expr
= error_mark_node
;
16257 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16258 LOOK_want::NORMAL
, false);
16261 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16262 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16263 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16265 if (complain
& tf_error
)
16267 error ("dependent-name %qE is parsed as a non-type, but "
16268 "instantiation yields a type", qualified_id
);
16269 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16271 return error_mark_node
;
16277 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16279 return error_mark_node
;
16280 /* Remember that there was a reference to this entity. */
16281 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16282 return error_mark_node
;
16285 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16287 if (complain
& tf_error
)
16288 qualified_name_lookup_error (scope
,
16289 TREE_OPERAND (qualified_id
, 1),
16290 expr
, input_location
);
16291 return error_mark_node
;
16296 /* We may be repeating a check already done during parsing, but
16297 if it was well-formed and passed then, it will pass again
16298 now, and if it didn't, we wouldn't have got here. The case
16299 we want to catch is when we couldn't tell then, and can now,
16300 namely when templ prior to substitution was an
16302 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16303 return error_mark_node
;
16305 if (variable_template_p (expr
))
16306 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16309 expr
= lookup_template_function (expr
, template_args
);
16312 if (expr
== error_mark_node
&& complain
& tf_error
)
16313 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16314 expr
, input_location
);
16315 else if (TYPE_P (scope
))
16317 expr
= (adjust_result_of_qualified_name_lookup
16318 (expr
, scope
, current_nonlambda_class_type ()));
16319 expr
= (finish_qualified_id_expr
16320 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16321 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16322 /*template_arg_p=*/false, complain
));
16325 /* Expressions do not generally have reference type. */
16326 if (TREE_CODE (expr
) != SCOPE_REF
16327 /* However, if we're about to form a pointer-to-member, we just
16328 want the referenced member referenced. */
16329 && TREE_CODE (expr
) != OFFSET_REF
)
16330 expr
= convert_from_reference (expr
);
16332 if (REF_PARENTHESIZED_P (qualified_id
))
16333 expr
= force_paren_expr (expr
);
16338 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16339 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16343 tsubst_init (tree init
, tree decl
, tree args
,
16344 tsubst_flags_t complain
, tree in_decl
)
16349 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16351 tree type
= TREE_TYPE (decl
);
16353 if (!init
&& type
!= error_mark_node
)
16355 if (tree auto_node
= type_uses_auto (type
))
16357 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16359 if (complain
& tf_error
)
16360 error ("initializer for %q#D expands to an empty list "
16361 "of expressions", decl
);
16362 return error_mark_node
;
16365 else if (!dependent_type_p (type
))
16367 /* If we had an initializer but it
16368 instantiated to nothing,
16369 value-initialize the object. This will
16370 only occur when the initializer was a
16371 pack expansion where the parameter packs
16372 used in that expansion were of length
16374 init
= build_value_init (type
, complain
);
16375 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16376 init
= get_target_expr_sfinae (init
, complain
);
16377 if (TREE_CODE (init
) == TARGET_EXPR
)
16378 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16385 /* If T is a reference to a dependent member of the current instantiation C and
16386 we are trying to refer to that member in a partial instantiation of C,
16387 return a SCOPE_REF; otherwise, return NULL_TREE.
16389 This can happen when forming a C++17 deduction guide, as in PR96199. */
16392 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16395 if (cxx_dialect
< cxx17
)
16398 tree ctx
= context_for_name_lookup (t
);
16399 if (!CLASS_TYPE_P (ctx
))
16402 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16403 if (dependent_scope_p (ctx
))
16404 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16405 /*template_p=*/false);
16410 /* Like tsubst, but deals with expressions. This function just replaces
16411 template parms; to finish processing the resultant expression, use
16412 tsubst_copy_and_build or tsubst_expr. */
16415 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16417 enum tree_code code
;
16420 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16423 code
= TREE_CODE (t
);
16428 r
= retrieve_local_specialization (t
);
16430 if (r
== NULL_TREE
)
16432 /* We get here for a use of 'this' in an NSDMI. */
16433 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16434 return current_class_ptr
;
16436 /* This can happen for a parameter name used later in a function
16437 declaration (such as in a late-specified return type). Just
16438 make a dummy decl, since it's only used for its type. */
16439 gcc_assert (cp_unevaluated_operand
!= 0);
16440 r
= tsubst_decl (t
, args
, complain
);
16441 /* Give it the template pattern as its context; its true context
16442 hasn't been instantiated yet and this is good enough for
16444 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16447 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16448 r
= argument_pack_select_arg (r
);
16449 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16450 return error_mark_node
;
16458 if (DECL_TEMPLATE_PARM_P (t
))
16459 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16460 /* There is no need to substitute into namespace-scope
16462 if (DECL_NAMESPACE_SCOPE_P (t
))
16464 /* If ARGS is NULL, then T is known to be non-dependent. */
16465 if (args
== NULL_TREE
)
16466 return scalar_constant_value (t
);
16468 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16471 /* Unfortunately, we cannot just call lookup_name here.
16474 template <int I> int f() {
16476 struct S { void g() { E e = a; } };
16479 When we instantiate f<7>::S::g(), say, lookup_name is not
16480 clever enough to find f<7>::a. */
16482 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16483 /*entering_scope=*/0);
16485 for (v
= TYPE_VALUES (enum_type
);
16487 v
= TREE_CHAIN (v
))
16488 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16489 return TREE_VALUE (v
);
16491 /* We didn't find the name. That should never happen; if
16492 name-lookup found it during preliminary parsing, we
16493 should find it again here during instantiation. */
16494 gcc_unreachable ();
16499 if (DECL_CONTEXT (t
))
16503 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16504 /*entering_scope=*/1);
16505 if (ctx
!= DECL_CONTEXT (t
))
16507 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16510 if (complain
& tf_error
)
16511 error ("using invalid field %qD", t
);
16512 return error_mark_node
;
16521 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16524 case FUNCTION_DECL
:
16525 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16526 r
= tsubst (t
, args
, complain
, in_decl
);
16527 else if (DECL_LOCAL_DECL_P (t
))
16529 /* Local specialization will have been created when we
16530 instantiated the DECL_EXPR_DECL. */
16531 r
= retrieve_local_specialization (t
);
16533 r
= error_mark_node
;
16535 else if (local_variable_p (t
)
16536 && uses_template_parms (DECL_CONTEXT (t
)))
16538 r
= retrieve_local_specialization (t
);
16539 if (r
== NULL_TREE
)
16541 /* First try name lookup to find the instantiation. */
16542 r
= lookup_name (DECL_NAME (t
));
16547 /* During error-recovery we may find a non-variable,
16548 even an OVERLOAD: just bail out and avoid ICEs and
16549 duplicate diagnostics (c++/62207). */
16550 gcc_assert (seen_error ());
16551 return error_mark_node
;
16553 if (!is_capture_proxy (r
))
16555 /* Make sure the one we found is the one we want. */
16556 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16557 if (ctx
!= DECL_CONTEXT (r
))
16566 /* This can happen for a variable used in a
16567 late-specified return type of a local lambda, or for a
16568 local static or constant. Building a new VAR_DECL
16569 should be OK in all those cases. */
16570 r
= tsubst_decl (t
, args
, complain
);
16571 if (local_specializations
)
16572 /* Avoid infinite recursion (79640). */
16573 register_local_specialization (r
, t
);
16574 if (decl_maybe_constant_var_p (r
))
16576 /* We can't call cp_finish_decl, so handle the
16577 initializer by hand. */
16578 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16579 complain
, in_decl
);
16580 if (!processing_template_decl
)
16581 init
= maybe_constant_init (init
);
16582 if (processing_template_decl
16583 ? potential_constant_expression (init
)
16584 : reduced_constant_expression_p (init
))
16585 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16586 = TREE_CONSTANT (r
) = true;
16587 DECL_INITIAL (r
) = init
;
16588 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16590 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16591 complain
, adc_variable_type
);
16593 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16594 || decl_constant_var_p (r
)
16596 if (!processing_template_decl
16597 && !TREE_STATIC (r
))
16598 r
= process_outer_var_ref (r
, complain
);
16600 /* Remember this for subsequent uses. */
16601 if (local_specializations
)
16602 register_local_specialization (r
, t
);
16604 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16605 r
= argument_pack_select_arg (r
);
16609 if (!mark_used (r
, complain
))
16610 return error_mark_node
;
16613 case NAMESPACE_DECL
:
16620 return tsubst_baselink (t
, current_nonlambda_class_type (),
16621 args
, complain
, in_decl
);
16623 case TEMPLATE_DECL
:
16624 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16625 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16626 args
, complain
, in_decl
);
16627 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16628 return tsubst (t
, args
, complain
, in_decl
);
16629 else if (DECL_CLASS_SCOPE_P (t
)
16630 && uses_template_parms (DECL_CONTEXT (t
)))
16632 /* Template template argument like the following example need
16635 template <template <class> class TT> struct C {};
16636 template <class T> struct D {
16637 template <class U> struct E {};
16642 We are processing the template argument `E' in #1 for
16643 the template instantiation #2. Originally, `E' is a
16644 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16645 have to substitute this with one having context `D<int>'. */
16647 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16648 if (dependent_scope_p (context
))
16650 /* When rewriting a constructor into a deduction guide, a
16651 non-dependent name can become dependent, so memtmpl<args>
16652 becomes context::template memtmpl<args>. */
16653 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16654 return build_qualified_name (type
, context
, DECL_NAME (t
),
16657 return lookup_field (context
, DECL_NAME(t
), 0, false);
16660 /* Ordinary template template argument. */
16663 case NON_LVALUE_EXPR
:
16664 case VIEW_CONVERT_EXPR
:
16666 /* Handle location wrappers by substituting the wrapped node
16667 first, *then* reusing the resulting type. Doing the type
16668 first ensures that we handle template parameters and
16669 parameter pack expansions. */
16670 if (location_wrapper_p (t
))
16672 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16673 complain
, in_decl
);
16674 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16676 tree op
= TREE_OPERAND (t
, 0);
16677 if (code
== VIEW_CONVERT_EXPR
16678 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16680 /* Wrapper to make a C++20 template parameter object const. */
16681 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16682 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16684 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16685 return build1 (code
, type
, op
);
16689 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16690 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16691 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16695 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16696 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16698 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16699 op
= build1 (code
, TREE_TYPE (op
), op
);
16700 REF_PARENTHESIZED_P (op
) = true;
16703 /* We shouldn't see any other uses of these in templates. */
16704 gcc_unreachable ();
16708 case REINTERPRET_CAST_EXPR
:
16709 case CONST_CAST_EXPR
:
16710 case STATIC_CAST_EXPR
:
16711 case DYNAMIC_CAST_EXPR
:
16712 case IMPLICIT_CONV_EXPR
:
16716 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16717 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16718 return build1 (code
, type
, op0
);
16722 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16723 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16725 tree expanded
, op
= TREE_OPERAND (t
, 0);
16728 if (SIZEOF_EXPR_TYPE_P (t
))
16729 op
= TREE_TYPE (op
);
16731 ++cp_unevaluated_operand
;
16732 ++c_inhibit_evaluation_warnings
;
16733 /* We only want to compute the number of arguments. */
16734 if (PACK_EXPANSION_P (op
))
16735 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16737 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16738 args
, complain
, in_decl
);
16739 --cp_unevaluated_operand
;
16740 --c_inhibit_evaluation_warnings
;
16742 if (TREE_CODE (expanded
) == TREE_VEC
)
16744 len
= TREE_VEC_LENGTH (expanded
);
16745 /* Set TREE_USED for the benefit of -Wunused. */
16746 for (int i
= 0; i
< len
; i
++)
16747 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16748 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16751 if (expanded
== error_mark_node
)
16752 return error_mark_node
;
16753 else if (PACK_EXPANSION_P (expanded
)
16754 || (TREE_CODE (expanded
) == TREE_VEC
16755 && pack_expansion_args_count (expanded
)))
16758 if (PACK_EXPANSION_P (expanded
))
16760 else if (TREE_VEC_LENGTH (expanded
) == 1)
16761 expanded
= TREE_VEC_ELT (expanded
, 0);
16763 expanded
= make_argument_pack (expanded
);
16765 if (TYPE_P (expanded
))
16766 return cxx_sizeof_or_alignof_type (input_location
,
16767 expanded
, SIZEOF_EXPR
,
16769 complain
& tf_error
);
16771 return cxx_sizeof_or_alignof_expr (input_location
,
16772 expanded
, SIZEOF_EXPR
,
16773 complain
& tf_error
);
16776 return build_int_cst (size_type_node
, len
);
16778 if (SIZEOF_EXPR_TYPE_P (t
))
16780 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16781 args
, complain
, in_decl
);
16782 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16783 r
= build1 (SIZEOF_EXPR
,
16784 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16785 SIZEOF_EXPR_TYPE_P (r
) = 1;
16792 case TRUTH_NOT_EXPR
:
16795 case UNARY_PLUS_EXPR
: /* Unary + */
16797 case AT_ENCODE_EXPR
:
16801 case REALPART_EXPR
:
16802 case IMAGPART_EXPR
:
16805 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16806 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16807 r
= build1 (code
, type
, op0
);
16808 if (code
== ALIGNOF_EXPR
)
16809 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16813 case COMPONENT_REF
:
16818 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16819 name
= TREE_OPERAND (t
, 1);
16820 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16822 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16823 complain
, in_decl
);
16824 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16826 else if (TREE_CODE (name
) == SCOPE_REF
16827 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16829 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16830 complain
, in_decl
);
16831 name
= TREE_OPERAND (name
, 1);
16832 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16833 complain
, in_decl
);
16834 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16835 name
= build_qualified_name (/*type=*/NULL_TREE
,
16837 /*template_p=*/false);
16839 else if (BASELINK_P (name
))
16840 name
= tsubst_baselink (name
,
16841 non_reference (TREE_TYPE (object
)),
16845 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16846 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16852 case TRUNC_DIV_EXPR
:
16853 case CEIL_DIV_EXPR
:
16854 case FLOOR_DIV_EXPR
:
16855 case ROUND_DIV_EXPR
:
16856 case EXACT_DIV_EXPR
:
16860 case TRUNC_MOD_EXPR
:
16861 case FLOOR_MOD_EXPR
:
16862 case TRUTH_ANDIF_EXPR
:
16863 case TRUTH_ORIF_EXPR
:
16864 case TRUTH_AND_EXPR
:
16865 case TRUTH_OR_EXPR
:
16876 case COMPOUND_EXPR
:
16879 case PREDECREMENT_EXPR
:
16880 case PREINCREMENT_EXPR
:
16881 case POSTDECREMENT_EXPR
:
16882 case POSTINCREMENT_EXPR
:
16884 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16885 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16886 return build_nt (code
, op0
, op1
);
16891 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16892 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16893 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16894 QUALIFIED_NAME_IS_TEMPLATE (t
));
16899 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16900 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16901 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16906 int n
= VL_EXP_OPERAND_LENGTH (t
);
16907 tree result
= build_vl_exp (CALL_EXPR
, n
);
16909 for (i
= 0; i
< n
; i
++)
16910 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16911 complain
, in_decl
);
16917 case PSEUDO_DTOR_EXPR
:
16918 case VEC_PERM_EXPR
:
16920 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16921 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16922 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16923 r
= build_nt (code
, op0
, op1
, op2
);
16924 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16930 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16931 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16932 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16933 r
= build_nt (code
, op0
, op1
, op2
);
16934 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16940 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16941 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16942 r
= build_nt (code
, op0
, op1
);
16943 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16944 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16948 case TEMPLATE_ID_EXPR
:
16950 /* Substituted template arguments */
16951 tree fn
= TREE_OPERAND (t
, 0);
16952 tree targs
= TREE_OPERAND (t
, 1);
16954 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16956 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16958 return lookup_template_function (fn
, targs
);
16963 tree purpose
, value
, chain
;
16965 if (t
== void_list_node
)
16968 purpose
= TREE_PURPOSE (t
);
16970 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16971 value
= TREE_VALUE (t
);
16973 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16974 chain
= TREE_CHAIN (t
);
16975 if (chain
&& chain
!= void_type_node
)
16976 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16977 if (purpose
== TREE_PURPOSE (t
)
16978 && value
== TREE_VALUE (t
)
16979 && chain
== TREE_CHAIN (t
))
16981 return tree_cons (purpose
, value
, chain
);
16986 case ENUMERAL_TYPE
:
16988 case TEMPLATE_TYPE_PARM
:
16989 case TEMPLATE_TEMPLATE_PARM
:
16990 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16991 case TEMPLATE_PARM_INDEX
:
16993 case REFERENCE_TYPE
:
16995 case FUNCTION_TYPE
:
16998 case TYPENAME_TYPE
:
16999 case UNBOUND_CLASS_TEMPLATE
:
17001 case DECLTYPE_TYPE
:
17003 return tsubst (t
, args
, complain
, in_decl
);
17007 /* Fall through. */
17008 case IDENTIFIER_NODE
:
17009 if (IDENTIFIER_CONV_OP_P (t
))
17011 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17012 return make_conv_op_name (new_type
);
17018 /* This is handled by tsubst_copy_and_build. */
17019 gcc_unreachable ();
17023 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17024 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17025 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17028 case CLEANUP_POINT_EXPR
:
17029 /* We shouldn't have built any of these during initial template
17030 generation. Instead, they should be built during instantiation
17031 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17032 gcc_unreachable ();
17036 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17037 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17038 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17039 r
= build2 (code
, type
, op0
, op1
);
17040 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17041 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17042 && !(complain
& tf_error
))
17043 return error_mark_node
;
17047 case EXPR_PACK_EXPANSION
:
17048 error ("invalid use of pack expansion expression");
17049 return error_mark_node
;
17051 case NONTYPE_ARGUMENT_PACK
:
17052 error ("use %<...%> to expand argument pack");
17053 return error_mark_node
;
17056 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17063 /* Instantiate any typedefs in the type. */
17064 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17065 r
= fold_convert (type
, t
);
17066 gcc_assert (TREE_CODE (r
) == code
);
17072 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17074 if (type
!= TREE_TYPE (t
))
17077 TREE_TYPE (r
) = type
;
17083 /* These can sometimes show up in a partial instantiation, but never
17084 involve template parms. */
17085 gcc_assert (!uses_template_parms (t
));
17088 case UNARY_LEFT_FOLD_EXPR
:
17089 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17090 case UNARY_RIGHT_FOLD_EXPR
:
17091 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17092 case BINARY_LEFT_FOLD_EXPR
:
17093 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17094 case BINARY_RIGHT_FOLD_EXPR
:
17095 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17099 case DEBUG_BEGIN_STMT
:
17100 /* ??? There's no point in copying it for now, but maybe some
17101 day it will contain more information, such as a pointer back
17102 to the containing function, inlined copy or so. */
17105 case CO_AWAIT_EXPR
:
17106 return tsubst_expr (t
, args
, complain
, in_decl
,
17107 /*integral_constant_expression_p=*/false);
17111 /* We shouldn't get here, but keep going if !flag_checking. */
17113 gcc_unreachable ();
17118 /* Helper function for tsubst_omp_clauses, used for instantiation of
17119 OMP_CLAUSE_DECL of clauses. */
17122 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17123 tree in_decl
, tree
*iterator_cache
)
17125 if (decl
== NULL_TREE
)
17128 /* Handle OpenMP iterators. */
17129 if (TREE_CODE (decl
) == TREE_LIST
17130 && TREE_PURPOSE (decl
)
17131 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17134 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17135 ret
= iterator_cache
[1];
17139 begin_scope (sk_omp
, NULL
);
17140 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17142 *tp
= copy_node (it
);
17143 TREE_VEC_ELT (*tp
, 0)
17144 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17145 TREE_VEC_ELT (*tp
, 1)
17146 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17147 /*integral_constant_expression_p=*/false);
17148 TREE_VEC_ELT (*tp
, 2)
17149 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17150 /*integral_constant_expression_p=*/false);
17151 TREE_VEC_ELT (*tp
, 3)
17152 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17153 /*integral_constant_expression_p=*/false);
17154 TREE_CHAIN (*tp
) = NULL_TREE
;
17155 tp
= &TREE_CHAIN (*tp
);
17157 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17158 iterator_cache
[0] = TREE_PURPOSE (decl
);
17159 iterator_cache
[1] = ret
;
17161 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17166 /* Handle an OpenMP array section represented as a TREE_LIST (or
17167 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17168 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17169 TREE_LIST. We can handle it exactly the same as an array section
17170 (purpose, value, and a chain), even though the nomenclature
17171 (low_bound, length, etc) is different. */
17172 if (TREE_CODE (decl
) == TREE_LIST
)
17175 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17176 /*integral_constant_expression_p=*/false);
17177 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17178 /*integral_constant_expression_p=*/false);
17179 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17181 if (TREE_PURPOSE (decl
) == low_bound
17182 && TREE_VALUE (decl
) == length
17183 && TREE_CHAIN (decl
) == chain
)
17185 tree ret
= tree_cons (low_bound
, length
, chain
);
17186 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17187 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17190 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17191 /*integral_constant_expression_p=*/false);
17192 /* Undo convert_from_reference tsubst_expr could have called. */
17194 && REFERENCE_REF_P (ret
)
17195 && !REFERENCE_REF_P (decl
))
17196 ret
= TREE_OPERAND (ret
, 0);
17200 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17203 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17204 tree args
, tsubst_flags_t complain
, tree in_decl
)
17206 tree new_clauses
= NULL_TREE
, nc
, oc
;
17207 tree linear_no_step
= NULL_TREE
;
17208 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17210 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17212 nc
= copy_node (oc
);
17213 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17216 switch (OMP_CLAUSE_CODE (nc
))
17218 case OMP_CLAUSE_LASTPRIVATE
:
17219 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17221 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17222 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17223 in_decl
, /*integral_constant_expression_p=*/false);
17224 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17225 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17228 case OMP_CLAUSE_PRIVATE
:
17229 case OMP_CLAUSE_SHARED
:
17230 case OMP_CLAUSE_FIRSTPRIVATE
:
17231 case OMP_CLAUSE_COPYIN
:
17232 case OMP_CLAUSE_COPYPRIVATE
:
17233 case OMP_CLAUSE_UNIFORM
:
17234 case OMP_CLAUSE_DEPEND
:
17235 case OMP_CLAUSE_FROM
:
17236 case OMP_CLAUSE_TO
:
17237 case OMP_CLAUSE_MAP
:
17238 case OMP_CLAUSE_NONTEMPORAL
:
17239 case OMP_CLAUSE_USE_DEVICE_PTR
:
17240 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17241 case OMP_CLAUSE_IS_DEVICE_PTR
:
17242 case OMP_CLAUSE_INCLUSIVE
:
17243 case OMP_CLAUSE_EXCLUSIVE
:
17244 OMP_CLAUSE_DECL (nc
)
17245 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17246 in_decl
, iterator_cache
);
17248 case OMP_CLAUSE_TILE
:
17249 case OMP_CLAUSE_IF
:
17250 case OMP_CLAUSE_NUM_THREADS
:
17251 case OMP_CLAUSE_SCHEDULE
:
17252 case OMP_CLAUSE_COLLAPSE
:
17253 case OMP_CLAUSE_FINAL
:
17254 case OMP_CLAUSE_DEVICE
:
17255 case OMP_CLAUSE_DIST_SCHEDULE
:
17256 case OMP_CLAUSE_NUM_TEAMS
:
17257 case OMP_CLAUSE_THREAD_LIMIT
:
17258 case OMP_CLAUSE_SAFELEN
:
17259 case OMP_CLAUSE_SIMDLEN
:
17260 case OMP_CLAUSE_NUM_TASKS
:
17261 case OMP_CLAUSE_GRAINSIZE
:
17262 case OMP_CLAUSE_PRIORITY
:
17263 case OMP_CLAUSE_ORDERED
:
17264 case OMP_CLAUSE_HINT
:
17265 case OMP_CLAUSE_NUM_GANGS
:
17266 case OMP_CLAUSE_NUM_WORKERS
:
17267 case OMP_CLAUSE_VECTOR_LENGTH
:
17268 case OMP_CLAUSE_WORKER
:
17269 case OMP_CLAUSE_VECTOR
:
17270 case OMP_CLAUSE_ASYNC
:
17271 case OMP_CLAUSE_WAIT
:
17272 OMP_CLAUSE_OPERAND (nc
, 0)
17273 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17274 in_decl
, /*integral_constant_expression_p=*/false);
17276 case OMP_CLAUSE_REDUCTION
:
17277 case OMP_CLAUSE_IN_REDUCTION
:
17278 case OMP_CLAUSE_TASK_REDUCTION
:
17279 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17281 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17282 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17284 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17285 complain
, in_decl
);
17286 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17287 = build_qualified_name (NULL_TREE
, scope
,
17288 TREE_OPERAND (placeholder
, 1),
17292 gcc_assert (identifier_p (placeholder
));
17294 OMP_CLAUSE_DECL (nc
)
17295 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17298 case OMP_CLAUSE_GANG
:
17299 case OMP_CLAUSE_ALIGNED
:
17300 OMP_CLAUSE_DECL (nc
)
17301 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17303 OMP_CLAUSE_OPERAND (nc
, 1)
17304 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17305 in_decl
, /*integral_constant_expression_p=*/false);
17307 case OMP_CLAUSE_LINEAR
:
17308 OMP_CLAUSE_DECL (nc
)
17309 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17311 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17313 gcc_assert (!linear_no_step
);
17314 linear_no_step
= nc
;
17316 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17317 OMP_CLAUSE_LINEAR_STEP (nc
)
17318 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17319 complain
, in_decl
, NULL
);
17321 OMP_CLAUSE_LINEAR_STEP (nc
)
17322 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17324 /*integral_constant_expression_p=*/false);
17326 case OMP_CLAUSE_NOWAIT
:
17327 case OMP_CLAUSE_DEFAULT
:
17328 case OMP_CLAUSE_UNTIED
:
17329 case OMP_CLAUSE_MERGEABLE
:
17330 case OMP_CLAUSE_INBRANCH
:
17331 case OMP_CLAUSE_NOTINBRANCH
:
17332 case OMP_CLAUSE_PROC_BIND
:
17333 case OMP_CLAUSE_FOR
:
17334 case OMP_CLAUSE_PARALLEL
:
17335 case OMP_CLAUSE_SECTIONS
:
17336 case OMP_CLAUSE_TASKGROUP
:
17337 case OMP_CLAUSE_NOGROUP
:
17338 case OMP_CLAUSE_THREADS
:
17339 case OMP_CLAUSE_SIMD
:
17340 case OMP_CLAUSE_DEFAULTMAP
:
17341 case OMP_CLAUSE_ORDER
:
17342 case OMP_CLAUSE_BIND
:
17343 case OMP_CLAUSE_INDEPENDENT
:
17344 case OMP_CLAUSE_AUTO
:
17345 case OMP_CLAUSE_SEQ
:
17346 case OMP_CLAUSE_IF_PRESENT
:
17347 case OMP_CLAUSE_FINALIZE
:
17350 gcc_unreachable ();
17352 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17353 switch (OMP_CLAUSE_CODE (nc
))
17355 case OMP_CLAUSE_SHARED
:
17356 case OMP_CLAUSE_PRIVATE
:
17357 case OMP_CLAUSE_FIRSTPRIVATE
:
17358 case OMP_CLAUSE_LASTPRIVATE
:
17359 case OMP_CLAUSE_COPYPRIVATE
:
17360 case OMP_CLAUSE_LINEAR
:
17361 case OMP_CLAUSE_REDUCTION
:
17362 case OMP_CLAUSE_IN_REDUCTION
:
17363 case OMP_CLAUSE_TASK_REDUCTION
:
17364 case OMP_CLAUSE_USE_DEVICE_PTR
:
17365 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17366 case OMP_CLAUSE_IS_DEVICE_PTR
:
17367 case OMP_CLAUSE_INCLUSIVE
:
17368 case OMP_CLAUSE_EXCLUSIVE
:
17369 /* tsubst_expr on SCOPE_REF results in returning
17370 finish_non_static_data_member result. Undo that here. */
17371 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17372 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17373 == IDENTIFIER_NODE
))
17375 tree t
= OMP_CLAUSE_DECL (nc
);
17378 switch (TREE_CODE (v
))
17380 case COMPONENT_REF
:
17384 case POINTER_PLUS_EXPR
:
17385 v
= TREE_OPERAND (v
, 0);
17388 if (DECL_CONTEXT (v
) == current_function_decl
17389 && DECL_ARTIFICIAL (v
)
17390 && DECL_NAME (v
) == this_identifier
)
17391 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17398 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17399 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17400 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17401 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17402 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17404 tree decl
= OMP_CLAUSE_DECL (nc
);
17407 retrofit_lang_decl (decl
);
17408 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17417 new_clauses
= nreverse (new_clauses
);
17418 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17420 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17421 if (linear_no_step
)
17422 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17423 if (nc
== linear_no_step
)
17425 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17429 return new_clauses
;
17432 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17435 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17438 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17440 tree purpose
, value
, chain
;
17445 if (TREE_CODE (t
) != TREE_LIST
)
17446 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17447 /*function_p=*/false,
17448 /*integral_constant_expression_p=*/false);
17450 if (t
== void_list_node
)
17453 purpose
= TREE_PURPOSE (t
);
17455 purpose
= RECUR (purpose
);
17456 value
= TREE_VALUE (t
);
17459 if (TREE_CODE (value
) != LABEL_DECL
)
17460 value
= RECUR (value
);
17463 value
= lookup_label (DECL_NAME (value
));
17464 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17465 TREE_USED (value
) = 1;
17468 chain
= TREE_CHAIN (t
);
17469 if (chain
&& chain
!= void_type_node
)
17470 chain
= RECUR (chain
);
17471 return tree_cons (purpose
, value
, chain
);
17475 /* Used to temporarily communicate the list of #pragma omp parallel
17476 clauses to #pragma omp for instantiation if they are combined
17479 static tree
*omp_parallel_combined_clauses
;
17481 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17482 tree
*, unsigned int *);
17484 /* Substitute one OMP_FOR iterator. */
17487 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17488 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17489 tree args
, tsubst_flags_t complain
, tree in_decl
,
17490 bool integral_constant_expression_p
)
17492 #define RECUR(NODE) \
17493 tsubst_expr ((NODE), args, complain, in_decl, \
17494 integral_constant_expression_p)
17495 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17498 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17499 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17501 decl
= TREE_OPERAND (init
, 0);
17502 init
= TREE_OPERAND (init
, 1);
17503 tree decl_expr
= NULL_TREE
;
17504 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17507 bool decomp
= false;
17508 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17510 tree v
= DECL_VALUE_EXPR (decl
);
17511 if (TREE_CODE (v
) == ARRAY_REF
17512 && VAR_P (TREE_OPERAND (v
, 0))
17513 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17515 tree decomp_first
= NULL_TREE
;
17516 unsigned decomp_cnt
= 0;
17517 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17518 maybe_push_decl (d
);
17519 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17520 in_decl
, &decomp_first
, &decomp_cnt
);
17522 if (d
== error_mark_node
)
17523 decl
= error_mark_node
;
17525 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17527 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17529 tree v
= build_nt (ARRAY_REF
, d
,
17530 size_int (decomp_cnt
- i
- 1),
17531 NULL_TREE
, NULL_TREE
);
17532 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17533 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17535 fit_decomposition_lang_decl (decomp_first
, d
);
17536 decomp_first
= DECL_CHAIN (decomp_first
);
17540 decl
= tsubst_decl (decl
, args
, complain
);
17542 maybe_push_decl (decl
);
17544 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17546 /* We need to jump through some hoops to handle declarations in the
17547 init-statement, since we might need to handle auto deduction,
17548 but we need to keep control of initialization. */
17550 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17551 decl
= tsubst_decl (decl
, args
, complain
);
17555 if (TREE_CODE (decl
) == SCOPE_REF
)
17557 decl
= RECUR (decl
);
17558 if (TREE_CODE (decl
) == COMPONENT_REF
)
17562 switch (TREE_CODE (v
))
17564 case COMPONENT_REF
:
17568 case POINTER_PLUS_EXPR
:
17569 v
= TREE_OPERAND (v
, 0);
17572 if (DECL_CONTEXT (v
) == current_function_decl
17573 && DECL_ARTIFICIAL (v
)
17574 && DECL_NAME (v
) == this_identifier
)
17576 decl
= TREE_OPERAND (decl
, 1);
17577 decl
= omp_privatize_field (decl
, false);
17587 decl
= RECUR (decl
);
17589 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17591 init
= copy_node (init
);
17592 TREE_VEC_ELT (init
, 0)
17593 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17594 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17595 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17598 init
= RECUR (init
);
17600 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17602 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17603 if (TREE_CODE (o
) == TREE_LIST
)
17604 TREE_VEC_ELT (orig_declv
, i
)
17605 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17606 RECUR (TREE_VALUE (o
)),
17609 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17614 tree this_pre_body
= NULL_TREE
;
17615 tree orig_init
= NULL_TREE
;
17616 tree orig_decl
= NULL_TREE
;
17617 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17618 orig_init
, cond
, incr
);
17621 if (orig_declv
== NULL_TREE
)
17622 orig_declv
= copy_node (declv
);
17623 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17626 else if (orig_declv
)
17627 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17630 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17631 if (!range_for
&& auto_node
&& init
)
17633 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17635 gcc_assert (!type_dependent_expression_p (decl
));
17637 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17641 /* Declare the variable, but don't let that initialize it. */
17642 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17643 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17645 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17650 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17651 if (COMPARISON_CLASS_P (cond
)
17652 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17654 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17655 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17656 TREE_VEC_ELT (rhs
, 0)
17657 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17658 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17659 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17660 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17664 cond
= RECUR (cond
);
17665 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17666 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17668 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17669 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17670 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17671 NOP_EXPR
, rhs
, complain
);
17674 incr
= RECUR (incr
);
17675 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17676 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17678 TREE_VEC_ELT (declv
, i
) = decl
;
17679 TREE_VEC_ELT (initv
, i
) = init
;
17680 TREE_VEC_ELT (condv
, i
) = cond
;
17681 TREE_VEC_ELT (incrv
, i
) = incr
;
17687 /* Declare and initialize the variable. */
17695 for (j
= ((omp_parallel_combined_clauses
== NULL
17696 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17698 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17700 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17701 && OMP_CLAUSE_DECL (*pc
) == decl
)
17703 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17704 && OMP_CLAUSE_DECL (*pc
) == decl
)
17708 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17710 *pc
= OMP_CLAUSE_CHAIN (c
);
17711 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17714 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17715 && OMP_CLAUSE_DECL (*pc
) == decl
)
17717 error ("iteration variable %qD should not be firstprivate",
17719 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17721 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17722 && OMP_CLAUSE_DECL (*pc
) == decl
)
17724 error ("iteration variable %qD should not be reduction",
17726 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17729 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17734 if (*pc
== NULL_TREE
)
17736 tree c
= build_omp_clause (input_location
,
17737 TREE_CODE (t
) == OMP_LOOP
17738 ? OMP_CLAUSE_LASTPRIVATE
17739 : OMP_CLAUSE_PRIVATE
);
17740 OMP_CLAUSE_DECL (c
) = decl
;
17741 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17744 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17749 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17750 if (COMPARISON_CLASS_P (cond
))
17752 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17753 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17754 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17757 cond
= RECUR (cond
);
17758 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17759 switch (TREE_CODE (incr
))
17761 case PREINCREMENT_EXPR
:
17762 case PREDECREMENT_EXPR
:
17763 case POSTINCREMENT_EXPR
:
17764 case POSTDECREMENT_EXPR
:
17765 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17766 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17769 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17770 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17772 tree rhs
= TREE_OPERAND (incr
, 1);
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 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17785 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17787 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17788 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17789 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17790 TREE_TYPE (decl
), lhs
,
17791 RECUR (TREE_OPERAND (incr
, 2))));
17793 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17794 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17795 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17797 tree rhs
= TREE_OPERAND (incr
, 2);
17798 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17799 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17800 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17801 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17802 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17806 incr
= RECUR (incr
);
17809 incr
= RECUR (incr
);
17813 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17814 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17815 TREE_VEC_ELT (declv
, i
) = decl
;
17816 TREE_VEC_ELT (initv
, i
) = init
;
17817 TREE_VEC_ELT (condv
, i
) = cond
;
17818 TREE_VEC_ELT (incrv
, i
) = incr
;
17823 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17824 of OMP_TARGET's body. */
17827 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17829 *walk_subtrees
= 0;
17830 switch (TREE_CODE (*tp
))
17835 case STATEMENT_LIST
:
17836 *walk_subtrees
= 1;
17844 /* Helper function for tsubst_expr. For decomposition declaration
17845 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17846 also the corresponding decls representing the identifiers
17847 of the decomposition declaration. Return DECL if successful
17848 or error_mark_node otherwise, set *FIRST to the first decl
17849 in the list chained through DECL_CHAIN and *CNT to the number
17853 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17854 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17857 tree decl2
, decl3
, prev
= decl
;
17859 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17860 for (decl2
= DECL_CHAIN (pattern_decl
);
17863 && DECL_DECOMPOSITION_P (decl2
)
17864 && DECL_NAME (decl2
);
17865 decl2
= DECL_CHAIN (decl2
))
17867 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17869 gcc_assert (errorcount
);
17870 return error_mark_node
;
17873 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17874 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17875 tree v
= DECL_VALUE_EXPR (decl2
);
17876 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17877 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17878 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17879 SET_DECL_VALUE_EXPR (decl2
, v
);
17880 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17882 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17885 gcc_assert (errorcount
);
17886 decl
= error_mark_node
;
17889 maybe_push_decl (decl3
);
17890 if (error_operand_p (decl3
))
17891 decl
= error_mark_node
;
17892 else if (decl
!= error_mark_node
17893 && DECL_CHAIN (decl3
) != prev
17896 gcc_assert (errorcount
);
17897 decl
= error_mark_node
;
17906 /* Return the proper local_specialization for init-capture pack DECL. */
17909 lookup_init_capture_pack (tree decl
)
17911 /* We handle normal pack captures by forwarding to the specialization of the
17912 captured parameter. We can't do that for pack init-captures; we need them
17913 to have their own local_specialization. We created the individual
17914 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17915 when we process the DECL_EXPR for the pack init-capture in the template.
17916 So, how do we find them? We don't know the capture proxy pack when
17917 building the individual resulting proxies, and we don't know the
17918 individual proxies when instantiating the pack. What we have in common is
17921 So...when we instantiate the FIELD_DECL, we stick the result in
17922 local_specializations. Then at the DECL_EXPR we look up that result, see
17923 how many elements it has, synthesize the names, and look them up. */
17925 tree cname
= DECL_NAME (decl
);
17926 tree val
= DECL_VALUE_EXPR (decl
);
17927 tree field
= TREE_OPERAND (val
, 1);
17928 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17929 tree fpack
= retrieve_local_specialization (field
);
17930 if (fpack
== error_mark_node
)
17931 return error_mark_node
;
17934 tree vec
= NULL_TREE
;
17935 tree r
= NULL_TREE
;
17936 if (TREE_CODE (fpack
) == TREE_VEC
)
17938 len
= TREE_VEC_LENGTH (fpack
);
17939 vec
= make_tree_vec (len
);
17940 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17941 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17943 for (int i
= 0; i
< len
; ++i
)
17945 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17946 tree elt
= lookup_name (ename
);
17948 TREE_VEC_ELT (vec
, i
) = elt
;
17955 /* Like tsubst_copy for expressions, etc. but also does semantic
17959 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17960 bool integral_constant_expression_p
)
17962 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17963 #define RECUR(NODE) \
17964 tsubst_expr ((NODE), args, complain, in_decl, \
17965 integral_constant_expression_p)
17971 if (t
== NULL_TREE
|| t
== error_mark_node
)
17974 loc
= input_location
;
17975 if (location_t eloc
= cp_expr_location (t
))
17976 input_location
= eloc
;
17977 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17978 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17980 switch (TREE_CODE (t
))
17982 case STATEMENT_LIST
:
17984 tree_stmt_iterator i
;
17985 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17986 RECUR (tsi_stmt (i
));
17990 case CTOR_INITIALIZER
:
17991 finish_mem_initializers (tsubst_initializer_list
17992 (TREE_OPERAND (t
, 0), args
));
17996 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17999 case CO_RETURN_EXPR
:
18000 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18003 case CO_YIELD_EXPR
:
18004 stmt
= finish_co_yield_expr (input_location
,
18005 RECUR (TREE_OPERAND (t
, 0)));
18009 case CO_AWAIT_EXPR
:
18010 stmt
= finish_co_await_expr (input_location
,
18011 RECUR (TREE_OPERAND (t
, 0)));
18016 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18017 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18018 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18020 finish_expr_stmt (tmp
);
18024 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18029 tree decl
, pattern_decl
;
18032 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18033 if (TREE_CODE (decl
) == LABEL_DECL
)
18034 finish_label_decl (DECL_NAME (decl
));
18035 else if (TREE_CODE (decl
) == USING_DECL
)
18037 tree scope
= USING_DECL_SCOPE (decl
);
18038 tree name
= DECL_NAME (decl
);
18040 scope
= tsubst (scope
, args
, complain
, in_decl
);
18041 finish_nonmember_using_decl (scope
, name
);
18043 else if (is_capture_proxy (decl
)
18044 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18046 /* We're in tsubst_lambda_expr, we've already inserted a new
18047 capture proxy, so look it up and register it. */
18049 if (!DECL_PACK_P (decl
))
18051 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18052 LOOK_want::HIDDEN_LAMBDA
);
18053 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18055 else if (is_normal_capture_proxy (decl
))
18057 inst
= (retrieve_local_specialization
18058 (DECL_CAPTURED_VARIABLE (decl
)));
18059 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18060 || DECL_PACK_P (inst
));
18063 inst
= lookup_init_capture_pack (decl
);
18065 register_local_specialization (inst
, decl
);
18068 else if (DECL_PRETTY_FUNCTION_P (decl
))
18069 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18071 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18072 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18073 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18074 /* Don't copy the old closure; we'll create a new one in
18075 tsubst_lambda_expr. */
18079 init
= DECL_INITIAL (decl
);
18080 decl
= tsubst (decl
, args
, complain
, in_decl
);
18081 if (decl
!= error_mark_node
)
18083 /* By marking the declaration as instantiated, we avoid
18084 trying to instantiate it. Since instantiate_decl can't
18085 handle local variables, and since we've already done
18086 all that needs to be done, that's the right thing to
18089 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18090 if (VAR_P (decl
) && !DECL_NAME (decl
)
18091 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18092 /* Anonymous aggregates are a special case. */
18093 finish_anon_union (decl
);
18094 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18096 DECL_CONTEXT (decl
) = current_function_decl
;
18097 if (DECL_NAME (decl
) == this_identifier
)
18099 tree lam
= DECL_CONTEXT (current_function_decl
);
18100 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18101 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18103 insert_capture_proxy (decl
);
18105 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18106 /* We already did a pushtag. */;
18107 else if (TREE_CODE (decl
) == FUNCTION_DECL
18108 && DECL_LOCAL_DECL_P (decl
)
18109 && DECL_OMP_DECLARE_REDUCTION_P (decl
))
18111 DECL_CONTEXT (decl
) = current_function_decl
;
18113 if (cp_check_omp_declare_reduction (decl
))
18114 instantiate_body (pattern_decl
, args
, decl
, true);
18118 bool const_init
= false;
18119 unsigned int cnt
= 0;
18120 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18121 maybe_push_decl (decl
);
18124 && DECL_DECOMPOSITION_P (decl
)
18125 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18126 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18127 complain
, in_decl
, &first
,
18130 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18133 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18136 if (ndecl
!= error_mark_node
)
18137 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18139 /* In a non-template function, VLA type declarations are
18140 handled in grokdeclarator; for templates, handle them
18142 predeclare_vla (decl
);
18144 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
18146 if (ndecl
!= error_mark_node
)
18147 cp_finish_decomp (ndecl
, first
, cnt
);
18156 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18157 RECUR (FOR_INIT_STMT (t
));
18158 finish_init_stmt (stmt
);
18159 tmp
= RECUR (FOR_COND (t
));
18160 finish_for_cond (tmp
, stmt
, false, 0);
18161 tmp
= RECUR (FOR_EXPR (t
));
18162 finish_for_expr (tmp
, stmt
);
18164 bool prev
= note_iteration_stmt_body_start ();
18165 RECUR (FOR_BODY (t
));
18166 note_iteration_stmt_body_end (prev
);
18168 finish_for_stmt (stmt
);
18171 case RANGE_FOR_STMT
:
18173 /* Construct another range_for, if this is not a final
18174 substitution (for inside a generic lambda of a
18175 template). Otherwise convert to a regular for. */
18177 stmt
= (processing_template_decl
18178 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18179 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18180 RECUR (RANGE_FOR_INIT_STMT (t
));
18181 decl
= RANGE_FOR_DECL (t
);
18182 decl
= tsubst (decl
, args
, complain
, in_decl
);
18183 maybe_push_decl (decl
);
18184 expr
= RECUR (RANGE_FOR_EXPR (t
));
18186 tree decomp_first
= NULL_TREE
;
18187 unsigned decomp_cnt
= 0;
18188 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18189 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18191 &decomp_first
, &decomp_cnt
);
18193 if (processing_template_decl
)
18195 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18196 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18197 finish_range_for_decl (stmt
, decl
, expr
);
18198 if (decomp_first
&& decl
!= error_mark_node
)
18199 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18203 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18204 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18205 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18206 decomp_first
, decomp_cnt
,
18207 RANGE_FOR_IVDEP (t
), unroll
);
18210 bool prev
= note_iteration_stmt_body_start ();
18211 RECUR (RANGE_FOR_BODY (t
));
18212 note_iteration_stmt_body_end (prev
);
18213 finish_for_stmt (stmt
);
18218 stmt
= begin_while_stmt ();
18219 tmp
= RECUR (WHILE_COND (t
));
18220 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18222 bool prev
= note_iteration_stmt_body_start ();
18223 RECUR (WHILE_BODY (t
));
18224 note_iteration_stmt_body_end (prev
);
18226 finish_while_stmt (stmt
);
18230 stmt
= begin_do_stmt ();
18232 bool prev
= note_iteration_stmt_body_start ();
18233 RECUR (DO_BODY (t
));
18234 note_iteration_stmt_body_end (prev
);
18236 finish_do_body (stmt
);
18237 tmp
= RECUR (DO_COND (t
));
18238 finish_do_stmt (tmp
, stmt
, false, 0);
18242 stmt
= begin_if_stmt ();
18243 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18244 if (IF_STMT_CONSTEXPR_P (t
))
18245 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18246 tmp
= RECUR (IF_COND (t
));
18247 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18248 if (IF_STMT_CONSTEXPR_P (t
)
18249 && instantiation_dependent_expression_p (tmp
))
18251 /* We're partially instantiating a generic lambda, but the condition
18252 of the constexpr if is still dependent. Don't substitute into the
18253 branches now, just remember the template arguments. */
18254 do_poplevel (IF_SCOPE (stmt
));
18255 IF_COND (stmt
) = IF_COND (t
);
18256 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18257 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18258 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18262 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18263 /* Don't instantiate the THEN_CLAUSE. */;
18266 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18267 bool inhibit
= integer_zerop (folded
);
18269 ++c_inhibit_evaluation_warnings
;
18270 RECUR (THEN_CLAUSE (t
));
18272 --c_inhibit_evaluation_warnings
;
18274 finish_then_clause (stmt
);
18276 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18277 /* Don't instantiate the ELSE_CLAUSE. */;
18278 else if (ELSE_CLAUSE (t
))
18280 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18281 bool inhibit
= integer_nonzerop (folded
);
18282 begin_else_clause (stmt
);
18284 ++c_inhibit_evaluation_warnings
;
18285 RECUR (ELSE_CLAUSE (t
));
18287 --c_inhibit_evaluation_warnings
;
18288 finish_else_clause (stmt
);
18291 finish_if_stmt (stmt
);
18295 if (BIND_EXPR_BODY_BLOCK (t
))
18296 stmt
= begin_function_body ();
18298 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18299 ? BCS_TRY_BLOCK
: 0);
18301 RECUR (BIND_EXPR_BODY (t
));
18303 if (BIND_EXPR_BODY_BLOCK (t
))
18304 finish_function_body (stmt
);
18306 finish_compound_stmt (stmt
);
18310 finish_break_stmt ();
18313 case CONTINUE_STMT
:
18314 finish_continue_stmt ();
18318 stmt
= begin_switch_stmt ();
18319 tmp
= RECUR (SWITCH_STMT_COND (t
));
18320 finish_switch_cond (tmp
, stmt
);
18321 RECUR (SWITCH_STMT_BODY (t
));
18322 finish_switch_stmt (stmt
);
18325 case CASE_LABEL_EXPR
:
18327 tree decl
= CASE_LABEL (t
);
18328 tree low
= RECUR (CASE_LOW (t
));
18329 tree high
= RECUR (CASE_HIGH (t
));
18330 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18331 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18333 tree label
= CASE_LABEL (l
);
18334 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18335 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18336 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18343 tree decl
= LABEL_EXPR_LABEL (t
);
18346 label
= finish_label_stmt (DECL_NAME (decl
));
18347 if (TREE_CODE (label
) == LABEL_DECL
)
18348 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18349 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18350 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18355 tmp
= GOTO_DESTINATION (t
);
18356 if (TREE_CODE (tmp
) != LABEL_DECL
)
18357 /* Computed goto's must be tsubst'd into. On the other hand,
18358 non-computed gotos must not be; the identifier in question
18359 will have no binding. */
18362 tmp
= DECL_NAME (tmp
);
18363 finish_goto_stmt (tmp
);
18368 tree string
= RECUR (ASM_STRING (t
));
18369 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18370 complain
, in_decl
);
18371 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18372 complain
, in_decl
);
18373 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18374 complain
, in_decl
);
18375 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18376 complain
, in_decl
);
18377 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18378 outputs
, inputs
, clobbers
, labels
,
18380 tree asm_expr
= tmp
;
18381 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18382 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18383 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18390 stmt
= begin_try_block ();
18391 RECUR (TRY_STMTS (t
));
18392 finish_cleanup_try_block (stmt
);
18393 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18397 tree compound_stmt
= NULL_TREE
;
18399 if (FN_TRY_BLOCK_P (t
))
18400 stmt
= begin_function_try_block (&compound_stmt
);
18402 stmt
= begin_try_block ();
18404 RECUR (TRY_STMTS (t
));
18406 if (FN_TRY_BLOCK_P (t
))
18407 finish_function_try_block (stmt
);
18409 finish_try_block (stmt
);
18411 RECUR (TRY_HANDLERS (t
));
18412 if (FN_TRY_BLOCK_P (t
))
18413 finish_function_handler_sequence (stmt
, compound_stmt
);
18415 finish_handler_sequence (stmt
);
18421 tree decl
= HANDLER_PARMS (t
);
18425 decl
= tsubst (decl
, args
, complain
, in_decl
);
18426 /* Prevent instantiate_decl from trying to instantiate
18427 this variable. We've already done all that needs to be
18429 if (decl
!= error_mark_node
)
18430 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18432 stmt
= begin_handler ();
18433 finish_handler_parms (decl
, stmt
);
18434 RECUR (HANDLER_BODY (t
));
18435 finish_handler (stmt
);
18440 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18441 if (CLASS_TYPE_P (tmp
))
18443 /* Local classes are not independent templates; they are
18444 instantiated along with their containing function. And this
18445 way we don't have to deal with pushing out of one local class
18446 to instantiate a member of another local class. */
18447 /* Closures are handled by the LAMBDA_EXPR. */
18448 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18449 complete_type (tmp
);
18450 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18452 || (TREE_CODE (fld
) == FUNCTION_DECL
18453 && !DECL_ARTIFICIAL (fld
)))
18454 && DECL_TEMPLATE_INSTANTIATION (fld
))
18455 instantiate_decl (fld
, /*defer_ok=*/false,
18456 /*expl_inst_class=*/false);
18460 case STATIC_ASSERT
:
18464 ++c_inhibit_evaluation_warnings
;
18466 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18469 /*integral_constant_expression_p=*/true);
18470 --c_inhibit_evaluation_warnings
;
18472 finish_static_assert (condition
,
18473 STATIC_ASSERT_MESSAGE (t
),
18474 STATIC_ASSERT_SOURCE_LOCATION (t
),
18475 /*member_p=*/false);
18480 case OACC_PARALLEL
:
18482 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18484 stmt
= begin_omp_parallel ();
18485 RECUR (OMP_BODY (t
));
18486 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18490 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18491 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18492 complain
, in_decl
);
18493 if (OMP_PARALLEL_COMBINED (t
))
18494 omp_parallel_combined_clauses
= &tmp
;
18495 stmt
= begin_omp_parallel ();
18496 RECUR (OMP_PARALLEL_BODY (t
));
18497 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18498 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18499 = OMP_PARALLEL_COMBINED (t
);
18500 pop_omp_privatization_clauses (r
);
18504 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18506 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18507 complain
, in_decl
);
18509 OMP_TASK_CLAUSES (t
) = tmp
;
18513 r
= push_omp_privatization_clauses (false);
18514 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18515 complain
, in_decl
);
18516 stmt
= begin_omp_task ();
18517 RECUR (OMP_TASK_BODY (t
));
18518 finish_omp_task (tmp
, stmt
);
18519 pop_omp_privatization_clauses (r
);
18525 case OMP_DISTRIBUTE
:
18529 tree clauses
, body
, pre_body
;
18530 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18531 tree orig_declv
= NULL_TREE
;
18532 tree incrv
= NULL_TREE
;
18533 enum c_omp_region_type ort
= C_ORT_OMP
;
18534 bool any_range_for
= false;
18537 if (TREE_CODE (t
) == OACC_LOOP
)
18540 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18541 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18543 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18545 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18546 if (OMP_FOR_ORIG_DECLS (t
))
18547 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18548 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18549 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18550 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18553 keep_next_level (true);
18554 stmt
= begin_omp_structured_block ();
18556 pre_body
= push_stmt_list ();
18557 RECUR (OMP_FOR_PRE_BODY (t
));
18558 pre_body
= pop_stmt_list (pre_body
);
18560 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18561 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18563 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18564 condv
, incrv
, &clauses
, args
,
18566 integral_constant_expression_p
);
18567 omp_parallel_combined_clauses
= NULL
;
18571 gcc_assert (orig_declv
);
18572 body
= begin_omp_structured_block ();
18573 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18574 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18575 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18576 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18577 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18578 TREE_VEC_ELT (declv
, i
));
18581 body
= push_stmt_list ();
18582 RECUR (OMP_FOR_BODY (t
));
18584 body
= finish_omp_structured_block (body
);
18586 body
= pop_stmt_list (body
);
18588 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18589 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18590 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18594 t
= make_node (TREE_CODE (t
));
18595 TREE_TYPE (t
) = void_type_node
;
18596 OMP_FOR_BODY (t
) = body
;
18597 OMP_FOR_PRE_BODY (t
) = pre_body
;
18598 OMP_FOR_CLAUSES (t
) = clauses
;
18599 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18603 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18605 pop_omp_privatization_clauses (r
);
18610 omp_parallel_combined_clauses
= NULL
;
18615 case OMP_TASKGROUP
:
18617 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18618 && OMP_TEAMS_COMBINED (t
));
18619 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18621 if (TREE_CODE (t
) == OMP_TEAMS
)
18623 keep_next_level (true);
18624 stmt
= begin_omp_structured_block ();
18625 RECUR (OMP_BODY (t
));
18626 stmt
= finish_omp_structured_block (stmt
);
18630 stmt
= push_stmt_list ();
18631 RECUR (OMP_BODY (t
));
18632 stmt
= pop_stmt_list (stmt
);
18635 if (TREE_CODE (t
) == OMP_CRITICAL
18636 && tmp
!= NULL_TREE
18637 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18639 error_at (OMP_CLAUSE_LOCATION (tmp
),
18640 "%<#pragma omp critical%> with %<hint%> clause requires "
18641 "a name, except when %<omp_sync_hint_none%> is used");
18642 RETURN (error_mark_node
);
18645 OMP_BODY (t
) = stmt
;
18646 OMP_CLAUSES (t
) = tmp
;
18648 pop_omp_privatization_clauses (r
);
18652 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18653 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18655 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18656 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18658 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18659 args
, complain
, in_decl
);
18660 if (tmp
== NULL_TREE
)
18661 tmp
= error_mark_node
;
18665 kind
= (enum omp_clause_depend_kind
)
18666 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18669 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18672 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18673 OMP_CLAUSE_DEPEND_SOURCE
,
18674 OMP_DEPOBJ_CLAUSES (t
));
18678 case OMP_TARGET_DATA
:
18680 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18681 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18683 keep_next_level (true);
18684 stmt
= begin_omp_structured_block ();
18686 RECUR (OMP_BODY (t
));
18687 stmt
= finish_omp_structured_block (stmt
);
18690 OMP_BODY (t
) = stmt
;
18691 OMP_CLAUSES (t
) = tmp
;
18692 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18694 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18697 /* For combined target teams, ensure the num_teams and
18698 thread_limit clause expressions are evaluated on the host,
18699 before entering the target construct. */
18701 for (c
= OMP_TEAMS_CLAUSES (teams
);
18702 c
; c
= OMP_CLAUSE_CHAIN (c
))
18703 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18704 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18705 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18707 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18708 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18709 if (expr
== error_mark_node
)
18711 tmp
= TARGET_EXPR_SLOT (expr
);
18713 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18714 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18715 OMP_CLAUSE_FIRSTPRIVATE
);
18716 OMP_CLAUSE_DECL (tc
) = tmp
;
18717 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18718 OMP_TARGET_CLAUSES (t
) = tc
;
18727 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18728 complain
, in_decl
);
18729 OACC_DECLARE_CLAUSES (t
) = tmp
;
18733 case OMP_TARGET_UPDATE
:
18734 case OMP_TARGET_ENTER_DATA
:
18735 case OMP_TARGET_EXIT_DATA
:
18736 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18737 complain
, in_decl
);
18739 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18743 case OACC_ENTER_DATA
:
18744 case OACC_EXIT_DATA
:
18746 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18747 complain
, in_decl
);
18749 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18754 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18755 complain
, in_decl
);
18756 stmt
= push_stmt_list ();
18757 RECUR (OMP_BODY (t
));
18758 stmt
= pop_stmt_list (stmt
);
18761 OMP_BODY (t
) = stmt
;
18762 OMP_ORDERED_CLAUSES (t
) = tmp
;
18767 omp_parallel_combined_clauses
= NULL
;
18770 stmt
= push_stmt_list ();
18771 RECUR (OMP_BODY (t
));
18772 stmt
= pop_stmt_list (stmt
);
18775 OMP_BODY (t
) = stmt
;
18780 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18782 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18783 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18784 complain
, in_decl
);
18785 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18787 tree op1
= TREE_OPERAND (t
, 1);
18788 tree rhs1
= NULL_TREE
;
18790 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18792 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18793 op1
= TREE_OPERAND (op1
, 1);
18795 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18796 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18797 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18798 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18799 OMP_ATOMIC_MEMORY_ORDER (t
));
18803 tree op1
= TREE_OPERAND (t
, 1);
18804 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18805 tree rhs1
= NULL_TREE
;
18806 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18807 enum tree_code opcode
= NOP_EXPR
;
18808 if (code
== OMP_ATOMIC_READ
)
18810 v
= RECUR (TREE_OPERAND (op1
, 0));
18811 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18813 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18814 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18816 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18817 v
= RECUR (TREE_OPERAND (op1
, 0));
18818 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18819 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18821 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18822 op11
= TREE_OPERAND (op11
, 1);
18824 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18825 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18826 opcode
= TREE_CODE (op11
);
18827 if (opcode
== MODIFY_EXPR
)
18833 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18834 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18836 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18837 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18841 case TRANSACTION_EXPR
:
18844 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18845 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18847 if (TRANSACTION_EXPR_IS_STMT (t
))
18849 tree body
= TRANSACTION_EXPR_BODY (t
);
18850 tree noex
= NULL_TREE
;
18851 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18853 noex
= MUST_NOT_THROW_COND (body
);
18854 if (noex
== NULL_TREE
)
18855 noex
= boolean_true_node
;
18856 body
= TREE_OPERAND (body
, 0);
18858 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18860 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18864 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18865 RECUR (TRANSACTION_EXPR_BODY (t
)),
18872 case MUST_NOT_THROW_EXPR
:
18874 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18875 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18876 RETURN (build_must_not_throw_expr (op0
, cond
));
18879 case EXPR_PACK_EXPANSION
:
18880 error ("invalid use of pack expansion expression");
18881 RETURN (error_mark_node
);
18883 case NONTYPE_ARGUMENT_PACK
:
18884 error ("use %<...%> to expand argument pack");
18885 RETURN (error_mark_node
);
18887 case COMPOUND_EXPR
:
18888 tmp
= RECUR (TREE_OPERAND (t
, 0));
18889 if (tmp
== NULL_TREE
)
18890 /* If the first operand was a statement, we're done with it. */
18891 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18892 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18893 RECUR (TREE_OPERAND (t
, 1)),
18896 case ANNOTATE_EXPR
:
18897 tmp
= RECUR (TREE_OPERAND (t
, 0));
18898 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18899 TREE_TYPE (tmp
), tmp
,
18900 RECUR (TREE_OPERAND (t
, 1)),
18901 RECUR (TREE_OPERAND (t
, 2))));
18904 RETURN (add_stmt (copy_node (t
)));
18907 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18909 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18910 /*function_p=*/false,
18911 integral_constant_expression_p
));
18914 RETURN (NULL_TREE
);
18916 input_location
= loc
;
18922 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18923 function. For description of the body see comment above
18924 cp_parser_omp_declare_reduction_exprs. */
18927 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18929 if (t
== NULL_TREE
|| t
== error_mark_node
)
18932 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
18934 tree_stmt_iterator tsi
;
18937 memset (stmts
, 0, sizeof stmts
);
18938 for (i
= 0, tsi
= tsi_start (t
);
18939 i
< 7 && !tsi_end_p (tsi
);
18940 i
++, tsi_next (&tsi
))
18941 stmts
[i
] = tsi_stmt (tsi
);
18942 gcc_assert (tsi_end_p (tsi
));
18946 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18947 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18948 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18949 args
, complain
, in_decl
);
18950 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18951 args
, complain
, in_decl
);
18952 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18953 expect to be pushing it. */
18954 DECL_CONTEXT (omp_out
) = current_function_decl
;
18955 DECL_CONTEXT (omp_in
) = current_function_decl
;
18956 keep_next_level (true);
18957 tree block
= begin_omp_structured_block ();
18958 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18959 block
= finish_omp_structured_block (block
);
18960 block
= maybe_cleanup_point_expr_void (block
);
18961 add_decl_expr (omp_out
);
18962 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18963 TREE_NO_WARNING (omp_out
) = 1;
18964 add_decl_expr (omp_in
);
18965 finish_expr_stmt (block
);
18969 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18970 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18971 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18972 args
, complain
, in_decl
);
18973 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18974 args
, complain
, in_decl
);
18975 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18976 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18977 keep_next_level (true);
18978 tree block
= begin_omp_structured_block ();
18979 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18980 block
= finish_omp_structured_block (block
);
18981 block
= maybe_cleanup_point_expr_void (block
);
18982 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18983 add_decl_expr (omp_priv
);
18984 add_decl_expr (omp_orig
);
18985 finish_expr_stmt (block
);
18987 add_decl_expr (omp_orig
);
18991 /* T is a postfix-expression that is not being used in a function
18992 call. Return the substituted version of T. */
18995 tsubst_non_call_postfix_expression (tree t
, tree args
,
18996 tsubst_flags_t complain
,
18999 if (TREE_CODE (t
) == SCOPE_REF
)
19000 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19001 /*done=*/false, /*address_p=*/false);
19003 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19004 /*function_p=*/false,
19005 /*integral_constant_expression_p=*/false);
19010 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19011 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19012 dependent init-capture. */
19015 prepend_one_capture (tree field
, tree init
, tree
&list
,
19016 tsubst_flags_t complain
)
19018 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19020 tree type
= NULL_TREE
;
19023 if (complain
& tf_error
)
19024 error ("empty initializer in lambda init-capture");
19025 init
= error_mark_node
;
19027 else if (TREE_CODE (init
) == TREE_LIST
)
19028 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19030 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19031 TREE_TYPE (field
) = type
;
19032 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19034 list
= tree_cons (field
, init
, list
);
19037 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19038 instantiation context. Instantiating a pack expansion containing a lambda
19039 might result in multiple lambdas all based on the same lambda in the
19043 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19045 tree oldfn
= lambda_function (t
);
19048 tree r
= build_lambda_expr ();
19050 LAMBDA_EXPR_LOCATION (r
)
19051 = LAMBDA_EXPR_LOCATION (t
);
19052 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19053 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19054 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19055 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19057 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19058 /* A lambda in a default argument outside a class gets no
19059 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19060 tsubst_default_argument calls start_lambda_scope, so we need to
19061 specifically ignore it here, and use the global scope. */
19062 record_null_lambda_scope (r
);
19064 record_lambda_scope (r
);
19066 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19067 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19069 vec
<tree
,va_gc
>* field_packs
= NULL
;
19071 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19072 cap
= TREE_CHAIN (cap
))
19074 tree ofield
= TREE_PURPOSE (cap
);
19075 tree init
= TREE_VALUE (cap
);
19076 if (PACK_EXPANSION_P (init
))
19077 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19079 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19080 /*fn*/false, /*constexpr*/false);
19082 if (init
== error_mark_node
)
19083 return error_mark_node
;
19085 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19086 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19088 if (!processing_template_decl
19089 && init
&& TREE_CODE (init
) != TREE_VEC
19090 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19092 /* For a VLA, simply tsubsting the field type won't work, we need to
19093 go through add_capture again. XXX do we want to do this for all
19095 tree name
= (get_identifier
19096 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19097 tree ftype
= TREE_TYPE (ofield
);
19098 bool by_ref
= (TYPE_REF_P (ftype
)
19099 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19100 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19101 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19105 if (PACK_EXPANSION_P (ofield
))
19106 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19107 tree field
= tsubst_decl (ofield
, args
, complain
);
19109 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19111 /* Remember these for when we've pushed local_specializations. */
19112 vec_safe_push (field_packs
, ofield
);
19113 vec_safe_push (field_packs
, field
);
19116 if (field
== error_mark_node
)
19117 return error_mark_node
;
19119 if (TREE_CODE (field
) == TREE_VEC
)
19121 int len
= TREE_VEC_LENGTH (field
);
19122 gcc_assert (TREE_CODE (init
) == TREE_VEC
19123 && TREE_VEC_LENGTH (init
) == len
);
19124 for (int i
= 0; i
< len
; ++i
)
19125 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19126 TREE_VEC_ELT (init
, i
),
19127 LAMBDA_EXPR_CAPTURE_LIST (r
),
19132 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19135 if (id_equal (DECL_NAME (field
), "__this"))
19136 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19140 tree type
= begin_lambda_type (r
);
19141 if (type
== error_mark_node
)
19142 return error_mark_node
;
19144 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19145 determine_visibility (TYPE_NAME (type
));
19147 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19149 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19150 ? DECL_TI_TEMPLATE (oldfn
)
19153 tree fntype
= static_fn_type (oldfn
);
19155 ++processing_template_decl
;
19156 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19158 --processing_template_decl
;
19160 if (fntype
== error_mark_node
)
19161 r
= error_mark_node
;
19164 /* The body of a lambda-expression is not a subexpression of the
19165 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19166 which would be skipped if cp_unevaluated_operand. */
19169 /* Fix the type of 'this'. */
19170 fntype
= build_memfn_type (fntype
, type
,
19171 type_memfn_quals (fntype
),
19172 type_memfn_rqual (fntype
));
19176 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19177 if (tmpl
== error_mark_node
)
19179 r
= error_mark_node
;
19182 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19183 finish_member_declaration (tmpl
);
19188 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19189 if (fn
== error_mark_node
)
19191 r
= error_mark_node
;
19194 finish_member_declaration (fn
);
19197 if (tree ci
= get_constraints (oldfn
))
19199 /* Substitute into the lambda's constraints. */
19201 ++processing_template_decl
;
19202 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19204 --processing_template_decl
;
19205 set_constraints (fn
, ci
);
19208 /* Let finish_function set this. */
19209 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19211 bool nested
= cfun
;
19213 push_function_context ();
19215 /* Still increment function_depth so that we don't GC in the
19216 middle of an expression. */
19219 local_specialization_stack
s (lss_copy
);
19221 tree body
= start_lambda_function (fn
, r
);
19223 /* Now record them for lookup_init_capture_pack. */
19224 int fplen
= vec_safe_length (field_packs
);
19225 for (int i
= 0; i
< fplen
; )
19227 tree pack
= (*field_packs
)[i
++];
19228 tree inst
= (*field_packs
)[i
++];
19229 register_local_specialization (inst
, pack
);
19231 release_tree_vector (field_packs
);
19233 register_parameter_specializations (oldfn
, fn
);
19237 /* We might not partially instantiate some parts of the function, so
19238 copy these flags from the original template. */
19239 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19240 current_function_returns_value
= ol
->returns_value
;
19241 current_function_returns_null
= ol
->returns_null
;
19242 current_function_returns_abnormally
= ol
->returns_abnormally
;
19243 current_function_infinite_loop
= ol
->infinite_loop
;
19246 /* [temp.deduct] A lambda-expression appearing in a function type or a
19247 template parameter is not considered part of the immediate context for
19248 the purposes of template argument deduction. */
19249 complain
= tf_warning_or_error
;
19251 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19252 /*constexpr*/false);
19254 finish_lambda_function (body
);
19257 pop_function_context ();
19261 /* The capture list was built up in reverse order; fix that now. */
19262 LAMBDA_EXPR_CAPTURE_LIST (r
)
19263 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19265 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19267 maybe_add_lambda_conv_op (type
);
19271 finish_struct (type
, /*attr*/NULL_TREE
);
19273 insert_pending_capture_proxies ();
19278 /* Like tsubst but deals with expressions and performs semantic
19279 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19282 tsubst_copy_and_build (tree t
,
19284 tsubst_flags_t complain
,
19287 bool integral_constant_expression_p
)
19289 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19290 #define RECUR(NODE) \
19291 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19292 /*function_p=*/false, \
19293 integral_constant_expression_p)
19296 location_t save_loc
;
19298 if (t
== NULL_TREE
|| t
== error_mark_node
)
19301 save_loc
= input_location
;
19302 if (location_t eloc
= cp_expr_location (t
))
19303 input_location
= eloc
;
19305 /* N3276 decltype magic only applies to calls at the top level or on the
19306 right side of a comma. */
19307 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19308 complain
&= ~tf_decltype
;
19310 switch (TREE_CODE (t
))
19314 /* Fall through. */
19315 case IDENTIFIER_NODE
:
19319 bool non_integral_constant_expression_p
;
19320 const char *error_msg
;
19322 if (IDENTIFIER_CONV_OP_P (t
))
19324 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19325 t
= make_conv_op_name (new_type
);
19328 /* Look up the name. */
19329 decl
= lookup_name (t
);
19331 /* By convention, expressions use ERROR_MARK_NODE to indicate
19332 failure, not NULL_TREE. */
19333 if (decl
== NULL_TREE
)
19334 decl
= error_mark_node
;
19336 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19338 integral_constant_expression_p
,
19339 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19340 &non_integral_constant_expression_p
,
19341 /*template_p=*/false,
19343 /*address_p=*/false,
19344 /*template_arg_p=*/false,
19349 if (!function_p
&& identifier_p (decl
))
19351 if (complain
& tf_error
)
19352 unqualified_name_lookup_error (decl
);
19353 decl
= error_mark_node
;
19358 case TEMPLATE_ID_EXPR
:
19361 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19362 tree targs
= TREE_OPERAND (t
, 1);
19365 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19366 if (targs
== error_mark_node
)
19367 RETURN (error_mark_node
);
19369 if (TREE_CODE (templ
) == SCOPE_REF
)
19371 tree name
= TREE_OPERAND (templ
, 1);
19372 tree tid
= lookup_template_function (name
, targs
);
19373 TREE_OPERAND (templ
, 1) = tid
;
19377 if (concept_definition_p (templ
))
19379 tree check
= build_concept_check (templ
, targs
, complain
);
19380 if (check
== error_mark_node
)
19381 RETURN (error_mark_node
);
19383 tree id
= unpack_concept_check (check
);
19385 /* If we built a function concept check, return the underlying
19386 template-id. So we can evaluate it as a function call. */
19387 if (function_concept_p (TREE_OPERAND (id
, 0)))
19393 if (variable_template_p (templ
))
19395 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19397 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19401 if (TREE_CODE (templ
) == COMPONENT_REF
)
19403 object
= TREE_OPERAND (templ
, 0);
19404 templ
= TREE_OPERAND (templ
, 1);
19407 object
= NULL_TREE
;
19408 templ
= lookup_template_function (templ
, targs
);
19411 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19412 object
, templ
, NULL_TREE
));
19414 RETURN (baselink_for_fns (templ
));
19419 tree r
= RECUR (TREE_OPERAND (t
, 0));
19421 if (REFERENCE_REF_P (t
))
19423 /* A type conversion to reference type will be enclosed in
19424 such an indirect ref, but the substitution of the cast
19425 will have also added such an indirect ref. */
19426 r
= convert_from_reference (r
);
19429 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19430 complain
|decltype_flag
);
19432 if (REF_PARENTHESIZED_P (t
))
19433 r
= force_paren_expr (r
);
19440 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19441 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19442 RETURN (build_nop (type
, op0
));
19445 case IMPLICIT_CONV_EXPR
:
19447 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19448 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19449 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19451 retval
= copy_node (t
);
19452 TREE_TYPE (retval
) = type
;
19453 TREE_OPERAND (retval
, 0) = expr
;
19456 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19457 /* We'll pass this to convert_nontype_argument again, we don't need
19458 to actually perform any conversion here. */
19460 int flags
= LOOKUP_IMPLICIT
;
19461 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19462 flags
= LOOKUP_NORMAL
;
19463 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19464 flags
|= LOOKUP_NO_NARROWING
;
19465 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19471 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19472 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19473 if (op0
== error_mark_node
)
19474 RETURN (error_mark_node
);
19475 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19479 case REINTERPRET_CAST_EXPR
:
19480 case CONST_CAST_EXPR
:
19481 case DYNAMIC_CAST_EXPR
:
19482 case STATIC_CAST_EXPR
:
19485 tree op
, r
= NULL_TREE
;
19487 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19488 if (integral_constant_expression_p
19489 && !cast_valid_in_integral_constant_expression_p (type
))
19491 if (complain
& tf_error
)
19492 error ("a cast to a type other than an integral or "
19493 "enumeration type cannot appear in a constant-expression");
19494 RETURN (error_mark_node
);
19497 op
= RECUR (TREE_OPERAND (t
, 0));
19499 warning_sentinel
s(warn_useless_cast
);
19500 warning_sentinel
s2(warn_ignored_qualifiers
);
19501 switch (TREE_CODE (t
))
19504 r
= build_functional_cast (input_location
, type
, op
, complain
);
19506 case REINTERPRET_CAST_EXPR
:
19507 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19509 case CONST_CAST_EXPR
:
19510 r
= build_const_cast (input_location
, type
, op
, complain
);
19512 case DYNAMIC_CAST_EXPR
:
19513 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19515 case STATIC_CAST_EXPR
:
19516 r
= build_static_cast (input_location
, type
, op
, complain
);
19517 if (IMPLICIT_RVALUE_P (t
))
19518 set_implicit_rvalue_p (r
);
19521 gcc_unreachable ();
19527 case POSTDECREMENT_EXPR
:
19528 case POSTINCREMENT_EXPR
:
19529 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19530 args
, complain
, in_decl
);
19531 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19532 complain
|decltype_flag
));
19534 case PREDECREMENT_EXPR
:
19535 case PREINCREMENT_EXPR
:
19539 case TRUTH_NOT_EXPR
:
19540 case UNARY_PLUS_EXPR
: /* Unary + */
19541 case REALPART_EXPR
:
19542 case IMAGPART_EXPR
:
19543 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19544 RECUR (TREE_OPERAND (t
, 0)),
19545 complain
|decltype_flag
));
19547 case FIX_TRUNC_EXPR
:
19548 gcc_unreachable ();
19551 op1
= TREE_OPERAND (t
, 0);
19552 if (TREE_CODE (op1
) == LABEL_DECL
)
19553 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19554 EXPR_LOCATION (op1
)));
19555 if (TREE_CODE (op1
) == SCOPE_REF
)
19556 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19557 /*done=*/true, /*address_p=*/true);
19559 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19561 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19562 complain
|decltype_flag
));
19567 case TRUNC_DIV_EXPR
:
19568 case CEIL_DIV_EXPR
:
19569 case FLOOR_DIV_EXPR
:
19570 case ROUND_DIV_EXPR
:
19571 case EXACT_DIV_EXPR
:
19575 case TRUNC_MOD_EXPR
:
19576 case FLOOR_MOD_EXPR
:
19577 case TRUTH_ANDIF_EXPR
:
19578 case TRUTH_ORIF_EXPR
:
19579 case TRUTH_AND_EXPR
:
19580 case TRUTH_OR_EXPR
:
19591 case SPACESHIP_EXPR
:
19595 /* If T was type-dependent, suppress warnings that depend on the range
19596 of the types involved. */
19597 bool was_dep
= type_dependent_expression_p_push (t
);
19599 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19600 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19602 warning_sentinel
s1(warn_type_limits
, was_dep
);
19603 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19604 warning_sentinel
s3(warn_logical_op
, was_dep
);
19605 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19607 tree r
= build_x_binary_op
19608 (input_location
, TREE_CODE (t
),
19610 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19612 : TREE_CODE (TREE_OPERAND (t
, 0))),
19614 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19616 : TREE_CODE (TREE_OPERAND (t
, 1))),
19618 complain
|decltype_flag
);
19619 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19620 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19625 case POINTER_PLUS_EXPR
:
19627 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19628 if (op0
== error_mark_node
)
19629 RETURN (error_mark_node
);
19630 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19631 if (op1
== error_mark_node
)
19632 RETURN (error_mark_node
);
19633 RETURN (fold_build_pointer_plus (op0
, op1
));
19637 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19638 /*address_p=*/false));
19640 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19641 args
, complain
, in_decl
);
19642 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19643 RECUR (TREE_OPERAND (t
, 1)),
19644 complain
|decltype_flag
));
19647 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19648 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19649 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19656 op1
= TREE_OPERAND (t
, 0);
19657 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19658 op1
= TREE_TYPE (op1
);
19659 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19660 && ALIGNOF_EXPR_STD_P (t
));
19663 /* When there are no ARGS, we are trying to evaluate a
19664 non-dependent expression from the parser. Trying to do
19665 the substitutions may not work. */
19667 op1
= TREE_TYPE (op1
);
19671 ++cp_unevaluated_operand
;
19672 ++c_inhibit_evaluation_warnings
;
19674 op1
= tsubst (op1
, args
, complain
, in_decl
);
19676 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19677 /*function_p=*/false,
19678 /*integral_constant_expression_p=*/
19680 --cp_unevaluated_operand
;
19681 --c_inhibit_evaluation_warnings
;
19684 r
= cxx_sizeof_or_alignof_type (input_location
,
19685 op1
, TREE_CODE (t
), std_alignof
,
19686 complain
& tf_error
);
19688 r
= cxx_sizeof_or_alignof_expr (input_location
,
19689 op1
, TREE_CODE (t
),
19690 complain
& tf_error
);
19691 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19693 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19695 if (!processing_template_decl
&& TYPE_P (op1
))
19697 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19698 build1 (NOP_EXPR
, op1
, error_mark_node
));
19699 SIZEOF_EXPR_TYPE_P (r
) = 1;
19702 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19703 TREE_SIDE_EFFECTS (r
) = 0;
19704 TREE_READONLY (r
) = 1;
19706 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19711 case AT_ENCODE_EXPR
:
19713 op1
= TREE_OPERAND (t
, 0);
19714 ++cp_unevaluated_operand
;
19715 ++c_inhibit_evaluation_warnings
;
19716 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19717 /*function_p=*/false,
19718 /*integral_constant_expression_p=*/false);
19719 --cp_unevaluated_operand
;
19720 --c_inhibit_evaluation_warnings
;
19721 RETURN (objc_build_encode_expr (op1
));
19724 case NOEXCEPT_EXPR
:
19725 op1
= TREE_OPERAND (t
, 0);
19726 ++cp_unevaluated_operand
;
19727 ++c_inhibit_evaluation_warnings
;
19728 ++cp_noexcept_operand
;
19729 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19730 /*function_p=*/false,
19731 /*integral_constant_expression_p=*/false);
19732 --cp_unevaluated_operand
;
19733 --c_inhibit_evaluation_warnings
;
19734 --cp_noexcept_operand
;
19735 RETURN (finish_noexcept_expr (op1
, complain
));
19739 warning_sentinel
s(warn_div_by_zero
);
19740 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19741 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19742 tree r
= build_x_modify_expr
19743 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19744 complain
|decltype_flag
);
19745 /* TREE_NO_WARNING must be set if either the expression was
19746 parenthesized or it uses an operator such as >>= rather
19747 than plain assignment. In the former case, it was already
19748 set and must be copied. In the latter case,
19749 build_x_modify_expr sets it and it must not be reset
19751 if (TREE_NO_WARNING (t
))
19752 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19758 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19759 args
, complain
, in_decl
);
19760 /* Remember that there was a reference to this entity. */
19762 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19763 RETURN (error_mark_node
);
19764 RETURN (build_x_arrow (input_location
, op1
, complain
));
19768 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19769 tree init
= RECUR (TREE_OPERAND (t
, 3));
19770 vec
<tree
, va_gc
> *placement_vec
;
19771 vec
<tree
, va_gc
> *init_vec
;
19773 location_t loc
= EXPR_LOCATION (t
);
19775 if (placement
== NULL_TREE
)
19776 placement_vec
= NULL
;
19777 else if (placement
== error_mark_node
)
19778 RETURN (error_mark_node
);
19781 placement_vec
= make_tree_vector ();
19782 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19783 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19786 /* If there was an initializer in the original tree, but it
19787 instantiated to an empty list, then we should pass a
19788 non-NULL empty vector to tell build_new that it was an
19789 empty initializer() rather than no initializer. This can
19790 only happen when the initializer is a pack expansion whose
19791 parameter packs are of length zero. */
19792 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19796 init_vec
= make_tree_vector ();
19797 if (init
== void_node
)
19798 gcc_assert (init_vec
!= NULL
);
19801 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19802 vec_safe_push (init_vec
, TREE_VALUE (init
));
19806 /* Avoid passing an enclosing decl to valid_array_size_p. */
19807 in_decl
= NULL_TREE
;
19809 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19810 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19811 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19812 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19815 if (placement_vec
!= NULL
)
19816 release_tree_vector (placement_vec
);
19817 if (init_vec
!= NULL
)
19818 release_tree_vector (init_vec
);
19825 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19826 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19827 RETURN (delete_sanity (input_location
, op0
, op1
,
19828 DELETE_EXPR_USE_VEC (t
),
19829 DELETE_EXPR_USE_GLOBAL (t
),
19833 case COMPOUND_EXPR
:
19835 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19836 complain
& ~tf_decltype
, in_decl
,
19837 /*function_p=*/false,
19838 integral_constant_expression_p
);
19839 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19841 RECUR (TREE_OPERAND (t
, 1)),
19842 complain
|decltype_flag
));
19848 unsigned int nargs
, i
;
19853 function
= CALL_EXPR_FN (t
);
19854 /* Internal function with no arguments. */
19855 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19858 /* When we parsed the expression, we determined whether or
19859 not Koenig lookup should be performed. */
19860 koenig_p
= KOENIG_LOOKUP_P (t
);
19861 if (function
== NULL_TREE
)
19864 qualified_p
= false;
19866 else if (TREE_CODE (function
) == SCOPE_REF
)
19868 qualified_p
= true;
19869 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19871 /*address_p=*/false);
19873 else if (koenig_p
&& identifier_p (function
))
19875 /* Do nothing; calling tsubst_copy_and_build on an identifier
19876 would incorrectly perform unqualified lookup again.
19878 Note that we can also have an IDENTIFIER_NODE if the earlier
19879 unqualified lookup found a member function; in that case
19880 koenig_p will be false and we do want to do the lookup
19881 again to find the instantiated member function.
19883 FIXME but doing that causes c++/15272, so we need to stop
19884 using IDENTIFIER_NODE in that situation. */
19885 qualified_p
= false;
19889 if (TREE_CODE (function
) == COMPONENT_REF
)
19891 tree op
= TREE_OPERAND (function
, 1);
19893 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19894 || (BASELINK_P (op
)
19895 && BASELINK_QUALIFIED_P (op
)));
19898 qualified_p
= false;
19900 if (TREE_CODE (function
) == ADDR_EXPR
19901 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19902 /* Avoid error about taking the address of a constructor. */
19903 function
= TREE_OPERAND (function
, 0);
19905 function
= tsubst_copy_and_build (function
, args
, complain
,
19908 integral_constant_expression_p
);
19910 if (BASELINK_P (function
))
19911 qualified_p
= true;
19914 nargs
= call_expr_nargs (t
);
19915 releasing_vec call_args
;
19916 for (i
= 0; i
< nargs
; ++i
)
19918 tree arg
= CALL_EXPR_ARG (t
, i
);
19920 if (!PACK_EXPANSION_P (arg
))
19921 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19924 /* Expand the pack expansion and push each entry onto
19926 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19927 if (TREE_CODE (arg
) == TREE_VEC
)
19929 unsigned int len
, j
;
19931 len
= TREE_VEC_LENGTH (arg
);
19932 for (j
= 0; j
< len
; ++j
)
19934 tree value
= TREE_VEC_ELT (arg
, j
);
19935 if (value
!= NULL_TREE
)
19936 value
= convert_from_reference (value
);
19937 vec_safe_push (call_args
, value
);
19942 /* A partial substitution. Add one entry. */
19943 vec_safe_push (call_args
, arg
);
19948 /* Stripped-down processing for a call in a thunk. Specifically, in
19949 the thunk template for a generic lambda. */
19950 if (call_from_lambda_thunk_p (t
))
19952 /* Now that we've expanded any packs, the number of call args
19953 might be different. */
19954 unsigned int cargs
= call_args
->length ();
19955 tree thisarg
= NULL_TREE
;
19956 if (TREE_CODE (function
) == COMPONENT_REF
)
19958 thisarg
= TREE_OPERAND (function
, 0);
19959 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19960 thisarg
= TREE_OPERAND (thisarg
, 0);
19961 function
= TREE_OPERAND (function
, 1);
19962 if (TREE_CODE (function
) == BASELINK
)
19963 function
= BASELINK_FUNCTIONS (function
);
19965 /* We aren't going to do normal overload resolution, so force the
19966 template-id to resolve. */
19967 function
= resolve_nondeduced_context (function
, complain
);
19968 for (unsigned i
= 0; i
< cargs
; ++i
)
19970 /* In a thunk, pass through args directly, without any
19972 tree arg
= (*call_args
)[i
];
19973 while (TREE_CODE (arg
) != PARM_DECL
)
19974 arg
= TREE_OPERAND (arg
, 0);
19975 (*call_args
)[i
] = arg
;
19979 /* If there are no other args, just push 'this'. */
19981 vec_safe_push (call_args
, thisarg
);
19984 /* Otherwise, shift the other args over to make room. */
19985 tree last
= (*call_args
)[cargs
- 1];
19986 vec_safe_push (call_args
, last
);
19987 for (int i
= cargs
- 1; i
> 0; --i
)
19988 (*call_args
)[i
] = (*call_args
)[i
- 1];
19989 (*call_args
)[0] = thisarg
;
19992 ret
= build_call_a (function
, call_args
->length (),
19993 call_args
->address ());
19994 /* The thunk location is not interesting. */
19995 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19996 CALL_FROM_THUNK_P (ret
) = true;
19997 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19998 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20003 /* We do not perform argument-dependent lookup if normal
20004 lookup finds a non-function, in accordance with the
20005 expected resolution of DR 218. */
20007 && ((is_overloaded_fn (function
)
20008 /* If lookup found a member function, the Koenig lookup is
20009 not appropriate, even if an unqualified-name was used
20010 to denote the function. */
20011 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20012 || identifier_p (function
))
20013 /* Only do this when substitution turns a dependent call
20014 into a non-dependent call. */
20015 && type_dependent_expression_p_push (t
)
20016 && !any_type_dependent_arguments_p (call_args
))
20017 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20019 if (function
!= NULL_TREE
20020 && identifier_p (function
)
20021 && !any_type_dependent_arguments_p (call_args
))
20023 if (koenig_p
&& (complain
& tf_warning_or_error
))
20025 /* For backwards compatibility and good diagnostics, try
20026 the unqualified lookup again if we aren't in SFINAE
20028 tree unq
= (tsubst_copy_and_build
20029 (function
, args
, complain
, in_decl
, true,
20030 integral_constant_expression_p
));
20031 if (unq
== error_mark_node
)
20032 RETURN (error_mark_node
);
20034 if (unq
!= function
)
20036 /* In a lambda fn, we have to be careful to not
20037 introduce new this captures. Legacy code can't
20038 be using lambdas anyway, so it's ok to be
20040 bool in_lambda
= (current_class_type
20041 && LAMBDA_TYPE_P (current_class_type
));
20042 char const *const msg
20043 = G_("%qD was not declared in this scope, "
20044 "and no declarations were found by "
20045 "argument-dependent lookup at the point "
20046 "of instantiation");
20050 error_at (cp_expr_loc_or_input_loc (t
),
20053 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20059 if (INDIRECT_REF_P (fn
))
20060 fn
= TREE_OPERAND (fn
, 0);
20061 if (is_overloaded_fn (fn
))
20062 fn
= get_first_fn (fn
);
20065 /* Can't say anything more. */;
20066 else if (DECL_CLASS_SCOPE_P (fn
))
20068 location_t loc
= cp_expr_loc_or_input_loc (t
);
20070 "declarations in dependent base %qT are "
20071 "not found by unqualified lookup",
20072 DECL_CLASS_CONTEXT (fn
));
20073 if (current_class_ptr
)
20075 "use %<this->%D%> instead", function
);
20078 "use %<%T::%D%> instead",
20079 current_class_name
, function
);
20082 inform (DECL_SOURCE_LOCATION (fn
),
20083 "%qD declared here, later in the "
20084 "translation unit", fn
);
20086 RETURN (error_mark_node
);
20092 if (identifier_p (function
))
20094 if (complain
& tf_error
)
20095 unqualified_name_lookup_error (function
);
20096 RETURN (error_mark_node
);
20100 /* Remember that there was a reference to this entity. */
20101 if (function
!= NULL_TREE
20102 && DECL_P (function
)
20103 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20104 RETURN (error_mark_node
);
20106 /* Put back tf_decltype for the actual call. */
20107 complain
|= decltype_flag
;
20109 if (function
== NULL_TREE
)
20110 switch (CALL_EXPR_IFN (t
))
20113 gcc_assert (nargs
== 1);
20114 if (vec_safe_length (call_args
) != 1)
20116 error_at (cp_expr_loc_or_input_loc (t
),
20117 "wrong number of arguments to "
20118 "%<__builtin_launder%>");
20119 ret
= error_mark_node
;
20122 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20123 (*call_args
)[0], complain
);
20126 case IFN_VEC_CONVERT
:
20127 gcc_assert (nargs
== 1);
20128 if (vec_safe_length (call_args
) != 1)
20130 error_at (cp_expr_loc_or_input_loc (t
),
20131 "wrong number of arguments to "
20132 "%<__builtin_convertvector%>");
20133 ret
= error_mark_node
;
20136 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20137 tsubst (TREE_TYPE (t
), args
,
20138 complain
, in_decl
),
20140 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20145 /* Unsupported internal function with arguments. */
20146 gcc_unreachable ();
20148 else if (TREE_CODE (function
) == OFFSET_REF
20149 || TREE_CODE (function
) == DOTSTAR_EXPR
20150 || TREE_CODE (function
) == MEMBER_REF
)
20151 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20153 else if (TREE_CODE (function
) == COMPONENT_REF
)
20155 tree instance
= TREE_OPERAND (function
, 0);
20156 tree fn
= TREE_OPERAND (function
, 1);
20158 if (processing_template_decl
20159 && (type_dependent_expression_p (instance
)
20160 || (!BASELINK_P (fn
)
20161 && TREE_CODE (fn
) != FIELD_DECL
)
20162 || type_dependent_expression_p (fn
)
20163 || any_type_dependent_arguments_p (call_args
)))
20164 ret
= build_min_nt_call_vec (function
, call_args
);
20165 else if (!BASELINK_P (fn
))
20166 ret
= finish_call_expr (function
, &call_args
,
20167 /*disallow_virtual=*/false,
20168 /*koenig_p=*/false,
20171 ret
= (build_new_method_call
20173 &call_args
, NULL_TREE
,
20174 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20178 else if (concept_check_p (function
))
20180 /* FUNCTION is a template-id referring to a concept definition. */
20181 tree id
= unpack_concept_check (function
);
20182 tree tmpl
= TREE_OPERAND (id
, 0);
20183 tree args
= TREE_OPERAND (id
, 1);
20185 /* Calls to standard and variable concepts should have been
20186 previously diagnosed. */
20187 gcc_assert (function_concept_p (tmpl
));
20189 /* Ensure the result is wrapped as a call expression. */
20190 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20193 ret
= finish_call_expr (function
, &call_args
,
20194 /*disallow_virtual=*/qualified_p
,
20198 if (ret
!= error_mark_node
)
20200 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20201 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20202 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20203 if (op
|| ord
|| rev
)
20205 function
= extract_call_expr (ret
);
20206 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20207 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20208 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20217 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20218 cond
= mark_rvalue_use (cond
);
20219 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20222 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20224 if (integer_zerop (folded_cond
))
20226 ++c_inhibit_evaluation_warnings
;
20227 exp1
= RECUR (TREE_OPERAND (t
, 1));
20228 --c_inhibit_evaluation_warnings
;
20229 exp2
= RECUR (TREE_OPERAND (t
, 2));
20233 exp1
= RECUR (TREE_OPERAND (t
, 1));
20234 ++c_inhibit_evaluation_warnings
;
20235 exp2
= RECUR (TREE_OPERAND (t
, 2));
20236 --c_inhibit_evaluation_warnings
;
20238 cond
= folded_cond
;
20242 exp1
= RECUR (TREE_OPERAND (t
, 1));
20243 exp2
= RECUR (TREE_OPERAND (t
, 2));
20246 warning_sentinel
s(warn_duplicated_branches
);
20247 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20248 cond
, exp1
, exp2
, complain
));
20251 case PSEUDO_DTOR_EXPR
:
20253 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20254 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20255 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20256 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20261 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20263 case COMPONENT_REF
:
20270 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20271 args
, complain
, in_decl
);
20272 /* Remember that there was a reference to this entity. */
20273 if (DECL_P (object
)
20274 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20275 RETURN (error_mark_node
);
20276 object_type
= TREE_TYPE (object
);
20278 member
= TREE_OPERAND (t
, 1);
20279 if (BASELINK_P (member
))
20280 member
= tsubst_baselink (member
,
20281 non_reference (TREE_TYPE (object
)),
20282 args
, complain
, in_decl
);
20284 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20285 if (member
== error_mark_node
)
20286 RETURN (error_mark_node
);
20288 if (TREE_CODE (member
) == FIELD_DECL
)
20290 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20291 if (TREE_CODE (r
) == COMPONENT_REF
)
20292 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20295 else if (type_dependent_expression_p (object
))
20296 /* We can't do much here. */;
20297 else if (!CLASS_TYPE_P (object_type
))
20299 if (scalarish_type_p (object_type
))
20301 tree s
= NULL_TREE
;
20302 tree dtor
= member
;
20304 if (TREE_CODE (dtor
) == SCOPE_REF
)
20306 s
= TREE_OPERAND (dtor
, 0);
20307 dtor
= TREE_OPERAND (dtor
, 1);
20309 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20311 dtor
= TREE_OPERAND (dtor
, 0);
20313 RETURN (finish_pseudo_destructor_expr
20314 (object
, s
, dtor
, input_location
));
20318 else if (TREE_CODE (member
) == SCOPE_REF
20319 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20321 /* Lookup the template functions now that we know what the
20323 tree scope
= TREE_OPERAND (member
, 0);
20324 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20325 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20326 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20327 /*complain=*/false);
20328 if (BASELINK_P (member
))
20330 BASELINK_FUNCTIONS (member
)
20331 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20333 member
= (adjust_result_of_qualified_name_lookup
20334 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20339 qualified_name_lookup_error (scope
, tmpl
, member
,
20341 RETURN (error_mark_node
);
20344 else if (TREE_CODE (member
) == SCOPE_REF
20345 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20346 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20348 if (complain
& tf_error
)
20350 if (TYPE_P (TREE_OPERAND (member
, 0)))
20351 error ("%qT is not a class or namespace",
20352 TREE_OPERAND (member
, 0));
20354 error ("%qD is not a class or namespace",
20355 TREE_OPERAND (member
, 0));
20357 RETURN (error_mark_node
);
20360 r
= finish_class_member_access_expr (object
, member
,
20361 /*template_p=*/false,
20363 if (TREE_CODE (r
) == COMPONENT_REF
)
20364 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20369 RETURN (build_throw
20370 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20374 vec
<constructor_elt
, va_gc
> *n
;
20375 constructor_elt
*ce
;
20376 unsigned HOST_WIDE_INT idx
;
20377 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20378 bool process_index_p
;
20380 bool need_copy_p
= false;
20383 if (type
== error_mark_node
)
20384 RETURN (error_mark_node
);
20386 /* We do not want to process the index of aggregate
20387 initializers as they are identifier nodes which will be
20388 looked up by digest_init. */
20389 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20391 if (null_member_pointer_value_p (t
))
20393 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20397 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20398 newlen
= vec_safe_length (n
);
20399 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20401 if (ce
->index
&& process_index_p
20402 /* An identifier index is looked up in the type
20403 being initialized, not the current scope. */
20404 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20405 ce
->index
= RECUR (ce
->index
);
20407 if (PACK_EXPANSION_P (ce
->value
))
20409 /* Substitute into the pack expansion. */
20410 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20413 if (ce
->value
== error_mark_node
20414 || PACK_EXPANSION_P (ce
->value
))
20416 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20417 /* Just move the argument into place. */
20418 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20421 /* Update the length of the final CONSTRUCTOR
20422 arguments vector, and note that we will need to
20424 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20425 need_copy_p
= true;
20429 ce
->value
= RECUR (ce
->value
);
20434 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20436 vec_alloc (n
, newlen
);
20437 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20439 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20441 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20442 for (i
= 0; i
< len
; ++i
)
20443 CONSTRUCTOR_APPEND_ELT (n
, 0,
20444 TREE_VEC_ELT (ce
->value
, i
));
20447 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20451 r
= build_constructor (init_list_type_node
, n
);
20452 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20453 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20454 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20456 if (TREE_HAS_CONSTRUCTOR (t
))
20458 fcl_t cl
= fcl_functional
;
20459 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20461 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20464 TREE_TYPE (r
) = type
;
20470 tree operand_0
= TREE_OPERAND (t
, 0);
20471 if (TYPE_P (operand_0
))
20473 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20474 RETURN (get_typeid (operand_0
, complain
));
20478 operand_0
= RECUR (operand_0
);
20479 RETURN (build_typeid (operand_0
, complain
));
20490 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20491 /* ??? We're doing a subset of finish_id_expression here. */
20492 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20493 /* Replace an evaluated use of the thread_local variable with
20494 a call to its wrapper. */
20496 else if (outer_automatic_var_p (r
))
20497 r
= process_outer_var_ref (r
, complain
);
20499 if (!TYPE_REF_P (TREE_TYPE (t
)))
20500 /* If the original type was a reference, we'll be wrapped in
20501 the appropriate INDIRECT_REF. */
20502 r
= convert_from_reference (r
);
20508 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20509 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20510 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20513 case OFFSETOF_EXPR
:
20516 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20517 in_decl
, /*function_p=*/false,
20518 /*integral_constant_expression_p=*/false);
20519 RETURN (finish_offsetof (object_ptr
,
20520 RECUR (TREE_OPERAND (t
, 0)),
20521 EXPR_LOCATION (t
)));
20524 case ADDRESSOF_EXPR
:
20525 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20526 RECUR (TREE_OPERAND (t
, 0)), complain
));
20530 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20531 complain
, in_decl
);
20532 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20533 complain
, in_decl
);
20534 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20535 TRAIT_EXPR_KIND (t
), type1
, type2
));
20540 tree old_stmt_expr
= cur_stmt_expr
;
20541 tree stmt_expr
= begin_stmt_expr ();
20543 cur_stmt_expr
= stmt_expr
;
20544 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20545 integral_constant_expression_p
);
20546 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20547 cur_stmt_expr
= old_stmt_expr
;
20549 /* If the resulting list of expression statement is empty,
20550 fold it further into void_node. */
20551 if (empty_expr_stmt_p (stmt_expr
))
20552 stmt_expr
= void_node
;
20554 RETURN (stmt_expr
);
20559 if (complain
& tf_partial
)
20561 /* We don't have a full set of template arguments yet; don't touch
20562 the lambda at all. */
20563 gcc_assert (processing_template_decl
);
20566 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20568 RETURN (build_lambda_object (r
));
20572 /* We can get here for a constant initializer of non-dependent type.
20573 FIXME stop folding in cp_parser_initializer_clause. */
20575 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20580 case TRANSACTION_EXPR
:
20581 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20582 integral_constant_expression_p
));
20585 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20587 case VEC_PERM_EXPR
:
20589 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20590 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20591 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20592 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20596 case REQUIRES_EXPR
:
20598 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20603 /* No need to substitute further, a RANGE_EXPR will always be built
20604 with constant operands. */
20607 case NON_LVALUE_EXPR
:
20608 case VIEW_CONVERT_EXPR
:
20609 if (location_wrapper_p (t
))
20610 /* We need to do this here as well as in tsubst_copy so we get the
20611 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20612 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20613 EXPR_LOCATION (t
)));
20617 /* Handle Objective-C++ constructs, if appropriate. */
20620 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20621 in_decl
, /*function_p=*/false);
20625 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20631 input_location
= save_loc
;
20635 /* Verify that the instantiated ARGS are valid. For type arguments,
20636 make sure that the type's linkage is ok. For non-type arguments,
20637 make sure they are constants if they are integral or enumerations.
20638 Emit an error under control of COMPLAIN, and return TRUE on error. */
20641 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20643 if (dependent_template_arg_p (t
))
20645 if (ARGUMENT_PACK_P (t
))
20647 tree vec
= ARGUMENT_PACK_ARGS (t
);
20648 int len
= TREE_VEC_LENGTH (vec
);
20649 bool result
= false;
20652 for (i
= 0; i
< len
; ++i
)
20653 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20657 else if (TYPE_P (t
))
20659 /* [basic.link]: A name with no linkage (notably, the name
20660 of a class or enumeration declared in a local scope)
20661 shall not be used to declare an entity with linkage.
20662 This implies that names with no linkage cannot be used as
20665 DR 757 relaxes this restriction for C++0x. */
20666 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20667 : no_linkage_check (t
, /*relaxed_p=*/false));
20671 /* DR 488 makes use of a type with no linkage cause
20672 type deduction to fail. */
20673 if (complain
& tf_error
)
20675 if (TYPE_UNNAMED_P (nt
))
20676 error ("%qT is/uses unnamed type", t
);
20678 error ("template argument for %qD uses local type %qT",
20683 /* In order to avoid all sorts of complications, we do not
20684 allow variably-modified types as template arguments. */
20685 else if (variably_modified_type_p (t
, NULL_TREE
))
20687 if (complain
& tf_error
)
20688 error ("%qT is a variably modified type", t
);
20692 /* Class template and alias template arguments should be OK. */
20693 else if (DECL_TYPE_TEMPLATE_P (t
))
20695 /* A non-type argument of integral or enumerated type must be a
20697 else if (TREE_TYPE (t
)
20698 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20699 && !REFERENCE_REF_P (t
)
20700 && !TREE_CONSTANT (t
))
20702 if (complain
& tf_error
)
20703 error ("integral expression %qE is not constant", t
);
20710 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20712 int ix
, len
= DECL_NTPARMS (tmpl
);
20713 bool result
= false;
20715 for (ix
= 0; ix
!= len
; ix
++)
20717 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20720 if (result
&& (complain
& tf_error
))
20721 error (" trying to instantiate %qD", tmpl
);
20725 /* We're out of SFINAE context now, so generate diagnostics for the access
20726 errors we saw earlier when instantiating D from TMPL and ARGS. */
20729 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20731 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20732 tree type
= TREE_TYPE (pattern
);
20733 location_t loc
= input_location
;
20735 push_access_scope (d
);
20736 push_deferring_access_checks (dk_no_deferred
);
20737 input_location
= DECL_SOURCE_LOCATION (pattern
);
20738 tsubst (type
, args
, tf_warning_or_error
, d
);
20739 input_location
= loc
;
20740 pop_deferring_access_checks ();
20741 pop_access_scope (d
);
20744 /* Instantiate the indicated variable, function, or alias template TMPL with
20745 the template arguments in TARG_PTR. */
20748 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20750 tree targ_ptr
= orig_args
;
20754 bool access_ok
= true;
20756 if (tmpl
== error_mark_node
)
20757 return error_mark_node
;
20759 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20761 /* If this function is a clone, handle it specially. */
20762 if (DECL_CLONED_FUNCTION_P (tmpl
))
20767 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20768 DECL_CLONED_FUNCTION. */
20769 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20770 targ_ptr
, complain
);
20771 if (spec
== error_mark_node
)
20772 return error_mark_node
;
20774 /* Look for the clone. */
20775 FOR_EACH_CLONE (clone
, spec
)
20776 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20778 /* We should always have found the clone by now. */
20779 gcc_unreachable ();
20783 if (targ_ptr
== error_mark_node
)
20784 return error_mark_node
;
20786 /* Check to see if we already have this specialization. */
20787 gen_tmpl
= most_general_template (tmpl
);
20788 if (TMPL_ARGS_DEPTH (targ_ptr
)
20789 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20790 /* targ_ptr only has the innermost template args, so add the outer ones
20791 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20792 the case of a non-dependent call within a template definition). */
20793 targ_ptr
= (add_outermost_template_args
20794 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20797 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20798 but it doesn't seem to be on the hot path. */
20799 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20801 gcc_checking_assert (tmpl
== gen_tmpl
20803 = retrieve_specialization (tmpl
, orig_args
, 0))
20805 || fndecl
== NULL_TREE
);
20807 if (spec
!= NULL_TREE
)
20809 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20811 if (complain
& tf_error
)
20812 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20813 return error_mark_node
;
20818 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20820 return error_mark_node
;
20822 /* We are building a FUNCTION_DECL, during which the access of its
20823 parameters and return types have to be checked. However this
20824 FUNCTION_DECL which is the desired context for access checking
20825 is not built yet. We solve this chicken-and-egg problem by
20826 deferring all checks until we have the FUNCTION_DECL. */
20827 push_deferring_access_checks (dk_deferred
);
20829 /* Instantiation of the function happens in the context of the function
20830 template, not the context of the overload resolution we're doing. */
20831 push_to_top_level ();
20832 /* If there are dependent arguments, e.g. because we're doing partial
20833 ordering, make sure processing_template_decl stays set. */
20834 if (uses_template_parms (targ_ptr
))
20835 ++processing_template_decl
;
20836 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20838 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20839 complain
, gen_tmpl
, true);
20840 push_nested_class (ctx
);
20843 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20845 fndecl
= NULL_TREE
;
20846 if (VAR_P (pattern
))
20848 /* We need to determine if we're using a partial or explicit
20849 specialization now, because the type of the variable could be
20851 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20852 tree elt
= most_specialized_partial_spec (tid
, complain
);
20853 if (elt
== error_mark_node
)
20854 pattern
= error_mark_node
;
20857 tree partial_tmpl
= TREE_VALUE (elt
);
20858 tree partial_args
= TREE_PURPOSE (elt
);
20859 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20860 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20864 /* Substitute template parameters to obtain the specialization. */
20865 if (fndecl
== NULL_TREE
)
20866 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20867 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20868 pop_nested_class ();
20869 pop_from_top_level ();
20871 if (fndecl
== error_mark_node
)
20873 pop_deferring_access_checks ();
20874 return error_mark_node
;
20877 /* The DECL_TI_TEMPLATE should always be the immediate parent
20878 template, not the most general template. */
20879 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20880 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20882 /* Now we know the specialization, compute access previously
20883 deferred. Do no access control for inheriting constructors,
20884 as we already checked access for the inherited constructor. */
20885 if (!(flag_new_inheriting_ctors
20886 && DECL_INHERITED_CTOR (fndecl
)))
20888 push_access_scope (fndecl
);
20889 if (!perform_deferred_access_checks (complain
))
20891 pop_access_scope (fndecl
);
20893 pop_deferring_access_checks ();
20895 /* If we've just instantiated the main entry point for a function,
20896 instantiate all the alternate entry points as well. We do this
20897 by cloning the instantiation of the main entry point, not by
20898 instantiating the template clones. */
20899 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20900 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20901 clone_cdtor (fndecl
, /*update_methods=*/false);
20905 if (!(complain
& tf_error
))
20907 /* Remember to reinstantiate when we're out of SFINAE so the user
20908 can see the errors. */
20909 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20911 return error_mark_node
;
20916 /* Wrapper for instantiate_template_1. */
20919 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20922 timevar_push (TV_TEMPLATE_INST
);
20923 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20924 timevar_pop (TV_TEMPLATE_INST
);
20928 /* Instantiate the alias template TMPL with ARGS. Also push a template
20929 instantiation level, which instantiate_template doesn't do because
20930 functions and variables have sufficient context established by the
20934 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20936 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20937 return error_mark_node
;
20940 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20941 args
, tmpl
, complain
,
20942 /*require_all_args=*/true,
20943 /*use_default_args=*/true);
20945 /* FIXME check for satisfaction in check_instantiated_args. */
20947 && !any_dependent_template_arguments_p (args
)
20948 && !constraints_satisfied_p (tmpl
, args
))
20950 if (complain
& tf_error
)
20952 auto_diagnostic_group d
;
20953 error ("template constraint failure for %qD", tmpl
);
20954 diagnose_constraints (input_location
, tmpl
, args
);
20956 return error_mark_node
;
20959 if (!push_tinst_level (tmpl
, args
))
20960 return error_mark_node
;
20961 tree r
= instantiate_template (tmpl
, args
, complain
);
20962 pop_tinst_level ();
20967 /* PARM is a template parameter pack for FN. Returns true iff
20968 PARM is used in a deducible way in the argument list of FN. */
20971 pack_deducible_p (tree parm
, tree fn
)
20973 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20974 for (; t
; t
= TREE_CHAIN (t
))
20976 tree type
= TREE_VALUE (t
);
20978 if (!PACK_EXPANSION_P (type
))
20980 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20981 packs
; packs
= TREE_CHAIN (packs
))
20982 if (template_args_equal (TREE_VALUE (packs
), parm
))
20984 /* The template parameter pack is used in a function parameter
20985 pack. If this is the end of the parameter list, the
20986 template parameter pack is deducible. */
20987 if (TREE_CHAIN (t
) == void_list_node
)
20990 /* Otherwise, not. Well, it could be deduced from
20991 a non-pack parameter, but doing so would end up with
20992 a deduction mismatch, so don't bother. */
20996 /* The template parameter pack isn't used in any function parameter
20997 packs, but it might be used deeper, e.g. tuple<Args...>. */
21001 /* Subroutine of fn_type_unification: check non-dependent parms for
21005 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21006 tree fn
, unification_kind_t strict
, int flags
,
21007 struct conversion
**convs
, bool explain_p
)
21009 /* Non-constructor methods need to leave a conversion for 'this', which
21010 isn't included in nargs here. */
21011 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21012 && !DECL_CONSTRUCTOR_P (fn
));
21014 for (unsigned ia
= 0;
21015 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21017 tree parm
= TREE_VALUE (parms
);
21019 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21020 && (!TREE_CHAIN (parms
)
21021 || TREE_CHAIN (parms
) == void_list_node
))
21022 /* For a function parameter pack that occurs at the end of the
21023 parameter-declaration-list, the type A of each remaining
21024 argument of the call is compared with the type P of the
21025 declarator-id of the function parameter pack. */
21028 parms
= TREE_CHAIN (parms
);
21030 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21031 /* For a function parameter pack that does not occur at the
21032 end of the parameter-declaration-list, the type of the
21033 parameter pack is a non-deduced context. */
21036 if (!uses_template_parms (parm
))
21038 tree arg
= args
[ia
];
21039 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21040 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21042 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21043 conv_p
, explain_p
))
21053 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21054 NARGS elements of the arguments that are being used when calling
21055 it. TARGS is a vector into which the deduced template arguments
21058 Returns either a FUNCTION_DECL for the matching specialization of FN or
21059 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21060 true, diagnostics will be printed to explain why it failed.
21062 If FN is a conversion operator, or we are trying to produce a specific
21063 specialization, RETURN_TYPE is the return type desired.
21065 The EXPLICIT_TARGS are explicit template arguments provided via a
21068 The parameter STRICT is one of:
21071 We are deducing arguments for a function call, as in
21072 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21073 deducing arguments for a call to the result of a conversion
21074 function template, as in [over.call.object].
21077 We are deducing arguments for a conversion function, as in
21078 [temp.deduct.conv].
21081 We are deducing arguments when doing an explicit instantiation
21082 as in [temp.explicit], when determining an explicit specialization
21083 as in [temp.expl.spec], or when taking the address of a function
21084 template, as in [temp.deduct.funcaddr]. */
21087 fn_type_unification (tree fn
,
21088 tree explicit_targs
,
21091 unsigned int nargs
,
21093 unification_kind_t strict
,
21095 struct conversion
**convs
,
21101 tree decl
= NULL_TREE
;
21102 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21104 static int deduction_depth
;
21105 /* type_unification_real will pass back any access checks from default
21106 template argument substitution. */
21107 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21108 /* We don't have all the template args yet. */
21109 bool incomplete
= true;
21112 if (flag_new_inheriting_ctors
)
21113 fn
= strip_inheriting_ctors (fn
);
21115 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21116 tree r
= error_mark_node
;
21118 tree full_targs
= targs
;
21119 if (TMPL_ARGS_DEPTH (targs
)
21120 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21121 full_targs
= (add_outermost_template_args
21122 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21126 complain
|= tf_decltype
;
21128 /* In C++0x, it's possible to have a function template whose type depends
21129 on itself recursively. This is most obvious with decltype, but can also
21130 occur with enumeration scope (c++/48969). So we need to catch infinite
21131 recursion and reject the substitution at deduction time; this function
21132 will return error_mark_node for any repeated substitution.
21134 This also catches excessive recursion such as when f<N> depends on
21135 f<N-1> across all integers, and returns error_mark_node for all the
21136 substitutions back up to the initial one.
21138 This is, of course, not reentrant. */
21139 if (excessive_deduction_depth
)
21140 return error_mark_node
;
21143 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21145 fntype
= TREE_TYPE (fn
);
21146 if (explicit_targs
)
21150 The specified template arguments must match the template
21151 parameters in kind (i.e., type, nontype, template), and there
21152 must not be more arguments than there are parameters;
21153 otherwise type deduction fails.
21155 Nontype arguments must match the types of the corresponding
21156 nontype template parameters, or must be convertible to the
21157 types of the corresponding nontype parameters as specified in
21158 _temp.arg.nontype_, otherwise type deduction fails.
21160 All references in the function type of the function template
21161 to the corresponding template parameters are replaced by the
21162 specified template argument values. If a substitution in a
21163 template parameter or in the function type of the function
21164 template results in an invalid type, type deduction fails. */
21165 int i
, len
= TREE_VEC_LENGTH (tparms
);
21166 location_t loc
= input_location
;
21167 incomplete
= false;
21169 if (explicit_targs
== error_mark_node
)
21172 if (TMPL_ARGS_DEPTH (explicit_targs
)
21173 < TMPL_ARGS_DEPTH (full_targs
))
21174 explicit_targs
= add_outermost_template_args (full_targs
,
21177 /* Adjust any explicit template arguments before entering the
21178 substitution context. */
21180 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21181 complain
|tf_partial
,
21182 /*require_all_args=*/false,
21183 /*use_default_args=*/false));
21184 if (explicit_targs
== error_mark_node
)
21187 /* Substitute the explicit args into the function type. This is
21188 necessary so that, for instance, explicitly declared function
21189 arguments can match null pointed constants. If we were given
21190 an incomplete set of explicit args, we must not do semantic
21191 processing during substitution as we could create partial
21193 for (i
= 0; i
< len
; i
++)
21195 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21196 bool parameter_pack
= false;
21197 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21199 /* Dig out the actual parm. */
21200 if (TREE_CODE (parm
) == TYPE_DECL
21201 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21203 parm
= TREE_TYPE (parm
);
21204 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21206 else if (TREE_CODE (parm
) == PARM_DECL
)
21208 parm
= DECL_INITIAL (parm
);
21209 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21212 if (targ
== NULL_TREE
)
21213 /* No explicit argument for this template parameter. */
21215 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21217 /* Mark the argument pack as "incomplete". We could
21218 still deduce more arguments during unification.
21219 We remove this mark in type_unification_real. */
21220 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21221 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21222 = ARGUMENT_PACK_ARGS (targ
);
21224 /* We have some incomplete argument packs. */
21231 if (!push_tinst_level (fn
, explicit_targs
))
21233 excessive_deduction_depth
= true;
21236 ++processing_template_decl
;
21237 input_location
= DECL_SOURCE_LOCATION (fn
);
21238 /* Ignore any access checks; we'll see them again in
21239 instantiate_template and they might have the wrong
21240 access path at this point. */
21241 push_deferring_access_checks (dk_deferred
);
21242 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21243 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21244 pop_deferring_access_checks ();
21245 input_location
= loc
;
21246 --processing_template_decl
;
21247 pop_tinst_level ();
21249 if (fntype
== error_mark_node
)
21253 /* Place the explicitly specified arguments in TARGS. */
21254 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21255 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21256 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21257 if (!incomplete
&& CHECKING_P
21258 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21259 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21260 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21263 if (return_type
&& strict
!= DEDUCE_CALL
)
21265 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21266 new_args
[0] = return_type
;
21267 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21275 /* Never do unification on the 'this' parameter. */
21276 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21278 if (return_type
&& strict
== DEDUCE_CALL
)
21280 /* We're deducing for a call to the result of a template conversion
21281 function. The parms we really want are in return_type. */
21282 if (INDIRECT_TYPE_P (return_type
))
21283 return_type
= TREE_TYPE (return_type
);
21284 parms
= TYPE_ARG_TYPES (return_type
);
21286 else if (return_type
)
21288 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21291 /* We allow incomplete unification without an error message here
21292 because the standard doesn't seem to explicitly prohibit it. Our
21293 callers must be ready to deal with unification failures in any
21296 /* If we aren't explaining yet, push tinst context so we can see where
21297 any errors (e.g. from class instantiations triggered by instantiation
21298 of default template arguments) come from. If we are explaining, this
21299 context is redundant. */
21300 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21302 excessive_deduction_depth
= true;
21306 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21307 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21308 strict
, &checks
, explain_p
);
21310 pop_tinst_level ();
21314 /* Now that we have bindings for all of the template arguments,
21315 ensure that the arguments deduced for the template template
21316 parameters have compatible template parameter lists. We cannot
21317 check this property before we have deduced all template
21318 arguments, because the template parameter types of a template
21319 template parameter might depend on prior template parameters
21320 deduced after the template template parameter. The following
21321 ill-formed example illustrates this issue:
21323 template<typename T, template<T> class C> void f(C<5>, T);
21325 template<int N> struct X {};
21328 f(X<5>(), 5l); // error: template argument deduction fails
21331 The template parameter list of 'C' depends on the template type
21332 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21333 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21334 time that we deduce 'C'. */
21335 if (!template_template_parm_bindings_ok_p
21336 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21338 unify_inconsistent_template_template_parameters (explain_p
);
21344 /* CWG2369: Check satisfaction before non-deducible conversions. */
21345 if (!constraints_satisfied_p (fn
, targs
))
21348 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21352 /* DR 1391: All parameters have args, now check non-dependent parms for
21353 convertibility. We don't do this if all args were explicitly specified,
21354 as the standard says that we substitute explicit args immediately. */
21356 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21360 /* All is well so far. Now, check:
21364 When all template arguments have been deduced, all uses of
21365 template parameters in nondeduced contexts are replaced with
21366 the corresponding deduced argument values. If the
21367 substitution results in an invalid type, as described above,
21368 type deduction fails. */
21369 if (!push_tinst_level (fn
, targs
))
21371 excessive_deduction_depth
= true;
21375 /* Also collect access checks from the instantiation. */
21376 reopen_deferring_access_checks (checks
);
21378 decl
= instantiate_template (fn
, targs
, complain
);
21380 checks
= get_deferred_access_checks ();
21381 pop_deferring_access_checks ();
21383 pop_tinst_level ();
21385 if (decl
== error_mark_node
)
21388 /* Now perform any access checks encountered during substitution. */
21389 push_access_scope (decl
);
21390 ok
= perform_access_checks (checks
, complain
);
21391 pop_access_scope (decl
);
21395 /* If we're looking for an exact match, check that what we got
21396 is indeed an exact match. It might not be if some template
21397 parameters are used in non-deduced contexts. But don't check
21398 for an exact match if we have dependent template arguments;
21399 in that case we're doing partial ordering, and we already know
21400 that we have two candidates that will provide the actual type. */
21401 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21403 tree substed
= TREE_TYPE (decl
);
21407 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21409 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21410 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21411 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21413 unify_type_mismatch (explain_p
, args
[i
],
21414 TREE_VALUE (sarg
));
21419 /* After doing deduction with the inherited constructor, actually return an
21420 instantiation of the inheriting constructor. */
21422 decl
= instantiate_template (orig_fn
, targs
, complain
);
21428 if (excessive_deduction_depth
)
21430 if (deduction_depth
== 0)
21431 /* Reset once we're all the way out. */
21432 excessive_deduction_depth
= false;
21438 /* Adjust types before performing type deduction, as described in
21439 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21440 sections are symmetric. PARM is the type of a function parameter
21441 or the return type of the conversion function. ARG is the type of
21442 the argument passed to the call, or the type of the value
21443 initialized with the result of the conversion function.
21444 ARG_EXPR is the original argument expression, which may be null. */
21447 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21460 /* Swap PARM and ARG throughout the remainder of this
21461 function; the handling is precisely symmetric since PARM
21462 will initialize ARG rather than vice versa. */
21463 std::swap (parm
, arg
);
21467 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21468 too, but here handle it by stripping the reference from PARM
21469 rather than by adding it to ARG. */
21470 if (TYPE_REF_P (*parm
)
21471 && TYPE_REF_IS_RVALUE (*parm
)
21472 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21473 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21474 && TYPE_REF_P (*arg
)
21475 && !TYPE_REF_IS_RVALUE (*arg
))
21476 *parm
= TREE_TYPE (*parm
);
21477 /* Nothing else to do in this case. */
21481 gcc_unreachable ();
21484 if (!TYPE_REF_P (*parm
))
21486 /* [temp.deduct.call]
21488 If P is not a reference type:
21490 --If A is an array type, the pointer type produced by the
21491 array-to-pointer standard conversion (_conv.array_) is
21492 used in place of A for type deduction; otherwise,
21494 --If A is a function type, the pointer type produced by
21495 the function-to-pointer standard conversion
21496 (_conv.func_) is used in place of A for type deduction;
21499 --If A is a cv-qualified type, the top level
21500 cv-qualifiers of A's type are ignored for type
21502 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21503 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21504 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21505 *arg
= build_pointer_type (*arg
);
21507 *arg
= TYPE_MAIN_VARIANT (*arg
);
21510 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21511 reference to a cv-unqualified template parameter that does not represent a
21512 template parameter of a class template (during class template argument
21513 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21514 an lvalue, the type "lvalue reference to A" is used in place of A for type
21516 if (TYPE_REF_P (*parm
)
21517 && TYPE_REF_IS_RVALUE (*parm
)
21518 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21519 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21520 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21521 && (arg_expr
? lvalue_p (arg_expr
)
21522 /* try_one_overload doesn't provide an arg_expr, but
21523 functions are always lvalues. */
21524 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21525 *arg
= build_reference_type (*arg
);
21527 /* [temp.deduct.call]
21529 If P is a cv-qualified type, the top level cv-qualifiers
21530 of P's type are ignored for type deduction. If P is a
21531 reference type, the type referred to by P is used for
21533 *parm
= TYPE_MAIN_VARIANT (*parm
);
21534 if (TYPE_REF_P (*parm
))
21536 *parm
= TREE_TYPE (*parm
);
21537 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21540 /* DR 322. For conversion deduction, remove a reference type on parm
21541 too (which has been swapped into ARG). */
21542 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21543 *arg
= TREE_TYPE (*arg
);
21548 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21549 template which doesn't contain any deducible template parameters; check if
21550 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21551 unify_one_argument. */
21554 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21555 int flags
, struct conversion
**conv_p
,
21561 type
= TREE_TYPE (arg
);
21565 if (same_type_p (parm
, type
))
21566 return unify_success (explain_p
);
21568 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21569 if (strict
== DEDUCE_CONV
)
21571 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21572 return unify_success (explain_p
);
21574 else if (strict
!= DEDUCE_EXACT
)
21577 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21579 /* Avoid recalculating this in add_function_candidate. */
21581 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21583 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21585 return unify_success (explain_p
);
21588 if (strict
== DEDUCE_EXACT
)
21589 return unify_type_mismatch (explain_p
, parm
, arg
);
21591 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21594 static bool uses_deducible_template_parms (tree type
);
21596 /* Returns true iff the expression EXPR is one from which a template
21597 argument can be deduced. In other words, if it's an undecorated
21598 use of a template non-type parameter. */
21601 deducible_expression (tree expr
)
21603 /* Strip implicit conversions. */
21604 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21605 expr
= TREE_OPERAND (expr
, 0);
21606 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21609 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21610 deducible way; that is, if it has a max value of <PARM> - 1. */
21613 deducible_array_bound (tree domain
)
21615 if (domain
== NULL_TREE
)
21618 tree max
= TYPE_MAX_VALUE (domain
);
21619 if (TREE_CODE (max
) != MINUS_EXPR
)
21622 return deducible_expression (TREE_OPERAND (max
, 0));
21625 /* Returns true iff the template arguments ARGS use a template parameter
21626 in a deducible way. */
21629 deducible_template_args (tree args
)
21631 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21634 tree elt
= TREE_VEC_ELT (args
, i
);
21635 if (ARGUMENT_PACK_P (elt
))
21636 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21639 if (PACK_EXPANSION_P (elt
))
21640 elt
= PACK_EXPANSION_PATTERN (elt
);
21641 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21643 else if (TYPE_P (elt
))
21644 deducible
= uses_deducible_template_parms (elt
);
21646 deducible
= deducible_expression (elt
);
21654 /* Returns true iff TYPE contains any deducible references to template
21655 parameters, as per 14.8.2.5. */
21658 uses_deducible_template_parms (tree type
)
21660 if (PACK_EXPANSION_P (type
))
21661 type
= PACK_EXPANSION_PATTERN (type
);
21668 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21669 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21675 if (INDIRECT_TYPE_P (type
))
21676 return uses_deducible_template_parms (TREE_TYPE (type
));
21678 /* T[integer-constant ]
21680 if (TREE_CODE (type
) == ARRAY_TYPE
)
21681 return (uses_deducible_template_parms (TREE_TYPE (type
))
21682 || deducible_array_bound (TYPE_DOMAIN (type
)));
21694 if (TYPE_PTRMEM_P (type
))
21695 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21696 || (uses_deducible_template_parms
21697 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21699 /* template-name <T> (where template-name refers to a class template)
21700 template-name <i> (where template-name refers to a class template) */
21701 if (CLASS_TYPE_P (type
)
21702 && CLASSTYPE_TEMPLATE_INFO (type
)
21703 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21704 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21705 (CLASSTYPE_TI_ARGS (type
)));
21710 if (FUNC_OR_METHOD_TYPE_P (type
))
21712 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21714 tree parm
= TYPE_ARG_TYPES (type
);
21715 if (TREE_CODE (type
) == METHOD_TYPE
)
21716 parm
= TREE_CHAIN (parm
);
21717 for (; parm
; parm
= TREE_CHAIN (parm
))
21718 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21725 /* Subroutine of type_unification_real and unify_pack_expansion to
21726 handle unification of a single P/A pair. Parameters are as
21727 for those functions. */
21730 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21731 int subr
, unification_kind_t strict
,
21734 tree arg_expr
= NULL_TREE
;
21737 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21738 return unify_invalid (explain_p
);
21739 if (arg
== unknown_type_node
)
21740 /* We can't deduce anything from this, but we might get all the
21741 template args from other function args. */
21742 return unify_success (explain_p
);
21744 /* Implicit conversions (Clause 4) will be performed on a function
21745 argument to convert it to the type of the corresponding function
21746 parameter if the parameter type contains no template-parameters that
21747 participate in template argument deduction. */
21748 if (strict
!= DEDUCE_EXACT
21749 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21750 /* For function parameters with no deducible template parameters,
21751 just return. We'll check non-dependent conversions later. */
21752 return unify_success (explain_p
);
21757 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21758 | UNIFY_ALLOW_MORE_CV_QUAL
21759 | UNIFY_ALLOW_DERIVED
);
21763 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21767 arg_strict
= UNIFY_ALLOW_NONE
;
21771 gcc_unreachable ();
21774 /* We only do these transformations if this is the top-level
21775 parameter_type_list in a call or declaration matching; in other
21776 situations (nested function declarators, template argument lists) we
21777 won't be comparing a type to an expression, and we don't do any type
21783 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21784 if (type_unknown_p (arg
))
21786 /* [temp.deduct.type] A template-argument can be
21787 deduced from a pointer to function or pointer
21788 to member function argument if the set of
21789 overloaded functions does not contain function
21790 templates and at most one of a set of
21791 overloaded functions provides a unique
21793 resolve_overloaded_unification (tparms
, targs
, parm
,
21795 arg_strict
, explain_p
);
21796 /* If a unique match was not found, this is a
21797 non-deduced context, so we still succeed. */
21798 return unify_success (explain_p
);
21802 arg
= unlowered_expr_type (arg
);
21803 if (arg
== error_mark_node
)
21804 return unify_invalid (explain_p
);
21808 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21811 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21812 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21813 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21815 /* For deduction from an init-list we need the actual list. */
21816 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21818 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21821 /* for_each_template_parm callback that always returns 0. */
21824 zero_r (tree
, void *)
21829 /* for_each_template_parm any_fn callback to handle deduction of a template
21830 type argument from the type of an array bound. */
21833 array_deduction_r (tree t
, void *data
)
21835 tree_pair_p d
= (tree_pair_p
)data
;
21836 tree
&tparms
= d
->purpose
;
21837 tree
&targs
= d
->value
;
21839 if (TREE_CODE (t
) == ARRAY_TYPE
)
21840 if (tree dom
= TYPE_DOMAIN (t
))
21841 if (tree max
= TYPE_MAX_VALUE (dom
))
21843 if (TREE_CODE (max
) == MINUS_EXPR
)
21844 max
= TREE_OPERAND (max
, 0);
21845 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21846 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21847 UNIFY_ALLOW_NONE
, /*explain*/false);
21850 /* Keep walking. */
21854 /* Try to deduce any not-yet-deduced template type arguments from the type of
21855 an array bound. This is handled separately from unify because 14.8.2.5 says
21856 "The type of a type parameter is only deduced from an array bound if it is
21857 not otherwise deduced." */
21860 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21862 tree_pair_s data
= { tparms
, targs
};
21863 hash_set
<tree
> visited
;
21864 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21865 /*nondeduced*/false, array_deduction_r
);
21868 /* Most parms like fn_type_unification.
21870 If SUBR is 1, we're being called recursively (to unify the
21871 arguments of a function or method parameter of a function
21874 CHECKS is a pointer to a vector of access checks encountered while
21875 substituting default template arguments. */
21878 type_unification_real (tree tparms
,
21882 unsigned int xnargs
,
21884 unification_kind_t strict
,
21885 vec
<deferred_access_check
, va_gc
> **checks
,
21890 int ntparms
= TREE_VEC_LENGTH (tparms
);
21891 int saw_undeduced
= 0;
21894 unsigned int nargs
;
21897 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21898 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21899 gcc_assert (ntparms
> 0);
21901 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21903 /* Reset the number of non-defaulted template arguments contained
21905 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21913 while (parms
&& parms
!= void_list_node
21916 parm
= TREE_VALUE (parms
);
21918 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21919 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21920 /* For a function parameter pack that occurs at the end of the
21921 parameter-declaration-list, the type A of each remaining
21922 argument of the call is compared with the type P of the
21923 declarator-id of the function parameter pack. */
21926 parms
= TREE_CHAIN (parms
);
21928 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21929 /* For a function parameter pack that does not occur at the
21930 end of the parameter-declaration-list, the type of the
21931 parameter pack is a non-deduced context. */
21937 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21943 && parms
!= void_list_node
21944 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21946 /* Unify the remaining arguments with the pack expansion type. */
21948 tree parmvec
= make_tree_vec (1);
21950 /* Allocate a TREE_VEC and copy in all of the arguments */
21951 argvec
= make_tree_vec (nargs
- ia
);
21952 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21953 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21955 /* Copy the parameter into parmvec. */
21956 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21957 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21958 /*subr=*/subr
, explain_p
))
21961 /* Advance to the end of the list of parameters. */
21962 parms
= TREE_CHAIN (parms
);
21965 /* Fail if we've reached the end of the parm list, and more args
21966 are present, and the parm list isn't variadic. */
21967 if (ia
< nargs
&& parms
== void_list_node
)
21968 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21969 /* Fail if parms are left and they don't have default values and
21970 they aren't all deduced as empty packs (c++/57397). This is
21971 consistent with sufficient_parms_p. */
21972 if (parms
&& parms
!= void_list_node
21973 && TREE_PURPOSE (parms
) == NULL_TREE
)
21975 unsigned int count
= nargs
;
21980 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21983 p
= TREE_CHAIN (p
);
21985 while (p
&& p
!= void_list_node
);
21986 if (count
!= nargs
)
21987 return unify_too_few_arguments (explain_p
, ia
, count
,
21993 tsubst_flags_t complain
= (explain_p
21994 ? tf_warning_or_error
21996 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21998 for (i
= 0; i
< ntparms
; i
++)
22000 tree targ
= TREE_VEC_ELT (targs
, i
);
22001 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22003 /* Clear the "incomplete" flags on all argument packs now so that
22004 substituting them into later default arguments works. */
22005 if (targ
&& ARGUMENT_PACK_P (targ
))
22007 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22008 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22011 if (targ
|| tparm
== error_mark_node
)
22013 tparm
= TREE_VALUE (tparm
);
22015 if (TREE_CODE (tparm
) == TYPE_DECL
22016 && !tried_array_deduction
)
22018 try_array_deduction (tparms
, targs
, xparms
);
22019 tried_array_deduction
= true;
22020 if (TREE_VEC_ELT (targs
, i
))
22024 /* If this is an undeduced nontype parameter that depends on
22025 a type parameter, try another pass; its type may have been
22026 deduced from a later argument than the one from which
22027 this parameter can be deduced. */
22028 if (TREE_CODE (tparm
) == PARM_DECL
22029 && uses_template_parms (TREE_TYPE (tparm
))
22030 && saw_undeduced
< 2)
22036 /* Core issue #226 (C++0x) [temp.deduct]:
22038 If a template argument has not been deduced, its
22039 default template argument, if any, is used.
22041 When we are in C++98 mode, TREE_PURPOSE will either
22042 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22043 to explicitly check cxx_dialect here. */
22044 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22045 /* OK, there is a default argument. Wait until after the
22046 conversion check to do substitution. */
22049 /* If the type parameter is a parameter pack, then it will
22050 be deduced to an empty parameter pack. */
22051 if (template_parameter_pack_p (tparm
))
22055 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22057 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22058 TREE_CONSTANT (arg
) = 1;
22061 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22063 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22065 TREE_VEC_ELT (targs
, i
) = arg
;
22069 return unify_parameter_deduction_failure (explain_p
, tparm
);
22072 /* Now substitute into the default template arguments. */
22073 for (i
= 0; i
< ntparms
; i
++)
22075 tree targ
= TREE_VEC_ELT (targs
, i
);
22076 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22078 if (targ
|| tparm
== error_mark_node
)
22080 tree parm
= TREE_VALUE (tparm
);
22081 tree arg
= TREE_PURPOSE (tparm
);
22082 reopen_deferring_access_checks (*checks
);
22083 location_t save_loc
= input_location
;
22085 input_location
= DECL_SOURCE_LOCATION (parm
);
22087 if (saw_undeduced
== 1
22088 && TREE_CODE (parm
) == PARM_DECL
22089 && uses_template_parms (TREE_TYPE (parm
)))
22091 /* The type of this non-type parameter depends on undeduced
22092 parameters. Don't try to use its default argument yet,
22093 since we might deduce an argument for it on the next pass,
22094 but do check whether the arguments we already have cause
22095 substitution failure, so that that happens before we try
22096 later default arguments (78489). */
22097 ++processing_template_decl
;
22098 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22100 --processing_template_decl
;
22101 if (type
== error_mark_node
)
22102 arg
= error_mark_node
;
22108 /* Even if the call is happening in template context, getting
22109 here means it's non-dependent, and a default argument is
22110 considered a separate definition under [temp.decls], so we can
22111 do this substitution without processing_template_decl. This
22112 is important if the default argument contains something that
22113 might be instantiation-dependent like access (87480). */
22114 processing_template_decl_sentinel s
;
22115 tree substed
= NULL_TREE
;
22116 if (saw_undeduced
== 1)
22118 /* First instatiate in template context, in case we still
22119 depend on undeduced template parameters. */
22120 ++processing_template_decl
;
22121 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22123 --processing_template_decl
;
22124 if (substed
!= error_mark_node
22125 && !uses_template_parms (substed
))
22126 /* We replaced all the tparms, substitute again out of
22127 template context. */
22128 substed
= NULL_TREE
;
22131 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22134 if (!uses_template_parms (substed
))
22135 arg
= convert_template_argument (parm
, substed
, full_targs
,
22136 complain
, i
, NULL_TREE
);
22137 else if (saw_undeduced
== 1)
22140 arg
= error_mark_node
;
22143 input_location
= save_loc
;
22144 *checks
= get_deferred_access_checks ();
22145 pop_deferring_access_checks ();
22147 if (arg
== error_mark_node
)
22151 TREE_VEC_ELT (targs
, i
) = arg
;
22152 /* The position of the first default template argument,
22153 is also the number of non-defaulted arguments in TARGS.
22155 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22156 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22160 if (saw_undeduced
++ == 1)
22164 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22165 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22167 return unify_success (explain_p
);
22170 /* Subroutine of type_unification_real. Args are like the variables
22171 at the call site. ARG is an overloaded function (or template-id);
22172 we try deducing template args from each of the overloads, and if
22173 only one succeeds, we go with that. Modifies TARGS and returns
22174 true on success. */
22177 resolve_overloaded_unification (tree tparms
,
22181 unification_kind_t strict
,
22185 tree tempargs
= copy_node (targs
);
22187 tree goodfn
= NULL_TREE
;
22190 if (TREE_CODE (arg
) == ADDR_EXPR
)
22192 arg
= TREE_OPERAND (arg
, 0);
22198 if (TREE_CODE (arg
) == COMPONENT_REF
)
22199 /* Handle `&x' where `x' is some static or non-static member
22201 arg
= TREE_OPERAND (arg
, 1);
22203 if (TREE_CODE (arg
) == OFFSET_REF
)
22204 arg
= TREE_OPERAND (arg
, 1);
22206 /* Strip baselink information. */
22207 if (BASELINK_P (arg
))
22208 arg
= BASELINK_FUNCTIONS (arg
);
22210 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22212 /* If we got some explicit template args, we need to plug them into
22213 the affected templates before we try to unify, in case the
22214 explicit args will completely resolve the templates in question. */
22217 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22218 arg
= TREE_OPERAND (arg
, 0);
22220 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22223 tree subargs
, elem
;
22225 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22228 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22229 expl_subargs
, NULL_TREE
, tf_none
,
22230 /*require_all_args=*/true,
22231 /*use_default_args=*/true);
22232 if (subargs
!= error_mark_node
22233 && !any_dependent_template_arguments_p (subargs
))
22235 fn
= instantiate_template (fn
, subargs
, tf_none
);
22236 if (!constraints_satisfied_p (fn
))
22238 if (undeduced_auto_decl (fn
))
22240 /* Instantiate the function to deduce its return type. */
22242 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22246 elem
= TREE_TYPE (fn
);
22247 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22248 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22249 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22258 /* If no templates (or more than one) are fully resolved by the
22259 explicit arguments, this template-id is a non-deduced context; it
22260 could still be OK if we deduce all template arguments for the
22261 enclosing call through other arguments. */
22265 else if (!OVL_P (arg
))
22266 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22267 -- but the deduction does not succeed because the expression is
22268 not just the function on its own. */
22271 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22274 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22275 strict
, sub_strict
, addr_p
, explain_p
)
22276 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22283 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22284 to function or pointer to member function argument if the set of
22285 overloaded functions does not contain function templates and at most
22286 one of a set of overloaded functions provides a unique match.
22288 So if we found multiple possibilities, we return success but don't
22289 deduce anything. */
22293 int i
= TREE_VEC_LENGTH (targs
);
22295 if (TREE_VEC_ELT (tempargs
, i
))
22297 tree old
= TREE_VEC_ELT (targs
, i
);
22298 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22299 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22300 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22301 /* Don't forget explicit template arguments in a pack. */
22302 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22303 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22304 TREE_VEC_ELT (targs
, i
) = new_
;
22313 /* Core DR 115: In contexts where deduction is done and fails, or in
22314 contexts where deduction is not done, if a template argument list is
22315 specified and it, along with any default template arguments, identifies
22316 a single function template specialization, then the template-id is an
22317 lvalue for the function template specialization. */
22320 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22322 tree expr
, offset
, baselink
;
22325 if (!type_unknown_p (orig_expr
))
22330 offset
= NULL_TREE
;
22331 baselink
= NULL_TREE
;
22333 if (TREE_CODE (expr
) == ADDR_EXPR
)
22335 expr
= TREE_OPERAND (expr
, 0);
22338 if (TREE_CODE (expr
) == OFFSET_REF
)
22341 expr
= TREE_OPERAND (expr
, 1);
22343 if (BASELINK_P (expr
))
22346 expr
= BASELINK_FUNCTIONS (expr
);
22349 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22352 tree goodfn
= NULL_TREE
;
22354 /* If we got some explicit template args, we need to plug them into
22355 the affected templates before we try to unify, in case the
22356 explicit args will completely resolve the templates in question. */
22358 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22359 tree arg
= TREE_OPERAND (expr
, 0);
22360 tree badfn
= NULL_TREE
;
22361 tree badargs
= NULL_TREE
;
22363 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22366 tree subargs
, elem
;
22368 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22371 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22372 expl_subargs
, NULL_TREE
, tf_none
,
22373 /*require_all_args=*/true,
22374 /*use_default_args=*/true);
22375 if (subargs
!= error_mark_node
22376 && !any_dependent_template_arguments_p (subargs
))
22378 elem
= instantiate_template (fn
, subargs
, tf_none
);
22379 if (elem
== error_mark_node
)
22384 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22385 && constraints_satisfied_p (elem
))
22394 mark_used (goodfn
);
22397 expr
= build_baselink (BASELINK_BINFO (baselink
),
22398 BASELINK_ACCESS_BINFO (baselink
),
22399 expr
, BASELINK_OPTYPE (baselink
));
22403 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22404 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22407 expr
= cp_build_addr_expr (expr
, complain
);
22410 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22411 /* There were no good options and at least one bad one, so let the
22412 user know what the problem is. */
22413 instantiate_template (badfn
, badargs
, complain
);
22418 /* As above, but error out if the expression remains overloaded. */
22421 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22423 exp
= resolve_nondeduced_context (exp
, complain
);
22424 if (type_unknown_p (exp
))
22426 if (complain
& tf_error
)
22427 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22428 return error_mark_node
;
22433 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22434 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22435 different overloads deduce different arguments for a given parm.
22436 ADDR_P is true if the expression for which deduction is being
22437 performed was of the form "& fn" rather than simply "fn".
22439 Returns 1 on success. */
22442 try_one_overload (tree tparms
,
22447 unification_kind_t strict
,
22456 if (arg
== error_mark_node
)
22459 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22460 to function or pointer to member function argument if the set of
22461 overloaded functions does not contain function templates and at most
22462 one of a set of overloaded functions provides a unique match.
22464 So if this is a template, just return success. */
22466 if (uses_template_parms (arg
))
22469 if (TREE_CODE (arg
) == METHOD_TYPE
)
22470 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22472 arg
= build_pointer_type (arg
);
22474 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22476 /* We don't copy orig_targs for this because if we have already deduced
22477 some template args from previous args, unify would complain when we
22478 try to deduce a template parameter for the same argument, even though
22479 there isn't really a conflict. */
22480 nargs
= TREE_VEC_LENGTH (targs
);
22481 tempargs
= make_tree_vec (nargs
);
22483 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22486 /* First make sure we didn't deduce anything that conflicts with
22487 explicitly specified args. */
22488 for (i
= nargs
; i
--; )
22490 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22491 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22495 else if (uses_template_parms (elt
))
22496 /* Since we're unifying against ourselves, we will fill in
22497 template args used in the function parm list with our own
22498 template parms. Discard them. */
22499 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22500 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22502 /* Check that the argument at each index of the deduced argument pack
22503 is equivalent to the corresponding explicitly specified argument.
22504 We may have deduced more arguments than were explicitly specified,
22507 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22508 that's wrong if we deduce the same argument pack from multiple
22509 function arguments: it's only incomplete the first time. */
22511 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22512 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22514 if (TREE_VEC_LENGTH (deduced_pack
)
22515 < TREE_VEC_LENGTH (explicit_pack
))
22518 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22519 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22520 TREE_VEC_ELT (deduced_pack
, j
)))
22523 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22527 for (i
= nargs
; i
--; )
22529 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22532 TREE_VEC_ELT (targs
, i
) = elt
;
22538 /* PARM is a template class (perhaps with unbound template
22539 parameters). ARG is a fully instantiated type. If ARG can be
22540 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22541 TARGS are as for unify. */
22544 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22547 tree copy_of_targs
;
22549 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22551 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22552 /* Matches anything. */;
22553 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22554 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22557 /* We need to make a new template argument vector for the call to
22558 unify. If we used TARGS, we'd clutter it up with the result of
22559 the attempted unification, even if this class didn't work out.
22560 We also don't want to commit ourselves to all the unifications
22561 we've already done, since unification is supposed to be done on
22562 an argument-by-argument basis. In other words, consider the
22563 following pathological case:
22565 template <int I, int J, int K>
22568 template <int I, int J>
22569 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22571 template <int I, int J, int K>
22572 void f(S<I, J, K>, S<I, I, I>);
22581 Now, by the time we consider the unification involving `s2', we
22582 already know that we must have `f<0, 0, 0>'. But, even though
22583 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22584 because there are two ways to unify base classes of S<0, 1, 2>
22585 with S<I, I, I>. If we kept the already deduced knowledge, we
22586 would reject the possibility I=1. */
22587 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22589 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22591 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22596 /* If unification failed, we're done. */
22597 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22598 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22604 /* Given a template type PARM and a class type ARG, find the unique
22605 base type in ARG that is an instance of PARM. We do not examine
22606 ARG itself; only its base-classes. If there is not exactly one
22607 appropriate base class, return NULL_TREE. PARM may be the type of
22608 a partial specialization, as well as a plain template type. Used
22611 static enum template_base_result
22612 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22613 bool explain_p
, tree
*result
)
22615 tree rval
= NULL_TREE
;
22618 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22620 binfo
= TYPE_BINFO (complete_type (arg
));
22623 /* The type could not be completed. */
22624 *result
= NULL_TREE
;
22625 return tbr_incomplete_type
;
22628 /* Walk in inheritance graph order. The search order is not
22629 important, and this avoids multiple walks of virtual bases. */
22630 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22632 tree r
= try_class_unification (tparms
, targs
, parm
,
22633 BINFO_TYPE (binfo
), explain_p
);
22637 /* If there is more than one satisfactory baseclass, then:
22641 If they yield more than one possible deduced A, the type
22645 if (rval
&& !same_type_p (r
, rval
))
22647 *result
= NULL_TREE
;
22648 return tbr_ambiguous_baseclass
;
22656 return tbr_success
;
22659 /* Returns the level of DECL, which declares a template parameter. */
22662 template_decl_level (tree decl
)
22664 switch (TREE_CODE (decl
))
22667 case TEMPLATE_DECL
:
22668 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22671 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22674 gcc_unreachable ();
22679 /* Decide whether ARG can be unified with PARM, considering only the
22680 cv-qualifiers of each type, given STRICT as documented for unify.
22681 Returns nonzero iff the unification is OK on that basis. */
22684 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22686 int arg_quals
= cp_type_quals (arg
);
22687 int parm_quals
= cp_type_quals (parm
);
22689 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22690 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22692 /* Although a CVR qualifier is ignored when being applied to a
22693 substituted template parameter ([8.3.2]/1 for example), that
22694 does not allow us to unify "const T" with "int&" because both
22695 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22696 It is ok when we're allowing additional CV qualifiers
22697 at the outer level [14.8.2.1]/3,1st bullet. */
22698 if ((TYPE_REF_P (arg
)
22699 || FUNC_OR_METHOD_TYPE_P (arg
))
22700 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22703 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22704 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22708 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22709 && (arg_quals
& parm_quals
) != parm_quals
)
22712 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22713 && (parm_quals
& arg_quals
) != arg_quals
)
22719 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22721 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22723 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22724 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22725 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22727 *index
= TEMPLATE_TYPE_IDX (parm
);
22728 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22732 *index
= TEMPLATE_PARM_IDX (parm
);
22733 *level
= TEMPLATE_PARM_LEVEL (parm
);
22737 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22739 if (unify (TP, TA, P, A, S, EP)) \
22743 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22744 expansion at the end of PACKED_PARMS. Returns 0 if the type
22745 deduction succeeds, 1 otherwise. STRICT is the same as in
22746 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22747 function call argument list. We'll need to adjust the arguments to make them
22748 types. SUBR tells us if this is from a recursive call to
22749 type_unification_real, or for comparing two template argument
22753 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22754 tree packed_args
, unification_kind_t strict
,
22755 bool subr
, bool explain_p
)
22758 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22759 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22760 tree pack
, packs
= NULL_TREE
;
22761 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22763 /* Add in any args remembered from an earlier partial instantiation. */
22764 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22765 int levels
= TMPL_ARGS_DEPTH (targs
);
22767 packed_args
= expand_template_argument_pack (packed_args
);
22769 int len
= TREE_VEC_LENGTH (packed_args
);
22771 /* Determine the parameter packs we will be deducing from the
22772 pattern, and record their current deductions. */
22773 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22774 pack
; pack
= TREE_CHAIN (pack
))
22776 tree parm_pack
= TREE_VALUE (pack
);
22779 /* Only template parameter packs can be deduced, not e.g. function
22780 parameter packs or __bases or __integer_pack. */
22781 if (!TEMPLATE_PARM_P (parm_pack
))
22784 /* Determine the index and level of this parameter pack. */
22785 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22786 if (level
< levels
)
22789 /* Keep track of the parameter packs and their corresponding
22791 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22792 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22795 /* Loop through all of the arguments that have not yet been
22796 unified and unify each with the pattern. */
22797 for (i
= start
; i
< len
; i
++)
22800 bool any_explicit
= false;
22801 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22803 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22804 or the element of its argument pack at the current index if
22805 this argument was explicitly specified. */
22806 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22810 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22813 if (TREE_VALUE (pack
)
22814 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22815 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22817 any_explicit
= true;
22818 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22820 TMPL_ARG (targs
, level
, idx
) = arg
;
22823 /* If we had explicit template arguments, substitute them into the
22824 pattern before deduction. */
22827 /* Some arguments might still be unspecified or dependent. */
22829 ++processing_template_decl
;
22830 dependent
= any_dependent_template_arguments_p (targs
);
22832 --processing_template_decl
;
22833 parm
= tsubst (pattern
, targs
,
22834 explain_p
? tf_warning_or_error
: tf_none
,
22837 --processing_template_decl
;
22838 if (parm
== error_mark_node
)
22844 /* Unify the pattern with the current argument. */
22845 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22849 /* For each parameter pack, collect the deduced value. */
22850 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22853 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22855 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22856 TMPL_ARG (targs
, level
, idx
);
22860 /* Verify that the results of unification with the parameter packs
22861 produce results consistent with what we've seen before, and make
22862 the deduced argument packs available. */
22863 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22865 tree old_pack
= TREE_VALUE (pack
);
22866 tree new_args
= TREE_TYPE (pack
);
22867 int i
, len
= TREE_VEC_LENGTH (new_args
);
22869 bool nondeduced_p
= false;
22871 /* By default keep the original deduced argument pack.
22872 If necessary, more specific code is going to update the
22873 resulting deduced argument later down in this function. */
22874 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22875 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22877 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22878 actually deduce anything. */
22879 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22880 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22881 nondeduced_p
= true;
22885 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22887 /* If we had fewer function args than explicit template args,
22888 just use the explicits. */
22889 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22890 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22891 if (len
< explicit_len
)
22892 new_args
= explicit_args
;
22898 /* Build the deduced *_ARGUMENT_PACK. */
22899 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22901 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22902 TREE_CONSTANT (result
) = 1;
22905 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22907 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22909 /* Note the deduced argument packs for this parameter
22911 TMPL_ARG (targs
, level
, idx
) = result
;
22913 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22914 && (ARGUMENT_PACK_ARGS (old_pack
)
22915 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22917 /* We only had the explicitly-provided arguments before, but
22918 now we have a complete set of arguments. */
22919 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22921 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22922 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22923 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22927 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22928 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22929 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22930 /* During template argument deduction for the aggregate deduction
22931 candidate, the number of elements in a trailing parameter pack
22932 is only deduced from the number of remaining function
22933 arguments if it is not otherwise deduced. */
22934 if (cxx_dialect
>= cxx20
22935 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
22936 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
22937 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
22938 if (!comp_template_args (old_args
, new_args
,
22939 &bad_old_arg
, &bad_new_arg
))
22940 /* Inconsistent unification of this parameter pack. */
22941 return unify_parameter_pack_inconsistent (explain_p
,
22947 return unify_success (explain_p
);
22950 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22951 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22952 parameters and return value are as for unify. */
22955 unify_array_domain (tree tparms
, tree targs
,
22956 tree parm_dom
, tree arg_dom
,
22964 /* Our representation of array types uses "N - 1" as the
22965 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22966 not an integer constant. We cannot unify arbitrarily
22967 complex expressions, so we eliminate the MINUS_EXPRs
22969 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22970 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22973 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22974 parm_max
= TREE_OPERAND (parm_max
, 0);
22976 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22977 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22980 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22981 trying to unify the type of a variable with the type
22982 of a template parameter. For example:
22984 template <unsigned int N>
22985 void f (char (&) [N]);
22992 Here, the type of the ARG will be "int [g(i)]", and
22993 may be a SAVE_EXPR, etc. */
22994 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22995 return unify_vla_arg (explain_p
, arg_dom
);
22996 arg_max
= TREE_OPERAND (arg_max
, 0);
22999 /* If only one of the bounds used a MINUS_EXPR, compensate
23000 by adding one to the other bound. */
23001 if (parm_cst
&& !arg_cst
)
23002 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23006 else if (arg_cst
&& !parm_cst
)
23007 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23012 return unify (tparms
, targs
, parm_max
, arg_max
,
23013 UNIFY_ALLOW_INTEGER
, explain_p
);
23016 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23018 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23023 if (PACK_EXPANSION_P (t
))
23024 t
= PACK_EXPANSION_PATTERN (t
);
23025 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23026 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23027 || DECL_TYPE_TEMPLATE_P (t
))
23029 else if (TYPE_P (t
))
23035 /* Deduce the value of template parameters. TPARMS is the (innermost)
23036 set of template parameters to a template. TARGS is the bindings
23037 for those template parameters, as determined thus far; TARGS may
23038 include template arguments for outer levels of template parameters
23039 as well. PARM is a parameter to a template function, or a
23040 subcomponent of that parameter; ARG is the corresponding argument.
23041 This function attempts to match PARM with ARG in a manner
23042 consistent with the existing assignments in TARGS. If more values
23043 are deduced, then TARGS is updated.
23045 Returns 0 if the type deduction succeeds, 1 otherwise. The
23046 parameter STRICT is a bitwise or of the following flags:
23049 Require an exact match between PARM and ARG.
23050 UNIFY_ALLOW_MORE_CV_QUAL:
23051 Allow the deduced ARG to be more cv-qualified (by qualification
23052 conversion) than ARG.
23053 UNIFY_ALLOW_LESS_CV_QUAL:
23054 Allow the deduced ARG to be less cv-qualified than ARG.
23055 UNIFY_ALLOW_DERIVED:
23056 Allow the deduced ARG to be a template base class of ARG,
23057 or a pointer to a template base class of the type pointed to by
23059 UNIFY_ALLOW_INTEGER:
23060 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23061 case for more information.
23062 UNIFY_ALLOW_OUTER_LEVEL:
23063 This is the outermost level of a deduction. Used to determine validity
23064 of qualification conversions. A valid qualification conversion must
23065 have const qualified pointers leading up to the inner type which
23066 requires additional CV quals, except at the outer level, where const
23067 is not required [conv.qual]. It would be normal to set this flag in
23068 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23069 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23070 This is the outermost level of a deduction, and PARM can be more CV
23071 qualified at this point.
23072 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23073 This is the outermost level of a deduction, and PARM can be less CV
23074 qualified at this point. */
23077 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23083 int strict_in
= strict
;
23084 tsubst_flags_t complain
= (explain_p
23085 ? tf_warning_or_error
23088 /* I don't think this will do the right thing with respect to types.
23089 But the only case I've seen it in so far has been array bounds, where
23090 signedness is the only information lost, and I think that will be
23091 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23092 finish_id_expression_1, and are also OK. */
23093 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23094 parm
= TREE_OPERAND (parm
, 0);
23096 if (arg
== error_mark_node
)
23097 return unify_invalid (explain_p
);
23098 if (arg
== unknown_type_node
23099 || arg
== init_list_type_node
)
23100 /* We can't deduce anything from this, but we might get all the
23101 template args from other function args. */
23102 return unify_success (explain_p
);
23104 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23105 return unify_success (explain_p
);
23107 /* If PARM uses template parameters, then we can't bail out here,
23108 even if ARG == PARM, since we won't record unifications for the
23109 template parameters. We might need them if we're trying to
23110 figure out which of two things is more specialized. */
23111 if (arg
== parm
&& !uses_template_parms (parm
))
23112 return unify_success (explain_p
);
23114 /* Handle init lists early, so the rest of the function can assume
23115 we're dealing with a type. */
23116 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23120 tree orig_parm
= parm
;
23122 if (!is_std_init_list (parm
)
23123 && TREE_CODE (parm
) != ARRAY_TYPE
)
23124 /* We can only deduce from an initializer list argument if the
23125 parameter is std::initializer_list or an array; otherwise this
23126 is a non-deduced context. */
23127 return unify_success (explain_p
);
23129 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23130 elttype
= TREE_TYPE (parm
);
23133 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23134 /* Deduction is defined in terms of a single type, so just punt
23135 on the (bizarre) std::initializer_list<T...>. */
23136 if (PACK_EXPANSION_P (elttype
))
23137 return unify_success (explain_p
);
23140 if (strict
!= DEDUCE_EXACT
23141 && TYPE_P (elttype
)
23142 && !uses_deducible_template_parms (elttype
))
23143 /* If ELTTYPE has no deducible template parms, skip deduction from
23144 the list elements. */;
23146 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23148 int elt_strict
= strict
;
23150 if (elt
== error_mark_node
)
23151 return unify_invalid (explain_p
);
23153 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23155 tree type
= TREE_TYPE (elt
);
23156 if (type
== error_mark_node
)
23157 return unify_invalid (explain_p
);
23158 /* It should only be possible to get here for a call. */
23159 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23160 elt_strict
|= maybe_adjust_types_for_deduction
23161 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23165 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23169 if (TREE_CODE (parm
) == ARRAY_TYPE
23170 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23172 /* Also deduce from the length of the initializer list. */
23173 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23174 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23175 if (idx
== error_mark_node
)
23176 return unify_invalid (explain_p
);
23177 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23181 /* If the std::initializer_list<T> deduction worked, replace the
23182 deduced A with std::initializer_list<A>. */
23183 if (orig_parm
!= parm
)
23185 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23186 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23187 targ
= listify (targ
);
23188 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23190 return unify_success (explain_p
);
23193 /* If parm and arg aren't the same kind of thing (template, type, or
23194 expression), fail early. */
23195 if (pa_kind (parm
) != pa_kind (arg
))
23196 return unify_invalid (explain_p
);
23198 /* Immediately reject some pairs that won't unify because of
23199 cv-qualification mismatches. */
23200 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23202 /* It is the elements of the array which hold the cv quals of an array
23203 type, and the elements might be template type parms. We'll check
23204 when we recurse. */
23205 && TREE_CODE (arg
) != ARRAY_TYPE
23206 /* We check the cv-qualifiers when unifying with template type
23207 parameters below. We want to allow ARG `const T' to unify with
23208 PARM `T' for example, when computing which of two templates
23209 is more specialized, for example. */
23210 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23211 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23212 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23214 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23215 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23216 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23217 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23218 strict
&= ~UNIFY_ALLOW_DERIVED
;
23219 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23220 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23222 switch (TREE_CODE (parm
))
23224 case TYPENAME_TYPE
:
23226 case UNBOUND_CLASS_TEMPLATE
:
23227 /* In a type which contains a nested-name-specifier, template
23228 argument values cannot be deduced for template parameters used
23229 within the nested-name-specifier. */
23230 return unify_success (explain_p
);
23232 case TEMPLATE_TYPE_PARM
:
23233 case TEMPLATE_TEMPLATE_PARM
:
23234 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23235 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23236 if (error_operand_p (tparm
))
23237 return unify_invalid (explain_p
);
23239 if (TEMPLATE_TYPE_LEVEL (parm
)
23240 != template_decl_level (tparm
))
23241 /* The PARM is not one we're trying to unify. Just check
23242 to see if it matches ARG. */
23244 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23245 && (is_auto (parm
) ? is_auto (arg
)
23246 : same_type_p (parm
, arg
)))
23247 return unify_success (explain_p
);
23249 return unify_type_mismatch (explain_p
, parm
, arg
);
23251 idx
= TEMPLATE_TYPE_IDX (parm
);
23252 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23253 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23254 if (error_operand_p (tparm
))
23255 return unify_invalid (explain_p
);
23257 /* Check for mixed types and values. */
23258 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23259 && TREE_CODE (tparm
) != TYPE_DECL
)
23260 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23261 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23262 gcc_unreachable ();
23264 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23266 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23267 && CLASS_TYPE_P (arg
))
23269 /* First try to match ARG directly. */
23270 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23274 /* Otherwise, look for a suitable base of ARG, as below. */
23275 enum template_base_result r
;
23276 r
= get_template_base (tparms
, targs
, parm
, arg
,
23279 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23283 /* ARG must be constructed from a template class or a template
23284 template parameter. */
23285 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23286 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23287 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23289 /* Deduce arguments T, i from TT<T> or TT<i>. */
23290 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23293 arg
= TYPE_TI_TEMPLATE (arg
);
23295 /* Fall through to deduce template name. */
23298 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23299 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23301 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23303 /* Simple cases: Value already set, does match or doesn't. */
23304 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23305 return unify_success (explain_p
);
23307 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23311 /* If PARM is `const T' and ARG is only `int', we don't have
23312 a match unless we are allowing additional qualification.
23313 If ARG is `const int' and PARM is just `T' that's OK;
23314 that binds `const int' to `T'. */
23315 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23317 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23319 /* Consider the case where ARG is `const volatile int' and
23320 PARM is `const T'. Then, T should be `volatile int'. */
23321 arg
= cp_build_qualified_type_real
23322 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23323 if (arg
== error_mark_node
)
23324 return unify_invalid (explain_p
);
23326 /* Simple cases: Value already set, does match or doesn't. */
23327 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23328 return unify_success (explain_p
);
23330 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23332 /* Make sure that ARG is not a variable-sized array. (Note
23333 that were talking about variable-sized arrays (like
23334 `int[n]'), rather than arrays of unknown size (like
23335 `int[]').) We'll get very confused by such a type since
23336 the bound of the array is not constant, and therefore
23337 not mangleable. Besides, such types are not allowed in
23338 ISO C++, so we can do as we please here. We do allow
23339 them for 'auto' deduction, since that isn't ABI-exposed. */
23340 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23341 return unify_vla_arg (explain_p
, arg
);
23343 /* Strip typedefs as in convert_template_argument. */
23344 arg
= canonicalize_type_argument (arg
, tf_none
);
23347 /* If ARG is a parameter pack or an expansion, we cannot unify
23348 against it unless PARM is also a parameter pack. */
23349 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23350 && !template_parameter_pack_p (parm
))
23351 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23353 /* If the argument deduction results is a METHOD_TYPE,
23354 then there is a problem.
23355 METHOD_TYPE doesn't map to any real C++ type the result of
23356 the deduction cannot be of that type. */
23357 if (TREE_CODE (arg
) == METHOD_TYPE
)
23358 return unify_method_type_error (explain_p
, arg
);
23360 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23361 return unify_success (explain_p
);
23363 case TEMPLATE_PARM_INDEX
:
23364 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23365 if (error_operand_p (tparm
))
23366 return unify_invalid (explain_p
);
23368 if (TEMPLATE_PARM_LEVEL (parm
)
23369 != template_decl_level (tparm
))
23371 /* The PARM is not one we're trying to unify. Just check
23372 to see if it matches ARG. */
23373 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23374 && cp_tree_equal (parm
, arg
));
23376 unify_expression_unequal (explain_p
, parm
, arg
);
23380 idx
= TEMPLATE_PARM_IDX (parm
);
23381 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23385 if ((strict
& UNIFY_ALLOW_INTEGER
)
23386 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23387 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23388 /* We're deducing from an array bound, the type doesn't matter. */
23389 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23390 int x
= !cp_tree_equal (targ
, arg
);
23392 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23396 /* [temp.deduct.type] If, in the declaration of a function template
23397 with a non-type template-parameter, the non-type
23398 template-parameter is used in an expression in the function
23399 parameter-list and, if the corresponding template-argument is
23400 deduced, the template-argument type shall match the type of the
23401 template-parameter exactly, except that a template-argument
23402 deduced from an array bound may be of any integral type.
23403 The non-type parameter might use already deduced type parameters. */
23404 tparm
= TREE_TYPE (parm
);
23405 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23406 /* We don't have enough levels of args to do any substitution. This
23407 can happen in the context of -fnew-ttp-matching. */;
23410 ++processing_template_decl
;
23411 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23412 --processing_template_decl
;
23414 if (tree a
= type_uses_auto (tparm
))
23416 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23417 if (tparm
== error_mark_node
)
23422 if (!TREE_TYPE (arg
))
23423 /* Template-parameter dependent expression. Just accept it for now.
23424 It will later be processed in convert_template_argument. */
23426 else if (same_type_ignoring_top_level_qualifiers_p
23427 (non_reference (TREE_TYPE (arg
)),
23428 non_reference (tparm
)))
23429 /* OK. Ignore top-level quals here because a class-type template
23430 parameter object is const. */;
23431 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23432 && CP_INTEGRAL_TYPE_P (tparm
))
23433 /* Convert the ARG to the type of PARM; the deduced non-type
23434 template argument must exactly match the types of the
23435 corresponding parameter. */
23436 arg
= fold (build_nop (tparm
, arg
));
23437 else if (uses_template_parms (tparm
))
23439 /* We haven't deduced the type of this parameter yet. */
23440 if (cxx_dialect
>= cxx17
23441 /* We deduce from array bounds in try_array_deduction. */
23442 && !(strict
& UNIFY_ALLOW_INTEGER
))
23444 /* Deduce it from the non-type argument. */
23445 tree atype
= TREE_TYPE (arg
);
23446 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23448 UNIFY_ALLOW_NONE
, explain_p
);
23451 /* Try again later. */
23452 return unify_success (explain_p
);
23455 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23457 /* If ARG is a parameter pack or an expansion, we cannot unify
23458 against it unless PARM is also a parameter pack. */
23459 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23460 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23461 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23464 bool removed_attr
= false;
23465 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23467 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23468 return unify_success (explain_p
);
23472 /* A pointer-to-member constant can be unified only with
23473 another constant. */
23474 if (TREE_CODE (arg
) != PTRMEM_CST
)
23475 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23477 /* Just unify the class member. It would be useless (and possibly
23478 wrong, depending on the strict flags) to unify also
23479 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23480 arg refer to the same variable, even if through different
23481 classes. For instance:
23483 struct A { int x; };
23486 Unification of &A::x and &B::x must succeed. */
23487 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23488 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23493 if (!TYPE_PTR_P (arg
))
23494 return unify_type_mismatch (explain_p
, parm
, arg
);
23496 /* [temp.deduct.call]
23498 A can be another pointer or pointer to member type that can
23499 be converted to the deduced A via a qualification
23500 conversion (_conv.qual_).
23502 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23503 This will allow for additional cv-qualification of the
23504 pointed-to types if appropriate. */
23506 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23507 /* The derived-to-base conversion only persists through one
23508 level of pointers. */
23509 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23511 return unify (tparms
, targs
, TREE_TYPE (parm
),
23512 TREE_TYPE (arg
), strict
, explain_p
);
23515 case REFERENCE_TYPE
:
23516 if (!TYPE_REF_P (arg
))
23517 return unify_type_mismatch (explain_p
, parm
, arg
);
23518 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23519 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23522 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23523 return unify_type_mismatch (explain_p
, parm
, arg
);
23524 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23525 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23526 return unify_type_mismatch (explain_p
, parm
, arg
);
23527 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23528 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23529 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23530 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23531 TYPE_DOMAIN (arg
), explain_p
);
23532 return unify_success (explain_p
);
23539 case ENUMERAL_TYPE
:
23542 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23543 return unify_type_mismatch (explain_p
, parm
, arg
);
23545 /* We have already checked cv-qualification at the top of the
23547 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23548 return unify_type_mismatch (explain_p
, parm
, arg
);
23550 /* As far as unification is concerned, this wins. Later checks
23551 will invalidate it if necessary. */
23552 return unify_success (explain_p
);
23554 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23555 /* Type INTEGER_CST can come from ordinary constant template args. */
23557 while (CONVERT_EXPR_P (arg
))
23558 arg
= TREE_OPERAND (arg
, 0);
23560 if (TREE_CODE (arg
) != INTEGER_CST
)
23561 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23562 return (tree_int_cst_equal (parm
, arg
)
23563 ? unify_success (explain_p
)
23564 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23568 int i
, len
, argslen
;
23569 int parm_variadic_p
= 0;
23571 if (TREE_CODE (arg
) != TREE_VEC
)
23572 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23574 len
= TREE_VEC_LENGTH (parm
);
23575 argslen
= TREE_VEC_LENGTH (arg
);
23577 /* Check for pack expansions in the parameters. */
23578 for (i
= 0; i
< len
; ++i
)
23580 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23583 /* We can unify against something with a trailing
23585 parm_variadic_p
= 1;
23587 /* [temp.deduct.type]/9: If the template argument list of
23588 P contains a pack expansion that is not the last
23589 template argument, the entire template argument list
23590 is a non-deduced context. */
23591 return unify_success (explain_p
);
23595 /* If we don't have enough arguments to satisfy the parameters
23596 (not counting the pack expression at the end), or we have
23597 too many arguments for a parameter list that doesn't end in
23598 a pack expression, we can't unify. */
23599 if (parm_variadic_p
23600 ? argslen
< len
- parm_variadic_p
23602 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23604 /* Unify all of the parameters that precede the (optional)
23605 pack expression. */
23606 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23608 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23609 TREE_VEC_ELT (parm
, i
),
23610 TREE_VEC_ELT (arg
, i
),
23611 UNIFY_ALLOW_NONE
, explain_p
);
23613 if (parm_variadic_p
)
23614 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23616 /*subr=*/true, explain_p
);
23617 return unify_success (explain_p
);
23622 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23623 return unify_type_mismatch (explain_p
, parm
, arg
);
23625 if (TYPE_PTRMEMFUNC_P (parm
))
23627 if (!TYPE_PTRMEMFUNC_P (arg
))
23628 return unify_type_mismatch (explain_p
, parm
, arg
);
23630 return unify (tparms
, targs
,
23631 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23632 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23633 strict
, explain_p
);
23635 else if (TYPE_PTRMEMFUNC_P (arg
))
23636 return unify_type_mismatch (explain_p
, parm
, arg
);
23638 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23640 tree t
= NULL_TREE
;
23642 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23644 /* First, we try to unify the PARM and ARG directly. */
23645 t
= try_class_unification (tparms
, targs
,
23646 parm
, arg
, explain_p
);
23650 /* Fallback to the special case allowed in
23651 [temp.deduct.call]:
23653 If P is a class, and P has the form
23654 template-id, then A can be a derived class of
23655 the deduced A. Likewise, if P is a pointer to
23656 a class of the form template-id, A can be a
23657 pointer to a derived class pointed to by the
23659 enum template_base_result r
;
23660 r
= get_template_base (tparms
, targs
, parm
, arg
,
23665 /* Don't give the derived diagnostic if we're
23666 already dealing with the same template. */
23668 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23669 && (CLASSTYPE_TI_TEMPLATE (parm
)
23670 == CLASSTYPE_TI_TEMPLATE (arg
)));
23671 return unify_no_common_base (explain_p
&& !same_template
,
23676 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23677 && (CLASSTYPE_TI_TEMPLATE (parm
)
23678 == CLASSTYPE_TI_TEMPLATE (arg
)))
23679 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23680 Then, we should unify `int' and `U'. */
23683 /* There's no chance of unification succeeding. */
23684 return unify_type_mismatch (explain_p
, parm
, arg
);
23686 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23687 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23689 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23690 return unify_type_mismatch (explain_p
, parm
, arg
);
23691 return unify_success (explain_p
);
23694 case FUNCTION_TYPE
:
23696 unsigned int nargs
;
23701 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23702 return unify_type_mismatch (explain_p
, parm
, arg
);
23704 /* CV qualifications for methods can never be deduced, they must
23705 match exactly. We need to check them explicitly here,
23706 because type_unification_real treats them as any other
23707 cv-qualified parameter. */
23708 if (TREE_CODE (parm
) == METHOD_TYPE
23709 && (!check_cv_quals_for_unify
23711 class_of_this_parm (arg
),
23712 class_of_this_parm (parm
))))
23713 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23714 if (TREE_CODE (arg
) == FUNCTION_TYPE
23715 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23716 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23717 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23718 return unify_type_mismatch (explain_p
, parm
, arg
);
23720 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23721 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23723 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23724 args
= XALLOCAVEC (tree
, nargs
);
23725 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23726 a
!= NULL_TREE
&& a
!= void_list_node
;
23727 a
= TREE_CHAIN (a
), ++i
)
23728 args
[i
] = TREE_VALUE (a
);
23731 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23732 args
, nargs
, 1, DEDUCE_EXACT
,
23736 if (flag_noexcept_type
)
23738 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23739 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23740 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23741 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23742 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23743 && uses_template_parms (TREE_PURPOSE (pspec
)))
23744 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23745 TREE_PURPOSE (aspec
),
23746 UNIFY_ALLOW_NONE
, explain_p
);
23747 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23748 return unify_type_mismatch (explain_p
, parm
, arg
);
23755 /* Unify a pointer to member with a pointer to member function, which
23756 deduces the type of the member as a function type. */
23757 if (TYPE_PTRMEMFUNC_P (arg
))
23759 /* Check top-level cv qualifiers */
23760 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23761 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23763 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23764 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23765 UNIFY_ALLOW_NONE
, explain_p
);
23767 /* Determine the type of the function we are unifying against. */
23768 tree fntype
= static_fn_type (arg
);
23770 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23773 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23774 return unify_type_mismatch (explain_p
, parm
, arg
);
23775 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23776 TYPE_OFFSET_BASETYPE (arg
),
23777 UNIFY_ALLOW_NONE
, explain_p
);
23778 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23779 strict
, explain_p
);
23782 if (DECL_TEMPLATE_PARM_P (parm
))
23783 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23784 if (arg
!= scalar_constant_value (parm
))
23785 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23786 return unify_success (explain_p
);
23789 case TEMPLATE_DECL
:
23790 /* Matched cases are handled by the ARG == PARM test above. */
23791 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23794 /* We might get a variable as a non-type template argument in parm if the
23795 corresponding parameter is type-dependent. Make any necessary
23796 adjustments based on whether arg is a reference. */
23797 if (CONSTANT_CLASS_P (arg
))
23798 parm
= fold_non_dependent_expr (parm
, complain
);
23799 else if (REFERENCE_REF_P (arg
))
23801 tree sub
= TREE_OPERAND (arg
, 0);
23803 if (TREE_CODE (sub
) == ADDR_EXPR
)
23804 arg
= TREE_OPERAND (sub
, 0);
23806 /* Now use the normal expression code to check whether they match. */
23809 case TYPE_ARGUMENT_PACK
:
23810 case NONTYPE_ARGUMENT_PACK
:
23811 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23812 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23815 case DECLTYPE_TYPE
:
23816 case UNDERLYING_TYPE
:
23817 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23818 or UNDERLYING_TYPE nodes. */
23819 return unify_success (explain_p
);
23822 /* Unification fails if we hit an error node. */
23823 return unify_invalid (explain_p
);
23826 if (REFERENCE_REF_P (parm
))
23828 bool pexp
= PACK_EXPANSION_P (arg
);
23830 arg
= PACK_EXPANSION_PATTERN (arg
);
23831 if (REFERENCE_REF_P (arg
))
23832 arg
= TREE_OPERAND (arg
, 0);
23834 arg
= make_pack_expansion (arg
, complain
);
23835 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23836 strict
, explain_p
);
23841 /* An unresolved overload is a nondeduced context. */
23842 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23843 return unify_success (explain_p
);
23844 gcc_assert (EXPR_P (parm
)
23845 || COMPOUND_LITERAL_P (parm
)
23846 || TREE_CODE (parm
) == TRAIT_EXPR
);
23848 /* We must be looking at an expression. This can happen with
23852 void foo(S<I>, S<I + 2>);
23856 template<typename T>
23857 void foo(A<T, T{}>);
23859 This is a "non-deduced context":
23863 The non-deduced contexts are:
23865 --A non-type template argument or an array bound in which
23866 a subexpression references a template parameter.
23868 In these cases, we assume deduction succeeded, but don't
23869 actually infer any unifications. */
23871 if (!uses_template_parms (parm
)
23872 && !template_args_equal (parm
, arg
))
23873 return unify_expression_unequal (explain_p
, parm
, arg
);
23875 return unify_success (explain_p
);
23878 #undef RECUR_AND_CHECK_FAILURE
23880 /* Note that DECL can be defined in this translation unit, if
23884 mark_definable (tree decl
)
23887 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23888 FOR_EACH_CLONE (clone
, decl
)
23889 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23892 /* Called if RESULT is explicitly instantiated, or is a member of an
23893 explicitly instantiated class. */
23896 mark_decl_instantiated (tree result
, int extern_p
)
23898 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23900 /* If this entity has already been written out, it's too late to
23901 make any modifications. */
23902 if (TREE_ASM_WRITTEN (result
))
23905 /* For anonymous namespace we don't need to do anything. */
23906 if (decl_anon_ns_mem_p (result
))
23908 gcc_assert (!TREE_PUBLIC (result
));
23912 if (TREE_CODE (result
) != FUNCTION_DECL
)
23913 /* The TREE_PUBLIC flag for function declarations will have been
23914 set correctly by tsubst. */
23915 TREE_PUBLIC (result
) = 1;
23917 /* This might have been set by an earlier implicit instantiation. */
23918 DECL_COMDAT (result
) = 0;
23921 DECL_NOT_REALLY_EXTERN (result
) = 0;
23924 mark_definable (result
);
23925 mark_needed (result
);
23926 /* Always make artificials weak. */
23927 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23928 comdat_linkage (result
);
23929 /* For WIN32 we also want to put explicit instantiations in
23930 linkonce sections. */
23931 else if (TREE_PUBLIC (result
))
23932 maybe_make_one_only (result
);
23933 if (TREE_CODE (result
) == FUNCTION_DECL
23934 && DECL_TEMPLATE_INSTANTIATED (result
))
23935 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23936 since start_preparsed_function wouldn't have if we had an earlier
23937 extern explicit instantiation. */
23938 DECL_EXTERNAL (result
) = 0;
23941 /* If EXTERN_P, then this function will not be emitted -- unless
23942 followed by an explicit instantiation, at which point its linkage
23943 will be adjusted. If !EXTERN_P, then this function will be
23944 emitted here. In neither circumstance do we want
23945 import_export_decl to adjust the linkage. */
23946 DECL_INTERFACE_KNOWN (result
) = 1;
23949 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23950 important template arguments. If any are missing, we check whether
23951 they're important by using error_mark_node for substituting into any
23952 args that were used for partial ordering (the ones between ARGS and END)
23953 and seeing if it bubbles up. */
23956 check_undeduced_parms (tree targs
, tree args
, tree end
)
23958 bool found
= false;
23960 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23961 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23964 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23968 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23969 if (substed
== error_mark_node
)
23975 /* Given two function templates PAT1 and PAT2, return:
23977 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23978 -1 if PAT2 is more specialized than PAT1.
23979 0 if neither is more specialized.
23981 LEN indicates the number of parameters we should consider
23982 (defaulted parameters should not be considered).
23984 The 1998 std underspecified function template partial ordering, and
23985 DR214 addresses the issue. We take pairs of arguments, one from
23986 each of the templates, and deduce them against each other. One of
23987 the templates will be more specialized if all the *other*
23988 template's arguments deduce against its arguments and at least one
23989 of its arguments *does* *not* deduce against the other template's
23990 corresponding argument. Deduction is done as for class templates.
23991 The arguments used in deduction have reference and top level cv
23992 qualifiers removed. Iff both arguments were originally reference
23993 types *and* deduction succeeds in both directions, an lvalue reference
23994 wins against an rvalue reference and otherwise the template
23995 with the more cv-qualified argument wins for that pairing (if
23996 neither is more cv-qualified, they both are equal). Unlike regular
23997 deduction, after all the arguments have been deduced in this way,
23998 we do *not* verify the deduced template argument values can be
23999 substituted into non-deduced contexts.
24001 The logic can be a bit confusing here, because we look at deduce1 and
24002 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24003 can find template arguments for pat1 to make arg1 look like arg2, that
24004 means that arg2 is at least as specialized as arg1. */
24007 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24009 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24010 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24011 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24012 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24013 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24014 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24015 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24016 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24017 tree origs1
, origs2
;
24018 bool lose1
= false;
24019 bool lose2
= false;
24021 /* Remove the this parameter from non-static member functions. If
24022 one is a non-static member function and the other is not a static
24023 member function, remove the first parameter from that function
24024 also. This situation occurs for operator functions where we
24025 locate both a member function (with this pointer) and non-member
24026 operator (with explicit first operand). */
24027 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24029 len
--; /* LEN is the number of significant arguments for DECL1 */
24030 args1
= TREE_CHAIN (args1
);
24031 if (!DECL_STATIC_FUNCTION_P (decl2
))
24032 args2
= TREE_CHAIN (args2
);
24034 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24036 args2
= TREE_CHAIN (args2
);
24037 if (!DECL_STATIC_FUNCTION_P (decl1
))
24040 args1
= TREE_CHAIN (args1
);
24044 /* If only one is a conversion operator, they are unordered. */
24045 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24048 /* Consider the return type for a conversion function */
24049 if (DECL_CONV_FN_P (decl1
))
24051 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24052 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24056 processing_template_decl
++;
24062 /* Stop when an ellipsis is seen. */
24063 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24065 tree arg1
= TREE_VALUE (args1
);
24066 tree arg2
= TREE_VALUE (args2
);
24067 int deduce1
, deduce2
;
24073 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24074 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24076 /* When both arguments are pack expansions, we need only
24077 unify the patterns themselves. */
24078 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24079 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24081 /* This is the last comparison we need to do. */
24085 if (TYPE_REF_P (arg1
))
24087 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24088 arg1
= TREE_TYPE (arg1
);
24089 quals1
= cp_type_quals (arg1
);
24092 if (TYPE_REF_P (arg2
))
24094 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24095 arg2
= TREE_TYPE (arg2
);
24096 quals2
= cp_type_quals (arg2
);
24099 arg1
= TYPE_MAIN_VARIANT (arg1
);
24100 arg2
= TYPE_MAIN_VARIANT (arg2
);
24102 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24104 int i
, len2
= remaining_arguments (args2
);
24105 tree parmvec
= make_tree_vec (1);
24106 tree argvec
= make_tree_vec (len2
);
24109 /* Setup the parameter vector, which contains only ARG1. */
24110 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24112 /* Setup the argument vector, which contains the remaining
24114 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24115 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24117 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24118 argvec
, DEDUCE_EXACT
,
24119 /*subr=*/true, /*explain_p=*/false)
24122 /* We cannot deduce in the other direction, because ARG1 is
24123 a pack expansion but ARG2 is not. */
24126 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24128 int i
, len1
= remaining_arguments (args1
);
24129 tree parmvec
= make_tree_vec (1);
24130 tree argvec
= make_tree_vec (len1
);
24133 /* Setup the parameter vector, which contains only ARG1. */
24134 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24136 /* Setup the argument vector, which contains the remaining
24138 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24139 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24141 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24142 argvec
, DEDUCE_EXACT
,
24143 /*subr=*/true, /*explain_p=*/false)
24146 /* We cannot deduce in the other direction, because ARG2 is
24147 a pack expansion but ARG1 is not.*/
24153 /* The normal case, where neither argument is a pack
24155 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24156 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24158 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24159 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24163 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24164 arg2, then arg2 is not as specialized as arg1. */
24170 /* "If, for a given type, deduction succeeds in both directions
24171 (i.e., the types are identical after the transformations above)
24172 and both P and A were reference types (before being replaced with
24173 the type referred to above):
24174 - if the type from the argument template was an lvalue reference and
24175 the type from the parameter template was not, the argument type is
24176 considered to be more specialized than the other; otherwise,
24177 - if the type from the argument template is more cv-qualified
24178 than the type from the parameter template (as described above),
24179 the argument type is considered to be more specialized than the other;
24181 - neither type is more specialized than the other." */
24183 if (deduce1
&& deduce2
)
24185 if (ref1
&& ref2
&& ref1
!= ref2
)
24192 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24194 if ((quals1
& quals2
) == quals2
)
24196 if ((quals1
& quals2
) == quals1
)
24201 if (lose1
&& lose2
)
24202 /* We've failed to deduce something in either direction.
24203 These must be unordered. */
24206 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24207 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24208 /* We have already processed all of the arguments in our
24209 handing of the pack expansion type. */
24212 args1
= TREE_CHAIN (args1
);
24213 args2
= TREE_CHAIN (args2
);
24216 /* "In most cases, all template parameters must have values in order for
24217 deduction to succeed, but for partial ordering purposes a template
24218 parameter may remain without a value provided it is not used in the
24219 types being used for partial ordering."
24221 Thus, if we are missing any of the targs1 we need to substitute into
24222 origs1, then pat2 is not as specialized as pat1. This can happen when
24223 there is a nondeduced context. */
24224 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24226 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24229 processing_template_decl
--;
24231 /* If both deductions succeed, the partial ordering selects the more
24232 constrained template. */
24233 /* P2113: If the corresponding template-parameters of the
24234 template-parameter-lists are not equivalent ([temp.over.link]) or if
24235 the function parameters that positionally correspond between the two
24236 templates are not of the same type, neither template is more
24237 specialized than the other. */
24238 if (!lose1
&& !lose2
24239 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24240 DECL_TEMPLATE_PARMS (pat2
))
24241 && compparms (origs1
, origs2
))
24243 int winner
= more_constrained (decl1
, decl2
);
24246 else if (winner
< 0)
24250 /* All things being equal, if the next argument is a pack expansion
24251 for one function but not for the other, prefer the
24252 non-variadic function. FIXME this is bogus; see c++/41958. */
24254 && args1
&& TREE_VALUE (args1
)
24255 && args2
&& TREE_VALUE (args2
))
24257 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24258 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24261 if (lose1
== lose2
)
24269 /* Determine which of two partial specializations of TMPL is more
24272 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24273 to the first partial specialization. The TREE_PURPOSE is the
24274 innermost set of template parameters for the partial
24275 specialization. PAT2 is similar, but for the second template.
24277 Return 1 if the first partial specialization is more specialized;
24278 -1 if the second is more specialized; 0 if neither is more
24281 See [temp.class.order] for information about determining which of
24282 two templates is more specialized. */
24285 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24289 bool any_deductions
= false;
24291 tree tmpl1
= TREE_VALUE (pat1
);
24292 tree tmpl2
= TREE_VALUE (pat2
);
24293 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24294 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24296 /* Just like what happens for functions, if we are ordering between
24297 different template specializations, we may encounter dependent
24298 types in the arguments, and we need our dependency check functions
24299 to behave correctly. */
24300 ++processing_template_decl
;
24301 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24305 any_deductions
= true;
24308 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24312 any_deductions
= true;
24314 --processing_template_decl
;
24316 /* If both deductions succeed, the partial ordering selects the more
24317 constrained template. */
24318 if (!winner
&& any_deductions
)
24319 winner
= more_constrained (tmpl1
, tmpl2
);
24321 /* In the case of a tie where at least one of the templates
24322 has a parameter pack at the end, the template with the most
24323 non-packed parameters wins. */
24326 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24327 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24329 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24330 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24331 int len1
= TREE_VEC_LENGTH (args1
);
24332 int len2
= TREE_VEC_LENGTH (args2
);
24334 /* We don't count the pack expansion at the end. */
24335 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24337 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24342 else if (len1
< len2
)
24349 /* Return the template arguments that will produce the function signature
24350 DECL from the function template FN, with the explicit template
24351 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24352 also match. Return NULL_TREE if no satisfactory arguments could be
24356 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24358 int ntparms
= DECL_NTPARMS (fn
);
24359 tree targs
= make_tree_vec (ntparms
);
24360 tree decl_type
= TREE_TYPE (decl
);
24361 tree decl_arg_types
;
24363 unsigned int nargs
, ix
;
24366 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24368 /* Never do unification on the 'this' parameter. */
24369 decl_arg_types
= skip_artificial_parms_for (decl
,
24370 TYPE_ARG_TYPES (decl_type
));
24372 nargs
= list_length (decl_arg_types
);
24373 args
= XALLOCAVEC (tree
, nargs
);
24374 for (arg
= decl_arg_types
, ix
= 0;
24375 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24376 arg
= TREE_CHAIN (arg
), ++ix
)
24377 args
[ix
] = TREE_VALUE (arg
);
24379 if (fn_type_unification (fn
, explicit_args
, targs
,
24381 (check_rettype
|| DECL_CONV_FN_P (fn
)
24382 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24383 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24384 /*explain_p=*/false,
24386 == error_mark_node
)
24392 /* Return the innermost template arguments that, when applied to a partial
24393 specialization SPEC_TMPL of TMPL, yield the ARGS.
24395 For example, suppose we have:
24397 template <class T, class U> struct S {};
24398 template <class T> struct S<T*, int> {};
24400 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24401 partial specialization and the ARGS will be {double*, int}. The resulting
24402 vector will be {double}, indicating that `T' is bound to `double'. */
24405 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24407 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24409 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24410 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24412 tree innermost_deduced_args
;
24414 innermost_deduced_args
= make_tree_vec (ntparms
);
24415 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24417 deduced_args
= copy_node (args
);
24418 SET_TMPL_ARGS_LEVEL (deduced_args
,
24419 TMPL_ARGS_DEPTH (deduced_args
),
24420 innermost_deduced_args
);
24423 deduced_args
= innermost_deduced_args
;
24425 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24427 if (unify (tparms
, deduced_args
,
24428 INNERMOST_TEMPLATE_ARGS (spec_args
),
24429 INNERMOST_TEMPLATE_ARGS (args
),
24430 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24433 for (i
= 0; i
< ntparms
; ++i
)
24434 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24436 if (!tried_array_deduction
)
24438 try_array_deduction (tparms
, innermost_deduced_args
,
24439 INNERMOST_TEMPLATE_ARGS (spec_args
));
24440 tried_array_deduction
= true;
24441 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24447 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24449 excessive_deduction_depth
= true;
24453 /* Verify that nondeduced template arguments agree with the type
24454 obtained from argument deduction.
24458 struct A { typedef int X; };
24459 template <class T, class U> struct C {};
24460 template <class T> struct C<T, typename T::X> {};
24462 Then with the instantiation `C<A, int>', we can deduce that
24463 `T' is `A' but unify () does not check whether `typename T::X'
24465 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24467 if (spec_args
!= error_mark_node
)
24468 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24469 INNERMOST_TEMPLATE_ARGS (spec_args
),
24470 tmpl
, tf_none
, false, false);
24472 pop_tinst_level ();
24474 if (spec_args
== error_mark_node
24475 /* We only need to check the innermost arguments; the other
24476 arguments will always agree. */
24477 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24478 INNERMOST_TEMPLATE_ARGS (args
)))
24481 /* Now that we have bindings for all of the template arguments,
24482 ensure that the arguments deduced for the template template
24483 parameters have compatible template parameter lists. See the use
24484 of template_template_parm_bindings_ok_p in fn_type_unification
24485 for more information. */
24486 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24489 return deduced_args
;
24492 // Compare two function templates T1 and T2 by deducing bindings
24493 // from one against the other. If both deductions succeed, compare
24494 // constraints to see which is more constrained.
24496 more_specialized_inst (tree t1
, tree t2
)
24501 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24507 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24513 // If both deductions succeed, then one may be more constrained.
24514 if (count
== 2 && fate
== 0)
24515 fate
= more_constrained (t1
, t2
);
24520 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24521 Return the TREE_LIST node with the most specialized template, if
24522 any. If there is no most specialized template, the error_mark_node
24525 Note that this function does not look at, or modify, the
24526 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24527 returned is one of the elements of INSTANTIATIONS, callers may
24528 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24529 and retrieve it from the value returned. */
24532 most_specialized_instantiation (tree templates
)
24536 ++processing_template_decl
;
24539 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24541 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24542 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24547 /* Equally specialized, move to next function. If there
24548 is no next function, nothing's most specialized. */
24549 fn
= TREE_CHAIN (fn
);
24557 /* Now verify that champ is better than everything earlier in the
24558 instantiation list. */
24559 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24560 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24567 processing_template_decl
--;
24570 return error_mark_node
;
24575 /* If DECL is a specialization of some template, return the most
24576 general such template. Otherwise, returns NULL_TREE.
24578 For example, given:
24580 template <class T> struct S { template <class U> void f(U); };
24582 if TMPL is `template <class U> void S<int>::f(U)' this will return
24583 the full template. This function will not trace past partial
24584 specializations, however. For example, given in addition:
24586 template <class T> struct S<T*> { template <class U> void f(U); };
24588 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24589 `template <class T> template <class U> S<T*>::f(U)'. */
24592 most_general_template (tree decl
)
24594 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24596 if (tree tinfo
= get_template_info (decl
))
24597 decl
= TI_TEMPLATE (tinfo
);
24598 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24599 template friend, or a FIELD_DECL for a capture pack. */
24600 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24604 /* Look for more and more general templates. */
24605 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24607 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24608 (See cp-tree.h for details.) */
24609 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24612 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24613 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24614 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24617 /* Stop if we run into an explicitly specialized class template. */
24618 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24619 && DECL_CONTEXT (decl
)
24620 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24623 decl
= DECL_TI_TEMPLATE (decl
);
24629 /* Return the most specialized of the template partial specializations
24630 which can produce TARGET, a specialization of some class or variable
24631 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24632 a TEMPLATE_DECL node corresponding to the partial specialization, while
24633 the TREE_PURPOSE is the set of template arguments that must be
24634 substituted into the template pattern in order to generate TARGET.
24636 If the choice of partial specialization is ambiguous, a diagnostic
24637 is issued, and the error_mark_node is returned. If there are no
24638 partial specializations matching TARGET, then NULL_TREE is
24639 returned, indicating that the primary template should be used. */
24642 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24644 tree list
= NULL_TREE
;
24649 tree outer_args
= NULL_TREE
;
24652 if (TYPE_P (target
))
24654 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24655 tmpl
= TI_TEMPLATE (tinfo
);
24656 args
= TI_ARGS (tinfo
);
24658 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24660 tmpl
= TREE_OPERAND (target
, 0);
24661 args
= TREE_OPERAND (target
, 1);
24663 else if (VAR_P (target
))
24665 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24666 tmpl
= TI_TEMPLATE (tinfo
);
24667 args
= TI_ARGS (tinfo
);
24670 gcc_unreachable ();
24672 tree main_tmpl
= most_general_template (tmpl
);
24674 /* For determining which partial specialization to use, only the
24675 innermost args are interesting. */
24676 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24678 outer_args
= strip_innermost_template_args (args
, 1);
24679 args
= INNERMOST_TEMPLATE_ARGS (args
);
24682 /* The caller hasn't called push_to_top_level yet, but we need
24683 get_partial_spec_bindings to be done in non-template context so that we'll
24684 fully resolve everything. */
24685 processing_template_decl_sentinel ptds
;
24687 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24689 const tree ospec_tmpl
= TREE_VALUE (t
);
24694 /* Substitute in the template args from the enclosing class. */
24695 ++processing_template_decl
;
24696 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24697 --processing_template_decl
;
24698 if (spec_tmpl
== error_mark_node
)
24699 return error_mark_node
;
24702 spec_tmpl
= ospec_tmpl
;
24704 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24708 spec_args
= add_to_template_args (outer_args
, spec_args
);
24710 /* Keep the candidate only if the constraints are satisfied,
24711 or if we're not compiling with concepts. */
24713 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24715 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24716 TREE_TYPE (list
) = TREE_TYPE (t
);
24724 ambiguous_p
= false;
24727 t
= TREE_CHAIN (t
);
24728 for (; t
; t
= TREE_CHAIN (t
))
24730 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24737 t
= TREE_CHAIN (t
);
24740 ambiguous_p
= true;
24749 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24751 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24754 ambiguous_p
= true;
24762 char *spaces
= NULL
;
24763 if (!(complain
& tf_error
))
24764 return error_mark_node
;
24765 if (TYPE_P (target
))
24766 error ("ambiguous template instantiation for %q#T", target
);
24768 error ("ambiguous template instantiation for %q#D", target
);
24769 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24770 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24772 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24773 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24774 "%s %#qS", spaces
? spaces
: str
, subst
);
24775 spaces
= spaces
? spaces
: get_spaces (str
);
24778 return error_mark_node
;
24784 /* Explicitly instantiate DECL. */
24787 do_decl_instantiation (tree decl
, tree storage
)
24789 tree result
= NULL_TREE
;
24792 if (!decl
|| decl
== error_mark_node
)
24793 /* An error occurred, for which grokdeclarator has already issued
24794 an appropriate message. */
24796 else if (! DECL_LANG_SPECIFIC (decl
))
24798 error ("explicit instantiation of non-template %q#D", decl
);
24801 else if (DECL_DECLARED_CONCEPT_P (decl
))
24804 error ("explicit instantiation of variable concept %q#D", decl
);
24806 error ("explicit instantiation of function concept %q#D", decl
);
24810 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24811 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24813 if (VAR_P (decl
) && !var_templ
)
24815 /* There is an asymmetry here in the way VAR_DECLs and
24816 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24817 the latter, the DECL we get back will be marked as a
24818 template instantiation, and the appropriate
24819 DECL_TEMPLATE_INFO will be set up. This does not happen for
24820 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24821 should handle VAR_DECLs as it currently handles
24823 if (!DECL_CLASS_SCOPE_P (decl
))
24825 error ("%qD is not a static data member of a class template", decl
);
24828 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24829 if (!result
|| !VAR_P (result
))
24831 error ("no matching template for %qD found", decl
);
24834 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24836 error ("type %qT for explicit instantiation %qD does not match "
24837 "declared type %qT", TREE_TYPE (result
), decl
,
24842 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24844 error ("explicit instantiation of %q#D", decl
);
24850 /* Check for various error cases. Note that if the explicit
24851 instantiation is valid the RESULT will currently be marked as an
24852 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24853 until we get here. */
24855 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24857 /* DR 259 [temp.spec].
24859 Both an explicit instantiation and a declaration of an explicit
24860 specialization shall not appear in a program unless the explicit
24861 instantiation follows a declaration of the explicit specialization.
24863 For a given set of template parameters, if an explicit
24864 instantiation of a template appears after a declaration of an
24865 explicit specialization for that template, the explicit
24866 instantiation has no effect. */
24869 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24873 No program shall explicitly instantiate any template more
24876 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24877 the first instantiation was `extern' and the second is not,
24878 and EXTERN_P for the opposite case. */
24879 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24880 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24881 /* If an "extern" explicit instantiation follows an ordinary
24882 explicit instantiation, the template is instantiated. */
24886 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24888 error ("no matching template for %qD found", result
);
24891 else if (!DECL_TEMPLATE_INFO (result
))
24893 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24897 if (storage
== NULL_TREE
)
24899 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24901 if (cxx_dialect
== cxx98
)
24902 pedwarn (input_location
, OPT_Wpedantic
,
24903 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24908 error ("storage class %qD applied to template instantiation", storage
);
24910 check_explicit_instantiation_namespace (result
);
24911 mark_decl_instantiated (result
, extern_p
);
24913 instantiate_decl (result
, /*defer_ok=*/true,
24914 /*expl_inst_class_mem_p=*/false);
24918 mark_class_instantiated (tree t
, int extern_p
)
24920 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24921 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24922 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24923 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24926 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24927 rest_of_type_compilation (t
, 1);
24931 /* Called from do_type_instantiation through binding_table_foreach to
24932 do recursive instantiation for the type bound in ENTRY. */
24934 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24936 tree storage
= *(tree
*) data
;
24938 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24939 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24940 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24941 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24944 /* Perform an explicit instantiation of template class T. STORAGE, if
24945 non-null, is the RID for extern, inline or static. COMPLAIN is
24946 nonzero if this is called from the parser, zero if called recursively,
24947 since the standard is unclear (as detailed below). */
24950 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24955 int previous_instantiation_extern_p
= 0;
24957 if (TREE_CODE (t
) == TYPE_DECL
)
24960 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24963 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24965 error ("explicit instantiation of non-class template %qD", tmpl
);
24967 error ("explicit instantiation of non-template type %qT", t
);
24973 if (!COMPLETE_TYPE_P (t
))
24975 if (complain
& tf_error
)
24976 error ("explicit instantiation of %q#T before definition of template",
24981 if (storage
!= NULL_TREE
)
24983 if (storage
== ridpointers
[(int) RID_EXTERN
])
24985 if (cxx_dialect
== cxx98
)
24986 pedwarn (input_location
, OPT_Wpedantic
,
24987 "ISO C++ 1998 forbids the use of %<extern%> on "
24988 "explicit instantiations");
24991 pedwarn (input_location
, OPT_Wpedantic
,
24992 "ISO C++ forbids the use of %qE"
24993 " on explicit instantiations", storage
);
24995 if (storage
== ridpointers
[(int) RID_INLINE
])
24997 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24999 else if (storage
== ridpointers
[(int) RID_STATIC
])
25003 error ("storage class %qD applied to template instantiation",
25009 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25011 /* DR 259 [temp.spec].
25013 Both an explicit instantiation and a declaration of an explicit
25014 specialization shall not appear in a program unless the explicit
25015 instantiation follows a declaration of the explicit specialization.
25017 For a given set of template parameters, if an explicit
25018 instantiation of a template appears after a declaration of an
25019 explicit specialization for that template, the explicit
25020 instantiation has no effect. */
25023 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
25027 No program shall explicitly instantiate any template more
25030 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25031 instantiation was `extern'. If EXTERN_P then the second is.
25032 These cases are OK. */
25033 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
25035 if (!previous_instantiation_extern_p
&& !extern_p
25036 && (complain
& tf_error
))
25037 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
25039 /* If we've already instantiated the template, just return now. */
25040 if (!CLASSTYPE_INTERFACE_ONLY (t
))
25044 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25045 mark_class_instantiated (t
, extern_p
);
25050 /* In contrast to implicit instantiation, where only the
25051 declarations, and not the definitions, of members are
25052 instantiated, we have here:
25056 An explicit instantiation that names a class template
25057 specialization is also an explicit instantiation of the same
25058 kind (declaration or definition) of each of its members (not
25059 including members inherited from base classes and members
25060 that are templates) that has not been previously explicitly
25061 specialized in the translation unit containing the explicit
25062 instantiation, provided that the associated constraints, if
25063 any, of that member are satisfied by the template arguments
25064 of the explicit instantiation. */
25065 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25067 || (TREE_CODE (fld
) == FUNCTION_DECL
25069 && user_provided_p (fld
)))
25070 && DECL_TEMPLATE_INSTANTIATION (fld
)
25071 && constraints_satisfied_p (fld
))
25073 mark_decl_instantiated (fld
, extern_p
);
25075 instantiate_decl (fld
, /*defer_ok=*/true,
25076 /*expl_inst_class_mem_p=*/true);
25079 if (CLASSTYPE_NESTED_UTDS (t
))
25080 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
25081 bt_instantiate_type_proc
, &storage
);
25084 /* Given a function DECL, which is a specialization of TMPL, modify
25085 DECL to be a re-instantiation of TMPL with the same template
25086 arguments. TMPL should be the template into which tsubst'ing
25087 should occur for DECL, not the most general template.
25089 One reason for doing this is a scenario like this:
25092 void f(const T&, int i);
25094 void g() { f(3, 7); }
25097 void f(const T& t, const int i) { }
25099 Note that when the template is first instantiated, with
25100 instantiate_template, the resulting DECL will have no name for the
25101 first parameter, and the wrong type for the second. So, when we go
25102 to instantiate the DECL, we regenerate it. */
25105 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25107 /* The arguments used to instantiate DECL, from the most general
25111 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25113 /* Make sure that we can see identifiers, and compute access
25115 push_access_scope (decl
);
25117 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25125 args_depth
= TMPL_ARGS_DEPTH (args
);
25126 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25127 if (args_depth
> parms_depth
)
25128 args
= get_innermost_template_args (args
, parms_depth
);
25130 /* Instantiate a dynamic exception-specification. noexcept will be
25132 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25133 if (TREE_VALUE (raises
))
25135 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25136 args
, tf_error
, NULL_TREE
,
25137 /*defer_ok*/false);
25138 if (specs
&& specs
!= error_mark_node
)
25139 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25143 /* Merge parameter declarations. */
25144 decl_parm
= skip_artificial_parms_for (decl
,
25145 DECL_ARGUMENTS (decl
));
25147 = skip_artificial_parms_for (code_pattern
,
25148 DECL_ARGUMENTS (code_pattern
));
25149 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25154 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25155 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25156 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25158 parm_type
= type_decays_to (parm_type
);
25159 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25160 TREE_TYPE (decl_parm
) = parm_type
;
25161 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25162 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25164 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25165 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25167 decl_parm
= DECL_CHAIN (decl_parm
);
25168 pattern_parm
= DECL_CHAIN (pattern_parm
);
25170 /* Merge any parameters that match with the function parameter
25172 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25175 tree expanded_types
;
25176 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25177 the parameters in this function parameter pack. */
25178 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25179 args
, tf_error
, NULL_TREE
);
25180 len
= TREE_VEC_LENGTH (expanded_types
);
25181 for (i
= 0; i
< len
; i
++)
25186 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25187 /* Rename the parameter to include the index. */
25188 DECL_NAME (decl_parm
) =
25189 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25190 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25191 parm_type
= type_decays_to (parm_type
);
25192 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25193 TREE_TYPE (decl_parm
) = parm_type
;
25194 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25195 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25197 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25198 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25200 decl_parm
= DECL_CHAIN (decl_parm
);
25203 /* Merge additional specifiers from the CODE_PATTERN. */
25204 if (DECL_DECLARED_INLINE_P (code_pattern
)
25205 && !DECL_DECLARED_INLINE_P (decl
))
25206 DECL_DECLARED_INLINE_P (decl
) = 1;
25208 maybe_instantiate_noexcept (decl
, tf_error
);
25210 else if (VAR_P (decl
))
25212 start_lambda_scope (decl
);
25213 DECL_INITIAL (decl
) =
25214 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25215 tf_error
, DECL_TI_TEMPLATE (decl
));
25216 finish_lambda_scope ();
25217 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25218 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25219 tf_error
, DECL_TI_TEMPLATE (decl
));
25222 gcc_unreachable ();
25224 pop_access_scope (decl
);
25227 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25228 substituted to get DECL. */
25231 template_for_substitution (tree decl
)
25233 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25235 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25236 for the instantiation. This is not always the most general
25237 template. Consider, for example:
25240 struct S { template <class U> void f();
25241 template <> void f<int>(); };
25243 and an instantiation of S<double>::f<int>. We want TD to be the
25244 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25245 while (/* An instantiation cannot have a definition, so we need a
25246 more general template. */
25247 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25248 /* We must also deal with friend templates. Given:
25250 template <class T> struct S {
25251 template <class U> friend void f() {};
25254 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25255 so far as the language is concerned, but that's still
25256 where we get the pattern for the instantiation from. On
25257 other hand, if the definition comes outside the class, say:
25259 template <class T> struct S {
25260 template <class U> friend void f();
25262 template <class U> friend void f() {}
25264 we don't need to look any further. That's what the check for
25265 DECL_INITIAL is for. */
25266 || (TREE_CODE (decl
) == FUNCTION_DECL
25267 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25268 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25270 /* The present template, TD, should not be a definition. If it
25271 were a definition, we should be using it! Note that we
25272 cannot restructure the loop to just keep going until we find
25273 a template with a definition, since that might go too far if
25274 a specialization was declared, but not defined. */
25276 /* Fetch the more general template. */
25277 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25283 /* Returns true if we need to instantiate this template instance even if we
25284 know we aren't going to emit it. */
25287 always_instantiate_p (tree decl
)
25289 /* We always instantiate inline functions so that we can inline them. An
25290 explicit instantiation declaration prohibits implicit instantiation of
25291 non-inline functions. With high levels of optimization, we would
25292 normally inline non-inline functions -- but we're not allowed to do
25293 that for "extern template" functions. Therefore, we check
25294 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25295 return ((TREE_CODE (decl
) == FUNCTION_DECL
25296 && (DECL_DECLARED_INLINE_P (decl
)
25297 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25298 /* And we need to instantiate static data members so that
25299 their initializers are available in integral constant
25302 && decl_maybe_constant_var_p (decl
)));
25305 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25306 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25307 error, true otherwise. */
25310 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25312 tree fntype
, spec
, noex
;
25314 /* Don't instantiate a noexcept-specification from template context. */
25315 if (processing_template_decl
25316 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25319 if (DECL_MAYBE_DELETED (fn
))
25321 if (fn
== current_function_decl
)
25322 /* We're in start_preparsed_function, keep going. */
25326 synthesize_method (fn
);
25328 return !DECL_MAYBE_DELETED (fn
);
25331 fntype
= TREE_TYPE (fn
);
25332 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25334 if (!spec
|| !TREE_PURPOSE (spec
))
25337 noex
= TREE_PURPOSE (spec
);
25338 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25339 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25342 tree orig_fn
= NULL_TREE
;
25343 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25344 its FUNCTION_DECL for the rest of this function -- push_access_scope
25345 doesn't accept TEMPLATE_DECLs. */
25346 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25349 fn
= DECL_TEMPLATE_RESULT (fn
);
25352 if (DECL_CLONED_FUNCTION_P (fn
))
25354 tree prime
= DECL_CLONED_FUNCTION (fn
);
25355 if (!maybe_instantiate_noexcept (prime
, complain
))
25357 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25359 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25361 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25362 bool added
= false;
25363 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25365 spec
= get_defaulted_eh_spec (fn
, complain
);
25366 if (spec
== error_mark_node
)
25367 /* This might have failed because of an unparsed DMI, so
25368 let's try again later. */
25371 else if (!(added
= !fns
->add (fn
)))
25373 /* If hash_set::add returns true, the element was already there. */
25374 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25375 DECL_SOURCE_LOCATION (fn
));
25377 "exception specification of %qD depends on itself",
25379 spec
= noexcept_false_spec
;
25381 else if (push_tinst_level (fn
))
25383 push_to_top_level ();
25384 push_access_scope (fn
);
25385 push_deferring_access_checks (dk_no_deferred
);
25386 input_location
= DECL_SOURCE_LOCATION (fn
);
25388 if (!DECL_LOCAL_DECL_P (fn
))
25390 /* If needed, set current_class_ptr for the benefit of
25391 tsubst_copy/PARM_DECL. The exception pattern will
25392 refer to the parm of the template, not the
25394 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25395 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25397 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25398 current_class_ptr
= NULL_TREE
;
25399 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25400 current_class_ptr
= this_parm
;
25404 /* If this function is represented by a TEMPLATE_DECL, then
25405 the deferred noexcept-specification might still contain
25406 dependent types, even after substitution. And we need the
25407 dependency check functions to work in build_noexcept_spec. */
25409 ++processing_template_decl
;
25411 /* Do deferred instantiation of the noexcept-specifier. */
25412 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25413 DEFERRED_NOEXCEPT_ARGS (noex
),
25414 tf_warning_or_error
, fn
,
25415 /*function_p=*/false,
25418 /* Build up the noexcept-specification. */
25419 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25422 --processing_template_decl
;
25424 pop_deferring_access_checks ();
25425 pop_access_scope (fn
);
25426 pop_tinst_level ();
25427 pop_from_top_level ();
25430 spec
= noexcept_false_spec
;
25436 if (spec
== error_mark_node
)
25438 /* This failed with a hard error, so let's go with false. */
25439 gcc_assert (seen_error ());
25440 spec
= noexcept_false_spec
;
25443 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25445 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25450 /* We're starting to process the function INST, an instantiation of PATTERN;
25451 add their parameters to local_specializations. */
25454 register_parameter_specializations (tree pattern
, tree inst
)
25456 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25457 tree spec_parm
= DECL_ARGUMENTS (inst
);
25458 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25460 register_local_specialization (spec_parm
, tmpl_parm
);
25461 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25462 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25464 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25466 if (!DECL_PACK_P (tmpl_parm
)
25467 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25469 register_local_specialization (spec_parm
, tmpl_parm
);
25470 spec_parm
= DECL_CHAIN (spec_parm
);
25474 /* Register the (value) argument pack as a specialization of
25475 TMPL_PARM, then move on. */
25476 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25477 register_local_specialization (argpack
, tmpl_parm
);
25480 gcc_assert (!spec_parm
);
25483 /* Instantiate the body of D using PATTERN with ARGS. We have
25484 already determined PATTERN is the correct template to use.
25485 NESTED_P is true if this is a nested function, in which case
25486 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25489 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25491 tree td
= NULL_TREE
;
25492 tree code_pattern
= pattern
;
25497 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25500 /* Only OMP reductions are nested. */
25501 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25503 vec
<tree
> omp_privatization_save
;
25504 if (current_function_decl
)
25505 save_omp_privatization_clauses (omp_privatization_save
);
25508 = !(current_function_decl
25509 && !LAMBDA_FUNCTION_P (d
)
25510 && decl_function_context (d
) == current_function_decl
);
25513 push_to_top_level ();
25516 gcc_assert (!processing_template_decl
);
25517 push_function_context ();
25518 cp_unevaluated_operand
= 0;
25519 c_inhibit_evaluation_warnings
= 0;
25524 /* The variable might be a lambda's extra scope, and that
25525 lambda's visibility depends on D's. */
25526 maybe_commonize_var (d
);
25527 determine_visibility (d
);
25530 /* Mark D as instantiated so that recursive calls to
25531 instantiate_decl do not try to instantiate it again. */
25532 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25535 /* Regenerate the declaration in case the template has been modified
25536 by a subsequent redeclaration. */
25537 regenerate_decl_from_template (d
, td
, args
);
25539 /* We already set the file and line above. Reset them now in case
25540 they changed as a result of calling regenerate_decl_from_template. */
25541 input_location
= DECL_SOURCE_LOCATION (d
);
25546 bool const_init
= false;
25548 /* Clear out DECL_RTL; whatever was there before may not be right
25549 since we've reset the type of the declaration. */
25550 SET_DECL_RTL (d
, NULL
);
25551 DECL_IN_AGGR_P (d
) = 0;
25553 /* The initializer is placed in DECL_INITIAL by
25554 regenerate_decl_from_template so we don't need to
25555 push/pop_access_scope again here. Pull it out so that
25556 cp_finish_decl can process it. */
25557 init
= DECL_INITIAL (d
);
25558 DECL_INITIAL (d
) = NULL_TREE
;
25559 DECL_INITIALIZED_P (d
) = 0;
25561 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25562 initializer. That function will defer actual emission until
25563 we have a chance to determine linkage. */
25564 DECL_EXTERNAL (d
) = 0;
25566 /* Enter the scope of D so that access-checking works correctly. */
25567 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25569 push_nested_class (DECL_CONTEXT (d
));
25571 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25572 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25575 pop_nested_class ();
25577 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25578 synthesize_method (d
);
25579 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25581 /* Set up the list of local specializations. */
25582 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25583 tree block
= NULL_TREE
;
25585 /* Set up context. */
25587 block
= push_stmt_list ();
25589 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25591 perform_instantiation_time_access_checks (code_pattern
, args
);
25593 /* Create substitution entries for the parameters. */
25594 register_parameter_specializations (code_pattern
, d
);
25596 /* Substitute into the body of the function. */
25597 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25598 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25599 tf_warning_or_error
, d
);
25602 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25603 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25604 /*integral_constant_expression_p=*/false);
25606 /* Set the current input_location to the end of the function
25607 so that finish_function knows where we are. */
25609 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25611 /* Remember if we saw an infinite loop in the template. */
25612 current_function_infinite_loop
25613 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25616 /* Finish the function. */
25618 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25621 d
= finish_function (/*inline_p=*/false);
25622 expand_or_defer_fn (d
);
25625 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25626 cp_check_omp_declare_reduction (d
);
25629 /* We're not deferring instantiation any more. */
25630 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25633 pop_from_top_level ();
25635 pop_function_context ();
25637 if (current_function_decl
)
25638 restore_omp_privatization_clauses (omp_privatization_save
);
25641 /* Produce the definition of D, a _DECL generated from a template. If
25642 DEFER_OK is true, then we don't have to actually do the
25643 instantiation now; we just have to do it sometime. Normally it is
25644 an error if this is an explicit instantiation but D is undefined.
25645 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25646 instantiated class template. */
25649 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25651 tree tmpl
= DECL_TI_TEMPLATE (d
);
25658 bool pattern_defined
;
25659 location_t saved_loc
= input_location
;
25660 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25661 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25665 /* This function should only be used to instantiate templates for
25666 functions and static member variables. */
25667 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25669 /* A concept is never instantiated. */
25670 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25672 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
25674 /* Variables are never deferred; if instantiation is required, they
25675 are instantiated right away. That allows for better code in the
25676 case that an expression refers to the value of the variable --
25677 if the variable has a constant value the referring expression can
25678 take advantage of that fact. */
25682 /* Don't instantiate cloned functions. Instead, instantiate the
25683 functions they cloned. */
25684 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25685 d
= DECL_CLONED_FUNCTION (d
);
25687 if (DECL_TEMPLATE_INSTANTIATED (d
)
25688 || TREE_TYPE (d
) == error_mark_node
25689 || (TREE_CODE (d
) == FUNCTION_DECL
25690 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25691 || DECL_TEMPLATE_SPECIALIZATION (d
))
25692 /* D has already been instantiated or explicitly specialized, so
25693 there's nothing for us to do here.
25695 It might seem reasonable to check whether or not D is an explicit
25696 instantiation, and, if so, stop here. But when an explicit
25697 instantiation is deferred until the end of the compilation,
25698 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25699 the instantiation. */
25702 /* Check to see whether we know that this template will be
25703 instantiated in some other file, as with "extern template"
25705 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25707 /* In general, we do not instantiate such templates. */
25708 if (external_p
&& !always_instantiate_p (d
))
25711 gen_tmpl
= most_general_template (tmpl
);
25712 gen_args
= DECL_TI_ARGS (d
);
25714 /* We should already have the extra args. */
25715 gcc_checking_assert (tmpl
== gen_tmpl
25716 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25717 == TMPL_ARGS_DEPTH (gen_args
)));
25718 /* And what's in the hash table should match D. */
25719 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25721 || spec
== NULL_TREE
);
25723 /* This needs to happen before any tsubsting. */
25724 if (! push_tinst_level (d
))
25727 timevar_push (TV_TEMPLATE_INST
);
25729 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25730 for the instantiation. */
25731 td
= template_for_substitution (d
);
25736 /* Look up an explicit specialization, if any. */
25737 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25738 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25739 if (elt
&& elt
!= error_mark_node
)
25741 td
= TREE_VALUE (elt
);
25742 args
= TREE_PURPOSE (elt
);
25746 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25748 /* We should never be trying to instantiate a member of a class
25749 template or partial specialization. */
25750 gcc_assert (d
!= code_pattern
);
25752 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25753 || DECL_TEMPLATE_SPECIALIZATION (td
))
25754 /* In the case of a friend template whose definition is provided
25755 outside the class, we may have too many arguments. Drop the
25756 ones we don't need. The same is true for specializations. */
25757 args
= get_innermost_template_args
25758 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25760 if (TREE_CODE (d
) == FUNCTION_DECL
)
25762 deleted_p
= DECL_DELETED_FN (code_pattern
);
25763 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25764 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25765 || DECL_DEFAULTED_FN (code_pattern
)
25771 if (DECL_CLASS_SCOPE_P (code_pattern
))
25772 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25774 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25777 /* We may be in the middle of deferred access check. Disable it now. */
25778 push_deferring_access_checks (dk_no_deferred
);
25780 /* Unless an explicit instantiation directive has already determined
25781 the linkage of D, remember that a definition is available for
25783 if (pattern_defined
25784 && !DECL_INTERFACE_KNOWN (d
)
25785 && !DECL_NOT_REALLY_EXTERN (d
))
25786 mark_definable (d
);
25788 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25789 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25790 input_location
= DECL_SOURCE_LOCATION (d
);
25792 /* If D is a member of an explicitly instantiated class template,
25793 and no definition is available, treat it like an implicit
25795 if (!pattern_defined
&& expl_inst_class_mem_p
25796 && DECL_EXPLICIT_INSTANTIATION (d
))
25798 /* Leave linkage flags alone on instantiations with anonymous
25800 if (TREE_PUBLIC (d
))
25802 DECL_NOT_REALLY_EXTERN (d
) = 0;
25803 DECL_INTERFACE_KNOWN (d
) = 0;
25805 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25808 /* Defer all other templates, unless we have been explicitly
25809 forbidden from doing so. */
25810 if (/* If there is no definition, we cannot instantiate the
25813 /* If it's OK to postpone instantiation, do so. */
25815 /* If this is a static data member that will be defined
25816 elsewhere, we don't want to instantiate the entire data
25817 member, but we do want to instantiate the initializer so that
25818 we can substitute that elsewhere. */
25819 || (external_p
&& VAR_P (d
))
25820 /* Handle here a deleted function too, avoid generating
25821 its body (c++/61080). */
25824 /* The definition of the static data member is now required so
25825 we must substitute the initializer. */
25827 && !DECL_INITIAL (d
)
25828 && DECL_INITIAL (code_pattern
))
25832 bool const_init
= false;
25833 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25835 ns
= decl_namespace_context (d
);
25836 push_nested_namespace (ns
);
25838 push_nested_class (DECL_CONTEXT (d
));
25839 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25841 tf_warning_or_error
, NULL_TREE
,
25842 /*integral_constant_expression_p=*/false);
25843 /* If instantiating the initializer involved instantiating this
25844 again, don't call cp_finish_decl twice. */
25845 if (!DECL_INITIAL (d
))
25847 /* Make sure the initializer is still constant, in case of
25848 circular dependency (template/instantiate6.C). */
25850 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25851 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25852 /*asmspec_tree=*/NULL_TREE
,
25853 LOOKUP_ONLYCONVERTING
);
25856 pop_nested_class ();
25857 pop_nested_namespace (ns
);
25860 /* We restore the source position here because it's used by
25861 add_pending_template. */
25862 input_location
= saved_loc
;
25864 if (at_eof
&& !pattern_defined
25865 && DECL_EXPLICIT_INSTANTIATION (d
)
25866 && DECL_NOT_REALLY_EXTERN (d
))
25869 The definition of a non-exported function template, a
25870 non-exported member function template, or a non-exported
25871 member function or static data member of a class template
25872 shall be present in every translation unit in which it is
25873 explicitly instantiated. */
25874 permerror (input_location
, "explicit instantiation of %qD "
25875 "but no definition available", d
);
25877 /* If we're in unevaluated context, we just wanted to get the
25878 constant value; this isn't an odr use, so don't queue
25879 a full instantiation. */
25880 if (!cp_unevaluated_operand
25881 /* ??? Historically, we have instantiated inline functions, even
25882 when marked as "extern template". */
25883 && !(external_p
&& VAR_P (d
)))
25884 add_pending_template (d
);
25888 if (variable_template_p (gen_tmpl
))
25889 note_variable_template_instantiation (d
);
25890 instantiate_body (td
, args
, d
, false);
25893 pop_deferring_access_checks ();
25894 timevar_pop (TV_TEMPLATE_INST
);
25895 pop_tinst_level ();
25896 input_location
= saved_loc
;
25897 cp_unevaluated_operand
= saved_unevaluated_operand
;
25898 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25903 /* Run through the list of templates that we wish we could
25904 instantiate, and instantiate any we can. RETRIES is the
25905 number of times we retry pending template instantiation. */
25908 instantiate_pending_templates (int retries
)
25911 location_t saved_loc
= input_location
;
25913 /* Instantiating templates may trigger vtable generation. This in turn
25914 may require further template instantiations. We place a limit here
25915 to avoid infinite loop. */
25916 if (pending_templates
&& retries
>= max_tinst_depth
)
25918 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25920 fatal_error (input_location
,
25921 "template instantiation depth exceeds maximum of %d"
25922 " instantiating %q+D, possibly from virtual table generation"
25923 " (use %<-ftemplate-depth=%> to increase the maximum)",
25924 max_tinst_depth
, decl
);
25925 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25926 /* Pretend that we defined it. */
25927 DECL_INITIAL (decl
) = error_mark_node
;
25933 struct pending_template
**t
= &pending_templates
;
25934 struct pending_template
*last
= NULL
;
25938 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25939 bool complete
= false;
25941 if (TYPE_P (instantiation
))
25943 if (!COMPLETE_TYPE_P (instantiation
))
25945 instantiate_class_template (instantiation
);
25946 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25947 for (tree fld
= TYPE_FIELDS (instantiation
);
25948 fld
; fld
= TREE_CHAIN (fld
))
25950 || (TREE_CODE (fld
) == FUNCTION_DECL
25951 && !DECL_ARTIFICIAL (fld
)))
25952 && DECL_TEMPLATE_INSTANTIATION (fld
))
25953 instantiate_decl (fld
,
25954 /*defer_ok=*/false,
25955 /*expl_inst_class_mem_p=*/false);
25957 if (COMPLETE_TYPE_P (instantiation
))
25961 complete
= COMPLETE_TYPE_P (instantiation
);
25965 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25966 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25969 = instantiate_decl (instantiation
,
25970 /*defer_ok=*/false,
25971 /*expl_inst_class_mem_p=*/false);
25972 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25976 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25977 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25982 /* If INSTANTIATION has been instantiated, then we don't
25983 need to consider it again in the future. */
25984 struct pending_template
*drop
= *t
;
25986 set_refcount_ptr (drop
->tinst
);
25987 pending_template_freelist ().free (drop
);
25995 set_refcount_ptr (current_tinst_level
);
25997 last_pending_template
= last
;
25999 while (reconsider
);
26001 input_location
= saved_loc
;
26004 /* Substitute ARGVEC into T, which is a list of initializers for
26005 either base class or a non-static data member. The TREE_PURPOSEs
26006 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26007 instantiate_decl. */
26010 tsubst_initializer_list (tree t
, tree argvec
)
26012 tree inits
= NULL_TREE
;
26013 tree target_ctor
= error_mark_node
;
26015 for (; t
; t
= TREE_CHAIN (t
))
26019 tree expanded_bases
= NULL_TREE
;
26020 tree expanded_arguments
= NULL_TREE
;
26023 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26028 /* Expand the base class expansion type into separate base
26030 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26031 tf_warning_or_error
,
26033 if (expanded_bases
== error_mark_node
)
26036 /* We'll be building separate TREE_LISTs of arguments for
26038 len
= TREE_VEC_LENGTH (expanded_bases
);
26039 expanded_arguments
= make_tree_vec (len
);
26040 for (i
= 0; i
< len
; i
++)
26041 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26043 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26044 expand each argument in the TREE_VALUE of t. */
26045 expr
= make_node (EXPR_PACK_EXPANSION
);
26046 PACK_EXPANSION_LOCAL_P (expr
) = true;
26047 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26048 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26050 if (TREE_VALUE (t
) == void_type_node
)
26051 /* VOID_TYPE_NODE is used to indicate
26052 value-initialization. */
26054 for (i
= 0; i
< len
; i
++)
26055 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26059 /* Substitute parameter packs into each argument in the
26061 in_base_initializer
= 1;
26062 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26064 tree expanded_exprs
;
26066 /* Expand the argument. */
26067 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26069 = tsubst_pack_expansion (expr
, argvec
,
26070 tf_warning_or_error
,
26072 if (expanded_exprs
== error_mark_node
)
26075 /* Prepend each of the expanded expressions to the
26076 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26077 for (i
= 0; i
< len
; i
++)
26079 TREE_VEC_ELT (expanded_arguments
, i
) =
26080 tree_cons (NULL_TREE
,
26081 TREE_VEC_ELT (expanded_exprs
, i
),
26082 TREE_VEC_ELT (expanded_arguments
, i
));
26085 in_base_initializer
= 0;
26087 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26088 since we built them backwards. */
26089 for (i
= 0; i
< len
; i
++)
26091 TREE_VEC_ELT (expanded_arguments
, i
) =
26092 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26097 for (i
= 0; i
< len
; ++i
)
26099 if (expanded_bases
)
26101 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26102 decl
= expand_member_init (decl
);
26103 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26108 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26109 tf_warning_or_error
, NULL_TREE
);
26111 decl
= expand_member_init (decl
);
26112 if (decl
&& !DECL_P (decl
))
26113 in_base_initializer
= 1;
26115 init
= TREE_VALUE (t
);
26117 if (init
!= void_type_node
)
26118 init
= tsubst_expr (init
, argvec
,
26119 tf_warning_or_error
, NULL_TREE
,
26120 /*integral_constant_expression_p=*/false);
26121 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26122 /* If we had an initializer but it instantiated to nothing,
26123 value-initialize the object. This will only occur when
26124 the initializer was a pack expansion where the parameter
26125 packs used in that expansion were of length zero. */
26126 init
= void_type_node
;
26127 in_base_initializer
= 0;
26130 if (target_ctor
!= error_mark_node
26131 && init
!= error_mark_node
)
26133 error ("mem-initializer for %qD follows constructor delegation",
26137 /* Look for a target constructor. */
26138 if (init
!= error_mark_node
26139 && decl
&& CLASS_TYPE_P (decl
)
26140 && same_type_p (decl
, current_class_type
))
26142 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26145 error ("constructor delegation follows mem-initializer for %qD",
26146 TREE_PURPOSE (inits
));
26149 target_ctor
= init
;
26154 init
= build_tree_list (decl
, init
);
26155 /* Carry over the dummy TREE_TYPE node containing the source
26157 TREE_TYPE (init
) = TREE_TYPE (t
);
26158 TREE_CHAIN (init
) = inits
;
26166 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26169 set_current_access_from_decl (tree decl
)
26171 if (TREE_PRIVATE (decl
))
26172 current_access_specifier
= access_private_node
;
26173 else if (TREE_PROTECTED (decl
))
26174 current_access_specifier
= access_protected_node
;
26176 current_access_specifier
= access_public_node
;
26179 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26180 is the instantiation (which should have been created with
26181 start_enum) and ARGS are the template arguments to use. */
26184 tsubst_enum (tree tag
, tree newtag
, tree args
)
26188 if (SCOPED_ENUM_P (newtag
))
26189 begin_scope (sk_scoped_enum
, newtag
);
26191 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26196 decl
= TREE_VALUE (e
);
26197 /* Note that in a template enum, the TREE_VALUE is the
26198 CONST_DECL, not the corresponding INTEGER_CST. */
26199 value
= tsubst_expr (DECL_INITIAL (decl
),
26200 args
, tf_warning_or_error
, NULL_TREE
,
26201 /*integral_constant_expression_p=*/true);
26203 /* Give this enumeration constant the correct access. */
26204 set_current_access_from_decl (decl
);
26206 /* Actually build the enumerator itself. Here we're assuming that
26207 enumerators can't have dependent attributes. */
26208 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26209 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26212 if (SCOPED_ENUM_P (newtag
))
26215 finish_enum_value_list (newtag
);
26216 finish_enum (newtag
);
26218 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26219 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26222 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26223 its type -- but without substituting the innermost set of template
26224 arguments. So, innermost set of template parameters will appear in
26228 get_mostly_instantiated_function_type (tree decl
)
26230 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26231 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26234 /* Return truthvalue if we're processing a template different from
26235 the last one involved in diagnostics. */
26237 problematic_instantiation_changed (void)
26239 return current_tinst_level
!= last_error_tinst_level
;
26242 /* Remember current template involved in diagnostics. */
26244 record_last_problematic_instantiation (void)
26246 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26249 struct tinst_level
*
26250 current_instantiation (void)
26252 return current_tinst_level
;
26255 /* Return TRUE if current_function_decl is being instantiated, false
26259 instantiating_current_function_p (void)
26261 return (current_instantiation ()
26262 && (current_instantiation ()->maybe_get_node ()
26263 == current_function_decl
));
26266 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26267 type. Return false for ok, true for disallowed. Issue error and
26268 inform messages under control of COMPLAIN. */
26271 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26273 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26275 else if (TYPE_PTR_P (type
))
26277 else if (TYPE_REF_P (type
)
26278 && !TYPE_REF_IS_RVALUE (type
))
26280 else if (TYPE_PTRMEM_P (type
))
26282 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26284 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26286 if (complain
& tf_error
)
26287 error ("non-type template parameters of deduced class type only "
26288 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26293 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26295 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26297 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26299 /* A bound template template parm could later be instantiated to have a valid
26300 nontype parm type via an alias template. */
26301 else if (cxx_dialect
>= cxx11
26302 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26304 else if (VOID_TYPE_P (type
))
26305 /* Fall through. */;
26306 else if (cxx_dialect
>= cxx20
)
26308 if (dependent_type_p (type
))
26310 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26312 if (structural_type_p (type
))
26314 if (complain
& tf_error
)
26316 auto_diagnostic_group d
;
26317 error ("%qT is not a valid type for a template non-type "
26318 "parameter because it is not structural", type
);
26319 structural_type_p (type
, true);
26323 else if (CLASS_TYPE_P (type
))
26325 if (complain
& tf_error
)
26326 error ("non-type template parameters of class type only available "
26327 "with %<-std=c++20%> or %<-std=gnu++20%>");
26331 if (complain
& tf_error
)
26333 if (type
== error_mark_node
)
26334 inform (input_location
, "invalid template non-type parameter");
26336 error ("%q#T is not a valid type for a template non-type parameter",
26342 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26343 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26346 dependent_type_p_r (tree type
)
26352 A type is dependent if it is:
26354 -- a template parameter. Template template parameters are types
26355 for us (since TYPE_P holds true for them) so we handle
26357 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26358 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26360 /* -- a qualified-id with a nested-name-specifier which contains a
26361 class-name that names a dependent type or whose unqualified-id
26362 names a dependent type. */
26363 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26366 /* An alias template specialization can be dependent even if the
26367 resulting type is not. */
26368 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26371 /* -- a cv-qualified type where the cv-unqualified type is
26373 No code is necessary for this bullet; the code below handles
26374 cv-qualified types, and we don't want to strip aliases with
26375 TYPE_MAIN_VARIANT because of DR 1558. */
26376 /* -- a compound type constructed from any dependent type. */
26377 if (TYPE_PTRMEM_P (type
))
26378 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26379 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26381 else if (INDIRECT_TYPE_P (type
))
26382 return dependent_type_p (TREE_TYPE (type
));
26383 else if (FUNC_OR_METHOD_TYPE_P (type
))
26387 if (dependent_type_p (TREE_TYPE (type
)))
26389 for (arg_type
= TYPE_ARG_TYPES (type
);
26391 arg_type
= TREE_CHAIN (arg_type
))
26392 if (dependent_type_p (TREE_VALUE (arg_type
)))
26394 if (cxx_dialect
>= cxx17
)
26395 /* A value-dependent noexcept-specifier makes the type dependent. */
26396 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26397 if (tree noex
= TREE_PURPOSE (spec
))
26398 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26399 affect overload resolution and treating it as dependent breaks
26400 things. Same for an unparsed noexcept expression. */
26401 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26402 && TREE_CODE (noex
) != DEFERRED_PARSE
26403 && value_dependent_expression_p (noex
))
26407 /* -- an array type constructed from any dependent type or whose
26408 size is specified by a constant expression that is
26411 We checked for type- and value-dependence of the bounds in
26412 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26413 if (TREE_CODE (type
) == ARRAY_TYPE
)
26415 if (TYPE_DOMAIN (type
)
26416 && dependent_type_p (TYPE_DOMAIN (type
)))
26418 return dependent_type_p (TREE_TYPE (type
));
26421 /* -- a template-id in which either the template name is a template
26423 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26425 /* ... or any of the template arguments is a dependent type or
26426 an expression that is type-dependent or value-dependent. */
26427 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26428 && (any_dependent_template_arguments_p
26429 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26432 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26433 dependent; if the argument of the `typeof' expression is not
26434 type-dependent, then it should already been have resolved. */
26435 if (TREE_CODE (type
) == TYPEOF_TYPE
26436 || TREE_CODE (type
) == DECLTYPE_TYPE
26437 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26440 /* A template argument pack is dependent if any of its packed
26442 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26444 tree args
= ARGUMENT_PACK_ARGS (type
);
26445 int i
, len
= TREE_VEC_LENGTH (args
);
26446 for (i
= 0; i
< len
; ++i
)
26447 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26451 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26452 be template parameters. */
26453 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26456 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26459 /* The standard does not specifically mention types that are local
26460 to template functions or local classes, but they should be
26461 considered dependent too. For example:
26463 template <int I> void f() {
26468 The size of `E' cannot be known until the value of `I' has been
26469 determined. Therefore, `E' must be considered dependent. */
26470 scope
= TYPE_CONTEXT (type
);
26471 if (scope
&& TYPE_P (scope
))
26472 return dependent_type_p (scope
);
26473 /* Don't use type_dependent_expression_p here, as it can lead
26474 to infinite recursion trying to determine whether a lambda
26475 nested in a lambda is dependent (c++/47687). */
26476 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26477 && DECL_LANG_SPECIFIC (scope
)
26478 && DECL_TEMPLATE_INFO (scope
)
26479 && (any_dependent_template_arguments_p
26480 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26483 /* Other types are non-dependent. */
26487 /* Returns TRUE if TYPE is dependent, in the sense of
26488 [temp.dep.type]. Note that a NULL type is considered dependent. */
26491 dependent_type_p (tree type
)
26493 /* If there are no template parameters in scope, then there can't be
26494 any dependent types. */
26495 if (!processing_template_decl
)
26497 /* If we are not processing a template, then nobody should be
26498 providing us with a dependent type. */
26500 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26504 /* If the type is NULL, we have not computed a type for the entity
26505 in question; in that case, the type is dependent. */
26509 /* Erroneous types can be considered non-dependent. */
26510 if (type
== error_mark_node
)
26513 /* Getting here with global_type_node means we improperly called this
26514 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26515 gcc_checking_assert (type
!= global_type_node
);
26517 /* If we have not already computed the appropriate value for TYPE,
26519 if (!TYPE_DEPENDENT_P_VALID (type
))
26521 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26522 TYPE_DEPENDENT_P_VALID (type
) = 1;
26525 return TYPE_DEPENDENT_P (type
);
26528 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26529 lookup. In other words, a dependent type that is not the current
26533 dependent_scope_p (tree scope
)
26535 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26536 && !currently_open_class (scope
));
26539 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26540 an unknown base of 'this' (and is therefore instantiation-dependent). */
26543 unknown_base_ref_p (tree t
)
26545 if (!current_class_ptr
)
26548 tree mem
= TREE_OPERAND (t
, 1);
26549 if (shared_member_p (mem
))
26552 tree cur
= current_nonlambda_class_type ();
26553 if (!any_dependent_bases_p (cur
))
26556 tree ctx
= TREE_OPERAND (t
, 0);
26557 if (DERIVED_FROM_P (ctx
, cur
))
26563 /* T is a SCOPE_REF; return whether we need to consider it
26564 instantiation-dependent so that we can check access at instantiation
26565 time even though we know which member it resolves to. */
26568 instantiation_dependent_scope_ref_p (tree t
)
26570 if (DECL_P (TREE_OPERAND (t
, 1))
26571 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26572 && !unknown_base_ref_p (t
)
26573 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26574 TREE_OPERAND (t
, 1)))
26580 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26581 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26584 /* Note that this predicate is not appropriate for general expressions;
26585 only constant expressions (that satisfy potential_constant_expression)
26586 can be tested for value dependence. */
26589 value_dependent_expression_p (tree expression
)
26591 if (!processing_template_decl
|| expression
== NULL_TREE
)
26594 /* A type-dependent expression is also value-dependent. */
26595 if (type_dependent_expression_p (expression
))
26598 switch (TREE_CODE (expression
))
26601 /* A dependent member function of the current instantiation. */
26602 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26604 case FUNCTION_DECL
:
26605 /* A dependent member function of the current instantiation. */
26606 if (DECL_CLASS_SCOPE_P (expression
)
26607 && dependent_type_p (DECL_CONTEXT (expression
)))
26611 case IDENTIFIER_NODE
:
26612 /* A name that has not been looked up -- must be dependent. */
26615 case TEMPLATE_PARM_INDEX
:
26616 /* A non-type template parm. */
26620 /* A non-type template parm. */
26621 if (DECL_TEMPLATE_PARM_P (expression
))
26623 return value_dependent_expression_p (DECL_INITIAL (expression
));
26626 /* A constant with literal type and is initialized
26627 with an expression that is value-dependent. */
26628 if (DECL_DEPENDENT_INIT_P (expression
)
26629 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26630 || TYPE_REF_P (TREE_TYPE (expression
)))
26632 if (DECL_HAS_VALUE_EXPR_P (expression
))
26634 tree value_expr
= DECL_VALUE_EXPR (expression
);
26635 if (value_dependent_expression_p (value_expr
)
26636 /* __PRETTY_FUNCTION__ inside a template function is dependent
26637 on the name of the function. */
26638 || (DECL_PRETTY_FUNCTION_P (expression
)
26639 /* It might be used in a template, but not a template
26640 function, in which case its DECL_VALUE_EXPR will be
26642 && value_expr
== error_mark_node
))
26647 case DYNAMIC_CAST_EXPR
:
26648 case STATIC_CAST_EXPR
:
26649 case CONST_CAST_EXPR
:
26650 case REINTERPRET_CAST_EXPR
:
26652 case IMPLICIT_CONV_EXPR
:
26653 /* These expressions are value-dependent if the type to which
26654 the cast occurs is dependent or the expression being casted
26655 is value-dependent. */
26657 tree type
= TREE_TYPE (expression
);
26659 if (dependent_type_p (type
))
26662 /* A functional cast has a list of operands. */
26663 expression
= TREE_OPERAND (expression
, 0);
26666 /* If there are no operands, it must be an expression such
26667 as "int()". This should not happen for aggregate types
26668 because it would form non-constant expressions. */
26669 gcc_assert (cxx_dialect
>= cxx11
26670 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26675 if (TREE_CODE (expression
) == TREE_LIST
)
26676 return any_value_dependent_elements_p (expression
);
26678 return value_dependent_expression_p (expression
);
26682 if (SIZEOF_EXPR_TYPE_P (expression
))
26683 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26687 /* A `sizeof' expression is value-dependent if the operand is
26688 type-dependent or is a pack expansion. */
26689 expression
= TREE_OPERAND (expression
, 0);
26690 if (PACK_EXPANSION_P (expression
))
26692 else if (TYPE_P (expression
))
26693 return dependent_type_p (expression
);
26694 return instantiation_dependent_uneval_expression_p (expression
);
26696 case AT_ENCODE_EXPR
:
26697 /* An 'encode' expression is value-dependent if the operand is
26699 expression
= TREE_OPERAND (expression
, 0);
26700 return dependent_type_p (expression
);
26702 case NOEXCEPT_EXPR
:
26703 expression
= TREE_OPERAND (expression
, 0);
26704 return instantiation_dependent_uneval_expression_p (expression
);
26707 /* All instantiation-dependent expressions should also be considered
26708 value-dependent. */
26709 return instantiation_dependent_scope_ref_p (expression
);
26711 case COMPONENT_REF
:
26712 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26713 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26715 case NONTYPE_ARGUMENT_PACK
:
26716 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26717 is value-dependent. */
26719 tree values
= ARGUMENT_PACK_ARGS (expression
);
26720 int i
, len
= TREE_VEC_LENGTH (values
);
26722 for (i
= 0; i
< len
; ++i
)
26723 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26731 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26733 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26739 if (TREE_CODE (type2
) != TREE_LIST
)
26740 return dependent_type_p (type2
);
26742 for (; type2
; type2
= TREE_CHAIN (type2
))
26743 if (dependent_type_p (TREE_VALUE (type2
)))
26750 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26751 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26754 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26755 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26759 tree op
= TREE_OPERAND (expression
, 0);
26760 return (value_dependent_expression_p (op
)
26761 || has_value_dependent_address (op
));
26764 case REQUIRES_EXPR
:
26765 /* Treat all requires-expressions as value-dependent so
26766 we don't try to fold them. */
26770 return dependent_type_p (TREE_OPERAND (expression
, 0));
26774 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26776 tree fn
= get_callee_fndecl (expression
);
26778 nargs
= call_expr_nargs (expression
);
26779 for (i
= 0; i
< nargs
; ++i
)
26781 tree op
= CALL_EXPR_ARG (expression
, i
);
26782 /* In a call to a constexpr member function, look through the
26783 implicit ADDR_EXPR on the object argument so that it doesn't
26784 cause the call to be considered value-dependent. We also
26785 look through it in potential_constant_expression. */
26786 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26787 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26788 && TREE_CODE (op
) == ADDR_EXPR
)
26789 op
= TREE_OPERAND (op
, 0);
26790 if (value_dependent_expression_p (op
))
26796 case TEMPLATE_ID_EXPR
:
26797 return concept_definition_p (TREE_OPERAND (expression
, 0));
26803 if (dependent_type_p (TREE_TYPE (expression
)))
26805 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26806 if (value_dependent_expression_p (val
))
26812 /* Treat a GNU statement expression as dependent to avoid crashing
26813 under instantiate_non_dependent_expr; it can't be constant. */
26817 /* A constant expression is value-dependent if any subexpression is
26818 value-dependent. */
26819 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26821 case tcc_reference
:
26823 case tcc_comparison
:
26825 case tcc_expression
:
26828 int i
, len
= cp_tree_operand_length (expression
);
26830 for (i
= 0; i
< len
; i
++)
26832 tree t
= TREE_OPERAND (expression
, i
);
26834 /* In some cases, some of the operands may be missing.
26835 (For example, in the case of PREDECREMENT_EXPR, the
26836 amount to increment by may be missing.) That doesn't
26837 make the expression dependent. */
26838 if (t
&& value_dependent_expression_p (t
))
26849 /* The expression is not value-dependent. */
26853 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26854 [temp.dep.expr]. Note that an expression with no type is
26855 considered dependent. Other parts of the compiler arrange for an
26856 expression with type-dependent subexpressions to have no type, so
26857 this function doesn't have to be fully recursive. */
26860 type_dependent_expression_p (tree expression
)
26862 if (!processing_template_decl
)
26865 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26868 STRIP_ANY_LOCATION_WRAPPER (expression
);
26870 /* An unresolved name is always dependent. */
26871 if (identifier_p (expression
)
26872 || TREE_CODE (expression
) == USING_DECL
26873 || TREE_CODE (expression
) == WILDCARD_DECL
)
26876 /* A lambda-expression in template context is dependent. dependent_type_p is
26877 true for a lambda in the scope of a class or function template, but that
26878 doesn't cover all template contexts, like a default template argument. */
26879 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26882 /* A fold expression is type-dependent. */
26883 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26884 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26885 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26886 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26889 /* Some expression forms are never type-dependent. */
26890 if (TREE_CODE (expression
) == SIZEOF_EXPR
26891 || TREE_CODE (expression
) == ALIGNOF_EXPR
26892 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26893 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26894 || TREE_CODE (expression
) == TRAIT_EXPR
26895 || TREE_CODE (expression
) == TYPEID_EXPR
26896 || TREE_CODE (expression
) == DELETE_EXPR
26897 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26898 || TREE_CODE (expression
) == THROW_EXPR
26899 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26902 /* The types of these expressions depends only on the type to which
26903 the cast occurs. */
26904 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26905 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26906 || TREE_CODE (expression
) == CONST_CAST_EXPR
26907 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26908 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26909 || TREE_CODE (expression
) == CAST_EXPR
)
26910 return dependent_type_p (TREE_TYPE (expression
));
26912 /* The types of these expressions depends only on the type created
26913 by the expression. */
26914 if (TREE_CODE (expression
) == NEW_EXPR
26915 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26917 /* For NEW_EXPR tree nodes created inside a template, either
26918 the object type itself or a TREE_LIST may appear as the
26920 tree type
= TREE_OPERAND (expression
, 1);
26921 if (TREE_CODE (type
) == TREE_LIST
)
26922 /* This is an array type. We need to check array dimensions
26924 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26925 || value_dependent_expression_p
26926 (TREE_OPERAND (TREE_VALUE (type
), 1));
26927 /* Array type whose dimension has to be deduced. */
26928 else if (TREE_CODE (type
) == ARRAY_TYPE
26929 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
26932 return dependent_type_p (type
);
26935 if (TREE_CODE (expression
) == SCOPE_REF
)
26937 tree scope
= TREE_OPERAND (expression
, 0);
26938 tree name
= TREE_OPERAND (expression
, 1);
26940 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26941 contains an identifier associated by name lookup with one or more
26942 declarations declared with a dependent type, or...a
26943 nested-name-specifier or qualified-id that names a member of an
26944 unknown specialization. */
26945 return (type_dependent_expression_p (name
)
26946 || dependent_scope_p (scope
));
26949 if (TREE_CODE (expression
) == TEMPLATE_DECL
26950 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26951 return uses_outer_template_parms (expression
);
26953 if (TREE_CODE (expression
) == STMT_EXPR
)
26954 expression
= stmt_expr_value_expr (expression
);
26956 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26961 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26963 if (type_dependent_expression_p (elt
))
26969 /* A static data member of the current instantiation with incomplete
26970 array type is type-dependent, as the definition and specializations
26971 can have different bounds. */
26972 if (VAR_P (expression
)
26973 && DECL_CLASS_SCOPE_P (expression
)
26974 && dependent_type_p (DECL_CONTEXT (expression
))
26975 && VAR_HAD_UNKNOWN_BOUND (expression
))
26978 /* An array of unknown bound depending on a variadic parameter, eg:
26980 template<typename... Args>
26981 void foo (Args... args)
26983 int arr[] = { args... };
26986 template<int... vals>
26989 int arr[] = { vals... };
26992 If the array has no length and has an initializer, it must be that
26993 we couldn't determine its length in cp_complete_array_type because
26994 it is dependent. */
26995 if (VAR_P (expression
)
26996 && TREE_TYPE (expression
) != NULL_TREE
26997 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26998 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26999 && DECL_INITIAL (expression
))
27002 /* A function or variable template-id is type-dependent if it has any
27003 dependent template arguments. */
27004 if (VAR_OR_FUNCTION_DECL_P (expression
)
27005 && DECL_LANG_SPECIFIC (expression
)
27006 && DECL_TEMPLATE_INFO (expression
))
27008 /* Consider the innermost template arguments, since those are the ones
27009 that come from the template-id; the template arguments for the
27010 enclosing class do not make it type-dependent unless they are used in
27011 the type of the decl. */
27012 if (instantiates_primary_template_p (expression
)
27013 && (any_dependent_template_arguments_p
27014 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27018 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27019 type-dependent. Checking this is important for functions with auto return
27020 type, which looks like a dependent type. */
27021 if (TREE_CODE (expression
) == FUNCTION_DECL
27022 && !(DECL_CLASS_SCOPE_P (expression
)
27023 && dependent_type_p (DECL_CONTEXT (expression
)))
27024 && !(DECL_LANG_SPECIFIC (expression
)
27025 && DECL_FRIEND_P (expression
)
27026 && (!DECL_FRIEND_CONTEXT (expression
)
27027 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27028 && !DECL_LOCAL_DECL_P (expression
))
27030 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27031 || undeduced_auto_decl (expression
));
27035 /* Always dependent, on the number of arguments if nothing else. */
27036 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27039 if (TREE_TYPE (expression
) == unknown_type_node
)
27041 if (TREE_CODE (expression
) == ADDR_EXPR
)
27042 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27043 if (TREE_CODE (expression
) == COMPONENT_REF
27044 || TREE_CODE (expression
) == OFFSET_REF
)
27046 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27048 expression
= TREE_OPERAND (expression
, 1);
27049 if (identifier_p (expression
))
27052 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27053 if (TREE_CODE (expression
) == SCOPE_REF
)
27056 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27057 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27058 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27061 if (BASELINK_P (expression
))
27063 if (BASELINK_OPTYPE (expression
)
27064 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27066 expression
= BASELINK_FUNCTIONS (expression
);
27069 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27071 if (any_dependent_template_arguments_p
27072 (TREE_OPERAND (expression
, 1)))
27074 expression
= TREE_OPERAND (expression
, 0);
27075 if (identifier_p (expression
))
27079 gcc_assert (OVL_P (expression
));
27081 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27082 if (type_dependent_expression_p (*iter
))
27088 /* The type of a non-type template parm declared with a placeholder type
27089 depends on the corresponding template argument, even though
27090 placeholders are not normally considered dependent. */
27091 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27092 && is_auto (TREE_TYPE (expression
)))
27095 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27097 /* Dependent type attributes might not have made it from the decl to
27099 if (DECL_P (expression
)
27100 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27103 return (dependent_type_p (TREE_TYPE (expression
)));
27106 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27107 type-dependent if the expression refers to a member of the current
27108 instantiation and the type of the referenced member is dependent, or the
27109 class member access expression refers to a member of an unknown
27112 This function returns true if the OBJECT in such a class member access
27113 expression is of an unknown specialization. */
27116 type_dependent_object_expression_p (tree object
)
27118 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27120 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27122 tree scope
= TREE_TYPE (object
);
27123 return (!scope
|| dependent_scope_p (scope
));
27126 /* walk_tree callback function for instantiation_dependent_expression_p,
27127 below. Returns non-zero if a dependent subexpression is found. */
27130 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27135 /* We don't have to worry about decltype currently because decltype
27136 of an instantiation-dependent expr is a dependent type. This
27137 might change depending on the resolution of DR 1172. */
27138 *walk_subtrees
= false;
27141 enum tree_code code
= TREE_CODE (*tp
);
27144 /* Don't treat an argument list as dependent just because it has no
27148 case NONTYPE_ARGUMENT_PACK
:
27151 case TEMPLATE_PARM_INDEX
:
27152 if (dependent_type_p (TREE_TYPE (*tp
)))
27154 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27156 /* We'll check value-dependence separately. */
27159 /* Handle expressions with type operands. */
27163 case AT_ENCODE_EXPR
:
27165 tree op
= TREE_OPERAND (*tp
, 0);
27166 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27167 op
= TREE_TYPE (op
);
27170 if (dependent_type_p (op
))
27174 *walk_subtrees
= false;
27181 case COMPONENT_REF
:
27182 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27183 /* In a template, finish_class_member_access_expr creates a
27184 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27185 type-dependent, so that we can check access control at
27186 instantiation time (PR 42277). See also Core issue 1273. */
27191 if (instantiation_dependent_scope_ref_p (*tp
))
27196 /* Treat statement-expressions as dependent. */
27200 /* Treat requires-expressions as dependent. */
27201 case REQUIRES_EXPR
:
27205 /* Treat concept checks as dependent. */
27206 if (concept_check_p (*tp
))
27210 case TEMPLATE_ID_EXPR
:
27211 /* Treat concept checks as dependent. */
27212 if (concept_check_p (*tp
))
27217 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27225 if (type_dependent_expression_p (*tp
))
27231 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27232 sense defined by the ABI:
27234 "An expression is instantiation-dependent if it is type-dependent
27235 or value-dependent, or it has a subexpression that is type-dependent
27236 or value-dependent."
27238 Except don't actually check value-dependence for unevaluated expressions,
27239 because in sizeof(i) we don't care about the value of i. Checking
27240 type-dependence will in turn check value-dependence of array bounds/template
27241 arguments as needed. */
27244 instantiation_dependent_uneval_expression_p (tree expression
)
27248 if (!processing_template_decl
)
27251 if (expression
== error_mark_node
)
27254 result
= cp_walk_tree_without_duplicates (&expression
,
27255 instantiation_dependent_r
, NULL
);
27256 return result
!= NULL_TREE
;
27259 /* As above, but also check value-dependence of the expression as a whole. */
27262 instantiation_dependent_expression_p (tree expression
)
27264 return (instantiation_dependent_uneval_expression_p (expression
)
27265 || value_dependent_expression_p (expression
));
27268 /* Like type_dependent_expression_p, but it also works while not processing
27269 a template definition, i.e. during substitution or mangling. */
27272 type_dependent_expression_p_push (tree expr
)
27275 ++processing_template_decl
;
27276 b
= type_dependent_expression_p (expr
);
27277 --processing_template_decl
;
27281 /* Returns TRUE if ARGS contains a type-dependent expression. */
27284 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27289 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27291 if (type_dependent_expression_p (arg
))
27297 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27298 expressions) contains any type-dependent expressions. */
27301 any_type_dependent_elements_p (const_tree list
)
27303 for (; list
; list
= TREE_CHAIN (list
))
27304 if (type_dependent_expression_p (TREE_VALUE (list
)))
27310 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27311 expressions) contains any value-dependent expressions. */
27314 any_value_dependent_elements_p (const_tree list
)
27316 for (; list
; list
= TREE_CHAIN (list
))
27317 if (value_dependent_expression_p (TREE_VALUE (list
)))
27323 /* Returns TRUE if the ARG (a template argument) is dependent. */
27326 dependent_template_arg_p (tree arg
)
27328 if (!processing_template_decl
)
27331 /* Assume a template argument that was wrongly written by the user
27332 is dependent. This is consistent with what
27333 any_dependent_template_arguments_p [that calls this function]
27335 if (!arg
|| arg
== error_mark_node
)
27338 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27339 arg
= argument_pack_select_arg (arg
);
27341 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27343 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27345 if (DECL_TEMPLATE_PARM_P (arg
))
27347 /* A member template of a dependent class is not necessarily
27348 type-dependent, but it is a dependent template argument because it
27349 will be a member of an unknown specialization to that template. */
27350 tree scope
= CP_DECL_CONTEXT (arg
);
27351 return TYPE_P (scope
) && dependent_type_p (scope
);
27353 else if (ARGUMENT_PACK_P (arg
))
27355 tree args
= ARGUMENT_PACK_ARGS (arg
);
27356 int i
, len
= TREE_VEC_LENGTH (args
);
27357 for (i
= 0; i
< len
; ++i
)
27359 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27365 else if (TYPE_P (arg
))
27366 return dependent_type_p (arg
);
27368 return value_dependent_expression_p (arg
);
27371 /* Returns true if ARGS (a collection of template arguments) contains
27372 any types that require structural equality testing. */
27375 any_template_arguments_need_structural_equality_p (tree args
)
27382 if (args
== error_mark_node
)
27385 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27387 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27388 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27390 tree arg
= TREE_VEC_ELT (level
, j
);
27391 tree packed_args
= NULL_TREE
;
27394 if (ARGUMENT_PACK_P (arg
))
27396 /* Look inside the argument pack. */
27397 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27398 len
= TREE_VEC_LENGTH (packed_args
);
27401 for (k
= 0; k
< len
; ++k
)
27404 arg
= TREE_VEC_ELT (packed_args
, k
);
27406 if (error_operand_p (arg
))
27408 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27410 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27412 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27413 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27422 /* Returns true if ARGS (a collection of template arguments) contains
27423 any dependent arguments. */
27426 any_dependent_template_arguments_p (const_tree args
)
27433 if (args
== error_mark_node
)
27436 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27438 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27439 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27440 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27447 /* Returns true if ARGS contains any errors. */
27450 any_erroneous_template_args_p (const_tree args
)
27455 if (args
== error_mark_node
)
27458 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27460 if (tree ti
= get_template_info (args
))
27461 args
= TI_ARGS (ti
);
27469 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27471 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27472 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27473 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27480 /* Returns TRUE if the template TMPL is type-dependent. */
27483 dependent_template_p (tree tmpl
)
27485 if (TREE_CODE (tmpl
) == OVERLOAD
)
27487 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27488 if (dependent_template_p (*iter
))
27493 /* Template template parameters are dependent. */
27494 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27495 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27497 /* So are names that have not been looked up. */
27498 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27503 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27506 dependent_template_id_p (tree tmpl
, tree args
)
27508 return (dependent_template_p (tmpl
)
27509 || any_dependent_template_arguments_p (args
));
27512 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27516 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27520 if (!processing_template_decl
)
27523 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27525 tree decl
= TREE_VEC_ELT (declv
, i
);
27526 tree init
= TREE_VEC_ELT (initv
, i
);
27527 tree cond
= TREE_VEC_ELT (condv
, i
);
27528 tree incr
= TREE_VEC_ELT (incrv
, i
);
27530 if (type_dependent_expression_p (decl
)
27531 || TREE_CODE (decl
) == SCOPE_REF
)
27534 if (init
&& type_dependent_expression_p (init
))
27537 if (cond
== global_namespace
)
27540 if (type_dependent_expression_p (cond
))
27543 if (COMPARISON_CLASS_P (cond
)
27544 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27545 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27548 if (TREE_CODE (incr
) == MODOP_EXPR
)
27550 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27551 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27554 else if (type_dependent_expression_p (incr
))
27556 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27558 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27560 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27562 tree t
= TREE_OPERAND (incr
, 1);
27563 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27564 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27567 /* If this loop has a class iterator with != comparison
27568 with increment other than i++/++i/i--/--i, make sure the
27569 increment is constant. */
27570 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27571 && TREE_CODE (cond
) == NE_EXPR
)
27573 if (TREE_OPERAND (t
, 0) == decl
)
27574 t
= TREE_OPERAND (t
, 1);
27576 t
= TREE_OPERAND (t
, 0);
27577 if (TREE_CODE (t
) != INTEGER_CST
)
27587 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27588 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27589 no such TYPE can be found. Note that this function peers inside
27590 uninstantiated templates and therefore should be used only in
27591 extremely limited situations. ONLY_CURRENT_P restricts this
27592 peering to the currently open classes hierarchy (which is required
27593 when comparing types). */
27596 resolve_typename_type (tree type
, bool only_current_p
)
27605 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27607 scope
= TYPE_CONTEXT (type
);
27608 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27609 gcc_checking_assert (uses_template_parms (scope
));
27611 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27612 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27613 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27614 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27615 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27616 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27617 the TYPENAME_TYPE instead, we avoid messing up with a possible
27618 typedef variant case. */
27619 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27621 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27622 it first before we can figure out what NAME refers to. */
27623 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27625 if (TYPENAME_IS_RESOLVING_P (scope
))
27626 /* Given a class template A with a dependent base with nested type C,
27627 typedef typename A::C::C C will land us here, as trying to resolve
27628 the initial A::C leads to the local C typedef, which leads back to
27629 A::C::C. So we break the recursion now. */
27632 scope
= resolve_typename_type (scope
, only_current_p
);
27634 /* If we don't know what SCOPE refers to, then we cannot resolve the
27636 if (!CLASS_TYPE_P (scope
))
27638 /* If this is a typedef, we don't want to look inside (c++/11987). */
27639 if (typedef_variant_p (type
))
27641 /* If SCOPE isn't the template itself, it will not have a valid
27642 TYPE_FIELDS list. */
27643 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27644 /* scope is either the template itself or a compatible instantiation
27645 like X<T>, so look up the name in the original template. */
27646 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27647 /* If scope has no fields, it can't be a current instantiation. Check this
27648 before currently_open_class to avoid infinite recursion (71515). */
27649 if (!TYPE_FIELDS (scope
))
27651 /* If the SCOPE is not the current instantiation, there's no reason
27652 to look inside it. */
27653 if (only_current_p
&& !currently_open_class (scope
))
27655 /* Enter the SCOPE so that name lookup will be resolved as if we
27656 were in the class definition. In particular, SCOPE will no
27657 longer be considered a dependent type. */
27658 pushed_scope
= push_scope (scope
);
27659 /* Look up the declaration. */
27660 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27661 tf_warning_or_error
);
27663 result
= NULL_TREE
;
27665 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27666 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27667 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27670 else if (identifier_p (fullname
)
27671 && TREE_CODE (decl
) == TYPE_DECL
)
27673 result
= TREE_TYPE (decl
);
27674 if (result
== error_mark_node
)
27675 result
= NULL_TREE
;
27677 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27678 && DECL_CLASS_TEMPLATE_P (decl
))
27680 /* Obtain the template and the arguments. */
27681 tree tmpl
= TREE_OPERAND (fullname
, 0);
27682 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27684 /* We get here with a plain identifier because a previous tentative
27685 parse of the nested-name-specifier as part of a ptr-operator saw
27686 ::template X<A>. The use of ::template is necessary in a
27687 ptr-operator, but wrong in a declarator-id.
27689 [temp.names]: In a qualified-id of a declarator-id, the keyword
27690 template shall not appear at the top level. */
27691 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27692 "keyword %<template%> not allowed in declarator-id");
27695 tree args
= TREE_OPERAND (fullname
, 1);
27696 /* Instantiate the template. */
27697 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27698 /*entering_scope=*/true,
27699 tf_error
| tf_user
);
27700 if (result
== error_mark_node
)
27701 result
= NULL_TREE
;
27704 /* Leave the SCOPE. */
27706 pop_scope (pushed_scope
);
27708 /* If we failed to resolve it, return the original typename. */
27712 /* If lookup found a typename type, resolve that too. */
27713 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27715 /* Ill-formed programs can cause infinite recursion here, so we
27716 must catch that. */
27717 TYPENAME_IS_RESOLVING_P (result
) = 1;
27718 result
= resolve_typename_type (result
, only_current_p
);
27719 TYPENAME_IS_RESOLVING_P (result
) = 0;
27722 /* Qualify the resulting type. */
27723 quals
= cp_type_quals (type
);
27725 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27730 /* EXPR is an expression which is not type-dependent. Return a proxy
27731 for EXPR that can be used to compute the types of larger
27732 expressions containing EXPR. */
27735 build_non_dependent_expr (tree expr
)
27737 tree orig_expr
= expr
;
27740 /* When checking, try to get a constant value for all non-dependent
27741 expressions in order to expose bugs in *_dependent_expression_p
27742 and constexpr. This can affect code generation, see PR70704, so
27743 only do this for -fchecking=2. */
27744 if (flag_checking
> 1
27745 && cxx_dialect
>= cxx11
27746 /* Don't do this during nsdmi parsing as it can lead to
27747 unexpected recursive instantiations. */
27748 && !parsing_nsdmi ()
27749 /* Don't do this during concept processing either and for
27750 the same reason. */
27751 && !processing_constraint_expression_p ())
27752 fold_non_dependent_expr (expr
, tf_none
);
27754 STRIP_ANY_LOCATION_WRAPPER (expr
);
27756 /* Preserve OVERLOADs; the functions must be available to resolve
27759 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27760 inner_expr
= stmt_expr_value_expr (inner_expr
);
27761 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27762 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27763 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27764 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27765 if (is_overloaded_fn (inner_expr
)
27766 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27768 /* There is no need to return a proxy for a variable or enumerator. */
27769 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27771 /* Preserve string constants; conversions from string constants to
27772 "char *" are allowed, even though normally a "const char *"
27773 cannot be used to initialize a "char *". */
27774 if (TREE_CODE (expr
) == STRING_CST
)
27776 /* Preserve void and arithmetic constants, as an optimization -- there is no
27777 reason to create a new node. */
27778 if (TREE_CODE (expr
) == VOID_CST
27779 || TREE_CODE (expr
) == INTEGER_CST
27780 || TREE_CODE (expr
) == REAL_CST
)
27782 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27783 There is at least one place where we want to know that a
27784 particular expression is a throw-expression: when checking a ?:
27785 expression, there are special rules if the second or third
27786 argument is a throw-expression. */
27787 if (TREE_CODE (expr
) == THROW_EXPR
)
27790 /* Don't wrap an initializer list, we need to be able to look inside. */
27791 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27794 /* Don't wrap a dummy object, we need to be able to test for it. */
27795 if (is_dummy_object (expr
))
27798 if (TREE_CODE (expr
) == COND_EXPR
)
27799 return build3 (COND_EXPR
,
27801 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27802 (TREE_OPERAND (expr
, 1)
27803 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27804 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27805 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27806 if (TREE_CODE (expr
) == COMPOUND_EXPR
27807 && !COMPOUND_EXPR_OVERLOADED (expr
))
27808 return build2 (COMPOUND_EXPR
,
27810 TREE_OPERAND (expr
, 0),
27811 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27813 /* If the type is unknown, it can't really be non-dependent */
27814 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27816 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27817 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27818 TREE_TYPE (expr
), expr
);
27821 /* ARGS is a vector of expressions as arguments to a function call.
27822 Replace the arguments with equivalent non-dependent expressions.
27823 This modifies ARGS in place. */
27826 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27831 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27833 tree newarg
= build_non_dependent_expr (arg
);
27835 (*args
)[ix
] = newarg
;
27839 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27840 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27841 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27844 make_auto_1 (tree name
, bool set_canonical
)
27846 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27847 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27848 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27849 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27850 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27851 TYPE_NAME (au
), NULL_TREE
);
27853 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27854 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27855 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27856 if (name
== decltype_auto_identifier
)
27857 AUTO_IS_DECLTYPE (au
) = true;
27863 make_decltype_auto (void)
27865 return make_auto_1 (decltype_auto_identifier
, true);
27871 return make_auto_1 (auto_identifier
, true);
27874 /* Return a C++17 deduction placeholder for class template TMPL. */
27877 make_template_placeholder (tree tmpl
)
27879 tree t
= make_auto_1 (auto_identifier
, false);
27880 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27881 /* Our canonical type depends on the placeholder. */
27882 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27886 /* True iff T is a C++17 class template deduction placeholder. */
27889 template_placeholder_p (tree t
)
27891 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27894 /* Make a "constrained auto" type-specifier. This is an auto or
27895 decltype(auto) type with constraints that must be associated after
27896 deduction. The constraint is formed from the given concept CON
27897 and its optional sequence of template arguments ARGS.
27899 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27902 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27904 /* Build the constraint. */
27905 tree tmpl
= DECL_TI_TEMPLATE (con
);
27907 if (TREE_CODE (con
) == FUNCTION_DECL
)
27908 expr
= ovl_make (tmpl
);
27909 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27911 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27913 /* Our canonical type depends on the constraint. */
27914 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27916 /* Attach the constraint to the type declaration. */
27917 return TYPE_NAME (type
);
27920 /* Make a "constrained auto" type-specifier. */
27923 make_constrained_auto (tree con
, tree args
)
27925 tree type
= make_auto_1 (auto_identifier
, false);
27926 return make_constrained_placeholder_type (type
, con
, args
);
27929 /* Make a "constrained decltype(auto)" type-specifier. */
27932 make_constrained_decltype_auto (tree con
, tree args
)
27934 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27935 return make_constrained_placeholder_type (type
, con
, args
);
27938 /* Build and return a concept definition. Like other templates, the
27939 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27940 the TEMPLATE_DECL. */
27943 finish_concept_definition (cp_expr id
, tree init
)
27945 gcc_assert (identifier_p (id
));
27946 gcc_assert (processing_template_decl
);
27948 location_t loc
= id
.get_location();
27950 /* A concept-definition shall not have associated constraints. */
27951 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27953 error_at (loc
, "a concept cannot be constrained");
27954 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27957 /* A concept-definition shall appear in namespace scope. Templates
27958 aren't allowed in block scope, so we only need to check for class
27960 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27962 error_at (loc
, "concept %qE not in namespace scope", *id
);
27963 return error_mark_node
;
27966 /* Initially build the concept declaration; its type is bool. */
27967 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27968 DECL_CONTEXT (decl
) = current_scope ();
27969 DECL_INITIAL (decl
) = init
;
27971 /* Push the enclosing template. */
27972 return push_template_decl (decl
);
27975 /* Given type ARG, return std::initializer_list<ARG>. */
27980 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27982 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27984 gcc_rich_location
richloc (input_location
);
27985 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27986 error_at (&richloc
,
27987 "deducing from brace-enclosed initializer list"
27988 " requires %<#include <initializer_list>%>");
27990 return error_mark_node
;
27992 tree argvec
= make_tree_vec (1);
27993 TREE_VEC_ELT (argvec
, 0) = arg
;
27995 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27996 NULL_TREE
, 0, tf_warning_or_error
);
27999 /* Replace auto in TYPE with std::initializer_list<auto>. */
28002 listify_autos (tree type
, tree auto_node
)
28004 tree init_auto
= listify (strip_top_quals (auto_node
));
28005 tree argvec
= make_tree_vec (1);
28006 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28007 if (processing_template_decl
)
28008 argvec
= add_to_template_args (current_template_args (), argvec
);
28009 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28012 /* Hash traits for hashing possibly constrained 'auto'
28013 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28015 struct auto_hash
: default_hash_traits
<tree
>
28017 static inline hashval_t
hash (tree
);
28018 static inline bool equal (tree
, tree
);
28021 /* Hash the 'auto' T. */
28024 auto_hash::hash (tree t
)
28026 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28027 /* Matching constrained-type-specifiers denote the same template
28028 parameter, so hash the constraint. */
28029 return hash_placeholder_constraint (c
);
28031 /* But unconstrained autos are all separate, so just hash the pointer. */
28032 return iterative_hash_object (t
, 0);
28035 /* Compare two 'auto's. */
28038 auto_hash::equal (tree t1
, tree t2
)
28043 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28044 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28046 /* Two unconstrained autos are distinct. */
28050 return equivalent_placeholder_constraints (c1
, c2
);
28053 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28054 constrained) auto, add it to the vector. */
28057 extract_autos_r (tree t
, void *data
)
28059 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28062 /* All the autos were built with index 0; fix that up now. */
28063 tree
*p
= hash
.find_slot (t
, INSERT
);
28066 /* If this is a repeated constrained-type-specifier, use the index we
28068 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28071 /* Otherwise this is new, so use the current count. */
28073 idx
= hash
.elements () - 1;
28075 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28078 /* Always keep walking. */
28082 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28083 says they can appear anywhere in the type. */
28086 extract_autos (tree type
)
28088 hash_set
<tree
> visited
;
28089 hash_table
<auto_hash
> hash (2);
28091 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28093 tree tree_vec
= make_tree_vec (hash
.elements());
28094 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28095 iter
!= hash
.end(); ++iter
)
28098 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28099 TREE_VEC_ELT (tree_vec
, i
)
28100 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28106 /* The stem for deduction guide names. */
28107 const char *const dguide_base
= "__dguide_";
28109 /* Return the name for a deduction guide for class template TMPL. */
28112 dguide_name (tree tmpl
)
28114 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28115 tree tname
= TYPE_IDENTIFIER (type
);
28116 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28117 + IDENTIFIER_LENGTH (tname
));
28118 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28119 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28120 IDENTIFIER_LENGTH (tname
) + 1);
28121 tree dname
= get_identifier (buf
);
28122 TREE_TYPE (dname
) = type
;
28126 /* True if NAME is the name of a deduction guide. */
28129 dguide_name_p (tree name
)
28131 return (TREE_CODE (name
) == IDENTIFIER_NODE
28132 && TREE_TYPE (name
)
28133 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28134 strlen (dguide_base
)));
28137 /* True if FN is a deduction guide. */
28140 deduction_guide_p (const_tree fn
)
28143 if (tree name
= DECL_NAME (fn
))
28144 return dguide_name_p (name
);
28148 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28151 copy_guide_p (const_tree fn
)
28153 gcc_assert (deduction_guide_p (fn
));
28154 if (!DECL_ARTIFICIAL (fn
))
28156 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28157 return (TREE_CHAIN (parms
) == void_list_node
28158 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28161 /* True if FN is a guide generated from a constructor template. */
28164 template_guide_p (const_tree fn
)
28166 gcc_assert (deduction_guide_p (fn
));
28167 if (!DECL_ARTIFICIAL (fn
))
28169 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28170 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28171 return PRIMARY_TEMPLATE_P (org
);
28175 /* True if FN is an aggregate initialization guide or the copy deduction
28179 builtin_guide_p (const_tree fn
)
28181 if (!deduction_guide_p (fn
))
28183 if (!DECL_ARTIFICIAL (fn
))
28184 /* Explicitly declared. */
28186 if (DECL_ABSTRACT_ORIGIN (fn
))
28187 /* Derived from a constructor. */
28192 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28193 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28194 template parameter types. Note that the handling of template template
28195 parameters relies on current_template_parms being set appropriately for the
28199 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28200 tree tsubst_args
, tsubst_flags_t complain
)
28202 if (olddecl
== error_mark_node
)
28203 return error_mark_node
;
28205 tree oldidx
= get_template_parm_index (olddecl
);
28208 if (TREE_CODE (olddecl
) == TYPE_DECL
28209 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28211 tree oldtype
= TREE_TYPE (olddecl
);
28212 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28213 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28214 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28215 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28216 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28220 newtype
= TREE_TYPE (olddecl
);
28221 if (type_uses_auto (newtype
))
28223 // Substitute once to fix references to other template parameters.
28224 newtype
= tsubst (newtype
, tsubst_args
,
28225 complain
|tf_partial
, NULL_TREE
);
28226 // Now substitute again to reduce the level of the auto.
28227 newtype
= tsubst (newtype
, current_template_args (),
28228 complain
, NULL_TREE
);
28231 newtype
= tsubst (newtype
, tsubst_args
,
28232 complain
, NULL_TREE
);
28236 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28237 DECL_NAME (olddecl
), newtype
);
28238 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28241 if (TREE_CODE (olddecl
) == TYPE_DECL
28242 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28244 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28245 = build_template_parm_index (index
, level
, level
,
28247 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28248 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28249 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28250 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28251 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28253 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28255 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28257 DECL_TEMPLATE_RESULT (newdecl
)
28258 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28259 DECL_NAME (olddecl
), newtype
);
28260 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28261 // First create a copy (ttargs) of tsubst_args with an
28262 // additional level for the template template parameter's own
28263 // template parameters (ttparms).
28264 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28265 (DECL_TEMPLATE_PARMS (olddecl
)));
28266 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28267 tree ttargs
= make_tree_vec (depth
+ 1);
28268 for (int i
= 0; i
< depth
; ++i
)
28269 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28270 TREE_VEC_ELT (ttargs
, depth
)
28271 = template_parms_level_to_args (ttparms
);
28272 // Substitute ttargs into ttparms to fix references to
28273 // other template parameters.
28274 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28275 complain
|tf_partial
);
28276 // Now substitute again with args based on tparms, to reduce
28277 // the level of the ttparms.
28278 ttargs
= current_template_args ();
28279 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28281 // Finally, tack the adjusted parms onto tparms.
28282 ttparms
= tree_cons (size_int (depth
), ttparms
,
28283 current_template_parms
);
28284 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28289 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28291 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28292 TREE_CODE (oldconst
),
28293 DECL_NAME (oldconst
), newtype
);
28294 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28295 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28296 SET_DECL_TEMPLATE_PARM_P (newconst
);
28297 newidx
= build_template_parm_index (index
, level
, level
,
28298 newconst
, newtype
);
28299 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28300 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28301 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28307 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28308 template parameter. */
28311 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28312 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28314 tree olddecl
= TREE_VALUE (oldelt
);
28315 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28317 if (newdecl
== error_mark_node
)
28318 return error_mark_node
;
28319 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28320 targs
, complain
, NULL_TREE
);
28321 tree list
= build_tree_list (newdef
, newdecl
);
28322 TEMPLATE_PARM_CONSTRAINTS (list
)
28323 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28324 targs
, complain
, NULL_TREE
);
28325 int depth
= TMPL_ARGS_DEPTH (targs
);
28326 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28330 /* Returns a C++17 class deduction guide template based on the constructor
28331 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28332 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28333 aggregate initialization guide. */
28336 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28338 tree tparms
, targs
, fparms
, fargs
, ci
;
28339 bool memtmpl
= false;
28342 tree fn_tmpl
= NULL_TREE
;
28346 ++processing_template_decl
;
28347 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28348 --processing_template_decl
;
28351 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28355 bool copy_p
= TYPE_REF_P (ctor
);
28357 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28359 fparms
= void_list_node
;
28361 else if (TREE_CODE (ctor
) == TREE_LIST
)
28364 gcc_unreachable ();
28366 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28367 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28368 targs
= CLASSTYPE_TI_ARGS (type
);
28371 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28372 explicit_p
= false;
28376 ++processing_template_decl
;
28380 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28381 : DECL_TI_TEMPLATE (ctor
));
28383 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28384 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28386 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28387 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28388 fully specialized args for the enclosing class. Strip those off, as
28389 the deduction guide won't have those template parameters. */
28390 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28391 TMPL_PARMS_DEPTH (tparms
));
28392 /* Discard the 'this' parameter. */
28393 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28394 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28395 ci
= get_constraints (ctor
);
28396 loc
= DECL_SOURCE_LOCATION (ctor
);
28397 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28399 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28403 /* For a member template constructor, we need to flatten the two
28404 template parameter lists into one, and then adjust the function
28405 signature accordingly. This gets...complicated. */
28406 tree save_parms
= current_template_parms
;
28408 /* For a member template we should have two levels of parms/args, one
28409 for the class and one for the constructor. We stripped
28410 specialized args for further enclosing classes above. */
28411 const int depth
= 2;
28412 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28414 /* Template args for translating references to the two-level template
28415 parameters into references to the one-level template parameters we
28417 tree tsubst_args
= copy_node (targs
);
28418 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28419 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28421 /* Template parms for the constructor template. */
28422 tree ftparms
= TREE_VALUE (tparms
);
28423 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28424 /* Template parms for the class template. */
28425 tparms
= TREE_CHAIN (tparms
);
28426 tree ctparms
= TREE_VALUE (tparms
);
28427 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28428 /* Template parms for the deduction guide start as a copy of the
28429 template parms for the class. We set current_template_parms for
28430 lookup_template_class_1. */
28431 current_template_parms
= tparms
= copy_node (tparms
);
28432 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28433 for (unsigned i
= 0; i
< clen
; ++i
)
28434 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28436 /* Now we need to rewrite the constructor parms to append them to the
28438 for (unsigned i
= 0; i
< flen
; ++i
)
28440 unsigned index
= i
+ clen
;
28441 unsigned level
= 1;
28442 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28444 = rewrite_tparm_list (oldelt
, index
, level
,
28445 tsubst_args
, i
, complain
);
28446 if (newelt
== error_mark_node
)
28448 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28451 /* Now we have a final set of template parms to substitute into the
28452 function signature. */
28453 targs
= template_parms_to_args (tparms
);
28454 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28456 if (fparms
== error_mark_node
)
28459 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28461 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28462 cp_unevaluated_operand. */
28464 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28465 current_template_parms
= save_parms
;
28469 /* Substitute in the same arguments to rewrite class members into
28470 references to members of an unknown specialization. */
28472 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28473 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28475 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28478 --processing_template_decl
;
28480 return error_mark_node
;
28485 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28486 tparms
= copy_node (tparms
);
28487 INNERMOST_TEMPLATE_PARMS (tparms
)
28488 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28491 tree fntype
= build_function_type (type
, fparms
);
28492 tree ded_fn
= build_lang_decl_loc (loc
,
28494 dguide_name (type
), fntype
);
28495 DECL_ARGUMENTS (ded_fn
) = fargs
;
28496 DECL_ARTIFICIAL (ded_fn
) = true;
28497 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28498 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28499 DECL_ARTIFICIAL (ded_tmpl
) = true;
28500 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28501 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28503 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28505 set_constraints (ded_tmpl
, ci
);
28510 /* Add to LIST the member types for the reshaped initializer CTOR. */
28513 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28515 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28516 tree idx
, val
; unsigned i
;
28517 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28519 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28520 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28521 && CONSTRUCTOR_NELTS (val
)
28522 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28523 type gets a single initializer. */
28524 && CP_AGGREGATE_TYPE_P (ftype
)
28525 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28526 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28528 tree subelt
= NULL_TREE
;
28529 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28530 subelt
= TREE_TYPE (ftype
);
28531 list
= collect_ctor_idx_types (val
, list
, subelt
);
28534 tree arg
= NULL_TREE
;
28535 if (i
== v
->length() - 1
28536 && PACK_EXPANSION_P (ftype
))
28537 /* Give the trailing pack expansion parameter a default argument to
28538 match aggregate initialization behavior, even if we deduce the
28539 length of the pack separately to more than we have initializers. */
28540 arg
= build_constructor (init_list_type_node
, NULL
);
28541 /* if ei is of array type and xi is a braced-init-list or string literal,
28542 Ti is an rvalue reference to the declared type of ei */
28543 STRIP_ANY_LOCATION_WRAPPER (val
);
28544 if (TREE_CODE (ftype
) == ARRAY_TYPE
28545 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28546 || TREE_CODE (val
) == STRING_CST
))
28548 if (TREE_CODE (val
) == STRING_CST
)
28549 ftype
= cp_build_qualified_type
28550 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28551 ftype
= (cp_build_reference_type
28552 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28554 list
= tree_cons (arg
, ftype
, list
);
28560 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28563 is_spec_or_derived (tree etype
, tree tmpl
)
28565 if (!etype
|| !CLASS_TYPE_P (etype
))
28568 tree type
= TREE_TYPE (tmpl
);
28569 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28570 (DECL_TEMPLATE_PARMS (tmpl
)));
28571 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28572 int err
= unify (tparms
, targs
, type
, etype
,
28573 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28578 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28582 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28584 if (cxx_dialect
< cxx20
)
28587 if (init
== NULL_TREE
)
28590 tree type
= TREE_TYPE (tmpl
);
28591 if (!CP_AGGREGATE_TYPE_P (type
))
28594 /* No aggregate candidate for copy-initialization. */
28595 if (args
->length() == 1)
28597 tree val
= (*args
)[0];
28598 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28602 /* If we encounter a problem, we just won't add the candidate. */
28603 tsubst_flags_t complain
= tf_none
;
28605 tree parms
= NULL_TREE
;
28606 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28608 init
= reshape_init (type
, init
, complain
);
28609 if (init
== error_mark_node
)
28611 parms
= collect_ctor_idx_types (init
, parms
);
28613 else if (TREE_CODE (init
) == TREE_LIST
)
28615 int len
= list_length (init
);
28616 for (tree field
= TYPE_FIELDS (type
);
28618 --len
, field
= DECL_CHAIN (field
))
28620 field
= next_initializable_field (field
);
28623 tree ftype
= finish_decltype_type (field
, true, complain
);
28624 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28628 /* Aggregate initialization doesn't apply to an initializer expression. */
28634 parms
= nreverse (parms
);
28635 TREE_CHAIN (last
) = void_list_node
;
28636 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28643 /* UGUIDES are the deduction guides for the underlying template of alias
28644 template TMPL; adjust them to be deduction guides for TMPL. */
28647 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28649 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28650 class type (9.2.8.2) where the template-name names an alias template A,
28651 the defining-type-id of A must be of the form
28653 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28655 as specified in 9.2.8.2. The guides of A are the set of functions or
28656 function templates formed as follows. For each function or function
28657 template f in the guides of the template named by the simple-template-id
28658 of the defining-type-id, the template arguments of the return type of f
28659 are deduced from the defining-type-id of A according to the process in
28660 13.10.2.5 with the exception that deduction does not fail if not all
28661 template arguments are deduced. Let g denote the result of substituting
28662 these deductions into f. If substitution succeeds, form a function or
28663 function template f' with the following properties and add it to the set
28666 * The function type of f' is the function type of g.
28668 * If f is a function template, f' is a function template whose template
28669 parameter list consists of all the template parameters of A (including
28670 their default template arguments) that appear in the above deductions or
28671 (recursively) in their default template arguments, followed by the
28672 template parameters of f that were not deduced (including their default
28673 template arguments), otherwise f' is not a function template.
28675 * The associated constraints (13.5.2) are the conjunction of the
28676 associated constraints of g and a constraint that is satisfied if and only
28677 if the arguments of A are deducible (see below) from the return type.
28679 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28682 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28683 considered to be so as well.
28685 * The explicit-specifier of f' is the explicit-specifier of g (if
28688 /* This implementation differs from the above in two significant ways:
28690 1) We include all template parameters of A, not just some.
28691 2) The added constraint is same_type instead of deducible.
28693 I believe that while it's probably possible to construct a testcase that
28694 behaves differently with this simplification, it should have the same
28695 effect for real uses. Including all template parameters means that we
28696 deduce all parameters of A when resolving the call, so when we're in the
28697 constraint we don't need to deduce them again, we can just check whether
28698 the deduction produced the desired result. */
28700 tsubst_flags_t complain
= tf_warning_or_error
;
28701 tree atype
= TREE_TYPE (tmpl
);
28702 tree aguides
= NULL_TREE
;
28703 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28704 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28705 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28706 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28710 location_t loc
= DECL_SOURCE_LOCATION (f
);
28711 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28713 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28715 processing_template_decl_sentinel
ptds (/*reset*/false);
28716 ++processing_template_decl
;
28718 /* Deduce template arguments for f from the type-id of A. */
28719 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28720 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28721 tree targs
= make_tree_vec (len
);
28722 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28725 /* The number of parms for f' is the number of parms for A plus
28726 non-deduced parms of f. */
28727 unsigned ndlen
= 0;
28729 for (unsigned i
= 0; i
< len
; ++i
)
28730 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28732 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28734 /* First copy over the parms of A. */
28735 for (j
= 0; j
< natparms
; ++j
)
28736 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28737 /* Now rewrite the non-deduced parms of f. */
28738 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28739 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28742 unsigned index
= j
++;
28743 unsigned level
= 1;
28744 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28745 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28746 targs
, i
, complain
);
28747 TREE_VEC_ELT (gtparms
, index
) = list
;
28749 gtparms
= build_tree_list (size_one_node
, gtparms
);
28751 /* Substitute the deduced arguments plus the rewritten template
28752 parameters into f to get g. This covers the type, copyness,
28753 guideness, and explicit-specifier. */
28754 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28755 if (g
== error_mark_node
)
28756 return error_mark_node
;
28757 DECL_USE_TEMPLATE (g
) = 0;
28758 fprime
= build_template_decl (g
, gtparms
, false);
28759 DECL_TEMPLATE_RESULT (fprime
) = g
;
28760 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28761 tree gtargs
= template_parms_to_args (gtparms
);
28762 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28763 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28765 /* Substitute the associated constraints. */
28766 tree ci
= get_constraints (f
);
28768 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28769 if (ci
== error_mark_node
)
28770 return error_mark_node
;
28772 /* Add a constraint that the return type matches the instantiation of
28773 A with the same template arguments. */
28774 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28775 if (!same_type_p (atype
, ret
)
28776 /* FIXME this should mean they don't compare as equivalent. */
28777 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28779 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28780 ci
= append_constraint (ci
, same
);
28785 remove_constraints (fprime
);
28786 set_constraints (fprime
, ci
);
28791 /* For a non-template deduction guide, if the arguments of A aren't
28792 deducible from the return type, don't add the candidate. */
28793 tree targs
= make_tree_vec (natparms
);
28794 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28795 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28796 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28802 aguides
= lookup_add (fprime
, aguides
);
28808 /* Return artificial deduction guides built from the constructors of class
28812 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28814 tree type
= TREE_TYPE (tmpl
);
28815 tree outer_args
= NULL_TREE
;
28816 if (DECL_CLASS_SCOPE_P (tmpl
)
28817 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28819 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28820 type
= TREE_TYPE (most_general_template (tmpl
));
28823 tree cands
= NULL_TREE
;
28825 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28827 /* Skip inherited constructors. */
28828 if (iter
.using_p ())
28831 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28832 cands
= lookup_add (guide
, cands
);
28835 /* Add implicit default constructor deduction guide. */
28836 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28838 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28840 cands
= lookup_add (guide
, cands
);
28843 /* Add copy guide. */
28845 tree gtype
= build_reference_type (type
);
28846 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28848 cands
= lookup_add (guide
, cands
);
28854 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28856 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28857 aggregate candidate is added separately because it depends on the
28858 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28862 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
28864 tree guides
= NULL_TREE
;
28865 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28867 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28868 tree tinfo
= get_template_info (under
);
28869 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
28874 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28875 dguide_name (tmpl
),
28876 LOOK_want::NORMAL
, /*complain*/false);
28877 if (guides
== error_mark_node
)
28878 guides
= NULL_TREE
;
28880 any_dguides_p
= true;
28883 /* Cache the deduction guides for a template. We also remember the result of
28884 lookup, and rebuild everything if it changes; should be very rare. */
28885 tree_pair_p cache
= NULL
;
28887 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28890 if (cache
->purpose
== guides
)
28891 return cache
->value
;
28895 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28896 cache
->purpose
= guides
;
28899 tree cands
= NULL_TREE
;
28900 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28901 cands
= alias_ctad_tweaks (tmpl
, guides
);
28904 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28905 for (ovl_iterator
it (guides
); it
; ++it
)
28906 cands
= lookup_add (*it
, cands
);
28909 cache
->value
= cands
;
28913 /* Return whether TMPL is a (class template argument-) deducible template. */
28916 ctad_template_p (tree tmpl
)
28918 /* A deducible template is either a class template or is an alias template
28919 whose defining-type-id is of the form
28921 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28923 where the nested-name-specifier (if any) is non-dependent and the
28924 template-name of the simple-template-id names a deducible template. */
28926 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28927 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28929 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28931 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28932 if (tree tinfo
= get_template_info (orig
))
28933 return ctad_template_p (TI_TEMPLATE (tinfo
));
28937 /* Deduce template arguments for the class template placeholder PTYPE for
28938 template TMPL based on the initializer INIT, and return the resulting
28942 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28943 int flags
, tsubst_flags_t complain
)
28945 /* We should have handled this in the caller. */
28946 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28949 /* Look through alias templates that just rename another template. */
28950 tmpl
= get_underlying_template (tmpl
);
28951 if (!ctad_template_p (tmpl
))
28953 if (complain
& tf_error
)
28954 error ("non-deducible template %qT used without template arguments", tmpl
);
28955 return error_mark_node
;
28957 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28959 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28960 if (complain
& tf_warning_or_error
)
28961 pedwarn (input_location
, 0, "alias template deduction only available "
28962 "with %<-std=c++20%> or %<-std=gnu++20%>");
28965 if (init
&& TREE_TYPE (init
) == ptype
)
28966 /* Using the template parm as its own argument. */
28969 tree type
= TREE_TYPE (tmpl
);
28971 bool try_list_ctor
= false;
28972 bool list_init_p
= false;
28974 releasing_vec rv_args
= NULL
;
28975 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28976 if (init
== NULL_TREE
)
28977 args
= make_tree_vector ();
28978 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28980 list_init_p
= true;
28981 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28982 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28984 /* As an exception, the first phase in 16.3.1.7 (considering the
28985 initializer list as a single argument) is omitted if the
28986 initializer list consists of a single expression of type cv U,
28987 where U is a specialization of C or a class derived from a
28988 specialization of C. */
28989 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28990 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28991 try_list_ctor
= false;
28993 if (try_list_ctor
|| is_std_init_list (type
))
28994 args
= make_tree_vector_single (init
);
28996 args
= make_tree_vector_from_ctor (init
);
28998 else if (TREE_CODE (init
) == TREE_LIST
)
28999 args
= make_tree_vector_from_list (init
);
29001 args
= make_tree_vector_single (init
);
29003 /* Do this now to avoid problems with erroneous args later on. */
29004 args
= resolve_args (args
, complain
);
29006 return error_mark_node
;
29008 bool any_dguides_p
= false;
29009 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29010 if (cands
== error_mark_node
)
29011 return error_mark_node
;
29013 /* Prune explicit deduction guides in copy-initialization context (but
29014 not copy-list-initialization). */
29015 bool elided
= false;
29016 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
29018 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29019 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29024 /* Found a nonconverting guide, prune the candidates. */
29025 tree pruned
= NULL_TREE
;
29026 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29027 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29028 pruned
= lookup_add (*iter
, pruned
);
29034 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29035 cands
= lookup_add (guide
, cands
);
29037 tree call
= error_mark_node
;
29039 /* If this is list-initialization and the class has a list constructor, first
29040 try deducing from the list as a single argument, as [over.match.list]. */
29041 tree list_cands
= NULL_TREE
;
29042 if (try_list_ctor
&& cands
)
29043 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29046 if (is_list_ctor (dg
))
29047 list_cands
= lookup_add (dg
, list_cands
);
29051 ++cp_unevaluated_operand
;
29052 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29053 --cp_unevaluated_operand
;
29055 if (call
== error_mark_node
)
29057 /* That didn't work, now try treating the list as a sequence of
29059 release_tree_vector (args
);
29060 args
= make_tree_vector_from_ctor (init
);
29064 if (elided
&& !cands
)
29066 error ("cannot deduce template arguments for copy-initialization"
29067 " of %qT, as it has no non-explicit deduction guides or "
29068 "user-declared constructors", type
);
29069 return error_mark_node
;
29071 else if (!cands
&& call
== error_mark_node
)
29073 error ("cannot deduce template arguments of %qT, as it has no viable "
29074 "deduction guides", type
);
29075 return error_mark_node
;
29078 if (call
== error_mark_node
)
29080 ++cp_unevaluated_operand
;
29081 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29082 --cp_unevaluated_operand
;
29085 if (call
== error_mark_node
)
29087 if (complain
& tf_warning_or_error
)
29089 error ("class template argument deduction failed:");
29091 ++cp_unevaluated_operand
;
29092 call
= build_new_function_call (cands
, &args
,
29093 complain
| tf_decltype
);
29094 --cp_unevaluated_operand
;
29097 inform (input_location
, "explicit deduction guides not considered "
29098 "for copy-initialization");
29100 return error_mark_node
;
29102 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29103 constructor is chosen, the initialization is ill-formed. */
29104 else if (flags
& LOOKUP_ONLYCONVERTING
)
29106 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29107 if (fndecl
&& DECL_NONCONVERTING_P (fndecl
))
29109 if (complain
& tf_warning_or_error
)
29111 // TODO: Pass down location from cp_finish_decl.
29112 error ("class template argument deduction for %qT failed: "
29113 "explicit deduction guide selected in "
29114 "copy-list-initialization", type
);
29115 inform (DECL_SOURCE_LOCATION (fndecl
),
29116 "explicit deduction guide declared here");
29119 return error_mark_node
;
29123 /* If CTAD succeeded but the type doesn't have any explicit deduction
29124 guides, this deduction might not be what the user intended. */
29125 if (call
!= error_mark_node
&& !any_dguides_p
)
29127 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29128 if (fndecl
!= NULL_TREE
29129 && (!DECL_IN_SYSTEM_HEADER (fndecl
)
29130 || global_dc
->dc_warn_system_headers
)
29131 && warning (OPT_Wctad_maybe_unsupported
,
29132 "%qT may not intend to support class template argument "
29133 "deduction", type
))
29134 inform (input_location
, "add a deduction guide to suppress this "
29138 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29141 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29142 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29143 The CONTEXT determines the context in which auto deduction is performed
29144 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29145 OUTER_TARGS are used during template argument deduction
29146 (context == adc_unify) to properly substitute the result, and is ignored
29149 For partial-concept-ids, extra args may be appended to the list of deduced
29150 template arguments prior to determining constraint satisfaction. */
29153 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29154 tsubst_flags_t complain
, auto_deduction_context context
,
29155 tree outer_targs
, int flags
)
29159 if (init
== error_mark_node
)
29160 return error_mark_node
;
29162 if (init
&& type_dependent_expression_p (init
)
29163 && context
!= adc_unify
)
29164 /* Defining a subset of type-dependent expressions that we can deduce
29165 from ahead of time isn't worth the trouble. */
29168 /* Similarly, we can't deduce from another undeduced decl. */
29169 if (init
&& undeduced_auto_decl (init
))
29172 /* We may be doing a partial substitution, but we still want to replace
29174 complain
&= ~tf_partial
;
29176 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29177 /* C++17 class template argument deduction. */
29178 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29180 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29181 /* Nothing we can do with this, even in deduction context. */
29184 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29185 with either a new invented type template parameter U or, if the
29186 initializer is a braced-init-list (8.5.4), with
29187 std::initializer_list<U>. */
29188 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29190 if (!DIRECT_LIST_INIT_P (init
))
29191 type
= listify_autos (type
, auto_node
);
29192 else if (CONSTRUCTOR_NELTS (init
) == 1)
29193 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29196 if (complain
& tf_warning_or_error
)
29198 if (permerror (input_location
, "direct-list-initialization of "
29199 "%<auto%> requires exactly one element"))
29200 inform (input_location
,
29201 "for deduction to %<std::initializer_list%>, use copy-"
29202 "list-initialization (i.e. add %<=%> before the %<{%>)");
29204 type
= listify_autos (type
, auto_node
);
29208 if (type
== error_mark_node
)
29209 return error_mark_node
;
29211 init
= resolve_nondeduced_context (init
, complain
);
29213 if (context
== adc_decomp_type
29214 && auto_node
== type
29215 && init
!= error_mark_node
29216 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29217 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29218 and initializer has array type, deduce cv-qualified array type. */
29219 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29221 else if (AUTO_IS_DECLTYPE (auto_node
))
29223 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29224 bool id
= (DECL_P (stripped_init
)
29225 || ((TREE_CODE (init
) == COMPONENT_REF
29226 || TREE_CODE (init
) == SCOPE_REF
)
29227 && !REF_PARENTHESIZED_P (init
)));
29228 targs
= make_tree_vec (1);
29229 TREE_VEC_ELT (targs
, 0)
29230 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29231 if (type
!= auto_node
)
29233 if (complain
& tf_error
)
29234 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29235 return error_mark_node
;
29237 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29239 if (complain
& tf_error
)
29240 error ("%<decltype(auto)%> cannot be cv-qualified");
29241 return error_mark_node
;
29246 if (error_operand_p (init
))
29247 return error_mark_node
;
29249 tree parms
= build_tree_list (NULL_TREE
, type
);
29253 tparms
= extract_autos (type
);
29256 tparms
= make_tree_vec (1);
29257 TREE_VEC_ELT (tparms
, 0)
29258 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29261 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29262 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29264 NULL
, /*explain_p=*/false);
29267 if (processing_template_decl
)
29268 /* Try again at instantiation time. */
29270 if (type
&& type
!= error_mark_node
29271 && (complain
& tf_error
))
29272 /* If type is error_mark_node a diagnostic must have been
29273 emitted by now. Also, having a mention to '<type error>'
29274 in the diagnostic is not really useful to the user. */
29277 && FNDECL_USED_AUTO (current_function_decl
)
29279 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29280 && LAMBDA_FUNCTION_P (current_function_decl
))
29281 error ("unable to deduce lambda return type from %qE", init
);
29283 error ("unable to deduce %qT from %qE", type
, init
);
29284 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29286 NULL
, /*explain_p=*/true);
29288 return error_mark_node
;
29292 /* Check any placeholder constraints against the deduced type. */
29293 if (flag_concepts
&& !processing_template_decl
)
29294 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29296 /* Use the deduced type to check the associated constraints. If we
29297 have a partial-concept-id, rebuild the argument list so that
29298 we check using the extra arguments. */
29299 check
= unpack_concept_check (check
);
29300 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29301 tree
cdecl = TREE_OPERAND (check
, 0);
29303 cdecl = OVL_FIRST (cdecl);
29304 tree cargs
= TREE_OPERAND (check
, 1);
29305 if (TREE_VEC_LENGTH (cargs
) > 1)
29307 cargs
= copy_node (cargs
);
29308 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29313 /* Rebuild the check using the deduced arguments. */
29314 check
= build_concept_check (cdecl, cargs
, tf_none
);
29316 if (!constraints_satisfied_p (check
))
29318 if (complain
& tf_warning_or_error
)
29320 auto_diagnostic_group d
;
29323 case adc_unspecified
:
29325 error("placeholder constraints not satisfied");
29327 case adc_variable_type
:
29328 case adc_decomp_type
:
29329 error ("deduced initializer does not satisfy "
29330 "placeholder constraints");
29332 case adc_return_type
:
29333 error ("deduced return type does not satisfy "
29334 "placeholder constraints");
29336 case adc_requirement
:
29337 error ("deduced expression type does not satisfy "
29338 "placeholder constraints");
29341 diagnose_constraints (input_location
, check
, targs
);
29343 return error_mark_node
;
29347 if (processing_template_decl
&& context
!= adc_unify
)
29348 outer_targs
= current_template_args ();
29349 targs
= add_to_template_args (outer_targs
, targs
);
29350 return tsubst (type
, targs
, complain
, NULL_TREE
);
29353 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29357 splice_late_return_type (tree type
, tree late_return_type
)
29359 if (late_return_type
)
29361 gcc_assert (is_auto (type
) || seen_error ());
29362 return late_return_type
;
29365 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29367 tree idx
= get_template_parm_index (*auto_node
);
29368 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29370 /* In an abbreviated function template we didn't know we were dealing
29371 with a function template when we saw the auto return type, so update
29372 it to have the correct level. */
29373 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29374 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29375 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29376 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29377 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29378 *auto_node
= new_auto
;
29384 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29385 'decltype(auto)' or a deduced class template. */
29388 is_auto (const_tree type
)
29390 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29391 && (TYPE_IDENTIFIER (type
) == auto_identifier
29392 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29398 /* for_each_template_parm callback for type_uses_auto. */
29401 is_auto_r (tree tp
, void */
*data*/
)
29403 return is_auto (tp
);
29406 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29407 a use of `auto'. Returns NULL_TREE otherwise. */
29410 type_uses_auto (tree type
)
29412 if (type
== NULL_TREE
)
29414 else if (flag_concepts
)
29416 /* The Concepts TS allows multiple autos in one type-specifier; just
29417 return the first one we find, do_auto_deduction will collect all of
29419 if (uses_template_parms (type
))
29420 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29421 /*visited*/NULL
, /*nondeduced*/false);
29425 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29431 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29432 concepts are enabled, auto is acceptable in template arguments, but
29433 only when TEMPL identifies a template class. Return TRUE if any
29434 such errors were reported. */
29437 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29439 /* If there were previous errors, nevermind. */
29440 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29443 /* If TMPL is an identifier, we're parsing and we can't tell yet
29444 whether TMPL is supposed to be a type, a function or a variable.
29445 We'll only be able to tell during template substitution, so we
29446 expect to be called again then. If concepts are enabled and we
29447 know we have a type, we're ok. */
29449 && (identifier_p (tmpl
)
29451 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29452 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29455 /* Quickly search for any occurrences of auto; usually there won't
29456 be any, and then we'll avoid allocating the vector. */
29457 if (!type_uses_auto (args
))
29460 bool errors
= false;
29462 tree vec
= extract_autos (args
);
29463 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29465 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29466 error_at (DECL_SOURCE_LOCATION (xauto
),
29467 "invalid use of %qT in template argument", xauto
);
29474 /* Recursively walk over && expressions searching for EXPR. Return a reference
29475 to that expression. */
29477 static tree
*find_template_requirement (tree
*t
, tree key
)
29481 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29483 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29485 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29491 /* Convert the generic type parameters in PARM that match the types given in the
29492 range [START_IDX, END_IDX) from the current_template_parms into generic type
29496 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29498 tree current
= current_template_parms
;
29499 int depth
= TMPL_PARMS_DEPTH (current
);
29500 current
= INNERMOST_TEMPLATE_PARMS (current
);
29501 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29503 for (int i
= 0; i
< start_idx
; ++i
)
29504 TREE_VEC_ELT (replacement
, i
)
29505 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29507 for (int i
= start_idx
; i
< end_idx
; ++i
)
29509 /* Create a distinct parameter pack type from the current parm and add it
29510 to the replacement args to tsubst below into the generic function
29512 tree node
= TREE_VEC_ELT (current
, i
);
29513 tree o
= TREE_TYPE (TREE_VALUE (node
));
29514 tree t
= copy_type (o
);
29515 TEMPLATE_TYPE_PARM_INDEX (t
)
29516 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29518 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29519 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29520 TYPE_MAIN_VARIANT (t
) = t
;
29521 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29522 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29523 TREE_VEC_ELT (replacement
, i
) = t
;
29525 /* Replace the current template parameter with new pack. */
29526 TREE_VALUE (node
) = TREE_CHAIN (t
);
29528 /* Surgically adjust the associated constraint of adjusted parameter
29529 and it's corresponding contribution to the current template
29531 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29533 tree id
= unpack_concept_check (constr
);
29534 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
29535 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29536 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29538 /* If there was a constraint, we also need to replace that in
29539 the template requirements, which we've already built. */
29540 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29541 reqs
= find_template_requirement (reqs
, constr
);
29546 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29547 TREE_VEC_ELT (replacement
, i
)
29548 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29550 /* If there are more levels then build up the replacement with the outer
29553 replacement
= add_to_template_args (template_parms_to_args
29554 (TREE_CHAIN (current_template_parms
)),
29557 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29560 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29564 declare_integer_pack (void)
29566 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29567 build_function_type_list (integer_type_node
,
29570 NULL_TREE
, ECF_CONST
);
29571 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29572 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29573 CP_BUILT_IN_INTEGER_PACK
);
29576 /* Set up the hash tables for template instantiations. */
29579 init_template_processing (void)
29581 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29582 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29584 if (cxx_dialect
>= cxx11
)
29585 declare_integer_pack ();
29588 /* Print stats about the template hash tables for -fstats. */
29591 print_template_statistics (void)
29593 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29594 "%f collisions\n", (long) decl_specializations
->size (),
29595 (long) decl_specializations
->elements (),
29596 decl_specializations
->collisions ());
29597 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29598 "%f collisions\n", (long) type_specializations
->size (),
29599 (long) type_specializations
->elements (),
29600 type_specializations
->collisions ());
29605 namespace selftest
{
29607 /* Verify that build_non_dependent_expr () works, for various expressions,
29608 and that location wrappers don't affect the results. */
29611 test_build_non_dependent_expr ()
29613 location_t loc
= BUILTINS_LOCATION
;
29615 /* Verify constants, without and with location wrappers. */
29616 tree int_cst
= build_int_cst (integer_type_node
, 42);
29617 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29619 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29620 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29621 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29623 tree string_lit
= build_string (4, "foo");
29624 TREE_TYPE (string_lit
) = char_array_type_node
;
29625 string_lit
= fix_string_type (string_lit
);
29626 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29628 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29629 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29630 ASSERT_EQ (wrapped_string_lit
,
29631 build_non_dependent_expr (wrapped_string_lit
));
29634 /* Verify that type_dependent_expression_p () works correctly, even
29635 in the presence of location wrapper nodes. */
29638 test_type_dependent_expression_p ()
29640 location_t loc
= BUILTINS_LOCATION
;
29642 tree name
= get_identifier ("foo");
29644 /* If no templates are involved, nothing is type-dependent. */
29645 gcc_assert (!processing_template_decl
);
29646 ASSERT_FALSE (type_dependent_expression_p (name
));
29648 ++processing_template_decl
;
29650 /* Within a template, an unresolved name is always type-dependent. */
29651 ASSERT_TRUE (type_dependent_expression_p (name
));
29653 /* Ensure it copes with NULL_TREE and errors. */
29654 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29655 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29657 /* A USING_DECL in a template should be type-dependent, even if wrapped
29658 with a location wrapper (PR c++/83799). */
29659 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29660 TREE_TYPE (using_decl
) = integer_type_node
;
29661 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29662 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29663 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29664 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29666 --processing_template_decl
;
29669 /* Run all of the selftests within this file. */
29674 test_build_non_dependent_expr ();
29675 test_type_dependent_expression_p ();
29678 } // namespace selftest
29680 #endif /* #if CHECKING_P */
29682 #include "gt-cp-pt.h"