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 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14937 top-level qualifiers as required. */
14938 type
= cv_unqualified (type_decays_to (type
));
14940 /* We do not substitute into default arguments here. The standard
14941 mandates that they be instantiated only when needed, which is
14942 done in build_over_call. */
14943 default_arg
= TREE_PURPOSE (arg_types
);
14945 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14946 since the new op() won't have any associated template arguments for us
14947 to refer to later. */
14948 if (lambda_fn_in_template_p (in_decl
))
14949 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14950 false/*fn*/, false/*constexpr*/);
14952 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14954 /* We've instantiated a template before its default arguments
14955 have been parsed. This can happen for a nested template
14956 class, and is not an error unless we require the default
14957 argument in a call of this function. */
14958 remaining_arg_types
=
14959 tree_cons (default_arg
, type
, remaining_arg_types
);
14960 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14961 remaining_arg_types
);
14964 remaining_arg_types
=
14965 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14968 return remaining_arg_types
;
14971 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14972 *not* handle the exception-specification for FNTYPE, because the
14973 initial substitution of explicitly provided template parameters
14974 during argument deduction forbids substitution into the
14975 exception-specification:
14979 All references in the function type of the function template to the
14980 corresponding template parameters are replaced by the specified tem-
14981 plate argument values. If a substitution in a template parameter or
14982 in the function type of the function template results in an invalid
14983 type, type deduction fails. [Note: The equivalent substitution in
14984 exception specifications is done only when the function is instanti-
14985 ated, at which point a program is ill-formed if the substitution
14986 results in an invalid type.] */
14989 tsubst_function_type (tree t
,
14991 tsubst_flags_t complain
,
14995 tree arg_types
= NULL_TREE
;
14997 /* The TYPE_CONTEXT is not used for function/method types. */
14998 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15000 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15002 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15004 if (late_return_type_p
)
15006 /* Substitute the argument types. */
15007 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15008 complain
, in_decl
);
15009 if (arg_types
== error_mark_node
)
15010 return error_mark_node
;
15012 tree save_ccp
= current_class_ptr
;
15013 tree save_ccr
= current_class_ref
;
15014 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15015 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15016 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15019 /* DR 1207: 'this' is in scope in the trailing return type. */
15020 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15023 /* Substitute the return type. */
15024 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15028 current_class_ptr
= save_ccp
;
15029 current_class_ref
= save_ccr
;
15033 /* Substitute the return type. */
15034 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15036 if (return_type
== error_mark_node
)
15037 return error_mark_node
;
15038 /* DR 486 clarifies that creation of a function type with an
15039 invalid return type is a deduction failure. */
15040 if (TREE_CODE (return_type
) == ARRAY_TYPE
15041 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15043 if (complain
& tf_error
)
15045 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15046 error ("function returning an array");
15048 error ("function returning a function");
15050 return error_mark_node
;
15053 if (!late_return_type_p
)
15055 /* Substitute the argument types. */
15056 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15057 complain
, in_decl
);
15058 if (arg_types
== error_mark_node
)
15059 return error_mark_node
;
15062 /* Construct a new type node and return it. */
15063 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15064 /*raises=*/NULL_TREE
, complain
);
15067 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15068 ARGS into that specification, and return the substituted
15069 specification. If there is no specification, return NULL_TREE. */
15072 tsubst_exception_specification (tree fntype
,
15074 tsubst_flags_t complain
,
15081 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15082 new_specs
= NULL_TREE
;
15083 if (specs
&& TREE_PURPOSE (specs
))
15085 /* A noexcept-specifier. */
15086 tree expr
= TREE_PURPOSE (specs
);
15087 if (TREE_CODE (expr
) == INTEGER_CST
)
15091 /* Defer instantiation of noexcept-specifiers to avoid
15092 excessive instantiations (c++/49107). */
15093 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15094 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15096 /* We already partially instantiated this member template,
15097 so combine the new args with the old. */
15098 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15099 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15100 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15101 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15105 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15106 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15111 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15113 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15115 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15117 new_specs
= tsubst_copy_and_build
15118 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15119 /*integral_constant_expression_p=*/true);
15121 new_specs
= build_noexcept_spec (new_specs
, complain
);
15125 if (! TREE_VALUE (specs
))
15132 tree expanded_specs
= NULL_TREE
;
15134 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15136 /* Expand the pack expansion type. */
15137 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15141 if (expanded_specs
== error_mark_node
)
15142 return error_mark_node
;
15143 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15144 len
= TREE_VEC_LENGTH (expanded_specs
);
15147 /* We're substituting into a member template, so
15148 we got a TYPE_PACK_EXPANSION back. Add that
15149 expansion and move on. */
15150 gcc_assert (TREE_CODE (expanded_specs
)
15151 == TYPE_PACK_EXPANSION
);
15152 new_specs
= add_exception_specifier (new_specs
,
15155 specs
= TREE_CHAIN (specs
);
15160 for (i
= 0; i
< len
; ++i
)
15162 if (expanded_specs
)
15163 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15165 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15166 if (spec
== error_mark_node
)
15168 new_specs
= add_exception_specifier (new_specs
, spec
,
15172 specs
= TREE_CHAIN (specs
);
15178 /* Substitute through a TREE_LIST of types or expressions, handling pack
15182 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15184 if (t
== void_list_node
)
15187 tree purpose
= TREE_PURPOSE (t
);
15188 tree purposevec
= NULL_TREE
;
15191 else if (PACK_EXPANSION_P (purpose
))
15193 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15194 if (TREE_CODE (purpose
) == TREE_VEC
)
15195 purposevec
= purpose
;
15197 else if (TYPE_P (purpose
))
15198 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15200 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15201 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15202 return error_mark_node
;
15204 tree value
= TREE_VALUE (t
);
15205 tree valuevec
= NULL_TREE
;
15208 else if (PACK_EXPANSION_P (value
))
15210 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15211 if (TREE_CODE (value
) == TREE_VEC
)
15214 else if (TYPE_P (value
))
15215 value
= tsubst (value
, args
, complain
, in_decl
);
15217 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15218 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15219 return error_mark_node
;
15221 tree chain
= TREE_CHAIN (t
);
15224 else if (TREE_CODE (chain
) == TREE_LIST
)
15225 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15226 else if (TYPE_P (chain
))
15227 chain
= tsubst (chain
, args
, complain
, in_decl
);
15229 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15230 if (chain
== error_mark_node
)
15231 return error_mark_node
;
15233 if (purpose
== TREE_PURPOSE (t
)
15234 && value
== TREE_VALUE (t
)
15235 && chain
== TREE_CHAIN (t
))
15239 /* Determine the number of arguments. */
15242 len
= TREE_VEC_LENGTH (purposevec
);
15243 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15246 len
= TREE_VEC_LENGTH (valuevec
);
15250 for (int i
= len
; i
-- > 0; )
15253 purpose
= TREE_VEC_ELT (purposevec
, i
);
15255 value
= TREE_VEC_ELT (valuevec
, i
);
15257 if (value
&& TYPE_P (value
))
15258 chain
= hash_tree_cons (purpose
, value
, chain
);
15260 chain
= tree_cons (purpose
, value
, chain
);
15266 /* Take the tree structure T and replace template parameters used
15267 therein with the argument vector ARGS. IN_DECL is an associated
15268 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15269 Issue error and warning messages under control of COMPLAIN. Note
15270 that we must be relatively non-tolerant of extensions here, in
15271 order to preserve conformance; if we allow substitutions that
15272 should not be allowed, we may allow argument deductions that should
15273 not succeed, and therefore report ambiguous overload situations
15274 where there are none. In theory, we could allow the substitution,
15275 but indicate that it should have failed, and allow our caller to
15276 make sure that the right thing happens, but we don't try to do this
15279 This function is used for dealing with types, decls and the like;
15280 for expressions, use tsubst_expr or tsubst_copy. */
15283 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15285 enum tree_code code
;
15286 tree type
, r
= NULL_TREE
;
15288 if (t
== NULL_TREE
|| t
== error_mark_node
15289 || t
== integer_type_node
15290 || t
== void_type_node
15291 || t
== char_type_node
15292 || t
== unknown_type_node
15293 || TREE_CODE (t
) == NAMESPACE_DECL
15294 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15298 return tsubst_decl (t
, args
, complain
);
15300 if (args
== NULL_TREE
)
15303 code
= TREE_CODE (t
);
15305 if (code
== IDENTIFIER_NODE
)
15306 type
= IDENTIFIER_TYPE_VALUE (t
);
15308 type
= TREE_TYPE (t
);
15310 gcc_assert (type
!= unknown_type_node
);
15312 /* Reuse typedefs. We need to do this to handle dependent attributes,
15313 such as attribute aligned. */
15315 && typedef_variant_p (t
))
15317 tree decl
= TYPE_NAME (t
);
15319 if (alias_template_specialization_p (t
, nt_opaque
))
15321 /* DECL represents an alias template and we want to
15323 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15324 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15325 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15327 else if (DECL_CLASS_SCOPE_P (decl
)
15328 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15329 && uses_template_parms (DECL_CONTEXT (decl
)))
15331 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15332 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15333 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15335 else if (DECL_FUNCTION_SCOPE_P (decl
)
15336 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15337 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15338 r
= retrieve_local_specialization (decl
);
15340 /* The typedef is from a non-template context. */
15346 r
= cp_build_qualified_type_real
15347 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15348 complain
| tf_ignore_bad_quals
);
15353 /* We don't have an instantiation yet, so drop the typedef. */
15354 int quals
= cp_type_quals (t
);
15355 t
= DECL_ORIGINAL_TYPE (decl
);
15356 t
= cp_build_qualified_type_real (t
, quals
,
15357 complain
| tf_ignore_bad_quals
);
15361 bool fndecl_type
= (complain
& tf_fndecl_type
);
15362 complain
&= ~tf_fndecl_type
;
15365 && code
!= TYPENAME_TYPE
15366 && code
!= TEMPLATE_TYPE_PARM
15367 && code
!= TEMPLATE_PARM_INDEX
15368 && code
!= IDENTIFIER_NODE
15369 && code
!= FUNCTION_TYPE
15370 && code
!= METHOD_TYPE
)
15371 type
= tsubst (type
, args
, complain
, in_decl
);
15372 if (type
== error_mark_node
)
15373 return error_mark_node
;
15379 case ENUMERAL_TYPE
:
15380 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15381 /*entering_scope=*/0);
15384 case IDENTIFIER_NODE
:
15395 if (t
== integer_type_node
)
15398 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15399 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15403 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15405 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15406 /*integral_constant_expression_p=*/false);
15408 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15410 if (TREE_CODE (max
) == NOP_EXPR
15411 && TREE_SIDE_EFFECTS (omax
)
15412 && !TREE_TYPE (max
))
15413 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15415 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15416 with TREE_SIDE_EFFECTS that indicates this is not an integral
15417 constant expression. */
15418 if (processing_template_decl
15419 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15421 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15422 TREE_SIDE_EFFECTS (max
) = 1;
15425 return compute_array_index_type (NULL_TREE
, max
, complain
);
15428 case TEMPLATE_TYPE_PARM
:
15429 case TEMPLATE_TEMPLATE_PARM
:
15430 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15431 case TEMPLATE_PARM_INDEX
:
15436 tree arg
= NULL_TREE
;
15440 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15441 template_parm_level_and_index (t
, &level
, &idx
);
15443 levels
= TMPL_ARGS_DEPTH (args
);
15444 if (level
<= levels
15445 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15447 arg
= TMPL_ARG (args
, level
, idx
);
15449 /* See through ARGUMENT_PACK_SELECT arguments. */
15450 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15451 arg
= argument_pack_select_arg (arg
);
15454 if (arg
== error_mark_node
)
15455 return error_mark_node
;
15456 else if (arg
!= NULL_TREE
)
15458 if (ARGUMENT_PACK_P (arg
))
15459 /* If ARG is an argument pack, we don't actually want to
15460 perform a substitution here, because substitutions
15461 for argument packs are only done
15462 element-by-element. We can get to this point when
15463 substituting the type of a non-type template
15464 parameter pack, when that type actually contains
15465 template parameter packs from an outer template, e.g.,
15467 template<typename... Types> struct A {
15468 template<Types... Values> struct B { };
15472 if (code
== TEMPLATE_TYPE_PARM
)
15476 /* When building concept checks for the purpose of
15477 deducing placeholders, we can end up with wildcards
15478 where types are expected. Adjust this to the deduced
15480 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15481 arg
= TREE_TYPE (TREE_TYPE (arg
));
15483 gcc_assert (TYPE_P (arg
));
15485 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15487 return cp_build_qualified_type_real
15488 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15490 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15492 /* We are processing a type constructed from a
15493 template template parameter. */
15494 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15495 args
, complain
, in_decl
);
15496 if (argvec
== error_mark_node
)
15497 return error_mark_node
;
15499 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15500 || TREE_CODE (arg
) == TEMPLATE_DECL
15501 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15503 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15504 /* Consider this code:
15506 template <template <class> class Template>
15508 template <class Arg> using Bind = Template<Arg>;
15511 template <template <class> class Template, class Arg>
15512 using Instantiate = Template<Arg>; //#0
15514 template <template <class> class Template,
15517 Instantiate<Internal<Template>::template Bind,
15520 When #1 is parsed, the
15521 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15522 parameter `Template' in #0 matches the
15523 UNBOUND_CLASS_TEMPLATE representing the argument
15524 `Internal<Template>::template Bind'; We then want
15525 to assemble the type `Bind<Argument>' that can't
15526 be fully created right now, because
15527 `Internal<Template>' not being complete, the Bind
15528 template cannot be looked up in that context. So
15529 we need to "store" `Bind<Argument>' for later
15530 when the context of Bind becomes complete. Let's
15531 store that in a TYPENAME_TYPE. */
15532 return make_typename_type (TYPE_CONTEXT (arg
),
15533 build_nt (TEMPLATE_ID_EXPR
,
15534 TYPE_IDENTIFIER (arg
),
15539 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15540 are resolving nested-types in the signature of a
15541 member function templates. Otherwise ARG is a
15542 TEMPLATE_DECL and is the real template to be
15544 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15545 arg
= TYPE_NAME (arg
);
15547 r
= lookup_template_class (arg
,
15549 DECL_CONTEXT (arg
),
15550 /*entering_scope=*/0,
15552 return cp_build_qualified_type_real
15553 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15555 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15558 /* TEMPLATE_PARM_INDEX. */
15559 return convert_from_reference (unshare_expr (arg
));
15563 /* This can happen during the attempted tsubst'ing in
15564 unify. This means that we don't yet have any information
15565 about the template parameter in question. */
15568 /* Early in template argument deduction substitution, we don't
15569 want to reduce the level of 'auto', or it will be confused
15570 with a normal template parm in subsequent deduction.
15571 Similarly, don't reduce the level of template parameters to
15572 avoid mismatches when deducing their types. */
15573 if (complain
& tf_partial
)
15576 /* If we get here, we must have been looking at a parm for a
15577 more deeply nested template. Make a new version of this
15578 template parameter, but with a lower level. */
15581 case TEMPLATE_TYPE_PARM
:
15582 case TEMPLATE_TEMPLATE_PARM
:
15583 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15584 if (cp_type_quals (t
))
15586 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15587 r
= cp_build_qualified_type_real
15588 (r
, cp_type_quals (t
),
15589 complain
| (code
== TEMPLATE_TYPE_PARM
15590 ? tf_ignore_bad_quals
: 0));
15592 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15593 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15594 && (r
= (TEMPLATE_PARM_DESCENDANTS
15595 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15596 && (r
= TREE_TYPE (r
))
15597 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15598 /* Break infinite recursion when substituting the constraints
15599 of a constrained placeholder. */;
15600 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15601 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15602 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15603 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15604 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15605 && (TEMPLATE_PARM_LEVEL (r
)
15606 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15607 /* Cache the simple case of lowering a type parameter. */
15612 TEMPLATE_TYPE_PARM_INDEX (r
)
15613 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15614 r
, levels
, args
, complain
);
15615 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15616 TYPE_MAIN_VARIANT (r
) = r
;
15617 TYPE_POINTER_TO (r
) = NULL_TREE
;
15618 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15620 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15622 /* Propagate constraints on placeholders since they are
15623 only instantiated during satisfaction. */
15624 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15625 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15626 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15628 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15629 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15633 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15634 /* We have reduced the level of the template
15635 template parameter, but not the levels of its
15636 template parameters, so canonical_type_parameter
15637 will not be able to find the canonical template
15638 template parameter for this level. Thus, we
15639 require structural equality checking to compare
15640 TEMPLATE_TEMPLATE_PARMs. */
15641 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15642 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15643 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15645 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15647 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15649 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15650 /* We might need to substitute into the types of non-type
15651 template parameters. */
15652 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15653 complain
, in_decl
);
15654 if (tmpl
== error_mark_node
)
15655 return error_mark_node
;
15656 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15657 complain
, in_decl
);
15658 if (argvec
== error_mark_node
)
15659 return error_mark_node
;
15661 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15662 = build_template_info (tmpl
, argvec
);
15667 case TEMPLATE_PARM_INDEX
:
15668 /* OK, now substitute the type of the non-type parameter. We
15669 couldn't do it earlier because it might be an auto parameter,
15670 and we wouldn't need to if we had an argument. */
15671 type
= tsubst (type
, args
, complain
, in_decl
);
15672 if (type
== error_mark_node
)
15673 return error_mark_node
;
15674 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15678 gcc_unreachable ();
15685 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15688 /* We should never be tsubsting a binfo. */
15689 gcc_unreachable ();
15692 /* A vector of template arguments. */
15693 gcc_assert (!type
);
15694 return tsubst_template_args (t
, args
, complain
, in_decl
);
15697 case REFERENCE_TYPE
:
15699 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15704 Type deduction may fail for any of the following
15707 -- Attempting to create a pointer to reference type.
15708 -- Attempting to create a reference to a reference type or
15709 a reference to void.
15711 Core issue 106 says that creating a reference to a reference
15712 during instantiation is no longer a cause for failure. We
15713 only enforce this check in strict C++98 mode. */
15714 if ((TYPE_REF_P (type
)
15715 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15716 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15718 static location_t last_loc
;
15720 /* We keep track of the last time we issued this error
15721 message to avoid spewing a ton of messages during a
15722 single bad template instantiation. */
15723 if (complain
& tf_error
15724 && last_loc
!= input_location
)
15726 if (VOID_TYPE_P (type
))
15727 error ("forming reference to void");
15728 else if (code
== POINTER_TYPE
)
15729 error ("forming pointer to reference type %qT", type
);
15731 error ("forming reference to reference type %qT", type
);
15732 last_loc
= input_location
;
15735 return error_mark_node
;
15737 else if (TREE_CODE (type
) == FUNCTION_TYPE
15738 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15739 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15741 if (complain
& tf_error
)
15743 if (code
== POINTER_TYPE
)
15744 error ("forming pointer to qualified function type %qT",
15747 error ("forming reference to qualified function type %qT",
15750 return error_mark_node
;
15752 else if (code
== POINTER_TYPE
)
15754 r
= build_pointer_type (type
);
15755 if (TREE_CODE (type
) == METHOD_TYPE
)
15756 r
= build_ptrmemfunc_type (r
);
15758 else if (TYPE_REF_P (type
))
15759 /* In C++0x, during template argument substitution, when there is an
15760 attempt to create a reference to a reference type, reference
15761 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15763 "If a template-argument for a template-parameter T names a type
15764 that is a reference to a type A, an attempt to create the type
15765 'lvalue reference to cv T' creates the type 'lvalue reference to
15766 A,' while an attempt to create the type type rvalue reference to
15767 cv T' creates the type T"
15769 r
= cp_build_reference_type
15771 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15773 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15774 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15776 if (r
!= error_mark_node
)
15777 /* Will this ever be needed for TYPE_..._TO values? */
15784 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15785 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15789 Type deduction may fail for any of the following
15792 -- Attempting to create "pointer to member of T" when T
15793 is not a class type. */
15794 if (complain
& tf_error
)
15795 error ("creating pointer to member of non-class type %qT", r
);
15796 return error_mark_node
;
15798 if (TYPE_REF_P (type
))
15800 if (complain
& tf_error
)
15801 error ("creating pointer to member reference type %qT", type
);
15802 return error_mark_node
;
15804 if (VOID_TYPE_P (type
))
15806 if (complain
& tf_error
)
15807 error ("creating pointer to member of type void");
15808 return error_mark_node
;
15810 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15811 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15813 /* The type of the implicit object parameter gets its
15814 cv-qualifiers from the FUNCTION_TYPE. */
15817 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15818 type_memfn_rqual (type
));
15819 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15820 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15824 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15828 case FUNCTION_TYPE
:
15833 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15834 if (fntype
== error_mark_node
)
15835 return error_mark_node
;
15837 /* Substitute the exception specification. */
15838 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15839 /*defer_ok*/fndecl_type
);
15840 if (specs
== error_mark_node
)
15841 return error_mark_node
;
15843 fntype
= build_exception_variant (fntype
, specs
);
15848 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15849 if (domain
== error_mark_node
)
15850 return error_mark_node
;
15852 /* As an optimization, we avoid regenerating the array type if
15853 it will obviously be the same as T. */
15854 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15857 /* These checks should match the ones in create_array_type_for_decl.
15861 The deduction may fail for any of the following reasons:
15863 -- Attempting to create an array with an element type that
15864 is void, a function type, or a reference type, or [DR337]
15865 an abstract class type. */
15866 if (VOID_TYPE_P (type
)
15867 || TREE_CODE (type
) == FUNCTION_TYPE
15868 || (TREE_CODE (type
) == ARRAY_TYPE
15869 && TYPE_DOMAIN (type
) == NULL_TREE
)
15870 || TYPE_REF_P (type
))
15872 if (complain
& tf_error
)
15873 error ("creating array of %qT", type
);
15874 return error_mark_node
;
15877 r
= build_cplus_array_type (type
, domain
);
15879 if (!valid_array_size_p (input_location
, r
, in_decl
,
15880 (complain
& tf_error
)))
15881 return error_mark_node
;
15883 if (TYPE_USER_ALIGN (t
))
15885 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15886 TYPE_USER_ALIGN (r
) = 1;
15892 case TYPENAME_TYPE
:
15894 tree ctx
= TYPE_CONTEXT (t
);
15895 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15897 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15898 if (ctx
== error_mark_node
15899 || TREE_VEC_LENGTH (ctx
) > 1)
15900 return error_mark_node
;
15901 if (TREE_VEC_LENGTH (ctx
) == 0)
15903 if (complain
& tf_error
)
15904 error ("%qD is instantiated for an empty pack",
15905 TYPENAME_TYPE_FULLNAME (t
));
15906 return error_mark_node
;
15908 ctx
= TREE_VEC_ELT (ctx
, 0);
15911 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15912 /*entering_scope=*/1);
15913 if (ctx
== error_mark_node
)
15914 return error_mark_node
;
15916 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15917 complain
, in_decl
);
15918 if (f
== error_mark_node
)
15919 return error_mark_node
;
15921 if (!MAYBE_CLASS_TYPE_P (ctx
))
15923 if (complain
& tf_error
)
15924 error ("%qT is not a class, struct, or union type", ctx
);
15925 return error_mark_node
;
15927 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15929 /* Normally, make_typename_type does not require that the CTX
15930 have complete type in order to allow things like:
15932 template <class T> struct S { typename S<T>::X Y; };
15934 But, such constructs have already been resolved by this
15935 point, so here CTX really should have complete type, unless
15936 it's a partial instantiation. */
15937 ctx
= complete_type (ctx
);
15938 if (!COMPLETE_TYPE_P (ctx
))
15940 if (complain
& tf_error
)
15941 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15942 return error_mark_node
;
15946 f
= make_typename_type (ctx
, f
, typename_type
,
15947 complain
| tf_keep_type_decl
);
15948 if (f
== error_mark_node
)
15950 if (TREE_CODE (f
) == TYPE_DECL
)
15952 complain
|= tf_ignore_bad_quals
;
15956 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15958 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15960 if (complain
& tf_error
)
15961 error ("%qT resolves to %qT, which is not an enumeration type",
15964 return error_mark_node
;
15966 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15968 if (complain
& tf_error
)
15969 error ("%qT resolves to %qT, which is not a class type",
15972 return error_mark_node
;
15976 return cp_build_qualified_type_real
15977 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15980 case UNBOUND_CLASS_TEMPLATE
:
15982 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15983 in_decl
, /*entering_scope=*/1);
15984 tree name
= TYPE_IDENTIFIER (t
);
15985 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15987 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15988 return error_mark_node
;
15991 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15992 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15999 ++cp_unevaluated_operand
;
16000 ++c_inhibit_evaluation_warnings
;
16002 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
16004 /*integral_constant_expression_p=*/false);
16006 --cp_unevaluated_operand
;
16007 --c_inhibit_evaluation_warnings
;
16009 type
= finish_typeof (type
);
16010 return cp_build_qualified_type_real (type
,
16012 | cp_type_quals (type
),
16016 case DECLTYPE_TYPE
:
16020 ++cp_unevaluated_operand
;
16021 ++c_inhibit_evaluation_warnings
;
16023 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16024 complain
|tf_decltype
, in_decl
,
16025 /*function_p*/false,
16026 /*integral_constant_expression*/false);
16028 --cp_unevaluated_operand
;
16029 --c_inhibit_evaluation_warnings
;
16031 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16032 type
= lambda_capture_field_type (type
,
16033 false /*explicit_init*/,
16034 DECLTYPE_FOR_REF_CAPTURE (t
));
16035 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16036 type
= lambda_proxy_type (type
);
16039 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16040 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16042 /* In a template ~id could be either a complement expression
16043 or an unqualified-id naming a destructor; if instantiating
16044 it produces an expression, it's not an id-expression or
16047 type
= finish_decltype_type (type
, id
, complain
);
16049 return cp_build_qualified_type_real (type
,
16051 | cp_type_quals (type
),
16052 complain
| tf_ignore_bad_quals
);
16055 case UNDERLYING_TYPE
:
16057 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16058 complain
, in_decl
);
16059 return finish_underlying_type (type
);
16062 case TYPE_ARGUMENT_PACK
:
16063 case NONTYPE_ARGUMENT_PACK
:
16064 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16079 /* We should use one of the expression tsubsts for these codes. */
16080 gcc_unreachable ();
16083 sorry ("use of %qs in template", get_tree_code_name (code
));
16084 return error_mark_node
;
16088 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16089 expression on the left-hand side of the "." or "->" operator. We
16090 only do the lookup if we had a dependent BASELINK. Otherwise we
16091 adjust it onto the instantiated heirarchy. */
16094 tsubst_baselink (tree baselink
, tree object_type
,
16095 tree args
, tsubst_flags_t complain
, tree in_decl
)
16097 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16098 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16099 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16101 tree optype
= BASELINK_OPTYPE (baselink
);
16102 optype
= tsubst (optype
, args
, complain
, in_decl
);
16104 tree template_args
= NULL_TREE
;
16105 bool template_id_p
= false;
16106 tree fns
= BASELINK_FUNCTIONS (baselink
);
16107 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16109 template_id_p
= true;
16110 template_args
= TREE_OPERAND (fns
, 1);
16111 fns
= TREE_OPERAND (fns
, 0);
16113 template_args
= tsubst_template_args (template_args
, args
,
16114 complain
, in_decl
);
16117 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16118 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16119 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16123 tree name
= OVL_NAME (fns
);
16124 if (IDENTIFIER_CONV_OP_P (name
))
16125 name
= make_conv_op_name (optype
);
16127 if (name
== complete_dtor_identifier
)
16128 /* Treat as-if non-dependent below. */
16129 dependent_p
= false;
16131 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16135 if ((complain
& tf_error
)
16136 && constructor_name_p (name
, qualifying_scope
))
16137 error ("cannot call constructor %<%T::%D%> directly",
16138 qualifying_scope
, name
);
16139 return error_mark_node
;
16142 if (BASELINK_P (baselink
))
16143 fns
= BASELINK_FUNCTIONS (baselink
);
16146 /* We're going to overwrite pieces below, make a duplicate. */
16147 baselink
= copy_node (baselink
);
16149 /* If lookup found a single function, mark it as used at this point.
16150 (If lookup found multiple functions the one selected later by
16151 overload resolution will be marked as used at that point.) */
16152 if (!template_id_p
&& !really_overloaded_fn (fns
))
16154 tree fn
= OVL_FIRST (fns
);
16155 bool ok
= mark_used (fn
, complain
);
16156 if (!ok
&& !(complain
& tf_error
))
16157 return error_mark_node
;
16158 if (ok
&& BASELINK_P (baselink
))
16159 /* We might have instantiated an auto function. */
16160 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16163 if (BASELINK_P (baselink
))
16165 /* Add back the template arguments, if present. */
16167 BASELINK_FUNCTIONS (baselink
)
16168 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16170 /* Update the conversion operator type. */
16171 BASELINK_OPTYPE (baselink
) = optype
;
16175 object_type
= current_class_type
;
16177 if (qualified_p
|| !dependent_p
)
16179 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16183 /* We need to call adjust_result_of_qualified_name_lookup in case the
16184 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16185 so that we still get virtual function binding. */
16186 BASELINK_QUALIFIED_P (baselink
) = false;
16192 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16193 true if the qualified-id will be a postfix-expression in-and-of
16194 itself; false if more of the postfix-expression follows the
16195 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16199 tsubst_qualified_id (tree qualified_id
, tree args
,
16200 tsubst_flags_t complain
, tree in_decl
,
16201 bool done
, bool address_p
)
16207 tree template_args
;
16208 location_t loc
= EXPR_LOCATION (qualified_id
);
16210 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16212 /* Figure out what name to look up. */
16213 name
= TREE_OPERAND (qualified_id
, 1);
16214 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16216 is_template
= true;
16217 template_args
= TREE_OPERAND (name
, 1);
16219 template_args
= tsubst_template_args (template_args
, args
,
16220 complain
, in_decl
);
16221 if (template_args
== error_mark_node
)
16222 return error_mark_node
;
16223 name
= TREE_OPERAND (name
, 0);
16227 is_template
= false;
16228 template_args
= NULL_TREE
;
16231 /* Substitute into the qualifying scope. When there are no ARGS, we
16232 are just trying to simplify a non-dependent expression. In that
16233 case the qualifying scope may be dependent, and, in any case,
16234 substituting will not help. */
16235 scope
= TREE_OPERAND (qualified_id
, 0);
16238 scope
= tsubst (scope
, args
, complain
, in_decl
);
16239 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16244 if (dependent_scope_p (scope
))
16247 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16248 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16249 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16250 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16254 if (!BASELINK_P (name
) && !DECL_P (expr
))
16256 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16258 /* A BIT_NOT_EXPR is used to represent a destructor. */
16259 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16261 error ("qualifying type %qT does not match destructor name ~%qT",
16262 scope
, TREE_OPERAND (expr
, 0));
16263 expr
= error_mark_node
;
16266 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16267 LOOK_want::NORMAL
, false);
16270 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16271 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16272 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16274 if (complain
& tf_error
)
16276 error ("dependent-name %qE is parsed as a non-type, but "
16277 "instantiation yields a type", qualified_id
);
16278 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16280 return error_mark_node
;
16286 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16288 return error_mark_node
;
16289 /* Remember that there was a reference to this entity. */
16290 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16291 return error_mark_node
;
16294 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16296 if (complain
& tf_error
)
16297 qualified_name_lookup_error (scope
,
16298 TREE_OPERAND (qualified_id
, 1),
16299 expr
, input_location
);
16300 return error_mark_node
;
16305 /* We may be repeating a check already done during parsing, but
16306 if it was well-formed and passed then, it will pass again
16307 now, and if it didn't, we wouldn't have got here. The case
16308 we want to catch is when we couldn't tell then, and can now,
16309 namely when templ prior to substitution was an
16311 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16312 return error_mark_node
;
16314 if (variable_template_p (expr
))
16315 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16318 expr
= lookup_template_function (expr
, template_args
);
16321 if (expr
== error_mark_node
&& complain
& tf_error
)
16322 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16323 expr
, input_location
);
16324 else if (TYPE_P (scope
))
16326 expr
= (adjust_result_of_qualified_name_lookup
16327 (expr
, scope
, current_nonlambda_class_type ()));
16328 expr
= (finish_qualified_id_expr
16329 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16330 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16331 /*template_arg_p=*/false, complain
));
16334 /* Expressions do not generally have reference type. */
16335 if (TREE_CODE (expr
) != SCOPE_REF
16336 /* However, if we're about to form a pointer-to-member, we just
16337 want the referenced member referenced. */
16338 && TREE_CODE (expr
) != OFFSET_REF
)
16339 expr
= convert_from_reference (expr
);
16341 if (REF_PARENTHESIZED_P (qualified_id
))
16342 expr
= force_paren_expr (expr
);
16344 expr
= maybe_wrap_with_location (expr
, loc
);
16349 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16350 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16354 tsubst_init (tree init
, tree decl
, tree args
,
16355 tsubst_flags_t complain
, tree in_decl
)
16360 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16362 tree type
= TREE_TYPE (decl
);
16364 if (!init
&& type
!= error_mark_node
)
16366 if (tree auto_node
= type_uses_auto (type
))
16368 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16370 if (complain
& tf_error
)
16371 error ("initializer for %q#D expands to an empty list "
16372 "of expressions", decl
);
16373 return error_mark_node
;
16376 else if (!dependent_type_p (type
))
16378 /* If we had an initializer but it
16379 instantiated to nothing,
16380 value-initialize the object. This will
16381 only occur when the initializer was a
16382 pack expansion where the parameter packs
16383 used in that expansion were of length
16385 init
= build_value_init (type
, complain
);
16386 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16387 init
= get_target_expr_sfinae (init
, complain
);
16388 if (TREE_CODE (init
) == TARGET_EXPR
)
16389 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16396 /* If T is a reference to a dependent member of the current instantiation C and
16397 we are trying to refer to that member in a partial instantiation of C,
16398 return a SCOPE_REF; otherwise, return NULL_TREE.
16400 This can happen when forming a C++17 deduction guide, as in PR96199. */
16403 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16406 if (cxx_dialect
< cxx17
)
16409 tree ctx
= context_for_name_lookup (t
);
16410 if (!CLASS_TYPE_P (ctx
))
16413 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16414 if (dependent_scope_p (ctx
))
16415 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16416 /*template_p=*/false);
16421 /* Like tsubst, but deals with expressions. This function just replaces
16422 template parms; to finish processing the resultant expression, use
16423 tsubst_copy_and_build or tsubst_expr. */
16426 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16428 enum tree_code code
;
16431 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16434 code
= TREE_CODE (t
);
16439 r
= retrieve_local_specialization (t
);
16441 if (r
== NULL_TREE
)
16443 /* We get here for a use of 'this' in an NSDMI. */
16444 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16445 return current_class_ptr
;
16447 /* This can happen for a parameter name used later in a function
16448 declaration (such as in a late-specified return type). Just
16449 make a dummy decl, since it's only used for its type. */
16450 gcc_assert (cp_unevaluated_operand
!= 0);
16451 r
= tsubst_decl (t
, args
, complain
);
16452 /* Give it the template pattern as its context; its true context
16453 hasn't been instantiated yet and this is good enough for
16455 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16458 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16459 r
= argument_pack_select_arg (r
);
16460 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16461 return error_mark_node
;
16469 if (DECL_TEMPLATE_PARM_P (t
))
16470 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16471 /* There is no need to substitute into namespace-scope
16473 if (DECL_NAMESPACE_SCOPE_P (t
))
16475 /* If ARGS is NULL, then T is known to be non-dependent. */
16476 if (args
== NULL_TREE
)
16477 return scalar_constant_value (t
);
16479 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16482 /* Unfortunately, we cannot just call lookup_name here.
16485 template <int I> int f() {
16487 struct S { void g() { E e = a; } };
16490 When we instantiate f<7>::S::g(), say, lookup_name is not
16491 clever enough to find f<7>::a. */
16493 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16494 /*entering_scope=*/0);
16496 for (v
= TYPE_VALUES (enum_type
);
16498 v
= TREE_CHAIN (v
))
16499 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16500 return TREE_VALUE (v
);
16502 /* We didn't find the name. That should never happen; if
16503 name-lookup found it during preliminary parsing, we
16504 should find it again here during instantiation. */
16505 gcc_unreachable ();
16510 if (DECL_CONTEXT (t
))
16514 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16515 /*entering_scope=*/1);
16516 if (ctx
!= DECL_CONTEXT (t
))
16518 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16521 if (complain
& tf_error
)
16522 error ("using invalid field %qD", t
);
16523 return error_mark_node
;
16532 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16535 case FUNCTION_DECL
:
16536 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16537 r
= tsubst (t
, args
, complain
, in_decl
);
16538 else if (DECL_LOCAL_DECL_P (t
))
16540 /* Local specialization will have been created when we
16541 instantiated the DECL_EXPR_DECL. */
16542 r
= retrieve_local_specialization (t
);
16544 r
= error_mark_node
;
16546 else if (local_variable_p (t
)
16547 && uses_template_parms (DECL_CONTEXT (t
)))
16549 r
= retrieve_local_specialization (t
);
16550 if (r
== NULL_TREE
)
16552 /* First try name lookup to find the instantiation. */
16553 r
= lookup_name (DECL_NAME (t
));
16558 /* During error-recovery we may find a non-variable,
16559 even an OVERLOAD: just bail out and avoid ICEs and
16560 duplicate diagnostics (c++/62207). */
16561 gcc_assert (seen_error ());
16562 return error_mark_node
;
16564 if (!is_capture_proxy (r
))
16566 /* Make sure the one we found is the one we want. */
16567 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16568 if (ctx
!= DECL_CONTEXT (r
))
16577 /* This can happen for a variable used in a
16578 late-specified return type of a local lambda, or for a
16579 local static or constant. Building a new VAR_DECL
16580 should be OK in all those cases. */
16581 r
= tsubst_decl (t
, args
, complain
);
16582 if (local_specializations
)
16583 /* Avoid infinite recursion (79640). */
16584 register_local_specialization (r
, t
);
16585 if (decl_maybe_constant_var_p (r
))
16587 /* We can't call cp_finish_decl, so handle the
16588 initializer by hand. */
16589 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16590 complain
, in_decl
);
16591 if (!processing_template_decl
)
16592 init
= maybe_constant_init (init
);
16593 if (processing_template_decl
16594 ? potential_constant_expression (init
)
16595 : reduced_constant_expression_p (init
))
16596 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16597 = TREE_CONSTANT (r
) = true;
16598 DECL_INITIAL (r
) = init
;
16599 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16601 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16602 complain
, adc_variable_type
);
16604 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16605 || decl_constant_var_p (r
)
16607 if (!processing_template_decl
16608 && !TREE_STATIC (r
))
16609 r
= process_outer_var_ref (r
, complain
);
16611 /* Remember this for subsequent uses. */
16612 if (local_specializations
)
16613 register_local_specialization (r
, t
);
16615 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16616 r
= argument_pack_select_arg (r
);
16620 if (!mark_used (r
, complain
))
16621 return error_mark_node
;
16624 case NAMESPACE_DECL
:
16631 return tsubst_baselink (t
, current_nonlambda_class_type (),
16632 args
, complain
, in_decl
);
16634 case TEMPLATE_DECL
:
16635 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16636 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16637 args
, complain
, in_decl
);
16638 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16639 return tsubst (t
, args
, complain
, in_decl
);
16640 else if (DECL_CLASS_SCOPE_P (t
)
16641 && uses_template_parms (DECL_CONTEXT (t
)))
16643 /* Template template argument like the following example need
16646 template <template <class> class TT> struct C {};
16647 template <class T> struct D {
16648 template <class U> struct E {};
16653 We are processing the template argument `E' in #1 for
16654 the template instantiation #2. Originally, `E' is a
16655 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16656 have to substitute this with one having context `D<int>'. */
16658 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16659 if (dependent_scope_p (context
))
16661 /* When rewriting a constructor into a deduction guide, a
16662 non-dependent name can become dependent, so memtmpl<args>
16663 becomes context::template memtmpl<args>. */
16664 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16665 return build_qualified_name (type
, context
, DECL_NAME (t
),
16668 return lookup_field (context
, DECL_NAME(t
), 0, false);
16671 /* Ordinary template template argument. */
16674 case NON_LVALUE_EXPR
:
16675 case VIEW_CONVERT_EXPR
:
16677 /* Handle location wrappers by substituting the wrapped node
16678 first, *then* reusing the resulting type. Doing the type
16679 first ensures that we handle template parameters and
16680 parameter pack expansions. */
16681 if (location_wrapper_p (t
))
16683 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16684 complain
, in_decl
);
16685 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16687 tree op
= TREE_OPERAND (t
, 0);
16688 if (code
== VIEW_CONVERT_EXPR
16689 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16691 /* Wrapper to make a C++20 template parameter object const. */
16692 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16693 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16695 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16696 return build1 (code
, type
, op
);
16700 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16701 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16702 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16706 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16707 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16709 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16710 op
= build1 (code
, TREE_TYPE (op
), op
);
16711 REF_PARENTHESIZED_P (op
) = true;
16714 /* We shouldn't see any other uses of these in templates. */
16715 gcc_unreachable ();
16719 case REINTERPRET_CAST_EXPR
:
16720 case CONST_CAST_EXPR
:
16721 case STATIC_CAST_EXPR
:
16722 case DYNAMIC_CAST_EXPR
:
16723 case IMPLICIT_CONV_EXPR
:
16727 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16728 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16729 return build1 (code
, type
, op0
);
16733 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16734 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16736 tree expanded
, op
= TREE_OPERAND (t
, 0);
16739 if (SIZEOF_EXPR_TYPE_P (t
))
16740 op
= TREE_TYPE (op
);
16742 ++cp_unevaluated_operand
;
16743 ++c_inhibit_evaluation_warnings
;
16744 /* We only want to compute the number of arguments. */
16745 if (PACK_EXPANSION_P (op
))
16746 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16748 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16749 args
, complain
, in_decl
);
16750 --cp_unevaluated_operand
;
16751 --c_inhibit_evaluation_warnings
;
16753 if (TREE_CODE (expanded
) == TREE_VEC
)
16755 len
= TREE_VEC_LENGTH (expanded
);
16756 /* Set TREE_USED for the benefit of -Wunused. */
16757 for (int i
= 0; i
< len
; i
++)
16758 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16759 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16762 if (expanded
== error_mark_node
)
16763 return error_mark_node
;
16764 else if (PACK_EXPANSION_P (expanded
)
16765 || (TREE_CODE (expanded
) == TREE_VEC
16766 && pack_expansion_args_count (expanded
)))
16769 if (PACK_EXPANSION_P (expanded
))
16771 else if (TREE_VEC_LENGTH (expanded
) == 1)
16772 expanded
= TREE_VEC_ELT (expanded
, 0);
16774 expanded
= make_argument_pack (expanded
);
16776 if (TYPE_P (expanded
))
16777 return cxx_sizeof_or_alignof_type (input_location
,
16778 expanded
, SIZEOF_EXPR
,
16780 complain
& tf_error
);
16782 return cxx_sizeof_or_alignof_expr (input_location
,
16783 expanded
, SIZEOF_EXPR
,
16785 complain
& tf_error
);
16788 return build_int_cst (size_type_node
, len
);
16790 if (SIZEOF_EXPR_TYPE_P (t
))
16792 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16793 args
, complain
, in_decl
);
16794 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16795 r
= build1 (SIZEOF_EXPR
,
16796 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16797 SIZEOF_EXPR_TYPE_P (r
) = 1;
16804 case TRUTH_NOT_EXPR
:
16807 case UNARY_PLUS_EXPR
: /* Unary + */
16809 case AT_ENCODE_EXPR
:
16813 case REALPART_EXPR
:
16814 case IMAGPART_EXPR
:
16817 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16818 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16819 r
= build1 (code
, type
, op0
);
16820 if (code
== ALIGNOF_EXPR
)
16821 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16825 case COMPONENT_REF
:
16830 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16831 name
= TREE_OPERAND (t
, 1);
16832 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16834 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16835 complain
, in_decl
);
16836 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16838 else if (TREE_CODE (name
) == SCOPE_REF
16839 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16841 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16842 complain
, in_decl
);
16843 name
= TREE_OPERAND (name
, 1);
16844 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16845 complain
, in_decl
);
16846 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16847 name
= build_qualified_name (/*type=*/NULL_TREE
,
16849 /*template_p=*/false);
16851 else if (BASELINK_P (name
))
16852 name
= tsubst_baselink (name
,
16853 non_reference (TREE_TYPE (object
)),
16857 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16858 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16864 case TRUNC_DIV_EXPR
:
16865 case CEIL_DIV_EXPR
:
16866 case FLOOR_DIV_EXPR
:
16867 case ROUND_DIV_EXPR
:
16868 case EXACT_DIV_EXPR
:
16872 case TRUNC_MOD_EXPR
:
16873 case FLOOR_MOD_EXPR
:
16874 case TRUTH_ANDIF_EXPR
:
16875 case TRUTH_ORIF_EXPR
:
16876 case TRUTH_AND_EXPR
:
16877 case TRUTH_OR_EXPR
:
16888 case COMPOUND_EXPR
:
16891 case PREDECREMENT_EXPR
:
16892 case PREINCREMENT_EXPR
:
16893 case POSTDECREMENT_EXPR
:
16894 case POSTINCREMENT_EXPR
:
16896 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16897 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16898 return build_nt (code
, op0
, op1
);
16903 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16904 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16905 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16906 QUALIFIED_NAME_IS_TEMPLATE (t
));
16911 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16912 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16913 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16918 int n
= VL_EXP_OPERAND_LENGTH (t
);
16919 tree result
= build_vl_exp (CALL_EXPR
, n
);
16921 for (i
= 0; i
< n
; i
++)
16922 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16923 complain
, in_decl
);
16929 case PSEUDO_DTOR_EXPR
:
16930 case VEC_PERM_EXPR
:
16932 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16933 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16934 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16935 r
= build_nt (code
, op0
, op1
, op2
);
16936 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16942 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16943 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16944 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16945 r
= build_nt (code
, op0
, op1
, op2
);
16946 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16952 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16953 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16954 r
= build_nt (code
, op0
, op1
);
16955 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16956 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16960 case TEMPLATE_ID_EXPR
:
16962 /* Substituted template arguments */
16963 tree fn
= TREE_OPERAND (t
, 0);
16964 tree targs
= TREE_OPERAND (t
, 1);
16966 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16968 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16970 return lookup_template_function (fn
, targs
);
16975 tree purpose
, value
, chain
;
16977 if (t
== void_list_node
)
16980 purpose
= TREE_PURPOSE (t
);
16982 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16983 value
= TREE_VALUE (t
);
16985 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16986 chain
= TREE_CHAIN (t
);
16987 if (chain
&& chain
!= void_type_node
)
16988 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16989 if (purpose
== TREE_PURPOSE (t
)
16990 && value
== TREE_VALUE (t
)
16991 && chain
== TREE_CHAIN (t
))
16993 return tree_cons (purpose
, value
, chain
);
16998 case ENUMERAL_TYPE
:
17000 case TEMPLATE_TYPE_PARM
:
17001 case TEMPLATE_TEMPLATE_PARM
:
17002 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17003 case TEMPLATE_PARM_INDEX
:
17005 case REFERENCE_TYPE
:
17007 case FUNCTION_TYPE
:
17010 case TYPENAME_TYPE
:
17011 case UNBOUND_CLASS_TEMPLATE
:
17013 case DECLTYPE_TYPE
:
17015 return tsubst (t
, args
, complain
, in_decl
);
17019 /* Fall through. */
17020 case IDENTIFIER_NODE
:
17021 if (IDENTIFIER_CONV_OP_P (t
))
17023 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17024 return make_conv_op_name (new_type
);
17030 /* This is handled by tsubst_copy_and_build. */
17031 gcc_unreachable ();
17035 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17036 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17037 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17040 case CLEANUP_POINT_EXPR
:
17041 /* We shouldn't have built any of these during initial template
17042 generation. Instead, they should be built during instantiation
17043 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17044 gcc_unreachable ();
17048 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17049 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17050 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17051 r
= build2 (code
, type
, op0
, op1
);
17052 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17053 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17054 && !(complain
& tf_error
))
17055 return error_mark_node
;
17059 case EXPR_PACK_EXPANSION
:
17060 error ("invalid use of pack expansion expression");
17061 return error_mark_node
;
17063 case NONTYPE_ARGUMENT_PACK
:
17064 error ("use %<...%> to expand argument pack");
17065 return error_mark_node
;
17068 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17075 /* Instantiate any typedefs in the type. */
17076 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17077 r
= fold_convert (type
, t
);
17078 gcc_assert (TREE_CODE (r
) == code
);
17084 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17086 if (type
!= TREE_TYPE (t
))
17089 TREE_TYPE (r
) = type
;
17095 /* These can sometimes show up in a partial instantiation, but never
17096 involve template parms. */
17097 gcc_assert (!uses_template_parms (t
));
17100 case UNARY_LEFT_FOLD_EXPR
:
17101 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17102 case UNARY_RIGHT_FOLD_EXPR
:
17103 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17104 case BINARY_LEFT_FOLD_EXPR
:
17105 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17106 case BINARY_RIGHT_FOLD_EXPR
:
17107 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17111 case DEBUG_BEGIN_STMT
:
17112 /* ??? There's no point in copying it for now, but maybe some
17113 day it will contain more information, such as a pointer back
17114 to the containing function, inlined copy or so. */
17117 case CO_AWAIT_EXPR
:
17118 return tsubst_expr (t
, args
, complain
, in_decl
,
17119 /*integral_constant_expression_p=*/false);
17123 /* We shouldn't get here, but keep going if !flag_checking. */
17125 gcc_unreachable ();
17130 /* Helper function for tsubst_omp_clauses, used for instantiation of
17131 OMP_CLAUSE_DECL of clauses. */
17134 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17135 tree in_decl
, tree
*iterator_cache
)
17137 if (decl
== NULL_TREE
)
17140 /* Handle OpenMP iterators. */
17141 if (TREE_CODE (decl
) == TREE_LIST
17142 && TREE_PURPOSE (decl
)
17143 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17146 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17147 ret
= iterator_cache
[1];
17151 begin_scope (sk_omp
, NULL
);
17152 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17154 *tp
= copy_node (it
);
17155 TREE_VEC_ELT (*tp
, 0)
17156 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17157 TREE_VEC_ELT (*tp
, 1)
17158 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17159 /*integral_constant_expression_p=*/false);
17160 TREE_VEC_ELT (*tp
, 2)
17161 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17162 /*integral_constant_expression_p=*/false);
17163 TREE_VEC_ELT (*tp
, 3)
17164 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17165 /*integral_constant_expression_p=*/false);
17166 TREE_CHAIN (*tp
) = NULL_TREE
;
17167 tp
= &TREE_CHAIN (*tp
);
17169 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17170 iterator_cache
[0] = TREE_PURPOSE (decl
);
17171 iterator_cache
[1] = ret
;
17173 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17178 /* Handle an OpenMP array section represented as a TREE_LIST (or
17179 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17180 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17181 TREE_LIST. We can handle it exactly the same as an array section
17182 (purpose, value, and a chain), even though the nomenclature
17183 (low_bound, length, etc) is different. */
17184 if (TREE_CODE (decl
) == TREE_LIST
)
17187 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17188 /*integral_constant_expression_p=*/false);
17189 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17190 /*integral_constant_expression_p=*/false);
17191 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17193 if (TREE_PURPOSE (decl
) == low_bound
17194 && TREE_VALUE (decl
) == length
17195 && TREE_CHAIN (decl
) == chain
)
17197 tree ret
= tree_cons (low_bound
, length
, chain
);
17198 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17199 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17202 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17203 /*integral_constant_expression_p=*/false);
17204 /* Undo convert_from_reference tsubst_expr could have called. */
17206 && REFERENCE_REF_P (ret
)
17207 && !REFERENCE_REF_P (decl
))
17208 ret
= TREE_OPERAND (ret
, 0);
17212 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17215 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17216 tree args
, tsubst_flags_t complain
, tree in_decl
)
17218 tree new_clauses
= NULL_TREE
, nc
, oc
;
17219 tree linear_no_step
= NULL_TREE
;
17220 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17222 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17224 nc
= copy_node (oc
);
17225 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17228 switch (OMP_CLAUSE_CODE (nc
))
17230 case OMP_CLAUSE_LASTPRIVATE
:
17231 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17233 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17234 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17235 in_decl
, /*integral_constant_expression_p=*/false);
17236 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17237 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17240 case OMP_CLAUSE_PRIVATE
:
17241 case OMP_CLAUSE_SHARED
:
17242 case OMP_CLAUSE_FIRSTPRIVATE
:
17243 case OMP_CLAUSE_COPYIN
:
17244 case OMP_CLAUSE_COPYPRIVATE
:
17245 case OMP_CLAUSE_UNIFORM
:
17246 case OMP_CLAUSE_DEPEND
:
17247 case OMP_CLAUSE_FROM
:
17248 case OMP_CLAUSE_TO
:
17249 case OMP_CLAUSE_MAP
:
17250 case OMP_CLAUSE_NONTEMPORAL
:
17251 case OMP_CLAUSE_USE_DEVICE_PTR
:
17252 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17253 case OMP_CLAUSE_IS_DEVICE_PTR
:
17254 case OMP_CLAUSE_INCLUSIVE
:
17255 case OMP_CLAUSE_EXCLUSIVE
:
17256 OMP_CLAUSE_DECL (nc
)
17257 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17258 in_decl
, iterator_cache
);
17260 case OMP_CLAUSE_TILE
:
17261 case OMP_CLAUSE_IF
:
17262 case OMP_CLAUSE_NUM_THREADS
:
17263 case OMP_CLAUSE_SCHEDULE
:
17264 case OMP_CLAUSE_COLLAPSE
:
17265 case OMP_CLAUSE_FINAL
:
17266 case OMP_CLAUSE_DEVICE
:
17267 case OMP_CLAUSE_DIST_SCHEDULE
:
17268 case OMP_CLAUSE_NUM_TEAMS
:
17269 case OMP_CLAUSE_THREAD_LIMIT
:
17270 case OMP_CLAUSE_SAFELEN
:
17271 case OMP_CLAUSE_SIMDLEN
:
17272 case OMP_CLAUSE_NUM_TASKS
:
17273 case OMP_CLAUSE_GRAINSIZE
:
17274 case OMP_CLAUSE_PRIORITY
:
17275 case OMP_CLAUSE_ORDERED
:
17276 case OMP_CLAUSE_HINT
:
17277 case OMP_CLAUSE_NUM_GANGS
:
17278 case OMP_CLAUSE_NUM_WORKERS
:
17279 case OMP_CLAUSE_VECTOR_LENGTH
:
17280 case OMP_CLAUSE_WORKER
:
17281 case OMP_CLAUSE_VECTOR
:
17282 case OMP_CLAUSE_ASYNC
:
17283 case OMP_CLAUSE_WAIT
:
17284 OMP_CLAUSE_OPERAND (nc
, 0)
17285 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17286 in_decl
, /*integral_constant_expression_p=*/false);
17288 case OMP_CLAUSE_REDUCTION
:
17289 case OMP_CLAUSE_IN_REDUCTION
:
17290 case OMP_CLAUSE_TASK_REDUCTION
:
17291 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17293 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17294 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17296 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17297 complain
, in_decl
);
17298 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17299 = build_qualified_name (NULL_TREE
, scope
,
17300 TREE_OPERAND (placeholder
, 1),
17304 gcc_assert (identifier_p (placeholder
));
17306 OMP_CLAUSE_DECL (nc
)
17307 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17310 case OMP_CLAUSE_GANG
:
17311 case OMP_CLAUSE_ALIGNED
:
17312 case OMP_CLAUSE_ALLOCATE
:
17313 OMP_CLAUSE_DECL (nc
)
17314 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17316 OMP_CLAUSE_OPERAND (nc
, 1)
17317 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17318 in_decl
, /*integral_constant_expression_p=*/false);
17320 case OMP_CLAUSE_LINEAR
:
17321 OMP_CLAUSE_DECL (nc
)
17322 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17324 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17326 gcc_assert (!linear_no_step
);
17327 linear_no_step
= nc
;
17329 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17330 OMP_CLAUSE_LINEAR_STEP (nc
)
17331 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17332 complain
, in_decl
, NULL
);
17334 OMP_CLAUSE_LINEAR_STEP (nc
)
17335 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17337 /*integral_constant_expression_p=*/false);
17339 case OMP_CLAUSE_NOWAIT
:
17340 case OMP_CLAUSE_DEFAULT
:
17341 case OMP_CLAUSE_UNTIED
:
17342 case OMP_CLAUSE_MERGEABLE
:
17343 case OMP_CLAUSE_INBRANCH
:
17344 case OMP_CLAUSE_NOTINBRANCH
:
17345 case OMP_CLAUSE_PROC_BIND
:
17346 case OMP_CLAUSE_FOR
:
17347 case OMP_CLAUSE_PARALLEL
:
17348 case OMP_CLAUSE_SECTIONS
:
17349 case OMP_CLAUSE_TASKGROUP
:
17350 case OMP_CLAUSE_NOGROUP
:
17351 case OMP_CLAUSE_THREADS
:
17352 case OMP_CLAUSE_SIMD
:
17353 case OMP_CLAUSE_DEFAULTMAP
:
17354 case OMP_CLAUSE_ORDER
:
17355 case OMP_CLAUSE_BIND
:
17356 case OMP_CLAUSE_INDEPENDENT
:
17357 case OMP_CLAUSE_AUTO
:
17358 case OMP_CLAUSE_SEQ
:
17359 case OMP_CLAUSE_IF_PRESENT
:
17360 case OMP_CLAUSE_FINALIZE
:
17363 gcc_unreachable ();
17365 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17366 switch (OMP_CLAUSE_CODE (nc
))
17368 case OMP_CLAUSE_SHARED
:
17369 case OMP_CLAUSE_PRIVATE
:
17370 case OMP_CLAUSE_FIRSTPRIVATE
:
17371 case OMP_CLAUSE_LASTPRIVATE
:
17372 case OMP_CLAUSE_COPYPRIVATE
:
17373 case OMP_CLAUSE_LINEAR
:
17374 case OMP_CLAUSE_REDUCTION
:
17375 case OMP_CLAUSE_IN_REDUCTION
:
17376 case OMP_CLAUSE_TASK_REDUCTION
:
17377 case OMP_CLAUSE_USE_DEVICE_PTR
:
17378 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17379 case OMP_CLAUSE_IS_DEVICE_PTR
:
17380 case OMP_CLAUSE_INCLUSIVE
:
17381 case OMP_CLAUSE_EXCLUSIVE
:
17382 case OMP_CLAUSE_ALLOCATE
:
17383 /* tsubst_expr on SCOPE_REF results in returning
17384 finish_non_static_data_member result. Undo that here. */
17385 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17386 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17387 == IDENTIFIER_NODE
))
17389 tree t
= OMP_CLAUSE_DECL (nc
);
17392 switch (TREE_CODE (v
))
17394 case COMPONENT_REF
:
17398 case POINTER_PLUS_EXPR
:
17399 v
= TREE_OPERAND (v
, 0);
17402 if (DECL_CONTEXT (v
) == current_function_decl
17403 && DECL_ARTIFICIAL (v
)
17404 && DECL_NAME (v
) == this_identifier
)
17405 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17412 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17413 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17414 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17415 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17416 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17418 tree decl
= OMP_CLAUSE_DECL (nc
);
17421 retrofit_lang_decl (decl
);
17422 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17431 new_clauses
= nreverse (new_clauses
);
17432 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17434 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17435 if (linear_no_step
)
17436 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17437 if (nc
== linear_no_step
)
17439 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17443 return new_clauses
;
17446 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17449 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17452 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17454 tree purpose
, value
, chain
;
17459 if (TREE_CODE (t
) != TREE_LIST
)
17460 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17461 /*function_p=*/false,
17462 /*integral_constant_expression_p=*/false);
17464 if (t
== void_list_node
)
17467 purpose
= TREE_PURPOSE (t
);
17469 purpose
= RECUR (purpose
);
17470 value
= TREE_VALUE (t
);
17473 if (TREE_CODE (value
) != LABEL_DECL
)
17474 value
= RECUR (value
);
17477 value
= lookup_label (DECL_NAME (value
));
17478 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17479 TREE_USED (value
) = 1;
17482 chain
= TREE_CHAIN (t
);
17483 if (chain
&& chain
!= void_type_node
)
17484 chain
= RECUR (chain
);
17485 return tree_cons (purpose
, value
, chain
);
17489 /* Used to temporarily communicate the list of #pragma omp parallel
17490 clauses to #pragma omp for instantiation if they are combined
17493 static tree
*omp_parallel_combined_clauses
;
17495 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17496 tree
*, unsigned int *);
17498 /* Substitute one OMP_FOR iterator. */
17501 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17502 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17503 tree args
, tsubst_flags_t complain
, tree in_decl
,
17504 bool integral_constant_expression_p
)
17506 #define RECUR(NODE) \
17507 tsubst_expr ((NODE), args, complain, in_decl, \
17508 integral_constant_expression_p)
17509 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17512 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17513 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17515 decl
= TREE_OPERAND (init
, 0);
17516 init
= TREE_OPERAND (init
, 1);
17517 tree decl_expr
= NULL_TREE
;
17518 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17521 bool decomp
= false;
17522 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17524 tree v
= DECL_VALUE_EXPR (decl
);
17525 if (TREE_CODE (v
) == ARRAY_REF
17526 && VAR_P (TREE_OPERAND (v
, 0))
17527 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17529 tree decomp_first
= NULL_TREE
;
17530 unsigned decomp_cnt
= 0;
17531 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17532 maybe_push_decl (d
);
17533 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17534 in_decl
, &decomp_first
, &decomp_cnt
);
17536 if (d
== error_mark_node
)
17537 decl
= error_mark_node
;
17539 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17541 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17543 tree v
= build_nt (ARRAY_REF
, d
,
17544 size_int (decomp_cnt
- i
- 1),
17545 NULL_TREE
, NULL_TREE
);
17546 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17547 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17549 fit_decomposition_lang_decl (decomp_first
, d
);
17550 decomp_first
= DECL_CHAIN (decomp_first
);
17554 decl
= tsubst_decl (decl
, args
, complain
);
17556 maybe_push_decl (decl
);
17558 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17560 /* We need to jump through some hoops to handle declarations in the
17561 init-statement, since we might need to handle auto deduction,
17562 but we need to keep control of initialization. */
17564 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17565 decl
= tsubst_decl (decl
, args
, complain
);
17569 if (TREE_CODE (decl
) == SCOPE_REF
)
17571 decl
= RECUR (decl
);
17572 if (TREE_CODE (decl
) == COMPONENT_REF
)
17576 switch (TREE_CODE (v
))
17578 case COMPONENT_REF
:
17582 case POINTER_PLUS_EXPR
:
17583 v
= TREE_OPERAND (v
, 0);
17586 if (DECL_CONTEXT (v
) == current_function_decl
17587 && DECL_ARTIFICIAL (v
)
17588 && DECL_NAME (v
) == this_identifier
)
17590 decl
= TREE_OPERAND (decl
, 1);
17591 decl
= omp_privatize_field (decl
, false);
17601 decl
= RECUR (decl
);
17603 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17605 init
= copy_node (init
);
17606 TREE_VEC_ELT (init
, 0)
17607 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17608 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17609 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17612 init
= RECUR (init
);
17614 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17616 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17617 if (TREE_CODE (o
) == TREE_LIST
)
17618 TREE_VEC_ELT (orig_declv
, i
)
17619 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17620 RECUR (TREE_VALUE (o
)),
17623 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17628 tree this_pre_body
= NULL_TREE
;
17629 tree orig_init
= NULL_TREE
;
17630 tree orig_decl
= NULL_TREE
;
17631 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17632 orig_init
, cond
, incr
);
17635 if (orig_declv
== NULL_TREE
)
17636 orig_declv
= copy_node (declv
);
17637 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17640 else if (orig_declv
)
17641 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17644 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17645 if (!range_for
&& auto_node
&& init
)
17647 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17649 gcc_assert (!type_dependent_expression_p (decl
));
17651 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17655 /* Declare the variable, but don't let that initialize it. */
17656 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17657 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17659 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17664 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17665 if (COMPARISON_CLASS_P (cond
)
17666 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17668 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17669 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17670 TREE_VEC_ELT (rhs
, 0)
17671 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17672 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17673 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17674 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17678 cond
= RECUR (cond
);
17679 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17680 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17682 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17683 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17684 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17685 NOP_EXPR
, rhs
, complain
);
17688 incr
= RECUR (incr
);
17689 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17690 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17692 TREE_VEC_ELT (declv
, i
) = decl
;
17693 TREE_VEC_ELT (initv
, i
) = init
;
17694 TREE_VEC_ELT (condv
, i
) = cond
;
17695 TREE_VEC_ELT (incrv
, i
) = incr
;
17701 /* Declare and initialize the variable. */
17709 for (j
= ((omp_parallel_combined_clauses
== NULL
17710 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17712 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17714 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17715 && OMP_CLAUSE_DECL (*pc
) == decl
)
17717 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17718 && OMP_CLAUSE_DECL (*pc
) == decl
)
17722 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17724 *pc
= OMP_CLAUSE_CHAIN (c
);
17725 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17728 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17729 && OMP_CLAUSE_DECL (*pc
) == decl
)
17731 error ("iteration variable %qD should not be firstprivate",
17733 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17735 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17736 && OMP_CLAUSE_DECL (*pc
) == decl
)
17738 error ("iteration variable %qD should not be reduction",
17740 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17743 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17748 if (*pc
== NULL_TREE
)
17750 tree c
= build_omp_clause (input_location
,
17751 TREE_CODE (t
) == OMP_LOOP
17752 ? OMP_CLAUSE_LASTPRIVATE
17753 : OMP_CLAUSE_PRIVATE
);
17754 OMP_CLAUSE_DECL (c
) = decl
;
17755 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17758 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17763 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17764 if (COMPARISON_CLASS_P (cond
))
17766 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17767 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17768 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17771 cond
= RECUR (cond
);
17772 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17773 switch (TREE_CODE (incr
))
17775 case PREINCREMENT_EXPR
:
17776 case PREDECREMENT_EXPR
:
17777 case POSTINCREMENT_EXPR
:
17778 case POSTDECREMENT_EXPR
:
17779 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17780 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17783 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17784 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17786 tree rhs
= TREE_OPERAND (incr
, 1);
17787 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17788 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17789 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17790 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17791 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17795 incr
= RECUR (incr
);
17798 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17799 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17801 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17802 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17803 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17804 TREE_TYPE (decl
), lhs
,
17805 RECUR (TREE_OPERAND (incr
, 2))));
17807 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17808 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17809 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17811 tree rhs
= TREE_OPERAND (incr
, 2);
17812 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17813 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17814 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17815 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17816 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17820 incr
= RECUR (incr
);
17823 incr
= RECUR (incr
);
17827 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17828 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17829 TREE_VEC_ELT (declv
, i
) = decl
;
17830 TREE_VEC_ELT (initv
, i
) = init
;
17831 TREE_VEC_ELT (condv
, i
) = cond
;
17832 TREE_VEC_ELT (incrv
, i
) = incr
;
17837 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17838 of OMP_TARGET's body. */
17841 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17843 *walk_subtrees
= 0;
17844 switch (TREE_CODE (*tp
))
17849 case STATEMENT_LIST
:
17850 *walk_subtrees
= 1;
17858 /* Helper function for tsubst_expr. For decomposition declaration
17859 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17860 also the corresponding decls representing the identifiers
17861 of the decomposition declaration. Return DECL if successful
17862 or error_mark_node otherwise, set *FIRST to the first decl
17863 in the list chained through DECL_CHAIN and *CNT to the number
17867 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17868 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17871 tree decl2
, decl3
, prev
= decl
;
17873 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17874 for (decl2
= DECL_CHAIN (pattern_decl
);
17877 && DECL_DECOMPOSITION_P (decl2
)
17878 && DECL_NAME (decl2
);
17879 decl2
= DECL_CHAIN (decl2
))
17881 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17883 gcc_assert (errorcount
);
17884 return error_mark_node
;
17887 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17888 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17889 tree v
= DECL_VALUE_EXPR (decl2
);
17890 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17891 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17892 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17893 SET_DECL_VALUE_EXPR (decl2
, v
);
17894 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17896 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17899 gcc_assert (errorcount
);
17900 decl
= error_mark_node
;
17903 maybe_push_decl (decl3
);
17904 if (error_operand_p (decl3
))
17905 decl
= error_mark_node
;
17906 else if (decl
!= error_mark_node
17907 && DECL_CHAIN (decl3
) != prev
17910 gcc_assert (errorcount
);
17911 decl
= error_mark_node
;
17920 /* Return the proper local_specialization for init-capture pack DECL. */
17923 lookup_init_capture_pack (tree decl
)
17925 /* We handle normal pack captures by forwarding to the specialization of the
17926 captured parameter. We can't do that for pack init-captures; we need them
17927 to have their own local_specialization. We created the individual
17928 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17929 when we process the DECL_EXPR for the pack init-capture in the template.
17930 So, how do we find them? We don't know the capture proxy pack when
17931 building the individual resulting proxies, and we don't know the
17932 individual proxies when instantiating the pack. What we have in common is
17935 So...when we instantiate the FIELD_DECL, we stick the result in
17936 local_specializations. Then at the DECL_EXPR we look up that result, see
17937 how many elements it has, synthesize the names, and look them up. */
17939 tree cname
= DECL_NAME (decl
);
17940 tree val
= DECL_VALUE_EXPR (decl
);
17941 tree field
= TREE_OPERAND (val
, 1);
17942 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17943 tree fpack
= retrieve_local_specialization (field
);
17944 if (fpack
== error_mark_node
)
17945 return error_mark_node
;
17948 tree vec
= NULL_TREE
;
17949 tree r
= NULL_TREE
;
17950 if (TREE_CODE (fpack
) == TREE_VEC
)
17952 len
= TREE_VEC_LENGTH (fpack
);
17953 vec
= make_tree_vec (len
);
17954 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17955 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17957 for (int i
= 0; i
< len
; ++i
)
17959 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17960 tree elt
= lookup_name (ename
);
17962 TREE_VEC_ELT (vec
, i
) = elt
;
17969 /* Like tsubst_copy for expressions, etc. but also does semantic
17973 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17974 bool integral_constant_expression_p
)
17976 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17977 #define RECUR(NODE) \
17978 tsubst_expr ((NODE), args, complain, in_decl, \
17979 integral_constant_expression_p)
17985 if (t
== NULL_TREE
|| t
== error_mark_node
)
17988 loc
= input_location
;
17989 if (location_t eloc
= cp_expr_location (t
))
17990 input_location
= eloc
;
17991 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17992 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17994 switch (TREE_CODE (t
))
17996 case STATEMENT_LIST
:
17998 tree_stmt_iterator i
;
17999 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
18000 RECUR (tsi_stmt (i
));
18004 case CTOR_INITIALIZER
:
18005 finish_mem_initializers (tsubst_initializer_list
18006 (TREE_OPERAND (t
, 0), args
));
18010 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18013 case CO_RETURN_EXPR
:
18014 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18017 case CO_YIELD_EXPR
:
18018 stmt
= finish_co_yield_expr (input_location
,
18019 RECUR (TREE_OPERAND (t
, 0)));
18023 case CO_AWAIT_EXPR
:
18024 stmt
= finish_co_await_expr (input_location
,
18025 RECUR (TREE_OPERAND (t
, 0)));
18030 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18031 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18032 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18034 finish_expr_stmt (tmp
);
18038 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18043 tree decl
, pattern_decl
;
18046 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18047 if (TREE_CODE (decl
) == LABEL_DECL
)
18048 finish_label_decl (DECL_NAME (decl
));
18049 else if (TREE_CODE (decl
) == USING_DECL
)
18051 /* We cannot have a member-using decl here (until 'using
18052 enum T' is a thing). */
18053 gcc_checking_assert (!DECL_DEPENDENT_P (decl
));
18055 /* This must be a non-dependent using-decl, and we'll have
18056 used the names it found during template parsing. We do
18057 not want to do the lookup again, because we might not
18058 find the things we found then. (Again, using enum T
18059 might mean we have to do things here.) */
18060 tree scope
= USING_DECL_SCOPE (decl
);
18061 gcc_checking_assert (scope
18062 == tsubst (scope
, args
, complain
, in_decl
));
18064 else if (is_capture_proxy (decl
)
18065 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18067 /* We're in tsubst_lambda_expr, we've already inserted a new
18068 capture proxy, so look it up and register it. */
18070 if (!DECL_PACK_P (decl
))
18072 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18073 LOOK_want::HIDDEN_LAMBDA
);
18074 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18076 else if (is_normal_capture_proxy (decl
))
18078 inst
= (retrieve_local_specialization
18079 (DECL_CAPTURED_VARIABLE (decl
)));
18080 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18081 || DECL_PACK_P (inst
));
18084 inst
= lookup_init_capture_pack (decl
);
18086 register_local_specialization (inst
, decl
);
18089 else if (DECL_PRETTY_FUNCTION_P (decl
))
18090 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18092 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18093 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18094 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18095 /* Don't copy the old closure; we'll create a new one in
18096 tsubst_lambda_expr. */
18100 init
= DECL_INITIAL (decl
);
18101 decl
= tsubst (decl
, args
, complain
, in_decl
);
18102 if (decl
!= error_mark_node
)
18104 /* By marking the declaration as instantiated, we avoid
18105 trying to instantiate it. Since instantiate_decl can't
18106 handle local variables, and since we've already done
18107 all that needs to be done, that's the right thing to
18110 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18111 if (VAR_P (decl
) && !DECL_NAME (decl
)
18112 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18113 /* Anonymous aggregates are a special case. */
18114 finish_anon_union (decl
);
18115 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18117 DECL_CONTEXT (decl
) = current_function_decl
;
18118 if (DECL_NAME (decl
) == this_identifier
)
18120 tree lam
= DECL_CONTEXT (current_function_decl
);
18121 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18122 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18124 insert_capture_proxy (decl
);
18126 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18127 /* We already did a pushtag. */;
18128 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18129 && DECL_LOCAL_DECL_P (decl
))
18131 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18132 DECL_CONTEXT (decl
) = NULL_TREE
;
18133 decl
= pushdecl (decl
);
18134 if (TREE_CODE (decl
) == FUNCTION_DECL
18135 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18136 && cp_check_omp_declare_reduction (decl
))
18137 instantiate_body (pattern_decl
, args
, decl
, true);
18141 bool const_init
= false;
18142 unsigned int cnt
= 0;
18143 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18144 maybe_push_decl (decl
);
18147 && DECL_DECOMPOSITION_P (decl
)
18148 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18149 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18150 complain
, in_decl
, &first
,
18153 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18156 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18159 if (ndecl
!= error_mark_node
)
18160 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18162 /* In a non-template function, VLA type declarations are
18163 handled in grokdeclarator; for templates, handle them
18165 predeclare_vla (decl
);
18167 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
18169 if (ndecl
!= error_mark_node
)
18170 cp_finish_decomp (ndecl
, first
, cnt
);
18179 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18180 RECUR (FOR_INIT_STMT (t
));
18181 finish_init_stmt (stmt
);
18182 tmp
= RECUR (FOR_COND (t
));
18183 finish_for_cond (tmp
, stmt
, false, 0);
18184 tmp
= RECUR (FOR_EXPR (t
));
18185 finish_for_expr (tmp
, stmt
);
18187 bool prev
= note_iteration_stmt_body_start ();
18188 RECUR (FOR_BODY (t
));
18189 note_iteration_stmt_body_end (prev
);
18191 finish_for_stmt (stmt
);
18194 case RANGE_FOR_STMT
:
18196 /* Construct another range_for, if this is not a final
18197 substitution (for inside a generic lambda of a
18198 template). Otherwise convert to a regular for. */
18200 stmt
= (processing_template_decl
18201 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18202 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18203 RECUR (RANGE_FOR_INIT_STMT (t
));
18204 decl
= RANGE_FOR_DECL (t
);
18205 decl
= tsubst (decl
, args
, complain
, in_decl
);
18206 maybe_push_decl (decl
);
18207 expr
= RECUR (RANGE_FOR_EXPR (t
));
18209 tree decomp_first
= NULL_TREE
;
18210 unsigned decomp_cnt
= 0;
18211 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18212 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18214 &decomp_first
, &decomp_cnt
);
18216 if (processing_template_decl
)
18218 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18219 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18220 finish_range_for_decl (stmt
, decl
, expr
);
18221 if (decomp_first
&& decl
!= error_mark_node
)
18222 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18226 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18227 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18228 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18229 decomp_first
, decomp_cnt
,
18230 RANGE_FOR_IVDEP (t
), unroll
);
18233 bool prev
= note_iteration_stmt_body_start ();
18234 RECUR (RANGE_FOR_BODY (t
));
18235 note_iteration_stmt_body_end (prev
);
18236 finish_for_stmt (stmt
);
18241 stmt
= begin_while_stmt ();
18242 tmp
= RECUR (WHILE_COND (t
));
18243 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18245 bool prev
= note_iteration_stmt_body_start ();
18246 RECUR (WHILE_BODY (t
));
18247 note_iteration_stmt_body_end (prev
);
18249 finish_while_stmt (stmt
);
18253 stmt
= begin_do_stmt ();
18255 bool prev
= note_iteration_stmt_body_start ();
18256 RECUR (DO_BODY (t
));
18257 note_iteration_stmt_body_end (prev
);
18259 finish_do_body (stmt
);
18260 tmp
= RECUR (DO_COND (t
));
18261 finish_do_stmt (tmp
, stmt
, false, 0);
18265 stmt
= begin_if_stmt ();
18266 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18267 if (IF_STMT_CONSTEXPR_P (t
))
18268 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18269 tmp
= RECUR (IF_COND (t
));
18270 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18271 if (IF_STMT_CONSTEXPR_P (t
)
18272 && instantiation_dependent_expression_p (tmp
))
18274 /* We're partially instantiating a generic lambda, but the condition
18275 of the constexpr if is still dependent. Don't substitute into the
18276 branches now, just remember the template arguments. */
18277 do_poplevel (IF_SCOPE (stmt
));
18278 IF_COND (stmt
) = IF_COND (t
);
18279 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18280 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18281 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18285 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18286 /* Don't instantiate the THEN_CLAUSE. */;
18289 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18290 bool inhibit
= integer_zerop (folded
);
18292 ++c_inhibit_evaluation_warnings
;
18293 RECUR (THEN_CLAUSE (t
));
18295 --c_inhibit_evaluation_warnings
;
18297 finish_then_clause (stmt
);
18299 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18300 /* Don't instantiate the ELSE_CLAUSE. */;
18301 else if (ELSE_CLAUSE (t
))
18303 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18304 bool inhibit
= integer_nonzerop (folded
);
18305 begin_else_clause (stmt
);
18307 ++c_inhibit_evaluation_warnings
;
18308 RECUR (ELSE_CLAUSE (t
));
18310 --c_inhibit_evaluation_warnings
;
18311 finish_else_clause (stmt
);
18314 finish_if_stmt (stmt
);
18318 if (BIND_EXPR_BODY_BLOCK (t
))
18319 stmt
= begin_function_body ();
18321 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18322 ? BCS_TRY_BLOCK
: 0);
18324 RECUR (BIND_EXPR_BODY (t
));
18326 if (BIND_EXPR_BODY_BLOCK (t
))
18327 finish_function_body (stmt
);
18329 finish_compound_stmt (stmt
);
18333 finish_break_stmt ();
18336 case CONTINUE_STMT
:
18337 finish_continue_stmt ();
18341 stmt
= begin_switch_stmt ();
18342 tmp
= RECUR (SWITCH_STMT_COND (t
));
18343 finish_switch_cond (tmp
, stmt
);
18344 RECUR (SWITCH_STMT_BODY (t
));
18345 finish_switch_stmt (stmt
);
18348 case CASE_LABEL_EXPR
:
18350 tree decl
= CASE_LABEL (t
);
18351 tree low
= RECUR (CASE_LOW (t
));
18352 tree high
= RECUR (CASE_HIGH (t
));
18353 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18354 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18356 tree label
= CASE_LABEL (l
);
18357 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18358 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18359 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18366 tree decl
= LABEL_EXPR_LABEL (t
);
18369 label
= finish_label_stmt (DECL_NAME (decl
));
18370 if (TREE_CODE (label
) == LABEL_DECL
)
18371 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18372 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18373 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18378 tmp
= GOTO_DESTINATION (t
);
18379 if (TREE_CODE (tmp
) != LABEL_DECL
)
18380 /* Computed goto's must be tsubst'd into. On the other hand,
18381 non-computed gotos must not be; the identifier in question
18382 will have no binding. */
18385 tmp
= DECL_NAME (tmp
);
18386 finish_goto_stmt (tmp
);
18391 tree string
= RECUR (ASM_STRING (t
));
18392 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18393 complain
, in_decl
);
18394 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18395 complain
, in_decl
);
18396 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18397 complain
, in_decl
);
18398 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18399 complain
, in_decl
);
18400 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18401 outputs
, inputs
, clobbers
, labels
,
18403 tree asm_expr
= tmp
;
18404 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18405 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18406 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18413 stmt
= begin_try_block ();
18414 RECUR (TRY_STMTS (t
));
18415 finish_cleanup_try_block (stmt
);
18416 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18420 tree compound_stmt
= NULL_TREE
;
18422 if (FN_TRY_BLOCK_P (t
))
18423 stmt
= begin_function_try_block (&compound_stmt
);
18425 stmt
= begin_try_block ();
18427 RECUR (TRY_STMTS (t
));
18429 if (FN_TRY_BLOCK_P (t
))
18430 finish_function_try_block (stmt
);
18432 finish_try_block (stmt
);
18434 RECUR (TRY_HANDLERS (t
));
18435 if (FN_TRY_BLOCK_P (t
))
18436 finish_function_handler_sequence (stmt
, compound_stmt
);
18438 finish_handler_sequence (stmt
);
18444 tree decl
= HANDLER_PARMS (t
);
18448 decl
= tsubst (decl
, args
, complain
, in_decl
);
18449 /* Prevent instantiate_decl from trying to instantiate
18450 this variable. We've already done all that needs to be
18452 if (decl
!= error_mark_node
)
18453 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18455 stmt
= begin_handler ();
18456 finish_handler_parms (decl
, stmt
);
18457 RECUR (HANDLER_BODY (t
));
18458 finish_handler (stmt
);
18463 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18464 if (CLASS_TYPE_P (tmp
))
18466 /* Local classes are not independent templates; they are
18467 instantiated along with their containing function. And this
18468 way we don't have to deal with pushing out of one local class
18469 to instantiate a member of another local class. */
18470 /* Closures are handled by the LAMBDA_EXPR. */
18471 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18472 complete_type (tmp
);
18473 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18475 || (TREE_CODE (fld
) == FUNCTION_DECL
18476 && !DECL_ARTIFICIAL (fld
)))
18477 && DECL_TEMPLATE_INSTANTIATION (fld
))
18478 instantiate_decl (fld
, /*defer_ok=*/false,
18479 /*expl_inst_class=*/false);
18483 case STATIC_ASSERT
:
18487 ++c_inhibit_evaluation_warnings
;
18489 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18492 /*integral_constant_expression_p=*/true);
18493 --c_inhibit_evaluation_warnings
;
18495 finish_static_assert (condition
,
18496 STATIC_ASSERT_MESSAGE (t
),
18497 STATIC_ASSERT_SOURCE_LOCATION (t
),
18498 /*member_p=*/false, /*show_expr_p=*/true);
18503 case OACC_PARALLEL
:
18505 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18507 stmt
= begin_omp_parallel ();
18508 RECUR (OMP_BODY (t
));
18509 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18513 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18514 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18515 complain
, in_decl
);
18516 if (OMP_PARALLEL_COMBINED (t
))
18517 omp_parallel_combined_clauses
= &tmp
;
18518 stmt
= begin_omp_parallel ();
18519 RECUR (OMP_PARALLEL_BODY (t
));
18520 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18521 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18522 = OMP_PARALLEL_COMBINED (t
);
18523 pop_omp_privatization_clauses (r
);
18527 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18529 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18530 complain
, in_decl
);
18532 OMP_TASK_CLAUSES (t
) = tmp
;
18536 r
= push_omp_privatization_clauses (false);
18537 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18538 complain
, in_decl
);
18539 stmt
= begin_omp_task ();
18540 RECUR (OMP_TASK_BODY (t
));
18541 finish_omp_task (tmp
, stmt
);
18542 pop_omp_privatization_clauses (r
);
18548 case OMP_DISTRIBUTE
:
18552 tree clauses
, body
, pre_body
;
18553 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18554 tree orig_declv
= NULL_TREE
;
18555 tree incrv
= NULL_TREE
;
18556 enum c_omp_region_type ort
= C_ORT_OMP
;
18557 bool any_range_for
= false;
18560 if (TREE_CODE (t
) == OACC_LOOP
)
18563 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18564 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18566 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18568 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18569 if (OMP_FOR_ORIG_DECLS (t
))
18570 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18571 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18572 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18573 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18576 keep_next_level (true);
18577 stmt
= begin_omp_structured_block ();
18579 pre_body
= push_stmt_list ();
18580 RECUR (OMP_FOR_PRE_BODY (t
));
18581 pre_body
= pop_stmt_list (pre_body
);
18583 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18584 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18586 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18587 condv
, incrv
, &clauses
, args
,
18589 integral_constant_expression_p
);
18590 omp_parallel_combined_clauses
= NULL
;
18594 gcc_assert (orig_declv
);
18595 body
= begin_omp_structured_block ();
18596 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18597 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18598 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18599 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18600 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18601 TREE_VEC_ELT (declv
, i
));
18604 body
= push_stmt_list ();
18605 RECUR (OMP_FOR_BODY (t
));
18607 body
= finish_omp_structured_block (body
);
18609 body
= pop_stmt_list (body
);
18611 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18612 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18613 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18617 t
= make_node (TREE_CODE (t
));
18618 TREE_TYPE (t
) = void_type_node
;
18619 OMP_FOR_BODY (t
) = body
;
18620 OMP_FOR_PRE_BODY (t
) = pre_body
;
18621 OMP_FOR_CLAUSES (t
) = clauses
;
18622 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18626 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18628 pop_omp_privatization_clauses (r
);
18633 omp_parallel_combined_clauses
= NULL
;
18638 case OMP_TASKGROUP
:
18640 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18641 && OMP_TEAMS_COMBINED (t
));
18642 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18644 if (TREE_CODE (t
) == OMP_TEAMS
)
18646 keep_next_level (true);
18647 stmt
= begin_omp_structured_block ();
18648 RECUR (OMP_BODY (t
));
18649 stmt
= finish_omp_structured_block (stmt
);
18653 stmt
= push_stmt_list ();
18654 RECUR (OMP_BODY (t
));
18655 stmt
= pop_stmt_list (stmt
);
18658 if (TREE_CODE (t
) == OMP_CRITICAL
18659 && tmp
!= NULL_TREE
18660 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18662 error_at (OMP_CLAUSE_LOCATION (tmp
),
18663 "%<#pragma omp critical%> with %<hint%> clause requires "
18664 "a name, except when %<omp_sync_hint_none%> is used");
18665 RETURN (error_mark_node
);
18668 OMP_BODY (t
) = stmt
;
18669 OMP_CLAUSES (t
) = tmp
;
18671 pop_omp_privatization_clauses (r
);
18675 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18676 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18678 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18679 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18681 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18682 args
, complain
, in_decl
);
18683 if (tmp
== NULL_TREE
)
18684 tmp
= error_mark_node
;
18688 kind
= (enum omp_clause_depend_kind
)
18689 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18692 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18695 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18696 OMP_CLAUSE_DEPEND_SOURCE
,
18697 OMP_DEPOBJ_CLAUSES (t
));
18701 case OMP_TARGET_DATA
:
18703 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18704 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18706 keep_next_level (true);
18707 stmt
= begin_omp_structured_block ();
18709 RECUR (OMP_BODY (t
));
18710 stmt
= finish_omp_structured_block (stmt
);
18713 OMP_BODY (t
) = stmt
;
18714 OMP_CLAUSES (t
) = tmp
;
18715 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18717 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18720 /* For combined target teams, ensure the num_teams and
18721 thread_limit clause expressions are evaluated on the host,
18722 before entering the target construct. */
18724 for (c
= OMP_TEAMS_CLAUSES (teams
);
18725 c
; c
= OMP_CLAUSE_CHAIN (c
))
18726 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18727 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18728 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18730 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18731 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18732 if (expr
== error_mark_node
)
18734 tmp
= TARGET_EXPR_SLOT (expr
);
18736 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18737 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18738 OMP_CLAUSE_FIRSTPRIVATE
);
18739 OMP_CLAUSE_DECL (tc
) = tmp
;
18740 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18741 OMP_TARGET_CLAUSES (t
) = tc
;
18750 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18751 complain
, in_decl
);
18752 OACC_DECLARE_CLAUSES (t
) = tmp
;
18756 case OMP_TARGET_UPDATE
:
18757 case OMP_TARGET_ENTER_DATA
:
18758 case OMP_TARGET_EXIT_DATA
:
18759 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18760 complain
, in_decl
);
18762 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18766 case OACC_ENTER_DATA
:
18767 case OACC_EXIT_DATA
:
18769 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18770 complain
, in_decl
);
18772 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18777 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18778 complain
, in_decl
);
18779 stmt
= push_stmt_list ();
18780 RECUR (OMP_BODY (t
));
18781 stmt
= pop_stmt_list (stmt
);
18784 OMP_BODY (t
) = stmt
;
18785 OMP_ORDERED_CLAUSES (t
) = tmp
;
18790 omp_parallel_combined_clauses
= NULL
;
18793 stmt
= push_stmt_list ();
18794 RECUR (OMP_BODY (t
));
18795 stmt
= pop_stmt_list (stmt
);
18798 OMP_BODY (t
) = stmt
;
18803 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18805 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18806 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18807 complain
, in_decl
);
18808 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18810 tree op1
= TREE_OPERAND (t
, 1);
18811 tree rhs1
= NULL_TREE
;
18813 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18815 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18816 op1
= TREE_OPERAND (op1
, 1);
18818 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18819 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18820 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18821 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18822 OMP_ATOMIC_MEMORY_ORDER (t
));
18826 tree op1
= TREE_OPERAND (t
, 1);
18827 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18828 tree rhs1
= NULL_TREE
;
18829 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18830 enum tree_code opcode
= NOP_EXPR
;
18831 if (code
== OMP_ATOMIC_READ
)
18833 v
= RECUR (TREE_OPERAND (op1
, 0));
18834 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18836 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18837 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18839 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18840 v
= RECUR (TREE_OPERAND (op1
, 0));
18841 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18842 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18844 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18845 op11
= TREE_OPERAND (op11
, 1);
18847 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18848 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18849 opcode
= TREE_CODE (op11
);
18850 if (opcode
== MODIFY_EXPR
)
18856 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18857 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18859 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18860 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18864 case TRANSACTION_EXPR
:
18867 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18868 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18870 if (TRANSACTION_EXPR_IS_STMT (t
))
18872 tree body
= TRANSACTION_EXPR_BODY (t
);
18873 tree noex
= NULL_TREE
;
18874 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18876 noex
= MUST_NOT_THROW_COND (body
);
18877 if (noex
== NULL_TREE
)
18878 noex
= boolean_true_node
;
18879 body
= TREE_OPERAND (body
, 0);
18881 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18883 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18887 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18888 RECUR (TRANSACTION_EXPR_BODY (t
)),
18895 case MUST_NOT_THROW_EXPR
:
18897 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18898 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18899 RETURN (build_must_not_throw_expr (op0
, cond
));
18902 case EXPR_PACK_EXPANSION
:
18903 error ("invalid use of pack expansion expression");
18904 RETURN (error_mark_node
);
18906 case NONTYPE_ARGUMENT_PACK
:
18907 error ("use %<...%> to expand argument pack");
18908 RETURN (error_mark_node
);
18910 case COMPOUND_EXPR
:
18911 tmp
= RECUR (TREE_OPERAND (t
, 0));
18912 if (tmp
== NULL_TREE
)
18913 /* If the first operand was a statement, we're done with it. */
18914 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18915 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18916 RECUR (TREE_OPERAND (t
, 1)),
18919 case ANNOTATE_EXPR
:
18920 tmp
= RECUR (TREE_OPERAND (t
, 0));
18921 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18922 TREE_TYPE (tmp
), tmp
,
18923 RECUR (TREE_OPERAND (t
, 1)),
18924 RECUR (TREE_OPERAND (t
, 2))));
18927 RETURN (add_stmt (copy_node (t
)));
18930 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18932 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18933 /*function_p=*/false,
18934 integral_constant_expression_p
));
18937 RETURN (NULL_TREE
);
18939 input_location
= loc
;
18945 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18946 function. For description of the body see comment above
18947 cp_parser_omp_declare_reduction_exprs. */
18950 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18952 if (t
== NULL_TREE
|| t
== error_mark_node
)
18955 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
18957 tree_stmt_iterator tsi
;
18960 memset (stmts
, 0, sizeof stmts
);
18961 for (i
= 0, tsi
= tsi_start (t
);
18962 i
< 7 && !tsi_end_p (tsi
);
18963 i
++, tsi_next (&tsi
))
18964 stmts
[i
] = tsi_stmt (tsi
);
18965 gcc_assert (tsi_end_p (tsi
));
18969 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18970 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18971 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18972 args
, complain
, in_decl
);
18973 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18974 args
, complain
, in_decl
);
18975 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18976 expect to be pushing it. */
18977 DECL_CONTEXT (omp_out
) = current_function_decl
;
18978 DECL_CONTEXT (omp_in
) = current_function_decl
;
18979 keep_next_level (true);
18980 tree block
= begin_omp_structured_block ();
18981 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18982 block
= finish_omp_structured_block (block
);
18983 block
= maybe_cleanup_point_expr_void (block
);
18984 add_decl_expr (omp_out
);
18985 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18986 TREE_NO_WARNING (omp_out
) = 1;
18987 add_decl_expr (omp_in
);
18988 finish_expr_stmt (block
);
18992 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18993 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18994 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18995 args
, complain
, in_decl
);
18996 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18997 args
, complain
, in_decl
);
18998 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18999 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19000 keep_next_level (true);
19001 tree block
= begin_omp_structured_block ();
19002 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
19003 block
= finish_omp_structured_block (block
);
19004 block
= maybe_cleanup_point_expr_void (block
);
19005 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19006 add_decl_expr (omp_priv
);
19007 add_decl_expr (omp_orig
);
19008 finish_expr_stmt (block
);
19010 add_decl_expr (omp_orig
);
19014 /* T is a postfix-expression that is not being used in a function
19015 call. Return the substituted version of T. */
19018 tsubst_non_call_postfix_expression (tree t
, tree args
,
19019 tsubst_flags_t complain
,
19022 if (TREE_CODE (t
) == SCOPE_REF
)
19023 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19024 /*done=*/false, /*address_p=*/false);
19026 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19027 /*function_p=*/false,
19028 /*integral_constant_expression_p=*/false);
19033 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19034 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19035 dependent init-capture. */
19038 prepend_one_capture (tree field
, tree init
, tree
&list
,
19039 tsubst_flags_t complain
)
19041 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19043 tree type
= NULL_TREE
;
19046 if (complain
& tf_error
)
19047 error ("empty initializer in lambda init-capture");
19048 init
= error_mark_node
;
19050 else if (TREE_CODE (init
) == TREE_LIST
)
19051 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19053 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19054 TREE_TYPE (field
) = type
;
19055 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19057 list
= tree_cons (field
, init
, list
);
19060 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19061 instantiation context. Instantiating a pack expansion containing a lambda
19062 might result in multiple lambdas all based on the same lambda in the
19066 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19068 tree oldfn
= lambda_function (t
);
19071 tree r
= build_lambda_expr ();
19073 LAMBDA_EXPR_LOCATION (r
)
19074 = LAMBDA_EXPR_LOCATION (t
);
19075 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19076 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19077 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19078 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19080 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19081 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19083 vec
<tree
,va_gc
>* field_packs
= NULL
;
19085 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19086 cap
= TREE_CHAIN (cap
))
19088 tree ofield
= TREE_PURPOSE (cap
);
19089 tree init
= TREE_VALUE (cap
);
19090 if (PACK_EXPANSION_P (init
))
19091 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19093 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19094 /*fn*/false, /*constexpr*/false);
19096 if (init
== error_mark_node
)
19097 return error_mark_node
;
19099 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19100 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19102 if (!processing_template_decl
19103 && init
&& TREE_CODE (init
) != TREE_VEC
19104 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19106 /* For a VLA, simply tsubsting the field type won't work, we need to
19107 go through add_capture again. XXX do we want to do this for all
19109 tree name
= (get_identifier
19110 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19111 tree ftype
= TREE_TYPE (ofield
);
19112 bool by_ref
= (TYPE_REF_P (ftype
)
19113 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19114 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19115 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19119 if (PACK_EXPANSION_P (ofield
))
19120 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19121 tree field
= tsubst_decl (ofield
, args
, complain
);
19123 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19125 /* Remember these for when we've pushed local_specializations. */
19126 vec_safe_push (field_packs
, ofield
);
19127 vec_safe_push (field_packs
, field
);
19130 if (field
== error_mark_node
)
19131 return error_mark_node
;
19133 if (TREE_CODE (field
) == TREE_VEC
)
19135 int len
= TREE_VEC_LENGTH (field
);
19136 gcc_assert (TREE_CODE (init
) == TREE_VEC
19137 && TREE_VEC_LENGTH (init
) == len
);
19138 for (int i
= 0; i
< len
; ++i
)
19139 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19140 TREE_VEC_ELT (init
, i
),
19141 LAMBDA_EXPR_CAPTURE_LIST (r
),
19146 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19149 if (id_equal (DECL_NAME (field
), "__this"))
19150 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19154 tree type
= begin_lambda_type (r
);
19155 if (type
== error_mark_node
)
19156 return error_mark_node
;
19158 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19159 /* A lambda in a default argument outside a class gets no
19160 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19161 tsubst_default_argument calls start_lambda_scope, so we need to
19162 specifically ignore it here, and use the global scope. */
19163 record_null_lambda_scope (r
);
19165 record_lambda_scope (r
);
19167 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19168 determine_visibility (TYPE_NAME (type
));
19170 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19172 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19173 ? DECL_TI_TEMPLATE (oldfn
)
19176 tree fntype
= static_fn_type (oldfn
);
19178 ++processing_template_decl
;
19179 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19181 --processing_template_decl
;
19183 if (fntype
== error_mark_node
)
19184 r
= error_mark_node
;
19187 /* The body of a lambda-expression is not a subexpression of the
19188 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19189 which would be skipped if cp_unevaluated_operand. */
19192 /* Fix the type of 'this'. */
19193 fntype
= build_memfn_type (fntype
, type
,
19194 type_memfn_quals (fntype
),
19195 type_memfn_rqual (fntype
));
19199 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19200 if (tmpl
== error_mark_node
)
19202 r
= error_mark_node
;
19205 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19206 finish_member_declaration (tmpl
);
19211 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19212 if (fn
== error_mark_node
)
19214 r
= error_mark_node
;
19217 finish_member_declaration (fn
);
19220 if (tree ci
= get_constraints (oldfn
))
19222 /* Substitute into the lambda's constraints. */
19224 ++processing_template_decl
;
19225 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19227 --processing_template_decl
;
19228 set_constraints (fn
, ci
);
19231 /* Let finish_function set this. */
19232 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19234 bool nested
= cfun
;
19236 push_function_context ();
19238 /* Still increment function_depth so that we don't GC in the
19239 middle of an expression. */
19242 local_specialization_stack
s (lss_copy
);
19244 tree body
= start_lambda_function (fn
, r
);
19246 /* Now record them for lookup_init_capture_pack. */
19247 int fplen
= vec_safe_length (field_packs
);
19248 for (int i
= 0; i
< fplen
; )
19250 tree pack
= (*field_packs
)[i
++];
19251 tree inst
= (*field_packs
)[i
++];
19252 register_local_specialization (inst
, pack
);
19254 release_tree_vector (field_packs
);
19256 register_parameter_specializations (oldfn
, fn
);
19260 /* We might not partially instantiate some parts of the function, so
19261 copy these flags from the original template. */
19262 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19263 current_function_returns_value
= ol
->returns_value
;
19264 current_function_returns_null
= ol
->returns_null
;
19265 current_function_returns_abnormally
= ol
->returns_abnormally
;
19266 current_function_infinite_loop
= ol
->infinite_loop
;
19269 /* [temp.deduct] A lambda-expression appearing in a function type or a
19270 template parameter is not considered part of the immediate context for
19271 the purposes of template argument deduction. */
19272 complain
= tf_warning_or_error
;
19274 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19275 /*constexpr*/false);
19277 finish_lambda_function (body
);
19280 pop_function_context ();
19284 /* The capture list was built up in reverse order; fix that now. */
19285 LAMBDA_EXPR_CAPTURE_LIST (r
)
19286 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19288 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19290 maybe_add_lambda_conv_op (type
);
19294 finish_struct (type
, /*attr*/NULL_TREE
);
19296 insert_pending_capture_proxies ();
19301 /* Like tsubst but deals with expressions and performs semantic
19302 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19303 "F<TARGS> (ARGS)". */
19306 tsubst_copy_and_build (tree t
,
19308 tsubst_flags_t complain
,
19311 bool integral_constant_expression_p
)
19313 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19314 #define RECUR(NODE) \
19315 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19316 /*function_p=*/false, \
19317 integral_constant_expression_p)
19320 location_t save_loc
;
19322 if (t
== NULL_TREE
|| t
== error_mark_node
)
19325 save_loc
= input_location
;
19326 if (location_t eloc
= cp_expr_location (t
))
19327 input_location
= eloc
;
19329 /* N3276 decltype magic only applies to calls at the top level or on the
19330 right side of a comma. */
19331 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19332 complain
&= ~tf_decltype
;
19334 switch (TREE_CODE (t
))
19338 /* Fall through. */
19339 case IDENTIFIER_NODE
:
19343 bool non_integral_constant_expression_p
;
19344 const char *error_msg
;
19346 if (IDENTIFIER_CONV_OP_P (t
))
19348 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19349 t
= make_conv_op_name (new_type
);
19352 /* Look up the name. */
19353 decl
= lookup_name (t
);
19355 /* By convention, expressions use ERROR_MARK_NODE to indicate
19356 failure, not NULL_TREE. */
19357 if (decl
== NULL_TREE
)
19358 decl
= error_mark_node
;
19360 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19362 integral_constant_expression_p
,
19363 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19364 &non_integral_constant_expression_p
,
19365 /*template_p=*/false,
19367 /*address_p=*/false,
19368 /*template_arg_p=*/false,
19373 if (!function_p
&& identifier_p (decl
))
19375 if (complain
& tf_error
)
19376 unqualified_name_lookup_error (decl
);
19377 decl
= error_mark_node
;
19382 case TEMPLATE_ID_EXPR
:
19385 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19388 integral_constant_expression_p
);
19389 tree targs
= TREE_OPERAND (t
, 1);
19392 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19393 if (targs
== error_mark_node
)
19394 RETURN (error_mark_node
);
19396 if (TREE_CODE (templ
) == SCOPE_REF
)
19398 tree name
= TREE_OPERAND (templ
, 1);
19399 tree tid
= lookup_template_function (name
, targs
);
19400 TREE_OPERAND (templ
, 1) = tid
;
19404 if (concept_definition_p (templ
))
19406 tree check
= build_concept_check (templ
, targs
, complain
);
19407 if (check
== error_mark_node
)
19408 RETURN (error_mark_node
);
19410 tree id
= unpack_concept_check (check
);
19412 /* If we built a function concept check, return the underlying
19413 template-id. So we can evaluate it as a function call. */
19414 if (function_concept_p (TREE_OPERAND (id
, 0)))
19420 if (variable_template_p (templ
))
19422 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19424 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19428 if (TREE_CODE (templ
) == COMPONENT_REF
)
19430 object
= TREE_OPERAND (templ
, 0);
19431 templ
= TREE_OPERAND (templ
, 1);
19434 object
= NULL_TREE
;
19436 tree tid
= lookup_template_function (templ
, targs
);
19439 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
19440 object
, tid
, NULL_TREE
));
19441 else if (identifier_p (templ
))
19443 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19444 name lookup found nothing when parsing the template name. */
19445 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
19449 RETURN (baselink_for_fns (tid
));
19454 tree r
= RECUR (TREE_OPERAND (t
, 0));
19456 if (REFERENCE_REF_P (t
))
19458 /* A type conversion to reference type will be enclosed in
19459 such an indirect ref, but the substitution of the cast
19460 will have also added such an indirect ref. */
19461 r
= convert_from_reference (r
);
19464 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19465 complain
|decltype_flag
);
19467 if (REF_PARENTHESIZED_P (t
))
19468 r
= force_paren_expr (r
);
19475 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19476 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19477 RETURN (build_nop (type
, op0
));
19480 case IMPLICIT_CONV_EXPR
:
19482 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19483 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19484 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19486 retval
= copy_node (t
);
19487 TREE_TYPE (retval
) = type
;
19488 TREE_OPERAND (retval
, 0) = expr
;
19491 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19492 /* We'll pass this to convert_nontype_argument again, we don't need
19493 to actually perform any conversion here. */
19495 int flags
= LOOKUP_IMPLICIT
;
19496 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19497 flags
= LOOKUP_NORMAL
;
19498 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19499 flags
|= LOOKUP_NO_NARROWING
;
19500 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19506 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19507 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19508 if (op0
== error_mark_node
)
19509 RETURN (error_mark_node
);
19510 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19514 case REINTERPRET_CAST_EXPR
:
19515 case CONST_CAST_EXPR
:
19516 case DYNAMIC_CAST_EXPR
:
19517 case STATIC_CAST_EXPR
:
19520 tree op
, r
= NULL_TREE
;
19522 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19523 if (integral_constant_expression_p
19524 && !cast_valid_in_integral_constant_expression_p (type
))
19526 if (complain
& tf_error
)
19527 error ("a cast to a type other than an integral or "
19528 "enumeration type cannot appear in a constant-expression");
19529 RETURN (error_mark_node
);
19532 op
= RECUR (TREE_OPERAND (t
, 0));
19534 warning_sentinel
s(warn_useless_cast
);
19535 warning_sentinel
s2(warn_ignored_qualifiers
);
19536 switch (TREE_CODE (t
))
19539 r
= build_functional_cast (input_location
, type
, op
, complain
);
19541 case REINTERPRET_CAST_EXPR
:
19542 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19544 case CONST_CAST_EXPR
:
19545 r
= build_const_cast (input_location
, type
, op
, complain
);
19547 case DYNAMIC_CAST_EXPR
:
19548 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19550 case STATIC_CAST_EXPR
:
19551 r
= build_static_cast (input_location
, type
, op
, complain
);
19552 if (IMPLICIT_RVALUE_P (t
))
19553 set_implicit_rvalue_p (r
);
19556 gcc_unreachable ();
19562 case POSTDECREMENT_EXPR
:
19563 case POSTINCREMENT_EXPR
:
19564 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19565 args
, complain
, in_decl
);
19566 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19567 complain
|decltype_flag
));
19569 case PREDECREMENT_EXPR
:
19570 case PREINCREMENT_EXPR
:
19574 case TRUTH_NOT_EXPR
:
19575 case UNARY_PLUS_EXPR
: /* Unary + */
19576 case REALPART_EXPR
:
19577 case IMAGPART_EXPR
:
19578 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19579 RECUR (TREE_OPERAND (t
, 0)),
19580 complain
|decltype_flag
));
19582 case FIX_TRUNC_EXPR
:
19583 gcc_unreachable ();
19586 op1
= TREE_OPERAND (t
, 0);
19587 if (TREE_CODE (op1
) == LABEL_DECL
)
19588 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19589 EXPR_LOCATION (op1
)));
19590 if (TREE_CODE (op1
) == SCOPE_REF
)
19591 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19592 /*done=*/true, /*address_p=*/true);
19594 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19596 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19597 complain
|decltype_flag
));
19602 case TRUNC_DIV_EXPR
:
19603 case CEIL_DIV_EXPR
:
19604 case FLOOR_DIV_EXPR
:
19605 case ROUND_DIV_EXPR
:
19606 case EXACT_DIV_EXPR
:
19610 case TRUNC_MOD_EXPR
:
19611 case FLOOR_MOD_EXPR
:
19612 case TRUTH_ANDIF_EXPR
:
19613 case TRUTH_ORIF_EXPR
:
19614 case TRUTH_AND_EXPR
:
19615 case TRUTH_OR_EXPR
:
19626 case SPACESHIP_EXPR
:
19630 /* If T was type-dependent, suppress warnings that depend on the range
19631 of the types involved. */
19632 ++processing_template_decl
;
19633 const bool was_dep
= (potential_constant_expression (t
)
19634 ? value_dependent_expression_p (t
)
19635 : type_dependent_expression_p (t
));
19636 --processing_template_decl
;
19637 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19638 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19640 warning_sentinel
s1(warn_type_limits
, was_dep
);
19641 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19642 warning_sentinel
s3(warn_logical_op
, was_dep
);
19643 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19645 tree r
= build_x_binary_op
19646 (input_location
, TREE_CODE (t
),
19648 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19650 : TREE_CODE (TREE_OPERAND (t
, 0))),
19652 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19654 : TREE_CODE (TREE_OPERAND (t
, 1))),
19656 complain
|decltype_flag
);
19657 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19658 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19663 case POINTER_PLUS_EXPR
:
19665 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19666 if (op0
== error_mark_node
)
19667 RETURN (error_mark_node
);
19668 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19669 if (op1
== error_mark_node
)
19670 RETURN (error_mark_node
);
19671 RETURN (fold_build_pointer_plus (op0
, op1
));
19675 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19676 /*address_p=*/false));
19678 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19679 args
, complain
, in_decl
);
19680 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19681 RECUR (TREE_OPERAND (t
, 1)),
19682 complain
|decltype_flag
));
19685 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19686 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19687 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19694 op1
= TREE_OPERAND (t
, 0);
19695 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19696 op1
= TREE_TYPE (op1
);
19697 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19698 && ALIGNOF_EXPR_STD_P (t
));
19701 /* When there are no ARGS, we are trying to evaluate a
19702 non-dependent expression from the parser. Trying to do
19703 the substitutions may not work. */
19705 op1
= TREE_TYPE (op1
);
19709 ++cp_unevaluated_operand
;
19710 ++c_inhibit_evaluation_warnings
;
19712 op1
= tsubst (op1
, args
, complain
, in_decl
);
19714 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19715 /*function_p=*/false,
19716 /*integral_constant_expression_p=*/
19718 --cp_unevaluated_operand
;
19719 --c_inhibit_evaluation_warnings
;
19722 r
= cxx_sizeof_or_alignof_type (input_location
,
19723 op1
, TREE_CODE (t
), std_alignof
,
19724 complain
& tf_error
);
19726 r
= cxx_sizeof_or_alignof_expr (input_location
,
19727 op1
, TREE_CODE (t
), std_alignof
,
19728 complain
& tf_error
);
19729 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19731 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19733 if (!processing_template_decl
&& TYPE_P (op1
))
19735 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19736 build1 (NOP_EXPR
, op1
, error_mark_node
));
19737 SIZEOF_EXPR_TYPE_P (r
) = 1;
19740 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19741 TREE_SIDE_EFFECTS (r
) = 0;
19742 TREE_READONLY (r
) = 1;
19744 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19749 case AT_ENCODE_EXPR
:
19751 op1
= TREE_OPERAND (t
, 0);
19752 ++cp_unevaluated_operand
;
19753 ++c_inhibit_evaluation_warnings
;
19754 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19755 /*function_p=*/false,
19756 /*integral_constant_expression_p=*/false);
19757 --cp_unevaluated_operand
;
19758 --c_inhibit_evaluation_warnings
;
19759 RETURN (objc_build_encode_expr (op1
));
19762 case NOEXCEPT_EXPR
:
19763 op1
= TREE_OPERAND (t
, 0);
19764 ++cp_unevaluated_operand
;
19765 ++c_inhibit_evaluation_warnings
;
19766 ++cp_noexcept_operand
;
19767 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19768 /*function_p=*/false,
19769 /*integral_constant_expression_p=*/false);
19770 --cp_unevaluated_operand
;
19771 --c_inhibit_evaluation_warnings
;
19772 --cp_noexcept_operand
;
19773 RETURN (finish_noexcept_expr (op1
, complain
));
19777 warning_sentinel
s(warn_div_by_zero
);
19778 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19779 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19780 tree r
= build_x_modify_expr
19781 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19782 complain
|decltype_flag
);
19783 /* TREE_NO_WARNING must be set if either the expression was
19784 parenthesized or it uses an operator such as >>= rather
19785 than plain assignment. In the former case, it was already
19786 set and must be copied. In the latter case,
19787 build_x_modify_expr sets it and it must not be reset
19789 if (TREE_NO_WARNING (t
))
19790 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19796 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19797 args
, complain
, in_decl
);
19798 /* Remember that there was a reference to this entity. */
19800 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19801 RETURN (error_mark_node
);
19802 RETURN (build_x_arrow (input_location
, op1
, complain
));
19806 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19807 tree init
= RECUR (TREE_OPERAND (t
, 3));
19808 vec
<tree
, va_gc
> *placement_vec
;
19809 vec
<tree
, va_gc
> *init_vec
;
19811 location_t loc
= EXPR_LOCATION (t
);
19813 if (placement
== NULL_TREE
)
19814 placement_vec
= NULL
;
19815 else if (placement
== error_mark_node
)
19816 RETURN (error_mark_node
);
19819 placement_vec
= make_tree_vector ();
19820 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19821 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19824 /* If there was an initializer in the original tree, but it
19825 instantiated to an empty list, then we should pass a
19826 non-NULL empty vector to tell build_new that it was an
19827 empty initializer() rather than no initializer. This can
19828 only happen when the initializer is a pack expansion whose
19829 parameter packs are of length zero. */
19830 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19834 init_vec
= make_tree_vector ();
19835 if (init
== void_node
)
19836 gcc_assert (init_vec
!= NULL
);
19839 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19840 vec_safe_push (init_vec
, TREE_VALUE (init
));
19844 /* Avoid passing an enclosing decl to valid_array_size_p. */
19845 in_decl
= NULL_TREE
;
19847 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19848 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19849 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19850 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19853 if (placement_vec
!= NULL
)
19854 release_tree_vector (placement_vec
);
19855 if (init_vec
!= NULL
)
19856 release_tree_vector (init_vec
);
19863 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19864 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19865 RETURN (delete_sanity (input_location
, op0
, op1
,
19866 DELETE_EXPR_USE_VEC (t
),
19867 DELETE_EXPR_USE_GLOBAL (t
),
19871 case COMPOUND_EXPR
:
19873 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19874 complain
& ~tf_decltype
, in_decl
,
19875 /*function_p=*/false,
19876 integral_constant_expression_p
);
19877 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19879 RECUR (TREE_OPERAND (t
, 1)),
19880 complain
|decltype_flag
));
19886 unsigned int nargs
, i
;
19891 function
= CALL_EXPR_FN (t
);
19892 /* Internal function with no arguments. */
19893 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19896 /* When we parsed the expression, we determined whether or
19897 not Koenig lookup should be performed. */
19898 koenig_p
= KOENIG_LOOKUP_P (t
);
19899 if (function
== NULL_TREE
)
19902 qualified_p
= false;
19904 else if (TREE_CODE (function
) == SCOPE_REF
)
19906 qualified_p
= true;
19907 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19909 /*address_p=*/false);
19911 else if (koenig_p
&& identifier_p (function
))
19913 /* Do nothing; calling tsubst_copy_and_build on an identifier
19914 would incorrectly perform unqualified lookup again.
19916 Note that we can also have an IDENTIFIER_NODE if the earlier
19917 unqualified lookup found a member function; in that case
19918 koenig_p will be false and we do want to do the lookup
19919 again to find the instantiated member function.
19921 FIXME but doing that causes c++/15272, so we need to stop
19922 using IDENTIFIER_NODE in that situation. */
19923 qualified_p
= false;
19927 if (TREE_CODE (function
) == COMPONENT_REF
)
19929 tree op
= TREE_OPERAND (function
, 1);
19931 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19932 || (BASELINK_P (op
)
19933 && BASELINK_QUALIFIED_P (op
)));
19936 qualified_p
= false;
19938 if (TREE_CODE (function
) == ADDR_EXPR
19939 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19940 /* Avoid error about taking the address of a constructor. */
19941 function
= TREE_OPERAND (function
, 0);
19943 function
= tsubst_copy_and_build (function
, args
, complain
,
19946 integral_constant_expression_p
);
19948 if (BASELINK_P (function
))
19949 qualified_p
= true;
19952 nargs
= call_expr_nargs (t
);
19953 releasing_vec call_args
;
19954 for (i
= 0; i
< nargs
; ++i
)
19956 tree arg
= CALL_EXPR_ARG (t
, i
);
19958 if (!PACK_EXPANSION_P (arg
))
19959 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19962 /* Expand the pack expansion and push each entry onto
19964 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19965 if (TREE_CODE (arg
) == TREE_VEC
)
19967 unsigned int len
, j
;
19969 len
= TREE_VEC_LENGTH (arg
);
19970 for (j
= 0; j
< len
; ++j
)
19972 tree value
= TREE_VEC_ELT (arg
, j
);
19973 if (value
!= NULL_TREE
)
19974 value
= convert_from_reference (value
);
19975 vec_safe_push (call_args
, value
);
19980 /* A partial substitution. Add one entry. */
19981 vec_safe_push (call_args
, arg
);
19986 /* Stripped-down processing for a call in a thunk. Specifically, in
19987 the thunk template for a generic lambda. */
19988 if (call_from_lambda_thunk_p (t
))
19990 /* Now that we've expanded any packs, the number of call args
19991 might be different. */
19992 unsigned int cargs
= call_args
->length ();
19993 tree thisarg
= NULL_TREE
;
19994 if (TREE_CODE (function
) == COMPONENT_REF
)
19996 thisarg
= TREE_OPERAND (function
, 0);
19997 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19998 thisarg
= TREE_OPERAND (thisarg
, 0);
19999 function
= TREE_OPERAND (function
, 1);
20000 if (TREE_CODE (function
) == BASELINK
)
20001 function
= BASELINK_FUNCTIONS (function
);
20003 /* We aren't going to do normal overload resolution, so force the
20004 template-id to resolve. */
20005 function
= resolve_nondeduced_context (function
, complain
);
20006 for (unsigned i
= 0; i
< cargs
; ++i
)
20008 /* In a thunk, pass through args directly, without any
20010 tree arg
= (*call_args
)[i
];
20011 while (TREE_CODE (arg
) != PARM_DECL
)
20012 arg
= TREE_OPERAND (arg
, 0);
20013 (*call_args
)[i
] = arg
;
20017 /* If there are no other args, just push 'this'. */
20019 vec_safe_push (call_args
, thisarg
);
20022 /* Otherwise, shift the other args over to make room. */
20023 tree last
= (*call_args
)[cargs
- 1];
20024 vec_safe_push (call_args
, last
);
20025 for (int i
= cargs
- 1; i
> 0; --i
)
20026 (*call_args
)[i
] = (*call_args
)[i
- 1];
20027 (*call_args
)[0] = thisarg
;
20030 ret
= build_call_a (function
, call_args
->length (),
20031 call_args
->address ());
20032 /* The thunk location is not interesting. */
20033 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20034 CALL_FROM_THUNK_P (ret
) = true;
20035 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20036 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20041 /* We do not perform argument-dependent lookup if normal
20042 lookup finds a non-function, in accordance with the
20043 resolution of DR 218. */
20045 && ((is_overloaded_fn (function
)
20046 /* If lookup found a member function, the Koenig lookup is
20047 not appropriate, even if an unqualified-name was used
20048 to denote the function. */
20049 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20050 || identifier_p (function
)
20051 /* C++20 P0846: Lookup found nothing. */
20052 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20053 && identifier_p (TREE_OPERAND (function
, 0))))
20054 /* Only do this when substitution turns a dependent call
20055 into a non-dependent call. */
20056 && type_dependent_expression_p_push (t
)
20057 && !any_type_dependent_arguments_p (call_args
))
20058 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20060 if (function
!= NULL_TREE
20061 && (identifier_p (function
)
20062 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20063 && identifier_p (TREE_OPERAND (function
, 0))))
20064 && !any_type_dependent_arguments_p (call_args
))
20066 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20067 function
= TREE_OPERAND (function
, 0);
20068 if (koenig_p
&& (complain
& tf_warning_or_error
))
20070 /* For backwards compatibility and good diagnostics, try
20071 the unqualified lookup again if we aren't in SFINAE
20073 tree unq
= (tsubst_copy_and_build
20074 (function
, args
, complain
, in_decl
, true,
20075 integral_constant_expression_p
));
20076 if (unq
== error_mark_node
)
20077 RETURN (error_mark_node
);
20079 if (unq
!= function
)
20081 /* In a lambda fn, we have to be careful to not
20082 introduce new this captures. Legacy code can't
20083 be using lambdas anyway, so it's ok to be
20085 bool in_lambda
= (current_class_type
20086 && LAMBDA_TYPE_P (current_class_type
));
20087 char const *const msg
20088 = G_("%qD was not declared in this scope, "
20089 "and no declarations were found by "
20090 "argument-dependent lookup at the point "
20091 "of instantiation");
20095 error_at (cp_expr_loc_or_input_loc (t
),
20098 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20104 if (INDIRECT_REF_P (fn
))
20105 fn
= TREE_OPERAND (fn
, 0);
20106 if (is_overloaded_fn (fn
))
20107 fn
= get_first_fn (fn
);
20110 /* Can't say anything more. */;
20111 else if (DECL_CLASS_SCOPE_P (fn
))
20113 location_t loc
= cp_expr_loc_or_input_loc (t
);
20115 "declarations in dependent base %qT are "
20116 "not found by unqualified lookup",
20117 DECL_CLASS_CONTEXT (fn
));
20118 if (current_class_ptr
)
20120 "use %<this->%D%> instead", function
);
20123 "use %<%T::%D%> instead",
20124 current_class_name
, function
);
20127 inform (DECL_SOURCE_LOCATION (fn
),
20128 "%qD declared here, later in the "
20129 "translation unit", fn
);
20131 RETURN (error_mark_node
);
20137 if (identifier_p (function
))
20139 if (complain
& tf_error
)
20140 unqualified_name_lookup_error (function
);
20141 RETURN (error_mark_node
);
20145 /* Remember that there was a reference to this entity. */
20146 if (function
!= NULL_TREE
20147 && DECL_P (function
)
20148 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20149 RETURN (error_mark_node
);
20151 /* Put back tf_decltype for the actual call. */
20152 complain
|= decltype_flag
;
20154 if (function
== NULL_TREE
)
20155 switch (CALL_EXPR_IFN (t
))
20158 gcc_assert (nargs
== 1);
20159 if (vec_safe_length (call_args
) != 1)
20161 error_at (cp_expr_loc_or_input_loc (t
),
20162 "wrong number of arguments to "
20163 "%<__builtin_launder%>");
20164 ret
= error_mark_node
;
20167 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20168 (*call_args
)[0], complain
);
20171 case IFN_VEC_CONVERT
:
20172 gcc_assert (nargs
== 1);
20173 if (vec_safe_length (call_args
) != 1)
20175 error_at (cp_expr_loc_or_input_loc (t
),
20176 "wrong number of arguments to "
20177 "%<__builtin_convertvector%>");
20178 ret
= error_mark_node
;
20181 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20182 tsubst (TREE_TYPE (t
), args
,
20183 complain
, in_decl
),
20185 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20190 /* Unsupported internal function with arguments. */
20191 gcc_unreachable ();
20193 else if (TREE_CODE (function
) == OFFSET_REF
20194 || TREE_CODE (function
) == DOTSTAR_EXPR
20195 || TREE_CODE (function
) == MEMBER_REF
)
20196 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20198 else if (TREE_CODE (function
) == COMPONENT_REF
)
20200 tree instance
= TREE_OPERAND (function
, 0);
20201 tree fn
= TREE_OPERAND (function
, 1);
20203 if (processing_template_decl
20204 && (type_dependent_expression_p (instance
)
20205 || (!BASELINK_P (fn
)
20206 && TREE_CODE (fn
) != FIELD_DECL
)
20207 || type_dependent_expression_p (fn
)
20208 || any_type_dependent_arguments_p (call_args
)))
20209 ret
= build_min_nt_call_vec (function
, call_args
);
20210 else if (!BASELINK_P (fn
))
20211 ret
= finish_call_expr (function
, &call_args
,
20212 /*disallow_virtual=*/false,
20213 /*koenig_p=*/false,
20216 ret
= (build_new_method_call
20218 &call_args
, NULL_TREE
,
20219 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20223 else if (concept_check_p (function
))
20225 /* FUNCTION is a template-id referring to a concept definition. */
20226 tree id
= unpack_concept_check (function
);
20227 tree tmpl
= TREE_OPERAND (id
, 0);
20228 tree args
= TREE_OPERAND (id
, 1);
20230 /* Calls to standard and variable concepts should have been
20231 previously diagnosed. */
20232 gcc_assert (function_concept_p (tmpl
));
20234 /* Ensure the result is wrapped as a call expression. */
20235 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20238 ret
= finish_call_expr (function
, &call_args
,
20239 /*disallow_virtual=*/qualified_p
,
20243 if (ret
!= error_mark_node
)
20245 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20246 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20247 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20248 if (op
|| ord
|| rev
)
20250 function
= extract_call_expr (ret
);
20251 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20252 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20253 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20262 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20263 cond
= mark_rvalue_use (cond
);
20264 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20267 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20269 if (integer_zerop (folded_cond
))
20271 ++c_inhibit_evaluation_warnings
;
20272 exp1
= RECUR (TREE_OPERAND (t
, 1));
20273 --c_inhibit_evaluation_warnings
;
20274 exp2
= RECUR (TREE_OPERAND (t
, 2));
20278 exp1
= RECUR (TREE_OPERAND (t
, 1));
20279 ++c_inhibit_evaluation_warnings
;
20280 exp2
= RECUR (TREE_OPERAND (t
, 2));
20281 --c_inhibit_evaluation_warnings
;
20283 cond
= folded_cond
;
20287 exp1
= RECUR (TREE_OPERAND (t
, 1));
20288 exp2
= RECUR (TREE_OPERAND (t
, 2));
20291 warning_sentinel
s(warn_duplicated_branches
);
20292 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20293 cond
, exp1
, exp2
, complain
));
20296 case PSEUDO_DTOR_EXPR
:
20298 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20299 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20300 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20301 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20306 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20308 case COMPONENT_REF
:
20315 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20316 args
, complain
, in_decl
);
20317 /* Remember that there was a reference to this entity. */
20318 if (DECL_P (object
)
20319 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20320 RETURN (error_mark_node
);
20321 object_type
= TREE_TYPE (object
);
20323 member
= TREE_OPERAND (t
, 1);
20324 if (BASELINK_P (member
))
20325 member
= tsubst_baselink (member
,
20326 non_reference (TREE_TYPE (object
)),
20327 args
, complain
, in_decl
);
20329 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20330 if (member
== error_mark_node
)
20331 RETURN (error_mark_node
);
20333 if (TREE_CODE (member
) == FIELD_DECL
)
20335 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20336 if (TREE_CODE (r
) == COMPONENT_REF
)
20337 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20340 else if (type_dependent_expression_p (object
))
20341 /* We can't do much here. */;
20342 else if (!CLASS_TYPE_P (object_type
))
20344 if (scalarish_type_p (object_type
))
20346 tree s
= NULL_TREE
;
20347 tree dtor
= member
;
20349 if (TREE_CODE (dtor
) == SCOPE_REF
)
20351 s
= TREE_OPERAND (dtor
, 0);
20352 dtor
= TREE_OPERAND (dtor
, 1);
20354 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20356 dtor
= TREE_OPERAND (dtor
, 0);
20358 RETURN (finish_pseudo_destructor_expr
20359 (object
, s
, dtor
, input_location
));
20363 else if (TREE_CODE (member
) == SCOPE_REF
20364 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20366 /* Lookup the template functions now that we know what the
20368 tree scope
= TREE_OPERAND (member
, 0);
20369 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20370 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20371 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20372 /*complain=*/false);
20373 if (BASELINK_P (member
))
20375 BASELINK_FUNCTIONS (member
)
20376 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20378 member
= (adjust_result_of_qualified_name_lookup
20379 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20384 qualified_name_lookup_error (scope
, tmpl
, member
,
20386 RETURN (error_mark_node
);
20389 else if (TREE_CODE (member
) == SCOPE_REF
20390 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20391 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20393 if (complain
& tf_error
)
20395 if (TYPE_P (TREE_OPERAND (member
, 0)))
20396 error ("%qT is not a class or namespace",
20397 TREE_OPERAND (member
, 0));
20399 error ("%qD is not a class or namespace",
20400 TREE_OPERAND (member
, 0));
20402 RETURN (error_mark_node
);
20405 r
= finish_class_member_access_expr (object
, member
,
20406 /*template_p=*/false,
20408 if (TREE_CODE (r
) == COMPONENT_REF
)
20409 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20414 RETURN (build_throw
20415 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20419 vec
<constructor_elt
, va_gc
> *n
;
20420 constructor_elt
*ce
;
20421 unsigned HOST_WIDE_INT idx
;
20422 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20423 bool process_index_p
;
20425 bool need_copy_p
= false;
20428 if (type
== error_mark_node
)
20429 RETURN (error_mark_node
);
20431 /* We do not want to process the index of aggregate
20432 initializers as they are identifier nodes which will be
20433 looked up by digest_init. */
20434 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20436 if (null_member_pointer_value_p (t
))
20438 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20442 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20443 newlen
= vec_safe_length (n
);
20444 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20446 if (ce
->index
&& process_index_p
20447 /* An identifier index is looked up in the type
20448 being initialized, not the current scope. */
20449 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20450 ce
->index
= RECUR (ce
->index
);
20452 if (PACK_EXPANSION_P (ce
->value
))
20454 /* Substitute into the pack expansion. */
20455 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20458 if (ce
->value
== error_mark_node
20459 || PACK_EXPANSION_P (ce
->value
))
20461 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20462 /* Just move the argument into place. */
20463 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20466 /* Update the length of the final CONSTRUCTOR
20467 arguments vector, and note that we will need to
20469 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20470 need_copy_p
= true;
20474 ce
->value
= RECUR (ce
->value
);
20479 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20481 vec_alloc (n
, newlen
);
20482 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20484 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20486 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20487 for (i
= 0; i
< len
; ++i
)
20488 CONSTRUCTOR_APPEND_ELT (n
, 0,
20489 TREE_VEC_ELT (ce
->value
, i
));
20492 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20496 r
= build_constructor (init_list_type_node
, n
);
20497 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20498 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20499 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20501 if (TREE_HAS_CONSTRUCTOR (t
))
20503 fcl_t cl
= fcl_functional
;
20504 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20506 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20509 TREE_TYPE (r
) = type
;
20515 tree operand_0
= TREE_OPERAND (t
, 0);
20516 if (TYPE_P (operand_0
))
20518 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20519 RETURN (get_typeid (operand_0
, complain
));
20523 operand_0
= RECUR (operand_0
);
20524 RETURN (build_typeid (operand_0
, complain
));
20535 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20536 /* ??? We're doing a subset of finish_id_expression here. */
20537 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20538 /* Replace an evaluated use of the thread_local variable with
20539 a call to its wrapper. */
20541 else if (outer_automatic_var_p (r
))
20542 r
= process_outer_var_ref (r
, complain
);
20544 if (!TYPE_REF_P (TREE_TYPE (t
)))
20545 /* If the original type was a reference, we'll be wrapped in
20546 the appropriate INDIRECT_REF. */
20547 r
= convert_from_reference (r
);
20553 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20554 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20555 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20558 case OFFSETOF_EXPR
:
20561 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20562 in_decl
, /*function_p=*/false,
20563 /*integral_constant_expression_p=*/false);
20564 RETURN (finish_offsetof (object_ptr
,
20565 RECUR (TREE_OPERAND (t
, 0)),
20566 EXPR_LOCATION (t
)));
20569 case ADDRESSOF_EXPR
:
20570 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20571 RECUR (TREE_OPERAND (t
, 0)), complain
));
20575 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20576 complain
, in_decl
);
20577 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20578 complain
, in_decl
);
20579 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20580 TRAIT_EXPR_KIND (t
), type1
, type2
));
20585 tree old_stmt_expr
= cur_stmt_expr
;
20586 tree stmt_expr
= begin_stmt_expr ();
20588 cur_stmt_expr
= stmt_expr
;
20589 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20590 integral_constant_expression_p
);
20591 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20592 cur_stmt_expr
= old_stmt_expr
;
20594 /* If the resulting list of expression statement is empty,
20595 fold it further into void_node. */
20596 if (empty_expr_stmt_p (stmt_expr
))
20597 stmt_expr
= void_node
;
20599 RETURN (stmt_expr
);
20604 if (complain
& tf_partial
)
20606 /* We don't have a full set of template arguments yet; don't touch
20607 the lambda at all. */
20608 gcc_assert (processing_template_decl
);
20611 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20613 RETURN (build_lambda_object (r
));
20617 /* We can get here for a constant initializer of non-dependent type.
20618 FIXME stop folding in cp_parser_initializer_clause. */
20620 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20625 case TRANSACTION_EXPR
:
20626 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20627 integral_constant_expression_p
));
20630 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20632 case VEC_PERM_EXPR
:
20634 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20635 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20636 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20637 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20641 case REQUIRES_EXPR
:
20643 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20648 /* No need to substitute further, a RANGE_EXPR will always be built
20649 with constant operands. */
20652 case NON_LVALUE_EXPR
:
20653 case VIEW_CONVERT_EXPR
:
20654 if (location_wrapper_p (t
))
20655 /* We need to do this here as well as in tsubst_copy so we get the
20656 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20657 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20658 EXPR_LOCATION (t
)));
20662 /* Handle Objective-C++ constructs, if appropriate. */
20665 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20666 in_decl
, /*function_p=*/false);
20670 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20676 input_location
= save_loc
;
20680 /* Verify that the instantiated ARGS are valid. For type arguments,
20681 make sure that the type's linkage is ok. For non-type arguments,
20682 make sure they are constants if they are integral or enumerations.
20683 Emit an error under control of COMPLAIN, and return TRUE on error. */
20686 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20688 if (dependent_template_arg_p (t
))
20690 if (ARGUMENT_PACK_P (t
))
20692 tree vec
= ARGUMENT_PACK_ARGS (t
);
20693 int len
= TREE_VEC_LENGTH (vec
);
20694 bool result
= false;
20697 for (i
= 0; i
< len
; ++i
)
20698 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20702 else if (TYPE_P (t
))
20704 /* [basic.link]: A name with no linkage (notably, the name
20705 of a class or enumeration declared in a local scope)
20706 shall not be used to declare an entity with linkage.
20707 This implies that names with no linkage cannot be used as
20710 DR 757 relaxes this restriction for C++0x. */
20711 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20712 : no_linkage_check (t
, /*relaxed_p=*/false));
20716 /* DR 488 makes use of a type with no linkage cause
20717 type deduction to fail. */
20718 if (complain
& tf_error
)
20720 if (TYPE_UNNAMED_P (nt
))
20721 error ("%qT is/uses unnamed type", t
);
20723 error ("template argument for %qD uses local type %qT",
20728 /* In order to avoid all sorts of complications, we do not
20729 allow variably-modified types as template arguments. */
20730 else if (variably_modified_type_p (t
, NULL_TREE
))
20732 if (complain
& tf_error
)
20733 error ("%qT is a variably modified type", t
);
20737 /* Class template and alias template arguments should be OK. */
20738 else if (DECL_TYPE_TEMPLATE_P (t
))
20740 /* A non-type argument of integral or enumerated type must be a
20742 else if (TREE_TYPE (t
)
20743 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20744 && !REFERENCE_REF_P (t
)
20745 && !TREE_CONSTANT (t
))
20747 if (complain
& tf_error
)
20748 error ("integral expression %qE is not constant", t
);
20755 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20757 int ix
, len
= DECL_NTPARMS (tmpl
);
20758 bool result
= false;
20760 for (ix
= 0; ix
!= len
; ix
++)
20762 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20765 if (result
&& (complain
& tf_error
))
20766 error (" trying to instantiate %qD", tmpl
);
20770 /* We're out of SFINAE context now, so generate diagnostics for the access
20771 errors we saw earlier when instantiating D from TMPL and ARGS. */
20774 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20776 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20777 tree type
= TREE_TYPE (pattern
);
20778 location_t loc
= input_location
;
20780 push_access_scope (d
);
20781 push_deferring_access_checks (dk_no_deferred
);
20782 input_location
= DECL_SOURCE_LOCATION (pattern
);
20783 tsubst (type
, args
, tf_warning_or_error
, d
);
20784 input_location
= loc
;
20785 pop_deferring_access_checks ();
20786 pop_access_scope (d
);
20789 /* Instantiate the indicated variable, function, or alias template TMPL with
20790 the template arguments in TARG_PTR. */
20793 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20795 tree targ_ptr
= orig_args
;
20799 bool access_ok
= true;
20801 if (tmpl
== error_mark_node
)
20802 return error_mark_node
;
20804 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20806 /* If this function is a clone, handle it specially. */
20807 if (DECL_CLONED_FUNCTION_P (tmpl
))
20812 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20813 DECL_CLONED_FUNCTION. */
20814 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20815 targ_ptr
, complain
);
20816 if (spec
== error_mark_node
)
20817 return error_mark_node
;
20819 /* Look for the clone. */
20820 FOR_EACH_CLONE (clone
, spec
)
20821 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20823 /* We should always have found the clone by now. */
20824 gcc_unreachable ();
20828 if (targ_ptr
== error_mark_node
)
20829 return error_mark_node
;
20831 /* Check to see if we already have this specialization. */
20832 gen_tmpl
= most_general_template (tmpl
);
20833 if (TMPL_ARGS_DEPTH (targ_ptr
)
20834 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20835 /* targ_ptr only has the innermost template args, so add the outer ones
20836 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20837 the case of a non-dependent call within a template definition). */
20838 targ_ptr
= (add_outermost_template_args
20839 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20842 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20843 but it doesn't seem to be on the hot path. */
20844 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20846 gcc_checking_assert (tmpl
== gen_tmpl
20848 = retrieve_specialization (tmpl
, orig_args
, 0))
20850 || fndecl
== NULL_TREE
);
20852 if (spec
!= NULL_TREE
)
20854 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20856 if (complain
& tf_error
)
20857 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20858 return error_mark_node
;
20863 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20865 return error_mark_node
;
20867 /* We are building a FUNCTION_DECL, during which the access of its
20868 parameters and return types have to be checked. However this
20869 FUNCTION_DECL which is the desired context for access checking
20870 is not built yet. We solve this chicken-and-egg problem by
20871 deferring all checks until we have the FUNCTION_DECL. */
20872 push_deferring_access_checks (dk_deferred
);
20874 /* Instantiation of the function happens in the context of the function
20875 template, not the context of the overload resolution we're doing. */
20876 push_to_top_level ();
20877 /* If there are dependent arguments, e.g. because we're doing partial
20878 ordering, make sure processing_template_decl stays set. */
20879 if (uses_template_parms (targ_ptr
))
20880 ++processing_template_decl
;
20881 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20883 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20884 complain
, gen_tmpl
, true);
20885 push_nested_class (ctx
);
20888 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20890 fndecl
= NULL_TREE
;
20891 if (VAR_P (pattern
))
20893 /* We need to determine if we're using a partial or explicit
20894 specialization now, because the type of the variable could be
20896 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20897 tree elt
= most_specialized_partial_spec (tid
, complain
);
20898 if (elt
== error_mark_node
)
20899 pattern
= error_mark_node
;
20902 tree partial_tmpl
= TREE_VALUE (elt
);
20903 tree partial_args
= TREE_PURPOSE (elt
);
20904 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20905 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20909 /* Substitute template parameters to obtain the specialization. */
20910 if (fndecl
== NULL_TREE
)
20911 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20912 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20913 pop_nested_class ();
20914 pop_from_top_level ();
20916 if (fndecl
== error_mark_node
)
20918 pop_deferring_access_checks ();
20919 return error_mark_node
;
20922 /* The DECL_TI_TEMPLATE should always be the immediate parent
20923 template, not the most general template. */
20924 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20925 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20927 /* Now we know the specialization, compute access previously
20928 deferred. Do no access control for inheriting constructors,
20929 as we already checked access for the inherited constructor. */
20930 if (!(flag_new_inheriting_ctors
20931 && DECL_INHERITED_CTOR (fndecl
)))
20933 push_access_scope (fndecl
);
20934 if (!perform_deferred_access_checks (complain
))
20936 pop_access_scope (fndecl
);
20938 pop_deferring_access_checks ();
20940 /* If we've just instantiated the main entry point for a function,
20941 instantiate all the alternate entry points as well. We do this
20942 by cloning the instantiation of the main entry point, not by
20943 instantiating the template clones. */
20944 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20945 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20946 clone_cdtor (fndecl
, /*update_methods=*/false);
20950 if (!(complain
& tf_error
))
20952 /* Remember to reinstantiate when we're out of SFINAE so the user
20953 can see the errors. */
20954 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20956 return error_mark_node
;
20961 /* Wrapper for instantiate_template_1. */
20964 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20967 timevar_push (TV_TEMPLATE_INST
);
20968 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20969 timevar_pop (TV_TEMPLATE_INST
);
20973 /* Instantiate the alias template TMPL with ARGS. Also push a template
20974 instantiation level, which instantiate_template doesn't do because
20975 functions and variables have sufficient context established by the
20979 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20981 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20982 return error_mark_node
;
20985 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20986 args
, tmpl
, complain
,
20987 /*require_all_args=*/true,
20988 /*use_default_args=*/true);
20990 /* FIXME check for satisfaction in check_instantiated_args. */
20992 && !any_dependent_template_arguments_p (args
)
20993 && !constraints_satisfied_p (tmpl
, args
))
20995 if (complain
& tf_error
)
20997 auto_diagnostic_group d
;
20998 error ("template constraint failure for %qD", tmpl
);
20999 diagnose_constraints (input_location
, tmpl
, args
);
21001 return error_mark_node
;
21004 if (!push_tinst_level (tmpl
, args
))
21005 return error_mark_node
;
21006 tree r
= instantiate_template (tmpl
, args
, complain
);
21007 pop_tinst_level ();
21012 /* PARM is a template parameter pack for FN. Returns true iff
21013 PARM is used in a deducible way in the argument list of FN. */
21016 pack_deducible_p (tree parm
, tree fn
)
21018 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
21019 for (; t
; t
= TREE_CHAIN (t
))
21021 tree type
= TREE_VALUE (t
);
21023 if (!PACK_EXPANSION_P (type
))
21025 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
21026 packs
; packs
= TREE_CHAIN (packs
))
21027 if (template_args_equal (TREE_VALUE (packs
), parm
))
21029 /* The template parameter pack is used in a function parameter
21030 pack. If this is the end of the parameter list, the
21031 template parameter pack is deducible. */
21032 if (TREE_CHAIN (t
) == void_list_node
)
21035 /* Otherwise, not. Well, it could be deduced from
21036 a non-pack parameter, but doing so would end up with
21037 a deduction mismatch, so don't bother. */
21041 /* The template parameter pack isn't used in any function parameter
21042 packs, but it might be used deeper, e.g. tuple<Args...>. */
21046 /* Subroutine of fn_type_unification: check non-dependent parms for
21050 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21051 tree fn
, unification_kind_t strict
, int flags
,
21052 struct conversion
**convs
, bool explain_p
)
21054 /* Non-constructor methods need to leave a conversion for 'this', which
21055 isn't included in nargs here. */
21056 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21057 && !DECL_CONSTRUCTOR_P (fn
));
21059 for (unsigned ia
= 0;
21060 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21062 tree parm
= TREE_VALUE (parms
);
21064 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21065 && (!TREE_CHAIN (parms
)
21066 || TREE_CHAIN (parms
) == void_list_node
))
21067 /* For a function parameter pack that occurs at the end of the
21068 parameter-declaration-list, the type A of each remaining
21069 argument of the call is compared with the type P of the
21070 declarator-id of the function parameter pack. */
21073 parms
= TREE_CHAIN (parms
);
21075 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21076 /* For a function parameter pack that does not occur at the
21077 end of the parameter-declaration-list, the type of the
21078 parameter pack is a non-deduced context. */
21081 if (!uses_template_parms (parm
))
21083 tree arg
= args
[ia
];
21084 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21085 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21087 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21088 conv_p
, explain_p
))
21098 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21099 NARGS elements of the arguments that are being used when calling
21100 it. TARGS is a vector into which the deduced template arguments
21103 Returns either a FUNCTION_DECL for the matching specialization of FN or
21104 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21105 true, diagnostics will be printed to explain why it failed.
21107 If FN is a conversion operator, or we are trying to produce a specific
21108 specialization, RETURN_TYPE is the return type desired.
21110 The EXPLICIT_TARGS are explicit template arguments provided via a
21113 The parameter STRICT is one of:
21116 We are deducing arguments for a function call, as in
21117 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21118 deducing arguments for a call to the result of a conversion
21119 function template, as in [over.call.object].
21122 We are deducing arguments for a conversion function, as in
21123 [temp.deduct.conv].
21126 We are deducing arguments when doing an explicit instantiation
21127 as in [temp.explicit], when determining an explicit specialization
21128 as in [temp.expl.spec], or when taking the address of a function
21129 template, as in [temp.deduct.funcaddr]. */
21132 fn_type_unification (tree fn
,
21133 tree explicit_targs
,
21136 unsigned int nargs
,
21138 unification_kind_t strict
,
21140 struct conversion
**convs
,
21146 tree decl
= NULL_TREE
;
21147 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21149 static int deduction_depth
;
21150 /* type_unification_real will pass back any access checks from default
21151 template argument substitution. */
21152 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21153 /* We don't have all the template args yet. */
21154 bool incomplete
= true;
21157 if (flag_new_inheriting_ctors
)
21158 fn
= strip_inheriting_ctors (fn
);
21160 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21161 tree r
= error_mark_node
;
21163 tree full_targs
= targs
;
21164 if (TMPL_ARGS_DEPTH (targs
)
21165 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21166 full_targs
= (add_outermost_template_args
21167 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21171 complain
|= tf_decltype
;
21173 /* In C++0x, it's possible to have a function template whose type depends
21174 on itself recursively. This is most obvious with decltype, but can also
21175 occur with enumeration scope (c++/48969). So we need to catch infinite
21176 recursion and reject the substitution at deduction time; this function
21177 will return error_mark_node for any repeated substitution.
21179 This also catches excessive recursion such as when f<N> depends on
21180 f<N-1> across all integers, and returns error_mark_node for all the
21181 substitutions back up to the initial one.
21183 This is, of course, not reentrant. */
21184 if (excessive_deduction_depth
)
21185 return error_mark_node
;
21188 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21190 fntype
= TREE_TYPE (fn
);
21191 if (explicit_targs
)
21195 The specified template arguments must match the template
21196 parameters in kind (i.e., type, nontype, template), and there
21197 must not be more arguments than there are parameters;
21198 otherwise type deduction fails.
21200 Nontype arguments must match the types of the corresponding
21201 nontype template parameters, or must be convertible to the
21202 types of the corresponding nontype parameters as specified in
21203 _temp.arg.nontype_, otherwise type deduction fails.
21205 All references in the function type of the function template
21206 to the corresponding template parameters are replaced by the
21207 specified template argument values. If a substitution in a
21208 template parameter or in the function type of the function
21209 template results in an invalid type, type deduction fails. */
21210 int i
, len
= TREE_VEC_LENGTH (tparms
);
21211 location_t loc
= input_location
;
21212 incomplete
= false;
21214 if (explicit_targs
== error_mark_node
)
21217 if (TMPL_ARGS_DEPTH (explicit_targs
)
21218 < TMPL_ARGS_DEPTH (full_targs
))
21219 explicit_targs
= add_outermost_template_args (full_targs
,
21222 /* Adjust any explicit template arguments before entering the
21223 substitution context. */
21225 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21226 complain
|tf_partial
,
21227 /*require_all_args=*/false,
21228 /*use_default_args=*/false));
21229 if (explicit_targs
== error_mark_node
)
21232 /* Substitute the explicit args into the function type. This is
21233 necessary so that, for instance, explicitly declared function
21234 arguments can match null pointed constants. If we were given
21235 an incomplete set of explicit args, we must not do semantic
21236 processing during substitution as we could create partial
21238 for (i
= 0; i
< len
; i
++)
21240 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21241 bool parameter_pack
= false;
21242 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21244 /* Dig out the actual parm. */
21245 if (TREE_CODE (parm
) == TYPE_DECL
21246 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21248 parm
= TREE_TYPE (parm
);
21249 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21251 else if (TREE_CODE (parm
) == PARM_DECL
)
21253 parm
= DECL_INITIAL (parm
);
21254 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21257 if (targ
== NULL_TREE
)
21258 /* No explicit argument for this template parameter. */
21260 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21262 /* Mark the argument pack as "incomplete". We could
21263 still deduce more arguments during unification.
21264 We remove this mark in type_unification_real. */
21265 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21266 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21267 = ARGUMENT_PACK_ARGS (targ
);
21269 /* We have some incomplete argument packs. */
21276 if (!push_tinst_level (fn
, explicit_targs
))
21278 excessive_deduction_depth
= true;
21281 ++processing_template_decl
;
21282 input_location
= DECL_SOURCE_LOCATION (fn
);
21283 /* Ignore any access checks; we'll see them again in
21284 instantiate_template and they might have the wrong
21285 access path at this point. */
21286 push_deferring_access_checks (dk_deferred
);
21287 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21288 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21289 pop_deferring_access_checks ();
21290 input_location
= loc
;
21291 --processing_template_decl
;
21292 pop_tinst_level ();
21294 if (fntype
== error_mark_node
)
21298 /* Place the explicitly specified arguments in TARGS. */
21299 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21300 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21301 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21302 if (!incomplete
&& CHECKING_P
21303 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21304 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21305 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21308 if (return_type
&& strict
!= DEDUCE_CALL
)
21310 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21311 new_args
[0] = return_type
;
21312 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21320 /* Never do unification on the 'this' parameter. */
21321 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21323 if (return_type
&& strict
== DEDUCE_CALL
)
21325 /* We're deducing for a call to the result of a template conversion
21326 function. The parms we really want are in return_type. */
21327 if (INDIRECT_TYPE_P (return_type
))
21328 return_type
= TREE_TYPE (return_type
);
21329 parms
= TYPE_ARG_TYPES (return_type
);
21331 else if (return_type
)
21333 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21336 /* We allow incomplete unification without an error message here
21337 because the standard doesn't seem to explicitly prohibit it. Our
21338 callers must be ready to deal with unification failures in any
21341 /* If we aren't explaining yet, push tinst context so we can see where
21342 any errors (e.g. from class instantiations triggered by instantiation
21343 of default template arguments) come from. If we are explaining, this
21344 context is redundant. */
21345 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21347 excessive_deduction_depth
= true;
21351 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21352 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21353 strict
, &checks
, explain_p
);
21355 pop_tinst_level ();
21359 /* Now that we have bindings for all of the template arguments,
21360 ensure that the arguments deduced for the template template
21361 parameters have compatible template parameter lists. We cannot
21362 check this property before we have deduced all template
21363 arguments, because the template parameter types of a template
21364 template parameter might depend on prior template parameters
21365 deduced after the template template parameter. The following
21366 ill-formed example illustrates this issue:
21368 template<typename T, template<T> class C> void f(C<5>, T);
21370 template<int N> struct X {};
21373 f(X<5>(), 5l); // error: template argument deduction fails
21376 The template parameter list of 'C' depends on the template type
21377 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21378 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21379 time that we deduce 'C'. */
21380 if (!template_template_parm_bindings_ok_p
21381 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21383 unify_inconsistent_template_template_parameters (explain_p
);
21389 /* CWG2369: Check satisfaction before non-deducible conversions. */
21390 if (!constraints_satisfied_p (fn
, targs
))
21393 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21397 /* DR 1391: All parameters have args, now check non-dependent parms for
21398 convertibility. We don't do this if all args were explicitly specified,
21399 as the standard says that we substitute explicit args immediately. */
21401 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21405 /* All is well so far. Now, check:
21409 When all template arguments have been deduced, all uses of
21410 template parameters in nondeduced contexts are replaced with
21411 the corresponding deduced argument values. If the
21412 substitution results in an invalid type, as described above,
21413 type deduction fails. */
21414 if (!push_tinst_level (fn
, targs
))
21416 excessive_deduction_depth
= true;
21420 /* Also collect access checks from the instantiation. */
21421 reopen_deferring_access_checks (checks
);
21423 decl
= instantiate_template (fn
, targs
, complain
);
21425 checks
= get_deferred_access_checks ();
21426 pop_deferring_access_checks ();
21428 pop_tinst_level ();
21430 if (decl
== error_mark_node
)
21433 /* Now perform any access checks encountered during substitution. */
21434 push_access_scope (decl
);
21435 ok
= perform_access_checks (checks
, complain
);
21436 pop_access_scope (decl
);
21440 /* If we're looking for an exact match, check that what we got
21441 is indeed an exact match. It might not be if some template
21442 parameters are used in non-deduced contexts. But don't check
21443 for an exact match if we have dependent template arguments;
21444 in that case we're doing partial ordering, and we already know
21445 that we have two candidates that will provide the actual type. */
21446 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21448 tree substed
= TREE_TYPE (decl
);
21452 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21454 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21455 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21456 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21458 unify_type_mismatch (explain_p
, args
[i
],
21459 TREE_VALUE (sarg
));
21464 /* After doing deduction with the inherited constructor, actually return an
21465 instantiation of the inheriting constructor. */
21467 decl
= instantiate_template (orig_fn
, targs
, complain
);
21473 if (excessive_deduction_depth
)
21475 if (deduction_depth
== 0)
21476 /* Reset once we're all the way out. */
21477 excessive_deduction_depth
= false;
21483 /* Adjust types before performing type deduction, as described in
21484 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21485 sections are symmetric. PARM is the type of a function parameter
21486 or the return type of the conversion function. ARG is the type of
21487 the argument passed to the call, or the type of the value
21488 initialized with the result of the conversion function.
21489 ARG_EXPR is the original argument expression, which may be null. */
21492 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21505 /* Swap PARM and ARG throughout the remainder of this
21506 function; the handling is precisely symmetric since PARM
21507 will initialize ARG rather than vice versa. */
21508 std::swap (parm
, arg
);
21512 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21513 too, but here handle it by stripping the reference from PARM
21514 rather than by adding it to ARG. */
21515 if (TYPE_REF_P (*parm
)
21516 && TYPE_REF_IS_RVALUE (*parm
)
21517 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21518 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21519 && TYPE_REF_P (*arg
)
21520 && !TYPE_REF_IS_RVALUE (*arg
))
21521 *parm
= TREE_TYPE (*parm
);
21522 /* Nothing else to do in this case. */
21526 gcc_unreachable ();
21529 if (!TYPE_REF_P (*parm
))
21531 /* [temp.deduct.call]
21533 If P is not a reference type:
21535 --If A is an array type, the pointer type produced by the
21536 array-to-pointer standard conversion (_conv.array_) is
21537 used in place of A for type deduction; otherwise,
21539 --If A is a function type, the pointer type produced by
21540 the function-to-pointer standard conversion
21541 (_conv.func_) is used in place of A for type deduction;
21544 --If A is a cv-qualified type, the top level
21545 cv-qualifiers of A's type are ignored for type
21547 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21548 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21549 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21550 *arg
= build_pointer_type (*arg
);
21552 *arg
= TYPE_MAIN_VARIANT (*arg
);
21555 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21556 reference to a cv-unqualified template parameter that does not represent a
21557 template parameter of a class template (during class template argument
21558 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21559 an lvalue, the type "lvalue reference to A" is used in place of A for type
21561 if (TYPE_REF_P (*parm
)
21562 && TYPE_REF_IS_RVALUE (*parm
)
21563 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21564 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21565 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21566 && (arg_expr
? lvalue_p (arg_expr
)
21567 /* try_one_overload doesn't provide an arg_expr, but
21568 functions are always lvalues. */
21569 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21570 *arg
= build_reference_type (*arg
);
21572 /* [temp.deduct.call]
21574 If P is a cv-qualified type, the top level cv-qualifiers
21575 of P's type are ignored for type deduction. If P is a
21576 reference type, the type referred to by P is used for
21578 *parm
= TYPE_MAIN_VARIANT (*parm
);
21579 if (TYPE_REF_P (*parm
))
21581 *parm
= TREE_TYPE (*parm
);
21582 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21585 /* DR 322. For conversion deduction, remove a reference type on parm
21586 too (which has been swapped into ARG). */
21587 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21588 *arg
= TREE_TYPE (*arg
);
21593 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21594 template which doesn't contain any deducible template parameters; check if
21595 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21596 unify_one_argument. */
21599 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21600 int flags
, struct conversion
**conv_p
,
21606 type
= TREE_TYPE (arg
);
21610 if (same_type_p (parm
, type
))
21611 return unify_success (explain_p
);
21613 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21614 if (strict
== DEDUCE_CONV
)
21616 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21617 return unify_success (explain_p
);
21619 else if (strict
!= DEDUCE_EXACT
)
21622 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21624 /* Avoid recalculating this in add_function_candidate. */
21626 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21628 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21630 return unify_success (explain_p
);
21633 if (strict
== DEDUCE_EXACT
)
21634 return unify_type_mismatch (explain_p
, parm
, arg
);
21636 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21639 static bool uses_deducible_template_parms (tree type
);
21641 /* Returns true iff the expression EXPR is one from which a template
21642 argument can be deduced. In other words, if it's an undecorated
21643 use of a template non-type parameter. */
21646 deducible_expression (tree expr
)
21648 /* Strip implicit conversions. */
21649 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21650 expr
= TREE_OPERAND (expr
, 0);
21651 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21654 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21655 deducible way; that is, if it has a max value of <PARM> - 1. */
21658 deducible_array_bound (tree domain
)
21660 if (domain
== NULL_TREE
)
21663 tree max
= TYPE_MAX_VALUE (domain
);
21664 if (TREE_CODE (max
) != MINUS_EXPR
)
21667 return deducible_expression (TREE_OPERAND (max
, 0));
21670 /* Returns true iff the template arguments ARGS use a template parameter
21671 in a deducible way. */
21674 deducible_template_args (tree args
)
21676 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21679 tree elt
= TREE_VEC_ELT (args
, i
);
21680 if (ARGUMENT_PACK_P (elt
))
21681 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21684 if (PACK_EXPANSION_P (elt
))
21685 elt
= PACK_EXPANSION_PATTERN (elt
);
21686 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21688 else if (TYPE_P (elt
))
21689 deducible
= uses_deducible_template_parms (elt
);
21691 deducible
= deducible_expression (elt
);
21699 /* Returns true iff TYPE contains any deducible references to template
21700 parameters, as per 14.8.2.5. */
21703 uses_deducible_template_parms (tree type
)
21705 if (PACK_EXPANSION_P (type
))
21706 type
= PACK_EXPANSION_PATTERN (type
);
21713 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21714 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21720 if (INDIRECT_TYPE_P (type
))
21721 return uses_deducible_template_parms (TREE_TYPE (type
));
21723 /* T[integer-constant ]
21725 if (TREE_CODE (type
) == ARRAY_TYPE
)
21726 return (uses_deducible_template_parms (TREE_TYPE (type
))
21727 || deducible_array_bound (TYPE_DOMAIN (type
)));
21739 if (TYPE_PTRMEM_P (type
))
21740 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21741 || (uses_deducible_template_parms
21742 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21744 /* template-name <T> (where template-name refers to a class template)
21745 template-name <i> (where template-name refers to a class template) */
21746 if (CLASS_TYPE_P (type
)
21747 && CLASSTYPE_TEMPLATE_INFO (type
)
21748 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21749 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21750 (CLASSTYPE_TI_ARGS (type
)));
21755 if (FUNC_OR_METHOD_TYPE_P (type
))
21757 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21759 tree parm
= TYPE_ARG_TYPES (type
);
21760 if (TREE_CODE (type
) == METHOD_TYPE
)
21761 parm
= TREE_CHAIN (parm
);
21762 for (; parm
; parm
= TREE_CHAIN (parm
))
21763 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21770 /* Subroutine of type_unification_real and unify_pack_expansion to
21771 handle unification of a single P/A pair. Parameters are as
21772 for those functions. */
21775 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21776 int subr
, unification_kind_t strict
,
21779 tree arg_expr
= NULL_TREE
;
21782 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21783 return unify_invalid (explain_p
);
21784 if (arg
== unknown_type_node
)
21785 /* We can't deduce anything from this, but we might get all the
21786 template args from other function args. */
21787 return unify_success (explain_p
);
21789 /* Implicit conversions (Clause 4) will be performed on a function
21790 argument to convert it to the type of the corresponding function
21791 parameter if the parameter type contains no template-parameters that
21792 participate in template argument deduction. */
21793 if (strict
!= DEDUCE_EXACT
21794 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21795 /* For function parameters with no deducible template parameters,
21796 just return. We'll check non-dependent conversions later. */
21797 return unify_success (explain_p
);
21802 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21803 | UNIFY_ALLOW_MORE_CV_QUAL
21804 | UNIFY_ALLOW_DERIVED
);
21808 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21812 arg_strict
= UNIFY_ALLOW_NONE
;
21816 gcc_unreachable ();
21819 /* We only do these transformations if this is the top-level
21820 parameter_type_list in a call or declaration matching; in other
21821 situations (nested function declarators, template argument lists) we
21822 won't be comparing a type to an expression, and we don't do any type
21828 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21829 if (type_unknown_p (arg
))
21831 /* [temp.deduct.type] A template-argument can be
21832 deduced from a pointer to function or pointer
21833 to member function argument if the set of
21834 overloaded functions does not contain function
21835 templates and at most one of a set of
21836 overloaded functions provides a unique
21838 resolve_overloaded_unification (tparms
, targs
, parm
,
21840 arg_strict
, explain_p
);
21841 /* If a unique match was not found, this is a
21842 non-deduced context, so we still succeed. */
21843 return unify_success (explain_p
);
21847 arg
= unlowered_expr_type (arg
);
21848 if (arg
== error_mark_node
)
21849 return unify_invalid (explain_p
);
21853 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21856 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21857 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21858 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21860 /* For deduction from an init-list we need the actual list. */
21861 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21863 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21866 /* for_each_template_parm callback that always returns 0. */
21869 zero_r (tree
, void *)
21874 /* for_each_template_parm any_fn callback to handle deduction of a template
21875 type argument from the type of an array bound. */
21878 array_deduction_r (tree t
, void *data
)
21880 tree_pair_p d
= (tree_pair_p
)data
;
21881 tree
&tparms
= d
->purpose
;
21882 tree
&targs
= d
->value
;
21884 if (TREE_CODE (t
) == ARRAY_TYPE
)
21885 if (tree dom
= TYPE_DOMAIN (t
))
21886 if (tree max
= TYPE_MAX_VALUE (dom
))
21888 if (TREE_CODE (max
) == MINUS_EXPR
)
21889 max
= TREE_OPERAND (max
, 0);
21890 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21891 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21892 UNIFY_ALLOW_NONE
, /*explain*/false);
21895 /* Keep walking. */
21899 /* Try to deduce any not-yet-deduced template type arguments from the type of
21900 an array bound. This is handled separately from unify because 14.8.2.5 says
21901 "The type of a type parameter is only deduced from an array bound if it is
21902 not otherwise deduced." */
21905 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21907 tree_pair_s data
= { tparms
, targs
};
21908 hash_set
<tree
> visited
;
21909 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21910 /*nondeduced*/false, array_deduction_r
);
21913 /* Most parms like fn_type_unification.
21915 If SUBR is 1, we're being called recursively (to unify the
21916 arguments of a function or method parameter of a function
21919 CHECKS is a pointer to a vector of access checks encountered while
21920 substituting default template arguments. */
21923 type_unification_real (tree tparms
,
21927 unsigned int xnargs
,
21929 unification_kind_t strict
,
21930 vec
<deferred_access_check
, va_gc
> **checks
,
21935 int ntparms
= TREE_VEC_LENGTH (tparms
);
21936 int saw_undeduced
= 0;
21939 unsigned int nargs
;
21942 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21943 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21944 gcc_assert (ntparms
> 0);
21946 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21948 /* Reset the number of non-defaulted template arguments contained
21950 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21958 while (parms
&& parms
!= void_list_node
21961 parm
= TREE_VALUE (parms
);
21963 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21964 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21965 /* For a function parameter pack that occurs at the end of the
21966 parameter-declaration-list, the type A of each remaining
21967 argument of the call is compared with the type P of the
21968 declarator-id of the function parameter pack. */
21971 parms
= TREE_CHAIN (parms
);
21973 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21974 /* For a function parameter pack that does not occur at the
21975 end of the parameter-declaration-list, the type of the
21976 parameter pack is a non-deduced context. */
21982 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21988 && parms
!= void_list_node
21989 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21991 /* Unify the remaining arguments with the pack expansion type. */
21993 tree parmvec
= make_tree_vec (1);
21995 /* Allocate a TREE_VEC and copy in all of the arguments */
21996 argvec
= make_tree_vec (nargs
- ia
);
21997 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21998 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
22000 /* Copy the parameter into parmvec. */
22001 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
22002 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
22003 /*subr=*/subr
, explain_p
))
22006 /* Advance to the end of the list of parameters. */
22007 parms
= TREE_CHAIN (parms
);
22010 /* Fail if we've reached the end of the parm list, and more args
22011 are present, and the parm list isn't variadic. */
22012 if (ia
< nargs
&& parms
== void_list_node
)
22013 return unify_too_many_arguments (explain_p
, nargs
, ia
);
22014 /* Fail if parms are left and they don't have default values and
22015 they aren't all deduced as empty packs (c++/57397). This is
22016 consistent with sufficient_parms_p. */
22017 if (parms
&& parms
!= void_list_node
22018 && TREE_PURPOSE (parms
) == NULL_TREE
)
22020 unsigned int count
= nargs
;
22025 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
22028 p
= TREE_CHAIN (p
);
22030 while (p
&& p
!= void_list_node
);
22031 if (count
!= nargs
)
22032 return unify_too_few_arguments (explain_p
, ia
, count
,
22038 tsubst_flags_t complain
= (explain_p
22039 ? tf_warning_or_error
22041 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22043 for (i
= 0; i
< ntparms
; i
++)
22045 tree targ
= TREE_VEC_ELT (targs
, i
);
22046 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22048 /* Clear the "incomplete" flags on all argument packs now so that
22049 substituting them into later default arguments works. */
22050 if (targ
&& ARGUMENT_PACK_P (targ
))
22052 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22053 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22056 if (targ
|| tparm
== error_mark_node
)
22058 tparm
= TREE_VALUE (tparm
);
22060 if (TREE_CODE (tparm
) == TYPE_DECL
22061 && !tried_array_deduction
)
22063 try_array_deduction (tparms
, targs
, xparms
);
22064 tried_array_deduction
= true;
22065 if (TREE_VEC_ELT (targs
, i
))
22069 /* If this is an undeduced nontype parameter that depends on
22070 a type parameter, try another pass; its type may have been
22071 deduced from a later argument than the one from which
22072 this parameter can be deduced. */
22073 if (TREE_CODE (tparm
) == PARM_DECL
22074 && uses_template_parms (TREE_TYPE (tparm
))
22075 && saw_undeduced
< 2)
22081 /* Core issue #226 (C++0x) [temp.deduct]:
22083 If a template argument has not been deduced, its
22084 default template argument, if any, is used.
22086 When we are in C++98 mode, TREE_PURPOSE will either
22087 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22088 to explicitly check cxx_dialect here. */
22089 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22090 /* OK, there is a default argument. Wait until after the
22091 conversion check to do substitution. */
22094 /* If the type parameter is a parameter pack, then it will
22095 be deduced to an empty parameter pack. */
22096 if (template_parameter_pack_p (tparm
))
22100 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22102 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22103 TREE_CONSTANT (arg
) = 1;
22106 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22108 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22110 TREE_VEC_ELT (targs
, i
) = arg
;
22114 return unify_parameter_deduction_failure (explain_p
, tparm
);
22117 /* Now substitute into the default template arguments. */
22118 for (i
= 0; i
< ntparms
; i
++)
22120 tree targ
= TREE_VEC_ELT (targs
, i
);
22121 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22123 if (targ
|| tparm
== error_mark_node
)
22125 tree parm
= TREE_VALUE (tparm
);
22126 tree arg
= TREE_PURPOSE (tparm
);
22127 reopen_deferring_access_checks (*checks
);
22128 location_t save_loc
= input_location
;
22130 input_location
= DECL_SOURCE_LOCATION (parm
);
22132 if (saw_undeduced
== 1
22133 && TREE_CODE (parm
) == PARM_DECL
22134 && uses_template_parms (TREE_TYPE (parm
)))
22136 /* The type of this non-type parameter depends on undeduced
22137 parameters. Don't try to use its default argument yet,
22138 since we might deduce an argument for it on the next pass,
22139 but do check whether the arguments we already have cause
22140 substitution failure, so that that happens before we try
22141 later default arguments (78489). */
22142 ++processing_template_decl
;
22143 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22145 --processing_template_decl
;
22146 if (type
== error_mark_node
)
22147 arg
= error_mark_node
;
22153 /* Even if the call is happening in template context, getting
22154 here means it's non-dependent, and a default argument is
22155 considered a separate definition under [temp.decls], so we can
22156 do this substitution without processing_template_decl. This
22157 is important if the default argument contains something that
22158 might be instantiation-dependent like access (87480). */
22159 processing_template_decl_sentinel s
;
22160 tree substed
= NULL_TREE
;
22161 if (saw_undeduced
== 1)
22163 /* First instatiate in template context, in case we still
22164 depend on undeduced template parameters. */
22165 ++processing_template_decl
;
22166 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22168 --processing_template_decl
;
22169 if (substed
!= error_mark_node
22170 && !uses_template_parms (substed
))
22171 /* We replaced all the tparms, substitute again out of
22172 template context. */
22173 substed
= NULL_TREE
;
22176 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22179 if (!uses_template_parms (substed
))
22180 arg
= convert_template_argument (parm
, substed
, full_targs
,
22181 complain
, i
, NULL_TREE
);
22182 else if (saw_undeduced
== 1)
22185 arg
= error_mark_node
;
22188 input_location
= save_loc
;
22189 *checks
= get_deferred_access_checks ();
22190 pop_deferring_access_checks ();
22192 if (arg
== error_mark_node
)
22196 TREE_VEC_ELT (targs
, i
) = arg
;
22197 /* The position of the first default template argument,
22198 is also the number of non-defaulted arguments in TARGS.
22200 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22201 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22205 if (saw_undeduced
++ == 1)
22209 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22210 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22212 return unify_success (explain_p
);
22215 /* Subroutine of type_unification_real. Args are like the variables
22216 at the call site. ARG is an overloaded function (or template-id);
22217 we try deducing template args from each of the overloads, and if
22218 only one succeeds, we go with that. Modifies TARGS and returns
22219 true on success. */
22222 resolve_overloaded_unification (tree tparms
,
22226 unification_kind_t strict
,
22230 tree tempargs
= copy_node (targs
);
22232 tree goodfn
= NULL_TREE
;
22235 if (TREE_CODE (arg
) == ADDR_EXPR
)
22237 arg
= TREE_OPERAND (arg
, 0);
22243 if (TREE_CODE (arg
) == COMPONENT_REF
)
22244 /* Handle `&x' where `x' is some static or non-static member
22246 arg
= TREE_OPERAND (arg
, 1);
22248 if (TREE_CODE (arg
) == OFFSET_REF
)
22249 arg
= TREE_OPERAND (arg
, 1);
22251 /* Strip baselink information. */
22252 if (BASELINK_P (arg
))
22253 arg
= BASELINK_FUNCTIONS (arg
);
22255 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22257 /* If we got some explicit template args, we need to plug them into
22258 the affected templates before we try to unify, in case the
22259 explicit args will completely resolve the templates in question. */
22262 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22263 arg
= TREE_OPERAND (arg
, 0);
22265 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22268 tree subargs
, elem
;
22270 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22273 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22274 expl_subargs
, NULL_TREE
, tf_none
,
22275 /*require_all_args=*/true,
22276 /*use_default_args=*/true);
22277 if (subargs
!= error_mark_node
22278 && !any_dependent_template_arguments_p (subargs
))
22280 fn
= instantiate_template (fn
, subargs
, tf_none
);
22281 if (!constraints_satisfied_p (fn
))
22283 if (undeduced_auto_decl (fn
))
22285 /* Instantiate the function to deduce its return type. */
22287 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22291 elem
= TREE_TYPE (fn
);
22292 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22293 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22294 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22303 /* If no templates (or more than one) are fully resolved by the
22304 explicit arguments, this template-id is a non-deduced context; it
22305 could still be OK if we deduce all template arguments for the
22306 enclosing call through other arguments. */
22310 else if (!OVL_P (arg
))
22311 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22312 -- but the deduction does not succeed because the expression is
22313 not just the function on its own. */
22316 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22319 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22320 strict
, sub_strict
, addr_p
, explain_p
)
22321 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22328 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22329 to function or pointer to member function argument if the set of
22330 overloaded functions does not contain function templates and at most
22331 one of a set of overloaded functions provides a unique match.
22333 So if we found multiple possibilities, we return success but don't
22334 deduce anything. */
22338 int i
= TREE_VEC_LENGTH (targs
);
22340 if (TREE_VEC_ELT (tempargs
, i
))
22342 tree old
= TREE_VEC_ELT (targs
, i
);
22343 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22344 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22345 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22346 /* Don't forget explicit template arguments in a pack. */
22347 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22348 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22349 TREE_VEC_ELT (targs
, i
) = new_
;
22358 /* Core DR 115: In contexts where deduction is done and fails, or in
22359 contexts where deduction is not done, if a template argument list is
22360 specified and it, along with any default template arguments, identifies
22361 a single function template specialization, then the template-id is an
22362 lvalue for the function template specialization. */
22365 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22367 tree expr
, offset
, baselink
;
22370 if (!type_unknown_p (orig_expr
))
22375 offset
= NULL_TREE
;
22376 baselink
= NULL_TREE
;
22378 if (TREE_CODE (expr
) == ADDR_EXPR
)
22380 expr
= TREE_OPERAND (expr
, 0);
22383 if (TREE_CODE (expr
) == OFFSET_REF
)
22386 expr
= TREE_OPERAND (expr
, 1);
22388 if (BASELINK_P (expr
))
22391 expr
= BASELINK_FUNCTIONS (expr
);
22394 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22397 tree goodfn
= NULL_TREE
;
22399 /* If we got some explicit template args, we need to plug them into
22400 the affected templates before we try to unify, in case the
22401 explicit args will completely resolve the templates in question. */
22403 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22404 tree arg
= TREE_OPERAND (expr
, 0);
22405 tree badfn
= NULL_TREE
;
22406 tree badargs
= NULL_TREE
;
22408 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22411 tree subargs
, elem
;
22413 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22416 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22417 expl_subargs
, NULL_TREE
, tf_none
,
22418 /*require_all_args=*/true,
22419 /*use_default_args=*/true);
22420 if (subargs
!= error_mark_node
22421 && !any_dependent_template_arguments_p (subargs
))
22423 elem
= instantiate_template (fn
, subargs
, tf_none
);
22424 if (elem
== error_mark_node
)
22429 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22430 && constraints_satisfied_p (elem
))
22439 mark_used (goodfn
);
22442 expr
= build_baselink (BASELINK_BINFO (baselink
),
22443 BASELINK_ACCESS_BINFO (baselink
),
22444 expr
, BASELINK_OPTYPE (baselink
));
22448 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22449 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22452 expr
= cp_build_addr_expr (expr
, complain
);
22455 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22456 /* There were no good options and at least one bad one, so let the
22457 user know what the problem is. */
22458 instantiate_template (badfn
, badargs
, complain
);
22463 /* As above, but error out if the expression remains overloaded. */
22466 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22468 exp
= resolve_nondeduced_context (exp
, complain
);
22469 if (type_unknown_p (exp
))
22471 if (complain
& tf_error
)
22472 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22473 return error_mark_node
;
22478 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22479 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22480 different overloads deduce different arguments for a given parm.
22481 ADDR_P is true if the expression for which deduction is being
22482 performed was of the form "& fn" rather than simply "fn".
22484 Returns 1 on success. */
22487 try_one_overload (tree tparms
,
22492 unification_kind_t strict
,
22501 if (arg
== error_mark_node
)
22504 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22505 to function or pointer to member function argument if the set of
22506 overloaded functions does not contain function templates and at most
22507 one of a set of overloaded functions provides a unique match.
22509 So if this is a template, just return success. */
22511 if (uses_template_parms (arg
))
22514 if (TREE_CODE (arg
) == METHOD_TYPE
)
22515 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22517 arg
= build_pointer_type (arg
);
22519 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22521 /* We don't copy orig_targs for this because if we have already deduced
22522 some template args from previous args, unify would complain when we
22523 try to deduce a template parameter for the same argument, even though
22524 there isn't really a conflict. */
22525 nargs
= TREE_VEC_LENGTH (targs
);
22526 tempargs
= make_tree_vec (nargs
);
22528 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22531 /* First make sure we didn't deduce anything that conflicts with
22532 explicitly specified args. */
22533 for (i
= nargs
; i
--; )
22535 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22536 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22540 else if (uses_template_parms (elt
))
22541 /* Since we're unifying against ourselves, we will fill in
22542 template args used in the function parm list with our own
22543 template parms. Discard them. */
22544 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22545 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22547 /* Check that the argument at each index of the deduced argument pack
22548 is equivalent to the corresponding explicitly specified argument.
22549 We may have deduced more arguments than were explicitly specified,
22552 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22553 that's wrong if we deduce the same argument pack from multiple
22554 function arguments: it's only incomplete the first time. */
22556 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22557 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22559 if (TREE_VEC_LENGTH (deduced_pack
)
22560 < TREE_VEC_LENGTH (explicit_pack
))
22563 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22564 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22565 TREE_VEC_ELT (deduced_pack
, j
)))
22568 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22572 for (i
= nargs
; i
--; )
22574 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22577 TREE_VEC_ELT (targs
, i
) = elt
;
22583 /* PARM is a template class (perhaps with unbound template
22584 parameters). ARG is a fully instantiated type. If ARG can be
22585 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22586 TARGS are as for unify. */
22589 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22592 tree copy_of_targs
;
22594 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22596 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22597 /* Matches anything. */;
22598 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22599 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22602 /* We need to make a new template argument vector for the call to
22603 unify. If we used TARGS, we'd clutter it up with the result of
22604 the attempted unification, even if this class didn't work out.
22605 We also don't want to commit ourselves to all the unifications
22606 we've already done, since unification is supposed to be done on
22607 an argument-by-argument basis. In other words, consider the
22608 following pathological case:
22610 template <int I, int J, int K>
22613 template <int I, int J>
22614 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22616 template <int I, int J, int K>
22617 void f(S<I, J, K>, S<I, I, I>);
22626 Now, by the time we consider the unification involving `s2', we
22627 already know that we must have `f<0, 0, 0>'. But, even though
22628 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22629 because there are two ways to unify base classes of S<0, 1, 2>
22630 with S<I, I, I>. If we kept the already deduced knowledge, we
22631 would reject the possibility I=1. */
22632 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22634 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22636 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22641 /* If unification failed, we're done. */
22642 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22643 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22649 /* Given a template type PARM and a class type ARG, find the unique
22650 base type in ARG that is an instance of PARM. We do not examine
22651 ARG itself; only its base-classes. If there is not exactly one
22652 appropriate base class, return NULL_TREE. PARM may be the type of
22653 a partial specialization, as well as a plain template type. Used
22656 static enum template_base_result
22657 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22658 bool explain_p
, tree
*result
)
22660 tree rval
= NULL_TREE
;
22663 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22665 binfo
= TYPE_BINFO (complete_type (arg
));
22668 /* The type could not be completed. */
22669 *result
= NULL_TREE
;
22670 return tbr_incomplete_type
;
22673 /* Walk in inheritance graph order. The search order is not
22674 important, and this avoids multiple walks of virtual bases. */
22675 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22677 tree r
= try_class_unification (tparms
, targs
, parm
,
22678 BINFO_TYPE (binfo
), explain_p
);
22682 /* If there is more than one satisfactory baseclass, then:
22686 If they yield more than one possible deduced A, the type
22690 if (rval
&& !same_type_p (r
, rval
))
22692 /* [temp.deduct.call]/4.3: If there is a class C that is a
22693 (direct or indirect) base class of D and derived (directly or
22694 indirectly) from a class B and that would be a valid deduced
22695 A, the deduced A cannot be B or pointer to B, respectively. */
22696 if (DERIVED_FROM_P (r
, rval
))
22699 else if (DERIVED_FROM_P (rval
, r
))
22700 /* Ignore rval. */;
22703 *result
= NULL_TREE
;
22704 return tbr_ambiguous_baseclass
;
22713 return tbr_success
;
22716 /* Returns the level of DECL, which declares a template parameter. */
22719 template_decl_level (tree decl
)
22721 switch (TREE_CODE (decl
))
22724 case TEMPLATE_DECL
:
22725 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22728 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22731 gcc_unreachable ();
22736 /* Decide whether ARG can be unified with PARM, considering only the
22737 cv-qualifiers of each type, given STRICT as documented for unify.
22738 Returns nonzero iff the unification is OK on that basis. */
22741 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22743 int arg_quals
= cp_type_quals (arg
);
22744 int parm_quals
= cp_type_quals (parm
);
22746 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22747 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22749 /* Although a CVR qualifier is ignored when being applied to a
22750 substituted template parameter ([8.3.2]/1 for example), that
22751 does not allow us to unify "const T" with "int&" because both
22752 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22753 It is ok when we're allowing additional CV qualifiers
22754 at the outer level [14.8.2.1]/3,1st bullet. */
22755 if ((TYPE_REF_P (arg
)
22756 || FUNC_OR_METHOD_TYPE_P (arg
))
22757 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22760 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22761 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22765 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22766 && (arg_quals
& parm_quals
) != parm_quals
)
22769 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22770 && (parm_quals
& arg_quals
) != arg_quals
)
22776 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22778 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22780 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22781 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22782 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22784 *index
= TEMPLATE_TYPE_IDX (parm
);
22785 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22789 *index
= TEMPLATE_PARM_IDX (parm
);
22790 *level
= TEMPLATE_PARM_LEVEL (parm
);
22794 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22796 if (unify (TP, TA, P, A, S, EP)) \
22800 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22801 expansion at the end of PACKED_PARMS. Returns 0 if the type
22802 deduction succeeds, 1 otherwise. STRICT is the same as in
22803 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22804 function call argument list. We'll need to adjust the arguments to make them
22805 types. SUBR tells us if this is from a recursive call to
22806 type_unification_real, or for comparing two template argument
22810 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22811 tree packed_args
, unification_kind_t strict
,
22812 bool subr
, bool explain_p
)
22815 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22816 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22817 tree pack
, packs
= NULL_TREE
;
22818 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22820 /* Add in any args remembered from an earlier partial instantiation. */
22821 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22822 int levels
= TMPL_ARGS_DEPTH (targs
);
22824 packed_args
= expand_template_argument_pack (packed_args
);
22826 int len
= TREE_VEC_LENGTH (packed_args
);
22828 /* Determine the parameter packs we will be deducing from the
22829 pattern, and record their current deductions. */
22830 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22831 pack
; pack
= TREE_CHAIN (pack
))
22833 tree parm_pack
= TREE_VALUE (pack
);
22836 /* Only template parameter packs can be deduced, not e.g. function
22837 parameter packs or __bases or __integer_pack. */
22838 if (!TEMPLATE_PARM_P (parm_pack
))
22841 /* Determine the index and level of this parameter pack. */
22842 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22843 if (level
< levels
)
22846 /* Keep track of the parameter packs and their corresponding
22848 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22849 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22852 /* Loop through all of the arguments that have not yet been
22853 unified and unify each with the pattern. */
22854 for (i
= start
; i
< len
; i
++)
22857 bool any_explicit
= false;
22858 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22860 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22861 or the element of its argument pack at the current index if
22862 this argument was explicitly specified. */
22863 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22867 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22870 if (TREE_VALUE (pack
)
22871 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22872 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22874 any_explicit
= true;
22875 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22877 TMPL_ARG (targs
, level
, idx
) = arg
;
22880 /* If we had explicit template arguments, substitute them into the
22881 pattern before deduction. */
22884 /* Some arguments might still be unspecified or dependent. */
22886 ++processing_template_decl
;
22887 dependent
= any_dependent_template_arguments_p (targs
);
22889 --processing_template_decl
;
22890 parm
= tsubst (pattern
, targs
,
22891 explain_p
? tf_warning_or_error
: tf_none
,
22894 --processing_template_decl
;
22895 if (parm
== error_mark_node
)
22901 /* Unify the pattern with the current argument. */
22902 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22906 /* For each parameter pack, collect the deduced value. */
22907 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22910 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22912 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22913 TMPL_ARG (targs
, level
, idx
);
22917 /* Verify that the results of unification with the parameter packs
22918 produce results consistent with what we've seen before, and make
22919 the deduced argument packs available. */
22920 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22922 tree old_pack
= TREE_VALUE (pack
);
22923 tree new_args
= TREE_TYPE (pack
);
22924 int i
, len
= TREE_VEC_LENGTH (new_args
);
22926 bool nondeduced_p
= false;
22928 /* By default keep the original deduced argument pack.
22929 If necessary, more specific code is going to update the
22930 resulting deduced argument later down in this function. */
22931 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22932 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22934 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22935 actually deduce anything. */
22936 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22937 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22938 nondeduced_p
= true;
22942 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22944 /* If we had fewer function args than explicit template args,
22945 just use the explicits. */
22946 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22947 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22948 if (len
< explicit_len
)
22949 new_args
= explicit_args
;
22955 /* Build the deduced *_ARGUMENT_PACK. */
22956 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22958 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22959 TREE_CONSTANT (result
) = 1;
22962 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22964 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22966 /* Note the deduced argument packs for this parameter
22968 TMPL_ARG (targs
, level
, idx
) = result
;
22970 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22971 && (ARGUMENT_PACK_ARGS (old_pack
)
22972 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22974 /* We only had the explicitly-provided arguments before, but
22975 now we have a complete set of arguments. */
22976 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22978 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22979 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22980 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22984 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22985 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22986 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22987 /* During template argument deduction for the aggregate deduction
22988 candidate, the number of elements in a trailing parameter pack
22989 is only deduced from the number of remaining function
22990 arguments if it is not otherwise deduced. */
22991 if (cxx_dialect
>= cxx20
22992 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
22993 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
22994 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
22995 if (!comp_template_args (old_args
, new_args
,
22996 &bad_old_arg
, &bad_new_arg
))
22997 /* Inconsistent unification of this parameter pack. */
22998 return unify_parameter_pack_inconsistent (explain_p
,
23004 return unify_success (explain_p
);
23007 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23008 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23009 parameters and return value are as for unify. */
23012 unify_array_domain (tree tparms
, tree targs
,
23013 tree parm_dom
, tree arg_dom
,
23021 /* Our representation of array types uses "N - 1" as the
23022 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23023 not an integer constant. We cannot unify arbitrarily
23024 complex expressions, so we eliminate the MINUS_EXPRs
23026 parm_max
= TYPE_MAX_VALUE (parm_dom
);
23027 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
23030 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
23031 parm_max
= TREE_OPERAND (parm_max
, 0);
23033 arg_max
= TYPE_MAX_VALUE (arg_dom
);
23034 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
23037 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23038 trying to unify the type of a variable with the type
23039 of a template parameter. For example:
23041 template <unsigned int N>
23042 void f (char (&) [N]);
23049 Here, the type of the ARG will be "int [g(i)]", and
23050 may be a SAVE_EXPR, etc. */
23051 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
23052 return unify_vla_arg (explain_p
, arg_dom
);
23053 arg_max
= TREE_OPERAND (arg_max
, 0);
23056 /* If only one of the bounds used a MINUS_EXPR, compensate
23057 by adding one to the other bound. */
23058 if (parm_cst
&& !arg_cst
)
23059 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23063 else if (arg_cst
&& !parm_cst
)
23064 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23069 return unify (tparms
, targs
, parm_max
, arg_max
,
23070 UNIFY_ALLOW_INTEGER
, explain_p
);
23073 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23075 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23080 if (PACK_EXPANSION_P (t
))
23081 t
= PACK_EXPANSION_PATTERN (t
);
23082 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23083 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23084 || DECL_TYPE_TEMPLATE_P (t
))
23086 else if (TYPE_P (t
))
23092 /* Deduce the value of template parameters. TPARMS is the (innermost)
23093 set of template parameters to a template. TARGS is the bindings
23094 for those template parameters, as determined thus far; TARGS may
23095 include template arguments for outer levels of template parameters
23096 as well. PARM is a parameter to a template function, or a
23097 subcomponent of that parameter; ARG is the corresponding argument.
23098 This function attempts to match PARM with ARG in a manner
23099 consistent with the existing assignments in TARGS. If more values
23100 are deduced, then TARGS is updated.
23102 Returns 0 if the type deduction succeeds, 1 otherwise. The
23103 parameter STRICT is a bitwise or of the following flags:
23106 Require an exact match between PARM and ARG.
23107 UNIFY_ALLOW_MORE_CV_QUAL:
23108 Allow the deduced ARG to be more cv-qualified (by qualification
23109 conversion) than ARG.
23110 UNIFY_ALLOW_LESS_CV_QUAL:
23111 Allow the deduced ARG to be less cv-qualified than ARG.
23112 UNIFY_ALLOW_DERIVED:
23113 Allow the deduced ARG to be a template base class of ARG,
23114 or a pointer to a template base class of the type pointed to by
23116 UNIFY_ALLOW_INTEGER:
23117 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23118 case for more information.
23119 UNIFY_ALLOW_OUTER_LEVEL:
23120 This is the outermost level of a deduction. Used to determine validity
23121 of qualification conversions. A valid qualification conversion must
23122 have const qualified pointers leading up to the inner type which
23123 requires additional CV quals, except at the outer level, where const
23124 is not required [conv.qual]. It would be normal to set this flag in
23125 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23126 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23127 This is the outermost level of a deduction, and PARM can be more CV
23128 qualified at this point.
23129 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23130 This is the outermost level of a deduction, and PARM can be less CV
23131 qualified at this point. */
23134 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23140 int strict_in
= strict
;
23141 tsubst_flags_t complain
= (explain_p
23142 ? tf_warning_or_error
23145 /* I don't think this will do the right thing with respect to types.
23146 But the only case I've seen it in so far has been array bounds, where
23147 signedness is the only information lost, and I think that will be
23148 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23149 finish_id_expression_1, and are also OK. */
23150 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23151 parm
= TREE_OPERAND (parm
, 0);
23153 if (arg
== error_mark_node
)
23154 return unify_invalid (explain_p
);
23155 if (arg
== unknown_type_node
23156 || arg
== init_list_type_node
)
23157 /* We can't deduce anything from this, but we might get all the
23158 template args from other function args. */
23159 return unify_success (explain_p
);
23161 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23162 return unify_success (explain_p
);
23164 /* If PARM uses template parameters, then we can't bail out here,
23165 even if ARG == PARM, since we won't record unifications for the
23166 template parameters. We might need them if we're trying to
23167 figure out which of two things is more specialized. */
23168 if (arg
== parm
&& !uses_template_parms (parm
))
23169 return unify_success (explain_p
);
23171 /* Handle init lists early, so the rest of the function can assume
23172 we're dealing with a type. */
23173 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23177 tree orig_parm
= parm
;
23179 if (!is_std_init_list (parm
)
23180 && TREE_CODE (parm
) != ARRAY_TYPE
)
23181 /* We can only deduce from an initializer list argument if the
23182 parameter is std::initializer_list or an array; otherwise this
23183 is a non-deduced context. */
23184 return unify_success (explain_p
);
23186 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23187 elttype
= TREE_TYPE (parm
);
23190 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23191 /* Deduction is defined in terms of a single type, so just punt
23192 on the (bizarre) std::initializer_list<T...>. */
23193 if (PACK_EXPANSION_P (elttype
))
23194 return unify_success (explain_p
);
23197 if (strict
!= DEDUCE_EXACT
23198 && TYPE_P (elttype
)
23199 && !uses_deducible_template_parms (elttype
))
23200 /* If ELTTYPE has no deducible template parms, skip deduction from
23201 the list elements. */;
23203 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23205 int elt_strict
= strict
;
23207 if (elt
== error_mark_node
)
23208 return unify_invalid (explain_p
);
23210 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23212 tree type
= TREE_TYPE (elt
);
23213 if (type
== error_mark_node
)
23214 return unify_invalid (explain_p
);
23215 /* It should only be possible to get here for a call. */
23216 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23217 elt_strict
|= maybe_adjust_types_for_deduction
23218 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23222 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23226 if (TREE_CODE (parm
) == ARRAY_TYPE
23227 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23229 /* Also deduce from the length of the initializer list. */
23230 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23231 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23232 if (idx
== error_mark_node
)
23233 return unify_invalid (explain_p
);
23234 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23238 /* If the std::initializer_list<T> deduction worked, replace the
23239 deduced A with std::initializer_list<A>. */
23240 if (orig_parm
!= parm
)
23242 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23243 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23244 targ
= listify (targ
);
23245 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23247 return unify_success (explain_p
);
23250 /* If parm and arg aren't the same kind of thing (template, type, or
23251 expression), fail early. */
23252 if (pa_kind (parm
) != pa_kind (arg
))
23253 return unify_invalid (explain_p
);
23255 /* Immediately reject some pairs that won't unify because of
23256 cv-qualification mismatches. */
23257 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23259 /* It is the elements of the array which hold the cv quals of an array
23260 type, and the elements might be template type parms. We'll check
23261 when we recurse. */
23262 && TREE_CODE (arg
) != ARRAY_TYPE
23263 /* We check the cv-qualifiers when unifying with template type
23264 parameters below. We want to allow ARG `const T' to unify with
23265 PARM `T' for example, when computing which of two templates
23266 is more specialized, for example. */
23267 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23268 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23269 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23271 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23272 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23273 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23274 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23275 strict
&= ~UNIFY_ALLOW_DERIVED
;
23276 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23277 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23279 switch (TREE_CODE (parm
))
23281 case TYPENAME_TYPE
:
23283 case UNBOUND_CLASS_TEMPLATE
:
23284 /* In a type which contains a nested-name-specifier, template
23285 argument values cannot be deduced for template parameters used
23286 within the nested-name-specifier. */
23287 return unify_success (explain_p
);
23289 case TEMPLATE_TYPE_PARM
:
23290 case TEMPLATE_TEMPLATE_PARM
:
23291 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23292 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23293 if (error_operand_p (tparm
))
23294 return unify_invalid (explain_p
);
23296 if (TEMPLATE_TYPE_LEVEL (parm
)
23297 != template_decl_level (tparm
))
23298 /* The PARM is not one we're trying to unify. Just check
23299 to see if it matches ARG. */
23301 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23302 && (is_auto (parm
) ? is_auto (arg
)
23303 : same_type_p (parm
, arg
)))
23304 return unify_success (explain_p
);
23306 return unify_type_mismatch (explain_p
, parm
, arg
);
23308 idx
= TEMPLATE_TYPE_IDX (parm
);
23309 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23310 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23311 if (error_operand_p (tparm
))
23312 return unify_invalid (explain_p
);
23314 /* Check for mixed types and values. */
23315 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23316 && TREE_CODE (tparm
) != TYPE_DECL
)
23317 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23318 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23319 gcc_unreachable ();
23321 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23323 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23324 && CLASS_TYPE_P (arg
))
23326 /* First try to match ARG directly. */
23327 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23331 /* Otherwise, look for a suitable base of ARG, as below. */
23332 enum template_base_result r
;
23333 r
= get_template_base (tparms
, targs
, parm
, arg
,
23336 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23340 /* ARG must be constructed from a template class or a template
23341 template parameter. */
23342 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23343 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23344 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23346 /* Deduce arguments T, i from TT<T> or TT<i>. */
23347 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23350 arg
= TYPE_TI_TEMPLATE (arg
);
23352 /* Fall through to deduce template name. */
23355 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23356 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23358 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23360 /* Simple cases: Value already set, does match or doesn't. */
23361 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23362 return unify_success (explain_p
);
23364 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23368 /* If PARM is `const T' and ARG is only `int', we don't have
23369 a match unless we are allowing additional qualification.
23370 If ARG is `const int' and PARM is just `T' that's OK;
23371 that binds `const int' to `T'. */
23372 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23374 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23376 /* Consider the case where ARG is `const volatile int' and
23377 PARM is `const T'. Then, T should be `volatile int'. */
23378 arg
= cp_build_qualified_type_real
23379 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23380 if (arg
== error_mark_node
)
23381 return unify_invalid (explain_p
);
23383 /* Simple cases: Value already set, does match or doesn't. */
23384 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23385 return unify_success (explain_p
);
23387 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23389 /* Make sure that ARG is not a variable-sized array. (Note
23390 that were talking about variable-sized arrays (like
23391 `int[n]'), rather than arrays of unknown size (like
23392 `int[]').) We'll get very confused by such a type since
23393 the bound of the array is not constant, and therefore
23394 not mangleable. Besides, such types are not allowed in
23395 ISO C++, so we can do as we please here. We do allow
23396 them for 'auto' deduction, since that isn't ABI-exposed. */
23397 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23398 return unify_vla_arg (explain_p
, arg
);
23400 /* Strip typedefs as in convert_template_argument. */
23401 arg
= canonicalize_type_argument (arg
, tf_none
);
23404 /* If ARG is a parameter pack or an expansion, we cannot unify
23405 against it unless PARM is also a parameter pack. */
23406 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23407 && !template_parameter_pack_p (parm
))
23408 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23410 /* If the argument deduction results is a METHOD_TYPE,
23411 then there is a problem.
23412 METHOD_TYPE doesn't map to any real C++ type the result of
23413 the deduction cannot be of that type. */
23414 if (TREE_CODE (arg
) == METHOD_TYPE
)
23415 return unify_method_type_error (explain_p
, arg
);
23417 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23418 return unify_success (explain_p
);
23420 case TEMPLATE_PARM_INDEX
:
23421 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23422 if (error_operand_p (tparm
))
23423 return unify_invalid (explain_p
);
23425 if (TEMPLATE_PARM_LEVEL (parm
)
23426 != template_decl_level (tparm
))
23428 /* The PARM is not one we're trying to unify. Just check
23429 to see if it matches ARG. */
23430 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23431 && cp_tree_equal (parm
, arg
));
23433 unify_expression_unequal (explain_p
, parm
, arg
);
23437 idx
= TEMPLATE_PARM_IDX (parm
);
23438 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23442 if ((strict
& UNIFY_ALLOW_INTEGER
)
23443 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23444 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23445 /* We're deducing from an array bound, the type doesn't matter. */
23446 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23447 int x
= !cp_tree_equal (targ
, arg
);
23449 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23453 /* [temp.deduct.type] If, in the declaration of a function template
23454 with a non-type template-parameter, the non-type
23455 template-parameter is used in an expression in the function
23456 parameter-list and, if the corresponding template-argument is
23457 deduced, the template-argument type shall match the type of the
23458 template-parameter exactly, except that a template-argument
23459 deduced from an array bound may be of any integral type.
23460 The non-type parameter might use already deduced type parameters. */
23461 tparm
= TREE_TYPE (parm
);
23462 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23463 /* We don't have enough levels of args to do any substitution. This
23464 can happen in the context of -fnew-ttp-matching. */;
23467 ++processing_template_decl
;
23468 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23469 --processing_template_decl
;
23471 if (tree a
= type_uses_auto (tparm
))
23473 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23474 if (tparm
== error_mark_node
)
23479 if (!TREE_TYPE (arg
))
23480 /* Template-parameter dependent expression. Just accept it for now.
23481 It will later be processed in convert_template_argument. */
23483 else if (same_type_ignoring_top_level_qualifiers_p
23484 (non_reference (TREE_TYPE (arg
)),
23485 non_reference (tparm
)))
23486 /* OK. Ignore top-level quals here because a class-type template
23487 parameter object is const. */;
23488 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23489 && CP_INTEGRAL_TYPE_P (tparm
))
23490 /* Convert the ARG to the type of PARM; the deduced non-type
23491 template argument must exactly match the types of the
23492 corresponding parameter. */
23493 arg
= fold (build_nop (tparm
, arg
));
23494 else if (uses_template_parms (tparm
))
23496 /* We haven't deduced the type of this parameter yet. */
23497 if (cxx_dialect
>= cxx17
23498 /* We deduce from array bounds in try_array_deduction. */
23499 && !(strict
& UNIFY_ALLOW_INTEGER
))
23501 /* Deduce it from the non-type argument. */
23502 tree atype
= TREE_TYPE (arg
);
23503 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23505 UNIFY_ALLOW_NONE
, explain_p
);
23508 /* Try again later. */
23509 return unify_success (explain_p
);
23512 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23514 /* If ARG is a parameter pack or an expansion, we cannot unify
23515 against it unless PARM is also a parameter pack. */
23516 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23517 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23518 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23521 bool removed_attr
= false;
23522 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23524 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23525 return unify_success (explain_p
);
23529 /* A pointer-to-member constant can be unified only with
23530 another constant. */
23531 if (TREE_CODE (arg
) != PTRMEM_CST
)
23532 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23534 /* Just unify the class member. It would be useless (and possibly
23535 wrong, depending on the strict flags) to unify also
23536 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23537 arg refer to the same variable, even if through different
23538 classes. For instance:
23540 struct A { int x; };
23543 Unification of &A::x and &B::x must succeed. */
23544 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23545 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23550 if (!TYPE_PTR_P (arg
))
23551 return unify_type_mismatch (explain_p
, parm
, arg
);
23553 /* [temp.deduct.call]
23555 A can be another pointer or pointer to member type that can
23556 be converted to the deduced A via a qualification
23557 conversion (_conv.qual_).
23559 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23560 This will allow for additional cv-qualification of the
23561 pointed-to types if appropriate. */
23563 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23564 /* The derived-to-base conversion only persists through one
23565 level of pointers. */
23566 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23568 return unify (tparms
, targs
, TREE_TYPE (parm
),
23569 TREE_TYPE (arg
), strict
, explain_p
);
23572 case REFERENCE_TYPE
:
23573 if (!TYPE_REF_P (arg
))
23574 return unify_type_mismatch (explain_p
, parm
, arg
);
23575 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23576 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23579 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23580 return unify_type_mismatch (explain_p
, parm
, arg
);
23581 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23582 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23583 return unify_type_mismatch (explain_p
, parm
, arg
);
23584 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23585 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23586 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23587 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23588 TYPE_DOMAIN (arg
), explain_p
);
23589 return unify_success (explain_p
);
23596 case ENUMERAL_TYPE
:
23599 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23600 return unify_type_mismatch (explain_p
, parm
, arg
);
23602 /* We have already checked cv-qualification at the top of the
23604 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23605 return unify_type_mismatch (explain_p
, parm
, arg
);
23607 /* As far as unification is concerned, this wins. Later checks
23608 will invalidate it if necessary. */
23609 return unify_success (explain_p
);
23611 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23612 /* Type INTEGER_CST can come from ordinary constant template args. */
23614 while (CONVERT_EXPR_P (arg
))
23615 arg
= TREE_OPERAND (arg
, 0);
23617 if (TREE_CODE (arg
) != INTEGER_CST
)
23618 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23619 return (tree_int_cst_equal (parm
, arg
)
23620 ? unify_success (explain_p
)
23621 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23625 int i
, len
, argslen
;
23626 int parm_variadic_p
= 0;
23628 if (TREE_CODE (arg
) != TREE_VEC
)
23629 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23631 len
= TREE_VEC_LENGTH (parm
);
23632 argslen
= TREE_VEC_LENGTH (arg
);
23634 /* Check for pack expansions in the parameters. */
23635 for (i
= 0; i
< len
; ++i
)
23637 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23640 /* We can unify against something with a trailing
23642 parm_variadic_p
= 1;
23644 /* [temp.deduct.type]/9: If the template argument list of
23645 P contains a pack expansion that is not the last
23646 template argument, the entire template argument list
23647 is a non-deduced context. */
23648 return unify_success (explain_p
);
23652 /* If we don't have enough arguments to satisfy the parameters
23653 (not counting the pack expression at the end), or we have
23654 too many arguments for a parameter list that doesn't end in
23655 a pack expression, we can't unify. */
23656 if (parm_variadic_p
23657 ? argslen
< len
- parm_variadic_p
23659 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23661 /* Unify all of the parameters that precede the (optional)
23662 pack expression. */
23663 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23665 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23666 TREE_VEC_ELT (parm
, i
),
23667 TREE_VEC_ELT (arg
, i
),
23668 UNIFY_ALLOW_NONE
, explain_p
);
23670 if (parm_variadic_p
)
23671 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23673 /*subr=*/true, explain_p
);
23674 return unify_success (explain_p
);
23679 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23680 return unify_type_mismatch (explain_p
, parm
, arg
);
23682 if (TYPE_PTRMEMFUNC_P (parm
))
23684 if (!TYPE_PTRMEMFUNC_P (arg
))
23685 return unify_type_mismatch (explain_p
, parm
, arg
);
23687 return unify (tparms
, targs
,
23688 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23689 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23690 strict
, explain_p
);
23692 else if (TYPE_PTRMEMFUNC_P (arg
))
23693 return unify_type_mismatch (explain_p
, parm
, arg
);
23695 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23697 tree t
= NULL_TREE
;
23699 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23701 /* First, we try to unify the PARM and ARG directly. */
23702 t
= try_class_unification (tparms
, targs
,
23703 parm
, arg
, explain_p
);
23707 /* Fallback to the special case allowed in
23708 [temp.deduct.call]:
23710 If P is a class, and P has the form
23711 template-id, then A can be a derived class of
23712 the deduced A. Likewise, if P is a pointer to
23713 a class of the form template-id, A can be a
23714 pointer to a derived class pointed to by the
23716 enum template_base_result r
;
23717 r
= get_template_base (tparms
, targs
, parm
, arg
,
23722 /* Don't give the derived diagnostic if we're
23723 already dealing with the same template. */
23725 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23726 && (CLASSTYPE_TI_TEMPLATE (parm
)
23727 == CLASSTYPE_TI_TEMPLATE (arg
)));
23728 return unify_no_common_base (explain_p
&& !same_template
,
23733 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23734 && (CLASSTYPE_TI_TEMPLATE (parm
)
23735 == CLASSTYPE_TI_TEMPLATE (arg
)))
23736 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23737 Then, we should unify `int' and `U'. */
23740 /* There's no chance of unification succeeding. */
23741 return unify_type_mismatch (explain_p
, parm
, arg
);
23743 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23744 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23746 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23747 return unify_type_mismatch (explain_p
, parm
, arg
);
23748 return unify_success (explain_p
);
23751 case FUNCTION_TYPE
:
23753 unsigned int nargs
;
23758 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23759 return unify_type_mismatch (explain_p
, parm
, arg
);
23761 /* CV qualifications for methods can never be deduced, they must
23762 match exactly. We need to check them explicitly here,
23763 because type_unification_real treats them as any other
23764 cv-qualified parameter. */
23765 if (TREE_CODE (parm
) == METHOD_TYPE
23766 && (!check_cv_quals_for_unify
23768 class_of_this_parm (arg
),
23769 class_of_this_parm (parm
))))
23770 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23771 if (TREE_CODE (arg
) == FUNCTION_TYPE
23772 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23773 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23774 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23775 return unify_type_mismatch (explain_p
, parm
, arg
);
23777 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23778 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23780 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23781 args
= XALLOCAVEC (tree
, nargs
);
23782 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23783 a
!= NULL_TREE
&& a
!= void_list_node
;
23784 a
= TREE_CHAIN (a
), ++i
)
23785 args
[i
] = TREE_VALUE (a
);
23788 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23789 args
, nargs
, 1, DEDUCE_EXACT
,
23793 if (flag_noexcept_type
)
23795 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23796 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23797 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23798 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23799 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23800 && uses_template_parms (TREE_PURPOSE (pspec
)))
23801 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23802 TREE_PURPOSE (aspec
),
23803 UNIFY_ALLOW_NONE
, explain_p
);
23804 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23805 return unify_type_mismatch (explain_p
, parm
, arg
);
23812 /* Unify a pointer to member with a pointer to member function, which
23813 deduces the type of the member as a function type. */
23814 if (TYPE_PTRMEMFUNC_P (arg
))
23816 /* Check top-level cv qualifiers */
23817 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23818 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23820 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23821 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23822 UNIFY_ALLOW_NONE
, explain_p
);
23824 /* Determine the type of the function we are unifying against. */
23825 tree fntype
= static_fn_type (arg
);
23827 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23830 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23831 return unify_type_mismatch (explain_p
, parm
, arg
);
23832 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23833 TYPE_OFFSET_BASETYPE (arg
),
23834 UNIFY_ALLOW_NONE
, explain_p
);
23835 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23836 strict
, explain_p
);
23839 if (DECL_TEMPLATE_PARM_P (parm
))
23840 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23841 if (arg
!= scalar_constant_value (parm
))
23842 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23843 return unify_success (explain_p
);
23846 case TEMPLATE_DECL
:
23847 /* Matched cases are handled by the ARG == PARM test above. */
23848 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23851 /* We might get a variable as a non-type template argument in parm if the
23852 corresponding parameter is type-dependent. Make any necessary
23853 adjustments based on whether arg is a reference. */
23854 if (CONSTANT_CLASS_P (arg
))
23855 parm
= fold_non_dependent_expr (parm
, complain
);
23856 else if (REFERENCE_REF_P (arg
))
23858 tree sub
= TREE_OPERAND (arg
, 0);
23860 if (TREE_CODE (sub
) == ADDR_EXPR
)
23861 arg
= TREE_OPERAND (sub
, 0);
23863 /* Now use the normal expression code to check whether they match. */
23866 case TYPE_ARGUMENT_PACK
:
23867 case NONTYPE_ARGUMENT_PACK
:
23868 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23869 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23872 case DECLTYPE_TYPE
:
23873 case UNDERLYING_TYPE
:
23874 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23875 or UNDERLYING_TYPE nodes. */
23876 return unify_success (explain_p
);
23879 /* Unification fails if we hit an error node. */
23880 return unify_invalid (explain_p
);
23883 if (REFERENCE_REF_P (parm
))
23885 bool pexp
= PACK_EXPANSION_P (arg
);
23887 arg
= PACK_EXPANSION_PATTERN (arg
);
23888 if (REFERENCE_REF_P (arg
))
23889 arg
= TREE_OPERAND (arg
, 0);
23891 arg
= make_pack_expansion (arg
, complain
);
23892 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23893 strict
, explain_p
);
23898 /* An unresolved overload is a nondeduced context. */
23899 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23900 return unify_success (explain_p
);
23901 gcc_assert (EXPR_P (parm
)
23902 || COMPOUND_LITERAL_P (parm
)
23903 || TREE_CODE (parm
) == TRAIT_EXPR
);
23905 /* We must be looking at an expression. This can happen with
23909 void foo(S<I>, S<I + 2>);
23913 template<typename T>
23914 void foo(A<T, T{}>);
23916 This is a "non-deduced context":
23920 The non-deduced contexts are:
23922 --A non-type template argument or an array bound in which
23923 a subexpression references a template parameter.
23925 In these cases, we assume deduction succeeded, but don't
23926 actually infer any unifications. */
23928 if (!uses_template_parms (parm
)
23929 && !template_args_equal (parm
, arg
))
23930 return unify_expression_unequal (explain_p
, parm
, arg
);
23932 return unify_success (explain_p
);
23935 #undef RECUR_AND_CHECK_FAILURE
23937 /* Note that DECL can be defined in this translation unit, if
23941 mark_definable (tree decl
)
23944 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23945 FOR_EACH_CLONE (clone
, decl
)
23946 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23949 /* Called if RESULT is explicitly instantiated, or is a member of an
23950 explicitly instantiated class. */
23953 mark_decl_instantiated (tree result
, int extern_p
)
23955 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23957 /* If this entity has already been written out, it's too late to
23958 make any modifications. */
23959 if (TREE_ASM_WRITTEN (result
))
23962 /* For anonymous namespace we don't need to do anything. */
23963 if (decl_anon_ns_mem_p (result
))
23965 gcc_assert (!TREE_PUBLIC (result
));
23969 if (TREE_CODE (result
) != FUNCTION_DECL
)
23970 /* The TREE_PUBLIC flag for function declarations will have been
23971 set correctly by tsubst. */
23972 TREE_PUBLIC (result
) = 1;
23974 /* This might have been set by an earlier implicit instantiation. */
23975 DECL_COMDAT (result
) = 0;
23978 DECL_NOT_REALLY_EXTERN (result
) = 0;
23981 mark_definable (result
);
23982 mark_needed (result
);
23983 /* Always make artificials weak. */
23984 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23985 comdat_linkage (result
);
23986 /* For WIN32 we also want to put explicit instantiations in
23987 linkonce sections. */
23988 else if (TREE_PUBLIC (result
))
23989 maybe_make_one_only (result
);
23990 if (TREE_CODE (result
) == FUNCTION_DECL
23991 && DECL_TEMPLATE_INSTANTIATED (result
))
23992 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23993 since start_preparsed_function wouldn't have if we had an earlier
23994 extern explicit instantiation. */
23995 DECL_EXTERNAL (result
) = 0;
23998 /* If EXTERN_P, then this function will not be emitted -- unless
23999 followed by an explicit instantiation, at which point its linkage
24000 will be adjusted. If !EXTERN_P, then this function will be
24001 emitted here. In neither circumstance do we want
24002 import_export_decl to adjust the linkage. */
24003 DECL_INTERFACE_KNOWN (result
) = 1;
24006 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24007 important template arguments. If any are missing, we check whether
24008 they're important by using error_mark_node for substituting into any
24009 args that were used for partial ordering (the ones between ARGS and END)
24010 and seeing if it bubbles up. */
24013 check_undeduced_parms (tree targs
, tree args
, tree end
)
24015 bool found
= false;
24017 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
24018 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
24021 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
24025 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
24026 if (substed
== error_mark_node
)
24032 /* Given two function templates PAT1 and PAT2, return:
24034 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24035 -1 if PAT2 is more specialized than PAT1.
24036 0 if neither is more specialized.
24038 LEN indicates the number of parameters we should consider
24039 (defaulted parameters should not be considered).
24041 The 1998 std underspecified function template partial ordering, and
24042 DR214 addresses the issue. We take pairs of arguments, one from
24043 each of the templates, and deduce them against each other. One of
24044 the templates will be more specialized if all the *other*
24045 template's arguments deduce against its arguments and at least one
24046 of its arguments *does* *not* deduce against the other template's
24047 corresponding argument. Deduction is done as for class templates.
24048 The arguments used in deduction have reference and top level cv
24049 qualifiers removed. Iff both arguments were originally reference
24050 types *and* deduction succeeds in both directions, an lvalue reference
24051 wins against an rvalue reference and otherwise the template
24052 with the more cv-qualified argument wins for that pairing (if
24053 neither is more cv-qualified, they both are equal). Unlike regular
24054 deduction, after all the arguments have been deduced in this way,
24055 we do *not* verify the deduced template argument values can be
24056 substituted into non-deduced contexts.
24058 The logic can be a bit confusing here, because we look at deduce1 and
24059 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24060 can find template arguments for pat1 to make arg1 look like arg2, that
24061 means that arg2 is at least as specialized as arg1. */
24064 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24066 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24067 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24068 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24069 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24070 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24071 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24072 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24073 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24074 tree origs1
, origs2
;
24075 bool lose1
= false;
24076 bool lose2
= false;
24078 /* Remove the this parameter from non-static member functions. If
24079 one is a non-static member function and the other is not a static
24080 member function, remove the first parameter from that function
24081 also. This situation occurs for operator functions where we
24082 locate both a member function (with this pointer) and non-member
24083 operator (with explicit first operand). */
24084 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24086 len
--; /* LEN is the number of significant arguments for DECL1 */
24087 args1
= TREE_CHAIN (args1
);
24088 if (!DECL_STATIC_FUNCTION_P (decl2
))
24089 args2
= TREE_CHAIN (args2
);
24091 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24093 args2
= TREE_CHAIN (args2
);
24094 if (!DECL_STATIC_FUNCTION_P (decl1
))
24097 args1
= TREE_CHAIN (args1
);
24101 /* If only one is a conversion operator, they are unordered. */
24102 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24105 /* Consider the return type for a conversion function */
24106 if (DECL_CONV_FN_P (decl1
))
24108 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24109 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24113 processing_template_decl
++;
24119 /* Stop when an ellipsis is seen. */
24120 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24122 tree arg1
= TREE_VALUE (args1
);
24123 tree arg2
= TREE_VALUE (args2
);
24124 int deduce1
, deduce2
;
24130 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24131 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24133 /* When both arguments are pack expansions, we need only
24134 unify the patterns themselves. */
24135 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24136 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24138 /* This is the last comparison we need to do. */
24142 if (TYPE_REF_P (arg1
))
24144 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24145 arg1
= TREE_TYPE (arg1
);
24146 quals1
= cp_type_quals (arg1
);
24149 if (TYPE_REF_P (arg2
))
24151 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24152 arg2
= TREE_TYPE (arg2
);
24153 quals2
= cp_type_quals (arg2
);
24156 arg1
= TYPE_MAIN_VARIANT (arg1
);
24157 arg2
= TYPE_MAIN_VARIANT (arg2
);
24159 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24161 int i
, len2
= remaining_arguments (args2
);
24162 tree parmvec
= make_tree_vec (1);
24163 tree argvec
= make_tree_vec (len2
);
24166 /* Setup the parameter vector, which contains only ARG1. */
24167 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24169 /* Setup the argument vector, which contains the remaining
24171 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24172 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24174 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24175 argvec
, DEDUCE_EXACT
,
24176 /*subr=*/true, /*explain_p=*/false)
24179 /* We cannot deduce in the other direction, because ARG1 is
24180 a pack expansion but ARG2 is not. */
24183 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24185 int i
, len1
= remaining_arguments (args1
);
24186 tree parmvec
= make_tree_vec (1);
24187 tree argvec
= make_tree_vec (len1
);
24190 /* Setup the parameter vector, which contains only ARG1. */
24191 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24193 /* Setup the argument vector, which contains the remaining
24195 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24196 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24198 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24199 argvec
, DEDUCE_EXACT
,
24200 /*subr=*/true, /*explain_p=*/false)
24203 /* We cannot deduce in the other direction, because ARG2 is
24204 a pack expansion but ARG1 is not.*/
24210 /* The normal case, where neither argument is a pack
24212 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24213 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24215 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24216 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24220 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24221 arg2, then arg2 is not as specialized as arg1. */
24227 /* "If, for a given type, deduction succeeds in both directions
24228 (i.e., the types are identical after the transformations above)
24229 and both P and A were reference types (before being replaced with
24230 the type referred to above):
24231 - if the type from the argument template was an lvalue reference and
24232 the type from the parameter template was not, the argument type is
24233 considered to be more specialized than the other; otherwise,
24234 - if the type from the argument template is more cv-qualified
24235 than the type from the parameter template (as described above),
24236 the argument type is considered to be more specialized than the other;
24238 - neither type is more specialized than the other." */
24240 if (deduce1
&& deduce2
)
24242 if (ref1
&& ref2
&& ref1
!= ref2
)
24249 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24251 if ((quals1
& quals2
) == quals2
)
24253 if ((quals1
& quals2
) == quals1
)
24258 if (lose1
&& lose2
)
24259 /* We've failed to deduce something in either direction.
24260 These must be unordered. */
24263 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24264 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24265 /* We have already processed all of the arguments in our
24266 handing of the pack expansion type. */
24269 args1
= TREE_CHAIN (args1
);
24270 args2
= TREE_CHAIN (args2
);
24273 /* "In most cases, all template parameters must have values in order for
24274 deduction to succeed, but for partial ordering purposes a template
24275 parameter may remain without a value provided it is not used in the
24276 types being used for partial ordering."
24278 Thus, if we are missing any of the targs1 we need to substitute into
24279 origs1, then pat2 is not as specialized as pat1. This can happen when
24280 there is a nondeduced context. */
24281 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24283 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24286 processing_template_decl
--;
24288 /* If both deductions succeed, the partial ordering selects the more
24289 constrained template. */
24290 /* P2113: If the corresponding template-parameters of the
24291 template-parameter-lists are not equivalent ([temp.over.link]) or if
24292 the function parameters that positionally correspond between the two
24293 templates are not of the same type, neither template is more
24294 specialized than the other. */
24295 if (!lose1
&& !lose2
24296 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24297 DECL_TEMPLATE_PARMS (pat2
))
24298 && compparms (origs1
, origs2
))
24300 int winner
= more_constrained (decl1
, decl2
);
24303 else if (winner
< 0)
24307 /* All things being equal, if the next argument is a pack expansion
24308 for one function but not for the other, prefer the
24309 non-variadic function. FIXME this is bogus; see c++/41958. */
24311 && args1
&& TREE_VALUE (args1
)
24312 && args2
&& TREE_VALUE (args2
))
24314 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24315 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24318 if (lose1
== lose2
)
24326 /* Determine which of two partial specializations of TMPL is more
24329 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24330 to the first partial specialization. The TREE_PURPOSE is the
24331 innermost set of template parameters for the partial
24332 specialization. PAT2 is similar, but for the second template.
24334 Return 1 if the first partial specialization is more specialized;
24335 -1 if the second is more specialized; 0 if neither is more
24338 See [temp.class.order] for information about determining which of
24339 two templates is more specialized. */
24342 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24346 bool any_deductions
= false;
24348 tree tmpl1
= TREE_VALUE (pat1
);
24349 tree tmpl2
= TREE_VALUE (pat2
);
24350 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24351 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24353 /* Just like what happens for functions, if we are ordering between
24354 different template specializations, we may encounter dependent
24355 types in the arguments, and we need our dependency check functions
24356 to behave correctly. */
24357 ++processing_template_decl
;
24358 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24362 any_deductions
= true;
24365 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24369 any_deductions
= true;
24371 --processing_template_decl
;
24373 /* If both deductions succeed, the partial ordering selects the more
24374 constrained template. */
24375 if (!winner
&& any_deductions
)
24376 winner
= more_constrained (tmpl1
, tmpl2
);
24378 /* In the case of a tie where at least one of the templates
24379 has a parameter pack at the end, the template with the most
24380 non-packed parameters wins. */
24383 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24384 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24386 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24387 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24388 int len1
= TREE_VEC_LENGTH (args1
);
24389 int len2
= TREE_VEC_LENGTH (args2
);
24391 /* We don't count the pack expansion at the end. */
24392 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24394 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24399 else if (len1
< len2
)
24406 /* Return the template arguments that will produce the function signature
24407 DECL from the function template FN, with the explicit template
24408 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24409 also match. Return NULL_TREE if no satisfactory arguments could be
24413 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24415 int ntparms
= DECL_NTPARMS (fn
);
24416 tree targs
= make_tree_vec (ntparms
);
24417 tree decl_type
= TREE_TYPE (decl
);
24418 tree decl_arg_types
;
24420 unsigned int nargs
, ix
;
24423 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24425 /* Never do unification on the 'this' parameter. */
24426 decl_arg_types
= skip_artificial_parms_for (decl
,
24427 TYPE_ARG_TYPES (decl_type
));
24429 nargs
= list_length (decl_arg_types
);
24430 args
= XALLOCAVEC (tree
, nargs
);
24431 for (arg
= decl_arg_types
, ix
= 0;
24432 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24433 arg
= TREE_CHAIN (arg
), ++ix
)
24434 args
[ix
] = TREE_VALUE (arg
);
24436 if (fn_type_unification (fn
, explicit_args
, targs
,
24438 (check_rettype
|| DECL_CONV_FN_P (fn
)
24439 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24440 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24441 /*explain_p=*/false,
24443 == error_mark_node
)
24449 /* Return the innermost template arguments that, when applied to a partial
24450 specialization SPEC_TMPL of TMPL, yield the ARGS.
24452 For example, suppose we have:
24454 template <class T, class U> struct S {};
24455 template <class T> struct S<T*, int> {};
24457 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24458 partial specialization and the ARGS will be {double*, int}. The resulting
24459 vector will be {double}, indicating that `T' is bound to `double'. */
24462 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24464 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24466 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24467 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24469 tree innermost_deduced_args
;
24471 innermost_deduced_args
= make_tree_vec (ntparms
);
24472 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24474 deduced_args
= copy_node (args
);
24475 SET_TMPL_ARGS_LEVEL (deduced_args
,
24476 TMPL_ARGS_DEPTH (deduced_args
),
24477 innermost_deduced_args
);
24480 deduced_args
= innermost_deduced_args
;
24482 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24484 if (unify (tparms
, deduced_args
,
24485 INNERMOST_TEMPLATE_ARGS (spec_args
),
24486 INNERMOST_TEMPLATE_ARGS (args
),
24487 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24490 for (i
= 0; i
< ntparms
; ++i
)
24491 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24493 if (!tried_array_deduction
)
24495 try_array_deduction (tparms
, innermost_deduced_args
,
24496 INNERMOST_TEMPLATE_ARGS (spec_args
));
24497 tried_array_deduction
= true;
24498 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24504 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24506 excessive_deduction_depth
= true;
24510 /* Verify that nondeduced template arguments agree with the type
24511 obtained from argument deduction.
24515 struct A { typedef int X; };
24516 template <class T, class U> struct C {};
24517 template <class T> struct C<T, typename T::X> {};
24519 Then with the instantiation `C<A, int>', we can deduce that
24520 `T' is `A' but unify () does not check whether `typename T::X'
24522 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24524 if (spec_args
!= error_mark_node
)
24525 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24526 INNERMOST_TEMPLATE_ARGS (spec_args
),
24527 tmpl
, tf_none
, false, false);
24529 pop_tinst_level ();
24531 if (spec_args
== error_mark_node
24532 /* We only need to check the innermost arguments; the other
24533 arguments will always agree. */
24534 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24535 INNERMOST_TEMPLATE_ARGS (args
)))
24538 /* Now that we have bindings for all of the template arguments,
24539 ensure that the arguments deduced for the template template
24540 parameters have compatible template parameter lists. See the use
24541 of template_template_parm_bindings_ok_p in fn_type_unification
24542 for more information. */
24543 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24546 return deduced_args
;
24549 // Compare two function templates T1 and T2 by deducing bindings
24550 // from one against the other. If both deductions succeed, compare
24551 // constraints to see which is more constrained.
24553 more_specialized_inst (tree t1
, tree t2
)
24558 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24564 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24570 // If both deductions succeed, then one may be more constrained.
24571 if (count
== 2 && fate
== 0)
24572 fate
= more_constrained (t1
, t2
);
24577 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24578 Return the TREE_LIST node with the most specialized template, if
24579 any. If there is no most specialized template, the error_mark_node
24582 Note that this function does not look at, or modify, the
24583 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24584 returned is one of the elements of INSTANTIATIONS, callers may
24585 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24586 and retrieve it from the value returned. */
24589 most_specialized_instantiation (tree templates
)
24593 ++processing_template_decl
;
24596 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24598 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24599 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24604 /* Equally specialized, move to next function. If there
24605 is no next function, nothing's most specialized. */
24606 fn
= TREE_CHAIN (fn
);
24614 /* Now verify that champ is better than everything earlier in the
24615 instantiation list. */
24616 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24617 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24624 processing_template_decl
--;
24627 return error_mark_node
;
24632 /* If DECL is a specialization of some template, return the most
24633 general such template. Otherwise, returns NULL_TREE.
24635 For example, given:
24637 template <class T> struct S { template <class U> void f(U); };
24639 if TMPL is `template <class U> void S<int>::f(U)' this will return
24640 the full template. This function will not trace past partial
24641 specializations, however. For example, given in addition:
24643 template <class T> struct S<T*> { template <class U> void f(U); };
24645 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24646 `template <class T> template <class U> S<T*>::f(U)'. */
24649 most_general_template (tree decl
)
24651 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24653 if (tree tinfo
= get_template_info (decl
))
24654 decl
= TI_TEMPLATE (tinfo
);
24655 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24656 template friend, or a FIELD_DECL for a capture pack. */
24657 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24661 /* Look for more and more general templates. */
24662 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24664 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24665 (See cp-tree.h for details.) */
24666 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24669 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24670 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24671 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24674 /* Stop if we run into an explicitly specialized class template. */
24675 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24676 && DECL_CONTEXT (decl
)
24677 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24680 decl
= DECL_TI_TEMPLATE (decl
);
24686 /* Return the most specialized of the template partial specializations
24687 which can produce TARGET, a specialization of some class or variable
24688 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24689 a TEMPLATE_DECL node corresponding to the partial specialization, while
24690 the TREE_PURPOSE is the set of template arguments that must be
24691 substituted into the template pattern in order to generate TARGET.
24693 If the choice of partial specialization is ambiguous, a diagnostic
24694 is issued, and the error_mark_node is returned. If there are no
24695 partial specializations matching TARGET, then NULL_TREE is
24696 returned, indicating that the primary template should be used. */
24699 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24701 tree list
= NULL_TREE
;
24706 tree outer_args
= NULL_TREE
;
24709 if (TYPE_P (target
))
24711 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24712 tmpl
= TI_TEMPLATE (tinfo
);
24713 args
= TI_ARGS (tinfo
);
24715 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24717 tmpl
= TREE_OPERAND (target
, 0);
24718 args
= TREE_OPERAND (target
, 1);
24720 else if (VAR_P (target
))
24722 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24723 tmpl
= TI_TEMPLATE (tinfo
);
24724 args
= TI_ARGS (tinfo
);
24727 gcc_unreachable ();
24729 tree main_tmpl
= most_general_template (tmpl
);
24731 /* For determining which partial specialization to use, only the
24732 innermost args are interesting. */
24733 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24735 outer_args
= strip_innermost_template_args (args
, 1);
24736 args
= INNERMOST_TEMPLATE_ARGS (args
);
24739 /* The caller hasn't called push_to_top_level yet, but we need
24740 get_partial_spec_bindings to be done in non-template context so that we'll
24741 fully resolve everything. */
24742 processing_template_decl_sentinel ptds
;
24744 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24746 const tree ospec_tmpl
= TREE_VALUE (t
);
24751 /* Substitute in the template args from the enclosing class. */
24752 ++processing_template_decl
;
24753 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24754 --processing_template_decl
;
24755 if (spec_tmpl
== error_mark_node
)
24756 return error_mark_node
;
24759 spec_tmpl
= ospec_tmpl
;
24761 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24765 spec_args
= add_to_template_args (outer_args
, spec_args
);
24767 /* Keep the candidate only if the constraints are satisfied,
24768 or if we're not compiling with concepts. */
24770 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24772 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24773 TREE_TYPE (list
) = TREE_TYPE (t
);
24781 ambiguous_p
= false;
24784 t
= TREE_CHAIN (t
);
24785 for (; t
; t
= TREE_CHAIN (t
))
24787 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24794 t
= TREE_CHAIN (t
);
24797 ambiguous_p
= true;
24806 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24808 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24811 ambiguous_p
= true;
24819 char *spaces
= NULL
;
24820 if (!(complain
& tf_error
))
24821 return error_mark_node
;
24822 if (TYPE_P (target
))
24823 error ("ambiguous template instantiation for %q#T", target
);
24825 error ("ambiguous template instantiation for %q#D", target
);
24826 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24827 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24829 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24830 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24831 "%s %#qS", spaces
? spaces
: str
, subst
);
24832 spaces
= spaces
? spaces
: get_spaces (str
);
24835 return error_mark_node
;
24841 /* Explicitly instantiate DECL. */
24844 do_decl_instantiation (tree decl
, tree storage
)
24846 tree result
= NULL_TREE
;
24849 if (!decl
|| decl
== error_mark_node
)
24850 /* An error occurred, for which grokdeclarator has already issued
24851 an appropriate message. */
24853 else if (! DECL_LANG_SPECIFIC (decl
))
24855 error ("explicit instantiation of non-template %q#D", decl
);
24858 else if (DECL_DECLARED_CONCEPT_P (decl
))
24861 error ("explicit instantiation of variable concept %q#D", decl
);
24863 error ("explicit instantiation of function concept %q#D", decl
);
24867 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24868 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24870 if (VAR_P (decl
) && !var_templ
)
24872 /* There is an asymmetry here in the way VAR_DECLs and
24873 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24874 the latter, the DECL we get back will be marked as a
24875 template instantiation, and the appropriate
24876 DECL_TEMPLATE_INFO will be set up. This does not happen for
24877 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24878 should handle VAR_DECLs as it currently handles
24880 if (!DECL_CLASS_SCOPE_P (decl
))
24882 error ("%qD is not a static data member of a class template", decl
);
24885 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24886 if (!result
|| !VAR_P (result
))
24888 error ("no matching template for %qD found", decl
);
24891 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24893 error ("type %qT for explicit instantiation %qD does not match "
24894 "declared type %qT", TREE_TYPE (result
), decl
,
24899 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24901 error ("explicit instantiation of %q#D", decl
);
24907 /* Check for various error cases. Note that if the explicit
24908 instantiation is valid the RESULT will currently be marked as an
24909 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24910 until we get here. */
24912 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24914 /* DR 259 [temp.spec].
24916 Both an explicit instantiation and a declaration of an explicit
24917 specialization shall not appear in a program unless the explicit
24918 instantiation follows a declaration of the explicit specialization.
24920 For a given set of template parameters, if an explicit
24921 instantiation of a template appears after a declaration of an
24922 explicit specialization for that template, the explicit
24923 instantiation has no effect. */
24926 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24930 No program shall explicitly instantiate any template more
24933 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24934 the first instantiation was `extern' and the second is not,
24935 and EXTERN_P for the opposite case. */
24936 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24937 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24938 /* If an "extern" explicit instantiation follows an ordinary
24939 explicit instantiation, the template is instantiated. */
24943 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24945 error ("no matching template for %qD found", result
);
24948 else if (!DECL_TEMPLATE_INFO (result
))
24950 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24954 if (storage
== NULL_TREE
)
24956 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24958 if (cxx_dialect
== cxx98
)
24959 pedwarn (input_location
, OPT_Wpedantic
,
24960 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24965 error ("storage class %qD applied to template instantiation", storage
);
24967 check_explicit_instantiation_namespace (result
);
24968 mark_decl_instantiated (result
, extern_p
);
24970 instantiate_decl (result
, /*defer_ok=*/true,
24971 /*expl_inst_class_mem_p=*/false);
24975 mark_class_instantiated (tree t
, int extern_p
)
24977 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24978 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24979 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24980 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24983 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24984 rest_of_type_compilation (t
, 1);
24988 /* Perform an explicit instantiation of template class T. STORAGE, if
24989 non-null, is the RID for extern, inline or static. COMPLAIN is
24990 nonzero if this is called from the parser, zero if called recursively,
24991 since the standard is unclear (as detailed below). */
24994 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24996 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
24998 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
24999 error ("explicit instantiation of non-class template %qD",
25002 error ("explicit instantiation of non-template type %qT", t
);
25008 if (!COMPLETE_TYPE_P (t
))
25010 if (complain
& tf_error
)
25011 error ("explicit instantiation of %q#T before definition of template",
25016 /* At most one of these will be true. */
25017 bool extern_p
= false;
25018 bool nomem_p
= false;
25019 bool static_p
= false;
25021 if (storage
!= NULL_TREE
)
25023 if (storage
== ridpointers
[(int) RID_EXTERN
])
25025 if (cxx_dialect
== cxx98
)
25026 pedwarn (input_location
, OPT_Wpedantic
,
25027 "ISO C++ 1998 forbids the use of %<extern%> on "
25028 "explicit instantiations");
25031 pedwarn (input_location
, OPT_Wpedantic
,
25032 "ISO C++ forbids the use of %qE"
25033 " on explicit instantiations", storage
);
25035 if (storage
== ridpointers
[(int) RID_INLINE
])
25037 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25039 else if (storage
== ridpointers
[(int) RID_STATIC
])
25042 error ("storage class %qD applied to template instantiation",
25046 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25047 /* DR 259 [temp.spec].
25049 Both an explicit instantiation and a declaration of an explicit
25050 specialization shall not appear in a program unless the
25051 explicit instantiation follows a declaration of the explicit
25054 For a given set of template parameters, if an explicit
25055 instantiation of a template appears after a declaration of an
25056 explicit specialization for that template, the explicit
25057 instantiation has no effect. */
25060 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
25062 /* We've already instantiated the template. */
25066 No program shall explicitly instantiate any template more
25069 If EXTERN_P then this is ok. */
25070 if (!extern_p
&& (complain
& tf_error
))
25071 permerror (input_location
,
25072 "duplicate explicit instantiation of %q#T", t
);
25077 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25078 mark_class_instantiated (t
, extern_p
);
25083 /* In contrast to implicit instantiation, where only the
25084 declarations, and not the definitions, of members are
25085 instantiated, we have here:
25089 An explicit instantiation that names a class template
25090 specialization is also an explicit instantiation of the same
25091 kind (declaration or definition) of each of its members (not
25092 including members inherited from base classes and members
25093 that are templates) that has not been previously explicitly
25094 specialized in the translation unit containing the explicit
25095 instantiation, provided that the associated constraints, if
25096 any, of that member are satisfied by the template arguments
25097 of the explicit instantiation. */
25098 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25100 || (TREE_CODE (fld
) == FUNCTION_DECL
25102 && user_provided_p (fld
)))
25103 && DECL_TEMPLATE_INSTANTIATION (fld
)
25104 && constraints_satisfied_p (fld
))
25106 mark_decl_instantiated (fld
, extern_p
);
25108 instantiate_decl (fld
, /*defer_ok=*/true,
25109 /*expl_inst_class_mem_p=*/true);
25111 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
25113 tree type
= TREE_TYPE (fld
);
25115 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25116 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
25117 do_type_instantiation (type
, storage
, 0);
25121 /* Given a function DECL, which is a specialization of TMPL, modify
25122 DECL to be a re-instantiation of TMPL with the same template
25123 arguments. TMPL should be the template into which tsubst'ing
25124 should occur for DECL, not the most general template.
25126 One reason for doing this is a scenario like this:
25129 void f(const T&, int i);
25131 void g() { f(3, 7); }
25134 void f(const T& t, const int i) { }
25136 Note that when the template is first instantiated, with
25137 instantiate_template, the resulting DECL will have no name for the
25138 first parameter, and the wrong type for the second. So, when we go
25139 to instantiate the DECL, we regenerate it. */
25142 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25144 /* The arguments used to instantiate DECL, from the most general
25148 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25150 /* Make sure that we can see identifiers, and compute access
25152 push_access_scope (decl
);
25154 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25162 args_depth
= TMPL_ARGS_DEPTH (args
);
25163 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25164 if (args_depth
> parms_depth
)
25165 args
= get_innermost_template_args (args
, parms_depth
);
25167 /* Instantiate a dynamic exception-specification. noexcept will be
25169 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25170 if (TREE_VALUE (raises
))
25172 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25173 args
, tf_error
, NULL_TREE
,
25174 /*defer_ok*/false);
25175 if (specs
&& specs
!= error_mark_node
)
25176 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25180 /* Merge parameter declarations. */
25181 decl_parm
= skip_artificial_parms_for (decl
,
25182 DECL_ARGUMENTS (decl
));
25184 = skip_artificial_parms_for (code_pattern
,
25185 DECL_ARGUMENTS (code_pattern
));
25186 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25191 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25192 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25193 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25195 parm_type
= type_decays_to (parm_type
);
25196 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25197 TREE_TYPE (decl_parm
) = parm_type
;
25198 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25199 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25201 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25202 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25204 decl_parm
= DECL_CHAIN (decl_parm
);
25205 pattern_parm
= DECL_CHAIN (pattern_parm
);
25207 /* Merge any parameters that match with the function parameter
25209 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25212 tree expanded_types
;
25213 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25214 the parameters in this function parameter pack. */
25215 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25216 args
, tf_error
, NULL_TREE
);
25217 len
= TREE_VEC_LENGTH (expanded_types
);
25218 for (i
= 0; i
< len
; i
++)
25223 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25224 /* Rename the parameter to include the index. */
25225 DECL_NAME (decl_parm
) =
25226 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25227 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25228 parm_type
= type_decays_to (parm_type
);
25229 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25230 TREE_TYPE (decl_parm
) = parm_type
;
25231 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25232 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25234 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25235 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25237 decl_parm
= DECL_CHAIN (decl_parm
);
25240 /* Merge additional specifiers from the CODE_PATTERN. */
25241 if (DECL_DECLARED_INLINE_P (code_pattern
)
25242 && !DECL_DECLARED_INLINE_P (decl
))
25243 DECL_DECLARED_INLINE_P (decl
) = 1;
25245 maybe_instantiate_noexcept (decl
, tf_error
);
25247 else if (VAR_P (decl
))
25249 start_lambda_scope (decl
);
25250 DECL_INITIAL (decl
) =
25251 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25252 tf_error
, DECL_TI_TEMPLATE (decl
));
25253 finish_lambda_scope ();
25254 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25255 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25256 tf_error
, DECL_TI_TEMPLATE (decl
));
25259 gcc_unreachable ();
25261 pop_access_scope (decl
);
25264 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25265 substituted to get DECL. */
25268 template_for_substitution (tree decl
)
25270 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25272 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25273 for the instantiation. This is not always the most general
25274 template. Consider, for example:
25277 struct S { template <class U> void f();
25278 template <> void f<int>(); };
25280 and an instantiation of S<double>::f<int>. We want TD to be the
25281 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25282 while (/* An instantiation cannot have a definition, so we need a
25283 more general template. */
25284 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25285 /* We must also deal with friend templates. Given:
25287 template <class T> struct S {
25288 template <class U> friend void f() {};
25291 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25292 so far as the language is concerned, but that's still
25293 where we get the pattern for the instantiation from. On
25294 other hand, if the definition comes outside the class, say:
25296 template <class T> struct S {
25297 template <class U> friend void f();
25299 template <class U> friend void f() {}
25301 we don't need to look any further. That's what the check for
25302 DECL_INITIAL is for. */
25303 || (TREE_CODE (decl
) == FUNCTION_DECL
25304 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25305 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25307 /* The present template, TD, should not be a definition. If it
25308 were a definition, we should be using it! Note that we
25309 cannot restructure the loop to just keep going until we find
25310 a template with a definition, since that might go too far if
25311 a specialization was declared, but not defined. */
25313 /* Fetch the more general template. */
25314 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25320 /* Returns true if we need to instantiate this template instance even if we
25321 know we aren't going to emit it. */
25324 always_instantiate_p (tree decl
)
25326 /* We always instantiate inline functions so that we can inline them. An
25327 explicit instantiation declaration prohibits implicit instantiation of
25328 non-inline functions. With high levels of optimization, we would
25329 normally inline non-inline functions -- but we're not allowed to do
25330 that for "extern template" functions. Therefore, we check
25331 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25332 return ((TREE_CODE (decl
) == FUNCTION_DECL
25333 && (DECL_DECLARED_INLINE_P (decl
)
25334 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25335 /* And we need to instantiate static data members so that
25336 their initializers are available in integral constant
25339 && decl_maybe_constant_var_p (decl
)));
25342 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25343 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25344 error, true otherwise. */
25347 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25349 tree fntype
, spec
, noex
;
25351 /* Don't instantiate a noexcept-specification from template context. */
25352 if (processing_template_decl
25353 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25356 if (DECL_MAYBE_DELETED (fn
))
25358 if (fn
== current_function_decl
)
25359 /* We're in start_preparsed_function, keep going. */
25363 synthesize_method (fn
);
25365 return !DECL_MAYBE_DELETED (fn
);
25368 fntype
= TREE_TYPE (fn
);
25369 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25371 if (!spec
|| !TREE_PURPOSE (spec
))
25374 noex
= TREE_PURPOSE (spec
);
25375 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25376 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25379 tree orig_fn
= NULL_TREE
;
25380 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25381 its FUNCTION_DECL for the rest of this function -- push_access_scope
25382 doesn't accept TEMPLATE_DECLs. */
25383 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25386 fn
= DECL_TEMPLATE_RESULT (fn
);
25389 if (DECL_CLONED_FUNCTION_P (fn
))
25391 tree prime
= DECL_CLONED_FUNCTION (fn
);
25392 if (!maybe_instantiate_noexcept (prime
, complain
))
25394 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25396 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25398 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25399 bool added
= false;
25400 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25402 spec
= get_defaulted_eh_spec (fn
, complain
);
25403 if (spec
== error_mark_node
)
25404 /* This might have failed because of an unparsed DMI, so
25405 let's try again later. */
25408 else if (!(added
= !fns
->add (fn
)))
25410 /* If hash_set::add returns true, the element was already there. */
25411 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25412 DECL_SOURCE_LOCATION (fn
));
25414 "exception specification of %qD depends on itself",
25416 spec
= noexcept_false_spec
;
25418 else if (push_tinst_level (fn
))
25420 push_to_top_level ();
25421 push_access_scope (fn
);
25422 push_deferring_access_checks (dk_no_deferred
);
25423 input_location
= DECL_SOURCE_LOCATION (fn
);
25425 if (!DECL_LOCAL_DECL_P (fn
))
25427 /* If needed, set current_class_ptr for the benefit of
25428 tsubst_copy/PARM_DECL. The exception pattern will
25429 refer to the parm of the template, not the
25431 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25432 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25434 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25435 current_class_ptr
= NULL_TREE
;
25436 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25437 current_class_ptr
= this_parm
;
25441 /* If this function is represented by a TEMPLATE_DECL, then
25442 the deferred noexcept-specification might still contain
25443 dependent types, even after substitution. And we need the
25444 dependency check functions to work in build_noexcept_spec. */
25446 ++processing_template_decl
;
25448 /* Do deferred instantiation of the noexcept-specifier. */
25449 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25450 DEFERRED_NOEXCEPT_ARGS (noex
),
25451 tf_warning_or_error
, fn
,
25452 /*function_p=*/false,
25455 /* Build up the noexcept-specification. */
25456 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25459 --processing_template_decl
;
25461 pop_deferring_access_checks ();
25462 pop_access_scope (fn
);
25463 pop_tinst_level ();
25464 pop_from_top_level ();
25467 spec
= noexcept_false_spec
;
25473 if (spec
== error_mark_node
)
25475 /* This failed with a hard error, so let's go with false. */
25476 gcc_assert (seen_error ());
25477 spec
= noexcept_false_spec
;
25480 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25482 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25487 /* We're starting to process the function INST, an instantiation of PATTERN;
25488 add their parameters to local_specializations. */
25491 register_parameter_specializations (tree pattern
, tree inst
)
25493 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25494 tree spec_parm
= DECL_ARGUMENTS (inst
);
25495 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25497 register_local_specialization (spec_parm
, tmpl_parm
);
25498 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25499 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25501 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25503 if (!DECL_PACK_P (tmpl_parm
)
25504 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25506 register_local_specialization (spec_parm
, tmpl_parm
);
25507 spec_parm
= DECL_CHAIN (spec_parm
);
25511 /* Register the (value) argument pack as a specialization of
25512 TMPL_PARM, then move on. */
25513 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25514 register_local_specialization (argpack
, tmpl_parm
);
25517 gcc_assert (!spec_parm
);
25520 /* Instantiate the body of D using PATTERN with ARGS. We have
25521 already determined PATTERN is the correct template to use.
25522 NESTED_P is true if this is a nested function, in which case
25523 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25526 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25528 tree td
= NULL_TREE
;
25529 tree code_pattern
= pattern
;
25534 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25537 /* Only OMP reductions are nested. */
25538 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25540 vec
<tree
> omp_privatization_save
;
25541 if (current_function_decl
)
25542 save_omp_privatization_clauses (omp_privatization_save
);
25545 = !(current_function_decl
25546 && !LAMBDA_FUNCTION_P (d
)
25547 && decl_function_context (d
) == current_function_decl
);
25550 push_to_top_level ();
25553 gcc_assert (!processing_template_decl
);
25554 push_function_context ();
25555 cp_unevaluated_operand
= 0;
25556 c_inhibit_evaluation_warnings
= 0;
25561 /* The variable might be a lambda's extra scope, and that
25562 lambda's visibility depends on D's. */
25563 maybe_commonize_var (d
);
25564 determine_visibility (d
);
25567 /* Mark D as instantiated so that recursive calls to
25568 instantiate_decl do not try to instantiate it again. */
25569 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25572 /* Regenerate the declaration in case the template has been modified
25573 by a subsequent redeclaration. */
25574 regenerate_decl_from_template (d
, td
, args
);
25576 /* We already set the file and line above. Reset them now in case
25577 they changed as a result of calling regenerate_decl_from_template. */
25578 input_location
= DECL_SOURCE_LOCATION (d
);
25582 /* Clear out DECL_RTL; whatever was there before may not be right
25583 since we've reset the type of the declaration. */
25584 SET_DECL_RTL (d
, NULL
);
25585 DECL_IN_AGGR_P (d
) = 0;
25587 /* The initializer is placed in DECL_INITIAL by
25588 regenerate_decl_from_template so we don't need to
25589 push/pop_access_scope again here. Pull it out so that
25590 cp_finish_decl can process it. */
25591 bool const_init
= false;
25592 tree init
= DECL_INITIAL (d
);
25593 DECL_INITIAL (d
) = NULL_TREE
;
25594 DECL_INITIALIZED_P (d
) = 0;
25596 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25597 initializer. That function will defer actual emission until
25598 we have a chance to determine linkage. */
25599 DECL_EXTERNAL (d
) = 0;
25601 /* Enter the scope of D so that access-checking works correctly. */
25602 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25604 push_nested_class (DECL_CONTEXT (d
));
25606 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25607 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25610 pop_nested_class ();
25612 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25613 synthesize_method (d
);
25614 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25616 /* Set up the list of local specializations. */
25617 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25618 tree block
= NULL_TREE
;
25620 /* Set up context. */
25622 block
= push_stmt_list ();
25625 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25627 perform_instantiation_time_access_checks (code_pattern
, args
);
25630 /* Create substitution entries for the parameters. */
25631 register_parameter_specializations (code_pattern
, d
);
25633 /* Substitute into the body of the function. */
25634 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25635 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25636 tf_warning_or_error
, d
);
25639 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25640 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25641 /*integral_constant_expression_p=*/false);
25643 /* Set the current input_location to the end of the function
25644 so that finish_function knows where we are. */
25646 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25648 /* Remember if we saw an infinite loop in the template. */
25649 current_function_infinite_loop
25650 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25653 /* Finish the function. */
25655 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25658 d
= finish_function (/*inline_p=*/false);
25659 expand_or_defer_fn (d
);
25662 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25663 cp_check_omp_declare_reduction (d
);
25666 /* We're not deferring instantiation any more. */
25668 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25671 pop_from_top_level ();
25673 pop_function_context ();
25675 if (current_function_decl
)
25676 restore_omp_privatization_clauses (omp_privatization_save
);
25679 /* Produce the definition of D, a _DECL generated from a template. If
25680 DEFER_OK is true, then we don't have to actually do the
25681 instantiation now; we just have to do it sometime. Normally it is
25682 an error if this is an explicit instantiation but D is undefined.
25683 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25684 instantiated class template. */
25687 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25689 tree tmpl
= DECL_TI_TEMPLATE (d
);
25696 bool pattern_defined
;
25697 location_t saved_loc
= input_location
;
25698 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25699 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25703 /* This function should only be used to instantiate templates for
25704 functions and static member variables. */
25705 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25707 /* A concept is never instantiated. */
25708 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25710 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
25712 /* Variables are never deferred; if instantiation is required, they
25713 are instantiated right away. That allows for better code in the
25714 case that an expression refers to the value of the variable --
25715 if the variable has a constant value the referring expression can
25716 take advantage of that fact. */
25720 /* Don't instantiate cloned functions. Instead, instantiate the
25721 functions they cloned. */
25722 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25723 d
= DECL_CLONED_FUNCTION (d
);
25725 if (DECL_TEMPLATE_INSTANTIATED (d
)
25726 || TREE_TYPE (d
) == error_mark_node
25727 || (TREE_CODE (d
) == FUNCTION_DECL
25728 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25729 || DECL_TEMPLATE_SPECIALIZATION (d
))
25730 /* D has already been instantiated or explicitly specialized, so
25731 there's nothing for us to do here.
25733 It might seem reasonable to check whether or not D is an explicit
25734 instantiation, and, if so, stop here. But when an explicit
25735 instantiation is deferred until the end of the compilation,
25736 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25737 the instantiation. */
25740 /* Check to see whether we know that this template will be
25741 instantiated in some other file, as with "extern template"
25743 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25745 /* In general, we do not instantiate such templates. */
25746 if (external_p
&& !always_instantiate_p (d
))
25749 gen_tmpl
= most_general_template (tmpl
);
25750 gen_args
= DECL_TI_ARGS (d
);
25752 /* We should already have the extra args. */
25753 gcc_checking_assert (tmpl
== gen_tmpl
25754 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25755 == TMPL_ARGS_DEPTH (gen_args
)));
25756 /* And what's in the hash table should match D. */
25757 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25759 || spec
== NULL_TREE
);
25761 /* This needs to happen before any tsubsting. */
25762 if (! push_tinst_level (d
))
25765 timevar_push (TV_TEMPLATE_INST
);
25767 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25768 for the instantiation. */
25769 td
= template_for_substitution (d
);
25774 /* Look up an explicit specialization, if any. */
25775 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25776 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25777 if (elt
&& elt
!= error_mark_node
)
25779 td
= TREE_VALUE (elt
);
25780 args
= TREE_PURPOSE (elt
);
25784 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25786 /* We should never be trying to instantiate a member of a class
25787 template or partial specialization. */
25788 gcc_assert (d
!= code_pattern
);
25790 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25791 || DECL_TEMPLATE_SPECIALIZATION (td
))
25792 /* In the case of a friend template whose definition is provided
25793 outside the class, we may have too many arguments. Drop the
25794 ones we don't need. The same is true for specializations. */
25795 args
= get_innermost_template_args
25796 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25798 if (TREE_CODE (d
) == FUNCTION_DECL
)
25800 deleted_p
= DECL_DELETED_FN (code_pattern
);
25801 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25802 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25803 || DECL_DEFAULTED_FN (code_pattern
)
25809 if (DECL_CLASS_SCOPE_P (code_pattern
))
25810 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25812 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25815 /* We may be in the middle of deferred access check. Disable it now. */
25816 push_deferring_access_checks (dk_no_deferred
);
25818 /* Unless an explicit instantiation directive has already determined
25819 the linkage of D, remember that a definition is available for
25821 if (pattern_defined
25822 && !DECL_INTERFACE_KNOWN (d
)
25823 && !DECL_NOT_REALLY_EXTERN (d
))
25824 mark_definable (d
);
25826 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25827 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25828 input_location
= DECL_SOURCE_LOCATION (d
);
25830 /* If D is a member of an explicitly instantiated class template,
25831 and no definition is available, treat it like an implicit
25833 if (!pattern_defined
&& expl_inst_class_mem_p
25834 && DECL_EXPLICIT_INSTANTIATION (d
))
25836 /* Leave linkage flags alone on instantiations with anonymous
25838 if (TREE_PUBLIC (d
))
25840 DECL_NOT_REALLY_EXTERN (d
) = 0;
25841 DECL_INTERFACE_KNOWN (d
) = 0;
25843 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25846 /* Defer all other templates, unless we have been explicitly
25847 forbidden from doing so. */
25848 if (/* If there is no definition, we cannot instantiate the
25851 /* If it's OK to postpone instantiation, do so. */
25853 /* If this is a static data member that will be defined
25854 elsewhere, we don't want to instantiate the entire data
25855 member, but we do want to instantiate the initializer so that
25856 we can substitute that elsewhere. */
25857 || (external_p
&& VAR_P (d
))
25858 /* Handle here a deleted function too, avoid generating
25859 its body (c++/61080). */
25862 /* The definition of the static data member is now required so
25863 we must substitute the initializer. */
25865 && !DECL_INITIAL (d
)
25866 && DECL_INITIAL (code_pattern
))
25870 bool const_init
= false;
25871 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25873 ns
= decl_namespace_context (d
);
25874 push_nested_namespace (ns
);
25876 push_nested_class (DECL_CONTEXT (d
));
25877 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25879 tf_warning_or_error
, NULL_TREE
,
25880 /*integral_constant_expression_p=*/false);
25881 /* If instantiating the initializer involved instantiating this
25882 again, don't call cp_finish_decl twice. */
25883 if (!DECL_INITIAL (d
))
25885 /* Make sure the initializer is still constant, in case of
25886 circular dependency (template/instantiate6.C). */
25888 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25889 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25890 /*asmspec_tree=*/NULL_TREE
,
25891 LOOKUP_ONLYCONVERTING
);
25894 pop_nested_class ();
25895 pop_nested_namespace (ns
);
25898 /* We restore the source position here because it's used by
25899 add_pending_template. */
25900 input_location
= saved_loc
;
25902 if (at_eof
&& !pattern_defined
25903 && DECL_EXPLICIT_INSTANTIATION (d
)
25904 && DECL_NOT_REALLY_EXTERN (d
))
25907 The definition of a non-exported function template, a
25908 non-exported member function template, or a non-exported
25909 member function or static data member of a class template
25910 shall be present in every translation unit in which it is
25911 explicitly instantiated. */
25912 permerror (input_location
, "explicit instantiation of %qD "
25913 "but no definition available", d
);
25915 /* If we're in unevaluated context, we just wanted to get the
25916 constant value; this isn't an odr use, so don't queue
25917 a full instantiation. */
25918 if (!cp_unevaluated_operand
25919 /* ??? Historically, we have instantiated inline functions, even
25920 when marked as "extern template". */
25921 && !(external_p
&& VAR_P (d
)))
25922 add_pending_template (d
);
25926 if (variable_template_p (gen_tmpl
))
25927 note_variable_template_instantiation (d
);
25928 instantiate_body (td
, args
, d
, false);
25931 pop_deferring_access_checks ();
25932 timevar_pop (TV_TEMPLATE_INST
);
25933 pop_tinst_level ();
25934 input_location
= saved_loc
;
25935 cp_unevaluated_operand
= saved_unevaluated_operand
;
25936 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25941 /* Run through the list of templates that we wish we could
25942 instantiate, and instantiate any we can. RETRIES is the
25943 number of times we retry pending template instantiation. */
25946 instantiate_pending_templates (int retries
)
25949 location_t saved_loc
= input_location
;
25951 /* Instantiating templates may trigger vtable generation. This in turn
25952 may require further template instantiations. We place a limit here
25953 to avoid infinite loop. */
25954 if (pending_templates
&& retries
>= max_tinst_depth
)
25956 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25958 fatal_error (input_location
,
25959 "template instantiation depth exceeds maximum of %d"
25960 " instantiating %q+D, possibly from virtual table generation"
25961 " (use %<-ftemplate-depth=%> to increase the maximum)",
25962 max_tinst_depth
, decl
);
25963 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25964 /* Pretend that we defined it. */
25965 DECL_INITIAL (decl
) = error_mark_node
;
25971 struct pending_template
**t
= &pending_templates
;
25972 struct pending_template
*last
= NULL
;
25976 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25977 bool complete
= false;
25979 if (TYPE_P (instantiation
))
25981 if (!COMPLETE_TYPE_P (instantiation
))
25983 instantiate_class_template (instantiation
);
25984 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25985 for (tree fld
= TYPE_FIELDS (instantiation
);
25986 fld
; fld
= TREE_CHAIN (fld
))
25988 || (TREE_CODE (fld
) == FUNCTION_DECL
25989 && !DECL_ARTIFICIAL (fld
)))
25990 && DECL_TEMPLATE_INSTANTIATION (fld
))
25991 instantiate_decl (fld
,
25992 /*defer_ok=*/false,
25993 /*expl_inst_class_mem_p=*/false);
25995 if (COMPLETE_TYPE_P (instantiation
))
25999 complete
= COMPLETE_TYPE_P (instantiation
);
26003 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26004 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
26007 = instantiate_decl (instantiation
,
26008 /*defer_ok=*/false,
26009 /*expl_inst_class_mem_p=*/false);
26010 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
26014 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26015 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
26020 /* If INSTANTIATION has been instantiated, then we don't
26021 need to consider it again in the future. */
26022 struct pending_template
*drop
= *t
;
26024 set_refcount_ptr (drop
->tinst
);
26025 pending_template_freelist ().free (drop
);
26033 set_refcount_ptr (current_tinst_level
);
26035 last_pending_template
= last
;
26037 while (reconsider
);
26039 input_location
= saved_loc
;
26042 /* Substitute ARGVEC into T, which is a list of initializers for
26043 either base class or a non-static data member. The TREE_PURPOSEs
26044 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26045 instantiate_decl. */
26048 tsubst_initializer_list (tree t
, tree argvec
)
26050 tree inits
= NULL_TREE
;
26051 tree target_ctor
= error_mark_node
;
26053 for (; t
; t
= TREE_CHAIN (t
))
26057 tree expanded_bases
= NULL_TREE
;
26058 tree expanded_arguments
= NULL_TREE
;
26061 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26066 /* Expand the base class expansion type into separate base
26068 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26069 tf_warning_or_error
,
26071 if (expanded_bases
== error_mark_node
)
26074 /* We'll be building separate TREE_LISTs of arguments for
26076 len
= TREE_VEC_LENGTH (expanded_bases
);
26077 expanded_arguments
= make_tree_vec (len
);
26078 for (i
= 0; i
< len
; i
++)
26079 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26081 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26082 expand each argument in the TREE_VALUE of t. */
26083 expr
= make_node (EXPR_PACK_EXPANSION
);
26084 PACK_EXPANSION_LOCAL_P (expr
) = true;
26085 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26086 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26088 if (TREE_VALUE (t
) == void_type_node
)
26089 /* VOID_TYPE_NODE is used to indicate
26090 value-initialization. */
26092 for (i
= 0; i
< len
; i
++)
26093 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26097 /* Substitute parameter packs into each argument in the
26099 in_base_initializer
= 1;
26100 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26102 tree expanded_exprs
;
26104 /* Expand the argument. */
26105 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26107 = tsubst_pack_expansion (expr
, argvec
,
26108 tf_warning_or_error
,
26110 if (expanded_exprs
== error_mark_node
)
26113 /* Prepend each of the expanded expressions to the
26114 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26115 for (i
= 0; i
< len
; i
++)
26117 TREE_VEC_ELT (expanded_arguments
, i
) =
26118 tree_cons (NULL_TREE
,
26119 TREE_VEC_ELT (expanded_exprs
, i
),
26120 TREE_VEC_ELT (expanded_arguments
, i
));
26123 in_base_initializer
= 0;
26125 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26126 since we built them backwards. */
26127 for (i
= 0; i
< len
; i
++)
26129 TREE_VEC_ELT (expanded_arguments
, i
) =
26130 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26135 for (i
= 0; i
< len
; ++i
)
26137 if (expanded_bases
)
26139 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26140 decl
= expand_member_init (decl
);
26141 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26146 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26147 tf_warning_or_error
, NULL_TREE
);
26149 decl
= expand_member_init (decl
);
26150 if (decl
&& !DECL_P (decl
))
26151 in_base_initializer
= 1;
26153 init
= TREE_VALUE (t
);
26155 if (init
!= void_type_node
)
26156 init
= tsubst_expr (init
, argvec
,
26157 tf_warning_or_error
, NULL_TREE
,
26158 /*integral_constant_expression_p=*/false);
26159 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26160 /* If we had an initializer but it instantiated to nothing,
26161 value-initialize the object. This will only occur when
26162 the initializer was a pack expansion where the parameter
26163 packs used in that expansion were of length zero. */
26164 init
= void_type_node
;
26165 in_base_initializer
= 0;
26168 if (target_ctor
!= error_mark_node
26169 && init
!= error_mark_node
)
26171 error ("mem-initializer for %qD follows constructor delegation",
26175 /* Look for a target constructor. */
26176 if (init
!= error_mark_node
26177 && decl
&& CLASS_TYPE_P (decl
)
26178 && same_type_p (decl
, current_class_type
))
26180 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26183 error ("constructor delegation follows mem-initializer for %qD",
26184 TREE_PURPOSE (inits
));
26187 target_ctor
= init
;
26192 init
= build_tree_list (decl
, init
);
26193 /* Carry over the dummy TREE_TYPE node containing the source
26195 TREE_TYPE (init
) = TREE_TYPE (t
);
26196 TREE_CHAIN (init
) = inits
;
26204 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26207 set_current_access_from_decl (tree decl
)
26209 if (TREE_PRIVATE (decl
))
26210 current_access_specifier
= access_private_node
;
26211 else if (TREE_PROTECTED (decl
))
26212 current_access_specifier
= access_protected_node
;
26214 current_access_specifier
= access_public_node
;
26217 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26218 is the instantiation (which should have been created with
26219 start_enum) and ARGS are the template arguments to use. */
26222 tsubst_enum (tree tag
, tree newtag
, tree args
)
26226 if (SCOPED_ENUM_P (newtag
))
26227 begin_scope (sk_scoped_enum
, newtag
);
26229 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26234 decl
= TREE_VALUE (e
);
26235 /* Note that in a template enum, the TREE_VALUE is the
26236 CONST_DECL, not the corresponding INTEGER_CST. */
26237 value
= tsubst_expr (DECL_INITIAL (decl
),
26238 args
, tf_warning_or_error
, NULL_TREE
,
26239 /*integral_constant_expression_p=*/true);
26241 /* Give this enumeration constant the correct access. */
26242 set_current_access_from_decl (decl
);
26244 /* Actually build the enumerator itself. Here we're assuming that
26245 enumerators can't have dependent attributes. */
26246 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26247 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26250 if (SCOPED_ENUM_P (newtag
))
26253 finish_enum_value_list (newtag
);
26254 finish_enum (newtag
);
26256 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26257 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26260 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26261 its type -- but without substituting the innermost set of template
26262 arguments. So, innermost set of template parameters will appear in
26266 get_mostly_instantiated_function_type (tree decl
)
26268 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26269 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26272 /* Return truthvalue if we're processing a template different from
26273 the last one involved in diagnostics. */
26275 problematic_instantiation_changed (void)
26277 return current_tinst_level
!= last_error_tinst_level
;
26280 /* Remember current template involved in diagnostics. */
26282 record_last_problematic_instantiation (void)
26284 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26287 struct tinst_level
*
26288 current_instantiation (void)
26290 return current_tinst_level
;
26293 /* Return TRUE if current_function_decl is being instantiated, false
26297 instantiating_current_function_p (void)
26299 return (current_instantiation ()
26300 && (current_instantiation ()->maybe_get_node ()
26301 == current_function_decl
));
26304 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26305 type. Return false for ok, true for disallowed. Issue error and
26306 inform messages under control of COMPLAIN. */
26309 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26311 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26313 else if (TYPE_PTR_P (type
))
26315 else if (TYPE_REF_P (type
)
26316 && !TYPE_REF_IS_RVALUE (type
))
26318 else if (TYPE_PTRMEM_P (type
))
26320 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26322 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26324 if (complain
& tf_error
)
26325 error ("non-type template parameters of deduced class type only "
26326 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26331 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26333 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26335 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26337 /* A bound template template parm could later be instantiated to have a valid
26338 nontype parm type via an alias template. */
26339 else if (cxx_dialect
>= cxx11
26340 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26342 else if (VOID_TYPE_P (type
))
26343 /* Fall through. */;
26344 else if (cxx_dialect
>= cxx20
)
26346 if (dependent_type_p (type
))
26348 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26350 if (structural_type_p (type
))
26352 if (complain
& tf_error
)
26354 auto_diagnostic_group d
;
26355 error ("%qT is not a valid type for a template non-type "
26356 "parameter because it is not structural", type
);
26357 structural_type_p (type
, true);
26361 else if (CLASS_TYPE_P (type
))
26363 if (complain
& tf_error
)
26364 error ("non-type template parameters of class type only available "
26365 "with %<-std=c++20%> or %<-std=gnu++20%>");
26369 if (complain
& tf_error
)
26371 if (type
== error_mark_node
)
26372 inform (input_location
, "invalid template non-type parameter");
26374 error ("%q#T is not a valid type for a template non-type parameter",
26380 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26381 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26384 dependent_type_p_r (tree type
)
26390 A type is dependent if it is:
26392 -- a template parameter. Template template parameters are types
26393 for us (since TYPE_P holds true for them) so we handle
26395 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26396 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26398 /* -- a qualified-id with a nested-name-specifier which contains a
26399 class-name that names a dependent type or whose unqualified-id
26400 names a dependent type. */
26401 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26404 /* An alias template specialization can be dependent even if the
26405 resulting type is not. */
26406 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26409 /* -- a cv-qualified type where the cv-unqualified type is
26411 No code is necessary for this bullet; the code below handles
26412 cv-qualified types, and we don't want to strip aliases with
26413 TYPE_MAIN_VARIANT because of DR 1558. */
26414 /* -- a compound type constructed from any dependent type. */
26415 if (TYPE_PTRMEM_P (type
))
26416 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26417 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26419 else if (INDIRECT_TYPE_P (type
))
26420 return dependent_type_p (TREE_TYPE (type
));
26421 else if (FUNC_OR_METHOD_TYPE_P (type
))
26425 if (dependent_type_p (TREE_TYPE (type
)))
26427 for (arg_type
= TYPE_ARG_TYPES (type
);
26429 arg_type
= TREE_CHAIN (arg_type
))
26430 if (dependent_type_p (TREE_VALUE (arg_type
)))
26432 if (cxx_dialect
>= cxx17
)
26433 /* A value-dependent noexcept-specifier makes the type dependent. */
26434 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26435 if (tree noex
= TREE_PURPOSE (spec
))
26436 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26437 affect overload resolution and treating it as dependent breaks
26438 things. Same for an unparsed noexcept expression. */
26439 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26440 && TREE_CODE (noex
) != DEFERRED_PARSE
26441 && value_dependent_expression_p (noex
))
26445 /* -- an array type constructed from any dependent type or whose
26446 size is specified by a constant expression that is
26449 We checked for type- and value-dependence of the bounds in
26450 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26451 if (TREE_CODE (type
) == ARRAY_TYPE
)
26453 if (TYPE_DOMAIN (type
)
26454 && dependent_type_p (TYPE_DOMAIN (type
)))
26456 return dependent_type_p (TREE_TYPE (type
));
26459 /* -- a template-id in which either the template name is a template
26461 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26463 /* ... or any of the template arguments is a dependent type or
26464 an expression that is type-dependent or value-dependent. */
26465 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26466 && (any_dependent_template_arguments_p
26467 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26470 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26471 dependent; if the argument of the `typeof' expression is not
26472 type-dependent, then it should already been have resolved. */
26473 if (TREE_CODE (type
) == TYPEOF_TYPE
26474 || TREE_CODE (type
) == DECLTYPE_TYPE
26475 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26478 /* A template argument pack is dependent if any of its packed
26480 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26482 tree args
= ARGUMENT_PACK_ARGS (type
);
26483 int i
, len
= TREE_VEC_LENGTH (args
);
26484 for (i
= 0; i
< len
; ++i
)
26485 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26489 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26490 be template parameters. */
26491 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26494 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26497 /* The standard does not specifically mention types that are local
26498 to template functions or local classes, but they should be
26499 considered dependent too. For example:
26501 template <int I> void f() {
26506 The size of `E' cannot be known until the value of `I' has been
26507 determined. Therefore, `E' must be considered dependent. */
26508 scope
= TYPE_CONTEXT (type
);
26509 if (scope
&& TYPE_P (scope
))
26510 return dependent_type_p (scope
);
26511 /* Don't use type_dependent_expression_p here, as it can lead
26512 to infinite recursion trying to determine whether a lambda
26513 nested in a lambda is dependent (c++/47687). */
26514 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26515 && DECL_LANG_SPECIFIC (scope
)
26516 && DECL_TEMPLATE_INFO (scope
)
26517 && (any_dependent_template_arguments_p
26518 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26521 /* Other types are non-dependent. */
26525 /* Returns TRUE if TYPE is dependent, in the sense of
26526 [temp.dep.type]. Note that a NULL type is considered dependent. */
26529 dependent_type_p (tree type
)
26531 /* If there are no template parameters in scope, then there can't be
26532 any dependent types. */
26533 if (!processing_template_decl
)
26535 /* If we are not processing a template, then nobody should be
26536 providing us with a dependent type. */
26538 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26542 /* If the type is NULL, we have not computed a type for the entity
26543 in question; in that case, the type is dependent. */
26547 /* Erroneous types can be considered non-dependent. */
26548 if (type
== error_mark_node
)
26551 /* Getting here with global_type_node means we improperly called this
26552 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26553 gcc_checking_assert (type
!= global_type_node
);
26555 /* If we have not already computed the appropriate value for TYPE,
26557 if (!TYPE_DEPENDENT_P_VALID (type
))
26559 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26560 TYPE_DEPENDENT_P_VALID (type
) = 1;
26563 return TYPE_DEPENDENT_P (type
);
26566 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26567 lookup. In other words, a dependent type that is not the current
26571 dependent_scope_p (tree scope
)
26573 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26574 && !currently_open_class (scope
));
26577 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26578 an unknown base of 'this' (and is therefore instantiation-dependent). */
26581 unknown_base_ref_p (tree t
)
26583 if (!current_class_ptr
)
26586 tree mem
= TREE_OPERAND (t
, 1);
26587 if (shared_member_p (mem
))
26590 tree cur
= current_nonlambda_class_type ();
26591 if (!any_dependent_bases_p (cur
))
26594 tree ctx
= TREE_OPERAND (t
, 0);
26595 if (DERIVED_FROM_P (ctx
, cur
))
26601 /* T is a SCOPE_REF; return whether we need to consider it
26602 instantiation-dependent so that we can check access at instantiation
26603 time even though we know which member it resolves to. */
26606 instantiation_dependent_scope_ref_p (tree t
)
26608 if (DECL_P (TREE_OPERAND (t
, 1))
26609 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26610 && !unknown_base_ref_p (t
)
26611 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26612 TREE_OPERAND (t
, 1)))
26618 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26619 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26622 /* Note that this predicate is not appropriate for general expressions;
26623 only constant expressions (that satisfy potential_constant_expression)
26624 can be tested for value dependence. */
26627 value_dependent_expression_p (tree expression
)
26629 if (!processing_template_decl
|| expression
== NULL_TREE
)
26632 /* A type-dependent expression is also value-dependent. */
26633 if (type_dependent_expression_p (expression
))
26636 switch (TREE_CODE (expression
))
26639 /* A dependent member function of the current instantiation. */
26640 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26642 case FUNCTION_DECL
:
26643 /* A dependent member function of the current instantiation. */
26644 if (DECL_CLASS_SCOPE_P (expression
)
26645 && dependent_type_p (DECL_CONTEXT (expression
)))
26649 case IDENTIFIER_NODE
:
26650 /* A name that has not been looked up -- must be dependent. */
26653 case TEMPLATE_PARM_INDEX
:
26654 /* A non-type template parm. */
26658 /* A non-type template parm. */
26659 if (DECL_TEMPLATE_PARM_P (expression
))
26661 return value_dependent_expression_p (DECL_INITIAL (expression
));
26664 /* A constant with literal type and is initialized
26665 with an expression that is value-dependent. */
26666 if (DECL_DEPENDENT_INIT_P (expression
)
26667 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26668 || TYPE_REF_P (TREE_TYPE (expression
)))
26670 if (DECL_HAS_VALUE_EXPR_P (expression
))
26672 tree value_expr
= DECL_VALUE_EXPR (expression
);
26673 if (value_dependent_expression_p (value_expr
)
26674 /* __PRETTY_FUNCTION__ inside a template function is dependent
26675 on the name of the function. */
26676 || (DECL_PRETTY_FUNCTION_P (expression
)
26677 /* It might be used in a template, but not a template
26678 function, in which case its DECL_VALUE_EXPR will be
26680 && value_expr
== error_mark_node
))
26685 case DYNAMIC_CAST_EXPR
:
26686 case STATIC_CAST_EXPR
:
26687 case CONST_CAST_EXPR
:
26688 case REINTERPRET_CAST_EXPR
:
26690 case IMPLICIT_CONV_EXPR
:
26691 /* These expressions are value-dependent if the type to which
26692 the cast occurs is dependent or the expression being casted
26693 is value-dependent. */
26695 tree type
= TREE_TYPE (expression
);
26697 if (dependent_type_p (type
))
26700 /* A functional cast has a list of operands. */
26701 expression
= TREE_OPERAND (expression
, 0);
26704 /* If there are no operands, it must be an expression such
26705 as "int()". This should not happen for aggregate types
26706 because it would form non-constant expressions. */
26707 gcc_assert (cxx_dialect
>= cxx11
26708 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26713 if (TREE_CODE (expression
) == TREE_LIST
)
26714 return any_value_dependent_elements_p (expression
);
26716 return value_dependent_expression_p (expression
);
26720 if (SIZEOF_EXPR_TYPE_P (expression
))
26721 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26725 /* A `sizeof' expression is value-dependent if the operand is
26726 type-dependent or is a pack expansion. */
26727 expression
= TREE_OPERAND (expression
, 0);
26728 if (PACK_EXPANSION_P (expression
))
26730 else if (TYPE_P (expression
))
26731 return dependent_type_p (expression
);
26732 return instantiation_dependent_uneval_expression_p (expression
);
26734 case AT_ENCODE_EXPR
:
26735 /* An 'encode' expression is value-dependent if the operand is
26737 expression
= TREE_OPERAND (expression
, 0);
26738 return dependent_type_p (expression
);
26740 case NOEXCEPT_EXPR
:
26741 expression
= TREE_OPERAND (expression
, 0);
26742 return instantiation_dependent_uneval_expression_p (expression
);
26745 /* All instantiation-dependent expressions should also be considered
26746 value-dependent. */
26747 return instantiation_dependent_scope_ref_p (expression
);
26749 case COMPONENT_REF
:
26750 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26751 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26753 case NONTYPE_ARGUMENT_PACK
:
26754 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26755 is value-dependent. */
26757 tree values
= ARGUMENT_PACK_ARGS (expression
);
26758 int i
, len
= TREE_VEC_LENGTH (values
);
26760 for (i
= 0; i
< len
; ++i
)
26761 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26769 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26771 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26777 if (TREE_CODE (type2
) != TREE_LIST
)
26778 return dependent_type_p (type2
);
26780 for (; type2
; type2
= TREE_CHAIN (type2
))
26781 if (dependent_type_p (TREE_VALUE (type2
)))
26788 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26789 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26792 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26793 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26797 tree op
= TREE_OPERAND (expression
, 0);
26798 return (value_dependent_expression_p (op
)
26799 || has_value_dependent_address (op
));
26802 case REQUIRES_EXPR
:
26803 /* Treat all requires-expressions as value-dependent so
26804 we don't try to fold them. */
26808 return dependent_type_p (TREE_OPERAND (expression
, 0));
26812 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26814 tree fn
= get_callee_fndecl (expression
);
26816 nargs
= call_expr_nargs (expression
);
26817 for (i
= 0; i
< nargs
; ++i
)
26819 tree op
= CALL_EXPR_ARG (expression
, i
);
26820 /* In a call to a constexpr member function, look through the
26821 implicit ADDR_EXPR on the object argument so that it doesn't
26822 cause the call to be considered value-dependent. We also
26823 look through it in potential_constant_expression. */
26824 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26825 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26826 && TREE_CODE (op
) == ADDR_EXPR
)
26827 op
= TREE_OPERAND (op
, 0);
26828 if (value_dependent_expression_p (op
))
26834 case TEMPLATE_ID_EXPR
:
26835 return concept_definition_p (TREE_OPERAND (expression
, 0));
26841 if (dependent_type_p (TREE_TYPE (expression
)))
26843 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26844 if (value_dependent_expression_p (val
))
26850 /* Treat a GNU statement expression as dependent to avoid crashing
26851 under instantiate_non_dependent_expr; it can't be constant. */
26855 /* A constant expression is value-dependent if any subexpression is
26856 value-dependent. */
26857 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26859 case tcc_reference
:
26861 case tcc_comparison
:
26863 case tcc_expression
:
26866 int i
, len
= cp_tree_operand_length (expression
);
26868 for (i
= 0; i
< len
; i
++)
26870 tree t
= TREE_OPERAND (expression
, i
);
26872 /* In some cases, some of the operands may be missing.
26873 (For example, in the case of PREDECREMENT_EXPR, the
26874 amount to increment by may be missing.) That doesn't
26875 make the expression dependent. */
26876 if (t
&& value_dependent_expression_p (t
))
26887 /* The expression is not value-dependent. */
26891 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26892 [temp.dep.expr]. Note that an expression with no type is
26893 considered dependent. Other parts of the compiler arrange for an
26894 expression with type-dependent subexpressions to have no type, so
26895 this function doesn't have to be fully recursive. */
26898 type_dependent_expression_p (tree expression
)
26900 if (!processing_template_decl
)
26903 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26906 STRIP_ANY_LOCATION_WRAPPER (expression
);
26908 /* An unresolved name is always dependent. */
26909 if (identifier_p (expression
)
26910 || TREE_CODE (expression
) == USING_DECL
26911 || TREE_CODE (expression
) == WILDCARD_DECL
)
26914 /* A lambda-expression in template context is dependent. dependent_type_p is
26915 true for a lambda in the scope of a class or function template, but that
26916 doesn't cover all template contexts, like a default template argument. */
26917 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26920 /* A fold expression is type-dependent. */
26921 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26922 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26923 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26924 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26927 /* Some expression forms are never type-dependent. */
26928 if (TREE_CODE (expression
) == SIZEOF_EXPR
26929 || TREE_CODE (expression
) == ALIGNOF_EXPR
26930 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26931 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26932 || TREE_CODE (expression
) == TRAIT_EXPR
26933 || TREE_CODE (expression
) == TYPEID_EXPR
26934 || TREE_CODE (expression
) == DELETE_EXPR
26935 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26936 || TREE_CODE (expression
) == THROW_EXPR
26937 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26940 /* The types of these expressions depends only on the type to which
26941 the cast occurs. */
26942 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26943 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26944 || TREE_CODE (expression
) == CONST_CAST_EXPR
26945 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26946 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26947 || TREE_CODE (expression
) == CAST_EXPR
)
26948 return dependent_type_p (TREE_TYPE (expression
));
26950 /* The types of these expressions depends only on the type created
26951 by the expression. */
26952 if (TREE_CODE (expression
) == NEW_EXPR
26953 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26955 /* For NEW_EXPR tree nodes created inside a template, either
26956 the object type itself or a TREE_LIST may appear as the
26958 tree type
= TREE_OPERAND (expression
, 1);
26959 if (TREE_CODE (type
) == TREE_LIST
)
26960 /* This is an array type. We need to check array dimensions
26962 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26963 || value_dependent_expression_p
26964 (TREE_OPERAND (TREE_VALUE (type
), 1));
26965 /* Array type whose dimension has to be deduced. */
26966 else if (TREE_CODE (type
) == ARRAY_TYPE
26967 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
26970 return dependent_type_p (type
);
26973 if (TREE_CODE (expression
) == SCOPE_REF
)
26975 tree scope
= TREE_OPERAND (expression
, 0);
26976 tree name
= TREE_OPERAND (expression
, 1);
26978 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26979 contains an identifier associated by name lookup with one or more
26980 declarations declared with a dependent type, or...a
26981 nested-name-specifier or qualified-id that names a member of an
26982 unknown specialization. */
26983 return (type_dependent_expression_p (name
)
26984 || dependent_scope_p (scope
));
26987 if (TREE_CODE (expression
) == TEMPLATE_DECL
26988 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26989 return uses_outer_template_parms (expression
);
26991 if (TREE_CODE (expression
) == STMT_EXPR
)
26992 expression
= stmt_expr_value_expr (expression
);
26994 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26999 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
27001 if (type_dependent_expression_p (elt
))
27007 /* A static data member of the current instantiation with incomplete
27008 array type is type-dependent, as the definition and specializations
27009 can have different bounds. */
27010 if (VAR_P (expression
)
27011 && DECL_CLASS_SCOPE_P (expression
)
27012 && dependent_type_p (DECL_CONTEXT (expression
))
27013 && VAR_HAD_UNKNOWN_BOUND (expression
))
27016 /* An array of unknown bound depending on a variadic parameter, eg:
27018 template<typename... Args>
27019 void foo (Args... args)
27021 int arr[] = { args... };
27024 template<int... vals>
27027 int arr[] = { vals... };
27030 If the array has no length and has an initializer, it must be that
27031 we couldn't determine its length in cp_complete_array_type because
27032 it is dependent. */
27033 if (VAR_P (expression
)
27034 && TREE_TYPE (expression
) != NULL_TREE
27035 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
27036 && !TYPE_DOMAIN (TREE_TYPE (expression
))
27037 && DECL_INITIAL (expression
))
27040 /* A function or variable template-id is type-dependent if it has any
27041 dependent template arguments. */
27042 if (VAR_OR_FUNCTION_DECL_P (expression
)
27043 && DECL_LANG_SPECIFIC (expression
)
27044 && DECL_TEMPLATE_INFO (expression
))
27046 /* Consider the innermost template arguments, since those are the ones
27047 that come from the template-id; the template arguments for the
27048 enclosing class do not make it type-dependent unless they are used in
27049 the type of the decl. */
27050 if (instantiates_primary_template_p (expression
)
27051 && (any_dependent_template_arguments_p
27052 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27056 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27057 type-dependent. Checking this is important for functions with auto return
27058 type, which looks like a dependent type. */
27059 if (TREE_CODE (expression
) == FUNCTION_DECL
27060 && !(DECL_CLASS_SCOPE_P (expression
)
27061 && dependent_type_p (DECL_CONTEXT (expression
)))
27062 && !(DECL_LANG_SPECIFIC (expression
)
27063 && DECL_UNIQUE_FRIEND_P (expression
)
27064 && (!DECL_FRIEND_CONTEXT (expression
)
27065 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27066 && !DECL_LOCAL_DECL_P (expression
))
27068 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27069 || undeduced_auto_decl (expression
));
27073 /* Always dependent, on the number of arguments if nothing else. */
27074 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27077 if (TREE_TYPE (expression
) == unknown_type_node
)
27079 if (TREE_CODE (expression
) == ADDR_EXPR
)
27080 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27081 if (TREE_CODE (expression
) == COMPONENT_REF
27082 || TREE_CODE (expression
) == OFFSET_REF
)
27084 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27086 expression
= TREE_OPERAND (expression
, 1);
27087 if (identifier_p (expression
))
27090 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27091 if (TREE_CODE (expression
) == SCOPE_REF
)
27094 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27095 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27096 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27099 if (BASELINK_P (expression
))
27101 if (BASELINK_OPTYPE (expression
)
27102 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27104 expression
= BASELINK_FUNCTIONS (expression
);
27107 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27109 if (any_dependent_template_arguments_p
27110 (TREE_OPERAND (expression
, 1)))
27112 expression
= TREE_OPERAND (expression
, 0);
27113 if (identifier_p (expression
))
27117 gcc_assert (OVL_P (expression
));
27119 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27120 if (type_dependent_expression_p (*iter
))
27126 /* The type of a non-type template parm declared with a placeholder type
27127 depends on the corresponding template argument, even though
27128 placeholders are not normally considered dependent. */
27129 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27130 && is_auto (TREE_TYPE (expression
)))
27133 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27135 /* Dependent type attributes might not have made it from the decl to
27137 if (DECL_P (expression
)
27138 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27141 return (dependent_type_p (TREE_TYPE (expression
)));
27144 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27145 type-dependent if the expression refers to a member of the current
27146 instantiation and the type of the referenced member is dependent, or the
27147 class member access expression refers to a member of an unknown
27150 This function returns true if the OBJECT in such a class member access
27151 expression is of an unknown specialization. */
27154 type_dependent_object_expression_p (tree object
)
27156 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27158 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27160 tree scope
= TREE_TYPE (object
);
27161 return (!scope
|| dependent_scope_p (scope
));
27164 /* walk_tree callback function for instantiation_dependent_expression_p,
27165 below. Returns non-zero if a dependent subexpression is found. */
27168 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27173 /* We don't have to worry about decltype currently because decltype
27174 of an instantiation-dependent expr is a dependent type. This
27175 might change depending on the resolution of DR 1172. */
27176 *walk_subtrees
= false;
27179 enum tree_code code
= TREE_CODE (*tp
);
27182 /* Don't treat an argument list as dependent just because it has no
27186 case NONTYPE_ARGUMENT_PACK
:
27189 case TEMPLATE_PARM_INDEX
:
27190 if (dependent_type_p (TREE_TYPE (*tp
)))
27192 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27194 /* We'll check value-dependence separately. */
27197 /* Handle expressions with type operands. */
27201 case AT_ENCODE_EXPR
:
27203 tree op
= TREE_OPERAND (*tp
, 0);
27204 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27205 op
= TREE_TYPE (op
);
27208 if (dependent_type_p (op
))
27212 *walk_subtrees
= false;
27219 case COMPONENT_REF
:
27220 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27221 /* In a template, finish_class_member_access_expr creates a
27222 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27223 type-dependent, so that we can check access control at
27224 instantiation time (PR 42277). See also Core issue 1273. */
27229 if (instantiation_dependent_scope_ref_p (*tp
))
27234 /* Treat statement-expressions as dependent. */
27238 /* Treat requires-expressions as dependent. */
27239 case REQUIRES_EXPR
:
27243 /* Treat concept checks as dependent. */
27244 if (concept_check_p (*tp
))
27248 case TEMPLATE_ID_EXPR
:
27249 /* Treat concept checks as dependent. */
27250 if (concept_check_p (*tp
))
27255 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27263 if (type_dependent_expression_p (*tp
))
27269 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27270 sense defined by the ABI:
27272 "An expression is instantiation-dependent if it is type-dependent
27273 or value-dependent, or it has a subexpression that is type-dependent
27274 or value-dependent."
27276 Except don't actually check value-dependence for unevaluated expressions,
27277 because in sizeof(i) we don't care about the value of i. Checking
27278 type-dependence will in turn check value-dependence of array bounds/template
27279 arguments as needed. */
27282 instantiation_dependent_uneval_expression_p (tree expression
)
27286 if (!processing_template_decl
)
27289 if (expression
== error_mark_node
)
27292 result
= cp_walk_tree_without_duplicates (&expression
,
27293 instantiation_dependent_r
, NULL
);
27294 return result
!= NULL_TREE
;
27297 /* As above, but also check value-dependence of the expression as a whole. */
27300 instantiation_dependent_expression_p (tree expression
)
27302 return (instantiation_dependent_uneval_expression_p (expression
)
27303 || (potential_constant_expression (expression
)
27304 && value_dependent_expression_p (expression
)));
27307 /* Like type_dependent_expression_p, but it also works while not processing
27308 a template definition, i.e. during substitution or mangling. */
27311 type_dependent_expression_p_push (tree expr
)
27314 ++processing_template_decl
;
27315 b
= type_dependent_expression_p (expr
);
27316 --processing_template_decl
;
27320 /* Returns TRUE if ARGS contains a type-dependent expression. */
27323 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27328 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27330 if (type_dependent_expression_p (arg
))
27336 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27337 expressions) contains any type-dependent expressions. */
27340 any_type_dependent_elements_p (const_tree list
)
27342 for (; list
; list
= TREE_CHAIN (list
))
27343 if (type_dependent_expression_p (TREE_VALUE (list
)))
27349 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27350 expressions) contains any value-dependent expressions. */
27353 any_value_dependent_elements_p (const_tree list
)
27355 for (; list
; list
= TREE_CHAIN (list
))
27356 if (value_dependent_expression_p (TREE_VALUE (list
)))
27362 /* Returns TRUE if the ARG (a template argument) is dependent. */
27365 dependent_template_arg_p (tree arg
)
27367 if (!processing_template_decl
)
27370 /* Assume a template argument that was wrongly written by the user
27371 is dependent. This is consistent with what
27372 any_dependent_template_arguments_p [that calls this function]
27374 if (!arg
|| arg
== error_mark_node
)
27377 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27378 arg
= argument_pack_select_arg (arg
);
27380 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27382 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27384 if (DECL_TEMPLATE_PARM_P (arg
))
27386 /* A member template of a dependent class is not necessarily
27387 type-dependent, but it is a dependent template argument because it
27388 will be a member of an unknown specialization to that template. */
27389 tree scope
= CP_DECL_CONTEXT (arg
);
27390 return TYPE_P (scope
) && dependent_type_p (scope
);
27392 else if (ARGUMENT_PACK_P (arg
))
27394 tree args
= ARGUMENT_PACK_ARGS (arg
);
27395 int i
, len
= TREE_VEC_LENGTH (args
);
27396 for (i
= 0; i
< len
; ++i
)
27398 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27404 else if (TYPE_P (arg
))
27405 return dependent_type_p (arg
);
27407 return value_dependent_expression_p (arg
);
27410 /* Returns true if ARGS (a collection of template arguments) contains
27411 any types that require structural equality testing. */
27414 any_template_arguments_need_structural_equality_p (tree args
)
27421 if (args
== error_mark_node
)
27424 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27426 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27427 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27429 tree arg
= TREE_VEC_ELT (level
, j
);
27430 tree packed_args
= NULL_TREE
;
27433 if (ARGUMENT_PACK_P (arg
))
27435 /* Look inside the argument pack. */
27436 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27437 len
= TREE_VEC_LENGTH (packed_args
);
27440 for (k
= 0; k
< len
; ++k
)
27443 arg
= TREE_VEC_ELT (packed_args
, k
);
27445 if (error_operand_p (arg
))
27447 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27449 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27451 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27452 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27461 /* Returns true if ARGS (a collection of template arguments) contains
27462 any dependent arguments. */
27465 any_dependent_template_arguments_p (const_tree args
)
27472 if (args
== error_mark_node
)
27475 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27477 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27478 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27479 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27486 /* Returns true if ARGS contains any errors. */
27489 any_erroneous_template_args_p (const_tree args
)
27494 if (args
== error_mark_node
)
27497 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27499 if (tree ti
= get_template_info (args
))
27500 args
= TI_ARGS (ti
);
27508 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27510 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27511 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27512 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27519 /* Returns TRUE if the template TMPL is type-dependent. */
27522 dependent_template_p (tree tmpl
)
27524 if (TREE_CODE (tmpl
) == OVERLOAD
)
27526 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27527 if (dependent_template_p (*iter
))
27532 /* Template template parameters are dependent. */
27533 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27534 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27536 /* So are names that have not been looked up. */
27537 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27542 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27545 dependent_template_id_p (tree tmpl
, tree args
)
27547 return (dependent_template_p (tmpl
)
27548 || any_dependent_template_arguments_p (args
));
27551 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27555 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27559 if (!processing_template_decl
)
27562 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27564 tree decl
= TREE_VEC_ELT (declv
, i
);
27565 tree init
= TREE_VEC_ELT (initv
, i
);
27566 tree cond
= TREE_VEC_ELT (condv
, i
);
27567 tree incr
= TREE_VEC_ELT (incrv
, i
);
27569 if (type_dependent_expression_p (decl
)
27570 || TREE_CODE (decl
) == SCOPE_REF
)
27573 if (init
&& type_dependent_expression_p (init
))
27576 if (cond
== global_namespace
)
27579 if (type_dependent_expression_p (cond
))
27582 if (COMPARISON_CLASS_P (cond
)
27583 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27584 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27587 if (TREE_CODE (incr
) == MODOP_EXPR
)
27589 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27590 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27593 else if (type_dependent_expression_p (incr
))
27595 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27597 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27599 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27601 tree t
= TREE_OPERAND (incr
, 1);
27602 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27603 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27606 /* If this loop has a class iterator with != comparison
27607 with increment other than i++/++i/i--/--i, make sure the
27608 increment is constant. */
27609 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27610 && TREE_CODE (cond
) == NE_EXPR
)
27612 if (TREE_OPERAND (t
, 0) == decl
)
27613 t
= TREE_OPERAND (t
, 1);
27615 t
= TREE_OPERAND (t
, 0);
27616 if (TREE_CODE (t
) != INTEGER_CST
)
27626 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27627 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27628 no such TYPE can be found. Note that this function peers inside
27629 uninstantiated templates and therefore should be used only in
27630 extremely limited situations. ONLY_CURRENT_P restricts this
27631 peering to the currently open classes hierarchy (which is required
27632 when comparing types). */
27635 resolve_typename_type (tree type
, bool only_current_p
)
27644 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27646 scope
= TYPE_CONTEXT (type
);
27647 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27648 gcc_checking_assert (uses_template_parms (scope
));
27650 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27651 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27652 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27653 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27654 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27655 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27656 the TYPENAME_TYPE instead, we avoid messing up with a possible
27657 typedef variant case. */
27658 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27660 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27661 it first before we can figure out what NAME refers to. */
27662 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27664 if (TYPENAME_IS_RESOLVING_P (scope
))
27665 /* Given a class template A with a dependent base with nested type C,
27666 typedef typename A::C::C C will land us here, as trying to resolve
27667 the initial A::C leads to the local C typedef, which leads back to
27668 A::C::C. So we break the recursion now. */
27671 scope
= resolve_typename_type (scope
, only_current_p
);
27673 /* If we don't know what SCOPE refers to, then we cannot resolve the
27675 if (!CLASS_TYPE_P (scope
))
27677 /* If this is a typedef, we don't want to look inside (c++/11987). */
27678 if (typedef_variant_p (type
))
27680 /* If SCOPE isn't the template itself, it will not have a valid
27681 TYPE_FIELDS list. */
27682 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27683 /* scope is either the template itself or a compatible instantiation
27684 like X<T>, so look up the name in the original template. */
27685 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27686 /* If scope has no fields, it can't be a current instantiation. Check this
27687 before currently_open_class to avoid infinite recursion (71515). */
27688 if (!TYPE_FIELDS (scope
))
27690 /* If the SCOPE is not the current instantiation, there's no reason
27691 to look inside it. */
27692 if (only_current_p
&& !currently_open_class (scope
))
27694 /* Enter the SCOPE so that name lookup will be resolved as if we
27695 were in the class definition. In particular, SCOPE will no
27696 longer be considered a dependent type. */
27697 pushed_scope
= push_scope (scope
);
27698 /* Look up the declaration. */
27699 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27700 tf_warning_or_error
);
27702 result
= NULL_TREE
;
27704 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27705 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27706 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27709 else if (identifier_p (fullname
)
27710 && TREE_CODE (decl
) == TYPE_DECL
)
27712 result
= TREE_TYPE (decl
);
27713 if (result
== error_mark_node
)
27714 result
= NULL_TREE
;
27716 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27717 && DECL_CLASS_TEMPLATE_P (decl
))
27719 /* Obtain the template and the arguments. */
27720 tree tmpl
= TREE_OPERAND (fullname
, 0);
27721 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27723 /* We get here with a plain identifier because a previous tentative
27724 parse of the nested-name-specifier as part of a ptr-operator saw
27725 ::template X<A>. The use of ::template is necessary in a
27726 ptr-operator, but wrong in a declarator-id.
27728 [temp.names]: In a qualified-id of a declarator-id, the keyword
27729 template shall not appear at the top level. */
27730 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27731 "keyword %<template%> not allowed in declarator-id");
27734 tree args
= TREE_OPERAND (fullname
, 1);
27735 /* Instantiate the template. */
27736 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27737 /*entering_scope=*/true,
27738 tf_error
| tf_user
);
27739 if (result
== error_mark_node
)
27740 result
= NULL_TREE
;
27743 /* Leave the SCOPE. */
27745 pop_scope (pushed_scope
);
27747 /* If we failed to resolve it, return the original typename. */
27751 /* If lookup found a typename type, resolve that too. */
27752 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27754 /* Ill-formed programs can cause infinite recursion here, so we
27755 must catch that. */
27756 TYPENAME_IS_RESOLVING_P (result
) = 1;
27757 result
= resolve_typename_type (result
, only_current_p
);
27758 TYPENAME_IS_RESOLVING_P (result
) = 0;
27761 /* Qualify the resulting type. */
27762 quals
= cp_type_quals (type
);
27764 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27769 /* EXPR is an expression which is not type-dependent. Return a proxy
27770 for EXPR that can be used to compute the types of larger
27771 expressions containing EXPR. */
27774 build_non_dependent_expr (tree expr
)
27776 tree orig_expr
= expr
;
27779 /* When checking, try to get a constant value for all non-dependent
27780 expressions in order to expose bugs in *_dependent_expression_p
27781 and constexpr. This can affect code generation, see PR70704, so
27782 only do this for -fchecking=2. */
27783 if (flag_checking
> 1
27784 && cxx_dialect
>= cxx11
27785 /* Don't do this during nsdmi parsing as it can lead to
27786 unexpected recursive instantiations. */
27787 && !parsing_nsdmi ()
27788 /* Don't do this during concept processing either and for
27789 the same reason. */
27790 && !processing_constraint_expression_p ())
27791 fold_non_dependent_expr (expr
, tf_none
);
27793 STRIP_ANY_LOCATION_WRAPPER (expr
);
27795 /* Preserve OVERLOADs; the functions must be available to resolve
27798 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27799 inner_expr
= stmt_expr_value_expr (inner_expr
);
27800 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27801 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27802 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27803 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27804 if (is_overloaded_fn (inner_expr
)
27805 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27807 /* There is no need to return a proxy for a variable or enumerator. */
27808 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27810 /* Preserve string constants; conversions from string constants to
27811 "char *" are allowed, even though normally a "const char *"
27812 cannot be used to initialize a "char *". */
27813 if (TREE_CODE (expr
) == STRING_CST
)
27815 /* Preserve void and arithmetic constants, as an optimization -- there is no
27816 reason to create a new node. */
27817 if (TREE_CODE (expr
) == VOID_CST
27818 || TREE_CODE (expr
) == INTEGER_CST
27819 || TREE_CODE (expr
) == REAL_CST
)
27821 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27822 There is at least one place where we want to know that a
27823 particular expression is a throw-expression: when checking a ?:
27824 expression, there are special rules if the second or third
27825 argument is a throw-expression. */
27826 if (TREE_CODE (expr
) == THROW_EXPR
)
27829 /* Don't wrap an initializer list, we need to be able to look inside. */
27830 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27833 /* Don't wrap a dummy object, we need to be able to test for it. */
27834 if (is_dummy_object (expr
))
27837 if (TREE_CODE (expr
) == COND_EXPR
)
27838 return build3 (COND_EXPR
,
27840 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27841 (TREE_OPERAND (expr
, 1)
27842 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27843 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27844 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27845 if (TREE_CODE (expr
) == COMPOUND_EXPR
27846 && !COMPOUND_EXPR_OVERLOADED (expr
))
27847 return build2 (COMPOUND_EXPR
,
27849 TREE_OPERAND (expr
, 0),
27850 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27852 /* If the type is unknown, it can't really be non-dependent */
27853 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27855 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27856 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27857 TREE_TYPE (expr
), expr
);
27860 /* ARGS is a vector of expressions as arguments to a function call.
27861 Replace the arguments with equivalent non-dependent expressions.
27862 This modifies ARGS in place. */
27865 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27870 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27872 tree newarg
= build_non_dependent_expr (arg
);
27874 (*args
)[ix
] = newarg
;
27878 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27879 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27880 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27883 make_auto_1 (tree name
, bool set_canonical
)
27885 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27886 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27887 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27888 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27889 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27890 TYPE_NAME (au
), NULL_TREE
);
27892 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27893 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27894 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27895 if (name
== decltype_auto_identifier
)
27896 AUTO_IS_DECLTYPE (au
) = true;
27902 make_decltype_auto (void)
27904 return make_auto_1 (decltype_auto_identifier
, true);
27910 return make_auto_1 (auto_identifier
, true);
27913 /* Return a C++17 deduction placeholder for class template TMPL. */
27916 make_template_placeholder (tree tmpl
)
27918 tree t
= make_auto_1 (auto_identifier
, false);
27919 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27920 /* Our canonical type depends on the placeholder. */
27921 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27925 /* True iff T is a C++17 class template deduction placeholder. */
27928 template_placeholder_p (tree t
)
27930 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27933 /* Make a "constrained auto" type-specifier. This is an auto or
27934 decltype(auto) type with constraints that must be associated after
27935 deduction. The constraint is formed from the given concept CON
27936 and its optional sequence of template arguments ARGS.
27938 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27941 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27943 /* Build the constraint. */
27944 tree tmpl
= DECL_TI_TEMPLATE (con
);
27946 if (TREE_CODE (con
) == FUNCTION_DECL
)
27947 expr
= ovl_make (tmpl
);
27948 ++processing_template_decl
;
27949 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27950 --processing_template_decl
;
27952 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27954 /* Our canonical type depends on the constraint. */
27955 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27957 /* Attach the constraint to the type declaration. */
27958 return TYPE_NAME (type
);
27961 /* Make a "constrained auto" type-specifier. */
27964 make_constrained_auto (tree con
, tree args
)
27966 tree type
= make_auto_1 (auto_identifier
, false);
27967 return make_constrained_placeholder_type (type
, con
, args
);
27970 /* Make a "constrained decltype(auto)" type-specifier. */
27973 make_constrained_decltype_auto (tree con
, tree args
)
27975 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27976 return make_constrained_placeholder_type (type
, con
, args
);
27979 /* Build and return a concept definition. Like other templates, the
27980 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27981 the TEMPLATE_DECL. */
27984 finish_concept_definition (cp_expr id
, tree init
)
27986 gcc_assert (identifier_p (id
));
27987 gcc_assert (processing_template_decl
);
27989 location_t loc
= id
.get_location();
27991 /* A concept-definition shall not have associated constraints. */
27992 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27994 error_at (loc
, "a concept cannot be constrained");
27995 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27998 /* A concept-definition shall appear in namespace scope. Templates
27999 aren't allowed in block scope, so we only need to check for class
28001 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28003 error_at (loc
, "concept %qE not in namespace scope", *id
);
28004 return error_mark_node
;
28007 /* Initially build the concept declaration; its type is bool. */
28008 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
28009 DECL_CONTEXT (decl
) = current_scope ();
28010 DECL_INITIAL (decl
) = init
;
28012 /* Push the enclosing template. */
28013 return push_template_decl (decl
);
28016 /* Given type ARG, return std::initializer_list<ARG>. */
28021 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
28023 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
28025 gcc_rich_location
richloc (input_location
);
28026 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
28027 error_at (&richloc
,
28028 "deducing from brace-enclosed initializer list"
28029 " requires %<#include <initializer_list>%>");
28031 return error_mark_node
;
28033 tree argvec
= make_tree_vec (1);
28034 TREE_VEC_ELT (argvec
, 0) = arg
;
28036 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
28037 NULL_TREE
, 0, tf_warning_or_error
);
28040 /* Replace auto in TYPE with std::initializer_list<auto>. */
28043 listify_autos (tree type
, tree auto_node
)
28045 tree init_auto
= listify (strip_top_quals (auto_node
));
28046 tree argvec
= make_tree_vec (1);
28047 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28048 if (processing_template_decl
)
28049 argvec
= add_to_template_args (current_template_args (), argvec
);
28050 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28053 /* Hash traits for hashing possibly constrained 'auto'
28054 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28056 struct auto_hash
: default_hash_traits
<tree
>
28058 static inline hashval_t
hash (tree
);
28059 static inline bool equal (tree
, tree
);
28062 /* Hash the 'auto' T. */
28065 auto_hash::hash (tree t
)
28067 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28068 /* Matching constrained-type-specifiers denote the same template
28069 parameter, so hash the constraint. */
28070 return hash_placeholder_constraint (c
);
28072 /* But unconstrained autos are all separate, so just hash the pointer. */
28073 return iterative_hash_object (t
, 0);
28076 /* Compare two 'auto's. */
28079 auto_hash::equal (tree t1
, tree t2
)
28084 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28085 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28087 /* Two unconstrained autos are distinct. */
28091 return equivalent_placeholder_constraints (c1
, c2
);
28094 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28095 constrained) auto, add it to the vector. */
28098 extract_autos_r (tree t
, void *data
)
28100 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28103 /* All the autos were built with index 0; fix that up now. */
28104 tree
*p
= hash
.find_slot (t
, INSERT
);
28107 /* If this is a repeated constrained-type-specifier, use the index we
28109 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28112 /* Otherwise this is new, so use the current count. */
28114 idx
= hash
.elements () - 1;
28116 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28119 /* Always keep walking. */
28123 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28124 says they can appear anywhere in the type. */
28127 extract_autos (tree type
)
28129 hash_set
<tree
> visited
;
28130 hash_table
<auto_hash
> hash (2);
28132 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28134 tree tree_vec
= make_tree_vec (hash
.elements());
28135 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28136 iter
!= hash
.end(); ++iter
)
28139 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28140 TREE_VEC_ELT (tree_vec
, i
)
28141 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28147 /* The stem for deduction guide names. */
28148 const char *const dguide_base
= "__dguide_";
28150 /* Return the name for a deduction guide for class template TMPL. */
28153 dguide_name (tree tmpl
)
28155 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28156 tree tname
= TYPE_IDENTIFIER (type
);
28157 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28158 + IDENTIFIER_LENGTH (tname
));
28159 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28160 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28161 IDENTIFIER_LENGTH (tname
) + 1);
28162 tree dname
= get_identifier (buf
);
28163 TREE_TYPE (dname
) = type
;
28167 /* True if NAME is the name of a deduction guide. */
28170 dguide_name_p (tree name
)
28172 return (TREE_CODE (name
) == IDENTIFIER_NODE
28173 && TREE_TYPE (name
)
28174 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28175 strlen (dguide_base
)));
28178 /* True if FN is a deduction guide. */
28181 deduction_guide_p (const_tree fn
)
28184 if (tree name
= DECL_NAME (fn
))
28185 return dguide_name_p (name
);
28189 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28192 copy_guide_p (const_tree fn
)
28194 gcc_assert (deduction_guide_p (fn
));
28195 if (!DECL_ARTIFICIAL (fn
))
28197 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28198 return (TREE_CHAIN (parms
) == void_list_node
28199 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28202 /* True if FN is a guide generated from a constructor template. */
28205 template_guide_p (const_tree fn
)
28207 gcc_assert (deduction_guide_p (fn
));
28208 if (!DECL_ARTIFICIAL (fn
))
28210 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28211 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28212 return PRIMARY_TEMPLATE_P (org
);
28216 /* True if FN is an aggregate initialization guide or the copy deduction
28220 builtin_guide_p (const_tree fn
)
28222 if (!deduction_guide_p (fn
))
28224 if (!DECL_ARTIFICIAL (fn
))
28225 /* Explicitly declared. */
28227 if (DECL_ABSTRACT_ORIGIN (fn
))
28228 /* Derived from a constructor. */
28233 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28234 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28235 template parameter types. Note that the handling of template template
28236 parameters relies on current_template_parms being set appropriately for the
28240 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28241 tree tsubst_args
, tsubst_flags_t complain
)
28243 if (olddecl
== error_mark_node
)
28244 return error_mark_node
;
28246 tree oldidx
= get_template_parm_index (olddecl
);
28249 if (TREE_CODE (olddecl
) == TYPE_DECL
28250 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28252 tree oldtype
= TREE_TYPE (olddecl
);
28253 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28254 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28255 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28256 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28257 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28261 newtype
= TREE_TYPE (olddecl
);
28262 if (type_uses_auto (newtype
))
28264 // Substitute once to fix references to other template parameters.
28265 newtype
= tsubst (newtype
, tsubst_args
,
28266 complain
|tf_partial
, NULL_TREE
);
28267 // Now substitute again to reduce the level of the auto.
28268 newtype
= tsubst (newtype
, current_template_args (),
28269 complain
, NULL_TREE
);
28272 newtype
= tsubst (newtype
, tsubst_args
,
28273 complain
, NULL_TREE
);
28277 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28278 DECL_NAME (olddecl
), newtype
);
28279 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28282 if (TREE_CODE (olddecl
) == TYPE_DECL
28283 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28285 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28286 = build_template_parm_index (index
, level
, level
,
28288 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28289 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28290 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28291 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28292 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28294 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28296 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28298 DECL_TEMPLATE_RESULT (newdecl
)
28299 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28300 DECL_NAME (olddecl
), newtype
);
28301 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28302 // First create a copy (ttargs) of tsubst_args with an
28303 // additional level for the template template parameter's own
28304 // template parameters (ttparms).
28305 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28306 (DECL_TEMPLATE_PARMS (olddecl
)));
28307 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28308 tree ttargs
= make_tree_vec (depth
+ 1);
28309 for (int i
= 0; i
< depth
; ++i
)
28310 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28311 TREE_VEC_ELT (ttargs
, depth
)
28312 = template_parms_level_to_args (ttparms
);
28313 // Substitute ttargs into ttparms to fix references to
28314 // other template parameters.
28315 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28316 complain
|tf_partial
);
28317 // Now substitute again with args based on tparms, to reduce
28318 // the level of the ttparms.
28319 ttargs
= current_template_args ();
28320 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28322 // Finally, tack the adjusted parms onto tparms.
28323 ttparms
= tree_cons (size_int (depth
), ttparms
,
28324 current_template_parms
);
28325 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28330 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28332 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28333 TREE_CODE (oldconst
),
28334 DECL_NAME (oldconst
), newtype
);
28335 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28336 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28337 SET_DECL_TEMPLATE_PARM_P (newconst
);
28338 newidx
= build_template_parm_index (index
, level
, level
,
28339 newconst
, newtype
);
28340 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28341 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28342 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28348 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28349 template parameter. */
28352 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28353 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28355 tree olddecl
= TREE_VALUE (oldelt
);
28356 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28358 if (newdecl
== error_mark_node
)
28359 return error_mark_node
;
28360 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28361 targs
, complain
, NULL_TREE
);
28362 tree list
= build_tree_list (newdef
, newdecl
);
28363 TEMPLATE_PARM_CONSTRAINTS (list
)
28364 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28365 targs
, complain
, NULL_TREE
);
28366 int depth
= TMPL_ARGS_DEPTH (targs
);
28367 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28371 /* Returns a C++17 class deduction guide template based on the constructor
28372 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28373 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28374 aggregate initialization guide. */
28377 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28379 tree tparms
, targs
, fparms
, fargs
, ci
;
28380 bool memtmpl
= false;
28383 tree fn_tmpl
= NULL_TREE
;
28387 ++processing_template_decl
;
28388 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28389 --processing_template_decl
;
28392 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28396 bool copy_p
= TYPE_REF_P (ctor
);
28398 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28400 fparms
= void_list_node
;
28402 else if (TREE_CODE (ctor
) == TREE_LIST
)
28405 gcc_unreachable ();
28407 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28408 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28409 targs
= CLASSTYPE_TI_ARGS (type
);
28412 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28413 explicit_p
= false;
28417 ++processing_template_decl
;
28421 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28422 : DECL_TI_TEMPLATE (ctor
));
28424 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28425 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28427 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28428 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28429 fully specialized args for the enclosing class. Strip those off, as
28430 the deduction guide won't have those template parameters. */
28431 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28432 TMPL_PARMS_DEPTH (tparms
));
28433 /* Discard the 'this' parameter. */
28434 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28435 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28436 ci
= get_constraints (ctor
);
28437 loc
= DECL_SOURCE_LOCATION (ctor
);
28438 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28440 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28444 /* For a member template constructor, we need to flatten the two
28445 template parameter lists into one, and then adjust the function
28446 signature accordingly. This gets...complicated. */
28447 tree save_parms
= current_template_parms
;
28449 /* For a member template we should have two levels of parms/args, one
28450 for the class and one for the constructor. We stripped
28451 specialized args for further enclosing classes above. */
28452 const int depth
= 2;
28453 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28455 /* Template args for translating references to the two-level template
28456 parameters into references to the one-level template parameters we
28458 tree tsubst_args
= copy_node (targs
);
28459 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28460 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28462 /* Template parms for the constructor template. */
28463 tree ftparms
= TREE_VALUE (tparms
);
28464 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28465 /* Template parms for the class template. */
28466 tparms
= TREE_CHAIN (tparms
);
28467 tree ctparms
= TREE_VALUE (tparms
);
28468 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28469 /* Template parms for the deduction guide start as a copy of the
28470 template parms for the class. We set current_template_parms for
28471 lookup_template_class_1. */
28472 current_template_parms
= tparms
= copy_node (tparms
);
28473 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28474 for (unsigned i
= 0; i
< clen
; ++i
)
28475 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28477 /* Now we need to rewrite the constructor parms to append them to the
28479 for (unsigned i
= 0; i
< flen
; ++i
)
28481 unsigned index
= i
+ clen
;
28482 unsigned level
= 1;
28483 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28485 = rewrite_tparm_list (oldelt
, index
, level
,
28486 tsubst_args
, i
, complain
);
28487 if (newelt
== error_mark_node
)
28489 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28492 /* Now we have a final set of template parms to substitute into the
28493 function signature. */
28494 targs
= template_parms_to_args (tparms
);
28495 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28497 if (fparms
== error_mark_node
)
28500 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28502 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28503 cp_unevaluated_operand. */
28505 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28506 current_template_parms
= save_parms
;
28510 /* Substitute in the same arguments to rewrite class members into
28511 references to members of an unknown specialization. */
28513 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28514 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28516 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28519 --processing_template_decl
;
28521 return error_mark_node
;
28526 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28527 tparms
= copy_node (tparms
);
28528 INNERMOST_TEMPLATE_PARMS (tparms
)
28529 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28532 tree fntype
= build_function_type (type
, fparms
);
28533 tree ded_fn
= build_lang_decl_loc (loc
,
28535 dguide_name (type
), fntype
);
28536 DECL_ARGUMENTS (ded_fn
) = fargs
;
28537 DECL_ARTIFICIAL (ded_fn
) = true;
28538 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28539 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28540 DECL_ARTIFICIAL (ded_tmpl
) = true;
28541 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28542 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28544 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28546 set_constraints (ded_tmpl
, ci
);
28551 /* Add to LIST the member types for the reshaped initializer CTOR. */
28554 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28556 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28557 tree idx
, val
; unsigned i
;
28558 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28560 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28561 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28562 && CONSTRUCTOR_NELTS (val
)
28563 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28564 type gets a single initializer. */
28565 && CP_AGGREGATE_TYPE_P (ftype
)
28566 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28567 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28569 tree subelt
= NULL_TREE
;
28570 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28571 subelt
= TREE_TYPE (ftype
);
28572 list
= collect_ctor_idx_types (val
, list
, subelt
);
28575 tree arg
= NULL_TREE
;
28576 if (i
== v
->length() - 1
28577 && PACK_EXPANSION_P (ftype
))
28578 /* Give the trailing pack expansion parameter a default argument to
28579 match aggregate initialization behavior, even if we deduce the
28580 length of the pack separately to more than we have initializers. */
28581 arg
= build_constructor (init_list_type_node
, NULL
);
28582 /* if ei is of array type and xi is a braced-init-list or string literal,
28583 Ti is an rvalue reference to the declared type of ei */
28584 STRIP_ANY_LOCATION_WRAPPER (val
);
28585 if (TREE_CODE (ftype
) == ARRAY_TYPE
28586 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28587 || TREE_CODE (val
) == STRING_CST
))
28589 if (TREE_CODE (val
) == STRING_CST
)
28590 ftype
= cp_build_qualified_type
28591 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28592 ftype
= (cp_build_reference_type
28593 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28595 list
= tree_cons (arg
, ftype
, list
);
28601 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28604 is_spec_or_derived (tree etype
, tree tmpl
)
28606 if (!etype
|| !CLASS_TYPE_P (etype
))
28609 tree type
= TREE_TYPE (tmpl
);
28610 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28611 (DECL_TEMPLATE_PARMS (tmpl
)));
28612 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28613 int err
= unify (tparms
, targs
, type
, etype
,
28614 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28619 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28623 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28625 if (cxx_dialect
< cxx20
)
28628 if (init
== NULL_TREE
)
28631 tree type
= TREE_TYPE (tmpl
);
28632 if (!CP_AGGREGATE_TYPE_P (type
))
28635 /* No aggregate candidate for copy-initialization. */
28636 if (args
->length() == 1)
28638 tree val
= (*args
)[0];
28639 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28643 /* If we encounter a problem, we just won't add the candidate. */
28644 tsubst_flags_t complain
= tf_none
;
28646 tree parms
= NULL_TREE
;
28647 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28649 init
= reshape_init (type
, init
, complain
);
28650 if (init
== error_mark_node
)
28652 parms
= collect_ctor_idx_types (init
, parms
);
28654 else if (TREE_CODE (init
) == TREE_LIST
)
28656 int len
= list_length (init
);
28657 for (tree field
= TYPE_FIELDS (type
);
28659 --len
, field
= DECL_CHAIN (field
))
28661 field
= next_initializable_field (field
);
28664 tree ftype
= finish_decltype_type (field
, true, complain
);
28665 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28669 /* Aggregate initialization doesn't apply to an initializer expression. */
28675 parms
= nreverse (parms
);
28676 TREE_CHAIN (last
) = void_list_node
;
28677 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28684 /* UGUIDES are the deduction guides for the underlying template of alias
28685 template TMPL; adjust them to be deduction guides for TMPL. */
28688 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28690 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28691 class type (9.2.8.2) where the template-name names an alias template A,
28692 the defining-type-id of A must be of the form
28694 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28696 as specified in 9.2.8.2. The guides of A are the set of functions or
28697 function templates formed as follows. For each function or function
28698 template f in the guides of the template named by the simple-template-id
28699 of the defining-type-id, the template arguments of the return type of f
28700 are deduced from the defining-type-id of A according to the process in
28701 13.10.2.5 with the exception that deduction does not fail if not all
28702 template arguments are deduced. Let g denote the result of substituting
28703 these deductions into f. If substitution succeeds, form a function or
28704 function template f' with the following properties and add it to the set
28707 * The function type of f' is the function type of g.
28709 * If f is a function template, f' is a function template whose template
28710 parameter list consists of all the template parameters of A (including
28711 their default template arguments) that appear in the above deductions or
28712 (recursively) in their default template arguments, followed by the
28713 template parameters of f that were not deduced (including their default
28714 template arguments), otherwise f' is not a function template.
28716 * The associated constraints (13.5.2) are the conjunction of the
28717 associated constraints of g and a constraint that is satisfied if and only
28718 if the arguments of A are deducible (see below) from the return type.
28720 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28723 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28724 considered to be so as well.
28726 * The explicit-specifier of f' is the explicit-specifier of g (if
28729 /* This implementation differs from the above in two significant ways:
28731 1) We include all template parameters of A, not just some.
28732 2) The added constraint is same_type instead of deducible.
28734 I believe that while it's probably possible to construct a testcase that
28735 behaves differently with this simplification, it should have the same
28736 effect for real uses. Including all template parameters means that we
28737 deduce all parameters of A when resolving the call, so when we're in the
28738 constraint we don't need to deduce them again, we can just check whether
28739 the deduction produced the desired result. */
28741 tsubst_flags_t complain
= tf_warning_or_error
;
28742 tree atype
= TREE_TYPE (tmpl
);
28743 tree aguides
= NULL_TREE
;
28744 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28745 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28746 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28747 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28751 location_t loc
= DECL_SOURCE_LOCATION (f
);
28752 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28754 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28756 processing_template_decl_sentinel
ptds (/*reset*/false);
28757 ++processing_template_decl
;
28759 /* Deduce template arguments for f from the type-id of A. */
28760 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28761 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28762 tree targs
= make_tree_vec (len
);
28763 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28766 /* The number of parms for f' is the number of parms for A plus
28767 non-deduced parms of f. */
28768 unsigned ndlen
= 0;
28770 for (unsigned i
= 0; i
< len
; ++i
)
28771 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28773 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28775 /* First copy over the parms of A. */
28776 for (j
= 0; j
< natparms
; ++j
)
28777 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28778 /* Now rewrite the non-deduced parms of f. */
28779 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28780 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28783 unsigned index
= j
++;
28784 unsigned level
= 1;
28785 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28786 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28787 targs
, i
, complain
);
28788 TREE_VEC_ELT (gtparms
, index
) = list
;
28790 gtparms
= build_tree_list (size_one_node
, gtparms
);
28792 /* Substitute the deduced arguments plus the rewritten template
28793 parameters into f to get g. This covers the type, copyness,
28794 guideness, and explicit-specifier. */
28795 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28796 if (g
== error_mark_node
)
28797 return error_mark_node
;
28798 DECL_USE_TEMPLATE (g
) = 0;
28799 fprime
= build_template_decl (g
, gtparms
, false);
28800 DECL_TEMPLATE_RESULT (fprime
) = g
;
28801 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28802 tree gtargs
= template_parms_to_args (gtparms
);
28803 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28804 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28806 /* Substitute the associated constraints. */
28807 tree ci
= get_constraints (f
);
28809 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28810 if (ci
== error_mark_node
)
28811 return error_mark_node
;
28813 /* Add a constraint that the return type matches the instantiation of
28814 A with the same template arguments. */
28815 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28816 if (!same_type_p (atype
, ret
)
28817 /* FIXME this should mean they don't compare as equivalent. */
28818 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28820 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28821 ci
= append_constraint (ci
, same
);
28826 remove_constraints (fprime
);
28827 set_constraints (fprime
, ci
);
28832 /* For a non-template deduction guide, if the arguments of A aren't
28833 deducible from the return type, don't add the candidate. */
28834 tree targs
= make_tree_vec (natparms
);
28835 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28836 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28837 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28843 aguides
= lookup_add (fprime
, aguides
);
28849 /* Return artificial deduction guides built from the constructors of class
28853 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28855 tree type
= TREE_TYPE (tmpl
);
28856 tree outer_args
= NULL_TREE
;
28857 if (DECL_CLASS_SCOPE_P (tmpl
)
28858 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28860 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28861 type
= TREE_TYPE (most_general_template (tmpl
));
28864 tree cands
= NULL_TREE
;
28866 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28868 /* Skip inherited constructors. */
28869 if (iter
.using_p ())
28872 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28873 cands
= lookup_add (guide
, cands
);
28876 /* Add implicit default constructor deduction guide. */
28877 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28879 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28881 cands
= lookup_add (guide
, cands
);
28884 /* Add copy guide. */
28886 tree gtype
= build_reference_type (type
);
28887 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28889 cands
= lookup_add (guide
, cands
);
28895 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28897 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28898 aggregate candidate is added separately because it depends on the
28899 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28903 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
28905 tree guides
= NULL_TREE
;
28906 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28908 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28909 tree tinfo
= get_template_info (under
);
28910 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
28915 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28916 dguide_name (tmpl
),
28917 LOOK_want::NORMAL
, /*complain*/false);
28918 if (guides
== error_mark_node
)
28919 guides
= NULL_TREE
;
28921 any_dguides_p
= true;
28924 /* Cache the deduction guides for a template. We also remember the result of
28925 lookup, and rebuild everything if it changes; should be very rare. */
28926 tree_pair_p cache
= NULL
;
28928 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28931 if (cache
->purpose
== guides
)
28932 return cache
->value
;
28936 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28937 cache
->purpose
= guides
;
28940 tree cands
= NULL_TREE
;
28941 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28942 cands
= alias_ctad_tweaks (tmpl
, guides
);
28945 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28946 for (ovl_iterator
it (guides
); it
; ++it
)
28947 cands
= lookup_add (*it
, cands
);
28950 cache
->value
= cands
;
28954 /* Return whether TMPL is a (class template argument-) deducible template. */
28957 ctad_template_p (tree tmpl
)
28959 /* A deducible template is either a class template or is an alias template
28960 whose defining-type-id is of the form
28962 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28964 where the nested-name-specifier (if any) is non-dependent and the
28965 template-name of the simple-template-id names a deducible template. */
28967 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28968 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28970 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28972 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28973 if (tree tinfo
= get_template_info (orig
))
28974 return ctad_template_p (TI_TEMPLATE (tinfo
));
28978 /* Deduce template arguments for the class template placeholder PTYPE for
28979 template TMPL based on the initializer INIT, and return the resulting
28983 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28984 int flags
, tsubst_flags_t complain
)
28986 /* We should have handled this in the caller. */
28987 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28990 /* Look through alias templates that just rename another template. */
28991 tmpl
= get_underlying_template (tmpl
);
28992 if (!ctad_template_p (tmpl
))
28994 if (complain
& tf_error
)
28995 error ("non-deducible template %qT used without template arguments", tmpl
);
28996 return error_mark_node
;
28998 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
29000 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29001 if (complain
& tf_warning_or_error
)
29002 pedwarn (input_location
, 0, "alias template deduction only available "
29003 "with %<-std=c++20%> or %<-std=gnu++20%>");
29006 if (init
&& TREE_TYPE (init
) == ptype
)
29007 /* Using the template parm as its own argument. */
29010 tree type
= TREE_TYPE (tmpl
);
29012 bool try_list_ctor
= false;
29013 bool list_init_p
= false;
29015 releasing_vec rv_args
= NULL
;
29016 vec
<tree
,va_gc
> *&args
= *&rv_args
;
29017 if (init
== NULL_TREE
)
29018 args
= make_tree_vector ();
29019 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29021 list_init_p
= true;
29022 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
29023 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
29025 /* As an exception, the first phase in 16.3.1.7 (considering the
29026 initializer list as a single argument) is omitted if the
29027 initializer list consists of a single expression of type cv U,
29028 where U is a specialization of C or a class derived from a
29029 specialization of C. */
29030 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
29031 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
29032 try_list_ctor
= false;
29034 if (try_list_ctor
|| is_std_init_list (type
))
29035 args
= make_tree_vector_single (init
);
29037 args
= make_tree_vector_from_ctor (init
);
29039 else if (TREE_CODE (init
) == TREE_LIST
)
29040 args
= make_tree_vector_from_list (init
);
29042 args
= make_tree_vector_single (init
);
29044 /* Do this now to avoid problems with erroneous args later on. */
29045 args
= resolve_args (args
, complain
);
29047 return error_mark_node
;
29049 bool any_dguides_p
= false;
29050 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29051 if (cands
== error_mark_node
)
29052 return error_mark_node
;
29054 /* Prune explicit deduction guides in copy-initialization context (but
29055 not copy-list-initialization). */
29056 bool elided
= false;
29057 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
29059 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29060 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29065 /* Found a nonconverting guide, prune the candidates. */
29066 tree pruned
= NULL_TREE
;
29067 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29068 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29069 pruned
= lookup_add (*iter
, pruned
);
29075 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29076 cands
= lookup_add (guide
, cands
);
29078 tree call
= error_mark_node
;
29080 /* If this is list-initialization and the class has a list constructor, first
29081 try deducing from the list as a single argument, as [over.match.list]. */
29082 tree list_cands
= NULL_TREE
;
29083 if (try_list_ctor
&& cands
)
29084 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29087 if (is_list_ctor (dg
))
29088 list_cands
= lookup_add (dg
, list_cands
);
29092 ++cp_unevaluated_operand
;
29093 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29094 --cp_unevaluated_operand
;
29096 if (call
== error_mark_node
)
29098 /* That didn't work, now try treating the list as a sequence of
29100 release_tree_vector (args
);
29101 args
= make_tree_vector_from_ctor (init
);
29105 if (elided
&& !cands
)
29107 error ("cannot deduce template arguments for copy-initialization"
29108 " of %qT, as it has no non-explicit deduction guides or "
29109 "user-declared constructors", type
);
29110 return error_mark_node
;
29112 else if (!cands
&& call
== error_mark_node
)
29114 error ("cannot deduce template arguments of %qT, as it has no viable "
29115 "deduction guides", type
);
29116 return error_mark_node
;
29119 if (call
== error_mark_node
)
29121 ++cp_unevaluated_operand
;
29122 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29123 --cp_unevaluated_operand
;
29126 if (call
== error_mark_node
)
29128 if (complain
& tf_warning_or_error
)
29130 error ("class template argument deduction failed:");
29132 ++cp_unevaluated_operand
;
29133 call
= build_new_function_call (cands
, &args
,
29134 complain
| tf_decltype
);
29135 --cp_unevaluated_operand
;
29138 inform (input_location
, "explicit deduction guides not considered "
29139 "for copy-initialization");
29141 return error_mark_node
;
29143 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29144 constructor is chosen, the initialization is ill-formed. */
29145 else if (flags
& LOOKUP_ONLYCONVERTING
)
29147 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29148 if (fndecl
&& DECL_NONCONVERTING_P (fndecl
))
29150 if (complain
& tf_warning_or_error
)
29152 // TODO: Pass down location from cp_finish_decl.
29153 error ("class template argument deduction for %qT failed: "
29154 "explicit deduction guide selected in "
29155 "copy-list-initialization", type
);
29156 inform (DECL_SOURCE_LOCATION (fndecl
),
29157 "explicit deduction guide declared here");
29160 return error_mark_node
;
29164 /* If CTAD succeeded but the type doesn't have any explicit deduction
29165 guides, this deduction might not be what the user intended. */
29166 if (call
!= error_mark_node
&& !any_dguides_p
)
29168 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29169 if (fndecl
!= NULL_TREE
29170 && (!DECL_IN_SYSTEM_HEADER (fndecl
)
29171 || global_dc
->dc_warn_system_headers
)
29172 && warning (OPT_Wctad_maybe_unsupported
,
29173 "%qT may not intend to support class template argument "
29174 "deduction", type
))
29175 inform (input_location
, "add a deduction guide to suppress this "
29179 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29182 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29183 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29184 The CONTEXT determines the context in which auto deduction is performed
29185 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29186 OUTER_TARGS are used during template argument deduction
29187 (context == adc_unify) to properly substitute the result, and is ignored
29190 For partial-concept-ids, extra args may be appended to the list of deduced
29191 template arguments prior to determining constraint satisfaction. */
29194 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29195 tsubst_flags_t complain
, auto_deduction_context context
,
29196 tree outer_targs
, int flags
)
29200 if (init
== error_mark_node
)
29201 return error_mark_node
;
29203 if (init
&& type_dependent_expression_p (init
)
29204 && context
!= adc_unify
)
29205 /* Defining a subset of type-dependent expressions that we can deduce
29206 from ahead of time isn't worth the trouble. */
29209 /* Similarly, we can't deduce from another undeduced decl. */
29210 if (init
&& undeduced_auto_decl (init
))
29213 /* We may be doing a partial substitution, but we still want to replace
29215 complain
&= ~tf_partial
;
29217 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29218 /* C++17 class template argument deduction. */
29219 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29221 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29222 /* Nothing we can do with this, even in deduction context. */
29225 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29226 with either a new invented type template parameter U or, if the
29227 initializer is a braced-init-list (8.5.4), with
29228 std::initializer_list<U>. */
29229 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29231 if (!DIRECT_LIST_INIT_P (init
))
29232 type
= listify_autos (type
, auto_node
);
29233 else if (CONSTRUCTOR_NELTS (init
) == 1)
29234 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29237 if (complain
& tf_warning_or_error
)
29239 if (permerror (input_location
, "direct-list-initialization of "
29240 "%<auto%> requires exactly one element"))
29241 inform (input_location
,
29242 "for deduction to %<std::initializer_list%>, use copy-"
29243 "list-initialization (i.e. add %<=%> before the %<{%>)");
29245 type
= listify_autos (type
, auto_node
);
29249 if (type
== error_mark_node
)
29250 return error_mark_node
;
29252 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29254 /* We don't recurse here because we can't deduce from a nested
29255 initializer_list. */
29256 if (CONSTRUCTOR_ELTS (init
))
29257 for (constructor_elt
&elt
: *CONSTRUCTOR_ELTS (init
))
29258 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
29261 init
= resolve_nondeduced_context (init
, complain
);
29263 if (context
== adc_decomp_type
29264 && auto_node
== type
29265 && init
!= error_mark_node
29266 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29267 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29268 and initializer has array type, deduce cv-qualified array type. */
29269 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29271 else if (AUTO_IS_DECLTYPE (auto_node
))
29273 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29274 if (REFERENCE_REF_P (stripped_init
))
29275 stripped_init
= TREE_OPERAND (stripped_init
, 0);
29276 bool id
= (DECL_P (stripped_init
)
29277 || ((TREE_CODE (init
) == COMPONENT_REF
29278 || TREE_CODE (init
) == SCOPE_REF
)
29279 && !REF_PARENTHESIZED_P (init
)));
29280 targs
= make_tree_vec (1);
29281 TREE_VEC_ELT (targs
, 0)
29282 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29283 if (type
!= auto_node
)
29285 if (complain
& tf_error
)
29286 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29287 return error_mark_node
;
29289 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29291 if (complain
& tf_error
)
29292 error ("%<decltype(auto)%> cannot be cv-qualified");
29293 return error_mark_node
;
29298 if (error_operand_p (init
))
29299 return error_mark_node
;
29301 tree parms
= build_tree_list (NULL_TREE
, type
);
29305 tparms
= extract_autos (type
);
29308 tparms
= make_tree_vec (1);
29309 TREE_VEC_ELT (tparms
, 0)
29310 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29313 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29314 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29316 NULL
, /*explain_p=*/false);
29319 if (processing_template_decl
)
29320 /* Try again at instantiation time. */
29322 if (type
&& type
!= error_mark_node
29323 && (complain
& tf_error
))
29324 /* If type is error_mark_node a diagnostic must have been
29325 emitted by now. Also, having a mention to '<type error>'
29326 in the diagnostic is not really useful to the user. */
29329 && FNDECL_USED_AUTO (current_function_decl
)
29331 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29332 && LAMBDA_FUNCTION_P (current_function_decl
))
29333 error ("unable to deduce lambda return type from %qE", init
);
29335 error ("unable to deduce %qT from %qE", type
, init
);
29336 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29338 NULL
, /*explain_p=*/true);
29340 return error_mark_node
;
29344 /* Check any placeholder constraints against the deduced type. */
29345 if (flag_concepts
&& !processing_template_decl
)
29346 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29348 /* Use the deduced type to check the associated constraints. If we
29349 have a partial-concept-id, rebuild the argument list so that
29350 we check using the extra arguments. */
29351 check
= unpack_concept_check (check
);
29352 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29353 tree
cdecl = TREE_OPERAND (check
, 0);
29355 cdecl = OVL_FIRST (cdecl);
29356 tree cargs
= TREE_OPERAND (check
, 1);
29357 if (TREE_VEC_LENGTH (cargs
) > 1)
29359 cargs
= copy_node (cargs
);
29360 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29365 /* Rebuild the check using the deduced arguments. */
29366 check
= build_concept_check (cdecl, cargs
, tf_none
);
29368 if (!constraints_satisfied_p (check
))
29370 if (complain
& tf_warning_or_error
)
29372 auto_diagnostic_group d
;
29375 case adc_unspecified
:
29377 error("placeholder constraints not satisfied");
29379 case adc_variable_type
:
29380 case adc_decomp_type
:
29381 error ("deduced initializer does not satisfy "
29382 "placeholder constraints");
29384 case adc_return_type
:
29385 error ("deduced return type does not satisfy "
29386 "placeholder constraints");
29388 case adc_requirement
:
29389 error ("deduced expression type does not satisfy "
29390 "placeholder constraints");
29393 diagnose_constraints (input_location
, check
, targs
);
29395 return error_mark_node
;
29399 if (processing_template_decl
&& context
!= adc_unify
)
29400 outer_targs
= current_template_args ();
29401 targs
= add_to_template_args (outer_targs
, targs
);
29402 return tsubst (type
, targs
, complain
, NULL_TREE
);
29405 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29409 splice_late_return_type (tree type
, tree late_return_type
)
29411 if (late_return_type
)
29413 gcc_assert (is_auto (type
) || seen_error ());
29414 return late_return_type
;
29417 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29419 tree idx
= get_template_parm_index (*auto_node
);
29420 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29422 /* In an abbreviated function template we didn't know we were dealing
29423 with a function template when we saw the auto return type, so update
29424 it to have the correct level. */
29425 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29426 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29427 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29428 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29429 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29430 *auto_node
= new_auto
;
29436 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29437 'decltype(auto)' or a deduced class template. */
29440 is_auto (const_tree type
)
29442 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29443 && (TYPE_IDENTIFIER (type
) == auto_identifier
29444 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29450 /* for_each_template_parm callback for type_uses_auto. */
29453 is_auto_r (tree tp
, void */
*data*/
)
29455 return is_auto (tp
);
29458 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29459 a use of `auto'. Returns NULL_TREE otherwise. */
29462 type_uses_auto (tree type
)
29464 if (type
== NULL_TREE
)
29466 else if (flag_concepts
)
29468 /* The Concepts TS allows multiple autos in one type-specifier; just
29469 return the first one we find, do_auto_deduction will collect all of
29471 if (uses_template_parms (type
))
29472 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29473 /*visited*/NULL
, /*nondeduced*/false);
29477 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29483 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29484 concepts are enabled, auto is acceptable in template arguments, but
29485 only when TEMPL identifies a template class. Return TRUE if any
29486 such errors were reported. */
29489 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29491 /* If there were previous errors, nevermind. */
29492 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29495 /* If TMPL is an identifier, we're parsing and we can't tell yet
29496 whether TMPL is supposed to be a type, a function or a variable.
29497 We'll only be able to tell during template substitution, so we
29498 expect to be called again then. If concepts are enabled and we
29499 know we have a type, we're ok. */
29501 && (identifier_p (tmpl
)
29503 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29504 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29507 /* Quickly search for any occurrences of auto; usually there won't
29508 be any, and then we'll avoid allocating the vector. */
29509 if (!type_uses_auto (args
))
29512 bool errors
= false;
29514 tree vec
= extract_autos (args
);
29515 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29517 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29518 error_at (DECL_SOURCE_LOCATION (xauto
),
29519 "invalid use of %qT in template argument", xauto
);
29526 /* Recursively walk over && expressions searching for EXPR. Return a reference
29527 to that expression. */
29529 static tree
*find_template_requirement (tree
*t
, tree key
)
29533 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29535 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29537 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29543 /* Convert the generic type parameters in PARM that match the types given in the
29544 range [START_IDX, END_IDX) from the current_template_parms into generic type
29548 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29550 tree current
= current_template_parms
;
29551 int depth
= TMPL_PARMS_DEPTH (current
);
29552 current
= INNERMOST_TEMPLATE_PARMS (current
);
29553 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29555 for (int i
= 0; i
< start_idx
; ++i
)
29556 TREE_VEC_ELT (replacement
, i
)
29557 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29559 for (int i
= start_idx
; i
< end_idx
; ++i
)
29561 /* Create a distinct parameter pack type from the current parm and add it
29562 to the replacement args to tsubst below into the generic function
29564 tree node
= TREE_VEC_ELT (current
, i
);
29565 tree o
= TREE_TYPE (TREE_VALUE (node
));
29566 tree t
= copy_type (o
);
29567 TEMPLATE_TYPE_PARM_INDEX (t
)
29568 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29570 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29571 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29572 TYPE_MAIN_VARIANT (t
) = t
;
29573 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29574 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29575 TREE_VEC_ELT (replacement
, i
) = t
;
29577 /* Replace the current template parameter with new pack. */
29578 TREE_VALUE (node
) = TREE_CHAIN (t
);
29580 /* Surgically adjust the associated constraint of adjusted parameter
29581 and it's corresponding contribution to the current template
29583 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29585 tree id
= unpack_concept_check (constr
);
29586 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
29587 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29588 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29590 /* If there was a constraint, we also need to replace that in
29591 the template requirements, which we've already built. */
29592 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29593 reqs
= find_template_requirement (reqs
, constr
);
29598 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29599 TREE_VEC_ELT (replacement
, i
)
29600 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29602 /* If there are more levels then build up the replacement with the outer
29605 replacement
= add_to_template_args (template_parms_to_args
29606 (TREE_CHAIN (current_template_parms
)),
29609 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29612 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29616 declare_integer_pack (void)
29618 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29619 build_function_type_list (integer_type_node
,
29622 NULL_TREE
, ECF_CONST
);
29623 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29624 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29625 CP_BUILT_IN_INTEGER_PACK
);
29628 /* Set up the hash tables for template instantiations. */
29631 init_template_processing (void)
29633 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29634 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29636 if (cxx_dialect
>= cxx11
)
29637 declare_integer_pack ();
29640 /* Print stats about the template hash tables for -fstats. */
29643 print_template_statistics (void)
29645 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29646 "%f collisions\n", (long) decl_specializations
->size (),
29647 (long) decl_specializations
->elements (),
29648 decl_specializations
->collisions ());
29649 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29650 "%f collisions\n", (long) type_specializations
->size (),
29651 (long) type_specializations
->elements (),
29652 type_specializations
->collisions ());
29657 namespace selftest
{
29659 /* Verify that build_non_dependent_expr () works, for various expressions,
29660 and that location wrappers don't affect the results. */
29663 test_build_non_dependent_expr ()
29665 location_t loc
= BUILTINS_LOCATION
;
29667 /* Verify constants, without and with location wrappers. */
29668 tree int_cst
= build_int_cst (integer_type_node
, 42);
29669 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29671 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29672 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29673 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29675 tree string_lit
= build_string (4, "foo");
29676 TREE_TYPE (string_lit
) = char_array_type_node
;
29677 string_lit
= fix_string_type (string_lit
);
29678 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29680 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29681 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29682 ASSERT_EQ (wrapped_string_lit
,
29683 build_non_dependent_expr (wrapped_string_lit
));
29686 /* Verify that type_dependent_expression_p () works correctly, even
29687 in the presence of location wrapper nodes. */
29690 test_type_dependent_expression_p ()
29692 location_t loc
= BUILTINS_LOCATION
;
29694 tree name
= get_identifier ("foo");
29696 /* If no templates are involved, nothing is type-dependent. */
29697 gcc_assert (!processing_template_decl
);
29698 ASSERT_FALSE (type_dependent_expression_p (name
));
29700 ++processing_template_decl
;
29702 /* Within a template, an unresolved name is always type-dependent. */
29703 ASSERT_TRUE (type_dependent_expression_p (name
));
29705 /* Ensure it copes with NULL_TREE and errors. */
29706 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29707 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29709 /* A USING_DECL in a template should be type-dependent, even if wrapped
29710 with a location wrapper (PR c++/83799). */
29711 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29712 TREE_TYPE (using_decl
) = integer_type_node
;
29713 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29714 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29715 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29716 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29718 --processing_template_decl
;
29721 /* Run all of the selftests within this file. */
29726 test_build_non_dependent_expr ();
29727 test_type_dependent_expression_p ();
29730 } // namespace selftest
29732 #endif /* #if CHECKING_P */
29734 #include "gt-cp-pt.h"