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_UNIQUE_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 (VAR_OR_FUNCTION_DECL_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_UNDECLARED_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 if (TREE_CODE (t
) == TYPE_DECL
)
4236 ppd
.parameter_packs
= ¶meter_packs
;
4237 ppd
.visited
= new hash_set
<tree
>;
4238 ppd
.type_pack_expansion_p
= false;
4239 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4242 /* It's OK for a lambda to have an unexpanded parameter pack from the
4243 containing context, but do complain about unexpanded capture packs. */
4244 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4245 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4246 for (; parameter_packs
;
4247 parameter_packs
= TREE_CHAIN (parameter_packs
))
4249 tree pack
= TREE_VALUE (parameter_packs
);
4250 if (is_capture_proxy (pack
))
4254 if (parameter_packs
)
4256 if (loc
== UNKNOWN_LOCATION
)
4257 loc
= cp_expr_loc_or_input_loc (t
);
4258 error_at (loc
, "parameter packs not expanded with %<...%>:");
4259 while (parameter_packs
)
4261 tree pack
= TREE_VALUE (parameter_packs
);
4262 tree name
= NULL_TREE
;
4264 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4265 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4266 name
= TYPE_NAME (pack
);
4267 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4268 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4269 else if (TREE_CODE (pack
) == CALL_EXPR
)
4270 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4272 name
= DECL_NAME (pack
);
4275 inform (loc
, " %qD", name
);
4277 inform (loc
, " %s", "<anonymous>");
4279 parameter_packs
= TREE_CHAIN (parameter_packs
);
4288 /* Expand any parameter packs that occur in the template arguments in
4291 expand_template_argument_pack (tree args
)
4293 if (args
== error_mark_node
)
4294 return error_mark_node
;
4296 tree result_args
= NULL_TREE
;
4297 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4298 int num_result_args
= -1;
4299 int non_default_args_count
= -1;
4301 /* First, determine if we need to expand anything, and the number of
4302 slots we'll need. */
4303 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4305 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4306 if (arg
== NULL_TREE
)
4308 if (ARGUMENT_PACK_P (arg
))
4310 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4311 if (num_result_args
< 0)
4312 num_result_args
= in_arg
+ num_packed
;
4314 num_result_args
+= num_packed
;
4318 if (num_result_args
>= 0)
4323 /* If no expansion is necessary, we're done. */
4324 if (num_result_args
< 0)
4327 /* Expand arguments. */
4328 result_args
= make_tree_vec (num_result_args
);
4329 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4330 non_default_args_count
=
4331 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4332 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4334 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4335 if (ARGUMENT_PACK_P (arg
))
4337 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4338 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4339 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4340 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4341 if (non_default_args_count
> 0)
4342 non_default_args_count
+= num_packed
- 1;
4346 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4350 if (non_default_args_count
>= 0)
4351 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4355 /* Checks if DECL shadows a template parameter.
4357 [temp.local]: A template-parameter shall not be redeclared within its
4358 scope (including nested scopes).
4360 Emits an error and returns TRUE if the DECL shadows a parameter,
4361 returns FALSE otherwise. */
4364 check_template_shadow (tree decl
)
4368 /* If we're not in a template, we can't possibly shadow a template
4370 if (!current_template_parms
)
4373 /* Figure out what we're shadowing. */
4374 decl
= OVL_FIRST (decl
);
4375 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4377 /* If there's no previous binding for this name, we're not shadowing
4378 anything, let alone a template parameter. */
4382 /* If we're not shadowing a template parameter, we're done. Note
4383 that OLDDECL might be an OVERLOAD (or perhaps even an
4384 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4386 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4389 /* We check for decl != olddecl to avoid bogus errors for using a
4390 name inside a class. We check TPFI to avoid duplicate errors for
4391 inline member templates. */
4393 || (DECL_TEMPLATE_PARM_P (decl
)
4394 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4397 /* Don't complain about the injected class name, as we've already
4398 complained about the class itself. */
4399 if (DECL_SELF_REFERENCE_P (decl
))
4402 if (DECL_TEMPLATE_PARM_P (decl
))
4403 error ("declaration of template parameter %q+D shadows "
4404 "template parameter", decl
);
4406 error ("declaration of %q+#D shadows template parameter", decl
);
4407 inform (DECL_SOURCE_LOCATION (olddecl
),
4408 "template parameter %qD declared here", olddecl
);
4412 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4413 ORIG_LEVEL, DECL, and TYPE. */
4416 build_template_parm_index (int index
,
4422 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4423 TEMPLATE_PARM_IDX (t
) = index
;
4424 TEMPLATE_PARM_LEVEL (t
) = level
;
4425 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4426 TEMPLATE_PARM_DECL (t
) = decl
;
4427 TREE_TYPE (t
) = type
;
4428 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4429 TREE_READONLY (t
) = TREE_READONLY (decl
);
4434 /* Find the canonical type parameter for the given template type
4435 parameter. Returns the canonical type parameter, which may be TYPE
4436 if no such parameter existed. */
4439 canonical_type_parameter (tree type
)
4441 int idx
= TEMPLATE_TYPE_IDX (type
);
4443 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4445 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4446 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4448 for (tree list
= (*canonical_template_parms
)[idx
];
4449 list
; list
= TREE_CHAIN (list
))
4450 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4451 return TREE_VALUE (list
);
4453 (*canonical_template_parms
)[idx
]
4454 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4458 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4459 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4460 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4461 new one is created. */
4464 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4465 tsubst_flags_t complain
)
4467 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4468 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4469 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4470 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4472 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4474 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4475 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4477 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4478 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4479 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4480 DECL_ARTIFICIAL (decl
) = 1;
4481 SET_DECL_TEMPLATE_PARM_P (decl
);
4483 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4484 TEMPLATE_PARM_LEVEL (index
) - levels
,
4485 TEMPLATE_PARM_ORIG_LEVEL (index
),
4487 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4488 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4489 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4491 /* Template template parameters need this. */
4493 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4495 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4496 TYPE_DECL
, DECL_NAME (decl
), type
);
4497 DECL_TEMPLATE_RESULT (decl
) = inner
;
4498 DECL_ARTIFICIAL (inner
) = true;
4499 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4500 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4503 /* Attach the TPI to the decl. */
4504 if (TREE_CODE (inner
) == TYPE_DECL
)
4505 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4507 DECL_INITIAL (decl
) = tpi
;
4510 return TEMPLATE_PARM_DESCENDANTS (index
);
4513 /* Process information from new template parameter PARM and append it
4514 to the LIST being built. This new parameter is a non-type
4515 parameter iff IS_NON_TYPE is true. This new parameter is a
4516 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4520 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4521 bool is_non_type
, bool is_parameter_pack
)
4523 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4524 tree prev
= NULL_TREE
;
4529 prev
= tree_last (list
);
4531 tree p
= TREE_VALUE (prev
);
4532 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4533 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4534 else if (TREE_CODE (p
) == PARM_DECL
)
4535 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4540 tree decl
= NULL_TREE
;
4541 tree defval
= TREE_PURPOSE (parm
);
4542 tree constr
= TREE_TYPE (parm
);
4546 parm
= TREE_VALUE (parm
);
4548 SET_DECL_TEMPLATE_PARM_P (parm
);
4550 if (TREE_TYPE (parm
) != error_mark_node
)
4554 The top-level cv-qualifiers on the template-parameter are
4555 ignored when determining its type. */
4556 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4557 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4558 TREE_TYPE (parm
) = error_mark_node
;
4559 else if (uses_parameter_packs (TREE_TYPE (parm
))
4560 && !is_parameter_pack
4561 /* If we're in a nested template parameter list, the template
4562 template parameter could be a parameter pack. */
4563 && processing_template_parmlist
== 1)
4565 /* This template parameter is not a parameter pack, but it
4566 should be. Complain about "bare" parameter packs. */
4567 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4569 /* Recover by calling this a parameter pack. */
4570 is_parameter_pack
= true;
4574 /* A template parameter is not modifiable. */
4575 TREE_CONSTANT (parm
) = 1;
4576 TREE_READONLY (parm
) = 1;
4577 decl
= build_decl (parm_loc
,
4578 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4579 TREE_CONSTANT (decl
) = 1;
4580 TREE_READONLY (decl
) = 1;
4581 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4582 = build_template_parm_index (idx
, processing_template_decl
,
4583 processing_template_decl
,
4584 decl
, TREE_TYPE (parm
));
4586 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4587 = is_parameter_pack
;
4592 parm
= TREE_VALUE (TREE_VALUE (parm
));
4594 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4596 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4597 /* This is for distinguishing between real templates and template
4598 template parameters */
4599 TREE_TYPE (parm
) = t
;
4601 /* any_template_parm_r expects to be able to get the targs of a
4602 DECL_TEMPLATE_RESULT. */
4603 tree result
= DECL_TEMPLATE_RESULT (parm
);
4604 TREE_TYPE (result
) = t
;
4605 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4606 tree tinfo
= build_template_info (parm
, args
);
4607 retrofit_lang_decl (result
);
4608 DECL_TEMPLATE_INFO (result
) = tinfo
;
4614 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4615 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4616 decl
= build_decl (parm_loc
,
4617 TYPE_DECL
, parm
, t
);
4620 TYPE_NAME (t
) = decl
;
4621 TYPE_STUB_DECL (t
) = decl
;
4623 TEMPLATE_TYPE_PARM_INDEX (t
)
4624 = build_template_parm_index (idx
, processing_template_decl
,
4625 processing_template_decl
,
4626 decl
, TREE_TYPE (parm
));
4627 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4628 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4629 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4631 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4633 DECL_ARTIFICIAL (decl
) = 1;
4634 SET_DECL_TEMPLATE_PARM_P (decl
);
4636 /* Build requirements for the type/template parameter.
4637 This must be done after SET_DECL_TEMPLATE_PARM_P or
4638 process_template_parm could fail. */
4639 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4641 decl
= pushdecl (decl
);
4645 /* Build the parameter node linking the parameter declaration,
4646 its default argument (if any), and its constraints (if any). */
4647 parm
= build_tree_list (defval
, parm
);
4648 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4651 TREE_CHAIN (prev
) = parm
;
4658 /* The end of a template parameter list has been reached. Process the
4659 tree list into a parameter vector, converting each parameter into a more
4660 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4664 end_template_parm_list (tree parms
)
4666 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4668 /* Pop the dummy parameter level and add the real one. We do not
4669 morph the dummy parameter in place, as it might have been
4670 captured by a (nested) template-template-parm. */
4671 current_template_parms
= TREE_CHAIN (current_template_parms
);
4673 current_template_parms
4674 = tree_cons (size_int (processing_template_decl
),
4675 saved_parmlist
, current_template_parms
);
4677 for (unsigned ix
= 0; parms
; ix
++)
4680 parms
= TREE_CHAIN (parms
);
4681 TREE_CHAIN (parm
) = NULL_TREE
;
4683 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4686 --processing_template_parmlist
;
4688 return saved_parmlist
;
4691 // Explicitly indicate the end of the template parameter list. We assume
4692 // that the current template parameters have been constructed and/or
4693 // managed explicitly, as when creating new template template parameters
4694 // from a shorthand constraint.
4696 end_template_parm_list ()
4698 --processing_template_parmlist
;
4701 /* end_template_decl is called after a template declaration is seen. */
4704 end_template_decl (void)
4706 reset_specialization ();
4708 if (! processing_template_decl
)
4711 /* This matches the pushlevel in begin_template_parm_list. */
4714 --processing_template_decl
;
4715 current_template_parms
= TREE_CHAIN (current_template_parms
);
4718 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4719 thereof, and converts it into an argument suitable to be passed to
4720 the type substitution functions. Note that if the TREE_LIST contains
4721 an error_mark node, the returned argument is error_mark_node. */
4724 template_parm_to_arg (tree t
)
4729 if (TREE_CODE (t
) == TREE_LIST
)
4732 if (error_operand_p (t
))
4733 return error_mark_node
;
4735 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4737 if (TREE_CODE (t
) == TYPE_DECL
4738 || TREE_CODE (t
) == TEMPLATE_DECL
)
4741 t
= DECL_INITIAL (t
);
4744 gcc_assert (TEMPLATE_PARM_P (t
));
4746 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4747 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4749 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4751 /* Turn this argument into a TYPE_ARGUMENT_PACK
4752 with a single element, which expands T. */
4753 tree vec
= make_tree_vec (1);
4755 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4757 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4759 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4760 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4765 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4767 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4768 with a single element, which expands T. */
4769 tree vec
= make_tree_vec (1);
4771 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4773 t
= convert_from_reference (t
);
4774 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4776 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4777 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4780 t
= convert_from_reference (t
);
4785 /* Given a single level of template parameters (a TREE_VEC), return it
4786 as a set of template arguments. */
4789 template_parms_level_to_args (tree parms
)
4791 tree a
= copy_node (parms
);
4792 TREE_TYPE (a
) = NULL_TREE
;
4793 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4794 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4802 /* Given a set of template parameters, return them as a set of template
4803 arguments. The template parameters are represented as a TREE_VEC, in
4804 the form documented in cp-tree.h for template arguments. */
4807 template_parms_to_args (tree parms
)
4810 tree args
= NULL_TREE
;
4811 int length
= TMPL_PARMS_DEPTH (parms
);
4814 /* If there is only one level of template parameters, we do not
4815 create a TREE_VEC of TREE_VECs. Instead, we return a single
4816 TREE_VEC containing the arguments. */
4818 args
= make_tree_vec (length
);
4820 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4822 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4825 TREE_VEC_ELT (args
, --l
) = a
;
4833 /* Within the declaration of a template, return the currently active
4834 template parameters as an argument TREE_VEC. */
4837 current_template_args (void)
4839 return template_parms_to_args (current_template_parms
);
4842 /* Return the fully generic arguments for of TMPL, i.e. what
4843 current_template_args would be while parsing it. */
4846 generic_targs_for (tree tmpl
)
4848 if (tmpl
== NULL_TREE
)
4850 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4851 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4852 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4853 template parameter, it has no TEMPLATE_INFO; for a partial
4854 specialization, it has the arguments for the primary template, and we
4855 want the arguments for the partial specialization. */;
4856 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4857 if (tree ti
= get_template_info (result
))
4858 return TI_ARGS (ti
);
4859 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4862 /* Update the declared TYPE by doing any lookups which were thought to be
4863 dependent, but are not now that we know the SCOPE of the declarator. */
4866 maybe_update_decl_type (tree orig_type
, tree scope
)
4868 tree type
= orig_type
;
4870 if (type
== NULL_TREE
)
4873 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4874 type
= TREE_TYPE (type
);
4876 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4877 && dependent_type_p (type
)
4878 /* Don't bother building up the args in this case. */
4879 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4881 /* tsubst in the args corresponding to the template parameters,
4882 including auto if present. Most things will be unchanged, but
4883 make_typename_type and tsubst_qualified_id will resolve
4884 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4885 tree args
= current_template_args ();
4886 tree auto_node
= type_uses_auto (type
);
4890 tree auto_vec
= make_tree_vec (1);
4891 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4892 args
= add_to_template_args (args
, auto_vec
);
4894 pushed
= push_scope (scope
);
4895 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4900 if (type
== error_mark_node
)
4903 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4905 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4908 type
= TYPE_NAME (type
);
4913 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4914 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4915 the new template is a member template. */
4918 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4920 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4921 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4922 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4923 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4924 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4925 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4926 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4927 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4932 struct template_parm_data
4934 /* The level of the template parameters we are currently
4938 /* The index of the specialization argument we are currently
4942 /* An array whose size is the number of template parameters. The
4943 elements are nonzero if the parameter has been used in any one
4944 of the arguments processed so far. */
4947 /* An array whose size is the number of template arguments. The
4948 elements are nonzero if the argument makes use of template
4949 parameters of this level. */
4950 int* arg_uses_template_parms
;
4953 /* Subroutine of push_template_decl used to see if each template
4954 parameter in a partial specialization is used in the explicit
4955 argument list. If T is of the LEVEL given in DATA (which is
4956 treated as a template_parm_data*), then DATA->PARMS is marked
4960 mark_template_parm (tree t
, void* data
)
4964 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4966 template_parm_level_and_index (t
, &level
, &idx
);
4968 if (level
== tpd
->level
)
4970 tpd
->parms
[idx
] = 1;
4971 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4974 /* In C++17 the type of a non-type argument is a deduced context. */
4975 if (cxx_dialect
>= cxx17
4976 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4977 for_each_template_parm (TREE_TYPE (t
),
4978 &mark_template_parm
,
4981 /*include_nondeduced_p=*/false);
4983 /* Return zero so that for_each_template_parm will continue the
4984 traversal of the tree; we want to mark *every* template parm. */
4988 /* Process the partial specialization DECL. */
4991 process_partial_specialization (tree decl
)
4993 tree type
= TREE_TYPE (decl
);
4994 tree tinfo
= get_template_info (decl
);
4995 tree maintmpl
= TI_TEMPLATE (tinfo
);
4996 tree specargs
= TI_ARGS (tinfo
);
4997 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4998 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5001 int nargs
= TREE_VEC_LENGTH (inner_args
);
5004 bool did_error_intro
= false;
5005 struct template_parm_data tpd
;
5006 struct template_parm_data tpd2
;
5008 gcc_assert (current_template_parms
);
5010 /* A concept cannot be specialized. */
5011 if (flag_concepts
&& variable_concept_p (maintmpl
))
5013 error ("specialization of variable concept %q#D", maintmpl
);
5014 return error_mark_node
;
5017 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5018 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5020 /* We check that each of the template parameters given in the
5021 partial specialization is used in the argument list to the
5022 specialization. For example:
5024 template <class T> struct S;
5025 template <class T> struct S<T*>;
5027 The second declaration is OK because `T*' uses the template
5028 parameter T, whereas
5030 template <class T> struct S<int>;
5032 is no good. Even trickier is:
5043 The S2<T> declaration is actually invalid; it is a
5044 full-specialization. Of course,
5047 struct S2<T (*)(U)>;
5049 or some such would have been OK. */
5050 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5051 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5052 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5054 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5055 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5056 for (i
= 0; i
< nargs
; ++i
)
5058 tpd
.current_arg
= i
;
5059 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5060 &mark_template_parm
,
5063 /*include_nondeduced_p=*/false);
5065 for (i
= 0; i
< ntparms
; ++i
)
5066 if (tpd
.parms
[i
] == 0)
5068 /* One of the template parms was not used in a deduced context in the
5070 if (!did_error_intro
)
5072 error ("template parameters not deducible in "
5073 "partial specialization:");
5074 did_error_intro
= true;
5077 inform (input_location
, " %qD",
5078 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5081 if (did_error_intro
)
5082 return error_mark_node
;
5084 /* [temp.class.spec]
5086 The argument list of the specialization shall not be identical to
5087 the implicit argument list of the primary template. */
5089 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5090 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5092 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5095 error ("partial specialization %q+D does not specialize "
5096 "any template arguments; to define the primary template, "
5097 "remove the template argument list", decl
);
5099 error ("partial specialization %q+D does not specialize any "
5100 "template arguments and is not more constrained than "
5101 "the primary template; to define the primary template, "
5102 "remove the template argument list", decl
);
5103 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5106 /* A partial specialization that replaces multiple parameters of the
5107 primary template with a pack expansion is less specialized for those
5109 if (nargs
< DECL_NTPARMS (maintmpl
))
5111 error ("partial specialization is not more specialized than the "
5112 "primary template because it replaces multiple parameters "
5113 "with a pack expansion");
5114 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5115 /* Avoid crash in process_partial_specialization. */
5119 else if (nargs
> DECL_NTPARMS (maintmpl
))
5121 error ("too many arguments for partial specialization %qT", type
);
5122 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5123 /* Avoid crash below. */
5127 /* If we aren't in a dependent class, we can actually try deduction. */
5128 else if (tpd
.level
== 1
5129 /* FIXME we should be able to handle a partial specialization of a
5130 partial instantiation, but currently we can't (c++/41727). */
5131 && TMPL_ARGS_DEPTH (specargs
) == 1
5132 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5134 auto_diagnostic_group d
;
5135 if (permerror (input_location
, "partial specialization %qD is not "
5136 "more specialized than", decl
))
5137 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5141 /* [temp.class.spec]
5143 A partially specialized non-type argument expression shall not
5144 involve template parameters of the partial specialization except
5145 when the argument expression is a simple identifier.
5147 The type of a template parameter corresponding to a specialized
5148 non-type argument shall not be dependent on a parameter of the
5151 Also, we verify that pack expansions only occur at the
5152 end of the argument list. */
5154 for (i
= 0; i
< nargs
; ++i
)
5156 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5157 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5158 tree packed_args
= NULL_TREE
;
5161 if (ARGUMENT_PACK_P (arg
))
5163 /* Extract the arguments from the argument pack. We'll be
5164 iterating over these in the following loop. */
5165 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5166 len
= TREE_VEC_LENGTH (packed_args
);
5169 for (j
= 0; j
< len
; j
++)
5172 /* Get the Jth argument in the parameter pack. */
5173 arg
= TREE_VEC_ELT (packed_args
, j
);
5175 if (PACK_EXPANSION_P (arg
))
5177 /* Pack expansions must come at the end of the
5179 if ((packed_args
&& j
< len
- 1)
5180 || (!packed_args
&& i
< nargs
- 1))
5182 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5183 error ("parameter pack argument %qE must be at the "
5184 "end of the template argument list", arg
);
5186 error ("parameter pack argument %qT must be at the "
5187 "end of the template argument list", arg
);
5191 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5192 /* We only care about the pattern. */
5193 arg
= PACK_EXPANSION_PATTERN (arg
);
5195 if (/* These first two lines are the `non-type' bit. */
5197 && TREE_CODE (arg
) != TEMPLATE_DECL
5198 /* This next two lines are the `argument expression is not just a
5199 simple identifier' condition and also the `specialized
5200 non-type argument' bit. */
5201 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5202 && !((REFERENCE_REF_P (arg
)
5203 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5204 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5206 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5207 || (packed_args
&& uses_template_parms (arg
)))
5208 error_at (cp_expr_loc_or_input_loc (arg
),
5209 "template argument %qE involves template "
5210 "parameter(s)", arg
);
5213 /* Look at the corresponding template parameter,
5214 marking which template parameters its type depends
5216 tree type
= TREE_TYPE (parm
);
5220 /* We haven't yet initialized TPD2. Do so now. */
5221 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5222 /* The number of parameters here is the number in the
5223 main template, which, as checked in the assertion
5225 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5227 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5230 /* Mark the template parameters. But this time, we're
5231 looking for the template parameters of the main
5232 template, not in the specialization. */
5233 tpd2
.current_arg
= i
;
5234 tpd2
.arg_uses_template_parms
[i
] = 0;
5235 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5236 for_each_template_parm (type
,
5237 &mark_template_parm
,
5240 /*include_nondeduced_p=*/false);
5242 if (tpd2
.arg_uses_template_parms
[i
])
5244 /* The type depended on some template parameters.
5245 If they are fully specialized in the
5246 specialization, that's OK. */
5249 for (j
= 0; j
< nargs
; ++j
)
5250 if (tpd2
.parms
[j
] != 0
5251 && tpd
.arg_uses_template_parms
[j
])
5254 error_n (input_location
, count
,
5255 "type %qT of template argument %qE depends "
5256 "on a template parameter",
5257 "type %qT of template argument %qE depends "
5258 "on template parameters",
5267 /* We should only get here once. */
5268 if (TREE_CODE (decl
) == TYPE_DECL
)
5269 gcc_assert (!COMPLETE_TYPE_P (type
));
5271 // Build the template decl.
5272 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5273 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5274 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5275 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5276 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5278 /* Give template template parms a DECL_CONTEXT of the template
5279 for which they are a parameter. */
5280 for (i
= 0; i
< ntparms
; ++i
)
5282 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5283 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5284 DECL_CONTEXT (parm
) = tmpl
;
5288 /* We didn't register this in check_explicit_specialization so we could
5289 wait until the constraints were set. */
5290 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5292 associate_classtype_constraints (type
);
5294 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5295 = tree_cons (specargs
, tmpl
,
5296 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5297 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5299 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5300 inst
= TREE_CHAIN (inst
))
5302 tree instance
= TREE_VALUE (inst
);
5303 if (TYPE_P (instance
)
5304 ? (COMPLETE_TYPE_P (instance
)
5305 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5306 : DECL_TEMPLATE_INSTANTIATION (instance
))
5308 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5309 tree inst_decl
= (DECL_P (instance
)
5310 ? instance
: TYPE_NAME (instance
));
5313 else if (spec
== error_mark_node
)
5314 permerror (input_location
,
5315 "declaration of %qD ambiguates earlier template "
5316 "instantiation for %qD", decl
, inst_decl
);
5317 else if (TREE_VALUE (spec
) == tmpl
)
5318 permerror (input_location
,
5319 "partial specialization of %qD after instantiation "
5320 "of %qD", decl
, inst_decl
);
5327 /* PARM is a template parameter of some form; return the corresponding
5328 TEMPLATE_PARM_INDEX. */
5331 get_template_parm_index (tree parm
)
5333 if (TREE_CODE (parm
) == PARM_DECL
5334 || TREE_CODE (parm
) == CONST_DECL
)
5335 parm
= DECL_INITIAL (parm
);
5336 else if (TREE_CODE (parm
) == TYPE_DECL
5337 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5338 parm
= TREE_TYPE (parm
);
5339 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5340 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5341 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5342 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5343 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5347 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5348 parameter packs used by the template parameter PARM. */
5351 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5353 /* A type parm can't refer to another parm. */
5354 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5356 else if (TREE_CODE (parm
) == PARM_DECL
)
5358 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5363 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5365 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5366 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5368 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5369 if (template_parameter_pack_p (p
))
5370 /* Any packs in the type are expanded by this parameter. */;
5372 fixed_parameter_pack_p_1 (p
, ppd
);
5376 /* PARM is a template parameter pack. Return any parameter packs used in
5377 its type or the type of any of its template parameters. If there are
5378 any such packs, it will be instantiated into a fixed template parameter
5379 list by partial instantiation rather than be fully deduced. */
5382 fixed_parameter_pack_p (tree parm
)
5384 /* This can only be true in a member template. */
5385 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5387 /* This can only be true for a parameter pack. */
5388 if (!template_parameter_pack_p (parm
))
5390 /* A type parm can't refer to another parm. */
5391 if (TREE_CODE (parm
) == TYPE_DECL
)
5394 tree parameter_packs
= NULL_TREE
;
5395 struct find_parameter_pack_data ppd
;
5396 ppd
.parameter_packs
= ¶meter_packs
;
5397 ppd
.visited
= new hash_set
<tree
>;
5398 ppd
.type_pack_expansion_p
= false;
5400 fixed_parameter_pack_p_1 (parm
, &ppd
);
5403 return parameter_packs
;
5406 /* Check that a template declaration's use of default arguments and
5407 parameter packs is not invalid. Here, PARMS are the template
5408 parameters. IS_PRIMARY is true if DECL is the thing declared by
5409 a primary template. IS_PARTIAL is true if DECL is a partial
5412 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5413 function template declaration or a friend class template
5414 declaration. In the function case, 1 indicates a declaration, 2
5415 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5416 emitted for extraneous default arguments.
5418 Returns TRUE if there were no errors found, FALSE otherwise. */
5421 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5422 bool is_partial
, int is_friend_decl
)
5425 int last_level_to_check
;
5427 bool no_errors
= true;
5431 A default template-argument shall not be specified in a
5432 function template declaration or a function template definition, nor
5433 in the template-parameter-list of the definition of a member of a
5436 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5437 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5438 /* You can't have a function template declaration in a local
5439 scope, nor you can you define a member of a class template in a
5443 if ((TREE_CODE (decl
) == TYPE_DECL
5445 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5446 || (TREE_CODE (decl
) == FUNCTION_DECL
5447 && LAMBDA_FUNCTION_P (decl
)))
5448 /* A lambda doesn't have an explicit declaration; don't complain
5449 about the parms of the enclosing class. */
5452 if (current_class_type
5453 && !TYPE_BEING_DEFINED (current_class_type
)
5454 && DECL_LANG_SPECIFIC (decl
)
5455 && DECL_DECLARES_FUNCTION_P (decl
)
5456 /* If this is either a friend defined in the scope of the class
5457 or a member function. */
5458 && (DECL_FUNCTION_MEMBER_P (decl
)
5459 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5460 : DECL_FRIEND_CONTEXT (decl
)
5461 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5463 /* And, if it was a member function, it really was defined in
5464 the scope of the class. */
5465 && (!DECL_FUNCTION_MEMBER_P (decl
)
5466 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5467 /* We already checked these parameters when the template was
5468 declared, so there's no need to do it again now. This function
5469 was defined in class scope, but we're processing its body now
5470 that the class is complete. */
5473 /* Core issue 226 (C++0x only): the following only applies to class
5476 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5480 If a template-parameter has a default template-argument, all
5481 subsequent template-parameters shall have a default
5482 template-argument supplied. */
5483 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5485 tree inner_parms
= TREE_VALUE (parm_level
);
5486 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5487 int seen_def_arg_p
= 0;
5490 for (i
= 0; i
< ntparms
; ++i
)
5492 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5494 if (parm
== error_mark_node
)
5497 if (TREE_PURPOSE (parm
))
5499 else if (seen_def_arg_p
5500 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5502 error ("no default argument for %qD", TREE_VALUE (parm
));
5503 /* For better subsequent error-recovery, we indicate that
5504 there should have been a default argument. */
5505 TREE_PURPOSE (parm
) = error_mark_node
;
5508 else if (!is_partial
5510 /* Don't complain about an enclosing partial
5512 && parm_level
== parms
5513 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5515 && template_parameter_pack_p (TREE_VALUE (parm
))
5516 /* A fixed parameter pack will be partially
5517 instantiated into a fixed length list. */
5518 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5520 /* A primary class template, primary variable template
5521 (DR 2032), or alias template can only have one
5522 parameter pack, at the end of the template
5525 error ("parameter pack %q+D must be at the end of the"
5526 " template parameter list", TREE_VALUE (parm
));
5528 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5536 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5540 /* For an ordinary class template, default template arguments are
5541 allowed at the innermost level, e.g.:
5542 template <class T = int>
5544 but, in a partial specialization, they're not allowed even
5545 there, as we have in [temp.class.spec]:
5547 The template parameter list of a specialization shall not
5548 contain default template argument values.
5550 So, for a partial specialization, or for a function template
5551 (in C++98/C++03), we look at all of them. */
5554 /* But, for a primary class template that is not a partial
5555 specialization we look at all template parameters except the
5557 parms
= TREE_CHAIN (parms
);
5559 /* Figure out what error message to issue. */
5560 if (is_friend_decl
== 2)
5561 msg
= G_("default template arguments may not be used in function template "
5562 "friend re-declaration");
5563 else if (is_friend_decl
)
5564 msg
= G_("default template arguments may not be used in template "
5565 "friend declarations");
5566 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5567 msg
= G_("default template arguments may not be used in function templates "
5568 "without %<-std=c++11%> or %<-std=gnu++11%>");
5569 else if (is_partial
)
5570 msg
= G_("default template arguments may not be used in "
5571 "partial specializations");
5572 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5573 msg
= G_("default argument for template parameter for class enclosing %qD");
5575 /* Per [temp.param]/9, "A default template-argument shall not be
5576 specified in the template-parameter-lists of the definition of
5577 a member of a class template that appears outside of the member's
5578 class.", thus if we aren't handling a member of a class template
5579 there is no need to examine the parameters. */
5582 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5583 /* If we're inside a class definition, there's no need to
5584 examine the parameters to the class itself. On the one
5585 hand, they will be checked when the class is defined, and,
5586 on the other, default arguments are valid in things like:
5587 template <class T = double>
5588 struct S { template <class U> void f(U); };
5589 Here the default argument for `S' has no bearing on the
5590 declaration of `f'. */
5591 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5593 /* Check everything. */
5594 last_level_to_check
= 0;
5596 for (parm_level
= parms
;
5597 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5598 parm_level
= TREE_CHAIN (parm_level
))
5600 tree inner_parms
= TREE_VALUE (parm_level
);
5604 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5605 for (i
= 0; i
< ntparms
; ++i
)
5607 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5610 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5615 if (is_friend_decl
== 2)
5622 /* Clear out the default argument so that we are not
5624 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5628 /* At this point, if we're still interested in issuing messages,
5629 they must apply to classes surrounding the object declared. */
5631 msg
= G_("default argument for template parameter for class "
5638 /* Worker for push_template_decl_real, called via
5639 for_each_template_parm. DATA is really an int, indicating the
5640 level of the parameters we are interested in. If T is a template
5641 parameter of that level, return nonzero. */
5644 template_parm_this_level_p (tree t
, void* data
)
5646 int this_level
= *(int *)data
;
5649 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5650 level
= TEMPLATE_PARM_LEVEL (t
);
5652 level
= TEMPLATE_TYPE_LEVEL (t
);
5653 return level
== this_level
;
5656 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5657 DATA is really an int, indicating the innermost outer level of parameters.
5658 If T is a template parameter of that level or further out, return
5662 template_parm_outer_level (tree t
, void *data
)
5664 int this_level
= *(int *)data
;
5667 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5668 level
= TEMPLATE_PARM_LEVEL (t
);
5670 level
= TEMPLATE_TYPE_LEVEL (t
);
5671 return level
<= this_level
;
5674 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5675 parameters given by current_template_args, or reuses a
5676 previously existing one, if appropriate. Returns the DECL, or an
5677 equivalent one, if it is replaced via a call to duplicate_decls.
5679 If IS_FRIEND is true, DECL is a friend declaration. */
5682 push_template_decl (tree decl
, bool is_friend
)
5684 if (decl
== error_mark_node
|| !current_template_parms
)
5685 return error_mark_node
;
5687 /* See if this is a partial specialization. */
5688 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5689 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5690 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5692 && DECL_LANG_SPECIFIC (decl
)
5693 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5694 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5696 /* No surprising friend functions. */
5697 gcc_checking_assert (is_friend
5698 || !(TREE_CODE (decl
) == FUNCTION_DECL
5699 && DECL_UNIQUE_FRIEND_P (decl
)));
5703 /* For a friend, we want the context of the friend, not
5704 the type of which it is a friend. */
5705 ctx
= CP_DECL_CONTEXT (decl
);
5706 else if (CP_DECL_CONTEXT (decl
)
5707 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5708 /* In the case of a virtual function, we want the class in which
5710 ctx
= CP_DECL_CONTEXT (decl
);
5712 /* Otherwise, if we're currently defining some class, the DECL
5713 is assumed to be a member of the class. */
5714 ctx
= current_scope ();
5716 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5719 if (!DECL_CONTEXT (decl
))
5720 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5722 /* See if this is a primary template. */
5723 bool is_primary
= false;
5724 if (is_friend
&& ctx
5725 && uses_template_parms_level (ctx
, processing_template_decl
))
5726 /* A friend template that specifies a class context, i.e.
5727 template <typename T> friend void A<T>::f();
5730 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5731 /* Lambdas are not primary. */
5734 is_primary
= template_parm_scope_p ();
5736 /* True if the template is a member template, in the sense of
5738 bool member_template_p
= false;
5742 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5744 if (DECL_CLASS_SCOPE_P (decl
))
5745 member_template_p
= true;
5747 if (TREE_CODE (decl
) == TYPE_DECL
5748 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5750 error ("template class without a name");
5751 return error_mark_node
;
5753 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5755 if (member_template_p
)
5757 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5758 error ("member template %qD may not have virt-specifiers", decl
);
5760 if (DECL_DESTRUCTOR_P (decl
))
5764 A destructor shall not be a member template. */
5765 error_at (DECL_SOURCE_LOCATION (decl
),
5766 "destructor %qD declared as member template", decl
);
5767 return error_mark_node
;
5769 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5770 && (!prototype_p (TREE_TYPE (decl
))
5771 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5772 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5773 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5774 == void_list_node
)))
5776 /* [basic.stc.dynamic.allocation]
5778 An allocation function can be a function
5779 template. ... Template allocation functions shall
5780 have two or more parameters. */
5781 error ("invalid template declaration of %qD", decl
);
5782 return error_mark_node
;
5785 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5786 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5788 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5789 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5790 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5792 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5793 if (TREE_CODE (t
) == TYPE_DECL
)
5795 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5796 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5799 else if (TREE_CODE (decl
) == TYPE_DECL
5800 && TYPE_DECL_ALIAS_P (decl
))
5801 /* alias-declaration */
5802 gcc_assert (!DECL_ARTIFICIAL (decl
));
5803 else if (VAR_P (decl
))
5804 /* C++14 variable template. */;
5805 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5806 /* C++20 concept definitions. */;
5809 error ("template declaration of %q#D", decl
);
5810 return error_mark_node
;
5814 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5815 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5816 || (VAR_OR_FUNCTION_DECL_P (decl
)
5817 && DECL_LOCAL_DECL_P (decl
))));
5819 /* Check to see that the rules regarding the use of default
5820 arguments are not being violated. We check args for a friend
5821 functions when we know whether it's a definition, introducing
5822 declaration or re-declaration. */
5823 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5824 check_default_tmpl_args (decl
, current_template_parms
,
5825 is_primary
, is_partial
, is_friend
);
5827 /* Ensure that there are no parameter packs in the type of this
5828 declaration that have not been expanded. */
5829 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5831 /* Check each of the arguments individually to see if there are
5832 any bare parameter packs. */
5833 tree type
= TREE_TYPE (decl
);
5834 tree arg
= DECL_ARGUMENTS (decl
);
5835 tree argtype
= TYPE_ARG_TYPES (type
);
5837 while (arg
&& argtype
)
5839 if (!DECL_PACK_P (arg
)
5840 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5842 /* This is a PARM_DECL that contains unexpanded parameter
5843 packs. We have already complained about this in the
5844 check_for_bare_parameter_packs call, so just replace
5845 these types with ERROR_MARK_NODE. */
5846 TREE_TYPE (arg
) = error_mark_node
;
5847 TREE_VALUE (argtype
) = error_mark_node
;
5850 arg
= DECL_CHAIN (arg
);
5851 argtype
= TREE_CHAIN (argtype
);
5854 /* Check for bare parameter packs in the return type and the
5855 exception specifiers. */
5856 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5857 /* Errors were already issued, set return type to int
5858 as the frontend doesn't expect error_mark_node as
5860 TREE_TYPE (type
) = integer_type_node
;
5861 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5862 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5864 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5865 ? DECL_ORIGINAL_TYPE (decl
)
5866 : TREE_TYPE (decl
)))
5868 TREE_TYPE (decl
) = error_mark_node
;
5869 return error_mark_node
;
5873 return process_partial_specialization (decl
);
5875 tree args
= current_template_args ();
5876 tree tmpl
= NULL_TREE
;
5877 bool new_template_p
= false;
5880 /* Does not get a template head. */
5882 gcc_checking_assert (!is_primary
);
5885 || TREE_CODE (ctx
) == FUNCTION_DECL
5886 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5887 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5888 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
5889 && DECL_TEMPLATE_INFO (decl
))))
5891 if (DECL_LANG_SPECIFIC (decl
)
5892 && DECL_TEMPLATE_INFO (decl
)
5893 && DECL_TI_TEMPLATE (decl
))
5894 tmpl
= DECL_TI_TEMPLATE (decl
);
5895 /* If DECL is a TYPE_DECL for a class-template, then there won't
5896 be DECL_LANG_SPECIFIC. The information equivalent to
5897 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5898 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5899 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5900 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5902 /* Since a template declaration already existed for this
5903 class-type, we must be redeclaring it here. Make sure
5904 that the redeclaration is valid. */
5905 redeclare_class_template (TREE_TYPE (decl
),
5906 current_template_parms
,
5907 current_template_constraints ());
5908 /* We don't need to create a new TEMPLATE_DECL; just use the
5909 one we already had. */
5910 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5914 tmpl
= build_template_decl (decl
, current_template_parms
,
5916 new_template_p
= true;
5918 if (DECL_LANG_SPECIFIC (decl
)
5919 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5921 /* A specialization of a member template of a template
5923 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5924 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5925 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5931 tree a
, t
, current
, parms
;
5933 tree tinfo
= get_template_info (decl
);
5937 error ("template definition of non-template %q#D", decl
);
5938 return error_mark_node
;
5941 tmpl
= TI_TEMPLATE (tinfo
);
5943 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5944 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5945 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5946 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5948 /* The declaration is a specialization of a member
5949 template, declared outside the class. Therefore, the
5950 innermost template arguments will be NULL, so we
5951 replace them with the arguments determined by the
5952 earlier call to check_explicit_specialization. */
5953 args
= DECL_TI_ARGS (decl
);
5956 = build_template_decl (decl
, current_template_parms
,
5958 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5959 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5960 DECL_TEMPLATE_INFO (new_tmpl
)
5961 = build_template_info (tmpl
, args
);
5963 register_specialization (new_tmpl
,
5964 most_general_template (tmpl
),
5970 /* Make sure the template headers we got make sense. */
5972 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5973 i
= TMPL_PARMS_DEPTH (parms
);
5974 if (TMPL_ARGS_DEPTH (args
) != i
)
5976 error ("expected %d levels of template parms for %q#D, got %d",
5977 i
, decl
, TMPL_ARGS_DEPTH (args
));
5978 DECL_INTERFACE_KNOWN (decl
) = 1;
5979 return error_mark_node
;
5982 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5984 a
= TMPL_ARGS_LEVEL (args
, i
);
5985 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5987 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5989 if (current
== decl
)
5990 error ("got %d template parameters for %q#D",
5991 TREE_VEC_LENGTH (a
), decl
);
5993 error ("got %d template parameters for %q#T",
5994 TREE_VEC_LENGTH (a
), current
);
5995 error (" but %d required", TREE_VEC_LENGTH (t
));
5996 /* Avoid crash in import_export_decl. */
5997 DECL_INTERFACE_KNOWN (decl
) = 1;
5998 return error_mark_node
;
6001 if (current
== decl
)
6003 else if (current
== NULL_TREE
)
6004 /* Can happen in erroneous input. */
6007 current
= get_containing_scope (current
);
6010 /* Check that the parms are used in the appropriate qualifying scopes
6011 in the declarator. */
6012 if (!comp_template_args
6014 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6016 error ("template arguments to %qD do not match original "
6017 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6018 if (!uses_template_parms (TI_ARGS (tinfo
)))
6019 inform (input_location
, "use %<template<>%> for"
6020 " an explicit specialization");
6021 /* Avoid crash in import_export_decl. */
6022 DECL_INTERFACE_KNOWN (decl
) = 1;
6023 return error_mark_node
;
6027 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6031 /* Push template declarations for global functions and types.
6032 Note that we do not try to push a global template friend
6033 declared in a template class; such a thing may well depend on
6034 the template parameters of the class and we'll push it when
6035 instantiating the befriending class. */
6037 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6039 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6040 if (pushed
== error_mark_node
)
6041 return error_mark_node
;
6043 /* pushdecl may have found an existing template. */
6046 decl
= DECL_TEMPLATE_RESULT (pushed
);
6052 /* The type may have been completed, or (erroneously) changed. */
6053 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6059 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6061 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6063 /* Give template template parms a DECL_CONTEXT of the template
6064 for which they are a parameter. */
6065 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6066 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6068 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6069 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6070 DECL_CONTEXT (parm
) = tmpl
;
6073 if (TREE_CODE (decl
) == TYPE_DECL
6074 && TYPE_DECL_ALIAS_P (decl
))
6077 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6079 /* ??? Why don't we do this here for all templates? */
6080 constr
= build_constraints (constr
, NULL_TREE
);
6081 set_constraints (decl
, constr
);
6083 if (complex_alias_template_p (tmpl
))
6084 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6088 /* The DECL_TI_ARGS of DECL contains full set of arguments
6089 referring wback to its most general template. If TMPL is a
6090 specialization, ARGS may only have the innermost set of
6091 arguments. Add the missing argument levels if necessary. */
6092 if (DECL_TEMPLATE_INFO (tmpl
))
6093 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6095 tree info
= build_template_info (tmpl
, args
);
6097 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6098 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6101 retrofit_lang_decl (decl
);
6102 DECL_TEMPLATE_INFO (decl
) = info
;
6106 if (flag_implicit_templates
6108 && TREE_PUBLIC (decl
)
6109 && VAR_OR_FUNCTION_DECL_P (decl
))
6110 /* Set DECL_COMDAT on template instantiations; if we force
6111 them to be emitted by explicit instantiation,
6112 mark_needed will tell cgraph to do the right thing. */
6113 DECL_COMDAT (decl
) = true;
6115 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6120 /* FN is an inheriting constructor that inherits from the constructor
6121 template INHERITED; turn FN into a constructor template with a matching
6125 add_inherited_template_parms (tree fn
, tree inherited
)
6128 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6129 inner_parms
= copy_node (inner_parms
);
6131 = tree_cons (size_int (processing_template_decl
+ 1),
6132 inner_parms
, current_template_parms
);
6133 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6134 tree args
= template_parms_to_args (parms
);
6135 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6136 DECL_ARTIFICIAL (tmpl
) = true;
6137 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6141 /* Called when a class template TYPE is redeclared with the indicated
6142 template PARMS, e.g.:
6144 template <class T> struct S;
6145 template <class T> struct S {}; */
6148 redeclare_class_template (tree type
, tree parms
, tree cons
)
6154 if (!TYPE_TEMPLATE_INFO (type
))
6156 error ("%qT is not a template type", type
);
6160 tmpl
= TYPE_TI_TEMPLATE (type
);
6161 if (!PRIMARY_TEMPLATE_P (tmpl
))
6162 /* The type is nested in some template class. Nothing to worry
6163 about here; there are no new template parameters for the nested
6169 error ("template specifiers not specified in declaration of %qD",
6174 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6175 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6177 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6179 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6180 "redeclared with %d template parameter",
6181 "redeclared with %d template parameters",
6182 TREE_VEC_LENGTH (parms
));
6183 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6184 "previous declaration %qD used %d template parameter",
6185 "previous declaration %qD used %d template parameters",
6186 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6190 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6197 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6198 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6201 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6202 if (error_operand_p (tmpl_parm
))
6205 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6206 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6207 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6209 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6211 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6212 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6213 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6214 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6215 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6216 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6217 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6218 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6219 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6221 auto_diagnostic_group d
;
6222 error ("template parameter %q+#D", tmpl_parm
);
6223 inform (input_location
, "redeclared here as %q#D", parm
);
6227 /* The parameters can be declared to introduce different
6229 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6230 tree p2
= TREE_VEC_ELT (parms
, i
);
6231 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6233 auto_diagnostic_group d
;
6234 error ("declaration of template parameter %q+#D with different "
6235 "constraints", parm
);
6236 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6237 "original declaration appeared here");
6241 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6243 /* We have in [temp.param]:
6245 A template-parameter may not be given default arguments
6246 by two different declarations in the same scope. */
6247 auto_diagnostic_group d
;
6248 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6249 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6250 "original definition appeared here");
6254 if (parm_default
!= NULL_TREE
)
6255 /* Update the previous template parameters (which are the ones
6256 that will really count) with the new default value. */
6257 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6258 else if (tmpl_default
!= NULL_TREE
)
6259 /* Update the new parameters, too; they'll be used as the
6260 parameters for any members. */
6261 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6263 /* Give each template template parm in this redeclaration a
6264 DECL_CONTEXT of the template for which they are a parameter. */
6265 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6267 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6268 DECL_CONTEXT (parm
) = tmpl
;
6271 if (TREE_CODE (parm
) == TYPE_DECL
)
6272 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6275 tree ci
= get_constraints (tmpl
);
6276 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6277 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6279 /* Two classes with different constraints declare different entities. */
6280 if (!cp_tree_equal (req1
, req2
))
6282 auto_diagnostic_group d
;
6283 error_at (input_location
, "redeclaration %q#D with different "
6284 "constraints", tmpl
);
6285 inform (DECL_SOURCE_LOCATION (tmpl
),
6286 "original declaration appeared here");
6293 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6294 to be used when the caller has already checked
6295 (processing_template_decl
6296 && !instantiation_dependent_expression_p (expr)
6297 && potential_constant_expression (expr))
6298 and cleared processing_template_decl. */
6301 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6303 return tsubst_copy_and_build (expr
,
6306 /*in_decl=*/NULL_TREE
,
6307 /*function_p=*/false,
6308 /*integral_constant_expression_p=*/true);
6311 /* Simplify EXPR if it is a non-dependent expression. Returns the
6312 (possibly simplified) expression. */
6315 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6317 if (expr
== NULL_TREE
)
6320 /* If we're in a template, but EXPR isn't value dependent, simplify
6321 it. We're supposed to treat:
6323 template <typename T> void f(T[1 + 1]);
6324 template <typename T> void f(T[2]);
6326 as two declarations of the same function, for example. */
6327 if (processing_template_decl
6328 && is_nondependent_constant_expression (expr
))
6330 processing_template_decl_sentinel s
;
6331 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6337 instantiate_non_dependent_expr (tree expr
)
6339 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6342 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6343 an uninstantiated expression. */
6346 instantiate_non_dependent_or_null (tree expr
)
6348 if (expr
== NULL_TREE
)
6350 if (processing_template_decl
)
6352 if (!is_nondependent_constant_expression (expr
))
6356 processing_template_decl_sentinel s
;
6357 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6363 /* True iff T is a specialization of a variable template. */
6366 variable_template_specialization_p (tree t
)
6368 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6370 tree tmpl
= DECL_TI_TEMPLATE (t
);
6371 return variable_template_p (tmpl
);
6374 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6375 template declaration, or a TYPE_DECL for an alias declaration. */
6378 alias_type_or_template_p (tree t
)
6382 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6385 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6386 || DECL_ALIAS_TEMPLATE_P (t
));
6389 /* If T is a specialization of an alias template, return it; otherwise return
6390 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6393 alias_template_specialization_p (const_tree t
,
6394 bool transparent_typedefs
)
6399 /* It's an alias template specialization if it's an alias and its
6400 TYPE_NAME is a specialization of a primary template. */
6401 if (typedef_variant_p (t
))
6403 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6404 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6405 return CONST_CAST_TREE (t
);
6406 if (transparent_typedefs
)
6407 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6409 transparent_typedefs
);
6415 /* An alias template is complex from a SFINAE perspective if a template-id
6416 using that alias can be ill-formed when the expansion is not, as with
6417 the void_t template. We determine this by checking whether the
6418 expansion for the alias template uses all its template parameters. */
6420 struct uses_all_template_parms_data
6427 uses_all_template_parms_r (tree t
, void *data_
)
6429 struct uses_all_template_parms_data
&data
6430 = *(struct uses_all_template_parms_data
*)data_
;
6431 tree idx
= get_template_parm_index (t
);
6433 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6434 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6438 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6441 complex_pack_expansion_r (tree t
, void *data_
)
6443 /* An alias template with a pack expansion that expands a pack from the
6444 enclosing class needs to be considered complex, to avoid confusion with
6445 the same pack being used as an argument to the alias's own template
6446 parameter (91966). */
6447 if (!PACK_EXPANSION_P (t
))
6449 struct uses_all_template_parms_data
&data
6450 = *(struct uses_all_template_parms_data
*)data_
;
6451 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6452 pack
= TREE_CHAIN (pack
))
6454 tree parm_pack
= TREE_VALUE (pack
);
6455 if (!TEMPLATE_PARM_P (parm_pack
))
6458 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6459 if (level
< data
.level
)
6466 complex_alias_template_p (const_tree tmpl
)
6468 /* A renaming alias isn't complex. */
6469 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6472 /* Any other constrained alias is complex. */
6473 if (get_constraints (tmpl
))
6476 struct uses_all_template_parms_data data
;
6477 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6478 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6479 data
.level
= TMPL_PARMS_DEPTH (parms
);
6480 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6481 data
.seen
= XALLOCAVEC (bool, len
);
6482 for (int i
= 0; i
< len
; ++i
)
6483 data
.seen
[i
] = false;
6485 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6486 NULL
, true, complex_pack_expansion_r
))
6488 for (int i
= 0; i
< len
; ++i
)
6494 /* If T is a specialization of a complex alias template with dependent
6495 template-arguments, return it; otherwise return NULL_TREE. If T is a
6496 typedef to such a specialization, return the specialization. */
6499 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6501 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6504 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6506 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6507 && (any_dependent_template_arguments_p
6508 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6509 return CONST_CAST_TREE (t
);
6511 if (transparent_typedefs
)
6513 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6514 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6520 /* Return the number of innermost template parameters in TMPL. */
6523 num_innermost_template_parms (const_tree tmpl
)
6525 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6526 return TREE_VEC_LENGTH (parms
);
6529 /* Return either TMPL or another template that it is equivalent to under DR
6530 1286: An alias that just changes the name of a template is equivalent to
6531 the other template. */
6534 get_underlying_template (tree tmpl
)
6536 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6537 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6539 /* Determine if the alias is equivalent to an underlying template. */
6540 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6541 /* The underlying type may have been ill-formed. Don't proceed. */
6544 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6548 tree underlying
= TI_TEMPLATE (tinfo
);
6549 if (!PRIMARY_TEMPLATE_P (underlying
)
6550 || (num_innermost_template_parms (tmpl
)
6551 != num_innermost_template_parms (underlying
)))
6554 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6555 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6558 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6559 it's appropriate to treat a less-constrained alias as equivalent. */
6560 if (!at_least_as_constrained (underlying
, tmpl
))
6563 /* Alias is equivalent. Strip it and repeat. */
6570 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6571 must be a reference-to-function or a pointer-to-function type, as specified
6572 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6573 and check that the resulting function has external linkage. */
6576 convert_nontype_argument_function (tree type
, tree expr
,
6577 tsubst_flags_t complain
)
6581 linkage_kind linkage
;
6583 fn
= instantiate_type (type
, fns
, tf_none
);
6584 if (fn
== error_mark_node
)
6585 return error_mark_node
;
6587 if (value_dependent_expression_p (fn
))
6590 fn_no_ptr
= strip_fnptr_conv (fn
);
6591 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6592 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6593 if (BASELINK_P (fn_no_ptr
))
6594 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6596 /* [temp.arg.nontype]/1
6598 A template-argument for a non-type, non-template template-parameter
6601 -- the address of an object or function with external [C++11: or
6602 internal] linkage. */
6604 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6605 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6607 if (complain
& tf_error
)
6609 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6610 error_at (loc
, "%qE is not a valid template argument for type %qT",
6612 if (TYPE_PTR_P (type
))
6613 inform (loc
, "it must be the address of a function "
6614 "with external linkage");
6616 inform (loc
, "it must be the name of a function with "
6617 "external linkage");
6622 linkage
= decl_linkage (fn_no_ptr
);
6623 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6625 if (complain
& tf_error
)
6627 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6628 if (cxx_dialect
>= cxx11
)
6629 error_at (loc
, "%qE is not a valid template argument for type "
6630 "%qT because %qD has no linkage",
6631 expr
, type
, fn_no_ptr
);
6633 error_at (loc
, "%qE is not a valid template argument for type "
6634 "%qT because %qD does not have external linkage",
6635 expr
, type
, fn_no_ptr
);
6641 if (TYPE_REF_P (type
))
6643 if (REFERENCE_REF_P (fn
))
6644 fn
= TREE_OPERAND (fn
, 0);
6646 fn
= build_address (fn
);
6648 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6649 fn
= build_nop (type
, fn
);
6654 /* Subroutine of convert_nontype_argument.
6655 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6656 Emit an error otherwise. */
6659 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6660 tsubst_flags_t complain
)
6662 tree orig_expr
= expr
;
6664 if (null_ptr_cst_p (expr
))
6666 if (TREE_CODE (expr
) == PTRMEM_CST
6667 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6668 PTRMEM_CST_CLASS (expr
)))
6670 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6672 if (processing_template_decl
6673 && TREE_CODE (expr
) == ADDR_EXPR
6674 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6676 if (complain
& tf_error
)
6678 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6679 error_at (loc
, "%qE is not a valid template argument for type %qT",
6681 if (TREE_CODE (expr
) != PTRMEM_CST
)
6682 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6684 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6689 /* Returns TRUE iff the address of OP is value-dependent.
6691 14.6.2.4 [temp.dep.temp]:
6692 A non-integral non-type template-argument is dependent if its type is
6693 dependent or it has either of the following forms
6696 and contains a nested-name-specifier which specifies a class-name that
6697 names a dependent type.
6699 We generalize this to just say that the address of a member of a
6700 dependent class is value-dependent; the above doesn't cover the
6701 address of a static data member named with an unqualified-id. */
6704 has_value_dependent_address (tree op
)
6706 STRIP_ANY_LOCATION_WRAPPER (op
);
6708 /* We could use get_inner_reference here, but there's no need;
6709 this is only relevant for template non-type arguments, which
6710 can only be expressed as &id-expression. */
6713 tree ctx
= CP_DECL_CONTEXT (op
);
6714 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6721 /* The next set of functions are used for providing helpful explanatory
6722 diagnostics for failed overload resolution. Their messages should be
6723 indented by two spaces for consistency with the messages in
6727 unify_success (bool /*explain_p*/)
6732 /* Other failure functions should call this one, to provide a single function
6733 for setting a breakpoint on. */
6736 unify_invalid (bool /*explain_p*/)
6742 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6745 inform (input_location
,
6746 " couldn%'t deduce template parameter %qD", parm
);
6747 return unify_invalid (explain_p
);
6751 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6754 inform (input_location
,
6755 " types %qT and %qT have incompatible cv-qualifiers",
6757 return unify_invalid (explain_p
);
6761 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6764 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6765 return unify_invalid (explain_p
);
6769 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6772 inform (input_location
,
6773 " template parameter %qD is not a parameter pack, but "
6776 return unify_invalid (explain_p
);
6780 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6783 inform (input_location
,
6784 " template argument %qE does not match "
6785 "pointer-to-member constant %qE",
6787 return unify_invalid (explain_p
);
6791 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6794 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6795 return unify_invalid (explain_p
);
6799 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6802 inform (input_location
,
6803 " inconsistent parameter pack deduction with %qT and %qT",
6805 return unify_invalid (explain_p
);
6809 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6814 inform (input_location
,
6815 " deduced conflicting types for parameter %qT (%qT and %qT)",
6816 parm
, first
, second
);
6818 inform (input_location
,
6819 " deduced conflicting values for non-type parameter "
6820 "%qE (%qE and %qE)", parm
, first
, second
);
6822 return unify_invalid (explain_p
);
6826 unify_vla_arg (bool explain_p
, tree arg
)
6829 inform (input_location
,
6830 " variable-sized array type %qT is not "
6831 "a valid template argument",
6833 return unify_invalid (explain_p
);
6837 unify_method_type_error (bool explain_p
, tree arg
)
6840 inform (input_location
,
6841 " member function type %qT is not a valid template argument",
6843 return unify_invalid (explain_p
);
6847 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6852 inform_n (input_location
, wanted
,
6853 " candidate expects at least %d argument, %d provided",
6854 " candidate expects at least %d arguments, %d provided",
6857 inform_n (input_location
, wanted
,
6858 " candidate expects %d argument, %d provided",
6859 " candidate expects %d arguments, %d provided",
6862 return unify_invalid (explain_p
);
6866 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6868 return unify_arity (explain_p
, have
, wanted
);
6872 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6873 bool least_p
= false)
6875 return unify_arity (explain_p
, have
, wanted
, least_p
);
6879 unify_arg_conversion (bool explain_p
, tree to_type
,
6880 tree from_type
, tree arg
)
6883 inform (cp_expr_loc_or_input_loc (arg
),
6884 " cannot convert %qE (type %qT) to type %qT",
6885 arg
, from_type
, to_type
);
6886 return unify_invalid (explain_p
);
6890 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6891 tree parm
, tree arg
)
6896 case tbr_ambiguous_baseclass
:
6897 inform (input_location
, " %qT is an ambiguous base class of %qT",
6901 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6904 return unify_invalid (explain_p
);
6908 unify_inconsistent_template_template_parameters (bool explain_p
)
6911 inform (input_location
,
6912 " template parameters of a template template argument are "
6913 "inconsistent with other deduced template arguments");
6914 return unify_invalid (explain_p
);
6918 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6921 inform (input_location
,
6922 " cannot deduce a template for %qT from non-template type %qT",
6924 return unify_invalid (explain_p
);
6928 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6931 inform (input_location
,
6932 " template argument %qE does not match %qE", arg
, parm
);
6933 return unify_invalid (explain_p
);
6936 /* True if T is a C++20 template parameter object to store the argument for a
6937 template parameter of class type. */
6940 template_parm_object_p (const_tree t
)
6942 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6943 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6946 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6947 argument for TYPE, points to an unsuitable object.
6949 Also adjust the type of the index in C++20 array subobject references. */
6952 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6954 switch (TREE_CODE (expr
))
6957 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6961 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6966 unsigned i
; tree elt
;
6967 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6968 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6975 tree decl
= TREE_OPERAND (expr
, 0);
6977 if (cxx_dialect
>= cxx20
)
6978 while (TREE_CODE (decl
) == COMPONENT_REF
6979 || TREE_CODE (decl
) == ARRAY_REF
)
6981 tree
&op
= TREE_OPERAND (decl
, 1);
6982 if (TREE_CODE (decl
) == ARRAY_REF
6983 && TREE_CODE (op
) == INTEGER_CST
)
6984 /* Canonicalize array offsets to ptrdiff_t; how they were
6985 written doesn't matter for subobject identity. */
6986 op
= fold_convert (ptrdiff_type_node
, op
);
6987 decl
= TREE_OPERAND (decl
, 0);
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 %qE is not a variable", expr
, type
, decl
);
6998 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7000 if (complain
& tf_error
)
7001 error_at (cp_expr_loc_or_input_loc (expr
),
7002 "%qE is not a valid template argument of type %qT "
7003 "in C++98 because %qD does not have external linkage",
7007 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7008 && decl_linkage (decl
) == lk_none
)
7010 if (complain
& tf_error
)
7011 error_at (cp_expr_loc_or_input_loc (expr
),
7012 "%qE is not a valid template argument of type %qT "
7013 "because %qD has no linkage", expr
, type
, decl
);
7016 /* C++17: For a non-type template-parameter of reference or pointer
7017 type, the value of the constant expression shall not refer to (or
7018 for a pointer type, shall not be the address of):
7019 * a subobject (4.5),
7020 * a temporary object (15.2),
7021 * a string literal (5.13.5),
7022 * the result of a typeid expression (8.2.8), or
7023 * a predefined __func__ variable (11.4.1). */
7024 else if (DECL_ARTIFICIAL (decl
))
7026 if (complain
& tf_error
)
7027 error ("the address of %qD is not a valid template argument",
7031 else if (cxx_dialect
< cxx20
7032 && !(same_type_ignoring_top_level_qualifiers_p
7033 (strip_array_types (TREE_TYPE (type
)),
7034 strip_array_types (TREE_TYPE (decl
)))))
7036 if (complain
& tf_error
)
7037 error ("the address of the %qT subobject of %qD is not a "
7038 "valid template argument", TREE_TYPE (type
), decl
);
7041 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7043 if (complain
& tf_error
)
7044 error ("the address of %qD is not a valid template argument "
7045 "because it does not have static storage duration",
7053 if (!INDIRECT_TYPE_P (type
))
7054 /* We're only concerned about pointers and references here. */;
7055 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7056 /* Null pointer values are OK in C++11. */;
7061 if (complain
& tf_error
)
7062 error ("%qD is not a valid template argument "
7063 "because %qD is a variable, not the address of "
7064 "a variable", expr
, expr
);
7069 if (complain
& tf_error
)
7070 error ("%qE is not a valid template argument for %qT "
7071 "because it is not the address of a variable",
7081 /* The template arguments corresponding to template parameter objects of types
7082 that contain pointers to members. */
7084 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7086 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7087 template argument EXPR. */
7090 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7092 if (TREE_CODE (expr
) == TARGET_EXPR
)
7093 expr
= TARGET_EXPR_INITIAL (expr
);
7095 if (!TREE_CONSTANT (expr
))
7097 if ((complain
& tf_error
)
7098 && require_rvalue_constant_expression (expr
))
7099 cxx_constant_value (expr
);
7100 return error_mark_node
;
7102 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7103 return error_mark_node
;
7105 tree name
= mangle_template_parm_object (expr
);
7106 tree decl
= get_global_binding (name
);
7110 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7111 decl
= create_temporary_var (type
);
7112 DECL_CONTEXT (decl
) = NULL_TREE
;
7113 TREE_STATIC (decl
) = true;
7114 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7115 TREE_READONLY (decl
) = true;
7116 DECL_NAME (decl
) = name
;
7117 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7118 comdat_linkage (decl
);
7120 if (!zero_init_p (type
))
7122 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7123 lower_var_init before we're done mangling. So store the original
7125 tree copy
= unshare_constructor (expr
);
7126 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7129 pushdecl_top_level_and_finish (decl
, expr
);
7134 /* Return the actual template argument corresponding to template parameter
7138 tparm_object_argument (tree var
)
7140 if (zero_init_p (TREE_TYPE (var
)))
7141 return DECL_INITIAL (var
);
7142 return *(tparm_obj_values
->get (var
));
7145 /* Attempt to convert the non-type template parameter EXPR to the
7146 indicated TYPE. If the conversion is successful, return the
7147 converted value. If the conversion is unsuccessful, return
7148 NULL_TREE if we issued an error message, or error_mark_node if we
7149 did not. We issue error messages for out-and-out bad template
7150 parameters, but not simply because the conversion failed, since we
7151 might be just trying to do argument deduction. Both TYPE and EXPR
7152 must be non-dependent.
7154 The conversion follows the special rules described in
7155 [temp.arg.nontype], and it is much more strict than an implicit
7158 This function is called twice for each template argument (see
7159 lookup_template_class for a more accurate description of this
7160 problem). This means that we need to handle expressions which
7161 are not valid in a C++ source, but can be created from the
7162 first call (for instance, casts to perform conversions). These
7163 hacks can go away after we fix the double coercion problem. */
7166 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7169 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7171 /* Detect immediately string literals as invalid non-type argument.
7172 This special-case is not needed for correctness (we would easily
7173 catch this later), but only to provide better diagnostic for this
7174 common user mistake. As suggested by DR 100, we do not mention
7175 linkage issues in the diagnostic as this is not the point. */
7176 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7178 if (complain
& tf_error
)
7179 error ("%qE is not a valid template argument for type %qT "
7180 "because string literals can never be used in this context",
7185 /* Add the ADDR_EXPR now for the benefit of
7186 value_dependent_expression_p. */
7187 if (TYPE_PTROBV_P (type
)
7188 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7190 expr
= decay_conversion (expr
, complain
);
7191 if (expr
== error_mark_node
)
7192 return error_mark_node
;
7195 /* If we are in a template, EXPR may be non-dependent, but still
7196 have a syntactic, rather than semantic, form. For example, EXPR
7197 might be a SCOPE_REF, rather than the VAR_DECL to which the
7198 SCOPE_REF refers. Preserving the qualifying scope is necessary
7199 so that access checking can be performed when the template is
7200 instantiated -- but here we need the resolved form so that we can
7201 convert the argument. */
7202 bool non_dep
= false;
7203 if (TYPE_REF_OBJ_P (type
)
7204 && has_value_dependent_address (expr
))
7205 /* If we want the address and it's value-dependent, don't fold. */;
7206 else if (processing_template_decl
7207 && is_nondependent_constant_expression (expr
))
7209 if (error_operand_p (expr
))
7210 return error_mark_node
;
7211 expr_type
= TREE_TYPE (expr
);
7213 /* If the argument is non-dependent, perform any conversions in
7214 non-dependent context as well. */
7215 processing_template_decl_sentinel
s (non_dep
);
7217 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7219 const bool val_dep_p
= value_dependent_expression_p (expr
);
7221 expr
= canonicalize_expr_argument (expr
, complain
);
7223 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7224 to a non-type argument of "nullptr". */
7225 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7226 expr
= fold_simple (convert (type
, expr
));
7228 /* In C++11, integral or enumeration non-type template arguments can be
7229 arbitrary constant expressions. Pointer and pointer to
7230 member arguments can be general constant expressions that evaluate
7231 to a null value, but otherwise still need to be of a specific form. */
7232 if (cxx_dialect
>= cxx11
)
7234 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7235 /* A PTRMEM_CST is already constant, and a valid template
7236 argument for a parameter of pointer to member type, we just want
7237 to leave it in that form rather than lower it to a
7239 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7240 || cxx_dialect
>= cxx17
)
7242 /* C++17: A template-argument for a non-type template-parameter shall
7243 be a converted constant expression (8.20) of the type of the
7244 template-parameter. */
7245 expr
= build_converted_constant_expr (type
, expr
, complain
);
7246 if (expr
== error_mark_node
)
7247 /* Make sure we return NULL_TREE only if we have really issued
7248 an error, as described above. */
7249 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7250 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7252 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7255 expr
= maybe_constant_value (expr
, NULL_TREE
,
7256 /*manifestly_const_eval=*/true);
7257 expr
= convert_from_reference (expr
);
7259 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7261 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7262 /*manifestly_const_eval=*/true);
7263 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7264 : null_member_pointer_value_p (folded
))
7269 if (TYPE_REF_P (type
))
7270 expr
= mark_lvalue_use (expr
);
7272 expr
= mark_rvalue_use (expr
);
7274 /* HACK: Due to double coercion, we can get a
7275 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7276 which is the tree that we built on the first call (see
7277 below when coercing to reference to object or to reference to
7278 function). We just strip everything and get to the arg.
7279 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7281 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7283 tree probe_type
, probe
= expr
;
7284 if (REFERENCE_REF_P (probe
))
7285 probe
= TREE_OPERAND (probe
, 0);
7286 probe_type
= TREE_TYPE (probe
);
7287 if (TREE_CODE (probe
) == NOP_EXPR
)
7289 /* ??? Maybe we could use convert_from_reference here, but we
7290 would need to relax its constraints because the NOP_EXPR
7291 could actually change the type to something more cv-qualified,
7292 and this is not folded by convert_from_reference. */
7293 tree addr
= TREE_OPERAND (probe
, 0);
7294 if (TYPE_REF_P (probe_type
)
7295 && TREE_CODE (addr
) == ADDR_EXPR
7296 && TYPE_PTR_P (TREE_TYPE (addr
))
7297 && (same_type_ignoring_top_level_qualifiers_p
7298 (TREE_TYPE (probe_type
),
7299 TREE_TYPE (TREE_TYPE (addr
)))))
7301 expr
= TREE_OPERAND (addr
, 0);
7302 expr_type
= TREE_TYPE (probe_type
);
7307 /* [temp.arg.nontype]/5, bullet 1
7309 For a non-type template-parameter of integral or enumeration type,
7310 integral promotions (_conv.prom_) and integral conversions
7311 (_conv.integral_) are applied. */
7312 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7313 || TREE_CODE (type
) == REAL_TYPE
)
7315 if (cxx_dialect
< cxx11
)
7317 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7318 t
= maybe_constant_value (t
);
7319 if (t
!= error_mark_node
)
7323 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7324 return error_mark_node
;
7326 /* Notice that there are constant expressions like '4 % 0' which
7327 do not fold into integer constants. */
7328 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7330 if (complain
& tf_error
)
7332 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7333 if (!require_potential_constant_expression (expr
))
7334 expr
= error_mark_node
;
7336 expr
= cxx_constant_value (expr
);
7337 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7338 inform (loc
, "in template argument for type %qT", type
);
7339 if (expr
== error_mark_node
)
7341 /* else cxx_constant_value complained but gave us
7342 a real constant, so go ahead. */
7343 if (!CONSTANT_CLASS_P (expr
))
7345 /* Some assemble time constant expressions like
7346 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7347 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7348 as we can emit them into .rodata initializers of
7349 variables, yet they can't fold into an INTEGER_CST at
7350 compile time. Refuse them here. */
7351 gcc_checking_assert (reduced_constant_expression_p (expr
));
7352 error_at (loc
, "template argument %qE for type %qT not "
7353 "a compile-time constant", expr
, type
);
7361 /* Avoid typedef problems. */
7362 if (TREE_TYPE (expr
) != type
)
7363 expr
= fold_convert (type
, expr
);
7365 /* [temp.arg.nontype]/5, bullet 2
7367 For a non-type template-parameter of type pointer to object,
7368 qualification conversions (_conv.qual_) and the array-to-pointer
7369 conversion (_conv.array_) are applied. */
7370 else if (TYPE_PTROBV_P (type
))
7372 tree decayed
= expr
;
7374 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7375 decay_conversion or an explicit cast. If it's a problematic cast,
7376 we'll complain about it below. */
7377 if (TREE_CODE (expr
) == NOP_EXPR
)
7381 if (TREE_CODE (probe
) == ADDR_EXPR
7382 && TYPE_PTR_P (TREE_TYPE (probe
)))
7385 expr_type
= TREE_TYPE (expr
);
7389 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7391 A template-argument for a non-type, non-template template-parameter
7392 shall be one of: [...]
7394 -- the name of a non-type template-parameter;
7395 -- the address of an object or function with external linkage, [...]
7396 expressed as "& id-expression" where the & is optional if the name
7397 refers to a function or array, or if the corresponding
7398 template-parameter is a reference.
7400 Here, we do not care about functions, as they are invalid anyway
7401 for a parameter of type pointer-to-object. */
7404 /* Non-type template parameters are OK. */
7406 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7407 /* Null pointer values are OK in C++11. */;
7408 else if (TREE_CODE (expr
) != ADDR_EXPR
7409 && !INDIRECT_TYPE_P (expr_type
))
7410 /* Other values, like integer constants, might be valid
7411 non-type arguments of some other type. */
7412 return error_mark_node
;
7413 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7418 expr
= perform_qualification_conversions (type
, expr
);
7419 if (expr
== error_mark_node
)
7420 return error_mark_node
;
7422 /* [temp.arg.nontype]/5, bullet 3
7424 For a non-type template-parameter of type reference to object, no
7425 conversions apply. The type referred to by the reference may be more
7426 cv-qualified than the (otherwise identical) type of the
7427 template-argument. The template-parameter is bound directly to the
7428 template-argument, which must be an lvalue. */
7429 else if (TYPE_REF_OBJ_P (type
))
7431 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7433 return error_mark_node
;
7435 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7437 if (complain
& tf_error
)
7438 error ("%qE is not a valid template argument for type %qT "
7439 "because of conflicts in cv-qualification", expr
, type
);
7443 if (!lvalue_p (expr
))
7445 if (complain
& tf_error
)
7446 error ("%qE is not a valid template argument for type %qT "
7447 "because it is not an lvalue", expr
, type
);
7451 /* [temp.arg.nontype]/1
7453 A template-argument for a non-type, non-template template-parameter
7454 shall be one of: [...]
7456 -- the address of an object or function with external linkage. */
7457 if (INDIRECT_REF_P (expr
)
7458 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7460 expr
= TREE_OPERAND (expr
, 0);
7463 if (complain
& tf_error
)
7464 error ("%q#D is not a valid template argument for type %qT "
7465 "because a reference variable does not have a constant "
7466 "address", expr
, type
);
7471 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7472 /* OK, dependent reference. We don't want to ask whether a DECL is
7473 itself value-dependent, since what we want here is its address. */;
7476 expr
= build_address (expr
);
7478 if (invalid_tparm_referent_p (type
, expr
, complain
))
7482 if (!same_type_p (type
, TREE_TYPE (expr
)))
7483 expr
= build_nop (type
, expr
);
7485 /* [temp.arg.nontype]/5, bullet 4
7487 For a non-type template-parameter of type pointer to function, only
7488 the function-to-pointer conversion (_conv.func_) is applied. If the
7489 template-argument represents a set of overloaded functions (or a
7490 pointer to such), the matching function is selected from the set
7492 else if (TYPE_PTRFN_P (type
))
7494 /* If the argument is a template-id, we might not have enough
7495 context information to decay the pointer. */
7496 if (!type_unknown_p (expr_type
))
7498 expr
= decay_conversion (expr
, complain
);
7499 if (expr
== error_mark_node
)
7500 return error_mark_node
;
7503 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7504 /* Null pointer values are OK in C++11. */
7505 return perform_qualification_conversions (type
, expr
);
7507 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7508 if (!expr
|| expr
== error_mark_node
)
7511 /* [temp.arg.nontype]/5, bullet 5
7513 For a non-type template-parameter of type reference to function, no
7514 conversions apply. If the template-argument represents a set of
7515 overloaded functions, the matching function is selected from the set
7517 else if (TYPE_REFFN_P (type
))
7519 if (TREE_CODE (expr
) == ADDR_EXPR
)
7521 if (complain
& tf_error
)
7523 error ("%qE is not a valid template argument for type %qT "
7524 "because it is a pointer", expr
, type
);
7525 inform (input_location
, "try using %qE instead",
7526 TREE_OPERAND (expr
, 0));
7531 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7532 if (!expr
|| expr
== error_mark_node
)
7535 /* [temp.arg.nontype]/5, bullet 6
7537 For a non-type template-parameter of type pointer to member function,
7538 no conversions apply. If the template-argument represents a set of
7539 overloaded member functions, the matching member function is selected
7540 from the set (_over.over_). */
7541 else if (TYPE_PTRMEMFUNC_P (type
))
7543 expr
= instantiate_type (type
, expr
, tf_none
);
7544 if (expr
== error_mark_node
)
7545 return error_mark_node
;
7547 /* [temp.arg.nontype] bullet 1 says the pointer to member
7548 expression must be a pointer-to-member constant. */
7550 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7553 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7554 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7555 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7556 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7558 /* [temp.arg.nontype]/5, bullet 7
7560 For a non-type template-parameter of type pointer to data member,
7561 qualification conversions (_conv.qual_) are applied. */
7562 else if (TYPE_PTRDATAMEM_P (type
))
7564 /* [temp.arg.nontype] bullet 1 says the pointer to member
7565 expression must be a pointer-to-member constant. */
7567 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7570 expr
= perform_qualification_conversions (type
, expr
);
7571 if (expr
== error_mark_node
)
7574 else if (NULLPTR_TYPE_P (type
))
7576 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7578 if (complain
& tf_error
)
7579 error ("%qE is not a valid template argument for type %qT "
7580 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7585 else if (CLASS_TYPE_P (type
))
7587 /* Replace the argument with a reference to the corresponding template
7588 parameter object. */
7590 expr
= get_template_parm_object (expr
, complain
);
7591 if (expr
== error_mark_node
)
7594 /* A template non-type parameter must be one of the above. */
7598 /* Sanity check: did we actually convert the argument to the
7600 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7601 (type
, TREE_TYPE (expr
)));
7602 return convert_from_reference (expr
);
7605 /* Subroutine of coerce_template_template_parms, which returns 1 if
7606 PARM_PARM and ARG_PARM match using the rule for the template
7607 parameters of template template parameters. Both PARM and ARG are
7608 template parameters; the rest of the arguments are the same as for
7609 coerce_template_template_parms.
7612 coerce_template_template_parm (tree parm
,
7614 tsubst_flags_t complain
,
7618 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7619 || parm
== NULL_TREE
|| error_operand_p (parm
))
7622 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7625 switch (TREE_CODE (parm
))
7628 /* We encounter instantiations of templates like
7629 template <template <template <class> class> class TT>
7632 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7633 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7635 if (!coerce_template_template_parms
7636 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7642 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7643 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7644 /* Argument is a parameter pack but parameter is not. */
7649 /* The tsubst call is used to handle cases such as
7651 template <int> class C {};
7652 template <class T, template <T> class TT> class D {};
7655 i.e. the parameter list of TT depends on earlier parameters. */
7656 if (!uses_template_parms (TREE_TYPE (arg
)))
7658 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7659 if (!uses_template_parms (t
)
7660 && !same_type_p (t
, TREE_TYPE (arg
)))
7664 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7665 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7666 /* Argument is a parameter pack but parameter is not. */
7678 /* Coerce template argument list ARGLIST for use with template
7679 template-parameter TEMPL. */
7682 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7683 tsubst_flags_t complain
)
7685 /* Consider an example where a template template parameter declared as
7687 template <class T, class U = std::allocator<T> > class TT
7689 The template parameter level of T and U are one level larger than
7690 of TT. To proper process the default argument of U, say when an
7691 instantiation `TT<int>' is seen, we need to build the full
7692 arguments containing {int} as the innermost level. Outer levels,
7693 available when not appearing as default template argument, can be
7694 obtained from the arguments of the enclosing template.
7696 Suppose that TT is later substituted with std::vector. The above
7697 instantiation is `TT<int, std::allocator<T> >' with TT at
7698 level 1, and T at level 2, while the template arguments at level 1
7699 becomes {std::vector} and the inner level 2 is {int}. */
7701 tree outer
= DECL_CONTEXT (templ
);
7703 outer
= generic_targs_for (outer
);
7704 else if (current_template_parms
)
7706 /* This is an argument of the current template, so we haven't set
7707 DECL_CONTEXT yet. */
7708 tree relevant_template_parms
;
7710 /* Parameter levels that are greater than the level of the given
7711 template template parm are irrelevant. */
7712 relevant_template_parms
= current_template_parms
;
7713 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7714 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7715 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7717 outer
= template_parms_to_args (relevant_template_parms
);
7721 arglist
= add_to_template_args (outer
, arglist
);
7723 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7724 return coerce_template_parms (parmlist
, arglist
, templ
,
7726 /*require_all_args=*/true,
7727 /*use_default_args=*/true);
7730 /* A cache of template template parameters with match-all default
7732 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7734 /* T is a bound template template-parameter. Copy its arguments into default
7735 arguments of the template template-parameter's template parameters. */
7738 add_defaults_to_ttp (tree otmpl
)
7740 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7743 tree ntmpl
= copy_node (otmpl
);
7745 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7746 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7747 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7748 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7749 TYPE_NAME (ntype
) = ntmpl
;
7750 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7752 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7753 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7754 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7755 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7757 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7758 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7759 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7760 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7761 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7763 tree o
= TREE_VEC_ELT (vec
, i
);
7764 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7766 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7767 TREE_PURPOSE (n
) = any_targ_node
;
7771 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7775 /* ARG is a bound potential template template-argument, and PARGS is a list
7776 of arguments for the corresponding template template-parameter. Adjust
7777 PARGS as appropriate for application to ARG's template, and if ARG is a
7778 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7779 arguments to the template template parameter. */
7782 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7784 ++processing_template_decl
;
7785 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7786 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7788 /* When comparing two template template-parameters in partial ordering,
7789 rewrite the one currently being used as an argument to have default
7790 arguments for all parameters. */
7791 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7792 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7793 if (pargs
!= error_mark_node
)
7794 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7795 TYPE_TI_ARGS (arg
));
7800 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7801 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7802 /*require_all*/true,
7803 /*use_default*/true);
7805 --processing_template_decl
;
7809 /* Subroutine of unify for the case when PARM is a
7810 BOUND_TEMPLATE_TEMPLATE_PARM. */
7813 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7816 tree parmvec
= TYPE_TI_ARGS (parm
);
7817 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7819 /* The template template parm might be variadic and the argument
7820 not, so flatten both argument lists. */
7821 parmvec
= expand_template_argument_pack (parmvec
);
7822 argvec
= expand_template_argument_pack (argvec
);
7826 /* In keeping with P0522R0, adjust P's template arguments
7827 to apply to A's template; then flatten it again. */
7828 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7829 nparmvec
= expand_template_argument_pack (nparmvec
);
7831 if (unify (tparms
, targs
, nparmvec
, argvec
,
7832 UNIFY_ALLOW_NONE
, explain_p
))
7835 /* If the P0522 adjustment eliminated a pack expansion, deduce
7838 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7839 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7840 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7845 /* Deduce arguments T, i from TT<T> or TT<i>.
7846 We check each element of PARMVEC and ARGVEC individually
7847 rather than the whole TREE_VEC since they can have
7848 different number of elements, which is allowed under N2555. */
7850 int len
= TREE_VEC_LENGTH (parmvec
);
7852 /* Check if the parameters end in a pack, making them
7854 int parm_variadic_p
= 0;
7856 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7857 parm_variadic_p
= 1;
7859 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7860 /* If the template argument list of P contains a pack
7861 expansion that is not the last template argument, the
7862 entire template argument list is a non-deduced
7864 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7865 return unify_success (explain_p
);
7867 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7868 return unify_too_few_arguments (explain_p
,
7869 TREE_VEC_LENGTH (argvec
), len
);
7871 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7872 if (unify (tparms
, targs
,
7873 TREE_VEC_ELT (parmvec
, i
),
7874 TREE_VEC_ELT (argvec
, i
),
7875 UNIFY_ALLOW_NONE
, explain_p
))
7879 && unify_pack_expansion (tparms
, targs
,
7882 /*subr=*/true, explain_p
))
7889 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7890 template template parameters. Both PARM_PARMS and ARG_PARMS are
7891 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7894 Consider the example:
7895 template <class T> class A;
7896 template<template <class U> class TT> class B;
7898 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7899 the parameters to A, and OUTER_ARGS contains A. */
7902 coerce_template_template_parms (tree parm_parms
,
7904 tsubst_flags_t complain
,
7908 int nparms
, nargs
, i
;
7912 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7913 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7915 nparms
= TREE_VEC_LENGTH (parm_parms
);
7916 nargs
= TREE_VEC_LENGTH (arg_parms
);
7920 /* P0522R0: A template template-parameter P is at least as specialized as
7921 a template template-argument A if, given the following rewrite to two
7922 function templates, the function template corresponding to P is at
7923 least as specialized as the function template corresponding to A
7924 according to the partial ordering rules for function templates
7925 ([temp.func.order]). Given an invented class template X with the
7926 template parameter list of A (including default arguments):
7928 * Each of the two function templates has the same template parameters,
7929 respectively, as P or A.
7931 * Each function template has a single function parameter whose type is
7932 a specialization of X with template arguments corresponding to the
7933 template parameters from the respective function template where, for
7934 each template parameter PP in the template parameter list of the
7935 function template, a corresponding template argument AA is formed. If
7936 PP declares a parameter pack, then AA is the pack expansion
7937 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7939 If the rewrite produces an invalid type, then P is not at least as
7940 specialized as A. */
7942 /* So coerce P's args to apply to A's parms, and then deduce between A's
7943 args and the converted args. If that succeeds, A is at least as
7944 specialized as P, so they match.*/
7945 tree pargs
= template_parms_level_to_args (parm_parms
);
7946 pargs
= add_outermost_template_args (outer_args
, pargs
);
7947 ++processing_template_decl
;
7948 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7949 /*require_all*/true, /*use_default*/true);
7950 --processing_template_decl
;
7951 if (pargs
!= error_mark_node
)
7953 tree targs
= make_tree_vec (nargs
);
7954 tree aargs
= template_parms_level_to_args (arg_parms
);
7955 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7961 /* Determine whether we have a parameter pack at the end of the
7962 template template parameter's template parameter list. */
7963 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7965 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7967 if (error_operand_p (parm
))
7970 switch (TREE_CODE (parm
))
7974 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7979 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7989 && !(variadic_p
&& nargs
>= nparms
- 1))
7992 /* Check all of the template parameters except the parameter pack at
7993 the end (if any). */
7994 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7996 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7997 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8000 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8001 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8003 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8011 /* Check each of the template parameters in the template
8012 argument against the template parameter pack at the end of
8013 the template template parameter. */
8014 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8017 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8019 for (; i
< nargs
; ++i
)
8021 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8024 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8026 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8035 /* Verifies that the deduced template arguments (in TARGS) for the
8036 template template parameters (in TPARMS) represent valid bindings,
8037 by comparing the template parameter list of each template argument
8038 to the template parameter list of its corresponding template
8039 template parameter, in accordance with DR150. This
8040 routine can only be called after all template arguments have been
8041 deduced. It will return TRUE if all of the template template
8042 parameter bindings are okay, FALSE otherwise. */
8044 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8046 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8049 /* We're dealing with template parms in this process. */
8050 ++processing_template_decl
;
8052 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8054 for (i
= 0; i
< ntparms
; ++i
)
8056 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8057 tree targ
= TREE_VEC_ELT (targs
, i
);
8059 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8061 tree packed_args
= NULL_TREE
;
8064 if (ARGUMENT_PACK_P (targ
))
8066 /* Look inside the argument pack. */
8067 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8068 len
= TREE_VEC_LENGTH (packed_args
);
8071 for (idx
= 0; idx
< len
; ++idx
)
8073 tree targ_parms
= NULL_TREE
;
8076 /* Extract the next argument from the argument
8078 targ
= TREE_VEC_ELT (packed_args
, idx
);
8080 if (PACK_EXPANSION_P (targ
))
8081 /* Look at the pattern of the pack expansion. */
8082 targ
= PACK_EXPANSION_PATTERN (targ
);
8084 /* Extract the template parameters from the template
8086 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8087 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8088 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8089 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8091 /* Verify that we can coerce the template template
8092 parameters from the template argument to the template
8093 parameter. This requires an exact match. */
8095 && !coerce_template_template_parms
8096 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8111 --processing_template_decl
;
8115 /* Since type attributes aren't mangled, we need to strip them from
8116 template type arguments. */
8119 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8121 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8123 bool removed_attributes
= false;
8124 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8125 if (removed_attributes
8126 && (complain
& tf_warning
))
8127 warning (OPT_Wignored_attributes
,
8128 "ignoring attributes on template argument %qT", arg
);
8132 /* And from inside dependent non-type arguments like sizeof(Type). */
8135 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8137 if (!arg
|| arg
== error_mark_node
)
8139 bool removed_attributes
= false;
8140 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8141 if (removed_attributes
8142 && (complain
& tf_warning
))
8143 warning (OPT_Wignored_attributes
,
8144 "ignoring attributes in template argument %qE", arg
);
8148 /* A template declaration can be substituted for a constrained
8149 template template parameter only when the argument is no more
8150 constrained than the parameter. */
8153 is_compatible_template_arg (tree parm
, tree arg
)
8155 tree parm_cons
= get_constraints (parm
);
8157 /* For now, allow constrained template template arguments
8158 and unconstrained template template parameters. */
8159 if (parm_cons
== NULL_TREE
)
8162 /* If the template parameter is constrained, we need to rewrite its
8163 constraints in terms of the ARG's template parameters. This ensures
8164 that all of the template parameter types will have the same depth.
8166 Note that this is only valid when coerce_template_template_parm is
8167 true for the innermost template parameters of PARM and ARG. In other
8168 words, because coercion is successful, this conversion will be valid. */
8169 tree new_args
= NULL_TREE
;
8172 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8173 new_args
= template_parms_level_to_args (aparms
);
8174 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8175 tf_none
, NULL_TREE
);
8176 if (parm_cons
== error_mark_node
)
8180 return weakly_subsumes (parm_cons
, arg
);
8183 // Convert a placeholder argument into a binding to the original
8184 // parameter. The original parameter is saved as the TREE_TYPE of
8187 convert_wildcard_argument (tree parm
, tree arg
)
8189 TREE_TYPE (arg
) = parm
;
8193 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8194 because one of them is dependent. But we need to represent the
8195 conversion for the benefit of cp_tree_equal. */
8198 maybe_convert_nontype_argument (tree type
, tree arg
)
8200 /* Auto parms get no conversion. */
8201 if (type_uses_auto (type
))
8203 /* We don't need or want to add this conversion now if we're going to use the
8204 argument for deduction. */
8205 if (value_dependent_expression_p (arg
))
8208 type
= cv_unqualified (type
);
8209 tree argtype
= TREE_TYPE (arg
);
8210 if (same_type_p (type
, argtype
))
8213 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8214 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8218 /* Convert the indicated template ARG as necessary to match the
8219 indicated template PARM. Returns the converted ARG, or
8220 error_mark_node if the conversion was unsuccessful. Error and
8221 warning messages are issued under control of COMPLAIN. This
8222 conversion is for the Ith parameter in the parameter list. ARGS is
8223 the full set of template arguments deduced so far. */
8226 convert_template_argument (tree parm
,
8229 tsubst_flags_t complain
,
8235 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8237 if (parm
== error_mark_node
|| error_operand_p (arg
))
8238 return error_mark_node
;
8240 /* Trivially convert placeholders. */
8241 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8242 return convert_wildcard_argument (parm
, arg
);
8244 if (arg
== any_targ_node
)
8247 if (TREE_CODE (arg
) == TREE_LIST
8248 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8250 /* The template argument was the name of some
8251 member function. That's usually
8252 invalid, but static members are OK. In any
8253 case, grab the underlying fields/functions
8254 and issue an error later if required. */
8255 TREE_TYPE (arg
) = unknown_type_node
;
8260 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8261 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8262 || requires_tmpl_type
);
8264 /* When determining whether an argument pack expansion is a template,
8265 look at the pattern. */
8266 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8267 arg
= PACK_EXPANSION_PATTERN (arg
);
8269 /* Deal with an injected-class-name used as a template template arg. */
8270 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8272 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8273 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8275 if (cxx_dialect
>= cxx11
)
8276 /* OK under DR 1004. */;
8277 else if (complain
& tf_warning_or_error
)
8278 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8279 " used as template template argument", TYPE_NAME (arg
));
8280 else if (flag_pedantic_errors
)
8288 ((TREE_CODE (arg
) == TEMPLATE_DECL
8289 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8290 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8291 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8292 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8295 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8296 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8297 arg
= TYPE_STUB_DECL (arg
);
8299 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8301 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8302 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8304 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8306 if (complain
& tf_error
)
8307 error ("invalid use of destructor %qE as a type", orig_arg
);
8308 return error_mark_node
;
8311 permerror (input_location
,
8312 "to refer to a type member of a template parameter, "
8313 "use %<typename %E%>", orig_arg
);
8315 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8316 TREE_OPERAND (arg
, 1),
8322 if (is_type
!= requires_type
)
8326 if (complain
& tf_error
)
8328 error ("type/value mismatch at argument %d in template "
8329 "parameter list for %qD",
8333 /* The template argument is a type, but we're expecting
8335 inform (input_location
,
8336 " expected a constant of type %qT, got %qT",
8338 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8339 /* [temp.arg]/2: "In a template-argument, an ambiguity
8340 between a type-id and an expression is resolved to a
8341 type-id, regardless of the form of the corresponding
8342 template-parameter." So give the user a clue. */
8343 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8344 inform (input_location
, " ambiguous template argument "
8345 "for non-type template parameter is treated as "
8348 else if (requires_tmpl_type
)
8349 inform (input_location
,
8350 " expected a class template, got %qE", orig_arg
);
8352 inform (input_location
,
8353 " expected a type, got %qE", orig_arg
);
8356 return error_mark_node
;
8358 if (is_tmpl_type
^ requires_tmpl_type
)
8360 if (in_decl
&& (complain
& tf_error
))
8362 error ("type/value mismatch at argument %d in template "
8363 "parameter list for %qD",
8366 inform (input_location
,
8367 " expected a type, got %qT", DECL_NAME (arg
));
8369 inform (input_location
,
8370 " expected a class template, got %qT", orig_arg
);
8372 return error_mark_node
;
8375 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8376 /* We already did the appropriate conversion when packing args. */
8380 if (requires_tmpl_type
)
8382 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8383 /* The number of argument required is not known yet.
8384 Just accept it for now. */
8388 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8391 /* Strip alias templates that are equivalent to another
8393 arg
= get_underlying_template (arg
);
8394 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8396 if (coerce_template_template_parms (parmparm
, argparm
,
8402 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8404 if (val
!= error_mark_node
)
8406 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8407 val
= TREE_TYPE (val
);
8408 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8409 val
= make_pack_expansion (val
, complain
);
8414 if (in_decl
&& (complain
& tf_error
))
8416 error ("type/value mismatch at argument %d in "
8417 "template parameter list for %qD",
8419 inform (input_location
,
8420 " expected a template of type %qD, got %qT",
8424 val
= error_mark_node
;
8427 // Check that the constraints are compatible before allowing the
8429 if (val
!= error_mark_node
)
8430 if (!is_compatible_template_arg (parm
, arg
))
8432 if (in_decl
&& (complain
& tf_error
))
8434 error ("constraint mismatch at argument %d in "
8435 "template parameter list for %qD",
8437 inform (input_location
, " expected %qD but got %qD",
8440 val
= error_mark_node
;
8446 /* We only form one instance of each template specialization.
8447 Therefore, if we use a non-canonical variant (i.e., a
8448 typedef), any future messages referring to the type will use
8449 the typedef, which is confusing if those future uses do not
8450 themselves also use the typedef. */
8452 val
= canonicalize_type_argument (val
, complain
);
8456 tree t
= TREE_TYPE (parm
);
8458 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8459 > TMPL_ARGS_DEPTH (args
))
8460 /* We don't have enough levels of args to do any substitution. This
8461 can happen in the context of -fnew-ttp-matching. */;
8462 else if (tree a
= type_uses_auto (t
))
8464 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8465 if (t
== error_mark_node
)
8466 return error_mark_node
;
8469 t
= tsubst (t
, args
, complain
, in_decl
);
8471 if (invalid_nontype_parm_type_p (t
, complain
))
8472 return error_mark_node
;
8474 if (t
!= TREE_TYPE (parm
))
8475 t
= canonicalize_type_argument (t
, complain
);
8477 if (!type_dependent_expression_p (orig_arg
)
8478 && !uses_template_parms (t
))
8479 /* We used to call digest_init here. However, digest_init
8480 will report errors, which we don't want when complain
8481 is zero. More importantly, digest_init will try too
8482 hard to convert things: for example, `0' should not be
8483 converted to pointer type at this point according to
8484 the standard. Accepting this is not merely an
8485 extension, since deciding whether or not these
8486 conversions can occur is part of determining which
8487 function template to call, or whether a given explicit
8488 argument specification is valid. */
8489 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8492 val
= canonicalize_expr_argument (orig_arg
, complain
);
8493 val
= maybe_convert_nontype_argument (t
, val
);
8497 if (val
== NULL_TREE
)
8498 val
= error_mark_node
;
8499 else if (val
== error_mark_node
&& (complain
& tf_error
))
8500 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8501 "could not convert template argument %qE from %qT to %qT",
8502 orig_arg
, TREE_TYPE (orig_arg
), t
);
8504 if (INDIRECT_REF_P (val
))
8506 /* Reject template arguments that are references to built-in
8507 functions with no library fallbacks. */
8508 const_tree inner
= TREE_OPERAND (val
, 0);
8509 const_tree innertype
= TREE_TYPE (inner
);
8511 && TYPE_REF_P (innertype
)
8512 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8513 && TREE_OPERAND_LENGTH (inner
) > 0
8514 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8515 return error_mark_node
;
8518 if (TREE_CODE (val
) == SCOPE_REF
)
8520 /* Strip typedefs from the SCOPE_REF. */
8521 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8522 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8524 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8525 QUALIFIED_NAME_IS_TEMPLATE (val
));
8532 /* Coerces the remaining template arguments in INNER_ARGS (from
8533 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8534 Returns the coerced argument pack. PARM_IDX is the position of this
8535 parameter in the template parameter list. ARGS is the original
8536 template argument list. */
8538 coerce_template_parameter_pack (tree parms
,
8546 tsubst_flags_t complain
)
8548 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8549 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8552 tree packed_parms
= NULL_TREE
;
8554 if (arg_idx
> nargs
)
8557 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8559 /* When the template parameter is a non-type template parameter pack
8560 or template template parameter pack whose type or template
8561 parameters use parameter packs, we know exactly how many arguments
8562 we are looking for. Build a vector of the instantiated decls for
8563 these template parameters in PACKED_PARMS. */
8564 /* We can't use make_pack_expansion here because it would interpret a
8565 _DECL as a use rather than a declaration. */
8566 tree decl
= TREE_VALUE (parm
);
8567 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8568 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8569 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8570 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8572 TREE_VEC_LENGTH (args
)--;
8573 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8574 TREE_VEC_LENGTH (args
)++;
8576 if (packed_parms
== error_mark_node
)
8577 return error_mark_node
;
8579 /* If we're doing a partial instantiation of a member template,
8580 verify that all of the types used for the non-type
8581 template parameter pack are, in fact, valid for non-type
8582 template parameters. */
8584 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8586 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8587 for (j
= 0; j
< len
; ++j
)
8589 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8590 if (TREE_CODE (t
) == PARM_DECL
8591 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8592 return error_mark_node
;
8594 /* We don't know how many args we have yet, just
8595 use the unconverted ones for now. */
8599 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8601 /* Check if we have a placeholder pack, which indicates we're
8602 in the context of a introduction list. In that case we want
8603 to match this pack to the single placeholder. */
8604 else if (arg_idx
< nargs
8605 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8606 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8608 nargs
= arg_idx
+ 1;
8609 packed_args
= make_tree_vec (1);
8612 packed_args
= make_tree_vec (nargs
- arg_idx
);
8614 /* Convert the remaining arguments, which will be a part of the
8615 parameter pack "parm". */
8616 int first_pack_arg
= arg_idx
;
8617 for (; arg_idx
< nargs
; ++arg_idx
)
8619 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8620 tree actual_parm
= TREE_VALUE (parm
);
8621 int pack_idx
= arg_idx
- first_pack_arg
;
8625 /* Once we've packed as many args as we have types, stop. */
8626 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8628 else if (PACK_EXPANSION_P (arg
))
8629 /* We don't know how many args we have yet, just
8630 use the unconverted ones for now. */
8633 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8636 if (arg
== error_mark_node
)
8638 if (complain
& tf_error
)
8639 error ("template argument %d is invalid", arg_idx
+ 1);
8642 arg
= convert_template_argument (actual_parm
,
8643 arg
, new_args
, complain
, parm_idx
,
8645 if (arg
== error_mark_node
)
8647 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8650 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8651 && TREE_VEC_LENGTH (packed_args
) > 0)
8653 if (complain
& tf_error
)
8654 error ("wrong number of template arguments (%d, should be %d)",
8655 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8656 return error_mark_node
;
8659 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8660 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8661 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8664 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8665 TREE_CONSTANT (argument_pack
) = 1;
8668 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8670 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8671 TREE_VEC_LENGTH (packed_args
));
8672 return argument_pack
;
8675 /* Returns the number of pack expansions in the template argument vector
8679 pack_expansion_args_count (tree args
)
8684 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8686 tree elt
= TREE_VEC_ELT (args
, i
);
8687 if (elt
&& PACK_EXPANSION_P (elt
))
8693 /* Convert all template arguments to their appropriate types, and
8694 return a vector containing the innermost resulting template
8695 arguments. If any error occurs, return error_mark_node. Error and
8696 warning messages are issued under control of COMPLAIN.
8698 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8699 for arguments not specified in ARGS. Otherwise, if
8700 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8701 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8702 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8706 coerce_template_parms (tree parms
,
8709 tsubst_flags_t complain
,
8710 bool require_all_args
,
8711 bool use_default_args
)
8713 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8714 tree orig_inner_args
;
8717 tree new_inner_args
;
8719 /* When used as a boolean value, indicates whether this is a
8720 variadic template parameter list. Since it's an int, we can also
8721 subtract it from nparms to get the number of non-variadic
8724 int variadic_args_p
= 0;
8725 int post_variadic_parms
= 0;
8727 /* Adjustment to nparms for fixed parameter packs. */
8728 int fixed_pack_adjust
= 0;
8729 int fixed_packs
= 0;
8732 /* Likewise for parameters with default arguments. */
8735 if (args
== error_mark_node
)
8736 return error_mark_node
;
8738 nparms
= TREE_VEC_LENGTH (parms
);
8740 /* Determine if there are any parameter packs or default arguments. */
8741 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8743 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8745 ++post_variadic_parms
;
8746 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8748 if (TREE_PURPOSE (parm
))
8752 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8753 /* If there are no parameters that follow a parameter pack, we need to
8754 expand any argument packs so that we can deduce a parameter pack from
8755 some non-packed args followed by an argument pack, as in variadic85.C.
8756 If there are such parameters, we need to leave argument packs intact
8757 so the arguments are assigned properly. This can happen when dealing
8758 with a nested class inside a partial specialization of a class
8759 template, as in variadic92.C, or when deducing a template parameter pack
8760 from a sub-declarator, as in variadic114.C. */
8761 if (!post_variadic_parms
)
8762 inner_args
= expand_template_argument_pack (inner_args
);
8764 /* Count any pack expansion args. */
8765 variadic_args_p
= pack_expansion_args_count (inner_args
);
8767 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8768 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8769 || (nargs
< nparms
- variadic_p
8772 && (!use_default_args
8773 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8774 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8777 if (complain
& tf_error
)
8779 if (variadic_p
|| default_p
)
8781 nparms
-= variadic_p
+ default_p
;
8782 error ("wrong number of template arguments "
8783 "(%d, should be at least %d)", nargs
, nparms
);
8786 error ("wrong number of template arguments "
8787 "(%d, should be %d)", nargs
, nparms
);
8790 inform (DECL_SOURCE_LOCATION (in_decl
),
8791 "provided for %qD", in_decl
);
8794 return error_mark_node
;
8796 /* We can't pass a pack expansion to a non-pack parameter of an alias
8797 template (DR 1430). */
8799 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8800 || concept_definition_p (in_decl
))
8802 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8804 if (complain
& tf_error
)
8806 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8808 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8809 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8811 if (PACK_EXPANSION_P (arg
)
8812 && !template_parameter_pack_p (parm
))
8814 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8815 error_at (location_of (arg
),
8816 "pack expansion argument for non-pack parameter "
8817 "%qD of alias template %qD", parm
, in_decl
);
8819 error_at (location_of (arg
),
8820 "pack expansion argument for non-pack parameter "
8821 "%qD of concept %qD", parm
, in_decl
);
8822 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8829 return error_mark_node
;
8832 /* We need to evaluate the template arguments, even though this
8833 template-id may be nested within a "sizeof". */
8836 new_inner_args
= make_tree_vec (nparms
);
8837 new_args
= add_outermost_template_args (args
, new_inner_args
);
8838 int pack_adjust
= 0;
8839 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8844 /* Get the Ith template parameter. */
8845 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8847 if (parm
== error_mark_node
)
8849 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8853 /* Calculate the next argument. */
8854 if (arg_idx
< nargs
)
8855 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8859 if (template_parameter_pack_p (TREE_VALUE (parm
))
8860 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8861 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8863 /* Some arguments will be placed in the
8864 template parameter pack PARM. */
8865 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8866 inner_args
, arg_idx
,
8870 if (arg
== NULL_TREE
)
8872 /* We don't know how many args we have yet, just use the
8873 unconverted (and still packed) ones for now. */
8874 new_inner_args
= orig_inner_args
;
8879 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8881 /* Store this argument. */
8882 if (arg
== error_mark_node
)
8885 /* We are done with all of the arguments. */
8891 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8892 arg_idx
+= pack_adjust
;
8893 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8896 fixed_pack_adjust
+= pack_adjust
;
8904 if (PACK_EXPANSION_P (arg
))
8906 /* "If every valid specialization of a variadic template
8907 requires an empty template parameter pack, the template is
8908 ill-formed, no diagnostic required." So check that the
8909 pattern works with this parameter. */
8910 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8911 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8915 if (conv
== error_mark_node
)
8917 if (complain
& tf_error
)
8918 inform (input_location
, "so any instantiation with a "
8919 "non-empty parameter pack would be ill-formed");
8922 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8923 /* Recover from missing typename. */
8924 TREE_VEC_ELT (inner_args
, arg_idx
)
8925 = make_pack_expansion (conv
, complain
);
8927 /* We don't know how many args we have yet, just
8928 use the unconverted ones for now. */
8929 new_inner_args
= inner_args
;
8934 else if (require_all_args
)
8936 /* There must be a default arg in this case. */
8937 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8939 /* The position of the first default template argument,
8940 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8942 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8943 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8944 arg_idx
- pack_adjust
);
8949 if (arg
== error_mark_node
)
8951 if (complain
& tf_error
)
8952 error ("template argument %d is invalid", arg_idx
+ 1);
8956 /* This can occur if there was an error in the template
8957 parameter list itself (which we would already have
8958 reported) that we are trying to recover from, e.g., a class
8959 template with a parameter list such as
8960 template<typename..., typename> (cpp0x/variadic150.C). */
8963 /* This can also happen with a fixed parameter pack (71834). */
8964 if (arg_idx
>= nargs
)
8968 arg
= convert_template_argument (TREE_VALUE (parm
),
8969 arg
, new_args
, complain
,
8972 if (arg
== error_mark_node
)
8975 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8978 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8980 /* If we had fixed parameter packs, we didn't know how many arguments we
8981 actually needed earlier; now we do. */
8982 nparms
+= fixed_pack_adjust
;
8983 variadic_p
-= fixed_packs
;
8987 if (arg_idx
< nargs
)
8989 /* We had some pack expansion arguments that will only work if the packs
8990 are empty, but wait until instantiation time to complain.
8991 See variadic-ttp3.C. */
8993 /* Except that we can't provide empty packs to alias templates or
8994 concepts when there are no corresponding parameters. Basically,
8995 we can get here with this:
8997 template<typename T> concept C = true;
8999 template<typename... Args>
9003 When parsing C<Args...>, we try to form a concept check of
9004 C<?, Args...>. Without the extra check for substituting an empty
9005 pack past the last parameter, we can accept the check as valid.
9007 FIXME: This may be valid for alias templates (but I doubt it).
9009 FIXME: The error could be better also. */
9010 if (in_decl
&& concept_definition_p (in_decl
))
9012 if (complain
& tf_error
)
9013 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9014 "too many arguments");
9015 return error_mark_node
;
9018 int len
= nparms
+ (nargs
- arg_idx
);
9019 tree args
= make_tree_vec (len
);
9021 for (; i
< nparms
; ++i
)
9022 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9023 for (; i
< len
; ++i
, ++arg_idx
)
9024 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9025 arg_idx
- pack_adjust
);
9026 new_inner_args
= args
;
9031 gcc_assert (!(complain
& tf_error
) || seen_error ());
9032 return error_mark_node
;
9035 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9036 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9037 TREE_VEC_LENGTH (new_inner_args
));
9039 return new_inner_args
;
9042 /* Convert all template arguments to their appropriate types, and
9043 return a vector containing the innermost resulting template
9044 arguments. If any error occurs, return error_mark_node. Error and
9045 warning messages are not issued.
9047 Note that no function argument deduction is performed, and default
9048 arguments are used to fill in unspecified arguments. */
9050 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9052 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9055 /* Convert all template arguments to their appropriate type, and
9056 instantiate default arguments as needed. This returns a vector
9057 containing the innermost resulting template arguments, or
9058 error_mark_node if unsuccessful. */
9060 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9061 tsubst_flags_t complain
)
9063 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9066 /* Like coerce_template_parms. If PARMS represents all template
9067 parameters levels, this function returns a vector of vectors
9068 representing all the resulting argument levels. Note that in this
9069 case, only the innermost arguments are coerced because the
9070 outermost ones are supposed to have been coerced already.
9072 Otherwise, if PARMS represents only (the innermost) vector of
9073 parameters, this function returns a vector containing just the
9074 innermost resulting arguments. */
9077 coerce_innermost_template_parms (tree parms
,
9080 tsubst_flags_t complain
,
9081 bool require_all_args
,
9082 bool use_default_args
)
9084 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9085 int args_depth
= TMPL_ARGS_DEPTH (args
);
9088 if (parms_depth
> 1)
9090 coerced_args
= make_tree_vec (parms_depth
);
9094 for (level
= parms
, cur_depth
= parms_depth
;
9095 parms_depth
> 0 && level
!= NULL_TREE
;
9096 level
= TREE_CHAIN (level
), --cur_depth
)
9099 if (cur_depth
== args_depth
)
9100 l
= coerce_template_parms (TREE_VALUE (level
),
9101 args
, in_decl
, complain
,
9105 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9107 if (l
== error_mark_node
)
9108 return error_mark_node
;
9110 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9114 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9115 args
, in_decl
, complain
,
9118 return coerced_args
;
9121 /* Returns true if T is a wrapper to make a C++20 template parameter
9125 class_nttp_const_wrapper_p (tree t
)
9127 if (cxx_dialect
< cxx20
)
9129 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9130 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9131 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9134 /* Returns 1 if template args OT and NT are equivalent. */
9137 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9141 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9143 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9146 if (class_nttp_const_wrapper_p (nt
))
9147 nt
= TREE_OPERAND (nt
, 0);
9148 if (class_nttp_const_wrapper_p (ot
))
9149 ot
= TREE_OPERAND (ot
, 0);
9151 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9152 /* For member templates */
9153 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9154 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9155 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9156 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9157 PACK_EXPANSION_PATTERN (nt
))
9158 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9159 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9160 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9161 return cp_tree_equal (ot
, nt
);
9162 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9164 else if (TYPE_P (nt
) || TYPE_P (ot
))
9166 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9168 /* Don't treat an alias template specialization with dependent
9169 arguments as equivalent to its underlying type when used as a
9170 template argument; we need them to be distinct so that we
9171 substitute into the specialization arguments at instantiation
9172 time. And aliases can't be equivalent without being ==, so
9173 we don't need to look any deeper.
9175 During partial ordering, however, we need to treat them normally so
9176 that we can order uses of the same alias with different
9177 cv-qualification (79960). */
9179 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9182 return same_type_p (ot
, nt
);
9186 /* Try to treat a template non-type argument that has been converted
9187 to the parameter type as equivalent to one that hasn't yet. */
9188 for (enum tree_code code1
= TREE_CODE (ot
);
9189 CONVERT_EXPR_CODE_P (code1
)
9190 || code1
== NON_LVALUE_EXPR
;
9191 code1
= TREE_CODE (ot
))
9192 ot
= TREE_OPERAND (ot
, 0);
9194 for (enum tree_code code2
= TREE_CODE (nt
);
9195 CONVERT_EXPR_CODE_P (code2
)
9196 || code2
== NON_LVALUE_EXPR
;
9197 code2
= TREE_CODE (nt
))
9198 nt
= TREE_OPERAND (nt
, 0);
9200 return cp_tree_equal (ot
, nt
);
9204 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9205 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9206 NEWARG_PTR with the offending arguments if they are non-NULL. */
9209 comp_template_args (tree oldargs
, tree newargs
,
9210 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9215 if (oldargs
== newargs
)
9218 if (!oldargs
|| !newargs
)
9221 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9224 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9226 tree nt
= TREE_VEC_ELT (newargs
, i
);
9227 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9229 if (! template_args_equal (ot
, nt
, partial_order
))
9231 if (oldarg_ptr
!= NULL
)
9233 if (newarg_ptr
!= NULL
)
9242 comp_template_args_porder (tree oargs
, tree nargs
)
9244 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9247 /* Implement a freelist interface for objects of type T.
9249 Head is a separate object, rather than a regular member, so that we
9250 can define it as a GTY deletable pointer, which is highly
9251 desirable. A data member could be declared that way, but then the
9252 containing object would implicitly get GTY((user)), which would
9253 prevent us from instantiating freelists as global objects.
9254 Although this way we can create freelist global objects, they're
9255 such thin wrappers that instantiating temporaries at every use
9256 loses nothing and saves permanent storage for the freelist object.
9258 Member functions next, anew, poison and reinit have default
9259 implementations that work for most of the types we're interested
9260 in, but if they don't work for some type, they should be explicitly
9261 specialized. See the comments before them for requirements, and
9262 the example specializations for the tree_list_freelist. */
9263 template <typename T
>
9266 /* Return the next object in a chain. We could just do type
9267 punning, but if we access the object with its underlying type, we
9268 avoid strict-aliasing trouble. This needs only work between
9269 poison and reinit. */
9270 static T
*&next (T
*obj
) { return obj
->next
; }
9272 /* Return a newly allocated, uninitialized or minimally-initialized
9273 object of type T. Any initialization performed by anew should
9274 either remain across the life of the object and the execution of
9275 poison, or be redone by reinit. */
9276 static T
*anew () { return ggc_alloc
<T
> (); }
9278 /* Optionally scribble all over the bits holding the object, so that
9279 they become (mostly?) uninitialized memory. This is called while
9280 preparing to make the object part of the free list. */
9281 static void poison (T
*obj
) {
9282 T
*p ATTRIBUTE_UNUSED
= obj
;
9283 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9285 #ifdef ENABLE_GC_CHECKING
9286 /* Poison the data, to indicate the data is garbage. */
9287 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9288 memset (p
, 0xa5, sizeof (*p
));
9290 /* Let valgrind know the object is free. */
9291 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9293 /* Let valgrind know the next portion of the object is available,
9294 but uninitialized. */
9295 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9298 /* Bring an object that underwent at least one lifecycle after anew
9299 and before the most recent free and poison, back to a usable
9300 state, reinitializing whatever is needed for it to be
9301 functionally equivalent to an object just allocated and returned
9302 by anew. This may poison or clear the next field, used by
9303 freelist housekeeping after poison was called. */
9304 static void reinit (T
*obj
) {
9305 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9307 #ifdef ENABLE_GC_CHECKING
9308 memset (q
, 0xa5, sizeof (*q
));
9310 /* Let valgrind know the entire object is available, but
9312 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9315 /* Reference a GTY-deletable pointer that points to the first object
9316 in the free list proper. */
9319 /* Construct a freelist object chaining objects off of HEAD. */
9320 freelist (T
*&head
) : head(head
) {}
9322 /* Add OBJ to the free object list. The former head becomes OBJ's
9331 /* Take an object from the free list, if one is available, or
9332 allocate a new one. Objects taken from the free list should be
9333 regarded as filled with garbage, except for bits that are
9334 configured to be preserved across free and alloc. */
9349 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9350 want to allocate a TREE_LIST using the usual interface, and ensure
9351 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9352 build_tree_list logic in reinit, so this could go out of sync. */
9355 freelist
<tree_node
>::next (tree obj
)
9357 return TREE_CHAIN (obj
);
9361 freelist
<tree_node
>::anew ()
9363 return build_tree_list (NULL
, NULL
);
9367 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9369 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9370 tree p ATTRIBUTE_UNUSED
= obj
;
9371 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9372 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9374 #ifdef ENABLE_GC_CHECKING
9375 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9377 /* Poison the data, to indicate the data is garbage. */
9378 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9379 memset (p
, 0xa5, size
);
9381 /* Let valgrind know the object is free. */
9382 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9383 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9384 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9385 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9387 #ifdef ENABLE_GC_CHECKING
9388 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9389 /* Keep TREE_CHAIN functional. */
9390 TREE_SET_CODE (obj
, TREE_LIST
);
9392 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9397 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9399 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9401 #ifdef ENABLE_GC_CHECKING
9402 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9403 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9404 memset (obj
, 0, sizeof (tree_list
));
9407 /* Let valgrind know the entire object is available, but
9409 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9411 #ifdef ENABLE_GC_CHECKING
9412 TREE_SET_CODE (obj
, TREE_LIST
);
9414 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9418 /* Point to the first object in the TREE_LIST freelist. */
9419 static GTY((deletable
)) tree tree_list_freelist_head
;
9420 /* Return the/an actual TREE_LIST freelist. */
9421 static inline freelist
<tree_node
>
9422 tree_list_freelist ()
9424 return tree_list_freelist_head
;
9427 /* Point to the first object in the tinst_level freelist. */
9428 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9429 /* Return the/an actual tinst_level freelist. */
9430 static inline freelist
<tinst_level
>
9431 tinst_level_freelist ()
9433 return tinst_level_freelist_head
;
9436 /* Point to the first object in the pending_template freelist. */
9437 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9438 /* Return the/an actual pending_template freelist. */
9439 static inline freelist
<pending_template
>
9440 pending_template_freelist ()
9442 return pending_template_freelist_head
;
9445 /* Build the TREE_LIST object out of a split list, store it
9446 permanently, and return it. */
9448 tinst_level::to_list ()
9450 gcc_assert (split_list_p ());
9451 tree ret
= tree_list_freelist ().alloc ();
9452 TREE_PURPOSE (ret
) = tldcl
;
9453 TREE_VALUE (ret
) = targs
;
9456 gcc_assert (tree_list_p ());
9460 const unsigned short tinst_level::refcount_infinity
;
9462 /* Increment OBJ's refcount unless it is already infinite. */
9463 static tinst_level
*
9464 inc_refcount_use (tinst_level
*obj
)
9466 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9471 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9473 tinst_level::free (tinst_level
*obj
)
9475 if (obj
->tree_list_p ())
9476 tree_list_freelist ().free (obj
->get_node ());
9477 tinst_level_freelist ().free (obj
);
9480 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9481 OBJ's DECL and OBJ, and start over with the tinst_level object that
9482 used to be referenced by OBJ's NEXT. */
9484 dec_refcount_use (tinst_level
*obj
)
9487 && obj
->refcount
!= tinst_level::refcount_infinity
9488 && !--obj
->refcount
)
9490 tinst_level
*next
= obj
->next
;
9491 tinst_level::free (obj
);
9496 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9497 and of the former PTR. Omitting the second argument is equivalent
9498 to passing (T*)NULL; this is allowed because passing the
9499 zero-valued integral constant NULL confuses type deduction and/or
9500 overload resolution. */
9501 template <typename T
>
9503 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9506 ptr
= inc_refcount_use (obj
);
9507 dec_refcount_use (save
);
9511 add_pending_template (tree d
)
9513 tree ti
= (TYPE_P (d
)
9514 ? CLASSTYPE_TEMPLATE_INFO (d
)
9515 : DECL_TEMPLATE_INFO (d
));
9516 struct pending_template
*pt
;
9519 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9522 /* We are called both from instantiate_decl, where we've already had a
9523 tinst_level pushed, and instantiate_template, where we haven't.
9525 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9526 level
= !current_tinst_level
9527 || current_tinst_level
->maybe_get_node () != d
;
9530 push_tinst_level (d
);
9532 pt
= pending_template_freelist ().alloc ();
9535 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9536 if (last_pending_template
)
9537 last_pending_template
->next
= pt
;
9539 pending_templates
= pt
;
9541 last_pending_template
= pt
;
9543 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9550 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9551 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9552 documentation for TEMPLATE_ID_EXPR. */
9555 lookup_template_function (tree fns
, tree arglist
)
9557 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9558 return error_mark_node
;
9560 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9562 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9564 error ("%q#D is not a function template", fns
);
9565 return error_mark_node
;
9568 if (BASELINK_P (fns
))
9570 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9572 BASELINK_FUNCTIONS (fns
),
9577 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9580 /* Within the scope of a template class S<T>, the name S gets bound
9581 (in build_self_reference) to a TYPE_DECL for the class, not a
9582 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9583 or one of its enclosing classes, and that type is a template,
9584 return the associated TEMPLATE_DECL. Otherwise, the original
9587 Also handle the case when DECL is a TREE_LIST of ambiguous
9588 injected-class-names from different bases. */
9591 maybe_get_template_decl_from_type_decl (tree decl
)
9593 if (decl
== NULL_TREE
)
9596 /* DR 176: A lookup that finds an injected-class-name (10.2
9597 [class.member.lookup]) can result in an ambiguity in certain cases
9598 (for example, if it is found in more than one base class). If all of
9599 the injected-class-names that are found refer to specializations of
9600 the same class template, and if the name is followed by a
9601 template-argument-list, the reference refers to the class template
9602 itself and not a specialization thereof, and is not ambiguous. */
9603 if (TREE_CODE (decl
) == TREE_LIST
)
9605 tree t
, tmpl
= NULL_TREE
;
9606 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9608 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9611 else if (tmpl
!= elt
)
9614 if (tmpl
&& t
== NULL_TREE
)
9620 return (decl
!= NULL_TREE
9621 && DECL_SELF_REFERENCE_P (decl
)
9622 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9623 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9626 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9627 parameters, find the desired type.
9629 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9631 IN_DECL, if non-NULL, is the template declaration we are trying to
9634 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9635 the class we are looking up.
9637 Issue error and warning messages under control of COMPLAIN.
9639 If the template class is really a local class in a template
9640 function, then the FUNCTION_CONTEXT is the function in which it is
9643 ??? Note that this function is currently called *twice* for each
9644 template-id: the first time from the parser, while creating the
9645 incomplete type (finish_template_type), and the second type during the
9646 real instantiation (instantiate_template_class). This is surely something
9647 that we want to avoid. It also causes some problems with argument
9648 coercion (see convert_nontype_argument for more information on this). */
9651 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9652 int entering_scope
, tsubst_flags_t complain
)
9654 tree templ
= NULL_TREE
, parmlist
;
9661 if (identifier_p (d1
))
9663 tree value
= innermost_non_namespace_value (d1
);
9664 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9669 push_decl_namespace (context
);
9670 templ
= lookup_name (d1
);
9671 templ
= maybe_get_template_decl_from_type_decl (templ
);
9673 pop_decl_namespace ();
9676 context
= DECL_CONTEXT (templ
);
9678 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9680 tree type
= TREE_TYPE (d1
);
9682 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9683 an implicit typename for the second A. Deal with it. */
9684 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9685 type
= TREE_TYPE (type
);
9687 if (CLASSTYPE_TEMPLATE_INFO (type
))
9689 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9690 d1
= DECL_NAME (templ
);
9693 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9694 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9696 templ
= TYPE_TI_TEMPLATE (d1
);
9697 d1
= DECL_NAME (templ
);
9699 else if (DECL_TYPE_TEMPLATE_P (d1
))
9702 d1
= DECL_NAME (templ
);
9703 context
= DECL_CONTEXT (templ
);
9705 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9708 d1
= DECL_NAME (templ
);
9711 /* Issue an error message if we didn't find a template. */
9714 if (complain
& tf_error
)
9715 error ("%qT is not a template", d1
);
9716 return error_mark_node
;
9719 if (TREE_CODE (templ
) != TEMPLATE_DECL
9720 /* Make sure it's a user visible template, if it was named by
9722 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9723 && !PRIMARY_TEMPLATE_P (templ
)))
9725 if (complain
& tf_error
)
9727 error ("non-template type %qT used as a template", d1
);
9729 error ("for template declaration %q+D", in_decl
);
9731 return error_mark_node
;
9734 complain
&= ~tf_user
;
9736 /* An alias that just changes the name of a template is equivalent to the
9737 other template, so if any of the arguments are pack expansions, strip
9738 the alias to avoid problems with a pack expansion passed to a non-pack
9739 alias template parameter (DR 1430). */
9740 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9741 templ
= get_underlying_template (templ
);
9743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9746 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9747 if (arglist2
== error_mark_node
9748 || (!uses_template_parms (arglist2
)
9749 && check_instantiated_args (templ
, arglist2
, complain
)))
9750 return error_mark_node
;
9752 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9757 tree template_type
= TREE_TYPE (templ
);
9760 tree found
= NULL_TREE
;
9763 int is_dependent_type
;
9764 int use_partial_inst_tmpl
= false;
9766 if (template_type
== error_mark_node
)
9767 /* An error occurred while building the template TEMPL, and a
9768 diagnostic has most certainly been emitted for that
9769 already. Let's propagate that error. */
9770 return error_mark_node
;
9772 gen_tmpl
= most_general_template (templ
);
9773 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9774 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9775 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9777 if (arg_depth
== 1 && parm_depth
> 1)
9779 /* We've been given an incomplete set of template arguments.
9782 template <class T> struct S1 {
9783 template <class U> struct S2 {};
9784 template <class U> struct S2<U*> {};
9787 we will be called with an ARGLIST of `U*', but the
9788 TEMPLATE will be `template <class T> template
9789 <class U> struct S1<T>::S2'. We must fill in the missing
9791 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9792 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9793 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9796 /* Now we should have enough arguments. */
9797 gcc_assert (parm_depth
== arg_depth
);
9799 /* From here on, we're only interested in the most general
9802 /* Calculate the BOUND_ARGS. These will be the args that are
9803 actually tsubst'd into the definition to create the
9805 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9807 /*require_all_args=*/true,
9808 /*use_default_args=*/true);
9810 if (arglist
== error_mark_node
)
9811 /* We were unable to bind the arguments. */
9812 return error_mark_node
;
9814 /* In the scope of a template class, explicit references to the
9815 template class refer to the type of the template, not any
9816 instantiation of it. For example, in:
9818 template <class T> class C { void f(C<T>); }
9820 the `C<T>' is just the same as `C'. Outside of the
9821 class, however, such a reference is an instantiation. */
9823 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9824 || currently_open_class (template_type
))
9826 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9828 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9829 return template_type
;
9832 /* If we already have this specialization, return it. */
9833 elt
.tmpl
= gen_tmpl
;
9835 elt
.spec
= NULL_TREE
;
9836 hash
= spec_hasher::hash (&elt
);
9837 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9842 /* If the template's constraints are not satisfied,
9843 then we cannot form a valid type.
9845 Note that the check is deferred until after the hash
9846 lookup. This prevents redundant checks on previously
9847 instantiated specializations. */
9849 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9850 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9852 if (complain
& tf_error
)
9854 auto_diagnostic_group d
;
9855 error ("template constraint failure for %qD", gen_tmpl
);
9856 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9858 return error_mark_node
;
9861 is_dependent_type
= uses_template_parms (arglist
);
9863 /* If the deduced arguments are invalid, then the binding
9865 if (!is_dependent_type
9866 && check_instantiated_args (gen_tmpl
,
9867 INNERMOST_TEMPLATE_ARGS (arglist
),
9869 return error_mark_node
;
9871 if (!is_dependent_type
9872 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9873 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9874 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9875 /* This occurs when the user has tried to define a tagged type
9876 in a scope that forbids it. We emitted an error during the
9877 parse. We didn't complete the bail out then, so here we
9879 return error_mark_node
;
9881 context
= DECL_CONTEXT (gen_tmpl
);
9882 if (context
&& TYPE_P (context
))
9884 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9885 context
= complete_type (context
);
9888 context
= tsubst (context
, arglist
, complain
, in_decl
);
9890 if (context
== error_mark_node
)
9891 return error_mark_node
;
9894 context
= global_namespace
;
9896 /* Create the type. */
9897 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9899 /* The user referred to a specialization of an alias
9900 template represented by GEN_TMPL.
9902 [temp.alias]/2 says:
9904 When a template-id refers to the specialization of an
9905 alias template, it is equivalent to the associated
9906 type obtained by substitution of its
9907 template-arguments for the template-parameters in the
9908 type-id of the alias template. */
9910 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9911 /* Note that the call above (by indirectly calling
9912 register_specialization in tsubst_decl) registers the
9913 TYPE_DECL representing the specialization of the alias
9914 template. So next time someone substitutes ARGLIST for
9915 the template parms into the alias template (GEN_TMPL),
9916 she'll get that TYPE_DECL back. */
9918 if (t
== error_mark_node
)
9921 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9923 if (!is_dependent_type
)
9925 set_current_access_from_decl (TYPE_NAME (template_type
));
9926 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9927 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9928 arglist
, complain
, in_decl
),
9929 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9930 arglist
, complain
, in_decl
),
9931 SCOPED_ENUM_P (template_type
), NULL
);
9933 if (t
== error_mark_node
)
9938 /* We don't want to call start_enum for this type, since
9939 the values for the enumeration constants may involve
9940 template parameters. And, no one should be interested
9941 in the enumeration constants for such a type. */
9942 t
= cxx_make_type (ENUMERAL_TYPE
);
9943 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9945 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9946 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9947 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9949 else if (CLASS_TYPE_P (template_type
))
9951 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9952 instantiated here. */
9953 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9955 t
= make_class_type (TREE_CODE (template_type
));
9956 CLASSTYPE_DECLARED_CLASS (t
)
9957 = CLASSTYPE_DECLARED_CLASS (template_type
);
9958 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9960 /* A local class. Make sure the decl gets registered properly. */
9961 if (context
== current_function_decl
)
9962 if (pushtag (DECL_NAME (gen_tmpl
), t
)
9964 return error_mark_node
;
9966 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9967 /* This instantiation is another name for the primary
9968 template type. Set the TYPE_CANONICAL field
9970 TYPE_CANONICAL (t
) = template_type
;
9971 else if (any_template_arguments_need_structural_equality_p (arglist
))
9972 /* Some of the template arguments require structural
9973 equality testing, so this template class requires
9974 structural equality testing. */
9975 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9980 /* If we called start_enum or pushtag above, this information
9981 will already be set up. */
9982 type_decl
= TYPE_NAME (t
);
9985 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9987 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9988 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9989 DECL_SOURCE_LOCATION (type_decl
)
9990 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9993 if (CLASS_TYPE_P (template_type
))
9995 TREE_PRIVATE (type_decl
)
9996 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9997 TREE_PROTECTED (type_decl
)
9998 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9999 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10001 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10002 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10006 if (OVERLOAD_TYPE_P (t
)
10007 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10009 static const char *tags
[] = {"abi_tag", "may_alias"};
10011 for (unsigned ix
= 0; ix
!= 2; ix
++)
10014 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10017 TYPE_ATTRIBUTES (t
)
10018 = tree_cons (TREE_PURPOSE (attributes
),
10019 TREE_VALUE (attributes
),
10020 TYPE_ATTRIBUTES (t
));
10024 /* Let's consider the explicit specialization of a member
10025 of a class template specialization that is implicitly instantiated,
10030 template<class U> struct M {}; //#0
10035 struct S<int>::M<char> //#1
10039 [temp.expl.spec]/4 says this is valid.
10041 In this case, when we write:
10044 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10047 When we encounter #1, we want to store the partial instantiation
10048 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10050 For all cases other than this "explicit specialization of member of a
10051 class template", we just want to store the most general template into
10052 the CLASSTYPE_TI_TEMPLATE of M.
10054 This case of "explicit specialization of member of a class template"
10056 1/ the enclosing class is an instantiation of, and therefore not
10057 the same as, the context of the most general template, and
10058 2/ we aren't looking at the partial instantiation itself, i.e.
10059 the innermost arguments are not the same as the innermost parms of
10060 the most general template.
10062 So it's only when 1/ and 2/ happens that we want to use the partial
10063 instantiation of the member template in lieu of its most general
10066 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10067 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10068 /* the enclosing class must be an instantiation... */
10069 && CLASS_TYPE_P (context
)
10070 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10072 TREE_VEC_LENGTH (arglist
)--;
10073 ++processing_template_decl
;
10074 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10075 tree partial_inst_args
=
10076 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10077 arglist
, complain
, NULL_TREE
);
10078 --processing_template_decl
;
10079 TREE_VEC_LENGTH (arglist
)++;
10080 if (partial_inst_args
== error_mark_node
)
10081 return error_mark_node
;
10082 use_partial_inst_tmpl
=
10083 /*...and we must not be looking at the partial instantiation
10085 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10086 partial_inst_args
);
10089 if (!use_partial_inst_tmpl
)
10090 /* This case is easy; there are no member templates involved. */
10094 /* This is a full instantiation of a member template. Find
10095 the partial instantiation of which this is an instance. */
10097 /* Temporarily reduce by one the number of levels in the ARGLIST
10098 so as to avoid comparing the last set of arguments. */
10099 TREE_VEC_LENGTH (arglist
)--;
10100 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10101 TREE_VEC_LENGTH (arglist
)++;
10102 /* FOUND is either a proper class type, or an alias
10103 template specialization. In the later case, it's a
10104 TYPE_DECL, resulting from the substituting of arguments
10105 for parameters in the TYPE_DECL of the alias template
10106 done earlier. So be careful while getting the template
10108 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10110 : (TREE_CODE (found
) == TYPE_DECL
10111 ? DECL_TI_TEMPLATE (found
)
10112 : CLASSTYPE_TI_TEMPLATE (found
)));
10114 if (DECL_CLASS_TEMPLATE_P (found
)
10115 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10117 /* If this partial instantiation is specialized, we want to
10118 use it for hash table lookup. */
10120 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10121 hash
= spec_hasher::hash (&elt
);
10125 /* Build template info for the new specialization. */
10126 if (TYPE_ALIAS_P (t
))
10128 /* This is constructed during instantiation of the alias
10129 decl. But for member templates of template classes, that
10130 is not correct as we need to refer to the partially
10131 instantiated template, not the most general template.
10132 The incorrect knowledge will not have escaped this
10133 instantiation process, so we're good just updating the
10134 template_info we made then. */
10135 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10136 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
));
10137 if (TI_TEMPLATE (ti
) != found
)
10139 gcc_checking_assert (DECL_TI_TEMPLATE (found
) == TI_TEMPLATE (ti
));
10140 TI_TEMPLATE (ti
) = found
;
10144 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10147 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10148 gcc_checking_assert (*slot
== NULL
);
10149 entry
= ggc_alloc
<spec_entry
> ();
10153 /* Note this use of the partial instantiation so we can check it
10154 later in maybe_process_partial_specialization. */
10155 DECL_TEMPLATE_INSTANTIATIONS (found
)
10156 = tree_cons (arglist
, t
,
10157 DECL_TEMPLATE_INSTANTIATIONS (found
));
10159 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10160 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10161 /* Now that the type has been registered on the instantiations
10162 list, we set up the enumerators. Because the enumeration
10163 constants may involve the enumeration type itself, we make
10164 sure to register the type first, and then create the
10165 constants. That way, doing tsubst_expr for the enumeration
10166 constants won't result in recursive calls here; we'll find
10167 the instantiation and exit above. */
10168 tsubst_enum (template_type
, t
, arglist
);
10170 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10171 /* If the type makes use of template parameters, the
10172 code that generates debugging information will crash. */
10173 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10175 /* Possibly limit visibility based on template args. */
10176 TREE_PUBLIC (type_decl
) = 1;
10177 determine_visibility (type_decl
);
10179 inherit_targ_abi_tags (t
);
10185 /* Wrapper for lookup_template_class_1. */
10188 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10189 int entering_scope
, tsubst_flags_t complain
)
10192 timevar_push (TV_TEMPLATE_INST
);
10193 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10194 entering_scope
, complain
);
10195 timevar_pop (TV_TEMPLATE_INST
);
10199 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10202 lookup_template_variable (tree templ
, tree arglist
)
10204 if (flag_concepts
&& variable_concept_p (templ
))
10205 return build_concept_check (templ
, arglist
, tf_none
);
10207 /* The type of the expression is NULL_TREE since the template-id could refer
10208 to an explicit or partial specialization. */
10209 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10212 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10215 finish_template_variable (tree var
, tsubst_flags_t complain
)
10217 tree templ
= TREE_OPERAND (var
, 0);
10218 tree arglist
= TREE_OPERAND (var
, 1);
10220 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10221 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10223 templ
= most_general_template (templ
);
10224 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10225 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10227 /*use_default*/true);
10228 if (arglist
== error_mark_node
)
10229 return error_mark_node
;
10231 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10233 if (complain
& tf_error
)
10235 auto_diagnostic_group d
;
10236 error ("use of invalid variable template %qE", var
);
10237 diagnose_constraints (location_of (var
), templ
, arglist
);
10239 return error_mark_node
;
10242 return instantiate_template (templ
, arglist
, complain
);
10245 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10246 TARGS template args, and instantiate it if it's not dependent. */
10249 lookup_and_finish_template_variable (tree templ
, tree targs
,
10250 tsubst_flags_t complain
)
10252 templ
= lookup_template_variable (templ
, targs
);
10253 if (!any_dependent_template_arguments_p (targs
))
10255 templ
= finish_template_variable (templ
, complain
);
10259 return convert_from_reference (templ
);
10262 /* If the set of template parameters PARMS contains a template parameter
10263 at the given LEVEL and INDEX, then return this parameter. Otherwise
10264 return NULL_TREE. */
10267 corresponding_template_parameter (tree parms
, int level
, int index
)
10269 while (TMPL_PARMS_DEPTH (parms
) > level
)
10270 parms
= TREE_CHAIN (parms
);
10272 if (TMPL_PARMS_DEPTH (parms
) != level
10273 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10276 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10277 /* As in template_parm_to_arg. */
10278 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10281 t
= DECL_INITIAL (t
);
10283 gcc_assert (TEMPLATE_PARM_P (t
));
10287 /* Return the template parameter from PARMS that positionally corresponds
10288 to the template parameter PARM, or else return NULL_TREE. */
10291 corresponding_template_parameter (tree parms
, tree parm
)
10294 template_parm_level_and_index (parm
, &level
, &index
);
10295 return corresponding_template_parameter (parms
, level
, index
);
10299 struct pair_fn_data
10304 /* True when we should also visit template parameters that occur in
10305 non-deduced contexts. */
10306 bool include_nondeduced_p
;
10307 hash_set
<tree
> *visited
;
10310 /* Called from for_each_template_parm via walk_tree. */
10313 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10316 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10317 tree_fn_t fn
= pfd
->fn
;
10318 void *data
= pfd
->data
;
10319 tree result
= NULL_TREE
;
10321 #define WALK_SUBTREE(NODE) \
10324 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10325 pfd->include_nondeduced_p, \
10327 if (result) goto out; \
10331 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10335 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10336 WALK_SUBTREE (TYPE_CONTEXT (t
));
10338 switch (TREE_CODE (t
))
10341 if (TYPE_PTRMEMFUNC_P (t
))
10343 /* Fall through. */
10346 case ENUMERAL_TYPE
:
10347 if (!TYPE_TEMPLATE_INFO (t
))
10348 *walk_subtrees
= 0;
10350 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10354 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10355 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10359 /* Since we're not going to walk subtrees, we have to do this
10360 explicitly here. */
10361 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10362 /* Fall through. */
10364 case FUNCTION_TYPE
:
10365 /* Check the return type. */
10366 WALK_SUBTREE (TREE_TYPE (t
));
10368 /* Check the parameter types. Since default arguments are not
10369 instantiated until they are needed, the TYPE_ARG_TYPES may
10370 contain expressions that involve template parameters. But,
10371 no-one should be looking at them yet. And, once they're
10372 instantiated, they don't contain template parameters, so
10373 there's no point in looking at them then, either. */
10377 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10378 WALK_SUBTREE (TREE_VALUE (parm
));
10380 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10381 want walk_tree walking into them itself. */
10382 *walk_subtrees
= 0;
10385 if (flag_noexcept_type
)
10387 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10389 WALK_SUBTREE (TREE_PURPOSE (spec
));
10394 case DECLTYPE_TYPE
:
10395 case UNDERLYING_TYPE
:
10396 if (pfd
->include_nondeduced_p
10397 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10399 pfd
->include_nondeduced_p
,
10401 return error_mark_node
;
10402 *walk_subtrees
= false;
10405 case FUNCTION_DECL
:
10407 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10408 WALK_SUBTREE (DECL_TI_ARGS (t
));
10409 /* Fall through. */
10413 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10414 WALK_SUBTREE (DECL_INITIAL (t
));
10415 if (DECL_CONTEXT (t
)
10416 && pfd
->include_nondeduced_p
)
10417 WALK_SUBTREE (DECL_CONTEXT (t
));
10420 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10421 /* Record template parameters such as `T' inside `TT<T>'. */
10422 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10423 /* Fall through. */
10425 case TEMPLATE_TEMPLATE_PARM
:
10426 case TEMPLATE_TYPE_PARM
:
10427 case TEMPLATE_PARM_INDEX
:
10428 if (fn
&& (*fn
)(t
, data
))
10434 case TEMPLATE_DECL
:
10435 /* A template template parameter is encountered. */
10436 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10437 WALK_SUBTREE (TREE_TYPE (t
));
10439 /* Already substituted template template parameter */
10440 *walk_subtrees
= 0;
10443 case TYPENAME_TYPE
:
10444 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10445 partial instantiation. */
10446 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10447 *walk_subtrees
= 0;
10451 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10452 && pfd
->include_nondeduced_p
)
10453 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10457 case COMPONENT_REF
:
10458 /* If there's no type, then this thing must be some expression
10459 involving template parameters. */
10460 if (!fn
&& !TREE_TYPE (t
))
10461 return error_mark_node
;
10466 case IMPLICIT_CONV_EXPR
:
10467 case REINTERPRET_CAST_EXPR
:
10468 case CONST_CAST_EXPR
:
10469 case STATIC_CAST_EXPR
:
10470 case DYNAMIC_CAST_EXPR
:
10474 case PSEUDO_DTOR_EXPR
:
10476 return error_mark_node
;
10480 if (pfd
->include_nondeduced_p
)
10481 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10484 case REQUIRES_EXPR
:
10487 return error_mark_node
;
10489 /* Recursively walk the type of each constraint variable. */
10490 tree p
= TREE_OPERAND (t
, 0);
10493 WALK_SUBTREE (TREE_TYPE (p
));
10494 p
= TREE_CHAIN (p
);
10503 #undef WALK_SUBTREE
10505 /* We didn't find any template parameters we liked. */
10510 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10511 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10512 call FN with the parameter and the DATA.
10513 If FN returns nonzero, the iteration is terminated, and
10514 for_each_template_parm returns 1. Otherwise, the iteration
10515 continues. If FN never returns a nonzero value, the value
10516 returned by for_each_template_parm is 0. If FN is NULL, it is
10517 considered to be the function which always returns 1.
10519 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10520 parameters that occur in non-deduced contexts. When false, only
10521 visits those template parameters that can be deduced. */
10524 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10525 hash_set
<tree
> *visited
,
10526 bool include_nondeduced_p
,
10529 struct pair_fn_data pfd
;
10534 pfd
.any_fn
= any_fn
;
10536 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10538 /* Walk the tree. (Conceptually, we would like to walk without
10539 duplicates, but for_each_template_parm_r recursively calls
10540 for_each_template_parm, so we would need to reorganize a fair
10541 bit to use walk_tree_without_duplicates, so we keep our own
10544 pfd
.visited
= visited
;
10546 pfd
.visited
= new hash_set
<tree
>;
10547 result
= cp_walk_tree (&t
,
10548 for_each_template_parm_r
,
10555 delete pfd
.visited
;
10562 struct find_template_parameter_info
10564 explicit find_template_parameter_info (tree ctx_parms
)
10565 : parm_list (NULL_TREE
),
10566 ctx_parms (ctx_parms
),
10567 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10570 hash_set
<tree
> visited
;
10571 hash_set
<tree
> parms
;
10577 /* Appends the declaration of T to the list in DATA. */
10580 keep_template_parm (tree t
, void* data
)
10582 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10584 /* Template parameters declared within the expression are not part of
10585 the parameter mapping. For example, in this concept:
10587 template<typename T>
10588 concept C = requires { <expr> } -> same_as<int>;
10590 the return specifier same_as<int> declares a new decltype parameter
10591 that must not be part of the parameter mapping. The same is true
10592 for generic lambda parameters, lambda template parameters, etc. */
10595 template_parm_level_and_index (t
, &level
, &index
);
10596 if (level
> ftpi
->max_depth
)
10599 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10600 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10601 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10602 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10604 /* This template parameter might be an argument to a cached dependent
10605 specalization that was formed earlier inside some other template, in
10606 which case the parameter is not among the ones that are in-scope.
10607 Look in CTX_PARMS to find the corresponding in-scope template
10608 parameter, and use it instead. */
10609 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10612 /* Arguments like const T yield parameters like const T. This means that
10613 a template-id like X<T, const T> would yield two distinct parameters:
10614 T and const T. Adjust types to their unqualified versions. */
10616 t
= TYPE_MAIN_VARIANT (t
);
10617 if (!ftpi
->parms
.add (t
))
10618 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10620 /* Verify the parameter we found has a valid index. */
10623 tree parms
= ftpi
->ctx_parms
;
10624 while (TMPL_PARMS_DEPTH (parms
) > level
)
10625 parms
= TREE_CHAIN (parms
);
10626 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10627 gcc_assert (index
< len
);
10633 /* Ensure that we recursively examine certain terms that are not normally
10634 visited in for_each_template_parm_r. */
10637 any_template_parm_r (tree t
, void *data
)
10639 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10641 #define WALK_SUBTREE(NODE) \
10644 for_each_template_parm (NODE, keep_template_parm, data, \
10645 &ftpi->visited, true, \
10646 any_template_parm_r); \
10650 /* A mention of a member alias/typedef is a use of all of its template
10651 arguments, including those from the enclosing class, so we don't use
10652 alias_template_specialization_p here. */
10653 if (TYPE_P (t
) && typedef_variant_p (t
))
10654 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10655 WALK_SUBTREE (TI_ARGS (tinfo
));
10657 switch (TREE_CODE (t
))
10659 case TEMPLATE_TYPE_PARM
:
10660 /* Type constraints of a placeholder type may contain parameters. */
10662 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10663 WALK_SUBTREE (constr
);
10666 case TEMPLATE_ID_EXPR
:
10667 /* Search through references to variable templates. */
10668 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10669 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10672 case TEMPLATE_PARM_INDEX
:
10674 /* A parameter or constraint variable may also depend on a template
10675 parameter without explicitly naming it. */
10676 WALK_SUBTREE (TREE_TYPE (t
));
10679 case TEMPLATE_DECL
:
10681 /* If T is a member template that shares template parameters with
10682 ctx_parms, we need to mark all those parameters for mapping. */
10683 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10684 tree cparms
= ftpi
->ctx_parms
;
10685 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10686 dparms
= TREE_CHAIN (dparms
);
10687 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10688 cparms
= TREE_CHAIN (cparms
);
10690 && (TREE_TYPE (TREE_VALUE (dparms
))
10691 != TREE_TYPE (TREE_VALUE (cparms
))))
10692 dparms
= TREE_CHAIN (dparms
),
10693 cparms
= TREE_CHAIN (cparms
);
10696 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10697 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10698 for (int i
= 0; i
< ddepth
; ++i
)
10699 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10706 /* Look in the parms and body. */
10707 tree fn
= lambda_function (t
);
10708 WALK_SUBTREE (TREE_TYPE (fn
));
10709 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10713 case IDENTIFIER_NODE
:
10714 if (IDENTIFIER_CONV_OP_P (t
))
10715 /* The conversion-type-id of a conversion operator may be dependent. */
10716 WALK_SUBTREE (TREE_TYPE (t
));
10723 /* Keep walking. */
10727 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10728 are the template parameters in scope. */
10731 find_template_parameters (tree t
, tree ctx_parms
)
10736 find_template_parameter_info
ftpi (ctx_parms
);
10737 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10738 /*include_nondeduced*/true, any_template_parm_r
);
10739 return ftpi
.parm_list
;
10742 /* Returns true if T depends on any template parameter. */
10745 uses_template_parms (tree t
)
10747 if (t
== NULL_TREE
)
10751 int saved_processing_template_decl
;
10753 saved_processing_template_decl
= processing_template_decl
;
10754 if (!saved_processing_template_decl
)
10755 processing_template_decl
= 1;
10757 dependent_p
= dependent_type_p (t
);
10758 else if (TREE_CODE (t
) == TREE_VEC
)
10759 dependent_p
= any_dependent_template_arguments_p (t
);
10760 else if (TREE_CODE (t
) == TREE_LIST
)
10761 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10762 || uses_template_parms (TREE_CHAIN (t
)));
10763 else if (TREE_CODE (t
) == TYPE_DECL
)
10764 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10765 else if (t
== error_mark_node
)
10766 dependent_p
= false;
10768 dependent_p
= instantiation_dependent_expression_p (t
);
10770 processing_template_decl
= saved_processing_template_decl
;
10772 return dependent_p
;
10775 /* Returns true iff current_function_decl is an incompletely instantiated
10776 template. Useful instead of processing_template_decl because the latter
10777 is set to 0 during instantiate_non_dependent_expr. */
10780 in_template_function (void)
10782 tree fn
= current_function_decl
;
10784 ++processing_template_decl
;
10785 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10786 && DECL_TEMPLATE_INFO (fn
)
10787 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10788 --processing_template_decl
;
10792 /* Returns true if T depends on any template parameter with level LEVEL. */
10795 uses_template_parms_level (tree t
, int level
)
10797 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10798 /*include_nondeduced_p=*/true);
10801 /* Returns true if the signature of DECL depends on any template parameter from
10802 its enclosing class. */
10805 uses_outer_template_parms (tree decl
)
10807 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10810 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10811 &depth
, NULL
, /*include_nondeduced_p=*/true))
10813 if (PRIMARY_TEMPLATE_P (decl
)
10814 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10815 (DECL_TEMPLATE_PARMS (decl
)),
10816 template_parm_outer_level
,
10817 &depth
, NULL
, /*include_nondeduced_p=*/true))
10819 tree ci
= get_constraints (decl
);
10821 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10822 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10823 &depth
, NULL
, /*nondeduced*/true))
10828 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10829 ill-formed translation unit, i.e. a variable or function that isn't
10830 usable in a constant expression. */
10833 neglectable_inst_p (tree d
)
10835 return (d
&& DECL_P (d
)
10836 && !undeduced_auto_decl (d
)
10837 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10838 : decl_maybe_constant_var_p (d
)));
10841 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10842 neglectable and instantiated from within an erroneous instantiation. */
10845 limit_bad_template_recursion (tree decl
)
10847 struct tinst_level
*lev
= current_tinst_level
;
10848 int errs
= errorcount
+ sorrycount
;
10849 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10852 for (; lev
; lev
= lev
->next
)
10853 if (neglectable_inst_p (lev
->maybe_get_node ()))
10856 return (lev
&& errs
> lev
->errors
);
10859 static int tinst_depth
;
10860 extern int max_tinst_depth
;
10863 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10865 /* We're starting to instantiate D; record the template instantiation context
10866 at LOC for diagnostics and to restore it later. */
10869 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10871 struct tinst_level
*new_level
;
10873 if (tinst_depth
>= max_tinst_depth
)
10875 /* Tell error.c not to try to instantiate any templates. */
10877 fatal_error (input_location
,
10878 "template instantiation depth exceeds maximum of %d"
10879 " (use %<-ftemplate-depth=%> to increase the maximum)",
10884 /* If the current instantiation caused problems, don't let it instantiate
10885 anything else. Do allow deduction substitution and decls usable in
10886 constant expressions. */
10887 if (!targs
&& limit_bad_template_recursion (tldcl
))
10889 /* Avoid no_linkage_errors and unused function warnings for this
10891 TREE_NO_WARNING (tldcl
) = 1;
10895 /* When not -quiet, dump template instantiations other than functions, since
10896 announce_function will take care of those. */
10897 if (!quiet_flag
&& !targs
10898 && TREE_CODE (tldcl
) != TREE_LIST
10899 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10900 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10902 new_level
= tinst_level_freelist ().alloc ();
10903 new_level
->tldcl
= tldcl
;
10904 new_level
->targs
= targs
;
10905 new_level
->locus
= loc
;
10906 new_level
->errors
= errorcount
+ sorrycount
;
10907 new_level
->next
= NULL
;
10908 new_level
->refcount
= 0;
10909 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10910 set_refcount_ptr (current_tinst_level
, new_level
);
10913 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10914 depth_reached
= tinst_depth
;
10919 /* We're starting substitution of TMPL<ARGS>; record the template
10920 substitution context for diagnostics and to restore it later. */
10923 push_tinst_level (tree tmpl
, tree args
)
10925 return push_tinst_level_loc (tmpl
, args
, input_location
);
10928 /* We're starting to instantiate D; record INPUT_LOCATION and the
10929 template instantiation context for diagnostics and to restore it
10933 push_tinst_level (tree d
)
10935 return push_tinst_level_loc (d
, input_location
);
10938 /* Likewise, but record LOC as the program location. */
10941 push_tinst_level_loc (tree d
, location_t loc
)
10943 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10944 return push_tinst_level_loc (d
, NULL
, loc
);
10947 /* We're done instantiating this template; return to the instantiation
10951 pop_tinst_level (void)
10953 /* Restore the filename and line number stashed away when we started
10954 this instantiation. */
10955 input_location
= current_tinst_level
->locus
;
10956 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10960 /* We're instantiating a deferred template; restore the template
10961 instantiation context in which the instantiation was requested, which
10962 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10965 reopen_tinst_level (struct tinst_level
*level
)
10967 struct tinst_level
*t
;
10970 for (t
= level
; t
; t
= t
->next
)
10973 set_refcount_ptr (current_tinst_level
, level
);
10974 pop_tinst_level ();
10975 if (current_tinst_level
)
10976 current_tinst_level
->errors
= errorcount
+sorrycount
;
10977 return level
->maybe_get_node ();
10980 /* Returns the TINST_LEVEL which gives the original instantiation
10983 struct tinst_level
*
10984 outermost_tinst_level (void)
10986 struct tinst_level
*level
= current_tinst_level
;
10988 while (level
->next
)
10989 level
= level
->next
;
10993 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10994 vector of template arguments, as for tsubst.
10996 Returns an appropriate tsubst'd friend declaration. */
10999 tsubst_friend_function (tree decl
, tree args
)
11003 if (TREE_CODE (decl
) == FUNCTION_DECL
11004 && DECL_TEMPLATE_INSTANTIATION (decl
)
11005 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11006 /* This was a friend declared with an explicit template
11007 argument list, e.g.:
11009 friend void f<>(T);
11011 to indicate that f was a template instantiation, not a new
11012 function declaration. Now, we have to figure out what
11013 instantiation of what template. */
11015 tree template_id
, arglist
, fns
;
11018 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11020 /* Friend functions are looked up in the containing namespace scope.
11021 We must enter that scope, to avoid finding member functions of the
11022 current class with same name. */
11023 push_nested_namespace (ns
);
11024 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11025 tf_warning_or_error
, NULL_TREE
,
11026 /*integral_constant_expression_p=*/false);
11027 pop_nested_namespace (ns
);
11028 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11029 tf_warning_or_error
, NULL_TREE
);
11030 template_id
= lookup_template_function (fns
, arglist
);
11032 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11033 tmpl
= determine_specialization (template_id
, new_friend
,
11035 /*need_member_template=*/0,
11036 TREE_VEC_LENGTH (args
),
11038 return instantiate_template (tmpl
, new_args
, tf_error
);
11041 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11042 if (new_friend
== error_mark_node
)
11043 return error_mark_node
;
11045 /* The NEW_FRIEND will look like an instantiation, to the
11046 compiler, but is not an instantiation from the point of view of
11047 the language. For example, we might have had:
11049 template <class T> struct S {
11050 template <class U> friend void f(T, U);
11053 Then, in S<int>, template <class U> void f(int, U) is not an
11054 instantiation of anything. */
11056 DECL_USE_TEMPLATE (new_friend
) = 0;
11057 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11059 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11060 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11061 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11063 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11064 match in decls_match. */
11065 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11066 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11067 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11068 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11071 /* The mangled name for the NEW_FRIEND is incorrect. The function
11072 is not a template instantiation and should not be mangled like
11073 one. Therefore, we forget the mangling here; we'll recompute it
11074 later if we need it. */
11075 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11077 SET_DECL_RTL (new_friend
, NULL
);
11078 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11081 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11086 /* We must save some information from NEW_FRIEND before calling
11087 duplicate decls since that function will free NEW_FRIEND if
11089 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11090 tree new_friend_result_template_info
= NULL_TREE
;
11091 bool new_friend_is_defn
=
11092 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11093 (template_for_substitution (new_friend
)))
11095 tree not_tmpl
= new_friend
;
11097 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11099 /* This declaration is a `primary' template. */
11100 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11102 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11103 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11106 /* Inside pushdecl_namespace_level, we will push into the
11107 current namespace. However, the friend function should go
11108 into the namespace of the template. */
11109 ns
= decl_namespace_context (new_friend
);
11110 push_nested_namespace (ns
);
11111 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11112 pop_nested_namespace (ns
);
11114 if (old_decl
== error_mark_node
)
11115 return error_mark_node
;
11117 if (old_decl
!= new_friend
)
11119 /* This new friend declaration matched an existing
11120 declaration. For example, given:
11122 template <class T> void f(T);
11123 template <class U> class C {
11124 template <class T> friend void f(T) {}
11127 the friend declaration actually provides the definition
11128 of `f', once C has been instantiated for some type. So,
11129 old_decl will be the out-of-class template declaration,
11130 while new_friend is the in-class definition.
11132 But, if `f' was called before this point, the
11133 instantiation of `f' will have DECL_TI_ARGS corresponding
11134 to `T' but not to `U', references to which might appear
11135 in the definition of `f'. Previously, the most general
11136 template for an instantiation of `f' was the out-of-class
11137 version; now it is the in-class version. Therefore, we
11138 run through all specialization of `f', adding to their
11139 DECL_TI_ARGS appropriately. In particular, they need a
11140 new set of outer arguments, corresponding to the
11141 arguments for this class instantiation.
11143 The same situation can arise with something like this:
11145 friend void f(int);
11146 template <class T> class C {
11147 friend void f(T) {}
11150 when `C<int>' is instantiated. Now, `f(int)' is defined
11153 if (!new_friend_is_defn
)
11154 /* On the other hand, if the in-class declaration does
11155 *not* provide a definition, then we don't want to alter
11156 existing definitions. We can just leave everything
11161 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11162 tree new_args
= TI_ARGS (new_friend_template_info
);
11164 /* Overwrite whatever template info was there before, if
11165 any, with the new template information pertaining to
11166 the declaration. */
11167 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11169 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11171 /* We should have called reregister_specialization in
11172 duplicate_decls. */
11173 gcc_assert (retrieve_specialization (new_template
,
11177 /* Instantiate it if the global has already been used. */
11178 if (DECL_ODR_USED (old_decl
))
11179 instantiate_decl (old_decl
, /*defer_ok=*/true,
11180 /*expl_inst_class_mem_p=*/false);
11186 /* Indicate that the old function template is a partial
11188 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11189 = new_friend_result_template_info
;
11191 gcc_assert (new_template
11192 == most_general_template (new_template
));
11193 gcc_assert (new_template
!= old_decl
);
11195 /* Reassign any specializations already in the hash table
11196 to the new more general template, and add the
11197 additional template args. */
11198 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11200 t
= TREE_CHAIN (t
))
11202 tree spec
= TREE_VALUE (t
);
11205 elt
.tmpl
= old_decl
;
11206 elt
.args
= DECL_TI_ARGS (spec
);
11207 elt
.spec
= NULL_TREE
;
11209 decl_specializations
->remove_elt (&elt
);
11211 DECL_TI_ARGS (spec
)
11212 = add_outermost_template_args (new_args
,
11213 DECL_TI_ARGS (spec
));
11215 register_specialization
11216 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11219 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11223 /* The information from NEW_FRIEND has been merged into OLD_DECL
11224 by duplicate_decls. */
11225 new_friend
= old_decl
;
11230 tree context
= DECL_CONTEXT (new_friend
);
11234 template <class T> class C {
11235 template <class U> friend void C1<U>::f (); // case 1
11236 friend void C2<T>::f (); // case 2
11238 we only need to make sure CONTEXT is a complete type for
11239 case 2. To distinguish between the two cases, we note that
11240 CONTEXT of case 1 remains dependent type after tsubst while
11241 this isn't true for case 2. */
11242 ++processing_template_decl
;
11243 dependent_p
= dependent_type_p (context
);
11244 --processing_template_decl
;
11247 && !complete_type_or_else (context
, NULL_TREE
))
11248 return error_mark_node
;
11250 if (COMPLETE_TYPE_P (context
))
11252 tree fn
= new_friend
;
11253 /* do_friend adds the TEMPLATE_DECL for any member friend
11254 template even if it isn't a member template, i.e.
11255 template <class T> friend A<T>::f();
11256 Look through it in that case. */
11257 if (TREE_CODE (fn
) == TEMPLATE_DECL
11258 && !PRIMARY_TEMPLATE_P (fn
))
11259 fn
= DECL_TEMPLATE_RESULT (fn
);
11260 /* Check to see that the declaration is really present, and,
11261 possibly obtain an improved declaration. */
11262 fn
= check_classfn (context
, fn
, NULL_TREE
);
11272 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11273 template arguments, as for tsubst.
11275 Returns an appropriate tsubst'd friend type or error_mark_node on
11279 tsubst_friend_class (tree friend_tmpl
, tree args
)
11283 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11285 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11286 return TREE_TYPE (tmpl
);
11289 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11290 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11291 push_nested_namespace (context
);
11294 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11295 push_nested_class (context
);
11298 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11299 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11301 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11303 /* The friend template has already been declared. Just
11304 check to see that the declarations match, and install any new
11305 default parameters. We must tsubst the default parameters,
11306 of course. We only need the innermost template parameters
11307 because that is all that redeclare_class_template will look
11309 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11310 > TMPL_ARGS_DEPTH (args
))
11312 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11313 args
, tf_warning_or_error
);
11314 location_t saved_input_location
= input_location
;
11315 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11316 tree cons
= get_constraints (tmpl
);
11317 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11318 input_location
= saved_input_location
;
11323 /* The friend template has not already been declared. In this
11324 case, the instantiation of the template class will cause the
11325 injection of this template into the namespace scope. */
11326 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11328 if (tmpl
!= error_mark_node
)
11330 /* The new TMPL is not an instantiation of anything, so we
11331 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11332 for the new type because that is supposed to be the
11333 corresponding template decl, i.e., TMPL. */
11334 DECL_USE_TEMPLATE (tmpl
) = 0;
11335 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11336 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11337 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11338 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11340 /* Substitute into and set the constraints on the new declaration. */
11341 if (tree ci
= get_constraints (friend_tmpl
))
11343 ++processing_template_decl
;
11344 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11345 DECL_FRIEND_CONTEXT (friend_tmpl
));
11346 --processing_template_decl
;
11347 set_constraints (tmpl
, ci
);
11350 /* Inject this template into the enclosing namspace scope. */
11351 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11355 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11356 pop_nested_namespace (context
);
11358 pop_nested_class ();
11360 return TREE_TYPE (tmpl
);
11363 /* Returns zero if TYPE cannot be completed later due to circularity.
11364 Otherwise returns one. */
11367 can_complete_type_without_circularity (tree type
)
11369 if (type
== NULL_TREE
|| type
== error_mark_node
)
11371 else if (COMPLETE_TYPE_P (type
))
11373 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11374 return can_complete_type_without_circularity (TREE_TYPE (type
));
11375 else if (CLASS_TYPE_P (type
)
11376 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11382 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11383 tsubst_flags_t
, tree
);
11385 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11386 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11389 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11390 tsubst_flags_t complain
, tree in_decl
)
11392 gcc_assert (ATTR_IS_DEPENDENT (t
));
11394 tree val
= TREE_VALUE (t
);
11395 if (val
== NULL_TREE
)
11396 /* Nothing to do. */;
11397 else if ((flag_openmp
|| flag_openmp_simd
)
11398 && is_attribute_p ("omp declare simd",
11399 get_attribute_name (t
)))
11401 tree clauses
= TREE_VALUE (val
);
11402 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11403 complain
, in_decl
);
11404 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11405 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11406 tree parms
= DECL_ARGUMENTS (*decl_p
);
11408 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11410 val
= build_tree_list (NULL_TREE
, clauses
);
11414 else if (flag_openmp
11415 && is_attribute_p ("omp declare variant base",
11416 get_attribute_name (t
)))
11418 ++cp_unevaluated_operand
;
11420 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11421 in_decl
, /*integral_constant_expression_p=*/false);
11422 --cp_unevaluated_operand
;
11423 tree chain
= TREE_CHAIN (val
);
11424 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11425 tree ctx
= copy_list (TREE_VALUE (val
));
11426 tree simd
= get_identifier ("simd");
11427 tree score
= get_identifier (" score");
11428 tree condition
= get_identifier ("condition");
11429 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11431 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11432 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11433 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11435 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11437 tree clauses
= TREE_VALUE (t2
);
11438 clauses
= tsubst_omp_clauses (clauses
,
11439 C_ORT_OMP_DECLARE_SIMD
, args
,
11440 complain
, in_decl
);
11441 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11442 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11443 TREE_VALUE (t2
) = clauses
;
11447 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11448 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11449 if (TREE_VALUE (t3
))
11452 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11453 && TREE_PURPOSE (t3
) != score
);
11454 tree v
= TREE_VALUE (t3
);
11455 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11457 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11458 v
= fold_non_dependent_expr (v
);
11459 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11460 || (TREE_PURPOSE (t3
) == score
11461 ? TREE_CODE (v
) != INTEGER_CST
11462 : !tree_fits_shwi_p (v
)))
11465 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11467 if (TREE_PURPOSE (t3
) == score
)
11468 error_at (loc
, "score argument must be "
11469 "constant integer expression");
11470 else if (allow_string
)
11471 error_at (loc
, "property must be constant "
11472 "integer expression or string "
11475 error_at (loc
, "property must be constant "
11476 "integer expression");
11479 else if (TREE_PURPOSE (t3
) == score
11480 && tree_int_cst_sgn (v
) < 0)
11483 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11485 error_at (loc
, "score argument must be "
11489 TREE_VALUE (t3
) = v
;
11494 val
= tree_cons (varid
, ctx
, chain
);
11496 /* If the first attribute argument is an identifier, don't
11497 pass it through tsubst. Attributes like mode, format,
11498 cleanup and several target specific attributes expect it
11500 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11503 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11504 /*integral_constant_expression_p=*/false);
11505 if (chain
!= TREE_CHAIN (val
))
11506 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11508 else if (PACK_EXPANSION_P (val
))
11510 /* An attribute pack expansion. */
11511 tree purp
= TREE_PURPOSE (t
);
11512 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11513 if (pack
== error_mark_node
)
11514 return error_mark_node
;
11515 int len
= TREE_VEC_LENGTH (pack
);
11516 tree list
= NULL_TREE
;
11518 for (int i
= 0; i
< len
; ++i
)
11520 tree elt
= TREE_VEC_ELT (pack
, i
);
11521 *q
= build_tree_list (purp
, elt
);
11522 q
= &TREE_CHAIN (*q
);
11527 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11528 /*integral_constant_expression_p=*/false);
11530 if (val
!= TREE_VALUE (t
))
11531 return build_tree_list (TREE_PURPOSE (t
), val
);
11535 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11536 unchanged or a new TREE_LIST chain. */
11539 tsubst_attributes (tree attributes
, tree args
,
11540 tsubst_flags_t complain
, tree in_decl
)
11542 tree last_dep
= NULL_TREE
;
11544 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11545 if (ATTR_IS_DEPENDENT (t
))
11548 attributes
= copy_list (attributes
);
11553 for (tree
*p
= &attributes
; *p
; )
11556 if (ATTR_IS_DEPENDENT (t
))
11558 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11563 p
= &TREE_CHAIN (*p
);
11564 *p
= TREE_CHAIN (t
);
11568 p
= &TREE_CHAIN (*p
);
11574 /* Apply any attributes which had to be deferred until instantiation
11575 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11576 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11579 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11580 tree args
, tsubst_flags_t complain
, tree in_decl
)
11582 tree last_dep
= NULL_TREE
;
11586 if (attributes
== NULL_TREE
)
11589 if (DECL_P (*decl_p
))
11591 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11593 p
= &DECL_ATTRIBUTES (*decl_p
);
11594 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11595 to our attributes parameter. */
11596 gcc_assert (*p
== attributes
);
11600 p
= &TYPE_ATTRIBUTES (*decl_p
);
11601 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11602 lookup_template_class_1, and should be preserved. */
11603 gcc_assert (*p
!= attributes
);
11605 p
= &TREE_CHAIN (*p
);
11608 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11609 if (ATTR_IS_DEPENDENT (t
))
11612 attributes
= copy_list (attributes
);
11619 tree late_attrs
= NULL_TREE
;
11620 tree
*q
= &late_attrs
;
11625 if (ATTR_IS_DEPENDENT (t
))
11627 *p
= TREE_CHAIN (t
);
11628 TREE_CHAIN (t
) = NULL_TREE
;
11629 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11631 q
= &TREE_CHAIN (*q
);
11634 p
= &TREE_CHAIN (t
);
11637 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11641 /* The template TMPL is being instantiated with the template arguments TARGS.
11642 Perform the access checks that we deferred when parsing the template. */
11645 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11648 deferred_access_check
*chk
;
11650 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11653 if (vec
<deferred_access_check
, va_gc
> *access_checks
11654 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11655 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11657 tree decl
= chk
->decl
;
11658 tree diag_decl
= chk
->diag_decl
;
11659 tree type_scope
= TREE_TYPE (chk
->binfo
);
11661 if (uses_template_parms (type_scope
))
11662 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11664 /* Make access check error messages point to the location
11665 of the use of the typedef. */
11666 iloc_sentinel
ils (chk
->loc
);
11667 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11668 decl
, diag_decl
, tf_warning_or_error
);
11673 instantiate_class_template_1 (tree type
)
11675 tree templ
, args
, pattern
, t
, member
;
11679 unsigned int saved_maximum_field_alignment
;
11682 if (type
== error_mark_node
)
11683 return error_mark_node
;
11685 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11686 || uses_template_parms (type
))
11689 /* Figure out which template is being instantiated. */
11690 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11691 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11693 /* Mark the type as in the process of being defined. */
11694 TYPE_BEING_DEFINED (type
) = 1;
11696 /* We may be in the middle of deferred access check. Disable
11698 deferring_access_check_sentinel
acs (dk_no_deferred
);
11700 /* Determine what specialization of the original template to
11702 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11703 if (t
== error_mark_node
)
11704 return error_mark_node
;
11707 /* This TYPE is actually an instantiation of a partial
11708 specialization. We replace the innermost set of ARGS with
11709 the arguments appropriate for substitution. For example,
11712 template <class T> struct S {};
11713 template <class T> struct S<T*> {};
11715 and supposing that we are instantiating S<int*>, ARGS will
11716 presently be {int*} -- but we need {int}. */
11717 pattern
= TREE_TYPE (t
);
11718 args
= TREE_PURPOSE (t
);
11722 pattern
= TREE_TYPE (templ
);
11723 args
= CLASSTYPE_TI_ARGS (type
);
11726 /* If the template we're instantiating is incomplete, then clearly
11727 there's nothing we can do. */
11728 if (!COMPLETE_TYPE_P (pattern
))
11730 /* We can try again later. */
11731 TYPE_BEING_DEFINED (type
) = 0;
11735 /* If we've recursively instantiated too many templates, stop. */
11736 if (! push_tinst_level (type
))
11739 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11740 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11742 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11743 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11744 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11745 fn_context
= error_mark_node
;
11747 push_to_top_level ();
11750 cp_unevaluated_operand
= 0;
11751 c_inhibit_evaluation_warnings
= 0;
11753 /* Use #pragma pack from the template context. */
11754 saved_maximum_field_alignment
= maximum_field_alignment
;
11755 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11757 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11759 /* Set the input location to the most specialized template definition.
11760 This is needed if tsubsting causes an error. */
11761 typedecl
= TYPE_MAIN_DECL (pattern
);
11762 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11763 DECL_SOURCE_LOCATION (typedecl
);
11765 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11766 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11767 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11768 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11769 if (ANON_AGGR_TYPE_P (pattern
))
11770 SET_ANON_AGGR_TYPE_P (type
);
11771 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11773 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11774 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11775 /* Adjust visibility for template arguments. */
11776 determine_visibility (TYPE_MAIN_DECL (type
));
11778 if (CLASS_TYPE_P (type
))
11779 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11781 pbinfo
= TYPE_BINFO (pattern
);
11783 /* We should never instantiate a nested class before its enclosing
11784 class; we need to look up the nested class by name before we can
11785 instantiate it, and that lookup should instantiate the enclosing
11787 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11788 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11790 base_list
= NULL_TREE
;
11791 if (BINFO_N_BASE_BINFOS (pbinfo
))
11797 /* We must enter the scope containing the type, as that is where
11798 the accessibility of types named in dependent bases are
11800 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11802 /* Substitute into each of the bases to determine the actual
11804 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11807 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11808 tree expanded_bases
= NULL_TREE
;
11811 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11814 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11815 args
, tf_error
, NULL_TREE
);
11816 if (expanded_bases
== error_mark_node
)
11819 len
= TREE_VEC_LENGTH (expanded_bases
);
11822 for (idx
= 0; idx
< len
; idx
++)
11824 if (expanded_bases
)
11825 /* Extract the already-expanded base class. */
11826 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11828 /* Substitute to figure out the base class. */
11829 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11832 if (base
== error_mark_node
)
11835 base_list
= tree_cons (access
, base
, base_list
);
11836 if (BINFO_VIRTUAL_P (pbase_binfo
))
11837 TREE_TYPE (base_list
) = integer_type_node
;
11841 /* The list is now in reverse order; correct that. */
11842 base_list
= nreverse (base_list
);
11845 pop_scope (pushed_scope
);
11847 /* Now call xref_basetypes to set up all the base-class
11849 xref_basetypes (type
, base_list
);
11851 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11852 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11853 args
, tf_error
, NULL_TREE
);
11854 fixup_attribute_variants (type
);
11856 /* Now that our base classes are set up, enter the scope of the
11857 class, so that name lookups into base classes, etc. will work
11858 correctly. This is precisely analogous to what we do in
11859 begin_class_definition when defining an ordinary non-template
11860 class, except we also need to push the enclosing classes. */
11861 push_nested_class (type
);
11863 /* Now members are processed in the order of declaration. */
11864 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11865 member
; member
= TREE_CHAIN (member
))
11867 tree t
= TREE_VALUE (member
);
11869 if (TREE_PURPOSE (member
))
11873 if (LAMBDA_TYPE_P (t
))
11874 /* A closure type for a lambda in an NSDMI or default argument.
11875 Ignore it; it will be regenerated when needed. */
11878 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11879 && TYPE_LANG_SPECIFIC (t
)
11880 && CLASSTYPE_IS_TEMPLATE (t
));
11882 /* If the member is a class template, then -- even after
11883 substitution -- there may be dependent types in the
11884 template argument list for the class. We increment
11885 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11886 that function will assume that no types are dependent
11887 when outside of a template. */
11888 if (class_template_p
)
11889 ++processing_template_decl
;
11890 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11891 if (class_template_p
)
11892 --processing_template_decl
;
11893 if (newtag
== error_mark_node
)
11896 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11898 tree name
= TYPE_IDENTIFIER (t
);
11900 if (class_template_p
)
11901 /* Unfortunately, lookup_template_class sets
11902 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11903 instantiation (i.e., for the type of a member
11904 template class nested within a template class.)
11905 This behavior is required for
11906 maybe_process_partial_specialization to work
11907 correctly, but is not accurate in this case;
11908 the TAG is not an instantiation of anything.
11909 (The corresponding TEMPLATE_DECL is an
11910 instantiation, but the TYPE is not.) */
11911 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11913 /* Now, we call pushtag to put this NEWTAG into the scope of
11914 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11915 pushtag calling push_template_decl. We don't have to do
11916 this for enums because it will already have been done in
11919 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11920 pushtag (name
, newtag
);
11923 else if (DECL_DECLARES_FUNCTION_P (t
))
11927 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11928 ++processing_template_decl
;
11929 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11930 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11931 --processing_template_decl
;
11932 set_current_access_from_decl (r
);
11933 finish_member_declaration (r
);
11934 /* Instantiate members marked with attribute used. */
11935 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11937 if (TREE_CODE (r
) == FUNCTION_DECL
11938 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11939 cp_check_omp_declare_reduction (r
);
11941 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11942 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11943 /* A closure type for a lambda in an NSDMI or default argument.
11944 Ignore it; it will be regenerated when needed. */;
11947 /* Build new TYPE_FIELDS. */
11948 if (TREE_CODE (t
) == STATIC_ASSERT
)
11949 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11950 /*integral_constant_expression_p=*/true);
11951 else if (TREE_CODE (t
) != CONST_DECL
)
11954 tree vec
= NULL_TREE
;
11957 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11958 /* The file and line for this declaration, to
11959 assist in error message reporting. Since we
11960 called push_tinst_level above, we don't need to
11962 input_location
= DECL_SOURCE_LOCATION (t
);
11964 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11965 ++processing_template_decl
;
11966 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11967 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11968 --processing_template_decl
;
11970 if (TREE_CODE (r
) == TREE_VEC
)
11972 /* A capture pack became multiple fields. */
11974 len
= TREE_VEC_LENGTH (vec
);
11977 for (int i
= 0; i
< len
; ++i
)
11980 r
= TREE_VEC_ELT (vec
, i
);
11985 [t]he initialization (and any associated
11986 side-effects) of a static data member does
11987 not occur unless the static data member is
11988 itself used in a way that requires the
11989 definition of the static data member to
11992 Therefore, we do not substitute into the
11993 initialized for the static data member here. */
11994 finish_static_data_member_decl
11996 /*init=*/NULL_TREE
,
11997 /*init_const_expr_p=*/false,
11998 /*asmspec_tree=*/NULL_TREE
,
12000 /* Instantiate members marked with attribute used. */
12001 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12004 else if (TREE_CODE (r
) == FIELD_DECL
)
12006 /* Determine whether R has a valid type and can be
12007 completed later. If R is invalid, then its type
12008 is replaced by error_mark_node. */
12009 tree rtype
= TREE_TYPE (r
);
12010 if (can_complete_type_without_circularity (rtype
))
12011 complete_type (rtype
);
12013 if (!complete_or_array_type_p (rtype
))
12015 /* If R's type couldn't be completed and
12016 it isn't a flexible array member (whose
12017 type is incomplete by definition) give
12019 cxx_incomplete_type_error (r
, rtype
);
12020 TREE_TYPE (r
) = error_mark_node
;
12022 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12023 && TYPE_DOMAIN (rtype
) == NULL_TREE
12024 && (TREE_CODE (type
) == UNION_TYPE
12025 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12027 error ("flexible array member %qD in union", r
);
12028 TREE_TYPE (r
) = error_mark_node
;
12030 else if (!verify_type_context (input_location
,
12031 TCTX_FIELD
, rtype
))
12032 TREE_TYPE (r
) = error_mark_node
;
12035 /* If it is a TYPE_DECL for a class-scoped
12036 ENUMERAL_TYPE, such a thing will already have
12037 been added to the field list by tsubst_enum
12038 in finish_member_declaration case above. */
12039 if (!(TREE_CODE (r
) == TYPE_DECL
12040 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12041 && DECL_ARTIFICIAL (r
)))
12043 set_current_access_from_decl (r
);
12044 finish_member_declaration (r
);
12052 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12053 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12055 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12057 tree friend_type
= t
;
12058 bool adjust_processing_template_decl
= false;
12060 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12062 /* template <class T> friend class C; */
12063 friend_type
= tsubst_friend_class (friend_type
, args
);
12064 adjust_processing_template_decl
= true;
12066 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12068 /* template <class T> friend class C::D; */
12069 friend_type
= tsubst (friend_type
, args
,
12070 tf_warning_or_error
, NULL_TREE
);
12071 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12072 friend_type
= TREE_TYPE (friend_type
);
12073 adjust_processing_template_decl
= true;
12075 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12076 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12078 /* This could be either
12082 when dependent_type_p is false or
12084 template <class U> friend class T::C;
12087 /* Bump processing_template_decl in case this is something like
12088 template <class T> friend struct A<T>::B. */
12089 ++processing_template_decl
;
12090 friend_type
= tsubst (friend_type
, args
,
12091 tf_warning_or_error
, NULL_TREE
);
12092 if (dependent_type_p (friend_type
))
12093 adjust_processing_template_decl
= true;
12094 --processing_template_decl
;
12096 else if (uses_template_parms (friend_type
))
12097 /* friend class C<T>; */
12098 friend_type
= tsubst (friend_type
, args
,
12099 tf_warning_or_error
, NULL_TREE
);
12105 where C is already declared or
12107 friend class C<int>;
12109 We don't have to do anything in these cases. */
12111 if (adjust_processing_template_decl
)
12112 /* Trick make_friend_class into realizing that the friend
12113 we're adding is a template, not an ordinary class. It's
12114 important that we use make_friend_class since it will
12115 perform some error-checking and output cross-reference
12117 ++processing_template_decl
;
12119 if (friend_type
!= error_mark_node
)
12120 make_friend_class (type
, friend_type
, /*complain=*/false);
12122 if (adjust_processing_template_decl
)
12123 --processing_template_decl
;
12127 /* Build new DECL_FRIENDLIST. */
12130 /* The file and line for this declaration, to
12131 assist in error message reporting. Since we
12132 called push_tinst_level above, we don't need to
12134 input_location
= DECL_SOURCE_LOCATION (t
);
12136 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12138 ++processing_template_decl
;
12139 push_deferring_access_checks (dk_no_check
);
12142 r
= tsubst_friend_function (t
, args
);
12143 add_friend (type
, r
, /*complain=*/false);
12144 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12146 pop_deferring_access_checks ();
12147 --processing_template_decl
;
12155 /* Restore these before substituting into the lambda capture
12157 cp_unevaluated_operand
= saved_unevaluated_operand
;
12158 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12161 /* Set the file and line number information to whatever is given for
12162 the class itself. This puts error messages involving generated
12163 implicit functions at a predictable point, and the same point
12164 that would be used for non-template classes. */
12165 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12167 unreverse_member_declarations (type
);
12168 finish_struct_1 (type
);
12169 TYPE_BEING_DEFINED (type
) = 0;
12171 /* We don't instantiate default arguments for member functions. 14.7.1:
12173 The implicit instantiation of a class template specialization causes
12174 the implicit instantiation of the declarations, but not of the
12175 definitions or default arguments, of the class member functions,
12176 member classes, static data members and member templates.... */
12178 perform_instantiation_time_access_checks (pattern
, args
);
12179 perform_deferred_access_checks (tf_warning_or_error
);
12180 pop_nested_class ();
12181 maximum_field_alignment
= saved_maximum_field_alignment
;
12183 pop_from_top_level ();
12184 pop_tinst_level ();
12186 /* The vtable for a template class can be emitted in any translation
12187 unit in which the class is instantiated. When there is no key
12188 method, however, finish_struct_1 will already have added TYPE to
12189 the keyed_classes. */
12190 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12191 vec_safe_push (keyed_classes
, type
);
12196 /* Wrapper for instantiate_class_template_1. */
12199 instantiate_class_template (tree type
)
12202 timevar_push (TV_TEMPLATE_INST
);
12203 ret
= instantiate_class_template_1 (type
);
12204 timevar_pop (TV_TEMPLATE_INST
);
12209 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12215 else if (TYPE_P (t
))
12216 r
= tsubst (t
, args
, complain
, in_decl
);
12219 if (!(complain
& tf_warning
))
12220 ++c_inhibit_evaluation_warnings
;
12221 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12222 /*integral_constant_expression_p=*/true);
12223 if (!(complain
& tf_warning
))
12224 --c_inhibit_evaluation_warnings
;
12230 /* Given a function parameter pack TMPL_PARM and some function parameters
12231 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12232 and set *SPEC_P to point at the next point in the list. */
12235 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12237 /* Collect all of the extra "packed" parameters into an
12240 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12241 tree spec_parm
= *spec_p
;
12244 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12246 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12249 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12250 parmvec
= make_tree_vec (len
);
12251 spec_parm
= *spec_p
;
12252 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12254 tree elt
= spec_parm
;
12255 if (DECL_PACK_P (elt
))
12256 elt
= make_pack_expansion (elt
);
12257 TREE_VEC_ELT (parmvec
, i
) = elt
;
12260 /* Build the argument packs. */
12261 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12262 *spec_p
= spec_parm
;
12267 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12268 NONTYPE_ARGUMENT_PACK. */
12271 make_fnparm_pack (tree spec_parm
)
12273 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12276 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12277 pack expansion with no extra args, 2 if it has extra args, or 0
12278 if it is not a pack expansion. */
12281 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12283 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12284 /* We're being called before this happens in tsubst_pack_expansion. */
12285 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12286 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12287 if (i
>= TREE_VEC_LENGTH (vec
))
12289 tree elt
= TREE_VEC_ELT (vec
, i
);
12291 /* A decl pack is itself an expansion. */
12292 elt
= TREE_TYPE (elt
);
12293 if (!PACK_EXPANSION_P (elt
))
12295 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12301 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12304 make_argument_pack_select (tree arg_pack
, unsigned index
)
12306 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12308 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12309 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12314 /* This is a subroutine of tsubst_pack_expansion.
12316 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12317 mechanism to store the (non complete list of) arguments of the
12318 substitution and return a non substituted pack expansion, in order
12319 to wait for when we have enough arguments to really perform the
12323 use_pack_expansion_extra_args_p (tree parm_packs
,
12325 bool has_empty_arg
)
12327 /* If one pack has an expansion and another pack has a normal
12328 argument or if one pack has an empty argument and an another
12329 one hasn't then tsubst_pack_expansion cannot perform the
12330 substitution and need to fall back on the
12331 PACK_EXPANSION_EXTRA mechanism. */
12332 if (parm_packs
== NULL_TREE
)
12334 else if (has_empty_arg
)
12336 /* If all the actual packs are pack expansions, we can still
12337 subsitute directly. */
12338 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12340 tree a
= TREE_VALUE (p
);
12341 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12342 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12343 a
= ARGUMENT_PACK_ARGS (a
);
12344 if (TREE_VEC_LENGTH (a
) == 1)
12345 a
= TREE_VEC_ELT (a
, 0);
12346 if (PACK_EXPANSION_P (a
))
12353 bool has_expansion_arg
= false;
12354 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12356 bool has_non_expansion_arg
= false;
12357 for (tree parm_pack
= parm_packs
;
12359 parm_pack
= TREE_CHAIN (parm_pack
))
12361 tree arg
= TREE_VALUE (parm_pack
);
12363 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12365 /* We can't substitute a pack expansion with extra args into
12369 has_expansion_arg
= true;
12371 has_non_expansion_arg
= true;
12374 if (has_expansion_arg
&& has_non_expansion_arg
)
12380 /* [temp.variadic]/6 says that:
12382 The instantiation of a pack expansion [...]
12383 produces a list E1,E2, ..., En, where N is the number of elements
12384 in the pack expansion parameters.
12386 This subroutine of tsubst_pack_expansion produces one of these Ei.
12388 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12389 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12390 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12391 INDEX is the index 'i' of the element Ei to produce. ARGS,
12392 COMPLAIN, and IN_DECL are the same parameters as for the
12393 tsubst_pack_expansion function.
12395 The function returns the resulting Ei upon successful completion,
12396 or error_mark_node.
12398 Note that this function possibly modifies the ARGS parameter, so
12399 it's the responsibility of the caller to restore it. */
12402 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12405 tree args
/* This parm gets
12407 tsubst_flags_t complain
,
12411 bool ith_elem_is_expansion
= false;
12413 /* For each parameter pack, change the substitution of the parameter
12414 pack to the ith argument in its argument pack, then expand the
12416 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12418 tree parm
= TREE_PURPOSE (pack
);
12419 tree arg_pack
= TREE_VALUE (pack
);
12420 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12422 ith_elem_is_expansion
|=
12423 argument_pack_element_is_expansion_p (arg_pack
, index
);
12425 /* Select the Ith argument from the pack. */
12426 if (TREE_CODE (parm
) == PARM_DECL
12428 || TREE_CODE (parm
) == FIELD_DECL
)
12432 aps
= make_argument_pack_select (arg_pack
, index
);
12433 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12434 return error_mark_node
;
12435 register_local_specialization (aps
, parm
);
12438 aps
= retrieve_local_specialization (parm
);
12443 template_parm_level_and_index (parm
, &level
, &idx
);
12447 aps
= make_argument_pack_select (arg_pack
, index
);
12448 /* Update the corresponding argument. */
12449 TMPL_ARG (args
, level
, idx
) = aps
;
12452 /* Re-use the ARGUMENT_PACK_SELECT. */
12453 aps
= TMPL_ARG (args
, level
, idx
);
12455 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12458 /* Substitute into the PATTERN with the (possibly altered)
12460 if (pattern
== in_decl
)
12461 /* Expanding a fixed parameter pack from
12462 coerce_template_parameter_pack. */
12463 t
= tsubst_decl (pattern
, args
, complain
);
12464 else if (pattern
== error_mark_node
)
12465 t
= error_mark_node
;
12466 else if (!TYPE_P (pattern
))
12467 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12468 /*integral_constant_expression_p=*/false);
12470 t
= tsubst (pattern
, args
, complain
, in_decl
);
12472 /* If the Ith argument pack element is a pack expansion, then
12473 the Ith element resulting from the substituting is going to
12474 be a pack expansion as well. */
12475 if (ith_elem_is_expansion
)
12476 t
= make_pack_expansion (t
, complain
);
12481 /* When the unexpanded parameter pack in a fold expression expands to an empty
12482 sequence, the value of the expression is as follows; the program is
12483 ill-formed if the operator is not listed in this table.
12490 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12492 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12493 if (!FOLD_EXPR_MODIFY_P (t
))
12496 case TRUTH_ANDIF_EXPR
:
12497 return boolean_true_node
;
12498 case TRUTH_ORIF_EXPR
:
12499 return boolean_false_node
;
12500 case COMPOUND_EXPR
:
12506 if (complain
& tf_error
)
12507 error_at (location_of (t
),
12508 "fold of empty expansion over %O", code
);
12509 return error_mark_node
;
12512 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12513 form an expression that combines the two terms using the
12517 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12519 tree op
= FOLD_EXPR_OP (t
);
12520 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12522 // Handle compound assignment operators.
12523 if (FOLD_EXPR_MODIFY_P (t
))
12524 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12526 warning_sentinel
s(warn_parentheses
);
12529 case COMPOUND_EXPR
:
12530 return build_x_compound_expr (input_location
, left
, right
, complain
);
12532 return build_x_binary_op (input_location
, code
,
12533 left
, TREE_CODE (left
),
12534 right
, TREE_CODE (right
),
12540 /* Substitute ARGS into the pack of a fold expression T. */
12543 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12545 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12548 /* Substitute ARGS into the pack of a fold expression T. */
12551 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12553 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12556 /* Expand a PACK of arguments into a grouped as left fold.
12557 Given a pack containing elements A0, A1, ..., An and an
12558 operator @, this builds the expression:
12560 ((A0 @ A1) @ A2) ... @ An
12562 Note that PACK must not be empty.
12564 The operator is defined by the original fold expression T. */
12567 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12569 tree left
= TREE_VEC_ELT (pack
, 0);
12570 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12572 tree right
= TREE_VEC_ELT (pack
, i
);
12573 left
= fold_expression (t
, left
, right
, complain
);
12578 /* Substitute into a unary left fold expression. */
12581 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12584 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12585 if (pack
== error_mark_node
)
12586 return error_mark_node
;
12587 if (PACK_EXPANSION_P (pack
))
12589 tree r
= copy_node (t
);
12590 FOLD_EXPR_PACK (r
) = pack
;
12593 if (TREE_VEC_LENGTH (pack
) == 0)
12594 return expand_empty_fold (t
, complain
);
12596 return expand_left_fold (t
, pack
, complain
);
12599 /* Substitute into a binary left fold expression.
12601 Do ths by building a single (non-empty) vector of argumnts and
12602 building the expression from those elements. */
12605 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12608 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12609 if (pack
== error_mark_node
)
12610 return error_mark_node
;
12611 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12612 if (init
== error_mark_node
)
12613 return error_mark_node
;
12615 if (PACK_EXPANSION_P (pack
))
12617 tree r
= copy_node (t
);
12618 FOLD_EXPR_PACK (r
) = pack
;
12619 FOLD_EXPR_INIT (r
) = init
;
12623 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12624 TREE_VEC_ELT (vec
, 0) = init
;
12625 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12626 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12628 return expand_left_fold (t
, vec
, complain
);
12631 /* Expand a PACK of arguments into a grouped as right fold.
12632 Given a pack containing elementns A0, A1, ..., and an
12633 operator @, this builds the expression:
12635 A0@ ... (An-2 @ (An-1 @ An))
12637 Note that PACK must not be empty.
12639 The operator is defined by the original fold expression T. */
12642 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12644 // Build the expression.
12645 int n
= TREE_VEC_LENGTH (pack
);
12646 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12647 for (--n
; n
!= 0; --n
)
12649 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12650 right
= fold_expression (t
, left
, right
, complain
);
12655 /* Substitute into a unary right fold expression. */
12658 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12661 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12662 if (pack
== error_mark_node
)
12663 return error_mark_node
;
12664 if (PACK_EXPANSION_P (pack
))
12666 tree r
= copy_node (t
);
12667 FOLD_EXPR_PACK (r
) = pack
;
12670 if (TREE_VEC_LENGTH (pack
) == 0)
12671 return expand_empty_fold (t
, complain
);
12673 return expand_right_fold (t
, pack
, complain
);
12676 /* Substitute into a binary right fold expression.
12678 Do ths by building a single (non-empty) vector of arguments and
12679 building the expression from those elements. */
12682 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12685 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12686 if (pack
== error_mark_node
)
12687 return error_mark_node
;
12688 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12689 if (init
== error_mark_node
)
12690 return error_mark_node
;
12692 if (PACK_EXPANSION_P (pack
))
12694 tree r
= copy_node (t
);
12695 FOLD_EXPR_PACK (r
) = pack
;
12696 FOLD_EXPR_INIT (r
) = init
;
12700 int n
= TREE_VEC_LENGTH (pack
);
12701 tree vec
= make_tree_vec (n
+ 1);
12702 for (int i
= 0; i
< n
; ++i
)
12703 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12704 TREE_VEC_ELT (vec
, n
) = init
;
12706 return expand_right_fold (t
, vec
, complain
);
12709 /* Walk through the pattern of a pack expansion, adding everything in
12710 local_specializations to a list. */
12715 hash_set
<tree
> internal
;
12717 tsubst_flags_t complain
;
12719 el_data (tsubst_flags_t c
)
12720 : extra (NULL_TREE
), complain (c
) {}
12723 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12725 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12726 tree
*extra
= &data
.extra
;
12727 tsubst_flags_t complain
= data
.complain
;
12729 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12730 /* Remember local typedefs (85214). */
12731 tp
= &TYPE_NAME (*tp
);
12733 if (TREE_CODE (*tp
) == DECL_EXPR
)
12734 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12735 else if (tree spec
= retrieve_local_specialization (*tp
))
12737 if (data
.internal
.contains (*tp
))
12738 /* Don't mess with variables declared within the pattern. */
12740 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12742 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12743 tree args
= ARGUMENT_PACK_ARGS (spec
);
12744 if (TREE_VEC_LENGTH (args
) == 1)
12746 tree elt
= TREE_VEC_ELT (args
, 0);
12747 if (PACK_EXPANSION_P (elt
))
12748 elt
= PACK_EXPANSION_PATTERN (elt
);
12749 if (DECL_PACK_P (elt
))
12752 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12754 /* Handle lambda capture here, since we aren't doing any
12755 substitution now, and so tsubst_copy won't call
12756 process_outer_var_ref. */
12757 tree args
= ARGUMENT_PACK_ARGS (spec
);
12758 int len
= TREE_VEC_LENGTH (args
);
12759 for (int i
= 0; i
< len
; ++i
)
12761 tree arg
= TREE_VEC_ELT (args
, i
);
12763 if (outer_automatic_var_p (arg
))
12764 carg
= process_outer_var_ref (arg
, complain
);
12767 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12771 spec
= copy_node (spec
);
12772 args
= copy_node (args
);
12773 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12774 register_local_specialization (spec
, *tp
);
12776 TREE_VEC_ELT (args
, i
) = carg
;
12781 if (outer_automatic_var_p (spec
))
12782 spec
= process_outer_var_ref (spec
, complain
);
12783 *extra
= tree_cons (*tp
, spec
, *extra
);
12788 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12790 el_data
data (complain
);
12791 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12795 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12796 for use in PACK_EXPANSION_EXTRA_ARGS. */
12799 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12802 if (local_specializations
)
12803 if (tree locals
= extract_local_specs (pattern
, complain
))
12804 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12808 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12809 normal template args to ARGS. */
12812 add_extra_args (tree extra
, tree args
)
12814 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12816 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12818 /* The partial instantiation involved local declarations collected in
12819 extract_local_specs; map from the general template to our local
12821 tree gen
= TREE_PURPOSE (elt
);
12822 tree inst
= TREE_VALUE (elt
);
12824 if (tree local
= retrieve_local_specialization (inst
))
12826 /* else inst is already a full instantiation of the pack. */
12827 register_local_specialization (inst
, gen
);
12829 gcc_assert (!TREE_PURPOSE (extra
));
12830 extra
= TREE_VALUE (extra
);
12833 /* I think we should always be able to substitute dependent args into the
12834 pattern. If that turns out to be incorrect in some cases, enable the
12835 alternate code (and add complain/in_decl parms to this function). */
12836 gcc_checking_assert (!uses_template_parms (extra
));
12838 if (!uses_template_parms (extra
))
12840 gcc_unreachable ();
12841 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12842 args
= add_outermost_template_args (args
, extra
);
12846 args
= add_to_template_args (extra
, args
);
12850 /* Substitute ARGS into T, which is an pack expansion
12851 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12852 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12853 (if only a partial substitution could be performed) or
12854 ERROR_MARK_NODE if there was an error. */
12856 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12860 tree pack
, packs
= NULL_TREE
;
12861 bool unsubstituted_packs
= false;
12864 bool need_local_specializations
= false;
12867 gcc_assert (PACK_EXPANSION_P (t
));
12868 pattern
= PACK_EXPANSION_PATTERN (t
);
12870 /* Add in any args remembered from an earlier partial instantiation. */
12871 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12873 levels
= TMPL_ARGS_DEPTH (args
);
12875 /* Determine the argument packs that will instantiate the parameter
12876 packs used in the expansion expression. While we're at it,
12877 compute the number of arguments to be expanded and make sure it
12879 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12880 pack
= TREE_CHAIN (pack
))
12882 tree parm_pack
= TREE_VALUE (pack
);
12883 tree arg_pack
= NULL_TREE
;
12884 tree orig_arg
= NULL_TREE
;
12887 if (TREE_CODE (parm_pack
) == BASES
)
12889 gcc_assert (parm_pack
== pattern
);
12890 if (BASES_DIRECT (parm_pack
))
12891 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12896 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12897 args
, complain
, in_decl
,
12900 else if (builtin_pack_call_p (parm_pack
))
12902 if (parm_pack
!= pattern
)
12904 if (complain
& tf_error
)
12905 sorry ("%qE is not the entire pattern of the pack expansion",
12907 return error_mark_node
;
12909 return expand_builtin_pack_call (parm_pack
, args
,
12910 complain
, in_decl
);
12912 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12914 /* We know we have correct local_specializations if this
12915 expansion is at function scope, or if we're dealing with a
12916 local parameter in a requires expression; for the latter,
12917 tsubst_requires_expr set it up appropriately. */
12918 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12919 arg_pack
= retrieve_local_specialization (parm_pack
);
12921 /* We can't rely on local_specializations for a parameter
12922 name used later in a function declaration (such as in a
12923 late-specified return type). Even if it exists, it might
12924 have the wrong value for a recursive call. */
12925 need_local_specializations
= true;
12929 /* This parameter pack was used in an unevaluated context. Just
12930 make a dummy decl, since it's only used for its type. */
12931 ++cp_unevaluated_operand
;
12932 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12933 --cp_unevaluated_operand
;
12934 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12935 /* Partial instantiation of the parm_pack, we can't build
12936 up an argument pack yet. */
12937 arg_pack
= NULL_TREE
;
12939 arg_pack
= make_fnparm_pack (arg_pack
);
12941 else if (DECL_PACK_P (arg_pack
))
12942 /* This argument pack isn't fully instantiated yet. */
12943 arg_pack
= NULL_TREE
;
12945 else if (is_capture_proxy (parm_pack
))
12947 arg_pack
= retrieve_local_specialization (parm_pack
);
12948 if (DECL_PACK_P (arg_pack
))
12949 arg_pack
= NULL_TREE
;
12954 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12955 if (level
<= levels
)
12956 arg_pack
= TMPL_ARG (args
, level
, idx
);
12958 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12959 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12960 arg_pack
= NULL_TREE
;
12963 orig_arg
= arg_pack
;
12964 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12965 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12967 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12968 /* This can only happen if we forget to expand an argument
12969 pack somewhere else. Just return an error, silently. */
12971 result
= make_tree_vec (1);
12972 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12979 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12981 /* Don't bother trying to do a partial substitution with
12982 incomplete packs; we'll try again after deduction. */
12983 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12988 else if (len
!= my_len
)
12990 if (!(complain
& tf_error
))
12991 /* Fail quietly. */;
12992 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12993 error ("mismatched argument pack lengths while expanding %qT",
12996 error ("mismatched argument pack lengths while expanding %qE",
12998 return error_mark_node
;
13001 /* Keep track of the parameter packs and their corresponding
13003 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13004 TREE_TYPE (packs
) = orig_arg
;
13008 /* We can't substitute for this parameter pack. We use a flag as
13009 well as the missing_level counter because function parameter
13010 packs don't have a level. */
13011 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13012 unsubstituted_packs
= true;
13016 /* If the expansion is just T..., return the matching argument pack, unless
13017 we need to call convert_from_reference on all the elements. This is an
13018 important optimization; see c++/68422. */
13019 if (!unsubstituted_packs
13020 && TREE_PURPOSE (packs
) == pattern
)
13022 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13024 /* If the argument pack is a single pack expansion, pull it out. */
13025 if (TREE_VEC_LENGTH (args
) == 1
13026 && pack_expansion_args_count (args
))
13027 return TREE_VEC_ELT (args
, 0);
13029 /* Types need no adjustment, nor does sizeof..., and if we still have
13030 some pack expansion args we won't do anything yet. */
13031 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13032 || PACK_EXPANSION_SIZEOF_P (t
)
13033 || pack_expansion_args_count (args
))
13035 /* Also optimize expression pack expansions if we can tell that the
13036 elements won't have reference type. */
13037 tree type
= TREE_TYPE (pattern
);
13038 if (type
&& !TYPE_REF_P (type
)
13039 && !PACK_EXPANSION_P (type
)
13040 && !WILDCARD_TYPE_P (type
))
13042 /* Otherwise use the normal path so we get convert_from_reference. */
13045 /* We cannot expand this expansion expression, because we don't have
13046 all of the argument packs we need. */
13047 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
13049 /* We got some full packs, but we can't substitute them in until we
13050 have values for all the packs. So remember these until then. */
13052 t
= make_pack_expansion (pattern
, complain
);
13053 PACK_EXPANSION_EXTRA_ARGS (t
)
13054 = build_extra_args (pattern
, args
, complain
);
13058 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13059 type, so create our own local specializations map; the current map is
13060 either NULL or (in the case of recursive unification) might have
13061 bindings that we don't want to use or alter. */
13062 local_specialization_stack
lss (need_local_specializations
13063 ? lss_blank
: lss_nop
);
13065 if (unsubstituted_packs
)
13067 /* There were no real arguments, we're just replacing a parameter
13068 pack with another version of itself. Substitute into the
13069 pattern and return a PACK_EXPANSION_*. The caller will need to
13071 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13072 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13073 /*integral_constant_expression_p=*/false);
13075 t
= tsubst (pattern
, args
, complain
, in_decl
);
13076 t
= make_pack_expansion (t
, complain
);
13080 gcc_assert (len
>= 0);
13082 /* For each argument in each argument pack, substitute into the
13084 result
= make_tree_vec (len
);
13085 tree elem_args
= copy_template_args (args
);
13086 for (i
= 0; i
< len
; ++i
)
13088 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13090 elem_args
, complain
,
13092 TREE_VEC_ELT (result
, i
) = t
;
13093 if (t
== error_mark_node
)
13095 result
= error_mark_node
;
13100 /* Update ARGS to restore the substitution from parameter packs to
13101 their argument packs. */
13102 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13104 tree parm
= TREE_PURPOSE (pack
);
13106 if (TREE_CODE (parm
) == PARM_DECL
13108 || TREE_CODE (parm
) == FIELD_DECL
)
13109 register_local_specialization (TREE_TYPE (pack
), parm
);
13114 if (TREE_VALUE (pack
) == NULL_TREE
)
13117 template_parm_level_and_index (parm
, &level
, &idx
);
13119 /* Update the corresponding argument. */
13120 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13121 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13124 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13128 /* If the dependent pack arguments were such that we end up with only a
13129 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13130 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13131 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13132 return TREE_VEC_ELT (result
, 0);
13137 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13138 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13139 parameter packs; all parms generated from a function parameter pack will
13140 have the same DECL_PARM_INDEX. */
13143 get_pattern_parm (tree parm
, tree tmpl
)
13145 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13148 if (DECL_ARTIFICIAL (parm
))
13150 for (patparm
= DECL_ARGUMENTS (pattern
);
13151 patparm
; patparm
= DECL_CHAIN (patparm
))
13152 if (DECL_ARTIFICIAL (patparm
)
13153 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13158 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13159 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13160 gcc_assert (DECL_PARM_INDEX (patparm
)
13161 == DECL_PARM_INDEX (parm
));
13167 /* Make an argument pack out of the TREE_VEC VEC. */
13170 make_argument_pack (tree vec
)
13174 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13175 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13178 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13179 TREE_CONSTANT (pack
) = 1;
13181 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13185 /* Return an exact copy of template args T that can be modified
13189 copy_template_args (tree t
)
13191 if (t
== error_mark_node
)
13194 int len
= TREE_VEC_LENGTH (t
);
13195 tree new_vec
= make_tree_vec (len
);
13197 for (int i
= 0; i
< len
; ++i
)
13199 tree elt
= TREE_VEC_ELT (t
, i
);
13200 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13201 elt
= copy_template_args (elt
);
13202 TREE_VEC_ELT (new_vec
, i
) = elt
;
13205 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13206 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13211 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13214 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13217 /* Substitute into each of the arguments. */
13218 tree new_arg
= TYPE_P (orig_arg
)
13219 ? cxx_make_type (TREE_CODE (orig_arg
))
13220 : make_node (TREE_CODE (orig_arg
));
13222 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13223 args
, complain
, in_decl
);
13224 if (pack_args
== error_mark_node
)
13225 new_arg
= error_mark_node
;
13227 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13229 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13230 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13235 /* Substitute ARGS into the vector or list of template arguments T. */
13238 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13241 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13244 if (t
== error_mark_node
)
13245 return error_mark_node
;
13247 len
= TREE_VEC_LENGTH (t
);
13248 elts
= XALLOCAVEC (tree
, len
);
13250 for (i
= 0; i
< len
; i
++)
13252 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13255 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13256 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13257 else if (PACK_EXPANSION_P (orig_arg
))
13259 /* Substitute into an expansion expression. */
13260 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13262 if (TREE_CODE (new_arg
) == TREE_VEC
)
13263 /* Add to the expanded length adjustment the number of
13264 expanded arguments. We subtract one from this
13265 measurement, because the argument pack expression
13266 itself is already counted as 1 in
13267 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13268 the argument pack is empty. */
13269 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13271 else if (ARGUMENT_PACK_P (orig_arg
))
13272 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13274 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13276 if (new_arg
== error_mark_node
)
13277 return error_mark_node
;
13280 if (new_arg
!= orig_arg
)
13287 /* Make space for the expanded arguments coming from template
13289 t
= make_tree_vec (len
+ expanded_len_adjust
);
13290 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13291 arguments for a member template.
13292 In that case each TREE_VEC in ORIG_T represents a level of template
13293 arguments, and ORIG_T won't carry any non defaulted argument count.
13294 It will rather be the nested TREE_VECs that will carry one.
13295 In other words, ORIG_T carries a non defaulted argument count only
13296 if it doesn't contain any nested TREE_VEC. */
13297 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13299 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13300 count
+= expanded_len_adjust
;
13301 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13303 for (i
= 0, out
= 0; i
< len
; i
++)
13305 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13306 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13307 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13311 /* Now expand the template argument pack "in place". */
13312 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13313 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13317 TREE_VEC_ELT (t
, out
) = elts
[i
];
13325 /* Substitute ARGS into one level PARMS of template parameters. */
13328 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13330 if (parms
== error_mark_node
)
13331 return error_mark_node
;
13333 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13335 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13337 tree tuple
= TREE_VEC_ELT (parms
, i
);
13339 if (tuple
== error_mark_node
)
13342 TREE_VEC_ELT (new_vec
, i
) =
13343 tsubst_template_parm (tuple
, args
, complain
);
13349 /* Return the result of substituting ARGS into the template parameters
13350 given by PARMS. If there are m levels of ARGS and m + n levels of
13351 PARMS, then the result will contain n levels of PARMS. For
13352 example, if PARMS is `template <class T> template <class U>
13353 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13354 result will be `template <int*, double, class V>'. */
13357 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13359 tree r
= NULL_TREE
;
13362 /* When substituting into a template, we must set
13363 PROCESSING_TEMPLATE_DECL as the template parameters may be
13364 dependent if they are based on one-another, and the dependency
13365 predicates are short-circuit outside of templates. */
13366 ++processing_template_decl
;
13368 for (new_parms
= &r
;
13369 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13370 new_parms
= &(TREE_CHAIN (*new_parms
)),
13371 parms
= TREE_CHAIN (parms
))
13373 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13376 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13377 - TMPL_ARGS_DEPTH (args
)),
13378 new_vec
, NULL_TREE
);
13379 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13380 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13383 --processing_template_decl
;
13388 /* Return the result of substituting ARGS into one template parameter
13389 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13390 parameter and which TREE_PURPOSE is the default argument of the
13391 template parameter. */
13394 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13396 tree default_value
, parm_decl
;
13398 if (args
== NULL_TREE
13400 || t
== error_mark_node
)
13403 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13405 default_value
= TREE_PURPOSE (t
);
13406 parm_decl
= TREE_VALUE (t
);
13407 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13409 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13410 if (TREE_CODE (parm_decl
) == PARM_DECL
13411 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13412 parm_decl
= error_mark_node
;
13413 default_value
= tsubst_template_arg (default_value
, args
,
13414 complain
, NULL_TREE
);
13415 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13417 tree r
= build_tree_list (default_value
, parm_decl
);
13418 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13422 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13423 type T. If T is not an aggregate or enumeration type, it is
13424 handled as if by tsubst. IN_DECL is as for tsubst. If
13425 ENTERING_SCOPE is nonzero, T is the context for a template which
13426 we are presently tsubst'ing. Return the substituted value. */
13429 tsubst_aggr_type (tree t
,
13431 tsubst_flags_t complain
,
13433 int entering_scope
)
13435 if (t
== NULL_TREE
)
13438 switch (TREE_CODE (t
))
13441 if (TYPE_PTRMEMFUNC_P (t
))
13442 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13444 /* Fall through. */
13445 case ENUMERAL_TYPE
:
13447 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13453 /* In "sizeof(X<I>)" we need to evaluate "I". */
13456 /* First, determine the context for the type we are looking
13458 context
= TYPE_CONTEXT (t
);
13459 if (context
&& TYPE_P (context
))
13461 context
= tsubst_aggr_type (context
, args
, complain
,
13462 in_decl
, /*entering_scope=*/1);
13463 /* If context is a nested class inside a class template,
13464 it may still need to be instantiated (c++/33959). */
13465 context
= complete_type (context
);
13468 /* Then, figure out what arguments are appropriate for the
13469 type we are trying to find. For example, given:
13471 template <class T> struct S;
13472 template <class T, class U> void f(T, U) { S<U> su; }
13474 and supposing that we are instantiating f<int, double>,
13475 then our ARGS will be {int, double}, but, when looking up
13476 S we only want {double}. */
13477 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13478 complain
, in_decl
);
13479 if (argvec
== error_mark_node
)
13480 r
= error_mark_node
;
13481 else if (!entering_scope
13482 && cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13484 /* See maybe_dependent_member_ref. */
13485 tree name
= TYPE_IDENTIFIER (t
);
13486 tree fullname
= name
;
13487 if (instantiates_primary_template_p (t
))
13488 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13489 INNERMOST_TEMPLATE_ARGS (argvec
));
13490 return build_typename_type (context
, name
, fullname
,
13495 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13496 entering_scope
, complain
);
13497 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13503 /* This is not a template type, so there's nothing to do. */
13507 return tsubst (t
, args
, complain
, in_decl
);
13511 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13513 /* Substitute into the default argument ARG (a default argument for
13514 FN), which has the indicated TYPE. */
13517 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13518 tsubst_flags_t complain
)
13520 int errs
= errorcount
+ sorrycount
;
13522 /* This can happen in invalid code. */
13523 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13527 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13528 && CONSTRUCTOR_NELTS (arg
) == 0)
13531 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13532 parm
= chain_index (parmnum
, parm
);
13533 tree parmtype
= TREE_TYPE (parm
);
13534 if (DECL_BY_REFERENCE (parm
))
13535 parmtype
= TREE_TYPE (parmtype
);
13536 if (parmtype
== error_mark_node
)
13537 return error_mark_node
;
13539 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13542 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13545 /* This default argument came from a template. Instantiate the
13546 default argument here, not in tsubst. In the case of
13555 we must be careful to do name lookup in the scope of S<T>,
13556 rather than in the current class. */
13557 push_to_top_level ();
13558 push_access_scope (fn
);
13559 push_deferring_access_checks (dk_no_deferred
);
13560 start_lambda_scope (parm
);
13562 /* The default argument expression may cause implicitly defined
13563 member functions to be synthesized, which will result in garbage
13564 collection. We must treat this situation as if we were within
13565 the body of function so as to avoid collecting live data on the
13568 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13569 complain
, NULL_TREE
,
13570 /*integral_constant_expression_p=*/false);
13573 finish_lambda_scope ();
13575 /* Make sure the default argument is reasonable. */
13576 arg
= check_default_argument (type
, arg
, complain
);
13578 if (errorcount
+sorrycount
> errs
13579 && (complain
& tf_warning_or_error
))
13580 inform (input_location
,
13581 " when instantiating default argument for call to %qD", fn
);
13583 pop_deferring_access_checks ();
13584 pop_access_scope (fn
);
13585 pop_from_top_level ();
13587 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13590 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13591 defarg_inst
->put (parm
, arg
);
13597 /* Substitute into all the default arguments for FN. */
13600 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13605 tmpl_args
= DECL_TI_ARGS (fn
);
13607 /* If this function is not yet instantiated, we certainly don't need
13608 its default arguments. */
13609 if (uses_template_parms (tmpl_args
))
13611 /* Don't do this again for clones. */
13612 if (DECL_CLONED_FUNCTION_P (fn
))
13616 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13618 arg
= TREE_CHAIN (arg
), ++i
)
13619 if (TREE_PURPOSE (arg
))
13620 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13622 TREE_PURPOSE (arg
),
13626 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13627 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13629 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13632 store_explicit_specifier (tree v
, tree t
)
13634 if (!explicit_specifier_map
)
13635 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13636 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13637 explicit_specifier_map
->put (v
, t
);
13640 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13643 lookup_explicit_specifier (tree v
)
13645 return *explicit_specifier_map
->get (v
);
13648 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13649 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13650 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13654 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13655 tree raises
, tsubst_flags_t complain
)
13657 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13660 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13662 new_type
= build_function_type (return_type
, arg_types
);
13663 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13667 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13668 /* Don't pick up extra function qualifiers from the basetype. */
13669 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13670 if (! MAYBE_CLASS_TYPE_P (r
))
13674 Type deduction may fail for any of the following
13677 -- Attempting to create "pointer to member of T" when T
13678 is not a class type. */
13679 if (complain
& tf_error
)
13680 error ("creating pointer to member function of non-class type %qT",
13682 return error_mark_node
;
13685 new_type
= build_method_type_directly (r
, return_type
,
13686 TREE_CHAIN (arg_types
));
13688 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13690 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13691 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13692 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13695 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13696 each of its formal parameters. If there is a disagreement then rebuild
13697 DECL's function type according to its formal parameter types, as part of a
13698 resolution for Core issues 1001/1322. */
13701 maybe_rebuild_function_decl_type (tree decl
)
13703 bool function_type_needs_rebuilding
= false;
13704 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13706 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13707 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13709 tree parm_type
= TREE_VALUE (parm_type_list
);
13710 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13711 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13713 function_type_needs_rebuilding
= true;
13717 parm_list
= DECL_CHAIN (parm_list
);
13718 parm_type_list
= TREE_CHAIN (parm_type_list
);
13722 if (!function_type_needs_rebuilding
)
13725 const tree fntype
= TREE_TYPE (decl
);
13726 tree parm_list
= DECL_ARGUMENTS (decl
);
13727 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13728 tree new_parm_type_list
= NULL_TREE
;
13729 tree
*q
= &new_parm_type_list
;
13730 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13732 *q
= copy_node (old_parm_type_list
);
13733 parm_list
= DECL_CHAIN (parm_list
);
13734 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13735 q
= &TREE_CHAIN (*q
);
13737 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13739 *q
= copy_node (old_parm_type_list
);
13740 tree
*new_parm_type
= &TREE_VALUE (*q
);
13741 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13742 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13743 *new_parm_type
= formal_parm_type_unqual
;
13745 parm_list
= DECL_CHAIN (parm_list
);
13746 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13747 q
= &TREE_CHAIN (*q
);
13749 if (old_parm_type_list
== void_list_node
)
13750 *q
= void_list_node
;
13753 = rebuild_function_or_method_type (fntype
,
13754 TREE_TYPE (fntype
), new_parm_type_list
,
13755 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13758 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13761 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13762 tree lambda_fntype
)
13764 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
13765 hashval_t hash
= 0;
13768 /* Nobody should be tsubst'ing into non-template functions. */
13769 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
13770 || DECL_LOCAL_DECL_P (t
));
13772 if (DECL_LOCAL_DECL_P (t
))
13774 if (tree spec
= retrieve_local_specialization (t
))
13777 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13779 /* If T is not dependent, just return it. */
13780 if (!uses_template_parms (DECL_TI_ARGS (t
))
13781 && !LAMBDA_FUNCTION_P (t
))
13784 /* Calculate the most general template of which R is a
13786 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13788 /* We're substituting a lambda function under tsubst_lambda_expr but not
13789 directly from it; find the matching function we're already inside.
13790 But don't do this if T is a generic lambda with a single level of
13791 template parms, as in that case we're doing a normal instantiation. */
13792 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13793 && (!generic_lambda_fn_p (t
)
13794 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13795 return enclosing_instantiation_of (t
);
13797 /* Calculate the complete set of arguments used to
13799 argvec
= tsubst_template_args (DECL_TI_ARGS
13800 (DECL_TEMPLATE_RESULT
13801 (DECL_TI_TEMPLATE (t
))),
13802 args
, complain
, in_decl
);
13803 if (argvec
== error_mark_node
)
13804 return error_mark_node
;
13806 /* Check to see if we already have this specialization. */
13807 if (!lambda_fntype
)
13809 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13810 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13814 /* We can see more levels of arguments than parameters if
13815 there was a specialization of a member template, like
13818 template <class T> struct S { template <class U> void f(); }
13819 template <> template <class U> void S<int>::f(U);
13821 Here, we'll be substituting into the specialization,
13822 because that's where we can find the code we actually
13823 want to generate, but we'll have enough arguments for
13824 the most general template.
13826 We also deal with the peculiar case:
13828 template <class T> struct S {
13829 template <class U> friend void f();
13831 template <class U> void f() {}
13833 template void f<double>();
13835 Here, the ARGS for the instantiation of will be {int,
13836 double}. But, we only need as many ARGS as there are
13837 levels of template parameters in CODE_PATTERN. We are
13838 careful not to get fooled into reducing the ARGS in
13841 template <class T> struct S { template <class U> void f(U); }
13842 template <class T> template <> void S<T>::f(int) {}
13844 which we can spot because the pattern will be a
13845 specialization in this case. */
13846 int args_depth
= TMPL_ARGS_DEPTH (args
);
13848 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13850 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13851 args
= get_innermost_template_args (args
, parms_depth
);
13855 /* This special case arises when we have something like this:
13857 template <class T> struct S {
13858 friend void f<int>(int, double);
13861 Here, the DECL_TI_TEMPLATE for the friend declaration
13862 will be an IDENTIFIER_NODE. We are being called from
13863 tsubst_friend_function, and we want only to create a
13864 new decl (R) with appropriate types so that we can call
13865 determine_specialization. */
13866 gen_tmpl
= NULL_TREE
;
13867 argvec
= NULL_TREE
;
13870 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13872 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13873 bool member
= ctx
&& TYPE_P (ctx
);
13875 if (member
&& !closure
)
13876 ctx
= tsubst_aggr_type (ctx
, args
,
13877 complain
, t
, /*entering_scope=*/1);
13879 tree type
= (lambda_fntype
? lambda_fntype
13880 : tsubst (TREE_TYPE (t
), args
,
13881 complain
| tf_fndecl_type
, in_decl
));
13882 if (type
== error_mark_node
)
13883 return error_mark_node
;
13885 /* If we hit excessive deduction depth, the type is bogus even if
13886 it isn't error_mark_node, so don't build a decl. */
13887 if (excessive_deduction_depth
)
13888 return error_mark_node
;
13890 /* We do NOT check for matching decls pushed separately at this
13891 point, as they may not represent instantiations of this
13892 template, and in any case are considered separate under the
13894 tree r
= copy_decl (t
);
13895 DECL_USE_TEMPLATE (r
) = 0;
13896 TREE_TYPE (r
) = type
;
13897 /* Clear out the mangled name and RTL for the instantiation. */
13898 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13899 SET_DECL_RTL (r
, NULL
);
13900 /* Leave DECL_INITIAL set on deleted instantiations. */
13901 if (!DECL_DELETED_FN (r
))
13902 DECL_INITIAL (r
) = NULL_TREE
;
13903 DECL_CONTEXT (r
) = ctx
;
13905 /* Handle explicit(dependent-expr). */
13906 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13908 tree spec
= lookup_explicit_specifier (t
);
13909 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13910 /*function_p=*/false,
13912 spec
= build_explicit_specifier (spec
, complain
);
13913 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13916 /* OpenMP UDRs have the only argument a reference to the declared
13917 type. We want to diagnose if the declared type is a reference,
13918 which is invalid, but as references to references are usually
13919 quietly merged, diagnose it here. */
13920 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13923 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13924 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13925 if (TYPE_REF_P (argtype
))
13926 error_at (DECL_SOURCE_LOCATION (t
),
13927 "reference type %qT in "
13928 "%<#pragma omp declare reduction%>", argtype
);
13929 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13930 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13934 if (member
&& DECL_CONV_FN_P (r
))
13935 /* Type-conversion operator. Reconstruct the name, in
13936 case it's the name of one of the template's parameters. */
13937 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13939 tree parms
= DECL_ARGUMENTS (t
);
13941 parms
= DECL_CHAIN (parms
);
13942 parms
= tsubst (parms
, args
, complain
, t
);
13943 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13944 DECL_CONTEXT (parm
) = r
;
13947 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13948 DECL_NAME (tparm
) = closure_identifier
;
13949 DECL_CHAIN (tparm
) = parms
;
13952 DECL_ARGUMENTS (r
) = parms
;
13953 DECL_RESULT (r
) = NULL_TREE
;
13955 maybe_rebuild_function_decl_type (r
);
13957 TREE_STATIC (r
) = 0;
13958 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13959 DECL_EXTERNAL (r
) = 1;
13960 /* If this is an instantiation of a function with internal
13961 linkage, we already know what object file linkage will be
13962 assigned to the instantiation. */
13963 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13964 DECL_DEFER_OUTPUT (r
) = 0;
13965 DECL_CHAIN (r
) = NULL_TREE
;
13966 DECL_PENDING_INLINE_INFO (r
) = 0;
13967 DECL_PENDING_INLINE_P (r
) = 0;
13968 DECL_SAVED_TREE (r
) = NULL_TREE
;
13969 DECL_STRUCT_FUNCTION (r
) = NULL
;
13971 /* We'll re-clone as appropriate in instantiate_template. */
13972 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13974 /* If we aren't complaining now, return on error before we register
13975 the specialization so that we'll complain eventually. */
13976 if ((complain
& tf_error
) == 0
13977 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13978 && !grok_op_properties (r
, /*complain=*/false))
13979 return error_mark_node
;
13981 /* Associate the constraints directly with the instantiation. We
13982 don't substitute through the constraints; that's only done when
13983 they are checked. */
13984 if (tree ci
= get_constraints (t
))
13985 /* Unless we're regenerating a lambda, in which case we'll set the
13986 lambda's constraints in tsubst_lambda_expr. */
13987 if (!lambda_fntype
)
13988 set_constraints (r
, ci
);
13990 if (DECL_FRIEND_CONTEXT (t
))
13991 SET_DECL_FRIEND_CONTEXT (r
,
13992 tsubst (DECL_FRIEND_CONTEXT (t
),
13993 args
, complain
, in_decl
));
13995 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13996 this in the special friend case mentioned above where
13997 GEN_TMPL is NULL. */
13998 if (gen_tmpl
&& !closure
)
14000 DECL_TEMPLATE_INFO (r
)
14001 = build_template_info (gen_tmpl
, argvec
);
14002 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14005 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14007 /* We instantiated this while substituting into
14008 the type earlier (template/friend54.C). */
14011 /* We're not supposed to instantiate default arguments
14012 until they are called, for a template. But, for a
14015 template <class T> void f ()
14016 { extern void g(int i = T()); }
14018 we should do the substitution when the template is
14019 instantiated. We handle the member function case in
14020 instantiate_class_template since the default arguments
14021 might refer to other members of the class. */
14023 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14024 && !uses_template_parms (argvec
))
14025 tsubst_default_arguments (r
, complain
);
14027 else if (DECL_LOCAL_DECL_P (r
))
14029 if (!cp_unevaluated_operand
)
14030 register_local_specialization (r
, t
);
14033 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14035 /* Copy the list of befriending classes. */
14036 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14038 friends
= &TREE_CHAIN (*friends
))
14040 *friends
= copy_node (*friends
);
14041 TREE_VALUE (*friends
)
14042 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14045 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14047 maybe_retrofit_in_chrg (r
);
14048 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14049 return error_mark_node
;
14050 /* If this is an instantiation of a member template, clone it.
14051 If it isn't, that'll be handled by
14052 clone_constructors_and_destructors. */
14053 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14054 clone_cdtor (r
, /*update_methods=*/false);
14056 else if ((complain
& tf_error
) != 0
14057 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14058 && !grok_op_properties (r
, /*complain=*/true))
14059 return error_mark_node
;
14061 /* Possibly limit visibility based on template args. */
14062 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14063 if (DECL_VISIBILITY_SPECIFIED (t
))
14065 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14066 DECL_ATTRIBUTES (r
)
14067 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14069 determine_visibility (r
);
14070 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14071 && !processing_template_decl
)
14072 defaulted_late_check (r
);
14074 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14075 args
, complain
, in_decl
);
14077 if (tree attr
= lookup_attribute ("omp declare variant base",
14078 DECL_ATTRIBUTES (r
)))
14079 omp_declare_variant_finalize (r
, attr
);
14084 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14087 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14088 tree lambda_fntype
)
14090 /* We can get here when processing a member function template,
14091 member class template, or template template parameter. */
14092 tree decl
= DECL_TEMPLATE_RESULT (t
);
14098 hashval_t hash
= 0;
14100 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14102 /* Template template parameter is treated here. */
14103 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14104 if (new_type
== error_mark_node
)
14105 r
= error_mark_node
;
14106 /* If we get a real template back, return it. This can happen in
14107 the context of most_specialized_partial_spec. */
14108 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14111 /* The new TEMPLATE_DECL was built in
14112 reduce_template_parm_level. */
14113 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14117 if (!lambda_fntype
)
14119 /* We might already have an instance of this template.
14120 The ARGS are for the surrounding class type, so the
14121 full args contain the tsubst'd args for the context,
14122 plus the innermost args from the template decl. */
14123 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14124 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14125 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14126 /* Because this is a template, the arguments will still be
14127 dependent, even after substitution. If
14128 PROCESSING_TEMPLATE_DECL is not set, the dependency
14129 predicates will short-circuit. */
14130 ++processing_template_decl
;
14131 full_args
= tsubst_template_args (tmpl_args
, args
,
14132 complain
, in_decl
);
14133 --processing_template_decl
;
14134 if (full_args
== error_mark_node
)
14135 return error_mark_node
;
14137 /* If this is a default template template argument,
14138 tsubst might not have changed anything. */
14139 if (full_args
== tmpl_args
)
14142 hash
= hash_tmpl_and_args (t
, full_args
);
14143 spec
= retrieve_specialization (t
, full_args
, hash
);
14144 if (spec
!= NULL_TREE
)
14147 /* Type partial instantiations are stored as the type by
14148 lookup_template_class_1, not here as the template. */
14149 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14154 /* Make a new template decl. It will be similar to the
14155 original, but will record the current template arguments.
14156 We also create a new function declaration, which is just
14157 like the old one, but points to this new template, rather
14158 than the old one. */
14160 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14161 DECL_CHAIN (r
) = NULL_TREE
;
14163 // Build new template info linking to the original template decl.
14164 if (!lambda_fntype
)
14166 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14167 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14170 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14172 /* The template parameters for this new template are all the
14173 template parameters for the old template, except the
14174 outermost level of parameters. */
14175 DECL_TEMPLATE_PARMS (r
)
14176 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14179 bool class_p
= false;
14181 ++processing_template_decl
;
14182 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14183 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14186 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14189 inner
= TREE_TYPE (inner
);
14192 inner
= tsubst_aggr_type (inner
, args
, complain
,
14193 in_decl
, /*entering*/1);
14195 inner
= tsubst (inner
, args
, complain
, in_decl
);
14197 --processing_template_decl
;
14198 if (inner
== error_mark_node
)
14199 return error_mark_node
;
14203 /* For a partial specialization, we need to keep pointing to
14204 the primary template. */
14205 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14206 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14208 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14209 inner
= TYPE_MAIN_DECL (inner
);
14211 else if (lambda_fntype
)
14213 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14214 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14218 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14219 DECL_TI_TEMPLATE (inner
) = r
;
14220 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14223 DECL_TEMPLATE_RESULT (r
) = inner
;
14224 TREE_TYPE (r
) = TREE_TYPE (inner
);
14225 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14227 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14228 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14230 if (PRIMARY_TEMPLATE_P (t
))
14231 DECL_PRIMARY_TEMPLATE (r
) = r
;
14233 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !lambda_fntype
)
14234 /* Record this non-type partial instantiation. */
14235 register_specialization (r
, t
,
14236 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14242 /* True if FN is the op() for a lambda in an uninstantiated template. */
14245 lambda_fn_in_template_p (tree fn
)
14247 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14249 tree closure
= DECL_CONTEXT (fn
);
14250 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14253 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14254 which the above is true. */
14257 instantiated_lambda_fn_p (tree fn
)
14259 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14261 tree closure
= DECL_CONTEXT (fn
);
14262 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14263 return LAMBDA_EXPR_INSTANTIATED (lam
);
14266 /* We're instantiating a variable from template function TCTX. Return the
14267 corresponding current enclosing scope. This gets complicated because lambda
14268 functions in templates are regenerated rather than instantiated, but generic
14269 lambda functions are subsequently instantiated. */
14272 enclosing_instantiation_of (tree otctx
)
14275 tree fn
= current_function_decl
;
14276 int lambda_count
= 0;
14278 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14279 || instantiated_lambda_fn_p (tctx
));
14280 tctx
= decl_function_context (tctx
))
14282 for (; fn
; fn
= decl_function_context (fn
))
14285 int flambda_count
= 0;
14286 for (; fn
&& instantiated_lambda_fn_p (fn
);
14287 fn
= decl_function_context (fn
))
14289 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14290 ? most_general_template (fn
) != most_general_template (tctx
)
14293 if (flambda_count
!= lambda_count
)
14295 gcc_assert (flambda_count
> lambda_count
);
14296 for (; flambda_count
> lambda_count
; --flambda_count
)
14297 ofn
= decl_function_context (ofn
);
14299 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14300 || DECL_CONV_FN_P (ofn
));
14303 gcc_unreachable ();
14306 /* Substitute the ARGS into the T, which is a _DECL. Return the
14307 result of the substitution. Issue error and warning messages under
14308 control of COMPLAIN. */
14311 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14313 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14314 location_t saved_loc
;
14315 tree r
= NULL_TREE
;
14317 hashval_t hash
= 0;
14319 /* Set the filename and linenumber to improve error-reporting. */
14320 saved_loc
= input_location
;
14321 input_location
= DECL_SOURCE_LOCATION (t
);
14323 switch (TREE_CODE (t
))
14325 case TEMPLATE_DECL
:
14326 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14329 case FUNCTION_DECL
:
14330 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14335 tree type
= NULL_TREE
;
14337 tree expanded_types
= NULL_TREE
;
14338 tree prev_r
= NULL_TREE
;
14339 tree first_r
= NULL_TREE
;
14341 if (DECL_PACK_P (t
))
14343 /* If there is a local specialization that isn't a
14344 parameter pack, it means that we're doing a "simple"
14345 substitution from inside tsubst_pack_expansion. Just
14346 return the local specialization (which will be a single
14348 tree spec
= retrieve_local_specialization (t
);
14350 && TREE_CODE (spec
) == PARM_DECL
14351 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14354 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14355 the parameters in this function parameter pack. */
14356 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14357 complain
, in_decl
);
14358 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14360 len
= TREE_VEC_LENGTH (expanded_types
);
14362 /* Zero-length parameter packs are boring. Just substitute
14364 if (len
== 0 && !cp_unevaluated_operand
)
14365 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14370 /* All we did was update the type. Make a note of that. */
14371 type
= expanded_types
;
14372 expanded_types
= NULL_TREE
;
14376 /* Loop through all of the parameters we'll build. When T is
14377 a function parameter pack, LEN is the number of expanded
14378 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14380 for (i
= 0; i
< len
; ++i
)
14384 if (DECL_TEMPLATE_PARM_P (t
))
14385 SET_DECL_TEMPLATE_PARM_P (r
);
14387 if (expanded_types
)
14388 /* We're on the Ith parameter of the function parameter
14391 /* Get the Ith type. */
14392 type
= TREE_VEC_ELT (expanded_types
, i
);
14394 /* Rename the parameter to include the index. */
14396 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14399 /* We're dealing with a normal parameter. */
14400 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14402 type
= type_decays_to (type
);
14403 TREE_TYPE (r
) = type
;
14404 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14406 if (DECL_INITIAL (r
))
14408 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14409 DECL_INITIAL (r
) = TREE_TYPE (r
);
14411 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14412 complain
, in_decl
);
14415 DECL_CONTEXT (r
) = NULL_TREE
;
14417 if (!DECL_TEMPLATE_PARM_P (r
))
14418 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14420 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14421 args
, complain
, in_decl
);
14423 /* Keep track of the first new parameter we
14424 generate. That's what will be returned to the
14429 /* Build a proper chain of parameters when substituting
14430 into a function parameter pack. */
14432 DECL_CHAIN (prev_r
) = r
;
14435 /* If cp_unevaluated_operand is set, we're just looking for a
14436 single dummy parameter, so don't keep going. */
14437 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14438 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14439 complain
, DECL_CHAIN (t
));
14441 /* FIRST_R contains the start of the chain we've built. */
14448 tree type
= NULL_TREE
;
14449 tree vec
= NULL_TREE
;
14450 tree expanded_types
= NULL_TREE
;
14453 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14455 /* This field is a lambda capture pack. Return a TREE_VEC of
14456 the expanded fields to instantiate_class_template_1. */
14457 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14458 complain
, in_decl
);
14459 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14461 len
= TREE_VEC_LENGTH (expanded_types
);
14462 vec
= make_tree_vec (len
);
14466 /* All we did was update the type. Make a note of that. */
14467 type
= expanded_types
;
14468 expanded_types
= NULL_TREE
;
14472 for (int i
= 0; i
< len
; ++i
)
14475 if (expanded_types
)
14477 type
= TREE_VEC_ELT (expanded_types
, i
);
14479 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14482 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14484 if (type
== error_mark_node
)
14485 RETURN (error_mark_node
);
14486 TREE_TYPE (r
) = type
;
14487 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14489 if (DECL_C_BIT_FIELD (r
))
14490 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14492 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14493 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14495 /*integral_constant_expression_p=*/true);
14496 if (DECL_INITIAL (t
))
14498 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14499 NSDMI in perform_member_init. Still set DECL_INITIAL
14500 so that we know there is one. */
14501 DECL_INITIAL (r
) = void_node
;
14502 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14503 retrofit_lang_decl (r
);
14504 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14506 /* We don't have to set DECL_CONTEXT here; it is set by
14507 finish_member_declaration. */
14508 DECL_CHAIN (r
) = NULL_TREE
;
14510 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14511 args
, complain
, in_decl
);
14514 TREE_VEC_ELT (vec
, i
) = r
;
14523 /* We reach here only for member using decls. We also need to check
14524 uses_template_parms because DECL_DEPENDENT_P is not set for a
14525 using-declaration that designates a member of the current
14526 instantiation (c++/53549). */
14527 if (DECL_DEPENDENT_P (t
)
14528 || uses_template_parms (USING_DECL_SCOPE (t
)))
14530 tree scope
= USING_DECL_SCOPE (t
);
14531 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14532 if (PACK_EXPANSION_P (scope
))
14534 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14535 int len
= TREE_VEC_LENGTH (vec
);
14536 r
= make_tree_vec (len
);
14537 for (int i
= 0; i
< len
; ++i
)
14539 tree escope
= TREE_VEC_ELT (vec
, i
);
14540 tree elt
= do_class_using_decl (escope
, name
);
14543 r
= error_mark_node
;
14548 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14549 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14551 TREE_VEC_ELT (r
, i
) = elt
;
14556 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14557 complain
, in_decl
);
14558 r
= do_class_using_decl (inst_scope
, name
);
14560 r
= error_mark_node
;
14563 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14564 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14571 DECL_CHAIN (r
) = NULL_TREE
;
14578 tree argvec
= NULL_TREE
;
14579 tree gen_tmpl
= NULL_TREE
;
14580 tree tmpl
= NULL_TREE
;
14581 tree type
= NULL_TREE
;
14583 if (TREE_TYPE (t
) == error_mark_node
)
14584 RETURN (error_mark_node
);
14586 if (TREE_CODE (t
) == TYPE_DECL
14587 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14589 /* If this is the canonical decl, we don't have to
14590 mess with instantiations, and often we can't (for
14591 typename, template type parms and such). Note that
14592 TYPE_NAME is not correct for the above test if
14593 we've copied the type for a typedef. */
14594 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14595 if (type
== error_mark_node
)
14596 RETURN (error_mark_node
);
14597 r
= TYPE_NAME (type
);
14601 /* Check to see if we already have the specialization we
14603 tree spec
= NULL_TREE
;
14604 bool local_p
= false;
14605 tree ctx
= DECL_CONTEXT (t
);
14606 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14607 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14610 if (DECL_CLASS_SCOPE_P (t
))
14612 ctx
= tsubst_aggr_type (ctx
, args
,
14614 in_decl
, /*entering_scope=*/1);
14615 /* If CTX is unchanged, then T is in fact the
14616 specialization we want. That situation occurs when
14617 referencing a static data member within in its own
14618 class. We can use pointer equality, rather than
14619 same_type_p, because DECL_CONTEXT is always
14621 if (ctx
== DECL_CONTEXT (t
)
14622 /* ... unless T is a member template; in which
14623 case our caller can be willing to create a
14624 specialization of that template represented
14626 && !(DECL_TI_TEMPLATE (t
)
14627 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14633 tmpl
= DECL_TI_TEMPLATE (t
);
14634 gen_tmpl
= most_general_template (tmpl
);
14635 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14636 if (argvec
!= error_mark_node
)
14637 argvec
= (coerce_innermost_template_parms
14638 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14639 argvec
, t
, complain
,
14640 /*all*/true, /*defarg*/true));
14641 if (argvec
== error_mark_node
)
14642 RETURN (error_mark_node
);
14643 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14644 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14649 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14650 /* Subsequent calls to pushdecl will fill this in. */
14652 /* A local variable. */
14654 /* Unless this is a reference to a static variable from an
14655 enclosing function, in which case we need to fill it in now. */
14656 if (TREE_STATIC (t
))
14658 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14659 if (fn
!= current_function_decl
)
14662 spec
= retrieve_local_specialization (t
);
14664 /* If we already have the specialization we need, there is
14665 nothing more to do. */
14672 /* Create a new node for the specialization we need. */
14673 if (type
== NULL_TREE
)
14675 if (is_typedef_decl (t
))
14676 type
= DECL_ORIGINAL_TYPE (t
);
14678 type
= TREE_TYPE (t
);
14680 && VAR_HAD_UNKNOWN_BOUND (t
)
14681 && type
!= error_mark_node
)
14682 type
= strip_array_domain (type
);
14683 tree sub_args
= args
;
14684 if (tree auto_node
= type_uses_auto (type
))
14686 /* Mask off any template args past the variable's context so we
14687 don't replace the auto with an unrelated argument. */
14688 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14689 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14691 /* This should never happen with the new lambda instantiation
14692 model, but keep the handling just in case. */
14693 gcc_assert (!CHECKING_P
),
14694 sub_args
= strip_innermost_template_args (args
, extra
);
14696 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14697 /* Substituting the type might have recursively instantiated this
14698 same alias (c++/86171). */
14699 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14700 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14709 DECL_INITIALIZED_P (r
) = 0;
14710 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14711 if (type
== error_mark_node
)
14712 RETURN (error_mark_node
);
14713 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14715 /* It may seem that this case cannot occur, since:
14720 declares a function, not a variable. However:
14723 template <typename T> void g() { T t; }
14724 template void g<f>();
14726 is an attempt to declare a variable with function
14728 error ("variable %qD has function type",
14729 /* R is not yet sufficiently initialized, so we
14730 just use its name. */
14732 RETURN (error_mark_node
);
14734 type
= complete_type (type
);
14735 /* Wait until cp_finish_decl to set this again, to handle
14736 circular dependency (template/instantiate6.C). */
14737 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14738 type
= check_var_type (DECL_NAME (r
), type
,
14739 DECL_SOURCE_LOCATION (r
));
14740 if (DECL_HAS_VALUE_EXPR_P (t
))
14742 tree ve
= DECL_VALUE_EXPR (t
);
14743 /* If the DECL_VALUE_EXPR is converted to the declared type,
14744 preserve the identity so that gimplify_type_sizes works. */
14745 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14747 ve
= TREE_OPERAND (ve
, 0);
14748 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14749 /*constant_expression_p=*/false);
14750 if (REFERENCE_REF_P (ve
))
14752 gcc_assert (TYPE_REF_P (type
));
14753 ve
= TREE_OPERAND (ve
, 0);
14756 ve
= build_nop (type
, ve
);
14757 else if (DECL_LANG_SPECIFIC (t
)
14758 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14759 && TREE_CODE (ve
) == COMPONENT_REF
14760 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14761 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14762 type
= TREE_TYPE (ve
);
14764 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14765 == TYPE_MAIN_VARIANT (type
));
14766 SET_DECL_VALUE_EXPR (r
, ve
);
14768 if (CP_DECL_THREAD_LOCAL_P (r
)
14769 && !processing_template_decl
)
14770 set_decl_tls_model (r
, decl_default_tls_model (r
));
14772 else if (DECL_SELF_REFERENCE_P (t
))
14773 SET_DECL_SELF_REFERENCE_P (r
);
14774 TREE_TYPE (r
) = type
;
14775 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14776 DECL_CONTEXT (r
) = ctx
;
14777 /* Clear out the mangled name and RTL for the instantiation. */
14778 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14779 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14780 SET_DECL_RTL (r
, NULL
);
14781 /* The initializer must not be expanded until it is required;
14782 see [temp.inst]. */
14783 DECL_INITIAL (r
) = NULL_TREE
;
14784 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14787 if (DECL_LANG_SPECIFIC (r
))
14788 SET_DECL_DEPENDENT_INIT_P (r
, false);
14790 SET_DECL_MODE (r
, VOIDmode
);
14792 /* Possibly limit visibility based on template args. */
14793 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14794 if (DECL_VISIBILITY_SPECIFIED (t
))
14796 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14797 DECL_ATTRIBUTES (r
)
14798 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14800 determine_visibility (r
);
14805 /* A static data member declaration is always marked
14806 external when it is declared in-class, even if an
14807 initializer is present. We mimic the non-template
14808 processing here. */
14809 DECL_EXTERNAL (r
) = 1;
14810 if (DECL_NAMESPACE_SCOPE_P (t
))
14811 DECL_NOT_REALLY_EXTERN (r
) = 1;
14813 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14814 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14815 if (!error_operand_p (r
) || (complain
& tf_error
))
14816 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14820 if (DECL_LANG_SPECIFIC (r
))
14821 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14822 if (!cp_unevaluated_operand
)
14823 register_local_specialization (r
, t
);
14826 DECL_CHAIN (r
) = NULL_TREE
;
14828 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14830 args
, complain
, in_decl
);
14832 /* Preserve a typedef that names a type. */
14833 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14835 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14836 set_underlying_type (r
);
14837 if (TYPE_DECL_ALIAS_P (r
))
14838 /* An alias template specialization can be dependent
14839 even if its underlying type is not. */
14840 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14843 layout_decl (r
, 0);
14848 gcc_unreachable ();
14853 /* Restore the file and line information. */
14854 input_location
= saved_loc
;
14859 /* Substitute into the complete parameter type list PARMS. */
14862 tsubst_function_parms (tree parms
,
14864 tsubst_flags_t complain
,
14867 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14870 /* Substitute into the ARG_TYPES of a function type.
14871 If END is a TREE_CHAIN, leave it and any following types
14875 tsubst_arg_types (tree arg_types
,
14878 tsubst_flags_t complain
,
14881 tree remaining_arg_types
;
14882 tree type
= NULL_TREE
;
14884 tree expanded_args
= NULL_TREE
;
14887 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14890 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14891 args
, end
, complain
, in_decl
);
14892 if (remaining_arg_types
== error_mark_node
)
14893 return error_mark_node
;
14895 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14897 /* For a pack expansion, perform substitution on the
14898 entire expression. Later on, we'll handle the arguments
14900 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14901 args
, complain
, in_decl
);
14903 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14904 /* So that we'll spin through the parameters, one by one. */
14905 i
= TREE_VEC_LENGTH (expanded_args
);
14908 /* We only partially substituted into the parameter
14909 pack. Our type is TYPE_PACK_EXPANSION. */
14910 type
= expanded_args
;
14911 expanded_args
= NULL_TREE
;
14919 type
= TREE_VEC_ELT (expanded_args
, i
);
14921 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14923 if (type
== error_mark_node
)
14924 return error_mark_node
;
14925 if (VOID_TYPE_P (type
))
14927 if (complain
& tf_error
)
14929 error ("invalid parameter type %qT", type
);
14931 error ("in declaration %q+D", in_decl
);
14933 return error_mark_node
;
14936 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14937 return error_mark_node
;
14939 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14940 top-level qualifiers as required. */
14941 type
= cv_unqualified (type_decays_to (type
));
14943 /* We do not substitute into default arguments here. The standard
14944 mandates that they be instantiated only when needed, which is
14945 done in build_over_call. */
14946 default_arg
= TREE_PURPOSE (arg_types
);
14948 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14949 since the new op() won't have any associated template arguments for us
14950 to refer to later. */
14951 if (lambda_fn_in_template_p (in_decl
))
14952 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14953 false/*fn*/, false/*constexpr*/);
14955 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14957 /* We've instantiated a template before its default arguments
14958 have been parsed. This can happen for a nested template
14959 class, and is not an error unless we require the default
14960 argument in a call of this function. */
14961 remaining_arg_types
=
14962 tree_cons (default_arg
, type
, remaining_arg_types
);
14963 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14964 remaining_arg_types
);
14967 remaining_arg_types
=
14968 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14971 return remaining_arg_types
;
14974 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14975 *not* handle the exception-specification for FNTYPE, because the
14976 initial substitution of explicitly provided template parameters
14977 during argument deduction forbids substitution into the
14978 exception-specification:
14982 All references in the function type of the function template to the
14983 corresponding template parameters are replaced by the specified tem-
14984 plate argument values. If a substitution in a template parameter or
14985 in the function type of the function template results in an invalid
14986 type, type deduction fails. [Note: The equivalent substitution in
14987 exception specifications is done only when the function is instanti-
14988 ated, at which point a program is ill-formed if the substitution
14989 results in an invalid type.] */
14992 tsubst_function_type (tree t
,
14994 tsubst_flags_t complain
,
14998 tree arg_types
= NULL_TREE
;
15000 /* The TYPE_CONTEXT is not used for function/method types. */
15001 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15003 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15005 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15007 if (late_return_type_p
)
15009 /* Substitute the argument types. */
15010 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15011 complain
, in_decl
);
15012 if (arg_types
== error_mark_node
)
15013 return error_mark_node
;
15015 tree save_ccp
= current_class_ptr
;
15016 tree save_ccr
= current_class_ref
;
15017 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15018 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15019 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15022 /* DR 1207: 'this' is in scope in the trailing return type. */
15023 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15026 /* Substitute the return type. */
15027 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15031 current_class_ptr
= save_ccp
;
15032 current_class_ref
= save_ccr
;
15036 /* Substitute the return type. */
15037 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15039 if (return_type
== error_mark_node
)
15040 return error_mark_node
;
15041 /* DR 486 clarifies that creation of a function type with an
15042 invalid return type is a deduction failure. */
15043 if (TREE_CODE (return_type
) == ARRAY_TYPE
15044 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15046 if (complain
& tf_error
)
15048 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15049 error ("function returning an array");
15051 error ("function returning a function");
15053 return error_mark_node
;
15056 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
15057 return error_mark_node
;
15059 if (!late_return_type_p
)
15061 /* Substitute the argument types. */
15062 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15063 complain
, in_decl
);
15064 if (arg_types
== error_mark_node
)
15065 return error_mark_node
;
15068 /* Construct a new type node and return it. */
15069 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15070 /*raises=*/NULL_TREE
, complain
);
15073 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15074 ARGS into that specification, and return the substituted
15075 specification. If there is no specification, return NULL_TREE. */
15078 tsubst_exception_specification (tree fntype
,
15080 tsubst_flags_t complain
,
15087 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15088 new_specs
= NULL_TREE
;
15089 if (specs
&& TREE_PURPOSE (specs
))
15091 /* A noexcept-specifier. */
15092 tree expr
= TREE_PURPOSE (specs
);
15093 if (TREE_CODE (expr
) == INTEGER_CST
)
15097 /* Defer instantiation of noexcept-specifiers to avoid
15098 excessive instantiations (c++/49107). */
15099 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15100 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15102 /* We already partially instantiated this member template,
15103 so combine the new args with the old. */
15104 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15105 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15106 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15107 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15111 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15112 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15117 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15119 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15121 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15123 new_specs
= tsubst_copy_and_build
15124 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15125 /*integral_constant_expression_p=*/true);
15127 new_specs
= build_noexcept_spec (new_specs
, complain
);
15131 if (! TREE_VALUE (specs
))
15138 tree expanded_specs
= NULL_TREE
;
15140 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15142 /* Expand the pack expansion type. */
15143 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15147 if (expanded_specs
== error_mark_node
)
15148 return error_mark_node
;
15149 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15150 len
= TREE_VEC_LENGTH (expanded_specs
);
15153 /* We're substituting into a member template, so
15154 we got a TYPE_PACK_EXPANSION back. Add that
15155 expansion and move on. */
15156 gcc_assert (TREE_CODE (expanded_specs
)
15157 == TYPE_PACK_EXPANSION
);
15158 new_specs
= add_exception_specifier (new_specs
,
15161 specs
= TREE_CHAIN (specs
);
15166 for (i
= 0; i
< len
; ++i
)
15168 if (expanded_specs
)
15169 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15171 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15172 if (spec
== error_mark_node
)
15174 new_specs
= add_exception_specifier (new_specs
, spec
,
15178 specs
= TREE_CHAIN (specs
);
15184 /* Substitute through a TREE_LIST of types or expressions, handling pack
15188 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15190 if (t
== void_list_node
)
15193 tree purpose
= TREE_PURPOSE (t
);
15194 tree purposevec
= NULL_TREE
;
15197 else if (PACK_EXPANSION_P (purpose
))
15199 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15200 if (TREE_CODE (purpose
) == TREE_VEC
)
15201 purposevec
= purpose
;
15203 else if (TYPE_P (purpose
))
15204 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15206 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15207 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15208 return error_mark_node
;
15210 tree value
= TREE_VALUE (t
);
15211 tree valuevec
= NULL_TREE
;
15214 else if (PACK_EXPANSION_P (value
))
15216 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15217 if (TREE_CODE (value
) == TREE_VEC
)
15220 else if (TYPE_P (value
))
15221 value
= tsubst (value
, args
, complain
, in_decl
);
15223 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15224 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15225 return error_mark_node
;
15227 tree chain
= TREE_CHAIN (t
);
15230 else if (TREE_CODE (chain
) == TREE_LIST
)
15231 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15232 else if (TYPE_P (chain
))
15233 chain
= tsubst (chain
, args
, complain
, in_decl
);
15235 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15236 if (chain
== error_mark_node
)
15237 return error_mark_node
;
15239 if (purpose
== TREE_PURPOSE (t
)
15240 && value
== TREE_VALUE (t
)
15241 && chain
== TREE_CHAIN (t
))
15245 /* Determine the number of arguments. */
15248 len
= TREE_VEC_LENGTH (purposevec
);
15249 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15252 len
= TREE_VEC_LENGTH (valuevec
);
15256 for (int i
= len
; i
-- > 0; )
15259 purpose
= TREE_VEC_ELT (purposevec
, i
);
15261 value
= TREE_VEC_ELT (valuevec
, i
);
15263 if (value
&& TYPE_P (value
))
15264 chain
= hash_tree_cons (purpose
, value
, chain
);
15266 chain
= tree_cons (purpose
, value
, chain
);
15272 /* Take the tree structure T and replace template parameters used
15273 therein with the argument vector ARGS. IN_DECL is an associated
15274 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15275 Issue error and warning messages under control of COMPLAIN. Note
15276 that we must be relatively non-tolerant of extensions here, in
15277 order to preserve conformance; if we allow substitutions that
15278 should not be allowed, we may allow argument deductions that should
15279 not succeed, and therefore report ambiguous overload situations
15280 where there are none. In theory, we could allow the substitution,
15281 but indicate that it should have failed, and allow our caller to
15282 make sure that the right thing happens, but we don't try to do this
15285 This function is used for dealing with types, decls and the like;
15286 for expressions, use tsubst_expr or tsubst_copy. */
15289 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15291 enum tree_code code
;
15292 tree type
, r
= NULL_TREE
;
15294 if (t
== NULL_TREE
|| t
== error_mark_node
15295 || t
== integer_type_node
15296 || t
== void_type_node
15297 || t
== char_type_node
15298 || t
== unknown_type_node
15299 || TREE_CODE (t
) == NAMESPACE_DECL
15300 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15304 return tsubst_decl (t
, args
, complain
);
15306 if (args
== NULL_TREE
)
15309 code
= TREE_CODE (t
);
15311 if (code
== IDENTIFIER_NODE
)
15312 type
= IDENTIFIER_TYPE_VALUE (t
);
15314 type
= TREE_TYPE (t
);
15316 gcc_assert (type
!= unknown_type_node
);
15318 /* Reuse typedefs. We need to do this to handle dependent attributes,
15319 such as attribute aligned. */
15321 && typedef_variant_p (t
))
15323 tree decl
= TYPE_NAME (t
);
15325 if (alias_template_specialization_p (t
, nt_opaque
))
15327 /* DECL represents an alias template and we want to
15329 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15330 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15331 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15333 else if (DECL_CLASS_SCOPE_P (decl
)
15334 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15335 && uses_template_parms (DECL_CONTEXT (decl
)))
15337 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15338 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15339 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15341 else if (DECL_FUNCTION_SCOPE_P (decl
)
15342 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15343 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15344 r
= retrieve_local_specialization (decl
);
15346 /* The typedef is from a non-template context. */
15352 r
= cp_build_qualified_type_real
15353 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15354 complain
| tf_ignore_bad_quals
);
15359 /* We don't have an instantiation yet, so drop the typedef. */
15360 int quals
= cp_type_quals (t
);
15361 t
= DECL_ORIGINAL_TYPE (decl
);
15362 t
= cp_build_qualified_type_real (t
, quals
,
15363 complain
| tf_ignore_bad_quals
);
15367 bool fndecl_type
= (complain
& tf_fndecl_type
);
15368 complain
&= ~tf_fndecl_type
;
15371 && code
!= TYPENAME_TYPE
15372 && code
!= TEMPLATE_TYPE_PARM
15373 && code
!= TEMPLATE_PARM_INDEX
15374 && code
!= IDENTIFIER_NODE
15375 && code
!= FUNCTION_TYPE
15376 && code
!= METHOD_TYPE
)
15377 type
= tsubst (type
, args
, complain
, in_decl
);
15378 if (type
== error_mark_node
)
15379 return error_mark_node
;
15385 case ENUMERAL_TYPE
:
15386 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15387 /*entering_scope=*/0);
15390 case IDENTIFIER_NODE
:
15401 if (t
== integer_type_node
)
15404 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15405 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15409 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15411 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15412 /*integral_constant_expression_p=*/false);
15414 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15416 if (TREE_CODE (max
) == NOP_EXPR
15417 && TREE_SIDE_EFFECTS (omax
)
15418 && !TREE_TYPE (max
))
15419 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15421 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15422 with TREE_SIDE_EFFECTS that indicates this is not an integral
15423 constant expression. */
15424 if (processing_template_decl
15425 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15427 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15428 TREE_SIDE_EFFECTS (max
) = 1;
15431 return compute_array_index_type (NULL_TREE
, max
, complain
);
15434 case TEMPLATE_TYPE_PARM
:
15435 case TEMPLATE_TEMPLATE_PARM
:
15436 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15437 case TEMPLATE_PARM_INDEX
:
15442 tree arg
= NULL_TREE
;
15446 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15447 template_parm_level_and_index (t
, &level
, &idx
);
15449 levels
= TMPL_ARGS_DEPTH (args
);
15450 if (level
<= levels
15451 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15453 arg
= TMPL_ARG (args
, level
, idx
);
15455 /* See through ARGUMENT_PACK_SELECT arguments. */
15456 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15457 arg
= argument_pack_select_arg (arg
);
15460 if (arg
== error_mark_node
)
15461 return error_mark_node
;
15462 else if (arg
!= NULL_TREE
)
15464 if (ARGUMENT_PACK_P (arg
))
15465 /* If ARG is an argument pack, we don't actually want to
15466 perform a substitution here, because substitutions
15467 for argument packs are only done
15468 element-by-element. We can get to this point when
15469 substituting the type of a non-type template
15470 parameter pack, when that type actually contains
15471 template parameter packs from an outer template, e.g.,
15473 template<typename... Types> struct A {
15474 template<Types... Values> struct B { };
15478 if (code
== TEMPLATE_TYPE_PARM
)
15482 /* When building concept checks for the purpose of
15483 deducing placeholders, we can end up with wildcards
15484 where types are expected. Adjust this to the deduced
15486 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15487 arg
= TREE_TYPE (TREE_TYPE (arg
));
15489 gcc_assert (TYPE_P (arg
));
15491 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15493 return cp_build_qualified_type_real
15494 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15496 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15498 /* We are processing a type constructed from a
15499 template template parameter. */
15500 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15501 args
, complain
, in_decl
);
15502 if (argvec
== error_mark_node
)
15503 return error_mark_node
;
15505 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15506 || TREE_CODE (arg
) == TEMPLATE_DECL
15507 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15509 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15510 /* Consider this code:
15512 template <template <class> class Template>
15514 template <class Arg> using Bind = Template<Arg>;
15517 template <template <class> class Template, class Arg>
15518 using Instantiate = Template<Arg>; //#0
15520 template <template <class> class Template,
15523 Instantiate<Internal<Template>::template Bind,
15526 When #1 is parsed, the
15527 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15528 parameter `Template' in #0 matches the
15529 UNBOUND_CLASS_TEMPLATE representing the argument
15530 `Internal<Template>::template Bind'; We then want
15531 to assemble the type `Bind<Argument>' that can't
15532 be fully created right now, because
15533 `Internal<Template>' not being complete, the Bind
15534 template cannot be looked up in that context. So
15535 we need to "store" `Bind<Argument>' for later
15536 when the context of Bind becomes complete. Let's
15537 store that in a TYPENAME_TYPE. */
15538 return make_typename_type (TYPE_CONTEXT (arg
),
15539 build_nt (TEMPLATE_ID_EXPR
,
15540 TYPE_IDENTIFIER (arg
),
15545 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15546 are resolving nested-types in the signature of a
15547 member function templates. Otherwise ARG is a
15548 TEMPLATE_DECL and is the real template to be
15550 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15551 arg
= TYPE_NAME (arg
);
15553 r
= lookup_template_class (arg
,
15555 DECL_CONTEXT (arg
),
15556 /*entering_scope=*/0,
15558 return cp_build_qualified_type_real
15559 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15561 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15564 /* TEMPLATE_PARM_INDEX. */
15565 return convert_from_reference (unshare_expr (arg
));
15569 /* This can happen during the attempted tsubst'ing in
15570 unify. This means that we don't yet have any information
15571 about the template parameter in question. */
15574 /* Early in template argument deduction substitution, we don't
15575 want to reduce the level of 'auto', or it will be confused
15576 with a normal template parm in subsequent deduction.
15577 Similarly, don't reduce the level of template parameters to
15578 avoid mismatches when deducing their types. */
15579 if (complain
& tf_partial
)
15582 /* If we get here, we must have been looking at a parm for a
15583 more deeply nested template. Make a new version of this
15584 template parameter, but with a lower level. */
15587 case TEMPLATE_TYPE_PARM
:
15588 case TEMPLATE_TEMPLATE_PARM
:
15589 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15590 if (cp_type_quals (t
))
15592 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15593 r
= cp_build_qualified_type_real
15594 (r
, cp_type_quals (t
),
15595 complain
| (code
== TEMPLATE_TYPE_PARM
15596 ? tf_ignore_bad_quals
: 0));
15598 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15599 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15600 && (r
= (TEMPLATE_PARM_DESCENDANTS
15601 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15602 && (r
= TREE_TYPE (r
))
15603 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15604 /* Break infinite recursion when substituting the constraints
15605 of a constrained placeholder. */;
15606 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15607 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15608 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15609 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15610 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15611 && (TEMPLATE_PARM_LEVEL (r
)
15612 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15613 /* Cache the simple case of lowering a type parameter. */
15618 TEMPLATE_TYPE_PARM_INDEX (r
)
15619 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15620 r
, levels
, args
, complain
);
15621 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15622 TYPE_MAIN_VARIANT (r
) = r
;
15623 TYPE_POINTER_TO (r
) = NULL_TREE
;
15624 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15626 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15628 /* Propagate constraints on placeholders since they are
15629 only instantiated during satisfaction. */
15630 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15631 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15632 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15634 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15635 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15639 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15640 /* We have reduced the level of the template
15641 template parameter, but not the levels of its
15642 template parameters, so canonical_type_parameter
15643 will not be able to find the canonical template
15644 template parameter for this level. Thus, we
15645 require structural equality checking to compare
15646 TEMPLATE_TEMPLATE_PARMs. */
15647 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15648 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15649 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15651 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15653 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15655 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15656 /* We might need to substitute into the types of non-type
15657 template parameters. */
15658 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15659 complain
, in_decl
);
15660 if (tmpl
== error_mark_node
)
15661 return error_mark_node
;
15662 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15663 complain
, in_decl
);
15664 if (argvec
== error_mark_node
)
15665 return error_mark_node
;
15667 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15668 = build_template_info (tmpl
, argvec
);
15673 case TEMPLATE_PARM_INDEX
:
15674 /* OK, now substitute the type of the non-type parameter. We
15675 couldn't do it earlier because it might be an auto parameter,
15676 and we wouldn't need to if we had an argument. */
15677 type
= tsubst (type
, args
, complain
, in_decl
);
15678 if (type
== error_mark_node
)
15679 return error_mark_node
;
15680 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15684 gcc_unreachable ();
15691 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15694 /* We should never be tsubsting a binfo. */
15695 gcc_unreachable ();
15698 /* A vector of template arguments. */
15699 gcc_assert (!type
);
15700 return tsubst_template_args (t
, args
, complain
, in_decl
);
15703 case REFERENCE_TYPE
:
15705 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15710 Type deduction may fail for any of the following
15713 -- Attempting to create a pointer to reference type.
15714 -- Attempting to create a reference to a reference type or
15715 a reference to void.
15717 Core issue 106 says that creating a reference to a reference
15718 during instantiation is no longer a cause for failure. We
15719 only enforce this check in strict C++98 mode. */
15720 if ((TYPE_REF_P (type
)
15721 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15722 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15724 static location_t last_loc
;
15726 /* We keep track of the last time we issued this error
15727 message to avoid spewing a ton of messages during a
15728 single bad template instantiation. */
15729 if (complain
& tf_error
15730 && last_loc
!= input_location
)
15732 if (VOID_TYPE_P (type
))
15733 error ("forming reference to void");
15734 else if (code
== POINTER_TYPE
)
15735 error ("forming pointer to reference type %qT", type
);
15737 error ("forming reference to reference type %qT", type
);
15738 last_loc
= input_location
;
15741 return error_mark_node
;
15743 else if (TREE_CODE (type
) == FUNCTION_TYPE
15744 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15745 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15747 if (complain
& tf_error
)
15749 if (code
== POINTER_TYPE
)
15750 error ("forming pointer to qualified function type %qT",
15753 error ("forming reference to qualified function type %qT",
15756 return error_mark_node
;
15758 else if (code
== POINTER_TYPE
)
15760 r
= build_pointer_type (type
);
15761 if (TREE_CODE (type
) == METHOD_TYPE
)
15762 r
= build_ptrmemfunc_type (r
);
15764 else if (TYPE_REF_P (type
))
15765 /* In C++0x, during template argument substitution, when there is an
15766 attempt to create a reference to a reference type, reference
15767 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15769 "If a template-argument for a template-parameter T names a type
15770 that is a reference to a type A, an attempt to create the type
15771 'lvalue reference to cv T' creates the type 'lvalue reference to
15772 A,' while an attempt to create the type type rvalue reference to
15773 cv T' creates the type T"
15775 r
= cp_build_reference_type
15777 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15779 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15780 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15782 if (r
!= error_mark_node
)
15783 /* Will this ever be needed for TYPE_..._TO values? */
15790 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15791 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15795 Type deduction may fail for any of the following
15798 -- Attempting to create "pointer to member of T" when T
15799 is not a class type. */
15800 if (complain
& tf_error
)
15801 error ("creating pointer to member of non-class type %qT", r
);
15802 return error_mark_node
;
15804 if (TYPE_REF_P (type
))
15806 if (complain
& tf_error
)
15807 error ("creating pointer to member reference type %qT", type
);
15808 return error_mark_node
;
15810 if (VOID_TYPE_P (type
))
15812 if (complain
& tf_error
)
15813 error ("creating pointer to member of type void");
15814 return error_mark_node
;
15816 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15817 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15819 /* The type of the implicit object parameter gets its
15820 cv-qualifiers from the FUNCTION_TYPE. */
15823 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15824 type_memfn_rqual (type
));
15825 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15826 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15830 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15834 case FUNCTION_TYPE
:
15839 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15840 if (fntype
== error_mark_node
)
15841 return error_mark_node
;
15843 /* Substitute the exception specification. */
15844 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15845 /*defer_ok*/fndecl_type
);
15846 if (specs
== error_mark_node
)
15847 return error_mark_node
;
15849 fntype
= build_exception_variant (fntype
, specs
);
15854 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15855 if (domain
== error_mark_node
)
15856 return error_mark_node
;
15858 /* As an optimization, we avoid regenerating the array type if
15859 it will obviously be the same as T. */
15860 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15863 /* These checks should match the ones in create_array_type_for_decl.
15867 The deduction may fail for any of the following reasons:
15869 -- Attempting to create an array with an element type that
15870 is void, a function type, or a reference type, or [DR337]
15871 an abstract class type. */
15872 if (VOID_TYPE_P (type
)
15873 || TREE_CODE (type
) == FUNCTION_TYPE
15874 || (TREE_CODE (type
) == ARRAY_TYPE
15875 && TYPE_DOMAIN (type
) == NULL_TREE
)
15876 || TYPE_REF_P (type
))
15878 if (complain
& tf_error
)
15879 error ("creating array of %qT", type
);
15880 return error_mark_node
;
15883 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15884 return error_mark_node
;
15886 r
= build_cplus_array_type (type
, domain
);
15888 if (!valid_array_size_p (input_location
, r
, in_decl
,
15889 (complain
& tf_error
)))
15890 return error_mark_node
;
15892 if (TYPE_USER_ALIGN (t
))
15894 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15895 TYPE_USER_ALIGN (r
) = 1;
15901 case TYPENAME_TYPE
:
15903 tree ctx
= TYPE_CONTEXT (t
);
15904 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15906 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15907 if (ctx
== error_mark_node
15908 || TREE_VEC_LENGTH (ctx
) > 1)
15909 return error_mark_node
;
15910 if (TREE_VEC_LENGTH (ctx
) == 0)
15912 if (complain
& tf_error
)
15913 error ("%qD is instantiated for an empty pack",
15914 TYPENAME_TYPE_FULLNAME (t
));
15915 return error_mark_node
;
15917 ctx
= TREE_VEC_ELT (ctx
, 0);
15920 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15921 /*entering_scope=*/1);
15922 if (ctx
== error_mark_node
)
15923 return error_mark_node
;
15925 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15926 complain
, in_decl
);
15927 if (f
== error_mark_node
)
15928 return error_mark_node
;
15930 if (!MAYBE_CLASS_TYPE_P (ctx
))
15932 if (complain
& tf_error
)
15933 error ("%qT is not a class, struct, or union type", ctx
);
15934 return error_mark_node
;
15936 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15938 /* Normally, make_typename_type does not require that the CTX
15939 have complete type in order to allow things like:
15941 template <class T> struct S { typename S<T>::X Y; };
15943 But, such constructs have already been resolved by this
15944 point, so here CTX really should have complete type, unless
15945 it's a partial instantiation. */
15946 ctx
= complete_type (ctx
);
15947 if (!COMPLETE_TYPE_P (ctx
))
15949 if (complain
& tf_error
)
15950 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15951 return error_mark_node
;
15955 f
= make_typename_type (ctx
, f
, typename_type
,
15956 complain
| tf_keep_type_decl
);
15957 if (f
== error_mark_node
)
15959 if (TREE_CODE (f
) == TYPE_DECL
)
15961 complain
|= tf_ignore_bad_quals
;
15965 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15967 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15969 if (complain
& tf_error
)
15970 error ("%qT resolves to %qT, which is not an enumeration type",
15973 return error_mark_node
;
15975 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15977 if (complain
& tf_error
)
15978 error ("%qT resolves to %qT, which is not a class type",
15981 return error_mark_node
;
15985 return cp_build_qualified_type_real
15986 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15989 case UNBOUND_CLASS_TEMPLATE
:
15991 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15992 in_decl
, /*entering_scope=*/1);
15993 tree name
= TYPE_IDENTIFIER (t
);
15994 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15996 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15997 return error_mark_node
;
16000 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16001 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16008 ++cp_unevaluated_operand
;
16009 ++c_inhibit_evaluation_warnings
;
16011 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
16013 /*integral_constant_expression_p=*/false);
16015 --cp_unevaluated_operand
;
16016 --c_inhibit_evaluation_warnings
;
16018 type
= finish_typeof (type
);
16019 return cp_build_qualified_type_real (type
,
16021 | cp_type_quals (type
),
16025 case DECLTYPE_TYPE
:
16029 ++cp_unevaluated_operand
;
16030 ++c_inhibit_evaluation_warnings
;
16032 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16033 complain
|tf_decltype
, in_decl
,
16034 /*function_p*/false,
16035 /*integral_constant_expression*/false);
16037 --cp_unevaluated_operand
;
16038 --c_inhibit_evaluation_warnings
;
16040 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16041 type
= lambda_capture_field_type (type
,
16042 false /*explicit_init*/,
16043 DECLTYPE_FOR_REF_CAPTURE (t
));
16044 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16045 type
= lambda_proxy_type (type
);
16048 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16049 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16051 /* In a template ~id could be either a complement expression
16052 or an unqualified-id naming a destructor; if instantiating
16053 it produces an expression, it's not an id-expression or
16056 type
= finish_decltype_type (type
, id
, complain
);
16058 return cp_build_qualified_type_real (type
,
16060 | cp_type_quals (type
),
16061 complain
| tf_ignore_bad_quals
);
16064 case UNDERLYING_TYPE
:
16066 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16067 complain
, in_decl
);
16068 return finish_underlying_type (type
);
16071 case TYPE_ARGUMENT_PACK
:
16072 case NONTYPE_ARGUMENT_PACK
:
16073 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16088 /* We should use one of the expression tsubsts for these codes. */
16089 gcc_unreachable ();
16092 sorry ("use of %qs in template", get_tree_code_name (code
));
16093 return error_mark_node
;
16097 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16098 expression on the left-hand side of the "." or "->" operator. We
16099 only do the lookup if we had a dependent BASELINK. Otherwise we
16100 adjust it onto the instantiated heirarchy. */
16103 tsubst_baselink (tree baselink
, tree object_type
,
16104 tree args
, tsubst_flags_t complain
, tree in_decl
)
16106 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16107 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16108 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16110 tree optype
= BASELINK_OPTYPE (baselink
);
16111 optype
= tsubst (optype
, args
, complain
, in_decl
);
16113 tree template_args
= NULL_TREE
;
16114 bool template_id_p
= false;
16115 tree fns
= BASELINK_FUNCTIONS (baselink
);
16116 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16118 template_id_p
= true;
16119 template_args
= TREE_OPERAND (fns
, 1);
16120 fns
= TREE_OPERAND (fns
, 0);
16122 template_args
= tsubst_template_args (template_args
, args
,
16123 complain
, in_decl
);
16126 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16127 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16128 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16132 tree name
= OVL_NAME (fns
);
16133 if (IDENTIFIER_CONV_OP_P (name
))
16134 name
= make_conv_op_name (optype
);
16136 if (name
== complete_dtor_identifier
)
16137 /* Treat as-if non-dependent below. */
16138 dependent_p
= false;
16140 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16144 if ((complain
& tf_error
)
16145 && constructor_name_p (name
, qualifying_scope
))
16146 error ("cannot call constructor %<%T::%D%> directly",
16147 qualifying_scope
, name
);
16148 return error_mark_node
;
16151 if (BASELINK_P (baselink
))
16152 fns
= BASELINK_FUNCTIONS (baselink
);
16155 /* We're going to overwrite pieces below, make a duplicate. */
16156 baselink
= copy_node (baselink
);
16158 /* If lookup found a single function, mark it as used at this point.
16159 (If lookup found multiple functions the one selected later by
16160 overload resolution will be marked as used at that point.) */
16161 if (!template_id_p
&& !really_overloaded_fn (fns
))
16163 tree fn
= OVL_FIRST (fns
);
16164 bool ok
= mark_used (fn
, complain
);
16165 if (!ok
&& !(complain
& tf_error
))
16166 return error_mark_node
;
16167 if (ok
&& BASELINK_P (baselink
))
16168 /* We might have instantiated an auto function. */
16169 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16172 if (BASELINK_P (baselink
))
16174 /* Add back the template arguments, if present. */
16176 BASELINK_FUNCTIONS (baselink
)
16177 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16179 /* Update the conversion operator type. */
16180 BASELINK_OPTYPE (baselink
) = optype
;
16184 object_type
= current_class_type
;
16186 if (qualified_p
|| !dependent_p
)
16188 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16192 /* We need to call adjust_result_of_qualified_name_lookup in case the
16193 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16194 so that we still get virtual function binding. */
16195 BASELINK_QUALIFIED_P (baselink
) = false;
16201 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16202 true if the qualified-id will be a postfix-expression in-and-of
16203 itself; false if more of the postfix-expression follows the
16204 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16208 tsubst_qualified_id (tree qualified_id
, tree args
,
16209 tsubst_flags_t complain
, tree in_decl
,
16210 bool done
, bool address_p
)
16216 tree template_args
;
16217 location_t loc
= EXPR_LOCATION (qualified_id
);
16219 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16221 /* Figure out what name to look up. */
16222 name
= TREE_OPERAND (qualified_id
, 1);
16223 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16225 is_template
= true;
16226 template_args
= TREE_OPERAND (name
, 1);
16228 template_args
= tsubst_template_args (template_args
, args
,
16229 complain
, in_decl
);
16230 if (template_args
== error_mark_node
)
16231 return error_mark_node
;
16232 name
= TREE_OPERAND (name
, 0);
16236 is_template
= false;
16237 template_args
= NULL_TREE
;
16240 /* Substitute into the qualifying scope. When there are no ARGS, we
16241 are just trying to simplify a non-dependent expression. In that
16242 case the qualifying scope may be dependent, and, in any case,
16243 substituting will not help. */
16244 scope
= TREE_OPERAND (qualified_id
, 0);
16247 scope
= tsubst (scope
, args
, complain
, in_decl
);
16248 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16253 if (dependent_scope_p (scope
))
16256 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16257 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16258 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16259 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16263 if (!BASELINK_P (name
) && !DECL_P (expr
))
16265 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16267 /* A BIT_NOT_EXPR is used to represent a destructor. */
16268 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16270 error ("qualifying type %qT does not match destructor name ~%qT",
16271 scope
, TREE_OPERAND (expr
, 0));
16272 expr
= error_mark_node
;
16275 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16276 LOOK_want::NORMAL
, false);
16279 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16280 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16281 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16283 if (complain
& tf_error
)
16285 error ("dependent-name %qE is parsed as a non-type, but "
16286 "instantiation yields a type", qualified_id
);
16287 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16289 return error_mark_node
;
16295 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16297 return error_mark_node
;
16298 /* Remember that there was a reference to this entity. */
16299 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16300 return error_mark_node
;
16303 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16305 if (complain
& tf_error
)
16306 qualified_name_lookup_error (scope
,
16307 TREE_OPERAND (qualified_id
, 1),
16308 expr
, input_location
);
16309 return error_mark_node
;
16314 /* We may be repeating a check already done during parsing, but
16315 if it was well-formed and passed then, it will pass again
16316 now, and if it didn't, we wouldn't have got here. The case
16317 we want to catch is when we couldn't tell then, and can now,
16318 namely when templ prior to substitution was an
16320 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16321 return error_mark_node
;
16323 if (variable_template_p (expr
))
16324 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16327 expr
= lookup_template_function (expr
, template_args
);
16330 if (expr
== error_mark_node
&& complain
& tf_error
)
16331 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16332 expr
, input_location
);
16333 else if (TYPE_P (scope
))
16335 expr
= (adjust_result_of_qualified_name_lookup
16336 (expr
, scope
, current_nonlambda_class_type ()));
16337 expr
= (finish_qualified_id_expr
16338 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16339 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16340 /*template_arg_p=*/false, complain
));
16343 /* Expressions do not generally have reference type. */
16344 if (TREE_CODE (expr
) != SCOPE_REF
16345 /* However, if we're about to form a pointer-to-member, we just
16346 want the referenced member referenced. */
16347 && TREE_CODE (expr
) != OFFSET_REF
)
16348 expr
= convert_from_reference (expr
);
16350 if (REF_PARENTHESIZED_P (qualified_id
))
16351 expr
= force_paren_expr (expr
);
16353 expr
= maybe_wrap_with_location (expr
, loc
);
16358 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16359 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16363 tsubst_init (tree init
, tree decl
, tree args
,
16364 tsubst_flags_t complain
, tree in_decl
)
16369 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16371 tree type
= TREE_TYPE (decl
);
16373 if (!init
&& type
!= error_mark_node
)
16375 if (tree auto_node
= type_uses_auto (type
))
16377 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16379 if (complain
& tf_error
)
16380 error ("initializer for %q#D expands to an empty list "
16381 "of expressions", decl
);
16382 return error_mark_node
;
16385 else if (!dependent_type_p (type
))
16387 /* If we had an initializer but it
16388 instantiated to nothing,
16389 value-initialize the object. This will
16390 only occur when the initializer was a
16391 pack expansion where the parameter packs
16392 used in that expansion were of length
16394 init
= build_value_init (type
, complain
);
16395 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16396 init
= get_target_expr_sfinae (init
, complain
);
16397 if (TREE_CODE (init
) == TARGET_EXPR
)
16398 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16405 /* If T is a reference to a dependent member of the current instantiation C and
16406 we are trying to refer to that member in a partial instantiation of C,
16407 return a SCOPE_REF; otherwise, return NULL_TREE.
16409 This can happen when forming a C++17 deduction guide, as in PR96199. */
16412 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16415 if (cxx_dialect
< cxx17
)
16418 tree ctx
= context_for_name_lookup (t
);
16419 if (!CLASS_TYPE_P (ctx
))
16422 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16423 if (dependent_scope_p (ctx
))
16424 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16425 /*template_p=*/false);
16430 /* Like tsubst, but deals with expressions. This function just replaces
16431 template parms; to finish processing the resultant expression, use
16432 tsubst_copy_and_build or tsubst_expr. */
16435 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16437 enum tree_code code
;
16440 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16443 code
= TREE_CODE (t
);
16448 r
= retrieve_local_specialization (t
);
16450 if (r
== NULL_TREE
)
16452 /* We get here for a use of 'this' in an NSDMI. */
16453 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16454 return current_class_ptr
;
16456 /* This can happen for a parameter name used later in a function
16457 declaration (such as in a late-specified return type). Just
16458 make a dummy decl, since it's only used for its type. */
16459 gcc_assert (cp_unevaluated_operand
!= 0);
16460 r
= tsubst_decl (t
, args
, complain
);
16461 /* Give it the template pattern as its context; its true context
16462 hasn't been instantiated yet and this is good enough for
16464 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16467 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16468 r
= argument_pack_select_arg (r
);
16469 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16470 return error_mark_node
;
16478 if (DECL_TEMPLATE_PARM_P (t
))
16479 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16480 /* There is no need to substitute into namespace-scope
16482 if (DECL_NAMESPACE_SCOPE_P (t
))
16484 /* If ARGS is NULL, then T is known to be non-dependent. */
16485 if (args
== NULL_TREE
)
16486 return scalar_constant_value (t
);
16488 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16491 /* Unfortunately, we cannot just call lookup_name here.
16494 template <int I> int f() {
16496 struct S { void g() { E e = a; } };
16499 When we instantiate f<7>::S::g(), say, lookup_name is not
16500 clever enough to find f<7>::a. */
16502 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16503 /*entering_scope=*/0);
16505 for (v
= TYPE_VALUES (enum_type
);
16507 v
= TREE_CHAIN (v
))
16508 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16509 return TREE_VALUE (v
);
16511 /* We didn't find the name. That should never happen; if
16512 name-lookup found it during preliminary parsing, we
16513 should find it again here during instantiation. */
16514 gcc_unreachable ();
16519 if (DECL_CONTEXT (t
))
16523 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16524 /*entering_scope=*/1);
16525 if (ctx
!= DECL_CONTEXT (t
))
16527 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16530 if (complain
& tf_error
)
16531 error ("using invalid field %qD", t
);
16532 return error_mark_node
;
16541 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16544 case FUNCTION_DECL
:
16545 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16546 r
= tsubst (t
, args
, complain
, in_decl
);
16547 else if (DECL_LOCAL_DECL_P (t
))
16549 /* Local specialization will have been created when we
16550 instantiated the DECL_EXPR_DECL. */
16551 r
= retrieve_local_specialization (t
);
16553 r
= error_mark_node
;
16555 else if (local_variable_p (t
)
16556 && uses_template_parms (DECL_CONTEXT (t
)))
16558 r
= retrieve_local_specialization (t
);
16559 if (r
== NULL_TREE
)
16561 /* First try name lookup to find the instantiation. */
16562 r
= lookup_name (DECL_NAME (t
));
16567 /* During error-recovery we may find a non-variable,
16568 even an OVERLOAD: just bail out and avoid ICEs and
16569 duplicate diagnostics (c++/62207). */
16570 gcc_assert (seen_error ());
16571 return error_mark_node
;
16573 if (!is_capture_proxy (r
))
16575 /* Make sure the one we found is the one we want. */
16576 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16577 if (ctx
!= DECL_CONTEXT (r
))
16586 /* This can happen for a variable used in a
16587 late-specified return type of a local lambda, or for a
16588 local static or constant. Building a new VAR_DECL
16589 should be OK in all those cases. */
16590 r
= tsubst_decl (t
, args
, complain
);
16591 if (local_specializations
)
16592 /* Avoid infinite recursion (79640). */
16593 register_local_specialization (r
, t
);
16594 if (decl_maybe_constant_var_p (r
))
16596 /* We can't call cp_finish_decl, so handle the
16597 initializer by hand. */
16598 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16599 complain
, in_decl
);
16600 if (!processing_template_decl
)
16601 init
= maybe_constant_init (init
);
16602 if (processing_template_decl
16603 ? potential_constant_expression (init
)
16604 : reduced_constant_expression_p (init
))
16605 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16606 = TREE_CONSTANT (r
) = true;
16607 DECL_INITIAL (r
) = init
;
16608 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16610 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16611 complain
, adc_variable_type
);
16613 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16614 || decl_constant_var_p (r
)
16616 if (!processing_template_decl
16617 && !TREE_STATIC (r
))
16618 r
= process_outer_var_ref (r
, complain
);
16620 /* Remember this for subsequent uses. */
16621 if (local_specializations
)
16622 register_local_specialization (r
, t
);
16624 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16625 r
= argument_pack_select_arg (r
);
16629 if (!mark_used (r
, complain
))
16630 return error_mark_node
;
16633 case NAMESPACE_DECL
:
16640 return tsubst_baselink (t
, current_nonlambda_class_type (),
16641 args
, complain
, in_decl
);
16643 case TEMPLATE_DECL
:
16644 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16645 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16646 args
, complain
, in_decl
);
16647 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16648 return tsubst (t
, args
, complain
, in_decl
);
16649 else if (DECL_CLASS_SCOPE_P (t
)
16650 && uses_template_parms (DECL_CONTEXT (t
)))
16652 /* Template template argument like the following example need
16655 template <template <class> class TT> struct C {};
16656 template <class T> struct D {
16657 template <class U> struct E {};
16662 We are processing the template argument `E' in #1 for
16663 the template instantiation #2. Originally, `E' is a
16664 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16665 have to substitute this with one having context `D<int>'. */
16667 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16668 if (dependent_scope_p (context
))
16670 /* When rewriting a constructor into a deduction guide, a
16671 non-dependent name can become dependent, so memtmpl<args>
16672 becomes context::template memtmpl<args>. */
16673 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16674 return build_qualified_name (type
, context
, DECL_NAME (t
),
16677 return lookup_field (context
, DECL_NAME(t
), 0, false);
16680 /* Ordinary template template argument. */
16683 case NON_LVALUE_EXPR
:
16684 case VIEW_CONVERT_EXPR
:
16686 /* Handle location wrappers by substituting the wrapped node
16687 first, *then* reusing the resulting type. Doing the type
16688 first ensures that we handle template parameters and
16689 parameter pack expansions. */
16690 if (location_wrapper_p (t
))
16692 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16693 complain
, in_decl
);
16694 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16696 tree op
= TREE_OPERAND (t
, 0);
16697 if (code
== VIEW_CONVERT_EXPR
16698 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16700 /* Wrapper to make a C++20 template parameter object const. */
16701 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16702 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16704 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16705 return build1 (code
, type
, op
);
16709 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16710 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16711 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16715 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16716 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16718 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16719 op
= build1 (code
, TREE_TYPE (op
), op
);
16720 REF_PARENTHESIZED_P (op
) = true;
16723 /* We shouldn't see any other uses of these in templates. */
16724 gcc_unreachable ();
16728 case REINTERPRET_CAST_EXPR
:
16729 case CONST_CAST_EXPR
:
16730 case STATIC_CAST_EXPR
:
16731 case DYNAMIC_CAST_EXPR
:
16732 case IMPLICIT_CONV_EXPR
:
16736 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16737 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16738 return build1 (code
, type
, op0
);
16742 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16743 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16745 tree expanded
, op
= TREE_OPERAND (t
, 0);
16748 if (SIZEOF_EXPR_TYPE_P (t
))
16749 op
= TREE_TYPE (op
);
16751 ++cp_unevaluated_operand
;
16752 ++c_inhibit_evaluation_warnings
;
16753 /* We only want to compute the number of arguments. */
16754 if (PACK_EXPANSION_P (op
))
16755 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16757 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16758 args
, complain
, in_decl
);
16759 --cp_unevaluated_operand
;
16760 --c_inhibit_evaluation_warnings
;
16762 if (TREE_CODE (expanded
) == TREE_VEC
)
16764 len
= TREE_VEC_LENGTH (expanded
);
16765 /* Set TREE_USED for the benefit of -Wunused. */
16766 for (int i
= 0; i
< len
; i
++)
16767 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16768 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16771 if (expanded
== error_mark_node
)
16772 return error_mark_node
;
16773 else if (PACK_EXPANSION_P (expanded
)
16774 || (TREE_CODE (expanded
) == TREE_VEC
16775 && pack_expansion_args_count (expanded
)))
16778 if (PACK_EXPANSION_P (expanded
))
16780 else if (TREE_VEC_LENGTH (expanded
) == 1)
16781 expanded
= TREE_VEC_ELT (expanded
, 0);
16783 expanded
= make_argument_pack (expanded
);
16785 if (TYPE_P (expanded
))
16786 return cxx_sizeof_or_alignof_type (input_location
,
16787 expanded
, SIZEOF_EXPR
,
16789 complain
& tf_error
);
16791 return cxx_sizeof_or_alignof_expr (input_location
,
16792 expanded
, SIZEOF_EXPR
,
16794 complain
& tf_error
);
16797 return build_int_cst (size_type_node
, len
);
16799 if (SIZEOF_EXPR_TYPE_P (t
))
16801 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16802 args
, complain
, in_decl
);
16803 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16804 r
= build1 (SIZEOF_EXPR
,
16805 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16806 SIZEOF_EXPR_TYPE_P (r
) = 1;
16813 case TRUTH_NOT_EXPR
:
16816 case UNARY_PLUS_EXPR
: /* Unary + */
16818 case AT_ENCODE_EXPR
:
16822 case REALPART_EXPR
:
16823 case IMAGPART_EXPR
:
16826 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16827 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16828 r
= build1 (code
, type
, op0
);
16829 if (code
== ALIGNOF_EXPR
)
16830 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16834 case COMPONENT_REF
:
16839 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16840 name
= TREE_OPERAND (t
, 1);
16841 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16843 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16844 complain
, in_decl
);
16845 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16847 else if (TREE_CODE (name
) == SCOPE_REF
16848 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16850 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16851 complain
, in_decl
);
16852 name
= TREE_OPERAND (name
, 1);
16853 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16854 complain
, in_decl
);
16855 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16856 name
= build_qualified_name (/*type=*/NULL_TREE
,
16858 /*template_p=*/false);
16860 else if (BASELINK_P (name
))
16861 name
= tsubst_baselink (name
,
16862 non_reference (TREE_TYPE (object
)),
16866 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16867 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16873 case TRUNC_DIV_EXPR
:
16874 case CEIL_DIV_EXPR
:
16875 case FLOOR_DIV_EXPR
:
16876 case ROUND_DIV_EXPR
:
16877 case EXACT_DIV_EXPR
:
16881 case TRUNC_MOD_EXPR
:
16882 case FLOOR_MOD_EXPR
:
16883 case TRUTH_ANDIF_EXPR
:
16884 case TRUTH_ORIF_EXPR
:
16885 case TRUTH_AND_EXPR
:
16886 case TRUTH_OR_EXPR
:
16897 case COMPOUND_EXPR
:
16900 case PREDECREMENT_EXPR
:
16901 case PREINCREMENT_EXPR
:
16902 case POSTDECREMENT_EXPR
:
16903 case POSTINCREMENT_EXPR
:
16905 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16906 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16907 return build_nt (code
, op0
, op1
);
16912 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16913 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16914 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16915 QUALIFIED_NAME_IS_TEMPLATE (t
));
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 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16927 int n
= VL_EXP_OPERAND_LENGTH (t
);
16928 tree result
= build_vl_exp (CALL_EXPR
, n
);
16930 for (i
= 0; i
< n
; i
++)
16931 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16932 complain
, in_decl
);
16938 case PSEUDO_DTOR_EXPR
:
16939 case VEC_PERM_EXPR
:
16941 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16942 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16943 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16944 r
= build_nt (code
, op0
, op1
, op2
);
16945 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16951 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16952 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16953 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16954 r
= build_nt (code
, op0
, op1
, op2
);
16955 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16961 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16962 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16963 r
= build_nt (code
, op0
, op1
);
16964 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16965 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16969 case TEMPLATE_ID_EXPR
:
16971 /* Substituted template arguments */
16972 tree fn
= TREE_OPERAND (t
, 0);
16973 tree targs
= TREE_OPERAND (t
, 1);
16975 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16977 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16979 return lookup_template_function (fn
, targs
);
16984 tree purpose
, value
, chain
;
16986 if (t
== void_list_node
)
16989 purpose
= TREE_PURPOSE (t
);
16991 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16992 value
= TREE_VALUE (t
);
16994 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16995 chain
= TREE_CHAIN (t
);
16996 if (chain
&& chain
!= void_type_node
)
16997 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16998 if (purpose
== TREE_PURPOSE (t
)
16999 && value
== TREE_VALUE (t
)
17000 && chain
== TREE_CHAIN (t
))
17002 return tree_cons (purpose
, value
, chain
);
17007 case ENUMERAL_TYPE
:
17009 case TEMPLATE_TYPE_PARM
:
17010 case TEMPLATE_TEMPLATE_PARM
:
17011 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17012 case TEMPLATE_PARM_INDEX
:
17014 case REFERENCE_TYPE
:
17016 case FUNCTION_TYPE
:
17019 case TYPENAME_TYPE
:
17020 case UNBOUND_CLASS_TEMPLATE
:
17022 case DECLTYPE_TYPE
:
17024 return tsubst (t
, args
, complain
, in_decl
);
17028 /* Fall through. */
17029 case IDENTIFIER_NODE
:
17030 if (IDENTIFIER_CONV_OP_P (t
))
17032 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17033 return make_conv_op_name (new_type
);
17039 /* This is handled by tsubst_copy_and_build. */
17040 gcc_unreachable ();
17044 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17045 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17046 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17049 case CLEANUP_POINT_EXPR
:
17050 /* We shouldn't have built any of these during initial template
17051 generation. Instead, they should be built during instantiation
17052 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17053 gcc_unreachable ();
17057 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17058 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17059 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17060 r
= build2 (code
, type
, op0
, op1
);
17061 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17062 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17063 && !(complain
& tf_error
))
17064 return error_mark_node
;
17068 case EXPR_PACK_EXPANSION
:
17069 error ("invalid use of pack expansion expression");
17070 return error_mark_node
;
17072 case NONTYPE_ARGUMENT_PACK
:
17073 error ("use %<...%> to expand argument pack");
17074 return error_mark_node
;
17077 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17084 /* Instantiate any typedefs in the type. */
17085 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17086 r
= fold_convert (type
, t
);
17087 gcc_assert (TREE_CODE (r
) == code
);
17093 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17095 if (type
!= TREE_TYPE (t
))
17098 TREE_TYPE (r
) = type
;
17104 /* These can sometimes show up in a partial instantiation, but never
17105 involve template parms. */
17106 gcc_assert (!uses_template_parms (t
));
17109 case UNARY_LEFT_FOLD_EXPR
:
17110 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17111 case UNARY_RIGHT_FOLD_EXPR
:
17112 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17113 case BINARY_LEFT_FOLD_EXPR
:
17114 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17115 case BINARY_RIGHT_FOLD_EXPR
:
17116 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17120 case DEBUG_BEGIN_STMT
:
17121 /* ??? There's no point in copying it for now, but maybe some
17122 day it will contain more information, such as a pointer back
17123 to the containing function, inlined copy or so. */
17126 case CO_AWAIT_EXPR
:
17127 return tsubst_expr (t
, args
, complain
, in_decl
,
17128 /*integral_constant_expression_p=*/false);
17132 /* We shouldn't get here, but keep going if !flag_checking. */
17134 gcc_unreachable ();
17139 /* Helper function for tsubst_omp_clauses, used for instantiation of
17140 OMP_CLAUSE_DECL of clauses. */
17143 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17144 tree in_decl
, tree
*iterator_cache
)
17146 if (decl
== NULL_TREE
)
17149 /* Handle OpenMP iterators. */
17150 if (TREE_CODE (decl
) == TREE_LIST
17151 && TREE_PURPOSE (decl
)
17152 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17155 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17156 ret
= iterator_cache
[1];
17160 begin_scope (sk_omp
, NULL
);
17161 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17163 *tp
= copy_node (it
);
17164 TREE_VEC_ELT (*tp
, 0)
17165 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17166 TREE_VEC_ELT (*tp
, 1)
17167 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17168 /*integral_constant_expression_p=*/false);
17169 TREE_VEC_ELT (*tp
, 2)
17170 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17171 /*integral_constant_expression_p=*/false);
17172 TREE_VEC_ELT (*tp
, 3)
17173 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17174 /*integral_constant_expression_p=*/false);
17175 TREE_CHAIN (*tp
) = NULL_TREE
;
17176 tp
= &TREE_CHAIN (*tp
);
17178 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17179 iterator_cache
[0] = TREE_PURPOSE (decl
);
17180 iterator_cache
[1] = ret
;
17182 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17187 /* Handle an OpenMP array section represented as a TREE_LIST (or
17188 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17189 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17190 TREE_LIST. We can handle it exactly the same as an array section
17191 (purpose, value, and a chain), even though the nomenclature
17192 (low_bound, length, etc) is different. */
17193 if (TREE_CODE (decl
) == TREE_LIST
)
17196 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17197 /*integral_constant_expression_p=*/false);
17198 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17199 /*integral_constant_expression_p=*/false);
17200 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17202 if (TREE_PURPOSE (decl
) == low_bound
17203 && TREE_VALUE (decl
) == length
17204 && TREE_CHAIN (decl
) == chain
)
17206 tree ret
= tree_cons (low_bound
, length
, chain
);
17207 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17208 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17211 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17212 /*integral_constant_expression_p=*/false);
17213 /* Undo convert_from_reference tsubst_expr could have called. */
17215 && REFERENCE_REF_P (ret
)
17216 && !REFERENCE_REF_P (decl
))
17217 ret
= TREE_OPERAND (ret
, 0);
17221 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17224 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17225 tree args
, tsubst_flags_t complain
, tree in_decl
)
17227 tree new_clauses
= NULL_TREE
, nc
, oc
;
17228 tree linear_no_step
= NULL_TREE
;
17229 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17231 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17233 nc
= copy_node (oc
);
17234 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17237 switch (OMP_CLAUSE_CODE (nc
))
17239 case OMP_CLAUSE_LASTPRIVATE
:
17240 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17242 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17243 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17244 in_decl
, /*integral_constant_expression_p=*/false);
17245 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17246 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17249 case OMP_CLAUSE_PRIVATE
:
17250 case OMP_CLAUSE_SHARED
:
17251 case OMP_CLAUSE_FIRSTPRIVATE
:
17252 case OMP_CLAUSE_COPYIN
:
17253 case OMP_CLAUSE_COPYPRIVATE
:
17254 case OMP_CLAUSE_UNIFORM
:
17255 case OMP_CLAUSE_DEPEND
:
17256 case OMP_CLAUSE_FROM
:
17257 case OMP_CLAUSE_TO
:
17258 case OMP_CLAUSE_MAP
:
17259 case OMP_CLAUSE_NONTEMPORAL
:
17260 case OMP_CLAUSE_USE_DEVICE_PTR
:
17261 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17262 case OMP_CLAUSE_IS_DEVICE_PTR
:
17263 case OMP_CLAUSE_INCLUSIVE
:
17264 case OMP_CLAUSE_EXCLUSIVE
:
17265 OMP_CLAUSE_DECL (nc
)
17266 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17267 in_decl
, iterator_cache
);
17269 case OMP_CLAUSE_TILE
:
17270 case OMP_CLAUSE_IF
:
17271 case OMP_CLAUSE_NUM_THREADS
:
17272 case OMP_CLAUSE_SCHEDULE
:
17273 case OMP_CLAUSE_COLLAPSE
:
17274 case OMP_CLAUSE_FINAL
:
17275 case OMP_CLAUSE_DEVICE
:
17276 case OMP_CLAUSE_DIST_SCHEDULE
:
17277 case OMP_CLAUSE_NUM_TEAMS
:
17278 case OMP_CLAUSE_THREAD_LIMIT
:
17279 case OMP_CLAUSE_SAFELEN
:
17280 case OMP_CLAUSE_SIMDLEN
:
17281 case OMP_CLAUSE_NUM_TASKS
:
17282 case OMP_CLAUSE_GRAINSIZE
:
17283 case OMP_CLAUSE_PRIORITY
:
17284 case OMP_CLAUSE_ORDERED
:
17285 case OMP_CLAUSE_HINT
:
17286 case OMP_CLAUSE_NUM_GANGS
:
17287 case OMP_CLAUSE_NUM_WORKERS
:
17288 case OMP_CLAUSE_VECTOR_LENGTH
:
17289 case OMP_CLAUSE_WORKER
:
17290 case OMP_CLAUSE_VECTOR
:
17291 case OMP_CLAUSE_ASYNC
:
17292 case OMP_CLAUSE_WAIT
:
17293 OMP_CLAUSE_OPERAND (nc
, 0)
17294 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17295 in_decl
, /*integral_constant_expression_p=*/false);
17297 case OMP_CLAUSE_REDUCTION
:
17298 case OMP_CLAUSE_IN_REDUCTION
:
17299 case OMP_CLAUSE_TASK_REDUCTION
:
17300 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17302 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17303 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17305 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17306 complain
, in_decl
);
17307 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17308 = build_qualified_name (NULL_TREE
, scope
,
17309 TREE_OPERAND (placeholder
, 1),
17313 gcc_assert (identifier_p (placeholder
));
17315 OMP_CLAUSE_DECL (nc
)
17316 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17319 case OMP_CLAUSE_GANG
:
17320 case OMP_CLAUSE_ALIGNED
:
17321 case OMP_CLAUSE_ALLOCATE
:
17322 OMP_CLAUSE_DECL (nc
)
17323 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17325 OMP_CLAUSE_OPERAND (nc
, 1)
17326 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17327 in_decl
, /*integral_constant_expression_p=*/false);
17329 case OMP_CLAUSE_LINEAR
:
17330 OMP_CLAUSE_DECL (nc
)
17331 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17333 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17335 gcc_assert (!linear_no_step
);
17336 linear_no_step
= nc
;
17338 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17339 OMP_CLAUSE_LINEAR_STEP (nc
)
17340 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17341 complain
, in_decl
, NULL
);
17343 OMP_CLAUSE_LINEAR_STEP (nc
)
17344 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17346 /*integral_constant_expression_p=*/false);
17348 case OMP_CLAUSE_NOWAIT
:
17349 case OMP_CLAUSE_DEFAULT
:
17350 case OMP_CLAUSE_UNTIED
:
17351 case OMP_CLAUSE_MERGEABLE
:
17352 case OMP_CLAUSE_INBRANCH
:
17353 case OMP_CLAUSE_NOTINBRANCH
:
17354 case OMP_CLAUSE_PROC_BIND
:
17355 case OMP_CLAUSE_FOR
:
17356 case OMP_CLAUSE_PARALLEL
:
17357 case OMP_CLAUSE_SECTIONS
:
17358 case OMP_CLAUSE_TASKGROUP
:
17359 case OMP_CLAUSE_NOGROUP
:
17360 case OMP_CLAUSE_THREADS
:
17361 case OMP_CLAUSE_SIMD
:
17362 case OMP_CLAUSE_DEFAULTMAP
:
17363 case OMP_CLAUSE_ORDER
:
17364 case OMP_CLAUSE_BIND
:
17365 case OMP_CLAUSE_INDEPENDENT
:
17366 case OMP_CLAUSE_AUTO
:
17367 case OMP_CLAUSE_SEQ
:
17368 case OMP_CLAUSE_IF_PRESENT
:
17369 case OMP_CLAUSE_FINALIZE
:
17372 gcc_unreachable ();
17374 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17375 switch (OMP_CLAUSE_CODE (nc
))
17377 case OMP_CLAUSE_SHARED
:
17378 case OMP_CLAUSE_PRIVATE
:
17379 case OMP_CLAUSE_FIRSTPRIVATE
:
17380 case OMP_CLAUSE_LASTPRIVATE
:
17381 case OMP_CLAUSE_COPYPRIVATE
:
17382 case OMP_CLAUSE_LINEAR
:
17383 case OMP_CLAUSE_REDUCTION
:
17384 case OMP_CLAUSE_IN_REDUCTION
:
17385 case OMP_CLAUSE_TASK_REDUCTION
:
17386 case OMP_CLAUSE_USE_DEVICE_PTR
:
17387 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17388 case OMP_CLAUSE_IS_DEVICE_PTR
:
17389 case OMP_CLAUSE_INCLUSIVE
:
17390 case OMP_CLAUSE_EXCLUSIVE
:
17391 case OMP_CLAUSE_ALLOCATE
:
17392 /* tsubst_expr on SCOPE_REF results in returning
17393 finish_non_static_data_member result. Undo that here. */
17394 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17395 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17396 == IDENTIFIER_NODE
))
17398 tree t
= OMP_CLAUSE_DECL (nc
);
17401 switch (TREE_CODE (v
))
17403 case COMPONENT_REF
:
17407 case POINTER_PLUS_EXPR
:
17408 v
= TREE_OPERAND (v
, 0);
17411 if (DECL_CONTEXT (v
) == current_function_decl
17412 && DECL_ARTIFICIAL (v
)
17413 && DECL_NAME (v
) == this_identifier
)
17414 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17421 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17422 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17423 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17424 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17425 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17427 tree decl
= OMP_CLAUSE_DECL (nc
);
17430 retrofit_lang_decl (decl
);
17431 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17440 new_clauses
= nreverse (new_clauses
);
17441 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17443 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17444 if (linear_no_step
)
17445 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17446 if (nc
== linear_no_step
)
17448 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17452 return new_clauses
;
17455 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17458 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17461 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17463 tree purpose
, value
, chain
;
17468 if (TREE_CODE (t
) != TREE_LIST
)
17469 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17470 /*function_p=*/false,
17471 /*integral_constant_expression_p=*/false);
17473 if (t
== void_list_node
)
17476 purpose
= TREE_PURPOSE (t
);
17478 purpose
= RECUR (purpose
);
17479 value
= TREE_VALUE (t
);
17482 if (TREE_CODE (value
) != LABEL_DECL
)
17483 value
= RECUR (value
);
17486 value
= lookup_label (DECL_NAME (value
));
17487 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17488 TREE_USED (value
) = 1;
17491 chain
= TREE_CHAIN (t
);
17492 if (chain
&& chain
!= void_type_node
)
17493 chain
= RECUR (chain
);
17494 return tree_cons (purpose
, value
, chain
);
17498 /* Used to temporarily communicate the list of #pragma omp parallel
17499 clauses to #pragma omp for instantiation if they are combined
17502 static tree
*omp_parallel_combined_clauses
;
17504 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17505 tree
*, unsigned int *);
17507 /* Substitute one OMP_FOR iterator. */
17510 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17511 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17512 tree args
, tsubst_flags_t complain
, tree in_decl
,
17513 bool integral_constant_expression_p
)
17515 #define RECUR(NODE) \
17516 tsubst_expr ((NODE), args, complain, in_decl, \
17517 integral_constant_expression_p)
17518 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17521 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17522 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17524 decl
= TREE_OPERAND (init
, 0);
17525 init
= TREE_OPERAND (init
, 1);
17526 tree decl_expr
= NULL_TREE
;
17527 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17530 bool decomp
= false;
17531 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17533 tree v
= DECL_VALUE_EXPR (decl
);
17534 if (TREE_CODE (v
) == ARRAY_REF
17535 && VAR_P (TREE_OPERAND (v
, 0))
17536 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17538 tree decomp_first
= NULL_TREE
;
17539 unsigned decomp_cnt
= 0;
17540 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17541 maybe_push_decl (d
);
17542 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17543 in_decl
, &decomp_first
, &decomp_cnt
);
17545 if (d
== error_mark_node
)
17546 decl
= error_mark_node
;
17548 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17550 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17552 tree v
= build_nt (ARRAY_REF
, d
,
17553 size_int (decomp_cnt
- i
- 1),
17554 NULL_TREE
, NULL_TREE
);
17555 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17556 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17558 fit_decomposition_lang_decl (decomp_first
, d
);
17559 decomp_first
= DECL_CHAIN (decomp_first
);
17563 decl
= tsubst_decl (decl
, args
, complain
);
17565 maybe_push_decl (decl
);
17567 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17569 /* We need to jump through some hoops to handle declarations in the
17570 init-statement, since we might need to handle auto deduction,
17571 but we need to keep control of initialization. */
17573 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17574 decl
= tsubst_decl (decl
, args
, complain
);
17578 if (TREE_CODE (decl
) == SCOPE_REF
)
17580 decl
= RECUR (decl
);
17581 if (TREE_CODE (decl
) == COMPONENT_REF
)
17585 switch (TREE_CODE (v
))
17587 case COMPONENT_REF
:
17591 case POINTER_PLUS_EXPR
:
17592 v
= TREE_OPERAND (v
, 0);
17595 if (DECL_CONTEXT (v
) == current_function_decl
17596 && DECL_ARTIFICIAL (v
)
17597 && DECL_NAME (v
) == this_identifier
)
17599 decl
= TREE_OPERAND (decl
, 1);
17600 decl
= omp_privatize_field (decl
, false);
17610 decl
= RECUR (decl
);
17612 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17614 init
= copy_node (init
);
17615 TREE_VEC_ELT (init
, 0)
17616 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17617 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17618 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17621 init
= RECUR (init
);
17623 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17625 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17626 if (TREE_CODE (o
) == TREE_LIST
)
17627 TREE_VEC_ELT (orig_declv
, i
)
17628 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17629 RECUR (TREE_VALUE (o
)),
17632 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17637 tree this_pre_body
= NULL_TREE
;
17638 tree orig_init
= NULL_TREE
;
17639 tree orig_decl
= NULL_TREE
;
17640 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17641 orig_init
, cond
, incr
);
17644 if (orig_declv
== NULL_TREE
)
17645 orig_declv
= copy_node (declv
);
17646 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17649 else if (orig_declv
)
17650 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17653 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17654 if (!range_for
&& auto_node
&& init
)
17656 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17658 gcc_assert (!type_dependent_expression_p (decl
));
17660 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17664 /* Declare the variable, but don't let that initialize it. */
17665 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17666 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17668 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17673 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17674 if (COMPARISON_CLASS_P (cond
)
17675 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17677 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17678 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17679 TREE_VEC_ELT (rhs
, 0)
17680 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17681 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17682 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17683 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17687 cond
= RECUR (cond
);
17688 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17689 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17691 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17692 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17693 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17694 NOP_EXPR
, rhs
, complain
);
17697 incr
= RECUR (incr
);
17698 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17699 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17701 TREE_VEC_ELT (declv
, i
) = decl
;
17702 TREE_VEC_ELT (initv
, i
) = init
;
17703 TREE_VEC_ELT (condv
, i
) = cond
;
17704 TREE_VEC_ELT (incrv
, i
) = incr
;
17710 /* Declare and initialize the variable. */
17718 for (j
= ((omp_parallel_combined_clauses
== NULL
17719 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17721 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17723 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17724 && OMP_CLAUSE_DECL (*pc
) == decl
)
17726 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17727 && OMP_CLAUSE_DECL (*pc
) == decl
)
17731 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17733 *pc
= OMP_CLAUSE_CHAIN (c
);
17734 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17737 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17738 && OMP_CLAUSE_DECL (*pc
) == decl
)
17740 error ("iteration variable %qD should not be firstprivate",
17742 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17744 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17745 && OMP_CLAUSE_DECL (*pc
) == decl
)
17747 error ("iteration variable %qD should not be reduction",
17749 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17752 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17757 if (*pc
== NULL_TREE
)
17759 tree c
= build_omp_clause (input_location
,
17760 TREE_CODE (t
) == OMP_LOOP
17761 ? OMP_CLAUSE_LASTPRIVATE
17762 : OMP_CLAUSE_PRIVATE
);
17763 OMP_CLAUSE_DECL (c
) = decl
;
17764 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17767 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17772 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17773 if (COMPARISON_CLASS_P (cond
))
17775 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17776 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17777 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17780 cond
= RECUR (cond
);
17781 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17782 switch (TREE_CODE (incr
))
17784 case PREINCREMENT_EXPR
:
17785 case PREDECREMENT_EXPR
:
17786 case POSTINCREMENT_EXPR
:
17787 case POSTDECREMENT_EXPR
:
17788 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17789 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17792 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17793 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17795 tree rhs
= TREE_OPERAND (incr
, 1);
17796 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17797 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17798 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17799 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17800 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17804 incr
= RECUR (incr
);
17807 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17808 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17810 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17811 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17812 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17813 TREE_TYPE (decl
), lhs
,
17814 RECUR (TREE_OPERAND (incr
, 2))));
17816 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17817 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17818 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17820 tree rhs
= TREE_OPERAND (incr
, 2);
17821 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17822 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17823 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17824 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17825 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17829 incr
= RECUR (incr
);
17832 incr
= RECUR (incr
);
17836 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17837 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17838 TREE_VEC_ELT (declv
, i
) = decl
;
17839 TREE_VEC_ELT (initv
, i
) = init
;
17840 TREE_VEC_ELT (condv
, i
) = cond
;
17841 TREE_VEC_ELT (incrv
, i
) = incr
;
17846 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17847 of OMP_TARGET's body. */
17850 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17852 *walk_subtrees
= 0;
17853 switch (TREE_CODE (*tp
))
17858 case STATEMENT_LIST
:
17859 *walk_subtrees
= 1;
17867 /* Helper function for tsubst_expr. For decomposition declaration
17868 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17869 also the corresponding decls representing the identifiers
17870 of the decomposition declaration. Return DECL if successful
17871 or error_mark_node otherwise, set *FIRST to the first decl
17872 in the list chained through DECL_CHAIN and *CNT to the number
17876 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17877 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17880 tree decl2
, decl3
, prev
= decl
;
17882 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17883 for (decl2
= DECL_CHAIN (pattern_decl
);
17886 && DECL_DECOMPOSITION_P (decl2
)
17887 && DECL_NAME (decl2
);
17888 decl2
= DECL_CHAIN (decl2
))
17890 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17892 gcc_assert (errorcount
);
17893 return error_mark_node
;
17896 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17897 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17898 tree v
= DECL_VALUE_EXPR (decl2
);
17899 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17900 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17901 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17902 SET_DECL_VALUE_EXPR (decl2
, v
);
17903 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17905 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17908 gcc_assert (errorcount
);
17909 decl
= error_mark_node
;
17912 maybe_push_decl (decl3
);
17913 if (error_operand_p (decl3
))
17914 decl
= error_mark_node
;
17915 else if (decl
!= error_mark_node
17916 && DECL_CHAIN (decl3
) != prev
17919 gcc_assert (errorcount
);
17920 decl
= error_mark_node
;
17929 /* Return the proper local_specialization for init-capture pack DECL. */
17932 lookup_init_capture_pack (tree decl
)
17934 /* We handle normal pack captures by forwarding to the specialization of the
17935 captured parameter. We can't do that for pack init-captures; we need them
17936 to have their own local_specialization. We created the individual
17937 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17938 when we process the DECL_EXPR for the pack init-capture in the template.
17939 So, how do we find them? We don't know the capture proxy pack when
17940 building the individual resulting proxies, and we don't know the
17941 individual proxies when instantiating the pack. What we have in common is
17944 So...when we instantiate the FIELD_DECL, we stick the result in
17945 local_specializations. Then at the DECL_EXPR we look up that result, see
17946 how many elements it has, synthesize the names, and look them up. */
17948 tree cname
= DECL_NAME (decl
);
17949 tree val
= DECL_VALUE_EXPR (decl
);
17950 tree field
= TREE_OPERAND (val
, 1);
17951 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17952 tree fpack
= retrieve_local_specialization (field
);
17953 if (fpack
== error_mark_node
)
17954 return error_mark_node
;
17957 tree vec
= NULL_TREE
;
17958 tree r
= NULL_TREE
;
17959 if (TREE_CODE (fpack
) == TREE_VEC
)
17961 len
= TREE_VEC_LENGTH (fpack
);
17962 vec
= make_tree_vec (len
);
17963 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17964 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17966 for (int i
= 0; i
< len
; ++i
)
17968 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17969 tree elt
= lookup_name (ename
);
17971 TREE_VEC_ELT (vec
, i
) = elt
;
17978 /* Like tsubst_copy for expressions, etc. but also does semantic
17982 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17983 bool integral_constant_expression_p
)
17985 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17986 #define RECUR(NODE) \
17987 tsubst_expr ((NODE), args, complain, in_decl, \
17988 integral_constant_expression_p)
17994 if (t
== NULL_TREE
|| t
== error_mark_node
)
17997 loc
= input_location
;
17998 if (location_t eloc
= cp_expr_location (t
))
17999 input_location
= eloc
;
18000 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18001 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18003 switch (TREE_CODE (t
))
18005 case STATEMENT_LIST
:
18007 tree_stmt_iterator i
;
18008 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
18009 RECUR (tsi_stmt (i
));
18013 case CTOR_INITIALIZER
:
18014 finish_mem_initializers (tsubst_initializer_list
18015 (TREE_OPERAND (t
, 0), args
));
18019 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18022 case CO_RETURN_EXPR
:
18023 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18026 case CO_YIELD_EXPR
:
18027 stmt
= finish_co_yield_expr (input_location
,
18028 RECUR (TREE_OPERAND (t
, 0)));
18032 case CO_AWAIT_EXPR
:
18033 stmt
= finish_co_await_expr (input_location
,
18034 RECUR (TREE_OPERAND (t
, 0)));
18039 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18040 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18041 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18043 finish_expr_stmt (tmp
);
18047 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18052 tree decl
, pattern_decl
;
18055 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18056 if (TREE_CODE (decl
) == LABEL_DECL
)
18057 finish_label_decl (DECL_NAME (decl
));
18058 else if (TREE_CODE (decl
) == USING_DECL
)
18060 /* We cannot have a member-using decl here (until 'using
18061 enum T' is a thing). */
18062 gcc_checking_assert (!DECL_DEPENDENT_P (decl
));
18064 /* This must be a non-dependent using-decl, and we'll have
18065 used the names it found during template parsing. We do
18066 not want to do the lookup again, because we might not
18067 find the things we found then. (Again, using enum T
18068 might mean we have to do things here.) */
18069 tree scope
= USING_DECL_SCOPE (decl
);
18070 gcc_checking_assert (scope
18071 == tsubst (scope
, args
, complain
, in_decl
));
18073 else if (is_capture_proxy (decl
)
18074 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18076 /* We're in tsubst_lambda_expr, we've already inserted a new
18077 capture proxy, so look it up and register it. */
18079 if (!DECL_PACK_P (decl
))
18081 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18082 LOOK_want::HIDDEN_LAMBDA
);
18083 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18085 else if (is_normal_capture_proxy (decl
))
18087 inst
= (retrieve_local_specialization
18088 (DECL_CAPTURED_VARIABLE (decl
)));
18089 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18090 || DECL_PACK_P (inst
));
18093 inst
= lookup_init_capture_pack (decl
);
18095 register_local_specialization (inst
, decl
);
18098 else if (DECL_PRETTY_FUNCTION_P (decl
))
18099 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18101 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18102 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18103 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18104 /* Don't copy the old closure; we'll create a new one in
18105 tsubst_lambda_expr. */
18109 init
= DECL_INITIAL (decl
);
18110 decl
= tsubst (decl
, args
, complain
, in_decl
);
18111 if (decl
!= error_mark_node
)
18113 /* By marking the declaration as instantiated, we avoid
18114 trying to instantiate it. Since instantiate_decl can't
18115 handle local variables, and since we've already done
18116 all that needs to be done, that's the right thing to
18119 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18120 if (VAR_P (decl
) && !DECL_NAME (decl
)
18121 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18122 /* Anonymous aggregates are a special case. */
18123 finish_anon_union (decl
);
18124 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18126 DECL_CONTEXT (decl
) = current_function_decl
;
18127 if (DECL_NAME (decl
) == this_identifier
)
18129 tree lam
= DECL_CONTEXT (current_function_decl
);
18130 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18131 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18133 insert_capture_proxy (decl
);
18135 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18136 /* We already did a pushtag. */;
18137 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18138 && DECL_LOCAL_DECL_P (decl
))
18140 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18141 DECL_CONTEXT (decl
) = NULL_TREE
;
18142 decl
= pushdecl (decl
);
18143 if (TREE_CODE (decl
) == FUNCTION_DECL
18144 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18145 && cp_check_omp_declare_reduction (decl
))
18146 instantiate_body (pattern_decl
, args
, decl
, true);
18150 bool const_init
= false;
18151 unsigned int cnt
= 0;
18152 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18153 maybe_push_decl (decl
);
18156 && DECL_DECOMPOSITION_P (decl
)
18157 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18158 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18159 complain
, in_decl
, &first
,
18162 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18165 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18168 if (ndecl
!= error_mark_node
)
18169 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18171 /* In a non-template function, VLA type declarations are
18172 handled in grokdeclarator; for templates, handle them
18174 predeclare_vla (decl
);
18176 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
18178 if (ndecl
!= error_mark_node
)
18179 cp_finish_decomp (ndecl
, first
, cnt
);
18188 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18189 RECUR (FOR_INIT_STMT (t
));
18190 finish_init_stmt (stmt
);
18191 tmp
= RECUR (FOR_COND (t
));
18192 finish_for_cond (tmp
, stmt
, false, 0);
18193 tmp
= RECUR (FOR_EXPR (t
));
18194 finish_for_expr (tmp
, stmt
);
18196 bool prev
= note_iteration_stmt_body_start ();
18197 RECUR (FOR_BODY (t
));
18198 note_iteration_stmt_body_end (prev
);
18200 finish_for_stmt (stmt
);
18203 case RANGE_FOR_STMT
:
18205 /* Construct another range_for, if this is not a final
18206 substitution (for inside a generic lambda of a
18207 template). Otherwise convert to a regular for. */
18209 stmt
= (processing_template_decl
18210 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18211 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18212 RECUR (RANGE_FOR_INIT_STMT (t
));
18213 decl
= RANGE_FOR_DECL (t
);
18214 decl
= tsubst (decl
, args
, complain
, in_decl
);
18215 maybe_push_decl (decl
);
18216 expr
= RECUR (RANGE_FOR_EXPR (t
));
18218 tree decomp_first
= NULL_TREE
;
18219 unsigned decomp_cnt
= 0;
18220 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18221 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18223 &decomp_first
, &decomp_cnt
);
18225 if (processing_template_decl
)
18227 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18228 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18229 finish_range_for_decl (stmt
, decl
, expr
);
18230 if (decomp_first
&& decl
!= error_mark_node
)
18231 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18235 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18236 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18237 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18238 decomp_first
, decomp_cnt
,
18239 RANGE_FOR_IVDEP (t
), unroll
);
18242 bool prev
= note_iteration_stmt_body_start ();
18243 RECUR (RANGE_FOR_BODY (t
));
18244 note_iteration_stmt_body_end (prev
);
18245 finish_for_stmt (stmt
);
18250 stmt
= begin_while_stmt ();
18251 tmp
= RECUR (WHILE_COND (t
));
18252 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18254 bool prev
= note_iteration_stmt_body_start ();
18255 RECUR (WHILE_BODY (t
));
18256 note_iteration_stmt_body_end (prev
);
18258 finish_while_stmt (stmt
);
18262 stmt
= begin_do_stmt ();
18264 bool prev
= note_iteration_stmt_body_start ();
18265 RECUR (DO_BODY (t
));
18266 note_iteration_stmt_body_end (prev
);
18268 finish_do_body (stmt
);
18269 tmp
= RECUR (DO_COND (t
));
18270 finish_do_stmt (tmp
, stmt
, false, 0);
18274 stmt
= begin_if_stmt ();
18275 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18276 if (IF_STMT_CONSTEXPR_P (t
))
18277 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18278 tmp
= RECUR (IF_COND (t
));
18279 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18280 if (IF_STMT_CONSTEXPR_P (t
)
18281 && instantiation_dependent_expression_p (tmp
))
18283 /* We're partially instantiating a generic lambda, but the condition
18284 of the constexpr if is still dependent. Don't substitute into the
18285 branches now, just remember the template arguments. */
18286 do_poplevel (IF_SCOPE (stmt
));
18287 IF_COND (stmt
) = IF_COND (t
);
18288 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18289 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18290 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18294 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18295 /* Don't instantiate the THEN_CLAUSE. */;
18298 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18299 bool inhibit
= integer_zerop (folded
);
18301 ++c_inhibit_evaluation_warnings
;
18302 RECUR (THEN_CLAUSE (t
));
18304 --c_inhibit_evaluation_warnings
;
18306 finish_then_clause (stmt
);
18308 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18309 /* Don't instantiate the ELSE_CLAUSE. */;
18310 else if (ELSE_CLAUSE (t
))
18312 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18313 bool inhibit
= integer_nonzerop (folded
);
18314 begin_else_clause (stmt
);
18316 ++c_inhibit_evaluation_warnings
;
18317 RECUR (ELSE_CLAUSE (t
));
18319 --c_inhibit_evaluation_warnings
;
18320 finish_else_clause (stmt
);
18323 finish_if_stmt (stmt
);
18327 if (BIND_EXPR_BODY_BLOCK (t
))
18328 stmt
= begin_function_body ();
18330 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18331 ? BCS_TRY_BLOCK
: 0);
18333 RECUR (BIND_EXPR_BODY (t
));
18335 if (BIND_EXPR_BODY_BLOCK (t
))
18336 finish_function_body (stmt
);
18338 finish_compound_stmt (stmt
);
18342 finish_break_stmt ();
18345 case CONTINUE_STMT
:
18346 finish_continue_stmt ();
18350 stmt
= begin_switch_stmt ();
18351 tmp
= RECUR (SWITCH_STMT_COND (t
));
18352 finish_switch_cond (tmp
, stmt
);
18353 RECUR (SWITCH_STMT_BODY (t
));
18354 finish_switch_stmt (stmt
);
18357 case CASE_LABEL_EXPR
:
18359 tree decl
= CASE_LABEL (t
);
18360 tree low
= RECUR (CASE_LOW (t
));
18361 tree high
= RECUR (CASE_HIGH (t
));
18362 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18363 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18365 tree label
= CASE_LABEL (l
);
18366 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18367 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18368 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18375 tree decl
= LABEL_EXPR_LABEL (t
);
18378 label
= finish_label_stmt (DECL_NAME (decl
));
18379 if (TREE_CODE (label
) == LABEL_DECL
)
18380 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18381 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18382 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18387 tmp
= GOTO_DESTINATION (t
);
18388 if (TREE_CODE (tmp
) != LABEL_DECL
)
18389 /* Computed goto's must be tsubst'd into. On the other hand,
18390 non-computed gotos must not be; the identifier in question
18391 will have no binding. */
18394 tmp
= DECL_NAME (tmp
);
18395 finish_goto_stmt (tmp
);
18400 tree string
= RECUR (ASM_STRING (t
));
18401 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18402 complain
, in_decl
);
18403 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18404 complain
, in_decl
);
18405 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18406 complain
, in_decl
);
18407 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18408 complain
, in_decl
);
18409 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18410 outputs
, inputs
, clobbers
, labels
,
18412 tree asm_expr
= tmp
;
18413 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18414 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18415 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18422 stmt
= begin_try_block ();
18423 RECUR (TRY_STMTS (t
));
18424 finish_cleanup_try_block (stmt
);
18425 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18429 tree compound_stmt
= NULL_TREE
;
18431 if (FN_TRY_BLOCK_P (t
))
18432 stmt
= begin_function_try_block (&compound_stmt
);
18434 stmt
= begin_try_block ();
18436 RECUR (TRY_STMTS (t
));
18438 if (FN_TRY_BLOCK_P (t
))
18439 finish_function_try_block (stmt
);
18441 finish_try_block (stmt
);
18443 RECUR (TRY_HANDLERS (t
));
18444 if (FN_TRY_BLOCK_P (t
))
18445 finish_function_handler_sequence (stmt
, compound_stmt
);
18447 finish_handler_sequence (stmt
);
18453 tree decl
= HANDLER_PARMS (t
);
18457 decl
= tsubst (decl
, args
, complain
, in_decl
);
18458 /* Prevent instantiate_decl from trying to instantiate
18459 this variable. We've already done all that needs to be
18461 if (decl
!= error_mark_node
)
18462 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18464 stmt
= begin_handler ();
18465 finish_handler_parms (decl
, stmt
);
18466 RECUR (HANDLER_BODY (t
));
18467 finish_handler (stmt
);
18472 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18473 if (CLASS_TYPE_P (tmp
))
18475 /* Local classes are not independent templates; they are
18476 instantiated along with their containing function. And this
18477 way we don't have to deal with pushing out of one local class
18478 to instantiate a member of another local class. */
18479 /* Closures are handled by the LAMBDA_EXPR. */
18480 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18481 complete_type (tmp
);
18482 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18484 || (TREE_CODE (fld
) == FUNCTION_DECL
18485 && !DECL_ARTIFICIAL (fld
)))
18486 && DECL_TEMPLATE_INSTANTIATION (fld
))
18487 instantiate_decl (fld
, /*defer_ok=*/false,
18488 /*expl_inst_class=*/false);
18492 case STATIC_ASSERT
:
18496 ++c_inhibit_evaluation_warnings
;
18498 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18501 /*integral_constant_expression_p=*/true);
18502 --c_inhibit_evaluation_warnings
;
18504 finish_static_assert (condition
,
18505 STATIC_ASSERT_MESSAGE (t
),
18506 STATIC_ASSERT_SOURCE_LOCATION (t
),
18507 /*member_p=*/false, /*show_expr_p=*/true);
18512 case OACC_PARALLEL
:
18514 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18516 stmt
= begin_omp_parallel ();
18517 RECUR (OMP_BODY (t
));
18518 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18522 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18523 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18524 complain
, in_decl
);
18525 if (OMP_PARALLEL_COMBINED (t
))
18526 omp_parallel_combined_clauses
= &tmp
;
18527 stmt
= begin_omp_parallel ();
18528 RECUR (OMP_PARALLEL_BODY (t
));
18529 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18530 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18531 = OMP_PARALLEL_COMBINED (t
);
18532 pop_omp_privatization_clauses (r
);
18536 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18538 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18539 complain
, in_decl
);
18541 OMP_TASK_CLAUSES (t
) = tmp
;
18545 r
= push_omp_privatization_clauses (false);
18546 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18547 complain
, in_decl
);
18548 stmt
= begin_omp_task ();
18549 RECUR (OMP_TASK_BODY (t
));
18550 finish_omp_task (tmp
, stmt
);
18551 pop_omp_privatization_clauses (r
);
18557 case OMP_DISTRIBUTE
:
18561 tree clauses
, body
, pre_body
;
18562 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18563 tree orig_declv
= NULL_TREE
;
18564 tree incrv
= NULL_TREE
;
18565 enum c_omp_region_type ort
= C_ORT_OMP
;
18566 bool any_range_for
= false;
18569 if (TREE_CODE (t
) == OACC_LOOP
)
18572 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18573 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18575 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18577 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18578 if (OMP_FOR_ORIG_DECLS (t
))
18579 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18580 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18581 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18582 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18585 keep_next_level (true);
18586 stmt
= begin_omp_structured_block ();
18588 pre_body
= push_stmt_list ();
18589 RECUR (OMP_FOR_PRE_BODY (t
));
18590 pre_body
= pop_stmt_list (pre_body
);
18592 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18593 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18595 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18596 condv
, incrv
, &clauses
, args
,
18598 integral_constant_expression_p
);
18599 omp_parallel_combined_clauses
= NULL
;
18603 gcc_assert (orig_declv
);
18604 body
= begin_omp_structured_block ();
18605 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18606 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18607 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18608 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18609 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18610 TREE_VEC_ELT (declv
, i
));
18613 body
= push_stmt_list ();
18614 RECUR (OMP_FOR_BODY (t
));
18616 body
= finish_omp_structured_block (body
);
18618 body
= pop_stmt_list (body
);
18620 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18621 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18622 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18626 t
= make_node (TREE_CODE (t
));
18627 TREE_TYPE (t
) = void_type_node
;
18628 OMP_FOR_BODY (t
) = body
;
18629 OMP_FOR_PRE_BODY (t
) = pre_body
;
18630 OMP_FOR_CLAUSES (t
) = clauses
;
18631 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18635 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18637 pop_omp_privatization_clauses (r
);
18642 omp_parallel_combined_clauses
= NULL
;
18647 case OMP_TASKGROUP
:
18649 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18650 && OMP_TEAMS_COMBINED (t
));
18651 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18653 if (TREE_CODE (t
) == OMP_TEAMS
)
18655 keep_next_level (true);
18656 stmt
= begin_omp_structured_block ();
18657 RECUR (OMP_BODY (t
));
18658 stmt
= finish_omp_structured_block (stmt
);
18662 stmt
= push_stmt_list ();
18663 RECUR (OMP_BODY (t
));
18664 stmt
= pop_stmt_list (stmt
);
18667 if (TREE_CODE (t
) == OMP_CRITICAL
18668 && tmp
!= NULL_TREE
18669 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18671 error_at (OMP_CLAUSE_LOCATION (tmp
),
18672 "%<#pragma omp critical%> with %<hint%> clause requires "
18673 "a name, except when %<omp_sync_hint_none%> is used");
18674 RETURN (error_mark_node
);
18677 OMP_BODY (t
) = stmt
;
18678 OMP_CLAUSES (t
) = tmp
;
18680 pop_omp_privatization_clauses (r
);
18684 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18685 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18687 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18688 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18690 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18691 args
, complain
, in_decl
);
18692 if (tmp
== NULL_TREE
)
18693 tmp
= error_mark_node
;
18697 kind
= (enum omp_clause_depend_kind
)
18698 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18701 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18704 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18705 OMP_CLAUSE_DEPEND_SOURCE
,
18706 OMP_DEPOBJ_CLAUSES (t
));
18710 case OMP_TARGET_DATA
:
18712 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18713 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18715 keep_next_level (true);
18716 stmt
= begin_omp_structured_block ();
18718 RECUR (OMP_BODY (t
));
18719 stmt
= finish_omp_structured_block (stmt
);
18722 OMP_BODY (t
) = stmt
;
18723 OMP_CLAUSES (t
) = tmp
;
18724 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18726 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18729 /* For combined target teams, ensure the num_teams and
18730 thread_limit clause expressions are evaluated on the host,
18731 before entering the target construct. */
18733 for (c
= OMP_TEAMS_CLAUSES (teams
);
18734 c
; c
= OMP_CLAUSE_CHAIN (c
))
18735 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18736 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18737 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18739 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18740 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18741 if (expr
== error_mark_node
)
18743 tmp
= TARGET_EXPR_SLOT (expr
);
18745 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18746 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18747 OMP_CLAUSE_FIRSTPRIVATE
);
18748 OMP_CLAUSE_DECL (tc
) = tmp
;
18749 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18750 OMP_TARGET_CLAUSES (t
) = tc
;
18759 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18760 complain
, in_decl
);
18761 OACC_DECLARE_CLAUSES (t
) = tmp
;
18765 case OMP_TARGET_UPDATE
:
18766 case OMP_TARGET_ENTER_DATA
:
18767 case OMP_TARGET_EXIT_DATA
:
18768 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18769 complain
, in_decl
);
18771 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18775 case OACC_ENTER_DATA
:
18776 case OACC_EXIT_DATA
:
18778 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18779 complain
, in_decl
);
18781 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18786 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18787 complain
, in_decl
);
18788 stmt
= push_stmt_list ();
18789 RECUR (OMP_BODY (t
));
18790 stmt
= pop_stmt_list (stmt
);
18793 OMP_BODY (t
) = stmt
;
18794 OMP_ORDERED_CLAUSES (t
) = tmp
;
18799 omp_parallel_combined_clauses
= NULL
;
18802 stmt
= push_stmt_list ();
18803 RECUR (OMP_BODY (t
));
18804 stmt
= pop_stmt_list (stmt
);
18807 OMP_BODY (t
) = stmt
;
18812 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18814 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18815 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18816 complain
, in_decl
);
18817 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18819 tree op1
= TREE_OPERAND (t
, 1);
18820 tree rhs1
= NULL_TREE
;
18822 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18824 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18825 op1
= TREE_OPERAND (op1
, 1);
18827 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18828 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18829 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18830 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18831 OMP_ATOMIC_MEMORY_ORDER (t
));
18835 tree op1
= TREE_OPERAND (t
, 1);
18836 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18837 tree rhs1
= NULL_TREE
;
18838 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18839 enum tree_code opcode
= NOP_EXPR
;
18840 if (code
== OMP_ATOMIC_READ
)
18842 v
= RECUR (TREE_OPERAND (op1
, 0));
18843 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18845 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18846 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18848 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18849 v
= RECUR (TREE_OPERAND (op1
, 0));
18850 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18851 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18853 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18854 op11
= TREE_OPERAND (op11
, 1);
18856 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18857 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18858 opcode
= TREE_CODE (op11
);
18859 if (opcode
== MODIFY_EXPR
)
18865 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18866 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18868 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18869 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18873 case TRANSACTION_EXPR
:
18876 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18877 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18879 if (TRANSACTION_EXPR_IS_STMT (t
))
18881 tree body
= TRANSACTION_EXPR_BODY (t
);
18882 tree noex
= NULL_TREE
;
18883 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18885 noex
= MUST_NOT_THROW_COND (body
);
18886 if (noex
== NULL_TREE
)
18887 noex
= boolean_true_node
;
18888 body
= TREE_OPERAND (body
, 0);
18890 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18892 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18896 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18897 RECUR (TRANSACTION_EXPR_BODY (t
)),
18904 case MUST_NOT_THROW_EXPR
:
18906 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18907 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18908 RETURN (build_must_not_throw_expr (op0
, cond
));
18911 case EXPR_PACK_EXPANSION
:
18912 error ("invalid use of pack expansion expression");
18913 RETURN (error_mark_node
);
18915 case NONTYPE_ARGUMENT_PACK
:
18916 error ("use %<...%> to expand argument pack");
18917 RETURN (error_mark_node
);
18919 case COMPOUND_EXPR
:
18920 tmp
= RECUR (TREE_OPERAND (t
, 0));
18921 if (tmp
== NULL_TREE
)
18922 /* If the first operand was a statement, we're done with it. */
18923 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18924 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18925 RECUR (TREE_OPERAND (t
, 1)),
18928 case ANNOTATE_EXPR
:
18929 tmp
= RECUR (TREE_OPERAND (t
, 0));
18930 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18931 TREE_TYPE (tmp
), tmp
,
18932 RECUR (TREE_OPERAND (t
, 1)),
18933 RECUR (TREE_OPERAND (t
, 2))));
18936 RETURN (add_stmt (copy_node (t
)));
18939 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18941 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18942 /*function_p=*/false,
18943 integral_constant_expression_p
));
18946 RETURN (NULL_TREE
);
18948 input_location
= loc
;
18954 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18955 function. For description of the body see comment above
18956 cp_parser_omp_declare_reduction_exprs. */
18959 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18961 if (t
== NULL_TREE
|| t
== error_mark_node
)
18964 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
18966 tree_stmt_iterator tsi
;
18969 memset (stmts
, 0, sizeof stmts
);
18970 for (i
= 0, tsi
= tsi_start (t
);
18971 i
< 7 && !tsi_end_p (tsi
);
18972 i
++, tsi_next (&tsi
))
18973 stmts
[i
] = tsi_stmt (tsi
);
18974 gcc_assert (tsi_end_p (tsi
));
18978 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18979 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18980 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18981 args
, complain
, in_decl
);
18982 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18983 args
, complain
, in_decl
);
18984 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18985 expect to be pushing it. */
18986 DECL_CONTEXT (omp_out
) = current_function_decl
;
18987 DECL_CONTEXT (omp_in
) = current_function_decl
;
18988 keep_next_level (true);
18989 tree block
= begin_omp_structured_block ();
18990 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18991 block
= finish_omp_structured_block (block
);
18992 block
= maybe_cleanup_point_expr_void (block
);
18993 add_decl_expr (omp_out
);
18994 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18995 TREE_NO_WARNING (omp_out
) = 1;
18996 add_decl_expr (omp_in
);
18997 finish_expr_stmt (block
);
19001 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19002 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19003 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19004 args
, complain
, in_decl
);
19005 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19006 args
, complain
, in_decl
);
19007 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19008 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19009 keep_next_level (true);
19010 tree block
= begin_omp_structured_block ();
19011 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
19012 block
= finish_omp_structured_block (block
);
19013 block
= maybe_cleanup_point_expr_void (block
);
19014 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19015 add_decl_expr (omp_priv
);
19016 add_decl_expr (omp_orig
);
19017 finish_expr_stmt (block
);
19019 add_decl_expr (omp_orig
);
19023 /* T is a postfix-expression that is not being used in a function
19024 call. Return the substituted version of T. */
19027 tsubst_non_call_postfix_expression (tree t
, tree args
,
19028 tsubst_flags_t complain
,
19031 if (TREE_CODE (t
) == SCOPE_REF
)
19032 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19033 /*done=*/false, /*address_p=*/false);
19035 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19036 /*function_p=*/false,
19037 /*integral_constant_expression_p=*/false);
19042 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19043 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19044 dependent init-capture. */
19047 prepend_one_capture (tree field
, tree init
, tree
&list
,
19048 tsubst_flags_t complain
)
19050 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19052 tree type
= NULL_TREE
;
19055 if (complain
& tf_error
)
19056 error ("empty initializer in lambda init-capture");
19057 init
= error_mark_node
;
19059 else if (TREE_CODE (init
) == TREE_LIST
)
19060 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19062 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19063 TREE_TYPE (field
) = type
;
19064 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19066 list
= tree_cons (field
, init
, list
);
19069 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19070 instantiation context. Instantiating a pack expansion containing a lambda
19071 might result in multiple lambdas all based on the same lambda in the
19075 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19077 tree oldfn
= lambda_function (t
);
19080 tree r
= build_lambda_expr ();
19082 LAMBDA_EXPR_LOCATION (r
)
19083 = LAMBDA_EXPR_LOCATION (t
);
19084 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19085 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19086 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19087 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19089 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19090 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19092 vec
<tree
,va_gc
>* field_packs
= NULL
;
19094 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19095 cap
= TREE_CHAIN (cap
))
19097 tree ofield
= TREE_PURPOSE (cap
);
19098 tree init
= TREE_VALUE (cap
);
19099 if (PACK_EXPANSION_P (init
))
19100 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19102 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19103 /*fn*/false, /*constexpr*/false);
19105 if (init
== error_mark_node
)
19106 return error_mark_node
;
19108 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19109 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19111 if (!processing_template_decl
19112 && init
&& TREE_CODE (init
) != TREE_VEC
19113 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19115 /* For a VLA, simply tsubsting the field type won't work, we need to
19116 go through add_capture again. XXX do we want to do this for all
19118 tree name
= (get_identifier
19119 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19120 tree ftype
= TREE_TYPE (ofield
);
19121 bool by_ref
= (TYPE_REF_P (ftype
)
19122 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19123 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19124 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19128 if (PACK_EXPANSION_P (ofield
))
19129 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19130 tree field
= tsubst_decl (ofield
, args
, complain
);
19132 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19134 /* Remember these for when we've pushed local_specializations. */
19135 vec_safe_push (field_packs
, ofield
);
19136 vec_safe_push (field_packs
, field
);
19139 if (field
== error_mark_node
)
19140 return error_mark_node
;
19142 if (TREE_CODE (field
) == TREE_VEC
)
19144 int len
= TREE_VEC_LENGTH (field
);
19145 gcc_assert (TREE_CODE (init
) == TREE_VEC
19146 && TREE_VEC_LENGTH (init
) == len
);
19147 for (int i
= 0; i
< len
; ++i
)
19148 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19149 TREE_VEC_ELT (init
, i
),
19150 LAMBDA_EXPR_CAPTURE_LIST (r
),
19155 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19158 if (id_equal (DECL_NAME (field
), "__this"))
19159 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19163 tree type
= begin_lambda_type (r
);
19164 if (type
== error_mark_node
)
19165 return error_mark_node
;
19167 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19168 /* A lambda in a default argument outside a class gets no
19169 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19170 tsubst_default_argument calls start_lambda_scope, so we need to
19171 specifically ignore it here, and use the global scope. */
19172 record_null_lambda_scope (r
);
19174 record_lambda_scope (r
);
19176 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19177 determine_visibility (TYPE_NAME (type
));
19179 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19181 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19182 ? DECL_TI_TEMPLATE (oldfn
)
19185 tree fntype
= static_fn_type (oldfn
);
19187 ++processing_template_decl
;
19188 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19190 --processing_template_decl
;
19192 if (fntype
== error_mark_node
)
19193 r
= error_mark_node
;
19196 /* The body of a lambda-expression is not a subexpression of the
19197 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19198 which would be skipped if cp_unevaluated_operand. */
19201 /* Fix the type of 'this'. */
19202 fntype
= build_memfn_type (fntype
, type
,
19203 type_memfn_quals (fntype
),
19204 type_memfn_rqual (fntype
));
19208 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19209 if (tmpl
== error_mark_node
)
19211 r
= error_mark_node
;
19214 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19215 finish_member_declaration (tmpl
);
19220 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19221 if (fn
== error_mark_node
)
19223 r
= error_mark_node
;
19226 finish_member_declaration (fn
);
19229 if (tree ci
= get_constraints (oldfn
))
19231 /* Substitute into the lambda's constraints. */
19233 ++processing_template_decl
;
19234 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19236 --processing_template_decl
;
19237 set_constraints (fn
, ci
);
19240 /* Let finish_function set this. */
19241 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19243 bool nested
= cfun
;
19245 push_function_context ();
19247 /* Still increment function_depth so that we don't GC in the
19248 middle of an expression. */
19251 local_specialization_stack
s (lss_copy
);
19253 tree body
= start_lambda_function (fn
, r
);
19255 /* Now record them for lookup_init_capture_pack. */
19256 int fplen
= vec_safe_length (field_packs
);
19257 for (int i
= 0; i
< fplen
; )
19259 tree pack
= (*field_packs
)[i
++];
19260 tree inst
= (*field_packs
)[i
++];
19261 register_local_specialization (inst
, pack
);
19263 release_tree_vector (field_packs
);
19265 register_parameter_specializations (oldfn
, fn
);
19269 /* We might not partially instantiate some parts of the function, so
19270 copy these flags from the original template. */
19271 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19272 current_function_returns_value
= ol
->returns_value
;
19273 current_function_returns_null
= ol
->returns_null
;
19274 current_function_returns_abnormally
= ol
->returns_abnormally
;
19275 current_function_infinite_loop
= ol
->infinite_loop
;
19278 /* [temp.deduct] A lambda-expression appearing in a function type or a
19279 template parameter is not considered part of the immediate context for
19280 the purposes of template argument deduction. */
19281 complain
= tf_warning_or_error
;
19283 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19284 /*constexpr*/false);
19286 finish_lambda_function (body
);
19289 pop_function_context ();
19293 /* The capture list was built up in reverse order; fix that now. */
19294 LAMBDA_EXPR_CAPTURE_LIST (r
)
19295 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19297 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19299 maybe_add_lambda_conv_op (type
);
19303 finish_struct (type
, /*attr*/NULL_TREE
);
19305 insert_pending_capture_proxies ();
19310 /* Like tsubst but deals with expressions and performs semantic
19311 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19312 "F<TARGS> (ARGS)". */
19315 tsubst_copy_and_build (tree t
,
19317 tsubst_flags_t complain
,
19320 bool integral_constant_expression_p
)
19322 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19323 #define RECUR(NODE) \
19324 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19325 /*function_p=*/false, \
19326 integral_constant_expression_p)
19329 location_t save_loc
;
19331 if (t
== NULL_TREE
|| t
== error_mark_node
)
19334 save_loc
= input_location
;
19335 if (location_t eloc
= cp_expr_location (t
))
19336 input_location
= eloc
;
19338 /* N3276 decltype magic only applies to calls at the top level or on the
19339 right side of a comma. */
19340 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19341 complain
&= ~tf_decltype
;
19343 switch (TREE_CODE (t
))
19347 /* Fall through. */
19348 case IDENTIFIER_NODE
:
19352 bool non_integral_constant_expression_p
;
19353 const char *error_msg
;
19355 if (IDENTIFIER_CONV_OP_P (t
))
19357 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19358 t
= make_conv_op_name (new_type
);
19361 /* Look up the name. */
19362 decl
= lookup_name (t
);
19364 /* By convention, expressions use ERROR_MARK_NODE to indicate
19365 failure, not NULL_TREE. */
19366 if (decl
== NULL_TREE
)
19367 decl
= error_mark_node
;
19369 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19371 integral_constant_expression_p
,
19372 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19373 &non_integral_constant_expression_p
,
19374 /*template_p=*/false,
19376 /*address_p=*/false,
19377 /*template_arg_p=*/false,
19382 if (!function_p
&& identifier_p (decl
))
19384 if (complain
& tf_error
)
19385 unqualified_name_lookup_error (decl
);
19386 decl
= error_mark_node
;
19391 case TEMPLATE_ID_EXPR
:
19394 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19397 integral_constant_expression_p
);
19398 tree targs
= TREE_OPERAND (t
, 1);
19401 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19402 if (targs
== error_mark_node
)
19403 RETURN (error_mark_node
);
19405 if (TREE_CODE (templ
) == SCOPE_REF
)
19407 tree name
= TREE_OPERAND (templ
, 1);
19408 tree tid
= lookup_template_function (name
, targs
);
19409 TREE_OPERAND (templ
, 1) = tid
;
19413 if (concept_definition_p (templ
))
19415 tree check
= build_concept_check (templ
, targs
, complain
);
19416 if (check
== error_mark_node
)
19417 RETURN (error_mark_node
);
19419 tree id
= unpack_concept_check (check
);
19421 /* If we built a function concept check, return the underlying
19422 template-id. So we can evaluate it as a function call. */
19423 if (function_concept_p (TREE_OPERAND (id
, 0)))
19429 if (variable_template_p (templ
))
19431 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19433 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19437 if (TREE_CODE (templ
) == COMPONENT_REF
)
19439 object
= TREE_OPERAND (templ
, 0);
19440 templ
= TREE_OPERAND (templ
, 1);
19443 object
= NULL_TREE
;
19445 tree tid
= lookup_template_function (templ
, targs
);
19448 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
19449 object
, tid
, NULL_TREE
));
19450 else if (identifier_p (templ
))
19452 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19453 name lookup found nothing when parsing the template name. */
19454 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
19458 RETURN (baselink_for_fns (tid
));
19463 tree r
= RECUR (TREE_OPERAND (t
, 0));
19465 if (REFERENCE_REF_P (t
))
19467 /* A type conversion to reference type will be enclosed in
19468 such an indirect ref, but the substitution of the cast
19469 will have also added such an indirect ref. */
19470 r
= convert_from_reference (r
);
19473 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19474 complain
|decltype_flag
);
19476 if (REF_PARENTHESIZED_P (t
))
19477 r
= force_paren_expr (r
);
19484 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19485 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19486 RETURN (build_nop (type
, op0
));
19489 case IMPLICIT_CONV_EXPR
:
19491 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19492 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19493 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19495 retval
= copy_node (t
);
19496 TREE_TYPE (retval
) = type
;
19497 TREE_OPERAND (retval
, 0) = expr
;
19500 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19501 /* We'll pass this to convert_nontype_argument again, we don't need
19502 to actually perform any conversion here. */
19504 int flags
= LOOKUP_IMPLICIT
;
19505 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19506 flags
= LOOKUP_NORMAL
;
19507 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19508 flags
|= LOOKUP_NO_NARROWING
;
19509 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19515 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19516 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19517 if (op0
== error_mark_node
)
19518 RETURN (error_mark_node
);
19519 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19523 case REINTERPRET_CAST_EXPR
:
19524 case CONST_CAST_EXPR
:
19525 case DYNAMIC_CAST_EXPR
:
19526 case STATIC_CAST_EXPR
:
19529 tree op
, r
= NULL_TREE
;
19531 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19532 if (integral_constant_expression_p
19533 && !cast_valid_in_integral_constant_expression_p (type
))
19535 if (complain
& tf_error
)
19536 error ("a cast to a type other than an integral or "
19537 "enumeration type cannot appear in a constant-expression");
19538 RETURN (error_mark_node
);
19541 op
= RECUR (TREE_OPERAND (t
, 0));
19543 warning_sentinel
s(warn_useless_cast
);
19544 warning_sentinel
s2(warn_ignored_qualifiers
);
19545 switch (TREE_CODE (t
))
19548 r
= build_functional_cast (input_location
, type
, op
, complain
);
19550 case REINTERPRET_CAST_EXPR
:
19551 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19553 case CONST_CAST_EXPR
:
19554 r
= build_const_cast (input_location
, type
, op
, complain
);
19556 case DYNAMIC_CAST_EXPR
:
19557 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19559 case STATIC_CAST_EXPR
:
19560 r
= build_static_cast (input_location
, type
, op
, complain
);
19561 if (IMPLICIT_RVALUE_P (t
))
19562 set_implicit_rvalue_p (r
);
19565 gcc_unreachable ();
19571 case POSTDECREMENT_EXPR
:
19572 case POSTINCREMENT_EXPR
:
19573 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19574 args
, complain
, in_decl
);
19575 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19576 complain
|decltype_flag
));
19578 case PREDECREMENT_EXPR
:
19579 case PREINCREMENT_EXPR
:
19583 case TRUTH_NOT_EXPR
:
19584 case UNARY_PLUS_EXPR
: /* Unary + */
19585 case REALPART_EXPR
:
19586 case IMAGPART_EXPR
:
19587 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19588 RECUR (TREE_OPERAND (t
, 0)),
19589 complain
|decltype_flag
));
19591 case FIX_TRUNC_EXPR
:
19592 gcc_unreachable ();
19595 op1
= TREE_OPERAND (t
, 0);
19596 if (TREE_CODE (op1
) == LABEL_DECL
)
19597 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19598 EXPR_LOCATION (op1
)));
19599 if (TREE_CODE (op1
) == SCOPE_REF
)
19600 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19601 /*done=*/true, /*address_p=*/true);
19603 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19605 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19606 complain
|decltype_flag
));
19611 case TRUNC_DIV_EXPR
:
19612 case CEIL_DIV_EXPR
:
19613 case FLOOR_DIV_EXPR
:
19614 case ROUND_DIV_EXPR
:
19615 case EXACT_DIV_EXPR
:
19619 case TRUNC_MOD_EXPR
:
19620 case FLOOR_MOD_EXPR
:
19621 case TRUTH_ANDIF_EXPR
:
19622 case TRUTH_ORIF_EXPR
:
19623 case TRUTH_AND_EXPR
:
19624 case TRUTH_OR_EXPR
:
19635 case SPACESHIP_EXPR
:
19639 /* If T was type-dependent, suppress warnings that depend on the range
19640 of the types involved. */
19641 ++processing_template_decl
;
19642 const bool was_dep
= (potential_constant_expression (t
)
19643 ? value_dependent_expression_p (t
)
19644 : type_dependent_expression_p (t
));
19645 --processing_template_decl
;
19646 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19647 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19649 warning_sentinel
s1(warn_type_limits
, was_dep
);
19650 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19651 warning_sentinel
s3(warn_logical_op
, was_dep
);
19652 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19654 tree r
= build_x_binary_op
19655 (input_location
, TREE_CODE (t
),
19657 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19659 : TREE_CODE (TREE_OPERAND (t
, 0))),
19661 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19663 : TREE_CODE (TREE_OPERAND (t
, 1))),
19665 complain
|decltype_flag
);
19666 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19667 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19672 case POINTER_PLUS_EXPR
:
19674 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19675 if (op0
== error_mark_node
)
19676 RETURN (error_mark_node
);
19677 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19678 if (op1
== error_mark_node
)
19679 RETURN (error_mark_node
);
19680 RETURN (fold_build_pointer_plus (op0
, op1
));
19684 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19685 /*address_p=*/false));
19687 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19688 args
, complain
, in_decl
);
19689 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19690 RECUR (TREE_OPERAND (t
, 1)),
19691 complain
|decltype_flag
));
19694 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19695 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19696 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19703 op1
= TREE_OPERAND (t
, 0);
19704 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19705 op1
= TREE_TYPE (op1
);
19706 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19707 && ALIGNOF_EXPR_STD_P (t
));
19710 /* When there are no ARGS, we are trying to evaluate a
19711 non-dependent expression from the parser. Trying to do
19712 the substitutions may not work. */
19714 op1
= TREE_TYPE (op1
);
19718 ++cp_unevaluated_operand
;
19719 ++c_inhibit_evaluation_warnings
;
19721 op1
= tsubst (op1
, args
, complain
, in_decl
);
19723 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19724 /*function_p=*/false,
19725 /*integral_constant_expression_p=*/
19727 --cp_unevaluated_operand
;
19728 --c_inhibit_evaluation_warnings
;
19731 r
= cxx_sizeof_or_alignof_type (input_location
,
19732 op1
, TREE_CODE (t
), std_alignof
,
19733 complain
& tf_error
);
19735 r
= cxx_sizeof_or_alignof_expr (input_location
,
19736 op1
, TREE_CODE (t
), std_alignof
,
19737 complain
& tf_error
);
19738 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19740 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19742 if (!processing_template_decl
&& TYPE_P (op1
))
19744 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19745 build1 (NOP_EXPR
, op1
, error_mark_node
));
19746 SIZEOF_EXPR_TYPE_P (r
) = 1;
19749 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19750 TREE_SIDE_EFFECTS (r
) = 0;
19751 TREE_READONLY (r
) = 1;
19753 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19758 case AT_ENCODE_EXPR
:
19760 op1
= TREE_OPERAND (t
, 0);
19761 ++cp_unevaluated_operand
;
19762 ++c_inhibit_evaluation_warnings
;
19763 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19764 /*function_p=*/false,
19765 /*integral_constant_expression_p=*/false);
19766 --cp_unevaluated_operand
;
19767 --c_inhibit_evaluation_warnings
;
19768 RETURN (objc_build_encode_expr (op1
));
19771 case NOEXCEPT_EXPR
:
19772 op1
= TREE_OPERAND (t
, 0);
19773 ++cp_unevaluated_operand
;
19774 ++c_inhibit_evaluation_warnings
;
19775 ++cp_noexcept_operand
;
19776 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19777 /*function_p=*/false,
19778 /*integral_constant_expression_p=*/false);
19779 --cp_unevaluated_operand
;
19780 --c_inhibit_evaluation_warnings
;
19781 --cp_noexcept_operand
;
19782 RETURN (finish_noexcept_expr (op1
, complain
));
19786 warning_sentinel
s(warn_div_by_zero
);
19787 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19788 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19789 tree r
= build_x_modify_expr
19790 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19791 complain
|decltype_flag
);
19792 /* TREE_NO_WARNING must be set if either the expression was
19793 parenthesized or it uses an operator such as >>= rather
19794 than plain assignment. In the former case, it was already
19795 set and must be copied. In the latter case,
19796 build_x_modify_expr sets it and it must not be reset
19798 if (TREE_NO_WARNING (t
))
19799 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19805 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19806 args
, complain
, in_decl
);
19807 /* Remember that there was a reference to this entity. */
19809 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19810 RETURN (error_mark_node
);
19811 RETURN (build_x_arrow (input_location
, op1
, complain
));
19815 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19816 tree init
= RECUR (TREE_OPERAND (t
, 3));
19817 vec
<tree
, va_gc
> *placement_vec
;
19818 vec
<tree
, va_gc
> *init_vec
;
19820 location_t loc
= EXPR_LOCATION (t
);
19822 if (placement
== NULL_TREE
)
19823 placement_vec
= NULL
;
19824 else if (placement
== error_mark_node
)
19825 RETURN (error_mark_node
);
19828 placement_vec
= make_tree_vector ();
19829 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19830 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19833 /* If there was an initializer in the original tree, but it
19834 instantiated to an empty list, then we should pass a
19835 non-NULL empty vector to tell build_new that it was an
19836 empty initializer() rather than no initializer. This can
19837 only happen when the initializer is a pack expansion whose
19838 parameter packs are of length zero. */
19839 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19843 init_vec
= make_tree_vector ();
19844 if (init
== void_node
)
19845 gcc_assert (init_vec
!= NULL
);
19848 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19849 vec_safe_push (init_vec
, TREE_VALUE (init
));
19853 /* Avoid passing an enclosing decl to valid_array_size_p. */
19854 in_decl
= NULL_TREE
;
19856 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19857 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19858 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19859 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19862 if (placement_vec
!= NULL
)
19863 release_tree_vector (placement_vec
);
19864 if (init_vec
!= NULL
)
19865 release_tree_vector (init_vec
);
19872 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19873 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19874 RETURN (delete_sanity (input_location
, op0
, op1
,
19875 DELETE_EXPR_USE_VEC (t
),
19876 DELETE_EXPR_USE_GLOBAL (t
),
19880 case COMPOUND_EXPR
:
19882 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19883 complain
& ~tf_decltype
, in_decl
,
19884 /*function_p=*/false,
19885 integral_constant_expression_p
);
19886 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19888 RECUR (TREE_OPERAND (t
, 1)),
19889 complain
|decltype_flag
));
19895 unsigned int nargs
, i
;
19900 function
= CALL_EXPR_FN (t
);
19901 /* Internal function with no arguments. */
19902 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19905 /* When we parsed the expression, we determined whether or
19906 not Koenig lookup should be performed. */
19907 koenig_p
= KOENIG_LOOKUP_P (t
);
19908 if (function
== NULL_TREE
)
19911 qualified_p
= false;
19913 else if (TREE_CODE (function
) == SCOPE_REF
)
19915 qualified_p
= true;
19916 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19918 /*address_p=*/false);
19920 else if (koenig_p
&& identifier_p (function
))
19922 /* Do nothing; calling tsubst_copy_and_build on an identifier
19923 would incorrectly perform unqualified lookup again.
19925 Note that we can also have an IDENTIFIER_NODE if the earlier
19926 unqualified lookup found a member function; in that case
19927 koenig_p will be false and we do want to do the lookup
19928 again to find the instantiated member function.
19930 FIXME but doing that causes c++/15272, so we need to stop
19931 using IDENTIFIER_NODE in that situation. */
19932 qualified_p
= false;
19936 if (TREE_CODE (function
) == COMPONENT_REF
)
19938 tree op
= TREE_OPERAND (function
, 1);
19940 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19941 || (BASELINK_P (op
)
19942 && BASELINK_QUALIFIED_P (op
)));
19945 qualified_p
= false;
19947 if (TREE_CODE (function
) == ADDR_EXPR
19948 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19949 /* Avoid error about taking the address of a constructor. */
19950 function
= TREE_OPERAND (function
, 0);
19952 function
= tsubst_copy_and_build (function
, args
, complain
,
19955 integral_constant_expression_p
);
19957 if (BASELINK_P (function
))
19958 qualified_p
= true;
19961 nargs
= call_expr_nargs (t
);
19962 releasing_vec call_args
;
19963 for (i
= 0; i
< nargs
; ++i
)
19965 tree arg
= CALL_EXPR_ARG (t
, i
);
19967 if (!PACK_EXPANSION_P (arg
))
19968 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19971 /* Expand the pack expansion and push each entry onto
19973 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19974 if (TREE_CODE (arg
) == TREE_VEC
)
19976 unsigned int len
, j
;
19978 len
= TREE_VEC_LENGTH (arg
);
19979 for (j
= 0; j
< len
; ++j
)
19981 tree value
= TREE_VEC_ELT (arg
, j
);
19982 if (value
!= NULL_TREE
)
19983 value
= convert_from_reference (value
);
19984 vec_safe_push (call_args
, value
);
19989 /* A partial substitution. Add one entry. */
19990 vec_safe_push (call_args
, arg
);
19995 /* Stripped-down processing for a call in a thunk. Specifically, in
19996 the thunk template for a generic lambda. */
19997 if (call_from_lambda_thunk_p (t
))
19999 /* Now that we've expanded any packs, the number of call args
20000 might be different. */
20001 unsigned int cargs
= call_args
->length ();
20002 tree thisarg
= NULL_TREE
;
20003 if (TREE_CODE (function
) == COMPONENT_REF
)
20005 thisarg
= TREE_OPERAND (function
, 0);
20006 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20007 thisarg
= TREE_OPERAND (thisarg
, 0);
20008 function
= TREE_OPERAND (function
, 1);
20009 if (TREE_CODE (function
) == BASELINK
)
20010 function
= BASELINK_FUNCTIONS (function
);
20012 /* We aren't going to do normal overload resolution, so force the
20013 template-id to resolve. */
20014 function
= resolve_nondeduced_context (function
, complain
);
20015 for (unsigned i
= 0; i
< cargs
; ++i
)
20017 /* In a thunk, pass through args directly, without any
20019 tree arg
= (*call_args
)[i
];
20020 while (TREE_CODE (arg
) != PARM_DECL
)
20021 arg
= TREE_OPERAND (arg
, 0);
20022 (*call_args
)[i
] = arg
;
20026 /* If there are no other args, just push 'this'. */
20028 vec_safe_push (call_args
, thisarg
);
20031 /* Otherwise, shift the other args over to make room. */
20032 tree last
= (*call_args
)[cargs
- 1];
20033 vec_safe_push (call_args
, last
);
20034 for (int i
= cargs
- 1; i
> 0; --i
)
20035 (*call_args
)[i
] = (*call_args
)[i
- 1];
20036 (*call_args
)[0] = thisarg
;
20039 ret
= build_call_a (function
, call_args
->length (),
20040 call_args
->address ());
20041 /* The thunk location is not interesting. */
20042 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20043 CALL_FROM_THUNK_P (ret
) = true;
20044 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20045 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20050 /* We do not perform argument-dependent lookup if normal
20051 lookup finds a non-function, in accordance with the
20052 resolution of DR 218. */
20054 && ((is_overloaded_fn (function
)
20055 /* If lookup found a member function, the Koenig lookup is
20056 not appropriate, even if an unqualified-name was used
20057 to denote the function. */
20058 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20059 || identifier_p (function
)
20060 /* C++20 P0846: Lookup found nothing. */
20061 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20062 && identifier_p (TREE_OPERAND (function
, 0))))
20063 /* Only do this when substitution turns a dependent call
20064 into a non-dependent call. */
20065 && type_dependent_expression_p_push (t
)
20066 && !any_type_dependent_arguments_p (call_args
))
20067 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20069 if (function
!= NULL_TREE
20070 && (identifier_p (function
)
20071 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20072 && identifier_p (TREE_OPERAND (function
, 0))))
20073 && !any_type_dependent_arguments_p (call_args
))
20075 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20076 function
= TREE_OPERAND (function
, 0);
20077 if (koenig_p
&& (complain
& tf_warning_or_error
))
20079 /* For backwards compatibility and good diagnostics, try
20080 the unqualified lookup again if we aren't in SFINAE
20082 tree unq
= (tsubst_copy_and_build
20083 (function
, args
, complain
, in_decl
, true,
20084 integral_constant_expression_p
));
20085 if (unq
== error_mark_node
)
20086 RETURN (error_mark_node
);
20088 if (unq
!= function
)
20090 /* In a lambda fn, we have to be careful to not
20091 introduce new this captures. Legacy code can't
20092 be using lambdas anyway, so it's ok to be
20094 bool in_lambda
= (current_class_type
20095 && LAMBDA_TYPE_P (current_class_type
));
20096 char const *const msg
20097 = G_("%qD was not declared in this scope, "
20098 "and no declarations were found by "
20099 "argument-dependent lookup at the point "
20100 "of instantiation");
20104 error_at (cp_expr_loc_or_input_loc (t
),
20107 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20113 if (INDIRECT_REF_P (fn
))
20114 fn
= TREE_OPERAND (fn
, 0);
20115 if (is_overloaded_fn (fn
))
20116 fn
= get_first_fn (fn
);
20119 /* Can't say anything more. */;
20120 else if (DECL_CLASS_SCOPE_P (fn
))
20122 location_t loc
= cp_expr_loc_or_input_loc (t
);
20124 "declarations in dependent base %qT are "
20125 "not found by unqualified lookup",
20126 DECL_CLASS_CONTEXT (fn
));
20127 if (current_class_ptr
)
20129 "use %<this->%D%> instead", function
);
20132 "use %<%T::%D%> instead",
20133 current_class_name
, function
);
20136 inform (DECL_SOURCE_LOCATION (fn
),
20137 "%qD declared here, later in the "
20138 "translation unit", fn
);
20140 RETURN (error_mark_node
);
20146 if (identifier_p (function
))
20148 if (complain
& tf_error
)
20149 unqualified_name_lookup_error (function
);
20150 RETURN (error_mark_node
);
20154 /* Remember that there was a reference to this entity. */
20155 if (function
!= NULL_TREE
20156 && DECL_P (function
)
20157 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20158 RETURN (error_mark_node
);
20160 /* Put back tf_decltype for the actual call. */
20161 complain
|= decltype_flag
;
20163 if (function
== NULL_TREE
)
20164 switch (CALL_EXPR_IFN (t
))
20167 gcc_assert (nargs
== 1);
20168 if (vec_safe_length (call_args
) != 1)
20170 error_at (cp_expr_loc_or_input_loc (t
),
20171 "wrong number of arguments to "
20172 "%<__builtin_launder%>");
20173 ret
= error_mark_node
;
20176 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20177 (*call_args
)[0], complain
);
20180 case IFN_VEC_CONVERT
:
20181 gcc_assert (nargs
== 1);
20182 if (vec_safe_length (call_args
) != 1)
20184 error_at (cp_expr_loc_or_input_loc (t
),
20185 "wrong number of arguments to "
20186 "%<__builtin_convertvector%>");
20187 ret
= error_mark_node
;
20190 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20191 tsubst (TREE_TYPE (t
), args
,
20192 complain
, in_decl
),
20194 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20199 /* Unsupported internal function with arguments. */
20200 gcc_unreachable ();
20202 else if (TREE_CODE (function
) == OFFSET_REF
20203 || TREE_CODE (function
) == DOTSTAR_EXPR
20204 || TREE_CODE (function
) == MEMBER_REF
)
20205 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20207 else if (TREE_CODE (function
) == COMPONENT_REF
)
20209 tree instance
= TREE_OPERAND (function
, 0);
20210 tree fn
= TREE_OPERAND (function
, 1);
20212 if (processing_template_decl
20213 && (type_dependent_expression_p (instance
)
20214 || (!BASELINK_P (fn
)
20215 && TREE_CODE (fn
) != FIELD_DECL
)
20216 || type_dependent_expression_p (fn
)
20217 || any_type_dependent_arguments_p (call_args
)))
20218 ret
= build_min_nt_call_vec (function
, call_args
);
20219 else if (!BASELINK_P (fn
))
20220 ret
= finish_call_expr (function
, &call_args
,
20221 /*disallow_virtual=*/false,
20222 /*koenig_p=*/false,
20225 ret
= (build_new_method_call
20227 &call_args
, NULL_TREE
,
20228 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20232 else if (concept_check_p (function
))
20234 /* FUNCTION is a template-id referring to a concept definition. */
20235 tree id
= unpack_concept_check (function
);
20236 tree tmpl
= TREE_OPERAND (id
, 0);
20237 tree args
= TREE_OPERAND (id
, 1);
20239 /* Calls to standard and variable concepts should have been
20240 previously diagnosed. */
20241 gcc_assert (function_concept_p (tmpl
));
20243 /* Ensure the result is wrapped as a call expression. */
20244 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20247 ret
= finish_call_expr (function
, &call_args
,
20248 /*disallow_virtual=*/qualified_p
,
20252 if (ret
!= error_mark_node
)
20254 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20255 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20256 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20257 if (op
|| ord
|| rev
)
20259 function
= extract_call_expr (ret
);
20260 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20261 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20262 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20271 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20272 cond
= mark_rvalue_use (cond
);
20273 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20276 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20278 if (integer_zerop (folded_cond
))
20280 ++c_inhibit_evaluation_warnings
;
20281 exp1
= RECUR (TREE_OPERAND (t
, 1));
20282 --c_inhibit_evaluation_warnings
;
20283 exp2
= RECUR (TREE_OPERAND (t
, 2));
20287 exp1
= RECUR (TREE_OPERAND (t
, 1));
20288 ++c_inhibit_evaluation_warnings
;
20289 exp2
= RECUR (TREE_OPERAND (t
, 2));
20290 --c_inhibit_evaluation_warnings
;
20292 cond
= folded_cond
;
20296 exp1
= RECUR (TREE_OPERAND (t
, 1));
20297 exp2
= RECUR (TREE_OPERAND (t
, 2));
20300 warning_sentinel
s(warn_duplicated_branches
);
20301 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20302 cond
, exp1
, exp2
, complain
));
20305 case PSEUDO_DTOR_EXPR
:
20307 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20308 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20309 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20310 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20315 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20317 case COMPONENT_REF
:
20324 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20325 args
, complain
, in_decl
);
20326 /* Remember that there was a reference to this entity. */
20327 if (DECL_P (object
)
20328 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20329 RETURN (error_mark_node
);
20330 object_type
= TREE_TYPE (object
);
20332 member
= TREE_OPERAND (t
, 1);
20333 if (BASELINK_P (member
))
20334 member
= tsubst_baselink (member
,
20335 non_reference (TREE_TYPE (object
)),
20336 args
, complain
, in_decl
);
20338 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20339 if (member
== error_mark_node
)
20340 RETURN (error_mark_node
);
20342 if (TREE_CODE (member
) == FIELD_DECL
)
20344 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20345 if (TREE_CODE (r
) == COMPONENT_REF
)
20346 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20349 else if (type_dependent_expression_p (object
))
20350 /* We can't do much here. */;
20351 else if (!CLASS_TYPE_P (object_type
))
20353 if (scalarish_type_p (object_type
))
20355 tree s
= NULL_TREE
;
20356 tree dtor
= member
;
20358 if (TREE_CODE (dtor
) == SCOPE_REF
)
20360 s
= TREE_OPERAND (dtor
, 0);
20361 dtor
= TREE_OPERAND (dtor
, 1);
20363 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20365 dtor
= TREE_OPERAND (dtor
, 0);
20367 RETURN (finish_pseudo_destructor_expr
20368 (object
, s
, dtor
, input_location
));
20372 else if (TREE_CODE (member
) == SCOPE_REF
20373 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20375 /* Lookup the template functions now that we know what the
20377 tree scope
= TREE_OPERAND (member
, 0);
20378 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20379 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20380 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20381 /*complain=*/false);
20382 if (BASELINK_P (member
))
20384 BASELINK_FUNCTIONS (member
)
20385 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20387 member
= (adjust_result_of_qualified_name_lookup
20388 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20393 qualified_name_lookup_error (scope
, tmpl
, member
,
20395 RETURN (error_mark_node
);
20398 else if (TREE_CODE (member
) == SCOPE_REF
20399 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20400 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20402 if (complain
& tf_error
)
20404 if (TYPE_P (TREE_OPERAND (member
, 0)))
20405 error ("%qT is not a class or namespace",
20406 TREE_OPERAND (member
, 0));
20408 error ("%qD is not a class or namespace",
20409 TREE_OPERAND (member
, 0));
20411 RETURN (error_mark_node
);
20414 r
= finish_class_member_access_expr (object
, member
,
20415 /*template_p=*/false,
20417 if (TREE_CODE (r
) == COMPONENT_REF
)
20418 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20423 RETURN (build_throw
20424 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20428 vec
<constructor_elt
, va_gc
> *n
;
20429 constructor_elt
*ce
;
20430 unsigned HOST_WIDE_INT idx
;
20431 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20432 bool process_index_p
;
20434 bool need_copy_p
= false;
20437 if (type
== error_mark_node
)
20438 RETURN (error_mark_node
);
20440 /* We do not want to process the index of aggregate
20441 initializers as they are identifier nodes which will be
20442 looked up by digest_init. */
20443 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20445 if (null_member_pointer_value_p (t
))
20447 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20451 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20452 newlen
= vec_safe_length (n
);
20453 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20455 if (ce
->index
&& process_index_p
20456 /* An identifier index is looked up in the type
20457 being initialized, not the current scope. */
20458 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20459 ce
->index
= RECUR (ce
->index
);
20461 if (PACK_EXPANSION_P (ce
->value
))
20463 /* Substitute into the pack expansion. */
20464 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20467 if (ce
->value
== error_mark_node
20468 || PACK_EXPANSION_P (ce
->value
))
20470 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20471 /* Just move the argument into place. */
20472 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20475 /* Update the length of the final CONSTRUCTOR
20476 arguments vector, and note that we will need to
20478 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20479 need_copy_p
= true;
20483 ce
->value
= RECUR (ce
->value
);
20488 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20490 vec_alloc (n
, newlen
);
20491 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20493 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20495 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20496 for (i
= 0; i
< len
; ++i
)
20497 CONSTRUCTOR_APPEND_ELT (n
, 0,
20498 TREE_VEC_ELT (ce
->value
, i
));
20501 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20505 r
= build_constructor (init_list_type_node
, n
);
20506 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20507 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20508 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20510 if (TREE_HAS_CONSTRUCTOR (t
))
20512 fcl_t cl
= fcl_functional
;
20513 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20515 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20518 TREE_TYPE (r
) = type
;
20524 tree operand_0
= TREE_OPERAND (t
, 0);
20525 if (TYPE_P (operand_0
))
20527 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20528 RETURN (get_typeid (operand_0
, complain
));
20532 operand_0
= RECUR (operand_0
);
20533 RETURN (build_typeid (operand_0
, complain
));
20544 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20545 /* ??? We're doing a subset of finish_id_expression here. */
20546 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20547 /* Replace an evaluated use of the thread_local variable with
20548 a call to its wrapper. */
20550 else if (outer_automatic_var_p (r
))
20551 r
= process_outer_var_ref (r
, complain
);
20553 if (!TYPE_REF_P (TREE_TYPE (t
)))
20554 /* If the original type was a reference, we'll be wrapped in
20555 the appropriate INDIRECT_REF. */
20556 r
= convert_from_reference (r
);
20562 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20563 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20564 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20567 case OFFSETOF_EXPR
:
20570 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20571 in_decl
, /*function_p=*/false,
20572 /*integral_constant_expression_p=*/false);
20573 RETURN (finish_offsetof (object_ptr
,
20574 RECUR (TREE_OPERAND (t
, 0)),
20575 EXPR_LOCATION (t
)));
20578 case ADDRESSOF_EXPR
:
20579 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20580 RECUR (TREE_OPERAND (t
, 0)), complain
));
20584 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20585 complain
, in_decl
);
20586 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20587 complain
, in_decl
);
20588 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20589 TRAIT_EXPR_KIND (t
), type1
, type2
));
20594 tree old_stmt_expr
= cur_stmt_expr
;
20595 tree stmt_expr
= begin_stmt_expr ();
20597 cur_stmt_expr
= stmt_expr
;
20598 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20599 integral_constant_expression_p
);
20600 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20601 cur_stmt_expr
= old_stmt_expr
;
20603 /* If the resulting list of expression statement is empty,
20604 fold it further into void_node. */
20605 if (empty_expr_stmt_p (stmt_expr
))
20606 stmt_expr
= void_node
;
20608 RETURN (stmt_expr
);
20613 if (complain
& tf_partial
)
20615 /* We don't have a full set of template arguments yet; don't touch
20616 the lambda at all. */
20617 gcc_assert (processing_template_decl
);
20620 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20622 RETURN (build_lambda_object (r
));
20626 /* We can get here for a constant initializer of non-dependent type.
20627 FIXME stop folding in cp_parser_initializer_clause. */
20629 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20634 case TRANSACTION_EXPR
:
20635 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20636 integral_constant_expression_p
));
20639 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20641 case VEC_PERM_EXPR
:
20643 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20644 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20645 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20646 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20650 case REQUIRES_EXPR
:
20652 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20657 /* No need to substitute further, a RANGE_EXPR will always be built
20658 with constant operands. */
20661 case NON_LVALUE_EXPR
:
20662 case VIEW_CONVERT_EXPR
:
20663 if (location_wrapper_p (t
))
20664 /* We need to do this here as well as in tsubst_copy so we get the
20665 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20666 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20667 EXPR_LOCATION (t
)));
20671 /* Handle Objective-C++ constructs, if appropriate. */
20674 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20675 in_decl
, /*function_p=*/false);
20679 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20685 input_location
= save_loc
;
20689 /* Verify that the instantiated ARGS are valid. For type arguments,
20690 make sure that the type's linkage is ok. For non-type arguments,
20691 make sure they are constants if they are integral or enumerations.
20692 Emit an error under control of COMPLAIN, and return TRUE on error. */
20695 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20697 if (dependent_template_arg_p (t
))
20699 if (ARGUMENT_PACK_P (t
))
20701 tree vec
= ARGUMENT_PACK_ARGS (t
);
20702 int len
= TREE_VEC_LENGTH (vec
);
20703 bool result
= false;
20706 for (i
= 0; i
< len
; ++i
)
20707 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20711 else if (TYPE_P (t
))
20713 /* [basic.link]: A name with no linkage (notably, the name
20714 of a class or enumeration declared in a local scope)
20715 shall not be used to declare an entity with linkage.
20716 This implies that names with no linkage cannot be used as
20719 DR 757 relaxes this restriction for C++0x. */
20720 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20721 : no_linkage_check (t
, /*relaxed_p=*/false));
20725 /* DR 488 makes use of a type with no linkage cause
20726 type deduction to fail. */
20727 if (complain
& tf_error
)
20729 if (TYPE_UNNAMED_P (nt
))
20730 error ("%qT is/uses unnamed type", t
);
20732 error ("template argument for %qD uses local type %qT",
20737 /* In order to avoid all sorts of complications, we do not
20738 allow variably-modified types as template arguments. */
20739 else if (variably_modified_type_p (t
, NULL_TREE
))
20741 if (complain
& tf_error
)
20742 error ("%qT is a variably modified type", t
);
20746 /* Class template and alias template arguments should be OK. */
20747 else if (DECL_TYPE_TEMPLATE_P (t
))
20749 /* A non-type argument of integral or enumerated type must be a
20751 else if (TREE_TYPE (t
)
20752 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20753 && !REFERENCE_REF_P (t
)
20754 && !TREE_CONSTANT (t
))
20756 if (complain
& tf_error
)
20757 error ("integral expression %qE is not constant", t
);
20764 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20766 int ix
, len
= DECL_NTPARMS (tmpl
);
20767 bool result
= false;
20769 for (ix
= 0; ix
!= len
; ix
++)
20771 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20774 if (result
&& (complain
& tf_error
))
20775 error (" trying to instantiate %qD", tmpl
);
20779 /* We're out of SFINAE context now, so generate diagnostics for the access
20780 errors we saw earlier when instantiating D from TMPL and ARGS. */
20783 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20785 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20786 tree type
= TREE_TYPE (pattern
);
20787 location_t loc
= input_location
;
20789 push_access_scope (d
);
20790 push_deferring_access_checks (dk_no_deferred
);
20791 input_location
= DECL_SOURCE_LOCATION (pattern
);
20792 tsubst (type
, args
, tf_warning_or_error
, d
);
20793 input_location
= loc
;
20794 pop_deferring_access_checks ();
20795 pop_access_scope (d
);
20798 /* Instantiate the indicated variable, function, or alias template TMPL with
20799 the template arguments in TARG_PTR. */
20802 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20804 tree targ_ptr
= orig_args
;
20808 bool access_ok
= true;
20810 if (tmpl
== error_mark_node
)
20811 return error_mark_node
;
20813 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20815 /* If this function is a clone, handle it specially. */
20816 if (DECL_CLONED_FUNCTION_P (tmpl
))
20821 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20822 DECL_CLONED_FUNCTION. */
20823 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20824 targ_ptr
, complain
);
20825 if (spec
== error_mark_node
)
20826 return error_mark_node
;
20828 /* Look for the clone. */
20829 FOR_EACH_CLONE (clone
, spec
)
20830 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20832 /* We should always have found the clone by now. */
20833 gcc_unreachable ();
20837 if (targ_ptr
== error_mark_node
)
20838 return error_mark_node
;
20840 /* Check to see if we already have this specialization. */
20841 gen_tmpl
= most_general_template (tmpl
);
20842 if (TMPL_ARGS_DEPTH (targ_ptr
)
20843 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20844 /* targ_ptr only has the innermost template args, so add the outer ones
20845 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20846 the case of a non-dependent call within a template definition). */
20847 targ_ptr
= (add_outermost_template_args
20848 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20851 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20852 but it doesn't seem to be on the hot path. */
20853 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20855 gcc_checking_assert (tmpl
== gen_tmpl
20857 = retrieve_specialization (tmpl
, orig_args
, 0))
20859 || fndecl
== NULL_TREE
);
20861 if (spec
!= NULL_TREE
)
20863 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20865 if (complain
& tf_error
)
20866 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20867 return error_mark_node
;
20872 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20874 return error_mark_node
;
20876 /* We are building a FUNCTION_DECL, during which the access of its
20877 parameters and return types have to be checked. However this
20878 FUNCTION_DECL which is the desired context for access checking
20879 is not built yet. We solve this chicken-and-egg problem by
20880 deferring all checks until we have the FUNCTION_DECL. */
20881 push_deferring_access_checks (dk_deferred
);
20883 /* Instantiation of the function happens in the context of the function
20884 template, not the context of the overload resolution we're doing. */
20885 push_to_top_level ();
20886 /* If there are dependent arguments, e.g. because we're doing partial
20887 ordering, make sure processing_template_decl stays set. */
20888 if (uses_template_parms (targ_ptr
))
20889 ++processing_template_decl
;
20890 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20892 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20893 complain
, gen_tmpl
, true);
20894 push_nested_class (ctx
);
20897 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20899 fndecl
= NULL_TREE
;
20900 if (VAR_P (pattern
))
20902 /* We need to determine if we're using a partial or explicit
20903 specialization now, because the type of the variable could be
20905 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20906 tree elt
= most_specialized_partial_spec (tid
, complain
);
20907 if (elt
== error_mark_node
)
20908 pattern
= error_mark_node
;
20911 tree partial_tmpl
= TREE_VALUE (elt
);
20912 tree partial_args
= TREE_PURPOSE (elt
);
20913 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20914 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20918 /* Substitute template parameters to obtain the specialization. */
20919 if (fndecl
== NULL_TREE
)
20920 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20921 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20922 pop_nested_class ();
20923 pop_from_top_level ();
20925 if (fndecl
== error_mark_node
)
20927 pop_deferring_access_checks ();
20928 return error_mark_node
;
20931 /* The DECL_TI_TEMPLATE should always be the immediate parent
20932 template, not the most general template. */
20933 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20934 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20936 /* Now we know the specialization, compute access previously
20937 deferred. Do no access control for inheriting constructors,
20938 as we already checked access for the inherited constructor. */
20939 if (!(flag_new_inheriting_ctors
20940 && DECL_INHERITED_CTOR (fndecl
)))
20942 push_access_scope (fndecl
);
20943 if (!perform_deferred_access_checks (complain
))
20945 pop_access_scope (fndecl
);
20947 pop_deferring_access_checks ();
20949 /* If we've just instantiated the main entry point for a function,
20950 instantiate all the alternate entry points as well. We do this
20951 by cloning the instantiation of the main entry point, not by
20952 instantiating the template clones. */
20953 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20954 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20955 clone_cdtor (fndecl
, /*update_methods=*/false);
20959 if (!(complain
& tf_error
))
20961 /* Remember to reinstantiate when we're out of SFINAE so the user
20962 can see the errors. */
20963 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20965 return error_mark_node
;
20970 /* Wrapper for instantiate_template_1. */
20973 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20976 timevar_push (TV_TEMPLATE_INST
);
20977 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20978 timevar_pop (TV_TEMPLATE_INST
);
20982 /* Instantiate the alias template TMPL with ARGS. Also push a template
20983 instantiation level, which instantiate_template doesn't do because
20984 functions and variables have sufficient context established by the
20988 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20990 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20991 return error_mark_node
;
20994 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20995 args
, tmpl
, complain
,
20996 /*require_all_args=*/true,
20997 /*use_default_args=*/true);
20999 /* FIXME check for satisfaction in check_instantiated_args. */
21001 && !any_dependent_template_arguments_p (args
)
21002 && !constraints_satisfied_p (tmpl
, args
))
21004 if (complain
& tf_error
)
21006 auto_diagnostic_group d
;
21007 error ("template constraint failure for %qD", tmpl
);
21008 diagnose_constraints (input_location
, tmpl
, args
);
21010 return error_mark_node
;
21013 if (!push_tinst_level (tmpl
, args
))
21014 return error_mark_node
;
21015 tree r
= instantiate_template (tmpl
, args
, complain
);
21016 pop_tinst_level ();
21021 /* PARM is a template parameter pack for FN. Returns true iff
21022 PARM is used in a deducible way in the argument list of FN. */
21025 pack_deducible_p (tree parm
, tree fn
)
21027 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
21028 for (; t
; t
= TREE_CHAIN (t
))
21030 tree type
= TREE_VALUE (t
);
21032 if (!PACK_EXPANSION_P (type
))
21034 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
21035 packs
; packs
= TREE_CHAIN (packs
))
21036 if (template_args_equal (TREE_VALUE (packs
), parm
))
21038 /* The template parameter pack is used in a function parameter
21039 pack. If this is the end of the parameter list, the
21040 template parameter pack is deducible. */
21041 if (TREE_CHAIN (t
) == void_list_node
)
21044 /* Otherwise, not. Well, it could be deduced from
21045 a non-pack parameter, but doing so would end up with
21046 a deduction mismatch, so don't bother. */
21050 /* The template parameter pack isn't used in any function parameter
21051 packs, but it might be used deeper, e.g. tuple<Args...>. */
21055 /* Subroutine of fn_type_unification: check non-dependent parms for
21059 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21060 tree fn
, unification_kind_t strict
, int flags
,
21061 struct conversion
**convs
, bool explain_p
)
21063 /* Non-constructor methods need to leave a conversion for 'this', which
21064 isn't included in nargs here. */
21065 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21066 && !DECL_CONSTRUCTOR_P (fn
));
21068 for (unsigned ia
= 0;
21069 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21071 tree parm
= TREE_VALUE (parms
);
21073 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21074 && (!TREE_CHAIN (parms
)
21075 || TREE_CHAIN (parms
) == void_list_node
))
21076 /* For a function parameter pack that occurs at the end of the
21077 parameter-declaration-list, the type A of each remaining
21078 argument of the call is compared with the type P of the
21079 declarator-id of the function parameter pack. */
21082 parms
= TREE_CHAIN (parms
);
21084 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21085 /* For a function parameter pack that does not occur at the
21086 end of the parameter-declaration-list, the type of the
21087 parameter pack is a non-deduced context. */
21090 if (!uses_template_parms (parm
))
21092 tree arg
= args
[ia
];
21093 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21094 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21096 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21097 conv_p
, explain_p
))
21107 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21108 NARGS elements of the arguments that are being used when calling
21109 it. TARGS is a vector into which the deduced template arguments
21112 Returns either a FUNCTION_DECL for the matching specialization of FN or
21113 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21114 true, diagnostics will be printed to explain why it failed.
21116 If FN is a conversion operator, or we are trying to produce a specific
21117 specialization, RETURN_TYPE is the return type desired.
21119 The EXPLICIT_TARGS are explicit template arguments provided via a
21122 The parameter STRICT is one of:
21125 We are deducing arguments for a function call, as in
21126 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21127 deducing arguments for a call to the result of a conversion
21128 function template, as in [over.call.object].
21131 We are deducing arguments for a conversion function, as in
21132 [temp.deduct.conv].
21135 We are deducing arguments when doing an explicit instantiation
21136 as in [temp.explicit], when determining an explicit specialization
21137 as in [temp.expl.spec], or when taking the address of a function
21138 template, as in [temp.deduct.funcaddr]. */
21141 fn_type_unification (tree fn
,
21142 tree explicit_targs
,
21145 unsigned int nargs
,
21147 unification_kind_t strict
,
21149 struct conversion
**convs
,
21155 tree decl
= NULL_TREE
;
21156 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21158 static int deduction_depth
;
21159 /* type_unification_real will pass back any access checks from default
21160 template argument substitution. */
21161 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21162 /* We don't have all the template args yet. */
21163 bool incomplete
= true;
21166 if (flag_new_inheriting_ctors
)
21167 fn
= strip_inheriting_ctors (fn
);
21169 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21170 tree r
= error_mark_node
;
21172 tree full_targs
= targs
;
21173 if (TMPL_ARGS_DEPTH (targs
)
21174 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21175 full_targs
= (add_outermost_template_args
21176 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21180 complain
|= tf_decltype
;
21182 /* In C++0x, it's possible to have a function template whose type depends
21183 on itself recursively. This is most obvious with decltype, but can also
21184 occur with enumeration scope (c++/48969). So we need to catch infinite
21185 recursion and reject the substitution at deduction time; this function
21186 will return error_mark_node for any repeated substitution.
21188 This also catches excessive recursion such as when f<N> depends on
21189 f<N-1> across all integers, and returns error_mark_node for all the
21190 substitutions back up to the initial one.
21192 This is, of course, not reentrant. */
21193 if (excessive_deduction_depth
)
21194 return error_mark_node
;
21197 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21199 fntype
= TREE_TYPE (fn
);
21200 if (explicit_targs
)
21204 The specified template arguments must match the template
21205 parameters in kind (i.e., type, nontype, template), and there
21206 must not be more arguments than there are parameters;
21207 otherwise type deduction fails.
21209 Nontype arguments must match the types of the corresponding
21210 nontype template parameters, or must be convertible to the
21211 types of the corresponding nontype parameters as specified in
21212 _temp.arg.nontype_, otherwise type deduction fails.
21214 All references in the function type of the function template
21215 to the corresponding template parameters are replaced by the
21216 specified template argument values. If a substitution in a
21217 template parameter or in the function type of the function
21218 template results in an invalid type, type deduction fails. */
21219 int i
, len
= TREE_VEC_LENGTH (tparms
);
21220 location_t loc
= input_location
;
21221 incomplete
= false;
21223 if (explicit_targs
== error_mark_node
)
21226 if (TMPL_ARGS_DEPTH (explicit_targs
)
21227 < TMPL_ARGS_DEPTH (full_targs
))
21228 explicit_targs
= add_outermost_template_args (full_targs
,
21231 /* Adjust any explicit template arguments before entering the
21232 substitution context. */
21234 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21235 complain
|tf_partial
,
21236 /*require_all_args=*/false,
21237 /*use_default_args=*/false));
21238 if (explicit_targs
== error_mark_node
)
21241 /* Substitute the explicit args into the function type. This is
21242 necessary so that, for instance, explicitly declared function
21243 arguments can match null pointed constants. If we were given
21244 an incomplete set of explicit args, we must not do semantic
21245 processing during substitution as we could create partial
21247 for (i
= 0; i
< len
; i
++)
21249 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21250 bool parameter_pack
= false;
21251 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21253 /* Dig out the actual parm. */
21254 if (TREE_CODE (parm
) == TYPE_DECL
21255 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21257 parm
= TREE_TYPE (parm
);
21258 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21260 else if (TREE_CODE (parm
) == PARM_DECL
)
21262 parm
= DECL_INITIAL (parm
);
21263 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21266 if (targ
== NULL_TREE
)
21267 /* No explicit argument for this template parameter. */
21269 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21271 /* Mark the argument pack as "incomplete". We could
21272 still deduce more arguments during unification.
21273 We remove this mark in type_unification_real. */
21274 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21275 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21276 = ARGUMENT_PACK_ARGS (targ
);
21278 /* We have some incomplete argument packs. */
21285 if (!push_tinst_level (fn
, explicit_targs
))
21287 excessive_deduction_depth
= true;
21290 ++processing_template_decl
;
21291 input_location
= DECL_SOURCE_LOCATION (fn
);
21292 /* Ignore any access checks; we'll see them again in
21293 instantiate_template and they might have the wrong
21294 access path at this point. */
21295 push_deferring_access_checks (dk_deferred
);
21296 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21297 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21298 pop_deferring_access_checks ();
21299 input_location
= loc
;
21300 --processing_template_decl
;
21301 pop_tinst_level ();
21303 if (fntype
== error_mark_node
)
21307 /* Place the explicitly specified arguments in TARGS. */
21308 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21309 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21310 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21311 if (!incomplete
&& CHECKING_P
21312 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21314 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21317 if (return_type
&& strict
!= DEDUCE_CALL
)
21319 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21320 new_args
[0] = return_type
;
21321 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21329 /* Never do unification on the 'this' parameter. */
21330 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21332 if (return_type
&& strict
== DEDUCE_CALL
)
21334 /* We're deducing for a call to the result of a template conversion
21335 function. The parms we really want are in return_type. */
21336 if (INDIRECT_TYPE_P (return_type
))
21337 return_type
= TREE_TYPE (return_type
);
21338 parms
= TYPE_ARG_TYPES (return_type
);
21340 else if (return_type
)
21342 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21345 /* We allow incomplete unification without an error message here
21346 because the standard doesn't seem to explicitly prohibit it. Our
21347 callers must be ready to deal with unification failures in any
21350 /* If we aren't explaining yet, push tinst context so we can see where
21351 any errors (e.g. from class instantiations triggered by instantiation
21352 of default template arguments) come from. If we are explaining, this
21353 context is redundant. */
21354 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21356 excessive_deduction_depth
= true;
21360 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21361 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21362 strict
, &checks
, explain_p
);
21364 pop_tinst_level ();
21368 /* Now that we have bindings for all of the template arguments,
21369 ensure that the arguments deduced for the template template
21370 parameters have compatible template parameter lists. We cannot
21371 check this property before we have deduced all template
21372 arguments, because the template parameter types of a template
21373 template parameter might depend on prior template parameters
21374 deduced after the template template parameter. The following
21375 ill-formed example illustrates this issue:
21377 template<typename T, template<T> class C> void f(C<5>, T);
21379 template<int N> struct X {};
21382 f(X<5>(), 5l); // error: template argument deduction fails
21385 The template parameter list of 'C' depends on the template type
21386 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21387 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21388 time that we deduce 'C'. */
21389 if (!template_template_parm_bindings_ok_p
21390 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21392 unify_inconsistent_template_template_parameters (explain_p
);
21398 /* CWG2369: Check satisfaction before non-deducible conversions. */
21399 if (!constraints_satisfied_p (fn
, targs
))
21402 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21406 /* DR 1391: All parameters have args, now check non-dependent parms for
21407 convertibility. We don't do this if all args were explicitly specified,
21408 as the standard says that we substitute explicit args immediately. */
21410 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21414 /* All is well so far. Now, check:
21418 When all template arguments have been deduced, all uses of
21419 template parameters in nondeduced contexts are replaced with
21420 the corresponding deduced argument values. If the
21421 substitution results in an invalid type, as described above,
21422 type deduction fails. */
21423 if (!push_tinst_level (fn
, targs
))
21425 excessive_deduction_depth
= true;
21429 /* Also collect access checks from the instantiation. */
21430 reopen_deferring_access_checks (checks
);
21432 decl
= instantiate_template (fn
, targs
, complain
);
21434 checks
= get_deferred_access_checks ();
21435 pop_deferring_access_checks ();
21437 pop_tinst_level ();
21439 if (decl
== error_mark_node
)
21442 /* Now perform any access checks encountered during substitution. */
21443 push_access_scope (decl
);
21444 ok
= perform_access_checks (checks
, complain
);
21445 pop_access_scope (decl
);
21449 /* If we're looking for an exact match, check that what we got
21450 is indeed an exact match. It might not be if some template
21451 parameters are used in non-deduced contexts. But don't check
21452 for an exact match if we have dependent template arguments;
21453 in that case we're doing partial ordering, and we already know
21454 that we have two candidates that will provide the actual type. */
21455 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21457 tree substed
= TREE_TYPE (decl
);
21461 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21463 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21464 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21465 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21467 unify_type_mismatch (explain_p
, args
[i
],
21468 TREE_VALUE (sarg
));
21473 /* After doing deduction with the inherited constructor, actually return an
21474 instantiation of the inheriting constructor. */
21476 decl
= instantiate_template (orig_fn
, targs
, complain
);
21482 if (excessive_deduction_depth
)
21484 if (deduction_depth
== 0)
21485 /* Reset once we're all the way out. */
21486 excessive_deduction_depth
= false;
21492 /* Adjust types before performing type deduction, as described in
21493 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21494 sections are symmetric. PARM is the type of a function parameter
21495 or the return type of the conversion function. ARG is the type of
21496 the argument passed to the call, or the type of the value
21497 initialized with the result of the conversion function.
21498 ARG_EXPR is the original argument expression, which may be null. */
21501 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21514 /* Swap PARM and ARG throughout the remainder of this
21515 function; the handling is precisely symmetric since PARM
21516 will initialize ARG rather than vice versa. */
21517 std::swap (parm
, arg
);
21521 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21522 too, but here handle it by stripping the reference from PARM
21523 rather than by adding it to ARG. */
21524 if (TYPE_REF_P (*parm
)
21525 && TYPE_REF_IS_RVALUE (*parm
)
21526 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21527 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21528 && TYPE_REF_P (*arg
)
21529 && !TYPE_REF_IS_RVALUE (*arg
))
21530 *parm
= TREE_TYPE (*parm
);
21531 /* Nothing else to do in this case. */
21535 gcc_unreachable ();
21538 if (!TYPE_REF_P (*parm
))
21540 /* [temp.deduct.call]
21542 If P is not a reference type:
21544 --If A is an array type, the pointer type produced by the
21545 array-to-pointer standard conversion (_conv.array_) is
21546 used in place of A for type deduction; otherwise,
21548 --If A is a function type, the pointer type produced by
21549 the function-to-pointer standard conversion
21550 (_conv.func_) is used in place of A for type deduction;
21553 --If A is a cv-qualified type, the top level
21554 cv-qualifiers of A's type are ignored for type
21556 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21557 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21558 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21559 *arg
= build_pointer_type (*arg
);
21561 *arg
= TYPE_MAIN_VARIANT (*arg
);
21564 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21565 reference to a cv-unqualified template parameter that does not represent a
21566 template parameter of a class template (during class template argument
21567 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21568 an lvalue, the type "lvalue reference to A" is used in place of A for type
21570 if (TYPE_REF_P (*parm
)
21571 && TYPE_REF_IS_RVALUE (*parm
)
21572 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21573 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21574 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21575 && (arg_expr
? lvalue_p (arg_expr
)
21576 /* try_one_overload doesn't provide an arg_expr, but
21577 functions are always lvalues. */
21578 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21579 *arg
= build_reference_type (*arg
);
21581 /* [temp.deduct.call]
21583 If P is a cv-qualified type, the top level cv-qualifiers
21584 of P's type are ignored for type deduction. If P is a
21585 reference type, the type referred to by P is used for
21587 *parm
= TYPE_MAIN_VARIANT (*parm
);
21588 if (TYPE_REF_P (*parm
))
21590 *parm
= TREE_TYPE (*parm
);
21591 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21594 /* DR 322. For conversion deduction, remove a reference type on parm
21595 too (which has been swapped into ARG). */
21596 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21597 *arg
= TREE_TYPE (*arg
);
21602 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21603 template which doesn't contain any deducible template parameters; check if
21604 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21605 unify_one_argument. */
21608 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21609 int flags
, struct conversion
**conv_p
,
21615 type
= TREE_TYPE (arg
);
21619 if (same_type_p (parm
, type
))
21620 return unify_success (explain_p
);
21622 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21623 if (strict
== DEDUCE_CONV
)
21625 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21626 return unify_success (explain_p
);
21628 else if (strict
!= DEDUCE_EXACT
)
21631 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21633 /* Avoid recalculating this in add_function_candidate. */
21635 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21637 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21639 return unify_success (explain_p
);
21642 if (strict
== DEDUCE_EXACT
)
21643 return unify_type_mismatch (explain_p
, parm
, arg
);
21645 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21648 static bool uses_deducible_template_parms (tree type
);
21650 /* Returns true iff the expression EXPR is one from which a template
21651 argument can be deduced. In other words, if it's an undecorated
21652 use of a template non-type parameter. */
21655 deducible_expression (tree expr
)
21657 /* Strip implicit conversions. */
21658 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21659 expr
= TREE_OPERAND (expr
, 0);
21660 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21663 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21664 deducible way; that is, if it has a max value of <PARM> - 1. */
21667 deducible_array_bound (tree domain
)
21669 if (domain
== NULL_TREE
)
21672 tree max
= TYPE_MAX_VALUE (domain
);
21673 if (TREE_CODE (max
) != MINUS_EXPR
)
21676 return deducible_expression (TREE_OPERAND (max
, 0));
21679 /* Returns true iff the template arguments ARGS use a template parameter
21680 in a deducible way. */
21683 deducible_template_args (tree args
)
21685 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21688 tree elt
= TREE_VEC_ELT (args
, i
);
21689 if (ARGUMENT_PACK_P (elt
))
21690 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21693 if (PACK_EXPANSION_P (elt
))
21694 elt
= PACK_EXPANSION_PATTERN (elt
);
21695 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21697 else if (TYPE_P (elt
))
21698 deducible
= uses_deducible_template_parms (elt
);
21700 deducible
= deducible_expression (elt
);
21708 /* Returns true iff TYPE contains any deducible references to template
21709 parameters, as per 14.8.2.5. */
21712 uses_deducible_template_parms (tree type
)
21714 if (PACK_EXPANSION_P (type
))
21715 type
= PACK_EXPANSION_PATTERN (type
);
21722 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21723 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21729 if (INDIRECT_TYPE_P (type
))
21730 return uses_deducible_template_parms (TREE_TYPE (type
));
21732 /* T[integer-constant ]
21734 if (TREE_CODE (type
) == ARRAY_TYPE
)
21735 return (uses_deducible_template_parms (TREE_TYPE (type
))
21736 || deducible_array_bound (TYPE_DOMAIN (type
)));
21748 if (TYPE_PTRMEM_P (type
))
21749 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21750 || (uses_deducible_template_parms
21751 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21753 /* template-name <T> (where template-name refers to a class template)
21754 template-name <i> (where template-name refers to a class template) */
21755 if (CLASS_TYPE_P (type
)
21756 && CLASSTYPE_TEMPLATE_INFO (type
)
21757 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21758 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21759 (CLASSTYPE_TI_ARGS (type
)));
21764 if (FUNC_OR_METHOD_TYPE_P (type
))
21766 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21768 tree parm
= TYPE_ARG_TYPES (type
);
21769 if (TREE_CODE (type
) == METHOD_TYPE
)
21770 parm
= TREE_CHAIN (parm
);
21771 for (; parm
; parm
= TREE_CHAIN (parm
))
21772 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21779 /* Subroutine of type_unification_real and unify_pack_expansion to
21780 handle unification of a single P/A pair. Parameters are as
21781 for those functions. */
21784 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21785 int subr
, unification_kind_t strict
,
21788 tree arg_expr
= NULL_TREE
;
21791 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21792 return unify_invalid (explain_p
);
21793 if (arg
== unknown_type_node
)
21794 /* We can't deduce anything from this, but we might get all the
21795 template args from other function args. */
21796 return unify_success (explain_p
);
21798 /* Implicit conversions (Clause 4) will be performed on a function
21799 argument to convert it to the type of the corresponding function
21800 parameter if the parameter type contains no template-parameters that
21801 participate in template argument deduction. */
21802 if (strict
!= DEDUCE_EXACT
21803 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21804 /* For function parameters with no deducible template parameters,
21805 just return. We'll check non-dependent conversions later. */
21806 return unify_success (explain_p
);
21811 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21812 | UNIFY_ALLOW_MORE_CV_QUAL
21813 | UNIFY_ALLOW_DERIVED
);
21817 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21821 arg_strict
= UNIFY_ALLOW_NONE
;
21825 gcc_unreachable ();
21828 /* We only do these transformations if this is the top-level
21829 parameter_type_list in a call or declaration matching; in other
21830 situations (nested function declarators, template argument lists) we
21831 won't be comparing a type to an expression, and we don't do any type
21837 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21838 if (type_unknown_p (arg
))
21840 /* [temp.deduct.type] A template-argument can be
21841 deduced from a pointer to function or pointer
21842 to member function argument if the set of
21843 overloaded functions does not contain function
21844 templates and at most one of a set of
21845 overloaded functions provides a unique
21847 resolve_overloaded_unification (tparms
, targs
, parm
,
21849 arg_strict
, explain_p
);
21850 /* If a unique match was not found, this is a
21851 non-deduced context, so we still succeed. */
21852 return unify_success (explain_p
);
21856 arg
= unlowered_expr_type (arg
);
21857 if (arg
== error_mark_node
)
21858 return unify_invalid (explain_p
);
21862 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21865 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21866 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21867 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21869 /* For deduction from an init-list we need the actual list. */
21870 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21872 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21875 /* for_each_template_parm callback that always returns 0. */
21878 zero_r (tree
, void *)
21883 /* for_each_template_parm any_fn callback to handle deduction of a template
21884 type argument from the type of an array bound. */
21887 array_deduction_r (tree t
, void *data
)
21889 tree_pair_p d
= (tree_pair_p
)data
;
21890 tree
&tparms
= d
->purpose
;
21891 tree
&targs
= d
->value
;
21893 if (TREE_CODE (t
) == ARRAY_TYPE
)
21894 if (tree dom
= TYPE_DOMAIN (t
))
21895 if (tree max
= TYPE_MAX_VALUE (dom
))
21897 if (TREE_CODE (max
) == MINUS_EXPR
)
21898 max
= TREE_OPERAND (max
, 0);
21899 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21900 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21901 UNIFY_ALLOW_NONE
, /*explain*/false);
21904 /* Keep walking. */
21908 /* Try to deduce any not-yet-deduced template type arguments from the type of
21909 an array bound. This is handled separately from unify because 14.8.2.5 says
21910 "The type of a type parameter is only deduced from an array bound if it is
21911 not otherwise deduced." */
21914 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21916 tree_pair_s data
= { tparms
, targs
};
21917 hash_set
<tree
> visited
;
21918 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21919 /*nondeduced*/false, array_deduction_r
);
21922 /* Most parms like fn_type_unification.
21924 If SUBR is 1, we're being called recursively (to unify the
21925 arguments of a function or method parameter of a function
21928 CHECKS is a pointer to a vector of access checks encountered while
21929 substituting default template arguments. */
21932 type_unification_real (tree tparms
,
21936 unsigned int xnargs
,
21938 unification_kind_t strict
,
21939 vec
<deferred_access_check
, va_gc
> **checks
,
21944 int ntparms
= TREE_VEC_LENGTH (tparms
);
21945 int saw_undeduced
= 0;
21948 unsigned int nargs
;
21951 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21952 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21953 gcc_assert (ntparms
> 0);
21955 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21957 /* Reset the number of non-defaulted template arguments contained
21959 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21967 while (parms
&& parms
!= void_list_node
21970 parm
= TREE_VALUE (parms
);
21972 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21973 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21974 /* For a function parameter pack that occurs at the end of the
21975 parameter-declaration-list, the type A of each remaining
21976 argument of the call is compared with the type P of the
21977 declarator-id of the function parameter pack. */
21980 parms
= TREE_CHAIN (parms
);
21982 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21983 /* For a function parameter pack that does not occur at the
21984 end of the parameter-declaration-list, the type of the
21985 parameter pack is a non-deduced context. */
21991 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21997 && parms
!= void_list_node
21998 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
22000 /* Unify the remaining arguments with the pack expansion type. */
22002 tree parmvec
= make_tree_vec (1);
22004 /* Allocate a TREE_VEC and copy in all of the arguments */
22005 argvec
= make_tree_vec (nargs
- ia
);
22006 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
22007 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
22009 /* Copy the parameter into parmvec. */
22010 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
22011 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
22012 /*subr=*/subr
, explain_p
))
22015 /* Advance to the end of the list of parameters. */
22016 parms
= TREE_CHAIN (parms
);
22019 /* Fail if we've reached the end of the parm list, and more args
22020 are present, and the parm list isn't variadic. */
22021 if (ia
< nargs
&& parms
== void_list_node
)
22022 return unify_too_many_arguments (explain_p
, nargs
, ia
);
22023 /* Fail if parms are left and they don't have default values and
22024 they aren't all deduced as empty packs (c++/57397). This is
22025 consistent with sufficient_parms_p. */
22026 if (parms
&& parms
!= void_list_node
22027 && TREE_PURPOSE (parms
) == NULL_TREE
)
22029 unsigned int count
= nargs
;
22034 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
22037 p
= TREE_CHAIN (p
);
22039 while (p
&& p
!= void_list_node
);
22040 if (count
!= nargs
)
22041 return unify_too_few_arguments (explain_p
, ia
, count
,
22047 tsubst_flags_t complain
= (explain_p
22048 ? tf_warning_or_error
22050 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22052 for (i
= 0; i
< ntparms
; i
++)
22054 tree targ
= TREE_VEC_ELT (targs
, i
);
22055 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22057 /* Clear the "incomplete" flags on all argument packs now so that
22058 substituting them into later default arguments works. */
22059 if (targ
&& ARGUMENT_PACK_P (targ
))
22061 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22062 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22065 if (targ
|| tparm
== error_mark_node
)
22067 tparm
= TREE_VALUE (tparm
);
22069 if (TREE_CODE (tparm
) == TYPE_DECL
22070 && !tried_array_deduction
)
22072 try_array_deduction (tparms
, targs
, xparms
);
22073 tried_array_deduction
= true;
22074 if (TREE_VEC_ELT (targs
, i
))
22078 /* If this is an undeduced nontype parameter that depends on
22079 a type parameter, try another pass; its type may have been
22080 deduced from a later argument than the one from which
22081 this parameter can be deduced. */
22082 if (TREE_CODE (tparm
) == PARM_DECL
22083 && uses_template_parms (TREE_TYPE (tparm
))
22084 && saw_undeduced
< 2)
22090 /* Core issue #226 (C++0x) [temp.deduct]:
22092 If a template argument has not been deduced, its
22093 default template argument, if any, is used.
22095 When we are in C++98 mode, TREE_PURPOSE will either
22096 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22097 to explicitly check cxx_dialect here. */
22098 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22099 /* OK, there is a default argument. Wait until after the
22100 conversion check to do substitution. */
22103 /* If the type parameter is a parameter pack, then it will
22104 be deduced to an empty parameter pack. */
22105 if (template_parameter_pack_p (tparm
))
22109 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22111 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22112 TREE_CONSTANT (arg
) = 1;
22115 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22117 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22119 TREE_VEC_ELT (targs
, i
) = arg
;
22123 return unify_parameter_deduction_failure (explain_p
, tparm
);
22126 /* Now substitute into the default template arguments. */
22127 for (i
= 0; i
< ntparms
; i
++)
22129 tree targ
= TREE_VEC_ELT (targs
, i
);
22130 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22132 if (targ
|| tparm
== error_mark_node
)
22134 tree parm
= TREE_VALUE (tparm
);
22135 tree arg
= TREE_PURPOSE (tparm
);
22136 reopen_deferring_access_checks (*checks
);
22137 location_t save_loc
= input_location
;
22139 input_location
= DECL_SOURCE_LOCATION (parm
);
22141 if (saw_undeduced
== 1
22142 && TREE_CODE (parm
) == PARM_DECL
22143 && uses_template_parms (TREE_TYPE (parm
)))
22145 /* The type of this non-type parameter depends on undeduced
22146 parameters. Don't try to use its default argument yet,
22147 since we might deduce an argument for it on the next pass,
22148 but do check whether the arguments we already have cause
22149 substitution failure, so that that happens before we try
22150 later default arguments (78489). */
22151 ++processing_template_decl
;
22152 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22154 --processing_template_decl
;
22155 if (type
== error_mark_node
)
22156 arg
= error_mark_node
;
22162 /* Even if the call is happening in template context, getting
22163 here means it's non-dependent, and a default argument is
22164 considered a separate definition under [temp.decls], so we can
22165 do this substitution without processing_template_decl. This
22166 is important if the default argument contains something that
22167 might be instantiation-dependent like access (87480). */
22168 processing_template_decl_sentinel s
;
22169 tree substed
= NULL_TREE
;
22170 if (saw_undeduced
== 1)
22172 /* First instatiate in template context, in case we still
22173 depend on undeduced template parameters. */
22174 ++processing_template_decl
;
22175 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22177 --processing_template_decl
;
22178 if (substed
!= error_mark_node
22179 && !uses_template_parms (substed
))
22180 /* We replaced all the tparms, substitute again out of
22181 template context. */
22182 substed
= NULL_TREE
;
22185 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22188 if (!uses_template_parms (substed
))
22189 arg
= convert_template_argument (parm
, substed
, full_targs
,
22190 complain
, i
, NULL_TREE
);
22191 else if (saw_undeduced
== 1)
22194 arg
= error_mark_node
;
22197 input_location
= save_loc
;
22198 *checks
= get_deferred_access_checks ();
22199 pop_deferring_access_checks ();
22201 if (arg
== error_mark_node
)
22205 TREE_VEC_ELT (targs
, i
) = arg
;
22206 /* The position of the first default template argument,
22207 is also the number of non-defaulted arguments in TARGS.
22209 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22210 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22214 if (saw_undeduced
++ == 1)
22218 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22219 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22221 return unify_success (explain_p
);
22224 /* Subroutine of type_unification_real. Args are like the variables
22225 at the call site. ARG is an overloaded function (or template-id);
22226 we try deducing template args from each of the overloads, and if
22227 only one succeeds, we go with that. Modifies TARGS and returns
22228 true on success. */
22231 resolve_overloaded_unification (tree tparms
,
22235 unification_kind_t strict
,
22239 tree tempargs
= copy_node (targs
);
22241 tree goodfn
= NULL_TREE
;
22244 if (TREE_CODE (arg
) == ADDR_EXPR
)
22246 arg
= TREE_OPERAND (arg
, 0);
22252 if (TREE_CODE (arg
) == COMPONENT_REF
)
22253 /* Handle `&x' where `x' is some static or non-static member
22255 arg
= TREE_OPERAND (arg
, 1);
22257 if (TREE_CODE (arg
) == OFFSET_REF
)
22258 arg
= TREE_OPERAND (arg
, 1);
22260 /* Strip baselink information. */
22261 if (BASELINK_P (arg
))
22262 arg
= BASELINK_FUNCTIONS (arg
);
22264 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22266 /* If we got some explicit template args, we need to plug them into
22267 the affected templates before we try to unify, in case the
22268 explicit args will completely resolve the templates in question. */
22271 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22272 arg
= TREE_OPERAND (arg
, 0);
22274 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22277 tree subargs
, elem
;
22279 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22282 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22283 expl_subargs
, NULL_TREE
, tf_none
,
22284 /*require_all_args=*/true,
22285 /*use_default_args=*/true);
22286 if (subargs
!= error_mark_node
22287 && !any_dependent_template_arguments_p (subargs
))
22289 fn
= instantiate_template (fn
, subargs
, tf_none
);
22290 if (!constraints_satisfied_p (fn
))
22292 if (undeduced_auto_decl (fn
))
22294 /* Instantiate the function to deduce its return type. */
22296 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22300 elem
= TREE_TYPE (fn
);
22301 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22302 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22303 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22312 /* If no templates (or more than one) are fully resolved by the
22313 explicit arguments, this template-id is a non-deduced context; it
22314 could still be OK if we deduce all template arguments for the
22315 enclosing call through other arguments. */
22319 else if (!OVL_P (arg
))
22320 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22321 -- but the deduction does not succeed because the expression is
22322 not just the function on its own. */
22325 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22328 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22329 strict
, sub_strict
, addr_p
, explain_p
)
22330 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22337 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22338 to function or pointer to member function argument if the set of
22339 overloaded functions does not contain function templates and at most
22340 one of a set of overloaded functions provides a unique match.
22342 So if we found multiple possibilities, we return success but don't
22343 deduce anything. */
22347 int i
= TREE_VEC_LENGTH (targs
);
22349 if (TREE_VEC_ELT (tempargs
, i
))
22351 tree old
= TREE_VEC_ELT (targs
, i
);
22352 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22353 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22354 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22355 /* Don't forget explicit template arguments in a pack. */
22356 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22357 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22358 TREE_VEC_ELT (targs
, i
) = new_
;
22367 /* Core DR 115: In contexts where deduction is done and fails, or in
22368 contexts where deduction is not done, if a template argument list is
22369 specified and it, along with any default template arguments, identifies
22370 a single function template specialization, then the template-id is an
22371 lvalue for the function template specialization. */
22374 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22376 tree expr
, offset
, baselink
;
22379 if (!type_unknown_p (orig_expr
))
22384 offset
= NULL_TREE
;
22385 baselink
= NULL_TREE
;
22387 if (TREE_CODE (expr
) == ADDR_EXPR
)
22389 expr
= TREE_OPERAND (expr
, 0);
22392 if (TREE_CODE (expr
) == OFFSET_REF
)
22395 expr
= TREE_OPERAND (expr
, 1);
22397 if (BASELINK_P (expr
))
22400 expr
= BASELINK_FUNCTIONS (expr
);
22403 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22406 tree goodfn
= NULL_TREE
;
22408 /* If we got some explicit template args, we need to plug them into
22409 the affected templates before we try to unify, in case the
22410 explicit args will completely resolve the templates in question. */
22412 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22413 tree arg
= TREE_OPERAND (expr
, 0);
22414 tree badfn
= NULL_TREE
;
22415 tree badargs
= NULL_TREE
;
22417 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22420 tree subargs
, elem
;
22422 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22425 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22426 expl_subargs
, NULL_TREE
, tf_none
,
22427 /*require_all_args=*/true,
22428 /*use_default_args=*/true);
22429 if (subargs
!= error_mark_node
22430 && !any_dependent_template_arguments_p (subargs
))
22432 elem
= instantiate_template (fn
, subargs
, tf_none
);
22433 if (elem
== error_mark_node
)
22438 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22439 && constraints_satisfied_p (elem
))
22448 mark_used (goodfn
);
22451 expr
= build_baselink (BASELINK_BINFO (baselink
),
22452 BASELINK_ACCESS_BINFO (baselink
),
22453 expr
, BASELINK_OPTYPE (baselink
));
22457 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22458 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22461 expr
= cp_build_addr_expr (expr
, complain
);
22464 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22465 /* There were no good options and at least one bad one, so let the
22466 user know what the problem is. */
22467 instantiate_template (badfn
, badargs
, complain
);
22472 /* As above, but error out if the expression remains overloaded. */
22475 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22477 exp
= resolve_nondeduced_context (exp
, complain
);
22478 if (type_unknown_p (exp
))
22480 if (complain
& tf_error
)
22481 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22482 return error_mark_node
;
22487 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22488 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22489 different overloads deduce different arguments for a given parm.
22490 ADDR_P is true if the expression for which deduction is being
22491 performed was of the form "& fn" rather than simply "fn".
22493 Returns 1 on success. */
22496 try_one_overload (tree tparms
,
22501 unification_kind_t strict
,
22510 if (arg
== error_mark_node
)
22513 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22514 to function or pointer to member function argument if the set of
22515 overloaded functions does not contain function templates and at most
22516 one of a set of overloaded functions provides a unique match.
22518 So if this is a template, just return success. */
22520 if (uses_template_parms (arg
))
22523 if (TREE_CODE (arg
) == METHOD_TYPE
)
22524 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22526 arg
= build_pointer_type (arg
);
22528 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22530 /* We don't copy orig_targs for this because if we have already deduced
22531 some template args from previous args, unify would complain when we
22532 try to deduce a template parameter for the same argument, even though
22533 there isn't really a conflict. */
22534 nargs
= TREE_VEC_LENGTH (targs
);
22535 tempargs
= make_tree_vec (nargs
);
22537 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22540 /* First make sure we didn't deduce anything that conflicts with
22541 explicitly specified args. */
22542 for (i
= nargs
; i
--; )
22544 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22545 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22549 else if (uses_template_parms (elt
))
22550 /* Since we're unifying against ourselves, we will fill in
22551 template args used in the function parm list with our own
22552 template parms. Discard them. */
22553 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22554 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22556 /* Check that the argument at each index of the deduced argument pack
22557 is equivalent to the corresponding explicitly specified argument.
22558 We may have deduced more arguments than were explicitly specified,
22561 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22562 that's wrong if we deduce the same argument pack from multiple
22563 function arguments: it's only incomplete the first time. */
22565 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22566 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22568 if (TREE_VEC_LENGTH (deduced_pack
)
22569 < TREE_VEC_LENGTH (explicit_pack
))
22572 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22573 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22574 TREE_VEC_ELT (deduced_pack
, j
)))
22577 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22581 for (i
= nargs
; i
--; )
22583 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22586 TREE_VEC_ELT (targs
, i
) = elt
;
22592 /* PARM is a template class (perhaps with unbound template
22593 parameters). ARG is a fully instantiated type. If ARG can be
22594 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22595 TARGS are as for unify. */
22598 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22601 tree copy_of_targs
;
22603 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22605 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22606 /* Matches anything. */;
22607 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22608 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22611 /* We need to make a new template argument vector for the call to
22612 unify. If we used TARGS, we'd clutter it up with the result of
22613 the attempted unification, even if this class didn't work out.
22614 We also don't want to commit ourselves to all the unifications
22615 we've already done, since unification is supposed to be done on
22616 an argument-by-argument basis. In other words, consider the
22617 following pathological case:
22619 template <int I, int J, int K>
22622 template <int I, int J>
22623 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22625 template <int I, int J, int K>
22626 void f(S<I, J, K>, S<I, I, I>);
22635 Now, by the time we consider the unification involving `s2', we
22636 already know that we must have `f<0, 0, 0>'. But, even though
22637 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22638 because there are two ways to unify base classes of S<0, 1, 2>
22639 with S<I, I, I>. If we kept the already deduced knowledge, we
22640 would reject the possibility I=1. */
22641 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22643 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22645 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22650 /* If unification failed, we're done. */
22651 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22652 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22658 /* Given a template type PARM and a class type ARG, find the unique
22659 base type in ARG that is an instance of PARM. We do not examine
22660 ARG itself; only its base-classes. If there is not exactly one
22661 appropriate base class, return NULL_TREE. PARM may be the type of
22662 a partial specialization, as well as a plain template type. Used
22665 static enum template_base_result
22666 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22667 bool explain_p
, tree
*result
)
22669 tree rval
= NULL_TREE
;
22672 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22674 binfo
= TYPE_BINFO (complete_type (arg
));
22677 /* The type could not be completed. */
22678 *result
= NULL_TREE
;
22679 return tbr_incomplete_type
;
22682 /* Walk in inheritance graph order. The search order is not
22683 important, and this avoids multiple walks of virtual bases. */
22684 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22686 tree r
= try_class_unification (tparms
, targs
, parm
,
22687 BINFO_TYPE (binfo
), explain_p
);
22691 /* If there is more than one satisfactory baseclass, then:
22695 If they yield more than one possible deduced A, the type
22699 if (rval
&& !same_type_p (r
, rval
))
22701 /* [temp.deduct.call]/4.3: If there is a class C that is a
22702 (direct or indirect) base class of D and derived (directly or
22703 indirectly) from a class B and that would be a valid deduced
22704 A, the deduced A cannot be B or pointer to B, respectively. */
22705 if (DERIVED_FROM_P (r
, rval
))
22708 else if (DERIVED_FROM_P (rval
, r
))
22709 /* Ignore rval. */;
22712 *result
= NULL_TREE
;
22713 return tbr_ambiguous_baseclass
;
22722 return tbr_success
;
22725 /* Returns the level of DECL, which declares a template parameter. */
22728 template_decl_level (tree decl
)
22730 switch (TREE_CODE (decl
))
22733 case TEMPLATE_DECL
:
22734 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22737 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22740 gcc_unreachable ();
22745 /* Decide whether ARG can be unified with PARM, considering only the
22746 cv-qualifiers of each type, given STRICT as documented for unify.
22747 Returns nonzero iff the unification is OK on that basis. */
22750 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22752 int arg_quals
= cp_type_quals (arg
);
22753 int parm_quals
= cp_type_quals (parm
);
22755 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22756 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22758 /* Although a CVR qualifier is ignored when being applied to a
22759 substituted template parameter ([8.3.2]/1 for example), that
22760 does not allow us to unify "const T" with "int&" because both
22761 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22762 It is ok when we're allowing additional CV qualifiers
22763 at the outer level [14.8.2.1]/3,1st bullet. */
22764 if ((TYPE_REF_P (arg
)
22765 || FUNC_OR_METHOD_TYPE_P (arg
))
22766 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22769 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22770 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22774 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22775 && (arg_quals
& parm_quals
) != parm_quals
)
22778 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22779 && (parm_quals
& arg_quals
) != arg_quals
)
22785 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22787 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22789 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22790 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22791 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22793 *index
= TEMPLATE_TYPE_IDX (parm
);
22794 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22798 *index
= TEMPLATE_PARM_IDX (parm
);
22799 *level
= TEMPLATE_PARM_LEVEL (parm
);
22803 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22805 if (unify (TP, TA, P, A, S, EP)) \
22809 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22810 expansion at the end of PACKED_PARMS. Returns 0 if the type
22811 deduction succeeds, 1 otherwise. STRICT is the same as in
22812 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22813 function call argument list. We'll need to adjust the arguments to make them
22814 types. SUBR tells us if this is from a recursive call to
22815 type_unification_real, or for comparing two template argument
22819 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22820 tree packed_args
, unification_kind_t strict
,
22821 bool subr
, bool explain_p
)
22824 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22825 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22826 tree pack
, packs
= NULL_TREE
;
22827 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22829 /* Add in any args remembered from an earlier partial instantiation. */
22830 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22831 int levels
= TMPL_ARGS_DEPTH (targs
);
22833 packed_args
= expand_template_argument_pack (packed_args
);
22835 int len
= TREE_VEC_LENGTH (packed_args
);
22837 /* Determine the parameter packs we will be deducing from the
22838 pattern, and record their current deductions. */
22839 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22840 pack
; pack
= TREE_CHAIN (pack
))
22842 tree parm_pack
= TREE_VALUE (pack
);
22845 /* Only template parameter packs can be deduced, not e.g. function
22846 parameter packs or __bases or __integer_pack. */
22847 if (!TEMPLATE_PARM_P (parm_pack
))
22850 /* Determine the index and level of this parameter pack. */
22851 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22852 if (level
< levels
)
22855 /* Keep track of the parameter packs and their corresponding
22857 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22858 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22861 /* Loop through all of the arguments that have not yet been
22862 unified and unify each with the pattern. */
22863 for (i
= start
; i
< len
; i
++)
22866 bool any_explicit
= false;
22867 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22869 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22870 or the element of its argument pack at the current index if
22871 this argument was explicitly specified. */
22872 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22876 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22879 if (TREE_VALUE (pack
)
22880 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22881 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22883 any_explicit
= true;
22884 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22886 TMPL_ARG (targs
, level
, idx
) = arg
;
22889 /* If we had explicit template arguments, substitute them into the
22890 pattern before deduction. */
22893 /* Some arguments might still be unspecified or dependent. */
22895 ++processing_template_decl
;
22896 dependent
= any_dependent_template_arguments_p (targs
);
22898 --processing_template_decl
;
22899 parm
= tsubst (pattern
, targs
,
22900 explain_p
? tf_warning_or_error
: tf_none
,
22903 --processing_template_decl
;
22904 if (parm
== error_mark_node
)
22910 /* Unify the pattern with the current argument. */
22911 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22915 /* For each parameter pack, collect the deduced value. */
22916 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22919 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22921 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22922 TMPL_ARG (targs
, level
, idx
);
22926 /* Verify that the results of unification with the parameter packs
22927 produce results consistent with what we've seen before, and make
22928 the deduced argument packs available. */
22929 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22931 tree old_pack
= TREE_VALUE (pack
);
22932 tree new_args
= TREE_TYPE (pack
);
22933 int i
, len
= TREE_VEC_LENGTH (new_args
);
22935 bool nondeduced_p
= false;
22937 /* By default keep the original deduced argument pack.
22938 If necessary, more specific code is going to update the
22939 resulting deduced argument later down in this function. */
22940 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22941 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22943 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22944 actually deduce anything. */
22945 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22946 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22947 nondeduced_p
= true;
22951 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22953 /* If we had fewer function args than explicit template args,
22954 just use the explicits. */
22955 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22956 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22957 if (len
< explicit_len
)
22958 new_args
= explicit_args
;
22964 /* Build the deduced *_ARGUMENT_PACK. */
22965 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22967 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22968 TREE_CONSTANT (result
) = 1;
22971 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22973 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22975 /* Note the deduced argument packs for this parameter
22977 TMPL_ARG (targs
, level
, idx
) = result
;
22979 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22980 && (ARGUMENT_PACK_ARGS (old_pack
)
22981 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22983 /* We only had the explicitly-provided arguments before, but
22984 now we have a complete set of arguments. */
22985 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22987 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22988 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22989 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22993 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22994 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22995 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22996 /* During template argument deduction for the aggregate deduction
22997 candidate, the number of elements in a trailing parameter pack
22998 is only deduced from the number of remaining function
22999 arguments if it is not otherwise deduced. */
23000 if (cxx_dialect
>= cxx20
23001 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
23002 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
23003 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
23004 if (!comp_template_args (old_args
, new_args
,
23005 &bad_old_arg
, &bad_new_arg
))
23006 /* Inconsistent unification of this parameter pack. */
23007 return unify_parameter_pack_inconsistent (explain_p
,
23013 return unify_success (explain_p
);
23016 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23017 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23018 parameters and return value are as for unify. */
23021 unify_array_domain (tree tparms
, tree targs
,
23022 tree parm_dom
, tree arg_dom
,
23030 /* Our representation of array types uses "N - 1" as the
23031 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23032 not an integer constant. We cannot unify arbitrarily
23033 complex expressions, so we eliminate the MINUS_EXPRs
23035 parm_max
= TYPE_MAX_VALUE (parm_dom
);
23036 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
23039 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
23040 parm_max
= TREE_OPERAND (parm_max
, 0);
23042 arg_max
= TYPE_MAX_VALUE (arg_dom
);
23043 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
23046 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23047 trying to unify the type of a variable with the type
23048 of a template parameter. For example:
23050 template <unsigned int N>
23051 void f (char (&) [N]);
23058 Here, the type of the ARG will be "int [g(i)]", and
23059 may be a SAVE_EXPR, etc. */
23060 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
23061 return unify_vla_arg (explain_p
, arg_dom
);
23062 arg_max
= TREE_OPERAND (arg_max
, 0);
23065 /* If only one of the bounds used a MINUS_EXPR, compensate
23066 by adding one to the other bound. */
23067 if (parm_cst
&& !arg_cst
)
23068 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23072 else if (arg_cst
&& !parm_cst
)
23073 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23078 return unify (tparms
, targs
, parm_max
, arg_max
,
23079 UNIFY_ALLOW_INTEGER
, explain_p
);
23082 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23084 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23089 if (PACK_EXPANSION_P (t
))
23090 t
= PACK_EXPANSION_PATTERN (t
);
23091 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23092 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23093 || DECL_TYPE_TEMPLATE_P (t
))
23095 else if (TYPE_P (t
))
23101 /* Deduce the value of template parameters. TPARMS is the (innermost)
23102 set of template parameters to a template. TARGS is the bindings
23103 for those template parameters, as determined thus far; TARGS may
23104 include template arguments for outer levels of template parameters
23105 as well. PARM is a parameter to a template function, or a
23106 subcomponent of that parameter; ARG is the corresponding argument.
23107 This function attempts to match PARM with ARG in a manner
23108 consistent with the existing assignments in TARGS. If more values
23109 are deduced, then TARGS is updated.
23111 Returns 0 if the type deduction succeeds, 1 otherwise. The
23112 parameter STRICT is a bitwise or of the following flags:
23115 Require an exact match between PARM and ARG.
23116 UNIFY_ALLOW_MORE_CV_QUAL:
23117 Allow the deduced ARG to be more cv-qualified (by qualification
23118 conversion) than ARG.
23119 UNIFY_ALLOW_LESS_CV_QUAL:
23120 Allow the deduced ARG to be less cv-qualified than ARG.
23121 UNIFY_ALLOW_DERIVED:
23122 Allow the deduced ARG to be a template base class of ARG,
23123 or a pointer to a template base class of the type pointed to by
23125 UNIFY_ALLOW_INTEGER:
23126 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23127 case for more information.
23128 UNIFY_ALLOW_OUTER_LEVEL:
23129 This is the outermost level of a deduction. Used to determine validity
23130 of qualification conversions. A valid qualification conversion must
23131 have const qualified pointers leading up to the inner type which
23132 requires additional CV quals, except at the outer level, where const
23133 is not required [conv.qual]. It would be normal to set this flag in
23134 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23135 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23136 This is the outermost level of a deduction, and PARM can be more CV
23137 qualified at this point.
23138 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23139 This is the outermost level of a deduction, and PARM can be less CV
23140 qualified at this point. */
23143 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23149 int strict_in
= strict
;
23150 tsubst_flags_t complain
= (explain_p
23151 ? tf_warning_or_error
23154 /* I don't think this will do the right thing with respect to types.
23155 But the only case I've seen it in so far has been array bounds, where
23156 signedness is the only information lost, and I think that will be
23157 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23158 finish_id_expression_1, and are also OK. */
23159 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23160 parm
= TREE_OPERAND (parm
, 0);
23162 if (arg
== error_mark_node
)
23163 return unify_invalid (explain_p
);
23164 if (arg
== unknown_type_node
23165 || arg
== init_list_type_node
)
23166 /* We can't deduce anything from this, but we might get all the
23167 template args from other function args. */
23168 return unify_success (explain_p
);
23170 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23171 return unify_success (explain_p
);
23173 /* If PARM uses template parameters, then we can't bail out here,
23174 even if ARG == PARM, since we won't record unifications for the
23175 template parameters. We might need them if we're trying to
23176 figure out which of two things is more specialized. */
23177 if (arg
== parm
&& !uses_template_parms (parm
))
23178 return unify_success (explain_p
);
23180 /* Handle init lists early, so the rest of the function can assume
23181 we're dealing with a type. */
23182 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23186 tree orig_parm
= parm
;
23188 if (!is_std_init_list (parm
)
23189 && TREE_CODE (parm
) != ARRAY_TYPE
)
23190 /* We can only deduce from an initializer list argument if the
23191 parameter is std::initializer_list or an array; otherwise this
23192 is a non-deduced context. */
23193 return unify_success (explain_p
);
23195 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23196 elttype
= TREE_TYPE (parm
);
23199 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23200 /* Deduction is defined in terms of a single type, so just punt
23201 on the (bizarre) std::initializer_list<T...>. */
23202 if (PACK_EXPANSION_P (elttype
))
23203 return unify_success (explain_p
);
23206 if (strict
!= DEDUCE_EXACT
23207 && TYPE_P (elttype
)
23208 && !uses_deducible_template_parms (elttype
))
23209 /* If ELTTYPE has no deducible template parms, skip deduction from
23210 the list elements. */;
23212 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23214 int elt_strict
= strict
;
23216 if (elt
== error_mark_node
)
23217 return unify_invalid (explain_p
);
23219 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23221 tree type
= TREE_TYPE (elt
);
23222 if (type
== error_mark_node
)
23223 return unify_invalid (explain_p
);
23224 /* It should only be possible to get here for a call. */
23225 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23226 elt_strict
|= maybe_adjust_types_for_deduction
23227 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23231 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23235 if (TREE_CODE (parm
) == ARRAY_TYPE
23236 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23238 /* Also deduce from the length of the initializer list. */
23239 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23240 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23241 if (idx
== error_mark_node
)
23242 return unify_invalid (explain_p
);
23243 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23247 /* If the std::initializer_list<T> deduction worked, replace the
23248 deduced A with std::initializer_list<A>. */
23249 if (orig_parm
!= parm
)
23251 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23252 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23253 targ
= listify (targ
);
23254 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23256 return unify_success (explain_p
);
23259 /* If parm and arg aren't the same kind of thing (template, type, or
23260 expression), fail early. */
23261 if (pa_kind (parm
) != pa_kind (arg
))
23262 return unify_invalid (explain_p
);
23264 /* Immediately reject some pairs that won't unify because of
23265 cv-qualification mismatches. */
23266 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23268 /* It is the elements of the array which hold the cv quals of an array
23269 type, and the elements might be template type parms. We'll check
23270 when we recurse. */
23271 && TREE_CODE (arg
) != ARRAY_TYPE
23272 /* We check the cv-qualifiers when unifying with template type
23273 parameters below. We want to allow ARG `const T' to unify with
23274 PARM `T' for example, when computing which of two templates
23275 is more specialized, for example. */
23276 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23277 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23278 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23280 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23281 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23282 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23283 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23284 strict
&= ~UNIFY_ALLOW_DERIVED
;
23285 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23286 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23288 switch (TREE_CODE (parm
))
23290 case TYPENAME_TYPE
:
23292 case UNBOUND_CLASS_TEMPLATE
:
23293 /* In a type which contains a nested-name-specifier, template
23294 argument values cannot be deduced for template parameters used
23295 within the nested-name-specifier. */
23296 return unify_success (explain_p
);
23298 case TEMPLATE_TYPE_PARM
:
23299 case TEMPLATE_TEMPLATE_PARM
:
23300 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23301 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23302 if (error_operand_p (tparm
))
23303 return unify_invalid (explain_p
);
23305 if (TEMPLATE_TYPE_LEVEL (parm
)
23306 != template_decl_level (tparm
))
23307 /* The PARM is not one we're trying to unify. Just check
23308 to see if it matches ARG. */
23310 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23311 && (is_auto (parm
) ? is_auto (arg
)
23312 : same_type_p (parm
, arg
)))
23313 return unify_success (explain_p
);
23315 return unify_type_mismatch (explain_p
, parm
, arg
);
23317 idx
= TEMPLATE_TYPE_IDX (parm
);
23318 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23319 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23320 if (error_operand_p (tparm
))
23321 return unify_invalid (explain_p
);
23323 /* Check for mixed types and values. */
23324 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23325 && TREE_CODE (tparm
) != TYPE_DECL
)
23326 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23327 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23328 gcc_unreachable ();
23330 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23332 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23333 && CLASS_TYPE_P (arg
))
23335 /* First try to match ARG directly. */
23336 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23340 /* Otherwise, look for a suitable base of ARG, as below. */
23341 enum template_base_result r
;
23342 r
= get_template_base (tparms
, targs
, parm
, arg
,
23345 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23349 /* ARG must be constructed from a template class or a template
23350 template parameter. */
23351 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23352 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23353 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23355 /* Deduce arguments T, i from TT<T> or TT<i>. */
23356 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23359 arg
= TYPE_TI_TEMPLATE (arg
);
23361 /* Fall through to deduce template name. */
23364 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23365 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23367 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23369 /* Simple cases: Value already set, does match or doesn't. */
23370 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23371 return unify_success (explain_p
);
23373 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23377 /* If PARM is `const T' and ARG is only `int', we don't have
23378 a match unless we are allowing additional qualification.
23379 If ARG is `const int' and PARM is just `T' that's OK;
23380 that binds `const int' to `T'. */
23381 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23383 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23385 /* Consider the case where ARG is `const volatile int' and
23386 PARM is `const T'. Then, T should be `volatile int'. */
23387 arg
= cp_build_qualified_type_real
23388 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23389 if (arg
== error_mark_node
)
23390 return unify_invalid (explain_p
);
23392 /* Simple cases: Value already set, does match or doesn't. */
23393 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23394 return unify_success (explain_p
);
23396 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23398 /* Make sure that ARG is not a variable-sized array. (Note
23399 that were talking about variable-sized arrays (like
23400 `int[n]'), rather than arrays of unknown size (like
23401 `int[]').) We'll get very confused by such a type since
23402 the bound of the array is not constant, and therefore
23403 not mangleable. Besides, such types are not allowed in
23404 ISO C++, so we can do as we please here. We do allow
23405 them for 'auto' deduction, since that isn't ABI-exposed. */
23406 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23407 return unify_vla_arg (explain_p
, arg
);
23409 /* Strip typedefs as in convert_template_argument. */
23410 arg
= canonicalize_type_argument (arg
, tf_none
);
23413 /* If ARG is a parameter pack or an expansion, we cannot unify
23414 against it unless PARM is also a parameter pack. */
23415 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23416 && !template_parameter_pack_p (parm
))
23417 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23419 /* If the argument deduction results is a METHOD_TYPE,
23420 then there is a problem.
23421 METHOD_TYPE doesn't map to any real C++ type the result of
23422 the deduction cannot be of that type. */
23423 if (TREE_CODE (arg
) == METHOD_TYPE
)
23424 return unify_method_type_error (explain_p
, arg
);
23426 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23427 return unify_success (explain_p
);
23429 case TEMPLATE_PARM_INDEX
:
23430 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23431 if (error_operand_p (tparm
))
23432 return unify_invalid (explain_p
);
23434 if (TEMPLATE_PARM_LEVEL (parm
)
23435 != template_decl_level (tparm
))
23437 /* The PARM is not one we're trying to unify. Just check
23438 to see if it matches ARG. */
23439 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23440 && cp_tree_equal (parm
, arg
));
23442 unify_expression_unequal (explain_p
, parm
, arg
);
23446 idx
= TEMPLATE_PARM_IDX (parm
);
23447 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23451 if ((strict
& UNIFY_ALLOW_INTEGER
)
23452 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23453 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23454 /* We're deducing from an array bound, the type doesn't matter. */
23455 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23456 int x
= !cp_tree_equal (targ
, arg
);
23458 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23462 /* [temp.deduct.type] If, in the declaration of a function template
23463 with a non-type template-parameter, the non-type
23464 template-parameter is used in an expression in the function
23465 parameter-list and, if the corresponding template-argument is
23466 deduced, the template-argument type shall match the type of the
23467 template-parameter exactly, except that a template-argument
23468 deduced from an array bound may be of any integral type.
23469 The non-type parameter might use already deduced type parameters. */
23470 tparm
= TREE_TYPE (parm
);
23471 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23472 /* We don't have enough levels of args to do any substitution. This
23473 can happen in the context of -fnew-ttp-matching. */;
23476 ++processing_template_decl
;
23477 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23478 --processing_template_decl
;
23480 if (tree a
= type_uses_auto (tparm
))
23482 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23483 if (tparm
== error_mark_node
)
23488 if (!TREE_TYPE (arg
))
23489 /* Template-parameter dependent expression. Just accept it for now.
23490 It will later be processed in convert_template_argument. */
23492 else if (same_type_ignoring_top_level_qualifiers_p
23493 (non_reference (TREE_TYPE (arg
)),
23494 non_reference (tparm
)))
23495 /* OK. Ignore top-level quals here because a class-type template
23496 parameter object is const. */;
23497 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23498 && CP_INTEGRAL_TYPE_P (tparm
))
23499 /* Convert the ARG to the type of PARM; the deduced non-type
23500 template argument must exactly match the types of the
23501 corresponding parameter. */
23502 arg
= fold (build_nop (tparm
, arg
));
23503 else if (uses_template_parms (tparm
))
23505 /* We haven't deduced the type of this parameter yet. */
23506 if (cxx_dialect
>= cxx17
23507 /* We deduce from array bounds in try_array_deduction. */
23508 && !(strict
& UNIFY_ALLOW_INTEGER
))
23510 /* Deduce it from the non-type argument. */
23511 tree atype
= TREE_TYPE (arg
);
23512 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23514 UNIFY_ALLOW_NONE
, explain_p
);
23517 /* Try again later. */
23518 return unify_success (explain_p
);
23521 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23523 /* If ARG is a parameter pack or an expansion, we cannot unify
23524 against it unless PARM is also a parameter pack. */
23525 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23526 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23527 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23530 bool removed_attr
= false;
23531 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23533 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23534 return unify_success (explain_p
);
23538 /* A pointer-to-member constant can be unified only with
23539 another constant. */
23540 if (TREE_CODE (arg
) != PTRMEM_CST
)
23541 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23543 /* Just unify the class member. It would be useless (and possibly
23544 wrong, depending on the strict flags) to unify also
23545 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23546 arg refer to the same variable, even if through different
23547 classes. For instance:
23549 struct A { int x; };
23552 Unification of &A::x and &B::x must succeed. */
23553 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23554 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23559 if (!TYPE_PTR_P (arg
))
23560 return unify_type_mismatch (explain_p
, parm
, arg
);
23562 /* [temp.deduct.call]
23564 A can be another pointer or pointer to member type that can
23565 be converted to the deduced A via a qualification
23566 conversion (_conv.qual_).
23568 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23569 This will allow for additional cv-qualification of the
23570 pointed-to types if appropriate. */
23572 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23573 /* The derived-to-base conversion only persists through one
23574 level of pointers. */
23575 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23577 return unify (tparms
, targs
, TREE_TYPE (parm
),
23578 TREE_TYPE (arg
), strict
, explain_p
);
23581 case REFERENCE_TYPE
:
23582 if (!TYPE_REF_P (arg
))
23583 return unify_type_mismatch (explain_p
, parm
, arg
);
23584 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23585 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23588 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23589 return unify_type_mismatch (explain_p
, parm
, arg
);
23590 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23591 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23592 return unify_type_mismatch (explain_p
, parm
, arg
);
23593 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23594 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23595 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23596 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23597 TYPE_DOMAIN (arg
), explain_p
);
23598 return unify_success (explain_p
);
23605 case ENUMERAL_TYPE
:
23608 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23609 return unify_type_mismatch (explain_p
, parm
, arg
);
23611 /* We have already checked cv-qualification at the top of the
23613 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23614 return unify_type_mismatch (explain_p
, parm
, arg
);
23616 /* As far as unification is concerned, this wins. Later checks
23617 will invalidate it if necessary. */
23618 return unify_success (explain_p
);
23620 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23621 /* Type INTEGER_CST can come from ordinary constant template args. */
23623 while (CONVERT_EXPR_P (arg
))
23624 arg
= TREE_OPERAND (arg
, 0);
23626 if (TREE_CODE (arg
) != INTEGER_CST
)
23627 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23628 return (tree_int_cst_equal (parm
, arg
)
23629 ? unify_success (explain_p
)
23630 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23634 int i
, len
, argslen
;
23635 int parm_variadic_p
= 0;
23637 if (TREE_CODE (arg
) != TREE_VEC
)
23638 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23640 len
= TREE_VEC_LENGTH (parm
);
23641 argslen
= TREE_VEC_LENGTH (arg
);
23643 /* Check for pack expansions in the parameters. */
23644 for (i
= 0; i
< len
; ++i
)
23646 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23649 /* We can unify against something with a trailing
23651 parm_variadic_p
= 1;
23653 /* [temp.deduct.type]/9: If the template argument list of
23654 P contains a pack expansion that is not the last
23655 template argument, the entire template argument list
23656 is a non-deduced context. */
23657 return unify_success (explain_p
);
23661 /* If we don't have enough arguments to satisfy the parameters
23662 (not counting the pack expression at the end), or we have
23663 too many arguments for a parameter list that doesn't end in
23664 a pack expression, we can't unify. */
23665 if (parm_variadic_p
23666 ? argslen
< len
- parm_variadic_p
23668 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23670 /* Unify all of the parameters that precede the (optional)
23671 pack expression. */
23672 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23674 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23675 TREE_VEC_ELT (parm
, i
),
23676 TREE_VEC_ELT (arg
, i
),
23677 UNIFY_ALLOW_NONE
, explain_p
);
23679 if (parm_variadic_p
)
23680 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23682 /*subr=*/true, explain_p
);
23683 return unify_success (explain_p
);
23688 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23689 return unify_type_mismatch (explain_p
, parm
, arg
);
23691 if (TYPE_PTRMEMFUNC_P (parm
))
23693 if (!TYPE_PTRMEMFUNC_P (arg
))
23694 return unify_type_mismatch (explain_p
, parm
, arg
);
23696 return unify (tparms
, targs
,
23697 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23698 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23699 strict
, explain_p
);
23701 else if (TYPE_PTRMEMFUNC_P (arg
))
23702 return unify_type_mismatch (explain_p
, parm
, arg
);
23704 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23706 tree t
= NULL_TREE
;
23708 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23710 /* First, we try to unify the PARM and ARG directly. */
23711 t
= try_class_unification (tparms
, targs
,
23712 parm
, arg
, explain_p
);
23716 /* Fallback to the special case allowed in
23717 [temp.deduct.call]:
23719 If P is a class, and P has the form
23720 template-id, then A can be a derived class of
23721 the deduced A. Likewise, if P is a pointer to
23722 a class of the form template-id, A can be a
23723 pointer to a derived class pointed to by the
23725 enum template_base_result r
;
23726 r
= get_template_base (tparms
, targs
, parm
, arg
,
23731 /* Don't give the derived diagnostic if we're
23732 already dealing with the same template. */
23734 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23735 && (CLASSTYPE_TI_TEMPLATE (parm
)
23736 == CLASSTYPE_TI_TEMPLATE (arg
)));
23737 return unify_no_common_base (explain_p
&& !same_template
,
23742 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23743 && (CLASSTYPE_TI_TEMPLATE (parm
)
23744 == CLASSTYPE_TI_TEMPLATE (arg
)))
23745 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23746 Then, we should unify `int' and `U'. */
23749 /* There's no chance of unification succeeding. */
23750 return unify_type_mismatch (explain_p
, parm
, arg
);
23752 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23753 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23755 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23756 return unify_type_mismatch (explain_p
, parm
, arg
);
23757 return unify_success (explain_p
);
23760 case FUNCTION_TYPE
:
23762 unsigned int nargs
;
23767 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23768 return unify_type_mismatch (explain_p
, parm
, arg
);
23770 /* CV qualifications for methods can never be deduced, they must
23771 match exactly. We need to check them explicitly here,
23772 because type_unification_real treats them as any other
23773 cv-qualified parameter. */
23774 if (TREE_CODE (parm
) == METHOD_TYPE
23775 && (!check_cv_quals_for_unify
23777 class_of_this_parm (arg
),
23778 class_of_this_parm (parm
))))
23779 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23780 if (TREE_CODE (arg
) == FUNCTION_TYPE
23781 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23782 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23783 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23784 return unify_type_mismatch (explain_p
, parm
, arg
);
23786 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23787 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23789 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23790 args
= XALLOCAVEC (tree
, nargs
);
23791 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23792 a
!= NULL_TREE
&& a
!= void_list_node
;
23793 a
= TREE_CHAIN (a
), ++i
)
23794 args
[i
] = TREE_VALUE (a
);
23797 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23798 args
, nargs
, 1, DEDUCE_EXACT
,
23802 if (flag_noexcept_type
)
23804 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23805 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23806 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23807 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23808 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23809 && uses_template_parms (TREE_PURPOSE (pspec
)))
23810 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23811 TREE_PURPOSE (aspec
),
23812 UNIFY_ALLOW_NONE
, explain_p
);
23813 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23814 return unify_type_mismatch (explain_p
, parm
, arg
);
23821 /* Unify a pointer to member with a pointer to member function, which
23822 deduces the type of the member as a function type. */
23823 if (TYPE_PTRMEMFUNC_P (arg
))
23825 /* Check top-level cv qualifiers */
23826 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23827 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23829 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23830 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23831 UNIFY_ALLOW_NONE
, explain_p
);
23833 /* Determine the type of the function we are unifying against. */
23834 tree fntype
= static_fn_type (arg
);
23836 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23839 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23840 return unify_type_mismatch (explain_p
, parm
, arg
);
23841 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23842 TYPE_OFFSET_BASETYPE (arg
),
23843 UNIFY_ALLOW_NONE
, explain_p
);
23844 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23845 strict
, explain_p
);
23848 if (DECL_TEMPLATE_PARM_P (parm
))
23849 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23850 if (arg
!= scalar_constant_value (parm
))
23851 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23852 return unify_success (explain_p
);
23855 case TEMPLATE_DECL
:
23856 /* Matched cases are handled by the ARG == PARM test above. */
23857 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23860 /* We might get a variable as a non-type template argument in parm if the
23861 corresponding parameter is type-dependent. Make any necessary
23862 adjustments based on whether arg is a reference. */
23863 if (CONSTANT_CLASS_P (arg
))
23864 parm
= fold_non_dependent_expr (parm
, complain
);
23865 else if (REFERENCE_REF_P (arg
))
23867 tree sub
= TREE_OPERAND (arg
, 0);
23869 if (TREE_CODE (sub
) == ADDR_EXPR
)
23870 arg
= TREE_OPERAND (sub
, 0);
23872 /* Now use the normal expression code to check whether they match. */
23875 case TYPE_ARGUMENT_PACK
:
23876 case NONTYPE_ARGUMENT_PACK
:
23877 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23878 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23881 case DECLTYPE_TYPE
:
23882 case UNDERLYING_TYPE
:
23883 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23884 or UNDERLYING_TYPE nodes. */
23885 return unify_success (explain_p
);
23888 /* Unification fails if we hit an error node. */
23889 return unify_invalid (explain_p
);
23892 if (REFERENCE_REF_P (parm
))
23894 bool pexp
= PACK_EXPANSION_P (arg
);
23896 arg
= PACK_EXPANSION_PATTERN (arg
);
23897 if (REFERENCE_REF_P (arg
))
23898 arg
= TREE_OPERAND (arg
, 0);
23900 arg
= make_pack_expansion (arg
, complain
);
23901 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23902 strict
, explain_p
);
23907 /* An unresolved overload is a nondeduced context. */
23908 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23909 return unify_success (explain_p
);
23910 gcc_assert (EXPR_P (parm
)
23911 || COMPOUND_LITERAL_P (parm
)
23912 || TREE_CODE (parm
) == TRAIT_EXPR
);
23914 /* We must be looking at an expression. This can happen with
23918 void foo(S<I>, S<I + 2>);
23922 template<typename T>
23923 void foo(A<T, T{}>);
23925 This is a "non-deduced context":
23929 The non-deduced contexts are:
23931 --A non-type template argument or an array bound in which
23932 a subexpression references a template parameter.
23934 In these cases, we assume deduction succeeded, but don't
23935 actually infer any unifications. */
23937 if (!uses_template_parms (parm
)
23938 && !template_args_equal (parm
, arg
))
23939 return unify_expression_unequal (explain_p
, parm
, arg
);
23941 return unify_success (explain_p
);
23944 #undef RECUR_AND_CHECK_FAILURE
23946 /* Note that DECL can be defined in this translation unit, if
23950 mark_definable (tree decl
)
23953 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23954 FOR_EACH_CLONE (clone
, decl
)
23955 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23958 /* Called if RESULT is explicitly instantiated, or is a member of an
23959 explicitly instantiated class. */
23962 mark_decl_instantiated (tree result
, int extern_p
)
23964 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23966 /* If this entity has already been written out, it's too late to
23967 make any modifications. */
23968 if (TREE_ASM_WRITTEN (result
))
23971 /* For anonymous namespace we don't need to do anything. */
23972 if (decl_anon_ns_mem_p (result
))
23974 gcc_assert (!TREE_PUBLIC (result
));
23978 if (TREE_CODE (result
) != FUNCTION_DECL
)
23979 /* The TREE_PUBLIC flag for function declarations will have been
23980 set correctly by tsubst. */
23981 TREE_PUBLIC (result
) = 1;
23983 /* This might have been set by an earlier implicit instantiation. */
23984 DECL_COMDAT (result
) = 0;
23987 DECL_NOT_REALLY_EXTERN (result
) = 0;
23990 mark_definable (result
);
23991 mark_needed (result
);
23992 /* Always make artificials weak. */
23993 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23994 comdat_linkage (result
);
23995 /* For WIN32 we also want to put explicit instantiations in
23996 linkonce sections. */
23997 else if (TREE_PUBLIC (result
))
23998 maybe_make_one_only (result
);
23999 if (TREE_CODE (result
) == FUNCTION_DECL
24000 && DECL_TEMPLATE_INSTANTIATED (result
))
24001 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24002 since start_preparsed_function wouldn't have if we had an earlier
24003 extern explicit instantiation. */
24004 DECL_EXTERNAL (result
) = 0;
24007 /* If EXTERN_P, then this function will not be emitted -- unless
24008 followed by an explicit instantiation, at which point its linkage
24009 will be adjusted. If !EXTERN_P, then this function will be
24010 emitted here. In neither circumstance do we want
24011 import_export_decl to adjust the linkage. */
24012 DECL_INTERFACE_KNOWN (result
) = 1;
24015 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24016 important template arguments. If any are missing, we check whether
24017 they're important by using error_mark_node for substituting into any
24018 args that were used for partial ordering (the ones between ARGS and END)
24019 and seeing if it bubbles up. */
24022 check_undeduced_parms (tree targs
, tree args
, tree end
)
24024 bool found
= false;
24026 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
24027 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
24030 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
24034 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
24035 if (substed
== error_mark_node
)
24041 /* Given two function templates PAT1 and PAT2, return:
24043 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24044 -1 if PAT2 is more specialized than PAT1.
24045 0 if neither is more specialized.
24047 LEN indicates the number of parameters we should consider
24048 (defaulted parameters should not be considered).
24050 The 1998 std underspecified function template partial ordering, and
24051 DR214 addresses the issue. We take pairs of arguments, one from
24052 each of the templates, and deduce them against each other. One of
24053 the templates will be more specialized if all the *other*
24054 template's arguments deduce against its arguments and at least one
24055 of its arguments *does* *not* deduce against the other template's
24056 corresponding argument. Deduction is done as for class templates.
24057 The arguments used in deduction have reference and top level cv
24058 qualifiers removed. Iff both arguments were originally reference
24059 types *and* deduction succeeds in both directions, an lvalue reference
24060 wins against an rvalue reference and otherwise the template
24061 with the more cv-qualified argument wins for that pairing (if
24062 neither is more cv-qualified, they both are equal). Unlike regular
24063 deduction, after all the arguments have been deduced in this way,
24064 we do *not* verify the deduced template argument values can be
24065 substituted into non-deduced contexts.
24067 The logic can be a bit confusing here, because we look at deduce1 and
24068 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24069 can find template arguments for pat1 to make arg1 look like arg2, that
24070 means that arg2 is at least as specialized as arg1. */
24073 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24075 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24076 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24077 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24078 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24079 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24080 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24081 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24082 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24083 tree origs1
, origs2
;
24084 bool lose1
= false;
24085 bool lose2
= false;
24087 /* Remove the this parameter from non-static member functions. If
24088 one is a non-static member function and the other is not a static
24089 member function, remove the first parameter from that function
24090 also. This situation occurs for operator functions where we
24091 locate both a member function (with this pointer) and non-member
24092 operator (with explicit first operand). */
24093 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24095 len
--; /* LEN is the number of significant arguments for DECL1 */
24096 args1
= TREE_CHAIN (args1
);
24097 if (!DECL_STATIC_FUNCTION_P (decl2
))
24098 args2
= TREE_CHAIN (args2
);
24100 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24102 args2
= TREE_CHAIN (args2
);
24103 if (!DECL_STATIC_FUNCTION_P (decl1
))
24106 args1
= TREE_CHAIN (args1
);
24110 /* If only one is a conversion operator, they are unordered. */
24111 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24114 /* Consider the return type for a conversion function */
24115 if (DECL_CONV_FN_P (decl1
))
24117 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24118 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24122 processing_template_decl
++;
24128 /* Stop when an ellipsis is seen. */
24129 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24131 tree arg1
= TREE_VALUE (args1
);
24132 tree arg2
= TREE_VALUE (args2
);
24133 int deduce1
, deduce2
;
24139 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24140 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24142 /* When both arguments are pack expansions, we need only
24143 unify the patterns themselves. */
24144 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24145 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24147 /* This is the last comparison we need to do. */
24151 if (TYPE_REF_P (arg1
))
24153 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24154 arg1
= TREE_TYPE (arg1
);
24155 quals1
= cp_type_quals (arg1
);
24158 if (TYPE_REF_P (arg2
))
24160 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24161 arg2
= TREE_TYPE (arg2
);
24162 quals2
= cp_type_quals (arg2
);
24165 arg1
= TYPE_MAIN_VARIANT (arg1
);
24166 arg2
= TYPE_MAIN_VARIANT (arg2
);
24168 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24170 int i
, len2
= remaining_arguments (args2
);
24171 tree parmvec
= make_tree_vec (1);
24172 tree argvec
= make_tree_vec (len2
);
24175 /* Setup the parameter vector, which contains only ARG1. */
24176 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24178 /* Setup the argument vector, which contains the remaining
24180 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24181 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24183 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24184 argvec
, DEDUCE_EXACT
,
24185 /*subr=*/true, /*explain_p=*/false)
24188 /* We cannot deduce in the other direction, because ARG1 is
24189 a pack expansion but ARG2 is not. */
24192 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24194 int i
, len1
= remaining_arguments (args1
);
24195 tree parmvec
= make_tree_vec (1);
24196 tree argvec
= make_tree_vec (len1
);
24199 /* Setup the parameter vector, which contains only ARG1. */
24200 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24202 /* Setup the argument vector, which contains the remaining
24204 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24205 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24207 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24208 argvec
, DEDUCE_EXACT
,
24209 /*subr=*/true, /*explain_p=*/false)
24212 /* We cannot deduce in the other direction, because ARG2 is
24213 a pack expansion but ARG1 is not.*/
24219 /* The normal case, where neither argument is a pack
24221 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24222 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24224 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24225 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24229 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24230 arg2, then arg2 is not as specialized as arg1. */
24236 /* "If, for a given type, deduction succeeds in both directions
24237 (i.e., the types are identical after the transformations above)
24238 and both P and A were reference types (before being replaced with
24239 the type referred to above):
24240 - if the type from the argument template was an lvalue reference and
24241 the type from the parameter template was not, the argument type is
24242 considered to be more specialized than the other; otherwise,
24243 - if the type from the argument template is more cv-qualified
24244 than the type from the parameter template (as described above),
24245 the argument type is considered to be more specialized than the other;
24247 - neither type is more specialized than the other." */
24249 if (deduce1
&& deduce2
)
24251 if (ref1
&& ref2
&& ref1
!= ref2
)
24258 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24260 if ((quals1
& quals2
) == quals2
)
24262 if ((quals1
& quals2
) == quals1
)
24267 if (lose1
&& lose2
)
24268 /* We've failed to deduce something in either direction.
24269 These must be unordered. */
24272 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24273 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24274 /* We have already processed all of the arguments in our
24275 handing of the pack expansion type. */
24278 args1
= TREE_CHAIN (args1
);
24279 args2
= TREE_CHAIN (args2
);
24282 /* "In most cases, all template parameters must have values in order for
24283 deduction to succeed, but for partial ordering purposes a template
24284 parameter may remain without a value provided it is not used in the
24285 types being used for partial ordering."
24287 Thus, if we are missing any of the targs1 we need to substitute into
24288 origs1, then pat2 is not as specialized as pat1. This can happen when
24289 there is a nondeduced context. */
24290 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24292 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24295 processing_template_decl
--;
24297 /* If both deductions succeed, the partial ordering selects the more
24298 constrained template. */
24299 /* P2113: If the corresponding template-parameters of the
24300 template-parameter-lists are not equivalent ([temp.over.link]) or if
24301 the function parameters that positionally correspond between the two
24302 templates are not of the same type, neither template is more
24303 specialized than the other. */
24304 if (!lose1
&& !lose2
24305 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24306 DECL_TEMPLATE_PARMS (pat2
))
24307 && compparms (origs1
, origs2
))
24309 int winner
= more_constrained (decl1
, decl2
);
24312 else if (winner
< 0)
24316 /* All things being equal, if the next argument is a pack expansion
24317 for one function but not for the other, prefer the
24318 non-variadic function. FIXME this is bogus; see c++/41958. */
24320 && args1
&& TREE_VALUE (args1
)
24321 && args2
&& TREE_VALUE (args2
))
24323 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24324 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24327 if (lose1
== lose2
)
24335 /* Determine which of two partial specializations of TMPL is more
24338 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24339 to the first partial specialization. The TREE_PURPOSE is the
24340 innermost set of template parameters for the partial
24341 specialization. PAT2 is similar, but for the second template.
24343 Return 1 if the first partial specialization is more specialized;
24344 -1 if the second is more specialized; 0 if neither is more
24347 See [temp.class.order] for information about determining which of
24348 two templates is more specialized. */
24351 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24355 bool any_deductions
= false;
24357 tree tmpl1
= TREE_VALUE (pat1
);
24358 tree tmpl2
= TREE_VALUE (pat2
);
24359 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24360 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24362 /* Just like what happens for functions, if we are ordering between
24363 different template specializations, we may encounter dependent
24364 types in the arguments, and we need our dependency check functions
24365 to behave correctly. */
24366 ++processing_template_decl
;
24367 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24371 any_deductions
= true;
24374 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24378 any_deductions
= true;
24380 --processing_template_decl
;
24382 /* If both deductions succeed, the partial ordering selects the more
24383 constrained template. */
24384 if (!winner
&& any_deductions
)
24385 winner
= more_constrained (tmpl1
, tmpl2
);
24387 /* In the case of a tie where at least one of the templates
24388 has a parameter pack at the end, the template with the most
24389 non-packed parameters wins. */
24392 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24393 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24395 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24396 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24397 int len1
= TREE_VEC_LENGTH (args1
);
24398 int len2
= TREE_VEC_LENGTH (args2
);
24400 /* We don't count the pack expansion at the end. */
24401 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24403 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24408 else if (len1
< len2
)
24415 /* Return the template arguments that will produce the function signature
24416 DECL from the function template FN, with the explicit template
24417 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24418 also match. Return NULL_TREE if no satisfactory arguments could be
24422 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24424 int ntparms
= DECL_NTPARMS (fn
);
24425 tree targs
= make_tree_vec (ntparms
);
24426 tree decl_type
= TREE_TYPE (decl
);
24427 tree decl_arg_types
;
24429 unsigned int nargs
, ix
;
24432 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24434 /* Never do unification on the 'this' parameter. */
24435 decl_arg_types
= skip_artificial_parms_for (decl
,
24436 TYPE_ARG_TYPES (decl_type
));
24438 nargs
= list_length (decl_arg_types
);
24439 args
= XALLOCAVEC (tree
, nargs
);
24440 for (arg
= decl_arg_types
, ix
= 0;
24441 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24442 arg
= TREE_CHAIN (arg
), ++ix
)
24443 args
[ix
] = TREE_VALUE (arg
);
24445 if (fn_type_unification (fn
, explicit_args
, targs
,
24447 (check_rettype
|| DECL_CONV_FN_P (fn
)
24448 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24449 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24450 /*explain_p=*/false,
24452 == error_mark_node
)
24458 /* Return the innermost template arguments that, when applied to a partial
24459 specialization SPEC_TMPL of TMPL, yield the ARGS.
24461 For example, suppose we have:
24463 template <class T, class U> struct S {};
24464 template <class T> struct S<T*, int> {};
24466 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24467 partial specialization and the ARGS will be {double*, int}. The resulting
24468 vector will be {double}, indicating that `T' is bound to `double'. */
24471 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24473 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24475 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24476 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24478 tree innermost_deduced_args
;
24480 innermost_deduced_args
= make_tree_vec (ntparms
);
24481 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24483 deduced_args
= copy_node (args
);
24484 SET_TMPL_ARGS_LEVEL (deduced_args
,
24485 TMPL_ARGS_DEPTH (deduced_args
),
24486 innermost_deduced_args
);
24489 deduced_args
= innermost_deduced_args
;
24491 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24493 if (unify (tparms
, deduced_args
,
24494 INNERMOST_TEMPLATE_ARGS (spec_args
),
24495 INNERMOST_TEMPLATE_ARGS (args
),
24496 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24499 for (i
= 0; i
< ntparms
; ++i
)
24500 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24502 if (!tried_array_deduction
)
24504 try_array_deduction (tparms
, innermost_deduced_args
,
24505 INNERMOST_TEMPLATE_ARGS (spec_args
));
24506 tried_array_deduction
= true;
24507 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24513 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24515 excessive_deduction_depth
= true;
24519 /* Verify that nondeduced template arguments agree with the type
24520 obtained from argument deduction.
24524 struct A { typedef int X; };
24525 template <class T, class U> struct C {};
24526 template <class T> struct C<T, typename T::X> {};
24528 Then with the instantiation `C<A, int>', we can deduce that
24529 `T' is `A' but unify () does not check whether `typename T::X'
24531 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24533 if (spec_args
!= error_mark_node
)
24534 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24535 INNERMOST_TEMPLATE_ARGS (spec_args
),
24536 tmpl
, tf_none
, false, false);
24538 pop_tinst_level ();
24540 if (spec_args
== error_mark_node
24541 /* We only need to check the innermost arguments; the other
24542 arguments will always agree. */
24543 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24544 INNERMOST_TEMPLATE_ARGS (args
)))
24547 /* Now that we have bindings for all of the template arguments,
24548 ensure that the arguments deduced for the template template
24549 parameters have compatible template parameter lists. See the use
24550 of template_template_parm_bindings_ok_p in fn_type_unification
24551 for more information. */
24552 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24555 return deduced_args
;
24558 // Compare two function templates T1 and T2 by deducing bindings
24559 // from one against the other. If both deductions succeed, compare
24560 // constraints to see which is more constrained.
24562 more_specialized_inst (tree t1
, tree t2
)
24567 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24573 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24579 // If both deductions succeed, then one may be more constrained.
24580 if (count
== 2 && fate
== 0)
24581 fate
= more_constrained (t1
, t2
);
24586 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24587 Return the TREE_LIST node with the most specialized template, if
24588 any. If there is no most specialized template, the error_mark_node
24591 Note that this function does not look at, or modify, the
24592 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24593 returned is one of the elements of INSTANTIATIONS, callers may
24594 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24595 and retrieve it from the value returned. */
24598 most_specialized_instantiation (tree templates
)
24602 ++processing_template_decl
;
24605 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24607 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24608 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24613 /* Equally specialized, move to next function. If there
24614 is no next function, nothing's most specialized. */
24615 fn
= TREE_CHAIN (fn
);
24623 /* Now verify that champ is better than everything earlier in the
24624 instantiation list. */
24625 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24626 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24633 processing_template_decl
--;
24636 return error_mark_node
;
24641 /* If DECL is a specialization of some template, return the most
24642 general such template. Otherwise, returns NULL_TREE.
24644 For example, given:
24646 template <class T> struct S { template <class U> void f(U); };
24648 if TMPL is `template <class U> void S<int>::f(U)' this will return
24649 the full template. This function will not trace past partial
24650 specializations, however. For example, given in addition:
24652 template <class T> struct S<T*> { template <class U> void f(U); };
24654 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24655 `template <class T> template <class U> S<T*>::f(U)'. */
24658 most_general_template (tree decl
)
24660 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24662 if (tree tinfo
= get_template_info (decl
))
24663 decl
= TI_TEMPLATE (tinfo
);
24664 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24665 template friend, or a FIELD_DECL for a capture pack. */
24666 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24670 /* Look for more and more general templates. */
24671 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24673 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24674 (See cp-tree.h for details.) */
24675 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24678 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24679 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24680 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24683 /* Stop if we run into an explicitly specialized class template. */
24684 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24685 && DECL_CONTEXT (decl
)
24686 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24689 decl
= DECL_TI_TEMPLATE (decl
);
24695 /* Return the most specialized of the template partial specializations
24696 which can produce TARGET, a specialization of some class or variable
24697 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24698 a TEMPLATE_DECL node corresponding to the partial specialization, while
24699 the TREE_PURPOSE is the set of template arguments that must be
24700 substituted into the template pattern in order to generate TARGET.
24702 If the choice of partial specialization is ambiguous, a diagnostic
24703 is issued, and the error_mark_node is returned. If there are no
24704 partial specializations matching TARGET, then NULL_TREE is
24705 returned, indicating that the primary template should be used. */
24708 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24710 tree list
= NULL_TREE
;
24715 tree outer_args
= NULL_TREE
;
24718 if (TYPE_P (target
))
24720 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24721 tmpl
= TI_TEMPLATE (tinfo
);
24722 args
= TI_ARGS (tinfo
);
24724 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24726 tmpl
= TREE_OPERAND (target
, 0);
24727 args
= TREE_OPERAND (target
, 1);
24729 else if (VAR_P (target
))
24731 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24732 tmpl
= TI_TEMPLATE (tinfo
);
24733 args
= TI_ARGS (tinfo
);
24736 gcc_unreachable ();
24738 tree main_tmpl
= most_general_template (tmpl
);
24740 /* For determining which partial specialization to use, only the
24741 innermost args are interesting. */
24742 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24744 outer_args
= strip_innermost_template_args (args
, 1);
24745 args
= INNERMOST_TEMPLATE_ARGS (args
);
24748 /* The caller hasn't called push_to_top_level yet, but we need
24749 get_partial_spec_bindings to be done in non-template context so that we'll
24750 fully resolve everything. */
24751 processing_template_decl_sentinel ptds
;
24753 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24755 const tree ospec_tmpl
= TREE_VALUE (t
);
24760 /* Substitute in the template args from the enclosing class. */
24761 ++processing_template_decl
;
24762 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24763 --processing_template_decl
;
24764 if (spec_tmpl
== error_mark_node
)
24765 return error_mark_node
;
24768 spec_tmpl
= ospec_tmpl
;
24770 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24774 spec_args
= add_to_template_args (outer_args
, spec_args
);
24776 /* Keep the candidate only if the constraints are satisfied,
24777 or if we're not compiling with concepts. */
24779 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24781 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24782 TREE_TYPE (list
) = TREE_TYPE (t
);
24790 ambiguous_p
= false;
24793 t
= TREE_CHAIN (t
);
24794 for (; t
; t
= TREE_CHAIN (t
))
24796 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24803 t
= TREE_CHAIN (t
);
24806 ambiguous_p
= true;
24815 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24817 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24820 ambiguous_p
= true;
24828 char *spaces
= NULL
;
24829 if (!(complain
& tf_error
))
24830 return error_mark_node
;
24831 if (TYPE_P (target
))
24832 error ("ambiguous template instantiation for %q#T", target
);
24834 error ("ambiguous template instantiation for %q#D", target
);
24835 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24836 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24838 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24839 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24840 "%s %#qS", spaces
? spaces
: str
, subst
);
24841 spaces
= spaces
? spaces
: get_spaces (str
);
24844 return error_mark_node
;
24850 /* Explicitly instantiate DECL. */
24853 do_decl_instantiation (tree decl
, tree storage
)
24855 tree result
= NULL_TREE
;
24858 if (!decl
|| decl
== error_mark_node
)
24859 /* An error occurred, for which grokdeclarator has already issued
24860 an appropriate message. */
24862 else if (! DECL_LANG_SPECIFIC (decl
))
24864 error ("explicit instantiation of non-template %q#D", decl
);
24867 else if (DECL_DECLARED_CONCEPT_P (decl
))
24870 error ("explicit instantiation of variable concept %q#D", decl
);
24872 error ("explicit instantiation of function concept %q#D", decl
);
24876 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24877 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24879 if (VAR_P (decl
) && !var_templ
)
24881 /* There is an asymmetry here in the way VAR_DECLs and
24882 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24883 the latter, the DECL we get back will be marked as a
24884 template instantiation, and the appropriate
24885 DECL_TEMPLATE_INFO will be set up. This does not happen for
24886 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24887 should handle VAR_DECLs as it currently handles
24889 if (!DECL_CLASS_SCOPE_P (decl
))
24891 error ("%qD is not a static data member of a class template", decl
);
24894 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24895 if (!result
|| !VAR_P (result
))
24897 error ("no matching template for %qD found", decl
);
24900 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24902 error ("type %qT for explicit instantiation %qD does not match "
24903 "declared type %qT", TREE_TYPE (result
), decl
,
24908 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24910 error ("explicit instantiation of %q#D", decl
);
24916 /* Check for various error cases. Note that if the explicit
24917 instantiation is valid the RESULT will currently be marked as an
24918 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24919 until we get here. */
24921 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24923 /* DR 259 [temp.spec].
24925 Both an explicit instantiation and a declaration of an explicit
24926 specialization shall not appear in a program unless the explicit
24927 instantiation follows a declaration of the explicit specialization.
24929 For a given set of template parameters, if an explicit
24930 instantiation of a template appears after a declaration of an
24931 explicit specialization for that template, the explicit
24932 instantiation has no effect. */
24935 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24939 No program shall explicitly instantiate any template more
24942 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24943 the first instantiation was `extern' and the second is not,
24944 and EXTERN_P for the opposite case. */
24945 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24946 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24947 /* If an "extern" explicit instantiation follows an ordinary
24948 explicit instantiation, the template is instantiated. */
24952 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24954 error ("no matching template for %qD found", result
);
24957 else if (!DECL_TEMPLATE_INFO (result
))
24959 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24963 if (storage
== NULL_TREE
)
24965 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24967 if (cxx_dialect
== cxx98
)
24968 pedwarn (input_location
, OPT_Wpedantic
,
24969 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24974 error ("storage class %qD applied to template instantiation", storage
);
24976 check_explicit_instantiation_namespace (result
);
24977 mark_decl_instantiated (result
, extern_p
);
24979 instantiate_decl (result
, /*defer_ok=*/true,
24980 /*expl_inst_class_mem_p=*/false);
24984 mark_class_instantiated (tree t
, int extern_p
)
24986 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24987 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24988 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24989 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24992 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24993 rest_of_type_compilation (t
, 1);
24997 /* Perform an explicit instantiation of template class T. STORAGE, if
24998 non-null, is the RID for extern, inline or static. COMPLAIN is
24999 nonzero if this is called from the parser, zero if called recursively,
25000 since the standard is unclear (as detailed below). */
25003 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
25005 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
25007 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
25008 error ("explicit instantiation of non-class template %qD",
25011 error ("explicit instantiation of non-template type %qT", t
);
25017 if (!COMPLETE_TYPE_P (t
))
25019 if (complain
& tf_error
)
25020 error ("explicit instantiation of %q#T before definition of template",
25025 /* At most one of these will be true. */
25026 bool extern_p
= false;
25027 bool nomem_p
= false;
25028 bool static_p
= false;
25030 if (storage
!= NULL_TREE
)
25032 if (storage
== ridpointers
[(int) RID_EXTERN
])
25034 if (cxx_dialect
== cxx98
)
25035 pedwarn (input_location
, OPT_Wpedantic
,
25036 "ISO C++ 1998 forbids the use of %<extern%> on "
25037 "explicit instantiations");
25040 pedwarn (input_location
, OPT_Wpedantic
,
25041 "ISO C++ forbids the use of %qE"
25042 " on explicit instantiations", storage
);
25044 if (storage
== ridpointers
[(int) RID_INLINE
])
25046 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25048 else if (storage
== ridpointers
[(int) RID_STATIC
])
25051 error ("storage class %qD applied to template instantiation",
25055 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25056 /* DR 259 [temp.spec].
25058 Both an explicit instantiation and a declaration of an explicit
25059 specialization shall not appear in a program unless the
25060 explicit instantiation follows a declaration of the explicit
25063 For a given set of template parameters, if an explicit
25064 instantiation of a template appears after a declaration of an
25065 explicit specialization for that template, the explicit
25066 instantiation has no effect. */
25069 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
25071 /* We've already instantiated the template. */
25075 No program shall explicitly instantiate any template more
25078 If EXTERN_P then this is ok. */
25079 if (!extern_p
&& (complain
& tf_error
))
25080 permerror (input_location
,
25081 "duplicate explicit instantiation of %q#T", t
);
25086 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25087 mark_class_instantiated (t
, extern_p
);
25092 /* In contrast to implicit instantiation, where only the
25093 declarations, and not the definitions, of members are
25094 instantiated, we have here:
25098 An explicit instantiation that names a class template
25099 specialization is also an explicit instantiation of the same
25100 kind (declaration or definition) of each of its members (not
25101 including members inherited from base classes and members
25102 that are templates) that has not been previously explicitly
25103 specialized in the translation unit containing the explicit
25104 instantiation, provided that the associated constraints, if
25105 any, of that member are satisfied by the template arguments
25106 of the explicit instantiation. */
25107 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25109 || (TREE_CODE (fld
) == FUNCTION_DECL
25111 && user_provided_p (fld
)))
25112 && DECL_TEMPLATE_INSTANTIATION (fld
)
25113 && constraints_satisfied_p (fld
))
25115 mark_decl_instantiated (fld
, extern_p
);
25117 instantiate_decl (fld
, /*defer_ok=*/true,
25118 /*expl_inst_class_mem_p=*/true);
25120 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
25122 tree type
= TREE_TYPE (fld
);
25124 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25125 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
25126 do_type_instantiation (type
, storage
, 0);
25130 /* Given a function DECL, which is a specialization of TMPL, modify
25131 DECL to be a re-instantiation of TMPL with the same template
25132 arguments. TMPL should be the template into which tsubst'ing
25133 should occur for DECL, not the most general template.
25135 One reason for doing this is a scenario like this:
25138 void f(const T&, int i);
25140 void g() { f(3, 7); }
25143 void f(const T& t, const int i) { }
25145 Note that when the template is first instantiated, with
25146 instantiate_template, the resulting DECL will have no name for the
25147 first parameter, and the wrong type for the second. So, when we go
25148 to instantiate the DECL, we regenerate it. */
25151 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25153 /* The arguments used to instantiate DECL, from the most general
25157 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25159 /* Make sure that we can see identifiers, and compute access
25161 push_access_scope (decl
);
25163 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25171 args_depth
= TMPL_ARGS_DEPTH (args
);
25172 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25173 if (args_depth
> parms_depth
)
25174 args
= get_innermost_template_args (args
, parms_depth
);
25176 /* Instantiate a dynamic exception-specification. noexcept will be
25178 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25179 if (TREE_VALUE (raises
))
25181 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25182 args
, tf_error
, NULL_TREE
,
25183 /*defer_ok*/false);
25184 if (specs
&& specs
!= error_mark_node
)
25185 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25189 /* Merge parameter declarations. */
25190 decl_parm
= skip_artificial_parms_for (decl
,
25191 DECL_ARGUMENTS (decl
));
25193 = skip_artificial_parms_for (code_pattern
,
25194 DECL_ARGUMENTS (code_pattern
));
25195 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25200 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25201 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25202 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25204 parm_type
= type_decays_to (parm_type
);
25205 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25206 TREE_TYPE (decl_parm
) = parm_type
;
25207 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25208 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25210 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25211 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25213 decl_parm
= DECL_CHAIN (decl_parm
);
25214 pattern_parm
= DECL_CHAIN (pattern_parm
);
25216 /* Merge any parameters that match with the function parameter
25218 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25221 tree expanded_types
;
25222 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25223 the parameters in this function parameter pack. */
25224 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25225 args
, tf_error
, NULL_TREE
);
25226 len
= TREE_VEC_LENGTH (expanded_types
);
25227 for (i
= 0; i
< len
; i
++)
25232 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25233 /* Rename the parameter to include the index. */
25234 DECL_NAME (decl_parm
) =
25235 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25236 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25237 parm_type
= type_decays_to (parm_type
);
25238 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25239 TREE_TYPE (decl_parm
) = parm_type
;
25240 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25241 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25243 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25244 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25246 decl_parm
= DECL_CHAIN (decl_parm
);
25249 /* Merge additional specifiers from the CODE_PATTERN. */
25250 if (DECL_DECLARED_INLINE_P (code_pattern
)
25251 && !DECL_DECLARED_INLINE_P (decl
))
25252 DECL_DECLARED_INLINE_P (decl
) = 1;
25254 maybe_instantiate_noexcept (decl
, tf_error
);
25256 else if (VAR_P (decl
))
25258 start_lambda_scope (decl
);
25259 DECL_INITIAL (decl
) =
25260 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25261 tf_error
, DECL_TI_TEMPLATE (decl
));
25262 finish_lambda_scope ();
25263 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25264 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25265 tf_error
, DECL_TI_TEMPLATE (decl
));
25268 gcc_unreachable ();
25270 pop_access_scope (decl
);
25273 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25274 substituted to get DECL. */
25277 template_for_substitution (tree decl
)
25279 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25281 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25282 for the instantiation. This is not always the most general
25283 template. Consider, for example:
25286 struct S { template <class U> void f();
25287 template <> void f<int>(); };
25289 and an instantiation of S<double>::f<int>. We want TD to be the
25290 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25291 while (/* An instantiation cannot have a definition, so we need a
25292 more general template. */
25293 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25294 /* We must also deal with friend templates. Given:
25296 template <class T> struct S {
25297 template <class U> friend void f() {};
25300 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25301 so far as the language is concerned, but that's still
25302 where we get the pattern for the instantiation from. On
25303 other hand, if the definition comes outside the class, say:
25305 template <class T> struct S {
25306 template <class U> friend void f();
25308 template <class U> friend void f() {}
25310 we don't need to look any further. That's what the check for
25311 DECL_INITIAL is for. */
25312 || (TREE_CODE (decl
) == FUNCTION_DECL
25313 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25314 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25316 /* The present template, TD, should not be a definition. If it
25317 were a definition, we should be using it! Note that we
25318 cannot restructure the loop to just keep going until we find
25319 a template with a definition, since that might go too far if
25320 a specialization was declared, but not defined. */
25322 /* Fetch the more general template. */
25323 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25329 /* Returns true if we need to instantiate this template instance even if we
25330 know we aren't going to emit it. */
25333 always_instantiate_p (tree decl
)
25335 /* We always instantiate inline functions so that we can inline them. An
25336 explicit instantiation declaration prohibits implicit instantiation of
25337 non-inline functions. With high levels of optimization, we would
25338 normally inline non-inline functions -- but we're not allowed to do
25339 that for "extern template" functions. Therefore, we check
25340 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25341 return ((TREE_CODE (decl
) == FUNCTION_DECL
25342 && (DECL_DECLARED_INLINE_P (decl
)
25343 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25344 /* And we need to instantiate static data members so that
25345 their initializers are available in integral constant
25348 && decl_maybe_constant_var_p (decl
)));
25351 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25352 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25353 error, true otherwise. */
25356 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25358 tree fntype
, spec
, noex
;
25360 /* Don't instantiate a noexcept-specification from template context. */
25361 if (processing_template_decl
25362 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25365 if (DECL_MAYBE_DELETED (fn
))
25367 if (fn
== current_function_decl
)
25368 /* We're in start_preparsed_function, keep going. */
25372 synthesize_method (fn
);
25374 return !DECL_MAYBE_DELETED (fn
);
25377 fntype
= TREE_TYPE (fn
);
25378 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25380 if (!spec
|| !TREE_PURPOSE (spec
))
25383 noex
= TREE_PURPOSE (spec
);
25384 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25385 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25388 tree orig_fn
= NULL_TREE
;
25389 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25390 its FUNCTION_DECL for the rest of this function -- push_access_scope
25391 doesn't accept TEMPLATE_DECLs. */
25392 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25395 fn
= DECL_TEMPLATE_RESULT (fn
);
25398 if (DECL_CLONED_FUNCTION_P (fn
))
25400 tree prime
= DECL_CLONED_FUNCTION (fn
);
25401 if (!maybe_instantiate_noexcept (prime
, complain
))
25403 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25405 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25407 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25408 bool added
= false;
25409 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25411 spec
= get_defaulted_eh_spec (fn
, complain
);
25412 if (spec
== error_mark_node
)
25413 /* This might have failed because of an unparsed DMI, so
25414 let's try again later. */
25417 else if (!(added
= !fns
->add (fn
)))
25419 /* If hash_set::add returns true, the element was already there. */
25420 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25421 DECL_SOURCE_LOCATION (fn
));
25423 "exception specification of %qD depends on itself",
25425 spec
= noexcept_false_spec
;
25427 else if (push_tinst_level (fn
))
25429 push_to_top_level ();
25430 push_access_scope (fn
);
25431 push_deferring_access_checks (dk_no_deferred
);
25432 input_location
= DECL_SOURCE_LOCATION (fn
);
25434 if (!DECL_LOCAL_DECL_P (fn
))
25436 /* If needed, set current_class_ptr for the benefit of
25437 tsubst_copy/PARM_DECL. The exception pattern will
25438 refer to the parm of the template, not the
25440 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25441 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25443 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25444 current_class_ptr
= NULL_TREE
;
25445 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25446 current_class_ptr
= this_parm
;
25450 /* If this function is represented by a TEMPLATE_DECL, then
25451 the deferred noexcept-specification might still contain
25452 dependent types, even after substitution. And we need the
25453 dependency check functions to work in build_noexcept_spec. */
25455 ++processing_template_decl
;
25457 /* Do deferred instantiation of the noexcept-specifier. */
25458 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25459 DEFERRED_NOEXCEPT_ARGS (noex
),
25460 tf_warning_or_error
, fn
,
25461 /*function_p=*/false,
25464 /* Build up the noexcept-specification. */
25465 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25468 --processing_template_decl
;
25470 pop_deferring_access_checks ();
25471 pop_access_scope (fn
);
25472 pop_tinst_level ();
25473 pop_from_top_level ();
25476 spec
= noexcept_false_spec
;
25482 if (spec
== error_mark_node
)
25484 /* This failed with a hard error, so let's go with false. */
25485 gcc_assert (seen_error ());
25486 spec
= noexcept_false_spec
;
25489 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25491 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25496 /* We're starting to process the function INST, an instantiation of PATTERN;
25497 add their parameters to local_specializations. */
25500 register_parameter_specializations (tree pattern
, tree inst
)
25502 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25503 tree spec_parm
= DECL_ARGUMENTS (inst
);
25504 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25506 register_local_specialization (spec_parm
, tmpl_parm
);
25507 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25508 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25510 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25512 if (!DECL_PACK_P (tmpl_parm
)
25513 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25515 register_local_specialization (spec_parm
, tmpl_parm
);
25516 spec_parm
= DECL_CHAIN (spec_parm
);
25520 /* Register the (value) argument pack as a specialization of
25521 TMPL_PARM, then move on. */
25522 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25523 register_local_specialization (argpack
, tmpl_parm
);
25526 gcc_assert (!spec_parm
);
25529 /* Instantiate the body of D using PATTERN with ARGS. We have
25530 already determined PATTERN is the correct template to use.
25531 NESTED_P is true if this is a nested function, in which case
25532 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25535 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25537 tree td
= NULL_TREE
;
25538 tree code_pattern
= pattern
;
25543 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25546 /* Only OMP reductions are nested. */
25547 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25549 vec
<tree
> omp_privatization_save
;
25550 if (current_function_decl
)
25551 save_omp_privatization_clauses (omp_privatization_save
);
25554 = !(current_function_decl
25555 && !LAMBDA_FUNCTION_P (d
)
25556 && decl_function_context (d
) == current_function_decl
);
25559 push_to_top_level ();
25562 gcc_assert (!processing_template_decl
);
25563 push_function_context ();
25564 cp_unevaluated_operand
= 0;
25565 c_inhibit_evaluation_warnings
= 0;
25570 /* The variable might be a lambda's extra scope, and that
25571 lambda's visibility depends on D's. */
25572 maybe_commonize_var (d
);
25573 determine_visibility (d
);
25576 /* Mark D as instantiated so that recursive calls to
25577 instantiate_decl do not try to instantiate it again. */
25578 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25581 /* Regenerate the declaration in case the template has been modified
25582 by a subsequent redeclaration. */
25583 regenerate_decl_from_template (d
, td
, args
);
25585 /* We already set the file and line above. Reset them now in case
25586 they changed as a result of calling regenerate_decl_from_template. */
25587 input_location
= DECL_SOURCE_LOCATION (d
);
25591 /* Clear out DECL_RTL; whatever was there before may not be right
25592 since we've reset the type of the declaration. */
25593 SET_DECL_RTL (d
, NULL
);
25594 DECL_IN_AGGR_P (d
) = 0;
25596 /* The initializer is placed in DECL_INITIAL by
25597 regenerate_decl_from_template so we don't need to
25598 push/pop_access_scope again here. Pull it out so that
25599 cp_finish_decl can process it. */
25600 bool const_init
= false;
25601 tree init
= DECL_INITIAL (d
);
25602 DECL_INITIAL (d
) = NULL_TREE
;
25603 DECL_INITIALIZED_P (d
) = 0;
25605 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25606 initializer. That function will defer actual emission until
25607 we have a chance to determine linkage. */
25608 DECL_EXTERNAL (d
) = 0;
25610 /* Enter the scope of D so that access-checking works correctly. */
25611 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25613 push_nested_class (DECL_CONTEXT (d
));
25615 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25616 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25619 pop_nested_class ();
25621 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25622 synthesize_method (d
);
25623 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25625 /* Set up the list of local specializations. */
25626 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25627 tree block
= NULL_TREE
;
25629 /* Set up context. */
25631 block
= push_stmt_list ();
25634 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25636 perform_instantiation_time_access_checks (code_pattern
, args
);
25639 /* Create substitution entries for the parameters. */
25640 register_parameter_specializations (code_pattern
, d
);
25642 /* Substitute into the body of the function. */
25643 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25644 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25645 tf_warning_or_error
, d
);
25648 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25649 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25650 /*integral_constant_expression_p=*/false);
25652 /* Set the current input_location to the end of the function
25653 so that finish_function knows where we are. */
25655 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25657 /* Remember if we saw an infinite loop in the template. */
25658 current_function_infinite_loop
25659 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25662 /* Finish the function. */
25664 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25667 d
= finish_function (/*inline_p=*/false);
25668 expand_or_defer_fn (d
);
25671 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25672 cp_check_omp_declare_reduction (d
);
25675 /* We're not deferring instantiation any more. */
25677 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25680 pop_from_top_level ();
25682 pop_function_context ();
25684 if (current_function_decl
)
25685 restore_omp_privatization_clauses (omp_privatization_save
);
25688 /* Produce the definition of D, a _DECL generated from a template. If
25689 DEFER_OK is true, then we don't have to actually do the
25690 instantiation now; we just have to do it sometime. Normally it is
25691 an error if this is an explicit instantiation but D is undefined.
25692 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25693 instantiated class template. */
25696 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25698 tree tmpl
= DECL_TI_TEMPLATE (d
);
25705 bool pattern_defined
;
25706 location_t saved_loc
= input_location
;
25707 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25708 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25712 /* This function should only be used to instantiate templates for
25713 functions and static member variables. */
25714 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25716 /* A concept is never instantiated. */
25717 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25719 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
25721 /* Variables are never deferred; if instantiation is required, they
25722 are instantiated right away. That allows for better code in the
25723 case that an expression refers to the value of the variable --
25724 if the variable has a constant value the referring expression can
25725 take advantage of that fact. */
25729 /* Don't instantiate cloned functions. Instead, instantiate the
25730 functions they cloned. */
25731 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25732 d
= DECL_CLONED_FUNCTION (d
);
25734 if (DECL_TEMPLATE_INSTANTIATED (d
)
25735 || TREE_TYPE (d
) == error_mark_node
25736 || (TREE_CODE (d
) == FUNCTION_DECL
25737 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25738 || DECL_TEMPLATE_SPECIALIZATION (d
))
25739 /* D has already been instantiated or explicitly specialized, so
25740 there's nothing for us to do here.
25742 It might seem reasonable to check whether or not D is an explicit
25743 instantiation, and, if so, stop here. But when an explicit
25744 instantiation is deferred until the end of the compilation,
25745 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25746 the instantiation. */
25749 /* Check to see whether we know that this template will be
25750 instantiated in some other file, as with "extern template"
25752 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25754 /* In general, we do not instantiate such templates. */
25755 if (external_p
&& !always_instantiate_p (d
))
25758 gen_tmpl
= most_general_template (tmpl
);
25759 gen_args
= DECL_TI_ARGS (d
);
25761 /* We should already have the extra args. */
25762 gcc_checking_assert (tmpl
== gen_tmpl
25763 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25764 == TMPL_ARGS_DEPTH (gen_args
)));
25765 /* And what's in the hash table should match D. */
25766 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25768 || spec
== NULL_TREE
);
25770 /* This needs to happen before any tsubsting. */
25771 if (! push_tinst_level (d
))
25774 timevar_push (TV_TEMPLATE_INST
);
25776 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25777 for the instantiation. */
25778 td
= template_for_substitution (d
);
25783 /* Look up an explicit specialization, if any. */
25784 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25785 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25786 if (elt
&& elt
!= error_mark_node
)
25788 td
= TREE_VALUE (elt
);
25789 args
= TREE_PURPOSE (elt
);
25793 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25795 /* We should never be trying to instantiate a member of a class
25796 template or partial specialization. */
25797 gcc_assert (d
!= code_pattern
);
25799 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25800 || DECL_TEMPLATE_SPECIALIZATION (td
))
25801 /* In the case of a friend template whose definition is provided
25802 outside the class, we may have too many arguments. Drop the
25803 ones we don't need. The same is true for specializations. */
25804 args
= get_innermost_template_args
25805 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25807 if (TREE_CODE (d
) == FUNCTION_DECL
)
25809 deleted_p
= DECL_DELETED_FN (code_pattern
);
25810 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25811 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25812 || DECL_DEFAULTED_FN (code_pattern
)
25818 if (DECL_CLASS_SCOPE_P (code_pattern
))
25819 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25821 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25824 /* We may be in the middle of deferred access check. Disable it now. */
25825 push_deferring_access_checks (dk_no_deferred
);
25827 /* Unless an explicit instantiation directive has already determined
25828 the linkage of D, remember that a definition is available for
25830 if (pattern_defined
25831 && !DECL_INTERFACE_KNOWN (d
)
25832 && !DECL_NOT_REALLY_EXTERN (d
))
25833 mark_definable (d
);
25835 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25836 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25837 input_location
= DECL_SOURCE_LOCATION (d
);
25839 /* If D is a member of an explicitly instantiated class template,
25840 and no definition is available, treat it like an implicit
25842 if (!pattern_defined
&& expl_inst_class_mem_p
25843 && DECL_EXPLICIT_INSTANTIATION (d
))
25845 /* Leave linkage flags alone on instantiations with anonymous
25847 if (TREE_PUBLIC (d
))
25849 DECL_NOT_REALLY_EXTERN (d
) = 0;
25850 DECL_INTERFACE_KNOWN (d
) = 0;
25852 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25855 /* Defer all other templates, unless we have been explicitly
25856 forbidden from doing so. */
25857 if (/* If there is no definition, we cannot instantiate the
25860 /* If it's OK to postpone instantiation, do so. */
25862 /* If this is a static data member that will be defined
25863 elsewhere, we don't want to instantiate the entire data
25864 member, but we do want to instantiate the initializer so that
25865 we can substitute that elsewhere. */
25866 || (external_p
&& VAR_P (d
))
25867 /* Handle here a deleted function too, avoid generating
25868 its body (c++/61080). */
25871 /* The definition of the static data member is now required so
25872 we must substitute the initializer. */
25874 && !DECL_INITIAL (d
)
25875 && DECL_INITIAL (code_pattern
))
25879 bool const_init
= false;
25880 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25882 ns
= decl_namespace_context (d
);
25883 push_nested_namespace (ns
);
25885 push_nested_class (DECL_CONTEXT (d
));
25886 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25888 tf_warning_or_error
, NULL_TREE
,
25889 /*integral_constant_expression_p=*/false);
25890 /* If instantiating the initializer involved instantiating this
25891 again, don't call cp_finish_decl twice. */
25892 if (!DECL_INITIAL (d
))
25894 /* Make sure the initializer is still constant, in case of
25895 circular dependency (template/instantiate6.C). */
25897 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25898 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25899 /*asmspec_tree=*/NULL_TREE
,
25900 LOOKUP_ONLYCONVERTING
);
25903 pop_nested_class ();
25904 pop_nested_namespace (ns
);
25907 /* We restore the source position here because it's used by
25908 add_pending_template. */
25909 input_location
= saved_loc
;
25911 if (at_eof
&& !pattern_defined
25912 && DECL_EXPLICIT_INSTANTIATION (d
)
25913 && DECL_NOT_REALLY_EXTERN (d
))
25916 The definition of a non-exported function template, a
25917 non-exported member function template, or a non-exported
25918 member function or static data member of a class template
25919 shall be present in every translation unit in which it is
25920 explicitly instantiated. */
25921 permerror (input_location
, "explicit instantiation of %qD "
25922 "but no definition available", d
);
25924 /* If we're in unevaluated context, we just wanted to get the
25925 constant value; this isn't an odr use, so don't queue
25926 a full instantiation. */
25927 if (!cp_unevaluated_operand
25928 /* ??? Historically, we have instantiated inline functions, even
25929 when marked as "extern template". */
25930 && !(external_p
&& VAR_P (d
)))
25931 add_pending_template (d
);
25935 if (variable_template_p (gen_tmpl
))
25936 note_variable_template_instantiation (d
);
25937 instantiate_body (td
, args
, d
, false);
25940 pop_deferring_access_checks ();
25941 timevar_pop (TV_TEMPLATE_INST
);
25942 pop_tinst_level ();
25943 input_location
= saved_loc
;
25944 cp_unevaluated_operand
= saved_unevaluated_operand
;
25945 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25950 /* Run through the list of templates that we wish we could
25951 instantiate, and instantiate any we can. RETRIES is the
25952 number of times we retry pending template instantiation. */
25955 instantiate_pending_templates (int retries
)
25958 location_t saved_loc
= input_location
;
25960 /* Instantiating templates may trigger vtable generation. This in turn
25961 may require further template instantiations. We place a limit here
25962 to avoid infinite loop. */
25963 if (pending_templates
&& retries
>= max_tinst_depth
)
25965 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25967 fatal_error (input_location
,
25968 "template instantiation depth exceeds maximum of %d"
25969 " instantiating %q+D, possibly from virtual table generation"
25970 " (use %<-ftemplate-depth=%> to increase the maximum)",
25971 max_tinst_depth
, decl
);
25972 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25973 /* Pretend that we defined it. */
25974 DECL_INITIAL (decl
) = error_mark_node
;
25980 struct pending_template
**t
= &pending_templates
;
25981 struct pending_template
*last
= NULL
;
25985 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25986 bool complete
= false;
25988 if (TYPE_P (instantiation
))
25990 if (!COMPLETE_TYPE_P (instantiation
))
25992 instantiate_class_template (instantiation
);
25993 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25994 for (tree fld
= TYPE_FIELDS (instantiation
);
25995 fld
; fld
= TREE_CHAIN (fld
))
25997 || (TREE_CODE (fld
) == FUNCTION_DECL
25998 && !DECL_ARTIFICIAL (fld
)))
25999 && DECL_TEMPLATE_INSTANTIATION (fld
))
26000 instantiate_decl (fld
,
26001 /*defer_ok=*/false,
26002 /*expl_inst_class_mem_p=*/false);
26004 if (COMPLETE_TYPE_P (instantiation
))
26008 complete
= COMPLETE_TYPE_P (instantiation
);
26012 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26013 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
26016 = instantiate_decl (instantiation
,
26017 /*defer_ok=*/false,
26018 /*expl_inst_class_mem_p=*/false);
26019 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
26023 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26024 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
26029 /* If INSTANTIATION has been instantiated, then we don't
26030 need to consider it again in the future. */
26031 struct pending_template
*drop
= *t
;
26033 set_refcount_ptr (drop
->tinst
);
26034 pending_template_freelist ().free (drop
);
26042 set_refcount_ptr (current_tinst_level
);
26044 last_pending_template
= last
;
26046 while (reconsider
);
26048 input_location
= saved_loc
;
26051 /* Substitute ARGVEC into T, which is a list of initializers for
26052 either base class or a non-static data member. The TREE_PURPOSEs
26053 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26054 instantiate_decl. */
26057 tsubst_initializer_list (tree t
, tree argvec
)
26059 tree inits
= NULL_TREE
;
26060 tree target_ctor
= error_mark_node
;
26062 for (; t
; t
= TREE_CHAIN (t
))
26066 tree expanded_bases
= NULL_TREE
;
26067 tree expanded_arguments
= NULL_TREE
;
26070 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26075 /* Expand the base class expansion type into separate base
26077 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26078 tf_warning_or_error
,
26080 if (expanded_bases
== error_mark_node
)
26083 /* We'll be building separate TREE_LISTs of arguments for
26085 len
= TREE_VEC_LENGTH (expanded_bases
);
26086 expanded_arguments
= make_tree_vec (len
);
26087 for (i
= 0; i
< len
; i
++)
26088 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26090 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26091 expand each argument in the TREE_VALUE of t. */
26092 expr
= make_node (EXPR_PACK_EXPANSION
);
26093 PACK_EXPANSION_LOCAL_P (expr
) = true;
26094 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26095 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26097 if (TREE_VALUE (t
) == void_type_node
)
26098 /* VOID_TYPE_NODE is used to indicate
26099 value-initialization. */
26101 for (i
= 0; i
< len
; i
++)
26102 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26106 /* Substitute parameter packs into each argument in the
26108 in_base_initializer
= 1;
26109 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26111 tree expanded_exprs
;
26113 /* Expand the argument. */
26114 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26116 = tsubst_pack_expansion (expr
, argvec
,
26117 tf_warning_or_error
,
26119 if (expanded_exprs
== error_mark_node
)
26122 /* Prepend each of the expanded expressions to the
26123 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26124 for (i
= 0; i
< len
; i
++)
26126 TREE_VEC_ELT (expanded_arguments
, i
) =
26127 tree_cons (NULL_TREE
,
26128 TREE_VEC_ELT (expanded_exprs
, i
),
26129 TREE_VEC_ELT (expanded_arguments
, i
));
26132 in_base_initializer
= 0;
26134 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26135 since we built them backwards. */
26136 for (i
= 0; i
< len
; i
++)
26138 TREE_VEC_ELT (expanded_arguments
, i
) =
26139 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26144 for (i
= 0; i
< len
; ++i
)
26146 if (expanded_bases
)
26148 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26149 decl
= expand_member_init (decl
);
26150 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26155 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26156 tf_warning_or_error
, NULL_TREE
);
26158 decl
= expand_member_init (decl
);
26159 if (decl
&& !DECL_P (decl
))
26160 in_base_initializer
= 1;
26162 init
= TREE_VALUE (t
);
26164 if (init
!= void_type_node
)
26165 init
= tsubst_expr (init
, argvec
,
26166 tf_warning_or_error
, NULL_TREE
,
26167 /*integral_constant_expression_p=*/false);
26168 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26169 /* If we had an initializer but it instantiated to nothing,
26170 value-initialize the object. This will only occur when
26171 the initializer was a pack expansion where the parameter
26172 packs used in that expansion were of length zero. */
26173 init
= void_type_node
;
26174 in_base_initializer
= 0;
26177 if (target_ctor
!= error_mark_node
26178 && init
!= error_mark_node
)
26180 error ("mem-initializer for %qD follows constructor delegation",
26184 /* Look for a target constructor. */
26185 if (init
!= error_mark_node
26186 && decl
&& CLASS_TYPE_P (decl
)
26187 && same_type_p (decl
, current_class_type
))
26189 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26192 error ("constructor delegation follows mem-initializer for %qD",
26193 TREE_PURPOSE (inits
));
26196 target_ctor
= init
;
26201 init
= build_tree_list (decl
, init
);
26202 /* Carry over the dummy TREE_TYPE node containing the source
26204 TREE_TYPE (init
) = TREE_TYPE (t
);
26205 TREE_CHAIN (init
) = inits
;
26213 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26216 set_current_access_from_decl (tree decl
)
26218 if (TREE_PRIVATE (decl
))
26219 current_access_specifier
= access_private_node
;
26220 else if (TREE_PROTECTED (decl
))
26221 current_access_specifier
= access_protected_node
;
26223 current_access_specifier
= access_public_node
;
26226 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26227 is the instantiation (which should have been created with
26228 start_enum) and ARGS are the template arguments to use. */
26231 tsubst_enum (tree tag
, tree newtag
, tree args
)
26235 if (SCOPED_ENUM_P (newtag
))
26236 begin_scope (sk_scoped_enum
, newtag
);
26238 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26243 decl
= TREE_VALUE (e
);
26244 /* Note that in a template enum, the TREE_VALUE is the
26245 CONST_DECL, not the corresponding INTEGER_CST. */
26246 value
= tsubst_expr (DECL_INITIAL (decl
),
26247 args
, tf_warning_or_error
, NULL_TREE
,
26248 /*integral_constant_expression_p=*/true);
26250 /* Give this enumeration constant the correct access. */
26251 set_current_access_from_decl (decl
);
26253 /* Actually build the enumerator itself. Here we're assuming that
26254 enumerators can't have dependent attributes. */
26255 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26256 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26259 if (SCOPED_ENUM_P (newtag
))
26262 finish_enum_value_list (newtag
);
26263 finish_enum (newtag
);
26265 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26266 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26269 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26270 its type -- but without substituting the innermost set of template
26271 arguments. So, innermost set of template parameters will appear in
26275 get_mostly_instantiated_function_type (tree decl
)
26277 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26278 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26281 /* Return truthvalue if we're processing a template different from
26282 the last one involved in diagnostics. */
26284 problematic_instantiation_changed (void)
26286 return current_tinst_level
!= last_error_tinst_level
;
26289 /* Remember current template involved in diagnostics. */
26291 record_last_problematic_instantiation (void)
26293 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26296 struct tinst_level
*
26297 current_instantiation (void)
26299 return current_tinst_level
;
26302 /* Return TRUE if current_function_decl is being instantiated, false
26306 instantiating_current_function_p (void)
26308 return (current_instantiation ()
26309 && (current_instantiation ()->maybe_get_node ()
26310 == current_function_decl
));
26313 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26314 type. Return false for ok, true for disallowed. Issue error and
26315 inform messages under control of COMPLAIN. */
26318 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26320 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26322 else if (TYPE_PTR_P (type
))
26324 else if (TYPE_REF_P (type
)
26325 && !TYPE_REF_IS_RVALUE (type
))
26327 else if (TYPE_PTRMEM_P (type
))
26329 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26331 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26333 if (complain
& tf_error
)
26334 error ("non-type template parameters of deduced class type only "
26335 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26340 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26342 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26344 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26346 /* A bound template template parm could later be instantiated to have a valid
26347 nontype parm type via an alias template. */
26348 else if (cxx_dialect
>= cxx11
26349 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26351 else if (VOID_TYPE_P (type
))
26352 /* Fall through. */;
26353 else if (cxx_dialect
>= cxx20
)
26355 if (dependent_type_p (type
))
26357 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26359 if (structural_type_p (type
))
26361 if (complain
& tf_error
)
26363 auto_diagnostic_group d
;
26364 error ("%qT is not a valid type for a template non-type "
26365 "parameter because it is not structural", type
);
26366 structural_type_p (type
, true);
26370 else if (CLASS_TYPE_P (type
))
26372 if (complain
& tf_error
)
26373 error ("non-type template parameters of class type only available "
26374 "with %<-std=c++20%> or %<-std=gnu++20%>");
26378 if (complain
& tf_error
)
26380 if (type
== error_mark_node
)
26381 inform (input_location
, "invalid template non-type parameter");
26383 error ("%q#T is not a valid type for a template non-type parameter",
26389 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26390 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26393 dependent_type_p_r (tree type
)
26399 A type is dependent if it is:
26401 -- a template parameter. Template template parameters are types
26402 for us (since TYPE_P holds true for them) so we handle
26404 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26405 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26407 /* -- a qualified-id with a nested-name-specifier which contains a
26408 class-name that names a dependent type or whose unqualified-id
26409 names a dependent type. */
26410 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26413 /* An alias template specialization can be dependent even if the
26414 resulting type is not. */
26415 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26418 /* -- a cv-qualified type where the cv-unqualified type is
26420 No code is necessary for this bullet; the code below handles
26421 cv-qualified types, and we don't want to strip aliases with
26422 TYPE_MAIN_VARIANT because of DR 1558. */
26423 /* -- a compound type constructed from any dependent type. */
26424 if (TYPE_PTRMEM_P (type
))
26425 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26426 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26428 else if (INDIRECT_TYPE_P (type
))
26429 return dependent_type_p (TREE_TYPE (type
));
26430 else if (FUNC_OR_METHOD_TYPE_P (type
))
26434 if (dependent_type_p (TREE_TYPE (type
)))
26436 for (arg_type
= TYPE_ARG_TYPES (type
);
26438 arg_type
= TREE_CHAIN (arg_type
))
26439 if (dependent_type_p (TREE_VALUE (arg_type
)))
26441 if (cxx_dialect
>= cxx17
)
26442 /* A value-dependent noexcept-specifier makes the type dependent. */
26443 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26444 if (tree noex
= TREE_PURPOSE (spec
))
26445 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26446 affect overload resolution and treating it as dependent breaks
26447 things. Same for an unparsed noexcept expression. */
26448 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26449 && TREE_CODE (noex
) != DEFERRED_PARSE
26450 && value_dependent_expression_p (noex
))
26454 /* -- an array type constructed from any dependent type or whose
26455 size is specified by a constant expression that is
26458 We checked for type- and value-dependence of the bounds in
26459 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26460 if (TREE_CODE (type
) == ARRAY_TYPE
)
26462 if (TYPE_DOMAIN (type
)
26463 && dependent_type_p (TYPE_DOMAIN (type
)))
26465 return dependent_type_p (TREE_TYPE (type
));
26468 /* -- a template-id in which either the template name is a template
26470 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26472 /* ... or any of the template arguments is a dependent type or
26473 an expression that is type-dependent or value-dependent. */
26474 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26475 && (any_dependent_template_arguments_p
26476 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26479 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26480 dependent; if the argument of the `typeof' expression is not
26481 type-dependent, then it should already been have resolved. */
26482 if (TREE_CODE (type
) == TYPEOF_TYPE
26483 || TREE_CODE (type
) == DECLTYPE_TYPE
26484 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26487 /* A template argument pack is dependent if any of its packed
26489 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26491 tree args
= ARGUMENT_PACK_ARGS (type
);
26492 int i
, len
= TREE_VEC_LENGTH (args
);
26493 for (i
= 0; i
< len
; ++i
)
26494 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26498 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26499 be template parameters. */
26500 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26503 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26506 /* The standard does not specifically mention types that are local
26507 to template functions or local classes, but they should be
26508 considered dependent too. For example:
26510 template <int I> void f() {
26515 The size of `E' cannot be known until the value of `I' has been
26516 determined. Therefore, `E' must be considered dependent. */
26517 scope
= TYPE_CONTEXT (type
);
26518 if (scope
&& TYPE_P (scope
))
26519 return dependent_type_p (scope
);
26520 /* Don't use type_dependent_expression_p here, as it can lead
26521 to infinite recursion trying to determine whether a lambda
26522 nested in a lambda is dependent (c++/47687). */
26523 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26524 && DECL_LANG_SPECIFIC (scope
)
26525 && DECL_TEMPLATE_INFO (scope
)
26526 && (any_dependent_template_arguments_p
26527 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26530 /* Other types are non-dependent. */
26534 /* Returns TRUE if TYPE is dependent, in the sense of
26535 [temp.dep.type]. Note that a NULL type is considered dependent. */
26538 dependent_type_p (tree type
)
26540 /* If there are no template parameters in scope, then there can't be
26541 any dependent types. */
26542 if (!processing_template_decl
)
26544 /* If we are not processing a template, then nobody should be
26545 providing us with a dependent type. */
26547 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26551 /* If the type is NULL, we have not computed a type for the entity
26552 in question; in that case, the type is dependent. */
26556 /* Erroneous types can be considered non-dependent. */
26557 if (type
== error_mark_node
)
26560 /* Getting here with global_type_node means we improperly called this
26561 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26562 gcc_checking_assert (type
!= global_type_node
);
26564 /* If we have not already computed the appropriate value for TYPE,
26566 if (!TYPE_DEPENDENT_P_VALID (type
))
26568 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26569 TYPE_DEPENDENT_P_VALID (type
) = 1;
26572 return TYPE_DEPENDENT_P (type
);
26575 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26576 lookup. In other words, a dependent type that is not the current
26580 dependent_scope_p (tree scope
)
26582 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26583 && !currently_open_class (scope
));
26586 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26587 an unknown base of 'this' (and is therefore instantiation-dependent). */
26590 unknown_base_ref_p (tree t
)
26592 if (!current_class_ptr
)
26595 tree mem
= TREE_OPERAND (t
, 1);
26596 if (shared_member_p (mem
))
26599 tree cur
= current_nonlambda_class_type ();
26600 if (!any_dependent_bases_p (cur
))
26603 tree ctx
= TREE_OPERAND (t
, 0);
26604 if (DERIVED_FROM_P (ctx
, cur
))
26610 /* T is a SCOPE_REF; return whether we need to consider it
26611 instantiation-dependent so that we can check access at instantiation
26612 time even though we know which member it resolves to. */
26615 instantiation_dependent_scope_ref_p (tree t
)
26617 if (DECL_P (TREE_OPERAND (t
, 1))
26618 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26619 && !unknown_base_ref_p (t
)
26620 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26621 TREE_OPERAND (t
, 1)))
26627 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26628 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26631 /* Note that this predicate is not appropriate for general expressions;
26632 only constant expressions (that satisfy potential_constant_expression)
26633 can be tested for value dependence. */
26636 value_dependent_expression_p (tree expression
)
26638 if (!processing_template_decl
|| expression
== NULL_TREE
)
26641 /* A type-dependent expression is also value-dependent. */
26642 if (type_dependent_expression_p (expression
))
26645 switch (TREE_CODE (expression
))
26648 /* A dependent member function of the current instantiation. */
26649 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26651 case FUNCTION_DECL
:
26652 /* A dependent member function of the current instantiation. */
26653 if (DECL_CLASS_SCOPE_P (expression
)
26654 && dependent_type_p (DECL_CONTEXT (expression
)))
26658 case IDENTIFIER_NODE
:
26659 /* A name that has not been looked up -- must be dependent. */
26662 case TEMPLATE_PARM_INDEX
:
26663 /* A non-type template parm. */
26667 /* A non-type template parm. */
26668 if (DECL_TEMPLATE_PARM_P (expression
))
26670 return value_dependent_expression_p (DECL_INITIAL (expression
));
26673 /* A constant with literal type and is initialized
26674 with an expression that is value-dependent. */
26675 if (DECL_DEPENDENT_INIT_P (expression
)
26676 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26677 || TYPE_REF_P (TREE_TYPE (expression
)))
26679 if (DECL_HAS_VALUE_EXPR_P (expression
))
26681 tree value_expr
= DECL_VALUE_EXPR (expression
);
26682 if (value_dependent_expression_p (value_expr
)
26683 /* __PRETTY_FUNCTION__ inside a template function is dependent
26684 on the name of the function. */
26685 || (DECL_PRETTY_FUNCTION_P (expression
)
26686 /* It might be used in a template, but not a template
26687 function, in which case its DECL_VALUE_EXPR will be
26689 && value_expr
== error_mark_node
))
26694 case DYNAMIC_CAST_EXPR
:
26695 case STATIC_CAST_EXPR
:
26696 case CONST_CAST_EXPR
:
26697 case REINTERPRET_CAST_EXPR
:
26699 case IMPLICIT_CONV_EXPR
:
26700 /* These expressions are value-dependent if the type to which
26701 the cast occurs is dependent or the expression being casted
26702 is value-dependent. */
26704 tree type
= TREE_TYPE (expression
);
26706 if (dependent_type_p (type
))
26709 /* A functional cast has a list of operands. */
26710 expression
= TREE_OPERAND (expression
, 0);
26713 /* If there are no operands, it must be an expression such
26714 as "int()". This should not happen for aggregate types
26715 because it would form non-constant expressions. */
26716 gcc_assert (cxx_dialect
>= cxx11
26717 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26722 if (TREE_CODE (expression
) == TREE_LIST
)
26723 return any_value_dependent_elements_p (expression
);
26725 return value_dependent_expression_p (expression
);
26729 if (SIZEOF_EXPR_TYPE_P (expression
))
26730 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26734 /* A `sizeof' expression is value-dependent if the operand is
26735 type-dependent or is a pack expansion. */
26736 expression
= TREE_OPERAND (expression
, 0);
26737 if (PACK_EXPANSION_P (expression
))
26739 else if (TYPE_P (expression
))
26740 return dependent_type_p (expression
);
26741 return instantiation_dependent_uneval_expression_p (expression
);
26743 case AT_ENCODE_EXPR
:
26744 /* An 'encode' expression is value-dependent if the operand is
26746 expression
= TREE_OPERAND (expression
, 0);
26747 return dependent_type_p (expression
);
26749 case NOEXCEPT_EXPR
:
26750 expression
= TREE_OPERAND (expression
, 0);
26751 return instantiation_dependent_uneval_expression_p (expression
);
26754 /* All instantiation-dependent expressions should also be considered
26755 value-dependent. */
26756 return instantiation_dependent_scope_ref_p (expression
);
26758 case COMPONENT_REF
:
26759 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26760 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26762 case NONTYPE_ARGUMENT_PACK
:
26763 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26764 is value-dependent. */
26766 tree values
= ARGUMENT_PACK_ARGS (expression
);
26767 int i
, len
= TREE_VEC_LENGTH (values
);
26769 for (i
= 0; i
< len
; ++i
)
26770 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26778 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26780 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26786 if (TREE_CODE (type2
) != TREE_LIST
)
26787 return dependent_type_p (type2
);
26789 for (; type2
; type2
= TREE_CHAIN (type2
))
26790 if (dependent_type_p (TREE_VALUE (type2
)))
26797 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26798 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26801 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26802 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26806 tree op
= TREE_OPERAND (expression
, 0);
26807 return (value_dependent_expression_p (op
)
26808 || has_value_dependent_address (op
));
26811 case REQUIRES_EXPR
:
26812 /* Treat all requires-expressions as value-dependent so
26813 we don't try to fold them. */
26817 return dependent_type_p (TREE_OPERAND (expression
, 0));
26821 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26823 tree fn
= get_callee_fndecl (expression
);
26825 nargs
= call_expr_nargs (expression
);
26826 for (i
= 0; i
< nargs
; ++i
)
26828 tree op
= CALL_EXPR_ARG (expression
, i
);
26829 /* In a call to a constexpr member function, look through the
26830 implicit ADDR_EXPR on the object argument so that it doesn't
26831 cause the call to be considered value-dependent. We also
26832 look through it in potential_constant_expression. */
26833 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26834 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26835 && TREE_CODE (op
) == ADDR_EXPR
)
26836 op
= TREE_OPERAND (op
, 0);
26837 if (value_dependent_expression_p (op
))
26843 case TEMPLATE_ID_EXPR
:
26844 return concept_definition_p (TREE_OPERAND (expression
, 0));
26850 if (dependent_type_p (TREE_TYPE (expression
)))
26852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26853 if (value_dependent_expression_p (val
))
26859 /* Treat a GNU statement expression as dependent to avoid crashing
26860 under instantiate_non_dependent_expr; it can't be constant. */
26864 /* A constant expression is value-dependent if any subexpression is
26865 value-dependent. */
26866 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26868 case tcc_reference
:
26870 case tcc_comparison
:
26872 case tcc_expression
:
26875 int i
, len
= cp_tree_operand_length (expression
);
26877 for (i
= 0; i
< len
; i
++)
26879 tree t
= TREE_OPERAND (expression
, i
);
26881 /* In some cases, some of the operands may be missing.
26882 (For example, in the case of PREDECREMENT_EXPR, the
26883 amount to increment by may be missing.) That doesn't
26884 make the expression dependent. */
26885 if (t
&& value_dependent_expression_p (t
))
26896 /* The expression is not value-dependent. */
26900 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26901 [temp.dep.expr]. Note that an expression with no type is
26902 considered dependent. Other parts of the compiler arrange for an
26903 expression with type-dependent subexpressions to have no type, so
26904 this function doesn't have to be fully recursive. */
26907 type_dependent_expression_p (tree expression
)
26909 if (!processing_template_decl
)
26912 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26915 STRIP_ANY_LOCATION_WRAPPER (expression
);
26917 /* An unresolved name is always dependent. */
26918 if (identifier_p (expression
)
26919 || TREE_CODE (expression
) == USING_DECL
26920 || TREE_CODE (expression
) == WILDCARD_DECL
)
26923 /* A lambda-expression in template context is dependent. dependent_type_p is
26924 true for a lambda in the scope of a class or function template, but that
26925 doesn't cover all template contexts, like a default template argument. */
26926 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26929 /* A fold expression is type-dependent. */
26930 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26931 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26932 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26933 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26936 /* Some expression forms are never type-dependent. */
26937 if (TREE_CODE (expression
) == SIZEOF_EXPR
26938 || TREE_CODE (expression
) == ALIGNOF_EXPR
26939 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26940 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26941 || TREE_CODE (expression
) == TRAIT_EXPR
26942 || TREE_CODE (expression
) == TYPEID_EXPR
26943 || TREE_CODE (expression
) == DELETE_EXPR
26944 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26945 || TREE_CODE (expression
) == THROW_EXPR
26946 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26949 /* The types of these expressions depends only on the type to which
26950 the cast occurs. */
26951 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26952 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26953 || TREE_CODE (expression
) == CONST_CAST_EXPR
26954 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26955 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26956 || TREE_CODE (expression
) == CAST_EXPR
)
26957 return dependent_type_p (TREE_TYPE (expression
));
26959 /* The types of these expressions depends only on the type created
26960 by the expression. */
26961 if (TREE_CODE (expression
) == NEW_EXPR
26962 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26964 /* For NEW_EXPR tree nodes created inside a template, either
26965 the object type itself or a TREE_LIST may appear as the
26967 tree type
= TREE_OPERAND (expression
, 1);
26968 if (TREE_CODE (type
) == TREE_LIST
)
26969 /* This is an array type. We need to check array dimensions
26971 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26972 || value_dependent_expression_p
26973 (TREE_OPERAND (TREE_VALUE (type
), 1));
26974 /* Array type whose dimension has to be deduced. */
26975 else if (TREE_CODE (type
) == ARRAY_TYPE
26976 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
26979 return dependent_type_p (type
);
26982 if (TREE_CODE (expression
) == SCOPE_REF
)
26984 tree scope
= TREE_OPERAND (expression
, 0);
26985 tree name
= TREE_OPERAND (expression
, 1);
26987 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26988 contains an identifier associated by name lookup with one or more
26989 declarations declared with a dependent type, or...a
26990 nested-name-specifier or qualified-id that names a member of an
26991 unknown specialization. */
26992 return (type_dependent_expression_p (name
)
26993 || dependent_scope_p (scope
));
26996 if (TREE_CODE (expression
) == TEMPLATE_DECL
26997 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26998 return uses_outer_template_parms (expression
);
27000 if (TREE_CODE (expression
) == STMT_EXPR
)
27001 expression
= stmt_expr_value_expr (expression
);
27003 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
27008 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
27010 if (type_dependent_expression_p (elt
))
27016 /* A static data member of the current instantiation with incomplete
27017 array type is type-dependent, as the definition and specializations
27018 can have different bounds. */
27019 if (VAR_P (expression
)
27020 && DECL_CLASS_SCOPE_P (expression
)
27021 && dependent_type_p (DECL_CONTEXT (expression
))
27022 && VAR_HAD_UNKNOWN_BOUND (expression
))
27025 /* An array of unknown bound depending on a variadic parameter, eg:
27027 template<typename... Args>
27028 void foo (Args... args)
27030 int arr[] = { args... };
27033 template<int... vals>
27036 int arr[] = { vals... };
27039 If the array has no length and has an initializer, it must be that
27040 we couldn't determine its length in cp_complete_array_type because
27041 it is dependent. */
27042 if (VAR_P (expression
)
27043 && TREE_TYPE (expression
) != NULL_TREE
27044 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
27045 && !TYPE_DOMAIN (TREE_TYPE (expression
))
27046 && DECL_INITIAL (expression
))
27049 /* A function or variable template-id is type-dependent if it has any
27050 dependent template arguments. */
27051 if (VAR_OR_FUNCTION_DECL_P (expression
)
27052 && DECL_LANG_SPECIFIC (expression
)
27053 && DECL_TEMPLATE_INFO (expression
))
27055 /* Consider the innermost template arguments, since those are the ones
27056 that come from the template-id; the template arguments for the
27057 enclosing class do not make it type-dependent unless they are used in
27058 the type of the decl. */
27059 if (instantiates_primary_template_p (expression
)
27060 && (any_dependent_template_arguments_p
27061 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27065 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27066 type-dependent. Checking this is important for functions with auto return
27067 type, which looks like a dependent type. */
27068 if (TREE_CODE (expression
) == FUNCTION_DECL
27069 && !(DECL_CLASS_SCOPE_P (expression
)
27070 && dependent_type_p (DECL_CONTEXT (expression
)))
27071 && !(DECL_LANG_SPECIFIC (expression
)
27072 && DECL_UNIQUE_FRIEND_P (expression
)
27073 && (!DECL_FRIEND_CONTEXT (expression
)
27074 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27075 && !DECL_LOCAL_DECL_P (expression
))
27077 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27078 || undeduced_auto_decl (expression
));
27082 /* Always dependent, on the number of arguments if nothing else. */
27083 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27086 if (TREE_TYPE (expression
) == unknown_type_node
)
27088 if (TREE_CODE (expression
) == ADDR_EXPR
)
27089 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27090 if (TREE_CODE (expression
) == COMPONENT_REF
27091 || TREE_CODE (expression
) == OFFSET_REF
)
27093 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27095 expression
= TREE_OPERAND (expression
, 1);
27096 if (identifier_p (expression
))
27099 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27100 if (TREE_CODE (expression
) == SCOPE_REF
)
27103 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27104 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27105 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27108 if (BASELINK_P (expression
))
27110 if (BASELINK_OPTYPE (expression
)
27111 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27113 expression
= BASELINK_FUNCTIONS (expression
);
27116 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27118 if (any_dependent_template_arguments_p
27119 (TREE_OPERAND (expression
, 1)))
27121 expression
= TREE_OPERAND (expression
, 0);
27122 if (identifier_p (expression
))
27126 gcc_assert (OVL_P (expression
));
27128 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27129 if (type_dependent_expression_p (*iter
))
27135 /* The type of a non-type template parm declared with a placeholder type
27136 depends on the corresponding template argument, even though
27137 placeholders are not normally considered dependent. */
27138 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27139 && is_auto (TREE_TYPE (expression
)))
27142 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27144 /* Dependent type attributes might not have made it from the decl to
27146 if (DECL_P (expression
)
27147 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27150 return (dependent_type_p (TREE_TYPE (expression
)));
27153 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27154 type-dependent if the expression refers to a member of the current
27155 instantiation and the type of the referenced member is dependent, or the
27156 class member access expression refers to a member of an unknown
27159 This function returns true if the OBJECT in such a class member access
27160 expression is of an unknown specialization. */
27163 type_dependent_object_expression_p (tree object
)
27165 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27167 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27169 tree scope
= TREE_TYPE (object
);
27170 return (!scope
|| dependent_scope_p (scope
));
27173 /* walk_tree callback function for instantiation_dependent_expression_p,
27174 below. Returns non-zero if a dependent subexpression is found. */
27177 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27182 /* We don't have to worry about decltype currently because decltype
27183 of an instantiation-dependent expr is a dependent type. This
27184 might change depending on the resolution of DR 1172. */
27185 *walk_subtrees
= false;
27188 enum tree_code code
= TREE_CODE (*tp
);
27191 /* Don't treat an argument list as dependent just because it has no
27195 case NONTYPE_ARGUMENT_PACK
:
27198 case TEMPLATE_PARM_INDEX
:
27199 if (dependent_type_p (TREE_TYPE (*tp
)))
27201 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27203 /* We'll check value-dependence separately. */
27206 /* Handle expressions with type operands. */
27210 case AT_ENCODE_EXPR
:
27212 tree op
= TREE_OPERAND (*tp
, 0);
27213 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27214 op
= TREE_TYPE (op
);
27217 if (dependent_type_p (op
))
27221 *walk_subtrees
= false;
27228 case COMPONENT_REF
:
27229 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27230 /* In a template, finish_class_member_access_expr creates a
27231 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27232 type-dependent, so that we can check access control at
27233 instantiation time (PR 42277). See also Core issue 1273. */
27238 if (instantiation_dependent_scope_ref_p (*tp
))
27243 /* Treat statement-expressions as dependent. */
27247 /* Treat requires-expressions as dependent. */
27248 case REQUIRES_EXPR
:
27252 /* Treat concept checks as dependent. */
27253 if (concept_check_p (*tp
))
27257 case TEMPLATE_ID_EXPR
:
27258 /* Treat concept checks as dependent. */
27259 if (concept_check_p (*tp
))
27264 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27272 if (type_dependent_expression_p (*tp
))
27278 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27279 sense defined by the ABI:
27281 "An expression is instantiation-dependent if it is type-dependent
27282 or value-dependent, or it has a subexpression that is type-dependent
27283 or value-dependent."
27285 Except don't actually check value-dependence for unevaluated expressions,
27286 because in sizeof(i) we don't care about the value of i. Checking
27287 type-dependence will in turn check value-dependence of array bounds/template
27288 arguments as needed. */
27291 instantiation_dependent_uneval_expression_p (tree expression
)
27295 if (!processing_template_decl
)
27298 if (expression
== error_mark_node
)
27301 result
= cp_walk_tree_without_duplicates (&expression
,
27302 instantiation_dependent_r
, NULL
);
27303 return result
!= NULL_TREE
;
27306 /* As above, but also check value-dependence of the expression as a whole. */
27309 instantiation_dependent_expression_p (tree expression
)
27311 return (instantiation_dependent_uneval_expression_p (expression
)
27312 || (potential_constant_expression (expression
)
27313 && value_dependent_expression_p (expression
)));
27316 /* Like type_dependent_expression_p, but it also works while not processing
27317 a template definition, i.e. during substitution or mangling. */
27320 type_dependent_expression_p_push (tree expr
)
27323 ++processing_template_decl
;
27324 b
= type_dependent_expression_p (expr
);
27325 --processing_template_decl
;
27329 /* Returns TRUE if ARGS contains a type-dependent expression. */
27332 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27337 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27339 if (type_dependent_expression_p (arg
))
27345 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27346 expressions) contains any type-dependent expressions. */
27349 any_type_dependent_elements_p (const_tree list
)
27351 for (; list
; list
= TREE_CHAIN (list
))
27352 if (type_dependent_expression_p (TREE_VALUE (list
)))
27358 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27359 expressions) contains any value-dependent expressions. */
27362 any_value_dependent_elements_p (const_tree list
)
27364 for (; list
; list
= TREE_CHAIN (list
))
27365 if (value_dependent_expression_p (TREE_VALUE (list
)))
27371 /* Returns TRUE if the ARG (a template argument) is dependent. */
27374 dependent_template_arg_p (tree arg
)
27376 if (!processing_template_decl
)
27379 /* Assume a template argument that was wrongly written by the user
27380 is dependent. This is consistent with what
27381 any_dependent_template_arguments_p [that calls this function]
27383 if (!arg
|| arg
== error_mark_node
)
27386 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27387 arg
= argument_pack_select_arg (arg
);
27389 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27391 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27393 if (DECL_TEMPLATE_PARM_P (arg
))
27395 /* A member template of a dependent class is not necessarily
27396 type-dependent, but it is a dependent template argument because it
27397 will be a member of an unknown specialization to that template. */
27398 tree scope
= CP_DECL_CONTEXT (arg
);
27399 return TYPE_P (scope
) && dependent_type_p (scope
);
27401 else if (ARGUMENT_PACK_P (arg
))
27403 tree args
= ARGUMENT_PACK_ARGS (arg
);
27404 int i
, len
= TREE_VEC_LENGTH (args
);
27405 for (i
= 0; i
< len
; ++i
)
27407 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27413 else if (TYPE_P (arg
))
27414 return dependent_type_p (arg
);
27416 return value_dependent_expression_p (arg
);
27419 /* Returns true if ARGS (a collection of template arguments) contains
27420 any types that require structural equality testing. */
27423 any_template_arguments_need_structural_equality_p (tree args
)
27430 if (args
== error_mark_node
)
27433 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27435 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27436 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27438 tree arg
= TREE_VEC_ELT (level
, j
);
27439 tree packed_args
= NULL_TREE
;
27442 if (ARGUMENT_PACK_P (arg
))
27444 /* Look inside the argument pack. */
27445 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27446 len
= TREE_VEC_LENGTH (packed_args
);
27449 for (k
= 0; k
< len
; ++k
)
27452 arg
= TREE_VEC_ELT (packed_args
, k
);
27454 if (error_operand_p (arg
))
27456 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27458 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27460 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27461 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27470 /* Returns true if ARGS (a collection of template arguments) contains
27471 any dependent arguments. */
27474 any_dependent_template_arguments_p (const_tree args
)
27481 if (args
== error_mark_node
)
27484 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27486 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27487 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27488 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27495 /* Returns true if ARGS contains any errors. */
27498 any_erroneous_template_args_p (const_tree args
)
27503 if (args
== error_mark_node
)
27506 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27508 if (tree ti
= get_template_info (args
))
27509 args
= TI_ARGS (ti
);
27517 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27519 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27520 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27521 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27528 /* Returns TRUE if the template TMPL is type-dependent. */
27531 dependent_template_p (tree tmpl
)
27533 if (TREE_CODE (tmpl
) == OVERLOAD
)
27535 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27536 if (dependent_template_p (*iter
))
27541 /* Template template parameters are dependent. */
27542 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27543 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27545 /* So are names that have not been looked up. */
27546 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27551 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27554 dependent_template_id_p (tree tmpl
, tree args
)
27556 return (dependent_template_p (tmpl
)
27557 || any_dependent_template_arguments_p (args
));
27560 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27564 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27568 if (!processing_template_decl
)
27571 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27573 tree decl
= TREE_VEC_ELT (declv
, i
);
27574 tree init
= TREE_VEC_ELT (initv
, i
);
27575 tree cond
= TREE_VEC_ELT (condv
, i
);
27576 tree incr
= TREE_VEC_ELT (incrv
, i
);
27578 if (type_dependent_expression_p (decl
)
27579 || TREE_CODE (decl
) == SCOPE_REF
)
27582 if (init
&& type_dependent_expression_p (init
))
27585 if (cond
== global_namespace
)
27588 if (type_dependent_expression_p (cond
))
27591 if (COMPARISON_CLASS_P (cond
)
27592 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27593 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27596 if (TREE_CODE (incr
) == MODOP_EXPR
)
27598 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27599 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27602 else if (type_dependent_expression_p (incr
))
27604 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27606 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27608 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27610 tree t
= TREE_OPERAND (incr
, 1);
27611 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27612 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27615 /* If this loop has a class iterator with != comparison
27616 with increment other than i++/++i/i--/--i, make sure the
27617 increment is constant. */
27618 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27619 && TREE_CODE (cond
) == NE_EXPR
)
27621 if (TREE_OPERAND (t
, 0) == decl
)
27622 t
= TREE_OPERAND (t
, 1);
27624 t
= TREE_OPERAND (t
, 0);
27625 if (TREE_CODE (t
) != INTEGER_CST
)
27635 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27636 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27637 no such TYPE can be found. Note that this function peers inside
27638 uninstantiated templates and therefore should be used only in
27639 extremely limited situations. ONLY_CURRENT_P restricts this
27640 peering to the currently open classes hierarchy (which is required
27641 when comparing types). */
27644 resolve_typename_type (tree type
, bool only_current_p
)
27653 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27655 scope
= TYPE_CONTEXT (type
);
27656 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27657 gcc_checking_assert (uses_template_parms (scope
));
27659 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27660 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27661 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27662 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27663 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27664 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27665 the TYPENAME_TYPE instead, we avoid messing up with a possible
27666 typedef variant case. */
27667 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27669 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27670 it first before we can figure out what NAME refers to. */
27671 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27673 if (TYPENAME_IS_RESOLVING_P (scope
))
27674 /* Given a class template A with a dependent base with nested type C,
27675 typedef typename A::C::C C will land us here, as trying to resolve
27676 the initial A::C leads to the local C typedef, which leads back to
27677 A::C::C. So we break the recursion now. */
27680 scope
= resolve_typename_type (scope
, only_current_p
);
27682 /* If we don't know what SCOPE refers to, then we cannot resolve the
27684 if (!CLASS_TYPE_P (scope
))
27686 /* If this is a typedef, we don't want to look inside (c++/11987). */
27687 if (typedef_variant_p (type
))
27689 /* If SCOPE isn't the template itself, it will not have a valid
27690 TYPE_FIELDS list. */
27691 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27692 /* scope is either the template itself or a compatible instantiation
27693 like X<T>, so look up the name in the original template. */
27694 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27695 /* If scope has no fields, it can't be a current instantiation. Check this
27696 before currently_open_class to avoid infinite recursion (71515). */
27697 if (!TYPE_FIELDS (scope
))
27699 /* If the SCOPE is not the current instantiation, there's no reason
27700 to look inside it. */
27701 if (only_current_p
&& !currently_open_class (scope
))
27703 /* Enter the SCOPE so that name lookup will be resolved as if we
27704 were in the class definition. In particular, SCOPE will no
27705 longer be considered a dependent type. */
27706 pushed_scope
= push_scope (scope
);
27707 /* Look up the declaration. */
27708 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27709 tf_warning_or_error
);
27711 result
= NULL_TREE
;
27713 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27714 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27715 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27718 else if (identifier_p (fullname
)
27719 && TREE_CODE (decl
) == TYPE_DECL
)
27721 result
= TREE_TYPE (decl
);
27722 if (result
== error_mark_node
)
27723 result
= NULL_TREE
;
27725 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27726 && DECL_CLASS_TEMPLATE_P (decl
))
27728 /* Obtain the template and the arguments. */
27729 tree tmpl
= TREE_OPERAND (fullname
, 0);
27730 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27732 /* We get here with a plain identifier because a previous tentative
27733 parse of the nested-name-specifier as part of a ptr-operator saw
27734 ::template X<A>. The use of ::template is necessary in a
27735 ptr-operator, but wrong in a declarator-id.
27737 [temp.names]: In a qualified-id of a declarator-id, the keyword
27738 template shall not appear at the top level. */
27739 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27740 "keyword %<template%> not allowed in declarator-id");
27743 tree args
= TREE_OPERAND (fullname
, 1);
27744 /* Instantiate the template. */
27745 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27746 /*entering_scope=*/true,
27747 tf_error
| tf_user
);
27748 if (result
== error_mark_node
)
27749 result
= NULL_TREE
;
27752 /* Leave the SCOPE. */
27754 pop_scope (pushed_scope
);
27756 /* If we failed to resolve it, return the original typename. */
27760 /* If lookup found a typename type, resolve that too. */
27761 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27763 /* Ill-formed programs can cause infinite recursion here, so we
27764 must catch that. */
27765 TYPENAME_IS_RESOLVING_P (result
) = 1;
27766 result
= resolve_typename_type (result
, only_current_p
);
27767 TYPENAME_IS_RESOLVING_P (result
) = 0;
27770 /* Qualify the resulting type. */
27771 quals
= cp_type_quals (type
);
27773 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27778 /* EXPR is an expression which is not type-dependent. Return a proxy
27779 for EXPR that can be used to compute the types of larger
27780 expressions containing EXPR. */
27783 build_non_dependent_expr (tree expr
)
27785 tree orig_expr
= expr
;
27788 /* When checking, try to get a constant value for all non-dependent
27789 expressions in order to expose bugs in *_dependent_expression_p
27790 and constexpr. This can affect code generation, see PR70704, so
27791 only do this for -fchecking=2. */
27792 if (flag_checking
> 1
27793 && cxx_dialect
>= cxx11
27794 /* Don't do this during nsdmi parsing as it can lead to
27795 unexpected recursive instantiations. */
27796 && !parsing_nsdmi ()
27797 /* Don't do this during concept processing either and for
27798 the same reason. */
27799 && !processing_constraint_expression_p ())
27800 fold_non_dependent_expr (expr
, tf_none
);
27802 STRIP_ANY_LOCATION_WRAPPER (expr
);
27804 /* Preserve OVERLOADs; the functions must be available to resolve
27807 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27808 inner_expr
= stmt_expr_value_expr (inner_expr
);
27809 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27810 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27811 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27812 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27813 if (is_overloaded_fn (inner_expr
)
27814 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27816 /* There is no need to return a proxy for a variable or enumerator. */
27817 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27819 /* Preserve string constants; conversions from string constants to
27820 "char *" are allowed, even though normally a "const char *"
27821 cannot be used to initialize a "char *". */
27822 if (TREE_CODE (expr
) == STRING_CST
)
27824 /* Preserve void and arithmetic constants, as an optimization -- there is no
27825 reason to create a new node. */
27826 if (TREE_CODE (expr
) == VOID_CST
27827 || TREE_CODE (expr
) == INTEGER_CST
27828 || TREE_CODE (expr
) == REAL_CST
)
27830 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27831 There is at least one place where we want to know that a
27832 particular expression is a throw-expression: when checking a ?:
27833 expression, there are special rules if the second or third
27834 argument is a throw-expression. */
27835 if (TREE_CODE (expr
) == THROW_EXPR
)
27838 /* Don't wrap an initializer list, we need to be able to look inside. */
27839 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27842 /* Don't wrap a dummy object, we need to be able to test for it. */
27843 if (is_dummy_object (expr
))
27846 if (TREE_CODE (expr
) == COND_EXPR
)
27847 return build3 (COND_EXPR
,
27849 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27850 (TREE_OPERAND (expr
, 1)
27851 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27852 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27853 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27854 if (TREE_CODE (expr
) == COMPOUND_EXPR
27855 && !COMPOUND_EXPR_OVERLOADED (expr
))
27856 return build2 (COMPOUND_EXPR
,
27858 TREE_OPERAND (expr
, 0),
27859 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27861 /* If the type is unknown, it can't really be non-dependent */
27862 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27864 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27865 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27866 TREE_TYPE (expr
), expr
);
27869 /* ARGS is a vector of expressions as arguments to a function call.
27870 Replace the arguments with equivalent non-dependent expressions.
27871 This modifies ARGS in place. */
27874 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27879 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27881 tree newarg
= build_non_dependent_expr (arg
);
27883 (*args
)[ix
] = newarg
;
27887 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27888 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27889 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27892 make_auto_1 (tree name
, bool set_canonical
)
27894 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27895 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27896 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27897 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27898 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27899 TYPE_NAME (au
), NULL_TREE
);
27901 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27902 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27903 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27904 if (name
== decltype_auto_identifier
)
27905 AUTO_IS_DECLTYPE (au
) = true;
27911 make_decltype_auto (void)
27913 return make_auto_1 (decltype_auto_identifier
, true);
27919 return make_auto_1 (auto_identifier
, true);
27922 /* Return a C++17 deduction placeholder for class template TMPL. */
27925 make_template_placeholder (tree tmpl
)
27927 tree t
= make_auto_1 (auto_identifier
, false);
27928 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27929 /* Our canonical type depends on the placeholder. */
27930 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27934 /* True iff T is a C++17 class template deduction placeholder. */
27937 template_placeholder_p (tree t
)
27939 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27942 /* Make a "constrained auto" type-specifier. This is an auto or
27943 decltype(auto) type with constraints that must be associated after
27944 deduction. The constraint is formed from the given concept CON
27945 and its optional sequence of template arguments ARGS.
27947 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27950 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27952 /* Build the constraint. */
27953 tree tmpl
= DECL_TI_TEMPLATE (con
);
27955 if (TREE_CODE (con
) == FUNCTION_DECL
)
27956 expr
= ovl_make (tmpl
);
27957 ++processing_template_decl
;
27958 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27959 --processing_template_decl
;
27961 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27963 /* Our canonical type depends on the constraint. */
27964 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27966 /* Attach the constraint to the type declaration. */
27967 return TYPE_NAME (type
);
27970 /* Make a "constrained auto" type-specifier. */
27973 make_constrained_auto (tree con
, tree args
)
27975 tree type
= make_auto_1 (auto_identifier
, false);
27976 return make_constrained_placeholder_type (type
, con
, args
);
27979 /* Make a "constrained decltype(auto)" type-specifier. */
27982 make_constrained_decltype_auto (tree con
, tree args
)
27984 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27985 return make_constrained_placeholder_type (type
, con
, args
);
27988 /* Build and return a concept definition. Like other templates, the
27989 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27990 the TEMPLATE_DECL. */
27993 finish_concept_definition (cp_expr id
, tree init
)
27995 gcc_assert (identifier_p (id
));
27996 gcc_assert (processing_template_decl
);
27998 location_t loc
= id
.get_location();
28000 /* A concept-definition shall not have associated constraints. */
28001 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
28003 error_at (loc
, "a concept cannot be constrained");
28004 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
28007 /* A concept-definition shall appear in namespace scope. Templates
28008 aren't allowed in block scope, so we only need to check for class
28010 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28012 error_at (loc
, "concept %qE not in namespace scope", *id
);
28013 return error_mark_node
;
28016 /* Initially build the concept declaration; its type is bool. */
28017 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
28018 DECL_CONTEXT (decl
) = current_scope ();
28019 DECL_INITIAL (decl
) = init
;
28021 /* Push the enclosing template. */
28022 return push_template_decl (decl
);
28025 /* Given type ARG, return std::initializer_list<ARG>. */
28030 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
28032 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
28034 gcc_rich_location
richloc (input_location
);
28035 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
28036 error_at (&richloc
,
28037 "deducing from brace-enclosed initializer list"
28038 " requires %<#include <initializer_list>%>");
28040 return error_mark_node
;
28042 tree argvec
= make_tree_vec (1);
28043 TREE_VEC_ELT (argvec
, 0) = arg
;
28045 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
28046 NULL_TREE
, 0, tf_warning_or_error
);
28049 /* Replace auto in TYPE with std::initializer_list<auto>. */
28052 listify_autos (tree type
, tree auto_node
)
28054 tree init_auto
= listify (strip_top_quals (auto_node
));
28055 tree argvec
= make_tree_vec (1);
28056 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28057 if (processing_template_decl
)
28058 argvec
= add_to_template_args (current_template_args (), argvec
);
28059 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28062 /* Hash traits for hashing possibly constrained 'auto'
28063 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28065 struct auto_hash
: default_hash_traits
<tree
>
28067 static inline hashval_t
hash (tree
);
28068 static inline bool equal (tree
, tree
);
28071 /* Hash the 'auto' T. */
28074 auto_hash::hash (tree t
)
28076 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28077 /* Matching constrained-type-specifiers denote the same template
28078 parameter, so hash the constraint. */
28079 return hash_placeholder_constraint (c
);
28081 /* But unconstrained autos are all separate, so just hash the pointer. */
28082 return iterative_hash_object (t
, 0);
28085 /* Compare two 'auto's. */
28088 auto_hash::equal (tree t1
, tree t2
)
28093 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28094 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28096 /* Two unconstrained autos are distinct. */
28100 return equivalent_placeholder_constraints (c1
, c2
);
28103 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28104 constrained) auto, add it to the vector. */
28107 extract_autos_r (tree t
, void *data
)
28109 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28112 /* All the autos were built with index 0; fix that up now. */
28113 tree
*p
= hash
.find_slot (t
, INSERT
);
28116 /* If this is a repeated constrained-type-specifier, use the index we
28118 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28121 /* Otherwise this is new, so use the current count. */
28123 idx
= hash
.elements () - 1;
28125 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28128 /* Always keep walking. */
28132 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28133 says they can appear anywhere in the type. */
28136 extract_autos (tree type
)
28138 hash_set
<tree
> visited
;
28139 hash_table
<auto_hash
> hash (2);
28141 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28143 tree tree_vec
= make_tree_vec (hash
.elements());
28144 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28145 iter
!= hash
.end(); ++iter
)
28148 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28149 TREE_VEC_ELT (tree_vec
, i
)
28150 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28156 /* The stem for deduction guide names. */
28157 const char *const dguide_base
= "__dguide_";
28159 /* Return the name for a deduction guide for class template TMPL. */
28162 dguide_name (tree tmpl
)
28164 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28165 tree tname
= TYPE_IDENTIFIER (type
);
28166 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28167 + IDENTIFIER_LENGTH (tname
));
28168 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28169 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28170 IDENTIFIER_LENGTH (tname
) + 1);
28171 tree dname
= get_identifier (buf
);
28172 TREE_TYPE (dname
) = type
;
28176 /* True if NAME is the name of a deduction guide. */
28179 dguide_name_p (tree name
)
28181 return (TREE_CODE (name
) == IDENTIFIER_NODE
28182 && TREE_TYPE (name
)
28183 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28184 strlen (dguide_base
)));
28187 /* True if FN is a deduction guide. */
28190 deduction_guide_p (const_tree fn
)
28193 if (tree name
= DECL_NAME (fn
))
28194 return dguide_name_p (name
);
28198 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28201 copy_guide_p (const_tree fn
)
28203 gcc_assert (deduction_guide_p (fn
));
28204 if (!DECL_ARTIFICIAL (fn
))
28206 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28207 return (TREE_CHAIN (parms
) == void_list_node
28208 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28211 /* True if FN is a guide generated from a constructor template. */
28214 template_guide_p (const_tree fn
)
28216 gcc_assert (deduction_guide_p (fn
));
28217 if (!DECL_ARTIFICIAL (fn
))
28219 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28220 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28221 return PRIMARY_TEMPLATE_P (org
);
28225 /* True if FN is an aggregate initialization guide or the copy deduction
28229 builtin_guide_p (const_tree fn
)
28231 if (!deduction_guide_p (fn
))
28233 if (!DECL_ARTIFICIAL (fn
))
28234 /* Explicitly declared. */
28236 if (DECL_ABSTRACT_ORIGIN (fn
))
28237 /* Derived from a constructor. */
28242 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28243 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28244 template parameter types. Note that the handling of template template
28245 parameters relies on current_template_parms being set appropriately for the
28249 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28250 tree tsubst_args
, tsubst_flags_t complain
)
28252 if (olddecl
== error_mark_node
)
28253 return error_mark_node
;
28255 tree oldidx
= get_template_parm_index (olddecl
);
28258 if (TREE_CODE (olddecl
) == TYPE_DECL
28259 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28261 tree oldtype
= TREE_TYPE (olddecl
);
28262 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28263 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28264 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28265 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28266 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28270 newtype
= TREE_TYPE (olddecl
);
28271 if (type_uses_auto (newtype
))
28273 // Substitute once to fix references to other template parameters.
28274 newtype
= tsubst (newtype
, tsubst_args
,
28275 complain
|tf_partial
, NULL_TREE
);
28276 // Now substitute again to reduce the level of the auto.
28277 newtype
= tsubst (newtype
, current_template_args (),
28278 complain
, NULL_TREE
);
28281 newtype
= tsubst (newtype
, tsubst_args
,
28282 complain
, NULL_TREE
);
28286 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28287 DECL_NAME (olddecl
), newtype
);
28288 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28291 if (TREE_CODE (olddecl
) == TYPE_DECL
28292 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28294 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28295 = build_template_parm_index (index
, level
, level
,
28297 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28298 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28299 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28300 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28301 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28303 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28305 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28307 DECL_TEMPLATE_RESULT (newdecl
)
28308 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28309 DECL_NAME (olddecl
), newtype
);
28310 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28311 // First create a copy (ttargs) of tsubst_args with an
28312 // additional level for the template template parameter's own
28313 // template parameters (ttparms).
28314 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28315 (DECL_TEMPLATE_PARMS (olddecl
)));
28316 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28317 tree ttargs
= make_tree_vec (depth
+ 1);
28318 for (int i
= 0; i
< depth
; ++i
)
28319 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28320 TREE_VEC_ELT (ttargs
, depth
)
28321 = template_parms_level_to_args (ttparms
);
28322 // Substitute ttargs into ttparms to fix references to
28323 // other template parameters.
28324 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28325 complain
|tf_partial
);
28326 // Now substitute again with args based on tparms, to reduce
28327 // the level of the ttparms.
28328 ttargs
= current_template_args ();
28329 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28331 // Finally, tack the adjusted parms onto tparms.
28332 ttparms
= tree_cons (size_int (depth
), ttparms
,
28333 current_template_parms
);
28334 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28339 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28341 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28342 TREE_CODE (oldconst
),
28343 DECL_NAME (oldconst
), newtype
);
28344 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28345 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28346 SET_DECL_TEMPLATE_PARM_P (newconst
);
28347 newidx
= build_template_parm_index (index
, level
, level
,
28348 newconst
, newtype
);
28349 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28350 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28351 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28357 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28358 template parameter. */
28361 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28362 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28364 tree olddecl
= TREE_VALUE (oldelt
);
28365 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28367 if (newdecl
== error_mark_node
)
28368 return error_mark_node
;
28369 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28370 targs
, complain
, NULL_TREE
);
28371 tree list
= build_tree_list (newdef
, newdecl
);
28372 TEMPLATE_PARM_CONSTRAINTS (list
)
28373 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28374 targs
, complain
, NULL_TREE
);
28375 int depth
= TMPL_ARGS_DEPTH (targs
);
28376 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28380 /* Returns a C++17 class deduction guide template based on the constructor
28381 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28382 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28383 aggregate initialization guide. */
28386 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28388 tree tparms
, targs
, fparms
, fargs
, ci
;
28389 bool memtmpl
= false;
28392 tree fn_tmpl
= NULL_TREE
;
28396 ++processing_template_decl
;
28397 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28398 --processing_template_decl
;
28401 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28405 bool copy_p
= TYPE_REF_P (ctor
);
28407 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28409 fparms
= void_list_node
;
28411 else if (TREE_CODE (ctor
) == TREE_LIST
)
28414 gcc_unreachable ();
28416 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28417 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28418 targs
= CLASSTYPE_TI_ARGS (type
);
28421 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28422 explicit_p
= false;
28426 ++processing_template_decl
;
28430 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28431 : DECL_TI_TEMPLATE (ctor
));
28433 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28434 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28436 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28437 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28438 fully specialized args for the enclosing class. Strip those off, as
28439 the deduction guide won't have those template parameters. */
28440 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28441 TMPL_PARMS_DEPTH (tparms
));
28442 /* Discard the 'this' parameter. */
28443 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28444 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28445 ci
= get_constraints (ctor
);
28446 loc
= DECL_SOURCE_LOCATION (ctor
);
28447 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28449 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28453 /* For a member template constructor, we need to flatten the two
28454 template parameter lists into one, and then adjust the function
28455 signature accordingly. This gets...complicated. */
28456 tree save_parms
= current_template_parms
;
28458 /* For a member template we should have two levels of parms/args, one
28459 for the class and one for the constructor. We stripped
28460 specialized args for further enclosing classes above. */
28461 const int depth
= 2;
28462 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28464 /* Template args for translating references to the two-level template
28465 parameters into references to the one-level template parameters we
28467 tree tsubst_args
= copy_node (targs
);
28468 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28469 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28471 /* Template parms for the constructor template. */
28472 tree ftparms
= TREE_VALUE (tparms
);
28473 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28474 /* Template parms for the class template. */
28475 tparms
= TREE_CHAIN (tparms
);
28476 tree ctparms
= TREE_VALUE (tparms
);
28477 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28478 /* Template parms for the deduction guide start as a copy of the
28479 template parms for the class. We set current_template_parms for
28480 lookup_template_class_1. */
28481 current_template_parms
= tparms
= copy_node (tparms
);
28482 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28483 for (unsigned i
= 0; i
< clen
; ++i
)
28484 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28486 /* Now we need to rewrite the constructor parms to append them to the
28488 for (unsigned i
= 0; i
< flen
; ++i
)
28490 unsigned index
= i
+ clen
;
28491 unsigned level
= 1;
28492 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28494 = rewrite_tparm_list (oldelt
, index
, level
,
28495 tsubst_args
, i
, complain
);
28496 if (newelt
== error_mark_node
)
28498 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28501 /* Now we have a final set of template parms to substitute into the
28502 function signature. */
28503 targs
= template_parms_to_args (tparms
);
28504 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28506 if (fparms
== error_mark_node
)
28509 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28511 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28512 cp_unevaluated_operand. */
28514 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28515 current_template_parms
= save_parms
;
28519 /* Substitute in the same arguments to rewrite class members into
28520 references to members of an unknown specialization. */
28522 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28523 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28525 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28528 --processing_template_decl
;
28530 return error_mark_node
;
28535 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28536 tparms
= copy_node (tparms
);
28537 INNERMOST_TEMPLATE_PARMS (tparms
)
28538 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28541 tree fntype
= build_function_type (type
, fparms
);
28542 tree ded_fn
= build_lang_decl_loc (loc
,
28544 dguide_name (type
), fntype
);
28545 DECL_ARGUMENTS (ded_fn
) = fargs
;
28546 DECL_ARTIFICIAL (ded_fn
) = true;
28547 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28548 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28549 DECL_ARTIFICIAL (ded_tmpl
) = true;
28550 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28551 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28553 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28555 set_constraints (ded_tmpl
, ci
);
28560 /* Add to LIST the member types for the reshaped initializer CTOR. */
28563 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28565 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28566 tree idx
, val
; unsigned i
;
28567 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28569 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28570 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28571 && CONSTRUCTOR_NELTS (val
)
28572 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28573 type gets a single initializer. */
28574 && CP_AGGREGATE_TYPE_P (ftype
)
28575 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28576 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28578 tree subelt
= NULL_TREE
;
28579 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28580 subelt
= TREE_TYPE (ftype
);
28581 list
= collect_ctor_idx_types (val
, list
, subelt
);
28584 tree arg
= NULL_TREE
;
28585 if (i
== v
->length() - 1
28586 && PACK_EXPANSION_P (ftype
))
28587 /* Give the trailing pack expansion parameter a default argument to
28588 match aggregate initialization behavior, even if we deduce the
28589 length of the pack separately to more than we have initializers. */
28590 arg
= build_constructor (init_list_type_node
, NULL
);
28591 /* if ei is of array type and xi is a braced-init-list or string literal,
28592 Ti is an rvalue reference to the declared type of ei */
28593 STRIP_ANY_LOCATION_WRAPPER (val
);
28594 if (TREE_CODE (ftype
) == ARRAY_TYPE
28595 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28596 || TREE_CODE (val
) == STRING_CST
))
28598 if (TREE_CODE (val
) == STRING_CST
)
28599 ftype
= cp_build_qualified_type
28600 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28601 ftype
= (cp_build_reference_type
28602 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28604 list
= tree_cons (arg
, ftype
, list
);
28610 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28613 is_spec_or_derived (tree etype
, tree tmpl
)
28615 if (!etype
|| !CLASS_TYPE_P (etype
))
28618 tree type
= TREE_TYPE (tmpl
);
28619 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28620 (DECL_TEMPLATE_PARMS (tmpl
)));
28621 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28622 int err
= unify (tparms
, targs
, type
, etype
,
28623 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28628 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28632 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28634 if (cxx_dialect
< cxx20
)
28637 if (init
== NULL_TREE
)
28640 tree type
= TREE_TYPE (tmpl
);
28641 if (!CP_AGGREGATE_TYPE_P (type
))
28644 /* No aggregate candidate for copy-initialization. */
28645 if (args
->length() == 1)
28647 tree val
= (*args
)[0];
28648 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28652 /* If we encounter a problem, we just won't add the candidate. */
28653 tsubst_flags_t complain
= tf_none
;
28655 tree parms
= NULL_TREE
;
28656 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28658 init
= reshape_init (type
, init
, complain
);
28659 if (init
== error_mark_node
)
28661 parms
= collect_ctor_idx_types (init
, parms
);
28663 else if (TREE_CODE (init
) == TREE_LIST
)
28665 int len
= list_length (init
);
28666 for (tree field
= TYPE_FIELDS (type
);
28668 --len
, field
= DECL_CHAIN (field
))
28670 field
= next_initializable_field (field
);
28673 tree ftype
= finish_decltype_type (field
, true, complain
);
28674 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28678 /* Aggregate initialization doesn't apply to an initializer expression. */
28684 parms
= nreverse (parms
);
28685 TREE_CHAIN (last
) = void_list_node
;
28686 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28693 /* UGUIDES are the deduction guides for the underlying template of alias
28694 template TMPL; adjust them to be deduction guides for TMPL. */
28697 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28699 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28700 class type (9.2.8.2) where the template-name names an alias template A,
28701 the defining-type-id of A must be of the form
28703 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28705 as specified in 9.2.8.2. The guides of A are the set of functions or
28706 function templates formed as follows. For each function or function
28707 template f in the guides of the template named by the simple-template-id
28708 of the defining-type-id, the template arguments of the return type of f
28709 are deduced from the defining-type-id of A according to the process in
28710 13.10.2.5 with the exception that deduction does not fail if not all
28711 template arguments are deduced. Let g denote the result of substituting
28712 these deductions into f. If substitution succeeds, form a function or
28713 function template f' with the following properties and add it to the set
28716 * The function type of f' is the function type of g.
28718 * If f is a function template, f' is a function template whose template
28719 parameter list consists of all the template parameters of A (including
28720 their default template arguments) that appear in the above deductions or
28721 (recursively) in their default template arguments, followed by the
28722 template parameters of f that were not deduced (including their default
28723 template arguments), otherwise f' is not a function template.
28725 * The associated constraints (13.5.2) are the conjunction of the
28726 associated constraints of g and a constraint that is satisfied if and only
28727 if the arguments of A are deducible (see below) from the return type.
28729 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28732 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28733 considered to be so as well.
28735 * The explicit-specifier of f' is the explicit-specifier of g (if
28738 /* This implementation differs from the above in two significant ways:
28740 1) We include all template parameters of A, not just some.
28741 2) The added constraint is same_type instead of deducible.
28743 I believe that while it's probably possible to construct a testcase that
28744 behaves differently with this simplification, it should have the same
28745 effect for real uses. Including all template parameters means that we
28746 deduce all parameters of A when resolving the call, so when we're in the
28747 constraint we don't need to deduce them again, we can just check whether
28748 the deduction produced the desired result. */
28750 tsubst_flags_t complain
= tf_warning_or_error
;
28751 tree atype
= TREE_TYPE (tmpl
);
28752 tree aguides
= NULL_TREE
;
28753 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28754 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28755 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28756 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28760 location_t loc
= DECL_SOURCE_LOCATION (f
);
28761 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28763 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28765 processing_template_decl_sentinel
ptds (/*reset*/false);
28766 ++processing_template_decl
;
28768 /* Deduce template arguments for f from the type-id of A. */
28769 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28770 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28771 tree targs
= make_tree_vec (len
);
28772 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28775 /* The number of parms for f' is the number of parms for A plus
28776 non-deduced parms of f. */
28777 unsigned ndlen
= 0;
28779 for (unsigned i
= 0; i
< len
; ++i
)
28780 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28782 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28784 /* First copy over the parms of A. */
28785 for (j
= 0; j
< natparms
; ++j
)
28786 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28787 /* Now rewrite the non-deduced parms of f. */
28788 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28789 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28792 unsigned index
= j
++;
28793 unsigned level
= 1;
28794 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28795 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28796 targs
, i
, complain
);
28797 TREE_VEC_ELT (gtparms
, index
) = list
;
28799 gtparms
= build_tree_list (size_one_node
, gtparms
);
28801 /* Substitute the deduced arguments plus the rewritten template
28802 parameters into f to get g. This covers the type, copyness,
28803 guideness, and explicit-specifier. */
28804 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28805 if (g
== error_mark_node
)
28806 return error_mark_node
;
28807 DECL_USE_TEMPLATE (g
) = 0;
28808 fprime
= build_template_decl (g
, gtparms
, false);
28809 DECL_TEMPLATE_RESULT (fprime
) = g
;
28810 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28811 tree gtargs
= template_parms_to_args (gtparms
);
28812 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28813 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28815 /* Substitute the associated constraints. */
28816 tree ci
= get_constraints (f
);
28818 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28819 if (ci
== error_mark_node
)
28820 return error_mark_node
;
28822 /* Add a constraint that the return type matches the instantiation of
28823 A with the same template arguments. */
28824 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28825 if (!same_type_p (atype
, ret
)
28826 /* FIXME this should mean they don't compare as equivalent. */
28827 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28829 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28830 ci
= append_constraint (ci
, same
);
28835 remove_constraints (fprime
);
28836 set_constraints (fprime
, ci
);
28841 /* For a non-template deduction guide, if the arguments of A aren't
28842 deducible from the return type, don't add the candidate. */
28843 tree targs
= make_tree_vec (natparms
);
28844 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28845 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28846 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28852 aguides
= lookup_add (fprime
, aguides
);
28858 /* Return artificial deduction guides built from the constructors of class
28862 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28864 tree type
= TREE_TYPE (tmpl
);
28865 tree outer_args
= NULL_TREE
;
28866 if (DECL_CLASS_SCOPE_P (tmpl
)
28867 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28869 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28870 type
= TREE_TYPE (most_general_template (tmpl
));
28873 tree cands
= NULL_TREE
;
28875 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28877 /* Skip inherited constructors. */
28878 if (iter
.using_p ())
28881 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28882 cands
= lookup_add (guide
, cands
);
28885 /* Add implicit default constructor deduction guide. */
28886 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28888 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28890 cands
= lookup_add (guide
, cands
);
28893 /* Add copy guide. */
28895 tree gtype
= build_reference_type (type
);
28896 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28898 cands
= lookup_add (guide
, cands
);
28904 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28906 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28907 aggregate candidate is added separately because it depends on the
28908 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28912 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
28914 tree guides
= NULL_TREE
;
28915 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28917 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28918 tree tinfo
= get_template_info (under
);
28919 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
28924 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28925 dguide_name (tmpl
),
28926 LOOK_want::NORMAL
, /*complain*/false);
28927 if (guides
== error_mark_node
)
28928 guides
= NULL_TREE
;
28930 any_dguides_p
= true;
28933 /* Cache the deduction guides for a template. We also remember the result of
28934 lookup, and rebuild everything if it changes; should be very rare. */
28935 tree_pair_p cache
= NULL
;
28937 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28940 if (cache
->purpose
== guides
)
28941 return cache
->value
;
28945 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28946 cache
->purpose
= guides
;
28949 tree cands
= NULL_TREE
;
28950 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28951 cands
= alias_ctad_tweaks (tmpl
, guides
);
28954 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28955 for (ovl_iterator
it (guides
); it
; ++it
)
28956 cands
= lookup_add (*it
, cands
);
28959 cache
->value
= cands
;
28963 /* Return whether TMPL is a (class template argument-) deducible template. */
28966 ctad_template_p (tree tmpl
)
28968 /* A deducible template is either a class template or is an alias template
28969 whose defining-type-id is of the form
28971 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28973 where the nested-name-specifier (if any) is non-dependent and the
28974 template-name of the simple-template-id names a deducible template. */
28976 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28977 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28979 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28981 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28982 if (tree tinfo
= get_template_info (orig
))
28983 return ctad_template_p (TI_TEMPLATE (tinfo
));
28987 /* Deduce template arguments for the class template placeholder PTYPE for
28988 template TMPL based on the initializer INIT, and return the resulting
28992 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28993 int flags
, tsubst_flags_t complain
)
28995 /* We should have handled this in the caller. */
28996 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28999 /* Look through alias templates that just rename another template. */
29000 tmpl
= get_underlying_template (tmpl
);
29001 if (!ctad_template_p (tmpl
))
29003 if (complain
& tf_error
)
29004 error ("non-deducible template %qT used without template arguments", tmpl
);
29005 return error_mark_node
;
29007 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
29009 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29010 if (complain
& tf_warning_or_error
)
29011 pedwarn (input_location
, 0, "alias template deduction only available "
29012 "with %<-std=c++20%> or %<-std=gnu++20%>");
29015 if (init
&& TREE_TYPE (init
) == ptype
)
29016 /* Using the template parm as its own argument. */
29019 tree type
= TREE_TYPE (tmpl
);
29021 bool try_list_ctor
= false;
29022 bool list_init_p
= false;
29024 releasing_vec rv_args
= NULL
;
29025 vec
<tree
,va_gc
> *&args
= *&rv_args
;
29026 if (init
== NULL_TREE
)
29027 args
= make_tree_vector ();
29028 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29030 list_init_p
= true;
29031 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
29032 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
29034 /* As an exception, the first phase in 16.3.1.7 (considering the
29035 initializer list as a single argument) is omitted if the
29036 initializer list consists of a single expression of type cv U,
29037 where U is a specialization of C or a class derived from a
29038 specialization of C. */
29039 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
29040 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
29041 try_list_ctor
= false;
29043 if (try_list_ctor
|| is_std_init_list (type
))
29044 args
= make_tree_vector_single (init
);
29046 args
= make_tree_vector_from_ctor (init
);
29048 else if (TREE_CODE (init
) == TREE_LIST
)
29049 args
= make_tree_vector_from_list (init
);
29051 args
= make_tree_vector_single (init
);
29053 /* Do this now to avoid problems with erroneous args later on. */
29054 args
= resolve_args (args
, complain
);
29056 return error_mark_node
;
29058 bool any_dguides_p
= false;
29059 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29060 if (cands
== error_mark_node
)
29061 return error_mark_node
;
29063 /* Prune explicit deduction guides in copy-initialization context (but
29064 not copy-list-initialization). */
29065 bool elided
= false;
29066 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
29068 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29069 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29074 /* Found a nonconverting guide, prune the candidates. */
29075 tree pruned
= NULL_TREE
;
29076 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29077 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29078 pruned
= lookup_add (*iter
, pruned
);
29084 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29085 cands
= lookup_add (guide
, cands
);
29087 tree call
= error_mark_node
;
29089 /* If this is list-initialization and the class has a list constructor, first
29090 try deducing from the list as a single argument, as [over.match.list]. */
29091 tree list_cands
= NULL_TREE
;
29092 if (try_list_ctor
&& cands
)
29093 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29096 if (is_list_ctor (dg
))
29097 list_cands
= lookup_add (dg
, list_cands
);
29101 ++cp_unevaluated_operand
;
29102 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29103 --cp_unevaluated_operand
;
29105 if (call
== error_mark_node
)
29107 /* That didn't work, now try treating the list as a sequence of
29109 release_tree_vector (args
);
29110 args
= make_tree_vector_from_ctor (init
);
29114 if (elided
&& !cands
)
29116 error ("cannot deduce template arguments for copy-initialization"
29117 " of %qT, as it has no non-explicit deduction guides or "
29118 "user-declared constructors", type
);
29119 return error_mark_node
;
29121 else if (!cands
&& call
== error_mark_node
)
29123 error ("cannot deduce template arguments of %qT, as it has no viable "
29124 "deduction guides", type
);
29125 return error_mark_node
;
29128 if (call
== error_mark_node
)
29130 ++cp_unevaluated_operand
;
29131 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29132 --cp_unevaluated_operand
;
29135 if (call
== error_mark_node
)
29137 if (complain
& tf_warning_or_error
)
29139 error ("class template argument deduction failed:");
29141 ++cp_unevaluated_operand
;
29142 call
= build_new_function_call (cands
, &args
,
29143 complain
| tf_decltype
);
29144 --cp_unevaluated_operand
;
29147 inform (input_location
, "explicit deduction guides not considered "
29148 "for copy-initialization");
29150 return error_mark_node
;
29152 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29153 constructor is chosen, the initialization is ill-formed. */
29154 else if (flags
& LOOKUP_ONLYCONVERTING
)
29156 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29157 if (fndecl
&& DECL_NONCONVERTING_P (fndecl
))
29159 if (complain
& tf_warning_or_error
)
29161 // TODO: Pass down location from cp_finish_decl.
29162 error ("class template argument deduction for %qT failed: "
29163 "explicit deduction guide selected in "
29164 "copy-list-initialization", type
);
29165 inform (DECL_SOURCE_LOCATION (fndecl
),
29166 "explicit deduction guide declared here");
29169 return error_mark_node
;
29173 /* If CTAD succeeded but the type doesn't have any explicit deduction
29174 guides, this deduction might not be what the user intended. */
29175 if (call
!= error_mark_node
&& !any_dguides_p
)
29177 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29178 if (fndecl
!= NULL_TREE
29179 && (!DECL_IN_SYSTEM_HEADER (fndecl
)
29180 || global_dc
->dc_warn_system_headers
)
29181 && warning (OPT_Wctad_maybe_unsupported
,
29182 "%qT may not intend to support class template argument "
29183 "deduction", type
))
29184 inform (input_location
, "add a deduction guide to suppress this "
29188 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29191 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29192 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29193 The CONTEXT determines the context in which auto deduction is performed
29194 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29195 OUTER_TARGS are used during template argument deduction
29196 (context == adc_unify) to properly substitute the result, and is ignored
29199 For partial-concept-ids, extra args may be appended to the list of deduced
29200 template arguments prior to determining constraint satisfaction. */
29203 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29204 tsubst_flags_t complain
, auto_deduction_context context
,
29205 tree outer_targs
, int flags
)
29209 if (init
== error_mark_node
)
29210 return error_mark_node
;
29212 if (init
&& type_dependent_expression_p (init
)
29213 && context
!= adc_unify
)
29214 /* Defining a subset of type-dependent expressions that we can deduce
29215 from ahead of time isn't worth the trouble. */
29218 /* Similarly, we can't deduce from another undeduced decl. */
29219 if (init
&& undeduced_auto_decl (init
))
29222 /* We may be doing a partial substitution, but we still want to replace
29224 complain
&= ~tf_partial
;
29226 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29227 /* C++17 class template argument deduction. */
29228 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29230 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29231 /* Nothing we can do with this, even in deduction context. */
29234 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29235 with either a new invented type template parameter U or, if the
29236 initializer is a braced-init-list (8.5.4), with
29237 std::initializer_list<U>. */
29238 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29240 if (!DIRECT_LIST_INIT_P (init
))
29241 type
= listify_autos (type
, auto_node
);
29242 else if (CONSTRUCTOR_NELTS (init
) == 1)
29243 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29246 if (complain
& tf_warning_or_error
)
29248 if (permerror (input_location
, "direct-list-initialization of "
29249 "%<auto%> requires exactly one element"))
29250 inform (input_location
,
29251 "for deduction to %<std::initializer_list%>, use copy-"
29252 "list-initialization (i.e. add %<=%> before the %<{%>)");
29254 type
= listify_autos (type
, auto_node
);
29258 if (type
== error_mark_node
)
29259 return error_mark_node
;
29261 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29262 /* We don't recurse here because we can't deduce from a nested
29263 initializer_list. */
29264 for (constructor_elt
&elt
: *CONSTRUCTOR_ELTS (init
))
29265 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
29267 init
= resolve_nondeduced_context (init
, complain
);
29269 if (context
== adc_decomp_type
29270 && auto_node
== type
29271 && init
!= error_mark_node
29272 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29273 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29274 and initializer has array type, deduce cv-qualified array type. */
29275 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29277 else if (AUTO_IS_DECLTYPE (auto_node
))
29279 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29280 if (REFERENCE_REF_P (stripped_init
))
29281 stripped_init
= TREE_OPERAND (stripped_init
, 0);
29282 bool id
= (DECL_P (stripped_init
)
29283 || ((TREE_CODE (init
) == COMPONENT_REF
29284 || TREE_CODE (init
) == SCOPE_REF
)
29285 && !REF_PARENTHESIZED_P (init
)));
29286 targs
= make_tree_vec (1);
29287 TREE_VEC_ELT (targs
, 0)
29288 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29289 if (type
!= auto_node
)
29291 if (complain
& tf_error
)
29292 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29293 return error_mark_node
;
29295 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29297 if (complain
& tf_error
)
29298 error ("%<decltype(auto)%> cannot be cv-qualified");
29299 return error_mark_node
;
29304 if (error_operand_p (init
))
29305 return error_mark_node
;
29307 tree parms
= build_tree_list (NULL_TREE
, type
);
29311 tparms
= extract_autos (type
);
29314 tparms
= make_tree_vec (1);
29315 TREE_VEC_ELT (tparms
, 0)
29316 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29319 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29320 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29322 NULL
, /*explain_p=*/false);
29325 if (processing_template_decl
)
29326 /* Try again at instantiation time. */
29328 if (type
&& type
!= error_mark_node
29329 && (complain
& tf_error
))
29330 /* If type is error_mark_node a diagnostic must have been
29331 emitted by now. Also, having a mention to '<type error>'
29332 in the diagnostic is not really useful to the user. */
29335 && FNDECL_USED_AUTO (current_function_decl
)
29337 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29338 && LAMBDA_FUNCTION_P (current_function_decl
))
29339 error ("unable to deduce lambda return type from %qE", init
);
29341 error ("unable to deduce %qT from %qE", type
, init
);
29342 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29344 NULL
, /*explain_p=*/true);
29346 return error_mark_node
;
29350 /* Check any placeholder constraints against the deduced type. */
29351 if (flag_concepts
&& !processing_template_decl
)
29352 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29354 /* Use the deduced type to check the associated constraints. If we
29355 have a partial-concept-id, rebuild the argument list so that
29356 we check using the extra arguments. */
29357 check
= unpack_concept_check (check
);
29358 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29359 tree
cdecl = TREE_OPERAND (check
, 0);
29361 cdecl = OVL_FIRST (cdecl);
29362 tree cargs
= TREE_OPERAND (check
, 1);
29363 if (TREE_VEC_LENGTH (cargs
) > 1)
29365 cargs
= copy_node (cargs
);
29366 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29371 /* Rebuild the check using the deduced arguments. */
29372 check
= build_concept_check (cdecl, cargs
, tf_none
);
29374 if (!constraints_satisfied_p (check
))
29376 if (complain
& tf_warning_or_error
)
29378 auto_diagnostic_group d
;
29381 case adc_unspecified
:
29383 error("placeholder constraints not satisfied");
29385 case adc_variable_type
:
29386 case adc_decomp_type
:
29387 error ("deduced initializer does not satisfy "
29388 "placeholder constraints");
29390 case adc_return_type
:
29391 error ("deduced return type does not satisfy "
29392 "placeholder constraints");
29394 case adc_requirement
:
29395 error ("deduced expression type does not satisfy "
29396 "placeholder constraints");
29399 diagnose_constraints (input_location
, check
, targs
);
29401 return error_mark_node
;
29405 if (processing_template_decl
&& context
!= adc_unify
)
29406 outer_targs
= current_template_args ();
29407 targs
= add_to_template_args (outer_targs
, targs
);
29408 return tsubst (type
, targs
, complain
, NULL_TREE
);
29411 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29415 splice_late_return_type (tree type
, tree late_return_type
)
29417 if (late_return_type
)
29419 gcc_assert (is_auto (type
) || seen_error ());
29420 return late_return_type
;
29423 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29425 tree idx
= get_template_parm_index (*auto_node
);
29426 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29428 /* In an abbreviated function template we didn't know we were dealing
29429 with a function template when we saw the auto return type, so update
29430 it to have the correct level. */
29431 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29432 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29433 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29434 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29435 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29436 *auto_node
= new_auto
;
29442 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29443 'decltype(auto)' or a deduced class template. */
29446 is_auto (const_tree type
)
29448 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29449 && (TYPE_IDENTIFIER (type
) == auto_identifier
29450 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29456 /* for_each_template_parm callback for type_uses_auto. */
29459 is_auto_r (tree tp
, void */
*data*/
)
29461 return is_auto (tp
);
29464 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29465 a use of `auto'. Returns NULL_TREE otherwise. */
29468 type_uses_auto (tree type
)
29470 if (type
== NULL_TREE
)
29472 else if (flag_concepts
)
29474 /* The Concepts TS allows multiple autos in one type-specifier; just
29475 return the first one we find, do_auto_deduction will collect all of
29477 if (uses_template_parms (type
))
29478 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29479 /*visited*/NULL
, /*nondeduced*/false);
29483 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29489 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29490 concepts are enabled, auto is acceptable in template arguments, but
29491 only when TEMPL identifies a template class. Return TRUE if any
29492 such errors were reported. */
29495 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29497 /* If there were previous errors, nevermind. */
29498 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29501 /* If TMPL is an identifier, we're parsing and we can't tell yet
29502 whether TMPL is supposed to be a type, a function or a variable.
29503 We'll only be able to tell during template substitution, so we
29504 expect to be called again then. If concepts are enabled and we
29505 know we have a type, we're ok. */
29507 && (identifier_p (tmpl
)
29509 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29510 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29513 /* Quickly search for any occurrences of auto; usually there won't
29514 be any, and then we'll avoid allocating the vector. */
29515 if (!type_uses_auto (args
))
29518 bool errors
= false;
29520 tree vec
= extract_autos (args
);
29521 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29523 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29524 error_at (DECL_SOURCE_LOCATION (xauto
),
29525 "invalid use of %qT in template argument", xauto
);
29532 /* Recursively walk over && expressions searching for EXPR. Return a reference
29533 to that expression. */
29535 static tree
*find_template_requirement (tree
*t
, tree key
)
29539 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29541 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29543 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29549 /* Convert the generic type parameters in PARM that match the types given in the
29550 range [START_IDX, END_IDX) from the current_template_parms into generic type
29554 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29556 tree current
= current_template_parms
;
29557 int depth
= TMPL_PARMS_DEPTH (current
);
29558 current
= INNERMOST_TEMPLATE_PARMS (current
);
29559 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29561 for (int i
= 0; i
< start_idx
; ++i
)
29562 TREE_VEC_ELT (replacement
, i
)
29563 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29565 for (int i
= start_idx
; i
< end_idx
; ++i
)
29567 /* Create a distinct parameter pack type from the current parm and add it
29568 to the replacement args to tsubst below into the generic function
29570 tree node
= TREE_VEC_ELT (current
, i
);
29571 tree o
= TREE_TYPE (TREE_VALUE (node
));
29572 tree t
= copy_type (o
);
29573 TEMPLATE_TYPE_PARM_INDEX (t
)
29574 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29576 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29577 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29578 TYPE_MAIN_VARIANT (t
) = t
;
29579 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29580 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29581 TREE_VEC_ELT (replacement
, i
) = t
;
29583 /* Replace the current template parameter with new pack. */
29584 TREE_VALUE (node
) = TREE_CHAIN (t
);
29586 /* Surgically adjust the associated constraint of adjusted parameter
29587 and it's corresponding contribution to the current template
29589 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29591 tree id
= unpack_concept_check (constr
);
29592 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
29593 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29594 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29596 /* If there was a constraint, we also need to replace that in
29597 the template requirements, which we've already built. */
29598 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29599 reqs
= find_template_requirement (reqs
, constr
);
29604 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29605 TREE_VEC_ELT (replacement
, i
)
29606 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29608 /* If there are more levels then build up the replacement with the outer
29611 replacement
= add_to_template_args (template_parms_to_args
29612 (TREE_CHAIN (current_template_parms
)),
29615 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29618 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29622 declare_integer_pack (void)
29624 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29625 build_function_type_list (integer_type_node
,
29628 NULL_TREE
, ECF_CONST
);
29629 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29630 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29631 CP_BUILT_IN_INTEGER_PACK
);
29634 /* Set up the hash tables for template instantiations. */
29637 init_template_processing (void)
29639 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29640 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29642 if (cxx_dialect
>= cxx11
)
29643 declare_integer_pack ();
29646 /* Print stats about the template hash tables for -fstats. */
29649 print_template_statistics (void)
29651 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29652 "%f collisions\n", (long) decl_specializations
->size (),
29653 (long) decl_specializations
->elements (),
29654 decl_specializations
->collisions ());
29655 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29656 "%f collisions\n", (long) type_specializations
->size (),
29657 (long) type_specializations
->elements (),
29658 type_specializations
->collisions ());
29663 namespace selftest
{
29665 /* Verify that build_non_dependent_expr () works, for various expressions,
29666 and that location wrappers don't affect the results. */
29669 test_build_non_dependent_expr ()
29671 location_t loc
= BUILTINS_LOCATION
;
29673 /* Verify constants, without and with location wrappers. */
29674 tree int_cst
= build_int_cst (integer_type_node
, 42);
29675 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29677 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29678 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29679 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29681 tree string_lit
= build_string (4, "foo");
29682 TREE_TYPE (string_lit
) = char_array_type_node
;
29683 string_lit
= fix_string_type (string_lit
);
29684 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29686 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29687 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29688 ASSERT_EQ (wrapped_string_lit
,
29689 build_non_dependent_expr (wrapped_string_lit
));
29692 /* Verify that type_dependent_expression_p () works correctly, even
29693 in the presence of location wrapper nodes. */
29696 test_type_dependent_expression_p ()
29698 location_t loc
= BUILTINS_LOCATION
;
29700 tree name
= get_identifier ("foo");
29702 /* If no templates are involved, nothing is type-dependent. */
29703 gcc_assert (!processing_template_decl
);
29704 ASSERT_FALSE (type_dependent_expression_p (name
));
29706 ++processing_template_decl
;
29708 /* Within a template, an unresolved name is always type-dependent. */
29709 ASSERT_TRUE (type_dependent_expression_p (name
));
29711 /* Ensure it copes with NULL_TREE and errors. */
29712 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29713 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29715 /* A USING_DECL in a template should be type-dependent, even if wrapped
29716 with a location wrapper (PR c++/83799). */
29717 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29718 TREE_TYPE (using_decl
) = integer_type_node
;
29719 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29720 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29721 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29722 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29724 --processing_template_decl
;
29727 /* Run all of the selftests within this file. */
29732 test_build_non_dependent_expr ();
29733 test_type_dependent_expression_p ();
29736 } // namespace selftest
29738 #endif /* #if CHECKING_P */
29740 #include "gt-cp-pt.h"