1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_nop
)
88 else if (policy
== lss_blank
|| !saved
)
89 local_specializations
= new hash_map
<tree
, tree
>;
91 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
94 local_specialization_stack::~local_specialization_stack ()
96 if (local_specializations
!= saved
)
98 delete local_specializations
;
99 local_specializations
= saved
;
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth
;
106 struct GTY((for_user
)) spec_entry
113 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
115 static hashval_t
hash (spec_entry
*);
116 static bool equal (spec_entry
*, spec_entry
*);
119 /* The general template is not in these tables. */
120 typedef hash_table
<spec_hasher
> spec_hash_table
;
121 static GTY (()) spec_hash_table
*decl_specializations
;
122 static GTY (()) spec_hash_table
*type_specializations
;
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
139 enum template_base_result
{
141 tbr_ambiguous_baseclass
,
145 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
146 unification_kind_t
, int,
148 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
149 unification_kind_t
, int, bool, bool);
150 static int unify (tree
, tree
, tree
, tree
, int, bool);
151 static void add_pending_template (tree
);
152 static tree
reopen_tinst_level (struct tinst_level
*);
153 static tree
tsubst_initializer_list (tree
, tree
);
154 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
155 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
157 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
159 static void tsubst_enum (tree
, tree
, tree
);
160 static tree
add_to_template_args (tree
, tree
);
161 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
162 static int check_non_deducible_conversion (tree
, tree
, int, int,
163 struct conversion
**, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
166 static int type_unification_real (tree
, tree
, tree
, const tree
*,
167 unsigned int, int, unification_kind_t
,
168 vec
<deferred_access_check
, va_gc
> **,
170 static void note_template_header (int);
171 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
172 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
173 static tree
convert_template_argument (tree
, tree
, tree
,
174 tsubst_flags_t
, int, tree
);
175 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
176 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
177 static tree
expand_template_argument_pack (tree
);
178 static tree
build_template_parm_index (int, int, int, tree
, tree
);
179 static bool inline_needs_template_parms (tree
, bool);
180 static void push_inline_template_parms_recursive (tree
, int);
181 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
182 static int mark_template_parm (tree
, void *);
183 static int template_parm_this_level_p (tree
, void *);
184 static tree
tsubst_friend_function (tree
, tree
);
185 static tree
tsubst_friend_class (tree
, tree
);
186 static int can_complete_type_without_circularity (tree
);
187 static tree
get_bindings (tree
, tree
, tree
, bool);
188 static int template_decl_level (tree
);
189 static int check_cv_quals_for_unify (int, tree
, tree
);
190 static int unify_pack_expansion (tree
, tree
, tree
,
191 tree
, unification_kind_t
, bool, bool);
192 static tree
copy_template_args (tree
);
193 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
194 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
195 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
196 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
198 static bool check_specialization_scope (void);
199 static tree
process_partial_specialization (tree
);
200 static void set_current_access_from_decl (tree
);
201 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
203 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
204 static bool class_nttp_const_wrapper_p (tree t
);
205 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
207 static bool template_template_parm_bindings_ok_p (tree
, tree
);
208 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
209 static tree
for_each_template_parm_r (tree
*, int *, void *);
210 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
211 static void copy_default_args_to_explicit_spec (tree
);
212 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
213 static bool dependent_template_arg_p (tree
);
214 static bool any_template_arguments_need_structural_equality_p (tree
);
215 static bool dependent_type_p_r (tree
);
216 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
218 static void perform_instantiation_time_access_checks (tree
, tree
);
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
get_underlying_template (tree
);
225 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
226 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
227 static tree
make_argument_pack (tree
);
228 static void register_parameter_specializations (tree
, tree
);
229 static tree
enclosing_instantiation_of (tree tctx
);
230 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
232 /* Make the current scope suitable for access checking when we are
233 processing T. T can be FUNCTION_DECL for instantiated function
234 template, VAR_DECL for static member variable, or TYPE_DECL for
235 alias template (needed by instantiate_decl). */
238 push_access_scope (tree t
)
240 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
241 || TREE_CODE (t
) == TYPE_DECL
);
243 if (DECL_FRIEND_CONTEXT (t
))
244 push_nested_class (DECL_FRIEND_CONTEXT (t
));
245 else if (DECL_CLASS_SCOPE_P (t
))
246 push_nested_class (DECL_CONTEXT (t
));
248 push_to_top_level ();
250 if (TREE_CODE (t
) == FUNCTION_DECL
)
252 vec_safe_push (saved_access_scope
, current_function_decl
);
253 current_function_decl
= t
;
257 /* Restore the scope set up by push_access_scope. T is the node we
261 pop_access_scope (tree t
)
263 if (TREE_CODE (t
) == FUNCTION_DECL
)
264 current_function_decl
= saved_access_scope
->pop();
266 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
269 pop_from_top_level ();
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
278 finish_member_template_decl (tree decl
)
280 if (decl
== error_mark_node
)
281 return error_mark_node
;
283 gcc_assert (DECL_P (decl
));
285 if (TREE_CODE (decl
) == TYPE_DECL
)
289 type
= TREE_TYPE (decl
);
290 if (type
== error_mark_node
)
291 return error_mark_node
;
292 if (MAYBE_CLASS_TYPE_P (type
)
293 && CLASSTYPE_TEMPLATE_INFO (type
)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
296 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
297 check_member_template (tmpl
);
302 else if (TREE_CODE (decl
) == FIELD_DECL
)
303 error_at (DECL_SOURCE_LOCATION (decl
),
304 "data member %qD cannot be a member template", decl
);
305 else if (DECL_TEMPLATE_INFO (decl
))
307 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
309 check_member_template (DECL_TI_TEMPLATE (decl
));
310 return DECL_TI_TEMPLATE (decl
);
316 error_at (DECL_SOURCE_LOCATION (decl
),
317 "invalid member template declaration %qD", decl
);
319 return error_mark_node
;
322 /* Create a template info node. */
325 build_template_info (tree template_decl
, tree template_args
)
327 tree result
= make_node (TEMPLATE_INFO
);
328 TI_TEMPLATE (result
) = template_decl
;
329 TI_ARGS (result
) = template_args
;
333 /* Return the template info node corresponding to T, whatever T is. */
336 get_template_info (const_tree t
)
338 tree tinfo
= NULL_TREE
;
340 if (!t
|| t
== error_mark_node
)
343 if (TREE_CODE (t
) == NAMESPACE_DECL
344 || TREE_CODE (t
) == PARM_DECL
)
347 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
348 tinfo
= DECL_TEMPLATE_INFO (t
);
350 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
353 if (OVERLOAD_TYPE_P (t
))
354 tinfo
= TYPE_TEMPLATE_INFO (t
);
355 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
356 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
361 /* Returns the template nesting level of the indicated class TYPE.
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
380 template_class_depth (tree type
)
384 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
386 tree tinfo
= get_template_info (type
);
388 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
394 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
397 type
= CP_DECL_CONTEXT (type
);
399 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
400 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
402 type
= CP_TYPE_CONTEXT (type
);
408 /* Return TRUE if NODE instantiates a template that has arguments of
409 its own, be it directly a primary template or indirectly through a
410 partial specializations. */
412 instantiates_primary_template_p (tree node
)
414 tree tinfo
= get_template_info (node
);
418 tree tmpl
= TI_TEMPLATE (tinfo
);
419 if (PRIMARY_TEMPLATE_P (tmpl
))
422 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
425 /* So now we know we have a specialization, but it could be a full
426 or a partial specialization. To tell which, compare the depth of
427 its template arguments with those of its context. */
429 tree ctxt
= DECL_CONTEXT (tmpl
);
430 tree ctinfo
= get_template_info (ctxt
);
434 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
435 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
438 /* Subroutine of maybe_begin_member_template_processing.
439 Returns true if processing DECL needs us to push template parms. */
442 inline_needs_template_parms (tree decl
, bool nsdmi
)
444 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
447 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
448 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
451 /* Subroutine of maybe_begin_member_template_processing.
452 Push the template parms in PARMS, starting from LEVELS steps into the
453 chain, and ending at the beginning, since template parms are listed
457 push_inline_template_parms_recursive (tree parmlist
, int levels
)
459 tree parms
= TREE_VALUE (parmlist
);
463 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
465 ++processing_template_decl
;
466 current_template_parms
467 = tree_cons (size_int (processing_template_decl
),
468 parms
, current_template_parms
);
469 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
471 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
473 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
475 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
477 if (error_operand_p (parm
))
480 gcc_assert (DECL_P (parm
));
482 switch (TREE_CODE (parm
))
490 /* Push the CONST_DECL. */
491 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
500 /* Restore the template parameter context for a member template, a
501 friend template defined in a class definition, or a non-template
502 member of template class. */
505 maybe_begin_member_template_processing (tree decl
)
509 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
513 tree ctx
= DECL_CONTEXT (decl
);
514 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
515 /* Disregard full specializations (c++/60999). */
516 && uses_template_parms (ctx
)
517 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
520 if (inline_needs_template_parms (decl
, nsdmi
))
522 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
523 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
525 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
528 parms
= TREE_CHAIN (parms
);
531 push_inline_template_parms_recursive (parms
, levels
);
534 /* Remember how many levels of template parameters we pushed so that
535 we can pop them later. */
536 inline_parm_levels
.safe_push (levels
);
539 /* Undo the effects of maybe_begin_member_template_processing. */
542 maybe_end_member_template_processing (void)
547 if (inline_parm_levels
.length () == 0)
550 last
= inline_parm_levels
.pop ();
551 for (i
= 0; i
< last
; ++i
)
553 --processing_template_decl
;
554 current_template_parms
= TREE_CHAIN (current_template_parms
);
559 /* Return a new template argument vector which contains all of ARGS,
560 but has as its innermost set of arguments the EXTRA_ARGS. */
563 add_to_template_args (tree args
, tree extra_args
)
570 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
573 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
574 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
576 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
577 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
579 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
580 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
585 /* Like add_to_template_args, but only the outermost ARGS are added to
586 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
587 (EXTRA_ARGS) levels are added. This function is used to combine
588 the template arguments from a partial instantiation with the
589 template arguments used to attain the full instantiation from the
590 partial instantiation.
592 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
595 add_outermost_template_args (tree args
, tree extra_args
)
601 if (TREE_CODE (args
) == TEMPLATE_DECL
)
603 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
607 /* If there are more levels of EXTRA_ARGS than there are ARGS,
608 something very fishy is going on. */
609 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
611 /* If *all* the new arguments will be the EXTRA_ARGS, just return
613 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
616 /* For the moment, we make ARGS look like it contains fewer levels. */
617 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
619 new_args
= add_to_template_args (args
, extra_args
);
621 /* Now, we restore ARGS to its full dimensions. */
622 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
627 /* Return the N levels of innermost template arguments from the ARGS. */
630 get_innermost_template_args (tree args
, int n
)
638 /* If N is 1, just return the innermost set of template arguments. */
640 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
642 /* If we're not removing anything, just return the arguments we were
644 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
645 gcc_assert (extra_levels
>= 0);
646 if (extra_levels
== 0)
649 /* Make a new set of arguments, not containing the outer arguments. */
650 new_args
= make_tree_vec (n
);
651 for (i
= 1; i
<= n
; ++i
)
652 SET_TMPL_ARGS_LEVEL (new_args
, i
,
653 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
658 /* The inverse of get_innermost_template_args: Return all but the innermost
659 EXTRA_LEVELS levels of template arguments from the ARGS. */
662 strip_innermost_template_args (tree args
, int extra_levels
)
665 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
670 /* If N is 1, just return the outermost set of template arguments. */
672 return TMPL_ARGS_LEVEL (args
, 1);
674 /* If we're not removing anything, just return the arguments we were
676 gcc_assert (extra_levels
>= 0);
677 if (extra_levels
== 0)
680 /* Make a new set of arguments, not containing the inner arguments. */
681 new_args
= make_tree_vec (n
);
682 for (i
= 1; i
<= n
; ++i
)
683 SET_TMPL_ARGS_LEVEL (new_args
, i
,
684 TMPL_ARGS_LEVEL (args
, i
));
689 /* We've got a template header coming up; push to a new level for storing
693 begin_template_parm_list (void)
695 /* We use a non-tag-transparent scope here, which causes pushtag to
696 put tags in this scope, rather than in the enclosing class or
697 namespace scope. This is the right thing, since we want
698 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
699 global template class, push_template_decl handles putting the
700 TEMPLATE_DECL into top-level scope. For a nested template class,
703 template <class T> struct S1 {
704 template <class T> struct S2 {};
707 pushtag contains special code to insert the TEMPLATE_DECL for S2
708 at the right scope. */
709 begin_scope (sk_template_parms
, NULL
);
710 ++processing_template_decl
;
711 ++processing_template_parmlist
;
712 note_template_header (0);
714 /* Add a dummy parameter level while we process the parameter list. */
715 current_template_parms
716 = tree_cons (size_int (processing_template_decl
),
718 current_template_parms
);
721 /* This routine is called when a specialization is declared. If it is
722 invalid to declare a specialization here, an error is reported and
723 false is returned, otherwise this routine will return true. */
726 check_specialization_scope (void)
728 tree scope
= current_scope ();
732 An explicit specialization shall be declared in the namespace of
733 which the template is a member, or, for member templates, in the
734 namespace of which the enclosing class or enclosing class
735 template is a member. An explicit specialization of a member
736 function, member class or static data member of a class template
737 shall be declared in the namespace of which the class template
739 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
741 error ("explicit specialization in non-namespace scope %qD", scope
);
747 In an explicit specialization declaration for a member of a class
748 template or a member template that appears in namespace scope,
749 the member template and some of its enclosing class templates may
750 remain unspecialized, except that the declaration shall not
751 explicitly specialize a class member template if its enclosing
752 class templates are not explicitly specialized as well. */
753 if (current_template_parms
)
755 error ("enclosing class templates are not explicitly specialized");
762 /* We've just seen template <>. */
765 begin_specialization (void)
767 begin_scope (sk_template_spec
, NULL
);
768 note_template_header (1);
769 return check_specialization_scope ();
772 /* Called at then end of processing a declaration preceded by
776 end_specialization (void)
779 reset_specialization ();
782 /* Any template <>'s that we have seen thus far are not referring to a
783 function specialization. */
786 reset_specialization (void)
788 processing_specialization
= 0;
789 template_header_count
= 0;
792 /* We've just seen a template header. If SPECIALIZATION is nonzero,
793 it was of the form template <>. */
796 note_template_header (int specialization
)
798 processing_specialization
= specialization
;
799 template_header_count
++;
802 /* We're beginning an explicit instantiation. */
805 begin_explicit_instantiation (void)
807 gcc_assert (!processing_explicit_instantiation
);
808 processing_explicit_instantiation
= true;
813 end_explicit_instantiation (void)
815 gcc_assert (processing_explicit_instantiation
);
816 processing_explicit_instantiation
= false;
819 /* An explicit specialization or partial specialization of TMPL is being
820 declared. Check that the namespace in which the specialization is
821 occurring is permissible. Returns false iff it is invalid to
822 specialize TMPL in the current namespace. */
825 check_specialization_namespace (tree tmpl
)
827 tree tpl_ns
= decl_namespace_context (tmpl
);
831 An explicit specialization shall be declared in a namespace enclosing the
832 specialized template. An explicit specialization whose declarator-id is
833 not qualified shall be declared in the nearest enclosing namespace of the
834 template, or, if the namespace is inline (7.3.1), any namespace from its
835 enclosing namespace set. */
836 if (current_scope() != DECL_CONTEXT (tmpl
)
837 && !at_namespace_scope_p ())
839 error ("specialization of %qD must appear at namespace scope", tmpl
);
843 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
844 /* Same or enclosing namespace. */
848 auto_diagnostic_group d
;
849 if (permerror (input_location
,
850 "specialization of %qD in different namespace", tmpl
))
851 inform (DECL_SOURCE_LOCATION (tmpl
),
852 " from definition of %q#D", tmpl
);
857 /* SPEC is an explicit instantiation. Check that it is valid to
858 perform this explicit instantiation in the current namespace. */
861 check_explicit_instantiation_namespace (tree spec
)
865 /* DR 275: An explicit instantiation shall appear in an enclosing
866 namespace of its template. */
867 ns
= decl_namespace_context (spec
);
868 if (!is_nested_namespace (current_namespace
, ns
))
869 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
870 "(which does not enclose namespace %qD)",
871 spec
, current_namespace
, ns
);
874 /* Returns the type of a template specialization only if that
875 specialization needs to be defined. Otherwise (e.g., if the type has
876 already been defined), the function returns NULL_TREE. */
879 maybe_new_partial_specialization (tree type
)
881 /* An implicit instantiation of an incomplete type implies
882 the definition of a new class template.
890 Here, S<T*> is an implicit instantiation of S whose type
892 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
895 /* It can also be the case that TYPE is a completed specialization.
896 Continuing the previous example, suppose we also declare:
902 Here, S<T*> refers to the specialization S<T*> defined
903 above. However, we need to differentiate definitions because
904 we intend to define a new partial specialization. In this case,
905 we rely on the fact that the constraints are different for
906 this declaration than that above.
908 Note that we also get here for injected class names and
909 late-parsed template definitions. We must ensure that we
910 do not create new type declarations for those cases. */
911 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
913 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
914 tree args
= CLASSTYPE_TI_ARGS (type
);
916 /* If there are no template parameters, this cannot be a new
917 partial template specialization? */
918 if (!current_template_parms
)
921 /* The injected-class-name is not a new partial specialization. */
922 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
925 /* If the constraints are not the same as those of the primary
926 then, we can probably create a new specialization. */
927 tree type_constr
= current_template_constraints ();
929 if (type
== TREE_TYPE (tmpl
))
931 tree main_constr
= get_constraints (tmpl
);
932 if (equivalent_constraints (type_constr
, main_constr
))
936 /* Also, if there's a pre-existing specialization with matching
937 constraints, then this also isn't new. */
938 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
941 tree spec_tmpl
= TREE_VALUE (specs
);
942 tree spec_args
= TREE_PURPOSE (specs
);
943 tree spec_constr
= get_constraints (spec_tmpl
);
944 if (comp_template_args (args
, spec_args
)
945 && equivalent_constraints (type_constr
, spec_constr
))
947 specs
= TREE_CHAIN (specs
);
950 /* Create a new type node (and corresponding type decl)
951 for the newly declared specialization. */
952 tree t
= make_class_type (TREE_CODE (type
));
953 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
954 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
956 /* We only need a separate type node for storing the definition of this
957 partial specialization; uses of S<T*> are unconstrained, so all are
958 equivalent. So keep TYPE_CANONICAL the same. */
959 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
961 /* Build the corresponding type decl. */
962 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
963 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
964 DECL_SOURCE_LOCATION (d
) = input_location
;
965 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
966 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
974 /* The TYPE is being declared. If it is a template type, that means it
975 is a partial specialization. Do appropriate error-checking. */
978 maybe_process_partial_specialization (tree type
)
982 if (type
== error_mark_node
)
983 return error_mark_node
;
985 /* A lambda that appears in specialization context is not itself a
987 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
990 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
992 error ("name of class shadows template template parameter %qD",
994 return error_mark_node
;
997 context
= TYPE_CONTEXT (type
);
999 if (TYPE_ALIAS_P (type
))
1001 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1003 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1004 error ("specialization of alias template %qD",
1005 TI_TEMPLATE (tinfo
));
1007 error ("explicit specialization of non-template %qT", type
);
1008 return error_mark_node
;
1010 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1012 /* This is for ordinary explicit specialization and partial
1013 specialization of a template class such as:
1015 template <> class C<int>;
1019 template <class T> class C<T*>;
1021 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1023 if (tree t
= maybe_new_partial_specialization (type
))
1025 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1026 && !at_namespace_scope_p ())
1027 return error_mark_node
;
1028 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1029 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1030 if (processing_template_decl
)
1032 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1033 if (decl
== error_mark_node
)
1034 return error_mark_node
;
1035 return TREE_TYPE (decl
);
1038 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1039 error ("specialization of %qT after instantiation", type
);
1040 else if (errorcount
&& !processing_specialization
1041 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1042 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1043 /* Trying to define a specialization either without a template<> header
1044 or in an inappropriate place. We've already given an error, so just
1045 bail now so we don't actually define the specialization. */
1046 return error_mark_node
;
1048 else if (CLASS_TYPE_P (type
)
1049 && !CLASSTYPE_USE_TEMPLATE (type
)
1050 && CLASSTYPE_TEMPLATE_INFO (type
)
1051 && context
&& CLASS_TYPE_P (context
)
1052 && CLASSTYPE_TEMPLATE_INFO (context
))
1054 /* This is for an explicit specialization of member class
1055 template according to [temp.expl.spec/18]:
1057 template <> template <class U> class C<int>::D;
1059 The context `C<int>' must be an implicit instantiation.
1060 Otherwise this is just a member class template declared
1063 template <> class C<int> { template <class U> class D; };
1064 template <> template <class U> class C<int>::D;
1066 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1067 while in the second case, `C<int>::D' is a primary template
1068 and `C<T>::D' may not exist. */
1070 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1071 && !COMPLETE_TYPE_P (type
))
1074 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1076 if (current_namespace
1077 != decl_namespace_context (tmpl
))
1079 if (permerror (input_location
,
1080 "specialization of %qD in different namespace",
1082 inform (DECL_SOURCE_LOCATION (tmpl
),
1083 "from definition of %q#D", tmpl
);
1086 /* Check for invalid specialization after instantiation:
1088 template <> template <> class C<int>::D<int>;
1089 template <> template <class U> class C<int>::D; */
1091 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1092 t
; t
= TREE_CHAIN (t
))
1094 tree inst
= TREE_VALUE (t
);
1095 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1096 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1098 /* We already have a full specialization of this partial
1099 instantiation, or a full specialization has been
1100 looked up but not instantiated. Reassign it to the
1101 new member specialization template. */
1105 elt
.tmpl
= most_general_template (tmpl
);
1106 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1109 type_specializations
->remove_elt (&elt
);
1112 CLASSTYPE_TI_ARGS (inst
)
1113 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1116 = type_specializations
->find_slot (&elt
, INSERT
);
1117 entry
= ggc_alloc
<spec_entry
> ();
1122 /* But if we've had an implicit instantiation, that's a
1123 problem ([temp.expl.spec]/6). */
1124 error ("specialization %qT after instantiation %qT",
1128 /* Mark TYPE as a specialization. And as a result, we only
1129 have one level of template argument for the innermost
1131 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1132 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1133 CLASSTYPE_TI_ARGS (type
)
1134 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1137 else if (processing_specialization
)
1139 /* Someday C++0x may allow for enum template specialization. */
1140 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1141 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1142 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1143 "of %qD not allowed by ISO C++", type
);
1146 error ("explicit specialization of non-template %qT", type
);
1147 return error_mark_node
;
1154 /* Returns nonzero if we can optimize the retrieval of specializations
1155 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1156 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1159 optimize_specialization_lookup_p (tree tmpl
)
1161 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1162 && DECL_CLASS_SCOPE_P (tmpl
)
1163 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1165 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1166 /* The optimized lookup depends on the fact that the
1167 template arguments for the member function template apply
1168 purely to the containing class, which is not true if the
1169 containing class is an explicit or partial
1171 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1172 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1173 && !DECL_CONV_FN_P (tmpl
)
1174 /* It is possible to have a template that is not a member
1175 template and is not a member of a template class:
1177 template <typename T>
1178 struct S { friend A::f(); };
1180 Here, the friend function is a template, but the context does
1181 not have template information. The optimized lookup relies
1182 on having ARGS be the template arguments for both the class
1183 and the function template. */
1184 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1187 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1188 gone through coerce_template_parms by now. */
1191 verify_unstripped_args_1 (tree inner
)
1193 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1195 tree arg
= TREE_VEC_ELT (inner
, i
);
1196 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1198 else if (TYPE_P (arg
))
1199 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1200 else if (ARGUMENT_PACK_P (arg
))
1201 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1202 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1203 /* Allow typedefs on the type of a non-type argument, since a
1204 parameter can have them. */;
1206 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1211 verify_unstripped_args (tree args
)
1213 ++processing_template_decl
;
1214 if (!any_dependent_template_arguments_p (args
))
1215 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1216 --processing_template_decl
;
1219 /* Retrieve the specialization (in the sense of [temp.spec] - a
1220 specialization is either an instantiation or an explicit
1221 specialization) of TMPL for the given template ARGS. If there is
1222 no such specialization, return NULL_TREE. The ARGS are a vector of
1223 arguments, or a vector of vectors of arguments, in the case of
1224 templates with more than one level of parameters.
1226 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1227 then we search for a partial specialization matching ARGS. This
1228 parameter is ignored if TMPL is not a class template.
1230 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1231 result is a NONTYPE_ARGUMENT_PACK. */
1234 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1236 if (tmpl
== NULL_TREE
)
1239 if (args
== error_mark_node
)
1242 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1243 || TREE_CODE (tmpl
) == FIELD_DECL
);
1245 /* There should be as many levels of arguments as there are
1246 levels of parameters. */
1247 gcc_assert (TMPL_ARGS_DEPTH (args
)
1248 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1249 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1250 : template_class_depth (DECL_CONTEXT (tmpl
))));
1253 verify_unstripped_args (args
);
1255 /* Lambda functions in templates aren't instantiated normally, but through
1256 tsubst_lambda_expr. */
1257 if (lambda_fn_in_template_p (tmpl
))
1260 if (optimize_specialization_lookup_p (tmpl
))
1262 /* The template arguments actually apply to the containing
1263 class. Find the class specialization with those
1265 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1266 tree class_specialization
1267 = retrieve_specialization (class_template
, args
, 0);
1268 if (!class_specialization
)
1271 /* Find the instance of TMPL. */
1272 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1273 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1276 if (tree ti
= get_template_info (fn
))
1277 if (TI_TEMPLATE (ti
) == tmpl
1278 /* using-declarations can bring in a different
1279 instantiation of tmpl as a member of a different
1280 instantiation of tmpl's class. We don't want those
1282 && DECL_CONTEXT (fn
) == class_specialization
)
1291 spec_hash_table
*specializations
;
1295 elt
.spec
= NULL_TREE
;
1297 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1298 specializations
= type_specializations
;
1300 specializations
= decl_specializations
;
1303 hash
= spec_hasher::hash (&elt
);
1304 found
= specializations
->find_with_hash (&elt
, hash
);
1312 /* Like retrieve_specialization, but for local declarations. */
1315 retrieve_local_specialization (tree tmpl
)
1317 if (local_specializations
== NULL
)
1320 tree
*slot
= local_specializations
->get (tmpl
);
1321 return slot
? *slot
: NULL_TREE
;
1324 /* Returns nonzero iff DECL is a specialization of TMPL. */
1327 is_specialization_of (tree decl
, tree tmpl
)
1331 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1335 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1341 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1343 for (t
= TREE_TYPE (decl
);
1345 t
= CLASSTYPE_USE_TEMPLATE (t
)
1346 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1347 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1354 /* Returns nonzero iff DECL is a specialization of friend declaration
1355 FRIEND_DECL according to [temp.friend]. */
1358 is_specialization_of_friend (tree decl
, tree friend_decl
)
1360 bool need_template
= true;
1363 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1364 || TREE_CODE (decl
) == TYPE_DECL
);
1366 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1367 of a template class, we want to check if DECL is a specialization
1369 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1370 && DECL_TEMPLATE_INFO (friend_decl
)
1371 && !DECL_USE_TEMPLATE (friend_decl
))
1373 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1374 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1375 need_template
= false;
1377 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1378 && !PRIMARY_TEMPLATE_P (friend_decl
))
1379 need_template
= false;
1381 /* There is nothing to do if this is not a template friend. */
1382 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1385 if (is_specialization_of (decl
, friend_decl
))
1389 A member of a class template may be declared to be a friend of a
1390 non-template class. In this case, the corresponding member of
1391 every specialization of the class template is a friend of the
1392 class granting friendship.
1394 For example, given a template friend declaration
1396 template <class T> friend void A<T>::f();
1398 the member function below is considered a friend
1400 template <> struct A<int> {
1404 For this type of template friend, TEMPLATE_DEPTH below will be
1405 nonzero. To determine if DECL is a friend of FRIEND, we first
1406 check if the enclosing class is a specialization of another. */
1408 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1410 && DECL_CLASS_SCOPE_P (decl
)
1411 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1412 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1414 /* Next, we check the members themselves. In order to handle
1415 a few tricky cases, such as when FRIEND_DECL's are
1417 template <class T> friend void A<T>::g(T t);
1418 template <class T> template <T t> friend void A<T>::h();
1422 void A<int>::g(int);
1423 template <int> void A<int>::h();
1425 we need to figure out ARGS, the template arguments from
1426 the context of DECL. This is required for template substitution
1427 of `T' in the function parameter of `g' and template parameter
1428 of `h' in the above examples. Here ARGS corresponds to `int'. */
1430 tree context
= DECL_CONTEXT (decl
);
1431 tree args
= NULL_TREE
;
1432 int current_depth
= 0;
1434 while (current_depth
< template_depth
)
1436 if (CLASSTYPE_TEMPLATE_INFO (context
))
1438 if (current_depth
== 0)
1439 args
= TYPE_TI_ARGS (context
);
1441 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1444 context
= TYPE_CONTEXT (context
);
1447 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1452 tree friend_args_type
;
1453 tree decl_args_type
;
1455 /* Make sure that both DECL and FRIEND_DECL are templates or
1457 is_template
= DECL_TEMPLATE_INFO (decl
)
1458 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1459 if (need_template
^ is_template
)
1461 else if (is_template
)
1463 /* If both are templates, check template parameter list. */
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1467 if (!comp_template_parms
1468 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1472 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1475 decl_type
= TREE_TYPE (decl
);
1477 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1478 tf_none
, NULL_TREE
);
1479 if (friend_type
== error_mark_node
)
1482 /* Check if return types match. */
1483 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1486 /* Check if function parameter types match, ignoring the
1487 `this' parameter. */
1488 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1489 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1491 friend_args_type
= TREE_CHAIN (friend_args_type
);
1492 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1493 decl_args_type
= TREE_CHAIN (decl_args_type
);
1495 return compparms (decl_args_type
, friend_args_type
);
1499 /* DECL is a TYPE_DECL */
1501 tree decl_type
= TREE_TYPE (decl
);
1503 /* Make sure that both DECL and FRIEND_DECL are templates or
1506 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1507 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1509 if (need_template
^ is_template
)
1511 else if (is_template
)
1514 /* If both are templates, check the name of the two
1515 TEMPLATE_DECL's first because is_friend didn't. */
1516 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1517 != DECL_NAME (friend_decl
))
1520 /* Now check template parameter list. */
1522 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1524 return comp_template_parms
1525 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1529 return (DECL_NAME (decl
)
1530 == DECL_NAME (friend_decl
));
1536 /* Register the specialization SPEC as a specialization of TMPL with
1537 the indicated ARGS. IS_FRIEND indicates whether the specialization
1538 is actually just a friend declaration. ATTRLIST is the list of
1539 attributes that the specialization is declared with or NULL when
1540 it isn't. Returns SPEC, or an equivalent prior declaration, if
1543 We also store instantiations of field packs in the hash table, even
1544 though they are not themselves templates, to make lookup easier. */
1547 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1551 spec_entry
**slot
= NULL
;
1554 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1555 || (TREE_CODE (tmpl
) == FIELD_DECL
1556 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1558 if (TREE_CODE (spec
) == FUNCTION_DECL
1559 && uses_template_parms (DECL_TI_ARGS (spec
)))
1560 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1561 register it; we want the corresponding TEMPLATE_DECL instead.
1562 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1563 the more obvious `uses_template_parms (spec)' to avoid problems
1564 with default function arguments. In particular, given
1565 something like this:
1567 template <class T> void f(T t1, T t = T())
1569 the default argument expression is not substituted for in an
1570 instantiation unless and until it is actually needed. */
1573 if (optimize_specialization_lookup_p (tmpl
))
1574 /* We don't put these specializations in the hash table, but we might
1575 want to give an error about a mismatch. */
1576 fn
= retrieve_specialization (tmpl
, args
, 0);
1584 hash
= spec_hasher::hash (&elt
);
1586 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1593 /* We can sometimes try to re-register a specialization that we've
1594 already got. In particular, regenerate_decl_from_template calls
1595 duplicate_decls which will update the specialization list. But,
1596 we'll still get called again here anyhow. It's more convenient
1597 to simply allow this than to try to prevent it. */
1600 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1602 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1604 if (DECL_ODR_USED (fn
)
1605 || DECL_EXPLICIT_INSTANTIATION (fn
))
1607 error ("specialization of %qD after instantiation",
1609 return error_mark_node
;
1614 /* This situation should occur only if the first
1615 specialization is an implicit instantiation, the
1616 second is an explicit specialization, and the
1617 implicit instantiation has not yet been used. That
1618 situation can occur if we have implicitly
1619 instantiated a member function and then specialized
1622 We can also wind up here if a friend declaration that
1623 looked like an instantiation turns out to be a
1626 template <class T> void foo(T);
1627 class S { friend void foo<>(int) };
1628 template <> void foo(int);
1630 We transform the existing DECL in place so that any
1631 pointers to it become pointers to the updated
1634 If there was a definition for the template, but not
1635 for the specialization, we want this to look as if
1636 there were no definition, and vice versa. */
1637 DECL_INITIAL (fn
) = NULL_TREE
;
1638 duplicate_decls (spec
, fn
, is_friend
);
1639 /* The call to duplicate_decls will have applied
1642 An explicit specialization of a function template
1643 is inline only if it is explicitly declared to be,
1644 and independently of whether its function template
1647 to the primary function; now copy the inline bits to
1648 the various clones. */
1649 FOR_EACH_CLONE (clone
, fn
)
1651 DECL_DECLARED_INLINE_P (clone
)
1652 = DECL_DECLARED_INLINE_P (fn
);
1653 DECL_SOURCE_LOCATION (clone
)
1654 = DECL_SOURCE_LOCATION (fn
);
1655 DECL_DELETED_FN (clone
)
1656 = DECL_DELETED_FN (fn
);
1658 check_specialization_namespace (tmpl
);
1663 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1665 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1666 if (dd
== error_mark_node
)
1667 /* We've already complained in duplicate_decls. */
1668 return error_mark_node
;
1670 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1671 /* Dup decl failed, but this is a new definition. Set the
1672 line number so any errors match this new
1674 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1680 return duplicate_decls (spec
, fn
, is_friend
);
1682 /* A specialization must be declared in the same namespace as the
1683 template it is specializing. */
1684 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1685 && !check_specialization_namespace (tmpl
))
1686 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1688 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1690 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1691 gcc_assert (tmpl
&& args
&& spec
);
1694 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1695 && PRIMARY_TEMPLATE_P (tmpl
)
1696 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1697 || variable_template_p (tmpl
))
1698 /* If TMPL is a forward declaration of a template function, keep a list
1699 of all specializations in case we need to reassign them to a friend
1700 template later in tsubst_friend_function.
1702 Also keep a list of all variable template instantiations so that
1703 process_partial_specialization can check whether a later partial
1704 specialization would have used it. */
1705 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1706 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1712 /* Returns true iff two spec_entry nodes are equivalent. */
1714 int comparing_specializations
;
1717 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1721 ++comparing_specializations
;
1722 equal
= (e1
->tmpl
== e2
->tmpl
1723 && comp_template_args (e1
->args
, e2
->args
));
1724 if (equal
&& flag_concepts
1725 /* tmpl could be a FIELD_DECL for a capture pack. */
1726 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1727 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1728 && uses_template_parms (e1
->args
))
1730 /* Partial specializations of a variable template can be distinguished by
1732 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1733 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1734 equal
= equivalent_constraints (c1
, c2
);
1736 --comparing_specializations
;
1741 /* Returns a hash for a template TMPL and template arguments ARGS. */
1744 hash_tmpl_and_args (tree tmpl
, tree args
)
1746 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1747 return iterative_hash_template_arg (args
, val
);
1750 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1754 spec_hasher::hash (spec_entry
*e
)
1756 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1759 /* Recursively calculate a hash value for a template argument ARG, for use
1760 in the hash tables of template specializations. We must be
1761 careful to (at least) skip the same entities template_args_equal
1765 iterative_hash_template_arg (tree arg
, hashval_t val
)
1767 if (arg
== NULL_TREE
)
1768 return iterative_hash_object (arg
, val
);
1771 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1772 while (CONVERT_EXPR_P (arg
)
1773 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1774 || class_nttp_const_wrapper_p (arg
))
1775 arg
= TREE_OPERAND (arg
, 0);
1777 enum tree_code code
= TREE_CODE (arg
);
1779 val
= iterative_hash_object (code
, val
);
1783 case ARGUMENT_PACK_SELECT
:
1789 case IDENTIFIER_NODE
:
1790 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1793 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1794 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1797 case TYPE_PACK_EXPANSION
:
1798 case EXPR_PACK_EXPANSION
:
1799 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1800 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1802 case TYPE_ARGUMENT_PACK
:
1803 case NONTYPE_ARGUMENT_PACK
:
1804 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1807 for (; arg
; arg
= TREE_CHAIN (arg
))
1808 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1812 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1813 val
= iterative_hash_template_arg (*iter
, val
);
1820 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1821 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1823 val
= iterative_hash_template_arg (field
, val
);
1824 val
= iterative_hash_template_arg (value
, val
);
1830 if (!DECL_ARTIFICIAL (arg
))
1832 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1833 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1835 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1838 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1841 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1842 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1844 case TEMPLATE_PARM_INDEX
:
1845 val
= iterative_hash_template_arg
1846 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1847 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1848 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1851 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1852 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1853 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1856 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1858 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1862 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1863 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1864 val
= iterative_hash_object (code
, val
);
1865 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1868 /* [temp.over.link] Two lambda-expressions are never considered
1871 So just hash the closure type. */
1872 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1875 case IMPLICIT_CONV_EXPR
:
1876 case STATIC_CAST_EXPR
:
1877 case REINTERPRET_CAST_EXPR
:
1878 case CONST_CAST_EXPR
:
1879 case DYNAMIC_CAST_EXPR
:
1881 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1882 /* Now hash operands as usual. */
1887 tree fn
= CALL_EXPR_FN (arg
);
1888 if (tree name
= dependent_name (fn
))
1890 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1891 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1894 val
= iterative_hash_template_arg (fn
, val
);
1895 call_expr_arg_iterator ai
;
1896 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1897 x
= next_call_expr_arg (&ai
))
1898 val
= iterative_hash_template_arg (x
, val
);
1906 char tclass
= TREE_CODE_CLASS (code
);
1910 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1912 // We want an alias specialization that survived strip_typedefs
1913 // to hash differently from its TYPE_CANONICAL, to avoid hash
1914 // collisions that compare as different in template_args_equal.
1915 // These could be dependent specializations that strip_typedefs
1916 // left alone, or untouched specializations because
1917 // coerce_template_parms returns the unconverted template
1918 // arguments if it sees incomplete argument packs.
1919 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1920 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1923 switch (TREE_CODE (arg
))
1925 case TEMPLATE_TEMPLATE_PARM
:
1927 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1929 /* Do not recurse with TPI directly, as that is unbounded
1931 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1932 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1937 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1941 if (tree canonical
= TYPE_CANONICAL (arg
))
1942 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1948 case tcc_declaration
:
1950 return iterative_hash_expr (arg
, val
);
1953 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1954 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1955 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1963 /* Unregister the specialization SPEC as a specialization of TMPL.
1964 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1965 if the SPEC was listed as a specialization of TMPL.
1967 Note that SPEC has been ggc_freed, so we can't look inside it. */
1970 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1975 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1976 elt
.args
= TI_ARGS (tinfo
);
1977 elt
.spec
= NULL_TREE
;
1979 entry
= decl_specializations
->find (&elt
);
1982 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1983 gcc_assert (new_spec
!= NULL_TREE
);
1984 entry
->spec
= new_spec
;
1991 /* Like register_specialization, but for local declarations. We are
1992 registering SPEC, an instantiation of TMPL. */
1995 register_local_specialization (tree spec
, tree tmpl
)
1997 gcc_assert (tmpl
!= spec
);
1998 local_specializations
->put (tmpl
, spec
);
2001 /* TYPE is a class type. Returns true if TYPE is an explicitly
2002 specialized class. */
2005 explicit_class_specialization_p (tree type
)
2007 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2009 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2012 /* Print the list of functions at FNS, going through all the overloads
2013 for each element of the list. Alternatively, FNS cannot be a
2014 TREE_LIST, in which case it will be printed together with all the
2017 MORE and *STR should respectively be FALSE and NULL when the function
2018 is called from the outside. They are used internally on recursive
2019 calls. print_candidates manages the two parameters and leaves NULL
2020 in *STR when it ends. */
2023 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2025 if (TREE_CODE (fns
) == TREE_LIST
)
2026 for (; fns
; fns
= TREE_CHAIN (fns
))
2027 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2029 for (lkp_iterator
iter (fns
); iter
;)
2034 const char *pfx
= *str
;
2038 pfx
= _("candidates are:");
2040 pfx
= _("candidate is:");
2041 *str
= get_spaces (pfx
);
2043 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2047 /* Print the list of candidate FNS in an error message. FNS can also
2048 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2051 print_candidates (tree fns
)
2054 print_candidates_1 (fns
, &str
);
2058 /* Get a (possibly) constrained template declaration for the
2059 purpose of ordering candidates. */
2061 get_template_for_ordering (tree list
)
2063 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2064 tree f
= TREE_VALUE (list
);
2065 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2066 return TI_TEMPLATE (ti
);
2070 /* Among candidates having the same signature, return the
2071 most constrained or NULL_TREE if there is no best candidate.
2072 If the signatures of candidates vary (e.g., template
2073 specialization vs. member function), then there can be no
2076 Note that we don't compare constraints on the functions
2077 themselves, but rather those of their templates. */
2079 most_constrained_function (tree candidates
)
2081 // Try to find the best candidate in a first pass.
2082 tree champ
= candidates
;
2083 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2085 int winner
= more_constrained (get_template_for_ordering (champ
),
2086 get_template_for_ordering (c
));
2088 champ
= c
; // The candidate is more constrained
2089 else if (winner
== 0)
2090 return NULL_TREE
; // Neither is more constrained
2093 // Verify that the champ is better than previous candidates.
2094 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2095 if (!more_constrained (get_template_for_ordering (champ
),
2096 get_template_for_ordering (c
)))
2104 /* Returns the template (one of the functions given by TEMPLATE_ID)
2105 which can be specialized to match the indicated DECL with the
2106 explicit template args given in TEMPLATE_ID. The DECL may be
2107 NULL_TREE if none is available. In that case, the functions in
2108 TEMPLATE_ID are non-members.
2110 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2111 specialization of a member template.
2113 The TEMPLATE_COUNT is the number of references to qualifying
2114 template classes that appeared in the name of the function. See
2115 check_explicit_specialization for a more accurate description.
2117 TSK indicates what kind of template declaration (if any) is being
2118 declared. TSK_TEMPLATE indicates that the declaration given by
2119 DECL, though a FUNCTION_DECL, has template parameters, and is
2120 therefore a template function.
2122 The template args (those explicitly specified and those deduced)
2123 are output in a newly created vector *TARGS_OUT.
2125 If it is impossible to determine the result, an error message is
2126 issued. The error_mark_node is returned to indicate failure. */
2129 determine_specialization (tree template_id
,
2132 int need_member_template
,
2138 tree explicit_targs
;
2139 tree candidates
= NULL_TREE
;
2141 /* A TREE_LIST of templates of which DECL may be a specialization.
2142 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2143 corresponding TREE_PURPOSE is the set of template arguments that,
2144 when used to instantiate the template, would produce a function
2145 with the signature of DECL. */
2146 tree templates
= NULL_TREE
;
2148 cp_binding_level
*b
;
2150 *targs_out
= NULL_TREE
;
2152 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2153 return error_mark_node
;
2155 /* We shouldn't be specializing a member template of an
2156 unspecialized class template; we already gave an error in
2157 check_specialization_scope, now avoid crashing. */
2159 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2160 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2162 gcc_assert (errorcount
);
2163 return error_mark_node
;
2166 fns
= TREE_OPERAND (template_id
, 0);
2167 explicit_targs
= TREE_OPERAND (template_id
, 1);
2169 if (fns
== error_mark_node
)
2170 return error_mark_node
;
2172 /* Check for baselinks. */
2173 if (BASELINK_P (fns
))
2174 fns
= BASELINK_FUNCTIONS (fns
);
2176 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2178 error_at (DECL_SOURCE_LOCATION (decl
),
2179 "%qD is not a function template", fns
);
2180 return error_mark_node
;
2182 else if (VAR_P (decl
) && !variable_template_p (fns
))
2184 error ("%qD is not a variable template", fns
);
2185 return error_mark_node
;
2188 /* Count the number of template headers specified for this
2191 for (b
= current_binding_level
;
2192 b
->kind
== sk_template_parms
;
2196 tree orig_fns
= fns
;
2198 if (variable_template_p (fns
))
2200 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2201 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2202 tf_warning_or_error
,
2203 /*req_all*/true, /*use_defarg*/true);
2204 if (targs
!= error_mark_node
)
2205 templates
= tree_cons (targs
, fns
, templates
);
2207 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2211 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2213 tree decl_arg_types
;
2217 /* In case of explicit specialization, we need to check if
2218 the number of template headers appearing in the specialization
2219 is correct. This is usually done in check_explicit_specialization,
2220 but the check done there cannot be exhaustive when specializing
2221 member functions. Consider the following code:
2223 template <> void A<int>::f(int);
2224 template <> template <> void A<int>::f(int);
2226 Assuming that A<int> is not itself an explicit specialization
2227 already, the first line specializes "f" which is a non-template
2228 member function, whilst the second line specializes "f" which
2229 is a template member function. So both lines are syntactically
2230 correct, and check_explicit_specialization does not reject
2233 Here, we can do better, as we are matching the specialization
2234 against the declarations. We count the number of template
2235 headers, and we check if they match TEMPLATE_COUNT + 1
2236 (TEMPLATE_COUNT is the number of qualifying template classes,
2237 plus there must be another header for the member template
2240 Notice that if header_count is zero, this is not a
2241 specialization but rather a template instantiation, so there
2242 is no check we can perform here. */
2243 if (header_count
&& header_count
!= template_count
+ 1)
2246 /* Check that the number of template arguments at the
2247 innermost level for DECL is the same as for FN. */
2248 if (current_binding_level
->kind
== sk_template_parms
2249 && !current_binding_level
->explicit_spec_p
2250 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2251 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2252 (current_template_parms
))))
2255 /* DECL might be a specialization of FN. */
2256 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2257 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2259 /* For a non-static member function, we need to make sure
2260 that the const qualification is the same. Since
2261 get_bindings does not try to merge the "this" parameter,
2262 we must do the comparison explicitly. */
2263 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2265 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2266 TREE_VALUE (decl_arg_types
)))
2269 /* And the ref-qualification. */
2270 if (type_memfn_rqual (TREE_TYPE (decl
))
2271 != type_memfn_rqual (TREE_TYPE (fn
)))
2275 /* Skip the "this" parameter and, for constructors of
2276 classes with virtual bases, the VTT parameter. A
2277 full specialization of a constructor will have a VTT
2278 parameter, but a template never will. */
2280 = skip_artificial_parms_for (decl
, decl_arg_types
);
2282 = skip_artificial_parms_for (fn
, fn_arg_types
);
2284 /* Function templates cannot be specializations; there are
2285 no partial specializations of functions. Therefore, if
2286 the type of DECL does not match FN, there is no
2289 Note that it should never be the case that we have both
2290 candidates added here, and for regular member functions
2292 if (tsk
== tsk_template
)
2294 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2295 current_template_parms
))
2297 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2298 TREE_TYPE (TREE_TYPE (fn
))))
2300 if (!compparms (fn_arg_types
, decl_arg_types
))
2303 tree freq
= get_trailing_function_requirements (fn
);
2304 tree dreq
= get_trailing_function_requirements (decl
);
2309 tree fargs
= DECL_TI_ARGS (fn
);
2310 tsubst_flags_t complain
= tf_none
;
2311 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2312 if (!cp_tree_equal (freq
, dreq
))
2316 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2320 /* See whether this function might be a specialization of this
2321 template. Suppress access control because we might be trying
2322 to make this specialization a friend, and we have already done
2323 access control for the declaration of the specialization. */
2324 push_deferring_access_checks (dk_no_check
);
2325 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2326 pop_deferring_access_checks ();
2329 /* We cannot deduce template arguments that when used to
2330 specialize TMPL will produce DECL. */
2333 if (uses_template_parms (targs
))
2334 /* We deduced something involving 'auto', which isn't a valid
2335 template argument. */
2338 /* Remove, from the set of candidates, all those functions
2339 whose constraints are not satisfied. */
2340 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2343 // Then, try to form the new function type.
2344 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2345 if (insttype
== error_mark_node
)
2348 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2349 if (!compparms (fn_arg_types
, decl_arg_types
))
2352 /* Save this template, and the arguments deduced. */
2353 templates
= tree_cons (targs
, fn
, templates
);
2355 else if (need_member_template
)
2356 /* FN is an ordinary member function, and we need a
2357 specialization of a member template. */
2359 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2360 /* We can get IDENTIFIER_NODEs here in certain erroneous
2363 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2364 /* This is just an ordinary non-member function. Nothing can
2365 be a specialization of that. */
2367 else if (DECL_ARTIFICIAL (fn
))
2368 /* Cannot specialize functions that are created implicitly. */
2372 tree decl_arg_types
;
2374 /* This is an ordinary member function. However, since
2375 we're here, we can assume its enclosing class is a
2376 template class. For example,
2378 template <typename T> struct S { void f(); };
2379 template <> void S<int>::f() {}
2381 Here, S<int>::f is a non-template, but S<int> is a
2382 template class. If FN has the same type as DECL, we
2383 might be in business. */
2385 if (!DECL_TEMPLATE_INFO (fn
))
2386 /* Its enclosing class is an explicit specialization
2387 of a template class. This is not a candidate. */
2390 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2391 TREE_TYPE (TREE_TYPE (fn
))))
2392 /* The return types differ. */
2395 /* Adjust the type of DECL in case FN is a static member. */
2396 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2397 if (DECL_STATIC_FUNCTION_P (fn
)
2398 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2399 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2401 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2405 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2406 && (type_memfn_rqual (TREE_TYPE (decl
))
2407 != type_memfn_rqual (TREE_TYPE (fn
))))
2410 // If the deduced arguments do not satisfy the constraints,
2411 // this is not a candidate.
2412 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2415 // Add the candidate.
2416 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2420 if (templates
&& TREE_CHAIN (templates
))
2426 It is possible for a specialization with a given function
2427 signature to be instantiated from more than one function
2428 template. In such cases, explicit specification of the
2429 template arguments must be used to uniquely identify the
2430 function template specialization being specialized.
2432 Note that here, there's no suggestion that we're supposed to
2433 determine which of the candidate templates is most
2434 specialized. However, we, also have:
2438 Partial ordering of overloaded function template
2439 declarations is used in the following contexts to select
2440 the function template to which a function template
2441 specialization refers:
2443 -- when an explicit specialization refers to a function
2446 So, we do use the partial ordering rules, at least for now.
2447 This extension can only serve to make invalid programs valid,
2448 so it's safe. And, there is strong anecdotal evidence that
2449 the committee intended the partial ordering rules to apply;
2450 the EDG front end has that behavior, and John Spicer claims
2451 that the committee simply forgot to delete the wording in
2452 [temp.expl.spec]. */
2453 tree tmpl
= most_specialized_instantiation (templates
);
2454 if (tmpl
!= error_mark_node
)
2457 TREE_CHAIN (templates
) = NULL_TREE
;
2461 // Concepts allows multiple declarations of member functions
2462 // with the same signature. Like above, we need to rely on
2463 // on the partial ordering of those candidates to determine which
2465 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2467 if (tree cand
= most_constrained_function (candidates
))
2470 TREE_CHAIN (cand
) = NULL_TREE
;
2474 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2476 error ("template-id %qD for %q+D does not match any template "
2477 "declaration", template_id
, decl
);
2478 if (header_count
&& header_count
!= template_count
+ 1)
2479 inform (DECL_SOURCE_LOCATION (decl
),
2480 "saw %d %<template<>%>, need %d for "
2481 "specializing a member function template",
2482 header_count
, template_count
+ 1);
2484 print_candidates (orig_fns
);
2485 return error_mark_node
;
2487 else if ((templates
&& TREE_CHAIN (templates
))
2488 || (candidates
&& TREE_CHAIN (candidates
))
2489 || (templates
&& candidates
))
2491 error ("ambiguous template specialization %qD for %q+D",
2493 candidates
= chainon (candidates
, templates
);
2494 print_candidates (candidates
);
2495 return error_mark_node
;
2498 /* We have one, and exactly one, match. */
2501 tree fn
= TREE_VALUE (candidates
);
2502 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2504 /* Propagate the candidate's constraints to the declaration. */
2505 if (tsk
!= tsk_template
)
2506 set_constraints (decl
, get_constraints (fn
));
2508 /* DECL is a re-declaration or partial instantiation of a template
2510 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2512 /* It was a specialization of an ordinary member function in a
2514 return DECL_TI_TEMPLATE (fn
);
2517 /* It was a specialization of a template. */
2518 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2519 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2521 *targs_out
= copy_node (targs
);
2522 SET_TMPL_ARGS_LEVEL (*targs_out
,
2523 TMPL_ARGS_DEPTH (*targs_out
),
2524 TREE_PURPOSE (templates
));
2527 *targs_out
= TREE_PURPOSE (templates
);
2528 return TREE_VALUE (templates
);
2531 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2532 but with the default argument values filled in from those in the
2536 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2539 tree new_spec_types
;
2544 if (spec_types
== void_list_node
)
2545 return void_list_node
;
2547 /* Substitute into the rest of the list. */
2549 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2550 TREE_CHAIN (tmpl_types
));
2552 /* Add the default argument for this parameter. */
2553 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2554 TREE_VALUE (spec_types
),
2558 /* DECL is an explicit specialization. Replicate default arguments
2559 from the template it specializes. (That way, code like:
2561 template <class T> void f(T = 3);
2562 template <> void f(double);
2565 works, as required.) An alternative approach would be to look up
2566 the correct default arguments at the call-site, but this approach
2567 is consistent with how implicit instantiations are handled. */
2570 copy_default_args_to_explicit_spec (tree decl
)
2575 tree new_spec_types
;
2579 tree object_type
= NULL_TREE
;
2580 tree in_charge
= NULL_TREE
;
2581 tree vtt
= NULL_TREE
;
2583 /* See if there's anything we need to do. */
2584 tmpl
= DECL_TI_TEMPLATE (decl
);
2585 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2586 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2587 if (TREE_PURPOSE (t
))
2592 old_type
= TREE_TYPE (decl
);
2593 spec_types
= TYPE_ARG_TYPES (old_type
);
2595 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2597 /* Remove the this pointer, but remember the object's type for
2599 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2600 spec_types
= TREE_CHAIN (spec_types
);
2601 tmpl_types
= TREE_CHAIN (tmpl_types
);
2603 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2605 /* DECL may contain more parameters than TMPL due to the extra
2606 in-charge parameter in constructors and destructors. */
2607 in_charge
= spec_types
;
2608 spec_types
= TREE_CHAIN (spec_types
);
2610 if (DECL_HAS_VTT_PARM_P (decl
))
2613 spec_types
= TREE_CHAIN (spec_types
);
2617 /* Compute the merged default arguments. */
2619 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2621 /* Compute the new FUNCTION_TYPE. */
2625 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2630 /* Put the in-charge parameter back. */
2631 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2632 TREE_VALUE (in_charge
),
2635 new_type
= build_method_type_directly (object_type
,
2636 TREE_TYPE (old_type
),
2640 new_type
= build_function_type (TREE_TYPE (old_type
),
2642 new_type
= cp_build_type_attribute_variant (new_type
,
2643 TYPE_ATTRIBUTES (old_type
));
2644 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2646 TREE_TYPE (decl
) = new_type
;
2649 /* Return the number of template headers we expect to see for a definition
2650 or specialization of CTYPE or one of its non-template members. */
2653 num_template_headers_for_class (tree ctype
)
2655 int num_templates
= 0;
2657 while (ctype
&& CLASS_TYPE_P (ctype
))
2659 /* You're supposed to have one `template <...>' for every
2660 template class, but you don't need one for a full
2661 specialization. For example:
2663 template <class T> struct S{};
2664 template <> struct S<int> { void f(); };
2665 void S<int>::f () {}
2667 is correct; there shouldn't be a `template <>' for the
2668 definition of `S<int>::f'. */
2669 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2670 /* If CTYPE does not have template information of any
2671 kind, then it is not a template, nor is it nested
2672 within a template. */
2674 if (explicit_class_specialization_p (ctype
))
2676 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2679 ctype
= TYPE_CONTEXT (ctype
);
2682 return num_templates
;
2685 /* Do a simple sanity check on the template headers that precede the
2686 variable declaration DECL. */
2689 check_template_variable (tree decl
)
2691 tree ctx
= CP_DECL_CONTEXT (decl
);
2692 int wanted
= num_template_headers_for_class (ctx
);
2693 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2694 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2696 if (cxx_dialect
< cxx14
)
2697 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2698 "variable templates only available with "
2699 "%<-std=c++14%> or %<-std=gnu++14%>");
2701 // Namespace-scope variable templates should have a template header.
2704 if (template_header_count
> wanted
)
2706 auto_diagnostic_group d
;
2707 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2708 "too many template headers for %qD "
2711 if (warned
&& CLASS_TYPE_P (ctx
)
2712 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2713 inform (DECL_SOURCE_LOCATION (decl
),
2714 "members of an explicitly specialized class are defined "
2715 "without a template header");
2719 /* An explicit specialization whose declarator-id or class-head-name is not
2720 qualified shall be declared in the nearest enclosing namespace of the
2721 template, or, if the namespace is inline (7.3.1), any namespace from its
2722 enclosing namespace set.
2724 If the name declared in the explicit instantiation is an unqualified name,
2725 the explicit instantiation shall appear in the namespace where its template
2726 is declared or, if that namespace is inline (7.3.1), any namespace from its
2727 enclosing namespace set. */
2730 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2732 tree tmpl
= most_general_template (t
);
2733 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2734 && !is_nested_namespace (current_namespace
,
2735 CP_DECL_CONTEXT (tmpl
), true))
2737 if (processing_specialization
)
2738 permerror (loc
, "explicit specialization of %qD outside its "
2739 "namespace must use a nested-name-specifier", tmpl
);
2740 else if (processing_explicit_instantiation
2741 && cxx_dialect
>= cxx11
)
2742 /* This was allowed in C++98, so only pedwarn. */
2743 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2744 "outside its namespace must use a nested-name-"
2749 /* Warn for a template specialization SPEC that is missing some of a set
2750 of function or type attributes that the template TEMPL is declared with.
2751 ATTRLIST is a list of additional attributes that SPEC should be taken
2752 to ultimately be declared with. */
2755 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2757 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2758 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2760 /* Avoid warning if the difference between the primary and
2761 the specialization is not in one of the attributes below. */
2762 const char* const blacklist
[] = {
2763 "alloc_align", "alloc_size", "assume_aligned", "format",
2764 "format_arg", "malloc", "nonnull", NULL
2767 /* Put together a list of the black listed attributes that the primary
2768 template is declared with that the specialization is not, in case
2769 it's not apparent from the most recent declaration of the primary. */
2771 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2777 auto_diagnostic_group d
;
2778 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2779 "explicit specialization %q#D may be missing attributes",
2781 inform (DECL_SOURCE_LOCATION (tmpl
),
2783 ? G_("missing primary template attributes %s")
2784 : G_("missing primary template attribute %s"),
2785 pp_formatted_text (&str
));
2788 /* Check to see if the function just declared, as indicated in
2789 DECLARATOR, and in DECL, is a specialization of a function
2790 template. We may also discover that the declaration is an explicit
2791 instantiation at this point.
2793 Returns DECL, or an equivalent declaration that should be used
2794 instead if all goes well. Issues an error message if something is
2795 amiss. Returns error_mark_node if the error is not easily
2798 FLAGS is a bitmask consisting of the following flags:
2800 2: The function has a definition.
2801 4: The function is a friend.
2803 The TEMPLATE_COUNT is the number of references to qualifying
2804 template classes that appeared in the name of the function. For
2807 template <class T> struct S { void f(); };
2810 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2811 classes are not counted in the TEMPLATE_COUNT, so that in
2813 template <class T> struct S {};
2814 template <> struct S<int> { void f(); }
2815 template <> void S<int>::f();
2817 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2818 invalid; there should be no template <>.)
2820 If the function is a specialization, it is marked as such via
2821 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2822 is set up correctly, and it is added to the list of specializations
2823 for that template. */
2826 check_explicit_specialization (tree declarator
,
2832 int have_def
= flags
& 2;
2833 int is_friend
= flags
& 4;
2834 bool is_concept
= flags
& 8;
2835 int specialization
= 0;
2836 int explicit_instantiation
= 0;
2837 int member_specialization
= 0;
2838 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2839 tree dname
= DECL_NAME (decl
);
2844 if (!processing_specialization
)
2847 tsk
= tsk_excessive_parms
;
2850 tsk
= current_tmpl_spec_kind (template_count
);
2855 if (processing_specialization
&& !VAR_P (decl
))
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2860 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2863 /* This could be something like:
2865 template <class T> void f(T);
2866 class S { friend void f<>(int); } */
2870 /* This case handles bogus declarations like template <>
2871 template <class T> void f<int>(); */
2873 error_at (cp_expr_loc_or_input_loc (declarator
),
2874 "template-id %qE in declaration of primary template",
2881 case tsk_invalid_member_spec
:
2882 /* The error has already been reported in
2883 check_specialization_scope. */
2884 return error_mark_node
;
2886 case tsk_invalid_expl_inst
:
2887 error ("template parameter list used in explicit instantiation");
2893 error ("definition provided for explicit instantiation");
2895 explicit_instantiation
= 1;
2898 case tsk_excessive_parms
:
2899 case tsk_insufficient_parms
:
2900 if (tsk
== tsk_excessive_parms
)
2901 error ("too many template parameter lists in declaration of %qD",
2903 else if (template_header_count
)
2904 error("too few template parameter lists in declaration of %qD", decl
);
2906 error("explicit specialization of %qD must be introduced by "
2907 "%<template <>%>", decl
);
2912 error ("explicit specialization declared %<concept%>");
2914 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2915 /* In cases like template<> constexpr bool v = true;
2916 We'll give an error in check_template_variable. */
2919 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2921 member_specialization
= 1;
2927 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2929 /* This case handles bogus declarations like template <>
2930 template <class T> void f<int>(); */
2932 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2933 error_at (cp_expr_loc_or_input_loc (declarator
),
2934 "template-id %qE in declaration of primary template",
2936 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2938 /* Partial specialization of variable template. */
2939 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2943 else if (cxx_dialect
< cxx14
)
2944 error_at (cp_expr_loc_or_input_loc (declarator
),
2945 "non-type partial specialization %qE "
2946 "is not allowed", declarator
);
2948 error_at (cp_expr_loc_or_input_loc (declarator
),
2949 "non-class, non-variable partial specialization %qE "
2950 "is not allowed", declarator
);
2955 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2956 /* This is a specialization of a member template, without
2957 specialization the containing class. Something like:
2959 template <class T> struct S {
2960 template <class U> void f (U);
2962 template <> template <class U> void S<int>::f(U) {}
2964 That's a specialization -- but of the entire template. */
2972 if ((specialization
|| member_specialization
)
2973 /* This doesn't apply to variable templates. */
2974 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2976 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2977 for (; t
; t
= TREE_CHAIN (t
))
2978 if (TREE_PURPOSE (t
))
2980 permerror (input_location
,
2981 "default argument specified in explicit specialization");
2986 if (specialization
|| member_specialization
|| explicit_instantiation
)
2988 tree tmpl
= NULL_TREE
;
2989 tree targs
= NULL_TREE
;
2990 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2992 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2993 if (!was_template_id
)
2997 gcc_assert (identifier_p (declarator
));
3002 /* If there is no class context, the explicit instantiation
3003 must be at namespace scope. */
3004 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3006 /* Find the namespace binding, using the declaration
3008 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3009 LOOK_want::NORMAL
, true);
3010 if (fns
== error_mark_node
)
3011 /* If lookup fails, look for a friend declaration so we can
3012 give a better diagnostic. */
3013 fns
= (lookup_qualified_name
3014 (CP_DECL_CONTEXT (decl
), dname
,
3015 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3018 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3020 error ("%qD is not a template function", dname
);
3021 fns
= error_mark_node
;
3025 declarator
= lookup_template_function (fns
, NULL_TREE
);
3028 if (declarator
== error_mark_node
)
3029 return error_mark_node
;
3031 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3033 if (!explicit_instantiation
)
3034 /* A specialization in class scope. This is invalid,
3035 but the error will already have been flagged by
3036 check_specialization_scope. */
3037 return error_mark_node
;
3040 /* It's not valid to write an explicit instantiation in
3043 class C { template void f(); }
3045 This case is caught by the parser. However, on
3048 template class C { void f(); };
3050 (which is invalid) we can get here. The error will be
3057 else if (ctype
!= NULL_TREE
3058 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3060 // We'll match variable templates in start_decl.
3064 /* Find the list of functions in ctype that have the same
3065 name as the declared function. */
3066 tree name
= TREE_OPERAND (declarator
, 0);
3068 if (constructor_name_p (name
, ctype
))
3070 if (DECL_CONSTRUCTOR_P (decl
)
3071 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3072 : !CLASSTYPE_DESTRUCTOR (ctype
))
3074 /* From [temp.expl.spec]:
3076 If such an explicit specialization for the member
3077 of a class template names an implicitly-declared
3078 special member function (clause _special_), the
3079 program is ill-formed.
3081 Similar language is found in [temp.explicit]. */
3082 error ("specialization of implicitly-declared special member function");
3083 return error_mark_node
;
3086 name
= DECL_NAME (decl
);
3089 /* For a type-conversion operator, We might be looking for
3090 `operator int' which will be a specialization of
3091 `operator T'. Grab all the conversion operators, and
3092 then select from them. */
3093 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3094 ? conv_op_identifier
: name
);
3096 if (fns
== NULL_TREE
)
3098 error ("no member function %qD declared in %qT", name
, ctype
);
3099 return error_mark_node
;
3102 TREE_OPERAND (declarator
, 0) = fns
;
3105 /* Figure out what exactly is being specialized at this point.
3106 Note that for an explicit instantiation, even one for a
3107 member function, we cannot tell a priori whether the
3108 instantiation is for a member template, or just a member
3109 function of a template class. Even if a member template is
3110 being instantiated, the member template arguments may be
3111 elided if they can be deduced from the rest of the
3113 tmpl
= determine_specialization (declarator
, decl
,
3115 member_specialization
,
3119 if (!tmpl
|| tmpl
== error_mark_node
)
3120 /* We couldn't figure out what this declaration was
3122 return error_mark_node
;
3125 if (TREE_CODE (decl
) == FUNCTION_DECL
3126 && DECL_HIDDEN_FRIEND_P (tmpl
))
3128 auto_diagnostic_group d
;
3129 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3130 "friend declaration %qD is not visible to "
3131 "explicit specialization", tmpl
))
3132 inform (DECL_SOURCE_LOCATION (tmpl
),
3133 "friend declaration here");
3135 else if (!ctype
&& !is_friend
3136 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3137 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3139 tree gen_tmpl
= most_general_template (tmpl
);
3141 if (explicit_instantiation
)
3143 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3144 is done by do_decl_instantiation later. */
3146 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3147 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3149 if (arg_depth
> parm_depth
)
3151 /* If TMPL is not the most general template (for
3152 example, if TMPL is a friend template that is
3153 injected into namespace scope), then there will
3154 be too many levels of TARGS. Remove some of them
3159 new_targs
= make_tree_vec (parm_depth
);
3160 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3161 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3162 = TREE_VEC_ELT (targs
, i
);
3166 return instantiate_template (tmpl
, targs
, tf_error
);
3169 /* If we thought that the DECL was a member function, but it
3170 turns out to be specializing a static member function,
3171 make DECL a static member function as well. */
3172 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3173 && DECL_STATIC_FUNCTION_P (tmpl
)
3174 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3175 revert_static_member_fn (decl
);
3177 /* If this is a specialization of a member template of a
3178 template class, we want to return the TEMPLATE_DECL, not
3179 the specialization of it. */
3180 if (tsk
== tsk_template
&& !was_template_id
)
3182 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3183 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3184 DECL_INITIAL (result
) = NULL_TREE
;
3188 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3189 DECL_SOURCE_LOCATION (result
)
3190 = DECL_SOURCE_LOCATION (decl
);
3191 /* We want to use the argument list specified in the
3192 definition, not in the original declaration. */
3193 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3194 for (parm
= DECL_ARGUMENTS (result
); parm
;
3195 parm
= DECL_CHAIN (parm
))
3196 DECL_CONTEXT (parm
) = result
;
3198 return register_specialization (tmpl
, gen_tmpl
, targs
,
3202 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3203 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3205 if (was_template_id
)
3206 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3208 /* Inherit default function arguments from the template
3209 DECL is specializing. */
3210 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3211 copy_default_args_to_explicit_spec (decl
);
3213 /* This specialization has the same protection as the
3214 template it specializes. */
3215 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3216 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3218 /* 7.1.1-1 [dcl.stc]
3220 A storage-class-specifier shall not be specified in an
3221 explicit specialization...
3223 The parser rejects these, so unless action is taken here,
3224 explicit function specializations will always appear with
3227 The action recommended by the C++ CWG in response to C++
3228 defect report 605 is to make the storage class and linkage
3229 of the explicit specialization match the templated function:
3231 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3233 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3235 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3236 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3238 /* A concept cannot be specialized. */
3239 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3241 error ("explicit specialization of function concept %qD",
3243 return error_mark_node
;
3246 /* This specialization has the same linkage and visibility as
3247 the function template it specializes. */
3248 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3249 if (! TREE_PUBLIC (decl
))
3251 DECL_INTERFACE_KNOWN (decl
) = 1;
3252 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3254 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3255 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3257 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3258 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3262 /* If DECL is a friend declaration, declared using an
3263 unqualified name, the namespace associated with DECL may
3264 have been set incorrectly. For example, in:
3266 template <typename T> void f(T);
3268 struct S { friend void f<int>(int); }
3271 we will have set the DECL_CONTEXT for the friend
3272 declaration to N, rather than to the global namespace. */
3273 if (DECL_NAMESPACE_SCOPE_P (decl
))
3274 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3276 if (is_friend
&& !have_def
)
3277 /* This is not really a declaration of a specialization.
3278 It's just the name of an instantiation. But, it's not
3279 a request for an instantiation, either. */
3280 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3281 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3282 /* A specialization is not necessarily COMDAT. */
3283 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3284 && DECL_DECLARED_INLINE_P (decl
));
3285 else if (VAR_P (decl
))
3286 DECL_COMDAT (decl
) = false;
3288 /* If this is a full specialization, register it so that we can find
3289 it again. Partial specializations will be registered in
3290 process_partial_specialization. */
3291 if (!processing_template_decl
)
3293 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3295 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3300 /* A 'structor should already have clones. */
3301 gcc_assert (decl
== error_mark_node
3302 || variable_template_p (tmpl
)
3303 || !(DECL_CONSTRUCTOR_P (decl
)
3304 || DECL_DESTRUCTOR_P (decl
))
3305 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3312 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3313 parameters. These are represented in the same format used for
3314 DECL_TEMPLATE_PARMS. */
3317 comp_template_parms (const_tree parms1
, const_tree parms2
)
3322 if (parms1
== parms2
)
3325 for (p1
= parms1
, p2
= parms2
;
3326 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3327 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3329 tree t1
= TREE_VALUE (p1
);
3330 tree t2
= TREE_VALUE (p2
);
3333 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3334 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3336 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3339 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3341 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3342 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3344 /* If either of the template parameters are invalid, assume
3345 they match for the sake of error recovery. */
3346 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3349 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3352 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3353 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3354 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3356 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3361 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3362 /* One set of parameters has more parameters lists than the
3369 /* Returns true if two template parameters are declared with
3370 equivalent constraints. */
3373 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3375 tree req1
= TREE_TYPE (parm1
);
3376 tree req2
= TREE_TYPE (parm2
);
3380 return cp_tree_equal (req1
, req2
);
3384 /* Returns true when two template parameters are equivalent. */
3387 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3389 tree decl1
= TREE_VALUE (parm1
);
3390 tree decl2
= TREE_VALUE (parm2
);
3392 /* If either of the template parameters are invalid, assume
3393 they match for the sake of error recovery. */
3394 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3397 /* ... they declare parameters of the same kind. */
3398 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3401 /* ... one parameter was introduced by a parameter declaration, then
3402 both are. This case arises as a result of eagerly rewriting declarations
3404 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3407 /* ... if either declares a pack, they both do. */
3408 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3411 if (TREE_CODE (decl1
) == PARM_DECL
)
3413 /* ... if they declare non-type parameters, the types are equivalent. */
3414 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3417 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3419 /* ... if they declare template template parameters, their template
3420 parameter lists are equivalent. */
3421 if (!template_heads_equivalent_p (decl1
, decl2
))
3425 /* ... if they are declared with a qualified-concept name, they both
3426 are, and those names are equivalent. */
3427 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3430 /* Returns true if two template parameters lists are equivalent.
3431 Two template parameter lists are equivalent if they have the
3432 same length and their corresponding parameters are equivalent.
3434 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3435 data structure returned by DECL_TEMPLATE_PARMS.
3437 This is generally the same implementation as comp_template_parms
3438 except that it also the concept names and arguments used to
3439 introduce parameters. */
3442 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3444 if (parms1
== parms2
)
3447 const_tree p1
= parms1
;
3448 const_tree p2
= parms2
;
3449 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3451 tree list1
= TREE_VALUE (p1
);
3452 tree list2
= TREE_VALUE (p2
);
3454 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3457 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3459 tree parm1
= TREE_VEC_ELT (list1
, i
);
3460 tree parm2
= TREE_VEC_ELT (list2
, i
);
3461 if (!template_parameters_equivalent_p (parm1
, parm2
))
3465 p1
= TREE_CHAIN (p1
);
3466 p2
= TREE_CHAIN (p2
);
3469 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3475 /* Return true if the requires-clause of the template parameter lists are
3476 equivalent and false otherwise. */
3478 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3480 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3481 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3482 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3484 if (!cp_tree_equal (req1
, req2
))
3489 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3490 Two template heads are equivalent if their template parameter
3491 lists are equivalent and their requires clauses are equivalent.
3493 In pre-C++20, this is equivalent to calling comp_template_parms
3494 for the template parameters of TMPL1 and TMPL2. */
3497 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3499 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3500 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3502 /* Don't change the matching rules for pre-C++20. */
3503 if (cxx_dialect
< cxx20
)
3504 return comp_template_parms (parms1
, parms2
);
3506 /* ... have the same number of template parameters, and their
3507 corresponding parameters are equivalent. */
3508 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3511 /* ... if either has a requires-clause, they both do and their
3512 corresponding constraint-expressions are equivalent. */
3513 return template_requirements_equivalent_p (parms1
, parms2
);
3516 /* Determine whether PARM is a parameter pack. */
3519 template_parameter_pack_p (const_tree parm
)
3521 /* Determine if we have a non-type template parameter pack. */
3522 if (TREE_CODE (parm
) == PARM_DECL
)
3523 return (DECL_TEMPLATE_PARM_P (parm
)
3524 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3525 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3526 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3528 /* If this is a list of template parameters, we could get a
3529 TYPE_DECL or a TEMPLATE_DECL. */
3530 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3531 parm
= TREE_TYPE (parm
);
3533 /* Otherwise it must be a type template parameter. */
3534 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3535 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3536 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3539 /* Determine if T is a function parameter pack. */
3542 function_parameter_pack_p (const_tree t
)
3544 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3545 return DECL_PACK_P (t
);
3549 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3550 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3553 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3555 if (! primary_func_tmpl_inst
3556 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3557 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3560 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3563 /* Return true iff the function parameter PARAM_DECL was expanded
3564 from the function parameter pack PACK. */
3567 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3569 if (DECL_ARTIFICIAL (param_decl
)
3570 || !function_parameter_pack_p (pack
))
3573 /* The parameter pack and its pack arguments have the same
3575 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3578 /* Determine whether ARGS describes a variadic template args list,
3579 i.e., one that is terminated by a template argument pack. */
3582 template_args_variadic_p (tree args
)
3587 if (args
== NULL_TREE
)
3590 args
= INNERMOST_TEMPLATE_ARGS (args
);
3591 nargs
= TREE_VEC_LENGTH (args
);
3596 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3598 return ARGUMENT_PACK_P (last_parm
);
3601 /* Generate a new name for the parameter pack name NAME (an
3602 IDENTIFIER_NODE) that incorporates its */
3605 make_ith_pack_parameter_name (tree name
, int i
)
3607 /* Munge the name to include the parameter index. */
3608 #define NUMBUF_LEN 128
3609 char numbuf
[NUMBUF_LEN
];
3613 if (name
== NULL_TREE
)
3615 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3616 newname_len
= IDENTIFIER_LENGTH (name
)
3617 + strlen (numbuf
) + 2;
3618 newname
= (char*)alloca (newname_len
);
3619 snprintf (newname
, newname_len
,
3620 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3621 return get_identifier (newname
);
3624 /* Return true if T is a primary function, class or alias template
3625 specialization, not including the template pattern. */
3628 primary_template_specialization_p (const_tree t
)
3633 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3634 return (DECL_LANG_SPECIFIC (t
)
3635 && DECL_USE_TEMPLATE (t
)
3636 && DECL_TEMPLATE_INFO (t
)
3637 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3638 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3639 return (CLASSTYPE_TEMPLATE_INFO (t
)
3640 && CLASSTYPE_USE_TEMPLATE (t
)
3641 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3642 else if (alias_template_specialization_p (t
, nt_transparent
))
3647 /* Return true if PARM is a template template parameter. */
3650 template_template_parameter_p (const_tree parm
)
3652 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3655 /* Return true iff PARM is a DECL representing a type template
3659 template_type_parameter_p (const_tree parm
)
3662 && (TREE_CODE (parm
) == TYPE_DECL
3663 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3664 && DECL_TEMPLATE_PARM_P (parm
));
3667 /* Return the template parameters of T if T is a
3668 primary template instantiation, NULL otherwise. */
3671 get_primary_template_innermost_parameters (const_tree t
)
3673 tree parms
= NULL
, template_info
= NULL
;
3675 if ((template_info
= get_template_info (t
))
3676 && primary_template_specialization_p (t
))
3677 parms
= INNERMOST_TEMPLATE_PARMS
3678 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3683 /* Return the template parameters of the LEVELth level from the full list
3684 of template parameters PARMS. */
3687 get_template_parms_at_level (tree parms
, int level
)
3691 || TREE_CODE (parms
) != TREE_LIST
3692 || level
> TMPL_PARMS_DEPTH (parms
))
3695 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3696 if (TMPL_PARMS_DEPTH (p
) == level
)
3702 /* Returns the template arguments of T if T is a template instantiation,
3706 get_template_innermost_arguments (const_tree t
)
3708 tree args
= NULL
, template_info
= NULL
;
3710 if ((template_info
= get_template_info (t
))
3711 && TI_ARGS (template_info
))
3712 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3717 /* Return the argument pack elements of T if T is a template argument pack,
3721 get_template_argument_pack_elems (const_tree t
)
3723 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3724 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3727 return ARGUMENT_PACK_ARGS (t
);
3730 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3731 ARGUMENT_PACK_SELECT represents. */
3734 argument_pack_select_arg (tree t
)
3736 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3737 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3739 /* If the selected argument is an expansion E, that most likely means we were
3740 called from gen_elem_of_pack_expansion_instantiation during the
3741 substituting of an argument pack (of which the Ith element is a pack
3742 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3743 In this case, the Ith element resulting from this substituting is going to
3744 be a pack expansion, which pattern is the pattern of E. Let's return the
3745 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3746 resulting pack expansion from it. */
3747 if (PACK_EXPANSION_P (arg
))
3749 /* Make sure we aren't throwing away arg info. */
3750 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3751 arg
= PACK_EXPANSION_PATTERN (arg
);
3758 /* True iff FN is a function representing a built-in variadic parameter
3762 builtin_pack_fn_p (tree fn
)
3765 || TREE_CODE (fn
) != FUNCTION_DECL
3766 || !DECL_IS_BUILTIN (fn
))
3769 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3775 /* True iff CALL is a call to a function representing a built-in variadic
3779 builtin_pack_call_p (tree call
)
3781 if (TREE_CODE (call
) != CALL_EXPR
)
3783 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3786 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3789 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3792 tree ohi
= CALL_EXPR_ARG (call
, 0);
3793 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3794 false/*fn*/, true/*int_cst*/);
3796 if (value_dependent_expression_p (hi
))
3800 call
= copy_node (call
);
3801 CALL_EXPR_ARG (call
, 0) = hi
;
3803 tree ex
= make_pack_expansion (call
, complain
);
3804 tree vec
= make_tree_vec (1);
3805 TREE_VEC_ELT (vec
, 0) = ex
;
3810 hi
= cxx_constant_value (hi
);
3811 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3813 /* Calculate the largest value of len that won't make the size of the vec
3814 overflow an int. The compiler will exceed resource limits long before
3815 this, but it seems a decent place to diagnose. */
3816 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3818 if (len
< 0 || len
> max
)
3820 if ((complain
& tf_error
)
3821 && hi
!= error_mark_node
)
3822 error ("argument to %<__integer_pack%> must be between 0 and %d",
3824 return error_mark_node
;
3827 tree vec
= make_tree_vec (len
);
3829 for (int i
= 0; i
< len
; ++i
)
3830 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3836 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3840 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3843 if (!builtin_pack_call_p (call
))
3846 tree fn
= CALL_EXPR_FN (call
);
3848 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3849 return expand_integer_pack (call
, args
, complain
, in_decl
);
3854 /* Structure used to track the progress of find_parameter_packs_r. */
3855 struct find_parameter_pack_data
3857 /* TREE_LIST that will contain all of the parameter packs found by
3859 tree
* parameter_packs
;
3861 /* Set of AST nodes that have been visited by the traversal. */
3862 hash_set
<tree
> *visited
;
3864 /* True iff we're making a type pack expansion. */
3865 bool type_pack_expansion_p
;
3868 /* Identifies all of the argument packs that occur in a template
3869 argument and appends them to the TREE_LIST inside DATA, which is a
3870 find_parameter_pack_data structure. This is a subroutine of
3871 make_pack_expansion and uses_parameter_packs. */
3873 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3876 struct find_parameter_pack_data
* ppd
=
3877 (struct find_parameter_pack_data
*)data
;
3878 bool parameter_pack_p
= false;
3880 /* Don't look through typedefs; we are interested in whether a
3881 parameter pack is actually written in the expression/type we're
3882 looking at, not the target type. */
3883 if (TYPE_P (t
) && typedef_variant_p (t
))
3885 /* But do look at arguments for an alias template. */
3886 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3887 cp_walk_tree (&TI_ARGS (tinfo
),
3888 &find_parameter_packs_r
,
3894 /* Identify whether this is a parameter pack or not. */
3895 switch (TREE_CODE (t
))
3897 case TEMPLATE_PARM_INDEX
:
3898 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3899 parameter_pack_p
= true;
3902 case TEMPLATE_TYPE_PARM
:
3903 t
= TYPE_MAIN_VARIANT (t
);
3905 case TEMPLATE_TEMPLATE_PARM
:
3906 /* If the placeholder appears in the decl-specifier-seq of a function
3907 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3908 is a pack expansion, the invented template parameter is a template
3910 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3911 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3912 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3913 parameter_pack_p
= true;
3918 if (DECL_PACK_P (t
))
3920 /* We don't want to walk into the type of a PARM_DECL,
3921 because we don't want to see the type parameter pack. */
3923 parameter_pack_p
= true;
3928 if (DECL_PACK_P (t
))
3930 /* We don't want to walk into the type of a variadic capture proxy,
3931 because we don't want to see the type parameter pack. */
3933 parameter_pack_p
= true;
3935 else if (variable_template_specialization_p (t
))
3937 cp_walk_tree (&DECL_TI_ARGS (t
),
3938 find_parameter_packs_r
,
3945 if (builtin_pack_call_p (t
))
3946 parameter_pack_p
= true;
3950 parameter_pack_p
= true;
3953 /* Not a parameter pack. */
3957 if (parameter_pack_p
)
3959 /* Add this parameter pack to the list. */
3960 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3964 cp_walk_tree (&TYPE_CONTEXT (t
),
3965 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3967 /* This switch statement will return immediately if we don't find a
3968 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3969 switch (TREE_CODE (t
))
3971 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3972 /* Check the template itself. */
3973 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3974 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3979 tree decl
= DECL_EXPR_DECL (t
);
3980 /* Ignore the declaration of a capture proxy for a parameter pack. */
3981 if (is_capture_proxy (decl
))
3983 if (is_typedef_decl (decl
))
3984 /* Since we stop at typedefs above, we need to look through them at
3985 the point of the DECL_EXPR. */
3986 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3987 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3992 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3994 cp_walk_tree (&TREE_TYPE (t
),
3995 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3998 case TYPE_PACK_EXPANSION
:
3999 case EXPR_PACK_EXPANSION
:
4004 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4009 case IDENTIFIER_NODE
:
4010 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4017 /* Since we defer implicit capture, look in the parms and body. */
4018 tree fn
= lambda_function (t
);
4019 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4021 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4028 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4029 type_pack_expansion_p to false so that any placeholders
4030 within the expression don't get marked as parameter packs. */
4031 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4032 ppd
->type_pack_expansion_p
= false;
4033 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4035 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4041 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4043 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4045 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4047 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4058 /* Determines if the expression or type T uses any parameter packs. */
4060 uses_parameter_packs (tree t
)
4062 tree parameter_packs
= NULL_TREE
;
4063 struct find_parameter_pack_data ppd
;
4064 ppd
.parameter_packs
= ¶meter_packs
;
4065 ppd
.visited
= new hash_set
<tree
>;
4066 ppd
.type_pack_expansion_p
= false;
4067 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4069 return parameter_packs
;
4072 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4073 representation a base-class initializer into a parameter pack
4074 expansion. If all goes well, the resulting node will be an
4075 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4078 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4081 tree parameter_packs
= NULL_TREE
;
4082 bool for_types
= false;
4083 struct find_parameter_pack_data ppd
;
4085 if (!arg
|| arg
== error_mark_node
)
4088 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4090 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4091 class initializer. In this case, the TREE_PURPOSE will be a
4092 _TYPE node (representing the base class expansion we're
4093 initializing) and the TREE_VALUE will be a TREE_LIST
4094 containing the initialization arguments.
4096 The resulting expansion looks somewhat different from most
4097 expansions. Rather than returning just one _EXPANSION, we
4098 return a TREE_LIST whose TREE_PURPOSE is a
4099 TYPE_PACK_EXPANSION containing the bases that will be
4100 initialized. The TREE_VALUE will be identical to the
4101 original TREE_VALUE, which is a list of arguments that will
4102 be passed to each base. We do not introduce any new pack
4103 expansion nodes into the TREE_VALUE (although it is possible
4104 that some already exist), because the TREE_PURPOSE and
4105 TREE_VALUE all need to be expanded together with the same
4106 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4107 resulting TREE_PURPOSE will mention the parameter packs in
4108 both the bases and the arguments to the bases. */
4111 tree parameter_packs
= NULL_TREE
;
4113 /* Determine which parameter packs will be used by the base
4115 ppd
.visited
= new hash_set
<tree
>;
4116 ppd
.parameter_packs
= ¶meter_packs
;
4117 ppd
.type_pack_expansion_p
= false;
4118 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4119 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4122 if (parameter_packs
== NULL_TREE
)
4124 if (complain
& tf_error
)
4125 error ("base initializer expansion %qT contains no parameter packs",
4128 return error_mark_node
;
4131 if (TREE_VALUE (arg
) != void_type_node
)
4133 /* Collect the sets of parameter packs used in each of the
4134 initialization arguments. */
4135 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4137 /* Determine which parameter packs will be expanded in this
4139 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4146 /* Create the pack expansion type for the base type. */
4147 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4148 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4149 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4150 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4153 they will rarely be compared to anything. */
4154 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4156 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4159 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4162 /* Build the PACK_EXPANSION_* node. */
4164 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4165 : make_node (EXPR_PACK_EXPANSION
);
4166 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4167 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4169 /* Propagate type and const-expression information. */
4170 TREE_TYPE (result
) = TREE_TYPE (arg
);
4171 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4172 /* Mark this read now, since the expansion might be length 0. */
4173 mark_exp_read (arg
);
4176 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4177 they will rarely be compared to anything. */
4178 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4180 /* Determine which parameter packs will be expanded. */
4181 ppd
.parameter_packs
= ¶meter_packs
;
4182 ppd
.visited
= new hash_set
<tree
>;
4183 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4184 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4187 /* Make sure we found some parameter packs. */
4188 if (parameter_packs
== NULL_TREE
)
4190 if (complain
& tf_error
)
4193 error ("expansion pattern %qT contains no parameter packs", arg
);
4195 error ("expansion pattern %qE contains no parameter packs", arg
);
4197 return error_mark_node
;
4199 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4201 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4206 /* Checks T for any "bare" parameter packs, which have not yet been
4207 expanded, and issues an error if any are found. This operation can
4208 only be done on full expressions or types (e.g., an expression
4209 statement, "if" condition, etc.), because we could have expressions like:
4211 foo(f(g(h(args)))...)
4213 where "args" is a parameter pack. check_for_bare_parameter_packs
4214 should not be called for the subexpressions args, h(args),
4215 g(h(args)), or f(g(h(args))), because we would produce erroneous
4218 Returns TRUE and emits an error if there were bare parameter packs,
4219 returns FALSE otherwise. */
4221 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4223 tree parameter_packs
= NULL_TREE
;
4224 struct find_parameter_pack_data ppd
;
4226 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4229 /* A lambda might use a parameter pack from the containing context. */
4230 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4231 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4234 if (TREE_CODE (t
) == TYPE_DECL
)
4237 ppd
.parameter_packs
= ¶meter_packs
;
4238 ppd
.visited
= new hash_set
<tree
>;
4239 ppd
.type_pack_expansion_p
= false;
4240 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4243 if (parameter_packs
)
4245 if (loc
== UNKNOWN_LOCATION
)
4246 loc
= cp_expr_loc_or_input_loc (t
);
4247 error_at (loc
, "parameter packs not expanded with %<...%>:");
4248 while (parameter_packs
)
4250 tree pack
= TREE_VALUE (parameter_packs
);
4251 tree name
= NULL_TREE
;
4253 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4254 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4255 name
= TYPE_NAME (pack
);
4256 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4257 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4258 else if (TREE_CODE (pack
) == CALL_EXPR
)
4259 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4261 name
= DECL_NAME (pack
);
4264 inform (loc
, " %qD", name
);
4266 inform (loc
, " %s", "<anonymous>");
4268 parameter_packs
= TREE_CHAIN (parameter_packs
);
4277 /* Expand any parameter packs that occur in the template arguments in
4280 expand_template_argument_pack (tree args
)
4282 if (args
== error_mark_node
)
4283 return error_mark_node
;
4285 tree result_args
= NULL_TREE
;
4286 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4287 int num_result_args
= -1;
4288 int non_default_args_count
= -1;
4290 /* First, determine if we need to expand anything, and the number of
4291 slots we'll need. */
4292 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4294 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4295 if (arg
== NULL_TREE
)
4297 if (ARGUMENT_PACK_P (arg
))
4299 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4300 if (num_result_args
< 0)
4301 num_result_args
= in_arg
+ num_packed
;
4303 num_result_args
+= num_packed
;
4307 if (num_result_args
>= 0)
4312 /* If no expansion is necessary, we're done. */
4313 if (num_result_args
< 0)
4316 /* Expand arguments. */
4317 result_args
= make_tree_vec (num_result_args
);
4318 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4319 non_default_args_count
=
4320 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4321 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4323 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4324 if (ARGUMENT_PACK_P (arg
))
4326 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4327 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4328 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4329 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4330 if (non_default_args_count
> 0)
4331 non_default_args_count
+= num_packed
- 1;
4335 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4339 if (non_default_args_count
>= 0)
4340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4344 /* Checks if DECL shadows a template parameter.
4346 [temp.local]: A template-parameter shall not be redeclared within its
4347 scope (including nested scopes).
4349 Emits an error and returns TRUE if the DECL shadows a parameter,
4350 returns FALSE otherwise. */
4353 check_template_shadow (tree decl
)
4357 /* If we're not in a template, we can't possibly shadow a template
4359 if (!current_template_parms
)
4362 /* Figure out what we're shadowing. */
4363 decl
= OVL_FIRST (decl
);
4364 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4366 /* If there's no previous binding for this name, we're not shadowing
4367 anything, let alone a template parameter. */
4371 /* If we're not shadowing a template parameter, we're done. Note
4372 that OLDDECL might be an OVERLOAD (or perhaps even an
4373 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4375 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4378 /* We check for decl != olddecl to avoid bogus errors for using a
4379 name inside a class. We check TPFI to avoid duplicate errors for
4380 inline member templates. */
4382 || (DECL_TEMPLATE_PARM_P (decl
)
4383 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4386 /* Don't complain about the injected class name, as we've already
4387 complained about the class itself. */
4388 if (DECL_SELF_REFERENCE_P (decl
))
4391 if (DECL_TEMPLATE_PARM_P (decl
))
4392 error ("declaration of template parameter %q+D shadows "
4393 "template parameter", decl
);
4395 error ("declaration of %q+#D shadows template parameter", decl
);
4396 inform (DECL_SOURCE_LOCATION (olddecl
),
4397 "template parameter %qD declared here", olddecl
);
4401 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4402 ORIG_LEVEL, DECL, and TYPE. */
4405 build_template_parm_index (int index
,
4411 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4412 TEMPLATE_PARM_IDX (t
) = index
;
4413 TEMPLATE_PARM_LEVEL (t
) = level
;
4414 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4415 TEMPLATE_PARM_DECL (t
) = decl
;
4416 TREE_TYPE (t
) = type
;
4417 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4418 TREE_READONLY (t
) = TREE_READONLY (decl
);
4423 /* Find the canonical type parameter for the given template type
4424 parameter. Returns the canonical type parameter, which may be TYPE
4425 if no such parameter existed. */
4428 canonical_type_parameter (tree type
)
4430 int idx
= TEMPLATE_TYPE_IDX (type
);
4432 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4434 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4435 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4437 for (tree list
= (*canonical_template_parms
)[idx
];
4438 list
; list
= TREE_CHAIN (list
))
4439 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4440 return TREE_VALUE (list
);
4442 (*canonical_template_parms
)[idx
]
4443 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4447 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4448 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4449 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4450 new one is created. */
4453 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4454 tsubst_flags_t complain
)
4456 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4457 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4458 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4459 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4461 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4463 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4464 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4466 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4467 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4468 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4469 DECL_ARTIFICIAL (decl
) = 1;
4470 SET_DECL_TEMPLATE_PARM_P (decl
);
4472 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4473 TEMPLATE_PARM_LEVEL (index
) - levels
,
4474 TEMPLATE_PARM_ORIG_LEVEL (index
),
4476 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4477 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4478 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4480 /* Template template parameters need this. */
4482 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4484 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4485 TYPE_DECL
, DECL_NAME (decl
), type
);
4486 DECL_TEMPLATE_RESULT (decl
) = inner
;
4487 DECL_ARTIFICIAL (inner
) = true;
4488 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4489 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4492 /* Attach the TPI to the decl. */
4493 if (TREE_CODE (inner
) == TYPE_DECL
)
4494 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4496 DECL_INITIAL (decl
) = tpi
;
4499 return TEMPLATE_PARM_DESCENDANTS (index
);
4502 /* Process information from new template parameter PARM and append it
4503 to the LIST being built. This new parameter is a non-type
4504 parameter iff IS_NON_TYPE is true. This new parameter is a
4505 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4509 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4510 bool is_non_type
, bool is_parameter_pack
)
4512 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4513 tree prev
= NULL_TREE
;
4518 prev
= tree_last (list
);
4520 tree p
= TREE_VALUE (prev
);
4521 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4522 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4523 else if (TREE_CODE (p
) == PARM_DECL
)
4524 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4529 tree decl
= NULL_TREE
;
4530 tree defval
= TREE_PURPOSE (parm
);
4531 tree constr
= TREE_TYPE (parm
);
4535 parm
= TREE_VALUE (parm
);
4537 SET_DECL_TEMPLATE_PARM_P (parm
);
4539 if (TREE_TYPE (parm
) != error_mark_node
)
4543 The top-level cv-qualifiers on the template-parameter are
4544 ignored when determining its type. */
4545 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4546 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4547 TREE_TYPE (parm
) = error_mark_node
;
4548 else if (uses_parameter_packs (TREE_TYPE (parm
))
4549 && !is_parameter_pack
4550 /* If we're in a nested template parameter list, the template
4551 template parameter could be a parameter pack. */
4552 && processing_template_parmlist
== 1)
4554 /* This template parameter is not a parameter pack, but it
4555 should be. Complain about "bare" parameter packs. */
4556 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4558 /* Recover by calling this a parameter pack. */
4559 is_parameter_pack
= true;
4563 /* A template parameter is not modifiable. */
4564 TREE_CONSTANT (parm
) = 1;
4565 TREE_READONLY (parm
) = 1;
4566 decl
= build_decl (parm_loc
,
4567 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4568 TREE_CONSTANT (decl
) = 1;
4569 TREE_READONLY (decl
) = 1;
4570 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4571 = build_template_parm_index (idx
, processing_template_decl
,
4572 processing_template_decl
,
4573 decl
, TREE_TYPE (parm
));
4575 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4576 = is_parameter_pack
;
4581 parm
= TREE_VALUE (TREE_VALUE (parm
));
4583 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4585 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4586 /* This is for distinguishing between real templates and template
4587 template parameters */
4588 TREE_TYPE (parm
) = t
;
4590 /* any_template_parm_r expects to be able to get the targs of a
4591 DECL_TEMPLATE_RESULT. */
4592 tree result
= DECL_TEMPLATE_RESULT (parm
);
4593 TREE_TYPE (result
) = t
;
4594 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4595 tree tinfo
= build_template_info (parm
, args
);
4596 retrofit_lang_decl (result
);
4597 DECL_TEMPLATE_INFO (result
) = tinfo
;
4603 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4604 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4605 decl
= build_decl (parm_loc
,
4606 TYPE_DECL
, parm
, t
);
4609 TYPE_NAME (t
) = decl
;
4610 TYPE_STUB_DECL (t
) = decl
;
4612 TEMPLATE_TYPE_PARM_INDEX (t
)
4613 = build_template_parm_index (idx
, processing_template_decl
,
4614 processing_template_decl
,
4615 decl
, TREE_TYPE (parm
));
4616 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4617 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4618 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4620 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4622 DECL_ARTIFICIAL (decl
) = 1;
4623 SET_DECL_TEMPLATE_PARM_P (decl
);
4625 /* Build requirements for the type/template parameter.
4626 This must be done after SET_DECL_TEMPLATE_PARM_P or
4627 process_template_parm could fail. */
4628 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4630 decl
= pushdecl (decl
);
4634 /* Build the parameter node linking the parameter declaration,
4635 its default argument (if any), and its constraints (if any). */
4636 parm
= build_tree_list (defval
, parm
);
4637 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4640 TREE_CHAIN (prev
) = parm
;
4647 /* The end of a template parameter list has been reached. Process the
4648 tree list into a parameter vector, converting each parameter into a more
4649 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4653 end_template_parm_list (tree parms
)
4655 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4657 /* Pop the dummy parameter level and add the real one. We do not
4658 morph the dummy parameter in place, as it might have been
4659 captured by a (nested) template-template-parm. */
4660 current_template_parms
= TREE_CHAIN (current_template_parms
);
4662 current_template_parms
4663 = tree_cons (size_int (processing_template_decl
),
4664 saved_parmlist
, current_template_parms
);
4666 for (unsigned ix
= 0; parms
; ix
++)
4669 parms
= TREE_CHAIN (parms
);
4670 TREE_CHAIN (parm
) = NULL_TREE
;
4672 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4675 --processing_template_parmlist
;
4677 return saved_parmlist
;
4680 // Explicitly indicate the end of the template parameter list. We assume
4681 // that the current template parameters have been constructed and/or
4682 // managed explicitly, as when creating new template template parameters
4683 // from a shorthand constraint.
4685 end_template_parm_list ()
4687 --processing_template_parmlist
;
4690 /* end_template_decl is called after a template declaration is seen. */
4693 end_template_decl (void)
4695 reset_specialization ();
4697 if (! processing_template_decl
)
4700 /* This matches the pushlevel in begin_template_parm_list. */
4703 --processing_template_decl
;
4704 current_template_parms
= TREE_CHAIN (current_template_parms
);
4707 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4708 thereof, and converts it into an argument suitable to be passed to
4709 the type substitution functions. Note that if the TREE_LIST contains
4710 an error_mark node, the returned argument is error_mark_node. */
4713 template_parm_to_arg (tree t
)
4718 if (TREE_CODE (t
) == TREE_LIST
)
4721 if (error_operand_p (t
))
4722 return error_mark_node
;
4724 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4726 if (TREE_CODE (t
) == TYPE_DECL
4727 || TREE_CODE (t
) == TEMPLATE_DECL
)
4730 t
= DECL_INITIAL (t
);
4733 gcc_assert (TEMPLATE_PARM_P (t
));
4735 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4736 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4738 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4740 /* Turn this argument into a TYPE_ARGUMENT_PACK
4741 with a single element, which expands T. */
4742 tree vec
= make_tree_vec (1);
4744 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4746 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4748 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4749 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4754 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4756 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4757 with a single element, which expands T. */
4758 tree vec
= make_tree_vec (1);
4760 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4762 t
= convert_from_reference (t
);
4763 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4765 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4766 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4769 t
= convert_from_reference (t
);
4774 /* Given a single level of template parameters (a TREE_VEC), return it
4775 as a set of template arguments. */
4778 template_parms_level_to_args (tree parms
)
4780 tree a
= copy_node (parms
);
4781 TREE_TYPE (a
) = NULL_TREE
;
4782 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4783 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4786 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4791 /* Given a set of template parameters, return them as a set of template
4792 arguments. The template parameters are represented as a TREE_VEC, in
4793 the form documented in cp-tree.h for template arguments. */
4796 template_parms_to_args (tree parms
)
4799 tree args
= NULL_TREE
;
4800 int length
= TMPL_PARMS_DEPTH (parms
);
4803 /* If there is only one level of template parameters, we do not
4804 create a TREE_VEC of TREE_VECs. Instead, we return a single
4805 TREE_VEC containing the arguments. */
4807 args
= make_tree_vec (length
);
4809 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4811 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4814 TREE_VEC_ELT (args
, --l
) = a
;
4822 /* Within the declaration of a template, return the currently active
4823 template parameters as an argument TREE_VEC. */
4826 current_template_args (void)
4828 return template_parms_to_args (current_template_parms
);
4831 /* Return the fully generic arguments for of TMPL, i.e. what
4832 current_template_args would be while parsing it. */
4835 generic_targs_for (tree tmpl
)
4837 if (tmpl
== NULL_TREE
)
4839 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4840 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4841 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4842 template parameter, it has no TEMPLATE_INFO; for a partial
4843 specialization, it has the arguments for the primary template, and we
4844 want the arguments for the partial specialization. */;
4845 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4846 if (tree ti
= get_template_info (result
))
4847 return TI_ARGS (ti
);
4848 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4851 /* Update the declared TYPE by doing any lookups which were thought to be
4852 dependent, but are not now that we know the SCOPE of the declarator. */
4855 maybe_update_decl_type (tree orig_type
, tree scope
)
4857 tree type
= orig_type
;
4859 if (type
== NULL_TREE
)
4862 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4863 type
= TREE_TYPE (type
);
4865 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4866 && dependent_type_p (type
)
4867 /* Don't bother building up the args in this case. */
4868 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4870 /* tsubst in the args corresponding to the template parameters,
4871 including auto if present. Most things will be unchanged, but
4872 make_typename_type and tsubst_qualified_id will resolve
4873 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4874 tree args
= current_template_args ();
4875 tree auto_node
= type_uses_auto (type
);
4879 tree auto_vec
= make_tree_vec (1);
4880 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4881 args
= add_to_template_args (args
, auto_vec
);
4883 pushed
= push_scope (scope
);
4884 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4889 if (type
== error_mark_node
)
4892 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4894 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4897 type
= TYPE_NAME (type
);
4902 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4903 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4904 the new template is a member template. */
4907 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4909 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4910 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4911 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4912 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4913 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4914 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4915 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4916 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4921 struct template_parm_data
4923 /* The level of the template parameters we are currently
4927 /* The index of the specialization argument we are currently
4931 /* An array whose size is the number of template parameters. The
4932 elements are nonzero if the parameter has been used in any one
4933 of the arguments processed so far. */
4936 /* An array whose size is the number of template arguments. The
4937 elements are nonzero if the argument makes use of template
4938 parameters of this level. */
4939 int* arg_uses_template_parms
;
4942 /* Subroutine of push_template_decl used to see if each template
4943 parameter in a partial specialization is used in the explicit
4944 argument list. If T is of the LEVEL given in DATA (which is
4945 treated as a template_parm_data*), then DATA->PARMS is marked
4949 mark_template_parm (tree t
, void* data
)
4953 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4955 template_parm_level_and_index (t
, &level
, &idx
);
4957 if (level
== tpd
->level
)
4959 tpd
->parms
[idx
] = 1;
4960 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4963 /* In C++17 the type of a non-type argument is a deduced context. */
4964 if (cxx_dialect
>= cxx17
4965 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4966 for_each_template_parm (TREE_TYPE (t
),
4967 &mark_template_parm
,
4970 /*include_nondeduced_p=*/false);
4972 /* Return zero so that for_each_template_parm will continue the
4973 traversal of the tree; we want to mark *every* template parm. */
4977 /* Process the partial specialization DECL. */
4980 process_partial_specialization (tree decl
)
4982 tree type
= TREE_TYPE (decl
);
4983 tree tinfo
= get_template_info (decl
);
4984 tree maintmpl
= TI_TEMPLATE (tinfo
);
4985 tree specargs
= TI_ARGS (tinfo
);
4986 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4987 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4990 int nargs
= TREE_VEC_LENGTH (inner_args
);
4993 bool did_error_intro
= false;
4994 struct template_parm_data tpd
;
4995 struct template_parm_data tpd2
;
4997 gcc_assert (current_template_parms
);
4999 /* A concept cannot be specialized. */
5000 if (flag_concepts
&& variable_concept_p (maintmpl
))
5002 error ("specialization of variable concept %q#D", maintmpl
);
5003 return error_mark_node
;
5006 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5007 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5009 /* We check that each of the template parameters given in the
5010 partial specialization is used in the argument list to the
5011 specialization. For example:
5013 template <class T> struct S;
5014 template <class T> struct S<T*>;
5016 The second declaration is OK because `T*' uses the template
5017 parameter T, whereas
5019 template <class T> struct S<int>;
5021 is no good. Even trickier is:
5032 The S2<T> declaration is actually invalid; it is a
5033 full-specialization. Of course,
5036 struct S2<T (*)(U)>;
5038 or some such would have been OK. */
5039 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5040 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5041 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5043 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5044 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5045 for (i
= 0; i
< nargs
; ++i
)
5047 tpd
.current_arg
= i
;
5048 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5049 &mark_template_parm
,
5052 /*include_nondeduced_p=*/false);
5054 for (i
= 0; i
< ntparms
; ++i
)
5055 if (tpd
.parms
[i
] == 0)
5057 /* One of the template parms was not used in a deduced context in the
5059 if (!did_error_intro
)
5061 error ("template parameters not deducible in "
5062 "partial specialization:");
5063 did_error_intro
= true;
5066 inform (input_location
, " %qD",
5067 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5070 if (did_error_intro
)
5071 return error_mark_node
;
5073 /* [temp.class.spec]
5075 The argument list of the specialization shall not be identical to
5076 the implicit argument list of the primary template. */
5078 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5079 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5081 || !strictly_subsumes (current_template_constraints (),
5082 main_args
, maintmpl
)))
5085 error ("partial specialization %q+D does not specialize "
5086 "any template arguments; to define the primary template, "
5087 "remove the template argument list", decl
);
5089 error ("partial specialization %q+D does not specialize any "
5090 "template arguments and is not more constrained than "
5091 "the primary template; to define the primary template, "
5092 "remove the template argument list", decl
);
5093 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5096 /* A partial specialization that replaces multiple parameters of the
5097 primary template with a pack expansion is less specialized for those
5099 if (nargs
< DECL_NTPARMS (maintmpl
))
5101 error ("partial specialization is not more specialized than the "
5102 "primary template because it replaces multiple parameters "
5103 "with a pack expansion");
5104 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5105 /* Avoid crash in process_partial_specialization. */
5109 else if (nargs
> DECL_NTPARMS (maintmpl
))
5111 error ("too many arguments for partial specialization %qT", type
);
5112 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5113 /* Avoid crash below. */
5117 /* If we aren't in a dependent class, we can actually try deduction. */
5118 else if (tpd
.level
== 1
5119 /* FIXME we should be able to handle a partial specialization of a
5120 partial instantiation, but currently we can't (c++/41727). */
5121 && TMPL_ARGS_DEPTH (specargs
) == 1
5122 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5124 auto_diagnostic_group d
;
5125 if (permerror (input_location
, "partial specialization %qD is not "
5126 "more specialized than", decl
))
5127 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5131 /* [temp.class.spec]
5133 A partially specialized non-type argument expression shall not
5134 involve template parameters of the partial specialization except
5135 when the argument expression is a simple identifier.
5137 The type of a template parameter corresponding to a specialized
5138 non-type argument shall not be dependent on a parameter of the
5141 Also, we verify that pack expansions only occur at the
5142 end of the argument list. */
5144 for (i
= 0; i
< nargs
; ++i
)
5146 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5147 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5148 tree packed_args
= NULL_TREE
;
5151 if (ARGUMENT_PACK_P (arg
))
5153 /* Extract the arguments from the argument pack. We'll be
5154 iterating over these in the following loop. */
5155 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5156 len
= TREE_VEC_LENGTH (packed_args
);
5159 for (j
= 0; j
< len
; j
++)
5162 /* Get the Jth argument in the parameter pack. */
5163 arg
= TREE_VEC_ELT (packed_args
, j
);
5165 if (PACK_EXPANSION_P (arg
))
5167 /* Pack expansions must come at the end of the
5169 if ((packed_args
&& j
< len
- 1)
5170 || (!packed_args
&& i
< nargs
- 1))
5172 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5173 error ("parameter pack argument %qE must be at the "
5174 "end of the template argument list", arg
);
5176 error ("parameter pack argument %qT must be at the "
5177 "end of the template argument list", arg
);
5181 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5182 /* We only care about the pattern. */
5183 arg
= PACK_EXPANSION_PATTERN (arg
);
5185 if (/* These first two lines are the `non-type' bit. */
5187 && TREE_CODE (arg
) != TEMPLATE_DECL
5188 /* This next two lines are the `argument expression is not just a
5189 simple identifier' condition and also the `specialized
5190 non-type argument' bit. */
5191 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5192 && !((REFERENCE_REF_P (arg
)
5193 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5194 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5196 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5197 || (packed_args
&& uses_template_parms (arg
)))
5198 error_at (cp_expr_loc_or_input_loc (arg
),
5199 "template argument %qE involves template "
5200 "parameter(s)", arg
);
5203 /* Look at the corresponding template parameter,
5204 marking which template parameters its type depends
5206 tree type
= TREE_TYPE (parm
);
5210 /* We haven't yet initialized TPD2. Do so now. */
5211 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5212 /* The number of parameters here is the number in the
5213 main template, which, as checked in the assertion
5215 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5217 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5220 /* Mark the template parameters. But this time, we're
5221 looking for the template parameters of the main
5222 template, not in the specialization. */
5223 tpd2
.current_arg
= i
;
5224 tpd2
.arg_uses_template_parms
[i
] = 0;
5225 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5226 for_each_template_parm (type
,
5227 &mark_template_parm
,
5230 /*include_nondeduced_p=*/false);
5232 if (tpd2
.arg_uses_template_parms
[i
])
5234 /* The type depended on some template parameters.
5235 If they are fully specialized in the
5236 specialization, that's OK. */
5239 for (j
= 0; j
< nargs
; ++j
)
5240 if (tpd2
.parms
[j
] != 0
5241 && tpd
.arg_uses_template_parms
[j
])
5244 error_n (input_location
, count
,
5245 "type %qT of template argument %qE depends "
5246 "on a template parameter",
5247 "type %qT of template argument %qE depends "
5248 "on template parameters",
5257 /* We should only get here once. */
5258 if (TREE_CODE (decl
) == TYPE_DECL
)
5259 gcc_assert (!COMPLETE_TYPE_P (type
));
5261 // Build the template decl.
5262 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5263 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5264 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5265 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5266 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5268 /* Give template template parms a DECL_CONTEXT of the template
5269 for which they are a parameter. */
5270 for (i
= 0; i
< ntparms
; ++i
)
5272 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5273 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5274 DECL_CONTEXT (parm
) = tmpl
;
5278 /* We didn't register this in check_explicit_specialization so we could
5279 wait until the constraints were set. */
5280 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5282 associate_classtype_constraints (type
);
5284 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5285 = tree_cons (specargs
, tmpl
,
5286 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5287 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5289 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5290 inst
= TREE_CHAIN (inst
))
5292 tree instance
= TREE_VALUE (inst
);
5293 if (TYPE_P (instance
)
5294 ? (COMPLETE_TYPE_P (instance
)
5295 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5296 : DECL_TEMPLATE_INSTANTIATION (instance
))
5298 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5299 tree inst_decl
= (DECL_P (instance
)
5300 ? instance
: TYPE_NAME (instance
));
5303 else if (spec
== error_mark_node
)
5304 permerror (input_location
,
5305 "declaration of %qD ambiguates earlier template "
5306 "instantiation for %qD", decl
, inst_decl
);
5307 else if (TREE_VALUE (spec
) == tmpl
)
5308 permerror (input_location
,
5309 "partial specialization of %qD after instantiation "
5310 "of %qD", decl
, inst_decl
);
5317 /* PARM is a template parameter of some form; return the corresponding
5318 TEMPLATE_PARM_INDEX. */
5321 get_template_parm_index (tree parm
)
5323 if (TREE_CODE (parm
) == PARM_DECL
5324 || TREE_CODE (parm
) == CONST_DECL
)
5325 parm
= DECL_INITIAL (parm
);
5326 else if (TREE_CODE (parm
) == TYPE_DECL
5327 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5328 parm
= TREE_TYPE (parm
);
5329 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5330 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5331 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5332 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5333 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5337 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5338 parameter packs used by the template parameter PARM. */
5341 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5343 /* A type parm can't refer to another parm. */
5344 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5346 else if (TREE_CODE (parm
) == PARM_DECL
)
5348 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5353 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5355 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5356 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5358 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5359 if (template_parameter_pack_p (p
))
5360 /* Any packs in the type are expanded by this parameter. */;
5362 fixed_parameter_pack_p_1 (p
, ppd
);
5366 /* PARM is a template parameter pack. Return any parameter packs used in
5367 its type or the type of any of its template parameters. If there are
5368 any such packs, it will be instantiated into a fixed template parameter
5369 list by partial instantiation rather than be fully deduced. */
5372 fixed_parameter_pack_p (tree parm
)
5374 /* This can only be true in a member template. */
5375 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5377 /* This can only be true for a parameter pack. */
5378 if (!template_parameter_pack_p (parm
))
5380 /* A type parm can't refer to another parm. */
5381 if (TREE_CODE (parm
) == TYPE_DECL
)
5384 tree parameter_packs
= NULL_TREE
;
5385 struct find_parameter_pack_data ppd
;
5386 ppd
.parameter_packs
= ¶meter_packs
;
5387 ppd
.visited
= new hash_set
<tree
>;
5388 ppd
.type_pack_expansion_p
= false;
5390 fixed_parameter_pack_p_1 (parm
, &ppd
);
5393 return parameter_packs
;
5396 /* Check that a template declaration's use of default arguments and
5397 parameter packs is not invalid. Here, PARMS are the template
5398 parameters. IS_PRIMARY is true if DECL is the thing declared by
5399 a primary template. IS_PARTIAL is true if DECL is a partial
5402 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5403 function template declaration or a friend class template
5404 declaration. In the function case, 1 indicates a declaration, 2
5405 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5406 emitted for extraneous default arguments.
5408 Returns TRUE if there were no errors found, FALSE otherwise. */
5411 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5412 bool is_partial
, int is_friend_decl
)
5415 int last_level_to_check
;
5417 bool no_errors
= true;
5421 A default template-argument shall not be specified in a
5422 function template declaration or a function template definition, nor
5423 in the template-parameter-list of the definition of a member of a
5426 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5427 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5428 /* You can't have a function template declaration in a local
5429 scope, nor you can you define a member of a class template in a
5433 if ((TREE_CODE (decl
) == TYPE_DECL
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5436 || (TREE_CODE (decl
) == FUNCTION_DECL
5437 && LAMBDA_FUNCTION_P (decl
)))
5438 /* A lambda doesn't have an explicit declaration; don't complain
5439 about the parms of the enclosing class. */
5442 if (current_class_type
5443 && !TYPE_BEING_DEFINED (current_class_type
)
5444 && DECL_LANG_SPECIFIC (decl
)
5445 && DECL_DECLARES_FUNCTION_P (decl
)
5446 /* If this is either a friend defined in the scope of the class
5447 or a member function. */
5448 && (DECL_FUNCTION_MEMBER_P (decl
)
5449 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5450 : DECL_FRIEND_CONTEXT (decl
)
5451 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5453 /* And, if it was a member function, it really was defined in
5454 the scope of the class. */
5455 && (!DECL_FUNCTION_MEMBER_P (decl
)
5456 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5457 /* We already checked these parameters when the template was
5458 declared, so there's no need to do it again now. This function
5459 was defined in class scope, but we're processing its body now
5460 that the class is complete. */
5463 /* Core issue 226 (C++0x only): the following only applies to class
5466 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5470 If a template-parameter has a default template-argument, all
5471 subsequent template-parameters shall have a default
5472 template-argument supplied. */
5473 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5475 tree inner_parms
= TREE_VALUE (parm_level
);
5476 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5477 int seen_def_arg_p
= 0;
5480 for (i
= 0; i
< ntparms
; ++i
)
5482 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5484 if (parm
== error_mark_node
)
5487 if (TREE_PURPOSE (parm
))
5489 else if (seen_def_arg_p
5490 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5492 error ("no default argument for %qD", TREE_VALUE (parm
));
5493 /* For better subsequent error-recovery, we indicate that
5494 there should have been a default argument. */
5495 TREE_PURPOSE (parm
) = error_mark_node
;
5498 else if (!is_partial
5500 /* Don't complain about an enclosing partial
5502 && parm_level
== parms
5503 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5505 && template_parameter_pack_p (TREE_VALUE (parm
))
5506 /* A fixed parameter pack will be partially
5507 instantiated into a fixed length list. */
5508 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5510 /* A primary class template, primary variable template
5511 (DR 2032), or alias template can only have one
5512 parameter pack, at the end of the template
5515 error ("parameter pack %q+D must be at the end of the"
5516 " template parameter list", TREE_VALUE (parm
));
5518 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5526 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5530 /* For an ordinary class template, default template arguments are
5531 allowed at the innermost level, e.g.:
5532 template <class T = int>
5534 but, in a partial specialization, they're not allowed even
5535 there, as we have in [temp.class.spec]:
5537 The template parameter list of a specialization shall not
5538 contain default template argument values.
5540 So, for a partial specialization, or for a function template
5541 (in C++98/C++03), we look at all of them. */
5544 /* But, for a primary class template that is not a partial
5545 specialization we look at all template parameters except the
5547 parms
= TREE_CHAIN (parms
);
5549 /* Figure out what error message to issue. */
5550 if (is_friend_decl
== 2)
5551 msg
= G_("default template arguments may not be used in function template "
5552 "friend re-declaration");
5553 else if (is_friend_decl
)
5554 msg
= G_("default template arguments may not be used in template "
5555 "friend declarations");
5556 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5557 msg
= G_("default template arguments may not be used in function templates "
5558 "without %<-std=c++11%> or %<-std=gnu++11%>");
5559 else if (is_partial
)
5560 msg
= G_("default template arguments may not be used in "
5561 "partial specializations");
5562 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5563 msg
= G_("default argument for template parameter for class enclosing %qD");
5565 /* Per [temp.param]/9, "A default template-argument shall not be
5566 specified in the template-parameter-lists of the definition of
5567 a member of a class template that appears outside of the member's
5568 class.", thus if we aren't handling a member of a class template
5569 there is no need to examine the parameters. */
5572 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5573 /* If we're inside a class definition, there's no need to
5574 examine the parameters to the class itself. On the one
5575 hand, they will be checked when the class is defined, and,
5576 on the other, default arguments are valid in things like:
5577 template <class T = double>
5578 struct S { template <class U> void f(U); };
5579 Here the default argument for `S' has no bearing on the
5580 declaration of `f'. */
5581 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5583 /* Check everything. */
5584 last_level_to_check
= 0;
5586 for (parm_level
= parms
;
5587 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5588 parm_level
= TREE_CHAIN (parm_level
))
5590 tree inner_parms
= TREE_VALUE (parm_level
);
5594 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5595 for (i
= 0; i
< ntparms
; ++i
)
5597 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5605 if (is_friend_decl
== 2)
5612 /* Clear out the default argument so that we are not
5614 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5618 /* At this point, if we're still interested in issuing messages,
5619 they must apply to classes surrounding the object declared. */
5621 msg
= G_("default argument for template parameter for class "
5628 /* Worker for push_template_decl_real, called via
5629 for_each_template_parm. DATA is really an int, indicating the
5630 level of the parameters we are interested in. If T is a template
5631 parameter of that level, return nonzero. */
5634 template_parm_this_level_p (tree t
, void* data
)
5636 int this_level
= *(int *)data
;
5639 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5640 level
= TEMPLATE_PARM_LEVEL (t
);
5642 level
= TEMPLATE_TYPE_LEVEL (t
);
5643 return level
== this_level
;
5646 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5647 DATA is really an int, indicating the innermost outer level of parameters.
5648 If T is a template parameter of that level or further out, return
5652 template_parm_outer_level (tree t
, void *data
)
5654 int this_level
= *(int *)data
;
5657 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5658 level
= TEMPLATE_PARM_LEVEL (t
);
5660 level
= TEMPLATE_TYPE_LEVEL (t
);
5661 return level
<= this_level
;
5664 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5665 parameters given by current_template_args, or reuses a
5666 previously existing one, if appropriate. Returns the DECL, or an
5667 equivalent one, if it is replaced via a call to duplicate_decls.
5669 If IS_FRIEND is true, DECL is a friend declaration. */
5672 push_template_decl_real (tree decl
, bool is_friend
)
5680 int new_template_p
= 0;
5681 /* True if the template is a member template, in the sense of
5683 bool member_template_p
= false;
5685 if (decl
== error_mark_node
|| !current_template_parms
)
5686 return error_mark_node
;
5688 /* See if this is a partial specialization. */
5689 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5690 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5691 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5693 && DECL_LANG_SPECIFIC (decl
)
5694 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5695 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5697 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5701 /* For a friend, we want the context of the friend, not
5702 the type of which it is a friend. */
5703 ctx
= CP_DECL_CONTEXT (decl
);
5704 else if (CP_DECL_CONTEXT (decl
)
5705 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5706 /* In the case of a virtual function, we want the class in which
5708 ctx
= CP_DECL_CONTEXT (decl
);
5710 /* Otherwise, if we're currently defining some class, the DECL
5711 is assumed to be a member of the class. */
5712 ctx
= current_scope ();
5714 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5717 if (!DECL_CONTEXT (decl
))
5718 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5720 /* See if this is a primary template. */
5721 if (is_friend
&& ctx
5722 && uses_template_parms_level (ctx
, processing_template_decl
))
5723 /* A friend template that specifies a class context, i.e.
5724 template <typename T> friend void A<T>::f();
5727 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5730 is_primary
= template_parm_scope_p ();
5734 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5736 if (DECL_CLASS_SCOPE_P (decl
))
5737 member_template_p
= true;
5738 if (TREE_CODE (decl
) == TYPE_DECL
5739 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5741 error ("template class without a name");
5742 return error_mark_node
;
5744 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5746 if (member_template_p
)
5748 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5749 error ("member template %qD may not have virt-specifiers", decl
);
5751 if (DECL_DESTRUCTOR_P (decl
))
5755 A destructor shall not be a member template. */
5756 error_at (DECL_SOURCE_LOCATION (decl
),
5757 "destructor %qD declared as member template", decl
);
5758 return error_mark_node
;
5760 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5761 && (!prototype_p (TREE_TYPE (decl
))
5762 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5763 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5764 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5765 == void_list_node
)))
5767 /* [basic.stc.dynamic.allocation]
5769 An allocation function can be a function
5770 template. ... Template allocation functions shall
5771 have two or more parameters. */
5772 error ("invalid template declaration of %qD", decl
);
5773 return error_mark_node
;
5776 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5777 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5779 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5780 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5781 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5783 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5784 if (TREE_CODE (t
) == TYPE_DECL
)
5786 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5787 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5790 else if (TREE_CODE (decl
) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl
))
5792 /* alias-declaration */
5793 gcc_assert (!DECL_ARTIFICIAL (decl
));
5794 else if (VAR_P (decl
))
5795 /* C++14 variable template. */;
5796 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5797 /* C++20 concept definitions. */;
5800 error ("template declaration of %q#D", decl
);
5801 return error_mark_node
;
5805 /* Check to see that the rules regarding the use of default
5806 arguments are not being violated. We check args for a friend
5807 functions when we know whether it's a definition, introducing
5808 declaration or re-declaration. */
5809 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5810 check_default_tmpl_args (decl
, current_template_parms
,
5811 is_primary
, is_partial
, is_friend
);
5813 /* Ensure that there are no parameter packs in the type of this
5814 declaration that have not been expanded. */
5815 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5817 /* Check each of the arguments individually to see if there are
5818 any bare parameter packs. */
5819 tree type
= TREE_TYPE (decl
);
5820 tree arg
= DECL_ARGUMENTS (decl
);
5821 tree argtype
= TYPE_ARG_TYPES (type
);
5823 while (arg
&& argtype
)
5825 if (!DECL_PACK_P (arg
)
5826 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5828 /* This is a PARM_DECL that contains unexpanded parameter
5829 packs. We have already complained about this in the
5830 check_for_bare_parameter_packs call, so just replace
5831 these types with ERROR_MARK_NODE. */
5832 TREE_TYPE (arg
) = error_mark_node
;
5833 TREE_VALUE (argtype
) = error_mark_node
;
5836 arg
= DECL_CHAIN (arg
);
5837 argtype
= TREE_CHAIN (argtype
);
5840 /* Check for bare parameter packs in the return type and the
5841 exception specifiers. */
5842 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5843 /* Errors were already issued, set return type to int
5844 as the frontend doesn't expect error_mark_node as
5846 TREE_TYPE (type
) = integer_type_node
;
5847 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5848 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5850 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5851 ? DECL_ORIGINAL_TYPE (decl
)
5852 : TREE_TYPE (decl
)))
5854 TREE_TYPE (decl
) = error_mark_node
;
5855 return error_mark_node
;
5859 return process_partial_specialization (decl
);
5861 args
= current_template_args ();
5864 || TREE_CODE (ctx
) == FUNCTION_DECL
5865 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5866 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5867 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5869 if (DECL_LANG_SPECIFIC (decl
)
5870 && DECL_TEMPLATE_INFO (decl
)
5871 && DECL_TI_TEMPLATE (decl
))
5872 tmpl
= DECL_TI_TEMPLATE (decl
);
5873 /* If DECL is a TYPE_DECL for a class-template, then there won't
5874 be DECL_LANG_SPECIFIC. The information equivalent to
5875 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5876 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5877 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5878 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5880 /* Since a template declaration already existed for this
5881 class-type, we must be redeclaring it here. Make sure
5882 that the redeclaration is valid. */
5883 redeclare_class_template (TREE_TYPE (decl
),
5884 current_template_parms
,
5885 current_template_constraints ());
5886 /* We don't need to create a new TEMPLATE_DECL; just use the
5887 one we already had. */
5888 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5892 tmpl
= build_template_decl (decl
, current_template_parms
,
5896 if (DECL_LANG_SPECIFIC (decl
)
5897 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5899 /* A specialization of a member template of a template
5901 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5902 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5903 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5909 tree a
, t
, current
, parms
;
5911 tree tinfo
= get_template_info (decl
);
5915 error ("template definition of non-template %q#D", decl
);
5916 return error_mark_node
;
5919 tmpl
= TI_TEMPLATE (tinfo
);
5921 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5922 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5923 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5924 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5928 /* The declaration is a specialization of a member
5929 template, declared outside the class. Therefore, the
5930 innermost template arguments will be NULL, so we
5931 replace them with the arguments determined by the
5932 earlier call to check_explicit_specialization. */
5933 args
= DECL_TI_ARGS (decl
);
5936 = build_template_decl (decl
, current_template_parms
,
5938 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5939 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5940 DECL_TEMPLATE_INFO (new_tmpl
)
5941 = build_template_info (tmpl
, args
);
5943 register_specialization (new_tmpl
,
5944 most_general_template (tmpl
),
5950 /* Make sure the template headers we got make sense. */
5952 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5953 i
= TMPL_PARMS_DEPTH (parms
);
5954 if (TMPL_ARGS_DEPTH (args
) != i
)
5956 error ("expected %d levels of template parms for %q#D, got %d",
5957 i
, decl
, TMPL_ARGS_DEPTH (args
));
5958 DECL_INTERFACE_KNOWN (decl
) = 1;
5959 return error_mark_node
;
5962 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5964 a
= TMPL_ARGS_LEVEL (args
, i
);
5965 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5967 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5969 if (current
== decl
)
5970 error ("got %d template parameters for %q#D",
5971 TREE_VEC_LENGTH (a
), decl
);
5973 error ("got %d template parameters for %q#T",
5974 TREE_VEC_LENGTH (a
), current
);
5975 error (" but %d required", TREE_VEC_LENGTH (t
));
5976 /* Avoid crash in import_export_decl. */
5977 DECL_INTERFACE_KNOWN (decl
) = 1;
5978 return error_mark_node
;
5981 if (current
== decl
)
5983 else if (current
== NULL_TREE
)
5984 /* Can happen in erroneous input. */
5987 current
= get_containing_scope (current
);
5990 /* Check that the parms are used in the appropriate qualifying scopes
5991 in the declarator. */
5992 if (!comp_template_args
5994 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5996 error ("template arguments to %qD do not match original "
5997 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5998 if (!uses_template_parms (TI_ARGS (tinfo
)))
5999 inform (input_location
, "use %<template<>%> for"
6000 " an explicit specialization");
6001 /* Avoid crash in import_export_decl. */
6002 DECL_INTERFACE_KNOWN (decl
) = 1;
6003 return error_mark_node
;
6007 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6011 /* Push template declarations for global functions and types.
6012 Note that we do not try to push a global template friend
6013 declared in a template class; such a thing may well depend on
6014 the template parameters of the class and we'll push it when
6015 instantiating the befriending class. */
6017 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6019 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
6020 if (tmpl
== error_mark_node
)
6021 return error_mark_node
;
6023 /* Hide template friend classes that haven't been declared yet. */
6024 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
6026 DECL_ANTICIPATED (tmpl
) = 1;
6027 DECL_FRIEND_P (tmpl
) = 1;
6032 /* The type may have been completed, or (erroneously) changed. */
6033 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6037 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6039 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6041 /* Give template template parms a DECL_CONTEXT of the template
6042 for which they are a parameter. */
6043 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6044 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6046 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6047 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6048 DECL_CONTEXT (parm
) = tmpl
;
6051 if (TREE_CODE (decl
) == TYPE_DECL
6052 && TYPE_DECL_ALIAS_P (decl
))
6055 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6057 /* ??? Why don't we do this here for all templates? */
6058 constr
= build_constraints (constr
, NULL_TREE
);
6059 set_constraints (decl
, constr
);
6061 if (complex_alias_template_p (tmpl
))
6062 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6066 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6067 back to its most general template. If TMPL is a specialization,
6068 ARGS may only have the innermost set of arguments. Add the missing
6069 argument levels if necessary. */
6070 if (DECL_TEMPLATE_INFO (tmpl
))
6071 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6073 info
= build_template_info (tmpl
, args
);
6075 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6076 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6080 retrofit_lang_decl (decl
);
6081 if (DECL_LANG_SPECIFIC (decl
)
6082 && !(VAR_OR_FUNCTION_DECL_P (decl
)
6083 && DECL_LOCAL_DECL_P (decl
)))
6084 DECL_TEMPLATE_INFO (decl
) = info
;
6087 if (flag_implicit_templates
6089 && TREE_PUBLIC (decl
)
6090 && VAR_OR_FUNCTION_DECL_P (decl
))
6091 /* Set DECL_COMDAT on template instantiations; if we force
6092 them to be emitted by explicit instantiation,
6093 mark_needed will tell cgraph to do the right thing. */
6094 DECL_COMDAT (decl
) = true;
6096 return DECL_TEMPLATE_RESULT (tmpl
);
6100 push_template_decl (tree decl
)
6102 return push_template_decl_real (decl
, false);
6105 /* FN is an inheriting constructor that inherits from the constructor
6106 template INHERITED; turn FN into a constructor template with a matching
6110 add_inherited_template_parms (tree fn
, tree inherited
)
6113 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6114 inner_parms
= copy_node (inner_parms
);
6116 = tree_cons (size_int (processing_template_decl
+ 1),
6117 inner_parms
, current_template_parms
);
6118 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6119 tree args
= template_parms_to_args (parms
);
6120 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6121 DECL_ARTIFICIAL (tmpl
) = true;
6122 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6126 /* Called when a class template TYPE is redeclared with the indicated
6127 template PARMS, e.g.:
6129 template <class T> struct S;
6130 template <class T> struct S {}; */
6133 redeclare_class_template (tree type
, tree parms
, tree cons
)
6139 if (!TYPE_TEMPLATE_INFO (type
))
6141 error ("%qT is not a template type", type
);
6145 tmpl
= TYPE_TI_TEMPLATE (type
);
6146 if (!PRIMARY_TEMPLATE_P (tmpl
))
6147 /* The type is nested in some template class. Nothing to worry
6148 about here; there are no new template parameters for the nested
6154 error ("template specifiers not specified in declaration of %qD",
6159 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6160 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6162 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6164 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6165 "redeclared with %d template parameter",
6166 "redeclared with %d template parameters",
6167 TREE_VEC_LENGTH (parms
));
6168 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6169 "previous declaration %qD used %d template parameter",
6170 "previous declaration %qD used %d template parameters",
6171 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6175 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6182 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6183 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6186 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6187 if (error_operand_p (tmpl_parm
))
6190 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6191 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6192 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6194 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6196 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6197 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6198 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6199 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6200 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6201 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6202 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6203 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6204 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6206 auto_diagnostic_group d
;
6207 error ("template parameter %q+#D", tmpl_parm
);
6208 inform (input_location
, "redeclared here as %q#D", parm
);
6212 /* The parameters can be declared to introduce different
6214 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6215 tree p2
= TREE_VEC_ELT (parms
, i
);
6216 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6218 auto_diagnostic_group d
;
6219 error ("declaration of template parameter %q+#D with different "
6220 "constraints", parm
);
6221 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6222 "original declaration appeared here");
6226 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6228 /* We have in [temp.param]:
6230 A template-parameter may not be given default arguments
6231 by two different declarations in the same scope. */
6232 auto_diagnostic_group d
;
6233 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6234 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6235 "original definition appeared here");
6239 if (parm_default
!= NULL_TREE
)
6240 /* Update the previous template parameters (which are the ones
6241 that will really count) with the new default value. */
6242 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6243 else if (tmpl_default
!= NULL_TREE
)
6244 /* Update the new parameters, too; they'll be used as the
6245 parameters for any members. */
6246 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6248 /* Give each template template parm in this redeclaration a
6249 DECL_CONTEXT of the template for which they are a parameter. */
6250 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6252 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6253 DECL_CONTEXT (parm
) = tmpl
;
6256 if (TREE_CODE (parm
) == TYPE_DECL
)
6257 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6260 tree ci
= get_constraints (tmpl
);
6261 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6262 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6264 /* Two classes with different constraints declare different entities. */
6265 if (!cp_tree_equal (req1
, req2
))
6267 auto_diagnostic_group d
;
6268 error_at (input_location
, "redeclaration %q#D with different "
6269 "constraints", tmpl
);
6270 inform (DECL_SOURCE_LOCATION (tmpl
),
6271 "original declaration appeared here");
6278 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6279 to be used when the caller has already checked
6280 (processing_template_decl
6281 && !instantiation_dependent_expression_p (expr)
6282 && potential_constant_expression (expr))
6283 and cleared processing_template_decl. */
6286 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6288 return tsubst_copy_and_build (expr
,
6291 /*in_decl=*/NULL_TREE
,
6292 /*function_p=*/false,
6293 /*integral_constant_expression_p=*/true);
6296 /* Simplify EXPR if it is a non-dependent expression. Returns the
6297 (possibly simplified) expression. */
6300 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6302 if (expr
== NULL_TREE
)
6305 /* If we're in a template, but EXPR isn't value dependent, simplify
6306 it. We're supposed to treat:
6308 template <typename T> void f(T[1 + 1]);
6309 template <typename T> void f(T[2]);
6311 as two declarations of the same function, for example. */
6312 if (processing_template_decl
6313 && is_nondependent_constant_expression (expr
))
6315 processing_template_decl_sentinel s
;
6316 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6322 instantiate_non_dependent_expr (tree expr
)
6324 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6327 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6328 an uninstantiated expression. */
6331 instantiate_non_dependent_or_null (tree expr
)
6333 if (expr
== NULL_TREE
)
6335 if (processing_template_decl
)
6337 if (!is_nondependent_constant_expression (expr
))
6341 processing_template_decl_sentinel s
;
6342 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6348 /* True iff T is a specialization of a variable template. */
6351 variable_template_specialization_p (tree t
)
6353 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6355 tree tmpl
= DECL_TI_TEMPLATE (t
);
6356 return variable_template_p (tmpl
);
6359 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6360 template declaration, or a TYPE_DECL for an alias declaration. */
6363 alias_type_or_template_p (tree t
)
6367 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6370 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6371 || DECL_ALIAS_TEMPLATE_P (t
));
6374 /* If T is a specialization of an alias template, return it; otherwise return
6375 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6378 alias_template_specialization_p (const_tree t
,
6379 bool transparent_typedefs
)
6384 /* It's an alias template specialization if it's an alias and its
6385 TYPE_NAME is a specialization of a primary template. */
6386 if (typedef_variant_p (t
))
6388 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6389 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6390 return CONST_CAST_TREE (t
);
6391 if (transparent_typedefs
)
6392 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6394 transparent_typedefs
);
6400 /* An alias template is complex from a SFINAE perspective if a template-id
6401 using that alias can be ill-formed when the expansion is not, as with
6402 the void_t template. We determine this by checking whether the
6403 expansion for the alias template uses all its template parameters. */
6405 struct uses_all_template_parms_data
6412 uses_all_template_parms_r (tree t
, void *data_
)
6414 struct uses_all_template_parms_data
&data
6415 = *(struct uses_all_template_parms_data
*)data_
;
6416 tree idx
= get_template_parm_index (t
);
6418 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6419 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6423 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6426 complex_pack_expansion_r (tree t
, void *data_
)
6428 /* An alias template with a pack expansion that expands a pack from the
6429 enclosing class needs to be considered complex, to avoid confusion with
6430 the same pack being used as an argument to the alias's own template
6431 parameter (91966). */
6432 if (!PACK_EXPANSION_P (t
))
6434 struct uses_all_template_parms_data
&data
6435 = *(struct uses_all_template_parms_data
*)data_
;
6436 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6437 pack
= TREE_CHAIN (pack
))
6439 tree parm_pack
= TREE_VALUE (pack
);
6440 if (!TEMPLATE_PARM_P (parm_pack
))
6443 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6444 if (level
< data
.level
)
6451 complex_alias_template_p (const_tree tmpl
)
6453 /* A renaming alias isn't complex. */
6454 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6457 /* Any other constrained alias is complex. */
6458 if (get_constraints (tmpl
))
6461 struct uses_all_template_parms_data data
;
6462 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6463 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6464 data
.level
= TMPL_PARMS_DEPTH (parms
);
6465 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6466 data
.seen
= XALLOCAVEC (bool, len
);
6467 for (int i
= 0; i
< len
; ++i
)
6468 data
.seen
[i
] = false;
6470 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6471 NULL
, true, complex_pack_expansion_r
))
6473 for (int i
= 0; i
< len
; ++i
)
6479 /* If T is a specialization of a complex alias template with dependent
6480 template-arguments, return it; otherwise return NULL_TREE. If T is a
6481 typedef to such a specialization, return the specialization. */
6484 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6486 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6489 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6491 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6492 && (any_dependent_template_arguments_p
6493 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6494 return CONST_CAST_TREE (t
);
6496 if (transparent_typedefs
)
6498 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6499 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6505 /* Return the number of innermost template parameters in TMPL. */
6508 num_innermost_template_parms (const_tree tmpl
)
6510 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6511 return TREE_VEC_LENGTH (parms
);
6514 /* Return either TMPL or another template that it is equivalent to under DR
6515 1286: An alias that just changes the name of a template is equivalent to
6516 the other template. */
6519 get_underlying_template (tree tmpl
)
6521 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6522 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6524 /* Determine if the alias is equivalent to an underlying template. */
6525 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6526 /* The underlying type may have been ill-formed. Don't proceed. */
6529 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6533 tree underlying
= TI_TEMPLATE (tinfo
);
6534 if (!PRIMARY_TEMPLATE_P (underlying
)
6535 || (num_innermost_template_parms (tmpl
)
6536 != num_innermost_template_parms (underlying
)))
6539 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6540 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6543 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6544 it's appropriate to treat a less-constrained alias as equivalent. */
6545 if (!at_least_as_constrained (underlying
, tmpl
))
6548 /* Alias is equivalent. Strip it and repeat. */
6555 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6556 must be a reference-to-function or a pointer-to-function type, as specified
6557 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6558 and check that the resulting function has external linkage. */
6561 convert_nontype_argument_function (tree type
, tree expr
,
6562 tsubst_flags_t complain
)
6566 linkage_kind linkage
;
6568 fn
= instantiate_type (type
, fns
, tf_none
);
6569 if (fn
== error_mark_node
)
6570 return error_mark_node
;
6572 if (value_dependent_expression_p (fn
))
6575 fn_no_ptr
= strip_fnptr_conv (fn
);
6576 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6577 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6578 if (BASELINK_P (fn_no_ptr
))
6579 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6581 /* [temp.arg.nontype]/1
6583 A template-argument for a non-type, non-template template-parameter
6586 -- the address of an object or function with external [C++11: or
6587 internal] linkage. */
6589 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6590 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6592 if (complain
& tf_error
)
6594 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6595 error_at (loc
, "%qE is not a valid template argument for type %qT",
6597 if (TYPE_PTR_P (type
))
6598 inform (loc
, "it must be the address of a function "
6599 "with external linkage");
6601 inform (loc
, "it must be the name of a function with "
6602 "external linkage");
6607 linkage
= decl_linkage (fn_no_ptr
);
6608 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6610 if (complain
& tf_error
)
6612 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6613 if (cxx_dialect
>= cxx11
)
6614 error_at (loc
, "%qE is not a valid template argument for type "
6615 "%qT because %qD has no linkage",
6616 expr
, type
, fn_no_ptr
);
6618 error_at (loc
, "%qE is not a valid template argument for type "
6619 "%qT because %qD does not have external linkage",
6620 expr
, type
, fn_no_ptr
);
6626 if (TYPE_REF_P (type
))
6628 if (REFERENCE_REF_P (fn
))
6629 fn
= TREE_OPERAND (fn
, 0);
6631 fn
= build_address (fn
);
6633 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6634 fn
= build_nop (type
, fn
);
6639 /* Subroutine of convert_nontype_argument.
6640 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6641 Emit an error otherwise. */
6644 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6645 tsubst_flags_t complain
)
6647 tree orig_expr
= expr
;
6649 if (null_ptr_cst_p (expr
))
6651 if (TREE_CODE (expr
) == PTRMEM_CST
6652 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6653 PTRMEM_CST_CLASS (expr
)))
6655 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6657 if (processing_template_decl
6658 && TREE_CODE (expr
) == ADDR_EXPR
6659 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6661 if (complain
& tf_error
)
6663 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6664 error_at (loc
, "%qE is not a valid template argument for type %qT",
6666 if (TREE_CODE (expr
) != PTRMEM_CST
)
6667 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6669 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6674 /* Returns TRUE iff the address of OP is value-dependent.
6676 14.6.2.4 [temp.dep.temp]:
6677 A non-integral non-type template-argument is dependent if its type is
6678 dependent or it has either of the following forms
6681 and contains a nested-name-specifier which specifies a class-name that
6682 names a dependent type.
6684 We generalize this to just say that the address of a member of a
6685 dependent class is value-dependent; the above doesn't cover the
6686 address of a static data member named with an unqualified-id. */
6689 has_value_dependent_address (tree op
)
6691 STRIP_ANY_LOCATION_WRAPPER (op
);
6693 /* We could use get_inner_reference here, but there's no need;
6694 this is only relevant for template non-type arguments, which
6695 can only be expressed as &id-expression. */
6698 tree ctx
= CP_DECL_CONTEXT (op
);
6699 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6706 /* The next set of functions are used for providing helpful explanatory
6707 diagnostics for failed overload resolution. Their messages should be
6708 indented by two spaces for consistency with the messages in
6712 unify_success (bool /*explain_p*/)
6717 /* Other failure functions should call this one, to provide a single function
6718 for setting a breakpoint on. */
6721 unify_invalid (bool /*explain_p*/)
6727 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6730 inform (input_location
,
6731 " couldn%'t deduce template parameter %qD", parm
);
6732 return unify_invalid (explain_p
);
6736 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6739 inform (input_location
,
6740 " types %qT and %qT have incompatible cv-qualifiers",
6742 return unify_invalid (explain_p
);
6746 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6749 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6750 return unify_invalid (explain_p
);
6754 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6757 inform (input_location
,
6758 " template parameter %qD is not a parameter pack, but "
6761 return unify_invalid (explain_p
);
6765 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6768 inform (input_location
,
6769 " template argument %qE does not match "
6770 "pointer-to-member constant %qE",
6772 return unify_invalid (explain_p
);
6776 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6779 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6780 return unify_invalid (explain_p
);
6784 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6787 inform (input_location
,
6788 " inconsistent parameter pack deduction with %qT and %qT",
6790 return unify_invalid (explain_p
);
6794 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6799 inform (input_location
,
6800 " deduced conflicting types for parameter %qT (%qT and %qT)",
6801 parm
, first
, second
);
6803 inform (input_location
,
6804 " deduced conflicting values for non-type parameter "
6805 "%qE (%qE and %qE)", parm
, first
, second
);
6807 return unify_invalid (explain_p
);
6811 unify_vla_arg (bool explain_p
, tree arg
)
6814 inform (input_location
,
6815 " variable-sized array type %qT is not "
6816 "a valid template argument",
6818 return unify_invalid (explain_p
);
6822 unify_method_type_error (bool explain_p
, tree arg
)
6825 inform (input_location
,
6826 " member function type %qT is not a valid template argument",
6828 return unify_invalid (explain_p
);
6832 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6837 inform_n (input_location
, wanted
,
6838 " candidate expects at least %d argument, %d provided",
6839 " candidate expects at least %d arguments, %d provided",
6842 inform_n (input_location
, wanted
,
6843 " candidate expects %d argument, %d provided",
6844 " candidate expects %d arguments, %d provided",
6847 return unify_invalid (explain_p
);
6851 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6853 return unify_arity (explain_p
, have
, wanted
);
6857 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6858 bool least_p
= false)
6860 return unify_arity (explain_p
, have
, wanted
, least_p
);
6864 unify_arg_conversion (bool explain_p
, tree to_type
,
6865 tree from_type
, tree arg
)
6868 inform (cp_expr_loc_or_input_loc (arg
),
6869 " cannot convert %qE (type %qT) to type %qT",
6870 arg
, from_type
, to_type
);
6871 return unify_invalid (explain_p
);
6875 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6876 tree parm
, tree arg
)
6881 case tbr_ambiguous_baseclass
:
6882 inform (input_location
, " %qT is an ambiguous base class of %qT",
6886 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6889 return unify_invalid (explain_p
);
6893 unify_inconsistent_template_template_parameters (bool explain_p
)
6896 inform (input_location
,
6897 " template parameters of a template template argument are "
6898 "inconsistent with other deduced template arguments");
6899 return unify_invalid (explain_p
);
6903 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6906 inform (input_location
,
6907 " cannot deduce a template for %qT from non-template type %qT",
6909 return unify_invalid (explain_p
);
6913 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6916 inform (input_location
,
6917 " template argument %qE does not match %qE", arg
, parm
);
6918 return unify_invalid (explain_p
);
6921 /* True if T is a C++20 template parameter object to store the argument for a
6922 template parameter of class type. */
6925 template_parm_object_p (const_tree t
)
6927 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6928 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6931 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6932 argument for TYPE, points to an unsuitable object.
6934 Also adjust the type of the index in C++20 array subobject references. */
6937 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6939 switch (TREE_CODE (expr
))
6942 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6946 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6951 unsigned i
; tree elt
;
6952 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6953 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6960 tree decl
= TREE_OPERAND (expr
, 0);
6962 if (cxx_dialect
>= cxx20
)
6963 while (TREE_CODE (decl
) == COMPONENT_REF
6964 || TREE_CODE (decl
) == ARRAY_REF
)
6966 tree
&op
= TREE_OPERAND (decl
, 1);
6967 if (TREE_CODE (decl
) == ARRAY_REF
6968 && TREE_CODE (op
) == INTEGER_CST
)
6969 /* Canonicalize array offsets to ptrdiff_t; how they were
6970 written doesn't matter for subobject identity. */
6971 op
= fold_convert (ptrdiff_type_node
, op
);
6972 decl
= TREE_OPERAND (decl
, 0);
6977 if (complain
& tf_error
)
6978 error_at (cp_expr_loc_or_input_loc (expr
),
6979 "%qE is not a valid template argument of type %qT "
6980 "because %qE is not a variable", expr
, type
, decl
);
6983 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6985 if (complain
& tf_error
)
6986 error_at (cp_expr_loc_or_input_loc (expr
),
6987 "%qE is not a valid template argument of type %qT "
6988 "in C++98 because %qD does not have external linkage",
6992 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6993 && decl_linkage (decl
) == lk_none
)
6995 if (complain
& tf_error
)
6996 error_at (cp_expr_loc_or_input_loc (expr
),
6997 "%qE is not a valid template argument of type %qT "
6998 "because %qD has no linkage", expr
, type
, decl
);
7001 /* C++17: For a non-type template-parameter of reference or pointer
7002 type, the value of the constant expression shall not refer to (or
7003 for a pointer type, shall not be the address of):
7004 * a subobject (4.5),
7005 * a temporary object (15.2),
7006 * a string literal (5.13.5),
7007 * the result of a typeid expression (8.2.8), or
7008 * a predefined __func__ variable (11.4.1). */
7009 else if (DECL_ARTIFICIAL (decl
))
7011 if (complain
& tf_error
)
7012 error ("the address of %qD is not a valid template argument",
7016 else if (cxx_dialect
< cxx20
7017 && !(same_type_ignoring_top_level_qualifiers_p
7018 (strip_array_types (TREE_TYPE (type
)),
7019 strip_array_types (TREE_TYPE (decl
)))))
7021 if (complain
& tf_error
)
7022 error ("the address of the %qT subobject of %qD is not a "
7023 "valid template argument", TREE_TYPE (type
), decl
);
7026 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7028 if (complain
& tf_error
)
7029 error ("the address of %qD is not a valid template argument "
7030 "because it does not have static storage duration",
7038 if (!INDIRECT_TYPE_P (type
))
7039 /* We're only concerned about pointers and references here. */;
7040 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7041 /* Null pointer values are OK in C++11. */;
7046 if (complain
& tf_error
)
7047 error ("%qD is not a valid template argument "
7048 "because %qD is a variable, not the address of "
7049 "a variable", expr
, expr
);
7054 if (complain
& tf_error
)
7055 error ("%qE is not a valid template argument for %qT "
7056 "because it is not the address of a variable",
7066 /* The template arguments corresponding to template parameter objects of types
7067 that contain pointers to members. */
7069 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7071 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7072 template argument EXPR. */
7075 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7077 if (TREE_CODE (expr
) == TARGET_EXPR
)
7078 expr
= TARGET_EXPR_INITIAL (expr
);
7080 if (!TREE_CONSTANT (expr
))
7082 if ((complain
& tf_error
)
7083 && require_rvalue_constant_expression (expr
))
7084 cxx_constant_value (expr
);
7085 return error_mark_node
;
7087 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7088 return error_mark_node
;
7090 tree name
= mangle_template_parm_object (expr
);
7091 tree decl
= get_global_binding (name
);
7095 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7096 decl
= create_temporary_var (type
);
7097 TREE_STATIC (decl
) = true;
7098 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7099 TREE_READONLY (decl
) = true;
7100 DECL_NAME (decl
) = name
;
7101 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7102 DECL_CONTEXT (decl
) = global_namespace
;
7103 comdat_linkage (decl
);
7105 if (!zero_init_p (type
))
7107 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7108 lower_var_init before we're done mangling. So store the original
7110 tree copy
= unshare_constructor (expr
);
7111 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7114 pushdecl_top_level_and_finish (decl
, expr
);
7119 /* Return the actual template argument corresponding to template parameter
7123 tparm_object_argument (tree var
)
7125 if (zero_init_p (TREE_TYPE (var
)))
7126 return DECL_INITIAL (var
);
7127 return *(tparm_obj_values
->get (var
));
7130 /* Attempt to convert the non-type template parameter EXPR to the
7131 indicated TYPE. If the conversion is successful, return the
7132 converted value. If the conversion is unsuccessful, return
7133 NULL_TREE if we issued an error message, or error_mark_node if we
7134 did not. We issue error messages for out-and-out bad template
7135 parameters, but not simply because the conversion failed, since we
7136 might be just trying to do argument deduction. Both TYPE and EXPR
7137 must be non-dependent.
7139 The conversion follows the special rules described in
7140 [temp.arg.nontype], and it is much more strict than an implicit
7143 This function is called twice for each template argument (see
7144 lookup_template_class for a more accurate description of this
7145 problem). This means that we need to handle expressions which
7146 are not valid in a C++ source, but can be created from the
7147 first call (for instance, casts to perform conversions). These
7148 hacks can go away after we fix the double coercion problem. */
7151 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7154 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7156 /* Detect immediately string literals as invalid non-type argument.
7157 This special-case is not needed for correctness (we would easily
7158 catch this later), but only to provide better diagnostic for this
7159 common user mistake. As suggested by DR 100, we do not mention
7160 linkage issues in the diagnostic as this is not the point. */
7161 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7163 if (complain
& tf_error
)
7164 error ("%qE is not a valid template argument for type %qT "
7165 "because string literals can never be used in this context",
7170 /* Add the ADDR_EXPR now for the benefit of
7171 value_dependent_expression_p. */
7172 if (TYPE_PTROBV_P (type
)
7173 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7175 expr
= decay_conversion (expr
, complain
);
7176 if (expr
== error_mark_node
)
7177 return error_mark_node
;
7180 /* If we are in a template, EXPR may be non-dependent, but still
7181 have a syntactic, rather than semantic, form. For example, EXPR
7182 might be a SCOPE_REF, rather than the VAR_DECL to which the
7183 SCOPE_REF refers. Preserving the qualifying scope is necessary
7184 so that access checking can be performed when the template is
7185 instantiated -- but here we need the resolved form so that we can
7186 convert the argument. */
7187 bool non_dep
= false;
7188 if (TYPE_REF_OBJ_P (type
)
7189 && has_value_dependent_address (expr
))
7190 /* If we want the address and it's value-dependent, don't fold. */;
7191 else if (processing_template_decl
7192 && is_nondependent_constant_expression (expr
))
7194 if (error_operand_p (expr
))
7195 return error_mark_node
;
7196 expr_type
= TREE_TYPE (expr
);
7198 /* If the argument is non-dependent, perform any conversions in
7199 non-dependent context as well. */
7200 processing_template_decl_sentinel
s (non_dep
);
7202 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7204 const bool val_dep_p
= value_dependent_expression_p (expr
);
7206 expr
= canonicalize_expr_argument (expr
, complain
);
7208 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7209 to a non-type argument of "nullptr". */
7210 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7211 expr
= fold_simple (convert (type
, expr
));
7213 /* In C++11, integral or enumeration non-type template arguments can be
7214 arbitrary constant expressions. Pointer and pointer to
7215 member arguments can be general constant expressions that evaluate
7216 to a null value, but otherwise still need to be of a specific form. */
7217 if (cxx_dialect
>= cxx11
)
7219 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7220 /* A PTRMEM_CST is already constant, and a valid template
7221 argument for a parameter of pointer to member type, we just want
7222 to leave it in that form rather than lower it to a
7224 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7225 || cxx_dialect
>= cxx17
)
7227 /* C++17: A template-argument for a non-type template-parameter shall
7228 be a converted constant expression (8.20) of the type of the
7229 template-parameter. */
7230 expr
= build_converted_constant_expr (type
, expr
, complain
);
7231 if (expr
== error_mark_node
)
7232 /* Make sure we return NULL_TREE only if we have really issued
7233 an error, as described above. */
7234 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7235 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7237 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7240 expr
= maybe_constant_value (expr
, NULL_TREE
,
7241 /*manifestly_const_eval=*/true);
7242 expr
= convert_from_reference (expr
);
7244 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7246 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7247 /*manifestly_const_eval=*/true);
7248 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7249 : null_member_pointer_value_p (folded
))
7254 if (TYPE_REF_P (type
))
7255 expr
= mark_lvalue_use (expr
);
7257 expr
= mark_rvalue_use (expr
);
7259 /* HACK: Due to double coercion, we can get a
7260 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7261 which is the tree that we built on the first call (see
7262 below when coercing to reference to object or to reference to
7263 function). We just strip everything and get to the arg.
7264 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7266 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7268 tree probe_type
, probe
= expr
;
7269 if (REFERENCE_REF_P (probe
))
7270 probe
= TREE_OPERAND (probe
, 0);
7271 probe_type
= TREE_TYPE (probe
);
7272 if (TREE_CODE (probe
) == NOP_EXPR
)
7274 /* ??? Maybe we could use convert_from_reference here, but we
7275 would need to relax its constraints because the NOP_EXPR
7276 could actually change the type to something more cv-qualified,
7277 and this is not folded by convert_from_reference. */
7278 tree addr
= TREE_OPERAND (probe
, 0);
7279 if (TYPE_REF_P (probe_type
)
7280 && TREE_CODE (addr
) == ADDR_EXPR
7281 && TYPE_PTR_P (TREE_TYPE (addr
))
7282 && (same_type_ignoring_top_level_qualifiers_p
7283 (TREE_TYPE (probe_type
),
7284 TREE_TYPE (TREE_TYPE (addr
)))))
7286 expr
= TREE_OPERAND (addr
, 0);
7287 expr_type
= TREE_TYPE (probe_type
);
7292 /* [temp.arg.nontype]/5, bullet 1
7294 For a non-type template-parameter of integral or enumeration type,
7295 integral promotions (_conv.prom_) and integral conversions
7296 (_conv.integral_) are applied. */
7297 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7298 || TREE_CODE (type
) == REAL_TYPE
)
7300 if (cxx_dialect
< cxx11
)
7302 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7303 t
= maybe_constant_value (t
);
7304 if (t
!= error_mark_node
)
7308 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7309 return error_mark_node
;
7311 /* Notice that there are constant expressions like '4 % 0' which
7312 do not fold into integer constants. */
7313 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7315 if (complain
& tf_error
)
7317 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7318 if (!require_potential_constant_expression (expr
))
7319 expr
= error_mark_node
;
7321 expr
= cxx_constant_value (expr
);
7322 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7323 inform (loc
, "in template argument for type %qT", type
);
7324 if (expr
== error_mark_node
)
7326 /* else cxx_constant_value complained but gave us
7327 a real constant, so go ahead. */
7328 if (!CONSTANT_CLASS_P (expr
))
7330 /* Some assemble time constant expressions like
7331 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7332 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7333 as we can emit them into .rodata initializers of
7334 variables, yet they can't fold into an INTEGER_CST at
7335 compile time. Refuse them here. */
7336 gcc_checking_assert (reduced_constant_expression_p (expr
));
7337 error_at (loc
, "template argument %qE for type %qT not "
7338 "a compile-time constant", expr
, type
);
7346 /* Avoid typedef problems. */
7347 if (TREE_TYPE (expr
) != type
)
7348 expr
= fold_convert (type
, expr
);
7350 /* [temp.arg.nontype]/5, bullet 2
7352 For a non-type template-parameter of type pointer to object,
7353 qualification conversions (_conv.qual_) and the array-to-pointer
7354 conversion (_conv.array_) are applied. */
7355 else if (TYPE_PTROBV_P (type
))
7357 tree decayed
= expr
;
7359 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7360 decay_conversion or an explicit cast. If it's a problematic cast,
7361 we'll complain about it below. */
7362 if (TREE_CODE (expr
) == NOP_EXPR
)
7366 if (TREE_CODE (probe
) == ADDR_EXPR
7367 && TYPE_PTR_P (TREE_TYPE (probe
)))
7370 expr_type
= TREE_TYPE (expr
);
7374 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7376 A template-argument for a non-type, non-template template-parameter
7377 shall be one of: [...]
7379 -- the name of a non-type template-parameter;
7380 -- the address of an object or function with external linkage, [...]
7381 expressed as "& id-expression" where the & is optional if the name
7382 refers to a function or array, or if the corresponding
7383 template-parameter is a reference.
7385 Here, we do not care about functions, as they are invalid anyway
7386 for a parameter of type pointer-to-object. */
7389 /* Non-type template parameters are OK. */
7391 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7392 /* Null pointer values are OK in C++11. */;
7393 else if (TREE_CODE (expr
) != ADDR_EXPR
7394 && !INDIRECT_TYPE_P (expr_type
))
7395 /* Other values, like integer constants, might be valid
7396 non-type arguments of some other type. */
7397 return error_mark_node
;
7398 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7403 expr
= perform_qualification_conversions (type
, expr
);
7404 if (expr
== error_mark_node
)
7405 return error_mark_node
;
7407 /* [temp.arg.nontype]/5, bullet 3
7409 For a non-type template-parameter of type reference to object, no
7410 conversions apply. The type referred to by the reference may be more
7411 cv-qualified than the (otherwise identical) type of the
7412 template-argument. The template-parameter is bound directly to the
7413 template-argument, which must be an lvalue. */
7414 else if (TYPE_REF_OBJ_P (type
))
7416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7418 return error_mark_node
;
7420 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7422 if (complain
& tf_error
)
7423 error ("%qE is not a valid template argument for type %qT "
7424 "because of conflicts in cv-qualification", expr
, type
);
7428 if (!lvalue_p (expr
))
7430 if (complain
& tf_error
)
7431 error ("%qE is not a valid template argument for type %qT "
7432 "because it is not an lvalue", expr
, type
);
7436 /* [temp.arg.nontype]/1
7438 A template-argument for a non-type, non-template template-parameter
7439 shall be one of: [...]
7441 -- the address of an object or function with external linkage. */
7442 if (INDIRECT_REF_P (expr
)
7443 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7445 expr
= TREE_OPERAND (expr
, 0);
7448 if (complain
& tf_error
)
7449 error ("%q#D is not a valid template argument for type %qT "
7450 "because a reference variable does not have a constant "
7451 "address", expr
, type
);
7456 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7457 /* OK, dependent reference. We don't want to ask whether a DECL is
7458 itself value-dependent, since what we want here is its address. */;
7461 expr
= build_address (expr
);
7463 if (invalid_tparm_referent_p (type
, expr
, complain
))
7467 if (!same_type_p (type
, TREE_TYPE (expr
)))
7468 expr
= build_nop (type
, expr
);
7470 /* [temp.arg.nontype]/5, bullet 4
7472 For a non-type template-parameter of type pointer to function, only
7473 the function-to-pointer conversion (_conv.func_) is applied. If the
7474 template-argument represents a set of overloaded functions (or a
7475 pointer to such), the matching function is selected from the set
7477 else if (TYPE_PTRFN_P (type
))
7479 /* If the argument is a template-id, we might not have enough
7480 context information to decay the pointer. */
7481 if (!type_unknown_p (expr_type
))
7483 expr
= decay_conversion (expr
, complain
);
7484 if (expr
== error_mark_node
)
7485 return error_mark_node
;
7488 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7489 /* Null pointer values are OK in C++11. */
7490 return perform_qualification_conversions (type
, expr
);
7492 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7493 if (!expr
|| expr
== error_mark_node
)
7496 /* [temp.arg.nontype]/5, bullet 5
7498 For a non-type template-parameter of type reference to function, no
7499 conversions apply. If the template-argument represents a set of
7500 overloaded functions, the matching function is selected from the set
7502 else if (TYPE_REFFN_P (type
))
7504 if (TREE_CODE (expr
) == ADDR_EXPR
)
7506 if (complain
& tf_error
)
7508 error ("%qE is not a valid template argument for type %qT "
7509 "because it is a pointer", expr
, type
);
7510 inform (input_location
, "try using %qE instead",
7511 TREE_OPERAND (expr
, 0));
7516 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7517 if (!expr
|| expr
== error_mark_node
)
7520 /* [temp.arg.nontype]/5, bullet 6
7522 For a non-type template-parameter of type pointer to member function,
7523 no conversions apply. If the template-argument represents a set of
7524 overloaded member functions, the matching member function is selected
7525 from the set (_over.over_). */
7526 else if (TYPE_PTRMEMFUNC_P (type
))
7528 expr
= instantiate_type (type
, expr
, tf_none
);
7529 if (expr
== error_mark_node
)
7530 return error_mark_node
;
7532 /* [temp.arg.nontype] bullet 1 says the pointer to member
7533 expression must be a pointer-to-member constant. */
7535 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7538 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7539 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7540 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7541 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7543 /* [temp.arg.nontype]/5, bullet 7
7545 For a non-type template-parameter of type pointer to data member,
7546 qualification conversions (_conv.qual_) are applied. */
7547 else if (TYPE_PTRDATAMEM_P (type
))
7549 /* [temp.arg.nontype] bullet 1 says the pointer to member
7550 expression must be a pointer-to-member constant. */
7552 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7555 expr
= perform_qualification_conversions (type
, expr
);
7556 if (expr
== error_mark_node
)
7559 else if (NULLPTR_TYPE_P (type
))
7561 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7563 if (complain
& tf_error
)
7564 error ("%qE is not a valid template argument for type %qT "
7565 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7570 else if (CLASS_TYPE_P (type
))
7572 /* Replace the argument with a reference to the corresponding template
7573 parameter object. */
7575 expr
= get_template_parm_object (expr
, complain
);
7576 if (expr
== error_mark_node
)
7579 /* A template non-type parameter must be one of the above. */
7583 /* Sanity check: did we actually convert the argument to the
7585 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7586 (type
, TREE_TYPE (expr
)));
7587 return convert_from_reference (expr
);
7590 /* Subroutine of coerce_template_template_parms, which returns 1 if
7591 PARM_PARM and ARG_PARM match using the rule for the template
7592 parameters of template template parameters. Both PARM and ARG are
7593 template parameters; the rest of the arguments are the same as for
7594 coerce_template_template_parms.
7597 coerce_template_template_parm (tree parm
,
7599 tsubst_flags_t complain
,
7603 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7604 || parm
== NULL_TREE
|| error_operand_p (parm
))
7607 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7610 switch (TREE_CODE (parm
))
7613 /* We encounter instantiations of templates like
7614 template <template <template <class> class> class TT>
7617 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7618 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7620 if (!coerce_template_template_parms
7621 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7627 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7628 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7629 /* Argument is a parameter pack but parameter is not. */
7634 /* The tsubst call is used to handle cases such as
7636 template <int> class C {};
7637 template <class T, template <T> class TT> class D {};
7640 i.e. the parameter list of TT depends on earlier parameters. */
7641 if (!uses_template_parms (TREE_TYPE (arg
)))
7643 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7644 if (!uses_template_parms (t
)
7645 && !same_type_p (t
, TREE_TYPE (arg
)))
7649 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7650 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7651 /* Argument is a parameter pack but parameter is not. */
7663 /* Coerce template argument list ARGLIST for use with template
7664 template-parameter TEMPL. */
7667 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7668 tsubst_flags_t complain
)
7670 /* Consider an example where a template template parameter declared as
7672 template <class T, class U = std::allocator<T> > class TT
7674 The template parameter level of T and U are one level larger than
7675 of TT. To proper process the default argument of U, say when an
7676 instantiation `TT<int>' is seen, we need to build the full
7677 arguments containing {int} as the innermost level. Outer levels,
7678 available when not appearing as default template argument, can be
7679 obtained from the arguments of the enclosing template.
7681 Suppose that TT is later substituted with std::vector. The above
7682 instantiation is `TT<int, std::allocator<T> >' with TT at
7683 level 1, and T at level 2, while the template arguments at level 1
7684 becomes {std::vector} and the inner level 2 is {int}. */
7686 tree outer
= DECL_CONTEXT (templ
);
7688 outer
= generic_targs_for (outer
);
7689 else if (current_template_parms
)
7691 /* This is an argument of the current template, so we haven't set
7692 DECL_CONTEXT yet. */
7693 tree relevant_template_parms
;
7695 /* Parameter levels that are greater than the level of the given
7696 template template parm are irrelevant. */
7697 relevant_template_parms
= current_template_parms
;
7698 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7699 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7700 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7702 outer
= template_parms_to_args (relevant_template_parms
);
7706 arglist
= add_to_template_args (outer
, arglist
);
7708 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7709 return coerce_template_parms (parmlist
, arglist
, templ
,
7711 /*require_all_args=*/true,
7712 /*use_default_args=*/true);
7715 /* A cache of template template parameters with match-all default
7717 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7719 /* T is a bound template template-parameter. Copy its arguments into default
7720 arguments of the template template-parameter's template parameters. */
7723 add_defaults_to_ttp (tree otmpl
)
7725 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7728 tree ntmpl
= copy_node (otmpl
);
7730 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7731 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7732 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7733 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7734 TYPE_NAME (ntype
) = ntmpl
;
7735 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7737 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7738 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7739 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7740 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7742 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7743 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7744 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7745 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7746 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7748 tree o
= TREE_VEC_ELT (vec
, i
);
7749 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7751 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7752 TREE_PURPOSE (n
) = any_targ_node
;
7756 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7760 /* ARG is a bound potential template template-argument, and PARGS is a list
7761 of arguments for the corresponding template template-parameter. Adjust
7762 PARGS as appropriate for application to ARG's template, and if ARG is a
7763 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7764 arguments to the template template parameter. */
7767 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7769 ++processing_template_decl
;
7770 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7771 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7773 /* When comparing two template template-parameters in partial ordering,
7774 rewrite the one currently being used as an argument to have default
7775 arguments for all parameters. */
7776 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7777 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7778 if (pargs
!= error_mark_node
)
7779 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7780 TYPE_TI_ARGS (arg
));
7785 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7786 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7787 /*require_all*/true,
7788 /*use_default*/true);
7790 --processing_template_decl
;
7794 /* Subroutine of unify for the case when PARM is a
7795 BOUND_TEMPLATE_TEMPLATE_PARM. */
7798 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7801 tree parmvec
= TYPE_TI_ARGS (parm
);
7802 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7804 /* The template template parm might be variadic and the argument
7805 not, so flatten both argument lists. */
7806 parmvec
= expand_template_argument_pack (parmvec
);
7807 argvec
= expand_template_argument_pack (argvec
);
7811 /* In keeping with P0522R0, adjust P's template arguments
7812 to apply to A's template; then flatten it again. */
7813 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7814 nparmvec
= expand_template_argument_pack (nparmvec
);
7816 if (unify (tparms
, targs
, nparmvec
, argvec
,
7817 UNIFY_ALLOW_NONE
, explain_p
))
7820 /* If the P0522 adjustment eliminated a pack expansion, deduce
7823 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7824 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7825 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7830 /* Deduce arguments T, i from TT<T> or TT<i>.
7831 We check each element of PARMVEC and ARGVEC individually
7832 rather than the whole TREE_VEC since they can have
7833 different number of elements, which is allowed under N2555. */
7835 int len
= TREE_VEC_LENGTH (parmvec
);
7837 /* Check if the parameters end in a pack, making them
7839 int parm_variadic_p
= 0;
7841 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7842 parm_variadic_p
= 1;
7844 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7845 /* If the template argument list of P contains a pack
7846 expansion that is not the last template argument, the
7847 entire template argument list is a non-deduced
7849 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7850 return unify_success (explain_p
);
7852 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7853 return unify_too_few_arguments (explain_p
,
7854 TREE_VEC_LENGTH (argvec
), len
);
7856 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7857 if (unify (tparms
, targs
,
7858 TREE_VEC_ELT (parmvec
, i
),
7859 TREE_VEC_ELT (argvec
, i
),
7860 UNIFY_ALLOW_NONE
, explain_p
))
7864 && unify_pack_expansion (tparms
, targs
,
7867 /*subr=*/true, explain_p
))
7874 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7875 template template parameters. Both PARM_PARMS and ARG_PARMS are
7876 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7879 Consider the example:
7880 template <class T> class A;
7881 template<template <class U> class TT> class B;
7883 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7884 the parameters to A, and OUTER_ARGS contains A. */
7887 coerce_template_template_parms (tree parm_parms
,
7889 tsubst_flags_t complain
,
7893 int nparms
, nargs
, i
;
7897 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7898 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7900 nparms
= TREE_VEC_LENGTH (parm_parms
);
7901 nargs
= TREE_VEC_LENGTH (arg_parms
);
7905 /* P0522R0: A template template-parameter P is at least as specialized as
7906 a template template-argument A if, given the following rewrite to two
7907 function templates, the function template corresponding to P is at
7908 least as specialized as the function template corresponding to A
7909 according to the partial ordering rules for function templates
7910 ([temp.func.order]). Given an invented class template X with the
7911 template parameter list of A (including default arguments):
7913 * Each of the two function templates has the same template parameters,
7914 respectively, as P or A.
7916 * Each function template has a single function parameter whose type is
7917 a specialization of X with template arguments corresponding to the
7918 template parameters from the respective function template where, for
7919 each template parameter PP in the template parameter list of the
7920 function template, a corresponding template argument AA is formed. If
7921 PP declares a parameter pack, then AA is the pack expansion
7922 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7924 If the rewrite produces an invalid type, then P is not at least as
7925 specialized as A. */
7927 /* So coerce P's args to apply to A's parms, and then deduce between A's
7928 args and the converted args. If that succeeds, A is at least as
7929 specialized as P, so they match.*/
7930 tree pargs
= template_parms_level_to_args (parm_parms
);
7931 pargs
= add_outermost_template_args (outer_args
, pargs
);
7932 ++processing_template_decl
;
7933 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7934 /*require_all*/true, /*use_default*/true);
7935 --processing_template_decl
;
7936 if (pargs
!= error_mark_node
)
7938 tree targs
= make_tree_vec (nargs
);
7939 tree aargs
= template_parms_level_to_args (arg_parms
);
7940 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7946 /* Determine whether we have a parameter pack at the end of the
7947 template template parameter's template parameter list. */
7948 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7950 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7952 if (error_operand_p (parm
))
7955 switch (TREE_CODE (parm
))
7959 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7964 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7974 && !(variadic_p
&& nargs
>= nparms
- 1))
7977 /* Check all of the template parameters except the parameter pack at
7978 the end (if any). */
7979 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7981 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7982 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7985 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7986 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7988 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7996 /* Check each of the template parameters in the template
7997 argument against the template parameter pack at the end of
7998 the template template parameter. */
7999 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8002 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8004 for (; i
< nargs
; ++i
)
8006 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8009 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8011 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8020 /* Verifies that the deduced template arguments (in TARGS) for the
8021 template template parameters (in TPARMS) represent valid bindings,
8022 by comparing the template parameter list of each template argument
8023 to the template parameter list of its corresponding template
8024 template parameter, in accordance with DR150. This
8025 routine can only be called after all template arguments have been
8026 deduced. It will return TRUE if all of the template template
8027 parameter bindings are okay, FALSE otherwise. */
8029 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8031 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8034 /* We're dealing with template parms in this process. */
8035 ++processing_template_decl
;
8037 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8039 for (i
= 0; i
< ntparms
; ++i
)
8041 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8042 tree targ
= TREE_VEC_ELT (targs
, i
);
8044 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8046 tree packed_args
= NULL_TREE
;
8049 if (ARGUMENT_PACK_P (targ
))
8051 /* Look inside the argument pack. */
8052 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8053 len
= TREE_VEC_LENGTH (packed_args
);
8056 for (idx
= 0; idx
< len
; ++idx
)
8058 tree targ_parms
= NULL_TREE
;
8061 /* Extract the next argument from the argument
8063 targ
= TREE_VEC_ELT (packed_args
, idx
);
8065 if (PACK_EXPANSION_P (targ
))
8066 /* Look at the pattern of the pack expansion. */
8067 targ
= PACK_EXPANSION_PATTERN (targ
);
8069 /* Extract the template parameters from the template
8071 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8072 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8073 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8074 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8076 /* Verify that we can coerce the template template
8077 parameters from the template argument to the template
8078 parameter. This requires an exact match. */
8080 && !coerce_template_template_parms
8081 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8096 --processing_template_decl
;
8100 /* Since type attributes aren't mangled, we need to strip them from
8101 template type arguments. */
8104 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8106 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8108 bool removed_attributes
= false;
8109 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8110 if (removed_attributes
8111 && (complain
& tf_warning
))
8112 warning (OPT_Wignored_attributes
,
8113 "ignoring attributes on template argument %qT", arg
);
8117 /* And from inside dependent non-type arguments like sizeof(Type). */
8120 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8122 if (!arg
|| arg
== error_mark_node
)
8124 bool removed_attributes
= false;
8125 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8126 if (removed_attributes
8127 && (complain
& tf_warning
))
8128 warning (OPT_Wignored_attributes
,
8129 "ignoring attributes in template argument %qE", arg
);
8133 // A template declaration can be substituted for a constrained
8134 // template template parameter only when the argument is more
8135 // constrained than the parameter.
8137 is_compatible_template_arg (tree parm
, tree arg
)
8139 tree parm_cons
= get_constraints (parm
);
8141 /* For now, allow constrained template template arguments
8142 and unconstrained template template parameters. */
8143 if (parm_cons
== NULL_TREE
)
8146 /* If the template parameter is constrained, we need to rewrite its
8147 constraints in terms of the ARG's template parameters. This ensures
8148 that all of the template parameter types will have the same depth.
8150 Note that this is only valid when coerce_template_template_parm is
8151 true for the innermost template parameters of PARM and ARG. In other
8152 words, because coercion is successful, this conversion will be valid. */
8153 tree new_args
= NULL_TREE
;
8156 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8157 new_args
= template_parms_level_to_args (aparms
);
8158 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8159 tf_none
, NULL_TREE
);
8160 if (parm_cons
== error_mark_node
)
8164 return weakly_subsumes (parm_cons
, new_args
, arg
);
8167 // Convert a placeholder argument into a binding to the original
8168 // parameter. The original parameter is saved as the TREE_TYPE of
8171 convert_wildcard_argument (tree parm
, tree arg
)
8173 TREE_TYPE (arg
) = parm
;
8177 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8178 because one of them is dependent. But we need to represent the
8179 conversion for the benefit of cp_tree_equal. */
8182 maybe_convert_nontype_argument (tree type
, tree arg
)
8184 /* Auto parms get no conversion. */
8185 if (type_uses_auto (type
))
8187 /* We don't need or want to add this conversion now if we're going to use the
8188 argument for deduction. */
8189 if (value_dependent_expression_p (arg
))
8192 type
= cv_unqualified (type
);
8193 tree argtype
= TREE_TYPE (arg
);
8194 if (same_type_p (type
, argtype
))
8197 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8198 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8202 /* Convert the indicated template ARG as necessary to match the
8203 indicated template PARM. Returns the converted ARG, or
8204 error_mark_node if the conversion was unsuccessful. Error and
8205 warning messages are issued under control of COMPLAIN. This
8206 conversion is for the Ith parameter in the parameter list. ARGS is
8207 the full set of template arguments deduced so far. */
8210 convert_template_argument (tree parm
,
8213 tsubst_flags_t complain
,
8219 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8221 if (parm
== error_mark_node
|| error_operand_p (arg
))
8222 return error_mark_node
;
8224 /* Trivially convert placeholders. */
8225 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8226 return convert_wildcard_argument (parm
, arg
);
8228 if (arg
== any_targ_node
)
8231 if (TREE_CODE (arg
) == TREE_LIST
8232 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8234 /* The template argument was the name of some
8235 member function. That's usually
8236 invalid, but static members are OK. In any
8237 case, grab the underlying fields/functions
8238 and issue an error later if required. */
8239 TREE_TYPE (arg
) = unknown_type_node
;
8244 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8245 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8246 || requires_tmpl_type
);
8248 /* When determining whether an argument pack expansion is a template,
8249 look at the pattern. */
8250 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8251 arg
= PACK_EXPANSION_PATTERN (arg
);
8253 /* Deal with an injected-class-name used as a template template arg. */
8254 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8256 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8257 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8259 if (cxx_dialect
>= cxx11
)
8260 /* OK under DR 1004. */;
8261 else if (complain
& tf_warning_or_error
)
8262 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8263 " used as template template argument", TYPE_NAME (arg
));
8264 else if (flag_pedantic_errors
)
8272 ((TREE_CODE (arg
) == TEMPLATE_DECL
8273 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8274 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8275 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8276 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8279 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8280 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8281 arg
= TYPE_STUB_DECL (arg
);
8283 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8285 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8286 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8288 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8290 if (complain
& tf_error
)
8291 error ("invalid use of destructor %qE as a type", orig_arg
);
8292 return error_mark_node
;
8295 permerror (input_location
,
8296 "to refer to a type member of a template parameter, "
8297 "use %<typename %E%>", orig_arg
);
8299 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8300 TREE_OPERAND (arg
, 1),
8306 if (is_type
!= requires_type
)
8310 if (complain
& tf_error
)
8312 error ("type/value mismatch at argument %d in template "
8313 "parameter list for %qD",
8317 /* The template argument is a type, but we're expecting
8319 inform (input_location
,
8320 " expected a constant of type %qT, got %qT",
8322 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8323 /* [temp.arg]/2: "In a template-argument, an ambiguity
8324 between a type-id and an expression is resolved to a
8325 type-id, regardless of the form of the corresponding
8326 template-parameter." So give the user a clue. */
8327 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8328 inform (input_location
, " ambiguous template argument "
8329 "for non-type template parameter is treated as "
8332 else if (requires_tmpl_type
)
8333 inform (input_location
,
8334 " expected a class template, got %qE", orig_arg
);
8336 inform (input_location
,
8337 " expected a type, got %qE", orig_arg
);
8340 return error_mark_node
;
8342 if (is_tmpl_type
^ requires_tmpl_type
)
8344 if (in_decl
&& (complain
& tf_error
))
8346 error ("type/value mismatch at argument %d in template "
8347 "parameter list for %qD",
8350 inform (input_location
,
8351 " expected a type, got %qT", DECL_NAME (arg
));
8353 inform (input_location
,
8354 " expected a class template, got %qT", orig_arg
);
8356 return error_mark_node
;
8359 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8360 /* We already did the appropriate conversion when packing args. */
8364 if (requires_tmpl_type
)
8366 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8367 /* The number of argument required is not known yet.
8368 Just accept it for now. */
8372 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8375 /* Strip alias templates that are equivalent to another
8377 arg
= get_underlying_template (arg
);
8378 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8380 if (coerce_template_template_parms (parmparm
, argparm
,
8386 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8388 if (val
!= error_mark_node
)
8390 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8391 val
= TREE_TYPE (val
);
8392 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8393 val
= make_pack_expansion (val
, complain
);
8398 if (in_decl
&& (complain
& tf_error
))
8400 error ("type/value mismatch at argument %d in "
8401 "template parameter list for %qD",
8403 inform (input_location
,
8404 " expected a template of type %qD, got %qT",
8408 val
= error_mark_node
;
8411 // Check that the constraints are compatible before allowing the
8413 if (val
!= error_mark_node
)
8414 if (!is_compatible_template_arg (parm
, arg
))
8416 if (in_decl
&& (complain
& tf_error
))
8418 error ("constraint mismatch at argument %d in "
8419 "template parameter list for %qD",
8421 inform (input_location
, " expected %qD but got %qD",
8424 val
= error_mark_node
;
8430 /* We only form one instance of each template specialization.
8431 Therefore, if we use a non-canonical variant (i.e., a
8432 typedef), any future messages referring to the type will use
8433 the typedef, which is confusing if those future uses do not
8434 themselves also use the typedef. */
8436 val
= canonicalize_type_argument (val
, complain
);
8440 tree t
= TREE_TYPE (parm
);
8442 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8443 > TMPL_ARGS_DEPTH (args
))
8444 /* We don't have enough levels of args to do any substitution. This
8445 can happen in the context of -fnew-ttp-matching. */;
8446 else if (tree a
= type_uses_auto (t
))
8448 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8449 if (t
== error_mark_node
)
8450 return error_mark_node
;
8453 t
= tsubst (t
, args
, complain
, in_decl
);
8455 if (invalid_nontype_parm_type_p (t
, complain
))
8456 return error_mark_node
;
8458 if (t
!= TREE_TYPE (parm
))
8459 t
= canonicalize_type_argument (t
, complain
);
8461 if (!type_dependent_expression_p (orig_arg
)
8462 && !uses_template_parms (t
))
8463 /* We used to call digest_init here. However, digest_init
8464 will report errors, which we don't want when complain
8465 is zero. More importantly, digest_init will try too
8466 hard to convert things: for example, `0' should not be
8467 converted to pointer type at this point according to
8468 the standard. Accepting this is not merely an
8469 extension, since deciding whether or not these
8470 conversions can occur is part of determining which
8471 function template to call, or whether a given explicit
8472 argument specification is valid. */
8473 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8476 val
= canonicalize_expr_argument (orig_arg
, complain
);
8477 val
= maybe_convert_nontype_argument (t
, val
);
8481 if (val
== NULL_TREE
)
8482 val
= error_mark_node
;
8483 else if (val
== error_mark_node
&& (complain
& tf_error
))
8484 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8485 "could not convert template argument %qE from %qT to %qT",
8486 orig_arg
, TREE_TYPE (orig_arg
), t
);
8488 if (INDIRECT_REF_P (val
))
8490 /* Reject template arguments that are references to built-in
8491 functions with no library fallbacks. */
8492 const_tree inner
= TREE_OPERAND (val
, 0);
8493 const_tree innertype
= TREE_TYPE (inner
);
8495 && TYPE_REF_P (innertype
)
8496 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8497 && TREE_OPERAND_LENGTH (inner
) > 0
8498 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8499 return error_mark_node
;
8502 if (TREE_CODE (val
) == SCOPE_REF
)
8504 /* Strip typedefs from the SCOPE_REF. */
8505 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8506 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8508 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8509 QUALIFIED_NAME_IS_TEMPLATE (val
));
8516 /* Coerces the remaining template arguments in INNER_ARGS (from
8517 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8518 Returns the coerced argument pack. PARM_IDX is the position of this
8519 parameter in the template parameter list. ARGS is the original
8520 template argument list. */
8522 coerce_template_parameter_pack (tree parms
,
8530 tsubst_flags_t complain
)
8532 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8533 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8536 tree packed_parms
= NULL_TREE
;
8538 if (arg_idx
> nargs
)
8541 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8543 /* When the template parameter is a non-type template parameter pack
8544 or template template parameter pack whose type or template
8545 parameters use parameter packs, we know exactly how many arguments
8546 we are looking for. Build a vector of the instantiated decls for
8547 these template parameters in PACKED_PARMS. */
8548 /* We can't use make_pack_expansion here because it would interpret a
8549 _DECL as a use rather than a declaration. */
8550 tree decl
= TREE_VALUE (parm
);
8551 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8552 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8553 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8554 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8556 TREE_VEC_LENGTH (args
)--;
8557 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8558 TREE_VEC_LENGTH (args
)++;
8560 if (packed_parms
== error_mark_node
)
8561 return error_mark_node
;
8563 /* If we're doing a partial instantiation of a member template,
8564 verify that all of the types used for the non-type
8565 template parameter pack are, in fact, valid for non-type
8566 template parameters. */
8568 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8570 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8571 for (j
= 0; j
< len
; ++j
)
8573 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8574 if (TREE_CODE (t
) == PARM_DECL
8575 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8576 return error_mark_node
;
8578 /* We don't know how many args we have yet, just
8579 use the unconverted ones for now. */
8583 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8585 /* Check if we have a placeholder pack, which indicates we're
8586 in the context of a introduction list. In that case we want
8587 to match this pack to the single placeholder. */
8588 else if (arg_idx
< nargs
8589 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8590 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8592 nargs
= arg_idx
+ 1;
8593 packed_args
= make_tree_vec (1);
8596 packed_args
= make_tree_vec (nargs
- arg_idx
);
8598 /* Convert the remaining arguments, which will be a part of the
8599 parameter pack "parm". */
8600 int first_pack_arg
= arg_idx
;
8601 for (; arg_idx
< nargs
; ++arg_idx
)
8603 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8604 tree actual_parm
= TREE_VALUE (parm
);
8605 int pack_idx
= arg_idx
- first_pack_arg
;
8609 /* Once we've packed as many args as we have types, stop. */
8610 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8612 else if (PACK_EXPANSION_P (arg
))
8613 /* We don't know how many args we have yet, just
8614 use the unconverted ones for now. */
8617 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8620 if (arg
== error_mark_node
)
8622 if (complain
& tf_error
)
8623 error ("template argument %d is invalid", arg_idx
+ 1);
8626 arg
= convert_template_argument (actual_parm
,
8627 arg
, new_args
, complain
, parm_idx
,
8629 if (arg
== error_mark_node
)
8631 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8634 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8635 && TREE_VEC_LENGTH (packed_args
) > 0)
8637 if (complain
& tf_error
)
8638 error ("wrong number of template arguments (%d, should be %d)",
8639 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8640 return error_mark_node
;
8643 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8644 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8645 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8648 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8649 TREE_CONSTANT (argument_pack
) = 1;
8652 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8654 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8655 TREE_VEC_LENGTH (packed_args
));
8656 return argument_pack
;
8659 /* Returns the number of pack expansions in the template argument vector
8663 pack_expansion_args_count (tree args
)
8668 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8670 tree elt
= TREE_VEC_ELT (args
, i
);
8671 if (elt
&& PACK_EXPANSION_P (elt
))
8677 /* Convert all template arguments to their appropriate types, and
8678 return a vector containing the innermost resulting template
8679 arguments. If any error occurs, return error_mark_node. Error and
8680 warning messages are issued under control of COMPLAIN.
8682 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8683 for arguments not specified in ARGS. Otherwise, if
8684 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8685 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8686 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8690 coerce_template_parms (tree parms
,
8693 tsubst_flags_t complain
,
8694 bool require_all_args
,
8695 bool use_default_args
)
8697 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8698 tree orig_inner_args
;
8701 tree new_inner_args
;
8703 /* When used as a boolean value, indicates whether this is a
8704 variadic template parameter list. Since it's an int, we can also
8705 subtract it from nparms to get the number of non-variadic
8708 int variadic_args_p
= 0;
8709 int post_variadic_parms
= 0;
8711 /* Adjustment to nparms for fixed parameter packs. */
8712 int fixed_pack_adjust
= 0;
8713 int fixed_packs
= 0;
8716 /* Likewise for parameters with default arguments. */
8719 if (args
== error_mark_node
)
8720 return error_mark_node
;
8722 nparms
= TREE_VEC_LENGTH (parms
);
8724 /* Determine if there are any parameter packs or default arguments. */
8725 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8727 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8729 ++post_variadic_parms
;
8730 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8732 if (TREE_PURPOSE (parm
))
8736 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8737 /* If there are no parameters that follow a parameter pack, we need to
8738 expand any argument packs so that we can deduce a parameter pack from
8739 some non-packed args followed by an argument pack, as in variadic85.C.
8740 If there are such parameters, we need to leave argument packs intact
8741 so the arguments are assigned properly. This can happen when dealing
8742 with a nested class inside a partial specialization of a class
8743 template, as in variadic92.C, or when deducing a template parameter pack
8744 from a sub-declarator, as in variadic114.C. */
8745 if (!post_variadic_parms
)
8746 inner_args
= expand_template_argument_pack (inner_args
);
8748 /* Count any pack expansion args. */
8749 variadic_args_p
= pack_expansion_args_count (inner_args
);
8751 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8752 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8753 || (nargs
< nparms
- variadic_p
8756 && (!use_default_args
8757 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8758 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8761 if (complain
& tf_error
)
8763 if (variadic_p
|| default_p
)
8765 nparms
-= variadic_p
+ default_p
;
8766 error ("wrong number of template arguments "
8767 "(%d, should be at least %d)", nargs
, nparms
);
8770 error ("wrong number of template arguments "
8771 "(%d, should be %d)", nargs
, nparms
);
8774 inform (DECL_SOURCE_LOCATION (in_decl
),
8775 "provided for %qD", in_decl
);
8778 return error_mark_node
;
8780 /* We can't pass a pack expansion to a non-pack parameter of an alias
8781 template (DR 1430). */
8783 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8784 || concept_definition_p (in_decl
))
8786 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8788 if (complain
& tf_error
)
8790 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8792 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8793 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8795 if (PACK_EXPANSION_P (arg
)
8796 && !template_parameter_pack_p (parm
))
8798 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8799 error_at (location_of (arg
),
8800 "pack expansion argument for non-pack parameter "
8801 "%qD of alias template %qD", parm
, in_decl
);
8803 error_at (location_of (arg
),
8804 "pack expansion argument for non-pack parameter "
8805 "%qD of concept %qD", parm
, in_decl
);
8806 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8813 return error_mark_node
;
8816 /* We need to evaluate the template arguments, even though this
8817 template-id may be nested within a "sizeof". */
8820 new_inner_args
= make_tree_vec (nparms
);
8821 new_args
= add_outermost_template_args (args
, new_inner_args
);
8822 int pack_adjust
= 0;
8823 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8828 /* Get the Ith template parameter. */
8829 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8831 if (parm
== error_mark_node
)
8833 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8837 /* Calculate the next argument. */
8838 if (arg_idx
< nargs
)
8839 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8843 if (template_parameter_pack_p (TREE_VALUE (parm
))
8844 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8845 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8847 /* Some arguments will be placed in the
8848 template parameter pack PARM. */
8849 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8850 inner_args
, arg_idx
,
8854 if (arg
== NULL_TREE
)
8856 /* We don't know how many args we have yet, just use the
8857 unconverted (and still packed) ones for now. */
8858 new_inner_args
= orig_inner_args
;
8863 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8865 /* Store this argument. */
8866 if (arg
== error_mark_node
)
8869 /* We are done with all of the arguments. */
8875 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8876 arg_idx
+= pack_adjust
;
8877 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8880 fixed_pack_adjust
+= pack_adjust
;
8888 if (PACK_EXPANSION_P (arg
))
8890 /* "If every valid specialization of a variadic template
8891 requires an empty template parameter pack, the template is
8892 ill-formed, no diagnostic required." So check that the
8893 pattern works with this parameter. */
8894 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8895 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8899 if (conv
== error_mark_node
)
8901 if (complain
& tf_error
)
8902 inform (input_location
, "so any instantiation with a "
8903 "non-empty parameter pack would be ill-formed");
8906 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8907 /* Recover from missing typename. */
8908 TREE_VEC_ELT (inner_args
, arg_idx
)
8909 = make_pack_expansion (conv
, complain
);
8911 /* We don't know how many args we have yet, just
8912 use the unconverted ones for now. */
8913 new_inner_args
= inner_args
;
8918 else if (require_all_args
)
8920 /* There must be a default arg in this case. */
8921 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8923 /* The position of the first default template argument,
8924 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8926 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8927 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8928 arg_idx
- pack_adjust
);
8933 if (arg
== error_mark_node
)
8935 if (complain
& tf_error
)
8936 error ("template argument %d is invalid", arg_idx
+ 1);
8940 /* This can occur if there was an error in the template
8941 parameter list itself (which we would already have
8942 reported) that we are trying to recover from, e.g., a class
8943 template with a parameter list such as
8944 template<typename..., typename> (cpp0x/variadic150.C). */
8947 /* This can also happen with a fixed parameter pack (71834). */
8948 if (arg_idx
>= nargs
)
8952 arg
= convert_template_argument (TREE_VALUE (parm
),
8953 arg
, new_args
, complain
,
8956 if (arg
== error_mark_node
)
8959 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8962 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8964 /* If we had fixed parameter packs, we didn't know how many arguments we
8965 actually needed earlier; now we do. */
8966 nparms
+= fixed_pack_adjust
;
8967 variadic_p
-= fixed_packs
;
8971 if (arg_idx
< nargs
)
8973 /* We had some pack expansion arguments that will only work if the packs
8974 are empty, but wait until instantiation time to complain.
8975 See variadic-ttp3.C. */
8977 /* Except that we can't provide empty packs to alias templates or
8978 concepts when there are no corresponding parameters. Basically,
8979 we can get here with this:
8981 template<typename T> concept C = true;
8983 template<typename... Args>
8987 When parsing C<Args...>, we try to form a concept check of
8988 C<?, Args...>. Without the extra check for substituting an empty
8989 pack past the last parameter, we can accept the check as valid.
8991 FIXME: This may be valid for alias templates (but I doubt it).
8993 FIXME: The error could be better also. */
8994 if (in_decl
&& concept_definition_p (in_decl
))
8996 if (complain
& tf_error
)
8997 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8998 "too many arguments");
8999 return error_mark_node
;
9002 int len
= nparms
+ (nargs
- arg_idx
);
9003 tree args
= make_tree_vec (len
);
9005 for (; i
< nparms
; ++i
)
9006 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9007 for (; i
< len
; ++i
, ++arg_idx
)
9008 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9009 arg_idx
- pack_adjust
);
9010 new_inner_args
= args
;
9015 gcc_assert (!(complain
& tf_error
) || seen_error ());
9016 return error_mark_node
;
9019 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9020 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9021 TREE_VEC_LENGTH (new_inner_args
));
9023 return new_inner_args
;
9026 /* Convert all template arguments to their appropriate types, and
9027 return a vector containing the innermost resulting template
9028 arguments. If any error occurs, return error_mark_node. Error and
9029 warning messages are not issued.
9031 Note that no function argument deduction is performed, and default
9032 arguments are used to fill in unspecified arguments. */
9034 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9036 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9039 /* Convert all template arguments to their appropriate type, and
9040 instantiate default arguments as needed. This returns a vector
9041 containing the innermost resulting template arguments, or
9042 error_mark_node if unsuccessful. */
9044 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9045 tsubst_flags_t complain
)
9047 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9050 /* Like coerce_template_parms. If PARMS represents all template
9051 parameters levels, this function returns a vector of vectors
9052 representing all the resulting argument levels. Note that in this
9053 case, only the innermost arguments are coerced because the
9054 outermost ones are supposed to have been coerced already.
9056 Otherwise, if PARMS represents only (the innermost) vector of
9057 parameters, this function returns a vector containing just the
9058 innermost resulting arguments. */
9061 coerce_innermost_template_parms (tree parms
,
9064 tsubst_flags_t complain
,
9065 bool require_all_args
,
9066 bool use_default_args
)
9068 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9069 int args_depth
= TMPL_ARGS_DEPTH (args
);
9072 if (parms_depth
> 1)
9074 coerced_args
= make_tree_vec (parms_depth
);
9078 for (level
= parms
, cur_depth
= parms_depth
;
9079 parms_depth
> 0 && level
!= NULL_TREE
;
9080 level
= TREE_CHAIN (level
), --cur_depth
)
9083 if (cur_depth
== args_depth
)
9084 l
= coerce_template_parms (TREE_VALUE (level
),
9085 args
, in_decl
, complain
,
9089 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9091 if (l
== error_mark_node
)
9092 return error_mark_node
;
9094 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9098 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9099 args
, in_decl
, complain
,
9102 return coerced_args
;
9105 /* Returns true if T is a wrapper to make a C++20 template parameter
9109 class_nttp_const_wrapper_p (tree t
)
9111 if (cxx_dialect
< cxx20
)
9113 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9114 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9115 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9118 /* Returns 1 if template args OT and NT are equivalent. */
9121 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9125 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9127 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9130 if (class_nttp_const_wrapper_p (nt
))
9131 nt
= TREE_OPERAND (nt
, 0);
9132 if (class_nttp_const_wrapper_p (ot
))
9133 ot
= TREE_OPERAND (ot
, 0);
9135 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9136 /* For member templates */
9137 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9138 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9139 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9140 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9141 PACK_EXPANSION_PATTERN (nt
))
9142 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9143 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9144 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9145 return cp_tree_equal (ot
, nt
);
9146 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9148 else if (TYPE_P (nt
) || TYPE_P (ot
))
9150 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9152 /* Don't treat an alias template specialization with dependent
9153 arguments as equivalent to its underlying type when used as a
9154 template argument; we need them to be distinct so that we
9155 substitute into the specialization arguments at instantiation
9156 time. And aliases can't be equivalent without being ==, so
9157 we don't need to look any deeper.
9159 During partial ordering, however, we need to treat them normally so
9160 that we can order uses of the same alias with different
9161 cv-qualification (79960). */
9163 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9166 return same_type_p (ot
, nt
);
9170 /* Try to treat a template non-type argument that has been converted
9171 to the parameter type as equivalent to one that hasn't yet. */
9172 for (enum tree_code code1
= TREE_CODE (ot
);
9173 CONVERT_EXPR_CODE_P (code1
)
9174 || code1
== NON_LVALUE_EXPR
;
9175 code1
= TREE_CODE (ot
))
9176 ot
= TREE_OPERAND (ot
, 0);
9178 for (enum tree_code code2
= TREE_CODE (nt
);
9179 CONVERT_EXPR_CODE_P (code2
)
9180 || code2
== NON_LVALUE_EXPR
;
9181 code2
= TREE_CODE (nt
))
9182 nt
= TREE_OPERAND (nt
, 0);
9184 return cp_tree_equal (ot
, nt
);
9188 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9189 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9190 NEWARG_PTR with the offending arguments if they are non-NULL. */
9193 comp_template_args (tree oldargs
, tree newargs
,
9194 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9199 if (oldargs
== newargs
)
9202 if (!oldargs
|| !newargs
)
9205 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9208 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9210 tree nt
= TREE_VEC_ELT (newargs
, i
);
9211 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9213 if (! template_args_equal (ot
, nt
, partial_order
))
9215 if (oldarg_ptr
!= NULL
)
9217 if (newarg_ptr
!= NULL
)
9226 comp_template_args_porder (tree oargs
, tree nargs
)
9228 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9231 /* Implement a freelist interface for objects of type T.
9233 Head is a separate object, rather than a regular member, so that we
9234 can define it as a GTY deletable pointer, which is highly
9235 desirable. A data member could be declared that way, but then the
9236 containing object would implicitly get GTY((user)), which would
9237 prevent us from instantiating freelists as global objects.
9238 Although this way we can create freelist global objects, they're
9239 such thin wrappers that instantiating temporaries at every use
9240 loses nothing and saves permanent storage for the freelist object.
9242 Member functions next, anew, poison and reinit have default
9243 implementations that work for most of the types we're interested
9244 in, but if they don't work for some type, they should be explicitly
9245 specialized. See the comments before them for requirements, and
9246 the example specializations for the tree_list_freelist. */
9247 template <typename T
>
9250 /* Return the next object in a chain. We could just do type
9251 punning, but if we access the object with its underlying type, we
9252 avoid strict-aliasing trouble. This needs only work between
9253 poison and reinit. */
9254 static T
*&next (T
*obj
) { return obj
->next
; }
9256 /* Return a newly allocated, uninitialized or minimally-initialized
9257 object of type T. Any initialization performed by anew should
9258 either remain across the life of the object and the execution of
9259 poison, or be redone by reinit. */
9260 static T
*anew () { return ggc_alloc
<T
> (); }
9262 /* Optionally scribble all over the bits holding the object, so that
9263 they become (mostly?) uninitialized memory. This is called while
9264 preparing to make the object part of the free list. */
9265 static void poison (T
*obj
) {
9266 T
*p ATTRIBUTE_UNUSED
= obj
;
9267 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9269 #ifdef ENABLE_GC_CHECKING
9270 /* Poison the data, to indicate the data is garbage. */
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9272 memset (p
, 0xa5, sizeof (*p
));
9274 /* Let valgrind know the object is free. */
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9277 /* Let valgrind know the next portion of the object is available,
9278 but uninitialized. */
9279 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9282 /* Bring an object that underwent at least one lifecycle after anew
9283 and before the most recent free and poison, back to a usable
9284 state, reinitializing whatever is needed for it to be
9285 functionally equivalent to an object just allocated and returned
9286 by anew. This may poison or clear the next field, used by
9287 freelist housekeeping after poison was called. */
9288 static void reinit (T
*obj
) {
9289 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9291 #ifdef ENABLE_GC_CHECKING
9292 memset (q
, 0xa5, sizeof (*q
));
9294 /* Let valgrind know the entire object is available, but
9296 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9299 /* Reference a GTY-deletable pointer that points to the first object
9300 in the free list proper. */
9303 /* Construct a freelist object chaining objects off of HEAD. */
9304 freelist (T
*&head
) : head(head
) {}
9306 /* Add OBJ to the free object list. The former head becomes OBJ's
9315 /* Take an object from the free list, if one is available, or
9316 allocate a new one. Objects taken from the free list should be
9317 regarded as filled with garbage, except for bits that are
9318 configured to be preserved across free and alloc. */
9333 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9334 want to allocate a TREE_LIST using the usual interface, and ensure
9335 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9336 build_tree_list logic in reinit, so this could go out of sync. */
9339 freelist
<tree_node
>::next (tree obj
)
9341 return TREE_CHAIN (obj
);
9345 freelist
<tree_node
>::anew ()
9347 return build_tree_list (NULL
, NULL
);
9351 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9353 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9354 tree p ATTRIBUTE_UNUSED
= obj
;
9355 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9356 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9358 #ifdef ENABLE_GC_CHECKING
9359 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9361 /* Poison the data, to indicate the data is garbage. */
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9363 memset (p
, 0xa5, size
);
9365 /* Let valgrind know the object is free. */
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9367 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9368 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9371 #ifdef ENABLE_GC_CHECKING
9372 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9373 /* Keep TREE_CHAIN functional. */
9374 TREE_SET_CODE (obj
, TREE_LIST
);
9376 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9381 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9383 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9385 #ifdef ENABLE_GC_CHECKING
9386 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9388 memset (obj
, 0, sizeof (tree_list
));
9391 /* Let valgrind know the entire object is available, but
9393 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9395 #ifdef ENABLE_GC_CHECKING
9396 TREE_SET_CODE (obj
, TREE_LIST
);
9398 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9402 /* Point to the first object in the TREE_LIST freelist. */
9403 static GTY((deletable
)) tree tree_list_freelist_head
;
9404 /* Return the/an actual TREE_LIST freelist. */
9405 static inline freelist
<tree_node
>
9406 tree_list_freelist ()
9408 return tree_list_freelist_head
;
9411 /* Point to the first object in the tinst_level freelist. */
9412 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9413 /* Return the/an actual tinst_level freelist. */
9414 static inline freelist
<tinst_level
>
9415 tinst_level_freelist ()
9417 return tinst_level_freelist_head
;
9420 /* Point to the first object in the pending_template freelist. */
9421 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9422 /* Return the/an actual pending_template freelist. */
9423 static inline freelist
<pending_template
>
9424 pending_template_freelist ()
9426 return pending_template_freelist_head
;
9429 /* Build the TREE_LIST object out of a split list, store it
9430 permanently, and return it. */
9432 tinst_level::to_list ()
9434 gcc_assert (split_list_p ());
9435 tree ret
= tree_list_freelist ().alloc ();
9436 TREE_PURPOSE (ret
) = tldcl
;
9437 TREE_VALUE (ret
) = targs
;
9440 gcc_assert (tree_list_p ());
9444 const unsigned short tinst_level::refcount_infinity
;
9446 /* Increment OBJ's refcount unless it is already infinite. */
9447 static tinst_level
*
9448 inc_refcount_use (tinst_level
*obj
)
9450 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9455 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9457 tinst_level::free (tinst_level
*obj
)
9459 if (obj
->tree_list_p ())
9460 tree_list_freelist ().free (obj
->get_node ());
9461 tinst_level_freelist ().free (obj
);
9464 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9465 OBJ's DECL and OBJ, and start over with the tinst_level object that
9466 used to be referenced by OBJ's NEXT. */
9468 dec_refcount_use (tinst_level
*obj
)
9471 && obj
->refcount
!= tinst_level::refcount_infinity
9472 && !--obj
->refcount
)
9474 tinst_level
*next
= obj
->next
;
9475 tinst_level::free (obj
);
9480 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9481 and of the former PTR. Omitting the second argument is equivalent
9482 to passing (T*)NULL; this is allowed because passing the
9483 zero-valued integral constant NULL confuses type deduction and/or
9484 overload resolution. */
9485 template <typename T
>
9487 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9490 ptr
= inc_refcount_use (obj
);
9491 dec_refcount_use (save
);
9495 add_pending_template (tree d
)
9497 tree ti
= (TYPE_P (d
)
9498 ? CLASSTYPE_TEMPLATE_INFO (d
)
9499 : DECL_TEMPLATE_INFO (d
));
9500 struct pending_template
*pt
;
9503 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9506 /* We are called both from instantiate_decl, where we've already had a
9507 tinst_level pushed, and instantiate_template, where we haven't.
9509 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9510 level
= !current_tinst_level
9511 || current_tinst_level
->maybe_get_node () != d
;
9514 push_tinst_level (d
);
9516 pt
= pending_template_freelist ().alloc ();
9519 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9520 if (last_pending_template
)
9521 last_pending_template
->next
= pt
;
9523 pending_templates
= pt
;
9525 last_pending_template
= pt
;
9527 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9534 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9535 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9536 documentation for TEMPLATE_ID_EXPR. */
9539 lookup_template_function (tree fns
, tree arglist
)
9541 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9542 return error_mark_node
;
9544 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9546 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9548 error ("%q#D is not a function template", fns
);
9549 return error_mark_node
;
9552 if (BASELINK_P (fns
))
9554 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9556 BASELINK_FUNCTIONS (fns
),
9561 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9564 /* Within the scope of a template class S<T>, the name S gets bound
9565 (in build_self_reference) to a TYPE_DECL for the class, not a
9566 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9567 or one of its enclosing classes, and that type is a template,
9568 return the associated TEMPLATE_DECL. Otherwise, the original
9571 Also handle the case when DECL is a TREE_LIST of ambiguous
9572 injected-class-names from different bases. */
9575 maybe_get_template_decl_from_type_decl (tree decl
)
9577 if (decl
== NULL_TREE
)
9580 /* DR 176: A lookup that finds an injected-class-name (10.2
9581 [class.member.lookup]) can result in an ambiguity in certain cases
9582 (for example, if it is found in more than one base class). If all of
9583 the injected-class-names that are found refer to specializations of
9584 the same class template, and if the name is followed by a
9585 template-argument-list, the reference refers to the class template
9586 itself and not a specialization thereof, and is not ambiguous. */
9587 if (TREE_CODE (decl
) == TREE_LIST
)
9589 tree t
, tmpl
= NULL_TREE
;
9590 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9592 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9595 else if (tmpl
!= elt
)
9598 if (tmpl
&& t
== NULL_TREE
)
9604 return (decl
!= NULL_TREE
9605 && DECL_SELF_REFERENCE_P (decl
)
9606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9607 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9610 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9611 parameters, find the desired type.
9613 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9615 IN_DECL, if non-NULL, is the template declaration we are trying to
9618 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9619 the class we are looking up.
9621 Issue error and warning messages under control of COMPLAIN.
9623 If the template class is really a local class in a template
9624 function, then the FUNCTION_CONTEXT is the function in which it is
9627 ??? Note that this function is currently called *twice* for each
9628 template-id: the first time from the parser, while creating the
9629 incomplete type (finish_template_type), and the second type during the
9630 real instantiation (instantiate_template_class). This is surely something
9631 that we want to avoid. It also causes some problems with argument
9632 coercion (see convert_nontype_argument for more information on this). */
9635 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9636 int entering_scope
, tsubst_flags_t complain
)
9638 tree templ
= NULL_TREE
, parmlist
;
9645 if (identifier_p (d1
))
9647 tree value
= innermost_non_namespace_value (d1
);
9648 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9653 push_decl_namespace (context
);
9654 templ
= lookup_name (d1
);
9655 templ
= maybe_get_template_decl_from_type_decl (templ
);
9657 pop_decl_namespace ();
9660 context
= DECL_CONTEXT (templ
);
9662 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9664 tree type
= TREE_TYPE (d1
);
9666 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9667 an implicit typename for the second A. Deal with it. */
9668 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9669 type
= TREE_TYPE (type
);
9671 if (CLASSTYPE_TEMPLATE_INFO (type
))
9673 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9674 d1
= DECL_NAME (templ
);
9677 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9678 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9680 templ
= TYPE_TI_TEMPLATE (d1
);
9681 d1
= DECL_NAME (templ
);
9683 else if (DECL_TYPE_TEMPLATE_P (d1
))
9686 d1
= DECL_NAME (templ
);
9687 context
= DECL_CONTEXT (templ
);
9689 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9692 d1
= DECL_NAME (templ
);
9695 /* Issue an error message if we didn't find a template. */
9698 if (complain
& tf_error
)
9699 error ("%qT is not a template", d1
);
9700 return error_mark_node
;
9703 if (TREE_CODE (templ
) != TEMPLATE_DECL
9704 /* Make sure it's a user visible template, if it was named by
9706 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9707 && !PRIMARY_TEMPLATE_P (templ
)))
9709 if (complain
& tf_error
)
9711 error ("non-template type %qT used as a template", d1
);
9713 error ("for template declaration %q+D", in_decl
);
9715 return error_mark_node
;
9718 complain
&= ~tf_user
;
9720 /* An alias that just changes the name of a template is equivalent to the
9721 other template, so if any of the arguments are pack expansions, strip
9722 the alias to avoid problems with a pack expansion passed to a non-pack
9723 alias template parameter (DR 1430). */
9724 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9725 templ
= get_underlying_template (templ
);
9727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9730 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9731 if (arglist2
== error_mark_node
9732 || (!uses_template_parms (arglist2
)
9733 && check_instantiated_args (templ
, arglist2
, complain
)))
9734 return error_mark_node
;
9736 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9741 tree template_type
= TREE_TYPE (templ
);
9744 tree found
= NULL_TREE
;
9747 int is_dependent_type
;
9748 int use_partial_inst_tmpl
= false;
9750 if (template_type
== error_mark_node
)
9751 /* An error occurred while building the template TEMPL, and a
9752 diagnostic has most certainly been emitted for that
9753 already. Let's propagate that error. */
9754 return error_mark_node
;
9756 gen_tmpl
= most_general_template (templ
);
9757 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9758 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9759 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9761 if (arg_depth
== 1 && parm_depth
> 1)
9763 /* We've been given an incomplete set of template arguments.
9766 template <class T> struct S1 {
9767 template <class U> struct S2 {};
9768 template <class U> struct S2<U*> {};
9771 we will be called with an ARGLIST of `U*', but the
9772 TEMPLATE will be `template <class T> template
9773 <class U> struct S1<T>::S2'. We must fill in the missing
9775 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9776 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9777 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9780 /* Now we should have enough arguments. */
9781 gcc_assert (parm_depth
== arg_depth
);
9783 /* From here on, we're only interested in the most general
9786 /* Calculate the BOUND_ARGS. These will be the args that are
9787 actually tsubst'd into the definition to create the
9789 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9791 /*require_all_args=*/true,
9792 /*use_default_args=*/true);
9794 if (arglist
== error_mark_node
)
9795 /* We were unable to bind the arguments. */
9796 return error_mark_node
;
9798 /* In the scope of a template class, explicit references to the
9799 template class refer to the type of the template, not any
9800 instantiation of it. For example, in:
9802 template <class T> class C { void f(C<T>); }
9804 the `C<T>' is just the same as `C'. Outside of the
9805 class, however, such a reference is an instantiation. */
9807 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9808 || currently_open_class (template_type
))
9810 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9812 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9813 return template_type
;
9816 /* If we already have this specialization, return it. */
9817 elt
.tmpl
= gen_tmpl
;
9819 elt
.spec
= NULL_TREE
;
9820 hash
= spec_hasher::hash (&elt
);
9821 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9826 /* If the template's constraints are not satisfied,
9827 then we cannot form a valid type.
9829 Note that the check is deferred until after the hash
9830 lookup. This prevents redundant checks on previously
9831 instantiated specializations. */
9833 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9834 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9836 if (complain
& tf_error
)
9838 auto_diagnostic_group d
;
9839 error ("template constraint failure for %qD", gen_tmpl
);
9840 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9842 return error_mark_node
;
9845 is_dependent_type
= uses_template_parms (arglist
);
9847 /* If the deduced arguments are invalid, then the binding
9849 if (!is_dependent_type
9850 && check_instantiated_args (gen_tmpl
,
9851 INNERMOST_TEMPLATE_ARGS (arglist
),
9853 return error_mark_node
;
9855 if (!is_dependent_type
9856 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9857 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9858 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9859 /* This occurs when the user has tried to define a tagged type
9860 in a scope that forbids it. We emitted an error during the
9861 parse. We didn't complete the bail out then, so here we
9863 return error_mark_node
;
9865 context
= DECL_CONTEXT (gen_tmpl
);
9866 if (context
&& TYPE_P (context
))
9868 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9869 context
= complete_type (context
);
9872 context
= tsubst (context
, arglist
, complain
, in_decl
);
9874 if (context
== error_mark_node
)
9875 return error_mark_node
;
9878 context
= global_namespace
;
9880 /* Create the type. */
9881 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9883 /* The user referred to a specialization of an alias
9884 template represented by GEN_TMPL.
9886 [temp.alias]/2 says:
9888 When a template-id refers to the specialization of an
9889 alias template, it is equivalent to the associated
9890 type obtained by substitution of its
9891 template-arguments for the template-parameters in the
9892 type-id of the alias template. */
9894 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9895 /* Note that the call above (by indirectly calling
9896 register_specialization in tsubst_decl) registers the
9897 TYPE_DECL representing the specialization of the alias
9898 template. So next time someone substitutes ARGLIST for
9899 the template parms into the alias template (GEN_TMPL),
9900 she'll get that TYPE_DECL back. */
9902 if (t
== error_mark_node
)
9905 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9907 if (!is_dependent_type
)
9909 set_current_access_from_decl (TYPE_NAME (template_type
));
9910 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9911 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9912 arglist
, complain
, in_decl
),
9913 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9914 arglist
, complain
, in_decl
),
9915 SCOPED_ENUM_P (template_type
), NULL
);
9917 if (t
== error_mark_node
)
9922 /* We don't want to call start_enum for this type, since
9923 the values for the enumeration constants may involve
9924 template parameters. And, no one should be interested
9925 in the enumeration constants for such a type. */
9926 t
= cxx_make_type (ENUMERAL_TYPE
);
9927 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9929 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9930 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9931 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9933 else if (CLASS_TYPE_P (template_type
))
9935 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9936 instantiated here. */
9937 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9939 t
= make_class_type (TREE_CODE (template_type
));
9940 CLASSTYPE_DECLARED_CLASS (t
)
9941 = CLASSTYPE_DECLARED_CLASS (template_type
);
9942 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9944 /* A local class. Make sure the decl gets registered properly. */
9945 if (context
== current_function_decl
)
9946 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9948 return error_mark_node
;
9950 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9951 /* This instantiation is another name for the primary
9952 template type. Set the TYPE_CANONICAL field
9954 TYPE_CANONICAL (t
) = template_type
;
9955 else if (any_template_arguments_need_structural_equality_p (arglist
))
9956 /* Some of the template arguments require structural
9957 equality testing, so this template class requires
9958 structural equality testing. */
9959 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9964 /* If we called start_enum or pushtag above, this information
9965 will already be set up. */
9966 type_decl
= TYPE_NAME (t
);
9969 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9971 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9972 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9973 DECL_SOURCE_LOCATION (type_decl
)
9974 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9977 if (CLASS_TYPE_P (template_type
))
9979 TREE_PRIVATE (type_decl
)
9980 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9981 TREE_PROTECTED (type_decl
)
9982 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9983 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9985 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9986 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9990 if (OVERLOAD_TYPE_P (t
)
9991 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9993 static const char *tags
[] = {"abi_tag", "may_alias"};
9995 for (unsigned ix
= 0; ix
!= 2; ix
++)
9998 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10001 TYPE_ATTRIBUTES (t
)
10002 = tree_cons (TREE_PURPOSE (attributes
),
10003 TREE_VALUE (attributes
),
10004 TYPE_ATTRIBUTES (t
));
10008 /* Let's consider the explicit specialization of a member
10009 of a class template specialization that is implicitly instantiated,
10014 template<class U> struct M {}; //#0
10019 struct S<int>::M<char> //#1
10023 [temp.expl.spec]/4 says this is valid.
10025 In this case, when we write:
10028 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10031 When we encounter #1, we want to store the partial instantiation
10032 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10034 For all cases other than this "explicit specialization of member of a
10035 class template", we just want to store the most general template into
10036 the CLASSTYPE_TI_TEMPLATE of M.
10038 This case of "explicit specialization of member of a class template"
10040 1/ the enclosing class is an instantiation of, and therefore not
10041 the same as, the context of the most general template, and
10042 2/ we aren't looking at the partial instantiation itself, i.e.
10043 the innermost arguments are not the same as the innermost parms of
10044 the most general template.
10046 So it's only when 1/ and 2/ happens that we want to use the partial
10047 instantiation of the member template in lieu of its most general
10050 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10051 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10052 /* the enclosing class must be an instantiation... */
10053 && CLASS_TYPE_P (context
)
10054 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10056 TREE_VEC_LENGTH (arglist
)--;
10057 ++processing_template_decl
;
10058 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10059 tree partial_inst_args
=
10060 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10061 arglist
, complain
, NULL_TREE
);
10062 --processing_template_decl
;
10063 TREE_VEC_LENGTH (arglist
)++;
10064 if (partial_inst_args
== error_mark_node
)
10065 return error_mark_node
;
10066 use_partial_inst_tmpl
=
10067 /*...and we must not be looking at the partial instantiation
10069 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10070 partial_inst_args
);
10073 if (!use_partial_inst_tmpl
)
10074 /* This case is easy; there are no member templates involved. */
10078 /* This is a full instantiation of a member template. Find
10079 the partial instantiation of which this is an instance. */
10081 /* Temporarily reduce by one the number of levels in the ARGLIST
10082 so as to avoid comparing the last set of arguments. */
10083 TREE_VEC_LENGTH (arglist
)--;
10084 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10085 TREE_VEC_LENGTH (arglist
)++;
10086 /* FOUND is either a proper class type, or an alias
10087 template specialization. In the later case, it's a
10088 TYPE_DECL, resulting from the substituting of arguments
10089 for parameters in the TYPE_DECL of the alias template
10090 done earlier. So be careful while getting the template
10092 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10094 : (TREE_CODE (found
) == TYPE_DECL
10095 ? DECL_TI_TEMPLATE (found
)
10096 : CLASSTYPE_TI_TEMPLATE (found
)));
10098 if (DECL_CLASS_TEMPLATE_P (found
)
10099 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10101 /* If this partial instantiation is specialized, we want to
10102 use it for hash table lookup. */
10104 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10105 hash
= spec_hasher::hash (&elt
);
10109 /* Build template info for the new specialization. */
10110 if (TYPE_ALIAS_P (t
))
10112 /* This is constructed during instantiation of the alias
10113 decl. But for member templates of template classes, that
10114 is not correct as we need to refer to the partially
10115 instantiated template, not the most general template.
10116 The incorrect knowledge will not have escaped this
10117 instantiation process, so we're good just updating the
10118 template_info we made then. */
10119 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10120 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
));
10121 if (TI_TEMPLATE (ti
) != found
)
10123 gcc_checking_assert (DECL_TI_TEMPLATE (found
) == TI_TEMPLATE (ti
));
10124 TI_TEMPLATE (ti
) = found
;
10128 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10131 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10132 gcc_checking_assert (*slot
== NULL
);
10133 entry
= ggc_alloc
<spec_entry
> ();
10137 /* Note this use of the partial instantiation so we can check it
10138 later in maybe_process_partial_specialization. */
10139 DECL_TEMPLATE_INSTANTIATIONS (found
)
10140 = tree_cons (arglist
, t
,
10141 DECL_TEMPLATE_INSTANTIATIONS (found
));
10143 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10144 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10145 /* Now that the type has been registered on the instantiations
10146 list, we set up the enumerators. Because the enumeration
10147 constants may involve the enumeration type itself, we make
10148 sure to register the type first, and then create the
10149 constants. That way, doing tsubst_expr for the enumeration
10150 constants won't result in recursive calls here; we'll find
10151 the instantiation and exit above. */
10152 tsubst_enum (template_type
, t
, arglist
);
10154 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10155 /* If the type makes use of template parameters, the
10156 code that generates debugging information will crash. */
10157 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10159 /* Possibly limit visibility based on template args. */
10160 TREE_PUBLIC (type_decl
) = 1;
10161 determine_visibility (type_decl
);
10163 inherit_targ_abi_tags (t
);
10169 /* Wrapper for lookup_template_class_1. */
10172 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10173 int entering_scope
, tsubst_flags_t complain
)
10176 timevar_push (TV_TEMPLATE_INST
);
10177 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10178 entering_scope
, complain
);
10179 timevar_pop (TV_TEMPLATE_INST
);
10183 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10186 lookup_template_variable (tree templ
, tree arglist
)
10188 if (flag_concepts
&& variable_concept_p (templ
))
10189 return build_concept_check (templ
, arglist
, tf_none
);
10191 /* The type of the expression is NULL_TREE since the template-id could refer
10192 to an explicit or partial specialization. */
10193 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10196 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10199 finish_template_variable (tree var
, tsubst_flags_t complain
)
10201 tree templ
= TREE_OPERAND (var
, 0);
10202 tree arglist
= TREE_OPERAND (var
, 1);
10204 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10205 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10207 templ
= most_general_template (templ
);
10208 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10209 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10211 /*use_default*/true);
10212 if (arglist
== error_mark_node
)
10213 return error_mark_node
;
10215 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10217 if (complain
& tf_error
)
10219 auto_diagnostic_group d
;
10220 error ("use of invalid variable template %qE", var
);
10221 diagnose_constraints (location_of (var
), templ
, arglist
);
10223 return error_mark_node
;
10226 return instantiate_template (templ
, arglist
, complain
);
10229 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10230 TARGS template args, and instantiate it if it's not dependent. */
10233 lookup_and_finish_template_variable (tree templ
, tree targs
,
10234 tsubst_flags_t complain
)
10236 templ
= lookup_template_variable (templ
, targs
);
10237 if (!any_dependent_template_arguments_p (targs
))
10239 templ
= finish_template_variable (templ
, complain
);
10243 return convert_from_reference (templ
);
10246 /* If the set of template parameters PARMS contains a template parameter
10247 at the given LEVEL and INDEX, then return this parameter. Otherwise
10248 return NULL_TREE. */
10251 corresponding_template_parameter (tree parms
, int level
, int index
)
10253 while (TMPL_PARMS_DEPTH (parms
) > level
)
10254 parms
= TREE_CHAIN (parms
);
10256 if (TMPL_PARMS_DEPTH (parms
) != level
10257 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10260 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10261 /* As in template_parm_to_arg. */
10262 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10265 t
= DECL_INITIAL (t
);
10267 gcc_assert (TEMPLATE_PARM_P (t
));
10271 /* Return the template parameter from PARMS that positionally corresponds
10272 to the template parameter PARM, or else return NULL_TREE. */
10275 corresponding_template_parameter (tree parms
, tree parm
)
10278 template_parm_level_and_index (parm
, &level
, &index
);
10279 return corresponding_template_parameter (parms
, level
, index
);
10283 struct pair_fn_data
10288 /* True when we should also visit template parameters that occur in
10289 non-deduced contexts. */
10290 bool include_nondeduced_p
;
10291 hash_set
<tree
> *visited
;
10294 /* Called from for_each_template_parm via walk_tree. */
10297 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10300 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10301 tree_fn_t fn
= pfd
->fn
;
10302 void *data
= pfd
->data
;
10303 tree result
= NULL_TREE
;
10305 #define WALK_SUBTREE(NODE) \
10308 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10309 pfd->include_nondeduced_p, \
10311 if (result) goto out; \
10315 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10319 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10320 WALK_SUBTREE (TYPE_CONTEXT (t
));
10322 switch (TREE_CODE (t
))
10325 if (TYPE_PTRMEMFUNC_P (t
))
10327 /* Fall through. */
10330 case ENUMERAL_TYPE
:
10331 if (!TYPE_TEMPLATE_INFO (t
))
10332 *walk_subtrees
= 0;
10334 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10338 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10339 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10343 /* Since we're not going to walk subtrees, we have to do this
10344 explicitly here. */
10345 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10346 /* Fall through. */
10348 case FUNCTION_TYPE
:
10349 /* Check the return type. */
10350 WALK_SUBTREE (TREE_TYPE (t
));
10352 /* Check the parameter types. Since default arguments are not
10353 instantiated until they are needed, the TYPE_ARG_TYPES may
10354 contain expressions that involve template parameters. But,
10355 no-one should be looking at them yet. And, once they're
10356 instantiated, they don't contain template parameters, so
10357 there's no point in looking at them then, either. */
10361 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10362 WALK_SUBTREE (TREE_VALUE (parm
));
10364 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10365 want walk_tree walking into them itself. */
10366 *walk_subtrees
= 0;
10369 if (flag_noexcept_type
)
10371 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10373 WALK_SUBTREE (TREE_PURPOSE (spec
));
10378 case DECLTYPE_TYPE
:
10379 case UNDERLYING_TYPE
:
10380 if (pfd
->include_nondeduced_p
10381 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10383 pfd
->include_nondeduced_p
,
10385 return error_mark_node
;
10386 *walk_subtrees
= false;
10389 case FUNCTION_DECL
:
10391 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10392 WALK_SUBTREE (DECL_TI_ARGS (t
));
10393 /* Fall through. */
10397 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10398 WALK_SUBTREE (DECL_INITIAL (t
));
10399 if (DECL_CONTEXT (t
)
10400 && pfd
->include_nondeduced_p
)
10401 WALK_SUBTREE (DECL_CONTEXT (t
));
10404 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10405 /* Record template parameters such as `T' inside `TT<T>'. */
10406 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10407 /* Fall through. */
10409 case TEMPLATE_TEMPLATE_PARM
:
10410 case TEMPLATE_TYPE_PARM
:
10411 case TEMPLATE_PARM_INDEX
:
10412 if (fn
&& (*fn
)(t
, data
))
10418 case TEMPLATE_DECL
:
10419 /* A template template parameter is encountered. */
10420 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10421 WALK_SUBTREE (TREE_TYPE (t
));
10423 /* Already substituted template template parameter */
10424 *walk_subtrees
= 0;
10427 case TYPENAME_TYPE
:
10428 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10429 partial instantiation. */
10430 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10431 *walk_subtrees
= 0;
10435 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10436 && pfd
->include_nondeduced_p
)
10437 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10441 case COMPONENT_REF
:
10442 /* If there's no type, then this thing must be some expression
10443 involving template parameters. */
10444 if (!fn
&& !TREE_TYPE (t
))
10445 return error_mark_node
;
10450 case IMPLICIT_CONV_EXPR
:
10451 case REINTERPRET_CAST_EXPR
:
10452 case CONST_CAST_EXPR
:
10453 case STATIC_CAST_EXPR
:
10454 case DYNAMIC_CAST_EXPR
:
10458 case PSEUDO_DTOR_EXPR
:
10460 return error_mark_node
;
10464 if (pfd
->include_nondeduced_p
)
10465 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10468 case REQUIRES_EXPR
:
10471 return error_mark_node
;
10473 /* Recursively walk the type of each constraint variable. */
10474 tree p
= TREE_OPERAND (t
, 0);
10477 WALK_SUBTREE (TREE_TYPE (p
));
10478 p
= TREE_CHAIN (p
);
10487 #undef WALK_SUBTREE
10489 /* We didn't find any template parameters we liked. */
10494 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10495 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10496 call FN with the parameter and the DATA.
10497 If FN returns nonzero, the iteration is terminated, and
10498 for_each_template_parm returns 1. Otherwise, the iteration
10499 continues. If FN never returns a nonzero value, the value
10500 returned by for_each_template_parm is 0. If FN is NULL, it is
10501 considered to be the function which always returns 1.
10503 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10504 parameters that occur in non-deduced contexts. When false, only
10505 visits those template parameters that can be deduced. */
10508 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10509 hash_set
<tree
> *visited
,
10510 bool include_nondeduced_p
,
10513 struct pair_fn_data pfd
;
10518 pfd
.any_fn
= any_fn
;
10520 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10522 /* Walk the tree. (Conceptually, we would like to walk without
10523 duplicates, but for_each_template_parm_r recursively calls
10524 for_each_template_parm, so we would need to reorganize a fair
10525 bit to use walk_tree_without_duplicates, so we keep our own
10528 pfd
.visited
= visited
;
10530 pfd
.visited
= new hash_set
<tree
>;
10531 result
= cp_walk_tree (&t
,
10532 for_each_template_parm_r
,
10539 delete pfd
.visited
;
10546 struct find_template_parameter_info
10548 explicit find_template_parameter_info (tree ctx_parms
)
10549 : parm_list (NULL_TREE
),
10550 ctx_parms (ctx_parms
),
10551 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10554 hash_set
<tree
> visited
;
10555 hash_set
<tree
> parms
;
10561 /* Appends the declaration of T to the list in DATA. */
10564 keep_template_parm (tree t
, void* data
)
10566 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10568 /* Template parameters declared within the expression are not part of
10569 the parameter mapping. For example, in this concept:
10571 template<typename T>
10572 concept C = requires { <expr> } -> same_as<int>;
10574 the return specifier same_as<int> declares a new decltype parameter
10575 that must not be part of the parameter mapping. The same is true
10576 for generic lambda parameters, lambda template parameters, etc. */
10579 template_parm_level_and_index (t
, &level
, &index
);
10580 if (level
> ftpi
->max_depth
)
10583 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10584 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10585 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10586 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10588 /* This template parameter might be an argument to a cached dependent
10589 specalization that was formed earlier inside some other template, in
10590 which case the parameter is not among the ones that are in-scope.
10591 Look in CTX_PARMS to find the corresponding in-scope template
10592 parameter, and use it instead. */
10593 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10596 /* Arguments like const T yield parameters like const T. This means that
10597 a template-id like X<T, const T> would yield two distinct parameters:
10598 T and const T. Adjust types to their unqualified versions. */
10600 t
= TYPE_MAIN_VARIANT (t
);
10601 if (!ftpi
->parms
.add (t
))
10602 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10607 /* Ensure that we recursively examine certain terms that are not normally
10608 visited in for_each_template_parm_r. */
10611 any_template_parm_r (tree t
, void *data
)
10613 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10615 #define WALK_SUBTREE(NODE) \
10618 for_each_template_parm (NODE, keep_template_parm, data, \
10619 &ftpi->visited, true, \
10620 any_template_parm_r); \
10624 /* A mention of a member alias/typedef is a use of all of its template
10625 arguments, including those from the enclosing class, so we don't use
10626 alias_template_specialization_p here. */
10627 if (TYPE_P (t
) && typedef_variant_p (t
))
10628 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10629 WALK_SUBTREE (TI_ARGS (tinfo
));
10631 switch (TREE_CODE (t
))
10633 case TEMPLATE_TYPE_PARM
:
10634 /* Type constraints of a placeholder type may contain parameters. */
10636 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10637 WALK_SUBTREE (constr
);
10640 case TEMPLATE_ID_EXPR
:
10641 /* Search through references to variable templates. */
10642 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10643 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10646 case TEMPLATE_PARM_INDEX
:
10648 /* A parameter or constraint variable may also depend on a template
10649 parameter without explicitly naming it. */
10650 WALK_SUBTREE (TREE_TYPE (t
));
10653 case TEMPLATE_DECL
:
10655 /* If T is a member template that shares template parameters with
10656 ctx_parms, we need to mark all those parameters for mapping. */
10657 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10658 tree cparms
= ftpi
->ctx_parms
;
10659 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10660 dparms
= TREE_CHAIN (dparms
);
10661 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10662 cparms
= TREE_CHAIN (cparms
);
10664 && (TREE_TYPE (TREE_VALUE (dparms
))
10665 != TREE_TYPE (TREE_VALUE (cparms
))))
10666 dparms
= TREE_CHAIN (dparms
),
10667 cparms
= TREE_CHAIN (cparms
);
10670 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10671 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10672 for (int i
= 0; i
< ddepth
; ++i
)
10673 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10680 /* Look in the parms and body. */
10681 tree fn
= lambda_function (t
);
10682 WALK_SUBTREE (TREE_TYPE (fn
));
10683 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10687 case IDENTIFIER_NODE
:
10688 if (IDENTIFIER_CONV_OP_P (t
))
10689 /* The conversion-type-id of a conversion operator may be dependent. */
10690 WALK_SUBTREE (TREE_TYPE (t
));
10697 /* Keep walking. */
10701 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10702 are the template parameters in scope. */
10705 find_template_parameters (tree t
, tree ctx_parms
)
10710 find_template_parameter_info
ftpi (ctx_parms
);
10711 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10712 /*include_nondeduced*/true, any_template_parm_r
);
10713 return ftpi
.parm_list
;
10716 /* Returns true if T depends on any template parameter. */
10719 uses_template_parms (tree t
)
10721 if (t
== NULL_TREE
)
10725 int saved_processing_template_decl
;
10727 saved_processing_template_decl
= processing_template_decl
;
10728 if (!saved_processing_template_decl
)
10729 processing_template_decl
= 1;
10731 dependent_p
= dependent_type_p (t
);
10732 else if (TREE_CODE (t
) == TREE_VEC
)
10733 dependent_p
= any_dependent_template_arguments_p (t
);
10734 else if (TREE_CODE (t
) == TREE_LIST
)
10735 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10736 || uses_template_parms (TREE_CHAIN (t
)));
10737 else if (TREE_CODE (t
) == TYPE_DECL
)
10738 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10739 else if (t
== error_mark_node
)
10740 dependent_p
= false;
10742 dependent_p
= value_dependent_expression_p (t
);
10744 processing_template_decl
= saved_processing_template_decl
;
10746 return dependent_p
;
10749 /* Returns true iff current_function_decl is an incompletely instantiated
10750 template. Useful instead of processing_template_decl because the latter
10751 is set to 0 during instantiate_non_dependent_expr. */
10754 in_template_function (void)
10756 tree fn
= current_function_decl
;
10758 ++processing_template_decl
;
10759 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10760 && DECL_TEMPLATE_INFO (fn
)
10761 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10762 --processing_template_decl
;
10766 /* Returns true if T depends on any template parameter with level LEVEL. */
10769 uses_template_parms_level (tree t
, int level
)
10771 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10772 /*include_nondeduced_p=*/true);
10775 /* Returns true if the signature of DECL depends on any template parameter from
10776 its enclosing class. */
10779 uses_outer_template_parms (tree decl
)
10781 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10784 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10785 &depth
, NULL
, /*include_nondeduced_p=*/true))
10787 if (PRIMARY_TEMPLATE_P (decl
)
10788 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10789 (DECL_TEMPLATE_PARMS (decl
)),
10790 template_parm_outer_level
,
10791 &depth
, NULL
, /*include_nondeduced_p=*/true))
10793 tree ci
= get_constraints (decl
);
10795 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10796 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10797 &depth
, NULL
, /*nondeduced*/true))
10802 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10803 ill-formed translation unit, i.e. a variable or function that isn't
10804 usable in a constant expression. */
10807 neglectable_inst_p (tree d
)
10809 return (d
&& DECL_P (d
)
10810 && !undeduced_auto_decl (d
)
10811 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10812 : decl_maybe_constant_var_p (d
)));
10815 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10816 neglectable and instantiated from within an erroneous instantiation. */
10819 limit_bad_template_recursion (tree decl
)
10821 struct tinst_level
*lev
= current_tinst_level
;
10822 int errs
= errorcount
+ sorrycount
;
10823 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10826 for (; lev
; lev
= lev
->next
)
10827 if (neglectable_inst_p (lev
->maybe_get_node ()))
10830 return (lev
&& errs
> lev
->errors
);
10833 static int tinst_depth
;
10834 extern int max_tinst_depth
;
10837 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10839 /* We're starting to instantiate D; record the template instantiation context
10840 at LOC for diagnostics and to restore it later. */
10843 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10845 struct tinst_level
*new_level
;
10847 if (tinst_depth
>= max_tinst_depth
)
10849 /* Tell error.c not to try to instantiate any templates. */
10851 fatal_error (input_location
,
10852 "template instantiation depth exceeds maximum of %d"
10853 " (use %<-ftemplate-depth=%> to increase the maximum)",
10858 /* If the current instantiation caused problems, don't let it instantiate
10859 anything else. Do allow deduction substitution and decls usable in
10860 constant expressions. */
10861 if (!targs
&& limit_bad_template_recursion (tldcl
))
10863 /* Avoid no_linkage_errors and unused function warnings for this
10865 TREE_NO_WARNING (tldcl
) = 1;
10869 /* When not -quiet, dump template instantiations other than functions, since
10870 announce_function will take care of those. */
10871 if (!quiet_flag
&& !targs
10872 && TREE_CODE (tldcl
) != TREE_LIST
10873 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10874 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10876 new_level
= tinst_level_freelist ().alloc ();
10877 new_level
->tldcl
= tldcl
;
10878 new_level
->targs
= targs
;
10879 new_level
->locus
= loc
;
10880 new_level
->errors
= errorcount
+ sorrycount
;
10881 new_level
->next
= NULL
;
10882 new_level
->refcount
= 0;
10883 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10884 set_refcount_ptr (current_tinst_level
, new_level
);
10887 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10888 depth_reached
= tinst_depth
;
10893 /* We're starting substitution of TMPL<ARGS>; record the template
10894 substitution context for diagnostics and to restore it later. */
10897 push_tinst_level (tree tmpl
, tree args
)
10899 return push_tinst_level_loc (tmpl
, args
, input_location
);
10902 /* We're starting to instantiate D; record INPUT_LOCATION and the
10903 template instantiation context for diagnostics and to restore it
10907 push_tinst_level (tree d
)
10909 return push_tinst_level_loc (d
, input_location
);
10912 /* Likewise, but record LOC as the program location. */
10915 push_tinst_level_loc (tree d
, location_t loc
)
10917 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10918 return push_tinst_level_loc (d
, NULL
, loc
);
10921 /* We're done instantiating this template; return to the instantiation
10925 pop_tinst_level (void)
10927 /* Restore the filename and line number stashed away when we started
10928 this instantiation. */
10929 input_location
= current_tinst_level
->locus
;
10930 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10934 /* We're instantiating a deferred template; restore the template
10935 instantiation context in which the instantiation was requested, which
10936 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10939 reopen_tinst_level (struct tinst_level
*level
)
10941 struct tinst_level
*t
;
10944 for (t
= level
; t
; t
= t
->next
)
10947 set_refcount_ptr (current_tinst_level
, level
);
10948 pop_tinst_level ();
10949 if (current_tinst_level
)
10950 current_tinst_level
->errors
= errorcount
+sorrycount
;
10951 return level
->maybe_get_node ();
10954 /* Returns the TINST_LEVEL which gives the original instantiation
10957 struct tinst_level
*
10958 outermost_tinst_level (void)
10960 struct tinst_level
*level
= current_tinst_level
;
10962 while (level
->next
)
10963 level
= level
->next
;
10967 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10968 vector of template arguments, as for tsubst.
10970 Returns an appropriate tsubst'd friend declaration. */
10973 tsubst_friend_function (tree decl
, tree args
)
10977 if (TREE_CODE (decl
) == FUNCTION_DECL
10978 && DECL_TEMPLATE_INSTANTIATION (decl
)
10979 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10980 /* This was a friend declared with an explicit template
10981 argument list, e.g.:
10983 friend void f<>(T);
10985 to indicate that f was a template instantiation, not a new
10986 function declaration. Now, we have to figure out what
10987 instantiation of what template. */
10989 tree template_id
, arglist
, fns
;
10992 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10994 /* Friend functions are looked up in the containing namespace scope.
10995 We must enter that scope, to avoid finding member functions of the
10996 current class with same name. */
10997 push_nested_namespace (ns
);
10998 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10999 tf_warning_or_error
, NULL_TREE
,
11000 /*integral_constant_expression_p=*/false);
11001 pop_nested_namespace (ns
);
11002 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11003 tf_warning_or_error
, NULL_TREE
);
11004 template_id
= lookup_template_function (fns
, arglist
);
11006 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11007 tmpl
= determine_specialization (template_id
, new_friend
,
11009 /*need_member_template=*/0,
11010 TREE_VEC_LENGTH (args
),
11012 return instantiate_template (tmpl
, new_args
, tf_error
);
11015 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11016 if (new_friend
== error_mark_node
)
11017 return error_mark_node
;
11019 /* The NEW_FRIEND will look like an instantiation, to the
11020 compiler, but is not an instantiation from the point of view of
11021 the language. For example, we might have had:
11023 template <class T> struct S {
11024 template <class U> friend void f(T, U);
11027 Then, in S<int>, template <class U> void f(int, U) is not an
11028 instantiation of anything. */
11030 DECL_USE_TEMPLATE (new_friend
) = 0;
11031 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11033 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11034 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11035 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11037 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11038 match in decls_match. */
11039 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11040 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11041 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11042 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11045 /* The mangled name for the NEW_FRIEND is incorrect. The function
11046 is not a template instantiation and should not be mangled like
11047 one. Therefore, we forget the mangling here; we'll recompute it
11048 later if we need it. */
11049 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11051 SET_DECL_RTL (new_friend
, NULL
);
11052 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11055 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11060 /* We must save some information from NEW_FRIEND before calling
11061 duplicate decls since that function will free NEW_FRIEND if
11063 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11064 tree new_friend_result_template_info
= NULL_TREE
;
11065 bool new_friend_is_defn
=
11066 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11067 (template_for_substitution (new_friend
)))
11069 tree not_tmpl
= new_friend
;
11071 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11073 /* This declaration is a `primary' template. */
11074 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11076 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11077 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11080 /* Inside pushdecl_namespace_level, we will push into the
11081 current namespace. However, the friend function should go
11082 into the namespace of the template. */
11083 ns
= decl_namespace_context (new_friend
);
11084 push_nested_namespace (ns
);
11085 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
11086 pop_nested_namespace (ns
);
11088 if (old_decl
== error_mark_node
)
11089 return error_mark_node
;
11091 if (old_decl
!= new_friend
)
11093 /* This new friend declaration matched an existing
11094 declaration. For example, given:
11096 template <class T> void f(T);
11097 template <class U> class C {
11098 template <class T> friend void f(T) {}
11101 the friend declaration actually provides the definition
11102 of `f', once C has been instantiated for some type. So,
11103 old_decl will be the out-of-class template declaration,
11104 while new_friend is the in-class definition.
11106 But, if `f' was called before this point, the
11107 instantiation of `f' will have DECL_TI_ARGS corresponding
11108 to `T' but not to `U', references to which might appear
11109 in the definition of `f'. Previously, the most general
11110 template for an instantiation of `f' was the out-of-class
11111 version; now it is the in-class version. Therefore, we
11112 run through all specialization of `f', adding to their
11113 DECL_TI_ARGS appropriately. In particular, they need a
11114 new set of outer arguments, corresponding to the
11115 arguments for this class instantiation.
11117 The same situation can arise with something like this:
11119 friend void f(int);
11120 template <class T> class C {
11121 friend void f(T) {}
11124 when `C<int>' is instantiated. Now, `f(int)' is defined
11127 if (!new_friend_is_defn
)
11128 /* On the other hand, if the in-class declaration does
11129 *not* provide a definition, then we don't want to alter
11130 existing definitions. We can just leave everything
11135 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11136 tree new_args
= TI_ARGS (new_friend_template_info
);
11138 /* Overwrite whatever template info was there before, if
11139 any, with the new template information pertaining to
11140 the declaration. */
11141 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11143 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11145 /* We should have called reregister_specialization in
11146 duplicate_decls. */
11147 gcc_assert (retrieve_specialization (new_template
,
11151 /* Instantiate it if the global has already been used. */
11152 if (DECL_ODR_USED (old_decl
))
11153 instantiate_decl (old_decl
, /*defer_ok=*/true,
11154 /*expl_inst_class_mem_p=*/false);
11160 /* Indicate that the old function template is a partial
11162 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11163 = new_friend_result_template_info
;
11165 gcc_assert (new_template
11166 == most_general_template (new_template
));
11167 gcc_assert (new_template
!= old_decl
);
11169 /* Reassign any specializations already in the hash table
11170 to the new more general template, and add the
11171 additional template args. */
11172 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11174 t
= TREE_CHAIN (t
))
11176 tree spec
= TREE_VALUE (t
);
11179 elt
.tmpl
= old_decl
;
11180 elt
.args
= DECL_TI_ARGS (spec
);
11181 elt
.spec
= NULL_TREE
;
11183 decl_specializations
->remove_elt (&elt
);
11185 DECL_TI_ARGS (spec
)
11186 = add_outermost_template_args (new_args
,
11187 DECL_TI_ARGS (spec
));
11189 register_specialization
11190 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11193 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11197 /* The information from NEW_FRIEND has been merged into OLD_DECL
11198 by duplicate_decls. */
11199 new_friend
= old_decl
;
11204 tree context
= DECL_CONTEXT (new_friend
);
11208 template <class T> class C {
11209 template <class U> friend void C1<U>::f (); // case 1
11210 friend void C2<T>::f (); // case 2
11212 we only need to make sure CONTEXT is a complete type for
11213 case 2. To distinguish between the two cases, we note that
11214 CONTEXT of case 1 remains dependent type after tsubst while
11215 this isn't true for case 2. */
11216 ++processing_template_decl
;
11217 dependent_p
= dependent_type_p (context
);
11218 --processing_template_decl
;
11221 && !complete_type_or_else (context
, NULL_TREE
))
11222 return error_mark_node
;
11224 if (COMPLETE_TYPE_P (context
))
11226 tree fn
= new_friend
;
11227 /* do_friend adds the TEMPLATE_DECL for any member friend
11228 template even if it isn't a member template, i.e.
11229 template <class T> friend A<T>::f();
11230 Look through it in that case. */
11231 if (TREE_CODE (fn
) == TEMPLATE_DECL
11232 && !PRIMARY_TEMPLATE_P (fn
))
11233 fn
= DECL_TEMPLATE_RESULT (fn
);
11234 /* Check to see that the declaration is really present, and,
11235 possibly obtain an improved declaration. */
11236 fn
= check_classfn (context
, fn
, NULL_TREE
);
11246 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11247 template arguments, as for tsubst.
11249 Returns an appropriate tsubst'd friend type or error_mark_node on
11253 tsubst_friend_class (tree friend_tmpl
, tree args
)
11257 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11259 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11260 return TREE_TYPE (tmpl
);
11263 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11264 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11265 push_nested_namespace (context
);
11268 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11269 push_nested_class (context
);
11272 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11273 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11275 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11277 /* The friend template has already been declared. Just
11278 check to see that the declarations match, and install any new
11279 default parameters. We must tsubst the default parameters,
11280 of course. We only need the innermost template parameters
11281 because that is all that redeclare_class_template will look
11283 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11284 > TMPL_ARGS_DEPTH (args
))
11286 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11287 args
, tf_warning_or_error
);
11288 location_t saved_input_location
= input_location
;
11289 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11290 tree cons
= get_constraints (tmpl
);
11291 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11292 input_location
= saved_input_location
;
11297 /* The friend template has not already been declared. In this
11298 case, the instantiation of the template class will cause the
11299 injection of this template into the namespace scope. */
11300 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11302 if (tmpl
!= error_mark_node
)
11304 /* The new TMPL is not an instantiation of anything, so we
11305 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11306 for the new type because that is supposed to be the
11307 corresponding template decl, i.e., TMPL. */
11308 DECL_USE_TEMPLATE (tmpl
) = 0;
11309 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11310 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11311 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11312 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11314 /* It is hidden. */
11315 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11316 DECL_ANTICIPATED (tmpl
)
11317 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11319 /* Substitute into and set the constraints on the new declaration. */
11320 if (tree ci
= get_constraints (friend_tmpl
))
11322 ++processing_template_decl
;
11323 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11324 DECL_FRIEND_CONTEXT (friend_tmpl
));
11325 --processing_template_decl
;
11326 set_constraints (tmpl
, ci
);
11329 /* Inject this template into the enclosing namspace scope. */
11330 tmpl
= pushdecl_namespace_level (tmpl
, true);
11334 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11335 pop_nested_namespace (context
);
11337 pop_nested_class ();
11339 return TREE_TYPE (tmpl
);
11342 /* Returns zero if TYPE cannot be completed later due to circularity.
11343 Otherwise returns one. */
11346 can_complete_type_without_circularity (tree type
)
11348 if (type
== NULL_TREE
|| type
== error_mark_node
)
11350 else if (COMPLETE_TYPE_P (type
))
11352 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11353 return can_complete_type_without_circularity (TREE_TYPE (type
));
11354 else if (CLASS_TYPE_P (type
)
11355 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11361 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11362 tsubst_flags_t
, tree
);
11364 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11365 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11368 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11369 tsubst_flags_t complain
, tree in_decl
)
11371 gcc_assert (ATTR_IS_DEPENDENT (t
));
11373 tree val
= TREE_VALUE (t
);
11374 if (val
== NULL_TREE
)
11375 /* Nothing to do. */;
11376 else if ((flag_openmp
|| flag_openmp_simd
)
11377 && is_attribute_p ("omp declare simd",
11378 get_attribute_name (t
)))
11380 tree clauses
= TREE_VALUE (val
);
11381 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11382 complain
, in_decl
);
11383 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11384 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11385 tree parms
= DECL_ARGUMENTS (*decl_p
);
11387 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11389 val
= build_tree_list (NULL_TREE
, clauses
);
11393 else if (flag_openmp
11394 && is_attribute_p ("omp declare variant base",
11395 get_attribute_name (t
)))
11397 ++cp_unevaluated_operand
;
11399 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11400 in_decl
, /*integral_constant_expression_p=*/false);
11401 --cp_unevaluated_operand
;
11402 tree chain
= TREE_CHAIN (val
);
11403 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11404 tree ctx
= copy_list (TREE_VALUE (val
));
11405 tree simd
= get_identifier ("simd");
11406 tree score
= get_identifier (" score");
11407 tree condition
= get_identifier ("condition");
11408 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11410 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11411 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11412 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11414 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11416 tree clauses
= TREE_VALUE (t2
);
11417 clauses
= tsubst_omp_clauses (clauses
,
11418 C_ORT_OMP_DECLARE_SIMD
, args
,
11419 complain
, in_decl
);
11420 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11421 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11422 TREE_VALUE (t2
) = clauses
;
11426 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11427 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11428 if (TREE_VALUE (t3
))
11431 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11432 && TREE_PURPOSE (t3
) != score
);
11433 tree v
= TREE_VALUE (t3
);
11434 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11436 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11437 v
= fold_non_dependent_expr (v
);
11438 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11439 || (TREE_PURPOSE (t3
) == score
11440 ? TREE_CODE (v
) != INTEGER_CST
11441 : !tree_fits_shwi_p (v
)))
11444 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11446 if (TREE_PURPOSE (t3
) == score
)
11447 error_at (loc
, "score argument must be "
11448 "constant integer expression");
11449 else if (allow_string
)
11450 error_at (loc
, "property must be constant "
11451 "integer expression or string "
11454 error_at (loc
, "property must be constant "
11455 "integer expression");
11458 else if (TREE_PURPOSE (t3
) == score
11459 && tree_int_cst_sgn (v
) < 0)
11462 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11464 error_at (loc
, "score argument must be "
11468 TREE_VALUE (t3
) = v
;
11473 val
= tree_cons (varid
, ctx
, chain
);
11475 /* If the first attribute argument is an identifier, don't
11476 pass it through tsubst. Attributes like mode, format,
11477 cleanup and several target specific attributes expect it
11479 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11482 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11483 /*integral_constant_expression_p=*/false);
11484 if (chain
!= TREE_CHAIN (val
))
11485 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11487 else if (PACK_EXPANSION_P (val
))
11489 /* An attribute pack expansion. */
11490 tree purp
= TREE_PURPOSE (t
);
11491 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11492 if (pack
== error_mark_node
)
11493 return error_mark_node
;
11494 int len
= TREE_VEC_LENGTH (pack
);
11495 tree list
= NULL_TREE
;
11497 for (int i
= 0; i
< len
; ++i
)
11499 tree elt
= TREE_VEC_ELT (pack
, i
);
11500 *q
= build_tree_list (purp
, elt
);
11501 q
= &TREE_CHAIN (*q
);
11506 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11507 /*integral_constant_expression_p=*/false);
11509 if (val
!= TREE_VALUE (t
))
11510 return build_tree_list (TREE_PURPOSE (t
), val
);
11514 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11515 unchanged or a new TREE_LIST chain. */
11518 tsubst_attributes (tree attributes
, tree args
,
11519 tsubst_flags_t complain
, tree in_decl
)
11521 tree last_dep
= NULL_TREE
;
11523 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11524 if (ATTR_IS_DEPENDENT (t
))
11527 attributes
= copy_list (attributes
);
11532 for (tree
*p
= &attributes
; *p
; )
11535 if (ATTR_IS_DEPENDENT (t
))
11537 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11542 p
= &TREE_CHAIN (*p
);
11543 *p
= TREE_CHAIN (t
);
11547 p
= &TREE_CHAIN (*p
);
11553 /* Apply any attributes which had to be deferred until instantiation
11554 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11555 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11558 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11559 tree args
, tsubst_flags_t complain
, tree in_decl
)
11561 tree last_dep
= NULL_TREE
;
11565 if (attributes
== NULL_TREE
)
11568 if (DECL_P (*decl_p
))
11570 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11572 p
= &DECL_ATTRIBUTES (*decl_p
);
11573 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11574 to our attributes parameter. */
11575 gcc_assert (*p
== attributes
);
11579 p
= &TYPE_ATTRIBUTES (*decl_p
);
11580 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11581 lookup_template_class_1, and should be preserved. */
11582 gcc_assert (*p
!= attributes
);
11584 p
= &TREE_CHAIN (*p
);
11587 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11588 if (ATTR_IS_DEPENDENT (t
))
11591 attributes
= copy_list (attributes
);
11598 tree late_attrs
= NULL_TREE
;
11599 tree
*q
= &late_attrs
;
11604 if (ATTR_IS_DEPENDENT (t
))
11606 *p
= TREE_CHAIN (t
);
11607 TREE_CHAIN (t
) = NULL_TREE
;
11608 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11610 q
= &TREE_CHAIN (*q
);
11613 p
= &TREE_CHAIN (t
);
11616 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11620 /* The template TMPL is being instantiated with the template arguments TARGS.
11621 Perform the access checks that we deferred when parsing the template. */
11624 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11627 deferred_access_check
*chk
;
11629 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11632 if (vec
<deferred_access_check
, va_gc
> *access_checks
11633 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11634 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11636 tree decl
= chk
->decl
;
11637 tree diag_decl
= chk
->diag_decl
;
11638 tree type_scope
= TREE_TYPE (chk
->binfo
);
11640 if (uses_template_parms (type_scope
))
11641 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11643 /* Make access check error messages point to the location
11644 of the use of the typedef. */
11645 iloc_sentinel
ils (chk
->loc
);
11646 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11647 decl
, diag_decl
, tf_warning_or_error
);
11652 instantiate_class_template_1 (tree type
)
11654 tree templ
, args
, pattern
, t
, member
;
11658 unsigned int saved_maximum_field_alignment
;
11661 if (type
== error_mark_node
)
11662 return error_mark_node
;
11664 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11665 || uses_template_parms (type
))
11668 /* Figure out which template is being instantiated. */
11669 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11670 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11672 /* Mark the type as in the process of being defined. */
11673 TYPE_BEING_DEFINED (type
) = 1;
11675 /* We may be in the middle of deferred access check. Disable
11677 deferring_access_check_sentinel
acs (dk_no_deferred
);
11679 /* Determine what specialization of the original template to
11681 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11682 if (t
== error_mark_node
)
11683 return error_mark_node
;
11686 /* This TYPE is actually an instantiation of a partial
11687 specialization. We replace the innermost set of ARGS with
11688 the arguments appropriate for substitution. For example,
11691 template <class T> struct S {};
11692 template <class T> struct S<T*> {};
11694 and supposing that we are instantiating S<int*>, ARGS will
11695 presently be {int*} -- but we need {int}. */
11696 pattern
= TREE_TYPE (t
);
11697 args
= TREE_PURPOSE (t
);
11701 pattern
= TREE_TYPE (templ
);
11702 args
= CLASSTYPE_TI_ARGS (type
);
11705 /* If the template we're instantiating is incomplete, then clearly
11706 there's nothing we can do. */
11707 if (!COMPLETE_TYPE_P (pattern
))
11709 /* We can try again later. */
11710 TYPE_BEING_DEFINED (type
) = 0;
11714 /* If we've recursively instantiated too many templates, stop. */
11715 if (! push_tinst_level (type
))
11718 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11719 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11721 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11722 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11723 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11724 fn_context
= error_mark_node
;
11726 push_to_top_level ();
11729 cp_unevaluated_operand
= 0;
11730 c_inhibit_evaluation_warnings
= 0;
11732 /* Use #pragma pack from the template context. */
11733 saved_maximum_field_alignment
= maximum_field_alignment
;
11734 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11736 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11738 /* Set the input location to the most specialized template definition.
11739 This is needed if tsubsting causes an error. */
11740 typedecl
= TYPE_MAIN_DECL (pattern
);
11741 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11742 DECL_SOURCE_LOCATION (typedecl
);
11744 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11745 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11746 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11747 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11748 if (ANON_AGGR_TYPE_P (pattern
))
11749 SET_ANON_AGGR_TYPE_P (type
);
11750 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11752 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11753 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11754 /* Adjust visibility for template arguments. */
11755 determine_visibility (TYPE_MAIN_DECL (type
));
11757 if (CLASS_TYPE_P (type
))
11758 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11760 pbinfo
= TYPE_BINFO (pattern
);
11762 /* We should never instantiate a nested class before its enclosing
11763 class; we need to look up the nested class by name before we can
11764 instantiate it, and that lookup should instantiate the enclosing
11766 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11767 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11769 base_list
= NULL_TREE
;
11770 if (BINFO_N_BASE_BINFOS (pbinfo
))
11776 /* We must enter the scope containing the type, as that is where
11777 the accessibility of types named in dependent bases are
11779 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11781 /* Substitute into each of the bases to determine the actual
11783 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11786 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11787 tree expanded_bases
= NULL_TREE
;
11790 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11793 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11794 args
, tf_error
, NULL_TREE
);
11795 if (expanded_bases
== error_mark_node
)
11798 len
= TREE_VEC_LENGTH (expanded_bases
);
11801 for (idx
= 0; idx
< len
; idx
++)
11803 if (expanded_bases
)
11804 /* Extract the already-expanded base class. */
11805 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11807 /* Substitute to figure out the base class. */
11808 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11811 if (base
== error_mark_node
)
11814 base_list
= tree_cons (access
, base
, base_list
);
11815 if (BINFO_VIRTUAL_P (pbase_binfo
))
11816 TREE_TYPE (base_list
) = integer_type_node
;
11820 /* The list is now in reverse order; correct that. */
11821 base_list
= nreverse (base_list
);
11824 pop_scope (pushed_scope
);
11826 /* Now call xref_basetypes to set up all the base-class
11828 xref_basetypes (type
, base_list
);
11830 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11831 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11832 args
, tf_error
, NULL_TREE
);
11833 fixup_attribute_variants (type
);
11835 /* Now that our base classes are set up, enter the scope of the
11836 class, so that name lookups into base classes, etc. will work
11837 correctly. This is precisely analogous to what we do in
11838 begin_class_definition when defining an ordinary non-template
11839 class, except we also need to push the enclosing classes. */
11840 push_nested_class (type
);
11842 /* Now members are processed in the order of declaration. */
11843 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11844 member
; member
= TREE_CHAIN (member
))
11846 tree t
= TREE_VALUE (member
);
11848 if (TREE_PURPOSE (member
))
11852 if (LAMBDA_TYPE_P (t
))
11853 /* A closure type for a lambda in an NSDMI or default argument.
11854 Ignore it; it will be regenerated when needed. */
11857 /* Build new CLASSTYPE_NESTED_UTDS. */
11858 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11859 && TYPE_LANG_SPECIFIC (t
)
11860 && CLASSTYPE_IS_TEMPLATE (t
));
11862 /* If the member is a class template, then -- even after
11863 substitution -- there may be dependent types in the
11864 template argument list for the class. We increment
11865 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11866 that function will assume that no types are dependent
11867 when outside of a template. */
11868 if (class_template_p
)
11869 ++processing_template_decl
;
11870 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11871 if (class_template_p
)
11872 --processing_template_decl
;
11873 if (newtag
== error_mark_node
)
11876 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11878 tree name
= TYPE_IDENTIFIER (t
);
11880 if (class_template_p
)
11881 /* Unfortunately, lookup_template_class sets
11882 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11883 instantiation (i.e., for the type of a member
11884 template class nested within a template class.)
11885 This behavior is required for
11886 maybe_process_partial_specialization to work
11887 correctly, but is not accurate in this case;
11888 the TAG is not an instantiation of anything.
11889 (The corresponding TEMPLATE_DECL is an
11890 instantiation, but the TYPE is not.) */
11891 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11893 /* Now, we call pushtag to put this NEWTAG into the scope of
11894 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11895 pushtag calling push_template_decl. We don't have to do
11896 this for enums because it will already have been done in
11899 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11900 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11903 else if (DECL_DECLARES_FUNCTION_P (t
))
11907 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11908 ++processing_template_decl
;
11909 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11910 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11911 --processing_template_decl
;
11912 set_current_access_from_decl (r
);
11913 finish_member_declaration (r
);
11914 /* Instantiate members marked with attribute used. */
11915 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11917 if (TREE_CODE (r
) == FUNCTION_DECL
11918 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11919 cp_check_omp_declare_reduction (r
);
11921 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11922 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11923 /* A closure type for a lambda in an NSDMI or default argument.
11924 Ignore it; it will be regenerated when needed. */;
11927 /* Build new TYPE_FIELDS. */
11928 if (TREE_CODE (t
) == STATIC_ASSERT
)
11929 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11930 /*integral_constant_expression_p=*/true);
11931 else if (TREE_CODE (t
) != CONST_DECL
)
11934 tree vec
= NULL_TREE
;
11937 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11938 /* The file and line for this declaration, to
11939 assist in error message reporting. Since we
11940 called push_tinst_level above, we don't need to
11942 input_location
= DECL_SOURCE_LOCATION (t
);
11944 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11945 ++processing_template_decl
;
11946 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11947 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11948 --processing_template_decl
;
11950 if (TREE_CODE (r
) == TREE_VEC
)
11952 /* A capture pack became multiple fields. */
11954 len
= TREE_VEC_LENGTH (vec
);
11957 for (int i
= 0; i
< len
; ++i
)
11960 r
= TREE_VEC_ELT (vec
, i
);
11965 [t]he initialization (and any associated
11966 side-effects) of a static data member does
11967 not occur unless the static data member is
11968 itself used in a way that requires the
11969 definition of the static data member to
11972 Therefore, we do not substitute into the
11973 initialized for the static data member here. */
11974 finish_static_data_member_decl
11976 /*init=*/NULL_TREE
,
11977 /*init_const_expr_p=*/false,
11978 /*asmspec_tree=*/NULL_TREE
,
11980 /* Instantiate members marked with attribute used. */
11981 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11984 else if (TREE_CODE (r
) == FIELD_DECL
)
11986 /* Determine whether R has a valid type and can be
11987 completed later. If R is invalid, then its type
11988 is replaced by error_mark_node. */
11989 tree rtype
= TREE_TYPE (r
);
11990 if (can_complete_type_without_circularity (rtype
))
11991 complete_type (rtype
);
11993 if (!complete_or_array_type_p (rtype
))
11995 /* If R's type couldn't be completed and
11996 it isn't a flexible array member (whose
11997 type is incomplete by definition) give
11999 cxx_incomplete_type_error (r
, rtype
);
12000 TREE_TYPE (r
) = error_mark_node
;
12002 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12003 && TYPE_DOMAIN (rtype
) == NULL_TREE
12004 && (TREE_CODE (type
) == UNION_TYPE
12005 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12007 error ("flexible array member %qD in union", r
);
12008 TREE_TYPE (r
) = error_mark_node
;
12010 else if (!verify_type_context (input_location
,
12011 TCTX_FIELD
, rtype
))
12012 TREE_TYPE (r
) = error_mark_node
;
12015 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
12016 such a thing will already have been added to the field
12017 list by tsubst_enum in finish_member_declaration in the
12018 CLASSTYPE_NESTED_UTDS case above. */
12019 if (!(TREE_CODE (r
) == TYPE_DECL
12020 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12021 && DECL_ARTIFICIAL (r
)))
12023 set_current_access_from_decl (r
);
12024 finish_member_declaration (r
);
12032 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12033 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12035 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12037 tree friend_type
= t
;
12038 bool adjust_processing_template_decl
= false;
12040 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12042 /* template <class T> friend class C; */
12043 friend_type
= tsubst_friend_class (friend_type
, args
);
12044 adjust_processing_template_decl
= true;
12046 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12048 /* template <class T> friend class C::D; */
12049 friend_type
= tsubst (friend_type
, args
,
12050 tf_warning_or_error
, NULL_TREE
);
12051 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12052 friend_type
= TREE_TYPE (friend_type
);
12053 adjust_processing_template_decl
= true;
12055 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12056 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12058 /* This could be either
12062 when dependent_type_p is false or
12064 template <class U> friend class T::C;
12067 /* Bump processing_template_decl in case this is something like
12068 template <class T> friend struct A<T>::B. */
12069 ++processing_template_decl
;
12070 friend_type
= tsubst (friend_type
, args
,
12071 tf_warning_or_error
, NULL_TREE
);
12072 if (dependent_type_p (friend_type
))
12073 adjust_processing_template_decl
= true;
12074 --processing_template_decl
;
12076 else if (uses_template_parms (friend_type
))
12077 /* friend class C<T>; */
12078 friend_type
= tsubst (friend_type
, args
,
12079 tf_warning_or_error
, NULL_TREE
);
12084 where C is already declared or
12086 friend class C<int>;
12088 We don't have to do anything in these cases. */
12090 if (adjust_processing_template_decl
)
12091 /* Trick make_friend_class into realizing that the friend
12092 we're adding is a template, not an ordinary class. It's
12093 important that we use make_friend_class since it will
12094 perform some error-checking and output cross-reference
12096 ++processing_template_decl
;
12098 if (friend_type
!= error_mark_node
)
12099 make_friend_class (type
, friend_type
, /*complain=*/false);
12101 if (adjust_processing_template_decl
)
12102 --processing_template_decl
;
12106 /* Build new DECL_FRIENDLIST. */
12109 /* The file and line for this declaration, to
12110 assist in error message reporting. Since we
12111 called push_tinst_level above, we don't need to
12113 input_location
= DECL_SOURCE_LOCATION (t
);
12115 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12117 ++processing_template_decl
;
12118 push_deferring_access_checks (dk_no_check
);
12121 r
= tsubst_friend_function (t
, args
);
12122 add_friend (type
, r
, /*complain=*/false);
12123 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12125 pop_deferring_access_checks ();
12126 --processing_template_decl
;
12134 /* Restore these before substituting into the lambda capture
12136 cp_unevaluated_operand
= saved_unevaluated_operand
;
12137 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12140 /* Set the file and line number information to whatever is given for
12141 the class itself. This puts error messages involving generated
12142 implicit functions at a predictable point, and the same point
12143 that would be used for non-template classes. */
12144 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12146 unreverse_member_declarations (type
);
12147 finish_struct_1 (type
);
12148 TYPE_BEING_DEFINED (type
) = 0;
12150 /* We don't instantiate default arguments for member functions. 14.7.1:
12152 The implicit instantiation of a class template specialization causes
12153 the implicit instantiation of the declarations, but not of the
12154 definitions or default arguments, of the class member functions,
12155 member classes, static data members and member templates.... */
12157 perform_instantiation_time_access_checks (pattern
, args
);
12158 perform_deferred_access_checks (tf_warning_or_error
);
12159 pop_nested_class ();
12160 maximum_field_alignment
= saved_maximum_field_alignment
;
12162 pop_from_top_level ();
12163 pop_tinst_level ();
12165 /* The vtable for a template class can be emitted in any translation
12166 unit in which the class is instantiated. When there is no key
12167 method, however, finish_struct_1 will already have added TYPE to
12168 the keyed_classes. */
12169 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12170 vec_safe_push (keyed_classes
, type
);
12175 /* Wrapper for instantiate_class_template_1. */
12178 instantiate_class_template (tree type
)
12181 timevar_push (TV_TEMPLATE_INST
);
12182 ret
= instantiate_class_template_1 (type
);
12183 timevar_pop (TV_TEMPLATE_INST
);
12188 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12194 else if (TYPE_P (t
))
12195 r
= tsubst (t
, args
, complain
, in_decl
);
12198 if (!(complain
& tf_warning
))
12199 ++c_inhibit_evaluation_warnings
;
12200 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12201 /*integral_constant_expression_p=*/true);
12202 if (!(complain
& tf_warning
))
12203 --c_inhibit_evaluation_warnings
;
12209 /* Given a function parameter pack TMPL_PARM and some function parameters
12210 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12211 and set *SPEC_P to point at the next point in the list. */
12214 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12216 /* Collect all of the extra "packed" parameters into an
12219 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12220 tree spec_parm
= *spec_p
;
12223 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12225 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12228 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12229 parmvec
= make_tree_vec (len
);
12230 spec_parm
= *spec_p
;
12231 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12233 tree elt
= spec_parm
;
12234 if (DECL_PACK_P (elt
))
12235 elt
= make_pack_expansion (elt
);
12236 TREE_VEC_ELT (parmvec
, i
) = elt
;
12239 /* Build the argument packs. */
12240 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12241 *spec_p
= spec_parm
;
12246 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12247 NONTYPE_ARGUMENT_PACK. */
12250 make_fnparm_pack (tree spec_parm
)
12252 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12255 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12256 pack expansion with no extra args, 2 if it has extra args, or 0
12257 if it is not a pack expansion. */
12260 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12262 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12263 /* We're being called before this happens in tsubst_pack_expansion. */
12264 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12265 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12266 if (i
>= TREE_VEC_LENGTH (vec
))
12268 tree elt
= TREE_VEC_ELT (vec
, i
);
12270 /* A decl pack is itself an expansion. */
12271 elt
= TREE_TYPE (elt
);
12272 if (!PACK_EXPANSION_P (elt
))
12274 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12280 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12283 make_argument_pack_select (tree arg_pack
, unsigned index
)
12285 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12287 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12288 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12293 /* This is a subroutine of tsubst_pack_expansion.
12295 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12296 mechanism to store the (non complete list of) arguments of the
12297 substitution and return a non substituted pack expansion, in order
12298 to wait for when we have enough arguments to really perform the
12302 use_pack_expansion_extra_args_p (tree parm_packs
,
12304 bool has_empty_arg
)
12306 /* If one pack has an expansion and another pack has a normal
12307 argument or if one pack has an empty argument and an another
12308 one hasn't then tsubst_pack_expansion cannot perform the
12309 substitution and need to fall back on the
12310 PACK_EXPANSION_EXTRA mechanism. */
12311 if (parm_packs
== NULL_TREE
)
12313 else if (has_empty_arg
)
12315 /* If all the actual packs are pack expansions, we can still
12316 subsitute directly. */
12317 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12319 tree a
= TREE_VALUE (p
);
12320 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12321 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12322 a
= ARGUMENT_PACK_ARGS (a
);
12323 if (TREE_VEC_LENGTH (a
) == 1)
12324 a
= TREE_VEC_ELT (a
, 0);
12325 if (PACK_EXPANSION_P (a
))
12332 bool has_expansion_arg
= false;
12333 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12335 bool has_non_expansion_arg
= false;
12336 for (tree parm_pack
= parm_packs
;
12338 parm_pack
= TREE_CHAIN (parm_pack
))
12340 tree arg
= TREE_VALUE (parm_pack
);
12342 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12344 /* We can't substitute a pack expansion with extra args into
12348 has_expansion_arg
= true;
12350 has_non_expansion_arg
= true;
12353 if (has_expansion_arg
&& has_non_expansion_arg
)
12359 /* [temp.variadic]/6 says that:
12361 The instantiation of a pack expansion [...]
12362 produces a list E1,E2, ..., En, where N is the number of elements
12363 in the pack expansion parameters.
12365 This subroutine of tsubst_pack_expansion produces one of these Ei.
12367 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12368 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12369 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12370 INDEX is the index 'i' of the element Ei to produce. ARGS,
12371 COMPLAIN, and IN_DECL are the same parameters as for the
12372 tsubst_pack_expansion function.
12374 The function returns the resulting Ei upon successful completion,
12375 or error_mark_node.
12377 Note that this function possibly modifies the ARGS parameter, so
12378 it's the responsibility of the caller to restore it. */
12381 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12384 tree args
/* This parm gets
12386 tsubst_flags_t complain
,
12390 bool ith_elem_is_expansion
= false;
12392 /* For each parameter pack, change the substitution of the parameter
12393 pack to the ith argument in its argument pack, then expand the
12395 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12397 tree parm
= TREE_PURPOSE (pack
);
12398 tree arg_pack
= TREE_VALUE (pack
);
12399 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12401 ith_elem_is_expansion
|=
12402 argument_pack_element_is_expansion_p (arg_pack
, index
);
12404 /* Select the Ith argument from the pack. */
12405 if (TREE_CODE (parm
) == PARM_DECL
12407 || TREE_CODE (parm
) == FIELD_DECL
)
12411 aps
= make_argument_pack_select (arg_pack
, index
);
12412 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12413 return error_mark_node
;
12414 register_local_specialization (aps
, parm
);
12417 aps
= retrieve_local_specialization (parm
);
12422 template_parm_level_and_index (parm
, &level
, &idx
);
12426 aps
= make_argument_pack_select (arg_pack
, index
);
12427 /* Update the corresponding argument. */
12428 TMPL_ARG (args
, level
, idx
) = aps
;
12431 /* Re-use the ARGUMENT_PACK_SELECT. */
12432 aps
= TMPL_ARG (args
, level
, idx
);
12434 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12437 /* Substitute into the PATTERN with the (possibly altered)
12439 if (pattern
== in_decl
)
12440 /* Expanding a fixed parameter pack from
12441 coerce_template_parameter_pack. */
12442 t
= tsubst_decl (pattern
, args
, complain
);
12443 else if (pattern
== error_mark_node
)
12444 t
= error_mark_node
;
12445 else if (!TYPE_P (pattern
))
12446 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12447 /*integral_constant_expression_p=*/false);
12449 t
= tsubst (pattern
, args
, complain
, in_decl
);
12451 /* If the Ith argument pack element is a pack expansion, then
12452 the Ith element resulting from the substituting is going to
12453 be a pack expansion as well. */
12454 if (ith_elem_is_expansion
)
12455 t
= make_pack_expansion (t
, complain
);
12460 /* When the unexpanded parameter pack in a fold expression expands to an empty
12461 sequence, the value of the expression is as follows; the program is
12462 ill-formed if the operator is not listed in this table.
12469 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12471 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12472 if (!FOLD_EXPR_MODIFY_P (t
))
12475 case TRUTH_ANDIF_EXPR
:
12476 return boolean_true_node
;
12477 case TRUTH_ORIF_EXPR
:
12478 return boolean_false_node
;
12479 case COMPOUND_EXPR
:
12485 if (complain
& tf_error
)
12486 error_at (location_of (t
),
12487 "fold of empty expansion over %O", code
);
12488 return error_mark_node
;
12491 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12492 form an expression that combines the two terms using the
12496 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12498 tree op
= FOLD_EXPR_OP (t
);
12499 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12501 // Handle compound assignment operators.
12502 if (FOLD_EXPR_MODIFY_P (t
))
12503 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12505 warning_sentinel
s(warn_parentheses
);
12508 case COMPOUND_EXPR
:
12509 return build_x_compound_expr (input_location
, left
, right
, complain
);
12511 return build_x_binary_op (input_location
, code
,
12512 left
, TREE_CODE (left
),
12513 right
, TREE_CODE (right
),
12519 /* Substitute ARGS into the pack of a fold expression T. */
12522 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12524 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12527 /* Substitute ARGS into the pack of a fold expression T. */
12530 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12532 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12535 /* Expand a PACK of arguments into a grouped as left fold.
12536 Given a pack containing elements A0, A1, ..., An and an
12537 operator @, this builds the expression:
12539 ((A0 @ A1) @ A2) ... @ An
12541 Note that PACK must not be empty.
12543 The operator is defined by the original fold expression T. */
12546 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12548 tree left
= TREE_VEC_ELT (pack
, 0);
12549 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12551 tree right
= TREE_VEC_ELT (pack
, i
);
12552 left
= fold_expression (t
, left
, right
, complain
);
12557 /* Substitute into a unary left fold expression. */
12560 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12563 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12564 if (pack
== error_mark_node
)
12565 return error_mark_node
;
12566 if (PACK_EXPANSION_P (pack
))
12568 tree r
= copy_node (t
);
12569 FOLD_EXPR_PACK (r
) = pack
;
12572 if (TREE_VEC_LENGTH (pack
) == 0)
12573 return expand_empty_fold (t
, complain
);
12575 return expand_left_fold (t
, pack
, complain
);
12578 /* Substitute into a binary left fold expression.
12580 Do ths by building a single (non-empty) vector of argumnts and
12581 building the expression from those elements. */
12584 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12587 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12588 if (pack
== error_mark_node
)
12589 return error_mark_node
;
12590 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12591 if (init
== error_mark_node
)
12592 return error_mark_node
;
12594 if (PACK_EXPANSION_P (pack
))
12596 tree r
= copy_node (t
);
12597 FOLD_EXPR_PACK (r
) = pack
;
12598 FOLD_EXPR_INIT (r
) = init
;
12602 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12603 TREE_VEC_ELT (vec
, 0) = init
;
12604 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12605 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12607 return expand_left_fold (t
, vec
, complain
);
12610 /* Expand a PACK of arguments into a grouped as right fold.
12611 Given a pack containing elementns A0, A1, ..., and an
12612 operator @, this builds the expression:
12614 A0@ ... (An-2 @ (An-1 @ An))
12616 Note that PACK must not be empty.
12618 The operator is defined by the original fold expression T. */
12621 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12623 // Build the expression.
12624 int n
= TREE_VEC_LENGTH (pack
);
12625 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12626 for (--n
; n
!= 0; --n
)
12628 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12629 right
= fold_expression (t
, left
, right
, complain
);
12634 /* Substitute into a unary right fold expression. */
12637 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12640 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12641 if (pack
== error_mark_node
)
12642 return error_mark_node
;
12643 if (PACK_EXPANSION_P (pack
))
12645 tree r
= copy_node (t
);
12646 FOLD_EXPR_PACK (r
) = pack
;
12649 if (TREE_VEC_LENGTH (pack
) == 0)
12650 return expand_empty_fold (t
, complain
);
12652 return expand_right_fold (t
, pack
, complain
);
12655 /* Substitute into a binary right fold expression.
12657 Do ths by building a single (non-empty) vector of arguments and
12658 building the expression from those elements. */
12661 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12664 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12665 if (pack
== error_mark_node
)
12666 return error_mark_node
;
12667 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12668 if (init
== error_mark_node
)
12669 return error_mark_node
;
12671 if (PACK_EXPANSION_P (pack
))
12673 tree r
= copy_node (t
);
12674 FOLD_EXPR_PACK (r
) = pack
;
12675 FOLD_EXPR_INIT (r
) = init
;
12679 int n
= TREE_VEC_LENGTH (pack
);
12680 tree vec
= make_tree_vec (n
+ 1);
12681 for (int i
= 0; i
< n
; ++i
)
12682 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12683 TREE_VEC_ELT (vec
, n
) = init
;
12685 return expand_right_fold (t
, vec
, complain
);
12688 /* Walk through the pattern of a pack expansion, adding everything in
12689 local_specializations to a list. */
12694 hash_set
<tree
> internal
;
12696 tsubst_flags_t complain
;
12698 el_data (tsubst_flags_t c
)
12699 : extra (NULL_TREE
), complain (c
) {}
12702 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12704 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12705 tree
*extra
= &data
.extra
;
12706 tsubst_flags_t complain
= data
.complain
;
12708 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12709 /* Remember local typedefs (85214). */
12710 tp
= &TYPE_NAME (*tp
);
12712 if (TREE_CODE (*tp
) == DECL_EXPR
)
12713 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12714 else if (tree spec
= retrieve_local_specialization (*tp
))
12716 if (data
.internal
.contains (*tp
))
12717 /* Don't mess with variables declared within the pattern. */
12719 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12721 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12722 tree args
= ARGUMENT_PACK_ARGS (spec
);
12723 if (TREE_VEC_LENGTH (args
) == 1)
12725 tree elt
= TREE_VEC_ELT (args
, 0);
12726 if (PACK_EXPANSION_P (elt
))
12727 elt
= PACK_EXPANSION_PATTERN (elt
);
12728 if (DECL_PACK_P (elt
))
12731 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12733 /* Handle lambda capture here, since we aren't doing any
12734 substitution now, and so tsubst_copy won't call
12735 process_outer_var_ref. */
12736 tree args
= ARGUMENT_PACK_ARGS (spec
);
12737 int len
= TREE_VEC_LENGTH (args
);
12738 for (int i
= 0; i
< len
; ++i
)
12740 tree arg
= TREE_VEC_ELT (args
, i
);
12742 if (outer_automatic_var_p (arg
))
12743 carg
= process_outer_var_ref (arg
, complain
);
12746 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12750 spec
= copy_node (spec
);
12751 args
= copy_node (args
);
12752 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12753 register_local_specialization (spec
, *tp
);
12755 TREE_VEC_ELT (args
, i
) = carg
;
12760 if (outer_automatic_var_p (spec
))
12761 spec
= process_outer_var_ref (spec
, complain
);
12762 *extra
= tree_cons (*tp
, spec
, *extra
);
12767 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12769 el_data
data (complain
);
12770 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12774 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12775 for use in PACK_EXPANSION_EXTRA_ARGS. */
12778 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12781 if (local_specializations
)
12782 if (tree locals
= extract_local_specs (pattern
, complain
))
12783 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12787 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12788 normal template args to ARGS. */
12791 add_extra_args (tree extra
, tree args
)
12793 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12795 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12797 /* The partial instantiation involved local declarations collected in
12798 extract_local_specs; map from the general template to our local
12800 tree gen
= TREE_PURPOSE (elt
);
12801 tree inst
= TREE_VALUE (elt
);
12803 if (tree local
= retrieve_local_specialization (inst
))
12805 /* else inst is already a full instantiation of the pack. */
12806 register_local_specialization (inst
, gen
);
12808 gcc_assert (!TREE_PURPOSE (extra
));
12809 extra
= TREE_VALUE (extra
);
12812 /* I think we should always be able to substitute dependent args into the
12813 pattern. If that turns out to be incorrect in some cases, enable the
12814 alternate code (and add complain/in_decl parms to this function). */
12815 gcc_checking_assert (!uses_template_parms (extra
));
12817 if (!uses_template_parms (extra
))
12819 gcc_unreachable ();
12820 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12821 args
= add_outermost_template_args (args
, extra
);
12825 args
= add_to_template_args (extra
, args
);
12829 /* Substitute ARGS into T, which is an pack expansion
12830 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12831 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12832 (if only a partial substitution could be performed) or
12833 ERROR_MARK_NODE if there was an error. */
12835 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12839 tree pack
, packs
= NULL_TREE
;
12840 bool unsubstituted_packs
= false;
12843 bool need_local_specializations
= false;
12846 gcc_assert (PACK_EXPANSION_P (t
));
12847 pattern
= PACK_EXPANSION_PATTERN (t
);
12849 /* Add in any args remembered from an earlier partial instantiation. */
12850 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12852 levels
= TMPL_ARGS_DEPTH (args
);
12854 /* Determine the argument packs that will instantiate the parameter
12855 packs used in the expansion expression. While we're at it,
12856 compute the number of arguments to be expanded and make sure it
12858 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12859 pack
= TREE_CHAIN (pack
))
12861 tree parm_pack
= TREE_VALUE (pack
);
12862 tree arg_pack
= NULL_TREE
;
12863 tree orig_arg
= NULL_TREE
;
12866 if (TREE_CODE (parm_pack
) == BASES
)
12868 gcc_assert (parm_pack
== pattern
);
12869 if (BASES_DIRECT (parm_pack
))
12870 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12875 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12876 args
, complain
, in_decl
,
12879 else if (builtin_pack_call_p (parm_pack
))
12881 if (parm_pack
!= pattern
)
12883 if (complain
& tf_error
)
12884 sorry ("%qE is not the entire pattern of the pack expansion",
12886 return error_mark_node
;
12888 return expand_builtin_pack_call (parm_pack
, args
,
12889 complain
, in_decl
);
12891 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12893 /* We know we have correct local_specializations if this
12894 expansion is at function scope, or if we're dealing with a
12895 local parameter in a requires expression; for the latter,
12896 tsubst_requires_expr set it up appropriately. */
12897 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12898 arg_pack
= retrieve_local_specialization (parm_pack
);
12900 /* We can't rely on local_specializations for a parameter
12901 name used later in a function declaration (such as in a
12902 late-specified return type). Even if it exists, it might
12903 have the wrong value for a recursive call. */
12904 need_local_specializations
= true;
12908 /* This parameter pack was used in an unevaluated context. Just
12909 make a dummy decl, since it's only used for its type. */
12910 ++cp_unevaluated_operand
;
12911 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12912 --cp_unevaluated_operand
;
12913 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12914 /* Partial instantiation of the parm_pack, we can't build
12915 up an argument pack yet. */
12916 arg_pack
= NULL_TREE
;
12918 arg_pack
= make_fnparm_pack (arg_pack
);
12920 else if (DECL_PACK_P (arg_pack
))
12921 /* This argument pack isn't fully instantiated yet. */
12922 arg_pack
= NULL_TREE
;
12924 else if (is_capture_proxy (parm_pack
))
12926 arg_pack
= retrieve_local_specialization (parm_pack
);
12927 if (DECL_PACK_P (arg_pack
))
12928 arg_pack
= NULL_TREE
;
12933 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12934 if (level
<= levels
)
12935 arg_pack
= TMPL_ARG (args
, level
, idx
);
12937 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12938 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12939 arg_pack
= NULL_TREE
;
12942 orig_arg
= arg_pack
;
12943 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12944 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12946 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12947 /* This can only happen if we forget to expand an argument
12948 pack somewhere else. Just return an error, silently. */
12950 result
= make_tree_vec (1);
12951 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12958 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12960 /* Don't bother trying to do a partial substitution with
12961 incomplete packs; we'll try again after deduction. */
12962 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12967 else if (len
!= my_len
)
12969 if (!(complain
& tf_error
))
12970 /* Fail quietly. */;
12971 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12972 error ("mismatched argument pack lengths while expanding %qT",
12975 error ("mismatched argument pack lengths while expanding %qE",
12977 return error_mark_node
;
12980 /* Keep track of the parameter packs and their corresponding
12982 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12983 TREE_TYPE (packs
) = orig_arg
;
12987 /* We can't substitute for this parameter pack. We use a flag as
12988 well as the missing_level counter because function parameter
12989 packs don't have a level. */
12990 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12991 unsubstituted_packs
= true;
12995 /* If the expansion is just T..., return the matching argument pack, unless
12996 we need to call convert_from_reference on all the elements. This is an
12997 important optimization; see c++/68422. */
12998 if (!unsubstituted_packs
12999 && TREE_PURPOSE (packs
) == pattern
)
13001 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13003 /* If the argument pack is a single pack expansion, pull it out. */
13004 if (TREE_VEC_LENGTH (args
) == 1
13005 && pack_expansion_args_count (args
))
13006 return TREE_VEC_ELT (args
, 0);
13008 /* Types need no adjustment, nor does sizeof..., and if we still have
13009 some pack expansion args we won't do anything yet. */
13010 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13011 || PACK_EXPANSION_SIZEOF_P (t
)
13012 || pack_expansion_args_count (args
))
13014 /* Also optimize expression pack expansions if we can tell that the
13015 elements won't have reference type. */
13016 tree type
= TREE_TYPE (pattern
);
13017 if (type
&& !TYPE_REF_P (type
)
13018 && !PACK_EXPANSION_P (type
)
13019 && !WILDCARD_TYPE_P (type
))
13021 /* Otherwise use the normal path so we get convert_from_reference. */
13024 /* We cannot expand this expansion expression, because we don't have
13025 all of the argument packs we need. */
13026 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
13028 /* We got some full packs, but we can't substitute them in until we
13029 have values for all the packs. So remember these until then. */
13031 t
= make_pack_expansion (pattern
, complain
);
13032 PACK_EXPANSION_EXTRA_ARGS (t
)
13033 = build_extra_args (pattern
, args
, complain
);
13037 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13038 type, so create our own local specializations map; the current map is
13039 either NULL or (in the case of recursive unification) might have
13040 bindings that we don't want to use or alter. */
13041 local_specialization_stack
lss (need_local_specializations
13042 ? lss_blank
: lss_nop
);
13044 if (unsubstituted_packs
)
13046 /* There were no real arguments, we're just replacing a parameter
13047 pack with another version of itself. Substitute into the
13048 pattern and return a PACK_EXPANSION_*. The caller will need to
13050 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13051 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13052 /*integral_constant_expression_p=*/false);
13054 t
= tsubst (pattern
, args
, complain
, in_decl
);
13055 t
= make_pack_expansion (t
, complain
);
13059 gcc_assert (len
>= 0);
13061 /* For each argument in each argument pack, substitute into the
13063 result
= make_tree_vec (len
);
13064 tree elem_args
= copy_template_args (args
);
13065 for (i
= 0; i
< len
; ++i
)
13067 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13069 elem_args
, complain
,
13071 TREE_VEC_ELT (result
, i
) = t
;
13072 if (t
== error_mark_node
)
13074 result
= error_mark_node
;
13079 /* Update ARGS to restore the substitution from parameter packs to
13080 their argument packs. */
13081 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13083 tree parm
= TREE_PURPOSE (pack
);
13085 if (TREE_CODE (parm
) == PARM_DECL
13087 || TREE_CODE (parm
) == FIELD_DECL
)
13088 register_local_specialization (TREE_TYPE (pack
), parm
);
13093 if (TREE_VALUE (pack
) == NULL_TREE
)
13096 template_parm_level_and_index (parm
, &level
, &idx
);
13098 /* Update the corresponding argument. */
13099 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13100 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13103 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13107 /* If the dependent pack arguments were such that we end up with only a
13108 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13109 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13110 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13111 return TREE_VEC_ELT (result
, 0);
13116 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13117 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13118 parameter packs; all parms generated from a function parameter pack will
13119 have the same DECL_PARM_INDEX. */
13122 get_pattern_parm (tree parm
, tree tmpl
)
13124 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13127 if (DECL_ARTIFICIAL (parm
))
13129 for (patparm
= DECL_ARGUMENTS (pattern
);
13130 patparm
; patparm
= DECL_CHAIN (patparm
))
13131 if (DECL_ARTIFICIAL (patparm
)
13132 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13137 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13138 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13139 gcc_assert (DECL_PARM_INDEX (patparm
)
13140 == DECL_PARM_INDEX (parm
));
13146 /* Make an argument pack out of the TREE_VEC VEC. */
13149 make_argument_pack (tree vec
)
13153 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13154 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13157 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13158 TREE_CONSTANT (pack
) = 1;
13160 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13164 /* Return an exact copy of template args T that can be modified
13168 copy_template_args (tree t
)
13170 if (t
== error_mark_node
)
13173 int len
= TREE_VEC_LENGTH (t
);
13174 tree new_vec
= make_tree_vec (len
);
13176 for (int i
= 0; i
< len
; ++i
)
13178 tree elt
= TREE_VEC_ELT (t
, i
);
13179 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13180 elt
= copy_template_args (elt
);
13181 TREE_VEC_ELT (new_vec
, i
) = elt
;
13184 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13185 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13190 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13193 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13196 /* Substitute into each of the arguments. */
13197 tree new_arg
= TYPE_P (orig_arg
)
13198 ? cxx_make_type (TREE_CODE (orig_arg
))
13199 : make_node (TREE_CODE (orig_arg
));
13201 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13202 args
, complain
, in_decl
);
13203 if (pack_args
== error_mark_node
)
13204 new_arg
= error_mark_node
;
13206 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13208 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13209 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13214 /* Substitute ARGS into the vector or list of template arguments T. */
13217 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13220 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13223 if (t
== error_mark_node
)
13224 return error_mark_node
;
13226 len
= TREE_VEC_LENGTH (t
);
13227 elts
= XALLOCAVEC (tree
, len
);
13229 for (i
= 0; i
< len
; i
++)
13231 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13234 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13235 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13236 else if (PACK_EXPANSION_P (orig_arg
))
13238 /* Substitute into an expansion expression. */
13239 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13241 if (TREE_CODE (new_arg
) == TREE_VEC
)
13242 /* Add to the expanded length adjustment the number of
13243 expanded arguments. We subtract one from this
13244 measurement, because the argument pack expression
13245 itself is already counted as 1 in
13246 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13247 the argument pack is empty. */
13248 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13250 else if (ARGUMENT_PACK_P (orig_arg
))
13251 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13253 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13255 if (new_arg
== error_mark_node
)
13256 return error_mark_node
;
13259 if (new_arg
!= orig_arg
)
13266 /* Make space for the expanded arguments coming from template
13268 t
= make_tree_vec (len
+ expanded_len_adjust
);
13269 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13270 arguments for a member template.
13271 In that case each TREE_VEC in ORIG_T represents a level of template
13272 arguments, and ORIG_T won't carry any non defaulted argument count.
13273 It will rather be the nested TREE_VECs that will carry one.
13274 In other words, ORIG_T carries a non defaulted argument count only
13275 if it doesn't contain any nested TREE_VEC. */
13276 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13278 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13279 count
+= expanded_len_adjust
;
13280 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13282 for (i
= 0, out
= 0; i
< len
; i
++)
13284 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13285 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13286 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13290 /* Now expand the template argument pack "in place". */
13291 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13292 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13296 TREE_VEC_ELT (t
, out
) = elts
[i
];
13304 /* Substitute ARGS into one level PARMS of template parameters. */
13307 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13309 if (parms
== error_mark_node
)
13310 return error_mark_node
;
13312 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13314 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13316 tree tuple
= TREE_VEC_ELT (parms
, i
);
13318 if (tuple
== error_mark_node
)
13321 TREE_VEC_ELT (new_vec
, i
) =
13322 tsubst_template_parm (tuple
, args
, complain
);
13328 /* Return the result of substituting ARGS into the template parameters
13329 given by PARMS. If there are m levels of ARGS and m + n levels of
13330 PARMS, then the result will contain n levels of PARMS. For
13331 example, if PARMS is `template <class T> template <class U>
13332 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13333 result will be `template <int*, double, class V>'. */
13336 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13338 tree r
= NULL_TREE
;
13341 /* When substituting into a template, we must set
13342 PROCESSING_TEMPLATE_DECL as the template parameters may be
13343 dependent if they are based on one-another, and the dependency
13344 predicates are short-circuit outside of templates. */
13345 ++processing_template_decl
;
13347 for (new_parms
= &r
;
13348 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13349 new_parms
= &(TREE_CHAIN (*new_parms
)),
13350 parms
= TREE_CHAIN (parms
))
13352 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13355 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13356 - TMPL_ARGS_DEPTH (args
)),
13357 new_vec
, NULL_TREE
);
13358 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13359 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13362 --processing_template_decl
;
13367 /* Return the result of substituting ARGS into one template parameter
13368 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13369 parameter and which TREE_PURPOSE is the default argument of the
13370 template parameter. */
13373 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13375 tree default_value
, parm_decl
;
13377 if (args
== NULL_TREE
13379 || t
== error_mark_node
)
13382 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13384 default_value
= TREE_PURPOSE (t
);
13385 parm_decl
= TREE_VALUE (t
);
13386 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13388 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13389 if (TREE_CODE (parm_decl
) == PARM_DECL
13390 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13391 parm_decl
= error_mark_node
;
13392 default_value
= tsubst_template_arg (default_value
, args
,
13393 complain
, NULL_TREE
);
13394 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13396 tree r
= build_tree_list (default_value
, parm_decl
);
13397 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13401 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13402 type T. If T is not an aggregate or enumeration type, it is
13403 handled as if by tsubst. IN_DECL is as for tsubst. If
13404 ENTERING_SCOPE is nonzero, T is the context for a template which
13405 we are presently tsubst'ing. Return the substituted value. */
13408 tsubst_aggr_type (tree t
,
13410 tsubst_flags_t complain
,
13412 int entering_scope
)
13414 if (t
== NULL_TREE
)
13417 switch (TREE_CODE (t
))
13420 if (TYPE_PTRMEMFUNC_P (t
))
13421 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13423 /* Fall through. */
13424 case ENUMERAL_TYPE
:
13426 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13432 /* In "sizeof(X<I>)" we need to evaluate "I". */
13435 /* First, determine the context for the type we are looking
13437 context
= TYPE_CONTEXT (t
);
13438 if (context
&& TYPE_P (context
))
13440 context
= tsubst_aggr_type (context
, args
, complain
,
13441 in_decl
, /*entering_scope=*/1);
13442 /* If context is a nested class inside a class template,
13443 it may still need to be instantiated (c++/33959). */
13444 context
= complete_type (context
);
13447 /* Then, figure out what arguments are appropriate for the
13448 type we are trying to find. For example, given:
13450 template <class T> struct S;
13451 template <class T, class U> void f(T, U) { S<U> su; }
13453 and supposing that we are instantiating f<int, double>,
13454 then our ARGS will be {int, double}, but, when looking up
13455 S we only want {double}. */
13456 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13457 complain
, in_decl
);
13458 if (argvec
== error_mark_node
)
13459 r
= error_mark_node
;
13460 else if (cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13462 /* See maybe_dependent_member_ref. */
13463 tree name
= TYPE_IDENTIFIER (t
);
13464 tree fullname
= name
;
13465 if (instantiates_primary_template_p (t
))
13466 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13467 INNERMOST_TEMPLATE_ARGS (argvec
));
13468 return build_typename_type (context
, name
, fullname
,
13473 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13474 entering_scope
, complain
);
13475 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13481 /* This is not a template type, so there's nothing to do. */
13485 return tsubst (t
, args
, complain
, in_decl
);
13489 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13491 /* Substitute into the default argument ARG (a default argument for
13492 FN), which has the indicated TYPE. */
13495 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13496 tsubst_flags_t complain
)
13498 int errs
= errorcount
+ sorrycount
;
13500 /* This can happen in invalid code. */
13501 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13505 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13506 && CONSTRUCTOR_NELTS (arg
) == 0)
13509 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13510 parm
= chain_index (parmnum
, parm
);
13511 tree parmtype
= TREE_TYPE (parm
);
13512 if (DECL_BY_REFERENCE (parm
))
13513 parmtype
= TREE_TYPE (parmtype
);
13514 if (parmtype
== error_mark_node
)
13515 return error_mark_node
;
13517 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13520 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13523 /* This default argument came from a template. Instantiate the
13524 default argument here, not in tsubst. In the case of
13533 we must be careful to do name lookup in the scope of S<T>,
13534 rather than in the current class. */
13535 push_to_top_level ();
13536 push_access_scope (fn
);
13537 push_deferring_access_checks (dk_no_deferred
);
13538 start_lambda_scope (parm
);
13540 /* The default argument expression may cause implicitly defined
13541 member functions to be synthesized, which will result in garbage
13542 collection. We must treat this situation as if we were within
13543 the body of function so as to avoid collecting live data on the
13546 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13547 complain
, NULL_TREE
,
13548 /*integral_constant_expression_p=*/false);
13551 finish_lambda_scope ();
13553 /* Make sure the default argument is reasonable. */
13554 arg
= check_default_argument (type
, arg
, complain
);
13556 if (errorcount
+sorrycount
> errs
13557 && (complain
& tf_warning_or_error
))
13558 inform (input_location
,
13559 " when instantiating default argument for call to %qD", fn
);
13561 pop_deferring_access_checks ();
13562 pop_access_scope (fn
);
13563 pop_from_top_level ();
13565 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13568 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13569 defarg_inst
->put (parm
, arg
);
13575 /* Substitute into all the default arguments for FN. */
13578 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13583 tmpl_args
= DECL_TI_ARGS (fn
);
13585 /* If this function is not yet instantiated, we certainly don't need
13586 its default arguments. */
13587 if (uses_template_parms (tmpl_args
))
13589 /* Don't do this again for clones. */
13590 if (DECL_CLONED_FUNCTION_P (fn
))
13594 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13596 arg
= TREE_CHAIN (arg
), ++i
)
13597 if (TREE_PURPOSE (arg
))
13598 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13600 TREE_PURPOSE (arg
),
13604 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13605 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13607 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13610 store_explicit_specifier (tree v
, tree t
)
13612 if (!explicit_specifier_map
)
13613 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13614 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13615 explicit_specifier_map
->put (v
, t
);
13618 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13621 lookup_explicit_specifier (tree v
)
13623 return *explicit_specifier_map
->get (v
);
13626 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13627 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13628 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13632 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13633 tree raises
, tsubst_flags_t complain
)
13635 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13638 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13640 new_type
= build_function_type (return_type
, arg_types
);
13641 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13645 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13646 /* Don't pick up extra function qualifiers from the basetype. */
13647 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13648 if (! MAYBE_CLASS_TYPE_P (r
))
13652 Type deduction may fail for any of the following
13655 -- Attempting to create "pointer to member of T" when T
13656 is not a class type. */
13657 if (complain
& tf_error
)
13658 error ("creating pointer to member function of non-class type %qT",
13660 return error_mark_node
;
13663 new_type
= build_method_type_directly (r
, return_type
,
13664 TREE_CHAIN (arg_types
));
13666 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13668 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13669 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13670 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13673 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13674 each of its formal parameters. If there is a disagreement then rebuild
13675 DECL's function type according to its formal parameter types, as part of a
13676 resolution for Core issues 1001/1322. */
13679 maybe_rebuild_function_decl_type (tree decl
)
13681 bool function_type_needs_rebuilding
= false;
13682 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13684 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13685 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13687 tree parm_type
= TREE_VALUE (parm_type_list
);
13688 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13689 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13691 function_type_needs_rebuilding
= true;
13695 parm_list
= DECL_CHAIN (parm_list
);
13696 parm_type_list
= TREE_CHAIN (parm_type_list
);
13700 if (!function_type_needs_rebuilding
)
13703 const tree fntype
= TREE_TYPE (decl
);
13704 tree parm_list
= DECL_ARGUMENTS (decl
);
13705 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13706 tree new_parm_type_list
= NULL_TREE
;
13707 tree
*q
= &new_parm_type_list
;
13708 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13710 *q
= copy_node (old_parm_type_list
);
13711 parm_list
= DECL_CHAIN (parm_list
);
13712 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13713 q
= &TREE_CHAIN (*q
);
13715 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13717 *q
= copy_node (old_parm_type_list
);
13718 tree
*new_parm_type
= &TREE_VALUE (*q
);
13719 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13720 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13721 *new_parm_type
= formal_parm_type_unqual
;
13723 parm_list
= DECL_CHAIN (parm_list
);
13724 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13725 q
= &TREE_CHAIN (*q
);
13727 if (old_parm_type_list
== void_list_node
)
13728 *q
= void_list_node
;
13731 = rebuild_function_or_method_type (fntype
,
13732 TREE_TYPE (fntype
), new_parm_type_list
,
13733 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13736 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13739 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13740 tree lambda_fntype
)
13742 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
13743 hashval_t hash
= 0;
13746 /* Nobody should be tsubst'ing into non-template functions. */
13747 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
13748 || DECL_LOCAL_DECL_P (t
));
13750 if (DECL_LOCAL_DECL_P (t
))
13752 if (tree spec
= retrieve_local_specialization (t
))
13755 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13757 /* If T is not dependent, just return it. */
13758 if (!uses_template_parms (DECL_TI_ARGS (t
))
13759 && !LAMBDA_FUNCTION_P (t
))
13762 /* Calculate the most general template of which R is a
13764 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13766 /* We're substituting a lambda function under tsubst_lambda_expr but not
13767 directly from it; find the matching function we're already inside.
13768 But don't do this if T is a generic lambda with a single level of
13769 template parms, as in that case we're doing a normal instantiation. */
13770 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13771 && (!generic_lambda_fn_p (t
)
13772 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13773 return enclosing_instantiation_of (t
);
13775 /* Calculate the complete set of arguments used to
13777 argvec
= tsubst_template_args (DECL_TI_ARGS
13778 (DECL_TEMPLATE_RESULT
13779 (DECL_TI_TEMPLATE (t
))),
13780 args
, complain
, in_decl
);
13781 if (argvec
== error_mark_node
)
13782 return error_mark_node
;
13784 /* Check to see if we already have this specialization. */
13785 if (!lambda_fntype
)
13787 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13788 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13792 /* We can see more levels of arguments than parameters if
13793 there was a specialization of a member template, like
13796 template <class T> struct S { template <class U> void f(); }
13797 template <> template <class U> void S<int>::f(U);
13799 Here, we'll be substituting into the specialization,
13800 because that's where we can find the code we actually
13801 want to generate, but we'll have enough arguments for
13802 the most general template.
13804 We also deal with the peculiar case:
13806 template <class T> struct S {
13807 template <class U> friend void f();
13809 template <class U> void f() {}
13811 template void f<double>();
13813 Here, the ARGS for the instantiation of will be {int,
13814 double}. But, we only need as many ARGS as there are
13815 levels of template parameters in CODE_PATTERN. We are
13816 careful not to get fooled into reducing the ARGS in
13819 template <class T> struct S { template <class U> void f(U); }
13820 template <class T> template <> void S<T>::f(int) {}
13822 which we can spot because the pattern will be a
13823 specialization in this case. */
13824 int args_depth
= TMPL_ARGS_DEPTH (args
);
13826 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13828 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13829 args
= get_innermost_template_args (args
, parms_depth
);
13833 /* This special case arises when we have something like this:
13835 template <class T> struct S {
13836 friend void f<int>(int, double);
13839 Here, the DECL_TI_TEMPLATE for the friend declaration
13840 will be an IDENTIFIER_NODE. We are being called from
13841 tsubst_friend_function, and we want only to create a
13842 new decl (R) with appropriate types so that we can call
13843 determine_specialization. */
13844 gen_tmpl
= NULL_TREE
;
13845 argvec
= NULL_TREE
;
13848 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13850 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13851 bool member
= ctx
&& TYPE_P (ctx
);
13853 if (member
&& !closure
)
13854 ctx
= tsubst_aggr_type (ctx
, args
,
13855 complain
, t
, /*entering_scope=*/1);
13857 tree type
= (lambda_fntype
? lambda_fntype
13858 : tsubst (TREE_TYPE (t
), args
,
13859 complain
| tf_fndecl_type
, in_decl
));
13860 if (type
== error_mark_node
)
13861 return error_mark_node
;
13863 /* If we hit excessive deduction depth, the type is bogus even if
13864 it isn't error_mark_node, so don't build a decl. */
13865 if (excessive_deduction_depth
)
13866 return error_mark_node
;
13868 /* We do NOT check for matching decls pushed separately at this
13869 point, as they may not represent instantiations of this
13870 template, and in any case are considered separate under the
13872 tree r
= copy_decl (t
);
13873 DECL_USE_TEMPLATE (r
) = 0;
13874 TREE_TYPE (r
) = type
;
13875 /* Clear out the mangled name and RTL for the instantiation. */
13876 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13877 SET_DECL_RTL (r
, NULL
);
13878 /* Leave DECL_INITIAL set on deleted instantiations. */
13879 if (!DECL_DELETED_FN (r
))
13880 DECL_INITIAL (r
) = NULL_TREE
;
13881 DECL_CONTEXT (r
) = ctx
;
13883 /* Handle explicit(dependent-expr). */
13884 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13886 tree spec
= lookup_explicit_specifier (t
);
13887 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13888 /*function_p=*/false,
13890 spec
= build_explicit_specifier (spec
, complain
);
13891 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13894 /* OpenMP UDRs have the only argument a reference to the declared
13895 type. We want to diagnose if the declared type is a reference,
13896 which is invalid, but as references to references are usually
13897 quietly merged, diagnose it here. */
13898 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13901 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13902 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13903 if (TYPE_REF_P (argtype
))
13904 error_at (DECL_SOURCE_LOCATION (t
),
13905 "reference type %qT in "
13906 "%<#pragma omp declare reduction%>", argtype
);
13907 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13908 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13912 if (member
&& DECL_CONV_FN_P (r
))
13913 /* Type-conversion operator. Reconstruct the name, in
13914 case it's the name of one of the template's parameters. */
13915 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13917 tree parms
= DECL_ARGUMENTS (t
);
13919 parms
= DECL_CHAIN (parms
);
13920 parms
= tsubst (parms
, args
, complain
, t
);
13921 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13922 DECL_CONTEXT (parm
) = r
;
13925 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13926 DECL_NAME (tparm
) = closure_identifier
;
13927 DECL_CHAIN (tparm
) = parms
;
13930 DECL_ARGUMENTS (r
) = parms
;
13931 DECL_RESULT (r
) = NULL_TREE
;
13933 maybe_rebuild_function_decl_type (r
);
13935 TREE_STATIC (r
) = 0;
13936 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13937 DECL_EXTERNAL (r
) = 1;
13938 /* If this is an instantiation of a function with internal
13939 linkage, we already know what object file linkage will be
13940 assigned to the instantiation. */
13941 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13942 DECL_DEFER_OUTPUT (r
) = 0;
13943 DECL_CHAIN (r
) = NULL_TREE
;
13944 DECL_PENDING_INLINE_INFO (r
) = 0;
13945 DECL_PENDING_INLINE_P (r
) = 0;
13946 DECL_SAVED_TREE (r
) = NULL_TREE
;
13947 DECL_STRUCT_FUNCTION (r
) = NULL
;
13949 /* We'll re-clone as appropriate in instantiate_template. */
13950 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13952 /* If we aren't complaining now, return on error before we register
13953 the specialization so that we'll complain eventually. */
13954 if ((complain
& tf_error
) == 0
13955 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13956 && !grok_op_properties (r
, /*complain=*/false))
13957 return error_mark_node
;
13959 /* Associate the constraints directly with the instantiation. We
13960 don't substitute through the constraints; that's only done when
13961 they are checked. */
13962 if (tree ci
= get_constraints (t
))
13963 /* Unless we're regenerating a lambda, in which case we'll set the
13964 lambda's constraints in tsubst_lambda_expr. */
13965 if (!lambda_fntype
)
13966 set_constraints (r
, ci
);
13968 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13969 SET_DECL_FRIEND_CONTEXT (r
,
13970 tsubst (DECL_FRIEND_CONTEXT (t
),
13971 args
, complain
, in_decl
));
13973 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13974 this in the special friend case mentioned above where
13975 GEN_TMPL is NULL. */
13976 if (gen_tmpl
&& !closure
)
13978 DECL_TEMPLATE_INFO (r
)
13979 = build_template_info (gen_tmpl
, argvec
);
13980 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13983 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13985 /* We instantiated this while substituting into
13986 the type earlier (template/friend54.C). */
13989 /* We're not supposed to instantiate default arguments
13990 until they are called, for a template. But, for a
13993 template <class T> void f ()
13994 { extern void g(int i = T()); }
13996 we should do the substitution when the template is
13997 instantiated. We handle the member function case in
13998 instantiate_class_template since the default arguments
13999 might refer to other members of the class. */
14001 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14002 && !uses_template_parms (argvec
))
14003 tsubst_default_arguments (r
, complain
);
14005 else if (DECL_LOCAL_DECL_P (r
))
14007 if (!cp_unevaluated_operand
)
14008 register_local_specialization (r
, t
);
14011 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14013 /* Copy the list of befriending classes. */
14014 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14016 friends
= &TREE_CHAIN (*friends
))
14018 *friends
= copy_node (*friends
);
14019 TREE_VALUE (*friends
)
14020 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14023 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14025 maybe_retrofit_in_chrg (r
);
14026 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14027 return error_mark_node
;
14028 /* If this is an instantiation of a member template, clone it.
14029 If it isn't, that'll be handled by
14030 clone_constructors_and_destructors. */
14031 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14032 clone_cdtor (r
, /*update_methods=*/false);
14034 else if ((complain
& tf_error
) != 0
14035 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14036 && !grok_op_properties (r
, /*complain=*/true))
14037 return error_mark_node
;
14039 /* Possibly limit visibility based on template args. */
14040 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14041 if (DECL_VISIBILITY_SPECIFIED (t
))
14043 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14044 DECL_ATTRIBUTES (r
)
14045 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14047 determine_visibility (r
);
14048 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14049 && !processing_template_decl
)
14050 defaulted_late_check (r
);
14052 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14053 args
, complain
, in_decl
);
14055 if (tree attr
= lookup_attribute ("omp declare variant base",
14056 DECL_ATTRIBUTES (r
)))
14057 omp_declare_variant_finalize (r
, attr
);
14062 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14065 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14066 tree lambda_fntype
)
14068 /* We can get here when processing a member function template,
14069 member class template, or template template parameter. */
14070 tree decl
= DECL_TEMPLATE_RESULT (t
);
14076 hashval_t hash
= 0;
14078 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14080 /* Template template parameter is treated here. */
14081 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14082 if (new_type
== error_mark_node
)
14083 r
= error_mark_node
;
14084 /* If we get a real template back, return it. This can happen in
14085 the context of most_specialized_partial_spec. */
14086 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14089 /* The new TEMPLATE_DECL was built in
14090 reduce_template_parm_level. */
14091 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14095 if (!lambda_fntype
)
14097 /* We might already have an instance of this template.
14098 The ARGS are for the surrounding class type, so the
14099 full args contain the tsubst'd args for the context,
14100 plus the innermost args from the template decl. */
14101 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14102 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14103 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14104 /* Because this is a template, the arguments will still be
14105 dependent, even after substitution. If
14106 PROCESSING_TEMPLATE_DECL is not set, the dependency
14107 predicates will short-circuit. */
14108 ++processing_template_decl
;
14109 full_args
= tsubst_template_args (tmpl_args
, args
,
14110 complain
, in_decl
);
14111 --processing_template_decl
;
14112 if (full_args
== error_mark_node
)
14113 return error_mark_node
;
14115 /* If this is a default template template argument,
14116 tsubst might not have changed anything. */
14117 if (full_args
== tmpl_args
)
14120 hash
= hash_tmpl_and_args (t
, full_args
);
14121 spec
= retrieve_specialization (t
, full_args
, hash
);
14122 if (spec
!= NULL_TREE
)
14125 /* Type partial instantiations are stored as the type by
14126 lookup_template_class_1, not here as the template. */
14127 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14132 /* Make a new template decl. It will be similar to the
14133 original, but will record the current template arguments.
14134 We also create a new function declaration, which is just
14135 like the old one, but points to this new template, rather
14136 than the old one. */
14138 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14139 DECL_CHAIN (r
) = NULL_TREE
;
14141 // Build new template info linking to the original template decl.
14142 if (!lambda_fntype
)
14144 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14145 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14148 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14150 /* The template parameters for this new template are all the
14151 template parameters for the old template, except the
14152 outermost level of parameters. */
14153 DECL_TEMPLATE_PARMS (r
)
14154 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14157 bool class_p
= false;
14159 ++processing_template_decl
;
14160 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14161 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14164 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14167 inner
= TREE_TYPE (inner
);
14169 inner
= tsubst (inner
, args
, complain
, in_decl
);
14171 --processing_template_decl
;
14172 if (inner
== error_mark_node
)
14173 return error_mark_node
;
14177 /* For a partial specialization, we need to keep pointing to
14178 the primary template. */
14179 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14180 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14182 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14183 inner
= TYPE_MAIN_DECL (inner
);
14185 else if (lambda_fntype
)
14187 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14188 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14192 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14193 DECL_TI_TEMPLATE (inner
) = r
;
14194 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14197 DECL_TEMPLATE_RESULT (r
) = inner
;
14198 TREE_TYPE (r
) = TREE_TYPE (inner
);
14199 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14201 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14202 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14204 if (PRIMARY_TEMPLATE_P (t
))
14205 DECL_PRIMARY_TEMPLATE (r
) = r
;
14207 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14209 /* Record this non-type partial instantiation. */
14210 register_specialization (r
, t
,
14211 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14217 /* True if FN is the op() for a lambda in an uninstantiated template. */
14220 lambda_fn_in_template_p (tree fn
)
14222 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14224 tree closure
= DECL_CONTEXT (fn
);
14225 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14228 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14229 which the above is true. */
14232 instantiated_lambda_fn_p (tree fn
)
14234 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14236 tree closure
= DECL_CONTEXT (fn
);
14237 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14238 return LAMBDA_EXPR_INSTANTIATED (lam
);
14241 /* We're instantiating a variable from template function TCTX. Return the
14242 corresponding current enclosing scope. This gets complicated because lambda
14243 functions in templates are regenerated rather than instantiated, but generic
14244 lambda functions are subsequently instantiated. */
14247 enclosing_instantiation_of (tree otctx
)
14250 tree fn
= current_function_decl
;
14251 int lambda_count
= 0;
14253 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14254 || instantiated_lambda_fn_p (tctx
));
14255 tctx
= decl_function_context (tctx
))
14257 for (; fn
; fn
= decl_function_context (fn
))
14260 int flambda_count
= 0;
14261 for (; fn
&& instantiated_lambda_fn_p (fn
);
14262 fn
= decl_function_context (fn
))
14264 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14265 ? most_general_template (fn
) != most_general_template (tctx
)
14268 if (flambda_count
!= lambda_count
)
14270 gcc_assert (flambda_count
> lambda_count
);
14271 for (; flambda_count
> lambda_count
; --flambda_count
)
14272 ofn
= decl_function_context (ofn
);
14274 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14275 || DECL_CONV_FN_P (ofn
));
14278 gcc_unreachable ();
14281 /* Substitute the ARGS into the T, which is a _DECL. Return the
14282 result of the substitution. Issue error and warning messages under
14283 control of COMPLAIN. */
14286 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14288 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14289 location_t saved_loc
;
14290 tree r
= NULL_TREE
;
14292 hashval_t hash
= 0;
14294 /* Set the filename and linenumber to improve error-reporting. */
14295 saved_loc
= input_location
;
14296 input_location
= DECL_SOURCE_LOCATION (t
);
14298 switch (TREE_CODE (t
))
14300 case TEMPLATE_DECL
:
14301 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14304 case FUNCTION_DECL
:
14305 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14310 tree type
= NULL_TREE
;
14312 tree expanded_types
= NULL_TREE
;
14313 tree prev_r
= NULL_TREE
;
14314 tree first_r
= NULL_TREE
;
14316 if (DECL_PACK_P (t
))
14318 /* If there is a local specialization that isn't a
14319 parameter pack, it means that we're doing a "simple"
14320 substitution from inside tsubst_pack_expansion. Just
14321 return the local specialization (which will be a single
14323 tree spec
= retrieve_local_specialization (t
);
14325 && TREE_CODE (spec
) == PARM_DECL
14326 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14329 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14330 the parameters in this function parameter pack. */
14331 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14332 complain
, in_decl
);
14333 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14335 len
= TREE_VEC_LENGTH (expanded_types
);
14337 /* Zero-length parameter packs are boring. Just substitute
14339 if (len
== 0 && !cp_unevaluated_operand
)
14340 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14345 /* All we did was update the type. Make a note of that. */
14346 type
= expanded_types
;
14347 expanded_types
= NULL_TREE
;
14351 /* Loop through all of the parameters we'll build. When T is
14352 a function parameter pack, LEN is the number of expanded
14353 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14355 for (i
= 0; i
< len
; ++i
)
14359 if (DECL_TEMPLATE_PARM_P (t
))
14360 SET_DECL_TEMPLATE_PARM_P (r
);
14362 if (expanded_types
)
14363 /* We're on the Ith parameter of the function parameter
14366 /* Get the Ith type. */
14367 type
= TREE_VEC_ELT (expanded_types
, i
);
14369 /* Rename the parameter to include the index. */
14371 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14374 /* We're dealing with a normal parameter. */
14375 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14377 type
= type_decays_to (type
);
14378 TREE_TYPE (r
) = type
;
14379 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14381 if (DECL_INITIAL (r
))
14383 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14384 DECL_INITIAL (r
) = TREE_TYPE (r
);
14386 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14387 complain
, in_decl
);
14390 DECL_CONTEXT (r
) = NULL_TREE
;
14392 if (!DECL_TEMPLATE_PARM_P (r
))
14393 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14395 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14396 args
, complain
, in_decl
);
14398 /* Keep track of the first new parameter we
14399 generate. That's what will be returned to the
14404 /* Build a proper chain of parameters when substituting
14405 into a function parameter pack. */
14407 DECL_CHAIN (prev_r
) = r
;
14410 /* If cp_unevaluated_operand is set, we're just looking for a
14411 single dummy parameter, so don't keep going. */
14412 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14413 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14414 complain
, DECL_CHAIN (t
));
14416 /* FIRST_R contains the start of the chain we've built. */
14423 tree type
= NULL_TREE
;
14424 tree vec
= NULL_TREE
;
14425 tree expanded_types
= NULL_TREE
;
14428 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14430 /* This field is a lambda capture pack. Return a TREE_VEC of
14431 the expanded fields to instantiate_class_template_1. */
14432 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14433 complain
, in_decl
);
14434 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14436 len
= TREE_VEC_LENGTH (expanded_types
);
14437 vec
= make_tree_vec (len
);
14441 /* All we did was update the type. Make a note of that. */
14442 type
= expanded_types
;
14443 expanded_types
= NULL_TREE
;
14447 for (int i
= 0; i
< len
; ++i
)
14450 if (expanded_types
)
14452 type
= TREE_VEC_ELT (expanded_types
, i
);
14454 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14457 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14459 if (type
== error_mark_node
)
14460 RETURN (error_mark_node
);
14461 TREE_TYPE (r
) = type
;
14462 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14464 if (DECL_C_BIT_FIELD (r
))
14465 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14467 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14468 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14470 /*integral_constant_expression_p=*/true);
14471 if (DECL_INITIAL (t
))
14473 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14474 NSDMI in perform_member_init. Still set DECL_INITIAL
14475 so that we know there is one. */
14476 DECL_INITIAL (r
) = void_node
;
14477 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14478 retrofit_lang_decl (r
);
14479 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14481 /* We don't have to set DECL_CONTEXT here; it is set by
14482 finish_member_declaration. */
14483 DECL_CHAIN (r
) = NULL_TREE
;
14485 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14486 args
, complain
, in_decl
);
14489 TREE_VEC_ELT (vec
, i
) = r
;
14498 /* We reach here only for member using decls. We also need to check
14499 uses_template_parms because DECL_DEPENDENT_P is not set for a
14500 using-declaration that designates a member of the current
14501 instantiation (c++/53549). */
14502 if (DECL_DEPENDENT_P (t
)
14503 || uses_template_parms (USING_DECL_SCOPE (t
)))
14505 tree scope
= USING_DECL_SCOPE (t
);
14506 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14507 if (PACK_EXPANSION_P (scope
))
14509 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14510 int len
= TREE_VEC_LENGTH (vec
);
14511 r
= make_tree_vec (len
);
14512 for (int i
= 0; i
< len
; ++i
)
14514 tree escope
= TREE_VEC_ELT (vec
, i
);
14515 tree elt
= do_class_using_decl (escope
, name
);
14518 r
= error_mark_node
;
14523 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14524 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14526 TREE_VEC_ELT (r
, i
) = elt
;
14531 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14532 complain
, in_decl
);
14533 r
= do_class_using_decl (inst_scope
, name
);
14535 r
= error_mark_node
;
14538 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14539 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14546 DECL_CHAIN (r
) = NULL_TREE
;
14553 tree argvec
= NULL_TREE
;
14554 tree gen_tmpl
= NULL_TREE
;
14555 tree tmpl
= NULL_TREE
;
14556 tree type
= NULL_TREE
;
14558 if (TREE_TYPE (t
) == error_mark_node
)
14559 RETURN (error_mark_node
);
14561 if (TREE_CODE (t
) == TYPE_DECL
14562 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14564 /* If this is the canonical decl, we don't have to
14565 mess with instantiations, and often we can't (for
14566 typename, template type parms and such). Note that
14567 TYPE_NAME is not correct for the above test if
14568 we've copied the type for a typedef. */
14569 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14570 if (type
== error_mark_node
)
14571 RETURN (error_mark_node
);
14572 r
= TYPE_NAME (type
);
14576 /* Check to see if we already have the specialization we
14578 tree spec
= NULL_TREE
;
14579 bool local_p
= false;
14580 tree ctx
= DECL_CONTEXT (t
);
14581 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14582 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14585 if (DECL_CLASS_SCOPE_P (t
))
14587 ctx
= tsubst_aggr_type (ctx
, args
,
14589 in_decl
, /*entering_scope=*/1);
14590 /* If CTX is unchanged, then T is in fact the
14591 specialization we want. That situation occurs when
14592 referencing a static data member within in its own
14593 class. We can use pointer equality, rather than
14594 same_type_p, because DECL_CONTEXT is always
14596 if (ctx
== DECL_CONTEXT (t
)
14597 /* ... unless T is a member template; in which
14598 case our caller can be willing to create a
14599 specialization of that template represented
14601 && !(DECL_TI_TEMPLATE (t
)
14602 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14608 tmpl
= DECL_TI_TEMPLATE (t
);
14609 gen_tmpl
= most_general_template (tmpl
);
14610 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14611 if (argvec
!= error_mark_node
)
14612 argvec
= (coerce_innermost_template_parms
14613 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14614 argvec
, t
, complain
,
14615 /*all*/true, /*defarg*/true));
14616 if (argvec
== error_mark_node
)
14617 RETURN (error_mark_node
);
14618 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14619 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14624 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14625 /* Subsequent calls to pushdecl will fill this in. */
14627 /* A local variable. */
14629 /* Unless this is a reference to a static variable from an
14630 enclosing function, in which case we need to fill it in now. */
14631 if (TREE_STATIC (t
))
14633 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14634 if (fn
!= current_function_decl
)
14637 spec
= retrieve_local_specialization (t
);
14639 /* If we already have the specialization we need, there is
14640 nothing more to do. */
14647 /* Create a new node for the specialization we need. */
14648 if (type
== NULL_TREE
)
14650 if (is_typedef_decl (t
))
14651 type
= DECL_ORIGINAL_TYPE (t
);
14653 type
= TREE_TYPE (t
);
14655 && VAR_HAD_UNKNOWN_BOUND (t
)
14656 && type
!= error_mark_node
)
14657 type
= strip_array_domain (type
);
14658 tree sub_args
= args
;
14659 if (tree auto_node
= type_uses_auto (type
))
14661 /* Mask off any template args past the variable's context so we
14662 don't replace the auto with an unrelated argument. */
14663 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14664 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14666 /* This should never happen with the new lambda instantiation
14667 model, but keep the handling just in case. */
14668 gcc_assert (!CHECKING_P
),
14669 sub_args
= strip_innermost_template_args (args
, extra
);
14671 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14672 /* Substituting the type might have recursively instantiated this
14673 same alias (c++/86171). */
14674 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14675 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14684 DECL_INITIALIZED_P (r
) = 0;
14685 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14686 if (type
== error_mark_node
)
14687 RETURN (error_mark_node
);
14688 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14690 /* It may seem that this case cannot occur, since:
14695 declares a function, not a variable. However:
14698 template <typename T> void g() { T t; }
14699 template void g<f>();
14701 is an attempt to declare a variable with function
14703 error ("variable %qD has function type",
14704 /* R is not yet sufficiently initialized, so we
14705 just use its name. */
14707 RETURN (error_mark_node
);
14709 type
= complete_type (type
);
14710 /* Wait until cp_finish_decl to set this again, to handle
14711 circular dependency (template/instantiate6.C). */
14712 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14713 type
= check_var_type (DECL_NAME (r
), type
,
14714 DECL_SOURCE_LOCATION (r
));
14715 if (DECL_HAS_VALUE_EXPR_P (t
))
14717 tree ve
= DECL_VALUE_EXPR (t
);
14718 /* If the DECL_VALUE_EXPR is converted to the declared type,
14719 preserve the identity so that gimplify_type_sizes works. */
14720 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14722 ve
= TREE_OPERAND (ve
, 0);
14723 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14724 /*constant_expression_p=*/false);
14725 if (REFERENCE_REF_P (ve
))
14727 gcc_assert (TYPE_REF_P (type
));
14728 ve
= TREE_OPERAND (ve
, 0);
14731 ve
= build_nop (type
, ve
);
14732 else if (DECL_LANG_SPECIFIC (t
)
14733 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14734 && TREE_CODE (ve
) == COMPONENT_REF
14735 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14736 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14737 type
= TREE_TYPE (ve
);
14739 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14740 == TYPE_MAIN_VARIANT (type
));
14741 SET_DECL_VALUE_EXPR (r
, ve
);
14743 if (CP_DECL_THREAD_LOCAL_P (r
)
14744 && !processing_template_decl
)
14745 set_decl_tls_model (r
, decl_default_tls_model (r
));
14747 else if (DECL_SELF_REFERENCE_P (t
))
14748 SET_DECL_SELF_REFERENCE_P (r
);
14749 TREE_TYPE (r
) = type
;
14750 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14751 DECL_CONTEXT (r
) = ctx
;
14752 /* Clear out the mangled name and RTL for the instantiation. */
14753 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14754 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14755 SET_DECL_RTL (r
, NULL
);
14756 /* The initializer must not be expanded until it is required;
14757 see [temp.inst]. */
14758 DECL_INITIAL (r
) = NULL_TREE
;
14759 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14762 if (DECL_LANG_SPECIFIC (r
))
14763 SET_DECL_DEPENDENT_INIT_P (r
, false);
14765 SET_DECL_MODE (r
, VOIDmode
);
14767 /* Possibly limit visibility based on template args. */
14768 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14769 if (DECL_VISIBILITY_SPECIFIED (t
))
14771 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14772 DECL_ATTRIBUTES (r
)
14773 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14775 determine_visibility (r
);
14780 /* A static data member declaration is always marked
14781 external when it is declared in-class, even if an
14782 initializer is present. We mimic the non-template
14783 processing here. */
14784 DECL_EXTERNAL (r
) = 1;
14785 if (DECL_NAMESPACE_SCOPE_P (t
))
14786 DECL_NOT_REALLY_EXTERN (r
) = 1;
14788 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14789 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14790 if (!error_operand_p (r
) || (complain
& tf_error
))
14791 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14795 if (DECL_LANG_SPECIFIC (r
))
14796 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14797 if (!cp_unevaluated_operand
)
14798 register_local_specialization (r
, t
);
14801 DECL_CHAIN (r
) = NULL_TREE
;
14803 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14805 args
, complain
, in_decl
);
14807 /* Preserve a typedef that names a type. */
14808 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14810 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14811 set_underlying_type (r
);
14812 if (TYPE_DECL_ALIAS_P (r
))
14813 /* An alias template specialization can be dependent
14814 even if its underlying type is not. */
14815 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14818 layout_decl (r
, 0);
14823 gcc_unreachable ();
14828 /* Restore the file and line information. */
14829 input_location
= saved_loc
;
14834 /* Substitute into the complete parameter type list PARMS. */
14837 tsubst_function_parms (tree parms
,
14839 tsubst_flags_t complain
,
14842 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14845 /* Substitute into the ARG_TYPES of a function type.
14846 If END is a TREE_CHAIN, leave it and any following types
14850 tsubst_arg_types (tree arg_types
,
14853 tsubst_flags_t complain
,
14856 tree remaining_arg_types
;
14857 tree type
= NULL_TREE
;
14859 tree expanded_args
= NULL_TREE
;
14862 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14865 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14866 args
, end
, complain
, in_decl
);
14867 if (remaining_arg_types
== error_mark_node
)
14868 return error_mark_node
;
14870 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14872 /* For a pack expansion, perform substitution on the
14873 entire expression. Later on, we'll handle the arguments
14875 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14876 args
, complain
, in_decl
);
14878 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14879 /* So that we'll spin through the parameters, one by one. */
14880 i
= TREE_VEC_LENGTH (expanded_args
);
14883 /* We only partially substituted into the parameter
14884 pack. Our type is TYPE_PACK_EXPANSION. */
14885 type
= expanded_args
;
14886 expanded_args
= NULL_TREE
;
14894 type
= TREE_VEC_ELT (expanded_args
, i
);
14896 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14898 if (type
== error_mark_node
)
14899 return error_mark_node
;
14900 if (VOID_TYPE_P (type
))
14902 if (complain
& tf_error
)
14904 error ("invalid parameter type %qT", type
);
14906 error ("in declaration %q+D", in_decl
);
14908 return error_mark_node
;
14911 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14912 return error_mark_node
;
14914 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14915 top-level qualifiers as required. */
14916 type
= cv_unqualified (type_decays_to (type
));
14918 /* We do not substitute into default arguments here. The standard
14919 mandates that they be instantiated only when needed, which is
14920 done in build_over_call. */
14921 default_arg
= TREE_PURPOSE (arg_types
);
14923 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14924 since the new op() won't have any associated template arguments for us
14925 to refer to later. */
14926 if (lambda_fn_in_template_p (in_decl
))
14927 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14928 false/*fn*/, false/*constexpr*/);
14930 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14932 /* We've instantiated a template before its default arguments
14933 have been parsed. This can happen for a nested template
14934 class, and is not an error unless we require the default
14935 argument in a call of this function. */
14936 remaining_arg_types
=
14937 tree_cons (default_arg
, type
, remaining_arg_types
);
14938 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14939 remaining_arg_types
);
14942 remaining_arg_types
=
14943 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14946 return remaining_arg_types
;
14949 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14950 *not* handle the exception-specification for FNTYPE, because the
14951 initial substitution of explicitly provided template parameters
14952 during argument deduction forbids substitution into the
14953 exception-specification:
14957 All references in the function type of the function template to the
14958 corresponding template parameters are replaced by the specified tem-
14959 plate argument values. If a substitution in a template parameter or
14960 in the function type of the function template results in an invalid
14961 type, type deduction fails. [Note: The equivalent substitution in
14962 exception specifications is done only when the function is instanti-
14963 ated, at which point a program is ill-formed if the substitution
14964 results in an invalid type.] */
14967 tsubst_function_type (tree t
,
14969 tsubst_flags_t complain
,
14973 tree arg_types
= NULL_TREE
;
14975 /* The TYPE_CONTEXT is not used for function/method types. */
14976 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14978 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14980 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14982 if (late_return_type_p
)
14984 /* Substitute the argument types. */
14985 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14986 complain
, in_decl
);
14987 if (arg_types
== error_mark_node
)
14988 return error_mark_node
;
14990 tree save_ccp
= current_class_ptr
;
14991 tree save_ccr
= current_class_ref
;
14992 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14993 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14994 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14997 /* DR 1207: 'this' is in scope in the trailing return type. */
14998 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15001 /* Substitute the return type. */
15002 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15006 current_class_ptr
= save_ccp
;
15007 current_class_ref
= save_ccr
;
15011 /* Substitute the return type. */
15012 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15014 if (return_type
== error_mark_node
)
15015 return error_mark_node
;
15016 /* DR 486 clarifies that creation of a function type with an
15017 invalid return type is a deduction failure. */
15018 if (TREE_CODE (return_type
) == ARRAY_TYPE
15019 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15021 if (complain
& tf_error
)
15023 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15024 error ("function returning an array");
15026 error ("function returning a function");
15028 return error_mark_node
;
15031 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
15032 return error_mark_node
;
15034 if (!late_return_type_p
)
15036 /* Substitute the argument types. */
15037 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15038 complain
, in_decl
);
15039 if (arg_types
== error_mark_node
)
15040 return error_mark_node
;
15043 /* Construct a new type node and return it. */
15044 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15045 /*raises=*/NULL_TREE
, complain
);
15048 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15049 ARGS into that specification, and return the substituted
15050 specification. If there is no specification, return NULL_TREE. */
15053 tsubst_exception_specification (tree fntype
,
15055 tsubst_flags_t complain
,
15062 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15063 new_specs
= NULL_TREE
;
15064 if (specs
&& TREE_PURPOSE (specs
))
15066 /* A noexcept-specifier. */
15067 tree expr
= TREE_PURPOSE (specs
);
15068 if (TREE_CODE (expr
) == INTEGER_CST
)
15072 /* Defer instantiation of noexcept-specifiers to avoid
15073 excessive instantiations (c++/49107). */
15074 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15075 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15077 /* We already partially instantiated this member template,
15078 so combine the new args with the old. */
15079 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15080 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15081 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15082 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15086 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15087 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15092 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15094 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15096 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15098 new_specs
= tsubst_copy_and_build
15099 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15100 /*integral_constant_expression_p=*/true);
15102 new_specs
= build_noexcept_spec (new_specs
, complain
);
15106 if (! TREE_VALUE (specs
))
15113 tree expanded_specs
= NULL_TREE
;
15115 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15117 /* Expand the pack expansion type. */
15118 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15122 if (expanded_specs
== error_mark_node
)
15123 return error_mark_node
;
15124 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15125 len
= TREE_VEC_LENGTH (expanded_specs
);
15128 /* We're substituting into a member template, so
15129 we got a TYPE_PACK_EXPANSION back. Add that
15130 expansion and move on. */
15131 gcc_assert (TREE_CODE (expanded_specs
)
15132 == TYPE_PACK_EXPANSION
);
15133 new_specs
= add_exception_specifier (new_specs
,
15136 specs
= TREE_CHAIN (specs
);
15141 for (i
= 0; i
< len
; ++i
)
15143 if (expanded_specs
)
15144 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15146 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15147 if (spec
== error_mark_node
)
15149 new_specs
= add_exception_specifier (new_specs
, spec
,
15153 specs
= TREE_CHAIN (specs
);
15159 /* Substitute through a TREE_LIST of types or expressions, handling pack
15163 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15165 if (t
== void_list_node
)
15168 tree purpose
= TREE_PURPOSE (t
);
15169 tree purposevec
= NULL_TREE
;
15172 else if (PACK_EXPANSION_P (purpose
))
15174 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15175 if (TREE_CODE (purpose
) == TREE_VEC
)
15176 purposevec
= purpose
;
15178 else if (TYPE_P (purpose
))
15179 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15181 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15182 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15183 return error_mark_node
;
15185 tree value
= TREE_VALUE (t
);
15186 tree valuevec
= NULL_TREE
;
15189 else if (PACK_EXPANSION_P (value
))
15191 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15192 if (TREE_CODE (value
) == TREE_VEC
)
15195 else if (TYPE_P (value
))
15196 value
= tsubst (value
, args
, complain
, in_decl
);
15198 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15199 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15200 return error_mark_node
;
15202 tree chain
= TREE_CHAIN (t
);
15205 else if (TREE_CODE (chain
) == TREE_LIST
)
15206 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15207 else if (TYPE_P (chain
))
15208 chain
= tsubst (chain
, args
, complain
, in_decl
);
15210 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15211 if (chain
== error_mark_node
)
15212 return error_mark_node
;
15214 if (purpose
== TREE_PURPOSE (t
)
15215 && value
== TREE_VALUE (t
)
15216 && chain
== TREE_CHAIN (t
))
15220 /* Determine the number of arguments. */
15223 len
= TREE_VEC_LENGTH (purposevec
);
15224 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15227 len
= TREE_VEC_LENGTH (valuevec
);
15231 for (int i
= len
; i
-- > 0; )
15234 purpose
= TREE_VEC_ELT (purposevec
, i
);
15236 value
= TREE_VEC_ELT (valuevec
, i
);
15238 if (value
&& TYPE_P (value
))
15239 chain
= hash_tree_cons (purpose
, value
, chain
);
15241 chain
= tree_cons (purpose
, value
, chain
);
15247 /* Take the tree structure T and replace template parameters used
15248 therein with the argument vector ARGS. IN_DECL is an associated
15249 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15250 Issue error and warning messages under control of COMPLAIN. Note
15251 that we must be relatively non-tolerant of extensions here, in
15252 order to preserve conformance; if we allow substitutions that
15253 should not be allowed, we may allow argument deductions that should
15254 not succeed, and therefore report ambiguous overload situations
15255 where there are none. In theory, we could allow the substitution,
15256 but indicate that it should have failed, and allow our caller to
15257 make sure that the right thing happens, but we don't try to do this
15260 This function is used for dealing with types, decls and the like;
15261 for expressions, use tsubst_expr or tsubst_copy. */
15264 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15266 enum tree_code code
;
15267 tree type
, r
= NULL_TREE
;
15269 if (t
== NULL_TREE
|| t
== error_mark_node
15270 || t
== integer_type_node
15271 || t
== void_type_node
15272 || t
== char_type_node
15273 || t
== unknown_type_node
15274 || TREE_CODE (t
) == NAMESPACE_DECL
15275 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15279 return tsubst_decl (t
, args
, complain
);
15281 if (args
== NULL_TREE
)
15284 code
= TREE_CODE (t
);
15286 if (code
== IDENTIFIER_NODE
)
15287 type
= IDENTIFIER_TYPE_VALUE (t
);
15289 type
= TREE_TYPE (t
);
15291 gcc_assert (type
!= unknown_type_node
);
15293 /* Reuse typedefs. We need to do this to handle dependent attributes,
15294 such as attribute aligned. */
15296 && typedef_variant_p (t
))
15298 tree decl
= TYPE_NAME (t
);
15300 if (alias_template_specialization_p (t
, nt_opaque
))
15302 /* DECL represents an alias template and we want to
15304 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15305 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15306 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15308 else if (DECL_CLASS_SCOPE_P (decl
)
15309 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15310 && uses_template_parms (DECL_CONTEXT (decl
)))
15312 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15313 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15314 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15316 else if (DECL_FUNCTION_SCOPE_P (decl
)
15317 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15318 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15319 r
= retrieve_local_specialization (decl
);
15321 /* The typedef is from a non-template context. */
15327 r
= cp_build_qualified_type_real
15328 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15329 complain
| tf_ignore_bad_quals
);
15334 /* We don't have an instantiation yet, so drop the typedef. */
15335 int quals
= cp_type_quals (t
);
15336 t
= DECL_ORIGINAL_TYPE (decl
);
15337 t
= cp_build_qualified_type_real (t
, quals
,
15338 complain
| tf_ignore_bad_quals
);
15342 bool fndecl_type
= (complain
& tf_fndecl_type
);
15343 complain
&= ~tf_fndecl_type
;
15346 && code
!= TYPENAME_TYPE
15347 && code
!= TEMPLATE_TYPE_PARM
15348 && code
!= TEMPLATE_PARM_INDEX
15349 && code
!= IDENTIFIER_NODE
15350 && code
!= FUNCTION_TYPE
15351 && code
!= METHOD_TYPE
)
15352 type
= tsubst (type
, args
, complain
, in_decl
);
15353 if (type
== error_mark_node
)
15354 return error_mark_node
;
15360 case ENUMERAL_TYPE
:
15361 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15362 /*entering_scope=*/0);
15365 case IDENTIFIER_NODE
:
15376 if (t
== integer_type_node
)
15379 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15380 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15384 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15386 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15387 /*integral_constant_expression_p=*/false);
15389 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15391 if (TREE_CODE (max
) == NOP_EXPR
15392 && TREE_SIDE_EFFECTS (omax
)
15393 && !TREE_TYPE (max
))
15394 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15396 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15397 with TREE_SIDE_EFFECTS that indicates this is not an integral
15398 constant expression. */
15399 if (processing_template_decl
15400 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15402 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15403 TREE_SIDE_EFFECTS (max
) = 1;
15406 return compute_array_index_type (NULL_TREE
, max
, complain
);
15409 case TEMPLATE_TYPE_PARM
:
15410 case TEMPLATE_TEMPLATE_PARM
:
15411 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15412 case TEMPLATE_PARM_INDEX
:
15417 tree arg
= NULL_TREE
;
15421 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15422 template_parm_level_and_index (t
, &level
, &idx
);
15424 levels
= TMPL_ARGS_DEPTH (args
);
15425 if (level
<= levels
15426 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15428 arg
= TMPL_ARG (args
, level
, idx
);
15430 /* See through ARGUMENT_PACK_SELECT arguments. */
15431 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15432 arg
= argument_pack_select_arg (arg
);
15435 if (arg
== error_mark_node
)
15436 return error_mark_node
;
15437 else if (arg
!= NULL_TREE
)
15439 if (ARGUMENT_PACK_P (arg
))
15440 /* If ARG is an argument pack, we don't actually want to
15441 perform a substitution here, because substitutions
15442 for argument packs are only done
15443 element-by-element. We can get to this point when
15444 substituting the type of a non-type template
15445 parameter pack, when that type actually contains
15446 template parameter packs from an outer template, e.g.,
15448 template<typename... Types> struct A {
15449 template<Types... Values> struct B { };
15453 if (code
== TEMPLATE_TYPE_PARM
)
15457 /* When building concept checks for the purpose of
15458 deducing placeholders, we can end up with wildcards
15459 where types are expected. Adjust this to the deduced
15461 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15462 arg
= TREE_TYPE (TREE_TYPE (arg
));
15464 gcc_assert (TYPE_P (arg
));
15466 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15468 return cp_build_qualified_type_real
15469 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15471 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15473 /* We are processing a type constructed from a
15474 template template parameter. */
15475 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15476 args
, complain
, in_decl
);
15477 if (argvec
== error_mark_node
)
15478 return error_mark_node
;
15480 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15481 || TREE_CODE (arg
) == TEMPLATE_DECL
15482 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15484 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15485 /* Consider this code:
15487 template <template <class> class Template>
15489 template <class Arg> using Bind = Template<Arg>;
15492 template <template <class> class Template, class Arg>
15493 using Instantiate = Template<Arg>; //#0
15495 template <template <class> class Template,
15498 Instantiate<Internal<Template>::template Bind,
15501 When #1 is parsed, the
15502 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15503 parameter `Template' in #0 matches the
15504 UNBOUND_CLASS_TEMPLATE representing the argument
15505 `Internal<Template>::template Bind'; We then want
15506 to assemble the type `Bind<Argument>' that can't
15507 be fully created right now, because
15508 `Internal<Template>' not being complete, the Bind
15509 template cannot be looked up in that context. So
15510 we need to "store" `Bind<Argument>' for later
15511 when the context of Bind becomes complete. Let's
15512 store that in a TYPENAME_TYPE. */
15513 return make_typename_type (TYPE_CONTEXT (arg
),
15514 build_nt (TEMPLATE_ID_EXPR
,
15515 TYPE_IDENTIFIER (arg
),
15520 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15521 are resolving nested-types in the signature of a
15522 member function templates. Otherwise ARG is a
15523 TEMPLATE_DECL and is the real template to be
15525 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15526 arg
= TYPE_NAME (arg
);
15528 r
= lookup_template_class (arg
,
15530 DECL_CONTEXT (arg
),
15531 /*entering_scope=*/0,
15533 return cp_build_qualified_type_real
15534 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15536 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15539 /* TEMPLATE_PARM_INDEX. */
15540 return convert_from_reference (unshare_expr (arg
));
15544 /* This can happen during the attempted tsubst'ing in
15545 unify. This means that we don't yet have any information
15546 about the template parameter in question. */
15549 /* Early in template argument deduction substitution, we don't
15550 want to reduce the level of 'auto', or it will be confused
15551 with a normal template parm in subsequent deduction.
15552 Similarly, don't reduce the level of template parameters to
15553 avoid mismatches when deducing their types. */
15554 if (complain
& tf_partial
)
15557 /* If we get here, we must have been looking at a parm for a
15558 more deeply nested template. Make a new version of this
15559 template parameter, but with a lower level. */
15562 case TEMPLATE_TYPE_PARM
:
15563 case TEMPLATE_TEMPLATE_PARM
:
15564 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15565 if (cp_type_quals (t
))
15567 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15568 r
= cp_build_qualified_type_real
15569 (r
, cp_type_quals (t
),
15570 complain
| (code
== TEMPLATE_TYPE_PARM
15571 ? tf_ignore_bad_quals
: 0));
15573 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15574 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15575 && (r
= (TEMPLATE_PARM_DESCENDANTS
15576 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15577 && (r
= TREE_TYPE (r
))
15578 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15579 /* Break infinite recursion when substituting the constraints
15580 of a constrained placeholder. */;
15581 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15582 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15583 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15584 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15585 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15586 && (TEMPLATE_PARM_LEVEL (r
)
15587 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15588 /* Cache the simple case of lowering a type parameter. */
15593 TEMPLATE_TYPE_PARM_INDEX (r
)
15594 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15595 r
, levels
, args
, complain
);
15596 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15597 TYPE_MAIN_VARIANT (r
) = r
;
15598 TYPE_POINTER_TO (r
) = NULL_TREE
;
15599 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15601 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15603 /* Propagate constraints on placeholders since they are
15604 only instantiated during satisfaction. */
15605 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15606 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15607 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15609 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15610 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15614 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15615 /* We have reduced the level of the template
15616 template parameter, but not the levels of its
15617 template parameters, so canonical_type_parameter
15618 will not be able to find the canonical template
15619 template parameter for this level. Thus, we
15620 require structural equality checking to compare
15621 TEMPLATE_TEMPLATE_PARMs. */
15622 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15623 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15624 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15626 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15628 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15630 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15631 /* We might need to substitute into the types of non-type
15632 template parameters. */
15633 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15634 complain
, in_decl
);
15635 if (tmpl
== error_mark_node
)
15636 return error_mark_node
;
15637 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15638 complain
, in_decl
);
15639 if (argvec
== error_mark_node
)
15640 return error_mark_node
;
15642 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15643 = build_template_info (tmpl
, argvec
);
15648 case TEMPLATE_PARM_INDEX
:
15649 /* OK, now substitute the type of the non-type parameter. We
15650 couldn't do it earlier because it might be an auto parameter,
15651 and we wouldn't need to if we had an argument. */
15652 type
= tsubst (type
, args
, complain
, in_decl
);
15653 if (type
== error_mark_node
)
15654 return error_mark_node
;
15655 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15659 gcc_unreachable ();
15666 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15669 /* We should never be tsubsting a binfo. */
15670 gcc_unreachable ();
15673 /* A vector of template arguments. */
15674 gcc_assert (!type
);
15675 return tsubst_template_args (t
, args
, complain
, in_decl
);
15678 case REFERENCE_TYPE
:
15680 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15685 Type deduction may fail for any of the following
15688 -- Attempting to create a pointer to reference type.
15689 -- Attempting to create a reference to a reference type or
15690 a reference to void.
15692 Core issue 106 says that creating a reference to a reference
15693 during instantiation is no longer a cause for failure. We
15694 only enforce this check in strict C++98 mode. */
15695 if ((TYPE_REF_P (type
)
15696 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15697 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15699 static location_t last_loc
;
15701 /* We keep track of the last time we issued this error
15702 message to avoid spewing a ton of messages during a
15703 single bad template instantiation. */
15704 if (complain
& tf_error
15705 && last_loc
!= input_location
)
15707 if (VOID_TYPE_P (type
))
15708 error ("forming reference to void");
15709 else if (code
== POINTER_TYPE
)
15710 error ("forming pointer to reference type %qT", type
);
15712 error ("forming reference to reference type %qT", type
);
15713 last_loc
= input_location
;
15716 return error_mark_node
;
15718 else if (TREE_CODE (type
) == FUNCTION_TYPE
15719 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15720 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15722 if (complain
& tf_error
)
15724 if (code
== POINTER_TYPE
)
15725 error ("forming pointer to qualified function type %qT",
15728 error ("forming reference to qualified function type %qT",
15731 return error_mark_node
;
15733 else if (code
== POINTER_TYPE
)
15735 r
= build_pointer_type (type
);
15736 if (TREE_CODE (type
) == METHOD_TYPE
)
15737 r
= build_ptrmemfunc_type (r
);
15739 else if (TYPE_REF_P (type
))
15740 /* In C++0x, during template argument substitution, when there is an
15741 attempt to create a reference to a reference type, reference
15742 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15744 "If a template-argument for a template-parameter T names a type
15745 that is a reference to a type A, an attempt to create the type
15746 'lvalue reference to cv T' creates the type 'lvalue reference to
15747 A,' while an attempt to create the type type rvalue reference to
15748 cv T' creates the type T"
15750 r
= cp_build_reference_type
15752 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15754 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15755 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15757 if (r
!= error_mark_node
)
15758 /* Will this ever be needed for TYPE_..._TO values? */
15765 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15766 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15770 Type deduction may fail for any of the following
15773 -- Attempting to create "pointer to member of T" when T
15774 is not a class type. */
15775 if (complain
& tf_error
)
15776 error ("creating pointer to member of non-class type %qT", r
);
15777 return error_mark_node
;
15779 if (TYPE_REF_P (type
))
15781 if (complain
& tf_error
)
15782 error ("creating pointer to member reference type %qT", type
);
15783 return error_mark_node
;
15785 if (VOID_TYPE_P (type
))
15787 if (complain
& tf_error
)
15788 error ("creating pointer to member of type void");
15789 return error_mark_node
;
15791 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15792 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15794 /* The type of the implicit object parameter gets its
15795 cv-qualifiers from the FUNCTION_TYPE. */
15798 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15799 type_memfn_rqual (type
));
15800 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15801 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15805 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15809 case FUNCTION_TYPE
:
15814 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15815 if (fntype
== error_mark_node
)
15816 return error_mark_node
;
15818 /* Substitute the exception specification. */
15819 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15820 /*defer_ok*/fndecl_type
);
15821 if (specs
== error_mark_node
)
15822 return error_mark_node
;
15824 fntype
= build_exception_variant (fntype
, specs
);
15829 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15830 if (domain
== error_mark_node
)
15831 return error_mark_node
;
15833 /* As an optimization, we avoid regenerating the array type if
15834 it will obviously be the same as T. */
15835 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15838 /* These checks should match the ones in create_array_type_for_decl.
15842 The deduction may fail for any of the following reasons:
15844 -- Attempting to create an array with an element type that
15845 is void, a function type, or a reference type, or [DR337]
15846 an abstract class type. */
15847 if (VOID_TYPE_P (type
)
15848 || TREE_CODE (type
) == FUNCTION_TYPE
15849 || (TREE_CODE (type
) == ARRAY_TYPE
15850 && TYPE_DOMAIN (type
) == NULL_TREE
)
15851 || TYPE_REF_P (type
))
15853 if (complain
& tf_error
)
15854 error ("creating array of %qT", type
);
15855 return error_mark_node
;
15858 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15859 return error_mark_node
;
15861 r
= build_cplus_array_type (type
, domain
);
15863 if (!valid_array_size_p (input_location
, r
, in_decl
,
15864 (complain
& tf_error
)))
15865 return error_mark_node
;
15867 if (TYPE_USER_ALIGN (t
))
15869 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15870 TYPE_USER_ALIGN (r
) = 1;
15876 case TYPENAME_TYPE
:
15878 tree ctx
= TYPE_CONTEXT (t
);
15879 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15881 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15882 if (ctx
== error_mark_node
15883 || TREE_VEC_LENGTH (ctx
) > 1)
15884 return error_mark_node
;
15885 if (TREE_VEC_LENGTH (ctx
) == 0)
15887 if (complain
& tf_error
)
15888 error ("%qD is instantiated for an empty pack",
15889 TYPENAME_TYPE_FULLNAME (t
));
15890 return error_mark_node
;
15892 ctx
= TREE_VEC_ELT (ctx
, 0);
15895 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15896 /*entering_scope=*/1);
15897 if (ctx
== error_mark_node
)
15898 return error_mark_node
;
15900 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15901 complain
, in_decl
);
15902 if (f
== error_mark_node
)
15903 return error_mark_node
;
15905 if (!MAYBE_CLASS_TYPE_P (ctx
))
15907 if (complain
& tf_error
)
15908 error ("%qT is not a class, struct, or union type", ctx
);
15909 return error_mark_node
;
15911 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15913 /* Normally, make_typename_type does not require that the CTX
15914 have complete type in order to allow things like:
15916 template <class T> struct S { typename S<T>::X Y; };
15918 But, such constructs have already been resolved by this
15919 point, so here CTX really should have complete type, unless
15920 it's a partial instantiation. */
15921 ctx
= complete_type (ctx
);
15922 if (!COMPLETE_TYPE_P (ctx
))
15924 if (complain
& tf_error
)
15925 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15926 return error_mark_node
;
15930 f
= make_typename_type (ctx
, f
, typename_type
,
15931 complain
| tf_keep_type_decl
);
15932 if (f
== error_mark_node
)
15934 if (TREE_CODE (f
) == TYPE_DECL
)
15936 complain
|= tf_ignore_bad_quals
;
15940 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15942 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15944 if (complain
& tf_error
)
15945 error ("%qT resolves to %qT, which is not an enumeration type",
15948 return error_mark_node
;
15950 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15952 if (complain
& tf_error
)
15953 error ("%qT resolves to %qT, which is not a class type",
15956 return error_mark_node
;
15960 return cp_build_qualified_type_real
15961 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15964 case UNBOUND_CLASS_TEMPLATE
:
15966 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15967 in_decl
, /*entering_scope=*/1);
15968 tree name
= TYPE_IDENTIFIER (t
);
15969 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15971 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15972 return error_mark_node
;
15975 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15976 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15983 ++cp_unevaluated_operand
;
15984 ++c_inhibit_evaluation_warnings
;
15986 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15988 /*integral_constant_expression_p=*/false);
15990 --cp_unevaluated_operand
;
15991 --c_inhibit_evaluation_warnings
;
15993 type
= finish_typeof (type
);
15994 return cp_build_qualified_type_real (type
,
15996 | cp_type_quals (type
),
16000 case DECLTYPE_TYPE
:
16004 ++cp_unevaluated_operand
;
16005 ++c_inhibit_evaluation_warnings
;
16007 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16008 complain
|tf_decltype
, in_decl
,
16009 /*function_p*/false,
16010 /*integral_constant_expression*/false);
16012 --cp_unevaluated_operand
;
16013 --c_inhibit_evaluation_warnings
;
16015 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16016 type
= lambda_capture_field_type (type
,
16017 false /*explicit_init*/,
16018 DECLTYPE_FOR_REF_CAPTURE (t
));
16019 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16020 type
= lambda_proxy_type (type
);
16023 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16024 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16026 /* In a template ~id could be either a complement expression
16027 or an unqualified-id naming a destructor; if instantiating
16028 it produces an expression, it's not an id-expression or
16031 type
= finish_decltype_type (type
, id
, complain
);
16033 return cp_build_qualified_type_real (type
,
16035 | cp_type_quals (type
),
16036 complain
| tf_ignore_bad_quals
);
16039 case UNDERLYING_TYPE
:
16041 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16042 complain
, in_decl
);
16043 return finish_underlying_type (type
);
16046 case TYPE_ARGUMENT_PACK
:
16047 case NONTYPE_ARGUMENT_PACK
:
16051 if (code
== NONTYPE_ARGUMENT_PACK
)
16052 r
= make_node (code
);
16054 r
= cxx_make_type (code
);
16056 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
16057 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
16058 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
16076 /* We should use one of the expression tsubsts for these codes. */
16077 gcc_unreachable ();
16080 sorry ("use of %qs in template", get_tree_code_name (code
));
16081 return error_mark_node
;
16085 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16086 expression on the left-hand side of the "." or "->" operator. We
16087 only do the lookup if we had a dependent BASELINK. Otherwise we
16088 adjust it onto the instantiated heirarchy. */
16091 tsubst_baselink (tree baselink
, tree object_type
,
16092 tree args
, tsubst_flags_t complain
, tree in_decl
)
16094 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16095 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16096 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16098 tree optype
= BASELINK_OPTYPE (baselink
);
16099 optype
= tsubst (optype
, args
, complain
, in_decl
);
16101 tree template_args
= NULL_TREE
;
16102 bool template_id_p
= false;
16103 tree fns
= BASELINK_FUNCTIONS (baselink
);
16104 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16106 template_id_p
= true;
16107 template_args
= TREE_OPERAND (fns
, 1);
16108 fns
= TREE_OPERAND (fns
, 0);
16110 template_args
= tsubst_template_args (template_args
, args
,
16111 complain
, in_decl
);
16114 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16115 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16116 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16120 tree name
= OVL_NAME (fns
);
16121 if (IDENTIFIER_CONV_OP_P (name
))
16122 name
= make_conv_op_name (optype
);
16124 if (name
== complete_dtor_identifier
)
16125 /* Treat as-if non-dependent below. */
16126 dependent_p
= false;
16128 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16132 if ((complain
& tf_error
)
16133 && constructor_name_p (name
, qualifying_scope
))
16134 error ("cannot call constructor %<%T::%D%> directly",
16135 qualifying_scope
, name
);
16136 return error_mark_node
;
16139 if (BASELINK_P (baselink
))
16140 fns
= BASELINK_FUNCTIONS (baselink
);
16143 /* We're going to overwrite pieces below, make a duplicate. */
16144 baselink
= copy_node (baselink
);
16146 /* If lookup found a single function, mark it as used at this point.
16147 (If lookup found multiple functions the one selected later by
16148 overload resolution will be marked as used at that point.) */
16149 if (!template_id_p
&& !really_overloaded_fn (fns
))
16151 tree fn
= OVL_FIRST (fns
);
16152 bool ok
= mark_used (fn
, complain
);
16153 if (!ok
&& !(complain
& tf_error
))
16154 return error_mark_node
;
16155 if (ok
&& BASELINK_P (baselink
))
16156 /* We might have instantiated an auto function. */
16157 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16160 if (BASELINK_P (baselink
))
16162 /* Add back the template arguments, if present. */
16164 BASELINK_FUNCTIONS (baselink
)
16165 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16167 /* Update the conversion operator type. */
16168 BASELINK_OPTYPE (baselink
) = optype
;
16172 object_type
= current_class_type
;
16174 if (qualified_p
|| !dependent_p
)
16176 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16180 /* We need to call adjust_result_of_qualified_name_lookup in case the
16181 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16182 so that we still get virtual function binding. */
16183 BASELINK_QUALIFIED_P (baselink
) = false;
16189 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16190 true if the qualified-id will be a postfix-expression in-and-of
16191 itself; false if more of the postfix-expression follows the
16192 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16196 tsubst_qualified_id (tree qualified_id
, tree args
,
16197 tsubst_flags_t complain
, tree in_decl
,
16198 bool done
, bool address_p
)
16204 tree template_args
;
16205 location_t loc
= UNKNOWN_LOCATION
;
16207 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16209 /* Figure out what name to look up. */
16210 name
= TREE_OPERAND (qualified_id
, 1);
16211 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16213 is_template
= true;
16214 loc
= EXPR_LOCATION (name
);
16215 template_args
= TREE_OPERAND (name
, 1);
16217 template_args
= tsubst_template_args (template_args
, args
,
16218 complain
, in_decl
);
16219 if (template_args
== error_mark_node
)
16220 return error_mark_node
;
16221 name
= TREE_OPERAND (name
, 0);
16225 is_template
= false;
16226 template_args
= NULL_TREE
;
16229 /* Substitute into the qualifying scope. When there are no ARGS, we
16230 are just trying to simplify a non-dependent expression. In that
16231 case the qualifying scope may be dependent, and, in any case,
16232 substituting will not help. */
16233 scope
= TREE_OPERAND (qualified_id
, 0);
16236 scope
= tsubst (scope
, args
, complain
, in_decl
);
16237 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16242 if (dependent_scope_p (scope
))
16245 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16246 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16247 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16248 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16252 if (!BASELINK_P (name
) && !DECL_P (expr
))
16254 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16256 /* A BIT_NOT_EXPR is used to represent a destructor. */
16257 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16259 error ("qualifying type %qT does not match destructor name ~%qT",
16260 scope
, TREE_OPERAND (expr
, 0));
16261 expr
= error_mark_node
;
16264 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16265 LOOK_want::NORMAL
, false);
16268 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16269 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16270 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16272 if (complain
& tf_error
)
16274 error ("dependent-name %qE is parsed as a non-type, but "
16275 "instantiation yields a type", qualified_id
);
16276 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16278 return error_mark_node
;
16284 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16286 return error_mark_node
;
16287 /* Remember that there was a reference to this entity. */
16288 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16289 return error_mark_node
;
16292 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16294 if (complain
& tf_error
)
16295 qualified_name_lookup_error (scope
,
16296 TREE_OPERAND (qualified_id
, 1),
16297 expr
, input_location
);
16298 return error_mark_node
;
16303 /* We may be repeating a check already done during parsing, but
16304 if it was well-formed and passed then, it will pass again
16305 now, and if it didn't, we wouldn't have got here. The case
16306 we want to catch is when we couldn't tell then, and can now,
16307 namely when templ prior to substitution was an
16309 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16310 return error_mark_node
;
16312 if (variable_template_p (expr
))
16313 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16316 expr
= lookup_template_function (expr
, template_args
);
16319 if (expr
== error_mark_node
&& complain
& tf_error
)
16320 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16321 expr
, input_location
);
16322 else if (TYPE_P (scope
))
16324 expr
= (adjust_result_of_qualified_name_lookup
16325 (expr
, scope
, current_nonlambda_class_type ()));
16326 expr
= (finish_qualified_id_expr
16327 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16328 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16329 /*template_arg_p=*/false, complain
));
16332 /* Expressions do not generally have reference type. */
16333 if (TREE_CODE (expr
) != SCOPE_REF
16334 /* However, if we're about to form a pointer-to-member, we just
16335 want the referenced member referenced. */
16336 && TREE_CODE (expr
) != OFFSET_REF
)
16337 expr
= convert_from_reference (expr
);
16339 if (REF_PARENTHESIZED_P (qualified_id
))
16340 expr
= force_paren_expr (expr
);
16345 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16346 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16350 tsubst_init (tree init
, tree decl
, tree args
,
16351 tsubst_flags_t complain
, tree in_decl
)
16356 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16358 tree type
= TREE_TYPE (decl
);
16360 if (!init
&& type
!= error_mark_node
)
16362 if (tree auto_node
= type_uses_auto (type
))
16364 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16366 if (complain
& tf_error
)
16367 error ("initializer for %q#D expands to an empty list "
16368 "of expressions", decl
);
16369 return error_mark_node
;
16372 else if (!dependent_type_p (type
))
16374 /* If we had an initializer but it
16375 instantiated to nothing,
16376 value-initialize the object. This will
16377 only occur when the initializer was a
16378 pack expansion where the parameter packs
16379 used in that expansion were of length
16381 init
= build_value_init (type
, complain
);
16382 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16383 init
= get_target_expr_sfinae (init
, complain
);
16384 if (TREE_CODE (init
) == TARGET_EXPR
)
16385 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16392 /* If T is a reference to a dependent member of the current instantiation C and
16393 we are trying to refer to that member in a partial instantiation of C,
16394 return a SCOPE_REF; otherwise, return NULL_TREE.
16396 This can happen when forming a C++17 deduction guide, as in PR96199. */
16399 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16402 if (cxx_dialect
< cxx17
)
16405 tree ctx
= context_for_name_lookup (t
);
16406 if (!CLASS_TYPE_P (ctx
))
16409 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16410 if (dependent_scope_p (ctx
))
16411 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16412 /*template_p=*/false);
16417 /* Like tsubst, but deals with expressions. This function just replaces
16418 template parms; to finish processing the resultant expression, use
16419 tsubst_copy_and_build or tsubst_expr. */
16422 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16424 enum tree_code code
;
16427 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16430 code
= TREE_CODE (t
);
16435 r
= retrieve_local_specialization (t
);
16437 if (r
== NULL_TREE
)
16439 /* We get here for a use of 'this' in an NSDMI. */
16440 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16441 return current_class_ptr
;
16443 /* This can happen for a parameter name used later in a function
16444 declaration (such as in a late-specified return type). Just
16445 make a dummy decl, since it's only used for its type. */
16446 gcc_assert (cp_unevaluated_operand
!= 0);
16447 r
= tsubst_decl (t
, args
, complain
);
16448 /* Give it the template pattern as its context; its true context
16449 hasn't been instantiated yet and this is good enough for
16451 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16454 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16455 r
= argument_pack_select_arg (r
);
16456 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16457 return error_mark_node
;
16465 if (DECL_TEMPLATE_PARM_P (t
))
16466 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16467 /* There is no need to substitute into namespace-scope
16469 if (DECL_NAMESPACE_SCOPE_P (t
))
16471 /* If ARGS is NULL, then T is known to be non-dependent. */
16472 if (args
== NULL_TREE
)
16473 return scalar_constant_value (t
);
16475 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16478 /* Unfortunately, we cannot just call lookup_name here.
16481 template <int I> int f() {
16483 struct S { void g() { E e = a; } };
16486 When we instantiate f<7>::S::g(), say, lookup_name is not
16487 clever enough to find f<7>::a. */
16489 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16490 /*entering_scope=*/0);
16492 for (v
= TYPE_VALUES (enum_type
);
16494 v
= TREE_CHAIN (v
))
16495 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16496 return TREE_VALUE (v
);
16498 /* We didn't find the name. That should never happen; if
16499 name-lookup found it during preliminary parsing, we
16500 should find it again here during instantiation. */
16501 gcc_unreachable ();
16506 if (DECL_CONTEXT (t
))
16510 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16511 /*entering_scope=*/1);
16512 if (ctx
!= DECL_CONTEXT (t
))
16514 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16517 if (complain
& tf_error
)
16518 error ("using invalid field %qD", t
);
16519 return error_mark_node
;
16528 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16531 case FUNCTION_DECL
:
16532 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16533 r
= tsubst (t
, args
, complain
, in_decl
);
16534 else if (local_variable_p (t
)
16535 && uses_template_parms (DECL_CONTEXT (t
)))
16537 r
= retrieve_local_specialization (t
);
16538 if (r
== NULL_TREE
)
16540 /* First try name lookup to find the instantiation. */
16541 r
= lookup_name (DECL_NAME (t
));
16546 /* During error-recovery we may find a non-variable,
16547 even an OVERLOAD: just bail out and avoid ICEs and
16548 duplicate diagnostics (c++/62207). */
16549 gcc_assert (seen_error ());
16550 return error_mark_node
;
16552 if (!is_capture_proxy (r
))
16554 /* Make sure the one we found is the one we want. */
16555 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16556 if (ctx
!= DECL_CONTEXT (r
))
16565 /* This can happen for a variable used in a
16566 late-specified return type of a local lambda, or for a
16567 local static or constant. Building a new VAR_DECL
16568 should be OK in all those cases. */
16569 r
= tsubst_decl (t
, args
, complain
);
16570 if (local_specializations
)
16571 /* Avoid infinite recursion (79640). */
16572 register_local_specialization (r
, t
);
16573 if (decl_maybe_constant_var_p (r
))
16575 /* We can't call cp_finish_decl, so handle the
16576 initializer by hand. */
16577 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16578 complain
, in_decl
);
16579 if (!processing_template_decl
)
16580 init
= maybe_constant_init (init
);
16581 if (processing_template_decl
16582 ? potential_constant_expression (init
)
16583 : reduced_constant_expression_p (init
))
16584 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16585 = TREE_CONSTANT (r
) = true;
16586 DECL_INITIAL (r
) = init
;
16587 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16589 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16590 complain
, adc_variable_type
);
16592 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16593 || decl_constant_var_p (r
)
16595 if (!processing_template_decl
16596 && !TREE_STATIC (r
))
16597 r
= process_outer_var_ref (r
, complain
);
16599 /* Remember this for subsequent uses. */
16600 if (local_specializations
)
16601 register_local_specialization (r
, t
);
16603 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16604 r
= argument_pack_select_arg (r
);
16608 if (!mark_used (r
, complain
))
16609 return error_mark_node
;
16612 case NAMESPACE_DECL
:
16619 return tsubst_baselink (t
, current_nonlambda_class_type (),
16620 args
, complain
, in_decl
);
16622 case TEMPLATE_DECL
:
16623 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16624 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16625 args
, complain
, in_decl
);
16626 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16627 return tsubst (t
, args
, complain
, in_decl
);
16628 else if (DECL_CLASS_SCOPE_P (t
)
16629 && uses_template_parms (DECL_CONTEXT (t
)))
16631 /* Template template argument like the following example need
16634 template <template <class> class TT> struct C {};
16635 template <class T> struct D {
16636 template <class U> struct E {};
16641 We are processing the template argument `E' in #1 for
16642 the template instantiation #2. Originally, `E' is a
16643 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16644 have to substitute this with one having context `D<int>'. */
16646 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16647 if (dependent_scope_p (context
))
16649 /* When rewriting a constructor into a deduction guide, a
16650 non-dependent name can become dependent, so memtmpl<args>
16651 becomes context::template memtmpl<args>. */
16652 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16653 return build_qualified_name (type
, context
, DECL_NAME (t
),
16656 return lookup_field (context
, DECL_NAME(t
), 0, false);
16659 /* Ordinary template template argument. */
16662 case NON_LVALUE_EXPR
:
16663 case VIEW_CONVERT_EXPR
:
16665 /* Handle location wrappers by substituting the wrapped node
16666 first, *then* reusing the resulting type. Doing the type
16667 first ensures that we handle template parameters and
16668 parameter pack expansions. */
16669 if (location_wrapper_p (t
))
16671 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16672 complain
, in_decl
);
16673 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16675 tree op
= TREE_OPERAND (t
, 0);
16676 if (code
== VIEW_CONVERT_EXPR
16677 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16679 /* Wrapper to make a C++20 template parameter object const. */
16680 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16681 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16683 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16684 return build1 (code
, type
, op
);
16688 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16689 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16690 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16694 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16695 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16697 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16698 op
= build1 (code
, TREE_TYPE (op
), op
);
16699 REF_PARENTHESIZED_P (op
) = true;
16702 /* We shouldn't see any other uses of these in templates. */
16703 gcc_unreachable ();
16707 case REINTERPRET_CAST_EXPR
:
16708 case CONST_CAST_EXPR
:
16709 case STATIC_CAST_EXPR
:
16710 case DYNAMIC_CAST_EXPR
:
16711 case IMPLICIT_CONV_EXPR
:
16715 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16716 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16717 return build1 (code
, type
, op0
);
16721 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16722 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16724 tree expanded
, op
= TREE_OPERAND (t
, 0);
16727 if (SIZEOF_EXPR_TYPE_P (t
))
16728 op
= TREE_TYPE (op
);
16730 ++cp_unevaluated_operand
;
16731 ++c_inhibit_evaluation_warnings
;
16732 /* We only want to compute the number of arguments. */
16733 if (PACK_EXPANSION_P (op
))
16734 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16736 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16737 args
, complain
, in_decl
);
16738 --cp_unevaluated_operand
;
16739 --c_inhibit_evaluation_warnings
;
16741 if (TREE_CODE (expanded
) == TREE_VEC
)
16743 len
= TREE_VEC_LENGTH (expanded
);
16744 /* Set TREE_USED for the benefit of -Wunused. */
16745 for (int i
= 0; i
< len
; i
++)
16746 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16747 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16750 if (expanded
== error_mark_node
)
16751 return error_mark_node
;
16752 else if (PACK_EXPANSION_P (expanded
)
16753 || (TREE_CODE (expanded
) == TREE_VEC
16754 && pack_expansion_args_count (expanded
)))
16757 if (PACK_EXPANSION_P (expanded
))
16759 else if (TREE_VEC_LENGTH (expanded
) == 1)
16760 expanded
= TREE_VEC_ELT (expanded
, 0);
16762 expanded
= make_argument_pack (expanded
);
16764 if (TYPE_P (expanded
))
16765 return cxx_sizeof_or_alignof_type (input_location
,
16766 expanded
, SIZEOF_EXPR
,
16768 complain
& tf_error
);
16770 return cxx_sizeof_or_alignof_expr (input_location
,
16771 expanded
, SIZEOF_EXPR
,
16772 complain
& tf_error
);
16775 return build_int_cst (size_type_node
, len
);
16777 if (SIZEOF_EXPR_TYPE_P (t
))
16779 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16780 args
, complain
, in_decl
);
16781 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16782 r
= build1 (SIZEOF_EXPR
,
16783 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16784 SIZEOF_EXPR_TYPE_P (r
) = 1;
16791 case TRUTH_NOT_EXPR
:
16794 case UNARY_PLUS_EXPR
: /* Unary + */
16796 case AT_ENCODE_EXPR
:
16800 case REALPART_EXPR
:
16801 case IMAGPART_EXPR
:
16804 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16805 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16806 r
= build1 (code
, type
, op0
);
16807 if (code
== ALIGNOF_EXPR
)
16808 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16812 case COMPONENT_REF
:
16817 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16818 name
= TREE_OPERAND (t
, 1);
16819 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16821 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16822 complain
, in_decl
);
16823 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16825 else if (TREE_CODE (name
) == SCOPE_REF
16826 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16828 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16829 complain
, in_decl
);
16830 name
= TREE_OPERAND (name
, 1);
16831 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16832 complain
, in_decl
);
16833 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16834 name
= build_qualified_name (/*type=*/NULL_TREE
,
16836 /*template_p=*/false);
16838 else if (BASELINK_P (name
))
16839 name
= tsubst_baselink (name
,
16840 non_reference (TREE_TYPE (object
)),
16844 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16845 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16851 case TRUNC_DIV_EXPR
:
16852 case CEIL_DIV_EXPR
:
16853 case FLOOR_DIV_EXPR
:
16854 case ROUND_DIV_EXPR
:
16855 case EXACT_DIV_EXPR
:
16859 case TRUNC_MOD_EXPR
:
16860 case FLOOR_MOD_EXPR
:
16861 case TRUTH_ANDIF_EXPR
:
16862 case TRUTH_ORIF_EXPR
:
16863 case TRUTH_AND_EXPR
:
16864 case TRUTH_OR_EXPR
:
16875 case COMPOUND_EXPR
:
16878 case PREDECREMENT_EXPR
:
16879 case PREINCREMENT_EXPR
:
16880 case POSTDECREMENT_EXPR
:
16881 case POSTINCREMENT_EXPR
:
16883 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16884 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16885 return build_nt (code
, op0
, op1
);
16890 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16891 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16892 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16893 QUALIFIED_NAME_IS_TEMPLATE (t
));
16898 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16899 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16900 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16905 int n
= VL_EXP_OPERAND_LENGTH (t
);
16906 tree result
= build_vl_exp (CALL_EXPR
, n
);
16908 for (i
= 0; i
< n
; i
++)
16909 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16910 complain
, in_decl
);
16916 case PSEUDO_DTOR_EXPR
:
16917 case VEC_PERM_EXPR
:
16919 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16920 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16921 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16922 r
= build_nt (code
, op0
, op1
, op2
);
16923 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16929 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16930 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16931 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16932 r
= build_nt (code
, op0
, op1
, op2
);
16933 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16939 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16940 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16941 r
= build_nt (code
, op0
, op1
);
16942 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16943 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16947 case TEMPLATE_ID_EXPR
:
16949 /* Substituted template arguments */
16950 tree fn
= TREE_OPERAND (t
, 0);
16951 tree targs
= TREE_OPERAND (t
, 1);
16953 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16955 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16957 return lookup_template_function (fn
, targs
);
16962 tree purpose
, value
, chain
;
16964 if (t
== void_list_node
)
16967 purpose
= TREE_PURPOSE (t
);
16969 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16970 value
= TREE_VALUE (t
);
16972 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16973 chain
= TREE_CHAIN (t
);
16974 if (chain
&& chain
!= void_type_node
)
16975 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16976 if (purpose
== TREE_PURPOSE (t
)
16977 && value
== TREE_VALUE (t
)
16978 && chain
== TREE_CHAIN (t
))
16980 return tree_cons (purpose
, value
, chain
);
16985 case ENUMERAL_TYPE
:
16987 case TEMPLATE_TYPE_PARM
:
16988 case TEMPLATE_TEMPLATE_PARM
:
16989 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16990 case TEMPLATE_PARM_INDEX
:
16992 case REFERENCE_TYPE
:
16994 case FUNCTION_TYPE
:
16997 case TYPENAME_TYPE
:
16998 case UNBOUND_CLASS_TEMPLATE
:
17000 case DECLTYPE_TYPE
:
17002 return tsubst (t
, args
, complain
, in_decl
);
17006 /* Fall through. */
17007 case IDENTIFIER_NODE
:
17008 if (IDENTIFIER_CONV_OP_P (t
))
17010 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17011 return make_conv_op_name (new_type
);
17017 /* This is handled by tsubst_copy_and_build. */
17018 gcc_unreachable ();
17022 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17023 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17024 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17027 case CLEANUP_POINT_EXPR
:
17028 /* We shouldn't have built any of these during initial template
17029 generation. Instead, they should be built during instantiation
17030 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17031 gcc_unreachable ();
17035 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17036 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17037 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17038 r
= build2 (code
, type
, op0
, op1
);
17039 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17040 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17041 && !(complain
& tf_error
))
17042 return error_mark_node
;
17046 case EXPR_PACK_EXPANSION
:
17047 error ("invalid use of pack expansion expression");
17048 return error_mark_node
;
17050 case NONTYPE_ARGUMENT_PACK
:
17051 error ("use %<...%> to expand argument pack");
17052 return error_mark_node
;
17055 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17062 /* Instantiate any typedefs in the type. */
17063 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17064 r
= fold_convert (type
, t
);
17065 gcc_assert (TREE_CODE (r
) == code
);
17071 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17073 if (type
!= TREE_TYPE (t
))
17076 TREE_TYPE (r
) = type
;
17082 /* These can sometimes show up in a partial instantiation, but never
17083 involve template parms. */
17084 gcc_assert (!uses_template_parms (t
));
17087 case UNARY_LEFT_FOLD_EXPR
:
17088 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17089 case UNARY_RIGHT_FOLD_EXPR
:
17090 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17091 case BINARY_LEFT_FOLD_EXPR
:
17092 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17093 case BINARY_RIGHT_FOLD_EXPR
:
17094 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17098 case DEBUG_BEGIN_STMT
:
17099 /* ??? There's no point in copying it for now, but maybe some
17100 day it will contain more information, such as a pointer back
17101 to the containing function, inlined copy or so. */
17104 case CO_AWAIT_EXPR
:
17105 return tsubst_expr (t
, args
, complain
, in_decl
,
17106 /*integral_constant_expression_p=*/false);
17110 /* We shouldn't get here, but keep going if !flag_checking. */
17112 gcc_unreachable ();
17117 /* Helper function for tsubst_omp_clauses, used for instantiation of
17118 OMP_CLAUSE_DECL of clauses. */
17121 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17122 tree in_decl
, tree
*iterator_cache
)
17124 if (decl
== NULL_TREE
)
17127 /* Handle OpenMP iterators. */
17128 if (TREE_CODE (decl
) == TREE_LIST
17129 && TREE_PURPOSE (decl
)
17130 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17133 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17134 ret
= iterator_cache
[1];
17138 begin_scope (sk_omp
, NULL
);
17139 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17141 *tp
= copy_node (it
);
17142 TREE_VEC_ELT (*tp
, 0)
17143 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17144 TREE_VEC_ELT (*tp
, 1)
17145 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17146 /*integral_constant_expression_p=*/false);
17147 TREE_VEC_ELT (*tp
, 2)
17148 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17149 /*integral_constant_expression_p=*/false);
17150 TREE_VEC_ELT (*tp
, 3)
17151 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17152 /*integral_constant_expression_p=*/false);
17153 TREE_CHAIN (*tp
) = NULL_TREE
;
17154 tp
= &TREE_CHAIN (*tp
);
17156 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17157 iterator_cache
[0] = TREE_PURPOSE (decl
);
17158 iterator_cache
[1] = ret
;
17160 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17165 /* Handle an OpenMP array section represented as a TREE_LIST (or
17166 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17167 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17168 TREE_LIST. We can handle it exactly the same as an array section
17169 (purpose, value, and a chain), even though the nomenclature
17170 (low_bound, length, etc) is different. */
17171 if (TREE_CODE (decl
) == TREE_LIST
)
17174 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17175 /*integral_constant_expression_p=*/false);
17176 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17177 /*integral_constant_expression_p=*/false);
17178 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17180 if (TREE_PURPOSE (decl
) == low_bound
17181 && TREE_VALUE (decl
) == length
17182 && TREE_CHAIN (decl
) == chain
)
17184 tree ret
= tree_cons (low_bound
, length
, chain
);
17185 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17186 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17189 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17190 /*integral_constant_expression_p=*/false);
17191 /* Undo convert_from_reference tsubst_expr could have called. */
17193 && REFERENCE_REF_P (ret
)
17194 && !REFERENCE_REF_P (decl
))
17195 ret
= TREE_OPERAND (ret
, 0);
17199 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17202 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17203 tree args
, tsubst_flags_t complain
, tree in_decl
)
17205 tree new_clauses
= NULL_TREE
, nc
, oc
;
17206 tree linear_no_step
= NULL_TREE
;
17207 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17209 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17211 nc
= copy_node (oc
);
17212 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17215 switch (OMP_CLAUSE_CODE (nc
))
17217 case OMP_CLAUSE_LASTPRIVATE
:
17218 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17220 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17221 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17222 in_decl
, /*integral_constant_expression_p=*/false);
17223 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17224 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17227 case OMP_CLAUSE_PRIVATE
:
17228 case OMP_CLAUSE_SHARED
:
17229 case OMP_CLAUSE_FIRSTPRIVATE
:
17230 case OMP_CLAUSE_COPYIN
:
17231 case OMP_CLAUSE_COPYPRIVATE
:
17232 case OMP_CLAUSE_UNIFORM
:
17233 case OMP_CLAUSE_DEPEND
:
17234 case OMP_CLAUSE_FROM
:
17235 case OMP_CLAUSE_TO
:
17236 case OMP_CLAUSE_MAP
:
17237 case OMP_CLAUSE_NONTEMPORAL
:
17238 case OMP_CLAUSE_USE_DEVICE_PTR
:
17239 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17240 case OMP_CLAUSE_IS_DEVICE_PTR
:
17241 case OMP_CLAUSE_INCLUSIVE
:
17242 case OMP_CLAUSE_EXCLUSIVE
:
17243 OMP_CLAUSE_DECL (nc
)
17244 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17245 in_decl
, iterator_cache
);
17247 case OMP_CLAUSE_TILE
:
17248 case OMP_CLAUSE_IF
:
17249 case OMP_CLAUSE_NUM_THREADS
:
17250 case OMP_CLAUSE_SCHEDULE
:
17251 case OMP_CLAUSE_COLLAPSE
:
17252 case OMP_CLAUSE_FINAL
:
17253 case OMP_CLAUSE_DEVICE
:
17254 case OMP_CLAUSE_DIST_SCHEDULE
:
17255 case OMP_CLAUSE_NUM_TEAMS
:
17256 case OMP_CLAUSE_THREAD_LIMIT
:
17257 case OMP_CLAUSE_SAFELEN
:
17258 case OMP_CLAUSE_SIMDLEN
:
17259 case OMP_CLAUSE_NUM_TASKS
:
17260 case OMP_CLAUSE_GRAINSIZE
:
17261 case OMP_CLAUSE_PRIORITY
:
17262 case OMP_CLAUSE_ORDERED
:
17263 case OMP_CLAUSE_HINT
:
17264 case OMP_CLAUSE_NUM_GANGS
:
17265 case OMP_CLAUSE_NUM_WORKERS
:
17266 case OMP_CLAUSE_VECTOR_LENGTH
:
17267 case OMP_CLAUSE_WORKER
:
17268 case OMP_CLAUSE_VECTOR
:
17269 case OMP_CLAUSE_ASYNC
:
17270 case OMP_CLAUSE_WAIT
:
17271 OMP_CLAUSE_OPERAND (nc
, 0)
17272 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17273 in_decl
, /*integral_constant_expression_p=*/false);
17275 case OMP_CLAUSE_REDUCTION
:
17276 case OMP_CLAUSE_IN_REDUCTION
:
17277 case OMP_CLAUSE_TASK_REDUCTION
:
17278 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17280 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17281 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17283 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17284 complain
, in_decl
);
17285 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17286 = build_qualified_name (NULL_TREE
, scope
,
17287 TREE_OPERAND (placeholder
, 1),
17291 gcc_assert (identifier_p (placeholder
));
17293 OMP_CLAUSE_DECL (nc
)
17294 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17297 case OMP_CLAUSE_GANG
:
17298 case OMP_CLAUSE_ALIGNED
:
17299 OMP_CLAUSE_DECL (nc
)
17300 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17302 OMP_CLAUSE_OPERAND (nc
, 1)
17303 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17304 in_decl
, /*integral_constant_expression_p=*/false);
17306 case OMP_CLAUSE_LINEAR
:
17307 OMP_CLAUSE_DECL (nc
)
17308 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17310 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17312 gcc_assert (!linear_no_step
);
17313 linear_no_step
= nc
;
17315 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17316 OMP_CLAUSE_LINEAR_STEP (nc
)
17317 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17318 complain
, in_decl
, NULL
);
17320 OMP_CLAUSE_LINEAR_STEP (nc
)
17321 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17323 /*integral_constant_expression_p=*/false);
17325 case OMP_CLAUSE_NOWAIT
:
17326 case OMP_CLAUSE_DEFAULT
:
17327 case OMP_CLAUSE_UNTIED
:
17328 case OMP_CLAUSE_MERGEABLE
:
17329 case OMP_CLAUSE_INBRANCH
:
17330 case OMP_CLAUSE_NOTINBRANCH
:
17331 case OMP_CLAUSE_PROC_BIND
:
17332 case OMP_CLAUSE_FOR
:
17333 case OMP_CLAUSE_PARALLEL
:
17334 case OMP_CLAUSE_SECTIONS
:
17335 case OMP_CLAUSE_TASKGROUP
:
17336 case OMP_CLAUSE_NOGROUP
:
17337 case OMP_CLAUSE_THREADS
:
17338 case OMP_CLAUSE_SIMD
:
17339 case OMP_CLAUSE_DEFAULTMAP
:
17340 case OMP_CLAUSE_ORDER
:
17341 case OMP_CLAUSE_BIND
:
17342 case OMP_CLAUSE_INDEPENDENT
:
17343 case OMP_CLAUSE_AUTO
:
17344 case OMP_CLAUSE_SEQ
:
17345 case OMP_CLAUSE_IF_PRESENT
:
17346 case OMP_CLAUSE_FINALIZE
:
17349 gcc_unreachable ();
17351 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17352 switch (OMP_CLAUSE_CODE (nc
))
17354 case OMP_CLAUSE_SHARED
:
17355 case OMP_CLAUSE_PRIVATE
:
17356 case OMP_CLAUSE_FIRSTPRIVATE
:
17357 case OMP_CLAUSE_LASTPRIVATE
:
17358 case OMP_CLAUSE_COPYPRIVATE
:
17359 case OMP_CLAUSE_LINEAR
:
17360 case OMP_CLAUSE_REDUCTION
:
17361 case OMP_CLAUSE_IN_REDUCTION
:
17362 case OMP_CLAUSE_TASK_REDUCTION
:
17363 case OMP_CLAUSE_USE_DEVICE_PTR
:
17364 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17365 case OMP_CLAUSE_IS_DEVICE_PTR
:
17366 case OMP_CLAUSE_INCLUSIVE
:
17367 case OMP_CLAUSE_EXCLUSIVE
:
17368 /* tsubst_expr on SCOPE_REF results in returning
17369 finish_non_static_data_member result. Undo that here. */
17370 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17371 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17372 == IDENTIFIER_NODE
))
17374 tree t
= OMP_CLAUSE_DECL (nc
);
17377 switch (TREE_CODE (v
))
17379 case COMPONENT_REF
:
17383 case POINTER_PLUS_EXPR
:
17384 v
= TREE_OPERAND (v
, 0);
17387 if (DECL_CONTEXT (v
) == current_function_decl
17388 && DECL_ARTIFICIAL (v
)
17389 && DECL_NAME (v
) == this_identifier
)
17390 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17397 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17398 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17399 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17400 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17401 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17403 tree decl
= OMP_CLAUSE_DECL (nc
);
17406 retrofit_lang_decl (decl
);
17407 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17416 new_clauses
= nreverse (new_clauses
);
17417 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17419 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17420 if (linear_no_step
)
17421 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17422 if (nc
== linear_no_step
)
17424 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17428 return new_clauses
;
17431 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17434 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17437 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17439 tree purpose
, value
, chain
;
17444 if (TREE_CODE (t
) != TREE_LIST
)
17445 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17446 /*function_p=*/false,
17447 /*integral_constant_expression_p=*/false);
17449 if (t
== void_list_node
)
17452 purpose
= TREE_PURPOSE (t
);
17454 purpose
= RECUR (purpose
);
17455 value
= TREE_VALUE (t
);
17458 if (TREE_CODE (value
) != LABEL_DECL
)
17459 value
= RECUR (value
);
17462 value
= lookup_label (DECL_NAME (value
));
17463 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17464 TREE_USED (value
) = 1;
17467 chain
= TREE_CHAIN (t
);
17468 if (chain
&& chain
!= void_type_node
)
17469 chain
= RECUR (chain
);
17470 return tree_cons (purpose
, value
, chain
);
17474 /* Used to temporarily communicate the list of #pragma omp parallel
17475 clauses to #pragma omp for instantiation if they are combined
17478 static tree
*omp_parallel_combined_clauses
;
17480 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17481 tree
*, unsigned int *);
17483 /* Substitute one OMP_FOR iterator. */
17486 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17487 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17488 tree args
, tsubst_flags_t complain
, tree in_decl
,
17489 bool integral_constant_expression_p
)
17491 #define RECUR(NODE) \
17492 tsubst_expr ((NODE), args, complain, in_decl, \
17493 integral_constant_expression_p)
17494 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17497 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17498 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17500 decl
= TREE_OPERAND (init
, 0);
17501 init
= TREE_OPERAND (init
, 1);
17502 tree decl_expr
= NULL_TREE
;
17503 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17506 bool decomp
= false;
17507 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17509 tree v
= DECL_VALUE_EXPR (decl
);
17510 if (TREE_CODE (v
) == ARRAY_REF
17511 && VAR_P (TREE_OPERAND (v
, 0))
17512 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17514 tree decomp_first
= NULL_TREE
;
17515 unsigned decomp_cnt
= 0;
17516 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17517 maybe_push_decl (d
);
17518 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17519 in_decl
, &decomp_first
, &decomp_cnt
);
17521 if (d
== error_mark_node
)
17522 decl
= error_mark_node
;
17524 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17526 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17528 tree v
= build_nt (ARRAY_REF
, d
,
17529 size_int (decomp_cnt
- i
- 1),
17530 NULL_TREE
, NULL_TREE
);
17531 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17532 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17534 fit_decomposition_lang_decl (decomp_first
, d
);
17535 decomp_first
= DECL_CHAIN (decomp_first
);
17539 decl
= tsubst_decl (decl
, args
, complain
);
17541 maybe_push_decl (decl
);
17543 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17545 /* We need to jump through some hoops to handle declarations in the
17546 init-statement, since we might need to handle auto deduction,
17547 but we need to keep control of initialization. */
17549 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17550 decl
= tsubst_decl (decl
, args
, complain
);
17554 if (TREE_CODE (decl
) == SCOPE_REF
)
17556 decl
= RECUR (decl
);
17557 if (TREE_CODE (decl
) == COMPONENT_REF
)
17561 switch (TREE_CODE (v
))
17563 case COMPONENT_REF
:
17567 case POINTER_PLUS_EXPR
:
17568 v
= TREE_OPERAND (v
, 0);
17571 if (DECL_CONTEXT (v
) == current_function_decl
17572 && DECL_ARTIFICIAL (v
)
17573 && DECL_NAME (v
) == this_identifier
)
17575 decl
= TREE_OPERAND (decl
, 1);
17576 decl
= omp_privatize_field (decl
, false);
17586 decl
= RECUR (decl
);
17588 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17590 init
= copy_node (init
);
17591 TREE_VEC_ELT (init
, 0)
17592 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17593 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17594 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17597 init
= RECUR (init
);
17599 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17601 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17602 if (TREE_CODE (o
) == TREE_LIST
)
17603 TREE_VEC_ELT (orig_declv
, i
)
17604 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17605 RECUR (TREE_VALUE (o
)),
17608 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17613 tree this_pre_body
= NULL_TREE
;
17614 tree orig_init
= NULL_TREE
;
17615 tree orig_decl
= NULL_TREE
;
17616 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17617 orig_init
, cond
, incr
);
17620 if (orig_declv
== NULL_TREE
)
17621 orig_declv
= copy_node (declv
);
17622 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17625 else if (orig_declv
)
17626 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17629 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17630 if (!range_for
&& auto_node
&& init
)
17632 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17634 gcc_assert (!type_dependent_expression_p (decl
));
17636 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17640 /* Declare the variable, but don't let that initialize it. */
17641 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17642 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17644 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17649 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17650 if (COMPARISON_CLASS_P (cond
)
17651 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17653 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17654 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17655 TREE_VEC_ELT (rhs
, 0)
17656 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17657 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17658 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17659 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17663 cond
= RECUR (cond
);
17664 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17665 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17667 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17668 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17669 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17670 NOP_EXPR
, rhs
, complain
);
17673 incr
= RECUR (incr
);
17674 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17675 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17677 TREE_VEC_ELT (declv
, i
) = decl
;
17678 TREE_VEC_ELT (initv
, i
) = init
;
17679 TREE_VEC_ELT (condv
, i
) = cond
;
17680 TREE_VEC_ELT (incrv
, i
) = incr
;
17686 /* Declare and initialize the variable. */
17694 for (j
= ((omp_parallel_combined_clauses
== NULL
17695 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17697 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17699 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17700 && OMP_CLAUSE_DECL (*pc
) == decl
)
17702 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17703 && OMP_CLAUSE_DECL (*pc
) == decl
)
17707 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17709 *pc
= OMP_CLAUSE_CHAIN (c
);
17710 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17713 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17714 && OMP_CLAUSE_DECL (*pc
) == decl
)
17716 error ("iteration variable %qD should not be firstprivate",
17718 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17720 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17721 && OMP_CLAUSE_DECL (*pc
) == decl
)
17723 error ("iteration variable %qD should not be reduction",
17725 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17728 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17733 if (*pc
== NULL_TREE
)
17735 tree c
= build_omp_clause (input_location
,
17736 TREE_CODE (t
) == OMP_LOOP
17737 ? OMP_CLAUSE_LASTPRIVATE
17738 : OMP_CLAUSE_PRIVATE
);
17739 OMP_CLAUSE_DECL (c
) = decl
;
17740 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17743 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17748 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17749 if (COMPARISON_CLASS_P (cond
))
17751 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17752 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17753 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17756 cond
= RECUR (cond
);
17757 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17758 switch (TREE_CODE (incr
))
17760 case PREINCREMENT_EXPR
:
17761 case PREDECREMENT_EXPR
:
17762 case POSTINCREMENT_EXPR
:
17763 case POSTDECREMENT_EXPR
:
17764 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17765 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17768 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17769 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17771 tree rhs
= TREE_OPERAND (incr
, 1);
17772 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17773 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17774 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17775 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17776 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17780 incr
= RECUR (incr
);
17783 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17784 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17786 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17787 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17788 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17789 TREE_TYPE (decl
), lhs
,
17790 RECUR (TREE_OPERAND (incr
, 2))));
17792 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17793 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17794 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17796 tree rhs
= TREE_OPERAND (incr
, 2);
17797 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17798 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17799 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17800 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17801 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17805 incr
= RECUR (incr
);
17808 incr
= RECUR (incr
);
17812 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17813 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17814 TREE_VEC_ELT (declv
, i
) = decl
;
17815 TREE_VEC_ELT (initv
, i
) = init
;
17816 TREE_VEC_ELT (condv
, i
) = cond
;
17817 TREE_VEC_ELT (incrv
, i
) = incr
;
17822 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17823 of OMP_TARGET's body. */
17826 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17828 *walk_subtrees
= 0;
17829 switch (TREE_CODE (*tp
))
17834 case STATEMENT_LIST
:
17835 *walk_subtrees
= 1;
17843 /* Helper function for tsubst_expr. For decomposition declaration
17844 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17845 also the corresponding decls representing the identifiers
17846 of the decomposition declaration. Return DECL if successful
17847 or error_mark_node otherwise, set *FIRST to the first decl
17848 in the list chained through DECL_CHAIN and *CNT to the number
17852 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17853 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17856 tree decl2
, decl3
, prev
= decl
;
17858 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17859 for (decl2
= DECL_CHAIN (pattern_decl
);
17862 && DECL_DECOMPOSITION_P (decl2
)
17863 && DECL_NAME (decl2
);
17864 decl2
= DECL_CHAIN (decl2
))
17866 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17868 gcc_assert (errorcount
);
17869 return error_mark_node
;
17872 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17873 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17874 tree v
= DECL_VALUE_EXPR (decl2
);
17875 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17876 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17877 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17878 SET_DECL_VALUE_EXPR (decl2
, v
);
17879 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17881 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17884 gcc_assert (errorcount
);
17885 decl
= error_mark_node
;
17888 maybe_push_decl (decl3
);
17889 if (error_operand_p (decl3
))
17890 decl
= error_mark_node
;
17891 else if (decl
!= error_mark_node
17892 && DECL_CHAIN (decl3
) != prev
17895 gcc_assert (errorcount
);
17896 decl
= error_mark_node
;
17905 /* Return the proper local_specialization for init-capture pack DECL. */
17908 lookup_init_capture_pack (tree decl
)
17910 /* We handle normal pack captures by forwarding to the specialization of the
17911 captured parameter. We can't do that for pack init-captures; we need them
17912 to have their own local_specialization. We created the individual
17913 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17914 when we process the DECL_EXPR for the pack init-capture in the template.
17915 So, how do we find them? We don't know the capture proxy pack when
17916 building the individual resulting proxies, and we don't know the
17917 individual proxies when instantiating the pack. What we have in common is
17920 So...when we instantiate the FIELD_DECL, we stick the result in
17921 local_specializations. Then at the DECL_EXPR we look up that result, see
17922 how many elements it has, synthesize the names, and look them up. */
17924 tree cname
= DECL_NAME (decl
);
17925 tree val
= DECL_VALUE_EXPR (decl
);
17926 tree field
= TREE_OPERAND (val
, 1);
17927 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17928 tree fpack
= retrieve_local_specialization (field
);
17929 if (fpack
== error_mark_node
)
17930 return error_mark_node
;
17933 tree vec
= NULL_TREE
;
17934 tree r
= NULL_TREE
;
17935 if (TREE_CODE (fpack
) == TREE_VEC
)
17937 len
= TREE_VEC_LENGTH (fpack
);
17938 vec
= make_tree_vec (len
);
17939 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17940 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17942 for (int i
= 0; i
< len
; ++i
)
17944 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17945 tree elt
= lookup_name (ename
);
17947 TREE_VEC_ELT (vec
, i
) = elt
;
17954 /* Like tsubst_copy for expressions, etc. but also does semantic
17958 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17959 bool integral_constant_expression_p
)
17961 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17962 #define RECUR(NODE) \
17963 tsubst_expr ((NODE), args, complain, in_decl, \
17964 integral_constant_expression_p)
17970 if (t
== NULL_TREE
|| t
== error_mark_node
)
17973 loc
= input_location
;
17974 if (location_t eloc
= cp_expr_location (t
))
17975 input_location
= eloc
;
17976 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17977 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17979 switch (TREE_CODE (t
))
17981 case STATEMENT_LIST
:
17983 tree_stmt_iterator i
;
17984 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17985 RECUR (tsi_stmt (i
));
17989 case CTOR_INITIALIZER
:
17990 finish_mem_initializers (tsubst_initializer_list
17991 (TREE_OPERAND (t
, 0), args
));
17995 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17998 case CO_RETURN_EXPR
:
17999 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18002 case CO_YIELD_EXPR
:
18003 stmt
= finish_co_yield_expr (input_location
,
18004 RECUR (TREE_OPERAND (t
, 0)));
18008 case CO_AWAIT_EXPR
:
18009 stmt
= finish_co_await_expr (input_location
,
18010 RECUR (TREE_OPERAND (t
, 0)));
18015 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18016 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18017 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18019 finish_expr_stmt (tmp
);
18023 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18028 tree decl
, pattern_decl
;
18031 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18032 if (TREE_CODE (decl
) == LABEL_DECL
)
18033 finish_label_decl (DECL_NAME (decl
));
18034 else if (TREE_CODE (decl
) == USING_DECL
)
18036 tree scope
= USING_DECL_SCOPE (decl
);
18037 tree name
= DECL_NAME (decl
);
18039 scope
= tsubst (scope
, args
, complain
, in_decl
);
18040 finish_nonmember_using_decl (scope
, name
);
18042 else if (is_capture_proxy (decl
)
18043 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18045 /* We're in tsubst_lambda_expr, we've already inserted a new
18046 capture proxy, so look it up and register it. */
18048 if (!DECL_PACK_P (decl
))
18050 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18051 LOOK_want::HIDDEN_LAMBDA
);
18052 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18054 else if (is_normal_capture_proxy (decl
))
18056 inst
= (retrieve_local_specialization
18057 (DECL_CAPTURED_VARIABLE (decl
)));
18058 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18059 || DECL_PACK_P (inst
));
18062 inst
= lookup_init_capture_pack (decl
);
18064 register_local_specialization (inst
, decl
);
18067 else if (DECL_PRETTY_FUNCTION_P (decl
))
18068 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18070 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18071 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18072 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18073 /* Don't copy the old closure; we'll create a new one in
18074 tsubst_lambda_expr. */
18078 init
= DECL_INITIAL (decl
);
18079 decl
= tsubst (decl
, args
, complain
, in_decl
);
18080 if (decl
!= error_mark_node
)
18082 /* By marking the declaration as instantiated, we avoid
18083 trying to instantiate it. Since instantiate_decl can't
18084 handle local variables, and since we've already done
18085 all that needs to be done, that's the right thing to
18088 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18089 if (VAR_P (decl
) && !DECL_NAME (decl
)
18090 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18091 /* Anonymous aggregates are a special case. */
18092 finish_anon_union (decl
);
18093 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18095 DECL_CONTEXT (decl
) = current_function_decl
;
18096 if (DECL_NAME (decl
) == this_identifier
)
18098 tree lam
= DECL_CONTEXT (current_function_decl
);
18099 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18100 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18102 insert_capture_proxy (decl
);
18104 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18105 /* We already did a pushtag. */;
18106 else if (TREE_CODE (decl
) == FUNCTION_DECL
18107 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18108 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
18110 /* We pretend this is regular local extern decl of
18111 a namespace-scope fn. Then we make it really
18112 local, it is a nested function. */
18113 gcc_checking_assert (DECL_LOCAL_DECL_P (decl
));
18114 DECL_CONTEXT (decl
) = global_namespace
;
18116 DECL_CONTEXT (decl
) = current_function_decl
;
18117 if (cp_check_omp_declare_reduction (decl
))
18118 instantiate_body (pattern_decl
, args
, decl
, true);
18122 bool const_init
= false;
18123 unsigned int cnt
= 0;
18124 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18125 maybe_push_decl (decl
);
18128 && DECL_DECOMPOSITION_P (decl
)
18129 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18130 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18131 complain
, in_decl
, &first
,
18134 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18137 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18140 if (ndecl
!= error_mark_node
)
18141 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18143 /* In a non-template function, VLA type declarations are
18144 handled in grokdeclarator; for templates, handle them
18146 predeclare_vla (decl
);
18148 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
18150 if (ndecl
!= error_mark_node
)
18151 cp_finish_decomp (ndecl
, first
, cnt
);
18160 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18161 RECUR (FOR_INIT_STMT (t
));
18162 finish_init_stmt (stmt
);
18163 tmp
= RECUR (FOR_COND (t
));
18164 finish_for_cond (tmp
, stmt
, false, 0);
18165 tmp
= RECUR (FOR_EXPR (t
));
18166 finish_for_expr (tmp
, stmt
);
18168 bool prev
= note_iteration_stmt_body_start ();
18169 RECUR (FOR_BODY (t
));
18170 note_iteration_stmt_body_end (prev
);
18172 finish_for_stmt (stmt
);
18175 case RANGE_FOR_STMT
:
18177 /* Construct another range_for, if this is not a final
18178 substitution (for inside a generic lambda of a
18179 template). Otherwise convert to a regular for. */
18181 stmt
= (processing_template_decl
18182 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18183 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18184 RECUR (RANGE_FOR_INIT_STMT (t
));
18185 decl
= RANGE_FOR_DECL (t
);
18186 decl
= tsubst (decl
, args
, complain
, in_decl
);
18187 maybe_push_decl (decl
);
18188 expr
= RECUR (RANGE_FOR_EXPR (t
));
18190 tree decomp_first
= NULL_TREE
;
18191 unsigned decomp_cnt
= 0;
18192 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18193 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18195 &decomp_first
, &decomp_cnt
);
18197 if (processing_template_decl
)
18199 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18200 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18201 finish_range_for_decl (stmt
, decl
, expr
);
18202 if (decomp_first
&& decl
!= error_mark_node
)
18203 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18207 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18208 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18209 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18210 decomp_first
, decomp_cnt
,
18211 RANGE_FOR_IVDEP (t
), unroll
);
18214 bool prev
= note_iteration_stmt_body_start ();
18215 RECUR (RANGE_FOR_BODY (t
));
18216 note_iteration_stmt_body_end (prev
);
18217 finish_for_stmt (stmt
);
18222 stmt
= begin_while_stmt ();
18223 tmp
= RECUR (WHILE_COND (t
));
18224 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18226 bool prev
= note_iteration_stmt_body_start ();
18227 RECUR (WHILE_BODY (t
));
18228 note_iteration_stmt_body_end (prev
);
18230 finish_while_stmt (stmt
);
18234 stmt
= begin_do_stmt ();
18236 bool prev
= note_iteration_stmt_body_start ();
18237 RECUR (DO_BODY (t
));
18238 note_iteration_stmt_body_end (prev
);
18240 finish_do_body (stmt
);
18241 tmp
= RECUR (DO_COND (t
));
18242 finish_do_stmt (tmp
, stmt
, false, 0);
18246 stmt
= begin_if_stmt ();
18247 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18248 if (IF_STMT_CONSTEXPR_P (t
))
18249 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18250 tmp
= RECUR (IF_COND (t
));
18251 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18252 if (IF_STMT_CONSTEXPR_P (t
)
18253 && instantiation_dependent_expression_p (tmp
))
18255 /* We're partially instantiating a generic lambda, but the condition
18256 of the constexpr if is still dependent. Don't substitute into the
18257 branches now, just remember the template arguments. */
18258 do_poplevel (IF_SCOPE (stmt
));
18259 IF_COND (stmt
) = IF_COND (t
);
18260 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18261 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18262 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18266 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18267 /* Don't instantiate the THEN_CLAUSE. */;
18270 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18271 bool inhibit
= integer_zerop (folded
);
18273 ++c_inhibit_evaluation_warnings
;
18274 RECUR (THEN_CLAUSE (t
));
18276 --c_inhibit_evaluation_warnings
;
18278 finish_then_clause (stmt
);
18280 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18281 /* Don't instantiate the ELSE_CLAUSE. */;
18282 else if (ELSE_CLAUSE (t
))
18284 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18285 bool inhibit
= integer_nonzerop (folded
);
18286 begin_else_clause (stmt
);
18288 ++c_inhibit_evaluation_warnings
;
18289 RECUR (ELSE_CLAUSE (t
));
18291 --c_inhibit_evaluation_warnings
;
18292 finish_else_clause (stmt
);
18295 finish_if_stmt (stmt
);
18299 if (BIND_EXPR_BODY_BLOCK (t
))
18300 stmt
= begin_function_body ();
18302 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18303 ? BCS_TRY_BLOCK
: 0);
18305 RECUR (BIND_EXPR_BODY (t
));
18307 if (BIND_EXPR_BODY_BLOCK (t
))
18308 finish_function_body (stmt
);
18310 finish_compound_stmt (stmt
);
18314 finish_break_stmt ();
18317 case CONTINUE_STMT
:
18318 finish_continue_stmt ();
18322 stmt
= begin_switch_stmt ();
18323 tmp
= RECUR (SWITCH_STMT_COND (t
));
18324 finish_switch_cond (tmp
, stmt
);
18325 RECUR (SWITCH_STMT_BODY (t
));
18326 finish_switch_stmt (stmt
);
18329 case CASE_LABEL_EXPR
:
18331 tree decl
= CASE_LABEL (t
);
18332 tree low
= RECUR (CASE_LOW (t
));
18333 tree high
= RECUR (CASE_HIGH (t
));
18334 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18335 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18337 tree label
= CASE_LABEL (l
);
18338 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18339 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18340 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18347 tree decl
= LABEL_EXPR_LABEL (t
);
18350 label
= finish_label_stmt (DECL_NAME (decl
));
18351 if (TREE_CODE (label
) == LABEL_DECL
)
18352 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18353 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18354 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18359 tmp
= GOTO_DESTINATION (t
);
18360 if (TREE_CODE (tmp
) != LABEL_DECL
)
18361 /* Computed goto's must be tsubst'd into. On the other hand,
18362 non-computed gotos must not be; the identifier in question
18363 will have no binding. */
18366 tmp
= DECL_NAME (tmp
);
18367 finish_goto_stmt (tmp
);
18372 tree string
= RECUR (ASM_STRING (t
));
18373 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18374 complain
, in_decl
);
18375 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18376 complain
, in_decl
);
18377 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18378 complain
, in_decl
);
18379 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18380 complain
, in_decl
);
18381 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18382 outputs
, inputs
, clobbers
, labels
,
18384 tree asm_expr
= tmp
;
18385 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18386 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18387 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18394 stmt
= begin_try_block ();
18395 RECUR (TRY_STMTS (t
));
18396 finish_cleanup_try_block (stmt
);
18397 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18401 tree compound_stmt
= NULL_TREE
;
18403 if (FN_TRY_BLOCK_P (t
))
18404 stmt
= begin_function_try_block (&compound_stmt
);
18406 stmt
= begin_try_block ();
18408 RECUR (TRY_STMTS (t
));
18410 if (FN_TRY_BLOCK_P (t
))
18411 finish_function_try_block (stmt
);
18413 finish_try_block (stmt
);
18415 RECUR (TRY_HANDLERS (t
));
18416 if (FN_TRY_BLOCK_P (t
))
18417 finish_function_handler_sequence (stmt
, compound_stmt
);
18419 finish_handler_sequence (stmt
);
18425 tree decl
= HANDLER_PARMS (t
);
18429 decl
= tsubst (decl
, args
, complain
, in_decl
);
18430 /* Prevent instantiate_decl from trying to instantiate
18431 this variable. We've already done all that needs to be
18433 if (decl
!= error_mark_node
)
18434 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18436 stmt
= begin_handler ();
18437 finish_handler_parms (decl
, stmt
);
18438 RECUR (HANDLER_BODY (t
));
18439 finish_handler (stmt
);
18444 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18445 if (CLASS_TYPE_P (tmp
))
18447 /* Local classes are not independent templates; they are
18448 instantiated along with their containing function. And this
18449 way we don't have to deal with pushing out of one local class
18450 to instantiate a member of another local class. */
18451 /* Closures are handled by the LAMBDA_EXPR. */
18452 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18453 complete_type (tmp
);
18454 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18456 || (TREE_CODE (fld
) == FUNCTION_DECL
18457 && !DECL_ARTIFICIAL (fld
)))
18458 && DECL_TEMPLATE_INSTANTIATION (fld
))
18459 instantiate_decl (fld
, /*defer_ok=*/false,
18460 /*expl_inst_class=*/false);
18464 case STATIC_ASSERT
:
18468 ++c_inhibit_evaluation_warnings
;
18470 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18473 /*integral_constant_expression_p=*/true);
18474 --c_inhibit_evaluation_warnings
;
18476 finish_static_assert (condition
,
18477 STATIC_ASSERT_MESSAGE (t
),
18478 STATIC_ASSERT_SOURCE_LOCATION (t
),
18479 /*member_p=*/false);
18484 case OACC_PARALLEL
:
18486 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18488 stmt
= begin_omp_parallel ();
18489 RECUR (OMP_BODY (t
));
18490 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18494 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18495 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18496 complain
, in_decl
);
18497 if (OMP_PARALLEL_COMBINED (t
))
18498 omp_parallel_combined_clauses
= &tmp
;
18499 stmt
= begin_omp_parallel ();
18500 RECUR (OMP_PARALLEL_BODY (t
));
18501 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18502 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18503 = OMP_PARALLEL_COMBINED (t
);
18504 pop_omp_privatization_clauses (r
);
18508 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18510 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18511 complain
, in_decl
);
18513 OMP_TASK_CLAUSES (t
) = tmp
;
18517 r
= push_omp_privatization_clauses (false);
18518 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18519 complain
, in_decl
);
18520 stmt
= begin_omp_task ();
18521 RECUR (OMP_TASK_BODY (t
));
18522 finish_omp_task (tmp
, stmt
);
18523 pop_omp_privatization_clauses (r
);
18529 case OMP_DISTRIBUTE
:
18533 tree clauses
, body
, pre_body
;
18534 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18535 tree orig_declv
= NULL_TREE
;
18536 tree incrv
= NULL_TREE
;
18537 enum c_omp_region_type ort
= C_ORT_OMP
;
18538 bool any_range_for
= false;
18541 if (TREE_CODE (t
) == OACC_LOOP
)
18544 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18545 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18547 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18549 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18550 if (OMP_FOR_ORIG_DECLS (t
))
18551 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18552 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18553 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18554 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18557 keep_next_level (true);
18558 stmt
= begin_omp_structured_block ();
18560 pre_body
= push_stmt_list ();
18561 RECUR (OMP_FOR_PRE_BODY (t
));
18562 pre_body
= pop_stmt_list (pre_body
);
18564 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18565 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18567 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18568 condv
, incrv
, &clauses
, args
,
18570 integral_constant_expression_p
);
18571 omp_parallel_combined_clauses
= NULL
;
18575 gcc_assert (orig_declv
);
18576 body
= begin_omp_structured_block ();
18577 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18578 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18579 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18580 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18581 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18582 TREE_VEC_ELT (declv
, i
));
18585 body
= push_stmt_list ();
18586 RECUR (OMP_FOR_BODY (t
));
18588 body
= finish_omp_structured_block (body
);
18590 body
= pop_stmt_list (body
);
18592 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18593 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18594 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18598 t
= make_node (TREE_CODE (t
));
18599 TREE_TYPE (t
) = void_type_node
;
18600 OMP_FOR_BODY (t
) = body
;
18601 OMP_FOR_PRE_BODY (t
) = pre_body
;
18602 OMP_FOR_CLAUSES (t
) = clauses
;
18603 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18607 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18609 pop_omp_privatization_clauses (r
);
18614 omp_parallel_combined_clauses
= NULL
;
18619 case OMP_TASKGROUP
:
18621 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18622 && OMP_TEAMS_COMBINED (t
));
18623 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18625 if (TREE_CODE (t
) == OMP_TEAMS
)
18627 keep_next_level (true);
18628 stmt
= begin_omp_structured_block ();
18629 RECUR (OMP_BODY (t
));
18630 stmt
= finish_omp_structured_block (stmt
);
18634 stmt
= push_stmt_list ();
18635 RECUR (OMP_BODY (t
));
18636 stmt
= pop_stmt_list (stmt
);
18639 if (TREE_CODE (t
) == OMP_CRITICAL
18640 && tmp
!= NULL_TREE
18641 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18643 error_at (OMP_CLAUSE_LOCATION (tmp
),
18644 "%<#pragma omp critical%> with %<hint%> clause requires "
18645 "a name, except when %<omp_sync_hint_none%> is used");
18646 RETURN (error_mark_node
);
18649 OMP_BODY (t
) = stmt
;
18650 OMP_CLAUSES (t
) = tmp
;
18652 pop_omp_privatization_clauses (r
);
18656 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18657 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18659 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18660 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18662 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18663 args
, complain
, in_decl
);
18664 if (tmp
== NULL_TREE
)
18665 tmp
= error_mark_node
;
18669 kind
= (enum omp_clause_depend_kind
)
18670 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18673 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18676 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18677 OMP_CLAUSE_DEPEND_SOURCE
,
18678 OMP_DEPOBJ_CLAUSES (t
));
18682 case OMP_TARGET_DATA
:
18684 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18685 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18687 keep_next_level (true);
18688 stmt
= begin_omp_structured_block ();
18690 RECUR (OMP_BODY (t
));
18691 stmt
= finish_omp_structured_block (stmt
);
18694 OMP_BODY (t
) = stmt
;
18695 OMP_CLAUSES (t
) = tmp
;
18696 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18698 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18701 /* For combined target teams, ensure the num_teams and
18702 thread_limit clause expressions are evaluated on the host,
18703 before entering the target construct. */
18705 for (c
= OMP_TEAMS_CLAUSES (teams
);
18706 c
; c
= OMP_CLAUSE_CHAIN (c
))
18707 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18708 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18709 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18711 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18712 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18713 if (expr
== error_mark_node
)
18715 tmp
= TARGET_EXPR_SLOT (expr
);
18717 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18718 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18719 OMP_CLAUSE_FIRSTPRIVATE
);
18720 OMP_CLAUSE_DECL (tc
) = tmp
;
18721 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18722 OMP_TARGET_CLAUSES (t
) = tc
;
18731 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18732 complain
, in_decl
);
18733 OACC_DECLARE_CLAUSES (t
) = tmp
;
18737 case OMP_TARGET_UPDATE
:
18738 case OMP_TARGET_ENTER_DATA
:
18739 case OMP_TARGET_EXIT_DATA
:
18740 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18741 complain
, in_decl
);
18743 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18747 case OACC_ENTER_DATA
:
18748 case OACC_EXIT_DATA
:
18750 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18751 complain
, in_decl
);
18753 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18758 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18759 complain
, in_decl
);
18760 stmt
= push_stmt_list ();
18761 RECUR (OMP_BODY (t
));
18762 stmt
= pop_stmt_list (stmt
);
18765 OMP_BODY (t
) = stmt
;
18766 OMP_ORDERED_CLAUSES (t
) = tmp
;
18771 omp_parallel_combined_clauses
= NULL
;
18774 stmt
= push_stmt_list ();
18775 RECUR (OMP_BODY (t
));
18776 stmt
= pop_stmt_list (stmt
);
18779 OMP_BODY (t
) = stmt
;
18784 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18786 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18787 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18788 complain
, in_decl
);
18789 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18791 tree op1
= TREE_OPERAND (t
, 1);
18792 tree rhs1
= NULL_TREE
;
18794 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18796 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18797 op1
= TREE_OPERAND (op1
, 1);
18799 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18800 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18801 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18802 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18803 OMP_ATOMIC_MEMORY_ORDER (t
));
18807 tree op1
= TREE_OPERAND (t
, 1);
18808 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18809 tree rhs1
= NULL_TREE
;
18810 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18811 enum tree_code opcode
= NOP_EXPR
;
18812 if (code
== OMP_ATOMIC_READ
)
18814 v
= RECUR (TREE_OPERAND (op1
, 0));
18815 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18817 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18818 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18820 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18821 v
= RECUR (TREE_OPERAND (op1
, 0));
18822 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18823 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18825 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18826 op11
= TREE_OPERAND (op11
, 1);
18828 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18829 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18830 opcode
= TREE_CODE (op11
);
18831 if (opcode
== MODIFY_EXPR
)
18837 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18838 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18840 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18841 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18845 case TRANSACTION_EXPR
:
18848 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18849 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18851 if (TRANSACTION_EXPR_IS_STMT (t
))
18853 tree body
= TRANSACTION_EXPR_BODY (t
);
18854 tree noex
= NULL_TREE
;
18855 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18857 noex
= MUST_NOT_THROW_COND (body
);
18858 if (noex
== NULL_TREE
)
18859 noex
= boolean_true_node
;
18860 body
= TREE_OPERAND (body
, 0);
18862 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18864 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18868 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18869 RECUR (TRANSACTION_EXPR_BODY (t
)),
18876 case MUST_NOT_THROW_EXPR
:
18878 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18879 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18880 RETURN (build_must_not_throw_expr (op0
, cond
));
18883 case EXPR_PACK_EXPANSION
:
18884 error ("invalid use of pack expansion expression");
18885 RETURN (error_mark_node
);
18887 case NONTYPE_ARGUMENT_PACK
:
18888 error ("use %<...%> to expand argument pack");
18889 RETURN (error_mark_node
);
18891 case COMPOUND_EXPR
:
18892 tmp
= RECUR (TREE_OPERAND (t
, 0));
18893 if (tmp
== NULL_TREE
)
18894 /* If the first operand was a statement, we're done with it. */
18895 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18896 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18897 RECUR (TREE_OPERAND (t
, 1)),
18900 case ANNOTATE_EXPR
:
18901 tmp
= RECUR (TREE_OPERAND (t
, 0));
18902 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18903 TREE_TYPE (tmp
), tmp
,
18904 RECUR (TREE_OPERAND (t
, 1)),
18905 RECUR (TREE_OPERAND (t
, 2))));
18908 RETURN (add_stmt (copy_node (t
)));
18911 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18913 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18914 /*function_p=*/false,
18915 integral_constant_expression_p
));
18918 RETURN (NULL_TREE
);
18920 input_location
= loc
;
18926 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18927 function. For description of the body see comment above
18928 cp_parser_omp_declare_reduction_exprs. */
18931 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18933 if (t
== NULL_TREE
|| t
== error_mark_node
)
18936 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
18938 tree_stmt_iterator tsi
;
18941 memset (stmts
, 0, sizeof stmts
);
18942 for (i
= 0, tsi
= tsi_start (t
);
18943 i
< 7 && !tsi_end_p (tsi
);
18944 i
++, tsi_next (&tsi
))
18945 stmts
[i
] = tsi_stmt (tsi
);
18946 gcc_assert (tsi_end_p (tsi
));
18950 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18951 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18952 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18953 args
, complain
, in_decl
);
18954 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18955 args
, complain
, in_decl
);
18956 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18957 expect to be pushing it. */
18958 DECL_CONTEXT (omp_out
) = current_function_decl
;
18959 DECL_CONTEXT (omp_in
) = current_function_decl
;
18960 keep_next_level (true);
18961 tree block
= begin_omp_structured_block ();
18962 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18963 block
= finish_omp_structured_block (block
);
18964 block
= maybe_cleanup_point_expr_void (block
);
18965 add_decl_expr (omp_out
);
18966 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18967 TREE_NO_WARNING (omp_out
) = 1;
18968 add_decl_expr (omp_in
);
18969 finish_expr_stmt (block
);
18973 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18974 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18975 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18976 args
, complain
, in_decl
);
18977 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18978 args
, complain
, in_decl
);
18979 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18980 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18981 keep_next_level (true);
18982 tree block
= begin_omp_structured_block ();
18983 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18984 block
= finish_omp_structured_block (block
);
18985 block
= maybe_cleanup_point_expr_void (block
);
18986 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18987 add_decl_expr (omp_priv
);
18988 add_decl_expr (omp_orig
);
18989 finish_expr_stmt (block
);
18991 add_decl_expr (omp_orig
);
18995 /* T is a postfix-expression that is not being used in a function
18996 call. Return the substituted version of T. */
18999 tsubst_non_call_postfix_expression (tree t
, tree args
,
19000 tsubst_flags_t complain
,
19003 if (TREE_CODE (t
) == SCOPE_REF
)
19004 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19005 /*done=*/false, /*address_p=*/false);
19007 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19008 /*function_p=*/false,
19009 /*integral_constant_expression_p=*/false);
19014 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19015 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19016 dependent init-capture. */
19019 prepend_one_capture (tree field
, tree init
, tree
&list
,
19020 tsubst_flags_t complain
)
19022 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19024 tree type
= NULL_TREE
;
19027 if (complain
& tf_error
)
19028 error ("empty initializer in lambda init-capture");
19029 init
= error_mark_node
;
19031 else if (TREE_CODE (init
) == TREE_LIST
)
19032 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19034 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19035 TREE_TYPE (field
) = type
;
19036 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19038 list
= tree_cons (field
, init
, list
);
19041 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19042 instantiation context. Instantiating a pack expansion containing a lambda
19043 might result in multiple lambdas all based on the same lambda in the
19047 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19049 tree oldfn
= lambda_function (t
);
19052 tree r
= build_lambda_expr ();
19054 LAMBDA_EXPR_LOCATION (r
)
19055 = LAMBDA_EXPR_LOCATION (t
);
19056 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19057 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19058 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19059 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19061 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19062 /* A lambda in a default argument outside a class gets no
19063 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19064 tsubst_default_argument calls start_lambda_scope, so we need to
19065 specifically ignore it here, and use the global scope. */
19066 record_null_lambda_scope (r
);
19068 record_lambda_scope (r
);
19070 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19071 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19073 vec
<tree
,va_gc
>* field_packs
= NULL
;
19075 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19076 cap
= TREE_CHAIN (cap
))
19078 tree ofield
= TREE_PURPOSE (cap
);
19079 tree init
= TREE_VALUE (cap
);
19080 if (PACK_EXPANSION_P (init
))
19081 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19083 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19084 /*fn*/false, /*constexpr*/false);
19086 if (init
== error_mark_node
)
19087 return error_mark_node
;
19089 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19090 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19092 if (!processing_template_decl
19093 && init
&& TREE_CODE (init
) != TREE_VEC
19094 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19096 /* For a VLA, simply tsubsting the field type won't work, we need to
19097 go through add_capture again. XXX do we want to do this for all
19099 tree name
= (get_identifier
19100 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19101 tree ftype
= TREE_TYPE (ofield
);
19102 bool by_ref
= (TYPE_REF_P (ftype
)
19103 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19104 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19105 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19109 if (PACK_EXPANSION_P (ofield
))
19110 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19111 tree field
= tsubst_decl (ofield
, args
, complain
);
19113 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19115 /* Remember these for when we've pushed local_specializations. */
19116 vec_safe_push (field_packs
, ofield
);
19117 vec_safe_push (field_packs
, field
);
19120 if (field
== error_mark_node
)
19121 return error_mark_node
;
19123 if (TREE_CODE (field
) == TREE_VEC
)
19125 int len
= TREE_VEC_LENGTH (field
);
19126 gcc_assert (TREE_CODE (init
) == TREE_VEC
19127 && TREE_VEC_LENGTH (init
) == len
);
19128 for (int i
= 0; i
< len
; ++i
)
19129 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19130 TREE_VEC_ELT (init
, i
),
19131 LAMBDA_EXPR_CAPTURE_LIST (r
),
19136 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19139 if (id_equal (DECL_NAME (field
), "__this"))
19140 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19144 tree type
= begin_lambda_type (r
);
19145 if (type
== error_mark_node
)
19146 return error_mark_node
;
19148 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19149 determine_visibility (TYPE_NAME (type
));
19151 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19153 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19154 ? DECL_TI_TEMPLATE (oldfn
)
19157 tree fntype
= static_fn_type (oldfn
);
19159 ++processing_template_decl
;
19160 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19162 --processing_template_decl
;
19164 if (fntype
== error_mark_node
)
19165 r
= error_mark_node
;
19168 /* The body of a lambda-expression is not a subexpression of the
19169 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19170 which would be skipped if cp_unevaluated_operand. */
19173 /* Fix the type of 'this'. */
19174 fntype
= build_memfn_type (fntype
, type
,
19175 type_memfn_quals (fntype
),
19176 type_memfn_rqual (fntype
));
19180 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19181 if (tmpl
== error_mark_node
)
19183 r
= error_mark_node
;
19186 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19187 finish_member_declaration (tmpl
);
19192 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19193 if (fn
== error_mark_node
)
19195 r
= error_mark_node
;
19198 finish_member_declaration (fn
);
19201 if (tree ci
= get_constraints (oldfn
))
19203 /* Substitute into the lambda's constraints. */
19205 ++processing_template_decl
;
19206 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19208 --processing_template_decl
;
19209 set_constraints (fn
, ci
);
19212 /* Let finish_function set this. */
19213 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19215 bool nested
= cfun
;
19217 push_function_context ();
19219 /* Still increment function_depth so that we don't GC in the
19220 middle of an expression. */
19223 local_specialization_stack
s (lss_copy
);
19225 tree body
= start_lambda_function (fn
, r
);
19227 /* Now record them for lookup_init_capture_pack. */
19228 int fplen
= vec_safe_length (field_packs
);
19229 for (int i
= 0; i
< fplen
; )
19231 tree pack
= (*field_packs
)[i
++];
19232 tree inst
= (*field_packs
)[i
++];
19233 register_local_specialization (inst
, pack
);
19235 release_tree_vector (field_packs
);
19237 register_parameter_specializations (oldfn
, fn
);
19241 /* We might not partially instantiate some parts of the function, so
19242 copy these flags from the original template. */
19243 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19244 current_function_returns_value
= ol
->returns_value
;
19245 current_function_returns_null
= ol
->returns_null
;
19246 current_function_returns_abnormally
= ol
->returns_abnormally
;
19247 current_function_infinite_loop
= ol
->infinite_loop
;
19250 /* [temp.deduct] A lambda-expression appearing in a function type or a
19251 template parameter is not considered part of the immediate context for
19252 the purposes of template argument deduction. */
19253 complain
= tf_warning_or_error
;
19255 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19256 /*constexpr*/false);
19258 finish_lambda_function (body
);
19261 pop_function_context ();
19265 /* The capture list was built up in reverse order; fix that now. */
19266 LAMBDA_EXPR_CAPTURE_LIST (r
)
19267 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19269 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19271 maybe_add_lambda_conv_op (type
);
19275 finish_struct (type
, /*attr*/NULL_TREE
);
19277 insert_pending_capture_proxies ();
19282 /* Like tsubst but deals with expressions and performs semantic
19283 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19286 tsubst_copy_and_build (tree t
,
19288 tsubst_flags_t complain
,
19291 bool integral_constant_expression_p
)
19293 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19294 #define RECUR(NODE) \
19295 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19296 /*function_p=*/false, \
19297 integral_constant_expression_p)
19300 location_t save_loc
;
19302 if (t
== NULL_TREE
|| t
== error_mark_node
)
19305 save_loc
= input_location
;
19306 if (location_t eloc
= cp_expr_location (t
))
19307 input_location
= eloc
;
19309 /* N3276 decltype magic only applies to calls at the top level or on the
19310 right side of a comma. */
19311 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19312 complain
&= ~tf_decltype
;
19314 switch (TREE_CODE (t
))
19318 /* Fall through. */
19319 case IDENTIFIER_NODE
:
19323 bool non_integral_constant_expression_p
;
19324 const char *error_msg
;
19326 if (IDENTIFIER_CONV_OP_P (t
))
19328 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19329 t
= make_conv_op_name (new_type
);
19332 /* Look up the name. */
19333 decl
= lookup_name (t
);
19335 /* By convention, expressions use ERROR_MARK_NODE to indicate
19336 failure, not NULL_TREE. */
19337 if (decl
== NULL_TREE
)
19338 decl
= error_mark_node
;
19340 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19342 integral_constant_expression_p
,
19343 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19344 &non_integral_constant_expression_p
,
19345 /*template_p=*/false,
19347 /*address_p=*/false,
19348 /*template_arg_p=*/false,
19353 if (!function_p
&& identifier_p (decl
))
19355 if (complain
& tf_error
)
19356 unqualified_name_lookup_error (decl
);
19357 decl
= error_mark_node
;
19362 case TEMPLATE_ID_EXPR
:
19365 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19366 tree targs
= TREE_OPERAND (t
, 1);
19369 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19370 if (targs
== error_mark_node
)
19371 RETURN (error_mark_node
);
19373 if (TREE_CODE (templ
) == SCOPE_REF
)
19375 tree name
= TREE_OPERAND (templ
, 1);
19376 tree tid
= lookup_template_function (name
, targs
);
19377 TREE_OPERAND (templ
, 1) = tid
;
19381 if (concept_definition_p (templ
))
19383 tree check
= build_concept_check (templ
, targs
, complain
);
19384 if (check
== error_mark_node
)
19385 RETURN (error_mark_node
);
19387 tree id
= unpack_concept_check (check
);
19389 /* If we built a function concept check, return the underlying
19390 template-id. So we can evaluate it as a function call. */
19391 if (function_concept_p (TREE_OPERAND (id
, 0)))
19397 if (variable_template_p (templ
))
19399 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19401 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19405 if (TREE_CODE (templ
) == COMPONENT_REF
)
19407 object
= TREE_OPERAND (templ
, 0);
19408 templ
= TREE_OPERAND (templ
, 1);
19411 object
= NULL_TREE
;
19412 templ
= lookup_template_function (templ
, targs
);
19415 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19416 object
, templ
, NULL_TREE
));
19418 RETURN (baselink_for_fns (templ
));
19423 tree r
= RECUR (TREE_OPERAND (t
, 0));
19425 if (REFERENCE_REF_P (t
))
19427 /* A type conversion to reference type will be enclosed in
19428 such an indirect ref, but the substitution of the cast
19429 will have also added such an indirect ref. */
19430 r
= convert_from_reference (r
);
19433 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19434 complain
|decltype_flag
);
19436 if (REF_PARENTHESIZED_P (t
))
19437 r
= force_paren_expr (r
);
19444 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19445 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19446 RETURN (build_nop (type
, op0
));
19449 case IMPLICIT_CONV_EXPR
:
19451 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19452 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19453 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19455 retval
= copy_node (t
);
19456 TREE_TYPE (retval
) = type
;
19457 TREE_OPERAND (retval
, 0) = expr
;
19460 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19461 /* We'll pass this to convert_nontype_argument again, we don't need
19462 to actually perform any conversion here. */
19464 int flags
= LOOKUP_IMPLICIT
;
19465 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19466 flags
= LOOKUP_NORMAL
;
19467 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19468 flags
|= LOOKUP_NO_NARROWING
;
19469 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19475 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19476 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19477 if (op0
== error_mark_node
)
19478 RETURN (error_mark_node
);
19479 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19483 case REINTERPRET_CAST_EXPR
:
19484 case CONST_CAST_EXPR
:
19485 case DYNAMIC_CAST_EXPR
:
19486 case STATIC_CAST_EXPR
:
19489 tree op
, r
= NULL_TREE
;
19491 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19492 if (integral_constant_expression_p
19493 && !cast_valid_in_integral_constant_expression_p (type
))
19495 if (complain
& tf_error
)
19496 error ("a cast to a type other than an integral or "
19497 "enumeration type cannot appear in a constant-expression");
19498 RETURN (error_mark_node
);
19501 op
= RECUR (TREE_OPERAND (t
, 0));
19503 warning_sentinel
s(warn_useless_cast
);
19504 warning_sentinel
s2(warn_ignored_qualifiers
);
19505 switch (TREE_CODE (t
))
19508 r
= build_functional_cast (input_location
, type
, op
, complain
);
19510 case REINTERPRET_CAST_EXPR
:
19511 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19513 case CONST_CAST_EXPR
:
19514 r
= build_const_cast (input_location
, type
, op
, complain
);
19516 case DYNAMIC_CAST_EXPR
:
19517 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19519 case STATIC_CAST_EXPR
:
19520 r
= build_static_cast (input_location
, type
, op
, complain
);
19521 if (IMPLICIT_RVALUE_P (t
))
19522 set_implicit_rvalue_p (r
);
19525 gcc_unreachable ();
19531 case POSTDECREMENT_EXPR
:
19532 case POSTINCREMENT_EXPR
:
19533 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19534 args
, complain
, in_decl
);
19535 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19536 complain
|decltype_flag
));
19538 case PREDECREMENT_EXPR
:
19539 case PREINCREMENT_EXPR
:
19543 case TRUTH_NOT_EXPR
:
19544 case UNARY_PLUS_EXPR
: /* Unary + */
19545 case REALPART_EXPR
:
19546 case IMAGPART_EXPR
:
19547 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19548 RECUR (TREE_OPERAND (t
, 0)),
19549 complain
|decltype_flag
));
19551 case FIX_TRUNC_EXPR
:
19552 gcc_unreachable ();
19555 op1
= TREE_OPERAND (t
, 0);
19556 if (TREE_CODE (op1
) == LABEL_DECL
)
19557 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19558 EXPR_LOCATION (op1
)));
19559 if (TREE_CODE (op1
) == SCOPE_REF
)
19560 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19561 /*done=*/true, /*address_p=*/true);
19563 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19565 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19566 complain
|decltype_flag
));
19571 case TRUNC_DIV_EXPR
:
19572 case CEIL_DIV_EXPR
:
19573 case FLOOR_DIV_EXPR
:
19574 case ROUND_DIV_EXPR
:
19575 case EXACT_DIV_EXPR
:
19579 case TRUNC_MOD_EXPR
:
19580 case FLOOR_MOD_EXPR
:
19581 case TRUTH_ANDIF_EXPR
:
19582 case TRUTH_ORIF_EXPR
:
19583 case TRUTH_AND_EXPR
:
19584 case TRUTH_OR_EXPR
:
19595 case SPACESHIP_EXPR
:
19599 /* If T was type-dependent, suppress warnings that depend on the range
19600 of the types involved. */
19601 bool was_dep
= type_dependent_expression_p_push (t
);
19603 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19604 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19606 warning_sentinel
s1(warn_type_limits
, was_dep
);
19607 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19608 warning_sentinel
s3(warn_logical_op
, was_dep
);
19609 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19611 tree r
= build_x_binary_op
19612 (input_location
, TREE_CODE (t
),
19614 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19616 : TREE_CODE (TREE_OPERAND (t
, 0))),
19618 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19620 : TREE_CODE (TREE_OPERAND (t
, 1))),
19622 complain
|decltype_flag
);
19623 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19624 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19629 case POINTER_PLUS_EXPR
:
19631 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19632 if (op0
== error_mark_node
)
19633 RETURN (error_mark_node
);
19634 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19635 if (op1
== error_mark_node
)
19636 RETURN (error_mark_node
);
19637 RETURN (fold_build_pointer_plus (op0
, op1
));
19641 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19642 /*address_p=*/false));
19644 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19645 args
, complain
, in_decl
);
19646 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19647 RECUR (TREE_OPERAND (t
, 1)),
19648 complain
|decltype_flag
));
19651 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19652 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19653 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19660 op1
= TREE_OPERAND (t
, 0);
19661 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19662 op1
= TREE_TYPE (op1
);
19663 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19664 && ALIGNOF_EXPR_STD_P (t
));
19667 /* When there are no ARGS, we are trying to evaluate a
19668 non-dependent expression from the parser. Trying to do
19669 the substitutions may not work. */
19671 op1
= TREE_TYPE (op1
);
19675 ++cp_unevaluated_operand
;
19676 ++c_inhibit_evaluation_warnings
;
19678 op1
= tsubst (op1
, args
, complain
, in_decl
);
19680 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19681 /*function_p=*/false,
19682 /*integral_constant_expression_p=*/
19684 --cp_unevaluated_operand
;
19685 --c_inhibit_evaluation_warnings
;
19688 r
= cxx_sizeof_or_alignof_type (input_location
,
19689 op1
, TREE_CODE (t
), std_alignof
,
19690 complain
& tf_error
);
19692 r
= cxx_sizeof_or_alignof_expr (input_location
,
19693 op1
, TREE_CODE (t
),
19694 complain
& tf_error
);
19695 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19697 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19699 if (!processing_template_decl
&& TYPE_P (op1
))
19701 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19702 build1 (NOP_EXPR
, op1
, error_mark_node
));
19703 SIZEOF_EXPR_TYPE_P (r
) = 1;
19706 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19707 TREE_SIDE_EFFECTS (r
) = 0;
19708 TREE_READONLY (r
) = 1;
19710 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19715 case AT_ENCODE_EXPR
:
19717 op1
= TREE_OPERAND (t
, 0);
19718 ++cp_unevaluated_operand
;
19719 ++c_inhibit_evaluation_warnings
;
19720 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19721 /*function_p=*/false,
19722 /*integral_constant_expression_p=*/false);
19723 --cp_unevaluated_operand
;
19724 --c_inhibit_evaluation_warnings
;
19725 RETURN (objc_build_encode_expr (op1
));
19728 case NOEXCEPT_EXPR
:
19729 op1
= TREE_OPERAND (t
, 0);
19730 ++cp_unevaluated_operand
;
19731 ++c_inhibit_evaluation_warnings
;
19732 ++cp_noexcept_operand
;
19733 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19734 /*function_p=*/false,
19735 /*integral_constant_expression_p=*/false);
19736 --cp_unevaluated_operand
;
19737 --c_inhibit_evaluation_warnings
;
19738 --cp_noexcept_operand
;
19739 RETURN (finish_noexcept_expr (op1
, complain
));
19743 warning_sentinel
s(warn_div_by_zero
);
19744 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19745 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19746 tree r
= build_x_modify_expr
19747 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19748 complain
|decltype_flag
);
19749 /* TREE_NO_WARNING must be set if either the expression was
19750 parenthesized or it uses an operator such as >>= rather
19751 than plain assignment. In the former case, it was already
19752 set and must be copied. In the latter case,
19753 build_x_modify_expr sets it and it must not be reset
19755 if (TREE_NO_WARNING (t
))
19756 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19762 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19763 args
, complain
, in_decl
);
19764 /* Remember that there was a reference to this entity. */
19766 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19767 RETURN (error_mark_node
);
19768 RETURN (build_x_arrow (input_location
, op1
, complain
));
19772 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19773 tree init
= RECUR (TREE_OPERAND (t
, 3));
19774 vec
<tree
, va_gc
> *placement_vec
;
19775 vec
<tree
, va_gc
> *init_vec
;
19777 location_t loc
= EXPR_LOCATION (t
);
19779 if (placement
== NULL_TREE
)
19780 placement_vec
= NULL
;
19781 else if (placement
== error_mark_node
)
19782 RETURN (error_mark_node
);
19785 placement_vec
= make_tree_vector ();
19786 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19787 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19790 /* If there was an initializer in the original tree, but it
19791 instantiated to an empty list, then we should pass a
19792 non-NULL empty vector to tell build_new that it was an
19793 empty initializer() rather than no initializer. This can
19794 only happen when the initializer is a pack expansion whose
19795 parameter packs are of length zero. */
19796 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19800 init_vec
= make_tree_vector ();
19801 if (init
== void_node
)
19802 gcc_assert (init_vec
!= NULL
);
19805 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19806 vec_safe_push (init_vec
, TREE_VALUE (init
));
19810 /* Avoid passing an enclosing decl to valid_array_size_p. */
19811 in_decl
= NULL_TREE
;
19813 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19814 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19815 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19816 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19819 if (placement_vec
!= NULL
)
19820 release_tree_vector (placement_vec
);
19821 if (init_vec
!= NULL
)
19822 release_tree_vector (init_vec
);
19829 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19830 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19831 RETURN (delete_sanity (input_location
, op0
, op1
,
19832 DELETE_EXPR_USE_VEC (t
),
19833 DELETE_EXPR_USE_GLOBAL (t
),
19837 case COMPOUND_EXPR
:
19839 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19840 complain
& ~tf_decltype
, in_decl
,
19841 /*function_p=*/false,
19842 integral_constant_expression_p
);
19843 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19845 RECUR (TREE_OPERAND (t
, 1)),
19846 complain
|decltype_flag
));
19852 unsigned int nargs
, i
;
19857 function
= CALL_EXPR_FN (t
);
19858 /* Internal function with no arguments. */
19859 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19862 /* When we parsed the expression, we determined whether or
19863 not Koenig lookup should be performed. */
19864 koenig_p
= KOENIG_LOOKUP_P (t
);
19865 if (function
== NULL_TREE
)
19868 qualified_p
= false;
19870 else if (TREE_CODE (function
) == SCOPE_REF
)
19872 qualified_p
= true;
19873 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19875 /*address_p=*/false);
19877 else if (koenig_p
&& identifier_p (function
))
19879 /* Do nothing; calling tsubst_copy_and_build on an identifier
19880 would incorrectly perform unqualified lookup again.
19882 Note that we can also have an IDENTIFIER_NODE if the earlier
19883 unqualified lookup found a member function; in that case
19884 koenig_p will be false and we do want to do the lookup
19885 again to find the instantiated member function.
19887 FIXME but doing that causes c++/15272, so we need to stop
19888 using IDENTIFIER_NODE in that situation. */
19889 qualified_p
= false;
19893 if (TREE_CODE (function
) == COMPONENT_REF
)
19895 tree op
= TREE_OPERAND (function
, 1);
19897 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19898 || (BASELINK_P (op
)
19899 && BASELINK_QUALIFIED_P (op
)));
19902 qualified_p
= false;
19904 if (TREE_CODE (function
) == ADDR_EXPR
19905 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19906 /* Avoid error about taking the address of a constructor. */
19907 function
= TREE_OPERAND (function
, 0);
19909 function
= tsubst_copy_and_build (function
, args
, complain
,
19912 integral_constant_expression_p
);
19914 if (BASELINK_P (function
))
19915 qualified_p
= true;
19918 nargs
= call_expr_nargs (t
);
19919 releasing_vec call_args
;
19920 for (i
= 0; i
< nargs
; ++i
)
19922 tree arg
= CALL_EXPR_ARG (t
, i
);
19924 if (!PACK_EXPANSION_P (arg
))
19925 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19928 /* Expand the pack expansion and push each entry onto
19930 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19931 if (TREE_CODE (arg
) == TREE_VEC
)
19933 unsigned int len
, j
;
19935 len
= TREE_VEC_LENGTH (arg
);
19936 for (j
= 0; j
< len
; ++j
)
19938 tree value
= TREE_VEC_ELT (arg
, j
);
19939 if (value
!= NULL_TREE
)
19940 value
= convert_from_reference (value
);
19941 vec_safe_push (call_args
, value
);
19946 /* A partial substitution. Add one entry. */
19947 vec_safe_push (call_args
, arg
);
19952 /* Stripped-down processing for a call in a thunk. Specifically, in
19953 the thunk template for a generic lambda. */
19954 if (CALL_FROM_THUNK_P (t
))
19956 /* Now that we've expanded any packs, the number of call args
19957 might be different. */
19958 unsigned int cargs
= call_args
->length ();
19959 tree thisarg
= NULL_TREE
;
19960 if (TREE_CODE (function
) == COMPONENT_REF
)
19962 thisarg
= TREE_OPERAND (function
, 0);
19963 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19964 thisarg
= TREE_OPERAND (thisarg
, 0);
19965 function
= TREE_OPERAND (function
, 1);
19966 if (TREE_CODE (function
) == BASELINK
)
19967 function
= BASELINK_FUNCTIONS (function
);
19969 /* We aren't going to do normal overload resolution, so force the
19970 template-id to resolve. */
19971 function
= resolve_nondeduced_context (function
, complain
);
19972 for (unsigned i
= 0; i
< cargs
; ++i
)
19974 /* In a thunk, pass through args directly, without any
19976 tree arg
= (*call_args
)[i
];
19977 while (TREE_CODE (arg
) != PARM_DECL
)
19978 arg
= TREE_OPERAND (arg
, 0);
19979 (*call_args
)[i
] = arg
;
19983 /* If there are no other args, just push 'this'. */
19985 vec_safe_push (call_args
, thisarg
);
19988 /* Otherwise, shift the other args over to make room. */
19989 tree last
= (*call_args
)[cargs
- 1];
19990 vec_safe_push (call_args
, last
);
19991 for (int i
= cargs
- 1; i
> 0; --i
)
19992 (*call_args
)[i
] = (*call_args
)[i
- 1];
19993 (*call_args
)[0] = thisarg
;
19996 ret
= build_call_a (function
, call_args
->length (),
19997 call_args
->address ());
19998 /* The thunk location is not interesting. */
19999 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20000 CALL_FROM_THUNK_P (ret
) = true;
20001 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20002 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20007 /* We do not perform argument-dependent lookup if normal
20008 lookup finds a non-function, in accordance with the
20009 expected resolution of DR 218. */
20011 && ((is_overloaded_fn (function
)
20012 /* If lookup found a member function, the Koenig lookup is
20013 not appropriate, even if an unqualified-name was used
20014 to denote the function. */
20015 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20016 || identifier_p (function
))
20017 /* Only do this when substitution turns a dependent call
20018 into a non-dependent call. */
20019 && type_dependent_expression_p_push (t
)
20020 && !any_type_dependent_arguments_p (call_args
))
20021 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20023 if (function
!= NULL_TREE
20024 && identifier_p (function
)
20025 && !any_type_dependent_arguments_p (call_args
))
20027 if (koenig_p
&& (complain
& tf_warning_or_error
))
20029 /* For backwards compatibility and good diagnostics, try
20030 the unqualified lookup again if we aren't in SFINAE
20032 tree unq
= (tsubst_copy_and_build
20033 (function
, args
, complain
, in_decl
, true,
20034 integral_constant_expression_p
));
20035 if (unq
== error_mark_node
)
20036 RETURN (error_mark_node
);
20038 if (unq
!= function
)
20040 /* In a lambda fn, we have to be careful to not
20041 introduce new this captures. Legacy code can't
20042 be using lambdas anyway, so it's ok to be
20044 bool in_lambda
= (current_class_type
20045 && LAMBDA_TYPE_P (current_class_type
));
20046 char const *const msg
20047 = G_("%qD was not declared in this scope, "
20048 "and no declarations were found by "
20049 "argument-dependent lookup at the point "
20050 "of instantiation");
20054 error_at (cp_expr_loc_or_input_loc (t
),
20057 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20063 if (INDIRECT_REF_P (fn
))
20064 fn
= TREE_OPERAND (fn
, 0);
20065 if (is_overloaded_fn (fn
))
20066 fn
= get_first_fn (fn
);
20069 /* Can't say anything more. */;
20070 else if (DECL_CLASS_SCOPE_P (fn
))
20072 location_t loc
= cp_expr_loc_or_input_loc (t
);
20074 "declarations in dependent base %qT are "
20075 "not found by unqualified lookup",
20076 DECL_CLASS_CONTEXT (fn
));
20077 if (current_class_ptr
)
20079 "use %<this->%D%> instead", function
);
20082 "use %<%T::%D%> instead",
20083 current_class_name
, function
);
20086 inform (DECL_SOURCE_LOCATION (fn
),
20087 "%qD declared here, later in the "
20088 "translation unit", fn
);
20090 RETURN (error_mark_node
);
20096 if (identifier_p (function
))
20098 if (complain
& tf_error
)
20099 unqualified_name_lookup_error (function
);
20100 RETURN (error_mark_node
);
20104 /* Remember that there was a reference to this entity. */
20105 if (function
!= NULL_TREE
20106 && DECL_P (function
)
20107 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20108 RETURN (error_mark_node
);
20110 /* Put back tf_decltype for the actual call. */
20111 complain
|= decltype_flag
;
20113 if (function
== NULL_TREE
)
20114 switch (CALL_EXPR_IFN (t
))
20117 gcc_assert (nargs
== 1);
20118 if (vec_safe_length (call_args
) != 1)
20120 error_at (cp_expr_loc_or_input_loc (t
),
20121 "wrong number of arguments to "
20122 "%<__builtin_launder%>");
20123 ret
= error_mark_node
;
20126 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20127 (*call_args
)[0], complain
);
20130 case IFN_VEC_CONVERT
:
20131 gcc_assert (nargs
== 1);
20132 if (vec_safe_length (call_args
) != 1)
20134 error_at (cp_expr_loc_or_input_loc (t
),
20135 "wrong number of arguments to "
20136 "%<__builtin_convertvector%>");
20137 ret
= error_mark_node
;
20140 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20141 tsubst (TREE_TYPE (t
), args
,
20142 complain
, in_decl
),
20144 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20149 /* Unsupported internal function with arguments. */
20150 gcc_unreachable ();
20152 else if (TREE_CODE (function
) == OFFSET_REF
20153 || TREE_CODE (function
) == DOTSTAR_EXPR
20154 || TREE_CODE (function
) == MEMBER_REF
)
20155 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20157 else if (TREE_CODE (function
) == COMPONENT_REF
)
20159 tree instance
= TREE_OPERAND (function
, 0);
20160 tree fn
= TREE_OPERAND (function
, 1);
20162 if (processing_template_decl
20163 && (type_dependent_expression_p (instance
)
20164 || (!BASELINK_P (fn
)
20165 && TREE_CODE (fn
) != FIELD_DECL
)
20166 || type_dependent_expression_p (fn
)
20167 || any_type_dependent_arguments_p (call_args
)))
20168 ret
= build_min_nt_call_vec (function
, call_args
);
20169 else if (!BASELINK_P (fn
))
20170 ret
= finish_call_expr (function
, &call_args
,
20171 /*disallow_virtual=*/false,
20172 /*koenig_p=*/false,
20175 ret
= (build_new_method_call
20177 &call_args
, NULL_TREE
,
20178 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20182 else if (concept_check_p (function
))
20184 /* FUNCTION is a template-id referring to a concept definition. */
20185 tree id
= unpack_concept_check (function
);
20186 tree tmpl
= TREE_OPERAND (id
, 0);
20187 tree args
= TREE_OPERAND (id
, 1);
20189 /* Calls to standard and variable concepts should have been
20190 previously diagnosed. */
20191 gcc_assert (function_concept_p (tmpl
));
20193 /* Ensure the result is wrapped as a call expression. */
20194 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20197 ret
= finish_call_expr (function
, &call_args
,
20198 /*disallow_virtual=*/qualified_p
,
20202 if (ret
!= error_mark_node
)
20204 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20205 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20206 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20207 if (op
|| ord
|| rev
)
20209 function
= extract_call_expr (ret
);
20210 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20211 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20212 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20221 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20222 cond
= mark_rvalue_use (cond
);
20223 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20226 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20228 if (integer_zerop (folded_cond
))
20230 ++c_inhibit_evaluation_warnings
;
20231 exp1
= RECUR (TREE_OPERAND (t
, 1));
20232 --c_inhibit_evaluation_warnings
;
20233 exp2
= RECUR (TREE_OPERAND (t
, 2));
20237 exp1
= RECUR (TREE_OPERAND (t
, 1));
20238 ++c_inhibit_evaluation_warnings
;
20239 exp2
= RECUR (TREE_OPERAND (t
, 2));
20240 --c_inhibit_evaluation_warnings
;
20242 cond
= folded_cond
;
20246 exp1
= RECUR (TREE_OPERAND (t
, 1));
20247 exp2
= RECUR (TREE_OPERAND (t
, 2));
20250 warning_sentinel
s(warn_duplicated_branches
);
20251 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20252 cond
, exp1
, exp2
, complain
));
20255 case PSEUDO_DTOR_EXPR
:
20257 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20258 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20259 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20260 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20265 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20267 case COMPONENT_REF
:
20274 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20275 args
, complain
, in_decl
);
20276 /* Remember that there was a reference to this entity. */
20277 if (DECL_P (object
)
20278 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20279 RETURN (error_mark_node
);
20280 object_type
= TREE_TYPE (object
);
20282 member
= TREE_OPERAND (t
, 1);
20283 if (BASELINK_P (member
))
20284 member
= tsubst_baselink (member
,
20285 non_reference (TREE_TYPE (object
)),
20286 args
, complain
, in_decl
);
20288 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20289 if (member
== error_mark_node
)
20290 RETURN (error_mark_node
);
20292 if (TREE_CODE (member
) == FIELD_DECL
)
20294 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20295 if (TREE_CODE (r
) == COMPONENT_REF
)
20296 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20299 else if (type_dependent_expression_p (object
))
20300 /* We can't do much here. */;
20301 else if (!CLASS_TYPE_P (object_type
))
20303 if (scalarish_type_p (object_type
))
20305 tree s
= NULL_TREE
;
20306 tree dtor
= member
;
20308 if (TREE_CODE (dtor
) == SCOPE_REF
)
20310 s
= TREE_OPERAND (dtor
, 0);
20311 dtor
= TREE_OPERAND (dtor
, 1);
20313 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20315 dtor
= TREE_OPERAND (dtor
, 0);
20317 RETURN (finish_pseudo_destructor_expr
20318 (object
, s
, dtor
, input_location
));
20322 else if (TREE_CODE (member
) == SCOPE_REF
20323 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20325 /* Lookup the template functions now that we know what the
20327 tree scope
= TREE_OPERAND (member
, 0);
20328 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20329 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20330 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20331 /*complain=*/false);
20332 if (BASELINK_P (member
))
20334 BASELINK_FUNCTIONS (member
)
20335 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20337 member
= (adjust_result_of_qualified_name_lookup
20338 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20343 qualified_name_lookup_error (scope
, tmpl
, member
,
20345 RETURN (error_mark_node
);
20348 else if (TREE_CODE (member
) == SCOPE_REF
20349 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20350 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20352 if (complain
& tf_error
)
20354 if (TYPE_P (TREE_OPERAND (member
, 0)))
20355 error ("%qT is not a class or namespace",
20356 TREE_OPERAND (member
, 0));
20358 error ("%qD is not a class or namespace",
20359 TREE_OPERAND (member
, 0));
20361 RETURN (error_mark_node
);
20364 r
= finish_class_member_access_expr (object
, member
,
20365 /*template_p=*/false,
20367 if (TREE_CODE (r
) == COMPONENT_REF
)
20368 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20373 RETURN (build_throw
20374 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20378 vec
<constructor_elt
, va_gc
> *n
;
20379 constructor_elt
*ce
;
20380 unsigned HOST_WIDE_INT idx
;
20381 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20382 bool process_index_p
;
20384 bool need_copy_p
= false;
20387 if (type
== error_mark_node
)
20388 RETURN (error_mark_node
);
20390 /* We do not want to process the index of aggregate
20391 initializers as they are identifier nodes which will be
20392 looked up by digest_init. */
20393 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20395 if (null_member_pointer_value_p (t
))
20397 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20401 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20402 newlen
= vec_safe_length (n
);
20403 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20405 if (ce
->index
&& process_index_p
20406 /* An identifier index is looked up in the type
20407 being initialized, not the current scope. */
20408 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20409 ce
->index
= RECUR (ce
->index
);
20411 if (PACK_EXPANSION_P (ce
->value
))
20413 /* Substitute into the pack expansion. */
20414 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20417 if (ce
->value
== error_mark_node
20418 || PACK_EXPANSION_P (ce
->value
))
20420 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20421 /* Just move the argument into place. */
20422 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20425 /* Update the length of the final CONSTRUCTOR
20426 arguments vector, and note that we will need to
20428 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20429 need_copy_p
= true;
20433 ce
->value
= RECUR (ce
->value
);
20438 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20440 vec_alloc (n
, newlen
);
20441 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20443 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20445 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20446 for (i
= 0; i
< len
; ++i
)
20447 CONSTRUCTOR_APPEND_ELT (n
, 0,
20448 TREE_VEC_ELT (ce
->value
, i
));
20451 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20455 r
= build_constructor (init_list_type_node
, n
);
20456 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20457 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20458 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20460 if (TREE_HAS_CONSTRUCTOR (t
))
20462 fcl_t cl
= fcl_functional
;
20463 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20465 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20468 TREE_TYPE (r
) = type
;
20474 tree operand_0
= TREE_OPERAND (t
, 0);
20475 if (TYPE_P (operand_0
))
20477 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20478 RETURN (get_typeid (operand_0
, complain
));
20482 operand_0
= RECUR (operand_0
);
20483 RETURN (build_typeid (operand_0
, complain
));
20494 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20495 /* ??? We're doing a subset of finish_id_expression here. */
20496 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20497 /* Replace an evaluated use of the thread_local variable with
20498 a call to its wrapper. */
20500 else if (outer_automatic_var_p (r
))
20501 r
= process_outer_var_ref (r
, complain
);
20503 if (!TYPE_REF_P (TREE_TYPE (t
)))
20504 /* If the original type was a reference, we'll be wrapped in
20505 the appropriate INDIRECT_REF. */
20506 r
= convert_from_reference (r
);
20512 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20513 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20514 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20517 case OFFSETOF_EXPR
:
20520 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20521 in_decl
, /*function_p=*/false,
20522 /*integral_constant_expression_p=*/false);
20523 RETURN (finish_offsetof (object_ptr
,
20524 RECUR (TREE_OPERAND (t
, 0)),
20525 EXPR_LOCATION (t
)));
20528 case ADDRESSOF_EXPR
:
20529 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20530 RECUR (TREE_OPERAND (t
, 0)), complain
));
20534 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20535 complain
, in_decl
);
20536 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20537 complain
, in_decl
);
20538 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20539 TRAIT_EXPR_KIND (t
), type1
, type2
));
20544 tree old_stmt_expr
= cur_stmt_expr
;
20545 tree stmt_expr
= begin_stmt_expr ();
20547 cur_stmt_expr
= stmt_expr
;
20548 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20549 integral_constant_expression_p
);
20550 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20551 cur_stmt_expr
= old_stmt_expr
;
20553 /* If the resulting list of expression statement is empty,
20554 fold it further into void_node. */
20555 if (empty_expr_stmt_p (stmt_expr
))
20556 stmt_expr
= void_node
;
20558 RETURN (stmt_expr
);
20563 if (complain
& tf_partial
)
20565 /* We don't have a full set of template arguments yet; don't touch
20566 the lambda at all. */
20567 gcc_assert (processing_template_decl
);
20570 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20572 RETURN (build_lambda_object (r
));
20576 /* We can get here for a constant initializer of non-dependent type.
20577 FIXME stop folding in cp_parser_initializer_clause. */
20579 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20584 case TRANSACTION_EXPR
:
20585 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20586 integral_constant_expression_p
));
20589 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20591 case VEC_PERM_EXPR
:
20593 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20594 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20595 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20596 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20600 case REQUIRES_EXPR
:
20602 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20607 /* No need to substitute further, a RANGE_EXPR will always be built
20608 with constant operands. */
20611 case NON_LVALUE_EXPR
:
20612 case VIEW_CONVERT_EXPR
:
20613 if (location_wrapper_p (t
))
20614 /* We need to do this here as well as in tsubst_copy so we get the
20615 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20616 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20617 EXPR_LOCATION (t
)));
20621 /* Handle Objective-C++ constructs, if appropriate. */
20624 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20625 in_decl
, /*function_p=*/false);
20629 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20635 input_location
= save_loc
;
20639 /* Verify that the instantiated ARGS are valid. For type arguments,
20640 make sure that the type's linkage is ok. For non-type arguments,
20641 make sure they are constants if they are integral or enumerations.
20642 Emit an error under control of COMPLAIN, and return TRUE on error. */
20645 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20647 if (dependent_template_arg_p (t
))
20649 if (ARGUMENT_PACK_P (t
))
20651 tree vec
= ARGUMENT_PACK_ARGS (t
);
20652 int len
= TREE_VEC_LENGTH (vec
);
20653 bool result
= false;
20656 for (i
= 0; i
< len
; ++i
)
20657 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20661 else if (TYPE_P (t
))
20663 /* [basic.link]: A name with no linkage (notably, the name
20664 of a class or enumeration declared in a local scope)
20665 shall not be used to declare an entity with linkage.
20666 This implies that names with no linkage cannot be used as
20669 DR 757 relaxes this restriction for C++0x. */
20670 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20671 : no_linkage_check (t
, /*relaxed_p=*/false));
20675 /* DR 488 makes use of a type with no linkage cause
20676 type deduction to fail. */
20677 if (complain
& tf_error
)
20679 if (TYPE_UNNAMED_P (nt
))
20680 error ("%qT is/uses unnamed type", t
);
20682 error ("template argument for %qD uses local type %qT",
20687 /* In order to avoid all sorts of complications, we do not
20688 allow variably-modified types as template arguments. */
20689 else if (variably_modified_type_p (t
, NULL_TREE
))
20691 if (complain
& tf_error
)
20692 error ("%qT is a variably modified type", t
);
20696 /* Class template and alias template arguments should be OK. */
20697 else if (DECL_TYPE_TEMPLATE_P (t
))
20699 /* A non-type argument of integral or enumerated type must be a
20701 else if (TREE_TYPE (t
)
20702 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20703 && !REFERENCE_REF_P (t
)
20704 && !TREE_CONSTANT (t
))
20706 if (complain
& tf_error
)
20707 error ("integral expression %qE is not constant", t
);
20714 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20716 int ix
, len
= DECL_NTPARMS (tmpl
);
20717 bool result
= false;
20719 for (ix
= 0; ix
!= len
; ix
++)
20721 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20724 if (result
&& (complain
& tf_error
))
20725 error (" trying to instantiate %qD", tmpl
);
20729 /* We're out of SFINAE context now, so generate diagnostics for the access
20730 errors we saw earlier when instantiating D from TMPL and ARGS. */
20733 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20735 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20736 tree type
= TREE_TYPE (pattern
);
20737 location_t loc
= input_location
;
20739 push_access_scope (d
);
20740 push_deferring_access_checks (dk_no_deferred
);
20741 input_location
= DECL_SOURCE_LOCATION (pattern
);
20742 tsubst (type
, args
, tf_warning_or_error
, d
);
20743 input_location
= loc
;
20744 pop_deferring_access_checks ();
20745 pop_access_scope (d
);
20748 /* Instantiate the indicated variable, function, or alias template TMPL with
20749 the template arguments in TARG_PTR. */
20752 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20754 tree targ_ptr
= orig_args
;
20758 bool access_ok
= true;
20760 if (tmpl
== error_mark_node
)
20761 return error_mark_node
;
20763 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20765 /* If this function is a clone, handle it specially. */
20766 if (DECL_CLONED_FUNCTION_P (tmpl
))
20771 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20772 DECL_CLONED_FUNCTION. */
20773 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20774 targ_ptr
, complain
);
20775 if (spec
== error_mark_node
)
20776 return error_mark_node
;
20778 /* Look for the clone. */
20779 FOR_EACH_CLONE (clone
, spec
)
20780 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20782 /* We should always have found the clone by now. */
20783 gcc_unreachable ();
20787 if (targ_ptr
== error_mark_node
)
20788 return error_mark_node
;
20790 /* Check to see if we already have this specialization. */
20791 gen_tmpl
= most_general_template (tmpl
);
20792 if (TMPL_ARGS_DEPTH (targ_ptr
)
20793 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20794 /* targ_ptr only has the innermost template args, so add the outer ones
20795 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20796 the case of a non-dependent call within a template definition). */
20797 targ_ptr
= (add_outermost_template_args
20798 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20801 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20802 but it doesn't seem to be on the hot path. */
20803 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20805 gcc_checking_assert (tmpl
== gen_tmpl
20807 = retrieve_specialization (tmpl
, orig_args
, 0))
20809 || fndecl
== NULL_TREE
);
20811 if (spec
!= NULL_TREE
)
20813 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20815 if (complain
& tf_error
)
20816 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20817 return error_mark_node
;
20822 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20824 return error_mark_node
;
20826 /* We are building a FUNCTION_DECL, during which the access of its
20827 parameters and return types have to be checked. However this
20828 FUNCTION_DECL which is the desired context for access checking
20829 is not built yet. We solve this chicken-and-egg problem by
20830 deferring all checks until we have the FUNCTION_DECL. */
20831 push_deferring_access_checks (dk_deferred
);
20833 /* Instantiation of the function happens in the context of the function
20834 template, not the context of the overload resolution we're doing. */
20835 push_to_top_level ();
20836 /* If there are dependent arguments, e.g. because we're doing partial
20837 ordering, make sure processing_template_decl stays set. */
20838 if (uses_template_parms (targ_ptr
))
20839 ++processing_template_decl
;
20840 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20842 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20843 complain
, gen_tmpl
, true);
20844 push_nested_class (ctx
);
20847 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20849 fndecl
= NULL_TREE
;
20850 if (VAR_P (pattern
))
20852 /* We need to determine if we're using a partial or explicit
20853 specialization now, because the type of the variable could be
20855 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20856 tree elt
= most_specialized_partial_spec (tid
, complain
);
20857 if (elt
== error_mark_node
)
20858 pattern
= error_mark_node
;
20861 tree partial_tmpl
= TREE_VALUE (elt
);
20862 tree partial_args
= TREE_PURPOSE (elt
);
20863 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20864 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20868 /* Substitute template parameters to obtain the specialization. */
20869 if (fndecl
== NULL_TREE
)
20870 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20871 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20872 pop_nested_class ();
20873 pop_from_top_level ();
20875 if (fndecl
== error_mark_node
)
20877 pop_deferring_access_checks ();
20878 return error_mark_node
;
20881 /* The DECL_TI_TEMPLATE should always be the immediate parent
20882 template, not the most general template. */
20883 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20884 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20886 /* Now we know the specialization, compute access previously
20887 deferred. Do no access control for inheriting constructors,
20888 as we already checked access for the inherited constructor. */
20889 if (!(flag_new_inheriting_ctors
20890 && DECL_INHERITED_CTOR (fndecl
)))
20892 push_access_scope (fndecl
);
20893 if (!perform_deferred_access_checks (complain
))
20895 pop_access_scope (fndecl
);
20897 pop_deferring_access_checks ();
20899 /* If we've just instantiated the main entry point for a function,
20900 instantiate all the alternate entry points as well. We do this
20901 by cloning the instantiation of the main entry point, not by
20902 instantiating the template clones. */
20903 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20904 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20905 clone_cdtor (fndecl
, /*update_methods=*/false);
20909 if (!(complain
& tf_error
))
20911 /* Remember to reinstantiate when we're out of SFINAE so the user
20912 can see the errors. */
20913 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20915 return error_mark_node
;
20920 /* Wrapper for instantiate_template_1. */
20923 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20926 timevar_push (TV_TEMPLATE_INST
);
20927 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20928 timevar_pop (TV_TEMPLATE_INST
);
20932 /* Instantiate the alias template TMPL with ARGS. Also push a template
20933 instantiation level, which instantiate_template doesn't do because
20934 functions and variables have sufficient context established by the
20938 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20940 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20941 return error_mark_node
;
20944 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20945 args
, tmpl
, complain
,
20946 /*require_all_args=*/true,
20947 /*use_default_args=*/true);
20949 /* FIXME check for satisfaction in check_instantiated_args. */
20951 && !any_dependent_template_arguments_p (args
)
20952 && !constraints_satisfied_p (tmpl
, args
))
20954 if (complain
& tf_error
)
20956 auto_diagnostic_group d
;
20957 error ("template constraint failure for %qD", tmpl
);
20958 diagnose_constraints (input_location
, tmpl
, args
);
20960 return error_mark_node
;
20963 if (!push_tinst_level (tmpl
, args
))
20964 return error_mark_node
;
20965 tree r
= instantiate_template (tmpl
, args
, complain
);
20966 pop_tinst_level ();
20971 /* PARM is a template parameter pack for FN. Returns true iff
20972 PARM is used in a deducible way in the argument list of FN. */
20975 pack_deducible_p (tree parm
, tree fn
)
20977 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20978 for (; t
; t
= TREE_CHAIN (t
))
20980 tree type
= TREE_VALUE (t
);
20982 if (!PACK_EXPANSION_P (type
))
20984 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20985 packs
; packs
= TREE_CHAIN (packs
))
20986 if (template_args_equal (TREE_VALUE (packs
), parm
))
20988 /* The template parameter pack is used in a function parameter
20989 pack. If this is the end of the parameter list, the
20990 template parameter pack is deducible. */
20991 if (TREE_CHAIN (t
) == void_list_node
)
20994 /* Otherwise, not. Well, it could be deduced from
20995 a non-pack parameter, but doing so would end up with
20996 a deduction mismatch, so don't bother. */
21000 /* The template parameter pack isn't used in any function parameter
21001 packs, but it might be used deeper, e.g. tuple<Args...>. */
21005 /* Subroutine of fn_type_unification: check non-dependent parms for
21009 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21010 tree fn
, unification_kind_t strict
, int flags
,
21011 struct conversion
**convs
, bool explain_p
)
21013 /* Non-constructor methods need to leave a conversion for 'this', which
21014 isn't included in nargs here. */
21015 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21016 && !DECL_CONSTRUCTOR_P (fn
));
21018 for (unsigned ia
= 0;
21019 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21021 tree parm
= TREE_VALUE (parms
);
21023 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21024 && (!TREE_CHAIN (parms
)
21025 || TREE_CHAIN (parms
) == void_list_node
))
21026 /* For a function parameter pack that occurs at the end of the
21027 parameter-declaration-list, the type A of each remaining
21028 argument of the call is compared with the type P of the
21029 declarator-id of the function parameter pack. */
21032 parms
= TREE_CHAIN (parms
);
21034 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21035 /* For a function parameter pack that does not occur at the
21036 end of the parameter-declaration-list, the type of the
21037 parameter pack is a non-deduced context. */
21040 if (!uses_template_parms (parm
))
21042 tree arg
= args
[ia
];
21043 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21044 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21046 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21047 conv_p
, explain_p
))
21057 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21058 NARGS elements of the arguments that are being used when calling
21059 it. TARGS is a vector into which the deduced template arguments
21062 Returns either a FUNCTION_DECL for the matching specialization of FN or
21063 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21064 true, diagnostics will be printed to explain why it failed.
21066 If FN is a conversion operator, or we are trying to produce a specific
21067 specialization, RETURN_TYPE is the return type desired.
21069 The EXPLICIT_TARGS are explicit template arguments provided via a
21072 The parameter STRICT is one of:
21075 We are deducing arguments for a function call, as in
21076 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21077 deducing arguments for a call to the result of a conversion
21078 function template, as in [over.call.object].
21081 We are deducing arguments for a conversion function, as in
21082 [temp.deduct.conv].
21085 We are deducing arguments when doing an explicit instantiation
21086 as in [temp.explicit], when determining an explicit specialization
21087 as in [temp.expl.spec], or when taking the address of a function
21088 template, as in [temp.deduct.funcaddr]. */
21091 fn_type_unification (tree fn
,
21092 tree explicit_targs
,
21095 unsigned int nargs
,
21097 unification_kind_t strict
,
21099 struct conversion
**convs
,
21105 tree decl
= NULL_TREE
;
21106 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21108 static int deduction_depth
;
21109 /* type_unification_real will pass back any access checks from default
21110 template argument substitution. */
21111 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21112 /* We don't have all the template args yet. */
21113 bool incomplete
= true;
21116 if (flag_new_inheriting_ctors
)
21117 fn
= strip_inheriting_ctors (fn
);
21119 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21120 tree r
= error_mark_node
;
21122 tree full_targs
= targs
;
21123 if (TMPL_ARGS_DEPTH (targs
)
21124 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21125 full_targs
= (add_outermost_template_args
21126 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21130 complain
|= tf_decltype
;
21132 /* In C++0x, it's possible to have a function template whose type depends
21133 on itself recursively. This is most obvious with decltype, but can also
21134 occur with enumeration scope (c++/48969). So we need to catch infinite
21135 recursion and reject the substitution at deduction time; this function
21136 will return error_mark_node for any repeated substitution.
21138 This also catches excessive recursion such as when f<N> depends on
21139 f<N-1> across all integers, and returns error_mark_node for all the
21140 substitutions back up to the initial one.
21142 This is, of course, not reentrant. */
21143 if (excessive_deduction_depth
)
21144 return error_mark_node
;
21147 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21149 fntype
= TREE_TYPE (fn
);
21150 if (explicit_targs
)
21154 The specified template arguments must match the template
21155 parameters in kind (i.e., type, nontype, template), and there
21156 must not be more arguments than there are parameters;
21157 otherwise type deduction fails.
21159 Nontype arguments must match the types of the corresponding
21160 nontype template parameters, or must be convertible to the
21161 types of the corresponding nontype parameters as specified in
21162 _temp.arg.nontype_, otherwise type deduction fails.
21164 All references in the function type of the function template
21165 to the corresponding template parameters are replaced by the
21166 specified template argument values. If a substitution in a
21167 template parameter or in the function type of the function
21168 template results in an invalid type, type deduction fails. */
21169 int i
, len
= TREE_VEC_LENGTH (tparms
);
21170 location_t loc
= input_location
;
21171 incomplete
= false;
21173 if (explicit_targs
== error_mark_node
)
21176 if (TMPL_ARGS_DEPTH (explicit_targs
)
21177 < TMPL_ARGS_DEPTH (full_targs
))
21178 explicit_targs
= add_outermost_template_args (full_targs
,
21181 /* Adjust any explicit template arguments before entering the
21182 substitution context. */
21184 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21185 complain
|tf_partial
,
21186 /*require_all_args=*/false,
21187 /*use_default_args=*/false));
21188 if (explicit_targs
== error_mark_node
)
21191 /* Substitute the explicit args into the function type. This is
21192 necessary so that, for instance, explicitly declared function
21193 arguments can match null pointed constants. If we were given
21194 an incomplete set of explicit args, we must not do semantic
21195 processing during substitution as we could create partial
21197 for (i
= 0; i
< len
; i
++)
21199 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21200 bool parameter_pack
= false;
21201 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21203 /* Dig out the actual parm. */
21204 if (TREE_CODE (parm
) == TYPE_DECL
21205 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21207 parm
= TREE_TYPE (parm
);
21208 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21210 else if (TREE_CODE (parm
) == PARM_DECL
)
21212 parm
= DECL_INITIAL (parm
);
21213 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21216 if (targ
== NULL_TREE
)
21217 /* No explicit argument for this template parameter. */
21219 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21221 /* Mark the argument pack as "incomplete". We could
21222 still deduce more arguments during unification.
21223 We remove this mark in type_unification_real. */
21224 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21225 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21226 = ARGUMENT_PACK_ARGS (targ
);
21228 /* We have some incomplete argument packs. */
21235 if (!push_tinst_level (fn
, explicit_targs
))
21237 excessive_deduction_depth
= true;
21240 ++processing_template_decl
;
21241 input_location
= DECL_SOURCE_LOCATION (fn
);
21242 /* Ignore any access checks; we'll see them again in
21243 instantiate_template and they might have the wrong
21244 access path at this point. */
21245 push_deferring_access_checks (dk_deferred
);
21246 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21247 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21248 pop_deferring_access_checks ();
21249 input_location
= loc
;
21250 --processing_template_decl
;
21251 pop_tinst_level ();
21253 if (fntype
== error_mark_node
)
21257 /* Place the explicitly specified arguments in TARGS. */
21258 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21259 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21260 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21261 if (!incomplete
&& CHECKING_P
21262 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21264 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21267 if (return_type
&& strict
!= DEDUCE_CALL
)
21269 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21270 new_args
[0] = return_type
;
21271 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21279 /* Never do unification on the 'this' parameter. */
21280 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21282 if (return_type
&& strict
== DEDUCE_CALL
)
21284 /* We're deducing for a call to the result of a template conversion
21285 function. The parms we really want are in return_type. */
21286 if (INDIRECT_TYPE_P (return_type
))
21287 return_type
= TREE_TYPE (return_type
);
21288 parms
= TYPE_ARG_TYPES (return_type
);
21290 else if (return_type
)
21292 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21295 /* We allow incomplete unification without an error message here
21296 because the standard doesn't seem to explicitly prohibit it. Our
21297 callers must be ready to deal with unification failures in any
21300 /* If we aren't explaining yet, push tinst context so we can see where
21301 any errors (e.g. from class instantiations triggered by instantiation
21302 of default template arguments) come from. If we are explaining, this
21303 context is redundant. */
21304 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21306 excessive_deduction_depth
= true;
21310 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21311 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21312 strict
, &checks
, explain_p
);
21314 pop_tinst_level ();
21318 /* Now that we have bindings for all of the template arguments,
21319 ensure that the arguments deduced for the template template
21320 parameters have compatible template parameter lists. We cannot
21321 check this property before we have deduced all template
21322 arguments, because the template parameter types of a template
21323 template parameter might depend on prior template parameters
21324 deduced after the template template parameter. The following
21325 ill-formed example illustrates this issue:
21327 template<typename T, template<T> class C> void f(C<5>, T);
21329 template<int N> struct X {};
21332 f(X<5>(), 5l); // error: template argument deduction fails
21335 The template parameter list of 'C' depends on the template type
21336 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21337 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21338 time that we deduce 'C'. */
21339 if (!template_template_parm_bindings_ok_p
21340 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21342 unify_inconsistent_template_template_parameters (explain_p
);
21348 /* CWG2369: Check satisfaction before non-deducible conversions. */
21349 if (!constraints_satisfied_p (fn
, targs
))
21352 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21356 /* DR 1391: All parameters have args, now check non-dependent parms for
21357 convertibility. We don't do this if all args were explicitly specified,
21358 as the standard says that we substitute explicit args immediately. */
21360 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21364 /* All is well so far. Now, check:
21368 When all template arguments have been deduced, all uses of
21369 template parameters in nondeduced contexts are replaced with
21370 the corresponding deduced argument values. If the
21371 substitution results in an invalid type, as described above,
21372 type deduction fails. */
21373 if (!push_tinst_level (fn
, targs
))
21375 excessive_deduction_depth
= true;
21379 /* Also collect access checks from the instantiation. */
21380 reopen_deferring_access_checks (checks
);
21382 decl
= instantiate_template (fn
, targs
, complain
);
21384 checks
= get_deferred_access_checks ();
21385 pop_deferring_access_checks ();
21387 pop_tinst_level ();
21389 if (decl
== error_mark_node
)
21392 /* Now perform any access checks encountered during substitution. */
21393 push_access_scope (decl
);
21394 ok
= perform_access_checks (checks
, complain
);
21395 pop_access_scope (decl
);
21399 /* If we're looking for an exact match, check that what we got
21400 is indeed an exact match. It might not be if some template
21401 parameters are used in non-deduced contexts. But don't check
21402 for an exact match if we have dependent template arguments;
21403 in that case we're doing partial ordering, and we already know
21404 that we have two candidates that will provide the actual type. */
21405 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21407 tree substed
= TREE_TYPE (decl
);
21411 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21413 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21414 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21415 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21417 unify_type_mismatch (explain_p
, args
[i
],
21418 TREE_VALUE (sarg
));
21423 /* After doing deduction with the inherited constructor, actually return an
21424 instantiation of the inheriting constructor. */
21426 decl
= instantiate_template (orig_fn
, targs
, complain
);
21432 if (excessive_deduction_depth
)
21434 if (deduction_depth
== 0)
21435 /* Reset once we're all the way out. */
21436 excessive_deduction_depth
= false;
21442 /* Adjust types before performing type deduction, as described in
21443 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21444 sections are symmetric. PARM is the type of a function parameter
21445 or the return type of the conversion function. ARG is the type of
21446 the argument passed to the call, or the type of the value
21447 initialized with the result of the conversion function.
21448 ARG_EXPR is the original argument expression, which may be null. */
21451 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21464 /* Swap PARM and ARG throughout the remainder of this
21465 function; the handling is precisely symmetric since PARM
21466 will initialize ARG rather than vice versa. */
21467 std::swap (parm
, arg
);
21471 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21472 too, but here handle it by stripping the reference from PARM
21473 rather than by adding it to ARG. */
21474 if (TYPE_REF_P (*parm
)
21475 && TYPE_REF_IS_RVALUE (*parm
)
21476 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21477 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21478 && TYPE_REF_P (*arg
)
21479 && !TYPE_REF_IS_RVALUE (*arg
))
21480 *parm
= TREE_TYPE (*parm
);
21481 /* Nothing else to do in this case. */
21485 gcc_unreachable ();
21488 if (!TYPE_REF_P (*parm
))
21490 /* [temp.deduct.call]
21492 If P is not a reference type:
21494 --If A is an array type, the pointer type produced by the
21495 array-to-pointer standard conversion (_conv.array_) is
21496 used in place of A for type deduction; otherwise,
21498 --If A is a function type, the pointer type produced by
21499 the function-to-pointer standard conversion
21500 (_conv.func_) is used in place of A for type deduction;
21503 --If A is a cv-qualified type, the top level
21504 cv-qualifiers of A's type are ignored for type
21506 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21507 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21508 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21509 *arg
= build_pointer_type (*arg
);
21511 *arg
= TYPE_MAIN_VARIANT (*arg
);
21514 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21515 reference to a cv-unqualified template parameter that does not represent a
21516 template parameter of a class template (during class template argument
21517 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21518 an lvalue, the type "lvalue reference to A" is used in place of A for type
21520 if (TYPE_REF_P (*parm
)
21521 && TYPE_REF_IS_RVALUE (*parm
)
21522 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21523 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21524 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21525 && (arg_expr
? lvalue_p (arg_expr
)
21526 /* try_one_overload doesn't provide an arg_expr, but
21527 functions are always lvalues. */
21528 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21529 *arg
= build_reference_type (*arg
);
21531 /* [temp.deduct.call]
21533 If P is a cv-qualified type, the top level cv-qualifiers
21534 of P's type are ignored for type deduction. If P is a
21535 reference type, the type referred to by P is used for
21537 *parm
= TYPE_MAIN_VARIANT (*parm
);
21538 if (TYPE_REF_P (*parm
))
21540 *parm
= TREE_TYPE (*parm
);
21541 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21544 /* DR 322. For conversion deduction, remove a reference type on parm
21545 too (which has been swapped into ARG). */
21546 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21547 *arg
= TREE_TYPE (*arg
);
21552 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21553 template which doesn't contain any deducible template parameters; check if
21554 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21555 unify_one_argument. */
21558 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21559 int flags
, struct conversion
**conv_p
,
21565 type
= TREE_TYPE (arg
);
21569 if (same_type_p (parm
, type
))
21570 return unify_success (explain_p
);
21572 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21573 if (strict
== DEDUCE_CONV
)
21575 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21576 return unify_success (explain_p
);
21578 else if (strict
!= DEDUCE_EXACT
)
21581 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21583 /* Avoid recalculating this in add_function_candidate. */
21585 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21587 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21589 return unify_success (explain_p
);
21592 if (strict
== DEDUCE_EXACT
)
21593 return unify_type_mismatch (explain_p
, parm
, arg
);
21595 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21598 static bool uses_deducible_template_parms (tree type
);
21600 /* Returns true iff the expression EXPR is one from which a template
21601 argument can be deduced. In other words, if it's an undecorated
21602 use of a template non-type parameter. */
21605 deducible_expression (tree expr
)
21607 /* Strip implicit conversions. */
21608 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21609 expr
= TREE_OPERAND (expr
, 0);
21610 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21613 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21614 deducible way; that is, if it has a max value of <PARM> - 1. */
21617 deducible_array_bound (tree domain
)
21619 if (domain
== NULL_TREE
)
21622 tree max
= TYPE_MAX_VALUE (domain
);
21623 if (TREE_CODE (max
) != MINUS_EXPR
)
21626 return deducible_expression (TREE_OPERAND (max
, 0));
21629 /* Returns true iff the template arguments ARGS use a template parameter
21630 in a deducible way. */
21633 deducible_template_args (tree args
)
21635 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21638 tree elt
= TREE_VEC_ELT (args
, i
);
21639 if (ARGUMENT_PACK_P (elt
))
21640 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21643 if (PACK_EXPANSION_P (elt
))
21644 elt
= PACK_EXPANSION_PATTERN (elt
);
21645 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21647 else if (TYPE_P (elt
))
21648 deducible
= uses_deducible_template_parms (elt
);
21650 deducible
= deducible_expression (elt
);
21658 /* Returns true iff TYPE contains any deducible references to template
21659 parameters, as per 14.8.2.5. */
21662 uses_deducible_template_parms (tree type
)
21664 if (PACK_EXPANSION_P (type
))
21665 type
= PACK_EXPANSION_PATTERN (type
);
21672 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21673 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21679 if (INDIRECT_TYPE_P (type
))
21680 return uses_deducible_template_parms (TREE_TYPE (type
));
21682 /* T[integer-constant ]
21684 if (TREE_CODE (type
) == ARRAY_TYPE
)
21685 return (uses_deducible_template_parms (TREE_TYPE (type
))
21686 || deducible_array_bound (TYPE_DOMAIN (type
)));
21698 if (TYPE_PTRMEM_P (type
))
21699 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21700 || (uses_deducible_template_parms
21701 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21703 /* template-name <T> (where template-name refers to a class template)
21704 template-name <i> (where template-name refers to a class template) */
21705 if (CLASS_TYPE_P (type
)
21706 && CLASSTYPE_TEMPLATE_INFO (type
)
21707 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21708 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21709 (CLASSTYPE_TI_ARGS (type
)));
21714 if (FUNC_OR_METHOD_TYPE_P (type
))
21716 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21718 tree parm
= TYPE_ARG_TYPES (type
);
21719 if (TREE_CODE (type
) == METHOD_TYPE
)
21720 parm
= TREE_CHAIN (parm
);
21721 for (; parm
; parm
= TREE_CHAIN (parm
))
21722 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21729 /* Subroutine of type_unification_real and unify_pack_expansion to
21730 handle unification of a single P/A pair. Parameters are as
21731 for those functions. */
21734 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21735 int subr
, unification_kind_t strict
,
21738 tree arg_expr
= NULL_TREE
;
21741 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21742 return unify_invalid (explain_p
);
21743 if (arg
== unknown_type_node
)
21744 /* We can't deduce anything from this, but we might get all the
21745 template args from other function args. */
21746 return unify_success (explain_p
);
21748 /* Implicit conversions (Clause 4) will be performed on a function
21749 argument to convert it to the type of the corresponding function
21750 parameter if the parameter type contains no template-parameters that
21751 participate in template argument deduction. */
21752 if (strict
!= DEDUCE_EXACT
21753 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21754 /* For function parameters with no deducible template parameters,
21755 just return. We'll check non-dependent conversions later. */
21756 return unify_success (explain_p
);
21761 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21762 | UNIFY_ALLOW_MORE_CV_QUAL
21763 | UNIFY_ALLOW_DERIVED
);
21767 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21771 arg_strict
= UNIFY_ALLOW_NONE
;
21775 gcc_unreachable ();
21778 /* We only do these transformations if this is the top-level
21779 parameter_type_list in a call or declaration matching; in other
21780 situations (nested function declarators, template argument lists) we
21781 won't be comparing a type to an expression, and we don't do any type
21787 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21788 if (type_unknown_p (arg
))
21790 /* [temp.deduct.type] A template-argument can be
21791 deduced from a pointer to function or pointer
21792 to member function argument if the set of
21793 overloaded functions does not contain function
21794 templates and at most one of a set of
21795 overloaded functions provides a unique
21797 resolve_overloaded_unification (tparms
, targs
, parm
,
21799 arg_strict
, explain_p
);
21800 /* If a unique match was not found, this is a
21801 non-deduced context, so we still succeed. */
21802 return unify_success (explain_p
);
21806 arg
= unlowered_expr_type (arg
);
21807 if (arg
== error_mark_node
)
21808 return unify_invalid (explain_p
);
21812 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21815 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21816 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21817 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21819 /* For deduction from an init-list we need the actual list. */
21820 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21822 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21825 /* for_each_template_parm callback that always returns 0. */
21828 zero_r (tree
, void *)
21833 /* for_each_template_parm any_fn callback to handle deduction of a template
21834 type argument from the type of an array bound. */
21837 array_deduction_r (tree t
, void *data
)
21839 tree_pair_p d
= (tree_pair_p
)data
;
21840 tree
&tparms
= d
->purpose
;
21841 tree
&targs
= d
->value
;
21843 if (TREE_CODE (t
) == ARRAY_TYPE
)
21844 if (tree dom
= TYPE_DOMAIN (t
))
21845 if (tree max
= TYPE_MAX_VALUE (dom
))
21847 if (TREE_CODE (max
) == MINUS_EXPR
)
21848 max
= TREE_OPERAND (max
, 0);
21849 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21850 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21851 UNIFY_ALLOW_NONE
, /*explain*/false);
21854 /* Keep walking. */
21858 /* Try to deduce any not-yet-deduced template type arguments from the type of
21859 an array bound. This is handled separately from unify because 14.8.2.5 says
21860 "The type of a type parameter is only deduced from an array bound if it is
21861 not otherwise deduced." */
21864 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21866 tree_pair_s data
= { tparms
, targs
};
21867 hash_set
<tree
> visited
;
21868 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21869 /*nondeduced*/false, array_deduction_r
);
21872 /* Most parms like fn_type_unification.
21874 If SUBR is 1, we're being called recursively (to unify the
21875 arguments of a function or method parameter of a function
21878 CHECKS is a pointer to a vector of access checks encountered while
21879 substituting default template arguments. */
21882 type_unification_real (tree tparms
,
21886 unsigned int xnargs
,
21888 unification_kind_t strict
,
21889 vec
<deferred_access_check
, va_gc
> **checks
,
21894 int ntparms
= TREE_VEC_LENGTH (tparms
);
21895 int saw_undeduced
= 0;
21898 unsigned int nargs
;
21901 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21902 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21903 gcc_assert (ntparms
> 0);
21905 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21907 /* Reset the number of non-defaulted template arguments contained
21909 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21917 while (parms
&& parms
!= void_list_node
21920 parm
= TREE_VALUE (parms
);
21922 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21923 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21924 /* For a function parameter pack that occurs at the end of the
21925 parameter-declaration-list, the type A of each remaining
21926 argument of the call is compared with the type P of the
21927 declarator-id of the function parameter pack. */
21930 parms
= TREE_CHAIN (parms
);
21932 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21933 /* For a function parameter pack that does not occur at the
21934 end of the parameter-declaration-list, the type of the
21935 parameter pack is a non-deduced context. */
21941 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21947 && parms
!= void_list_node
21948 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21950 /* Unify the remaining arguments with the pack expansion type. */
21952 tree parmvec
= make_tree_vec (1);
21954 /* Allocate a TREE_VEC and copy in all of the arguments */
21955 argvec
= make_tree_vec (nargs
- ia
);
21956 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21957 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21959 /* Copy the parameter into parmvec. */
21960 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21961 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21962 /*subr=*/subr
, explain_p
))
21965 /* Advance to the end of the list of parameters. */
21966 parms
= TREE_CHAIN (parms
);
21969 /* Fail if we've reached the end of the parm list, and more args
21970 are present, and the parm list isn't variadic. */
21971 if (ia
< nargs
&& parms
== void_list_node
)
21972 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21973 /* Fail if parms are left and they don't have default values and
21974 they aren't all deduced as empty packs (c++/57397). This is
21975 consistent with sufficient_parms_p. */
21976 if (parms
&& parms
!= void_list_node
21977 && TREE_PURPOSE (parms
) == NULL_TREE
)
21979 unsigned int count
= nargs
;
21984 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21987 p
= TREE_CHAIN (p
);
21989 while (p
&& p
!= void_list_node
);
21990 if (count
!= nargs
)
21991 return unify_too_few_arguments (explain_p
, ia
, count
,
21997 tsubst_flags_t complain
= (explain_p
21998 ? tf_warning_or_error
22000 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22002 for (i
= 0; i
< ntparms
; i
++)
22004 tree targ
= TREE_VEC_ELT (targs
, i
);
22005 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22007 /* Clear the "incomplete" flags on all argument packs now so that
22008 substituting them into later default arguments works. */
22009 if (targ
&& ARGUMENT_PACK_P (targ
))
22011 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22012 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22015 if (targ
|| tparm
== error_mark_node
)
22017 tparm
= TREE_VALUE (tparm
);
22019 if (TREE_CODE (tparm
) == TYPE_DECL
22020 && !tried_array_deduction
)
22022 try_array_deduction (tparms
, targs
, xparms
);
22023 tried_array_deduction
= true;
22024 if (TREE_VEC_ELT (targs
, i
))
22028 /* If this is an undeduced nontype parameter that depends on
22029 a type parameter, try another pass; its type may have been
22030 deduced from a later argument than the one from which
22031 this parameter can be deduced. */
22032 if (TREE_CODE (tparm
) == PARM_DECL
22033 && uses_template_parms (TREE_TYPE (tparm
))
22034 && saw_undeduced
< 2)
22040 /* Core issue #226 (C++0x) [temp.deduct]:
22042 If a template argument has not been deduced, its
22043 default template argument, if any, is used.
22045 When we are in C++98 mode, TREE_PURPOSE will either
22046 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22047 to explicitly check cxx_dialect here. */
22048 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22049 /* OK, there is a default argument. Wait until after the
22050 conversion check to do substitution. */
22053 /* If the type parameter is a parameter pack, then it will
22054 be deduced to an empty parameter pack. */
22055 if (template_parameter_pack_p (tparm
))
22059 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22061 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22062 TREE_CONSTANT (arg
) = 1;
22065 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22067 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22069 TREE_VEC_ELT (targs
, i
) = arg
;
22073 return unify_parameter_deduction_failure (explain_p
, tparm
);
22076 /* Now substitute into the default template arguments. */
22077 for (i
= 0; i
< ntparms
; i
++)
22079 tree targ
= TREE_VEC_ELT (targs
, i
);
22080 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22082 if (targ
|| tparm
== error_mark_node
)
22084 tree parm
= TREE_VALUE (tparm
);
22085 tree arg
= TREE_PURPOSE (tparm
);
22086 reopen_deferring_access_checks (*checks
);
22087 location_t save_loc
= input_location
;
22089 input_location
= DECL_SOURCE_LOCATION (parm
);
22091 if (saw_undeduced
== 1
22092 && TREE_CODE (parm
) == PARM_DECL
22093 && uses_template_parms (TREE_TYPE (parm
)))
22095 /* The type of this non-type parameter depends on undeduced
22096 parameters. Don't try to use its default argument yet,
22097 since we might deduce an argument for it on the next pass,
22098 but do check whether the arguments we already have cause
22099 substitution failure, so that that happens before we try
22100 later default arguments (78489). */
22101 ++processing_template_decl
;
22102 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22104 --processing_template_decl
;
22105 if (type
== error_mark_node
)
22106 arg
= error_mark_node
;
22112 /* Even if the call is happening in template context, getting
22113 here means it's non-dependent, and a default argument is
22114 considered a separate definition under [temp.decls], so we can
22115 do this substitution without processing_template_decl. This
22116 is important if the default argument contains something that
22117 might be instantiation-dependent like access (87480). */
22118 processing_template_decl_sentinel s
;
22119 tree substed
= NULL_TREE
;
22120 if (saw_undeduced
== 1)
22122 /* First instatiate in template context, in case we still
22123 depend on undeduced template parameters. */
22124 ++processing_template_decl
;
22125 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22127 --processing_template_decl
;
22128 if (substed
!= error_mark_node
22129 && !uses_template_parms (substed
))
22130 /* We replaced all the tparms, substitute again out of
22131 template context. */
22132 substed
= NULL_TREE
;
22135 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22138 if (!uses_template_parms (substed
))
22139 arg
= convert_template_argument (parm
, substed
, full_targs
,
22140 complain
, i
, NULL_TREE
);
22141 else if (saw_undeduced
== 1)
22144 arg
= error_mark_node
;
22147 input_location
= save_loc
;
22148 *checks
= get_deferred_access_checks ();
22149 pop_deferring_access_checks ();
22151 if (arg
== error_mark_node
)
22155 TREE_VEC_ELT (targs
, i
) = arg
;
22156 /* The position of the first default template argument,
22157 is also the number of non-defaulted arguments in TARGS.
22159 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22160 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22164 if (saw_undeduced
++ == 1)
22168 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22169 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22171 return unify_success (explain_p
);
22174 /* Subroutine of type_unification_real. Args are like the variables
22175 at the call site. ARG is an overloaded function (or template-id);
22176 we try deducing template args from each of the overloads, and if
22177 only one succeeds, we go with that. Modifies TARGS and returns
22178 true on success. */
22181 resolve_overloaded_unification (tree tparms
,
22185 unification_kind_t strict
,
22189 tree tempargs
= copy_node (targs
);
22191 tree goodfn
= NULL_TREE
;
22194 if (TREE_CODE (arg
) == ADDR_EXPR
)
22196 arg
= TREE_OPERAND (arg
, 0);
22202 if (TREE_CODE (arg
) == COMPONENT_REF
)
22203 /* Handle `&x' where `x' is some static or non-static member
22205 arg
= TREE_OPERAND (arg
, 1);
22207 if (TREE_CODE (arg
) == OFFSET_REF
)
22208 arg
= TREE_OPERAND (arg
, 1);
22210 /* Strip baselink information. */
22211 if (BASELINK_P (arg
))
22212 arg
= BASELINK_FUNCTIONS (arg
);
22214 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22216 /* If we got some explicit template args, we need to plug them into
22217 the affected templates before we try to unify, in case the
22218 explicit args will completely resolve the templates in question. */
22221 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22222 arg
= TREE_OPERAND (arg
, 0);
22224 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22227 tree subargs
, elem
;
22229 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22232 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22233 expl_subargs
, NULL_TREE
, tf_none
,
22234 /*require_all_args=*/true,
22235 /*use_default_args=*/true);
22236 if (subargs
!= error_mark_node
22237 && !any_dependent_template_arguments_p (subargs
))
22239 fn
= instantiate_template (fn
, subargs
, tf_none
);
22240 if (!constraints_satisfied_p (fn
))
22242 if (undeduced_auto_decl (fn
))
22244 /* Instantiate the function to deduce its return type. */
22246 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22250 elem
= TREE_TYPE (fn
);
22251 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22252 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22253 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22262 /* If no templates (or more than one) are fully resolved by the
22263 explicit arguments, this template-id is a non-deduced context; it
22264 could still be OK if we deduce all template arguments for the
22265 enclosing call through other arguments. */
22269 else if (!OVL_P (arg
))
22270 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22271 -- but the deduction does not succeed because the expression is
22272 not just the function on its own. */
22275 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22278 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22279 strict
, sub_strict
, addr_p
, explain_p
)
22280 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22287 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22288 to function or pointer to member function argument if the set of
22289 overloaded functions does not contain function templates and at most
22290 one of a set of overloaded functions provides a unique match.
22292 So if we found multiple possibilities, we return success but don't
22293 deduce anything. */
22297 int i
= TREE_VEC_LENGTH (targs
);
22299 if (TREE_VEC_ELT (tempargs
, i
))
22301 tree old
= TREE_VEC_ELT (targs
, i
);
22302 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22303 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22304 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22305 /* Don't forget explicit template arguments in a pack. */
22306 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22307 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22308 TREE_VEC_ELT (targs
, i
) = new_
;
22317 /* Core DR 115: In contexts where deduction is done and fails, or in
22318 contexts where deduction is not done, if a template argument list is
22319 specified and it, along with any default template arguments, identifies
22320 a single function template specialization, then the template-id is an
22321 lvalue for the function template specialization. */
22324 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22326 tree expr
, offset
, baselink
;
22329 if (!type_unknown_p (orig_expr
))
22334 offset
= NULL_TREE
;
22335 baselink
= NULL_TREE
;
22337 if (TREE_CODE (expr
) == ADDR_EXPR
)
22339 expr
= TREE_OPERAND (expr
, 0);
22342 if (TREE_CODE (expr
) == OFFSET_REF
)
22345 expr
= TREE_OPERAND (expr
, 1);
22347 if (BASELINK_P (expr
))
22350 expr
= BASELINK_FUNCTIONS (expr
);
22353 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22356 tree goodfn
= NULL_TREE
;
22358 /* If we got some explicit template args, we need to plug them into
22359 the affected templates before we try to unify, in case the
22360 explicit args will completely resolve the templates in question. */
22362 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22363 tree arg
= TREE_OPERAND (expr
, 0);
22364 tree badfn
= NULL_TREE
;
22365 tree badargs
= NULL_TREE
;
22367 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22370 tree subargs
, elem
;
22372 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22375 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22376 expl_subargs
, NULL_TREE
, tf_none
,
22377 /*require_all_args=*/true,
22378 /*use_default_args=*/true);
22379 if (subargs
!= error_mark_node
22380 && !any_dependent_template_arguments_p (subargs
))
22382 elem
= instantiate_template (fn
, subargs
, tf_none
);
22383 if (elem
== error_mark_node
)
22388 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22389 && constraints_satisfied_p (elem
))
22398 mark_used (goodfn
);
22401 expr
= build_baselink (BASELINK_BINFO (baselink
),
22402 BASELINK_ACCESS_BINFO (baselink
),
22403 expr
, BASELINK_OPTYPE (baselink
));
22407 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22408 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22411 expr
= cp_build_addr_expr (expr
, complain
);
22414 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22415 /* There were no good options and at least one bad one, so let the
22416 user know what the problem is. */
22417 instantiate_template (badfn
, badargs
, complain
);
22422 /* As above, but error out if the expression remains overloaded. */
22425 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22427 exp
= resolve_nondeduced_context (exp
, complain
);
22428 if (type_unknown_p (exp
))
22430 if (complain
& tf_error
)
22431 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22432 return error_mark_node
;
22437 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22438 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22439 different overloads deduce different arguments for a given parm.
22440 ADDR_P is true if the expression for which deduction is being
22441 performed was of the form "& fn" rather than simply "fn".
22443 Returns 1 on success. */
22446 try_one_overload (tree tparms
,
22451 unification_kind_t strict
,
22460 if (arg
== error_mark_node
)
22463 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22464 to function or pointer to member function argument if the set of
22465 overloaded functions does not contain function templates and at most
22466 one of a set of overloaded functions provides a unique match.
22468 So if this is a template, just return success. */
22470 if (uses_template_parms (arg
))
22473 if (TREE_CODE (arg
) == METHOD_TYPE
)
22474 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22476 arg
= build_pointer_type (arg
);
22478 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22480 /* We don't copy orig_targs for this because if we have already deduced
22481 some template args from previous args, unify would complain when we
22482 try to deduce a template parameter for the same argument, even though
22483 there isn't really a conflict. */
22484 nargs
= TREE_VEC_LENGTH (targs
);
22485 tempargs
= make_tree_vec (nargs
);
22487 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22490 /* First make sure we didn't deduce anything that conflicts with
22491 explicitly specified args. */
22492 for (i
= nargs
; i
--; )
22494 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22495 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22499 else if (uses_template_parms (elt
))
22500 /* Since we're unifying against ourselves, we will fill in
22501 template args used in the function parm list with our own
22502 template parms. Discard them. */
22503 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22504 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22506 /* Check that the argument at each index of the deduced argument pack
22507 is equivalent to the corresponding explicitly specified argument.
22508 We may have deduced more arguments than were explicitly specified,
22511 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22512 that's wrong if we deduce the same argument pack from multiple
22513 function arguments: it's only incomplete the first time. */
22515 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22516 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22518 if (TREE_VEC_LENGTH (deduced_pack
)
22519 < TREE_VEC_LENGTH (explicit_pack
))
22522 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22523 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22524 TREE_VEC_ELT (deduced_pack
, j
)))
22527 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22531 for (i
= nargs
; i
--; )
22533 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22536 TREE_VEC_ELT (targs
, i
) = elt
;
22542 /* PARM is a template class (perhaps with unbound template
22543 parameters). ARG is a fully instantiated type. If ARG can be
22544 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22545 TARGS are as for unify. */
22548 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22551 tree copy_of_targs
;
22553 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22555 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22556 /* Matches anything. */;
22557 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22558 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22561 /* We need to make a new template argument vector for the call to
22562 unify. If we used TARGS, we'd clutter it up with the result of
22563 the attempted unification, even if this class didn't work out.
22564 We also don't want to commit ourselves to all the unifications
22565 we've already done, since unification is supposed to be done on
22566 an argument-by-argument basis. In other words, consider the
22567 following pathological case:
22569 template <int I, int J, int K>
22572 template <int I, int J>
22573 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22575 template <int I, int J, int K>
22576 void f(S<I, J, K>, S<I, I, I>);
22585 Now, by the time we consider the unification involving `s2', we
22586 already know that we must have `f<0, 0, 0>'. But, even though
22587 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22588 because there are two ways to unify base classes of S<0, 1, 2>
22589 with S<I, I, I>. If we kept the already deduced knowledge, we
22590 would reject the possibility I=1. */
22591 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22593 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22595 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22600 /* If unification failed, we're done. */
22601 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22602 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22608 /* Given a template type PARM and a class type ARG, find the unique
22609 base type in ARG that is an instance of PARM. We do not examine
22610 ARG itself; only its base-classes. If there is not exactly one
22611 appropriate base class, return NULL_TREE. PARM may be the type of
22612 a partial specialization, as well as a plain template type. Used
22615 static enum template_base_result
22616 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22617 bool explain_p
, tree
*result
)
22619 tree rval
= NULL_TREE
;
22622 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22624 binfo
= TYPE_BINFO (complete_type (arg
));
22627 /* The type could not be completed. */
22628 *result
= NULL_TREE
;
22629 return tbr_incomplete_type
;
22632 /* Walk in inheritance graph order. The search order is not
22633 important, and this avoids multiple walks of virtual bases. */
22634 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22636 tree r
= try_class_unification (tparms
, targs
, parm
,
22637 BINFO_TYPE (binfo
), explain_p
);
22641 /* If there is more than one satisfactory baseclass, then:
22645 If they yield more than one possible deduced A, the type
22649 if (rval
&& !same_type_p (r
, rval
))
22651 *result
= NULL_TREE
;
22652 return tbr_ambiguous_baseclass
;
22660 return tbr_success
;
22663 /* Returns the level of DECL, which declares a template parameter. */
22666 template_decl_level (tree decl
)
22668 switch (TREE_CODE (decl
))
22671 case TEMPLATE_DECL
:
22672 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22675 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22678 gcc_unreachable ();
22683 /* Decide whether ARG can be unified with PARM, considering only the
22684 cv-qualifiers of each type, given STRICT as documented for unify.
22685 Returns nonzero iff the unification is OK on that basis. */
22688 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22690 int arg_quals
= cp_type_quals (arg
);
22691 int parm_quals
= cp_type_quals (parm
);
22693 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22694 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22696 /* Although a CVR qualifier is ignored when being applied to a
22697 substituted template parameter ([8.3.2]/1 for example), that
22698 does not allow us to unify "const T" with "int&" because both
22699 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22700 It is ok when we're allowing additional CV qualifiers
22701 at the outer level [14.8.2.1]/3,1st bullet. */
22702 if ((TYPE_REF_P (arg
)
22703 || FUNC_OR_METHOD_TYPE_P (arg
))
22704 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22707 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22708 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22712 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22713 && (arg_quals
& parm_quals
) != parm_quals
)
22716 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22717 && (parm_quals
& arg_quals
) != arg_quals
)
22723 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22725 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22727 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22728 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22729 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22731 *index
= TEMPLATE_TYPE_IDX (parm
);
22732 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22736 *index
= TEMPLATE_PARM_IDX (parm
);
22737 *level
= TEMPLATE_PARM_LEVEL (parm
);
22741 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22743 if (unify (TP, TA, P, A, S, EP)) \
22747 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22748 expansion at the end of PACKED_PARMS. Returns 0 if the type
22749 deduction succeeds, 1 otherwise. STRICT is the same as in
22750 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22751 function call argument list. We'll need to adjust the arguments to make them
22752 types. SUBR tells us if this is from a recursive call to
22753 type_unification_real, or for comparing two template argument
22757 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22758 tree packed_args
, unification_kind_t strict
,
22759 bool subr
, bool explain_p
)
22762 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22763 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22764 tree pack
, packs
= NULL_TREE
;
22765 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22767 /* Add in any args remembered from an earlier partial instantiation. */
22768 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22769 int levels
= TMPL_ARGS_DEPTH (targs
);
22771 packed_args
= expand_template_argument_pack (packed_args
);
22773 int len
= TREE_VEC_LENGTH (packed_args
);
22775 /* Determine the parameter packs we will be deducing from the
22776 pattern, and record their current deductions. */
22777 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22778 pack
; pack
= TREE_CHAIN (pack
))
22780 tree parm_pack
= TREE_VALUE (pack
);
22783 /* Only template parameter packs can be deduced, not e.g. function
22784 parameter packs or __bases or __integer_pack. */
22785 if (!TEMPLATE_PARM_P (parm_pack
))
22788 /* Determine the index and level of this parameter pack. */
22789 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22790 if (level
< levels
)
22793 /* Keep track of the parameter packs and their corresponding
22795 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22796 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22799 /* Loop through all of the arguments that have not yet been
22800 unified and unify each with the pattern. */
22801 for (i
= start
; i
< len
; i
++)
22804 bool any_explicit
= false;
22805 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22807 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22808 or the element of its argument pack at the current index if
22809 this argument was explicitly specified. */
22810 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22814 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22817 if (TREE_VALUE (pack
)
22818 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22819 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22821 any_explicit
= true;
22822 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22824 TMPL_ARG (targs
, level
, idx
) = arg
;
22827 /* If we had explicit template arguments, substitute them into the
22828 pattern before deduction. */
22831 /* Some arguments might still be unspecified or dependent. */
22833 ++processing_template_decl
;
22834 dependent
= any_dependent_template_arguments_p (targs
);
22836 --processing_template_decl
;
22837 parm
= tsubst (pattern
, targs
,
22838 explain_p
? tf_warning_or_error
: tf_none
,
22841 --processing_template_decl
;
22842 if (parm
== error_mark_node
)
22848 /* Unify the pattern with the current argument. */
22849 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22853 /* For each parameter pack, collect the deduced value. */
22854 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22857 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22859 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22860 TMPL_ARG (targs
, level
, idx
);
22864 /* Verify that the results of unification with the parameter packs
22865 produce results consistent with what we've seen before, and make
22866 the deduced argument packs available. */
22867 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22869 tree old_pack
= TREE_VALUE (pack
);
22870 tree new_args
= TREE_TYPE (pack
);
22871 int i
, len
= TREE_VEC_LENGTH (new_args
);
22873 bool nondeduced_p
= false;
22875 /* By default keep the original deduced argument pack.
22876 If necessary, more specific code is going to update the
22877 resulting deduced argument later down in this function. */
22878 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22879 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22881 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22882 actually deduce anything. */
22883 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22884 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22885 nondeduced_p
= true;
22889 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22891 /* If we had fewer function args than explicit template args,
22892 just use the explicits. */
22893 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22894 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22895 if (len
< explicit_len
)
22896 new_args
= explicit_args
;
22902 /* Build the deduced *_ARGUMENT_PACK. */
22903 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22905 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22906 TREE_CONSTANT (result
) = 1;
22909 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22911 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22913 /* Note the deduced argument packs for this parameter
22915 TMPL_ARG (targs
, level
, idx
) = result
;
22917 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22918 && (ARGUMENT_PACK_ARGS (old_pack
)
22919 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22921 /* We only had the explicitly-provided arguments before, but
22922 now we have a complete set of arguments. */
22923 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22925 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22926 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22927 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22931 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22932 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22933 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22934 /* During template argument deduction for the aggregate deduction
22935 candidate, the number of elements in a trailing parameter pack
22936 is only deduced from the number of remaining function
22937 arguments if it is not otherwise deduced. */
22938 if (cxx_dialect
>= cxx20
22939 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
22940 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
22941 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
22942 if (!comp_template_args (old_args
, new_args
,
22943 &bad_old_arg
, &bad_new_arg
))
22944 /* Inconsistent unification of this parameter pack. */
22945 return unify_parameter_pack_inconsistent (explain_p
,
22951 return unify_success (explain_p
);
22954 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22955 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22956 parameters and return value are as for unify. */
22959 unify_array_domain (tree tparms
, tree targs
,
22960 tree parm_dom
, tree arg_dom
,
22968 /* Our representation of array types uses "N - 1" as the
22969 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22970 not an integer constant. We cannot unify arbitrarily
22971 complex expressions, so we eliminate the MINUS_EXPRs
22973 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22974 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22977 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22978 parm_max
= TREE_OPERAND (parm_max
, 0);
22980 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22981 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22984 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22985 trying to unify the type of a variable with the type
22986 of a template parameter. For example:
22988 template <unsigned int N>
22989 void f (char (&) [N]);
22996 Here, the type of the ARG will be "int [g(i)]", and
22997 may be a SAVE_EXPR, etc. */
22998 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22999 return unify_vla_arg (explain_p
, arg_dom
);
23000 arg_max
= TREE_OPERAND (arg_max
, 0);
23003 /* If only one of the bounds used a MINUS_EXPR, compensate
23004 by adding one to the other bound. */
23005 if (parm_cst
&& !arg_cst
)
23006 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23010 else if (arg_cst
&& !parm_cst
)
23011 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23016 return unify (tparms
, targs
, parm_max
, arg_max
,
23017 UNIFY_ALLOW_INTEGER
, explain_p
);
23020 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23022 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23027 if (PACK_EXPANSION_P (t
))
23028 t
= PACK_EXPANSION_PATTERN (t
);
23029 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23030 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23031 || DECL_TYPE_TEMPLATE_P (t
))
23033 else if (TYPE_P (t
))
23039 /* Deduce the value of template parameters. TPARMS is the (innermost)
23040 set of template parameters to a template. TARGS is the bindings
23041 for those template parameters, as determined thus far; TARGS may
23042 include template arguments for outer levels of template parameters
23043 as well. PARM is a parameter to a template function, or a
23044 subcomponent of that parameter; ARG is the corresponding argument.
23045 This function attempts to match PARM with ARG in a manner
23046 consistent with the existing assignments in TARGS. If more values
23047 are deduced, then TARGS is updated.
23049 Returns 0 if the type deduction succeeds, 1 otherwise. The
23050 parameter STRICT is a bitwise or of the following flags:
23053 Require an exact match between PARM and ARG.
23054 UNIFY_ALLOW_MORE_CV_QUAL:
23055 Allow the deduced ARG to be more cv-qualified (by qualification
23056 conversion) than ARG.
23057 UNIFY_ALLOW_LESS_CV_QUAL:
23058 Allow the deduced ARG to be less cv-qualified than ARG.
23059 UNIFY_ALLOW_DERIVED:
23060 Allow the deduced ARG to be a template base class of ARG,
23061 or a pointer to a template base class of the type pointed to by
23063 UNIFY_ALLOW_INTEGER:
23064 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23065 case for more information.
23066 UNIFY_ALLOW_OUTER_LEVEL:
23067 This is the outermost level of a deduction. Used to determine validity
23068 of qualification conversions. A valid qualification conversion must
23069 have const qualified pointers leading up to the inner type which
23070 requires additional CV quals, except at the outer level, where const
23071 is not required [conv.qual]. It would be normal to set this flag in
23072 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23073 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23074 This is the outermost level of a deduction, and PARM can be more CV
23075 qualified at this point.
23076 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23077 This is the outermost level of a deduction, and PARM can be less CV
23078 qualified at this point. */
23081 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23087 int strict_in
= strict
;
23088 tsubst_flags_t complain
= (explain_p
23089 ? tf_warning_or_error
23092 /* I don't think this will do the right thing with respect to types.
23093 But the only case I've seen it in so far has been array bounds, where
23094 signedness is the only information lost, and I think that will be
23095 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23096 finish_id_expression_1, and are also OK. */
23097 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23098 parm
= TREE_OPERAND (parm
, 0);
23100 if (arg
== error_mark_node
)
23101 return unify_invalid (explain_p
);
23102 if (arg
== unknown_type_node
23103 || arg
== init_list_type_node
)
23104 /* We can't deduce anything from this, but we might get all the
23105 template args from other function args. */
23106 return unify_success (explain_p
);
23108 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23109 return unify_success (explain_p
);
23111 /* If PARM uses template parameters, then we can't bail out here,
23112 even if ARG == PARM, since we won't record unifications for the
23113 template parameters. We might need them if we're trying to
23114 figure out which of two things is more specialized. */
23115 if (arg
== parm
&& !uses_template_parms (parm
))
23116 return unify_success (explain_p
);
23118 /* Handle init lists early, so the rest of the function can assume
23119 we're dealing with a type. */
23120 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23124 tree orig_parm
= parm
;
23126 if (!is_std_init_list (parm
)
23127 && TREE_CODE (parm
) != ARRAY_TYPE
)
23128 /* We can only deduce from an initializer list argument if the
23129 parameter is std::initializer_list or an array; otherwise this
23130 is a non-deduced context. */
23131 return unify_success (explain_p
);
23133 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23134 elttype
= TREE_TYPE (parm
);
23137 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23138 /* Deduction is defined in terms of a single type, so just punt
23139 on the (bizarre) std::initializer_list<T...>. */
23140 if (PACK_EXPANSION_P (elttype
))
23141 return unify_success (explain_p
);
23144 if (strict
!= DEDUCE_EXACT
23145 && TYPE_P (elttype
)
23146 && !uses_deducible_template_parms (elttype
))
23147 /* If ELTTYPE has no deducible template parms, skip deduction from
23148 the list elements. */;
23150 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23152 int elt_strict
= strict
;
23154 if (elt
== error_mark_node
)
23155 return unify_invalid (explain_p
);
23157 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23159 tree type
= TREE_TYPE (elt
);
23160 if (type
== error_mark_node
)
23161 return unify_invalid (explain_p
);
23162 /* It should only be possible to get here for a call. */
23163 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23164 elt_strict
|= maybe_adjust_types_for_deduction
23165 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23169 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23173 if (TREE_CODE (parm
) == ARRAY_TYPE
23174 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23176 /* Also deduce from the length of the initializer list. */
23177 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23178 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23179 if (idx
== error_mark_node
)
23180 return unify_invalid (explain_p
);
23181 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23185 /* If the std::initializer_list<T> deduction worked, replace the
23186 deduced A with std::initializer_list<A>. */
23187 if (orig_parm
!= parm
)
23189 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23190 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23191 targ
= listify (targ
);
23192 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23194 return unify_success (explain_p
);
23197 /* If parm and arg aren't the same kind of thing (template, type, or
23198 expression), fail early. */
23199 if (pa_kind (parm
) != pa_kind (arg
))
23200 return unify_invalid (explain_p
);
23202 /* Immediately reject some pairs that won't unify because of
23203 cv-qualification mismatches. */
23204 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23206 /* It is the elements of the array which hold the cv quals of an array
23207 type, and the elements might be template type parms. We'll check
23208 when we recurse. */
23209 && TREE_CODE (arg
) != ARRAY_TYPE
23210 /* We check the cv-qualifiers when unifying with template type
23211 parameters below. We want to allow ARG `const T' to unify with
23212 PARM `T' for example, when computing which of two templates
23213 is more specialized, for example. */
23214 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23215 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23216 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23218 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23219 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23220 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23221 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23222 strict
&= ~UNIFY_ALLOW_DERIVED
;
23223 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23224 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23226 switch (TREE_CODE (parm
))
23228 case TYPENAME_TYPE
:
23230 case UNBOUND_CLASS_TEMPLATE
:
23231 /* In a type which contains a nested-name-specifier, template
23232 argument values cannot be deduced for template parameters used
23233 within the nested-name-specifier. */
23234 return unify_success (explain_p
);
23236 case TEMPLATE_TYPE_PARM
:
23237 case TEMPLATE_TEMPLATE_PARM
:
23238 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23239 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23240 if (error_operand_p (tparm
))
23241 return unify_invalid (explain_p
);
23243 if (TEMPLATE_TYPE_LEVEL (parm
)
23244 != template_decl_level (tparm
))
23245 /* The PARM is not one we're trying to unify. Just check
23246 to see if it matches ARG. */
23248 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23249 && (is_auto (parm
) ? is_auto (arg
)
23250 : same_type_p (parm
, arg
)))
23251 return unify_success (explain_p
);
23253 return unify_type_mismatch (explain_p
, parm
, arg
);
23255 idx
= TEMPLATE_TYPE_IDX (parm
);
23256 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23257 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23258 if (error_operand_p (tparm
))
23259 return unify_invalid (explain_p
);
23261 /* Check for mixed types and values. */
23262 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23263 && TREE_CODE (tparm
) != TYPE_DECL
)
23264 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23265 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23266 gcc_unreachable ();
23268 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23270 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23271 && CLASS_TYPE_P (arg
))
23273 /* First try to match ARG directly. */
23274 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23278 /* Otherwise, look for a suitable base of ARG, as below. */
23279 enum template_base_result r
;
23280 r
= get_template_base (tparms
, targs
, parm
, arg
,
23283 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23287 /* ARG must be constructed from a template class or a template
23288 template parameter. */
23289 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23290 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23291 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23293 /* Deduce arguments T, i from TT<T> or TT<i>. */
23294 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23297 arg
= TYPE_TI_TEMPLATE (arg
);
23299 /* Fall through to deduce template name. */
23302 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23303 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23305 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23307 /* Simple cases: Value already set, does match or doesn't. */
23308 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23309 return unify_success (explain_p
);
23311 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23315 /* If PARM is `const T' and ARG is only `int', we don't have
23316 a match unless we are allowing additional qualification.
23317 If ARG is `const int' and PARM is just `T' that's OK;
23318 that binds `const int' to `T'. */
23319 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23321 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23323 /* Consider the case where ARG is `const volatile int' and
23324 PARM is `const T'. Then, T should be `volatile int'. */
23325 arg
= cp_build_qualified_type_real
23326 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23327 if (arg
== error_mark_node
)
23328 return unify_invalid (explain_p
);
23330 /* Simple cases: Value already set, does match or doesn't. */
23331 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23332 return unify_success (explain_p
);
23334 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23336 /* Make sure that ARG is not a variable-sized array. (Note
23337 that were talking about variable-sized arrays (like
23338 `int[n]'), rather than arrays of unknown size (like
23339 `int[]').) We'll get very confused by such a type since
23340 the bound of the array is not constant, and therefore
23341 not mangleable. Besides, such types are not allowed in
23342 ISO C++, so we can do as we please here. We do allow
23343 them for 'auto' deduction, since that isn't ABI-exposed. */
23344 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23345 return unify_vla_arg (explain_p
, arg
);
23347 /* Strip typedefs as in convert_template_argument. */
23348 arg
= canonicalize_type_argument (arg
, tf_none
);
23351 /* If ARG is a parameter pack or an expansion, we cannot unify
23352 against it unless PARM is also a parameter pack. */
23353 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23354 && !template_parameter_pack_p (parm
))
23355 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23357 /* If the argument deduction results is a METHOD_TYPE,
23358 then there is a problem.
23359 METHOD_TYPE doesn't map to any real C++ type the result of
23360 the deduction cannot be of that type. */
23361 if (TREE_CODE (arg
) == METHOD_TYPE
)
23362 return unify_method_type_error (explain_p
, arg
);
23364 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23365 return unify_success (explain_p
);
23367 case TEMPLATE_PARM_INDEX
:
23368 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23369 if (error_operand_p (tparm
))
23370 return unify_invalid (explain_p
);
23372 if (TEMPLATE_PARM_LEVEL (parm
)
23373 != template_decl_level (tparm
))
23375 /* The PARM is not one we're trying to unify. Just check
23376 to see if it matches ARG. */
23377 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23378 && cp_tree_equal (parm
, arg
));
23380 unify_expression_unequal (explain_p
, parm
, arg
);
23384 idx
= TEMPLATE_PARM_IDX (parm
);
23385 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23389 if ((strict
& UNIFY_ALLOW_INTEGER
)
23390 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23391 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23392 /* We're deducing from an array bound, the type doesn't matter. */
23393 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23394 int x
= !cp_tree_equal (targ
, arg
);
23396 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23400 /* [temp.deduct.type] If, in the declaration of a function template
23401 with a non-type template-parameter, the non-type
23402 template-parameter is used in an expression in the function
23403 parameter-list and, if the corresponding template-argument is
23404 deduced, the template-argument type shall match the type of the
23405 template-parameter exactly, except that a template-argument
23406 deduced from an array bound may be of any integral type.
23407 The non-type parameter might use already deduced type parameters. */
23408 tparm
= TREE_TYPE (parm
);
23409 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23410 /* We don't have enough levels of args to do any substitution. This
23411 can happen in the context of -fnew-ttp-matching. */;
23414 ++processing_template_decl
;
23415 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23416 --processing_template_decl
;
23418 if (tree a
= type_uses_auto (tparm
))
23420 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23421 if (tparm
== error_mark_node
)
23426 if (!TREE_TYPE (arg
))
23427 /* Template-parameter dependent expression. Just accept it for now.
23428 It will later be processed in convert_template_argument. */
23430 else if (same_type_ignoring_top_level_qualifiers_p
23431 (non_reference (TREE_TYPE (arg
)),
23432 non_reference (tparm
)))
23433 /* OK. Ignore top-level quals here because a class-type template
23434 parameter object is const. */;
23435 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23436 && CP_INTEGRAL_TYPE_P (tparm
))
23437 /* Convert the ARG to the type of PARM; the deduced non-type
23438 template argument must exactly match the types of the
23439 corresponding parameter. */
23440 arg
= fold (build_nop (tparm
, arg
));
23441 else if (uses_template_parms (tparm
))
23443 /* We haven't deduced the type of this parameter yet. */
23444 if (cxx_dialect
>= cxx17
23445 /* We deduce from array bounds in try_array_deduction. */
23446 && !(strict
& UNIFY_ALLOW_INTEGER
))
23448 /* Deduce it from the non-type argument. */
23449 tree atype
= TREE_TYPE (arg
);
23450 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23452 UNIFY_ALLOW_NONE
, explain_p
);
23455 /* Try again later. */
23456 return unify_success (explain_p
);
23459 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23461 /* If ARG is a parameter pack or an expansion, we cannot unify
23462 against it unless PARM is also a parameter pack. */
23463 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23464 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23465 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23468 bool removed_attr
= false;
23469 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23471 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23472 return unify_success (explain_p
);
23476 /* A pointer-to-member constant can be unified only with
23477 another constant. */
23478 if (TREE_CODE (arg
) != PTRMEM_CST
)
23479 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23481 /* Just unify the class member. It would be useless (and possibly
23482 wrong, depending on the strict flags) to unify also
23483 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23484 arg refer to the same variable, even if through different
23485 classes. For instance:
23487 struct A { int x; };
23490 Unification of &A::x and &B::x must succeed. */
23491 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23492 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23497 if (!TYPE_PTR_P (arg
))
23498 return unify_type_mismatch (explain_p
, parm
, arg
);
23500 /* [temp.deduct.call]
23502 A can be another pointer or pointer to member type that can
23503 be converted to the deduced A via a qualification
23504 conversion (_conv.qual_).
23506 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23507 This will allow for additional cv-qualification of the
23508 pointed-to types if appropriate. */
23510 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23511 /* The derived-to-base conversion only persists through one
23512 level of pointers. */
23513 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23515 return unify (tparms
, targs
, TREE_TYPE (parm
),
23516 TREE_TYPE (arg
), strict
, explain_p
);
23519 case REFERENCE_TYPE
:
23520 if (!TYPE_REF_P (arg
))
23521 return unify_type_mismatch (explain_p
, parm
, arg
);
23522 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23523 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23526 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23527 return unify_type_mismatch (explain_p
, parm
, arg
);
23528 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23529 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23530 return unify_type_mismatch (explain_p
, parm
, arg
);
23531 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23532 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23533 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23534 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23535 TYPE_DOMAIN (arg
), explain_p
);
23536 return unify_success (explain_p
);
23543 case ENUMERAL_TYPE
:
23546 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23547 return unify_type_mismatch (explain_p
, parm
, arg
);
23549 /* We have already checked cv-qualification at the top of the
23551 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23552 return unify_type_mismatch (explain_p
, parm
, arg
);
23554 /* As far as unification is concerned, this wins. Later checks
23555 will invalidate it if necessary. */
23556 return unify_success (explain_p
);
23558 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23559 /* Type INTEGER_CST can come from ordinary constant template args. */
23561 while (CONVERT_EXPR_P (arg
))
23562 arg
= TREE_OPERAND (arg
, 0);
23564 if (TREE_CODE (arg
) != INTEGER_CST
)
23565 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23566 return (tree_int_cst_equal (parm
, arg
)
23567 ? unify_success (explain_p
)
23568 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23572 int i
, len
, argslen
;
23573 int parm_variadic_p
= 0;
23575 if (TREE_CODE (arg
) != TREE_VEC
)
23576 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23578 len
= TREE_VEC_LENGTH (parm
);
23579 argslen
= TREE_VEC_LENGTH (arg
);
23581 /* Check for pack expansions in the parameters. */
23582 for (i
= 0; i
< len
; ++i
)
23584 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23587 /* We can unify against something with a trailing
23589 parm_variadic_p
= 1;
23591 /* [temp.deduct.type]/9: If the template argument list of
23592 P contains a pack expansion that is not the last
23593 template argument, the entire template argument list
23594 is a non-deduced context. */
23595 return unify_success (explain_p
);
23599 /* If we don't have enough arguments to satisfy the parameters
23600 (not counting the pack expression at the end), or we have
23601 too many arguments for a parameter list that doesn't end in
23602 a pack expression, we can't unify. */
23603 if (parm_variadic_p
23604 ? argslen
< len
- parm_variadic_p
23606 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23608 /* Unify all of the parameters that precede the (optional)
23609 pack expression. */
23610 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23612 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23613 TREE_VEC_ELT (parm
, i
),
23614 TREE_VEC_ELT (arg
, i
),
23615 UNIFY_ALLOW_NONE
, explain_p
);
23617 if (parm_variadic_p
)
23618 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23620 /*subr=*/true, explain_p
);
23621 return unify_success (explain_p
);
23626 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23627 return unify_type_mismatch (explain_p
, parm
, arg
);
23629 if (TYPE_PTRMEMFUNC_P (parm
))
23631 if (!TYPE_PTRMEMFUNC_P (arg
))
23632 return unify_type_mismatch (explain_p
, parm
, arg
);
23634 return unify (tparms
, targs
,
23635 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23636 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23637 strict
, explain_p
);
23639 else if (TYPE_PTRMEMFUNC_P (arg
))
23640 return unify_type_mismatch (explain_p
, parm
, arg
);
23642 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23644 tree t
= NULL_TREE
;
23646 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23648 /* First, we try to unify the PARM and ARG directly. */
23649 t
= try_class_unification (tparms
, targs
,
23650 parm
, arg
, explain_p
);
23654 /* Fallback to the special case allowed in
23655 [temp.deduct.call]:
23657 If P is a class, and P has the form
23658 template-id, then A can be a derived class of
23659 the deduced A. Likewise, if P is a pointer to
23660 a class of the form template-id, A can be a
23661 pointer to a derived class pointed to by the
23663 enum template_base_result r
;
23664 r
= get_template_base (tparms
, targs
, parm
, arg
,
23669 /* Don't give the derived diagnostic if we're
23670 already dealing with the same template. */
23672 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23673 && (CLASSTYPE_TI_TEMPLATE (parm
)
23674 == CLASSTYPE_TI_TEMPLATE (arg
)));
23675 return unify_no_common_base (explain_p
&& !same_template
,
23680 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23681 && (CLASSTYPE_TI_TEMPLATE (parm
)
23682 == CLASSTYPE_TI_TEMPLATE (arg
)))
23683 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23684 Then, we should unify `int' and `U'. */
23687 /* There's no chance of unification succeeding. */
23688 return unify_type_mismatch (explain_p
, parm
, arg
);
23690 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23691 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23693 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23694 return unify_type_mismatch (explain_p
, parm
, arg
);
23695 return unify_success (explain_p
);
23698 case FUNCTION_TYPE
:
23700 unsigned int nargs
;
23705 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23706 return unify_type_mismatch (explain_p
, parm
, arg
);
23708 /* CV qualifications for methods can never be deduced, they must
23709 match exactly. We need to check them explicitly here,
23710 because type_unification_real treats them as any other
23711 cv-qualified parameter. */
23712 if (TREE_CODE (parm
) == METHOD_TYPE
23713 && (!check_cv_quals_for_unify
23715 class_of_this_parm (arg
),
23716 class_of_this_parm (parm
))))
23717 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23718 if (TREE_CODE (arg
) == FUNCTION_TYPE
23719 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23720 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23721 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23722 return unify_type_mismatch (explain_p
, parm
, arg
);
23724 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23725 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23727 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23728 args
= XALLOCAVEC (tree
, nargs
);
23729 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23730 a
!= NULL_TREE
&& a
!= void_list_node
;
23731 a
= TREE_CHAIN (a
), ++i
)
23732 args
[i
] = TREE_VALUE (a
);
23735 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23736 args
, nargs
, 1, DEDUCE_EXACT
,
23740 if (flag_noexcept_type
)
23742 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23743 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23744 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23745 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23746 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23747 && uses_template_parms (TREE_PURPOSE (pspec
)))
23748 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23749 TREE_PURPOSE (aspec
),
23750 UNIFY_ALLOW_NONE
, explain_p
);
23751 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23752 return unify_type_mismatch (explain_p
, parm
, arg
);
23759 /* Unify a pointer to member with a pointer to member function, which
23760 deduces the type of the member as a function type. */
23761 if (TYPE_PTRMEMFUNC_P (arg
))
23763 /* Check top-level cv qualifiers */
23764 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23765 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23767 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23768 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23769 UNIFY_ALLOW_NONE
, explain_p
);
23771 /* Determine the type of the function we are unifying against. */
23772 tree fntype
= static_fn_type (arg
);
23774 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23777 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23778 return unify_type_mismatch (explain_p
, parm
, arg
);
23779 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23780 TYPE_OFFSET_BASETYPE (arg
),
23781 UNIFY_ALLOW_NONE
, explain_p
);
23782 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23783 strict
, explain_p
);
23786 if (DECL_TEMPLATE_PARM_P (parm
))
23787 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23788 if (arg
!= scalar_constant_value (parm
))
23789 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23790 return unify_success (explain_p
);
23793 case TEMPLATE_DECL
:
23794 /* Matched cases are handled by the ARG == PARM test above. */
23795 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23798 /* We might get a variable as a non-type template argument in parm if the
23799 corresponding parameter is type-dependent. Make any necessary
23800 adjustments based on whether arg is a reference. */
23801 if (CONSTANT_CLASS_P (arg
))
23802 parm
= fold_non_dependent_expr (parm
, complain
);
23803 else if (REFERENCE_REF_P (arg
))
23805 tree sub
= TREE_OPERAND (arg
, 0);
23807 if (TREE_CODE (sub
) == ADDR_EXPR
)
23808 arg
= TREE_OPERAND (sub
, 0);
23810 /* Now use the normal expression code to check whether they match. */
23813 case TYPE_ARGUMENT_PACK
:
23814 case NONTYPE_ARGUMENT_PACK
:
23815 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23816 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23819 case DECLTYPE_TYPE
:
23820 case UNDERLYING_TYPE
:
23821 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23822 or UNDERLYING_TYPE nodes. */
23823 return unify_success (explain_p
);
23826 /* Unification fails if we hit an error node. */
23827 return unify_invalid (explain_p
);
23830 if (REFERENCE_REF_P (parm
))
23832 bool pexp
= PACK_EXPANSION_P (arg
);
23834 arg
= PACK_EXPANSION_PATTERN (arg
);
23835 if (REFERENCE_REF_P (arg
))
23836 arg
= TREE_OPERAND (arg
, 0);
23838 arg
= make_pack_expansion (arg
, complain
);
23839 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23840 strict
, explain_p
);
23845 /* An unresolved overload is a nondeduced context. */
23846 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23847 return unify_success (explain_p
);
23848 gcc_assert (EXPR_P (parm
)
23849 || COMPOUND_LITERAL_P (parm
)
23850 || TREE_CODE (parm
) == TRAIT_EXPR
);
23852 /* We must be looking at an expression. This can happen with
23856 void foo(S<I>, S<I + 2>);
23860 template<typename T>
23861 void foo(A<T, T{}>);
23863 This is a "non-deduced context":
23867 The non-deduced contexts are:
23869 --A non-type template argument or an array bound in which
23870 a subexpression references a template parameter.
23872 In these cases, we assume deduction succeeded, but don't
23873 actually infer any unifications. */
23875 if (!uses_template_parms (parm
)
23876 && !template_args_equal (parm
, arg
))
23877 return unify_expression_unequal (explain_p
, parm
, arg
);
23879 return unify_success (explain_p
);
23882 #undef RECUR_AND_CHECK_FAILURE
23884 /* Note that DECL can be defined in this translation unit, if
23888 mark_definable (tree decl
)
23891 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23892 FOR_EACH_CLONE (clone
, decl
)
23893 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23896 /* Called if RESULT is explicitly instantiated, or is a member of an
23897 explicitly instantiated class. */
23900 mark_decl_instantiated (tree result
, int extern_p
)
23902 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23904 /* If this entity has already been written out, it's too late to
23905 make any modifications. */
23906 if (TREE_ASM_WRITTEN (result
))
23909 /* For anonymous namespace we don't need to do anything. */
23910 if (decl_anon_ns_mem_p (result
))
23912 gcc_assert (!TREE_PUBLIC (result
));
23916 if (TREE_CODE (result
) != FUNCTION_DECL
)
23917 /* The TREE_PUBLIC flag for function declarations will have been
23918 set correctly by tsubst. */
23919 TREE_PUBLIC (result
) = 1;
23921 /* This might have been set by an earlier implicit instantiation. */
23922 DECL_COMDAT (result
) = 0;
23925 DECL_NOT_REALLY_EXTERN (result
) = 0;
23928 mark_definable (result
);
23929 mark_needed (result
);
23930 /* Always make artificials weak. */
23931 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23932 comdat_linkage (result
);
23933 /* For WIN32 we also want to put explicit instantiations in
23934 linkonce sections. */
23935 else if (TREE_PUBLIC (result
))
23936 maybe_make_one_only (result
);
23937 if (TREE_CODE (result
) == FUNCTION_DECL
23938 && DECL_TEMPLATE_INSTANTIATED (result
))
23939 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23940 since start_preparsed_function wouldn't have if we had an earlier
23941 extern explicit instantiation. */
23942 DECL_EXTERNAL (result
) = 0;
23945 /* If EXTERN_P, then this function will not be emitted -- unless
23946 followed by an explicit instantiation, at which point its linkage
23947 will be adjusted. If !EXTERN_P, then this function will be
23948 emitted here. In neither circumstance do we want
23949 import_export_decl to adjust the linkage. */
23950 DECL_INTERFACE_KNOWN (result
) = 1;
23953 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23954 important template arguments. If any are missing, we check whether
23955 they're important by using error_mark_node for substituting into any
23956 args that were used for partial ordering (the ones between ARGS and END)
23957 and seeing if it bubbles up. */
23960 check_undeduced_parms (tree targs
, tree args
, tree end
)
23962 bool found
= false;
23964 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23965 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23968 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23972 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23973 if (substed
== error_mark_node
)
23979 /* Given two function templates PAT1 and PAT2, return:
23981 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23982 -1 if PAT2 is more specialized than PAT1.
23983 0 if neither is more specialized.
23985 LEN indicates the number of parameters we should consider
23986 (defaulted parameters should not be considered).
23988 The 1998 std underspecified function template partial ordering, and
23989 DR214 addresses the issue. We take pairs of arguments, one from
23990 each of the templates, and deduce them against each other. One of
23991 the templates will be more specialized if all the *other*
23992 template's arguments deduce against its arguments and at least one
23993 of its arguments *does* *not* deduce against the other template's
23994 corresponding argument. Deduction is done as for class templates.
23995 The arguments used in deduction have reference and top level cv
23996 qualifiers removed. Iff both arguments were originally reference
23997 types *and* deduction succeeds in both directions, an lvalue reference
23998 wins against an rvalue reference and otherwise the template
23999 with the more cv-qualified argument wins for that pairing (if
24000 neither is more cv-qualified, they both are equal). Unlike regular
24001 deduction, after all the arguments have been deduced in this way,
24002 we do *not* verify the deduced template argument values can be
24003 substituted into non-deduced contexts.
24005 The logic can be a bit confusing here, because we look at deduce1 and
24006 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24007 can find template arguments for pat1 to make arg1 look like arg2, that
24008 means that arg2 is at least as specialized as arg1. */
24011 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24013 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24014 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24015 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24016 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24017 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24018 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24019 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24020 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24021 tree origs1
, origs2
;
24022 bool lose1
= false;
24023 bool lose2
= false;
24025 /* Remove the this parameter from non-static member functions. If
24026 one is a non-static member function and the other is not a static
24027 member function, remove the first parameter from that function
24028 also. This situation occurs for operator functions where we
24029 locate both a member function (with this pointer) and non-member
24030 operator (with explicit first operand). */
24031 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24033 len
--; /* LEN is the number of significant arguments for DECL1 */
24034 args1
= TREE_CHAIN (args1
);
24035 if (!DECL_STATIC_FUNCTION_P (decl2
))
24036 args2
= TREE_CHAIN (args2
);
24038 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24040 args2
= TREE_CHAIN (args2
);
24041 if (!DECL_STATIC_FUNCTION_P (decl1
))
24044 args1
= TREE_CHAIN (args1
);
24048 /* If only one is a conversion operator, they are unordered. */
24049 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24052 /* Consider the return type for a conversion function */
24053 if (DECL_CONV_FN_P (decl1
))
24055 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24056 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24060 processing_template_decl
++;
24066 /* Stop when an ellipsis is seen. */
24067 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24069 tree arg1
= TREE_VALUE (args1
);
24070 tree arg2
= TREE_VALUE (args2
);
24071 int deduce1
, deduce2
;
24077 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24078 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24080 /* When both arguments are pack expansions, we need only
24081 unify the patterns themselves. */
24082 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24083 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24085 /* This is the last comparison we need to do. */
24089 if (TYPE_REF_P (arg1
))
24091 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24092 arg1
= TREE_TYPE (arg1
);
24093 quals1
= cp_type_quals (arg1
);
24096 if (TYPE_REF_P (arg2
))
24098 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24099 arg2
= TREE_TYPE (arg2
);
24100 quals2
= cp_type_quals (arg2
);
24103 arg1
= TYPE_MAIN_VARIANT (arg1
);
24104 arg2
= TYPE_MAIN_VARIANT (arg2
);
24106 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24108 int i
, len2
= remaining_arguments (args2
);
24109 tree parmvec
= make_tree_vec (1);
24110 tree argvec
= make_tree_vec (len2
);
24113 /* Setup the parameter vector, which contains only ARG1. */
24114 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24116 /* Setup the argument vector, which contains the remaining
24118 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24119 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24121 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24122 argvec
, DEDUCE_EXACT
,
24123 /*subr=*/true, /*explain_p=*/false)
24126 /* We cannot deduce in the other direction, because ARG1 is
24127 a pack expansion but ARG2 is not. */
24130 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24132 int i
, len1
= remaining_arguments (args1
);
24133 tree parmvec
= make_tree_vec (1);
24134 tree argvec
= make_tree_vec (len1
);
24137 /* Setup the parameter vector, which contains only ARG1. */
24138 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24140 /* Setup the argument vector, which contains the remaining
24142 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24143 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24145 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24146 argvec
, DEDUCE_EXACT
,
24147 /*subr=*/true, /*explain_p=*/false)
24150 /* We cannot deduce in the other direction, because ARG2 is
24151 a pack expansion but ARG1 is not.*/
24157 /* The normal case, where neither argument is a pack
24159 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24160 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24162 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24163 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24167 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24168 arg2, then arg2 is not as specialized as arg1. */
24174 /* "If, for a given type, deduction succeeds in both directions
24175 (i.e., the types are identical after the transformations above)
24176 and both P and A were reference types (before being replaced with
24177 the type referred to above):
24178 - if the type from the argument template was an lvalue reference and
24179 the type from the parameter template was not, the argument type is
24180 considered to be more specialized than the other; otherwise,
24181 - if the type from the argument template is more cv-qualified
24182 than the type from the parameter template (as described above),
24183 the argument type is considered to be more specialized than the other;
24185 - neither type is more specialized than the other." */
24187 if (deduce1
&& deduce2
)
24189 if (ref1
&& ref2
&& ref1
!= ref2
)
24196 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24198 if ((quals1
& quals2
) == quals2
)
24200 if ((quals1
& quals2
) == quals1
)
24205 if (lose1
&& lose2
)
24206 /* We've failed to deduce something in either direction.
24207 These must be unordered. */
24210 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24211 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24212 /* We have already processed all of the arguments in our
24213 handing of the pack expansion type. */
24216 args1
= TREE_CHAIN (args1
);
24217 args2
= TREE_CHAIN (args2
);
24220 /* "In most cases, all template parameters must have values in order for
24221 deduction to succeed, but for partial ordering purposes a template
24222 parameter may remain without a value provided it is not used in the
24223 types being used for partial ordering."
24225 Thus, if we are missing any of the targs1 we need to substitute into
24226 origs1, then pat2 is not as specialized as pat1. This can happen when
24227 there is a nondeduced context. */
24228 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24230 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24233 processing_template_decl
--;
24235 /* If both deductions succeed, the partial ordering selects the more
24236 constrained template. */
24237 /* P2113: If the corresponding template-parameters of the
24238 template-parameter-lists are not equivalent ([temp.over.link]) or if
24239 the function parameters that positionally correspond between the two
24240 templates are not of the same type, neither template is more
24241 specialized than the other. */
24242 if (!lose1
&& !lose2
24243 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24244 DECL_TEMPLATE_PARMS (pat2
))
24245 && compparms (origs1
, origs2
))
24247 int winner
= more_constrained (decl1
, decl2
);
24250 else if (winner
< 0)
24254 /* All things being equal, if the next argument is a pack expansion
24255 for one function but not for the other, prefer the
24256 non-variadic function. FIXME this is bogus; see c++/41958. */
24258 && args1
&& TREE_VALUE (args1
)
24259 && args2
&& TREE_VALUE (args2
))
24261 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24262 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24265 if (lose1
== lose2
)
24273 /* Determine which of two partial specializations of TMPL is more
24276 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24277 to the first partial specialization. The TREE_PURPOSE is the
24278 innermost set of template parameters for the partial
24279 specialization. PAT2 is similar, but for the second template.
24281 Return 1 if the first partial specialization is more specialized;
24282 -1 if the second is more specialized; 0 if neither is more
24285 See [temp.class.order] for information about determining which of
24286 two templates is more specialized. */
24289 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24293 bool any_deductions
= false;
24295 tree tmpl1
= TREE_VALUE (pat1
);
24296 tree tmpl2
= TREE_VALUE (pat2
);
24297 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24298 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24300 /* Just like what happens for functions, if we are ordering between
24301 different template specializations, we may encounter dependent
24302 types in the arguments, and we need our dependency check functions
24303 to behave correctly. */
24304 ++processing_template_decl
;
24305 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24309 any_deductions
= true;
24312 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24316 any_deductions
= true;
24318 --processing_template_decl
;
24320 /* If both deductions succeed, the partial ordering selects the more
24321 constrained template. */
24322 if (!winner
&& any_deductions
)
24323 winner
= more_constrained (tmpl1
, tmpl2
);
24325 /* In the case of a tie where at least one of the templates
24326 has a parameter pack at the end, the template with the most
24327 non-packed parameters wins. */
24330 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24331 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24333 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24334 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24335 int len1
= TREE_VEC_LENGTH (args1
);
24336 int len2
= TREE_VEC_LENGTH (args2
);
24338 /* We don't count the pack expansion at the end. */
24339 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24341 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24346 else if (len1
< len2
)
24353 /* Return the template arguments that will produce the function signature
24354 DECL from the function template FN, with the explicit template
24355 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24356 also match. Return NULL_TREE if no satisfactory arguments could be
24360 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24362 int ntparms
= DECL_NTPARMS (fn
);
24363 tree targs
= make_tree_vec (ntparms
);
24364 tree decl_type
= TREE_TYPE (decl
);
24365 tree decl_arg_types
;
24367 unsigned int nargs
, ix
;
24370 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24372 /* Never do unification on the 'this' parameter. */
24373 decl_arg_types
= skip_artificial_parms_for (decl
,
24374 TYPE_ARG_TYPES (decl_type
));
24376 nargs
= list_length (decl_arg_types
);
24377 args
= XALLOCAVEC (tree
, nargs
);
24378 for (arg
= decl_arg_types
, ix
= 0;
24379 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24380 arg
= TREE_CHAIN (arg
), ++ix
)
24381 args
[ix
] = TREE_VALUE (arg
);
24383 if (fn_type_unification (fn
, explicit_args
, targs
,
24385 (check_rettype
|| DECL_CONV_FN_P (fn
)
24386 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24387 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24388 /*explain_p=*/false,
24390 == error_mark_node
)
24396 /* Return the innermost template arguments that, when applied to a partial
24397 specialization SPEC_TMPL of TMPL, yield the ARGS.
24399 For example, suppose we have:
24401 template <class T, class U> struct S {};
24402 template <class T> struct S<T*, int> {};
24404 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24405 partial specialization and the ARGS will be {double*, int}. The resulting
24406 vector will be {double}, indicating that `T' is bound to `double'. */
24409 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24411 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24413 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24414 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24416 tree innermost_deduced_args
;
24418 innermost_deduced_args
= make_tree_vec (ntparms
);
24419 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24421 deduced_args
= copy_node (args
);
24422 SET_TMPL_ARGS_LEVEL (deduced_args
,
24423 TMPL_ARGS_DEPTH (deduced_args
),
24424 innermost_deduced_args
);
24427 deduced_args
= innermost_deduced_args
;
24429 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24431 if (unify (tparms
, deduced_args
,
24432 INNERMOST_TEMPLATE_ARGS (spec_args
),
24433 INNERMOST_TEMPLATE_ARGS (args
),
24434 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24437 for (i
= 0; i
< ntparms
; ++i
)
24438 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24440 if (!tried_array_deduction
)
24442 try_array_deduction (tparms
, innermost_deduced_args
,
24443 INNERMOST_TEMPLATE_ARGS (spec_args
));
24444 tried_array_deduction
= true;
24445 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24451 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24453 excessive_deduction_depth
= true;
24457 /* Verify that nondeduced template arguments agree with the type
24458 obtained from argument deduction.
24462 struct A { typedef int X; };
24463 template <class T, class U> struct C {};
24464 template <class T> struct C<T, typename T::X> {};
24466 Then with the instantiation `C<A, int>', we can deduce that
24467 `T' is `A' but unify () does not check whether `typename T::X'
24469 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24471 if (spec_args
!= error_mark_node
)
24472 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24473 INNERMOST_TEMPLATE_ARGS (spec_args
),
24474 tmpl
, tf_none
, false, false);
24476 pop_tinst_level ();
24478 if (spec_args
== error_mark_node
24479 /* We only need to check the innermost arguments; the other
24480 arguments will always agree. */
24481 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24482 INNERMOST_TEMPLATE_ARGS (args
)))
24485 /* Now that we have bindings for all of the template arguments,
24486 ensure that the arguments deduced for the template template
24487 parameters have compatible template parameter lists. See the use
24488 of template_template_parm_bindings_ok_p in fn_type_unification
24489 for more information. */
24490 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24493 return deduced_args
;
24496 // Compare two function templates T1 and T2 by deducing bindings
24497 // from one against the other. If both deductions succeed, compare
24498 // constraints to see which is more constrained.
24500 more_specialized_inst (tree t1
, tree t2
)
24505 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24511 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24517 // If both deductions succeed, then one may be more constrained.
24518 if (count
== 2 && fate
== 0)
24519 fate
= more_constrained (t1
, t2
);
24524 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24525 Return the TREE_LIST node with the most specialized template, if
24526 any. If there is no most specialized template, the error_mark_node
24529 Note that this function does not look at, or modify, the
24530 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24531 returned is one of the elements of INSTANTIATIONS, callers may
24532 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24533 and retrieve it from the value returned. */
24536 most_specialized_instantiation (tree templates
)
24540 ++processing_template_decl
;
24543 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24545 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24546 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24551 /* Equally specialized, move to next function. If there
24552 is no next function, nothing's most specialized. */
24553 fn
= TREE_CHAIN (fn
);
24561 /* Now verify that champ is better than everything earlier in the
24562 instantiation list. */
24563 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24564 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24571 processing_template_decl
--;
24574 return error_mark_node
;
24579 /* If DECL is a specialization of some template, return the most
24580 general such template. Otherwise, returns NULL_TREE.
24582 For example, given:
24584 template <class T> struct S { template <class U> void f(U); };
24586 if TMPL is `template <class U> void S<int>::f(U)' this will return
24587 the full template. This function will not trace past partial
24588 specializations, however. For example, given in addition:
24590 template <class T> struct S<T*> { template <class U> void f(U); };
24592 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24593 `template <class T> template <class U> S<T*>::f(U)'. */
24596 most_general_template (tree decl
)
24598 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24600 if (tree tinfo
= get_template_info (decl
))
24601 decl
= TI_TEMPLATE (tinfo
);
24602 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24603 template friend, or a FIELD_DECL for a capture pack. */
24604 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24608 /* Look for more and more general templates. */
24609 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24611 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24612 (See cp-tree.h for details.) */
24613 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24616 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24617 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24618 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24621 /* Stop if we run into an explicitly specialized class template. */
24622 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24623 && DECL_CONTEXT (decl
)
24624 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24627 decl
= DECL_TI_TEMPLATE (decl
);
24633 /* Return the most specialized of the template partial specializations
24634 which can produce TARGET, a specialization of some class or variable
24635 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24636 a TEMPLATE_DECL node corresponding to the partial specialization, while
24637 the TREE_PURPOSE is the set of template arguments that must be
24638 substituted into the template pattern in order to generate TARGET.
24640 If the choice of partial specialization is ambiguous, a diagnostic
24641 is issued, and the error_mark_node is returned. If there are no
24642 partial specializations matching TARGET, then NULL_TREE is
24643 returned, indicating that the primary template should be used. */
24646 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24648 tree list
= NULL_TREE
;
24653 tree outer_args
= NULL_TREE
;
24656 if (TYPE_P (target
))
24658 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24659 tmpl
= TI_TEMPLATE (tinfo
);
24660 args
= TI_ARGS (tinfo
);
24662 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24664 tmpl
= TREE_OPERAND (target
, 0);
24665 args
= TREE_OPERAND (target
, 1);
24667 else if (VAR_P (target
))
24669 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24670 tmpl
= TI_TEMPLATE (tinfo
);
24671 args
= TI_ARGS (tinfo
);
24674 gcc_unreachable ();
24676 tree main_tmpl
= most_general_template (tmpl
);
24678 /* For determining which partial specialization to use, only the
24679 innermost args are interesting. */
24680 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24682 outer_args
= strip_innermost_template_args (args
, 1);
24683 args
= INNERMOST_TEMPLATE_ARGS (args
);
24686 /* The caller hasn't called push_to_top_level yet, but we need
24687 get_partial_spec_bindings to be done in non-template context so that we'll
24688 fully resolve everything. */
24689 processing_template_decl_sentinel ptds
;
24691 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24693 const tree ospec_tmpl
= TREE_VALUE (t
);
24698 /* Substitute in the template args from the enclosing class. */
24699 ++processing_template_decl
;
24700 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24701 --processing_template_decl
;
24702 if (spec_tmpl
== error_mark_node
)
24703 return error_mark_node
;
24706 spec_tmpl
= ospec_tmpl
;
24708 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24712 spec_args
= add_to_template_args (outer_args
, spec_args
);
24714 /* Keep the candidate only if the constraints are satisfied,
24715 or if we're not compiling with concepts. */
24717 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24719 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24720 TREE_TYPE (list
) = TREE_TYPE (t
);
24728 ambiguous_p
= false;
24731 t
= TREE_CHAIN (t
);
24732 for (; t
; t
= TREE_CHAIN (t
))
24734 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24741 t
= TREE_CHAIN (t
);
24744 ambiguous_p
= true;
24753 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24755 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24758 ambiguous_p
= true;
24766 char *spaces
= NULL
;
24767 if (!(complain
& tf_error
))
24768 return error_mark_node
;
24769 if (TYPE_P (target
))
24770 error ("ambiguous template instantiation for %q#T", target
);
24772 error ("ambiguous template instantiation for %q#D", target
);
24773 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24774 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24776 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24777 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24778 "%s %#qS", spaces
? spaces
: str
, subst
);
24779 spaces
= spaces
? spaces
: get_spaces (str
);
24782 return error_mark_node
;
24788 /* Explicitly instantiate DECL. */
24791 do_decl_instantiation (tree decl
, tree storage
)
24793 tree result
= NULL_TREE
;
24796 if (!decl
|| decl
== error_mark_node
)
24797 /* An error occurred, for which grokdeclarator has already issued
24798 an appropriate message. */
24800 else if (! DECL_LANG_SPECIFIC (decl
))
24802 error ("explicit instantiation of non-template %q#D", decl
);
24805 else if (DECL_DECLARED_CONCEPT_P (decl
))
24808 error ("explicit instantiation of variable concept %q#D", decl
);
24810 error ("explicit instantiation of function concept %q#D", decl
);
24814 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24815 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24817 if (VAR_P (decl
) && !var_templ
)
24819 /* There is an asymmetry here in the way VAR_DECLs and
24820 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24821 the latter, the DECL we get back will be marked as a
24822 template instantiation, and the appropriate
24823 DECL_TEMPLATE_INFO will be set up. This does not happen for
24824 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24825 should handle VAR_DECLs as it currently handles
24827 if (!DECL_CLASS_SCOPE_P (decl
))
24829 error ("%qD is not a static data member of a class template", decl
);
24832 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24833 if (!result
|| !VAR_P (result
))
24835 error ("no matching template for %qD found", decl
);
24838 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24840 error ("type %qT for explicit instantiation %qD does not match "
24841 "declared type %qT", TREE_TYPE (result
), decl
,
24846 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24848 error ("explicit instantiation of %q#D", decl
);
24854 /* Check for various error cases. Note that if the explicit
24855 instantiation is valid the RESULT will currently be marked as an
24856 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24857 until we get here. */
24859 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24861 /* DR 259 [temp.spec].
24863 Both an explicit instantiation and a declaration of an explicit
24864 specialization shall not appear in a program unless the explicit
24865 instantiation follows a declaration of the explicit specialization.
24867 For a given set of template parameters, if an explicit
24868 instantiation of a template appears after a declaration of an
24869 explicit specialization for that template, the explicit
24870 instantiation has no effect. */
24873 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24877 No program shall explicitly instantiate any template more
24880 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24881 the first instantiation was `extern' and the second is not,
24882 and EXTERN_P for the opposite case. */
24883 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24884 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24885 /* If an "extern" explicit instantiation follows an ordinary
24886 explicit instantiation, the template is instantiated. */
24890 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24892 error ("no matching template for %qD found", result
);
24895 else if (!DECL_TEMPLATE_INFO (result
))
24897 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24901 if (storage
== NULL_TREE
)
24903 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24905 if (cxx_dialect
== cxx98
)
24906 pedwarn (input_location
, OPT_Wpedantic
,
24907 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24912 error ("storage class %qD applied to template instantiation", storage
);
24914 check_explicit_instantiation_namespace (result
);
24915 mark_decl_instantiated (result
, extern_p
);
24917 instantiate_decl (result
, /*defer_ok=*/true,
24918 /*expl_inst_class_mem_p=*/false);
24922 mark_class_instantiated (tree t
, int extern_p
)
24924 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24925 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24926 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24927 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24930 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24931 rest_of_type_compilation (t
, 1);
24935 /* Called from do_type_instantiation through binding_table_foreach to
24936 do recursive instantiation for the type bound in ENTRY. */
24938 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24940 tree storage
= *(tree
*) data
;
24942 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24943 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24944 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24945 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24948 /* Perform an explicit instantiation of template class T. STORAGE, if
24949 non-null, is the RID for extern, inline or static. COMPLAIN is
24950 nonzero if this is called from the parser, zero if called recursively,
24951 since the standard is unclear (as detailed below). */
24954 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24959 int previous_instantiation_extern_p
= 0;
24961 if (TREE_CODE (t
) == TYPE_DECL
)
24964 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24967 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24969 error ("explicit instantiation of non-class template %qD", tmpl
);
24971 error ("explicit instantiation of non-template type %qT", t
);
24977 if (!COMPLETE_TYPE_P (t
))
24979 if (complain
& tf_error
)
24980 error ("explicit instantiation of %q#T before definition of template",
24985 if (storage
!= NULL_TREE
)
24987 if (storage
== ridpointers
[(int) RID_EXTERN
])
24989 if (cxx_dialect
== cxx98
)
24990 pedwarn (input_location
, OPT_Wpedantic
,
24991 "ISO C++ 1998 forbids the use of %<extern%> on "
24992 "explicit instantiations");
24995 pedwarn (input_location
, OPT_Wpedantic
,
24996 "ISO C++ forbids the use of %qE"
24997 " on explicit instantiations", storage
);
24999 if (storage
== ridpointers
[(int) RID_INLINE
])
25001 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25003 else if (storage
== ridpointers
[(int) RID_STATIC
])
25007 error ("storage class %qD applied to template instantiation",
25013 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25015 /* DR 259 [temp.spec].
25017 Both an explicit instantiation and a declaration of an explicit
25018 specialization shall not appear in a program unless the explicit
25019 instantiation follows a declaration of the explicit specialization.
25021 For a given set of template parameters, if an explicit
25022 instantiation of a template appears after a declaration of an
25023 explicit specialization for that template, the explicit
25024 instantiation has no effect. */
25027 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
25031 No program shall explicitly instantiate any template more
25034 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25035 instantiation was `extern'. If EXTERN_P then the second is.
25036 These cases are OK. */
25037 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
25039 if (!previous_instantiation_extern_p
&& !extern_p
25040 && (complain
& tf_error
))
25041 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
25043 /* If we've already instantiated the template, just return now. */
25044 if (!CLASSTYPE_INTERFACE_ONLY (t
))
25048 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25049 mark_class_instantiated (t
, extern_p
);
25054 /* In contrast to implicit instantiation, where only the
25055 declarations, and not the definitions, of members are
25056 instantiated, we have here:
25060 An explicit instantiation that names a class template
25061 specialization is also an explicit instantiation of the same
25062 kind (declaration or definition) of each of its members (not
25063 including members inherited from base classes and members
25064 that are templates) that has not been previously explicitly
25065 specialized in the translation unit containing the explicit
25066 instantiation, provided that the associated constraints, if
25067 any, of that member are satisfied by the template arguments
25068 of the explicit instantiation. */
25069 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25071 || (TREE_CODE (fld
) == FUNCTION_DECL
25073 && user_provided_p (fld
)))
25074 && DECL_TEMPLATE_INSTANTIATION (fld
)
25075 && constraints_satisfied_p (fld
))
25077 mark_decl_instantiated (fld
, extern_p
);
25079 instantiate_decl (fld
, /*defer_ok=*/true,
25080 /*expl_inst_class_mem_p=*/true);
25083 if (CLASSTYPE_NESTED_UTDS (t
))
25084 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
25085 bt_instantiate_type_proc
, &storage
);
25088 /* Given a function DECL, which is a specialization of TMPL, modify
25089 DECL to be a re-instantiation of TMPL with the same template
25090 arguments. TMPL should be the template into which tsubst'ing
25091 should occur for DECL, not the most general template.
25093 One reason for doing this is a scenario like this:
25096 void f(const T&, int i);
25098 void g() { f(3, 7); }
25101 void f(const T& t, const int i) { }
25103 Note that when the template is first instantiated, with
25104 instantiate_template, the resulting DECL will have no name for the
25105 first parameter, and the wrong type for the second. So, when we go
25106 to instantiate the DECL, we regenerate it. */
25109 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25111 /* The arguments used to instantiate DECL, from the most general
25115 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25117 /* Make sure that we can see identifiers, and compute access
25119 push_access_scope (decl
);
25121 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25129 args_depth
= TMPL_ARGS_DEPTH (args
);
25130 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25131 if (args_depth
> parms_depth
)
25132 args
= get_innermost_template_args (args
, parms_depth
);
25134 /* Instantiate a dynamic exception-specification. noexcept will be
25136 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25137 if (TREE_VALUE (raises
))
25139 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25140 args
, tf_error
, NULL_TREE
,
25141 /*defer_ok*/false);
25142 if (specs
&& specs
!= error_mark_node
)
25143 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25147 /* Merge parameter declarations. */
25148 decl_parm
= skip_artificial_parms_for (decl
,
25149 DECL_ARGUMENTS (decl
));
25151 = skip_artificial_parms_for (code_pattern
,
25152 DECL_ARGUMENTS (code_pattern
));
25153 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25158 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25159 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25160 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25162 parm_type
= type_decays_to (parm_type
);
25163 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25164 TREE_TYPE (decl_parm
) = parm_type
;
25165 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25166 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25168 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25169 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25171 decl_parm
= DECL_CHAIN (decl_parm
);
25172 pattern_parm
= DECL_CHAIN (pattern_parm
);
25174 /* Merge any parameters that match with the function parameter
25176 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25179 tree expanded_types
;
25180 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25181 the parameters in this function parameter pack. */
25182 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25183 args
, tf_error
, NULL_TREE
);
25184 len
= TREE_VEC_LENGTH (expanded_types
);
25185 for (i
= 0; i
< len
; i
++)
25190 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25191 /* Rename the parameter to include the index. */
25192 DECL_NAME (decl_parm
) =
25193 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25194 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
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
);
25207 /* Merge additional specifiers from the CODE_PATTERN. */
25208 if (DECL_DECLARED_INLINE_P (code_pattern
)
25209 && !DECL_DECLARED_INLINE_P (decl
))
25210 DECL_DECLARED_INLINE_P (decl
) = 1;
25212 maybe_instantiate_noexcept (decl
, tf_error
);
25214 else if (VAR_P (decl
))
25216 start_lambda_scope (decl
);
25217 DECL_INITIAL (decl
) =
25218 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25219 tf_error
, DECL_TI_TEMPLATE (decl
));
25220 finish_lambda_scope ();
25221 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25222 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25223 tf_error
, DECL_TI_TEMPLATE (decl
));
25226 gcc_unreachable ();
25228 pop_access_scope (decl
);
25231 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25232 substituted to get DECL. */
25235 template_for_substitution (tree decl
)
25237 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25239 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25240 for the instantiation. This is not always the most general
25241 template. Consider, for example:
25244 struct S { template <class U> void f();
25245 template <> void f<int>(); };
25247 and an instantiation of S<double>::f<int>. We want TD to be the
25248 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25249 while (/* An instantiation cannot have a definition, so we need a
25250 more general template. */
25251 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25252 /* We must also deal with friend templates. Given:
25254 template <class T> struct S {
25255 template <class U> friend void f() {};
25258 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25259 so far as the language is concerned, but that's still
25260 where we get the pattern for the instantiation from. On
25261 other hand, if the definition comes outside the class, say:
25263 template <class T> struct S {
25264 template <class U> friend void f();
25266 template <class U> friend void f() {}
25268 we don't need to look any further. That's what the check for
25269 DECL_INITIAL is for. */
25270 || (TREE_CODE (decl
) == FUNCTION_DECL
25271 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25272 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25274 /* The present template, TD, should not be a definition. If it
25275 were a definition, we should be using it! Note that we
25276 cannot restructure the loop to just keep going until we find
25277 a template with a definition, since that might go too far if
25278 a specialization was declared, but not defined. */
25280 /* Fetch the more general template. */
25281 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25287 /* Returns true if we need to instantiate this template instance even if we
25288 know we aren't going to emit it. */
25291 always_instantiate_p (tree decl
)
25293 /* We always instantiate inline functions so that we can inline them. An
25294 explicit instantiation declaration prohibits implicit instantiation of
25295 non-inline functions. With high levels of optimization, we would
25296 normally inline non-inline functions -- but we're not allowed to do
25297 that for "extern template" functions. Therefore, we check
25298 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25299 return ((TREE_CODE (decl
) == FUNCTION_DECL
25300 && (DECL_DECLARED_INLINE_P (decl
)
25301 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25302 /* And we need to instantiate static data members so that
25303 their initializers are available in integral constant
25306 && decl_maybe_constant_var_p (decl
)));
25309 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25310 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25311 error, true otherwise. */
25314 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25316 tree fntype
, spec
, noex
;
25318 /* Don't instantiate a noexcept-specification from template context. */
25319 if (processing_template_decl
25320 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25323 if (DECL_MAYBE_DELETED (fn
))
25325 if (fn
== current_function_decl
)
25326 /* We're in start_preparsed_function, keep going. */
25330 synthesize_method (fn
);
25332 return !DECL_MAYBE_DELETED (fn
);
25335 fntype
= TREE_TYPE (fn
);
25336 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25338 if (!spec
|| !TREE_PURPOSE (spec
))
25341 noex
= TREE_PURPOSE (spec
);
25342 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25343 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25346 tree orig_fn
= NULL_TREE
;
25347 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25348 its FUNCTION_DECL for the rest of this function -- push_access_scope
25349 doesn't accept TEMPLATE_DECLs. */
25350 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25353 fn
= DECL_TEMPLATE_RESULT (fn
);
25356 if (DECL_CLONED_FUNCTION_P (fn
))
25358 tree prime
= DECL_CLONED_FUNCTION (fn
);
25359 if (!maybe_instantiate_noexcept (prime
, complain
))
25361 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25363 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25365 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25366 bool added
= false;
25367 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25369 spec
= get_defaulted_eh_spec (fn
, complain
);
25370 if (spec
== error_mark_node
)
25371 /* This might have failed because of an unparsed DMI, so
25372 let's try again later. */
25375 else if (!(added
= !fns
->add (fn
)))
25377 /* If hash_set::add returns true, the element was already there. */
25378 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25379 DECL_SOURCE_LOCATION (fn
));
25381 "exception specification of %qD depends on itself",
25383 spec
= noexcept_false_spec
;
25385 else if (push_tinst_level (fn
))
25387 push_to_top_level ();
25388 push_access_scope (fn
);
25389 push_deferring_access_checks (dk_no_deferred
);
25390 input_location
= DECL_SOURCE_LOCATION (fn
);
25392 /* If needed, set current_class_ptr for the benefit of
25393 tsubst_copy/PARM_DECL. */
25394 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25395 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25397 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25398 current_class_ptr
= NULL_TREE
;
25399 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25400 current_class_ptr
= this_parm
;
25403 /* If this function is represented by a TEMPLATE_DECL, then
25404 the deferred noexcept-specification might still contain
25405 dependent types, even after substitution. And we need the
25406 dependency check functions to work in build_noexcept_spec. */
25408 ++processing_template_decl
;
25410 /* Do deferred instantiation of the noexcept-specifier. */
25411 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25412 DEFERRED_NOEXCEPT_ARGS (noex
),
25413 tf_warning_or_error
, fn
,
25414 /*function_p=*/false,
25417 /* Build up the noexcept-specification. */
25418 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25421 --processing_template_decl
;
25423 pop_deferring_access_checks ();
25424 pop_access_scope (fn
);
25425 pop_tinst_level ();
25426 pop_from_top_level ();
25429 spec
= noexcept_false_spec
;
25435 if (spec
== error_mark_node
)
25437 /* This failed with a hard error, so let's go with false. */
25438 gcc_assert (seen_error ());
25439 spec
= noexcept_false_spec
;
25442 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25444 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25449 /* We're starting to process the function INST, an instantiation of PATTERN;
25450 add their parameters to local_specializations. */
25453 register_parameter_specializations (tree pattern
, tree inst
)
25455 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25456 tree spec_parm
= DECL_ARGUMENTS (inst
);
25457 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25459 register_local_specialization (spec_parm
, tmpl_parm
);
25460 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25461 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25463 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25465 if (!DECL_PACK_P (tmpl_parm
)
25466 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25468 register_local_specialization (spec_parm
, tmpl_parm
);
25469 spec_parm
= DECL_CHAIN (spec_parm
);
25473 /* Register the (value) argument pack as a specialization of
25474 TMPL_PARM, then move on. */
25475 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25476 register_local_specialization (argpack
, tmpl_parm
);
25479 gcc_assert (!spec_parm
);
25482 /* Instantiate the body of D using PATTERN with ARGS. We have
25483 already determined PATTERN is the correct template to use.
25484 NESTED_P is true if this is a nested function, in which case
25485 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25488 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25490 tree td
= NULL_TREE
;
25491 tree code_pattern
= pattern
;
25496 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25499 /* Only OMP reductions are nested. */
25500 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25502 vec
<tree
> omp_privatization_save
;
25503 if (current_function_decl
)
25504 save_omp_privatization_clauses (omp_privatization_save
);
25507 = !(current_function_decl
25508 && !LAMBDA_FUNCTION_P (d
)
25509 && decl_function_context (d
) == current_function_decl
);
25512 push_to_top_level ();
25515 gcc_assert (!processing_template_decl
);
25516 push_function_context ();
25517 cp_unevaluated_operand
= 0;
25518 c_inhibit_evaluation_warnings
= 0;
25523 /* The variable might be a lambda's extra scope, and that
25524 lambda's visibility depends on D's. */
25525 maybe_commonize_var (d
);
25526 determine_visibility (d
);
25529 /* Mark D as instantiated so that recursive calls to
25530 instantiate_decl do not try to instantiate it again. */
25531 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25534 /* Regenerate the declaration in case the template has been modified
25535 by a subsequent redeclaration. */
25536 regenerate_decl_from_template (d
, td
, args
);
25538 /* We already set the file and line above. Reset them now in case
25539 they changed as a result of calling regenerate_decl_from_template. */
25540 input_location
= DECL_SOURCE_LOCATION (d
);
25545 bool const_init
= false;
25547 /* Clear out DECL_RTL; whatever was there before may not be right
25548 since we've reset the type of the declaration. */
25549 SET_DECL_RTL (d
, NULL
);
25550 DECL_IN_AGGR_P (d
) = 0;
25552 /* The initializer is placed in DECL_INITIAL by
25553 regenerate_decl_from_template so we don't need to
25554 push/pop_access_scope again here. Pull it out so that
25555 cp_finish_decl can process it. */
25556 init
= DECL_INITIAL (d
);
25557 DECL_INITIAL (d
) = NULL_TREE
;
25558 DECL_INITIALIZED_P (d
) = 0;
25560 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25561 initializer. That function will defer actual emission until
25562 we have a chance to determine linkage. */
25563 DECL_EXTERNAL (d
) = 0;
25565 /* Enter the scope of D so that access-checking works correctly. */
25566 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25568 push_nested_class (DECL_CONTEXT (d
));
25570 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25571 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25574 pop_nested_class ();
25576 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25577 synthesize_method (d
);
25578 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25580 /* Set up the list of local specializations. */
25581 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25582 tree block
= NULL_TREE
;
25584 /* Set up context. */
25586 block
= push_stmt_list ();
25588 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25590 perform_instantiation_time_access_checks (code_pattern
, args
);
25592 /* Create substitution entries for the parameters. */
25593 register_parameter_specializations (code_pattern
, d
);
25595 /* Substitute into the body of the function. */
25596 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25597 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25598 tf_warning_or_error
, d
);
25601 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25602 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25603 /*integral_constant_expression_p=*/false);
25605 /* Set the current input_location to the end of the function
25606 so that finish_function knows where we are. */
25608 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25610 /* Remember if we saw an infinite loop in the template. */
25611 current_function_infinite_loop
25612 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25615 /* Finish the function. */
25617 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25620 d
= finish_function (/*inline_p=*/false);
25621 expand_or_defer_fn (d
);
25624 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25625 cp_check_omp_declare_reduction (d
);
25628 /* We're not deferring instantiation any more. */
25629 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25632 pop_from_top_level ();
25634 pop_function_context ();
25636 if (current_function_decl
)
25637 restore_omp_privatization_clauses (omp_privatization_save
);
25640 /* Produce the definition of D, a _DECL generated from a template. If
25641 DEFER_OK is true, then we don't have to actually do the
25642 instantiation now; we just have to do it sometime. Normally it is
25643 an error if this is an explicit instantiation but D is undefined.
25644 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25645 instantiated class template. */
25648 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25650 tree tmpl
= DECL_TI_TEMPLATE (d
);
25657 bool pattern_defined
;
25658 location_t saved_loc
= input_location
;
25659 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25660 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25664 /* This function should only be used to instantiate templates for
25665 functions and static member variables. */
25666 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25668 /* A concept is never instantiated. */
25669 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25671 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
25673 /* Variables are never deferred; if instantiation is required, they
25674 are instantiated right away. That allows for better code in the
25675 case that an expression refers to the value of the variable --
25676 if the variable has a constant value the referring expression can
25677 take advantage of that fact. */
25681 /* Don't instantiate cloned functions. Instead, instantiate the
25682 functions they cloned. */
25683 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25684 d
= DECL_CLONED_FUNCTION (d
);
25686 if (DECL_TEMPLATE_INSTANTIATED (d
)
25687 || TREE_TYPE (d
) == error_mark_node
25688 || (TREE_CODE (d
) == FUNCTION_DECL
25689 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25690 || DECL_TEMPLATE_SPECIALIZATION (d
))
25691 /* D has already been instantiated or explicitly specialized, so
25692 there's nothing for us to do here.
25694 It might seem reasonable to check whether or not D is an explicit
25695 instantiation, and, if so, stop here. But when an explicit
25696 instantiation is deferred until the end of the compilation,
25697 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25698 the instantiation. */
25701 /* Check to see whether we know that this template will be
25702 instantiated in some other file, as with "extern template"
25704 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25706 /* In general, we do not instantiate such templates. */
25707 if (external_p
&& !always_instantiate_p (d
))
25710 gen_tmpl
= most_general_template (tmpl
);
25711 gen_args
= DECL_TI_ARGS (d
);
25713 /* We should already have the extra args. */
25714 gcc_checking_assert (tmpl
== gen_tmpl
25715 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25716 == TMPL_ARGS_DEPTH (gen_args
)));
25717 /* And what's in the hash table should match D. */
25718 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25720 || spec
== NULL_TREE
);
25722 /* This needs to happen before any tsubsting. */
25723 if (! push_tinst_level (d
))
25726 timevar_push (TV_TEMPLATE_INST
);
25728 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25729 for the instantiation. */
25730 td
= template_for_substitution (d
);
25735 /* Look up an explicit specialization, if any. */
25736 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25737 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25738 if (elt
&& elt
!= error_mark_node
)
25740 td
= TREE_VALUE (elt
);
25741 args
= TREE_PURPOSE (elt
);
25745 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25747 /* We should never be trying to instantiate a member of a class
25748 template or partial specialization. */
25749 gcc_assert (d
!= code_pattern
);
25751 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25752 || DECL_TEMPLATE_SPECIALIZATION (td
))
25753 /* In the case of a friend template whose definition is provided
25754 outside the class, we may have too many arguments. Drop the
25755 ones we don't need. The same is true for specializations. */
25756 args
= get_innermost_template_args
25757 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25759 if (TREE_CODE (d
) == FUNCTION_DECL
)
25761 deleted_p
= DECL_DELETED_FN (code_pattern
);
25762 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25763 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25764 || DECL_DEFAULTED_FN (code_pattern
)
25770 if (DECL_CLASS_SCOPE_P (code_pattern
))
25771 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25773 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25776 /* We may be in the middle of deferred access check. Disable it now. */
25777 push_deferring_access_checks (dk_no_deferred
);
25779 /* Unless an explicit instantiation directive has already determined
25780 the linkage of D, remember that a definition is available for
25782 if (pattern_defined
25783 && !DECL_INTERFACE_KNOWN (d
)
25784 && !DECL_NOT_REALLY_EXTERN (d
))
25785 mark_definable (d
);
25787 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25788 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25789 input_location
= DECL_SOURCE_LOCATION (d
);
25791 /* If D is a member of an explicitly instantiated class template,
25792 and no definition is available, treat it like an implicit
25794 if (!pattern_defined
&& expl_inst_class_mem_p
25795 && DECL_EXPLICIT_INSTANTIATION (d
))
25797 /* Leave linkage flags alone on instantiations with anonymous
25799 if (TREE_PUBLIC (d
))
25801 DECL_NOT_REALLY_EXTERN (d
) = 0;
25802 DECL_INTERFACE_KNOWN (d
) = 0;
25804 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25807 /* Defer all other templates, unless we have been explicitly
25808 forbidden from doing so. */
25809 if (/* If there is no definition, we cannot instantiate the
25812 /* If it's OK to postpone instantiation, do so. */
25814 /* If this is a static data member that will be defined
25815 elsewhere, we don't want to instantiate the entire data
25816 member, but we do want to instantiate the initializer so that
25817 we can substitute that elsewhere. */
25818 || (external_p
&& VAR_P (d
))
25819 /* Handle here a deleted function too, avoid generating
25820 its body (c++/61080). */
25823 /* The definition of the static data member is now required so
25824 we must substitute the initializer. */
25826 && !DECL_INITIAL (d
)
25827 && DECL_INITIAL (code_pattern
))
25831 bool const_init
= false;
25832 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25834 ns
= decl_namespace_context (d
);
25835 push_nested_namespace (ns
);
25837 push_nested_class (DECL_CONTEXT (d
));
25838 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25840 tf_warning_or_error
, NULL_TREE
,
25841 /*integral_constant_expression_p=*/false);
25842 /* If instantiating the initializer involved instantiating this
25843 again, don't call cp_finish_decl twice. */
25844 if (!DECL_INITIAL (d
))
25846 /* Make sure the initializer is still constant, in case of
25847 circular dependency (template/instantiate6.C). */
25849 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25850 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25851 /*asmspec_tree=*/NULL_TREE
,
25852 LOOKUP_ONLYCONVERTING
);
25855 pop_nested_class ();
25856 pop_nested_namespace (ns
);
25859 /* We restore the source position here because it's used by
25860 add_pending_template. */
25861 input_location
= saved_loc
;
25863 if (at_eof
&& !pattern_defined
25864 && DECL_EXPLICIT_INSTANTIATION (d
)
25865 && DECL_NOT_REALLY_EXTERN (d
))
25868 The definition of a non-exported function template, a
25869 non-exported member function template, or a non-exported
25870 member function or static data member of a class template
25871 shall be present in every translation unit in which it is
25872 explicitly instantiated. */
25873 permerror (input_location
, "explicit instantiation of %qD "
25874 "but no definition available", d
);
25876 /* If we're in unevaluated context, we just wanted to get the
25877 constant value; this isn't an odr use, so don't queue
25878 a full instantiation. */
25879 if (!cp_unevaluated_operand
25880 /* ??? Historically, we have instantiated inline functions, even
25881 when marked as "extern template". */
25882 && !(external_p
&& VAR_P (d
)))
25883 add_pending_template (d
);
25887 if (variable_template_p (gen_tmpl
))
25888 note_variable_template_instantiation (d
);
25889 instantiate_body (td
, args
, d
, false);
25892 pop_deferring_access_checks ();
25893 timevar_pop (TV_TEMPLATE_INST
);
25894 pop_tinst_level ();
25895 input_location
= saved_loc
;
25896 cp_unevaluated_operand
= saved_unevaluated_operand
;
25897 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25902 /* Run through the list of templates that we wish we could
25903 instantiate, and instantiate any we can. RETRIES is the
25904 number of times we retry pending template instantiation. */
25907 instantiate_pending_templates (int retries
)
25910 location_t saved_loc
= input_location
;
25912 /* Instantiating templates may trigger vtable generation. This in turn
25913 may require further template instantiations. We place a limit here
25914 to avoid infinite loop. */
25915 if (pending_templates
&& retries
>= max_tinst_depth
)
25917 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25919 fatal_error (input_location
,
25920 "template instantiation depth exceeds maximum of %d"
25921 " instantiating %q+D, possibly from virtual table generation"
25922 " (use %<-ftemplate-depth=%> to increase the maximum)",
25923 max_tinst_depth
, decl
);
25924 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25925 /* Pretend that we defined it. */
25926 DECL_INITIAL (decl
) = error_mark_node
;
25932 struct pending_template
**t
= &pending_templates
;
25933 struct pending_template
*last
= NULL
;
25937 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25938 bool complete
= false;
25940 if (TYPE_P (instantiation
))
25942 if (!COMPLETE_TYPE_P (instantiation
))
25944 instantiate_class_template (instantiation
);
25945 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25946 for (tree fld
= TYPE_FIELDS (instantiation
);
25947 fld
; fld
= TREE_CHAIN (fld
))
25949 || (TREE_CODE (fld
) == FUNCTION_DECL
25950 && !DECL_ARTIFICIAL (fld
)))
25951 && DECL_TEMPLATE_INSTANTIATION (fld
))
25952 instantiate_decl (fld
,
25953 /*defer_ok=*/false,
25954 /*expl_inst_class_mem_p=*/false);
25956 if (COMPLETE_TYPE_P (instantiation
))
25960 complete
= COMPLETE_TYPE_P (instantiation
);
25964 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25965 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25968 = instantiate_decl (instantiation
,
25969 /*defer_ok=*/false,
25970 /*expl_inst_class_mem_p=*/false);
25971 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25975 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25976 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25981 /* If INSTANTIATION has been instantiated, then we don't
25982 need to consider it again in the future. */
25983 struct pending_template
*drop
= *t
;
25985 set_refcount_ptr (drop
->tinst
);
25986 pending_template_freelist ().free (drop
);
25994 set_refcount_ptr (current_tinst_level
);
25996 last_pending_template
= last
;
25998 while (reconsider
);
26000 input_location
= saved_loc
;
26003 /* Substitute ARGVEC into T, which is a list of initializers for
26004 either base class or a non-static data member. The TREE_PURPOSEs
26005 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26006 instantiate_decl. */
26009 tsubst_initializer_list (tree t
, tree argvec
)
26011 tree inits
= NULL_TREE
;
26012 tree target_ctor
= error_mark_node
;
26014 for (; t
; t
= TREE_CHAIN (t
))
26018 tree expanded_bases
= NULL_TREE
;
26019 tree expanded_arguments
= NULL_TREE
;
26022 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26027 /* Expand the base class expansion type into separate base
26029 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26030 tf_warning_or_error
,
26032 if (expanded_bases
== error_mark_node
)
26035 /* We'll be building separate TREE_LISTs of arguments for
26037 len
= TREE_VEC_LENGTH (expanded_bases
);
26038 expanded_arguments
= make_tree_vec (len
);
26039 for (i
= 0; i
< len
; i
++)
26040 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26042 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26043 expand each argument in the TREE_VALUE of t. */
26044 expr
= make_node (EXPR_PACK_EXPANSION
);
26045 PACK_EXPANSION_LOCAL_P (expr
) = true;
26046 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26047 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26049 if (TREE_VALUE (t
) == void_type_node
)
26050 /* VOID_TYPE_NODE is used to indicate
26051 value-initialization. */
26053 for (i
= 0; i
< len
; i
++)
26054 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26058 /* Substitute parameter packs into each argument in the
26060 in_base_initializer
= 1;
26061 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26063 tree expanded_exprs
;
26065 /* Expand the argument. */
26066 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26068 = tsubst_pack_expansion (expr
, argvec
,
26069 tf_warning_or_error
,
26071 if (expanded_exprs
== error_mark_node
)
26074 /* Prepend each of the expanded expressions to the
26075 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26076 for (i
= 0; i
< len
; i
++)
26078 TREE_VEC_ELT (expanded_arguments
, i
) =
26079 tree_cons (NULL_TREE
,
26080 TREE_VEC_ELT (expanded_exprs
, i
),
26081 TREE_VEC_ELT (expanded_arguments
, i
));
26084 in_base_initializer
= 0;
26086 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26087 since we built them backwards. */
26088 for (i
= 0; i
< len
; i
++)
26090 TREE_VEC_ELT (expanded_arguments
, i
) =
26091 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26096 for (i
= 0; i
< len
; ++i
)
26098 if (expanded_bases
)
26100 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26101 decl
= expand_member_init (decl
);
26102 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26107 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26108 tf_warning_or_error
, NULL_TREE
);
26110 decl
= expand_member_init (decl
);
26111 if (decl
&& !DECL_P (decl
))
26112 in_base_initializer
= 1;
26114 init
= TREE_VALUE (t
);
26116 if (init
!= void_type_node
)
26117 init
= tsubst_expr (init
, argvec
,
26118 tf_warning_or_error
, NULL_TREE
,
26119 /*integral_constant_expression_p=*/false);
26120 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26121 /* If we had an initializer but it instantiated to nothing,
26122 value-initialize the object. This will only occur when
26123 the initializer was a pack expansion where the parameter
26124 packs used in that expansion were of length zero. */
26125 init
= void_type_node
;
26126 in_base_initializer
= 0;
26129 if (target_ctor
!= error_mark_node
26130 && init
!= error_mark_node
)
26132 error ("mem-initializer for %qD follows constructor delegation",
26136 /* Look for a target constructor. */
26137 if (init
!= error_mark_node
26138 && decl
&& CLASS_TYPE_P (decl
)
26139 && same_type_p (decl
, current_class_type
))
26141 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26144 error ("constructor delegation follows mem-initializer for %qD",
26145 TREE_PURPOSE (inits
));
26148 target_ctor
= init
;
26153 init
= build_tree_list (decl
, init
);
26154 /* Carry over the dummy TREE_TYPE node containing the source
26156 TREE_TYPE (init
) = TREE_TYPE (t
);
26157 TREE_CHAIN (init
) = inits
;
26165 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26168 set_current_access_from_decl (tree decl
)
26170 if (TREE_PRIVATE (decl
))
26171 current_access_specifier
= access_private_node
;
26172 else if (TREE_PROTECTED (decl
))
26173 current_access_specifier
= access_protected_node
;
26175 current_access_specifier
= access_public_node
;
26178 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26179 is the instantiation (which should have been created with
26180 start_enum) and ARGS are the template arguments to use. */
26183 tsubst_enum (tree tag
, tree newtag
, tree args
)
26187 if (SCOPED_ENUM_P (newtag
))
26188 begin_scope (sk_scoped_enum
, newtag
);
26190 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26195 decl
= TREE_VALUE (e
);
26196 /* Note that in a template enum, the TREE_VALUE is the
26197 CONST_DECL, not the corresponding INTEGER_CST. */
26198 value
= tsubst_expr (DECL_INITIAL (decl
),
26199 args
, tf_warning_or_error
, NULL_TREE
,
26200 /*integral_constant_expression_p=*/true);
26202 /* Give this enumeration constant the correct access. */
26203 set_current_access_from_decl (decl
);
26205 /* Actually build the enumerator itself. Here we're assuming that
26206 enumerators can't have dependent attributes. */
26207 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26208 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26211 if (SCOPED_ENUM_P (newtag
))
26214 finish_enum_value_list (newtag
);
26215 finish_enum (newtag
);
26217 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26218 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26221 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26222 its type -- but without substituting the innermost set of template
26223 arguments. So, innermost set of template parameters will appear in
26227 get_mostly_instantiated_function_type (tree decl
)
26229 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26230 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26233 /* Return truthvalue if we're processing a template different from
26234 the last one involved in diagnostics. */
26236 problematic_instantiation_changed (void)
26238 return current_tinst_level
!= last_error_tinst_level
;
26241 /* Remember current template involved in diagnostics. */
26243 record_last_problematic_instantiation (void)
26245 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26248 struct tinst_level
*
26249 current_instantiation (void)
26251 return current_tinst_level
;
26254 /* Return TRUE if current_function_decl is being instantiated, false
26258 instantiating_current_function_p (void)
26260 return (current_instantiation ()
26261 && (current_instantiation ()->maybe_get_node ()
26262 == current_function_decl
));
26265 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26266 type. Return false for ok, true for disallowed. Issue error and
26267 inform messages under control of COMPLAIN. */
26270 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26272 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26274 else if (TYPE_PTR_P (type
))
26276 else if (TYPE_REF_P (type
)
26277 && !TYPE_REF_IS_RVALUE (type
))
26279 else if (TYPE_PTRMEM_P (type
))
26281 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26283 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26285 if (complain
& tf_error
)
26286 error ("non-type template parameters of deduced class type only "
26287 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26292 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26294 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26296 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26298 /* A bound template template parm could later be instantiated to have a valid
26299 nontype parm type via an alias template. */
26300 else if (cxx_dialect
>= cxx11
26301 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26303 else if (VOID_TYPE_P (type
))
26304 /* Fall through. */;
26305 else if (cxx_dialect
>= cxx20
)
26307 if (dependent_type_p (type
))
26309 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26311 if (structural_type_p (type
))
26313 if (complain
& tf_error
)
26315 auto_diagnostic_group d
;
26316 error ("%qT is not a valid type for a template non-type "
26317 "parameter because it is not structural", type
);
26318 structural_type_p (type
, true);
26322 else if (CLASS_TYPE_P (type
))
26324 if (complain
& tf_error
)
26325 error ("non-type template parameters of class type only available "
26326 "with %<-std=c++20%> or %<-std=gnu++20%>");
26330 if (complain
& tf_error
)
26332 if (type
== error_mark_node
)
26333 inform (input_location
, "invalid template non-type parameter");
26335 error ("%q#T is not a valid type for a template non-type parameter",
26341 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26342 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26345 dependent_type_p_r (tree type
)
26351 A type is dependent if it is:
26353 -- a template parameter. Template template parameters are types
26354 for us (since TYPE_P holds true for them) so we handle
26356 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26357 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26359 /* -- a qualified-id with a nested-name-specifier which contains a
26360 class-name that names a dependent type or whose unqualified-id
26361 names a dependent type. */
26362 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26365 /* An alias template specialization can be dependent even if the
26366 resulting type is not. */
26367 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26370 /* -- a cv-qualified type where the cv-unqualified type is
26372 No code is necessary for this bullet; the code below handles
26373 cv-qualified types, and we don't want to strip aliases with
26374 TYPE_MAIN_VARIANT because of DR 1558. */
26375 /* -- a compound type constructed from any dependent type. */
26376 if (TYPE_PTRMEM_P (type
))
26377 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26378 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26380 else if (INDIRECT_TYPE_P (type
))
26381 return dependent_type_p (TREE_TYPE (type
));
26382 else if (FUNC_OR_METHOD_TYPE_P (type
))
26386 if (dependent_type_p (TREE_TYPE (type
)))
26388 for (arg_type
= TYPE_ARG_TYPES (type
);
26390 arg_type
= TREE_CHAIN (arg_type
))
26391 if (dependent_type_p (TREE_VALUE (arg_type
)))
26393 if (cxx_dialect
>= cxx17
)
26394 /* A value-dependent noexcept-specifier makes the type dependent. */
26395 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26396 if (tree noex
= TREE_PURPOSE (spec
))
26397 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26398 affect overload resolution and treating it as dependent breaks
26399 things. Same for an unparsed noexcept expression. */
26400 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26401 && TREE_CODE (noex
) != DEFERRED_PARSE
26402 && value_dependent_expression_p (noex
))
26406 /* -- an array type constructed from any dependent type or whose
26407 size is specified by a constant expression that is
26410 We checked for type- and value-dependence of the bounds in
26411 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26412 if (TREE_CODE (type
) == ARRAY_TYPE
)
26414 if (TYPE_DOMAIN (type
)
26415 && dependent_type_p (TYPE_DOMAIN (type
)))
26417 return dependent_type_p (TREE_TYPE (type
));
26420 /* -- a template-id in which either the template name is a template
26422 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26424 /* ... or any of the template arguments is a dependent type or
26425 an expression that is type-dependent or value-dependent. */
26426 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26427 && (any_dependent_template_arguments_p
26428 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26431 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26432 dependent; if the argument of the `typeof' expression is not
26433 type-dependent, then it should already been have resolved. */
26434 if (TREE_CODE (type
) == TYPEOF_TYPE
26435 || TREE_CODE (type
) == DECLTYPE_TYPE
26436 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26439 /* A template argument pack is dependent if any of its packed
26441 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26443 tree args
= ARGUMENT_PACK_ARGS (type
);
26444 int i
, len
= TREE_VEC_LENGTH (args
);
26445 for (i
= 0; i
< len
; ++i
)
26446 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26450 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26451 be template parameters. */
26452 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26455 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26458 /* The standard does not specifically mention types that are local
26459 to template functions or local classes, but they should be
26460 considered dependent too. For example:
26462 template <int I> void f() {
26467 The size of `E' cannot be known until the value of `I' has been
26468 determined. Therefore, `E' must be considered dependent. */
26469 scope
= TYPE_CONTEXT (type
);
26470 if (scope
&& TYPE_P (scope
))
26471 return dependent_type_p (scope
);
26472 /* Don't use type_dependent_expression_p here, as it can lead
26473 to infinite recursion trying to determine whether a lambda
26474 nested in a lambda is dependent (c++/47687). */
26475 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26476 && DECL_LANG_SPECIFIC (scope
)
26477 && DECL_TEMPLATE_INFO (scope
)
26478 && (any_dependent_template_arguments_p
26479 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26482 /* Other types are non-dependent. */
26486 /* Returns TRUE if TYPE is dependent, in the sense of
26487 [temp.dep.type]. Note that a NULL type is considered dependent. */
26490 dependent_type_p (tree type
)
26492 /* If there are no template parameters in scope, then there can't be
26493 any dependent types. */
26494 if (!processing_template_decl
)
26496 /* If we are not processing a template, then nobody should be
26497 providing us with a dependent type. */
26499 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26503 /* If the type is NULL, we have not computed a type for the entity
26504 in question; in that case, the type is dependent. */
26508 /* Erroneous types can be considered non-dependent. */
26509 if (type
== error_mark_node
)
26512 /* Getting here with global_type_node means we improperly called this
26513 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26514 gcc_checking_assert (type
!= global_type_node
);
26516 /* If we have not already computed the appropriate value for TYPE,
26518 if (!TYPE_DEPENDENT_P_VALID (type
))
26520 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26521 TYPE_DEPENDENT_P_VALID (type
) = 1;
26524 return TYPE_DEPENDENT_P (type
);
26527 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26528 lookup. In other words, a dependent type that is not the current
26532 dependent_scope_p (tree scope
)
26534 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26535 && !currently_open_class (scope
));
26538 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26539 an unknown base of 'this' (and is therefore instantiation-dependent). */
26542 unknown_base_ref_p (tree t
)
26544 if (!current_class_ptr
)
26547 tree mem
= TREE_OPERAND (t
, 1);
26548 if (shared_member_p (mem
))
26551 tree cur
= current_nonlambda_class_type ();
26552 if (!any_dependent_bases_p (cur
))
26555 tree ctx
= TREE_OPERAND (t
, 0);
26556 if (DERIVED_FROM_P (ctx
, cur
))
26562 /* T is a SCOPE_REF; return whether we need to consider it
26563 instantiation-dependent so that we can check access at instantiation
26564 time even though we know which member it resolves to. */
26567 instantiation_dependent_scope_ref_p (tree t
)
26569 if (DECL_P (TREE_OPERAND (t
, 1))
26570 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26571 && !unknown_base_ref_p (t
)
26572 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26573 TREE_OPERAND (t
, 1)))
26579 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26580 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26583 /* Note that this predicate is not appropriate for general expressions;
26584 only constant expressions (that satisfy potential_constant_expression)
26585 can be tested for value dependence. */
26588 value_dependent_expression_p (tree expression
)
26590 if (!processing_template_decl
|| expression
== NULL_TREE
)
26593 /* A type-dependent expression is also value-dependent. */
26594 if (type_dependent_expression_p (expression
))
26597 switch (TREE_CODE (expression
))
26600 /* A dependent member function of the current instantiation. */
26601 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26603 case FUNCTION_DECL
:
26604 /* A dependent member function of the current instantiation. */
26605 if (DECL_CLASS_SCOPE_P (expression
)
26606 && dependent_type_p (DECL_CONTEXT (expression
)))
26610 case IDENTIFIER_NODE
:
26611 /* A name that has not been looked up -- must be dependent. */
26614 case TEMPLATE_PARM_INDEX
:
26615 /* A non-type template parm. */
26619 /* A non-type template parm. */
26620 if (DECL_TEMPLATE_PARM_P (expression
))
26622 return value_dependent_expression_p (DECL_INITIAL (expression
));
26625 /* A constant with literal type and is initialized
26626 with an expression that is value-dependent. */
26627 if (DECL_DEPENDENT_INIT_P (expression
)
26628 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26629 || TYPE_REF_P (TREE_TYPE (expression
)))
26631 if (DECL_HAS_VALUE_EXPR_P (expression
))
26633 tree value_expr
= DECL_VALUE_EXPR (expression
);
26634 if (value_dependent_expression_p (value_expr
)
26635 /* __PRETTY_FUNCTION__ inside a template function is dependent
26636 on the name of the function. */
26637 || (DECL_PRETTY_FUNCTION_P (expression
)
26638 /* It might be used in a template, but not a template
26639 function, in which case its DECL_VALUE_EXPR will be
26641 && value_expr
== error_mark_node
))
26646 case DYNAMIC_CAST_EXPR
:
26647 case STATIC_CAST_EXPR
:
26648 case CONST_CAST_EXPR
:
26649 case REINTERPRET_CAST_EXPR
:
26651 case IMPLICIT_CONV_EXPR
:
26652 /* These expressions are value-dependent if the type to which
26653 the cast occurs is dependent or the expression being casted
26654 is value-dependent. */
26656 tree type
= TREE_TYPE (expression
);
26658 if (dependent_type_p (type
))
26661 /* A functional cast has a list of operands. */
26662 expression
= TREE_OPERAND (expression
, 0);
26665 /* If there are no operands, it must be an expression such
26666 as "int()". This should not happen for aggregate types
26667 because it would form non-constant expressions. */
26668 gcc_assert (cxx_dialect
>= cxx11
26669 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26674 if (TREE_CODE (expression
) == TREE_LIST
)
26675 return any_value_dependent_elements_p (expression
);
26677 return value_dependent_expression_p (expression
);
26681 if (SIZEOF_EXPR_TYPE_P (expression
))
26682 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26686 /* A `sizeof' expression is value-dependent if the operand is
26687 type-dependent or is a pack expansion. */
26688 expression
= TREE_OPERAND (expression
, 0);
26689 if (PACK_EXPANSION_P (expression
))
26691 else if (TYPE_P (expression
))
26692 return dependent_type_p (expression
);
26693 return instantiation_dependent_uneval_expression_p (expression
);
26695 case AT_ENCODE_EXPR
:
26696 /* An 'encode' expression is value-dependent if the operand is
26698 expression
= TREE_OPERAND (expression
, 0);
26699 return dependent_type_p (expression
);
26701 case NOEXCEPT_EXPR
:
26702 expression
= TREE_OPERAND (expression
, 0);
26703 return instantiation_dependent_uneval_expression_p (expression
);
26706 /* All instantiation-dependent expressions should also be considered
26707 value-dependent. */
26708 return instantiation_dependent_scope_ref_p (expression
);
26710 case COMPONENT_REF
:
26711 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26712 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26714 case NONTYPE_ARGUMENT_PACK
:
26715 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26716 is value-dependent. */
26718 tree values
= ARGUMENT_PACK_ARGS (expression
);
26719 int i
, len
= TREE_VEC_LENGTH (values
);
26721 for (i
= 0; i
< len
; ++i
)
26722 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26730 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26732 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26738 if (TREE_CODE (type2
) != TREE_LIST
)
26739 return dependent_type_p (type2
);
26741 for (; type2
; type2
= TREE_CHAIN (type2
))
26742 if (dependent_type_p (TREE_VALUE (type2
)))
26749 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26750 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26753 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26754 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26758 tree op
= TREE_OPERAND (expression
, 0);
26759 return (value_dependent_expression_p (op
)
26760 || has_value_dependent_address (op
));
26763 case REQUIRES_EXPR
:
26764 /* Treat all requires-expressions as value-dependent so
26765 we don't try to fold them. */
26769 return dependent_type_p (TREE_OPERAND (expression
, 0));
26773 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26775 tree fn
= get_callee_fndecl (expression
);
26777 nargs
= call_expr_nargs (expression
);
26778 for (i
= 0; i
< nargs
; ++i
)
26780 tree op
= CALL_EXPR_ARG (expression
, i
);
26781 /* In a call to a constexpr member function, look through the
26782 implicit ADDR_EXPR on the object argument so that it doesn't
26783 cause the call to be considered value-dependent. We also
26784 look through it in potential_constant_expression. */
26785 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26786 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26787 && TREE_CODE (op
) == ADDR_EXPR
)
26788 op
= TREE_OPERAND (op
, 0);
26789 if (value_dependent_expression_p (op
))
26795 case TEMPLATE_ID_EXPR
:
26796 return concept_definition_p (TREE_OPERAND (expression
, 0));
26802 if (dependent_type_p (TREE_TYPE (expression
)))
26804 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26805 if (value_dependent_expression_p (val
))
26811 /* Treat a GNU statement expression as dependent to avoid crashing
26812 under instantiate_non_dependent_expr; it can't be constant. */
26816 /* A constant expression is value-dependent if any subexpression is
26817 value-dependent. */
26818 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26820 case tcc_reference
:
26822 case tcc_comparison
:
26824 case tcc_expression
:
26827 int i
, len
= cp_tree_operand_length (expression
);
26829 for (i
= 0; i
< len
; i
++)
26831 tree t
= TREE_OPERAND (expression
, i
);
26833 /* In some cases, some of the operands may be missing.
26834 (For example, in the case of PREDECREMENT_EXPR, the
26835 amount to increment by may be missing.) That doesn't
26836 make the expression dependent. */
26837 if (t
&& value_dependent_expression_p (t
))
26848 /* The expression is not value-dependent. */
26852 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26853 [temp.dep.expr]. Note that an expression with no type is
26854 considered dependent. Other parts of the compiler arrange for an
26855 expression with type-dependent subexpressions to have no type, so
26856 this function doesn't have to be fully recursive. */
26859 type_dependent_expression_p (tree expression
)
26861 if (!processing_template_decl
)
26864 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26867 STRIP_ANY_LOCATION_WRAPPER (expression
);
26869 /* An unresolved name is always dependent. */
26870 if (identifier_p (expression
)
26871 || TREE_CODE (expression
) == USING_DECL
26872 || TREE_CODE (expression
) == WILDCARD_DECL
)
26875 /* A lambda-expression in template context is dependent. dependent_type_p is
26876 true for a lambda in the scope of a class or function template, but that
26877 doesn't cover all template contexts, like a default template argument. */
26878 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26881 /* A fold expression is type-dependent. */
26882 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26883 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26884 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26885 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26888 /* Some expression forms are never type-dependent. */
26889 if (TREE_CODE (expression
) == SIZEOF_EXPR
26890 || TREE_CODE (expression
) == ALIGNOF_EXPR
26891 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26892 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26893 || TREE_CODE (expression
) == TRAIT_EXPR
26894 || TREE_CODE (expression
) == TYPEID_EXPR
26895 || TREE_CODE (expression
) == DELETE_EXPR
26896 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26897 || TREE_CODE (expression
) == THROW_EXPR
26898 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26901 /* The types of these expressions depends only on the type to which
26902 the cast occurs. */
26903 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26904 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26905 || TREE_CODE (expression
) == CONST_CAST_EXPR
26906 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26907 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26908 || TREE_CODE (expression
) == CAST_EXPR
)
26909 return dependent_type_p (TREE_TYPE (expression
));
26911 /* The types of these expressions depends only on the type created
26912 by the expression. */
26913 if (TREE_CODE (expression
) == NEW_EXPR
26914 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26916 /* For NEW_EXPR tree nodes created inside a template, either
26917 the object type itself or a TREE_LIST may appear as the
26919 tree type
= TREE_OPERAND (expression
, 1);
26920 if (TREE_CODE (type
) == TREE_LIST
)
26921 /* This is an array type. We need to check array dimensions
26923 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26924 || value_dependent_expression_p
26925 (TREE_OPERAND (TREE_VALUE (type
), 1));
26926 /* Array type whose dimension has to be deduced. */
26927 else if (TREE_CODE (type
) == ARRAY_TYPE
26928 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
26931 return dependent_type_p (type
);
26934 if (TREE_CODE (expression
) == SCOPE_REF
)
26936 tree scope
= TREE_OPERAND (expression
, 0);
26937 tree name
= TREE_OPERAND (expression
, 1);
26939 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26940 contains an identifier associated by name lookup with one or more
26941 declarations declared with a dependent type, or...a
26942 nested-name-specifier or qualified-id that names a member of an
26943 unknown specialization. */
26944 return (type_dependent_expression_p (name
)
26945 || dependent_scope_p (scope
));
26948 if (TREE_CODE (expression
) == TEMPLATE_DECL
26949 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26950 return uses_outer_template_parms (expression
);
26952 if (TREE_CODE (expression
) == STMT_EXPR
)
26953 expression
= stmt_expr_value_expr (expression
);
26955 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26960 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26962 if (type_dependent_expression_p (elt
))
26968 /* A static data member of the current instantiation with incomplete
26969 array type is type-dependent, as the definition and specializations
26970 can have different bounds. */
26971 if (VAR_P (expression
)
26972 && DECL_CLASS_SCOPE_P (expression
)
26973 && dependent_type_p (DECL_CONTEXT (expression
))
26974 && VAR_HAD_UNKNOWN_BOUND (expression
))
26977 /* An array of unknown bound depending on a variadic parameter, eg:
26979 template<typename... Args>
26980 void foo (Args... args)
26982 int arr[] = { args... };
26985 template<int... vals>
26988 int arr[] = { vals... };
26991 If the array has no length and has an initializer, it must be that
26992 we couldn't determine its length in cp_complete_array_type because
26993 it is dependent. */
26994 if (VAR_P (expression
)
26995 && TREE_TYPE (expression
) != NULL_TREE
26996 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26997 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26998 && DECL_INITIAL (expression
))
27001 /* A function or variable template-id is type-dependent if it has any
27002 dependent template arguments. */
27003 if (VAR_OR_FUNCTION_DECL_P (expression
)
27004 && DECL_LANG_SPECIFIC (expression
)
27005 && DECL_TEMPLATE_INFO (expression
))
27007 /* Consider the innermost template arguments, since those are the ones
27008 that come from the template-id; the template arguments for the
27009 enclosing class do not make it type-dependent unless they are used in
27010 the type of the decl. */
27011 if (instantiates_primary_template_p (expression
)
27012 && (any_dependent_template_arguments_p
27013 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27017 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27018 type-dependent. Checking this is important for functions with auto return
27019 type, which looks like a dependent type. */
27020 if (TREE_CODE (expression
) == FUNCTION_DECL
27021 && !(DECL_CLASS_SCOPE_P (expression
)
27022 && dependent_type_p (DECL_CONTEXT (expression
)))
27023 && !(DECL_LANG_SPECIFIC (expression
)
27024 && DECL_FRIEND_P (expression
)
27025 && (!DECL_FRIEND_CONTEXT (expression
)
27026 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27027 && !DECL_LOCAL_DECL_P (expression
))
27029 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27030 || undeduced_auto_decl (expression
));
27034 /* Always dependent, on the number of arguments if nothing else. */
27035 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27038 if (TREE_TYPE (expression
) == unknown_type_node
)
27040 if (TREE_CODE (expression
) == ADDR_EXPR
)
27041 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27042 if (TREE_CODE (expression
) == COMPONENT_REF
27043 || TREE_CODE (expression
) == OFFSET_REF
)
27045 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27047 expression
= TREE_OPERAND (expression
, 1);
27048 if (identifier_p (expression
))
27051 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27052 if (TREE_CODE (expression
) == SCOPE_REF
)
27055 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27056 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27057 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27060 if (BASELINK_P (expression
))
27062 if (BASELINK_OPTYPE (expression
)
27063 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27065 expression
= BASELINK_FUNCTIONS (expression
);
27068 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27070 if (any_dependent_template_arguments_p
27071 (TREE_OPERAND (expression
, 1)))
27073 expression
= TREE_OPERAND (expression
, 0);
27074 if (identifier_p (expression
))
27078 gcc_assert (OVL_P (expression
));
27080 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27081 if (type_dependent_expression_p (*iter
))
27087 /* The type of a non-type template parm declared with a placeholder type
27088 depends on the corresponding template argument, even though
27089 placeholders are not normally considered dependent. */
27090 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27091 && is_auto (TREE_TYPE (expression
)))
27094 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27096 /* Dependent type attributes might not have made it from the decl to
27098 if (DECL_P (expression
)
27099 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27102 return (dependent_type_p (TREE_TYPE (expression
)));
27105 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27106 type-dependent if the expression refers to a member of the current
27107 instantiation and the type of the referenced member is dependent, or the
27108 class member access expression refers to a member of an unknown
27111 This function returns true if the OBJECT in such a class member access
27112 expression is of an unknown specialization. */
27115 type_dependent_object_expression_p (tree object
)
27117 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27119 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27121 tree scope
= TREE_TYPE (object
);
27122 return (!scope
|| dependent_scope_p (scope
));
27125 /* walk_tree callback function for instantiation_dependent_expression_p,
27126 below. Returns non-zero if a dependent subexpression is found. */
27129 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27134 /* We don't have to worry about decltype currently because decltype
27135 of an instantiation-dependent expr is a dependent type. This
27136 might change depending on the resolution of DR 1172. */
27137 *walk_subtrees
= false;
27140 enum tree_code code
= TREE_CODE (*tp
);
27143 /* Don't treat an argument list as dependent just because it has no
27147 case NONTYPE_ARGUMENT_PACK
:
27150 case TEMPLATE_PARM_INDEX
:
27151 if (dependent_type_p (TREE_TYPE (*tp
)))
27153 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27155 /* We'll check value-dependence separately. */
27158 /* Handle expressions with type operands. */
27162 case AT_ENCODE_EXPR
:
27164 tree op
= TREE_OPERAND (*tp
, 0);
27165 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27166 op
= TREE_TYPE (op
);
27169 if (dependent_type_p (op
))
27173 *walk_subtrees
= false;
27180 case COMPONENT_REF
:
27181 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27182 /* In a template, finish_class_member_access_expr creates a
27183 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27184 type-dependent, so that we can check access control at
27185 instantiation time (PR 42277). See also Core issue 1273. */
27190 if (instantiation_dependent_scope_ref_p (*tp
))
27195 /* Treat statement-expressions as dependent. */
27199 /* Treat requires-expressions as dependent. */
27200 case REQUIRES_EXPR
:
27204 /* Treat concept checks as dependent. */
27205 if (concept_check_p (*tp
))
27209 case TEMPLATE_ID_EXPR
:
27210 /* Treat concept checks as dependent. */
27211 if (concept_check_p (*tp
))
27216 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27224 if (type_dependent_expression_p (*tp
))
27230 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27231 sense defined by the ABI:
27233 "An expression is instantiation-dependent if it is type-dependent
27234 or value-dependent, or it has a subexpression that is type-dependent
27235 or value-dependent."
27237 Except don't actually check value-dependence for unevaluated expressions,
27238 because in sizeof(i) we don't care about the value of i. Checking
27239 type-dependence will in turn check value-dependence of array bounds/template
27240 arguments as needed. */
27243 instantiation_dependent_uneval_expression_p (tree expression
)
27247 if (!processing_template_decl
)
27250 if (expression
== error_mark_node
)
27253 result
= cp_walk_tree_without_duplicates (&expression
,
27254 instantiation_dependent_r
, NULL
);
27255 return result
!= NULL_TREE
;
27258 /* As above, but also check value-dependence of the expression as a whole. */
27261 instantiation_dependent_expression_p (tree expression
)
27263 return (instantiation_dependent_uneval_expression_p (expression
)
27264 || value_dependent_expression_p (expression
));
27267 /* Like type_dependent_expression_p, but it also works while not processing
27268 a template definition, i.e. during substitution or mangling. */
27271 type_dependent_expression_p_push (tree expr
)
27274 ++processing_template_decl
;
27275 b
= type_dependent_expression_p (expr
);
27276 --processing_template_decl
;
27280 /* Returns TRUE if ARGS contains a type-dependent expression. */
27283 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27288 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27290 if (type_dependent_expression_p (arg
))
27296 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27297 expressions) contains any type-dependent expressions. */
27300 any_type_dependent_elements_p (const_tree list
)
27302 for (; list
; list
= TREE_CHAIN (list
))
27303 if (type_dependent_expression_p (TREE_VALUE (list
)))
27309 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27310 expressions) contains any value-dependent expressions. */
27313 any_value_dependent_elements_p (const_tree list
)
27315 for (; list
; list
= TREE_CHAIN (list
))
27316 if (value_dependent_expression_p (TREE_VALUE (list
)))
27322 /* Returns TRUE if the ARG (a template argument) is dependent. */
27325 dependent_template_arg_p (tree arg
)
27327 if (!processing_template_decl
)
27330 /* Assume a template argument that was wrongly written by the user
27331 is dependent. This is consistent with what
27332 any_dependent_template_arguments_p [that calls this function]
27334 if (!arg
|| arg
== error_mark_node
)
27337 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27338 arg
= argument_pack_select_arg (arg
);
27340 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27342 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27344 if (DECL_TEMPLATE_PARM_P (arg
))
27346 /* A member template of a dependent class is not necessarily
27347 type-dependent, but it is a dependent template argument because it
27348 will be a member of an unknown specialization to that template. */
27349 tree scope
= CP_DECL_CONTEXT (arg
);
27350 return TYPE_P (scope
) && dependent_type_p (scope
);
27352 else if (ARGUMENT_PACK_P (arg
))
27354 tree args
= ARGUMENT_PACK_ARGS (arg
);
27355 int i
, len
= TREE_VEC_LENGTH (args
);
27356 for (i
= 0; i
< len
; ++i
)
27358 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27364 else if (TYPE_P (arg
))
27365 return dependent_type_p (arg
);
27367 return value_dependent_expression_p (arg
);
27370 /* Returns true if ARGS (a collection of template arguments) contains
27371 any types that require structural equality testing. */
27374 any_template_arguments_need_structural_equality_p (tree args
)
27381 if (args
== error_mark_node
)
27384 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27386 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27387 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27389 tree arg
= TREE_VEC_ELT (level
, j
);
27390 tree packed_args
= NULL_TREE
;
27393 if (ARGUMENT_PACK_P (arg
))
27395 /* Look inside the argument pack. */
27396 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27397 len
= TREE_VEC_LENGTH (packed_args
);
27400 for (k
= 0; k
< len
; ++k
)
27403 arg
= TREE_VEC_ELT (packed_args
, k
);
27405 if (error_operand_p (arg
))
27407 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27409 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27411 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27412 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27421 /* Returns true if ARGS (a collection of template arguments) contains
27422 any dependent arguments. */
27425 any_dependent_template_arguments_p (const_tree args
)
27432 if (args
== error_mark_node
)
27435 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27437 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27438 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27439 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27446 /* Returns true if ARGS contains any errors. */
27449 any_erroneous_template_args_p (const_tree args
)
27454 if (args
== error_mark_node
)
27457 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27459 if (tree ti
= get_template_info (args
))
27460 args
= TI_ARGS (ti
);
27468 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27470 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27471 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27472 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27479 /* Returns TRUE if the template TMPL is type-dependent. */
27482 dependent_template_p (tree tmpl
)
27484 if (TREE_CODE (tmpl
) == OVERLOAD
)
27486 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27487 if (dependent_template_p (*iter
))
27492 /* Template template parameters are dependent. */
27493 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27494 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27496 /* So are names that have not been looked up. */
27497 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27502 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27505 dependent_template_id_p (tree tmpl
, tree args
)
27507 return (dependent_template_p (tmpl
)
27508 || any_dependent_template_arguments_p (args
));
27511 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27515 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27519 if (!processing_template_decl
)
27522 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27524 tree decl
= TREE_VEC_ELT (declv
, i
);
27525 tree init
= TREE_VEC_ELT (initv
, i
);
27526 tree cond
= TREE_VEC_ELT (condv
, i
);
27527 tree incr
= TREE_VEC_ELT (incrv
, i
);
27529 if (type_dependent_expression_p (decl
)
27530 || TREE_CODE (decl
) == SCOPE_REF
)
27533 if (init
&& type_dependent_expression_p (init
))
27536 if (cond
== global_namespace
)
27539 if (type_dependent_expression_p (cond
))
27542 if (COMPARISON_CLASS_P (cond
)
27543 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27544 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27547 if (TREE_CODE (incr
) == MODOP_EXPR
)
27549 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27550 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27553 else if (type_dependent_expression_p (incr
))
27555 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27557 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27559 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27561 tree t
= TREE_OPERAND (incr
, 1);
27562 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27563 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27566 /* If this loop has a class iterator with != comparison
27567 with increment other than i++/++i/i--/--i, make sure the
27568 increment is constant. */
27569 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27570 && TREE_CODE (cond
) == NE_EXPR
)
27572 if (TREE_OPERAND (t
, 0) == decl
)
27573 t
= TREE_OPERAND (t
, 1);
27575 t
= TREE_OPERAND (t
, 0);
27576 if (TREE_CODE (t
) != INTEGER_CST
)
27586 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27587 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27588 no such TYPE can be found. Note that this function peers inside
27589 uninstantiated templates and therefore should be used only in
27590 extremely limited situations. ONLY_CURRENT_P restricts this
27591 peering to the currently open classes hierarchy (which is required
27592 when comparing types). */
27595 resolve_typename_type (tree type
, bool only_current_p
)
27604 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27606 scope
= TYPE_CONTEXT (type
);
27607 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27608 gcc_checking_assert (uses_template_parms (scope
));
27610 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27611 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27612 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27613 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27614 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27615 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27616 the TYPENAME_TYPE instead, we avoid messing up with a possible
27617 typedef variant case. */
27618 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27620 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27621 it first before we can figure out what NAME refers to. */
27622 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27624 if (TYPENAME_IS_RESOLVING_P (scope
))
27625 /* Given a class template A with a dependent base with nested type C,
27626 typedef typename A::C::C C will land us here, as trying to resolve
27627 the initial A::C leads to the local C typedef, which leads back to
27628 A::C::C. So we break the recursion now. */
27631 scope
= resolve_typename_type (scope
, only_current_p
);
27633 /* If we don't know what SCOPE refers to, then we cannot resolve the
27635 if (!CLASS_TYPE_P (scope
))
27637 /* If this is a typedef, we don't want to look inside (c++/11987). */
27638 if (typedef_variant_p (type
))
27640 /* If SCOPE isn't the template itself, it will not have a valid
27641 TYPE_FIELDS list. */
27642 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27643 /* scope is either the template itself or a compatible instantiation
27644 like X<T>, so look up the name in the original template. */
27645 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27646 /* If scope has no fields, it can't be a current instantiation. Check this
27647 before currently_open_class to avoid infinite recursion (71515). */
27648 if (!TYPE_FIELDS (scope
))
27650 /* If the SCOPE is not the current instantiation, there's no reason
27651 to look inside it. */
27652 if (only_current_p
&& !currently_open_class (scope
))
27654 /* Enter the SCOPE so that name lookup will be resolved as if we
27655 were in the class definition. In particular, SCOPE will no
27656 longer be considered a dependent type. */
27657 pushed_scope
= push_scope (scope
);
27658 /* Look up the declaration. */
27659 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27660 tf_warning_or_error
);
27662 result
= NULL_TREE
;
27664 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27665 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27666 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27669 else if (identifier_p (fullname
)
27670 && TREE_CODE (decl
) == TYPE_DECL
)
27672 result
= TREE_TYPE (decl
);
27673 if (result
== error_mark_node
)
27674 result
= NULL_TREE
;
27676 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27677 && DECL_CLASS_TEMPLATE_P (decl
))
27679 /* Obtain the template and the arguments. */
27680 tree tmpl
= TREE_OPERAND (fullname
, 0);
27681 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27683 /* We get here with a plain identifier because a previous tentative
27684 parse of the nested-name-specifier as part of a ptr-operator saw
27685 ::template X<A>. The use of ::template is necessary in a
27686 ptr-operator, but wrong in a declarator-id.
27688 [temp.names]: In a qualified-id of a declarator-id, the keyword
27689 template shall not appear at the top level. */
27690 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27691 "keyword %<template%> not allowed in declarator-id");
27694 tree args
= TREE_OPERAND (fullname
, 1);
27695 /* Instantiate the template. */
27696 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27697 /*entering_scope=*/true,
27698 tf_error
| tf_user
);
27699 if (result
== error_mark_node
)
27700 result
= NULL_TREE
;
27703 /* Leave the SCOPE. */
27705 pop_scope (pushed_scope
);
27707 /* If we failed to resolve it, return the original typename. */
27711 /* If lookup found a typename type, resolve that too. */
27712 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27714 /* Ill-formed programs can cause infinite recursion here, so we
27715 must catch that. */
27716 TYPENAME_IS_RESOLVING_P (result
) = 1;
27717 result
= resolve_typename_type (result
, only_current_p
);
27718 TYPENAME_IS_RESOLVING_P (result
) = 0;
27721 /* Qualify the resulting type. */
27722 quals
= cp_type_quals (type
);
27724 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27729 /* EXPR is an expression which is not type-dependent. Return a proxy
27730 for EXPR that can be used to compute the types of larger
27731 expressions containing EXPR. */
27734 build_non_dependent_expr (tree expr
)
27736 tree orig_expr
= expr
;
27739 /* When checking, try to get a constant value for all non-dependent
27740 expressions in order to expose bugs in *_dependent_expression_p
27741 and constexpr. This can affect code generation, see PR70704, so
27742 only do this for -fchecking=2. */
27743 if (flag_checking
> 1
27744 && cxx_dialect
>= cxx11
27745 /* Don't do this during nsdmi parsing as it can lead to
27746 unexpected recursive instantiations. */
27747 && !parsing_nsdmi ()
27748 /* Don't do this during concept processing either and for
27749 the same reason. */
27750 && !processing_constraint_expression_p ())
27751 fold_non_dependent_expr (expr
, tf_none
);
27753 STRIP_ANY_LOCATION_WRAPPER (expr
);
27755 /* Preserve OVERLOADs; the functions must be available to resolve
27758 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27759 inner_expr
= stmt_expr_value_expr (inner_expr
);
27760 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27761 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27762 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27763 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27764 if (is_overloaded_fn (inner_expr
)
27765 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27767 /* There is no need to return a proxy for a variable or enumerator. */
27768 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27770 /* Preserve string constants; conversions from string constants to
27771 "char *" are allowed, even though normally a "const char *"
27772 cannot be used to initialize a "char *". */
27773 if (TREE_CODE (expr
) == STRING_CST
)
27775 /* Preserve void and arithmetic constants, as an optimization -- there is no
27776 reason to create a new node. */
27777 if (TREE_CODE (expr
) == VOID_CST
27778 || TREE_CODE (expr
) == INTEGER_CST
27779 || TREE_CODE (expr
) == REAL_CST
)
27781 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27782 There is at least one place where we want to know that a
27783 particular expression is a throw-expression: when checking a ?:
27784 expression, there are special rules if the second or third
27785 argument is a throw-expression. */
27786 if (TREE_CODE (expr
) == THROW_EXPR
)
27789 /* Don't wrap an initializer list, we need to be able to look inside. */
27790 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27793 /* Don't wrap a dummy object, we need to be able to test for it. */
27794 if (is_dummy_object (expr
))
27797 if (TREE_CODE (expr
) == COND_EXPR
)
27798 return build3 (COND_EXPR
,
27800 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27801 (TREE_OPERAND (expr
, 1)
27802 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27803 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27804 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27805 if (TREE_CODE (expr
) == COMPOUND_EXPR
27806 && !COMPOUND_EXPR_OVERLOADED (expr
))
27807 return build2 (COMPOUND_EXPR
,
27809 TREE_OPERAND (expr
, 0),
27810 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27812 /* If the type is unknown, it can't really be non-dependent */
27813 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27815 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27816 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27817 TREE_TYPE (expr
), expr
);
27820 /* ARGS is a vector of expressions as arguments to a function call.
27821 Replace the arguments with equivalent non-dependent expressions.
27822 This modifies ARGS in place. */
27825 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27830 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27832 tree newarg
= build_non_dependent_expr (arg
);
27834 (*args
)[ix
] = newarg
;
27838 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27839 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27840 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27843 make_auto_1 (tree name
, bool set_canonical
)
27845 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27846 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27847 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27848 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27849 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27850 TYPE_NAME (au
), NULL_TREE
);
27852 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27853 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27854 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27855 if (name
== decltype_auto_identifier
)
27856 AUTO_IS_DECLTYPE (au
) = true;
27862 make_decltype_auto (void)
27864 return make_auto_1 (decltype_auto_identifier
, true);
27870 return make_auto_1 (auto_identifier
, true);
27873 /* Return a C++17 deduction placeholder for class template TMPL. */
27876 make_template_placeholder (tree tmpl
)
27878 tree t
= make_auto_1 (auto_identifier
, false);
27879 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27880 /* Our canonical type depends on the placeholder. */
27881 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27885 /* True iff T is a C++17 class template deduction placeholder. */
27888 template_placeholder_p (tree t
)
27890 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27893 /* Make a "constrained auto" type-specifier. This is an auto or
27894 decltype(auto) type with constraints that must be associated after
27895 deduction. The constraint is formed from the given concept CON
27896 and its optional sequence of template arguments ARGS.
27898 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27901 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27903 /* Build the constraint. */
27904 tree tmpl
= DECL_TI_TEMPLATE (con
);
27906 if (TREE_CODE (con
) == FUNCTION_DECL
)
27907 expr
= ovl_make (tmpl
);
27908 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27910 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27912 /* Our canonical type depends on the constraint. */
27913 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27915 /* Attach the constraint to the type declaration. */
27916 return TYPE_NAME (type
);
27919 /* Make a "constrained auto" type-specifier. */
27922 make_constrained_auto (tree con
, tree args
)
27924 tree type
= make_auto_1 (auto_identifier
, false);
27925 return make_constrained_placeholder_type (type
, con
, args
);
27928 /* Make a "constrained decltype(auto)" type-specifier. */
27931 make_constrained_decltype_auto (tree con
, tree args
)
27933 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27934 return make_constrained_placeholder_type (type
, con
, args
);
27937 /* Build and return a concept definition. Like other templates, the
27938 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27939 the TEMPLATE_DECL. */
27942 finish_concept_definition (cp_expr id
, tree init
)
27944 gcc_assert (identifier_p (id
));
27945 gcc_assert (processing_template_decl
);
27947 location_t loc
= id
.get_location();
27949 /* A concept-definition shall not have associated constraints. */
27950 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27952 error_at (loc
, "a concept cannot be constrained");
27953 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27956 /* A concept-definition shall appear in namespace scope. Templates
27957 aren't allowed in block scope, so we only need to check for class
27959 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27961 error_at (loc
, "concept %qE not in namespace scope", *id
);
27962 return error_mark_node
;
27965 /* Initially build the concept declaration; its type is bool. */
27966 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27967 DECL_CONTEXT (decl
) = current_scope ();
27968 DECL_INITIAL (decl
) = init
;
27970 /* Push the enclosing template. */
27971 return push_template_decl (decl
);
27974 /* Given type ARG, return std::initializer_list<ARG>. */
27979 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27981 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27983 gcc_rich_location
richloc (input_location
);
27984 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27985 error_at (&richloc
,
27986 "deducing from brace-enclosed initializer list"
27987 " requires %<#include <initializer_list>%>");
27989 return error_mark_node
;
27991 tree argvec
= make_tree_vec (1);
27992 TREE_VEC_ELT (argvec
, 0) = arg
;
27994 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27995 NULL_TREE
, 0, tf_warning_or_error
);
27998 /* Replace auto in TYPE with std::initializer_list<auto>. */
28001 listify_autos (tree type
, tree auto_node
)
28003 tree init_auto
= listify (strip_top_quals (auto_node
));
28004 tree argvec
= make_tree_vec (1);
28005 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28006 if (processing_template_decl
)
28007 argvec
= add_to_template_args (current_template_args (), argvec
);
28008 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28011 /* Hash traits for hashing possibly constrained 'auto'
28012 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28014 struct auto_hash
: default_hash_traits
<tree
>
28016 static inline hashval_t
hash (tree
);
28017 static inline bool equal (tree
, tree
);
28020 /* Hash the 'auto' T. */
28023 auto_hash::hash (tree t
)
28025 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28026 /* Matching constrained-type-specifiers denote the same template
28027 parameter, so hash the constraint. */
28028 return hash_placeholder_constraint (c
);
28030 /* But unconstrained autos are all separate, so just hash the pointer. */
28031 return iterative_hash_object (t
, 0);
28034 /* Compare two 'auto's. */
28037 auto_hash::equal (tree t1
, tree t2
)
28042 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28043 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28045 /* Two unconstrained autos are distinct. */
28049 return equivalent_placeholder_constraints (c1
, c2
);
28052 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28053 constrained) auto, add it to the vector. */
28056 extract_autos_r (tree t
, void *data
)
28058 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28061 /* All the autos were built with index 0; fix that up now. */
28062 tree
*p
= hash
.find_slot (t
, INSERT
);
28065 /* If this is a repeated constrained-type-specifier, use the index we
28067 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28070 /* Otherwise this is new, so use the current count. */
28072 idx
= hash
.elements () - 1;
28074 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28077 /* Always keep walking. */
28081 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28082 says they can appear anywhere in the type. */
28085 extract_autos (tree type
)
28087 hash_set
<tree
> visited
;
28088 hash_table
<auto_hash
> hash (2);
28090 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28092 tree tree_vec
= make_tree_vec (hash
.elements());
28093 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28094 iter
!= hash
.end(); ++iter
)
28097 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28098 TREE_VEC_ELT (tree_vec
, i
)
28099 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28105 /* The stem for deduction guide names. */
28106 const char *const dguide_base
= "__dguide_";
28108 /* Return the name for a deduction guide for class template TMPL. */
28111 dguide_name (tree tmpl
)
28113 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28114 tree tname
= TYPE_IDENTIFIER (type
);
28115 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28116 + IDENTIFIER_LENGTH (tname
));
28117 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28118 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28119 IDENTIFIER_LENGTH (tname
) + 1);
28120 tree dname
= get_identifier (buf
);
28121 TREE_TYPE (dname
) = type
;
28125 /* True if NAME is the name of a deduction guide. */
28128 dguide_name_p (tree name
)
28130 return (TREE_CODE (name
) == IDENTIFIER_NODE
28131 && TREE_TYPE (name
)
28132 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28133 strlen (dguide_base
)));
28136 /* True if FN is a deduction guide. */
28139 deduction_guide_p (const_tree fn
)
28142 if (tree name
= DECL_NAME (fn
))
28143 return dguide_name_p (name
);
28147 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28150 copy_guide_p (const_tree fn
)
28152 gcc_assert (deduction_guide_p (fn
));
28153 if (!DECL_ARTIFICIAL (fn
))
28155 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28156 return (TREE_CHAIN (parms
) == void_list_node
28157 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28160 /* True if FN is a guide generated from a constructor template. */
28163 template_guide_p (const_tree fn
)
28165 gcc_assert (deduction_guide_p (fn
));
28166 if (!DECL_ARTIFICIAL (fn
))
28168 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28169 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28170 return PRIMARY_TEMPLATE_P (org
);
28174 /* True if FN is an aggregate initialization guide or the copy deduction
28178 builtin_guide_p (const_tree fn
)
28180 if (!deduction_guide_p (fn
))
28182 if (!DECL_ARTIFICIAL (fn
))
28183 /* Explicitly declared. */
28185 if (DECL_ABSTRACT_ORIGIN (fn
))
28186 /* Derived from a constructor. */
28191 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28192 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28193 template parameter types. Note that the handling of template template
28194 parameters relies on current_template_parms being set appropriately for the
28198 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28199 tree tsubst_args
, tsubst_flags_t complain
)
28201 if (olddecl
== error_mark_node
)
28202 return error_mark_node
;
28204 tree oldidx
= get_template_parm_index (olddecl
);
28207 if (TREE_CODE (olddecl
) == TYPE_DECL
28208 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28210 tree oldtype
= TREE_TYPE (olddecl
);
28211 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28212 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28213 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28214 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28215 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28219 newtype
= TREE_TYPE (olddecl
);
28220 if (type_uses_auto (newtype
))
28222 // Substitute once to fix references to other template parameters.
28223 newtype
= tsubst (newtype
, tsubst_args
,
28224 complain
|tf_partial
, NULL_TREE
);
28225 // Now substitute again to reduce the level of the auto.
28226 newtype
= tsubst (newtype
, current_template_args (),
28227 complain
, NULL_TREE
);
28230 newtype
= tsubst (newtype
, tsubst_args
,
28231 complain
, NULL_TREE
);
28235 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28236 DECL_NAME (olddecl
), newtype
);
28237 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28240 if (TREE_CODE (olddecl
) == TYPE_DECL
28241 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28243 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28244 = build_template_parm_index (index
, level
, level
,
28246 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28247 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28248 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28249 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28250 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28252 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28254 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28256 DECL_TEMPLATE_RESULT (newdecl
)
28257 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28258 DECL_NAME (olddecl
), newtype
);
28259 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28260 // First create a copy (ttargs) of tsubst_args with an
28261 // additional level for the template template parameter's own
28262 // template parameters (ttparms).
28263 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28264 (DECL_TEMPLATE_PARMS (olddecl
)));
28265 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28266 tree ttargs
= make_tree_vec (depth
+ 1);
28267 for (int i
= 0; i
< depth
; ++i
)
28268 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28269 TREE_VEC_ELT (ttargs
, depth
)
28270 = template_parms_level_to_args (ttparms
);
28271 // Substitute ttargs into ttparms to fix references to
28272 // other template parameters.
28273 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28274 complain
|tf_partial
);
28275 // Now substitute again with args based on tparms, to reduce
28276 // the level of the ttparms.
28277 ttargs
= current_template_args ();
28278 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28280 // Finally, tack the adjusted parms onto tparms.
28281 ttparms
= tree_cons (size_int (depth
), ttparms
,
28282 current_template_parms
);
28283 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28288 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28290 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28291 TREE_CODE (oldconst
),
28292 DECL_NAME (oldconst
), newtype
);
28293 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28294 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28295 SET_DECL_TEMPLATE_PARM_P (newconst
);
28296 newidx
= build_template_parm_index (index
, level
, level
,
28297 newconst
, newtype
);
28298 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28299 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28300 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28306 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28307 template parameter. */
28310 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28311 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28313 tree olddecl
= TREE_VALUE (oldelt
);
28314 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28316 if (newdecl
== error_mark_node
)
28317 return error_mark_node
;
28318 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28319 targs
, complain
, NULL_TREE
);
28320 tree list
= build_tree_list (newdef
, newdecl
);
28321 TEMPLATE_PARM_CONSTRAINTS (list
)
28322 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28323 targs
, complain
, NULL_TREE
);
28324 int depth
= TMPL_ARGS_DEPTH (targs
);
28325 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28329 /* Returns a C++17 class deduction guide template based on the constructor
28330 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28331 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28332 aggregate initialization guide. */
28335 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28337 tree tparms
, targs
, fparms
, fargs
, ci
;
28338 bool memtmpl
= false;
28341 tree fn_tmpl
= NULL_TREE
;
28345 ++processing_template_decl
;
28346 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28347 --processing_template_decl
;
28350 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28354 bool copy_p
= TYPE_REF_P (ctor
);
28356 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28358 fparms
= void_list_node
;
28360 else if (TREE_CODE (ctor
) == TREE_LIST
)
28363 gcc_unreachable ();
28365 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28366 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28367 targs
= CLASSTYPE_TI_ARGS (type
);
28370 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28371 explicit_p
= false;
28375 ++processing_template_decl
;
28379 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28380 : DECL_TI_TEMPLATE (ctor
));
28382 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28383 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28385 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28386 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28387 fully specialized args for the enclosing class. Strip those off, as
28388 the deduction guide won't have those template parameters. */
28389 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28390 TMPL_PARMS_DEPTH (tparms
));
28391 /* Discard the 'this' parameter. */
28392 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28393 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28394 ci
= get_constraints (ctor
);
28395 loc
= DECL_SOURCE_LOCATION (ctor
);
28396 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28398 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28402 /* For a member template constructor, we need to flatten the two
28403 template parameter lists into one, and then adjust the function
28404 signature accordingly. This gets...complicated. */
28405 tree save_parms
= current_template_parms
;
28407 /* For a member template we should have two levels of parms/args, one
28408 for the class and one for the constructor. We stripped
28409 specialized args for further enclosing classes above. */
28410 const int depth
= 2;
28411 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28413 /* Template args for translating references to the two-level template
28414 parameters into references to the one-level template parameters we
28416 tree tsubst_args
= copy_node (targs
);
28417 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28418 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28420 /* Template parms for the constructor template. */
28421 tree ftparms
= TREE_VALUE (tparms
);
28422 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28423 /* Template parms for the class template. */
28424 tparms
= TREE_CHAIN (tparms
);
28425 tree ctparms
= TREE_VALUE (tparms
);
28426 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28427 /* Template parms for the deduction guide start as a copy of the
28428 template parms for the class. We set current_template_parms for
28429 lookup_template_class_1. */
28430 current_template_parms
= tparms
= copy_node (tparms
);
28431 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28432 for (unsigned i
= 0; i
< clen
; ++i
)
28433 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28435 /* Now we need to rewrite the constructor parms to append them to the
28437 for (unsigned i
= 0; i
< flen
; ++i
)
28439 unsigned index
= i
+ clen
;
28440 unsigned level
= 1;
28441 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28443 = rewrite_tparm_list (oldelt
, index
, level
,
28444 tsubst_args
, i
, complain
);
28445 if (newelt
== error_mark_node
)
28447 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28450 /* Now we have a final set of template parms to substitute into the
28451 function signature. */
28452 targs
= template_parms_to_args (tparms
);
28453 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28455 if (fparms
== error_mark_node
)
28458 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28460 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28461 cp_unevaluated_operand. */
28463 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28464 current_template_parms
= save_parms
;
28468 /* Substitute in the same arguments to rewrite class members into
28469 references to members of an unknown specialization. */
28471 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28472 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28474 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28477 --processing_template_decl
;
28479 return error_mark_node
;
28484 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28485 tparms
= copy_node (tparms
);
28486 INNERMOST_TEMPLATE_PARMS (tparms
)
28487 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28490 tree fntype
= build_function_type (type
, fparms
);
28491 tree ded_fn
= build_lang_decl_loc (loc
,
28493 dguide_name (type
), fntype
);
28494 DECL_ARGUMENTS (ded_fn
) = fargs
;
28495 DECL_ARTIFICIAL (ded_fn
) = true;
28496 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28497 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28498 DECL_ARTIFICIAL (ded_tmpl
) = true;
28499 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28500 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28502 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28504 set_constraints (ded_tmpl
, ci
);
28509 /* Add to LIST the member types for the reshaped initializer CTOR. */
28512 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28514 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28515 tree idx
, val
; unsigned i
;
28516 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28518 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28519 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28520 && CONSTRUCTOR_NELTS (val
)
28521 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28522 type gets a single initializer. */
28523 && CP_AGGREGATE_TYPE_P (ftype
)
28524 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28525 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28527 tree subelt
= NULL_TREE
;
28528 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28529 subelt
= TREE_TYPE (ftype
);
28530 list
= collect_ctor_idx_types (val
, list
, subelt
);
28533 tree arg
= NULL_TREE
;
28534 if (i
== v
->length() - 1
28535 && PACK_EXPANSION_P (ftype
))
28536 /* Give the trailing pack expansion parameter a default argument to
28537 match aggregate initialization behavior, even if we deduce the
28538 length of the pack separately to more than we have initializers. */
28539 arg
= build_constructor (init_list_type_node
, NULL
);
28540 /* if ei is of array type and xi is a braced-init-list or string literal,
28541 Ti is an rvalue reference to the declared type of ei */
28542 STRIP_ANY_LOCATION_WRAPPER (val
);
28543 if (TREE_CODE (ftype
) == ARRAY_TYPE
28544 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28545 || TREE_CODE (val
) == STRING_CST
))
28547 if (TREE_CODE (val
) == STRING_CST
)
28548 ftype
= cp_build_qualified_type
28549 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28550 ftype
= (cp_build_reference_type
28551 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28553 list
= tree_cons (arg
, ftype
, list
);
28559 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28562 is_spec_or_derived (tree etype
, tree tmpl
)
28564 if (!etype
|| !CLASS_TYPE_P (etype
))
28567 tree type
= TREE_TYPE (tmpl
);
28568 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28569 (DECL_TEMPLATE_PARMS (tmpl
)));
28570 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28571 int err
= unify (tparms
, targs
, type
, etype
,
28572 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28577 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28581 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28583 if (cxx_dialect
< cxx20
)
28586 if (init
== NULL_TREE
)
28589 tree type
= TREE_TYPE (tmpl
);
28590 if (!CP_AGGREGATE_TYPE_P (type
))
28593 /* No aggregate candidate for copy-initialization. */
28594 if (args
->length() == 1)
28596 tree val
= (*args
)[0];
28597 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28601 /* If we encounter a problem, we just won't add the candidate. */
28602 tsubst_flags_t complain
= tf_none
;
28604 tree parms
= NULL_TREE
;
28605 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28607 init
= reshape_init (type
, init
, complain
);
28608 if (init
== error_mark_node
)
28610 parms
= collect_ctor_idx_types (init
, parms
);
28612 else if (TREE_CODE (init
) == TREE_LIST
)
28614 int len
= list_length (init
);
28615 for (tree field
= TYPE_FIELDS (type
);
28617 --len
, field
= DECL_CHAIN (field
))
28619 field
= next_initializable_field (field
);
28622 tree ftype
= finish_decltype_type (field
, true, complain
);
28623 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28627 /* Aggregate initialization doesn't apply to an initializer expression. */
28633 parms
= nreverse (parms
);
28634 TREE_CHAIN (last
) = void_list_node
;
28635 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28642 /* UGUIDES are the deduction guides for the underlying template of alias
28643 template TMPL; adjust them to be deduction guides for TMPL. */
28646 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28648 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28649 class type (9.2.8.2) where the template-name names an alias template A,
28650 the defining-type-id of A must be of the form
28652 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28654 as specified in 9.2.8.2. The guides of A are the set of functions or
28655 function templates formed as follows. For each function or function
28656 template f in the guides of the template named by the simple-template-id
28657 of the defining-type-id, the template arguments of the return type of f
28658 are deduced from the defining-type-id of A according to the process in
28659 13.10.2.5 with the exception that deduction does not fail if not all
28660 template arguments are deduced. Let g denote the result of substituting
28661 these deductions into f. If substitution succeeds, form a function or
28662 function template f' with the following properties and add it to the set
28665 * The function type of f' is the function type of g.
28667 * If f is a function template, f' is a function template whose template
28668 parameter list consists of all the template parameters of A (including
28669 their default template arguments) that appear in the above deductions or
28670 (recursively) in their default template arguments, followed by the
28671 template parameters of f that were not deduced (including their default
28672 template arguments), otherwise f' is not a function template.
28674 * The associated constraints (13.5.2) are the conjunction of the
28675 associated constraints of g and a constraint that is satisfied if and only
28676 if the arguments of A are deducible (see below) from the return type.
28678 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28681 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28682 considered to be so as well.
28684 * The explicit-specifier of f' is the explicit-specifier of g (if
28687 /* This implementation differs from the above in two significant ways:
28689 1) We include all template parameters of A, not just some.
28690 2) The added constraint is same_type instead of deducible.
28692 I believe that while it's probably possible to construct a testcase that
28693 behaves differently with this simplification, it should have the same
28694 effect for real uses. Including all template parameters means that we
28695 deduce all parameters of A when resolving the call, so when we're in the
28696 constraint we don't need to deduce them again, we can just check whether
28697 the deduction produced the desired result. */
28699 tsubst_flags_t complain
= tf_warning_or_error
;
28700 tree atype
= TREE_TYPE (tmpl
);
28701 tree aguides
= NULL_TREE
;
28702 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28703 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28704 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28705 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28709 location_t loc
= DECL_SOURCE_LOCATION (f
);
28710 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28712 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28714 processing_template_decl_sentinel
ptds (/*reset*/false);
28715 ++processing_template_decl
;
28717 /* Deduce template arguments for f from the type-id of A. */
28718 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28719 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28720 tree targs
= make_tree_vec (len
);
28721 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28724 /* The number of parms for f' is the number of parms for A plus
28725 non-deduced parms of f. */
28726 unsigned ndlen
= 0;
28728 for (unsigned i
= 0; i
< len
; ++i
)
28729 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28731 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28733 /* First copy over the parms of A. */
28734 for (j
= 0; j
< natparms
; ++j
)
28735 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28736 /* Now rewrite the non-deduced parms of f. */
28737 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28738 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28741 unsigned index
= j
++;
28742 unsigned level
= 1;
28743 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28744 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28745 targs
, i
, complain
);
28746 TREE_VEC_ELT (gtparms
, index
) = list
;
28748 gtparms
= build_tree_list (size_one_node
, gtparms
);
28750 /* Substitute the deduced arguments plus the rewritten template
28751 parameters into f to get g. This covers the type, copyness,
28752 guideness, and explicit-specifier. */
28753 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28754 if (g
== error_mark_node
)
28755 return error_mark_node
;
28756 DECL_USE_TEMPLATE (g
) = 0;
28757 fprime
= build_template_decl (g
, gtparms
, false);
28758 DECL_TEMPLATE_RESULT (fprime
) = g
;
28759 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28760 tree gtargs
= template_parms_to_args (gtparms
);
28761 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28762 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28764 /* Substitute the associated constraints. */
28765 tree ci
= get_constraints (f
);
28767 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28768 if (ci
== error_mark_node
)
28769 return error_mark_node
;
28771 /* Add a constraint that the return type matches the instantiation of
28772 A with the same template arguments. */
28773 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28774 if (!same_type_p (atype
, ret
)
28775 /* FIXME this should mean they don't compare as equivalent. */
28776 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28778 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28779 ci
= append_constraint (ci
, same
);
28784 remove_constraints (fprime
);
28785 set_constraints (fprime
, ci
);
28790 /* For a non-template deduction guide, if the arguments of A aren't
28791 deducible from the return type, don't add the candidate. */
28792 tree targs
= make_tree_vec (natparms
);
28793 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28794 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28795 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28801 aguides
= lookup_add (fprime
, aguides
);
28807 /* Return artificial deduction guides built from the constructors of class
28811 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28813 tree type
= TREE_TYPE (tmpl
);
28814 tree outer_args
= NULL_TREE
;
28815 if (DECL_CLASS_SCOPE_P (tmpl
)
28816 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28818 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28819 type
= TREE_TYPE (most_general_template (tmpl
));
28822 tree cands
= NULL_TREE
;
28824 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28826 /* Skip inherited constructors. */
28827 if (iter
.using_p ())
28830 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28831 cands
= lookup_add (guide
, cands
);
28834 /* Add implicit default constructor deduction guide. */
28835 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28837 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28839 cands
= lookup_add (guide
, cands
);
28842 /* Add copy guide. */
28844 tree gtype
= build_reference_type (type
);
28845 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28847 cands
= lookup_add (guide
, cands
);
28853 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28855 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28856 aggregate candidate is added separately because it depends on the
28857 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28861 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
28863 tree guides
= NULL_TREE
;
28864 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28866 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28867 tree tinfo
= get_template_info (under
);
28868 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
28873 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28874 dguide_name (tmpl
),
28875 LOOK_want::NORMAL
, /*complain*/false);
28876 if (guides
== error_mark_node
)
28877 guides
= NULL_TREE
;
28879 any_dguides_p
= true;
28882 /* Cache the deduction guides for a template. We also remember the result of
28883 lookup, and rebuild everything if it changes; should be very rare. */
28884 tree_pair_p cache
= NULL
;
28886 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28889 if (cache
->purpose
== guides
)
28890 return cache
->value
;
28894 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28895 cache
->purpose
= guides
;
28898 tree cands
= NULL_TREE
;
28899 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28900 cands
= alias_ctad_tweaks (tmpl
, guides
);
28903 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28904 for (ovl_iterator
it (guides
); it
; ++it
)
28905 cands
= lookup_add (*it
, cands
);
28908 cache
->value
= cands
;
28912 /* Return whether TMPL is a (class template argument-) deducible template. */
28915 ctad_template_p (tree tmpl
)
28917 /* A deducible template is either a class template or is an alias template
28918 whose defining-type-id is of the form
28920 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28922 where the nested-name-specifier (if any) is non-dependent and the
28923 template-name of the simple-template-id names a deducible template. */
28925 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28926 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28928 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28930 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28931 if (tree tinfo
= get_template_info (orig
))
28932 return ctad_template_p (TI_TEMPLATE (tinfo
));
28936 /* Deduce template arguments for the class template placeholder PTYPE for
28937 template TMPL based on the initializer INIT, and return the resulting
28941 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28942 int flags
, tsubst_flags_t complain
)
28944 /* We should have handled this in the caller. */
28945 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28948 /* Look through alias templates that just rename another template. */
28949 tmpl
= get_underlying_template (tmpl
);
28950 if (!ctad_template_p (tmpl
))
28952 if (complain
& tf_error
)
28953 error ("non-deducible template %qT used without template arguments", tmpl
);
28954 return error_mark_node
;
28956 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28958 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28959 if (complain
& tf_warning_or_error
)
28960 pedwarn (input_location
, 0, "alias template deduction only available "
28961 "with %<-std=c++20%> or %<-std=gnu++20%>");
28964 if (init
&& TREE_TYPE (init
) == ptype
)
28965 /* Using the template parm as its own argument. */
28968 tree type
= TREE_TYPE (tmpl
);
28970 bool try_list_ctor
= false;
28972 releasing_vec rv_args
= NULL
;
28973 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28974 if (init
== NULL_TREE
)
28975 args
= make_tree_vector ();
28976 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28978 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28979 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28981 /* As an exception, the first phase in 16.3.1.7 (considering the
28982 initializer list as a single argument) is omitted if the
28983 initializer list consists of a single expression of type cv U,
28984 where U is a specialization of C or a class derived from a
28985 specialization of C. */
28986 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28987 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28988 try_list_ctor
= false;
28990 if (try_list_ctor
|| is_std_init_list (type
))
28991 args
= make_tree_vector_single (init
);
28993 args
= make_tree_vector_from_ctor (init
);
28995 else if (TREE_CODE (init
) == TREE_LIST
)
28996 args
= make_tree_vector_from_list (init
);
28998 args
= make_tree_vector_single (init
);
29000 /* Do this now to avoid problems with erroneous args later on. */
29001 args
= resolve_args (args
, complain
);
29003 return error_mark_node
;
29005 bool any_dguides_p
= false;
29006 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29007 if (cands
== error_mark_node
)
29008 return error_mark_node
;
29010 /* Prune explicit deduction guides in copy-initialization context. */
29011 bool elided
= false;
29012 if (flags
& LOOKUP_ONLYCONVERTING
)
29014 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29015 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29020 /* Found a nonconverting guide, prune the candidates. */
29021 tree pruned
= NULL_TREE
;
29022 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29023 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29024 pruned
= lookup_add (*iter
, pruned
);
29030 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29031 cands
= lookup_add (guide
, cands
);
29033 tree call
= error_mark_node
;
29035 /* If this is list-initialization and the class has a list constructor, first
29036 try deducing from the list as a single argument, as [over.match.list]. */
29037 tree list_cands
= NULL_TREE
;
29038 if (try_list_ctor
&& cands
)
29039 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29042 if (is_list_ctor (dg
))
29043 list_cands
= lookup_add (dg
, list_cands
);
29047 ++cp_unevaluated_operand
;
29048 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29049 --cp_unevaluated_operand
;
29051 if (call
== error_mark_node
)
29053 /* That didn't work, now try treating the list as a sequence of
29055 release_tree_vector (args
);
29056 args
= make_tree_vector_from_ctor (init
);
29060 if (elided
&& !cands
)
29062 error ("cannot deduce template arguments for copy-initialization"
29063 " of %qT, as it has no non-explicit deduction guides or "
29064 "user-declared constructors", type
);
29065 return error_mark_node
;
29067 else if (!cands
&& call
== error_mark_node
)
29069 error ("cannot deduce template arguments of %qT, as it has no viable "
29070 "deduction guides", type
);
29071 return error_mark_node
;
29074 if (call
== error_mark_node
)
29076 ++cp_unevaluated_operand
;
29077 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29078 --cp_unevaluated_operand
;
29081 if (call
== error_mark_node
29082 && (complain
& tf_warning_or_error
))
29084 error ("class template argument deduction failed:");
29086 ++cp_unevaluated_operand
;
29087 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
29088 --cp_unevaluated_operand
;
29091 inform (input_location
, "explicit deduction guides not considered "
29092 "for copy-initialization");
29095 /* If CTAD succeeded but the type doesn't have any explicit deduction
29096 guides, this deduction might not be what the user intended. */
29097 if (call
!= error_mark_node
&& !any_dguides_p
)
29099 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29100 if (fndecl
!= NULL_TREE
29101 && (!DECL_IN_SYSTEM_HEADER (fndecl
)
29102 || global_dc
->dc_warn_system_headers
)
29103 && warning (OPT_Wctad_maybe_unsupported
,
29104 "%qT may not intend to support class template argument "
29105 "deduction", type
))
29106 inform (input_location
, "add a deduction guide to suppress this "
29110 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29113 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29114 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29115 The CONTEXT determines the context in which auto deduction is performed
29116 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29117 OUTER_TARGS are used during template argument deduction
29118 (context == adc_unify) to properly substitute the result, and is ignored
29121 For partial-concept-ids, extra args may be appended to the list of deduced
29122 template arguments prior to determining constraint satisfaction. */
29125 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29126 tsubst_flags_t complain
, auto_deduction_context context
,
29127 tree outer_targs
, int flags
)
29131 if (init
== error_mark_node
)
29132 return error_mark_node
;
29134 if (init
&& type_dependent_expression_p (init
)
29135 && context
!= adc_unify
)
29136 /* Defining a subset of type-dependent expressions that we can deduce
29137 from ahead of time isn't worth the trouble. */
29140 /* Similarly, we can't deduce from another undeduced decl. */
29141 if (init
&& undeduced_auto_decl (init
))
29144 /* We may be doing a partial substitution, but we still want to replace
29146 complain
&= ~tf_partial
;
29148 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29149 /* C++17 class template argument deduction. */
29150 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29152 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29153 /* Nothing we can do with this, even in deduction context. */
29156 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29157 with either a new invented type template parameter U or, if the
29158 initializer is a braced-init-list (8.5.4), with
29159 std::initializer_list<U>. */
29160 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29162 if (!DIRECT_LIST_INIT_P (init
))
29163 type
= listify_autos (type
, auto_node
);
29164 else if (CONSTRUCTOR_NELTS (init
) == 1)
29165 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29168 if (complain
& tf_warning_or_error
)
29170 if (permerror (input_location
, "direct-list-initialization of "
29171 "%<auto%> requires exactly one element"))
29172 inform (input_location
,
29173 "for deduction to %<std::initializer_list%>, use copy-"
29174 "list-initialization (i.e. add %<=%> before the %<{%>)");
29176 type
= listify_autos (type
, auto_node
);
29180 if (type
== error_mark_node
)
29181 return error_mark_node
;
29183 init
= resolve_nondeduced_context (init
, complain
);
29185 if (context
== adc_decomp_type
29186 && auto_node
== type
29187 && init
!= error_mark_node
29188 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29189 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29190 and initializer has array type, deduce cv-qualified array type. */
29191 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29193 else if (AUTO_IS_DECLTYPE (auto_node
))
29195 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29196 bool id
= (DECL_P (stripped_init
)
29197 || ((TREE_CODE (init
) == COMPONENT_REF
29198 || TREE_CODE (init
) == SCOPE_REF
)
29199 && !REF_PARENTHESIZED_P (init
)));
29200 targs
= make_tree_vec (1);
29201 TREE_VEC_ELT (targs
, 0)
29202 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29203 if (type
!= auto_node
)
29205 if (complain
& tf_error
)
29206 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29207 return error_mark_node
;
29209 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29211 if (complain
& tf_error
)
29212 error ("%<decltype(auto)%> cannot be cv-qualified");
29213 return error_mark_node
;
29218 if (error_operand_p (init
))
29219 return error_mark_node
;
29221 tree parms
= build_tree_list (NULL_TREE
, type
);
29225 tparms
= extract_autos (type
);
29228 tparms
= make_tree_vec (1);
29229 TREE_VEC_ELT (tparms
, 0)
29230 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29233 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29234 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29236 NULL
, /*explain_p=*/false);
29239 if (processing_template_decl
)
29240 /* Try again at instantiation time. */
29242 if (type
&& type
!= error_mark_node
29243 && (complain
& tf_error
))
29244 /* If type is error_mark_node a diagnostic must have been
29245 emitted by now. Also, having a mention to '<type error>'
29246 in the diagnostic is not really useful to the user. */
29249 && FNDECL_USED_AUTO (current_function_decl
)
29251 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29252 && LAMBDA_FUNCTION_P (current_function_decl
))
29253 error ("unable to deduce lambda return type from %qE", init
);
29255 error ("unable to deduce %qT from %qE", type
, init
);
29256 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29258 NULL
, /*explain_p=*/true);
29260 return error_mark_node
;
29264 /* Check any placeholder constraints against the deduced type. */
29265 if (flag_concepts
&& !processing_template_decl
)
29266 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29268 /* Use the deduced type to check the associated constraints. If we
29269 have a partial-concept-id, rebuild the argument list so that
29270 we check using the extra arguments. */
29271 check
= unpack_concept_check (check
);
29272 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29273 tree
cdecl = TREE_OPERAND (check
, 0);
29275 cdecl = OVL_FIRST (cdecl);
29276 tree cargs
= TREE_OPERAND (check
, 1);
29277 if (TREE_VEC_LENGTH (cargs
) > 1)
29279 cargs
= copy_node (cargs
);
29280 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29285 /* Rebuild the check using the deduced arguments. */
29286 check
= build_concept_check (cdecl, cargs
, tf_none
);
29288 if (!constraints_satisfied_p (check
))
29290 if (complain
& tf_warning_or_error
)
29292 auto_diagnostic_group d
;
29295 case adc_unspecified
:
29297 error("placeholder constraints not satisfied");
29299 case adc_variable_type
:
29300 case adc_decomp_type
:
29301 error ("deduced initializer does not satisfy "
29302 "placeholder constraints");
29304 case adc_return_type
:
29305 error ("deduced return type does not satisfy "
29306 "placeholder constraints");
29308 case adc_requirement
:
29309 error ("deduced expression type does not satisfy "
29310 "placeholder constraints");
29313 diagnose_constraints (input_location
, check
, targs
);
29315 return error_mark_node
;
29319 if (processing_template_decl
&& context
!= adc_unify
)
29320 outer_targs
= current_template_args ();
29321 targs
= add_to_template_args (outer_targs
, targs
);
29322 return tsubst (type
, targs
, complain
, NULL_TREE
);
29325 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29329 splice_late_return_type (tree type
, tree late_return_type
)
29331 if (late_return_type
)
29333 gcc_assert (is_auto (type
) || seen_error ());
29334 return late_return_type
;
29337 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29339 tree idx
= get_template_parm_index (*auto_node
);
29340 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29342 /* In an abbreviated function template we didn't know we were dealing
29343 with a function template when we saw the auto return type, so update
29344 it to have the correct level. */
29345 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29346 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29347 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29348 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29349 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29350 *auto_node
= new_auto
;
29356 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29357 'decltype(auto)' or a deduced class template. */
29360 is_auto (const_tree type
)
29362 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29363 && (TYPE_IDENTIFIER (type
) == auto_identifier
29364 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29370 /* for_each_template_parm callback for type_uses_auto. */
29373 is_auto_r (tree tp
, void */
*data*/
)
29375 return is_auto (tp
);
29378 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29379 a use of `auto'. Returns NULL_TREE otherwise. */
29382 type_uses_auto (tree type
)
29384 if (type
== NULL_TREE
)
29386 else if (flag_concepts
)
29388 /* The Concepts TS allows multiple autos in one type-specifier; just
29389 return the first one we find, do_auto_deduction will collect all of
29391 if (uses_template_parms (type
))
29392 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29393 /*visited*/NULL
, /*nondeduced*/false);
29397 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29403 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29404 concepts are enabled, auto is acceptable in template arguments, but
29405 only when TEMPL identifies a template class. Return TRUE if any
29406 such errors were reported. */
29409 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29411 /* If there were previous errors, nevermind. */
29412 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29415 /* If TMPL is an identifier, we're parsing and we can't tell yet
29416 whether TMPL is supposed to be a type, a function or a variable.
29417 We'll only be able to tell during template substitution, so we
29418 expect to be called again then. If concepts are enabled and we
29419 know we have a type, we're ok. */
29421 && (identifier_p (tmpl
)
29423 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29424 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29427 /* Quickly search for any occurrences of auto; usually there won't
29428 be any, and then we'll avoid allocating the vector. */
29429 if (!type_uses_auto (args
))
29432 bool errors
= false;
29434 tree vec
= extract_autos (args
);
29435 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29437 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29438 error_at (DECL_SOURCE_LOCATION (xauto
),
29439 "invalid use of %qT in template argument", xauto
);
29446 /* Recursively walk over && expressions searching for EXPR. Return a reference
29447 to that expression. */
29449 static tree
*find_template_requirement (tree
*t
, tree key
)
29453 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29455 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29457 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29463 /* Convert the generic type parameters in PARM that match the types given in the
29464 range [START_IDX, END_IDX) from the current_template_parms into generic type
29468 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29470 tree current
= current_template_parms
;
29471 int depth
= TMPL_PARMS_DEPTH (current
);
29472 current
= INNERMOST_TEMPLATE_PARMS (current
);
29473 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29475 for (int i
= 0; i
< start_idx
; ++i
)
29476 TREE_VEC_ELT (replacement
, i
)
29477 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29479 for (int i
= start_idx
; i
< end_idx
; ++i
)
29481 /* Create a distinct parameter pack type from the current parm and add it
29482 to the replacement args to tsubst below into the generic function
29484 tree node
= TREE_VEC_ELT (current
, i
);
29485 tree o
= TREE_TYPE (TREE_VALUE (node
));
29486 tree t
= copy_type (o
);
29487 TEMPLATE_TYPE_PARM_INDEX (t
)
29488 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29490 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29491 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29492 TYPE_MAIN_VARIANT (t
) = t
;
29493 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29494 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29495 TREE_VEC_ELT (replacement
, i
) = t
;
29497 /* Replace the current template parameter with new pack. */
29498 TREE_VALUE (node
) = TREE_CHAIN (t
);
29500 /* Surgically adjust the associated constraint of adjusted parameter
29501 and it's corresponding contribution to the current template
29503 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29505 tree id
= unpack_concept_check (constr
);
29506 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
29507 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29508 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29510 /* If there was a constraint, we also need to replace that in
29511 the template requirements, which we've already built. */
29512 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29513 reqs
= find_template_requirement (reqs
, constr
);
29518 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29519 TREE_VEC_ELT (replacement
, i
)
29520 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29522 /* If there are more levels then build up the replacement with the outer
29525 replacement
= add_to_template_args (template_parms_to_args
29526 (TREE_CHAIN (current_template_parms
)),
29529 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29532 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29536 declare_integer_pack (void)
29538 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29539 build_function_type_list (integer_type_node
,
29542 NULL_TREE
, ECF_CONST
);
29543 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29544 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29545 CP_BUILT_IN_INTEGER_PACK
);
29548 /* Set up the hash tables for template instantiations. */
29551 init_template_processing (void)
29553 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29554 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29556 if (cxx_dialect
>= cxx11
)
29557 declare_integer_pack ();
29560 /* Print stats about the template hash tables for -fstats. */
29563 print_template_statistics (void)
29565 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29566 "%f collisions\n", (long) decl_specializations
->size (),
29567 (long) decl_specializations
->elements (),
29568 decl_specializations
->collisions ());
29569 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29570 "%f collisions\n", (long) type_specializations
->size (),
29571 (long) type_specializations
->elements (),
29572 type_specializations
->collisions ());
29577 namespace selftest
{
29579 /* Verify that build_non_dependent_expr () works, for various expressions,
29580 and that location wrappers don't affect the results. */
29583 test_build_non_dependent_expr ()
29585 location_t loc
= BUILTINS_LOCATION
;
29587 /* Verify constants, without and with location wrappers. */
29588 tree int_cst
= build_int_cst (integer_type_node
, 42);
29589 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29591 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29592 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29593 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29595 tree string_lit
= build_string (4, "foo");
29596 TREE_TYPE (string_lit
) = char_array_type_node
;
29597 string_lit
= fix_string_type (string_lit
);
29598 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29600 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29601 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29602 ASSERT_EQ (wrapped_string_lit
,
29603 build_non_dependent_expr (wrapped_string_lit
));
29606 /* Verify that type_dependent_expression_p () works correctly, even
29607 in the presence of location wrapper nodes. */
29610 test_type_dependent_expression_p ()
29612 location_t loc
= BUILTINS_LOCATION
;
29614 tree name
= get_identifier ("foo");
29616 /* If no templates are involved, nothing is type-dependent. */
29617 gcc_assert (!processing_template_decl
);
29618 ASSERT_FALSE (type_dependent_expression_p (name
));
29620 ++processing_template_decl
;
29622 /* Within a template, an unresolved name is always type-dependent. */
29623 ASSERT_TRUE (type_dependent_expression_p (name
));
29625 /* Ensure it copes with NULL_TREE and errors. */
29626 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29627 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29629 /* A USING_DECL in a template should be type-dependent, even if wrapped
29630 with a location wrapper (PR c++/83799). */
29631 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29632 TREE_TYPE (using_decl
) = integer_type_node
;
29633 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29634 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29635 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29636 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29638 --processing_template_decl
;
29641 /* Run all of the selftests within this file. */
29646 test_build_non_dependent_expr ();
29647 test_type_dependent_expression_p ();
29650 } // namespace selftest
29652 #endif /* #if CHECKING_P */
29654 #include "gt-cp-pt.h"