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
);
231 /* Make the current scope suitable for access checking when we are
232 processing T. T can be FUNCTION_DECL for instantiated function
233 template, VAR_DECL for static member variable, or TYPE_DECL for
234 alias template (needed by instantiate_decl). */
237 push_access_scope (tree t
)
239 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
240 || TREE_CODE (t
) == TYPE_DECL
);
242 if (DECL_FRIEND_CONTEXT (t
))
243 push_nested_class (DECL_FRIEND_CONTEXT (t
));
244 else if (DECL_CLASS_SCOPE_P (t
))
245 push_nested_class (DECL_CONTEXT (t
));
247 push_to_top_level ();
249 if (TREE_CODE (t
) == FUNCTION_DECL
)
251 vec_safe_push (saved_access_scope
, current_function_decl
);
252 current_function_decl
= t
;
256 /* Restore the scope set up by push_access_scope. T is the node we
260 pop_access_scope (tree t
)
262 if (TREE_CODE (t
) == FUNCTION_DECL
)
263 current_function_decl
= saved_access_scope
->pop();
265 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
277 finish_member_template_decl (tree decl
)
279 if (decl
== error_mark_node
)
280 return error_mark_node
;
282 gcc_assert (DECL_P (decl
));
284 if (TREE_CODE (decl
) == TYPE_DECL
)
288 type
= TREE_TYPE (decl
);
289 if (type
== error_mark_node
)
290 return error_mark_node
;
291 if (MAYBE_CLASS_TYPE_P (type
)
292 && CLASSTYPE_TEMPLATE_INFO (type
)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
295 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
296 check_member_template (tmpl
);
301 else if (TREE_CODE (decl
) == FIELD_DECL
)
302 error_at (DECL_SOURCE_LOCATION (decl
),
303 "data member %qD cannot be a member template", decl
);
304 else if (DECL_TEMPLATE_INFO (decl
))
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
308 check_member_template (DECL_TI_TEMPLATE (decl
));
309 return DECL_TI_TEMPLATE (decl
);
315 error_at (DECL_SOURCE_LOCATION (decl
),
316 "invalid member template declaration %qD", decl
);
318 return error_mark_node
;
321 /* Create a template info node. */
324 build_template_info (tree template_decl
, tree template_args
)
326 tree result
= make_node (TEMPLATE_INFO
);
327 TI_TEMPLATE (result
) = template_decl
;
328 TI_ARGS (result
) = template_args
;
332 /* Return the template info node corresponding to T, whatever T is. */
335 get_template_info (const_tree t
)
337 tree tinfo
= NULL_TREE
;
339 if (!t
|| t
== error_mark_node
)
342 if (TREE_CODE (t
) == NAMESPACE_DECL
343 || TREE_CODE (t
) == PARM_DECL
)
346 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
347 tinfo
= DECL_TEMPLATE_INFO (t
);
349 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
352 if (OVERLOAD_TYPE_P (t
))
353 tinfo
= TYPE_TEMPLATE_INFO (t
);
354 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
355 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
360 /* Returns the template nesting level of the indicated class TYPE.
370 A<T>::B<U> has depth two, while A<T> has depth one.
371 Both A<T>::B<int> and A<int>::B<U> have depth one, if
372 they are instantiations, not specializations.
374 This function is guaranteed to return 0 if passed NULL_TREE so
375 that, for example, `template_class_depth (current_class_type)' is
379 template_class_depth (tree type
)
383 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
385 tree tinfo
= get_template_info (type
);
387 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
388 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
393 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
396 type
= CP_DECL_CONTEXT (type
);
398 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
399 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
401 type
= CP_TYPE_CONTEXT (type
);
407 /* Return TRUE if NODE instantiates a template that has arguments of
408 its own, be it directly a primary template or indirectly through a
409 partial specializations. */
411 instantiates_primary_template_p (tree node
)
413 tree tinfo
= get_template_info (node
);
417 tree tmpl
= TI_TEMPLATE (tinfo
);
418 if (PRIMARY_TEMPLATE_P (tmpl
))
421 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
424 /* So now we know we have a specialization, but it could be a full
425 or a partial specialization. To tell which, compare the depth of
426 its template arguments with those of its context. */
428 tree ctxt
= DECL_CONTEXT (tmpl
);
429 tree ctinfo
= get_template_info (ctxt
);
433 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
434 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
437 /* Subroutine of maybe_begin_member_template_processing.
438 Returns true if processing DECL needs us to push template parms. */
441 inline_needs_template_parms (tree decl
, bool nsdmi
)
443 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
446 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
447 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
450 /* Subroutine of maybe_begin_member_template_processing.
451 Push the template parms in PARMS, starting from LEVELS steps into the
452 chain, and ending at the beginning, since template parms are listed
456 push_inline_template_parms_recursive (tree parmlist
, int levels
)
458 tree parms
= TREE_VALUE (parmlist
);
462 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
464 ++processing_template_decl
;
465 current_template_parms
466 = tree_cons (size_int (processing_template_decl
),
467 parms
, current_template_parms
);
468 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
470 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
472 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
474 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
476 if (error_operand_p (parm
))
479 gcc_assert (DECL_P (parm
));
481 switch (TREE_CODE (parm
))
489 /* Push the CONST_DECL. */
490 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
499 /* Restore the template parameter context for a member template, a
500 friend template defined in a class definition, or a non-template
501 member of template class. */
504 maybe_begin_member_template_processing (tree decl
)
508 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
512 tree ctx
= DECL_CONTEXT (decl
);
513 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
514 /* Disregard full specializations (c++/60999). */
515 && uses_template_parms (ctx
)
516 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
519 if (inline_needs_template_parms (decl
, nsdmi
))
521 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
522 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
524 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
527 parms
= TREE_CHAIN (parms
);
530 push_inline_template_parms_recursive (parms
, levels
);
533 /* Remember how many levels of template parameters we pushed so that
534 we can pop them later. */
535 inline_parm_levels
.safe_push (levels
);
538 /* Undo the effects of maybe_begin_member_template_processing. */
541 maybe_end_member_template_processing (void)
546 if (inline_parm_levels
.length () == 0)
549 last
= inline_parm_levels
.pop ();
550 for (i
= 0; i
< last
; ++i
)
552 --processing_template_decl
;
553 current_template_parms
= TREE_CHAIN (current_template_parms
);
558 /* Return a new template argument vector which contains all of ARGS,
559 but has as its innermost set of arguments the EXTRA_ARGS. */
562 add_to_template_args (tree args
, tree extra_args
)
569 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
572 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
573 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
575 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
576 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
578 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
579 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
584 /* Like add_to_template_args, but only the outermost ARGS are added to
585 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
586 (EXTRA_ARGS) levels are added. This function is used to combine
587 the template arguments from a partial instantiation with the
588 template arguments used to attain the full instantiation from the
589 partial instantiation. */
592 add_outermost_template_args (tree args
, tree extra_args
)
596 /* If there are more levels of EXTRA_ARGS than there are ARGS,
597 something very fishy is going on. */
598 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
600 /* If *all* the new arguments will be the EXTRA_ARGS, just return
602 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
605 /* For the moment, we make ARGS look like it contains fewer levels. */
606 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
608 new_args
= add_to_template_args (args
, extra_args
);
610 /* Now, we restore ARGS to its full dimensions. */
611 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
616 /* Return the N levels of innermost template arguments from the ARGS. */
619 get_innermost_template_args (tree args
, int n
)
627 /* If N is 1, just return the innermost set of template arguments. */
629 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
631 /* If we're not removing anything, just return the arguments we were
633 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
634 gcc_assert (extra_levels
>= 0);
635 if (extra_levels
== 0)
638 /* Make a new set of arguments, not containing the outer arguments. */
639 new_args
= make_tree_vec (n
);
640 for (i
= 1; i
<= n
; ++i
)
641 SET_TMPL_ARGS_LEVEL (new_args
, i
,
642 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
647 /* The inverse of get_innermost_template_args: Return all but the innermost
648 EXTRA_LEVELS levels of template arguments from the ARGS. */
651 strip_innermost_template_args (tree args
, int extra_levels
)
654 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
659 /* If N is 1, just return the outermost set of template arguments. */
661 return TMPL_ARGS_LEVEL (args
, 1);
663 /* If we're not removing anything, just return the arguments we were
665 gcc_assert (extra_levels
>= 0);
666 if (extra_levels
== 0)
669 /* Make a new set of arguments, not containing the inner arguments. */
670 new_args
= make_tree_vec (n
);
671 for (i
= 1; i
<= n
; ++i
)
672 SET_TMPL_ARGS_LEVEL (new_args
, i
,
673 TMPL_ARGS_LEVEL (args
, i
));
678 /* We've got a template header coming up; push to a new level for storing
682 begin_template_parm_list (void)
684 /* We use a non-tag-transparent scope here, which causes pushtag to
685 put tags in this scope, rather than in the enclosing class or
686 namespace scope. This is the right thing, since we want
687 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
688 global template class, push_template_decl handles putting the
689 TEMPLATE_DECL into top-level scope. For a nested template class,
692 template <class T> struct S1 {
693 template <class T> struct S2 {};
696 pushtag contains special code to insert the TEMPLATE_DECL for S2
697 at the right scope. */
698 begin_scope (sk_template_parms
, NULL
);
699 ++processing_template_decl
;
700 ++processing_template_parmlist
;
701 note_template_header (0);
703 /* Add a dummy parameter level while we process the parameter list. */
704 current_template_parms
705 = tree_cons (size_int (processing_template_decl
),
707 current_template_parms
);
710 /* This routine is called when a specialization is declared. If it is
711 invalid to declare a specialization here, an error is reported and
712 false is returned, otherwise this routine will return true. */
715 check_specialization_scope (void)
717 tree scope
= current_scope ();
721 An explicit specialization shall be declared in the namespace of
722 which the template is a member, or, for member templates, in the
723 namespace of which the enclosing class or enclosing class
724 template is a member. An explicit specialization of a member
725 function, member class or static data member of a class template
726 shall be declared in the namespace of which the class template
728 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
730 error ("explicit specialization in non-namespace scope %qD", scope
);
736 In an explicit specialization declaration for a member of a class
737 template or a member template that appears in namespace scope,
738 the member template and some of its enclosing class templates may
739 remain unspecialized, except that the declaration shall not
740 explicitly specialize a class member template if its enclosing
741 class templates are not explicitly specialized as well. */
742 if (current_template_parms
)
744 error ("enclosing class templates are not explicitly specialized");
751 /* We've just seen template <>. */
754 begin_specialization (void)
756 begin_scope (sk_template_spec
, NULL
);
757 note_template_header (1);
758 return check_specialization_scope ();
761 /* Called at then end of processing a declaration preceded by
765 end_specialization (void)
768 reset_specialization ();
771 /* Any template <>'s that we have seen thus far are not referring to a
772 function specialization. */
775 reset_specialization (void)
777 processing_specialization
= 0;
778 template_header_count
= 0;
781 /* We've just seen a template header. If SPECIALIZATION is nonzero,
782 it was of the form template <>. */
785 note_template_header (int specialization
)
787 processing_specialization
= specialization
;
788 template_header_count
++;
791 /* We're beginning an explicit instantiation. */
794 begin_explicit_instantiation (void)
796 gcc_assert (!processing_explicit_instantiation
);
797 processing_explicit_instantiation
= true;
802 end_explicit_instantiation (void)
804 gcc_assert (processing_explicit_instantiation
);
805 processing_explicit_instantiation
= false;
808 /* An explicit specialization or partial specialization of TMPL is being
809 declared. Check that the namespace in which the specialization is
810 occurring is permissible. Returns false iff it is invalid to
811 specialize TMPL in the current namespace. */
814 check_specialization_namespace (tree tmpl
)
816 tree tpl_ns
= decl_namespace_context (tmpl
);
820 An explicit specialization shall be declared in a namespace enclosing the
821 specialized template. An explicit specialization whose declarator-id is
822 not qualified shall be declared in the nearest enclosing namespace of the
823 template, or, if the namespace is inline (7.3.1), any namespace from its
824 enclosing namespace set. */
825 if (current_scope() != DECL_CONTEXT (tmpl
)
826 && !at_namespace_scope_p ())
828 error ("specialization of %qD must appear at namespace scope", tmpl
);
832 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
833 /* Same or enclosing namespace. */
837 auto_diagnostic_group d
;
838 if (permerror (input_location
,
839 "specialization of %qD in different namespace", tmpl
))
840 inform (DECL_SOURCE_LOCATION (tmpl
),
841 " from definition of %q#D", tmpl
);
846 /* SPEC is an explicit instantiation. Check that it is valid to
847 perform this explicit instantiation in the current namespace. */
850 check_explicit_instantiation_namespace (tree spec
)
854 /* DR 275: An explicit instantiation shall appear in an enclosing
855 namespace of its template. */
856 ns
= decl_namespace_context (spec
);
857 if (!is_nested_namespace (current_namespace
, ns
))
858 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
859 "(which does not enclose namespace %qD)",
860 spec
, current_namespace
, ns
);
863 /* Returns the type of a template specialization only if that
864 specialization needs to be defined. Otherwise (e.g., if the type has
865 already been defined), the function returns NULL_TREE. */
868 maybe_new_partial_specialization (tree type
)
870 /* An implicit instantiation of an incomplete type implies
871 the definition of a new class template.
879 Here, S<T*> is an implicit instantiation of S whose type
881 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
884 /* It can also be the case that TYPE is a completed specialization.
885 Continuing the previous example, suppose we also declare:
891 Here, S<T*> refers to the specialization S<T*> defined
892 above. However, we need to differentiate definitions because
893 we intend to define a new partial specialization. In this case,
894 we rely on the fact that the constraints are different for
895 this declaration than that above.
897 Note that we also get here for injected class names and
898 late-parsed template definitions. We must ensure that we
899 do not create new type declarations for those cases. */
900 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
902 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
903 tree args
= CLASSTYPE_TI_ARGS (type
);
905 /* If there are no template parameters, this cannot be a new
906 partial template specialization? */
907 if (!current_template_parms
)
910 /* The injected-class-name is not a new partial specialization. */
911 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
914 /* If the constraints are not the same as those of the primary
915 then, we can probably create a new specialization. */
916 tree type_constr
= current_template_constraints ();
918 if (type
== TREE_TYPE (tmpl
))
920 tree main_constr
= get_constraints (tmpl
);
921 if (equivalent_constraints (type_constr
, main_constr
))
925 /* Also, if there's a pre-existing specialization with matching
926 constraints, then this also isn't new. */
927 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
930 tree spec_tmpl
= TREE_VALUE (specs
);
931 tree spec_args
= TREE_PURPOSE (specs
);
932 tree spec_constr
= get_constraints (spec_tmpl
);
933 if (comp_template_args (args
, spec_args
)
934 && equivalent_constraints (type_constr
, spec_constr
))
936 specs
= TREE_CHAIN (specs
);
939 /* Create a new type node (and corresponding type decl)
940 for the newly declared specialization. */
941 tree t
= make_class_type (TREE_CODE (type
));
942 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
943 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
945 /* We only need a separate type node for storing the definition of this
946 partial specialization; uses of S<T*> are unconstrained, so all are
947 equivalent. So keep TYPE_CANONICAL the same. */
948 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
950 /* Build the corresponding type decl. */
951 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
952 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
953 DECL_SOURCE_LOCATION (d
) = input_location
;
954 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
955 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
963 /* The TYPE is being declared. If it is a template type, that means it
964 is a partial specialization. Do appropriate error-checking. */
967 maybe_process_partial_specialization (tree type
)
971 if (type
== error_mark_node
)
972 return error_mark_node
;
974 /* A lambda that appears in specialization context is not itself a
976 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
979 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
981 error ("name of class shadows template template parameter %qD",
983 return error_mark_node
;
986 context
= TYPE_CONTEXT (type
);
988 if (TYPE_ALIAS_P (type
))
990 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
992 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
993 error ("specialization of alias template %qD",
994 TI_TEMPLATE (tinfo
));
996 error ("explicit specialization of non-template %qT", type
);
997 return error_mark_node
;
999 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1001 /* This is for ordinary explicit specialization and partial
1002 specialization of a template class such as:
1004 template <> class C<int>;
1008 template <class T> class C<T*>;
1010 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1012 if (tree t
= maybe_new_partial_specialization (type
))
1014 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1015 && !at_namespace_scope_p ())
1016 return error_mark_node
;
1017 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1018 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1019 if (processing_template_decl
)
1021 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1022 if (decl
== error_mark_node
)
1023 return error_mark_node
;
1024 return TREE_TYPE (decl
);
1027 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1028 error ("specialization of %qT after instantiation", type
);
1029 else if (errorcount
&& !processing_specialization
1030 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1031 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1032 /* Trying to define a specialization either without a template<> header
1033 or in an inappropriate place. We've already given an error, so just
1034 bail now so we don't actually define the specialization. */
1035 return error_mark_node
;
1037 else if (CLASS_TYPE_P (type
)
1038 && !CLASSTYPE_USE_TEMPLATE (type
)
1039 && CLASSTYPE_TEMPLATE_INFO (type
)
1040 && context
&& CLASS_TYPE_P (context
)
1041 && CLASSTYPE_TEMPLATE_INFO (context
))
1043 /* This is for an explicit specialization of member class
1044 template according to [temp.expl.spec/18]:
1046 template <> template <class U> class C<int>::D;
1048 The context `C<int>' must be an implicit instantiation.
1049 Otherwise this is just a member class template declared
1052 template <> class C<int> { template <class U> class D; };
1053 template <> template <class U> class C<int>::D;
1055 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1056 while in the second case, `C<int>::D' is a primary template
1057 and `C<T>::D' may not exist. */
1059 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1060 && !COMPLETE_TYPE_P (type
))
1063 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1065 if (current_namespace
1066 != decl_namespace_context (tmpl
))
1068 if (permerror (input_location
,
1069 "specialization of %qD in different namespace",
1071 inform (DECL_SOURCE_LOCATION (tmpl
),
1072 "from definition of %q#D", tmpl
);
1075 /* Check for invalid specialization after instantiation:
1077 template <> template <> class C<int>::D<int>;
1078 template <> template <class U> class C<int>::D; */
1080 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1081 t
; t
= TREE_CHAIN (t
))
1083 tree inst
= TREE_VALUE (t
);
1084 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1085 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1087 /* We already have a full specialization of this partial
1088 instantiation, or a full specialization has been
1089 looked up but not instantiated. Reassign it to the
1090 new member specialization template. */
1094 elt
.tmpl
= most_general_template (tmpl
);
1095 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1098 type_specializations
->remove_elt (&elt
);
1101 CLASSTYPE_TI_ARGS (inst
)
1102 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1105 = type_specializations
->find_slot (&elt
, INSERT
);
1106 entry
= ggc_alloc
<spec_entry
> ();
1111 /* But if we've had an implicit instantiation, that's a
1112 problem ([temp.expl.spec]/6). */
1113 error ("specialization %qT after instantiation %qT",
1117 /* Mark TYPE as a specialization. And as a result, we only
1118 have one level of template argument for the innermost
1120 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1121 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1122 CLASSTYPE_TI_ARGS (type
)
1123 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1126 else if (processing_specialization
)
1128 /* Someday C++0x may allow for enum template specialization. */
1129 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1130 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1131 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1132 "of %qD not allowed by ISO C++", type
);
1135 error ("explicit specialization of non-template %qT", type
);
1136 return error_mark_node
;
1143 /* Returns nonzero if we can optimize the retrieval of specializations
1144 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1145 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1148 optimize_specialization_lookup_p (tree tmpl
)
1150 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1151 && DECL_CLASS_SCOPE_P (tmpl
)
1152 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1154 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1155 /* The optimized lookup depends on the fact that the
1156 template arguments for the member function template apply
1157 purely to the containing class, which is not true if the
1158 containing class is an explicit or partial
1160 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1161 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1162 && !DECL_CONV_FN_P (tmpl
)
1163 /* It is possible to have a template that is not a member
1164 template and is not a member of a template class:
1166 template <typename T>
1167 struct S { friend A::f(); };
1169 Here, the friend function is a template, but the context does
1170 not have template information. The optimized lookup relies
1171 on having ARGS be the template arguments for both the class
1172 and the function template. */
1173 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1176 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1177 gone through coerce_template_parms by now. */
1180 verify_unstripped_args_1 (tree inner
)
1182 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1184 tree arg
= TREE_VEC_ELT (inner
, i
);
1185 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1187 else if (TYPE_P (arg
))
1188 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1189 else if (ARGUMENT_PACK_P (arg
))
1190 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1191 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1192 /* Allow typedefs on the type of a non-type argument, since a
1193 parameter can have them. */;
1195 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1200 verify_unstripped_args (tree args
)
1202 ++processing_template_decl
;
1203 if (!any_dependent_template_arguments_p (args
))
1204 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1205 --processing_template_decl
;
1208 /* Retrieve the specialization (in the sense of [temp.spec] - a
1209 specialization is either an instantiation or an explicit
1210 specialization) of TMPL for the given template ARGS. If there is
1211 no such specialization, return NULL_TREE. The ARGS are a vector of
1212 arguments, or a vector of vectors of arguments, in the case of
1213 templates with more than one level of parameters.
1215 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1216 then we search for a partial specialization matching ARGS. This
1217 parameter is ignored if TMPL is not a class template.
1219 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1220 result is a NONTYPE_ARGUMENT_PACK. */
1223 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1225 if (tmpl
== NULL_TREE
)
1228 if (args
== error_mark_node
)
1231 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1232 || TREE_CODE (tmpl
) == FIELD_DECL
);
1234 /* There should be as many levels of arguments as there are
1235 levels of parameters. */
1236 gcc_assert (TMPL_ARGS_DEPTH (args
)
1237 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1238 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1239 : template_class_depth (DECL_CONTEXT (tmpl
))));
1242 verify_unstripped_args (args
);
1244 /* Lambda functions in templates aren't instantiated normally, but through
1245 tsubst_lambda_expr. */
1246 if (lambda_fn_in_template_p (tmpl
))
1249 if (optimize_specialization_lookup_p (tmpl
))
1251 /* The template arguments actually apply to the containing
1252 class. Find the class specialization with those
1254 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1255 tree class_specialization
1256 = retrieve_specialization (class_template
, args
, 0);
1257 if (!class_specialization
)
1260 /* Find the instance of TMPL. */
1261 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1262 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1265 if (tree ti
= get_template_info (fn
))
1266 if (TI_TEMPLATE (ti
) == tmpl
1267 /* using-declarations can bring in a different
1268 instantiation of tmpl as a member of a different
1269 instantiation of tmpl's class. We don't want those
1271 && DECL_CONTEXT (fn
) == class_specialization
)
1280 spec_hash_table
*specializations
;
1284 elt
.spec
= NULL_TREE
;
1286 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1287 specializations
= type_specializations
;
1289 specializations
= decl_specializations
;
1292 hash
= spec_hasher::hash (&elt
);
1293 found
= specializations
->find_with_hash (&elt
, hash
);
1301 /* Like retrieve_specialization, but for local declarations. */
1304 retrieve_local_specialization (tree tmpl
)
1306 if (local_specializations
== NULL
)
1309 tree
*slot
= local_specializations
->get (tmpl
);
1310 return slot
? *slot
: NULL_TREE
;
1313 /* Returns nonzero iff DECL is a specialization of TMPL. */
1316 is_specialization_of (tree decl
, tree tmpl
)
1320 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1324 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1330 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1332 for (t
= TREE_TYPE (decl
);
1334 t
= CLASSTYPE_USE_TEMPLATE (t
)
1335 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1336 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1343 /* Returns nonzero iff DECL is a specialization of friend declaration
1344 FRIEND_DECL according to [temp.friend]. */
1347 is_specialization_of_friend (tree decl
, tree friend_decl
)
1349 bool need_template
= true;
1352 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1353 || TREE_CODE (decl
) == TYPE_DECL
);
1355 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1356 of a template class, we want to check if DECL is a specialization
1358 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1359 && DECL_TEMPLATE_INFO (friend_decl
)
1360 && !DECL_USE_TEMPLATE (friend_decl
))
1362 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1363 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1364 need_template
= false;
1366 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1367 && !PRIMARY_TEMPLATE_P (friend_decl
))
1368 need_template
= false;
1370 /* There is nothing to do if this is not a template friend. */
1371 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1374 if (is_specialization_of (decl
, friend_decl
))
1378 A member of a class template may be declared to be a friend of a
1379 non-template class. In this case, the corresponding member of
1380 every specialization of the class template is a friend of the
1381 class granting friendship.
1383 For example, given a template friend declaration
1385 template <class T> friend void A<T>::f();
1387 the member function below is considered a friend
1389 template <> struct A<int> {
1393 For this type of template friend, TEMPLATE_DEPTH below will be
1394 nonzero. To determine if DECL is a friend of FRIEND, we first
1395 check if the enclosing class is a specialization of another. */
1397 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1399 && DECL_CLASS_SCOPE_P (decl
)
1400 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1401 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1403 /* Next, we check the members themselves. In order to handle
1404 a few tricky cases, such as when FRIEND_DECL's are
1406 template <class T> friend void A<T>::g(T t);
1407 template <class T> template <T t> friend void A<T>::h();
1411 void A<int>::g(int);
1412 template <int> void A<int>::h();
1414 we need to figure out ARGS, the template arguments from
1415 the context of DECL. This is required for template substitution
1416 of `T' in the function parameter of `g' and template parameter
1417 of `h' in the above examples. Here ARGS corresponds to `int'. */
1419 tree context
= DECL_CONTEXT (decl
);
1420 tree args
= NULL_TREE
;
1421 int current_depth
= 0;
1423 while (current_depth
< template_depth
)
1425 if (CLASSTYPE_TEMPLATE_INFO (context
))
1427 if (current_depth
== 0)
1428 args
= TYPE_TI_ARGS (context
);
1430 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1433 context
= TYPE_CONTEXT (context
);
1436 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1441 tree friend_args_type
;
1442 tree decl_args_type
;
1444 /* Make sure that both DECL and FRIEND_DECL are templates or
1446 is_template
= DECL_TEMPLATE_INFO (decl
)
1447 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1448 if (need_template
^ is_template
)
1450 else if (is_template
)
1452 /* If both are templates, check template parameter list. */
1454 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1456 if (!comp_template_parms
1457 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1461 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1464 decl_type
= TREE_TYPE (decl
);
1466 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1467 tf_none
, NULL_TREE
);
1468 if (friend_type
== error_mark_node
)
1471 /* Check if return types match. */
1472 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1475 /* Check if function parameter types match, ignoring the
1476 `this' parameter. */
1477 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1478 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1479 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1480 friend_args_type
= TREE_CHAIN (friend_args_type
);
1481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1482 decl_args_type
= TREE_CHAIN (decl_args_type
);
1484 return compparms (decl_args_type
, friend_args_type
);
1488 /* DECL is a TYPE_DECL */
1490 tree decl_type
= TREE_TYPE (decl
);
1492 /* Make sure that both DECL and FRIEND_DECL are templates or
1495 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1496 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1498 if (need_template
^ is_template
)
1500 else if (is_template
)
1503 /* If both are templates, check the name of the two
1504 TEMPLATE_DECL's first because is_friend didn't. */
1505 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1506 != DECL_NAME (friend_decl
))
1509 /* Now check template parameter list. */
1511 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1513 return comp_template_parms
1514 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1518 return (DECL_NAME (decl
)
1519 == DECL_NAME (friend_decl
));
1525 /* Register the specialization SPEC as a specialization of TMPL with
1526 the indicated ARGS. IS_FRIEND indicates whether the specialization
1527 is actually just a friend declaration. ATTRLIST is the list of
1528 attributes that the specialization is declared with or NULL when
1529 it isn't. Returns SPEC, or an equivalent prior declaration, if
1532 We also store instantiations of field packs in the hash table, even
1533 though they are not themselves templates, to make lookup easier. */
1536 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1540 spec_entry
**slot
= NULL
;
1543 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1544 || (TREE_CODE (tmpl
) == FIELD_DECL
1545 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1547 if (TREE_CODE (spec
) == FUNCTION_DECL
1548 && uses_template_parms (DECL_TI_ARGS (spec
)))
1549 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1550 register it; we want the corresponding TEMPLATE_DECL instead.
1551 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1552 the more obvious `uses_template_parms (spec)' to avoid problems
1553 with default function arguments. In particular, given
1554 something like this:
1556 template <class T> void f(T t1, T t = T())
1558 the default argument expression is not substituted for in an
1559 instantiation unless and until it is actually needed. */
1562 if (optimize_specialization_lookup_p (tmpl
))
1563 /* We don't put these specializations in the hash table, but we might
1564 want to give an error about a mismatch. */
1565 fn
= retrieve_specialization (tmpl
, args
, 0);
1573 hash
= spec_hasher::hash (&elt
);
1575 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1582 /* We can sometimes try to re-register a specialization that we've
1583 already got. In particular, regenerate_decl_from_template calls
1584 duplicate_decls which will update the specialization list. But,
1585 we'll still get called again here anyhow. It's more convenient
1586 to simply allow this than to try to prevent it. */
1589 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1591 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1593 if (DECL_ODR_USED (fn
)
1594 || DECL_EXPLICIT_INSTANTIATION (fn
))
1596 error ("specialization of %qD after instantiation",
1598 return error_mark_node
;
1603 /* This situation should occur only if the first
1604 specialization is an implicit instantiation, the
1605 second is an explicit specialization, and the
1606 implicit instantiation has not yet been used. That
1607 situation can occur if we have implicitly
1608 instantiated a member function and then specialized
1611 We can also wind up here if a friend declaration that
1612 looked like an instantiation turns out to be a
1615 template <class T> void foo(T);
1616 class S { friend void foo<>(int) };
1617 template <> void foo(int);
1619 We transform the existing DECL in place so that any
1620 pointers to it become pointers to the updated
1623 If there was a definition for the template, but not
1624 for the specialization, we want this to look as if
1625 there were no definition, and vice versa. */
1626 DECL_INITIAL (fn
) = NULL_TREE
;
1627 duplicate_decls (spec
, fn
, is_friend
);
1628 /* The call to duplicate_decls will have applied
1631 An explicit specialization of a function template
1632 is inline only if it is explicitly declared to be,
1633 and independently of whether its function template
1636 to the primary function; now copy the inline bits to
1637 the various clones. */
1638 FOR_EACH_CLONE (clone
, fn
)
1640 DECL_DECLARED_INLINE_P (clone
)
1641 = DECL_DECLARED_INLINE_P (fn
);
1642 DECL_SOURCE_LOCATION (clone
)
1643 = DECL_SOURCE_LOCATION (fn
);
1644 DECL_DELETED_FN (clone
)
1645 = DECL_DELETED_FN (fn
);
1647 check_specialization_namespace (tmpl
);
1652 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1654 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1655 if (dd
== error_mark_node
)
1656 /* We've already complained in duplicate_decls. */
1657 return error_mark_node
;
1659 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1660 /* Dup decl failed, but this is a new definition. Set the
1661 line number so any errors match this new
1663 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1669 return duplicate_decls (spec
, fn
, is_friend
);
1671 /* A specialization must be declared in the same namespace as the
1672 template it is specializing. */
1673 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1674 && !check_specialization_namespace (tmpl
))
1675 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1677 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1679 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1680 gcc_assert (tmpl
&& args
&& spec
);
1683 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1684 && PRIMARY_TEMPLATE_P (tmpl
)
1685 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1686 || variable_template_p (tmpl
))
1687 /* If TMPL is a forward declaration of a template function, keep a list
1688 of all specializations in case we need to reassign them to a friend
1689 template later in tsubst_friend_function.
1691 Also keep a list of all variable template instantiations so that
1692 process_partial_specialization can check whether a later partial
1693 specialization would have used it. */
1694 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1695 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1701 /* Returns true iff two spec_entry nodes are equivalent. */
1703 int comparing_specializations
;
1706 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1710 ++comparing_specializations
;
1711 equal
= (e1
->tmpl
== e2
->tmpl
1712 && comp_template_args (e1
->args
, e2
->args
));
1713 if (equal
&& flag_concepts
1714 /* tmpl could be a FIELD_DECL for a capture pack. */
1715 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1716 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1717 && uses_template_parms (e1
->args
))
1719 /* Partial specializations of a variable template can be distinguished by
1721 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1722 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1723 equal
= equivalent_constraints (c1
, c2
);
1725 --comparing_specializations
;
1730 /* Returns a hash for a template TMPL and template arguments ARGS. */
1733 hash_tmpl_and_args (tree tmpl
, tree args
)
1735 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1736 return iterative_hash_template_arg (args
, val
);
1739 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1743 spec_hasher::hash (spec_entry
*e
)
1745 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1748 /* Recursively calculate a hash value for a template argument ARG, for use
1749 in the hash tables of template specializations. We must be
1750 careful to (at least) skip the same entities template_args_equal
1754 iterative_hash_template_arg (tree arg
, hashval_t val
)
1756 if (arg
== NULL_TREE
)
1757 return iterative_hash_object (arg
, val
);
1760 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1761 while (CONVERT_EXPR_P (arg
)
1762 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1763 || class_nttp_const_wrapper_p (arg
))
1764 arg
= TREE_OPERAND (arg
, 0);
1766 enum tree_code code
= TREE_CODE (arg
);
1768 val
= iterative_hash_object (code
, val
);
1772 case ARGUMENT_PACK_SELECT
:
1778 case IDENTIFIER_NODE
:
1779 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1782 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1783 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1786 case TYPE_PACK_EXPANSION
:
1787 case EXPR_PACK_EXPANSION
:
1788 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1789 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1791 case TYPE_ARGUMENT_PACK
:
1792 case NONTYPE_ARGUMENT_PACK
:
1793 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1796 for (; arg
; arg
= TREE_CHAIN (arg
))
1797 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1801 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1802 val
= iterative_hash_template_arg (*iter
, val
);
1809 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1810 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1812 val
= iterative_hash_template_arg (field
, val
);
1813 val
= iterative_hash_template_arg (value
, val
);
1819 if (!DECL_ARTIFICIAL (arg
))
1821 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1822 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1824 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1827 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1830 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1831 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1833 case TEMPLATE_PARM_INDEX
:
1834 val
= iterative_hash_template_arg
1835 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1836 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1837 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1840 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1841 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1842 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1845 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1847 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1851 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1852 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1853 val
= iterative_hash_object (code
, val
);
1854 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1857 /* [temp.over.link] Two lambda-expressions are never considered
1860 So just hash the closure type. */
1861 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1864 case IMPLICIT_CONV_EXPR
:
1865 case STATIC_CAST_EXPR
:
1866 case REINTERPRET_CAST_EXPR
:
1867 case CONST_CAST_EXPR
:
1868 case DYNAMIC_CAST_EXPR
:
1870 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1871 /* Now hash operands as usual. */
1876 tree fn
= CALL_EXPR_FN (arg
);
1877 if (tree name
= dependent_name (fn
))
1879 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1880 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1883 val
= iterative_hash_template_arg (fn
, val
);
1884 call_expr_arg_iterator ai
;
1885 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1886 x
= next_call_expr_arg (&ai
))
1887 val
= iterative_hash_template_arg (x
, val
);
1895 char tclass
= TREE_CODE_CLASS (code
);
1899 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1901 // We want an alias specialization that survived strip_typedefs
1902 // to hash differently from its TYPE_CANONICAL, to avoid hash
1903 // collisions that compare as different in template_args_equal.
1904 // These could be dependent specializations that strip_typedefs
1905 // left alone, or untouched specializations because
1906 // coerce_template_parms returns the unconverted template
1907 // arguments if it sees incomplete argument packs.
1908 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1909 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1912 switch (TREE_CODE (arg
))
1914 case TEMPLATE_TEMPLATE_PARM
:
1916 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1918 /* Do not recurse with TPI directly, as that is unbounded
1920 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1921 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1926 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1930 if (tree canonical
= TYPE_CANONICAL (arg
))
1931 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1937 case tcc_declaration
:
1939 return iterative_hash_expr (arg
, val
);
1942 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1943 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1944 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1952 /* Unregister the specialization SPEC as a specialization of TMPL.
1953 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1954 if the SPEC was listed as a specialization of TMPL.
1956 Note that SPEC has been ggc_freed, so we can't look inside it. */
1959 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1964 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1965 elt
.args
= TI_ARGS (tinfo
);
1966 elt
.spec
= NULL_TREE
;
1968 entry
= decl_specializations
->find (&elt
);
1971 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1972 gcc_assert (new_spec
!= NULL_TREE
);
1973 entry
->spec
= new_spec
;
1980 /* Like register_specialization, but for local declarations. We are
1981 registering SPEC, an instantiation of TMPL. */
1984 register_local_specialization (tree spec
, tree tmpl
)
1986 gcc_assert (tmpl
!= spec
);
1987 local_specializations
->put (tmpl
, spec
);
1990 /* TYPE is a class type. Returns true if TYPE is an explicitly
1991 specialized class. */
1994 explicit_class_specialization_p (tree type
)
1996 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1998 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2001 /* Print the list of functions at FNS, going through all the overloads
2002 for each element of the list. Alternatively, FNS cannot be a
2003 TREE_LIST, in which case it will be printed together with all the
2006 MORE and *STR should respectively be FALSE and NULL when the function
2007 is called from the outside. They are used internally on recursive
2008 calls. print_candidates manages the two parameters and leaves NULL
2009 in *STR when it ends. */
2012 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2014 if (TREE_CODE (fns
) == TREE_LIST
)
2015 for (; fns
; fns
= TREE_CHAIN (fns
))
2016 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2018 for (lkp_iterator
iter (fns
); iter
;)
2023 const char *pfx
= *str
;
2027 pfx
= _("candidates are:");
2029 pfx
= _("candidate is:");
2030 *str
= get_spaces (pfx
);
2032 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2036 /* Print the list of candidate FNS in an error message. FNS can also
2037 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2040 print_candidates (tree fns
)
2043 print_candidates_1 (fns
, &str
);
2047 /* Get a (possibly) constrained template declaration for the
2048 purpose of ordering candidates. */
2050 get_template_for_ordering (tree list
)
2052 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2053 tree f
= TREE_VALUE (list
);
2054 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2055 return TI_TEMPLATE (ti
);
2059 /* Among candidates having the same signature, return the
2060 most constrained or NULL_TREE if there is no best candidate.
2061 If the signatures of candidates vary (e.g., template
2062 specialization vs. member function), then there can be no
2065 Note that we don't compare constraints on the functions
2066 themselves, but rather those of their templates. */
2068 most_constrained_function (tree candidates
)
2070 // Try to find the best candidate in a first pass.
2071 tree champ
= candidates
;
2072 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2074 int winner
= more_constrained (get_template_for_ordering (champ
),
2075 get_template_for_ordering (c
));
2077 champ
= c
; // The candidate is more constrained
2078 else if (winner
== 0)
2079 return NULL_TREE
; // Neither is more constrained
2082 // Verify that the champ is better than previous candidates.
2083 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2084 if (!more_constrained (get_template_for_ordering (champ
),
2085 get_template_for_ordering (c
)))
2093 /* Returns the template (one of the functions given by TEMPLATE_ID)
2094 which can be specialized to match the indicated DECL with the
2095 explicit template args given in TEMPLATE_ID. The DECL may be
2096 NULL_TREE if none is available. In that case, the functions in
2097 TEMPLATE_ID are non-members.
2099 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2100 specialization of a member template.
2102 The TEMPLATE_COUNT is the number of references to qualifying
2103 template classes that appeared in the name of the function. See
2104 check_explicit_specialization for a more accurate description.
2106 TSK indicates what kind of template declaration (if any) is being
2107 declared. TSK_TEMPLATE indicates that the declaration given by
2108 DECL, though a FUNCTION_DECL, has template parameters, and is
2109 therefore a template function.
2111 The template args (those explicitly specified and those deduced)
2112 are output in a newly created vector *TARGS_OUT.
2114 If it is impossible to determine the result, an error message is
2115 issued. The error_mark_node is returned to indicate failure. */
2118 determine_specialization (tree template_id
,
2121 int need_member_template
,
2127 tree explicit_targs
;
2128 tree candidates
= NULL_TREE
;
2130 /* A TREE_LIST of templates of which DECL may be a specialization.
2131 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2132 corresponding TREE_PURPOSE is the set of template arguments that,
2133 when used to instantiate the template, would produce a function
2134 with the signature of DECL. */
2135 tree templates
= NULL_TREE
;
2137 cp_binding_level
*b
;
2139 *targs_out
= NULL_TREE
;
2141 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2142 return error_mark_node
;
2144 /* We shouldn't be specializing a member template of an
2145 unspecialized class template; we already gave an error in
2146 check_specialization_scope, now avoid crashing. */
2148 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2149 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2151 gcc_assert (errorcount
);
2152 return error_mark_node
;
2155 fns
= TREE_OPERAND (template_id
, 0);
2156 explicit_targs
= TREE_OPERAND (template_id
, 1);
2158 if (fns
== error_mark_node
)
2159 return error_mark_node
;
2161 /* Check for baselinks. */
2162 if (BASELINK_P (fns
))
2163 fns
= BASELINK_FUNCTIONS (fns
);
2165 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2167 error_at (DECL_SOURCE_LOCATION (decl
),
2168 "%qD is not a function template", fns
);
2169 return error_mark_node
;
2171 else if (VAR_P (decl
) && !variable_template_p (fns
))
2173 error ("%qD is not a variable template", fns
);
2174 return error_mark_node
;
2177 /* Count the number of template headers specified for this
2180 for (b
= current_binding_level
;
2181 b
->kind
== sk_template_parms
;
2185 tree orig_fns
= fns
;
2187 if (variable_template_p (fns
))
2189 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2190 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2191 tf_warning_or_error
,
2192 /*req_all*/true, /*use_defarg*/true);
2193 if (targs
!= error_mark_node
)
2194 templates
= tree_cons (targs
, fns
, templates
);
2196 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2200 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2202 tree decl_arg_types
;
2206 /* In case of explicit specialization, we need to check if
2207 the number of template headers appearing in the specialization
2208 is correct. This is usually done in check_explicit_specialization,
2209 but the check done there cannot be exhaustive when specializing
2210 member functions. Consider the following code:
2212 template <> void A<int>::f(int);
2213 template <> template <> void A<int>::f(int);
2215 Assuming that A<int> is not itself an explicit specialization
2216 already, the first line specializes "f" which is a non-template
2217 member function, whilst the second line specializes "f" which
2218 is a template member function. So both lines are syntactically
2219 correct, and check_explicit_specialization does not reject
2222 Here, we can do better, as we are matching the specialization
2223 against the declarations. We count the number of template
2224 headers, and we check if they match TEMPLATE_COUNT + 1
2225 (TEMPLATE_COUNT is the number of qualifying template classes,
2226 plus there must be another header for the member template
2229 Notice that if header_count is zero, this is not a
2230 specialization but rather a template instantiation, so there
2231 is no check we can perform here. */
2232 if (header_count
&& header_count
!= template_count
+ 1)
2235 /* Check that the number of template arguments at the
2236 innermost level for DECL is the same as for FN. */
2237 if (current_binding_level
->kind
== sk_template_parms
2238 && !current_binding_level
->explicit_spec_p
2239 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2240 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2241 (current_template_parms
))))
2244 /* DECL might be a specialization of FN. */
2245 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2246 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2248 /* For a non-static member function, we need to make sure
2249 that the const qualification is the same. Since
2250 get_bindings does not try to merge the "this" parameter,
2251 we must do the comparison explicitly. */
2252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2254 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2255 TREE_VALUE (decl_arg_types
)))
2258 /* And the ref-qualification. */
2259 if (type_memfn_rqual (TREE_TYPE (decl
))
2260 != type_memfn_rqual (TREE_TYPE (fn
)))
2264 /* Skip the "this" parameter and, for constructors of
2265 classes with virtual bases, the VTT parameter. A
2266 full specialization of a constructor will have a VTT
2267 parameter, but a template never will. */
2269 = skip_artificial_parms_for (decl
, decl_arg_types
);
2271 = skip_artificial_parms_for (fn
, fn_arg_types
);
2273 /* Function templates cannot be specializations; there are
2274 no partial specializations of functions. Therefore, if
2275 the type of DECL does not match FN, there is no
2278 Note that it should never be the case that we have both
2279 candidates added here, and for regular member functions
2281 if (tsk
== tsk_template
)
2283 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2284 current_template_parms
))
2286 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2287 TREE_TYPE (TREE_TYPE (fn
))))
2289 if (!compparms (fn_arg_types
, decl_arg_types
))
2292 tree freq
= get_trailing_function_requirements (fn
);
2293 tree dreq
= get_trailing_function_requirements (decl
);
2298 tree fargs
= DECL_TI_ARGS (fn
);
2299 tsubst_flags_t complain
= tf_none
;
2300 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2301 if (!cp_tree_equal (freq
, dreq
))
2305 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2309 /* See whether this function might be a specialization of this
2310 template. Suppress access control because we might be trying
2311 to make this specialization a friend, and we have already done
2312 access control for the declaration of the specialization. */
2313 push_deferring_access_checks (dk_no_check
);
2314 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2315 pop_deferring_access_checks ();
2318 /* We cannot deduce template arguments that when used to
2319 specialize TMPL will produce DECL. */
2322 if (uses_template_parms (targs
))
2323 /* We deduced something involving 'auto', which isn't a valid
2324 template argument. */
2327 /* Remove, from the set of candidates, all those functions
2328 whose constraints are not satisfied. */
2329 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2332 // Then, try to form the new function type.
2333 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2334 if (insttype
== error_mark_node
)
2337 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2338 if (!compparms (fn_arg_types
, decl_arg_types
))
2341 /* Save this template, and the arguments deduced. */
2342 templates
= tree_cons (targs
, fn
, templates
);
2344 else if (need_member_template
)
2345 /* FN is an ordinary member function, and we need a
2346 specialization of a member template. */
2348 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2349 /* We can get IDENTIFIER_NODEs here in certain erroneous
2352 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2353 /* This is just an ordinary non-member function. Nothing can
2354 be a specialization of that. */
2356 else if (DECL_ARTIFICIAL (fn
))
2357 /* Cannot specialize functions that are created implicitly. */
2361 tree decl_arg_types
;
2363 /* This is an ordinary member function. However, since
2364 we're here, we can assume its enclosing class is a
2365 template class. For example,
2367 template <typename T> struct S { void f(); };
2368 template <> void S<int>::f() {}
2370 Here, S<int>::f is a non-template, but S<int> is a
2371 template class. If FN has the same type as DECL, we
2372 might be in business. */
2374 if (!DECL_TEMPLATE_INFO (fn
))
2375 /* Its enclosing class is an explicit specialization
2376 of a template class. This is not a candidate. */
2379 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2380 TREE_TYPE (TREE_TYPE (fn
))))
2381 /* The return types differ. */
2384 /* Adjust the type of DECL in case FN is a static member. */
2385 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2386 if (DECL_STATIC_FUNCTION_P (fn
)
2387 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2388 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2390 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2394 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2395 && (type_memfn_rqual (TREE_TYPE (decl
))
2396 != type_memfn_rqual (TREE_TYPE (fn
))))
2399 // If the deduced arguments do not satisfy the constraints,
2400 // this is not a candidate.
2401 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2404 // Add the candidate.
2405 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2409 if (templates
&& TREE_CHAIN (templates
))
2415 It is possible for a specialization with a given function
2416 signature to be instantiated from more than one function
2417 template. In such cases, explicit specification of the
2418 template arguments must be used to uniquely identify the
2419 function template specialization being specialized.
2421 Note that here, there's no suggestion that we're supposed to
2422 determine which of the candidate templates is most
2423 specialized. However, we, also have:
2427 Partial ordering of overloaded function template
2428 declarations is used in the following contexts to select
2429 the function template to which a function template
2430 specialization refers:
2432 -- when an explicit specialization refers to a function
2435 So, we do use the partial ordering rules, at least for now.
2436 This extension can only serve to make invalid programs valid,
2437 so it's safe. And, there is strong anecdotal evidence that
2438 the committee intended the partial ordering rules to apply;
2439 the EDG front end has that behavior, and John Spicer claims
2440 that the committee simply forgot to delete the wording in
2441 [temp.expl.spec]. */
2442 tree tmpl
= most_specialized_instantiation (templates
);
2443 if (tmpl
!= error_mark_node
)
2446 TREE_CHAIN (templates
) = NULL_TREE
;
2450 // Concepts allows multiple declarations of member functions
2451 // with the same signature. Like above, we need to rely on
2452 // on the partial ordering of those candidates to determine which
2454 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2456 if (tree cand
= most_constrained_function (candidates
))
2459 TREE_CHAIN (cand
) = NULL_TREE
;
2463 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2465 error ("template-id %qD for %q+D does not match any template "
2466 "declaration", template_id
, decl
);
2467 if (header_count
&& header_count
!= template_count
+ 1)
2468 inform (DECL_SOURCE_LOCATION (decl
),
2469 "saw %d %<template<>%>, need %d for "
2470 "specializing a member function template",
2471 header_count
, template_count
+ 1);
2473 print_candidates (orig_fns
);
2474 return error_mark_node
;
2476 else if ((templates
&& TREE_CHAIN (templates
))
2477 || (candidates
&& TREE_CHAIN (candidates
))
2478 || (templates
&& candidates
))
2480 error ("ambiguous template specialization %qD for %q+D",
2482 candidates
= chainon (candidates
, templates
);
2483 print_candidates (candidates
);
2484 return error_mark_node
;
2487 /* We have one, and exactly one, match. */
2490 tree fn
= TREE_VALUE (candidates
);
2491 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2493 /* Propagate the candidate's constraints to the declaration. */
2494 if (tsk
!= tsk_template
)
2495 set_constraints (decl
, get_constraints (fn
));
2497 /* DECL is a re-declaration or partial instantiation of a template
2499 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2501 /* It was a specialization of an ordinary member function in a
2503 return DECL_TI_TEMPLATE (fn
);
2506 /* It was a specialization of a template. */
2507 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2508 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2510 *targs_out
= copy_node (targs
);
2511 SET_TMPL_ARGS_LEVEL (*targs_out
,
2512 TMPL_ARGS_DEPTH (*targs_out
),
2513 TREE_PURPOSE (templates
));
2516 *targs_out
= TREE_PURPOSE (templates
);
2517 return TREE_VALUE (templates
);
2520 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2521 but with the default argument values filled in from those in the
2525 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2528 tree new_spec_types
;
2533 if (spec_types
== void_list_node
)
2534 return void_list_node
;
2536 /* Substitute into the rest of the list. */
2538 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2539 TREE_CHAIN (tmpl_types
));
2541 /* Add the default argument for this parameter. */
2542 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2543 TREE_VALUE (spec_types
),
2547 /* DECL is an explicit specialization. Replicate default arguments
2548 from the template it specializes. (That way, code like:
2550 template <class T> void f(T = 3);
2551 template <> void f(double);
2554 works, as required.) An alternative approach would be to look up
2555 the correct default arguments at the call-site, but this approach
2556 is consistent with how implicit instantiations are handled. */
2559 copy_default_args_to_explicit_spec (tree decl
)
2564 tree new_spec_types
;
2568 tree object_type
= NULL_TREE
;
2569 tree in_charge
= NULL_TREE
;
2570 tree vtt
= NULL_TREE
;
2572 /* See if there's anything we need to do. */
2573 tmpl
= DECL_TI_TEMPLATE (decl
);
2574 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2575 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2576 if (TREE_PURPOSE (t
))
2581 old_type
= TREE_TYPE (decl
);
2582 spec_types
= TYPE_ARG_TYPES (old_type
);
2584 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2586 /* Remove the this pointer, but remember the object's type for
2588 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2589 spec_types
= TREE_CHAIN (spec_types
);
2590 tmpl_types
= TREE_CHAIN (tmpl_types
);
2592 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2594 /* DECL may contain more parameters than TMPL due to the extra
2595 in-charge parameter in constructors and destructors. */
2596 in_charge
= spec_types
;
2597 spec_types
= TREE_CHAIN (spec_types
);
2599 if (DECL_HAS_VTT_PARM_P (decl
))
2602 spec_types
= TREE_CHAIN (spec_types
);
2606 /* Compute the merged default arguments. */
2608 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2610 /* Compute the new FUNCTION_TYPE. */
2614 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2619 /* Put the in-charge parameter back. */
2620 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2621 TREE_VALUE (in_charge
),
2624 new_type
= build_method_type_directly (object_type
,
2625 TREE_TYPE (old_type
),
2629 new_type
= build_function_type (TREE_TYPE (old_type
),
2631 new_type
= cp_build_type_attribute_variant (new_type
,
2632 TYPE_ATTRIBUTES (old_type
));
2633 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2635 TREE_TYPE (decl
) = new_type
;
2638 /* Return the number of template headers we expect to see for a definition
2639 or specialization of CTYPE or one of its non-template members. */
2642 num_template_headers_for_class (tree ctype
)
2644 int num_templates
= 0;
2646 while (ctype
&& CLASS_TYPE_P (ctype
))
2648 /* You're supposed to have one `template <...>' for every
2649 template class, but you don't need one for a full
2650 specialization. For example:
2652 template <class T> struct S{};
2653 template <> struct S<int> { void f(); };
2654 void S<int>::f () {}
2656 is correct; there shouldn't be a `template <>' for the
2657 definition of `S<int>::f'. */
2658 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2659 /* If CTYPE does not have template information of any
2660 kind, then it is not a template, nor is it nested
2661 within a template. */
2663 if (explicit_class_specialization_p (ctype
))
2665 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2668 ctype
= TYPE_CONTEXT (ctype
);
2671 return num_templates
;
2674 /* Do a simple sanity check on the template headers that precede the
2675 variable declaration DECL. */
2678 check_template_variable (tree decl
)
2680 tree ctx
= CP_DECL_CONTEXT (decl
);
2681 int wanted
= num_template_headers_for_class (ctx
);
2682 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2683 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2685 if (cxx_dialect
< cxx14
)
2686 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2687 "variable templates only available with "
2688 "%<-std=c++14%> or %<-std=gnu++14%>");
2690 // Namespace-scope variable templates should have a template header.
2693 if (template_header_count
> wanted
)
2695 auto_diagnostic_group d
;
2696 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2697 "too many template headers for %qD "
2700 if (warned
&& CLASS_TYPE_P (ctx
)
2701 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2702 inform (DECL_SOURCE_LOCATION (decl
),
2703 "members of an explicitly specialized class are defined "
2704 "without a template header");
2708 /* An explicit specialization whose declarator-id or class-head-name is not
2709 qualified shall be declared in the nearest enclosing namespace of the
2710 template, or, if the namespace is inline (7.3.1), any namespace from its
2711 enclosing namespace set.
2713 If the name declared in the explicit instantiation is an unqualified name,
2714 the explicit instantiation shall appear in the namespace where its template
2715 is declared or, if that namespace is inline (7.3.1), any namespace from its
2716 enclosing namespace set. */
2719 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2721 tree tmpl
= most_general_template (t
);
2722 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2723 && !is_nested_namespace (current_namespace
,
2724 CP_DECL_CONTEXT (tmpl
), true))
2726 if (processing_specialization
)
2727 permerror (loc
, "explicit specialization of %qD outside its "
2728 "namespace must use a nested-name-specifier", tmpl
);
2729 else if (processing_explicit_instantiation
2730 && cxx_dialect
>= cxx11
)
2731 /* This was allowed in C++98, so only pedwarn. */
2732 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2733 "outside its namespace must use a nested-name-"
2738 /* Warn for a template specialization SPEC that is missing some of a set
2739 of function or type attributes that the template TEMPL is declared with.
2740 ATTRLIST is a list of additional attributes that SPEC should be taken
2741 to ultimately be declared with. */
2744 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2746 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2747 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2749 /* Avoid warning if the difference between the primary and
2750 the specialization is not in one of the attributes below. */
2751 const char* const blacklist
[] = {
2752 "alloc_align", "alloc_size", "assume_aligned", "format",
2753 "format_arg", "malloc", "nonnull", NULL
2756 /* Put together a list of the black listed attributes that the primary
2757 template is declared with that the specialization is not, in case
2758 it's not apparent from the most recent declaration of the primary. */
2760 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2766 auto_diagnostic_group d
;
2767 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2768 "explicit specialization %q#D may be missing attributes",
2770 inform (DECL_SOURCE_LOCATION (tmpl
),
2772 ? G_("missing primary template attributes %s")
2773 : G_("missing primary template attribute %s"),
2774 pp_formatted_text (&str
));
2777 /* Check to see if the function just declared, as indicated in
2778 DECLARATOR, and in DECL, is a specialization of a function
2779 template. We may also discover that the declaration is an explicit
2780 instantiation at this point.
2782 Returns DECL, or an equivalent declaration that should be used
2783 instead if all goes well. Issues an error message if something is
2784 amiss. Returns error_mark_node if the error is not easily
2787 FLAGS is a bitmask consisting of the following flags:
2789 2: The function has a definition.
2790 4: The function is a friend.
2792 The TEMPLATE_COUNT is the number of references to qualifying
2793 template classes that appeared in the name of the function. For
2796 template <class T> struct S { void f(); };
2799 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2800 classes are not counted in the TEMPLATE_COUNT, so that in
2802 template <class T> struct S {};
2803 template <> struct S<int> { void f(); }
2804 template <> void S<int>::f();
2806 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2807 invalid; there should be no template <>.)
2809 If the function is a specialization, it is marked as such via
2810 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2811 is set up correctly, and it is added to the list of specializations
2812 for that template. */
2815 check_explicit_specialization (tree declarator
,
2821 int have_def
= flags
& 2;
2822 int is_friend
= flags
& 4;
2823 bool is_concept
= flags
& 8;
2824 int specialization
= 0;
2825 int explicit_instantiation
= 0;
2826 int member_specialization
= 0;
2827 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2828 tree dname
= DECL_NAME (decl
);
2833 if (!processing_specialization
)
2836 tsk
= tsk_excessive_parms
;
2839 tsk
= current_tmpl_spec_kind (template_count
);
2844 if (processing_specialization
&& !VAR_P (decl
))
2847 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2849 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2852 /* This could be something like:
2854 template <class T> void f(T);
2855 class S { friend void f<>(int); } */
2859 /* This case handles bogus declarations like template <>
2860 template <class T> void f<int>(); */
2862 error_at (cp_expr_loc_or_input_loc (declarator
),
2863 "template-id %qE in declaration of primary template",
2870 case tsk_invalid_member_spec
:
2871 /* The error has already been reported in
2872 check_specialization_scope. */
2873 return error_mark_node
;
2875 case tsk_invalid_expl_inst
:
2876 error ("template parameter list used in explicit instantiation");
2882 error ("definition provided for explicit instantiation");
2884 explicit_instantiation
= 1;
2887 case tsk_excessive_parms
:
2888 case tsk_insufficient_parms
:
2889 if (tsk
== tsk_excessive_parms
)
2890 error ("too many template parameter lists in declaration of %qD",
2892 else if (template_header_count
)
2893 error("too few template parameter lists in declaration of %qD", decl
);
2895 error("explicit specialization of %qD must be introduced by "
2896 "%<template <>%>", decl
);
2901 error ("explicit specialization declared %<concept%>");
2903 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2904 /* In cases like template<> constexpr bool v = true;
2905 We'll give an error in check_template_variable. */
2908 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2910 member_specialization
= 1;
2916 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2918 /* This case handles bogus declarations like template <>
2919 template <class T> void f<int>(); */
2921 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2922 error_at (cp_expr_loc_or_input_loc (declarator
),
2923 "template-id %qE in declaration of primary template",
2925 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2927 /* Partial specialization of variable template. */
2928 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2932 else if (cxx_dialect
< cxx14
)
2933 error_at (cp_expr_loc_or_input_loc (declarator
),
2934 "non-type partial specialization %qE "
2935 "is not allowed", declarator
);
2937 error_at (cp_expr_loc_or_input_loc (declarator
),
2938 "non-class, non-variable partial specialization %qE "
2939 "is not allowed", declarator
);
2944 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2945 /* This is a specialization of a member template, without
2946 specialization the containing class. Something like:
2948 template <class T> struct S {
2949 template <class U> void f (U);
2951 template <> template <class U> void S<int>::f(U) {}
2953 That's a specialization -- but of the entire template. */
2961 if ((specialization
|| member_specialization
)
2962 /* This doesn't apply to variable templates. */
2963 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2965 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2966 for (; t
; t
= TREE_CHAIN (t
))
2967 if (TREE_PURPOSE (t
))
2969 permerror (input_location
,
2970 "default argument specified in explicit specialization");
2975 if (specialization
|| member_specialization
|| explicit_instantiation
)
2977 tree tmpl
= NULL_TREE
;
2978 tree targs
= NULL_TREE
;
2979 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2981 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2982 if (!was_template_id
)
2986 gcc_assert (identifier_p (declarator
));
2991 /* If there is no class context, the explicit instantiation
2992 must be at namespace scope. */
2993 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2995 /* Find the namespace binding, using the declaration
2997 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2999 if (fns
== error_mark_node
)
3000 /* If lookup fails, look for a friend declaration so we can
3001 give a better diagnostic. */
3002 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3003 /*type*/false, /*complain*/true,
3006 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3008 error ("%qD is not a template function", dname
);
3009 fns
= error_mark_node
;
3013 declarator
= lookup_template_function (fns
, NULL_TREE
);
3016 if (declarator
== error_mark_node
)
3017 return error_mark_node
;
3019 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3021 if (!explicit_instantiation
)
3022 /* A specialization in class scope. This is invalid,
3023 but the error will already have been flagged by
3024 check_specialization_scope. */
3025 return error_mark_node
;
3028 /* It's not valid to write an explicit instantiation in
3031 class C { template void f(); }
3033 This case is caught by the parser. However, on
3036 template class C { void f(); };
3038 (which is invalid) we can get here. The error will be
3045 else if (ctype
!= NULL_TREE
3046 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3048 // We'll match variable templates in start_decl.
3052 /* Find the list of functions in ctype that have the same
3053 name as the declared function. */
3054 tree name
= TREE_OPERAND (declarator
, 0);
3056 if (constructor_name_p (name
, ctype
))
3058 if (DECL_CONSTRUCTOR_P (decl
)
3059 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3060 : !CLASSTYPE_DESTRUCTOR (ctype
))
3062 /* From [temp.expl.spec]:
3064 If such an explicit specialization for the member
3065 of a class template names an implicitly-declared
3066 special member function (clause _special_), the
3067 program is ill-formed.
3069 Similar language is found in [temp.explicit]. */
3070 error ("specialization of implicitly-declared special member function");
3071 return error_mark_node
;
3074 name
= DECL_NAME (decl
);
3077 /* For a type-conversion operator, We might be looking for
3078 `operator int' which will be a specialization of
3079 `operator T'. Grab all the conversion operators, and
3080 then select from them. */
3081 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3082 ? conv_op_identifier
: name
);
3084 if (fns
== NULL_TREE
)
3086 error ("no member function %qD declared in %qT", name
, ctype
);
3087 return error_mark_node
;
3090 TREE_OPERAND (declarator
, 0) = fns
;
3093 /* Figure out what exactly is being specialized at this point.
3094 Note that for an explicit instantiation, even one for a
3095 member function, we cannot tell a priori whether the
3096 instantiation is for a member template, or just a member
3097 function of a template class. Even if a member template is
3098 being instantiated, the member template arguments may be
3099 elided if they can be deduced from the rest of the
3101 tmpl
= determine_specialization (declarator
, decl
,
3103 member_specialization
,
3107 if (!tmpl
|| tmpl
== error_mark_node
)
3108 /* We couldn't figure out what this declaration was
3110 return error_mark_node
;
3113 if (TREE_CODE (decl
) == FUNCTION_DECL
3114 && DECL_HIDDEN_FRIEND_P (tmpl
))
3116 auto_diagnostic_group d
;
3117 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3118 "friend declaration %qD is not visible to "
3119 "explicit specialization", tmpl
))
3120 inform (DECL_SOURCE_LOCATION (tmpl
),
3121 "friend declaration here");
3123 else if (!ctype
&& !is_friend
3124 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3125 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3127 tree gen_tmpl
= most_general_template (tmpl
);
3129 if (explicit_instantiation
)
3131 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3132 is done by do_decl_instantiation later. */
3134 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3135 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3137 if (arg_depth
> parm_depth
)
3139 /* If TMPL is not the most general template (for
3140 example, if TMPL is a friend template that is
3141 injected into namespace scope), then there will
3142 be too many levels of TARGS. Remove some of them
3147 new_targs
= make_tree_vec (parm_depth
);
3148 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3149 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3150 = TREE_VEC_ELT (targs
, i
);
3154 return instantiate_template (tmpl
, targs
, tf_error
);
3157 /* If we thought that the DECL was a member function, but it
3158 turns out to be specializing a static member function,
3159 make DECL a static member function as well. */
3160 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3161 && DECL_STATIC_FUNCTION_P (tmpl
)
3162 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3163 revert_static_member_fn (decl
);
3165 /* If this is a specialization of a member template of a
3166 template class, we want to return the TEMPLATE_DECL, not
3167 the specialization of it. */
3168 if (tsk
== tsk_template
&& !was_template_id
)
3170 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3171 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3172 DECL_INITIAL (result
) = NULL_TREE
;
3176 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3177 DECL_SOURCE_LOCATION (result
)
3178 = DECL_SOURCE_LOCATION (decl
);
3179 /* We want to use the argument list specified in the
3180 definition, not in the original declaration. */
3181 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3182 for (parm
= DECL_ARGUMENTS (result
); parm
;
3183 parm
= DECL_CHAIN (parm
))
3184 DECL_CONTEXT (parm
) = result
;
3186 return register_specialization (tmpl
, gen_tmpl
, targs
,
3190 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3191 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3193 if (was_template_id
)
3194 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3196 /* Inherit default function arguments from the template
3197 DECL is specializing. */
3198 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3199 copy_default_args_to_explicit_spec (decl
);
3201 /* This specialization has the same protection as the
3202 template it specializes. */
3203 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3204 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3206 /* 7.1.1-1 [dcl.stc]
3208 A storage-class-specifier shall not be specified in an
3209 explicit specialization...
3211 The parser rejects these, so unless action is taken here,
3212 explicit function specializations will always appear with
3215 The action recommended by the C++ CWG in response to C++
3216 defect report 605 is to make the storage class and linkage
3217 of the explicit specialization match the templated function:
3219 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3221 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3223 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3224 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3226 /* A concept cannot be specialized. */
3227 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3229 error ("explicit specialization of function concept %qD",
3231 return error_mark_node
;
3234 /* This specialization has the same linkage and visibility as
3235 the function template it specializes. */
3236 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3237 if (! TREE_PUBLIC (decl
))
3239 DECL_INTERFACE_KNOWN (decl
) = 1;
3240 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3242 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3243 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3245 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3246 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3250 /* If DECL is a friend declaration, declared using an
3251 unqualified name, the namespace associated with DECL may
3252 have been set incorrectly. For example, in:
3254 template <typename T> void f(T);
3256 struct S { friend void f<int>(int); }
3259 we will have set the DECL_CONTEXT for the friend
3260 declaration to N, rather than to the global namespace. */
3261 if (DECL_NAMESPACE_SCOPE_P (decl
))
3262 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3264 if (is_friend
&& !have_def
)
3265 /* This is not really a declaration of a specialization.
3266 It's just the name of an instantiation. But, it's not
3267 a request for an instantiation, either. */
3268 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3269 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3270 /* A specialization is not necessarily COMDAT. */
3271 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3272 && DECL_DECLARED_INLINE_P (decl
));
3273 else if (VAR_P (decl
))
3274 DECL_COMDAT (decl
) = false;
3276 /* If this is a full specialization, register it so that we can find
3277 it again. Partial specializations will be registered in
3278 process_partial_specialization. */
3279 if (!processing_template_decl
)
3281 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3283 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3288 /* A 'structor should already have clones. */
3289 gcc_assert (decl
== error_mark_node
3290 || variable_template_p (tmpl
)
3291 || !(DECL_CONSTRUCTOR_P (decl
)
3292 || DECL_DESTRUCTOR_P (decl
))
3293 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3300 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3301 parameters. These are represented in the same format used for
3302 DECL_TEMPLATE_PARMS. */
3305 comp_template_parms (const_tree parms1
, const_tree parms2
)
3310 if (parms1
== parms2
)
3313 for (p1
= parms1
, p2
= parms2
;
3314 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3315 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3317 tree t1
= TREE_VALUE (p1
);
3318 tree t2
= TREE_VALUE (p2
);
3321 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3322 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3324 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3327 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3329 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3330 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3332 /* If either of the template parameters are invalid, assume
3333 they match for the sake of error recovery. */
3334 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3337 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3340 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3341 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3342 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3344 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3349 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3350 /* One set of parameters has more parameters lists than the
3357 /* Returns true if two template parameters are declared with
3358 equivalent constraints. */
3361 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3363 tree req1
= TREE_TYPE (parm1
);
3364 tree req2
= TREE_TYPE (parm2
);
3368 return cp_tree_equal (req1
, req2
);
3372 /* Returns true when two template parameters are equivalent. */
3375 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3377 tree decl1
= TREE_VALUE (parm1
);
3378 tree decl2
= TREE_VALUE (parm2
);
3380 /* If either of the template parameters are invalid, assume
3381 they match for the sake of error recovery. */
3382 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3385 /* ... they declare parameters of the same kind. */
3386 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3389 /* ... one parameter was introduced by a parameter declaration, then
3390 both are. This case arises as a result of eagerly rewriting declarations
3392 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3395 /* ... if either declares a pack, they both do. */
3396 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3399 if (TREE_CODE (decl1
) == PARM_DECL
)
3401 /* ... if they declare non-type parameters, the types are equivalent. */
3402 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3405 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3407 /* ... if they declare template template parameters, their template
3408 parameter lists are equivalent. */
3409 if (!template_heads_equivalent_p (decl1
, decl2
))
3413 /* ... if they are declared with a qualified-concept name, they both
3414 are, and those names are equivalent. */
3415 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3418 /* Returns true if two template parameters lists are equivalent.
3419 Two template parameter lists are equivalent if they have the
3420 same length and their corresponding parameters are equivalent.
3422 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3423 data structure returned by DECL_TEMPLATE_PARMS.
3425 This is generally the same implementation as comp_template_parms
3426 except that it also the concept names and arguments used to
3427 introduce parameters. */
3430 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3432 if (parms1
== parms2
)
3435 const_tree p1
= parms1
;
3436 const_tree p2
= parms2
;
3437 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3439 tree list1
= TREE_VALUE (p1
);
3440 tree list2
= TREE_VALUE (p2
);
3442 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3445 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3447 tree parm1
= TREE_VEC_ELT (list1
, i
);
3448 tree parm2
= TREE_VEC_ELT (list2
, i
);
3449 if (!template_parameters_equivalent_p (parm1
, parm2
))
3453 p1
= TREE_CHAIN (p1
);
3454 p2
= TREE_CHAIN (p2
);
3457 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3463 /* Return true if the requires-clause of the template parameter lists are
3464 equivalent and false otherwise. */
3466 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3468 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3469 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3470 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3472 if (!cp_tree_equal (req1
, req2
))
3477 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3478 Two template heads are equivalent if their template parameter
3479 lists are equivalent and their requires clauses are equivalent.
3481 In pre-C++20, this is equivalent to calling comp_template_parms
3482 for the template parameters of TMPL1 and TMPL2. */
3485 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3487 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3488 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3490 /* Don't change the matching rules for pre-C++20. */
3491 if (cxx_dialect
< cxx20
)
3492 return comp_template_parms (parms1
, parms2
);
3494 /* ... have the same number of template parameters, and their
3495 corresponding parameters are equivalent. */
3496 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3499 /* ... if either has a requires-clause, they both do and their
3500 corresponding constraint-expressions are equivalent. */
3501 return template_requirements_equivalent_p (parms1
, parms2
);
3504 /* Determine whether PARM is a parameter pack. */
3507 template_parameter_pack_p (const_tree parm
)
3509 /* Determine if we have a non-type template parameter pack. */
3510 if (TREE_CODE (parm
) == PARM_DECL
)
3511 return (DECL_TEMPLATE_PARM_P (parm
)
3512 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3513 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3514 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3516 /* If this is a list of template parameters, we could get a
3517 TYPE_DECL or a TEMPLATE_DECL. */
3518 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3519 parm
= TREE_TYPE (parm
);
3521 /* Otherwise it must be a type template parameter. */
3522 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3523 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3524 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3527 /* Determine if T is a function parameter pack. */
3530 function_parameter_pack_p (const_tree t
)
3532 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3533 return DECL_PACK_P (t
);
3537 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3538 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3541 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3543 if (! primary_func_tmpl_inst
3544 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3545 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3548 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3551 /* Return true iff the function parameter PARAM_DECL was expanded
3552 from the function parameter pack PACK. */
3555 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3557 if (DECL_ARTIFICIAL (param_decl
)
3558 || !function_parameter_pack_p (pack
))
3561 /* The parameter pack and its pack arguments have the same
3563 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3566 /* Determine whether ARGS describes a variadic template args list,
3567 i.e., one that is terminated by a template argument pack. */
3570 template_args_variadic_p (tree args
)
3575 if (args
== NULL_TREE
)
3578 args
= INNERMOST_TEMPLATE_ARGS (args
);
3579 nargs
= TREE_VEC_LENGTH (args
);
3584 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3586 return ARGUMENT_PACK_P (last_parm
);
3589 /* Generate a new name for the parameter pack name NAME (an
3590 IDENTIFIER_NODE) that incorporates its */
3593 make_ith_pack_parameter_name (tree name
, int i
)
3595 /* Munge the name to include the parameter index. */
3596 #define NUMBUF_LEN 128
3597 char numbuf
[NUMBUF_LEN
];
3601 if (name
== NULL_TREE
)
3603 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3604 newname_len
= IDENTIFIER_LENGTH (name
)
3605 + strlen (numbuf
) + 2;
3606 newname
= (char*)alloca (newname_len
);
3607 snprintf (newname
, newname_len
,
3608 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3609 return get_identifier (newname
);
3612 /* Return true if T is a primary function, class or alias template
3613 specialization, not including the template pattern. */
3616 primary_template_specialization_p (const_tree t
)
3621 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3622 return (DECL_LANG_SPECIFIC (t
)
3623 && DECL_USE_TEMPLATE (t
)
3624 && DECL_TEMPLATE_INFO (t
)
3625 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3626 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3627 return (CLASSTYPE_TEMPLATE_INFO (t
)
3628 && CLASSTYPE_USE_TEMPLATE (t
)
3629 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3630 else if (alias_template_specialization_p (t
, nt_transparent
))
3635 /* Return true if PARM is a template template parameter. */
3638 template_template_parameter_p (const_tree parm
)
3640 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3643 /* Return true iff PARM is a DECL representing a type template
3647 template_type_parameter_p (const_tree parm
)
3650 && (TREE_CODE (parm
) == TYPE_DECL
3651 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3652 && DECL_TEMPLATE_PARM_P (parm
));
3655 /* Return the template parameters of T if T is a
3656 primary template instantiation, NULL otherwise. */
3659 get_primary_template_innermost_parameters (const_tree t
)
3661 tree parms
= NULL
, template_info
= NULL
;
3663 if ((template_info
= get_template_info (t
))
3664 && primary_template_specialization_p (t
))
3665 parms
= INNERMOST_TEMPLATE_PARMS
3666 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3671 /* Return the template parameters of the LEVELth level from the full list
3672 of template parameters PARMS. */
3675 get_template_parms_at_level (tree parms
, int level
)
3679 || TREE_CODE (parms
) != TREE_LIST
3680 || level
> TMPL_PARMS_DEPTH (parms
))
3683 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3684 if (TMPL_PARMS_DEPTH (p
) == level
)
3690 /* Returns the template arguments of T if T is a template instantiation,
3694 get_template_innermost_arguments (const_tree t
)
3696 tree args
= NULL
, template_info
= NULL
;
3698 if ((template_info
= get_template_info (t
))
3699 && TI_ARGS (template_info
))
3700 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3705 /* Return the argument pack elements of T if T is a template argument pack,
3709 get_template_argument_pack_elems (const_tree t
)
3711 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3712 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3715 return ARGUMENT_PACK_ARGS (t
);
3718 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3719 ARGUMENT_PACK_SELECT represents. */
3722 argument_pack_select_arg (tree t
)
3724 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3725 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3727 /* If the selected argument is an expansion E, that most likely means we were
3728 called from gen_elem_of_pack_expansion_instantiation during the
3729 substituting of an argument pack (of which the Ith element is a pack
3730 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3731 In this case, the Ith element resulting from this substituting is going to
3732 be a pack expansion, which pattern is the pattern of E. Let's return the
3733 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3734 resulting pack expansion from it. */
3735 if (PACK_EXPANSION_P (arg
))
3737 /* Make sure we aren't throwing away arg info. */
3738 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3739 arg
= PACK_EXPANSION_PATTERN (arg
);
3746 /* True iff FN is a function representing a built-in variadic parameter
3750 builtin_pack_fn_p (tree fn
)
3753 || TREE_CODE (fn
) != FUNCTION_DECL
3754 || !DECL_IS_BUILTIN (fn
))
3757 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3763 /* True iff CALL is a call to a function representing a built-in variadic
3767 builtin_pack_call_p (tree call
)
3769 if (TREE_CODE (call
) != CALL_EXPR
)
3771 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3774 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3777 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3780 tree ohi
= CALL_EXPR_ARG (call
, 0);
3781 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3782 false/*fn*/, true/*int_cst*/);
3784 if (value_dependent_expression_p (hi
))
3788 call
= copy_node (call
);
3789 CALL_EXPR_ARG (call
, 0) = hi
;
3791 tree ex
= make_pack_expansion (call
, complain
);
3792 tree vec
= make_tree_vec (1);
3793 TREE_VEC_ELT (vec
, 0) = ex
;
3798 hi
= cxx_constant_value (hi
);
3799 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3801 /* Calculate the largest value of len that won't make the size of the vec
3802 overflow an int. The compiler will exceed resource limits long before
3803 this, but it seems a decent place to diagnose. */
3804 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3806 if (len
< 0 || len
> max
)
3808 if ((complain
& tf_error
)
3809 && hi
!= error_mark_node
)
3810 error ("argument to %<__integer_pack%> must be between 0 and %d",
3812 return error_mark_node
;
3815 tree vec
= make_tree_vec (len
);
3817 for (int i
= 0; i
< len
; ++i
)
3818 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3824 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3828 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3831 if (!builtin_pack_call_p (call
))
3834 tree fn
= CALL_EXPR_FN (call
);
3836 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3837 return expand_integer_pack (call
, args
, complain
, in_decl
);
3842 /* Structure used to track the progress of find_parameter_packs_r. */
3843 struct find_parameter_pack_data
3845 /* TREE_LIST that will contain all of the parameter packs found by
3847 tree
* parameter_packs
;
3849 /* Set of AST nodes that have been visited by the traversal. */
3850 hash_set
<tree
> *visited
;
3852 /* True iff we're making a type pack expansion. */
3853 bool type_pack_expansion_p
;
3856 /* Identifies all of the argument packs that occur in a template
3857 argument and appends them to the TREE_LIST inside DATA, which is a
3858 find_parameter_pack_data structure. This is a subroutine of
3859 make_pack_expansion and uses_parameter_packs. */
3861 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3864 struct find_parameter_pack_data
* ppd
=
3865 (struct find_parameter_pack_data
*)data
;
3866 bool parameter_pack_p
= false;
3868 /* Don't look through typedefs; we are interested in whether a
3869 parameter pack is actually written in the expression/type we're
3870 looking at, not the target type. */
3871 if (TYPE_P (t
) && typedef_variant_p (t
))
3873 /* But do look at arguments for an alias template. */
3874 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3875 cp_walk_tree (&TI_ARGS (tinfo
),
3876 &find_parameter_packs_r
,
3882 /* Identify whether this is a parameter pack or not. */
3883 switch (TREE_CODE (t
))
3885 case TEMPLATE_PARM_INDEX
:
3886 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3887 parameter_pack_p
= true;
3890 case TEMPLATE_TYPE_PARM
:
3891 t
= TYPE_MAIN_VARIANT (t
);
3893 case TEMPLATE_TEMPLATE_PARM
:
3894 /* If the placeholder appears in the decl-specifier-seq of a function
3895 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3896 is a pack expansion, the invented template parameter is a template
3898 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3899 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3900 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3901 parameter_pack_p
= true;
3906 if (DECL_PACK_P (t
))
3908 /* We don't want to walk into the type of a PARM_DECL,
3909 because we don't want to see the type parameter pack. */
3911 parameter_pack_p
= true;
3916 if (DECL_PACK_P (t
))
3918 /* We don't want to walk into the type of a variadic capture proxy,
3919 because we don't want to see the type parameter pack. */
3921 parameter_pack_p
= true;
3923 else if (variable_template_specialization_p (t
))
3925 cp_walk_tree (&DECL_TI_ARGS (t
),
3926 find_parameter_packs_r
,
3933 if (builtin_pack_call_p (t
))
3934 parameter_pack_p
= true;
3938 parameter_pack_p
= true;
3941 /* Not a parameter pack. */
3945 if (parameter_pack_p
)
3947 /* Add this parameter pack to the list. */
3948 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3952 cp_walk_tree (&TYPE_CONTEXT (t
),
3953 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3955 /* This switch statement will return immediately if we don't find a
3956 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3957 switch (TREE_CODE (t
))
3959 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3960 /* Check the template itself. */
3961 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3962 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3967 tree decl
= DECL_EXPR_DECL (t
);
3968 /* Ignore the declaration of a capture proxy for a parameter pack. */
3969 if (is_capture_proxy (decl
))
3971 if (is_typedef_decl (decl
))
3972 /* Since we stop at typedefs above, we need to look through them at
3973 the point of the DECL_EXPR. */
3974 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3975 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3980 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3982 cp_walk_tree (&TREE_TYPE (t
),
3983 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3986 case TYPE_PACK_EXPANSION
:
3987 case EXPR_PACK_EXPANSION
:
3992 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3997 case IDENTIFIER_NODE
:
3998 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4005 /* Since we defer implicit capture, look in the parms and body. */
4006 tree fn
= lambda_function (t
);
4007 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4009 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4016 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4017 type_pack_expansion_p to false so that any placeholders
4018 within the expression don't get marked as parameter packs. */
4019 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4020 ppd
->type_pack_expansion_p
= false;
4021 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4023 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4029 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4031 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4033 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4035 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4046 /* Determines if the expression or type T uses any parameter packs. */
4048 uses_parameter_packs (tree t
)
4050 tree parameter_packs
= NULL_TREE
;
4051 struct find_parameter_pack_data ppd
;
4052 ppd
.parameter_packs
= ¶meter_packs
;
4053 ppd
.visited
= new hash_set
<tree
>;
4054 ppd
.type_pack_expansion_p
= false;
4055 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4057 return parameter_packs
;
4060 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4061 representation a base-class initializer into a parameter pack
4062 expansion. If all goes well, the resulting node will be an
4063 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4066 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4069 tree parameter_packs
= NULL_TREE
;
4070 bool for_types
= false;
4071 struct find_parameter_pack_data ppd
;
4073 if (!arg
|| arg
== error_mark_node
)
4076 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4078 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4079 class initializer. In this case, the TREE_PURPOSE will be a
4080 _TYPE node (representing the base class expansion we're
4081 initializing) and the TREE_VALUE will be a TREE_LIST
4082 containing the initialization arguments.
4084 The resulting expansion looks somewhat different from most
4085 expansions. Rather than returning just one _EXPANSION, we
4086 return a TREE_LIST whose TREE_PURPOSE is a
4087 TYPE_PACK_EXPANSION containing the bases that will be
4088 initialized. The TREE_VALUE will be identical to the
4089 original TREE_VALUE, which is a list of arguments that will
4090 be passed to each base. We do not introduce any new pack
4091 expansion nodes into the TREE_VALUE (although it is possible
4092 that some already exist), because the TREE_PURPOSE and
4093 TREE_VALUE all need to be expanded together with the same
4094 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4095 resulting TREE_PURPOSE will mention the parameter packs in
4096 both the bases and the arguments to the bases. */
4099 tree parameter_packs
= NULL_TREE
;
4101 /* Determine which parameter packs will be used by the base
4103 ppd
.visited
= new hash_set
<tree
>;
4104 ppd
.parameter_packs
= ¶meter_packs
;
4105 ppd
.type_pack_expansion_p
= false;
4106 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4107 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4110 if (parameter_packs
== NULL_TREE
)
4112 if (complain
& tf_error
)
4113 error ("base initializer expansion %qT contains no parameter packs",
4116 return error_mark_node
;
4119 if (TREE_VALUE (arg
) != void_type_node
)
4121 /* Collect the sets of parameter packs used in each of the
4122 initialization arguments. */
4123 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4125 /* Determine which parameter packs will be expanded in this
4127 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4134 /* Create the pack expansion type for the base type. */
4135 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4136 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4137 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4138 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4140 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4141 they will rarely be compared to anything. */
4142 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4144 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4147 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4150 /* Build the PACK_EXPANSION_* node. */
4152 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4153 : make_node (EXPR_PACK_EXPANSION
);
4154 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4155 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4157 /* Propagate type and const-expression information. */
4158 TREE_TYPE (result
) = TREE_TYPE (arg
);
4159 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4160 /* Mark this read now, since the expansion might be length 0. */
4161 mark_exp_read (arg
);
4164 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4165 they will rarely be compared to anything. */
4166 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4168 /* Determine which parameter packs will be expanded. */
4169 ppd
.parameter_packs
= ¶meter_packs
;
4170 ppd
.visited
= new hash_set
<tree
>;
4171 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4172 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4175 /* Make sure we found some parameter packs. */
4176 if (parameter_packs
== NULL_TREE
)
4178 if (complain
& tf_error
)
4181 error ("expansion pattern %qT contains no parameter packs", arg
);
4183 error ("expansion pattern %qE contains no parameter packs", arg
);
4185 return error_mark_node
;
4187 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4189 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4194 /* Checks T for any "bare" parameter packs, which have not yet been
4195 expanded, and issues an error if any are found. This operation can
4196 only be done on full expressions or types (e.g., an expression
4197 statement, "if" condition, etc.), because we could have expressions like:
4199 foo(f(g(h(args)))...)
4201 where "args" is a parameter pack. check_for_bare_parameter_packs
4202 should not be called for the subexpressions args, h(args),
4203 g(h(args)), or f(g(h(args))), because we would produce erroneous
4206 Returns TRUE and emits an error if there were bare parameter packs,
4207 returns FALSE otherwise. */
4209 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4211 tree parameter_packs
= NULL_TREE
;
4212 struct find_parameter_pack_data ppd
;
4214 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4217 /* A lambda might use a parameter pack from the containing context. */
4218 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4219 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4222 if (TREE_CODE (t
) == TYPE_DECL
)
4225 ppd
.parameter_packs
= ¶meter_packs
;
4226 ppd
.visited
= new hash_set
<tree
>;
4227 ppd
.type_pack_expansion_p
= false;
4228 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4231 if (parameter_packs
)
4233 if (loc
== UNKNOWN_LOCATION
)
4234 loc
= cp_expr_loc_or_input_loc (t
);
4235 error_at (loc
, "parameter packs not expanded with %<...%>:");
4236 while (parameter_packs
)
4238 tree pack
= TREE_VALUE (parameter_packs
);
4239 tree name
= NULL_TREE
;
4241 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4242 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4243 name
= TYPE_NAME (pack
);
4244 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4245 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4246 else if (TREE_CODE (pack
) == CALL_EXPR
)
4247 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4249 name
= DECL_NAME (pack
);
4252 inform (loc
, " %qD", name
);
4254 inform (loc
, " %s", "<anonymous>");
4256 parameter_packs
= TREE_CHAIN (parameter_packs
);
4265 /* Expand any parameter packs that occur in the template arguments in
4268 expand_template_argument_pack (tree args
)
4270 if (args
== error_mark_node
)
4271 return error_mark_node
;
4273 tree result_args
= NULL_TREE
;
4274 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4275 int num_result_args
= -1;
4276 int non_default_args_count
= -1;
4278 /* First, determine if we need to expand anything, and the number of
4279 slots we'll need. */
4280 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4282 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4283 if (arg
== NULL_TREE
)
4285 if (ARGUMENT_PACK_P (arg
))
4287 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4288 if (num_result_args
< 0)
4289 num_result_args
= in_arg
+ num_packed
;
4291 num_result_args
+= num_packed
;
4295 if (num_result_args
>= 0)
4300 /* If no expansion is necessary, we're done. */
4301 if (num_result_args
< 0)
4304 /* Expand arguments. */
4305 result_args
= make_tree_vec (num_result_args
);
4306 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4307 non_default_args_count
=
4308 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4309 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4311 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4312 if (ARGUMENT_PACK_P (arg
))
4314 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4315 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4316 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4317 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4318 if (non_default_args_count
> 0)
4319 non_default_args_count
+= num_packed
- 1;
4323 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4327 if (non_default_args_count
>= 0)
4328 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4332 /* Checks if DECL shadows a template parameter.
4334 [temp.local]: A template-parameter shall not be redeclared within its
4335 scope (including nested scopes).
4337 Emits an error and returns TRUE if the DECL shadows a parameter,
4338 returns FALSE otherwise. */
4341 check_template_shadow (tree decl
)
4345 /* If we're not in a template, we can't possibly shadow a template
4347 if (!current_template_parms
)
4350 /* Figure out what we're shadowing. */
4351 decl
= OVL_FIRST (decl
);
4352 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4354 /* If there's no previous binding for this name, we're not shadowing
4355 anything, let alone a template parameter. */
4359 /* If we're not shadowing a template parameter, we're done. Note
4360 that OLDDECL might be an OVERLOAD (or perhaps even an
4361 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4363 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4366 /* We check for decl != olddecl to avoid bogus errors for using a
4367 name inside a class. We check TPFI to avoid duplicate errors for
4368 inline member templates. */
4370 || (DECL_TEMPLATE_PARM_P (decl
)
4371 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4374 /* Don't complain about the injected class name, as we've already
4375 complained about the class itself. */
4376 if (DECL_SELF_REFERENCE_P (decl
))
4379 if (DECL_TEMPLATE_PARM_P (decl
))
4380 error ("declaration of template parameter %q+D shadows "
4381 "template parameter", decl
);
4383 error ("declaration of %q+#D shadows template parameter", decl
);
4384 inform (DECL_SOURCE_LOCATION (olddecl
),
4385 "template parameter %qD declared here", olddecl
);
4389 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4390 ORIG_LEVEL, DECL, and TYPE. */
4393 build_template_parm_index (int index
,
4399 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4400 TEMPLATE_PARM_IDX (t
) = index
;
4401 TEMPLATE_PARM_LEVEL (t
) = level
;
4402 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4403 TEMPLATE_PARM_DECL (t
) = decl
;
4404 TREE_TYPE (t
) = type
;
4405 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4406 TREE_READONLY (t
) = TREE_READONLY (decl
);
4411 /* Find the canonical type parameter for the given template type
4412 parameter. Returns the canonical type parameter, which may be TYPE
4413 if no such parameter existed. */
4416 canonical_type_parameter (tree type
)
4418 int idx
= TEMPLATE_TYPE_IDX (type
);
4420 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4422 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4423 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4425 for (tree list
= (*canonical_template_parms
)[idx
];
4426 list
; list
= TREE_CHAIN (list
))
4427 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4428 return TREE_VALUE (list
);
4430 (*canonical_template_parms
)[idx
]
4431 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4435 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4436 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4437 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4438 new one is created. */
4441 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4442 tsubst_flags_t complain
)
4444 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4445 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4446 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4447 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4449 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4451 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4452 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4454 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4455 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4456 DECL_ARTIFICIAL (decl
) = 1;
4457 SET_DECL_TEMPLATE_PARM_P (decl
);
4459 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4460 TEMPLATE_PARM_LEVEL (index
) - levels
,
4461 TEMPLATE_PARM_ORIG_LEVEL (index
),
4463 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4464 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4465 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4467 /* Template template parameters need this. */
4469 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4471 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4472 TYPE_DECL
, DECL_NAME (decl
), type
);
4473 DECL_TEMPLATE_RESULT (decl
) = inner
;
4474 DECL_ARTIFICIAL (inner
) = true;
4475 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4476 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4479 /* Attach the TPI to the decl. */
4480 if (TREE_CODE (inner
) == TYPE_DECL
)
4481 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4483 DECL_INITIAL (decl
) = tpi
;
4486 return TEMPLATE_PARM_DESCENDANTS (index
);
4489 /* Process information from new template parameter PARM and append it
4490 to the LIST being built. This new parameter is a non-type
4491 parameter iff IS_NON_TYPE is true. This new parameter is a
4492 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4496 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4497 bool is_non_type
, bool is_parameter_pack
)
4499 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4500 tree prev
= NULL_TREE
;
4505 prev
= tree_last (list
);
4507 tree p
= TREE_VALUE (prev
);
4508 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4509 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4510 else if (TREE_CODE (p
) == PARM_DECL
)
4511 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4516 tree decl
= NULL_TREE
;
4517 tree defval
= TREE_PURPOSE (parm
);
4518 tree constr
= TREE_TYPE (parm
);
4522 parm
= TREE_VALUE (parm
);
4524 SET_DECL_TEMPLATE_PARM_P (parm
);
4526 if (TREE_TYPE (parm
) != error_mark_node
)
4530 The top-level cv-qualifiers on the template-parameter are
4531 ignored when determining its type. */
4532 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4533 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4534 TREE_TYPE (parm
) = error_mark_node
;
4535 else if (uses_parameter_packs (TREE_TYPE (parm
))
4536 && !is_parameter_pack
4537 /* If we're in a nested template parameter list, the template
4538 template parameter could be a parameter pack. */
4539 && processing_template_parmlist
== 1)
4541 /* This template parameter is not a parameter pack, but it
4542 should be. Complain about "bare" parameter packs. */
4543 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4545 /* Recover by calling this a parameter pack. */
4546 is_parameter_pack
= true;
4550 /* A template parameter is not modifiable. */
4551 TREE_CONSTANT (parm
) = 1;
4552 TREE_READONLY (parm
) = 1;
4553 decl
= build_decl (parm_loc
,
4554 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4555 TREE_CONSTANT (decl
) = 1;
4556 TREE_READONLY (decl
) = 1;
4557 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4558 = build_template_parm_index (idx
, processing_template_decl
,
4559 processing_template_decl
,
4560 decl
, TREE_TYPE (parm
));
4562 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4563 = is_parameter_pack
;
4568 parm
= TREE_VALUE (TREE_VALUE (parm
));
4570 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4572 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4573 /* This is for distinguishing between real templates and template
4574 template parameters */
4575 TREE_TYPE (parm
) = t
;
4577 /* any_template_parm_r expects to be able to get the targs of a
4578 DECL_TEMPLATE_RESULT. */
4579 tree result
= DECL_TEMPLATE_RESULT (parm
);
4580 TREE_TYPE (result
) = t
;
4581 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4582 tree tinfo
= build_template_info (parm
, args
);
4583 retrofit_lang_decl (result
);
4584 DECL_TEMPLATE_INFO (result
) = tinfo
;
4590 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4591 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4592 decl
= build_decl (parm_loc
,
4593 TYPE_DECL
, parm
, t
);
4596 TYPE_NAME (t
) = decl
;
4597 TYPE_STUB_DECL (t
) = decl
;
4599 TEMPLATE_TYPE_PARM_INDEX (t
)
4600 = build_template_parm_index (idx
, processing_template_decl
,
4601 processing_template_decl
,
4602 decl
, TREE_TYPE (parm
));
4603 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4604 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4605 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4607 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4609 DECL_ARTIFICIAL (decl
) = 1;
4610 SET_DECL_TEMPLATE_PARM_P (decl
);
4612 /* Build requirements for the type/template parameter.
4613 This must be done after SET_DECL_TEMPLATE_PARM_P or
4614 process_template_parm could fail. */
4615 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4617 decl
= pushdecl (decl
);
4621 /* Build the parameter node linking the parameter declaration,
4622 its default argument (if any), and its constraints (if any). */
4623 parm
= build_tree_list (defval
, parm
);
4624 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4627 TREE_CHAIN (prev
) = parm
;
4634 /* The end of a template parameter list has been reached. Process the
4635 tree list into a parameter vector, converting each parameter into a more
4636 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4640 end_template_parm_list (tree parms
)
4642 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4644 /* Pop the dummy parameter level and add the real one. We do not
4645 morph the dummy parameter in place, as it might have been
4646 captured by a (nested) template-template-parm. */
4647 current_template_parms
= TREE_CHAIN (current_template_parms
);
4649 current_template_parms
4650 = tree_cons (size_int (processing_template_decl
),
4651 saved_parmlist
, current_template_parms
);
4653 for (unsigned ix
= 0; parms
; ix
++)
4656 parms
= TREE_CHAIN (parms
);
4657 TREE_CHAIN (parm
) = NULL_TREE
;
4659 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4662 --processing_template_parmlist
;
4664 return saved_parmlist
;
4667 // Explicitly indicate the end of the template parameter list. We assume
4668 // that the current template parameters have been constructed and/or
4669 // managed explicitly, as when creating new template template parameters
4670 // from a shorthand constraint.
4672 end_template_parm_list ()
4674 --processing_template_parmlist
;
4677 /* end_template_decl is called after a template declaration is seen. */
4680 end_template_decl (void)
4682 reset_specialization ();
4684 if (! processing_template_decl
)
4687 /* This matches the pushlevel in begin_template_parm_list. */
4690 --processing_template_decl
;
4691 current_template_parms
= TREE_CHAIN (current_template_parms
);
4694 /* Takes a TREE_LIST representing a template parameter and convert it
4695 into an argument suitable to be passed to the type substitution
4696 functions. Note that If the TREE_LIST contains an error_mark
4697 node, the returned argument is error_mark_node. */
4700 template_parm_to_arg (tree t
)
4704 || TREE_CODE (t
) != TREE_LIST
)
4707 if (error_operand_p (TREE_VALUE (t
)))
4708 return error_mark_node
;
4712 if (TREE_CODE (t
) == TYPE_DECL
4713 || TREE_CODE (t
) == TEMPLATE_DECL
)
4717 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4719 /* Turn this argument into a TYPE_ARGUMENT_PACK
4720 with a single element, which expands T. */
4721 tree vec
= make_tree_vec (1);
4723 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4725 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4727 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4728 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4733 t
= DECL_INITIAL (t
);
4735 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4737 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4738 with a single element, which expands T. */
4739 tree vec
= make_tree_vec (1);
4741 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4743 t
= convert_from_reference (t
);
4744 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4746 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4747 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4750 t
= convert_from_reference (t
);
4755 /* Given a single level of template parameters (a TREE_VEC), return it
4756 as a set of template arguments. */
4759 template_parms_level_to_args (tree parms
)
4761 tree a
= copy_node (parms
);
4762 TREE_TYPE (a
) = NULL_TREE
;
4763 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4764 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4767 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4772 /* Given a set of template parameters, return them as a set of template
4773 arguments. The template parameters are represented as a TREE_VEC, in
4774 the form documented in cp-tree.h for template arguments. */
4777 template_parms_to_args (tree parms
)
4780 tree args
= NULL_TREE
;
4781 int length
= TMPL_PARMS_DEPTH (parms
);
4784 /* If there is only one level of template parameters, we do not
4785 create a TREE_VEC of TREE_VECs. Instead, we return a single
4786 TREE_VEC containing the arguments. */
4788 args
= make_tree_vec (length
);
4790 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4792 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4795 TREE_VEC_ELT (args
, --l
) = a
;
4803 /* Within the declaration of a template, return the currently active
4804 template parameters as an argument TREE_VEC. */
4807 current_template_args (void)
4809 return template_parms_to_args (current_template_parms
);
4812 /* Return the fully generic arguments for of TMPL, i.e. what
4813 current_template_args would be while parsing it. */
4816 generic_targs_for (tree tmpl
)
4818 if (tmpl
== NULL_TREE
)
4820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4821 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4822 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4823 template parameter, it has no TEMPLATE_INFO; for a partial
4824 specialization, it has the arguments for the primary template, and we
4825 want the arguments for the partial specialization. */;
4826 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4827 if (tree ti
= get_template_info (result
))
4828 return TI_ARGS (ti
);
4829 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4832 /* Update the declared TYPE by doing any lookups which were thought to be
4833 dependent, but are not now that we know the SCOPE of the declarator. */
4836 maybe_update_decl_type (tree orig_type
, tree scope
)
4838 tree type
= orig_type
;
4840 if (type
== NULL_TREE
)
4843 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4844 type
= TREE_TYPE (type
);
4846 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4847 && dependent_type_p (type
)
4848 /* Don't bother building up the args in this case. */
4849 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4851 /* tsubst in the args corresponding to the template parameters,
4852 including auto if present. Most things will be unchanged, but
4853 make_typename_type and tsubst_qualified_id will resolve
4854 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4855 tree args
= current_template_args ();
4856 tree auto_node
= type_uses_auto (type
);
4860 tree auto_vec
= make_tree_vec (1);
4861 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4862 args
= add_to_template_args (args
, auto_vec
);
4864 pushed
= push_scope (scope
);
4865 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4870 if (type
== error_mark_node
)
4873 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4875 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4878 type
= TYPE_NAME (type
);
4883 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4884 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4885 the new template is a member template. */
4888 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4890 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4891 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4892 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4893 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4894 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4895 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4896 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4897 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4902 struct template_parm_data
4904 /* The level of the template parameters we are currently
4908 /* The index of the specialization argument we are currently
4912 /* An array whose size is the number of template parameters. The
4913 elements are nonzero if the parameter has been used in any one
4914 of the arguments processed so far. */
4917 /* An array whose size is the number of template arguments. The
4918 elements are nonzero if the argument makes use of template
4919 parameters of this level. */
4920 int* arg_uses_template_parms
;
4923 /* Subroutine of push_template_decl used to see if each template
4924 parameter in a partial specialization is used in the explicit
4925 argument list. If T is of the LEVEL given in DATA (which is
4926 treated as a template_parm_data*), then DATA->PARMS is marked
4930 mark_template_parm (tree t
, void* data
)
4934 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4936 template_parm_level_and_index (t
, &level
, &idx
);
4938 if (level
== tpd
->level
)
4940 tpd
->parms
[idx
] = 1;
4941 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4944 /* In C++17 the type of a non-type argument is a deduced context. */
4945 if (cxx_dialect
>= cxx17
4946 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4947 for_each_template_parm (TREE_TYPE (t
),
4948 &mark_template_parm
,
4951 /*include_nondeduced_p=*/false);
4953 /* Return zero so that for_each_template_parm will continue the
4954 traversal of the tree; we want to mark *every* template parm. */
4958 /* Process the partial specialization DECL. */
4961 process_partial_specialization (tree decl
)
4963 tree type
= TREE_TYPE (decl
);
4964 tree tinfo
= get_template_info (decl
);
4965 tree maintmpl
= TI_TEMPLATE (tinfo
);
4966 tree specargs
= TI_ARGS (tinfo
);
4967 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4968 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4971 int nargs
= TREE_VEC_LENGTH (inner_args
);
4974 bool did_error_intro
= false;
4975 struct template_parm_data tpd
;
4976 struct template_parm_data tpd2
;
4978 gcc_assert (current_template_parms
);
4980 /* A concept cannot be specialized. */
4981 if (flag_concepts
&& variable_concept_p (maintmpl
))
4983 error ("specialization of variable concept %q#D", maintmpl
);
4984 return error_mark_node
;
4987 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4988 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4990 /* We check that each of the template parameters given in the
4991 partial specialization is used in the argument list to the
4992 specialization. For example:
4994 template <class T> struct S;
4995 template <class T> struct S<T*>;
4997 The second declaration is OK because `T*' uses the template
4998 parameter T, whereas
5000 template <class T> struct S<int>;
5002 is no good. Even trickier is:
5013 The S2<T> declaration is actually invalid; it is a
5014 full-specialization. Of course,
5017 struct S2<T (*)(U)>;
5019 or some such would have been OK. */
5020 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5021 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5022 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5024 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5025 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5026 for (i
= 0; i
< nargs
; ++i
)
5028 tpd
.current_arg
= i
;
5029 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5030 &mark_template_parm
,
5033 /*include_nondeduced_p=*/false);
5035 for (i
= 0; i
< ntparms
; ++i
)
5036 if (tpd
.parms
[i
] == 0)
5038 /* One of the template parms was not used in a deduced context in the
5040 if (!did_error_intro
)
5042 error ("template parameters not deducible in "
5043 "partial specialization:");
5044 did_error_intro
= true;
5047 inform (input_location
, " %qD",
5048 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5051 if (did_error_intro
)
5052 return error_mark_node
;
5054 /* [temp.class.spec]
5056 The argument list of the specialization shall not be identical to
5057 the implicit argument list of the primary template. */
5059 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5060 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5062 || !strictly_subsumes (current_template_constraints (),
5063 main_args
, maintmpl
)))
5066 error ("partial specialization %q+D does not specialize "
5067 "any template arguments; to define the primary template, "
5068 "remove the template argument list", decl
);
5070 error ("partial specialization %q+D does not specialize any "
5071 "template arguments and is not more constrained than "
5072 "the primary template; to define the primary template, "
5073 "remove the template argument list", decl
);
5074 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5077 /* A partial specialization that replaces multiple parameters of the
5078 primary template with a pack expansion is less specialized for those
5080 if (nargs
< DECL_NTPARMS (maintmpl
))
5082 error ("partial specialization is not more specialized than the "
5083 "primary template because it replaces multiple parameters "
5084 "with a pack expansion");
5085 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5086 /* Avoid crash in process_partial_specialization. */
5090 else if (nargs
> DECL_NTPARMS (maintmpl
))
5092 error ("too many arguments for partial specialization %qT", type
);
5093 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5094 /* Avoid crash below. */
5098 /* If we aren't in a dependent class, we can actually try deduction. */
5099 else if (tpd
.level
== 1
5100 /* FIXME we should be able to handle a partial specialization of a
5101 partial instantiation, but currently we can't (c++/41727). */
5102 && TMPL_ARGS_DEPTH (specargs
) == 1
5103 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5105 auto_diagnostic_group d
;
5106 if (permerror (input_location
, "partial specialization %qD is not "
5107 "more specialized than", decl
))
5108 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5112 /* [temp.class.spec]
5114 A partially specialized non-type argument expression shall not
5115 involve template parameters of the partial specialization except
5116 when the argument expression is a simple identifier.
5118 The type of a template parameter corresponding to a specialized
5119 non-type argument shall not be dependent on a parameter of the
5122 Also, we verify that pack expansions only occur at the
5123 end of the argument list. */
5125 for (i
= 0; i
< nargs
; ++i
)
5127 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5128 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5129 tree packed_args
= NULL_TREE
;
5132 if (ARGUMENT_PACK_P (arg
))
5134 /* Extract the arguments from the argument pack. We'll be
5135 iterating over these in the following loop. */
5136 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5137 len
= TREE_VEC_LENGTH (packed_args
);
5140 for (j
= 0; j
< len
; j
++)
5143 /* Get the Jth argument in the parameter pack. */
5144 arg
= TREE_VEC_ELT (packed_args
, j
);
5146 if (PACK_EXPANSION_P (arg
))
5148 /* Pack expansions must come at the end of the
5150 if ((packed_args
&& j
< len
- 1)
5151 || (!packed_args
&& i
< nargs
- 1))
5153 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5154 error ("parameter pack argument %qE must be at the "
5155 "end of the template argument list", arg
);
5157 error ("parameter pack argument %qT must be at the "
5158 "end of the template argument list", arg
);
5162 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5163 /* We only care about the pattern. */
5164 arg
= PACK_EXPANSION_PATTERN (arg
);
5166 if (/* These first two lines are the `non-type' bit. */
5168 && TREE_CODE (arg
) != TEMPLATE_DECL
5169 /* This next two lines are the `argument expression is not just a
5170 simple identifier' condition and also the `specialized
5171 non-type argument' bit. */
5172 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5173 && !((REFERENCE_REF_P (arg
)
5174 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5175 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5177 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5178 || (packed_args
&& uses_template_parms (arg
)))
5179 error_at (cp_expr_loc_or_input_loc (arg
),
5180 "template argument %qE involves template "
5181 "parameter(s)", arg
);
5184 /* Look at the corresponding template parameter,
5185 marking which template parameters its type depends
5187 tree type
= TREE_TYPE (parm
);
5191 /* We haven't yet initialized TPD2. Do so now. */
5192 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5193 /* The number of parameters here is the number in the
5194 main template, which, as checked in the assertion
5196 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5198 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5201 /* Mark the template parameters. But this time, we're
5202 looking for the template parameters of the main
5203 template, not in the specialization. */
5204 tpd2
.current_arg
= i
;
5205 tpd2
.arg_uses_template_parms
[i
] = 0;
5206 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5207 for_each_template_parm (type
,
5208 &mark_template_parm
,
5211 /*include_nondeduced_p=*/false);
5213 if (tpd2
.arg_uses_template_parms
[i
])
5215 /* The type depended on some template parameters.
5216 If they are fully specialized in the
5217 specialization, that's OK. */
5220 for (j
= 0; j
< nargs
; ++j
)
5221 if (tpd2
.parms
[j
] != 0
5222 && tpd
.arg_uses_template_parms
[j
])
5225 error_n (input_location
, count
,
5226 "type %qT of template argument %qE depends "
5227 "on a template parameter",
5228 "type %qT of template argument %qE depends "
5229 "on template parameters",
5238 /* We should only get here once. */
5239 if (TREE_CODE (decl
) == TYPE_DECL
)
5240 gcc_assert (!COMPLETE_TYPE_P (type
));
5242 // Build the template decl.
5243 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5244 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5245 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5246 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5247 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5249 /* Give template template parms a DECL_CONTEXT of the template
5250 for which they are a parameter. */
5251 for (i
= 0; i
< ntparms
; ++i
)
5253 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5254 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5255 DECL_CONTEXT (parm
) = tmpl
;
5259 /* We didn't register this in check_explicit_specialization so we could
5260 wait until the constraints were set. */
5261 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5263 associate_classtype_constraints (type
);
5265 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5266 = tree_cons (specargs
, tmpl
,
5267 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5268 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5270 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5271 inst
= TREE_CHAIN (inst
))
5273 tree instance
= TREE_VALUE (inst
);
5274 if (TYPE_P (instance
)
5275 ? (COMPLETE_TYPE_P (instance
)
5276 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5277 : DECL_TEMPLATE_INSTANTIATION (instance
))
5279 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5280 tree inst_decl
= (DECL_P (instance
)
5281 ? instance
: TYPE_NAME (instance
));
5284 else if (spec
== error_mark_node
)
5285 permerror (input_location
,
5286 "declaration of %qD ambiguates earlier template "
5287 "instantiation for %qD", decl
, inst_decl
);
5288 else if (TREE_VALUE (spec
) == tmpl
)
5289 permerror (input_location
,
5290 "partial specialization of %qD after instantiation "
5291 "of %qD", decl
, inst_decl
);
5298 /* PARM is a template parameter of some form; return the corresponding
5299 TEMPLATE_PARM_INDEX. */
5302 get_template_parm_index (tree parm
)
5304 if (TREE_CODE (parm
) == PARM_DECL
5305 || TREE_CODE (parm
) == CONST_DECL
)
5306 parm
= DECL_INITIAL (parm
);
5307 else if (TREE_CODE (parm
) == TYPE_DECL
5308 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5309 parm
= TREE_TYPE (parm
);
5310 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5311 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5312 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5313 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5314 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5318 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5319 parameter packs used by the template parameter PARM. */
5322 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5324 /* A type parm can't refer to another parm. */
5325 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5327 else if (TREE_CODE (parm
) == PARM_DECL
)
5329 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5334 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5336 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5337 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5339 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5340 if (template_parameter_pack_p (p
))
5341 /* Any packs in the type are expanded by this parameter. */;
5343 fixed_parameter_pack_p_1 (p
, ppd
);
5347 /* PARM is a template parameter pack. Return any parameter packs used in
5348 its type or the type of any of its template parameters. If there are
5349 any such packs, it will be instantiated into a fixed template parameter
5350 list by partial instantiation rather than be fully deduced. */
5353 fixed_parameter_pack_p (tree parm
)
5355 /* This can only be true in a member template. */
5356 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5358 /* This can only be true for a parameter pack. */
5359 if (!template_parameter_pack_p (parm
))
5361 /* A type parm can't refer to another parm. */
5362 if (TREE_CODE (parm
) == TYPE_DECL
)
5365 tree parameter_packs
= NULL_TREE
;
5366 struct find_parameter_pack_data ppd
;
5367 ppd
.parameter_packs
= ¶meter_packs
;
5368 ppd
.visited
= new hash_set
<tree
>;
5369 ppd
.type_pack_expansion_p
= false;
5371 fixed_parameter_pack_p_1 (parm
, &ppd
);
5374 return parameter_packs
;
5377 /* Check that a template declaration's use of default arguments and
5378 parameter packs is not invalid. Here, PARMS are the template
5379 parameters. IS_PRIMARY is true if DECL is the thing declared by
5380 a primary template. IS_PARTIAL is true if DECL is a partial
5383 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5384 function template declaration or a friend class template
5385 declaration. In the function case, 1 indicates a declaration, 2
5386 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5387 emitted for extraneous default arguments.
5389 Returns TRUE if there were no errors found, FALSE otherwise. */
5392 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5393 bool is_partial
, int is_friend_decl
)
5396 int last_level_to_check
;
5398 bool no_errors
= true;
5402 A default template-argument shall not be specified in a
5403 function template declaration or a function template definition, nor
5404 in the template-parameter-list of the definition of a member of a
5407 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5408 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5409 /* You can't have a function template declaration in a local
5410 scope, nor you can you define a member of a class template in a
5414 if ((TREE_CODE (decl
) == TYPE_DECL
5416 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5417 || (TREE_CODE (decl
) == FUNCTION_DECL
5418 && LAMBDA_FUNCTION_P (decl
)))
5419 /* A lambda doesn't have an explicit declaration; don't complain
5420 about the parms of the enclosing class. */
5423 if (current_class_type
5424 && !TYPE_BEING_DEFINED (current_class_type
)
5425 && DECL_LANG_SPECIFIC (decl
)
5426 && DECL_DECLARES_FUNCTION_P (decl
)
5427 /* If this is either a friend defined in the scope of the class
5428 or a member function. */
5429 && (DECL_FUNCTION_MEMBER_P (decl
)
5430 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5431 : DECL_FRIEND_CONTEXT (decl
)
5432 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5434 /* And, if it was a member function, it really was defined in
5435 the scope of the class. */
5436 && (!DECL_FUNCTION_MEMBER_P (decl
)
5437 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5438 /* We already checked these parameters when the template was
5439 declared, so there's no need to do it again now. This function
5440 was defined in class scope, but we're processing its body now
5441 that the class is complete. */
5444 /* Core issue 226 (C++0x only): the following only applies to class
5447 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5451 If a template-parameter has a default template-argument, all
5452 subsequent template-parameters shall have a default
5453 template-argument supplied. */
5454 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5456 tree inner_parms
= TREE_VALUE (parm_level
);
5457 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5458 int seen_def_arg_p
= 0;
5461 for (i
= 0; i
< ntparms
; ++i
)
5463 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5465 if (parm
== error_mark_node
)
5468 if (TREE_PURPOSE (parm
))
5470 else if (seen_def_arg_p
5471 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5473 error ("no default argument for %qD", TREE_VALUE (parm
));
5474 /* For better subsequent error-recovery, we indicate that
5475 there should have been a default argument. */
5476 TREE_PURPOSE (parm
) = error_mark_node
;
5479 else if (!is_partial
5481 /* Don't complain about an enclosing partial
5483 && parm_level
== parms
5484 && TREE_CODE (decl
) == TYPE_DECL
5486 && template_parameter_pack_p (TREE_VALUE (parm
))
5487 /* A fixed parameter pack will be partially
5488 instantiated into a fixed length list. */
5489 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5491 /* A primary class template can only have one
5492 parameter pack, at the end of the template
5495 error ("parameter pack %q+D must be at the end of the"
5496 " template parameter list", TREE_VALUE (parm
));
5498 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5506 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5510 /* For an ordinary class template, default template arguments are
5511 allowed at the innermost level, e.g.:
5512 template <class T = int>
5514 but, in a partial specialization, they're not allowed even
5515 there, as we have in [temp.class.spec]:
5517 The template parameter list of a specialization shall not
5518 contain default template argument values.
5520 So, for a partial specialization, or for a function template
5521 (in C++98/C++03), we look at all of them. */
5524 /* But, for a primary class template that is not a partial
5525 specialization we look at all template parameters except the
5527 parms
= TREE_CHAIN (parms
);
5529 /* Figure out what error message to issue. */
5530 if (is_friend_decl
== 2)
5531 msg
= G_("default template arguments may not be used in function template "
5532 "friend re-declaration");
5533 else if (is_friend_decl
)
5534 msg
= G_("default template arguments may not be used in template "
5535 "friend declarations");
5536 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5537 msg
= G_("default template arguments may not be used in function templates "
5538 "without %<-std=c++11%> or %<-std=gnu++11%>");
5539 else if (is_partial
)
5540 msg
= G_("default template arguments may not be used in "
5541 "partial specializations");
5542 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5543 msg
= G_("default argument for template parameter for class enclosing %qD");
5545 /* Per [temp.param]/9, "A default template-argument shall not be
5546 specified in the template-parameter-lists of the definition of
5547 a member of a class template that appears outside of the member's
5548 class.", thus if we aren't handling a member of a class template
5549 there is no need to examine the parameters. */
5552 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5553 /* If we're inside a class definition, there's no need to
5554 examine the parameters to the class itself. On the one
5555 hand, they will be checked when the class is defined, and,
5556 on the other, default arguments are valid in things like:
5557 template <class T = double>
5558 struct S { template <class U> void f(U); };
5559 Here the default argument for `S' has no bearing on the
5560 declaration of `f'. */
5561 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5563 /* Check everything. */
5564 last_level_to_check
= 0;
5566 for (parm_level
= parms
;
5567 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5568 parm_level
= TREE_CHAIN (parm_level
))
5570 tree inner_parms
= TREE_VALUE (parm_level
);
5574 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5575 for (i
= 0; i
< ntparms
; ++i
)
5577 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5580 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5585 if (is_friend_decl
== 2)
5592 /* Clear out the default argument so that we are not
5594 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5598 /* At this point, if we're still interested in issuing messages,
5599 they must apply to classes surrounding the object declared. */
5601 msg
= G_("default argument for template parameter for class "
5608 /* Worker for push_template_decl_real, called via
5609 for_each_template_parm. DATA is really an int, indicating the
5610 level of the parameters we are interested in. If T is a template
5611 parameter of that level, return nonzero. */
5614 template_parm_this_level_p (tree t
, void* data
)
5616 int this_level
= *(int *)data
;
5619 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5620 level
= TEMPLATE_PARM_LEVEL (t
);
5622 level
= TEMPLATE_TYPE_LEVEL (t
);
5623 return level
== this_level
;
5626 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5627 DATA is really an int, indicating the innermost outer level of parameters.
5628 If T is a template parameter of that level or further out, return
5632 template_parm_outer_level (tree t
, void *data
)
5634 int this_level
= *(int *)data
;
5637 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5638 level
= TEMPLATE_PARM_LEVEL (t
);
5640 level
= TEMPLATE_TYPE_LEVEL (t
);
5641 return level
<= this_level
;
5644 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5645 parameters given by current_template_args, or reuses a
5646 previously existing one, if appropriate. Returns the DECL, or an
5647 equivalent one, if it is replaced via a call to duplicate_decls.
5649 If IS_FRIEND is true, DECL is a friend declaration. */
5652 push_template_decl_real (tree decl
, bool is_friend
)
5660 int new_template_p
= 0;
5661 /* True if the template is a member template, in the sense of
5663 bool member_template_p
= false;
5665 if (decl
== error_mark_node
|| !current_template_parms
)
5666 return error_mark_node
;
5668 /* See if this is a partial specialization. */
5669 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5670 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5671 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5673 && DECL_LANG_SPECIFIC (decl
)
5674 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5675 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5677 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5681 /* For a friend, we want the context of the friend, not
5682 the type of which it is a friend. */
5683 ctx
= CP_DECL_CONTEXT (decl
);
5684 else if (CP_DECL_CONTEXT (decl
)
5685 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5686 /* In the case of a virtual function, we want the class in which
5688 ctx
= CP_DECL_CONTEXT (decl
);
5690 /* Otherwise, if we're currently defining some class, the DECL
5691 is assumed to be a member of the class. */
5692 ctx
= current_scope ();
5694 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5697 if (!DECL_CONTEXT (decl
))
5698 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5700 /* See if this is a primary template. */
5701 if (is_friend
&& ctx
5702 && uses_template_parms_level (ctx
, processing_template_decl
))
5703 /* A friend template that specifies a class context, i.e.
5704 template <typename T> friend void A<T>::f();
5707 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5710 is_primary
= template_parm_scope_p ();
5714 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5716 if (DECL_CLASS_SCOPE_P (decl
))
5717 member_template_p
= true;
5718 if (TREE_CODE (decl
) == TYPE_DECL
5719 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5721 error ("template class without a name");
5722 return error_mark_node
;
5724 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5726 if (member_template_p
)
5728 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5729 error ("member template %qD may not have virt-specifiers", decl
);
5731 if (DECL_DESTRUCTOR_P (decl
))
5735 A destructor shall not be a member template. */
5736 error_at (DECL_SOURCE_LOCATION (decl
),
5737 "destructor %qD declared as member template", decl
);
5738 return error_mark_node
;
5740 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5741 && (!prototype_p (TREE_TYPE (decl
))
5742 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5743 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5744 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5745 == void_list_node
)))
5747 /* [basic.stc.dynamic.allocation]
5749 An allocation function can be a function
5750 template. ... Template allocation functions shall
5751 have two or more parameters. */
5752 error ("invalid template declaration of %qD", decl
);
5753 return error_mark_node
;
5756 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5757 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5759 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5760 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5761 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5763 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5764 if (TREE_CODE (t
) == TYPE_DECL
)
5766 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5767 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5770 else if (TREE_CODE (decl
) == TYPE_DECL
5771 && TYPE_DECL_ALIAS_P (decl
))
5772 /* alias-declaration */
5773 gcc_assert (!DECL_ARTIFICIAL (decl
));
5774 else if (VAR_P (decl
))
5775 /* C++14 variable template. */;
5776 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5777 /* C++20 concept definitions. */;
5780 error ("template declaration of %q#D", decl
);
5781 return error_mark_node
;
5785 /* Check to see that the rules regarding the use of default
5786 arguments are not being violated. We check args for a friend
5787 functions when we know whether it's a definition, introducing
5788 declaration or re-declaration. */
5789 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5790 check_default_tmpl_args (decl
, current_template_parms
,
5791 is_primary
, is_partial
, is_friend
);
5793 /* Ensure that there are no parameter packs in the type of this
5794 declaration that have not been expanded. */
5795 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5797 /* Check each of the arguments individually to see if there are
5798 any bare parameter packs. */
5799 tree type
= TREE_TYPE (decl
);
5800 tree arg
= DECL_ARGUMENTS (decl
);
5801 tree argtype
= TYPE_ARG_TYPES (type
);
5803 while (arg
&& argtype
)
5805 if (!DECL_PACK_P (arg
)
5806 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5808 /* This is a PARM_DECL that contains unexpanded parameter
5809 packs. We have already complained about this in the
5810 check_for_bare_parameter_packs call, so just replace
5811 these types with ERROR_MARK_NODE. */
5812 TREE_TYPE (arg
) = error_mark_node
;
5813 TREE_VALUE (argtype
) = error_mark_node
;
5816 arg
= DECL_CHAIN (arg
);
5817 argtype
= TREE_CHAIN (argtype
);
5820 /* Check for bare parameter packs in the return type and the
5821 exception specifiers. */
5822 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5823 /* Errors were already issued, set return type to int
5824 as the frontend doesn't expect error_mark_node as
5826 TREE_TYPE (type
) = integer_type_node
;
5827 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5828 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5830 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5831 ? DECL_ORIGINAL_TYPE (decl
)
5832 : TREE_TYPE (decl
)))
5834 TREE_TYPE (decl
) = error_mark_node
;
5835 return error_mark_node
;
5839 return process_partial_specialization (decl
);
5841 args
= current_template_args ();
5844 || TREE_CODE (ctx
) == FUNCTION_DECL
5845 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5846 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5847 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5849 if (DECL_LANG_SPECIFIC (decl
)
5850 && DECL_TEMPLATE_INFO (decl
)
5851 && DECL_TI_TEMPLATE (decl
))
5852 tmpl
= DECL_TI_TEMPLATE (decl
);
5853 /* If DECL is a TYPE_DECL for a class-template, then there won't
5854 be DECL_LANG_SPECIFIC. The information equivalent to
5855 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5856 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5857 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5858 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5860 /* Since a template declaration already existed for this
5861 class-type, we must be redeclaring it here. Make sure
5862 that the redeclaration is valid. */
5863 redeclare_class_template (TREE_TYPE (decl
),
5864 current_template_parms
,
5865 current_template_constraints ());
5866 /* We don't need to create a new TEMPLATE_DECL; just use the
5867 one we already had. */
5868 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5872 tmpl
= build_template_decl (decl
, current_template_parms
,
5876 if (DECL_LANG_SPECIFIC (decl
)
5877 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5879 /* A specialization of a member template of a template
5881 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5882 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5883 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5889 tree a
, t
, current
, parms
;
5891 tree tinfo
= get_template_info (decl
);
5895 error ("template definition of non-template %q#D", decl
);
5896 return error_mark_node
;
5899 tmpl
= TI_TEMPLATE (tinfo
);
5901 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5902 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5903 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5904 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5908 /* The declaration is a specialization of a member
5909 template, declared outside the class. Therefore, the
5910 innermost template arguments will be NULL, so we
5911 replace them with the arguments determined by the
5912 earlier call to check_explicit_specialization. */
5913 args
= DECL_TI_ARGS (decl
);
5916 = build_template_decl (decl
, current_template_parms
,
5918 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5919 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5920 DECL_TEMPLATE_INFO (new_tmpl
)
5921 = build_template_info (tmpl
, args
);
5923 register_specialization (new_tmpl
,
5924 most_general_template (tmpl
),
5930 /* Make sure the template headers we got make sense. */
5932 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5933 i
= TMPL_PARMS_DEPTH (parms
);
5934 if (TMPL_ARGS_DEPTH (args
) != i
)
5936 error ("expected %d levels of template parms for %q#D, got %d",
5937 i
, decl
, TMPL_ARGS_DEPTH (args
));
5938 DECL_INTERFACE_KNOWN (decl
) = 1;
5939 return error_mark_node
;
5942 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5944 a
= TMPL_ARGS_LEVEL (args
, i
);
5945 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5947 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5949 if (current
== decl
)
5950 error ("got %d template parameters for %q#D",
5951 TREE_VEC_LENGTH (a
), decl
);
5953 error ("got %d template parameters for %q#T",
5954 TREE_VEC_LENGTH (a
), current
);
5955 error (" but %d required", TREE_VEC_LENGTH (t
));
5956 /* Avoid crash in import_export_decl. */
5957 DECL_INTERFACE_KNOWN (decl
) = 1;
5958 return error_mark_node
;
5961 if (current
== decl
)
5963 else if (current
== NULL_TREE
)
5964 /* Can happen in erroneous input. */
5967 current
= get_containing_scope (current
);
5970 /* Check that the parms are used in the appropriate qualifying scopes
5971 in the declarator. */
5972 if (!comp_template_args
5974 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5976 error ("template arguments to %qD do not match original "
5977 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5978 if (!uses_template_parms (TI_ARGS (tinfo
)))
5979 inform (input_location
, "use %<template<>%> for"
5980 " an explicit specialization");
5981 /* Avoid crash in import_export_decl. */
5982 DECL_INTERFACE_KNOWN (decl
) = 1;
5983 return error_mark_node
;
5987 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl
) == decl
);
5991 /* Push template declarations for global functions and types.
5992 Note that we do not try to push a global template friend
5993 declared in a template class; such a thing may well depend on
5994 the template parameters of the class and we'll push it when
5995 instantiating the befriending class. */
5997 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5999 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
6000 if (tmpl
== error_mark_node
)
6001 return error_mark_node
;
6003 /* Hide template friend classes that haven't been declared yet. */
6004 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
6006 DECL_ANTICIPATED (tmpl
) = 1;
6007 DECL_FRIEND_P (tmpl
) = 1;
6012 /* The type may have been completed, or (erroneously) changed. */
6013 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6017 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6019 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6021 /* Give template template parms a DECL_CONTEXT of the template
6022 for which they are a parameter. */
6023 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6024 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6026 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6027 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6028 DECL_CONTEXT (parm
) = tmpl
;
6031 if (TREE_CODE (decl
) == TYPE_DECL
6032 && TYPE_DECL_ALIAS_P (decl
))
6035 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6037 /* ??? Why don't we do this here for all templates? */
6038 constr
= build_constraints (constr
, NULL_TREE
);
6039 set_constraints (decl
, constr
);
6041 if (complex_alias_template_p (tmpl
))
6042 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6046 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6047 back to its most general template. If TMPL is a specialization,
6048 ARGS may only have the innermost set of arguments. Add the missing
6049 argument levels if necessary. */
6050 if (DECL_TEMPLATE_INFO (tmpl
))
6051 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6053 info
= build_template_info (tmpl
, args
);
6055 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6056 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6060 retrofit_lang_decl (decl
);
6061 if (DECL_LANG_SPECIFIC (decl
))
6062 DECL_TEMPLATE_INFO (decl
) = info
;
6065 if (flag_implicit_templates
6067 && TREE_PUBLIC (decl
)
6068 && VAR_OR_FUNCTION_DECL_P (decl
))
6069 /* Set DECL_COMDAT on template instantiations; if we force
6070 them to be emitted by explicit instantiation,
6071 mark_needed will tell cgraph to do the right thing. */
6072 DECL_COMDAT (decl
) = true;
6074 return DECL_TEMPLATE_RESULT (tmpl
);
6078 push_template_decl (tree decl
)
6080 return push_template_decl_real (decl
, false);
6083 /* FN is an inheriting constructor that inherits from the constructor
6084 template INHERITED; turn FN into a constructor template with a matching
6088 add_inherited_template_parms (tree fn
, tree inherited
)
6091 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6092 inner_parms
= copy_node (inner_parms
);
6094 = tree_cons (size_int (processing_template_decl
+ 1),
6095 inner_parms
, current_template_parms
);
6096 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6097 tree args
= template_parms_to_args (parms
);
6098 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6099 DECL_ARTIFICIAL (tmpl
) = true;
6100 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6104 /* Called when a class template TYPE is redeclared with the indicated
6105 template PARMS, e.g.:
6107 template <class T> struct S;
6108 template <class T> struct S {}; */
6111 redeclare_class_template (tree type
, tree parms
, tree cons
)
6117 if (!TYPE_TEMPLATE_INFO (type
))
6119 error ("%qT is not a template type", type
);
6123 tmpl
= TYPE_TI_TEMPLATE (type
);
6124 if (!PRIMARY_TEMPLATE_P (tmpl
))
6125 /* The type is nested in some template class. Nothing to worry
6126 about here; there are no new template parameters for the nested
6132 error ("template specifiers not specified in declaration of %qD",
6137 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6138 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6140 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6142 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6143 "redeclared with %d template parameter",
6144 "redeclared with %d template parameters",
6145 TREE_VEC_LENGTH (parms
));
6146 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6147 "previous declaration %qD used %d template parameter",
6148 "previous declaration %qD used %d template parameters",
6149 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6153 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6160 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6161 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6164 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6165 if (error_operand_p (tmpl_parm
))
6168 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6169 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6170 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6172 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6174 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6175 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6176 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6177 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6178 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6179 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6180 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6181 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6182 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6184 auto_diagnostic_group d
;
6185 error ("template parameter %q+#D", tmpl_parm
);
6186 inform (input_location
, "redeclared here as %q#D", parm
);
6190 /* The parameters can be declared to introduce different
6192 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6193 tree p2
= TREE_VEC_ELT (parms
, i
);
6194 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6196 auto_diagnostic_group d
;
6197 error ("declaration of template parameter %q+#D with different "
6198 "constraints", parm
);
6199 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6200 "original declaration appeared here");
6204 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6206 /* We have in [temp.param]:
6208 A template-parameter may not be given default arguments
6209 by two different declarations in the same scope. */
6210 auto_diagnostic_group d
;
6211 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6212 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6213 "original definition appeared here");
6217 if (parm_default
!= NULL_TREE
)
6218 /* Update the previous template parameters (which are the ones
6219 that will really count) with the new default value. */
6220 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6221 else if (tmpl_default
!= NULL_TREE
)
6222 /* Update the new parameters, too; they'll be used as the
6223 parameters for any members. */
6224 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6226 /* Give each template template parm in this redeclaration a
6227 DECL_CONTEXT of the template for which they are a parameter. */
6228 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6230 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6231 DECL_CONTEXT (parm
) = tmpl
;
6234 if (TREE_CODE (parm
) == TYPE_DECL
)
6235 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6238 tree ci
= get_constraints (tmpl
);
6239 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6240 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6242 /* Two classes with different constraints declare different entities. */
6243 if (!cp_tree_equal (req1
, req2
))
6245 auto_diagnostic_group d
;
6246 error_at (input_location
, "redeclaration %q#D with different "
6247 "constraints", tmpl
);
6248 inform (DECL_SOURCE_LOCATION (tmpl
),
6249 "original declaration appeared here");
6256 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6257 to be used when the caller has already checked
6258 (processing_template_decl
6259 && !instantiation_dependent_expression_p (expr)
6260 && potential_constant_expression (expr))
6261 and cleared processing_template_decl. */
6264 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6266 return tsubst_copy_and_build (expr
,
6269 /*in_decl=*/NULL_TREE
,
6270 /*function_p=*/false,
6271 /*integral_constant_expression_p=*/true);
6274 /* Simplify EXPR if it is a non-dependent expression. Returns the
6275 (possibly simplified) expression. */
6278 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6280 if (expr
== NULL_TREE
)
6283 /* If we're in a template, but EXPR isn't value dependent, simplify
6284 it. We're supposed to treat:
6286 template <typename T> void f(T[1 + 1]);
6287 template <typename T> void f(T[2]);
6289 as two declarations of the same function, for example. */
6290 if (processing_template_decl
6291 && is_nondependent_constant_expression (expr
))
6293 processing_template_decl_sentinel s
;
6294 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6300 instantiate_non_dependent_expr (tree expr
)
6302 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6305 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6306 an uninstantiated expression. */
6309 instantiate_non_dependent_or_null (tree expr
)
6311 if (expr
== NULL_TREE
)
6313 if (processing_template_decl
)
6315 if (!is_nondependent_constant_expression (expr
))
6319 processing_template_decl_sentinel s
;
6320 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6326 /* True iff T is a specialization of a variable template. */
6329 variable_template_specialization_p (tree t
)
6331 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6333 tree tmpl
= DECL_TI_TEMPLATE (t
);
6334 return variable_template_p (tmpl
);
6337 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6338 template declaration, or a TYPE_DECL for an alias declaration. */
6341 alias_type_or_template_p (tree t
)
6345 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6348 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6349 || DECL_ALIAS_TEMPLATE_P (t
));
6352 /* If T is a specialization of an alias template, return it; otherwise return
6353 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6356 alias_template_specialization_p (const_tree t
,
6357 bool transparent_typedefs
)
6362 /* It's an alias template specialization if it's an alias and its
6363 TYPE_NAME is a specialization of a primary template. */
6364 if (typedef_variant_p (t
))
6366 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6367 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6368 return CONST_CAST_TREE (t
);
6369 if (transparent_typedefs
)
6370 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6372 transparent_typedefs
);
6378 /* An alias template is complex from a SFINAE perspective if a template-id
6379 using that alias can be ill-formed when the expansion is not, as with
6380 the void_t template. We determine this by checking whether the
6381 expansion for the alias template uses all its template parameters. */
6383 struct uses_all_template_parms_data
6390 uses_all_template_parms_r (tree t
, void *data_
)
6392 struct uses_all_template_parms_data
&data
6393 = *(struct uses_all_template_parms_data
*)data_
;
6394 tree idx
= get_template_parm_index (t
);
6396 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6397 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6401 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6404 complex_pack_expansion_r (tree t
, void *data_
)
6406 /* An alias template with a pack expansion that expands a pack from the
6407 enclosing class needs to be considered complex, to avoid confusion with
6408 the same pack being used as an argument to the alias's own template
6409 parameter (91966). */
6410 if (!PACK_EXPANSION_P (t
))
6412 struct uses_all_template_parms_data
&data
6413 = *(struct uses_all_template_parms_data
*)data_
;
6414 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6415 pack
= TREE_CHAIN (pack
))
6417 tree parm_pack
= TREE_VALUE (pack
);
6418 if (!TEMPLATE_PARM_P (parm_pack
))
6421 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6422 if (level
< data
.level
)
6429 complex_alias_template_p (const_tree tmpl
)
6431 /* A renaming alias isn't complex. */
6432 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6435 /* Any other constrained alias is complex. */
6436 if (get_constraints (tmpl
))
6439 struct uses_all_template_parms_data data
;
6440 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6441 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6442 data
.level
= TMPL_PARMS_DEPTH (parms
);
6443 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6444 data
.seen
= XALLOCAVEC (bool, len
);
6445 for (int i
= 0; i
< len
; ++i
)
6446 data
.seen
[i
] = false;
6448 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6449 NULL
, true, complex_pack_expansion_r
))
6451 for (int i
= 0; i
< len
; ++i
)
6457 /* If T is a specialization of a complex alias template with dependent
6458 template-arguments, return it; otherwise return NULL_TREE. If T is a
6459 typedef to such a specialization, return the specialization. */
6462 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6464 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6467 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6469 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6470 && (any_dependent_template_arguments_p
6471 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6472 return CONST_CAST_TREE (t
);
6474 if (transparent_typedefs
)
6476 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6477 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6483 /* Return the number of innermost template parameters in TMPL. */
6486 num_innermost_template_parms (const_tree tmpl
)
6488 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6489 return TREE_VEC_LENGTH (parms
);
6492 /* Return either TMPL or another template that it is equivalent to under DR
6493 1286: An alias that just changes the name of a template is equivalent to
6494 the other template. */
6497 get_underlying_template (tree tmpl
)
6499 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6500 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6502 /* Determine if the alias is equivalent to an underlying template. */
6503 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6504 /* The underlying type may have been ill-formed. Don't proceed. */
6507 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6511 tree underlying
= TI_TEMPLATE (tinfo
);
6512 if (!PRIMARY_TEMPLATE_P (underlying
)
6513 || (num_innermost_template_parms (tmpl
)
6514 != num_innermost_template_parms (underlying
)))
6517 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6518 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6521 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6522 it's appropriate to treat a less-constrained alias as equivalent. */
6523 if (!at_least_as_constrained (underlying
, tmpl
))
6526 /* Alias is equivalent. Strip it and repeat. */
6533 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6534 must be a reference-to-function or a pointer-to-function type, as specified
6535 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6536 and check that the resulting function has external linkage. */
6539 convert_nontype_argument_function (tree type
, tree expr
,
6540 tsubst_flags_t complain
)
6544 linkage_kind linkage
;
6546 fn
= instantiate_type (type
, fns
, tf_none
);
6547 if (fn
== error_mark_node
)
6548 return error_mark_node
;
6550 if (value_dependent_expression_p (fn
))
6553 fn_no_ptr
= strip_fnptr_conv (fn
);
6554 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6555 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6556 if (BASELINK_P (fn_no_ptr
))
6557 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6559 /* [temp.arg.nontype]/1
6561 A template-argument for a non-type, non-template template-parameter
6564 -- the address of an object or function with external [C++11: or
6565 internal] linkage. */
6567 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6568 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6570 if (complain
& tf_error
)
6572 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6573 error_at (loc
, "%qE is not a valid template argument for type %qT",
6575 if (TYPE_PTR_P (type
))
6576 inform (loc
, "it must be the address of a function "
6577 "with external linkage");
6579 inform (loc
, "it must be the name of a function with "
6580 "external linkage");
6585 linkage
= decl_linkage (fn_no_ptr
);
6586 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6588 if (complain
& tf_error
)
6590 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6591 if (cxx_dialect
>= cxx11
)
6592 error_at (loc
, "%qE is not a valid template argument for type "
6593 "%qT because %qD has no linkage",
6594 expr
, type
, fn_no_ptr
);
6596 error_at (loc
, "%qE is not a valid template argument for type "
6597 "%qT because %qD does not have external linkage",
6598 expr
, type
, fn_no_ptr
);
6604 if (TYPE_REF_P (type
))
6606 if (REFERENCE_REF_P (fn
))
6607 fn
= TREE_OPERAND (fn
, 0);
6609 fn
= build_address (fn
);
6611 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6612 fn
= build_nop (type
, fn
);
6617 /* Subroutine of convert_nontype_argument.
6618 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6619 Emit an error otherwise. */
6622 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6623 tsubst_flags_t complain
)
6625 tree orig_expr
= expr
;
6627 if (null_ptr_cst_p (expr
))
6629 if (TREE_CODE (expr
) == PTRMEM_CST
6630 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6631 PTRMEM_CST_CLASS (expr
)))
6633 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6635 if (processing_template_decl
6636 && TREE_CODE (expr
) == ADDR_EXPR
6637 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6639 if (complain
& tf_error
)
6641 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6642 error_at (loc
, "%qE is not a valid template argument for type %qT",
6644 if (TREE_CODE (expr
) != PTRMEM_CST
)
6645 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6647 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6652 /* Returns TRUE iff the address of OP is value-dependent.
6654 14.6.2.4 [temp.dep.temp]:
6655 A non-integral non-type template-argument is dependent if its type is
6656 dependent or it has either of the following forms
6659 and contains a nested-name-specifier which specifies a class-name that
6660 names a dependent type.
6662 We generalize this to just say that the address of a member of a
6663 dependent class is value-dependent; the above doesn't cover the
6664 address of a static data member named with an unqualified-id. */
6667 has_value_dependent_address (tree op
)
6669 STRIP_ANY_LOCATION_WRAPPER (op
);
6671 /* We could use get_inner_reference here, but there's no need;
6672 this is only relevant for template non-type arguments, which
6673 can only be expressed as &id-expression. */
6676 tree ctx
= CP_DECL_CONTEXT (op
);
6677 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6684 /* The next set of functions are used for providing helpful explanatory
6685 diagnostics for failed overload resolution. Their messages should be
6686 indented by two spaces for consistency with the messages in
6690 unify_success (bool /*explain_p*/)
6695 /* Other failure functions should call this one, to provide a single function
6696 for setting a breakpoint on. */
6699 unify_invalid (bool /*explain_p*/)
6705 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6708 inform (input_location
,
6709 " couldn%'t deduce template parameter %qD", parm
);
6710 return unify_invalid (explain_p
);
6714 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6717 inform (input_location
,
6718 " types %qT and %qT have incompatible cv-qualifiers",
6720 return unify_invalid (explain_p
);
6724 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6727 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6728 return unify_invalid (explain_p
);
6732 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6735 inform (input_location
,
6736 " template parameter %qD is not a parameter pack, but "
6739 return unify_invalid (explain_p
);
6743 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6746 inform (input_location
,
6747 " template argument %qE does not match "
6748 "pointer-to-member constant %qE",
6750 return unify_invalid (explain_p
);
6754 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6757 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6758 return unify_invalid (explain_p
);
6762 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6765 inform (input_location
,
6766 " inconsistent parameter pack deduction with %qT and %qT",
6768 return unify_invalid (explain_p
);
6772 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6777 inform (input_location
,
6778 " deduced conflicting types for parameter %qT (%qT and %qT)",
6779 parm
, first
, second
);
6781 inform (input_location
,
6782 " deduced conflicting values for non-type parameter "
6783 "%qE (%qE and %qE)", parm
, first
, second
);
6785 return unify_invalid (explain_p
);
6789 unify_vla_arg (bool explain_p
, tree arg
)
6792 inform (input_location
,
6793 " variable-sized array type %qT is not "
6794 "a valid template argument",
6796 return unify_invalid (explain_p
);
6800 unify_method_type_error (bool explain_p
, tree arg
)
6803 inform (input_location
,
6804 " member function type %qT is not a valid template argument",
6806 return unify_invalid (explain_p
);
6810 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6815 inform_n (input_location
, wanted
,
6816 " candidate expects at least %d argument, %d provided",
6817 " candidate expects at least %d arguments, %d provided",
6820 inform_n (input_location
, wanted
,
6821 " candidate expects %d argument, %d provided",
6822 " candidate expects %d arguments, %d provided",
6825 return unify_invalid (explain_p
);
6829 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6831 return unify_arity (explain_p
, have
, wanted
);
6835 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6836 bool least_p
= false)
6838 return unify_arity (explain_p
, have
, wanted
, least_p
);
6842 unify_arg_conversion (bool explain_p
, tree to_type
,
6843 tree from_type
, tree arg
)
6846 inform (cp_expr_loc_or_input_loc (arg
),
6847 " cannot convert %qE (type %qT) to type %qT",
6848 arg
, from_type
, to_type
);
6849 return unify_invalid (explain_p
);
6853 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6854 tree parm
, tree arg
)
6859 case tbr_ambiguous_baseclass
:
6860 inform (input_location
, " %qT is an ambiguous base class of %qT",
6864 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6867 return unify_invalid (explain_p
);
6871 unify_inconsistent_template_template_parameters (bool explain_p
)
6874 inform (input_location
,
6875 " template parameters of a template template argument are "
6876 "inconsistent with other deduced template arguments");
6877 return unify_invalid (explain_p
);
6881 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6884 inform (input_location
,
6885 " cannot deduce a template for %qT from non-template type %qT",
6887 return unify_invalid (explain_p
);
6891 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6894 inform (input_location
,
6895 " template argument %qE does not match %qE", arg
, parm
);
6896 return unify_invalid (explain_p
);
6899 /* True if T is a C++20 template parameter object to store the argument for a
6900 template parameter of class type. */
6903 template_parm_object_p (const_tree t
)
6905 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6906 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6909 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6910 argument for TYPE, points to an unsuitable object.
6912 Also adjust the type of the index in C++20 array subobject references. */
6915 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6917 switch (TREE_CODE (expr
))
6920 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6924 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6929 unsigned i
; tree elt
;
6930 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6931 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6938 tree decl
= TREE_OPERAND (expr
, 0);
6940 if (cxx_dialect
>= cxx20
)
6941 while (TREE_CODE (decl
) == COMPONENT_REF
6942 || TREE_CODE (decl
) == ARRAY_REF
)
6944 tree
&op
= TREE_OPERAND (decl
, 1);
6945 if (TREE_CODE (decl
) == ARRAY_REF
6946 && TREE_CODE (op
) == INTEGER_CST
)
6947 /* Canonicalize array offsets to ptrdiff_t; how they were
6948 written doesn't matter for subobject identity. */
6949 op
= fold_convert (ptrdiff_type_node
, op
);
6950 decl
= TREE_OPERAND (decl
, 0);
6955 if (complain
& tf_error
)
6956 error_at (cp_expr_loc_or_input_loc (expr
),
6957 "%qE is not a valid template argument of type %qT "
6958 "because %qE is not a variable", expr
, type
, decl
);
6961 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6963 if (complain
& tf_error
)
6964 error_at (cp_expr_loc_or_input_loc (expr
),
6965 "%qE is not a valid template argument of type %qT "
6966 "in C++98 because %qD does not have external linkage",
6970 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6971 && decl_linkage (decl
) == lk_none
)
6973 if (complain
& tf_error
)
6974 error_at (cp_expr_loc_or_input_loc (expr
),
6975 "%qE is not a valid template argument of type %qT "
6976 "because %qD has no linkage", expr
, type
, decl
);
6979 /* C++17: For a non-type template-parameter of reference or pointer
6980 type, the value of the constant expression shall not refer to (or
6981 for a pointer type, shall not be the address of):
6982 * a subobject (4.5),
6983 * a temporary object (15.2),
6984 * a string literal (5.13.5),
6985 * the result of a typeid expression (8.2.8), or
6986 * a predefined __func__ variable (11.4.1). */
6987 else if (DECL_ARTIFICIAL (decl
))
6989 if (complain
& tf_error
)
6990 error ("the address of %qD is not a valid template argument",
6994 else if (cxx_dialect
< cxx20
6995 && !(same_type_ignoring_top_level_qualifiers_p
6996 (strip_array_types (TREE_TYPE (type
)),
6997 strip_array_types (TREE_TYPE (decl
)))))
6999 if (complain
& tf_error
)
7000 error ("the address of the %qT subobject of %qD is not a "
7001 "valid template argument", TREE_TYPE (type
), decl
);
7004 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7006 if (complain
& tf_error
)
7007 error ("the address of %qD is not a valid template argument "
7008 "because it does not have static storage duration",
7016 if (!INDIRECT_TYPE_P (type
))
7017 /* We're only concerned about pointers and references here. */;
7018 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7019 /* Null pointer values are OK in C++11. */;
7024 if (complain
& tf_error
)
7025 error ("%qD is not a valid template argument "
7026 "because %qD is a variable, not the address of "
7027 "a variable", expr
, expr
);
7032 if (complain
& tf_error
)
7033 error ("%qE is not a valid template argument for %qT "
7034 "because it is not the address of a variable",
7044 /* The template arguments corresponding to template parameter objects of types
7045 that contain pointers to members. */
7047 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7049 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7050 template argument EXPR. */
7053 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7055 if (TREE_CODE (expr
) == TARGET_EXPR
)
7056 expr
= TARGET_EXPR_INITIAL (expr
);
7058 if (!TREE_CONSTANT (expr
))
7060 if ((complain
& tf_error
)
7061 && require_rvalue_constant_expression (expr
))
7062 cxx_constant_value (expr
);
7063 return error_mark_node
;
7065 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7066 return error_mark_node
;
7068 tree name
= mangle_template_parm_object (expr
);
7069 tree decl
= get_global_binding (name
);
7073 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7074 decl
= create_temporary_var (type
);
7075 TREE_STATIC (decl
) = true;
7076 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7077 TREE_READONLY (decl
) = true;
7078 DECL_NAME (decl
) = name
;
7079 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7080 DECL_CONTEXT (decl
) = global_namespace
;
7081 comdat_linkage (decl
);
7083 if (!zero_init_p (type
))
7085 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7086 lower_var_init before we're done mangling. So store the original
7088 tree copy
= unshare_constructor (expr
);
7089 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7092 pushdecl_top_level_and_finish (decl
, expr
);
7097 /* Return the actual template argument corresponding to template parameter
7101 tparm_object_argument (tree var
)
7103 if (zero_init_p (TREE_TYPE (var
)))
7104 return DECL_INITIAL (var
);
7105 return *(tparm_obj_values
->get (var
));
7108 /* Attempt to convert the non-type template parameter EXPR to the
7109 indicated TYPE. If the conversion is successful, return the
7110 converted value. If the conversion is unsuccessful, return
7111 NULL_TREE if we issued an error message, or error_mark_node if we
7112 did not. We issue error messages for out-and-out bad template
7113 parameters, but not simply because the conversion failed, since we
7114 might be just trying to do argument deduction. Both TYPE and EXPR
7115 must be non-dependent.
7117 The conversion follows the special rules described in
7118 [temp.arg.nontype], and it is much more strict than an implicit
7121 This function is called twice for each template argument (see
7122 lookup_template_class for a more accurate description of this
7123 problem). This means that we need to handle expressions which
7124 are not valid in a C++ source, but can be created from the
7125 first call (for instance, casts to perform conversions). These
7126 hacks can go away after we fix the double coercion problem. */
7129 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7132 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7134 /* Detect immediately string literals as invalid non-type argument.
7135 This special-case is not needed for correctness (we would easily
7136 catch this later), but only to provide better diagnostic for this
7137 common user mistake. As suggested by DR 100, we do not mention
7138 linkage issues in the diagnostic as this is not the point. */
7139 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7141 if (complain
& tf_error
)
7142 error ("%qE is not a valid template argument for type %qT "
7143 "because string literals can never be used in this context",
7148 /* Add the ADDR_EXPR now for the benefit of
7149 value_dependent_expression_p. */
7150 if (TYPE_PTROBV_P (type
)
7151 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7153 expr
= decay_conversion (expr
, complain
);
7154 if (expr
== error_mark_node
)
7155 return error_mark_node
;
7158 /* If we are in a template, EXPR may be non-dependent, but still
7159 have a syntactic, rather than semantic, form. For example, EXPR
7160 might be a SCOPE_REF, rather than the VAR_DECL to which the
7161 SCOPE_REF refers. Preserving the qualifying scope is necessary
7162 so that access checking can be performed when the template is
7163 instantiated -- but here we need the resolved form so that we can
7164 convert the argument. */
7165 bool non_dep
= false;
7166 if (TYPE_REF_OBJ_P (type
)
7167 && has_value_dependent_address (expr
))
7168 /* If we want the address and it's value-dependent, don't fold. */;
7169 else if (processing_template_decl
7170 && is_nondependent_constant_expression (expr
))
7172 if (error_operand_p (expr
))
7173 return error_mark_node
;
7174 expr_type
= TREE_TYPE (expr
);
7176 /* If the argument is non-dependent, perform any conversions in
7177 non-dependent context as well. */
7178 processing_template_decl_sentinel
s (non_dep
);
7180 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7182 const bool val_dep_p
= value_dependent_expression_p (expr
);
7184 expr
= canonicalize_expr_argument (expr
, complain
);
7186 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7187 to a non-type argument of "nullptr". */
7188 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7189 expr
= fold_simple (convert (type
, expr
));
7191 /* In C++11, integral or enumeration non-type template arguments can be
7192 arbitrary constant expressions. Pointer and pointer to
7193 member arguments can be general constant expressions that evaluate
7194 to a null value, but otherwise still need to be of a specific form. */
7195 if (cxx_dialect
>= cxx11
)
7197 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7198 /* A PTRMEM_CST is already constant, and a valid template
7199 argument for a parameter of pointer to member type, we just want
7200 to leave it in that form rather than lower it to a
7202 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7203 || cxx_dialect
>= cxx17
)
7205 /* C++17: A template-argument for a non-type template-parameter shall
7206 be a converted constant expression (8.20) of the type of the
7207 template-parameter. */
7208 expr
= build_converted_constant_expr (type
, expr
, complain
);
7209 if (expr
== error_mark_node
)
7210 /* Make sure we return NULL_TREE only if we have really issued
7211 an error, as described above. */
7212 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7213 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7215 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7218 expr
= maybe_constant_value (expr
, NULL_TREE
,
7219 /*manifestly_const_eval=*/true);
7220 expr
= convert_from_reference (expr
);
7222 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7224 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7225 /*manifestly_const_eval=*/true);
7226 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7227 : null_member_pointer_value_p (folded
))
7232 if (TYPE_REF_P (type
))
7233 expr
= mark_lvalue_use (expr
);
7235 expr
= mark_rvalue_use (expr
);
7237 /* HACK: Due to double coercion, we can get a
7238 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7239 which is the tree that we built on the first call (see
7240 below when coercing to reference to object or to reference to
7241 function). We just strip everything and get to the arg.
7242 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7244 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7246 tree probe_type
, probe
= expr
;
7247 if (REFERENCE_REF_P (probe
))
7248 probe
= TREE_OPERAND (probe
, 0);
7249 probe_type
= TREE_TYPE (probe
);
7250 if (TREE_CODE (probe
) == NOP_EXPR
)
7252 /* ??? Maybe we could use convert_from_reference here, but we
7253 would need to relax its constraints because the NOP_EXPR
7254 could actually change the type to something more cv-qualified,
7255 and this is not folded by convert_from_reference. */
7256 tree addr
= TREE_OPERAND (probe
, 0);
7257 if (TYPE_REF_P (probe_type
)
7258 && TREE_CODE (addr
) == ADDR_EXPR
7259 && TYPE_PTR_P (TREE_TYPE (addr
))
7260 && (same_type_ignoring_top_level_qualifiers_p
7261 (TREE_TYPE (probe_type
),
7262 TREE_TYPE (TREE_TYPE (addr
)))))
7264 expr
= TREE_OPERAND (addr
, 0);
7265 expr_type
= TREE_TYPE (probe_type
);
7270 /* [temp.arg.nontype]/5, bullet 1
7272 For a non-type template-parameter of integral or enumeration type,
7273 integral promotions (_conv.prom_) and integral conversions
7274 (_conv.integral_) are applied. */
7275 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7276 || TREE_CODE (type
) == REAL_TYPE
)
7278 if (cxx_dialect
< cxx11
)
7280 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7281 t
= maybe_constant_value (t
);
7282 if (t
!= error_mark_node
)
7286 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7287 return error_mark_node
;
7289 /* Notice that there are constant expressions like '4 % 0' which
7290 do not fold into integer constants. */
7291 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7293 if (complain
& tf_error
)
7295 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7296 if (!require_potential_constant_expression (expr
))
7297 expr
= error_mark_node
;
7299 expr
= cxx_constant_value (expr
);
7300 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7301 inform (loc
, "in template argument for type %qT", type
);
7302 if (expr
== error_mark_node
)
7304 /* else cxx_constant_value complained but gave us
7305 a real constant, so go ahead. */
7306 if (!CONSTANT_CLASS_P (expr
))
7308 /* Some assemble time constant expressions like
7309 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7310 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7311 as we can emit them into .rodata initializers of
7312 variables, yet they can't fold into an INTEGER_CST at
7313 compile time. Refuse them here. */
7314 gcc_checking_assert (reduced_constant_expression_p (expr
));
7315 error_at (loc
, "template argument %qE for type %qT not "
7316 "a compile-time constant", expr
, type
);
7324 /* Avoid typedef problems. */
7325 if (TREE_TYPE (expr
) != type
)
7326 expr
= fold_convert (type
, expr
);
7328 /* [temp.arg.nontype]/5, bullet 2
7330 For a non-type template-parameter of type pointer to object,
7331 qualification conversions (_conv.qual_) and the array-to-pointer
7332 conversion (_conv.array_) are applied. */
7333 else if (TYPE_PTROBV_P (type
))
7335 tree decayed
= expr
;
7337 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7338 decay_conversion or an explicit cast. If it's a problematic cast,
7339 we'll complain about it below. */
7340 if (TREE_CODE (expr
) == NOP_EXPR
)
7344 if (TREE_CODE (probe
) == ADDR_EXPR
7345 && TYPE_PTR_P (TREE_TYPE (probe
)))
7348 expr_type
= TREE_TYPE (expr
);
7352 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7354 A template-argument for a non-type, non-template template-parameter
7355 shall be one of: [...]
7357 -- the name of a non-type template-parameter;
7358 -- the address of an object or function with external linkage, [...]
7359 expressed as "& id-expression" where the & is optional if the name
7360 refers to a function or array, or if the corresponding
7361 template-parameter is a reference.
7363 Here, we do not care about functions, as they are invalid anyway
7364 for a parameter of type pointer-to-object. */
7367 /* Non-type template parameters are OK. */
7369 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7370 /* Null pointer values are OK in C++11. */;
7371 else if (TREE_CODE (expr
) != ADDR_EXPR
7372 && !INDIRECT_TYPE_P (expr_type
))
7373 /* Other values, like integer constants, might be valid
7374 non-type arguments of some other type. */
7375 return error_mark_node
;
7376 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7381 expr
= perform_qualification_conversions (type
, expr
);
7382 if (expr
== error_mark_node
)
7383 return error_mark_node
;
7385 /* [temp.arg.nontype]/5, bullet 3
7387 For a non-type template-parameter of type reference to object, no
7388 conversions apply. The type referred to by the reference may be more
7389 cv-qualified than the (otherwise identical) type of the
7390 template-argument. The template-parameter is bound directly to the
7391 template-argument, which must be an lvalue. */
7392 else if (TYPE_REF_OBJ_P (type
))
7394 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7396 return error_mark_node
;
7398 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7400 if (complain
& tf_error
)
7401 error ("%qE is not a valid template argument for type %qT "
7402 "because of conflicts in cv-qualification", expr
, type
);
7406 if (!lvalue_p (expr
))
7408 if (complain
& tf_error
)
7409 error ("%qE is not a valid template argument for type %qT "
7410 "because it is not an lvalue", expr
, type
);
7414 /* [temp.arg.nontype]/1
7416 A template-argument for a non-type, non-template template-parameter
7417 shall be one of: [...]
7419 -- the address of an object or function with external linkage. */
7420 if (INDIRECT_REF_P (expr
)
7421 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7423 expr
= TREE_OPERAND (expr
, 0);
7426 if (complain
& tf_error
)
7427 error ("%q#D is not a valid template argument for type %qT "
7428 "because a reference variable does not have a constant "
7429 "address", expr
, type
);
7434 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7435 /* OK, dependent reference. We don't want to ask whether a DECL is
7436 itself value-dependent, since what we want here is its address. */;
7439 expr
= build_address (expr
);
7441 if (invalid_tparm_referent_p (type
, expr
, complain
))
7445 if (!same_type_p (type
, TREE_TYPE (expr
)))
7446 expr
= build_nop (type
, expr
);
7448 /* [temp.arg.nontype]/5, bullet 4
7450 For a non-type template-parameter of type pointer to function, only
7451 the function-to-pointer conversion (_conv.func_) is applied. If the
7452 template-argument represents a set of overloaded functions (or a
7453 pointer to such), the matching function is selected from the set
7455 else if (TYPE_PTRFN_P (type
))
7457 /* If the argument is a template-id, we might not have enough
7458 context information to decay the pointer. */
7459 if (!type_unknown_p (expr_type
))
7461 expr
= decay_conversion (expr
, complain
);
7462 if (expr
== error_mark_node
)
7463 return error_mark_node
;
7466 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7467 /* Null pointer values are OK in C++11. */
7468 return perform_qualification_conversions (type
, expr
);
7470 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7471 if (!expr
|| expr
== error_mark_node
)
7474 /* [temp.arg.nontype]/5, bullet 5
7476 For a non-type template-parameter of type reference to function, no
7477 conversions apply. If the template-argument represents a set of
7478 overloaded functions, the matching function is selected from the set
7480 else if (TYPE_REFFN_P (type
))
7482 if (TREE_CODE (expr
) == ADDR_EXPR
)
7484 if (complain
& tf_error
)
7486 error ("%qE is not a valid template argument for type %qT "
7487 "because it is a pointer", expr
, type
);
7488 inform (input_location
, "try using %qE instead",
7489 TREE_OPERAND (expr
, 0));
7494 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7495 if (!expr
|| expr
== error_mark_node
)
7498 /* [temp.arg.nontype]/5, bullet 6
7500 For a non-type template-parameter of type pointer to member function,
7501 no conversions apply. If the template-argument represents a set of
7502 overloaded member functions, the matching member function is selected
7503 from the set (_over.over_). */
7504 else if (TYPE_PTRMEMFUNC_P (type
))
7506 expr
= instantiate_type (type
, expr
, tf_none
);
7507 if (expr
== error_mark_node
)
7508 return error_mark_node
;
7510 /* [temp.arg.nontype] bullet 1 says the pointer to member
7511 expression must be a pointer-to-member constant. */
7513 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7516 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7517 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7518 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7519 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7521 /* [temp.arg.nontype]/5, bullet 7
7523 For a non-type template-parameter of type pointer to data member,
7524 qualification conversions (_conv.qual_) are applied. */
7525 else if (TYPE_PTRDATAMEM_P (type
))
7527 /* [temp.arg.nontype] bullet 1 says the pointer to member
7528 expression must be a pointer-to-member constant. */
7530 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7533 expr
= perform_qualification_conversions (type
, expr
);
7534 if (expr
== error_mark_node
)
7537 else if (NULLPTR_TYPE_P (type
))
7539 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7541 if (complain
& tf_error
)
7542 error ("%qE is not a valid template argument for type %qT "
7543 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7548 else if (CLASS_TYPE_P (type
))
7550 /* Replace the argument with a reference to the corresponding template
7551 parameter object. */
7553 expr
= get_template_parm_object (expr
, complain
);
7554 if (expr
== error_mark_node
)
7557 /* A template non-type parameter must be one of the above. */
7561 /* Sanity check: did we actually convert the argument to the
7563 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7564 (type
, TREE_TYPE (expr
)));
7565 return convert_from_reference (expr
);
7568 /* Subroutine of coerce_template_template_parms, which returns 1 if
7569 PARM_PARM and ARG_PARM match using the rule for the template
7570 parameters of template template parameters. Both PARM and ARG are
7571 template parameters; the rest of the arguments are the same as for
7572 coerce_template_template_parms.
7575 coerce_template_template_parm (tree parm
,
7577 tsubst_flags_t complain
,
7581 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7582 || parm
== NULL_TREE
|| error_operand_p (parm
))
7585 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7588 switch (TREE_CODE (parm
))
7591 /* We encounter instantiations of templates like
7592 template <template <template <class> class> class TT>
7595 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7596 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7598 if (!coerce_template_template_parms
7599 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7605 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7606 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7607 /* Argument is a parameter pack but parameter is not. */
7612 /* The tsubst call is used to handle cases such as
7614 template <int> class C {};
7615 template <class T, template <T> class TT> class D {};
7618 i.e. the parameter list of TT depends on earlier parameters. */
7619 if (!uses_template_parms (TREE_TYPE (arg
)))
7621 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7622 if (!uses_template_parms (t
)
7623 && !same_type_p (t
, TREE_TYPE (arg
)))
7627 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7628 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7629 /* Argument is a parameter pack but parameter is not. */
7641 /* Coerce template argument list ARGLIST for use with template
7642 template-parameter TEMPL. */
7645 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7646 tsubst_flags_t complain
)
7648 /* Consider an example where a template template parameter declared as
7650 template <class T, class U = std::allocator<T> > class TT
7652 The template parameter level of T and U are one level larger than
7653 of TT. To proper process the default argument of U, say when an
7654 instantiation `TT<int>' is seen, we need to build the full
7655 arguments containing {int} as the innermost level. Outer levels,
7656 available when not appearing as default template argument, can be
7657 obtained from the arguments of the enclosing template.
7659 Suppose that TT is later substituted with std::vector. The above
7660 instantiation is `TT<int, std::allocator<T> >' with TT at
7661 level 1, and T at level 2, while the template arguments at level 1
7662 becomes {std::vector} and the inner level 2 is {int}. */
7664 tree outer
= DECL_CONTEXT (templ
);
7666 outer
= generic_targs_for (outer
);
7667 else if (current_template_parms
)
7669 /* This is an argument of the current template, so we haven't set
7670 DECL_CONTEXT yet. */
7671 tree relevant_template_parms
;
7673 /* Parameter levels that are greater than the level of the given
7674 template template parm are irrelevant. */
7675 relevant_template_parms
= current_template_parms
;
7676 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7677 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7678 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7680 outer
= template_parms_to_args (relevant_template_parms
);
7684 arglist
= add_to_template_args (outer
, arglist
);
7686 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7687 return coerce_template_parms (parmlist
, arglist
, templ
,
7689 /*require_all_args=*/true,
7690 /*use_default_args=*/true);
7693 /* A cache of template template parameters with match-all default
7695 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7697 /* T is a bound template template-parameter. Copy its arguments into default
7698 arguments of the template template-parameter's template parameters. */
7701 add_defaults_to_ttp (tree otmpl
)
7703 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7706 tree ntmpl
= copy_node (otmpl
);
7708 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7709 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7710 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7711 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7712 TYPE_NAME (ntype
) = ntmpl
;
7713 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7715 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7716 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7717 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7718 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7720 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7721 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7722 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7723 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7724 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7726 tree o
= TREE_VEC_ELT (vec
, i
);
7727 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7729 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7730 TREE_PURPOSE (n
) = any_targ_node
;
7734 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7738 /* ARG is a bound potential template template-argument, and PARGS is a list
7739 of arguments for the corresponding template template-parameter. Adjust
7740 PARGS as appropriate for application to ARG's template, and if ARG is a
7741 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7742 arguments to the template template parameter. */
7745 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7747 ++processing_template_decl
;
7748 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7749 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7751 /* When comparing two template template-parameters in partial ordering,
7752 rewrite the one currently being used as an argument to have default
7753 arguments for all parameters. */
7754 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7755 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7756 if (pargs
!= error_mark_node
)
7757 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7758 TYPE_TI_ARGS (arg
));
7763 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7764 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7765 /*require_all*/true,
7766 /*use_default*/true);
7768 --processing_template_decl
;
7772 /* Subroutine of unify for the case when PARM is a
7773 BOUND_TEMPLATE_TEMPLATE_PARM. */
7776 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7779 tree parmvec
= TYPE_TI_ARGS (parm
);
7780 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7782 /* The template template parm might be variadic and the argument
7783 not, so flatten both argument lists. */
7784 parmvec
= expand_template_argument_pack (parmvec
);
7785 argvec
= expand_template_argument_pack (argvec
);
7789 /* In keeping with P0522R0, adjust P's template arguments
7790 to apply to A's template; then flatten it again. */
7791 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7792 nparmvec
= expand_template_argument_pack (nparmvec
);
7794 if (unify (tparms
, targs
, nparmvec
, argvec
,
7795 UNIFY_ALLOW_NONE
, explain_p
))
7798 /* If the P0522 adjustment eliminated a pack expansion, deduce
7801 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7802 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7803 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7808 /* Deduce arguments T, i from TT<T> or TT<i>.
7809 We check each element of PARMVEC and ARGVEC individually
7810 rather than the whole TREE_VEC since they can have
7811 different number of elements, which is allowed under N2555. */
7813 int len
= TREE_VEC_LENGTH (parmvec
);
7815 /* Check if the parameters end in a pack, making them
7817 int parm_variadic_p
= 0;
7819 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7820 parm_variadic_p
= 1;
7822 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7823 /* If the template argument list of P contains a pack
7824 expansion that is not the last template argument, the
7825 entire template argument list is a non-deduced
7827 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7828 return unify_success (explain_p
);
7830 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7831 return unify_too_few_arguments (explain_p
,
7832 TREE_VEC_LENGTH (argvec
), len
);
7834 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7835 if (unify (tparms
, targs
,
7836 TREE_VEC_ELT (parmvec
, i
),
7837 TREE_VEC_ELT (argvec
, i
),
7838 UNIFY_ALLOW_NONE
, explain_p
))
7842 && unify_pack_expansion (tparms
, targs
,
7845 /*subr=*/true, explain_p
))
7852 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7853 template template parameters. Both PARM_PARMS and ARG_PARMS are
7854 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7857 Consider the example:
7858 template <class T> class A;
7859 template<template <class U> class TT> class B;
7861 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7862 the parameters to A, and OUTER_ARGS contains A. */
7865 coerce_template_template_parms (tree parm_parms
,
7867 tsubst_flags_t complain
,
7871 int nparms
, nargs
, i
;
7875 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7876 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7878 nparms
= TREE_VEC_LENGTH (parm_parms
);
7879 nargs
= TREE_VEC_LENGTH (arg_parms
);
7883 /* P0522R0: A template template-parameter P is at least as specialized as
7884 a template template-argument A if, given the following rewrite to two
7885 function templates, the function template corresponding to P is at
7886 least as specialized as the function template corresponding to A
7887 according to the partial ordering rules for function templates
7888 ([temp.func.order]). Given an invented class template X with the
7889 template parameter list of A (including default arguments):
7891 * Each of the two function templates has the same template parameters,
7892 respectively, as P or A.
7894 * Each function template has a single function parameter whose type is
7895 a specialization of X with template arguments corresponding to the
7896 template parameters from the respective function template where, for
7897 each template parameter PP in the template parameter list of the
7898 function template, a corresponding template argument AA is formed. If
7899 PP declares a parameter pack, then AA is the pack expansion
7900 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7902 If the rewrite produces an invalid type, then P is not at least as
7903 specialized as A. */
7905 /* So coerce P's args to apply to A's parms, and then deduce between A's
7906 args and the converted args. If that succeeds, A is at least as
7907 specialized as P, so they match.*/
7908 tree pargs
= template_parms_level_to_args (parm_parms
);
7909 pargs
= add_outermost_template_args (outer_args
, pargs
);
7910 ++processing_template_decl
;
7911 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7912 /*require_all*/true, /*use_default*/true);
7913 --processing_template_decl
;
7914 if (pargs
!= error_mark_node
)
7916 tree targs
= make_tree_vec (nargs
);
7917 tree aargs
= template_parms_level_to_args (arg_parms
);
7918 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7924 /* Determine whether we have a parameter pack at the end of the
7925 template template parameter's template parameter list. */
7926 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7928 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7930 if (error_operand_p (parm
))
7933 switch (TREE_CODE (parm
))
7937 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7942 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7952 && !(variadic_p
&& nargs
>= nparms
- 1))
7955 /* Check all of the template parameters except the parameter pack at
7956 the end (if any). */
7957 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7959 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7960 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7963 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7964 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7966 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7974 /* Check each of the template parameters in the template
7975 argument against the template parameter pack at the end of
7976 the template template parameter. */
7977 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7980 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7982 for (; i
< nargs
; ++i
)
7984 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7987 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7989 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7998 /* Verifies that the deduced template arguments (in TARGS) for the
7999 template template parameters (in TPARMS) represent valid bindings,
8000 by comparing the template parameter list of each template argument
8001 to the template parameter list of its corresponding template
8002 template parameter, in accordance with DR150. This
8003 routine can only be called after all template arguments have been
8004 deduced. It will return TRUE if all of the template template
8005 parameter bindings are okay, FALSE otherwise. */
8007 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8009 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8012 /* We're dealing with template parms in this process. */
8013 ++processing_template_decl
;
8015 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8017 for (i
= 0; i
< ntparms
; ++i
)
8019 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8020 tree targ
= TREE_VEC_ELT (targs
, i
);
8022 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8024 tree packed_args
= NULL_TREE
;
8027 if (ARGUMENT_PACK_P (targ
))
8029 /* Look inside the argument pack. */
8030 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8031 len
= TREE_VEC_LENGTH (packed_args
);
8034 for (idx
= 0; idx
< len
; ++idx
)
8036 tree targ_parms
= NULL_TREE
;
8039 /* Extract the next argument from the argument
8041 targ
= TREE_VEC_ELT (packed_args
, idx
);
8043 if (PACK_EXPANSION_P (targ
))
8044 /* Look at the pattern of the pack expansion. */
8045 targ
= PACK_EXPANSION_PATTERN (targ
);
8047 /* Extract the template parameters from the template
8049 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8050 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8051 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8052 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8054 /* Verify that we can coerce the template template
8055 parameters from the template argument to the template
8056 parameter. This requires an exact match. */
8058 && !coerce_template_template_parms
8059 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8074 --processing_template_decl
;
8078 /* Since type attributes aren't mangled, we need to strip them from
8079 template type arguments. */
8082 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8084 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8086 bool removed_attributes
= false;
8087 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8088 if (removed_attributes
8089 && (complain
& tf_warning
))
8090 warning (OPT_Wignored_attributes
,
8091 "ignoring attributes on template argument %qT", arg
);
8095 /* And from inside dependent non-type arguments like sizeof(Type). */
8098 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8100 if (!arg
|| arg
== error_mark_node
)
8102 bool removed_attributes
= false;
8103 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8104 if (removed_attributes
8105 && (complain
& tf_warning
))
8106 warning (OPT_Wignored_attributes
,
8107 "ignoring attributes in template argument %qE", arg
);
8111 // A template declaration can be substituted for a constrained
8112 // template template parameter only when the argument is more
8113 // constrained than the parameter.
8115 is_compatible_template_arg (tree parm
, tree arg
)
8117 tree parm_cons
= get_constraints (parm
);
8119 /* For now, allow constrained template template arguments
8120 and unconstrained template template parameters. */
8121 if (parm_cons
== NULL_TREE
)
8124 /* If the template parameter is constrained, we need to rewrite its
8125 constraints in terms of the ARG's template parameters. This ensures
8126 that all of the template parameter types will have the same depth.
8128 Note that this is only valid when coerce_template_template_parm is
8129 true for the innermost template parameters of PARM and ARG. In other
8130 words, because coercion is successful, this conversion will be valid. */
8131 tree new_args
= NULL_TREE
;
8134 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8135 new_args
= template_parms_level_to_args (aparms
);
8136 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8137 tf_none
, NULL_TREE
);
8138 if (parm_cons
== error_mark_node
)
8142 return weakly_subsumes (parm_cons
, new_args
, arg
);
8145 // Convert a placeholder argument into a binding to the original
8146 // parameter. The original parameter is saved as the TREE_TYPE of
8149 convert_wildcard_argument (tree parm
, tree arg
)
8151 TREE_TYPE (arg
) = parm
;
8155 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8156 because one of them is dependent. But we need to represent the
8157 conversion for the benefit of cp_tree_equal. */
8160 maybe_convert_nontype_argument (tree type
, tree arg
)
8162 /* Auto parms get no conversion. */
8163 if (type_uses_auto (type
))
8165 /* We don't need or want to add this conversion now if we're going to use the
8166 argument for deduction. */
8167 if (value_dependent_expression_p (arg
))
8170 type
= cv_unqualified (type
);
8171 tree argtype
= TREE_TYPE (arg
);
8172 if (same_type_p (type
, argtype
))
8175 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8176 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8180 /* Convert the indicated template ARG as necessary to match the
8181 indicated template PARM. Returns the converted ARG, or
8182 error_mark_node if the conversion was unsuccessful. Error and
8183 warning messages are issued under control of COMPLAIN. This
8184 conversion is for the Ith parameter in the parameter list. ARGS is
8185 the full set of template arguments deduced so far. */
8188 convert_template_argument (tree parm
,
8191 tsubst_flags_t complain
,
8197 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8199 if (parm
== error_mark_node
|| error_operand_p (arg
))
8200 return error_mark_node
;
8202 /* Trivially convert placeholders. */
8203 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8204 return convert_wildcard_argument (parm
, arg
);
8206 if (arg
== any_targ_node
)
8209 if (TREE_CODE (arg
) == TREE_LIST
8210 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8212 /* The template argument was the name of some
8213 member function. That's usually
8214 invalid, but static members are OK. In any
8215 case, grab the underlying fields/functions
8216 and issue an error later if required. */
8217 TREE_TYPE (arg
) = unknown_type_node
;
8222 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8223 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8224 || requires_tmpl_type
);
8226 /* When determining whether an argument pack expansion is a template,
8227 look at the pattern. */
8228 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8229 arg
= PACK_EXPANSION_PATTERN (arg
);
8231 /* Deal with an injected-class-name used as a template template arg. */
8232 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8234 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8235 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8237 if (cxx_dialect
>= cxx11
)
8238 /* OK under DR 1004. */;
8239 else if (complain
& tf_warning_or_error
)
8240 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8241 " used as template template argument", TYPE_NAME (arg
));
8242 else if (flag_pedantic_errors
)
8250 ((TREE_CODE (arg
) == TEMPLATE_DECL
8251 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8252 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8253 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8254 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8257 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8258 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8259 arg
= TYPE_STUB_DECL (arg
);
8261 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8263 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8264 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8266 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8268 if (complain
& tf_error
)
8269 error ("invalid use of destructor %qE as a type", orig_arg
);
8270 return error_mark_node
;
8273 permerror (input_location
,
8274 "to refer to a type member of a template parameter, "
8275 "use %<typename %E%>", orig_arg
);
8277 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8278 TREE_OPERAND (arg
, 1),
8284 if (is_type
!= requires_type
)
8288 if (complain
& tf_error
)
8290 error ("type/value mismatch at argument %d in template "
8291 "parameter list for %qD",
8295 /* The template argument is a type, but we're expecting
8297 inform (input_location
,
8298 " expected a constant of type %qT, got %qT",
8300 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8301 /* [temp.arg]/2: "In a template-argument, an ambiguity
8302 between a type-id and an expression is resolved to a
8303 type-id, regardless of the form of the corresponding
8304 template-parameter." So give the user a clue. */
8305 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8306 inform (input_location
, " ambiguous template argument "
8307 "for non-type template parameter is treated as "
8310 else if (requires_tmpl_type
)
8311 inform (input_location
,
8312 " expected a class template, got %qE", orig_arg
);
8314 inform (input_location
,
8315 " expected a type, got %qE", orig_arg
);
8318 return error_mark_node
;
8320 if (is_tmpl_type
^ requires_tmpl_type
)
8322 if (in_decl
&& (complain
& tf_error
))
8324 error ("type/value mismatch at argument %d in template "
8325 "parameter list for %qD",
8328 inform (input_location
,
8329 " expected a type, got %qT", DECL_NAME (arg
));
8331 inform (input_location
,
8332 " expected a class template, got %qT", orig_arg
);
8334 return error_mark_node
;
8337 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8338 /* We already did the appropriate conversion when packing args. */
8342 if (requires_tmpl_type
)
8344 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8345 /* The number of argument required is not known yet.
8346 Just accept it for now. */
8350 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8353 /* Strip alias templates that are equivalent to another
8355 arg
= get_underlying_template (arg
);
8356 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8358 if (coerce_template_template_parms (parmparm
, argparm
,
8364 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8366 if (val
!= error_mark_node
)
8368 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8369 val
= TREE_TYPE (val
);
8370 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8371 val
= make_pack_expansion (val
, complain
);
8376 if (in_decl
&& (complain
& tf_error
))
8378 error ("type/value mismatch at argument %d in "
8379 "template parameter list for %qD",
8381 inform (input_location
,
8382 " expected a template of type %qD, got %qT",
8386 val
= error_mark_node
;
8389 // Check that the constraints are compatible before allowing the
8391 if (val
!= error_mark_node
)
8392 if (!is_compatible_template_arg (parm
, arg
))
8394 if (in_decl
&& (complain
& tf_error
))
8396 error ("constraint mismatch at argument %d in "
8397 "template parameter list for %qD",
8399 inform (input_location
, " expected %qD but got %qD",
8402 val
= error_mark_node
;
8408 /* We only form one instance of each template specialization.
8409 Therefore, if we use a non-canonical variant (i.e., a
8410 typedef), any future messages referring to the type will use
8411 the typedef, which is confusing if those future uses do not
8412 themselves also use the typedef. */
8414 val
= canonicalize_type_argument (val
, complain
);
8418 tree t
= TREE_TYPE (parm
);
8420 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8421 > TMPL_ARGS_DEPTH (args
))
8422 /* We don't have enough levels of args to do any substitution. This
8423 can happen in the context of -fnew-ttp-matching. */;
8424 else if (tree a
= type_uses_auto (t
))
8426 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8427 if (t
== error_mark_node
)
8428 return error_mark_node
;
8431 t
= tsubst (t
, args
, complain
, in_decl
);
8433 if (invalid_nontype_parm_type_p (t
, complain
))
8434 return error_mark_node
;
8436 if (t
!= TREE_TYPE (parm
))
8437 t
= canonicalize_type_argument (t
, complain
);
8439 if (!type_dependent_expression_p (orig_arg
)
8440 && !uses_template_parms (t
))
8441 /* We used to call digest_init here. However, digest_init
8442 will report errors, which we don't want when complain
8443 is zero. More importantly, digest_init will try too
8444 hard to convert things: for example, `0' should not be
8445 converted to pointer type at this point according to
8446 the standard. Accepting this is not merely an
8447 extension, since deciding whether or not these
8448 conversions can occur is part of determining which
8449 function template to call, or whether a given explicit
8450 argument specification is valid. */
8451 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8454 val
= canonicalize_expr_argument (orig_arg
, complain
);
8455 val
= maybe_convert_nontype_argument (t
, val
);
8459 if (val
== NULL_TREE
)
8460 val
= error_mark_node
;
8461 else if (val
== error_mark_node
&& (complain
& tf_error
))
8462 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8463 "could not convert template argument %qE from %qT to %qT",
8464 orig_arg
, TREE_TYPE (orig_arg
), t
);
8466 if (INDIRECT_REF_P (val
))
8468 /* Reject template arguments that are references to built-in
8469 functions with no library fallbacks. */
8470 const_tree inner
= TREE_OPERAND (val
, 0);
8471 const_tree innertype
= TREE_TYPE (inner
);
8473 && TYPE_REF_P (innertype
)
8474 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8475 && TREE_OPERAND_LENGTH (inner
) > 0
8476 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8477 return error_mark_node
;
8480 if (TREE_CODE (val
) == SCOPE_REF
)
8482 /* Strip typedefs from the SCOPE_REF. */
8483 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8484 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8486 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8487 QUALIFIED_NAME_IS_TEMPLATE (val
));
8494 /* Coerces the remaining template arguments in INNER_ARGS (from
8495 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8496 Returns the coerced argument pack. PARM_IDX is the position of this
8497 parameter in the template parameter list. ARGS is the original
8498 template argument list. */
8500 coerce_template_parameter_pack (tree parms
,
8508 tsubst_flags_t complain
)
8510 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8511 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8514 tree packed_parms
= NULL_TREE
;
8516 if (arg_idx
> nargs
)
8519 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8521 /* When the template parameter is a non-type template parameter pack
8522 or template template parameter pack whose type or template
8523 parameters use parameter packs, we know exactly how many arguments
8524 we are looking for. Build a vector of the instantiated decls for
8525 these template parameters in PACKED_PARMS. */
8526 /* We can't use make_pack_expansion here because it would interpret a
8527 _DECL as a use rather than a declaration. */
8528 tree decl
= TREE_VALUE (parm
);
8529 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8530 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8531 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8532 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8534 TREE_VEC_LENGTH (args
)--;
8535 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8536 TREE_VEC_LENGTH (args
)++;
8538 if (packed_parms
== error_mark_node
)
8539 return error_mark_node
;
8541 /* If we're doing a partial instantiation of a member template,
8542 verify that all of the types used for the non-type
8543 template parameter pack are, in fact, valid for non-type
8544 template parameters. */
8546 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8548 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8549 for (j
= 0; j
< len
; ++j
)
8551 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8552 if (TREE_CODE (t
) == PARM_DECL
8553 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8554 return error_mark_node
;
8556 /* We don't know how many args we have yet, just
8557 use the unconverted ones for now. */
8561 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8563 /* Check if we have a placeholder pack, which indicates we're
8564 in the context of a introduction list. In that case we want
8565 to match this pack to the single placeholder. */
8566 else if (arg_idx
< nargs
8567 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8568 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8570 nargs
= arg_idx
+ 1;
8571 packed_args
= make_tree_vec (1);
8574 packed_args
= make_tree_vec (nargs
- arg_idx
);
8576 /* Convert the remaining arguments, which will be a part of the
8577 parameter pack "parm". */
8578 int first_pack_arg
= arg_idx
;
8579 for (; arg_idx
< nargs
; ++arg_idx
)
8581 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8582 tree actual_parm
= TREE_VALUE (parm
);
8583 int pack_idx
= arg_idx
- first_pack_arg
;
8587 /* Once we've packed as many args as we have types, stop. */
8588 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8590 else if (PACK_EXPANSION_P (arg
))
8591 /* We don't know how many args we have yet, just
8592 use the unconverted ones for now. */
8595 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8598 if (arg
== error_mark_node
)
8600 if (complain
& tf_error
)
8601 error ("template argument %d is invalid", arg_idx
+ 1);
8604 arg
= convert_template_argument (actual_parm
,
8605 arg
, new_args
, complain
, parm_idx
,
8607 if (arg
== error_mark_node
)
8609 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8612 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8613 && TREE_VEC_LENGTH (packed_args
) > 0)
8615 if (complain
& tf_error
)
8616 error ("wrong number of template arguments (%d, should be %d)",
8617 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8618 return error_mark_node
;
8621 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8622 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8623 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8626 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8627 TREE_CONSTANT (argument_pack
) = 1;
8630 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8632 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8633 TREE_VEC_LENGTH (packed_args
));
8634 return argument_pack
;
8637 /* Returns the number of pack expansions in the template argument vector
8641 pack_expansion_args_count (tree args
)
8646 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8648 tree elt
= TREE_VEC_ELT (args
, i
);
8649 if (elt
&& PACK_EXPANSION_P (elt
))
8655 /* Convert all template arguments to their appropriate types, and
8656 return a vector containing the innermost resulting template
8657 arguments. If any error occurs, return error_mark_node. Error and
8658 warning messages are issued under control of COMPLAIN.
8660 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8661 for arguments not specified in ARGS. Otherwise, if
8662 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8663 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8664 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8668 coerce_template_parms (tree parms
,
8671 tsubst_flags_t complain
,
8672 bool require_all_args
,
8673 bool use_default_args
)
8675 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8676 tree orig_inner_args
;
8679 tree new_inner_args
;
8681 /* When used as a boolean value, indicates whether this is a
8682 variadic template parameter list. Since it's an int, we can also
8683 subtract it from nparms to get the number of non-variadic
8686 int variadic_args_p
= 0;
8687 int post_variadic_parms
= 0;
8689 /* Adjustment to nparms for fixed parameter packs. */
8690 int fixed_pack_adjust
= 0;
8691 int fixed_packs
= 0;
8694 /* Likewise for parameters with default arguments. */
8697 if (args
== error_mark_node
)
8698 return error_mark_node
;
8700 nparms
= TREE_VEC_LENGTH (parms
);
8702 /* Determine if there are any parameter packs or default arguments. */
8703 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8705 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8707 ++post_variadic_parms
;
8708 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8710 if (TREE_PURPOSE (parm
))
8714 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8715 /* If there are no parameters that follow a parameter pack, we need to
8716 expand any argument packs so that we can deduce a parameter pack from
8717 some non-packed args followed by an argument pack, as in variadic85.C.
8718 If there are such parameters, we need to leave argument packs intact
8719 so the arguments are assigned properly. This can happen when dealing
8720 with a nested class inside a partial specialization of a class
8721 template, as in variadic92.C, or when deducing a template parameter pack
8722 from a sub-declarator, as in variadic114.C. */
8723 if (!post_variadic_parms
)
8724 inner_args
= expand_template_argument_pack (inner_args
);
8726 /* Count any pack expansion args. */
8727 variadic_args_p
= pack_expansion_args_count (inner_args
);
8729 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8730 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8731 || (nargs
< nparms
- variadic_p
8734 && (!use_default_args
8735 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8736 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8739 if (complain
& tf_error
)
8741 if (variadic_p
|| default_p
)
8743 nparms
-= variadic_p
+ default_p
;
8744 error ("wrong number of template arguments "
8745 "(%d, should be at least %d)", nargs
, nparms
);
8748 error ("wrong number of template arguments "
8749 "(%d, should be %d)", nargs
, nparms
);
8752 inform (DECL_SOURCE_LOCATION (in_decl
),
8753 "provided for %qD", in_decl
);
8756 return error_mark_node
;
8758 /* We can't pass a pack expansion to a non-pack parameter of an alias
8759 template (DR 1430). */
8761 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8762 || concept_definition_p (in_decl
))
8764 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8766 if (complain
& tf_error
)
8768 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8770 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8771 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8773 if (PACK_EXPANSION_P (arg
)
8774 && !template_parameter_pack_p (parm
))
8776 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8777 error_at (location_of (arg
),
8778 "pack expansion argument for non-pack parameter "
8779 "%qD of alias template %qD", parm
, in_decl
);
8781 error_at (location_of (arg
),
8782 "pack expansion argument for non-pack parameter "
8783 "%qD of concept %qD", parm
, in_decl
);
8784 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8791 return error_mark_node
;
8794 /* We need to evaluate the template arguments, even though this
8795 template-id may be nested within a "sizeof". */
8798 new_inner_args
= make_tree_vec (nparms
);
8799 new_args
= add_outermost_template_args (args
, new_inner_args
);
8800 int pack_adjust
= 0;
8801 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8806 /* Get the Ith template parameter. */
8807 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8809 if (parm
== error_mark_node
)
8811 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8815 /* Calculate the next argument. */
8816 if (arg_idx
< nargs
)
8817 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8821 if (template_parameter_pack_p (TREE_VALUE (parm
))
8822 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8823 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8825 /* Some arguments will be placed in the
8826 template parameter pack PARM. */
8827 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8828 inner_args
, arg_idx
,
8832 if (arg
== NULL_TREE
)
8834 /* We don't know how many args we have yet, just use the
8835 unconverted (and still packed) ones for now. */
8836 new_inner_args
= orig_inner_args
;
8841 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8843 /* Store this argument. */
8844 if (arg
== error_mark_node
)
8847 /* We are done with all of the arguments. */
8853 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8854 arg_idx
+= pack_adjust
;
8855 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8858 fixed_pack_adjust
+= pack_adjust
;
8866 if (PACK_EXPANSION_P (arg
))
8868 /* "If every valid specialization of a variadic template
8869 requires an empty template parameter pack, the template is
8870 ill-formed, no diagnostic required." So check that the
8871 pattern works with this parameter. */
8872 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8873 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8877 if (conv
== error_mark_node
)
8879 if (complain
& tf_error
)
8880 inform (input_location
, "so any instantiation with a "
8881 "non-empty parameter pack would be ill-formed");
8884 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8885 /* Recover from missing typename. */
8886 TREE_VEC_ELT (inner_args
, arg_idx
)
8887 = make_pack_expansion (conv
, complain
);
8889 /* We don't know how many args we have yet, just
8890 use the unconverted ones for now. */
8891 new_inner_args
= inner_args
;
8896 else if (require_all_args
)
8898 /* There must be a default arg in this case. */
8899 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8901 /* The position of the first default template argument,
8902 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8904 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8905 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8906 arg_idx
- pack_adjust
);
8911 if (arg
== error_mark_node
)
8913 if (complain
& tf_error
)
8914 error ("template argument %d is invalid", arg_idx
+ 1);
8918 /* This can occur if there was an error in the template
8919 parameter list itself (which we would already have
8920 reported) that we are trying to recover from, e.g., a class
8921 template with a parameter list such as
8922 template<typename..., typename> (cpp0x/variadic150.C). */
8925 /* This can also happen with a fixed parameter pack (71834). */
8926 if (arg_idx
>= nargs
)
8930 arg
= convert_template_argument (TREE_VALUE (parm
),
8931 arg
, new_args
, complain
,
8934 if (arg
== error_mark_node
)
8937 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8940 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8942 /* If we had fixed parameter packs, we didn't know how many arguments we
8943 actually needed earlier; now we do. */
8944 nparms
+= fixed_pack_adjust
;
8945 variadic_p
-= fixed_packs
;
8949 if (arg_idx
< nargs
)
8951 /* We had some pack expansion arguments that will only work if the packs
8952 are empty, but wait until instantiation time to complain.
8953 See variadic-ttp3.C. */
8955 /* Except that we can't provide empty packs to alias templates or
8956 concepts when there are no corresponding parameters. Basically,
8957 we can get here with this:
8959 template<typename T> concept C = true;
8961 template<typename... Args>
8965 When parsing C<Args...>, we try to form a concept check of
8966 C<?, Args...>. Without the extra check for substituting an empty
8967 pack past the last parameter, we can accept the check as valid.
8969 FIXME: This may be valid for alias templates (but I doubt it).
8971 FIXME: The error could be better also. */
8972 if (in_decl
&& concept_definition_p (in_decl
))
8974 if (complain
& tf_error
)
8975 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8976 "too many arguments");
8977 return error_mark_node
;
8980 int len
= nparms
+ (nargs
- arg_idx
);
8981 tree args
= make_tree_vec (len
);
8983 for (; i
< nparms
; ++i
)
8984 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8985 for (; i
< len
; ++i
, ++arg_idx
)
8986 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8987 arg_idx
- pack_adjust
);
8988 new_inner_args
= args
;
8993 gcc_assert (!(complain
& tf_error
) || seen_error ());
8994 return error_mark_node
;
8997 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8998 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8999 TREE_VEC_LENGTH (new_inner_args
));
9001 return new_inner_args
;
9004 /* Convert all template arguments to their appropriate types, and
9005 return a vector containing the innermost resulting template
9006 arguments. If any error occurs, return error_mark_node. Error and
9007 warning messages are not issued.
9009 Note that no function argument deduction is performed, and default
9010 arguments are used to fill in unspecified arguments. */
9012 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9014 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9017 /* Convert all template arguments to their appropriate type, and
9018 instantiate default arguments as needed. This returns a vector
9019 containing the innermost resulting template arguments, or
9020 error_mark_node if unsuccessful. */
9022 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9023 tsubst_flags_t complain
)
9025 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9028 /* Like coerce_template_parms. If PARMS represents all template
9029 parameters levels, this function returns a vector of vectors
9030 representing all the resulting argument levels. Note that in this
9031 case, only the innermost arguments are coerced because the
9032 outermost ones are supposed to have been coerced already.
9034 Otherwise, if PARMS represents only (the innermost) vector of
9035 parameters, this function returns a vector containing just the
9036 innermost resulting arguments. */
9039 coerce_innermost_template_parms (tree parms
,
9042 tsubst_flags_t complain
,
9043 bool require_all_args
,
9044 bool use_default_args
)
9046 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9047 int args_depth
= TMPL_ARGS_DEPTH (args
);
9050 if (parms_depth
> 1)
9052 coerced_args
= make_tree_vec (parms_depth
);
9056 for (level
= parms
, cur_depth
= parms_depth
;
9057 parms_depth
> 0 && level
!= NULL_TREE
;
9058 level
= TREE_CHAIN (level
), --cur_depth
)
9061 if (cur_depth
== args_depth
)
9062 l
= coerce_template_parms (TREE_VALUE (level
),
9063 args
, in_decl
, complain
,
9067 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9069 if (l
== error_mark_node
)
9070 return error_mark_node
;
9072 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9076 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9077 args
, in_decl
, complain
,
9080 return coerced_args
;
9083 /* Returns true if T is a wrapper to make a C++20 template parameter
9087 class_nttp_const_wrapper_p (tree t
)
9089 if (cxx_dialect
< cxx20
)
9091 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9092 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9093 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9096 /* Returns 1 if template args OT and NT are equivalent. */
9099 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9103 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9105 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9108 if (class_nttp_const_wrapper_p (nt
))
9109 nt
= TREE_OPERAND (nt
, 0);
9110 if (class_nttp_const_wrapper_p (ot
))
9111 ot
= TREE_OPERAND (ot
, 0);
9113 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9114 /* For member templates */
9115 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9116 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9117 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9118 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9119 PACK_EXPANSION_PATTERN (nt
))
9120 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9121 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9122 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9123 return cp_tree_equal (ot
, nt
);
9124 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9126 else if (TYPE_P (nt
) || TYPE_P (ot
))
9128 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9130 /* Don't treat an alias template specialization with dependent
9131 arguments as equivalent to its underlying type when used as a
9132 template argument; we need them to be distinct so that we
9133 substitute into the specialization arguments at instantiation
9134 time. And aliases can't be equivalent without being ==, so
9135 we don't need to look any deeper.
9137 During partial ordering, however, we need to treat them normally so
9138 that we can order uses of the same alias with different
9139 cv-qualification (79960). */
9141 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9144 return same_type_p (ot
, nt
);
9148 /* Try to treat a template non-type argument that has been converted
9149 to the parameter type as equivalent to one that hasn't yet. */
9150 for (enum tree_code code1
= TREE_CODE (ot
);
9151 CONVERT_EXPR_CODE_P (code1
)
9152 || code1
== NON_LVALUE_EXPR
;
9153 code1
= TREE_CODE (ot
))
9154 ot
= TREE_OPERAND (ot
, 0);
9156 for (enum tree_code code2
= TREE_CODE (nt
);
9157 CONVERT_EXPR_CODE_P (code2
)
9158 || code2
== NON_LVALUE_EXPR
;
9159 code2
= TREE_CODE (nt
))
9160 nt
= TREE_OPERAND (nt
, 0);
9162 return cp_tree_equal (ot
, nt
);
9166 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9167 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9168 NEWARG_PTR with the offending arguments if they are non-NULL. */
9171 comp_template_args (tree oldargs
, tree newargs
,
9172 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9177 if (oldargs
== newargs
)
9180 if (!oldargs
|| !newargs
)
9183 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9186 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9188 tree nt
= TREE_VEC_ELT (newargs
, i
);
9189 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9191 if (! template_args_equal (ot
, nt
, partial_order
))
9193 if (oldarg_ptr
!= NULL
)
9195 if (newarg_ptr
!= NULL
)
9204 comp_template_args_porder (tree oargs
, tree nargs
)
9206 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9209 /* Implement a freelist interface for objects of type T.
9211 Head is a separate object, rather than a regular member, so that we
9212 can define it as a GTY deletable pointer, which is highly
9213 desirable. A data member could be declared that way, but then the
9214 containing object would implicitly get GTY((user)), which would
9215 prevent us from instantiating freelists as global objects.
9216 Although this way we can create freelist global objects, they're
9217 such thin wrappers that instantiating temporaries at every use
9218 loses nothing and saves permanent storage for the freelist object.
9220 Member functions next, anew, poison and reinit have default
9221 implementations that work for most of the types we're interested
9222 in, but if they don't work for some type, they should be explicitly
9223 specialized. See the comments before them for requirements, and
9224 the example specializations for the tree_list_freelist. */
9225 template <typename T
>
9228 /* Return the next object in a chain. We could just do type
9229 punning, but if we access the object with its underlying type, we
9230 avoid strict-aliasing trouble. This needs only work between
9231 poison and reinit. */
9232 static T
*&next (T
*obj
) { return obj
->next
; }
9234 /* Return a newly allocated, uninitialized or minimally-initialized
9235 object of type T. Any initialization performed by anew should
9236 either remain across the life of the object and the execution of
9237 poison, or be redone by reinit. */
9238 static T
*anew () { return ggc_alloc
<T
> (); }
9240 /* Optionally scribble all over the bits holding the object, so that
9241 they become (mostly?) uninitialized memory. This is called while
9242 preparing to make the object part of the free list. */
9243 static void poison (T
*obj
) {
9244 T
*p ATTRIBUTE_UNUSED
= obj
;
9245 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9247 #ifdef ENABLE_GC_CHECKING
9248 /* Poison the data, to indicate the data is garbage. */
9249 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9250 memset (p
, 0xa5, sizeof (*p
));
9252 /* Let valgrind know the object is free. */
9253 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9255 /* Let valgrind know the next portion of the object is available,
9256 but uninitialized. */
9257 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9260 /* Bring an object that underwent at least one lifecycle after anew
9261 and before the most recent free and poison, back to a usable
9262 state, reinitializing whatever is needed for it to be
9263 functionally equivalent to an object just allocated and returned
9264 by anew. This may poison or clear the next field, used by
9265 freelist housekeeping after poison was called. */
9266 static void reinit (T
*obj
) {
9267 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9269 #ifdef ENABLE_GC_CHECKING
9270 memset (q
, 0xa5, sizeof (*q
));
9272 /* Let valgrind know the entire object is available, but
9274 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9277 /* Reference a GTY-deletable pointer that points to the first object
9278 in the free list proper. */
9281 /* Construct a freelist object chaining objects off of HEAD. */
9282 freelist (T
*&head
) : head(head
) {}
9284 /* Add OBJ to the free object list. The former head becomes OBJ's
9293 /* Take an object from the free list, if one is available, or
9294 allocate a new one. Objects taken from the free list should be
9295 regarded as filled with garbage, except for bits that are
9296 configured to be preserved across free and alloc. */
9311 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9312 want to allocate a TREE_LIST using the usual interface, and ensure
9313 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9314 build_tree_list logic in reinit, so this could go out of sync. */
9317 freelist
<tree_node
>::next (tree obj
)
9319 return TREE_CHAIN (obj
);
9323 freelist
<tree_node
>::anew ()
9325 return build_tree_list (NULL
, NULL
);
9329 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9331 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9332 tree p ATTRIBUTE_UNUSED
= obj
;
9333 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9334 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9336 #ifdef ENABLE_GC_CHECKING
9337 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9339 /* Poison the data, to indicate the data is garbage. */
9340 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9341 memset (p
, 0xa5, size
);
9343 /* Let valgrind know the object is free. */
9344 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9345 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9346 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9347 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9349 #ifdef ENABLE_GC_CHECKING
9350 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9351 /* Keep TREE_CHAIN functional. */
9352 TREE_SET_CODE (obj
, TREE_LIST
);
9354 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9359 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9361 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9363 #ifdef ENABLE_GC_CHECKING
9364 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9366 memset (obj
, 0, sizeof (tree_list
));
9369 /* Let valgrind know the entire object is available, but
9371 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9373 #ifdef ENABLE_GC_CHECKING
9374 TREE_SET_CODE (obj
, TREE_LIST
);
9376 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9380 /* Point to the first object in the TREE_LIST freelist. */
9381 static GTY((deletable
)) tree tree_list_freelist_head
;
9382 /* Return the/an actual TREE_LIST freelist. */
9383 static inline freelist
<tree_node
>
9384 tree_list_freelist ()
9386 return tree_list_freelist_head
;
9389 /* Point to the first object in the tinst_level freelist. */
9390 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9391 /* Return the/an actual tinst_level freelist. */
9392 static inline freelist
<tinst_level
>
9393 tinst_level_freelist ()
9395 return tinst_level_freelist_head
;
9398 /* Point to the first object in the pending_template freelist. */
9399 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9400 /* Return the/an actual pending_template freelist. */
9401 static inline freelist
<pending_template
>
9402 pending_template_freelist ()
9404 return pending_template_freelist_head
;
9407 /* Build the TREE_LIST object out of a split list, store it
9408 permanently, and return it. */
9410 tinst_level::to_list ()
9412 gcc_assert (split_list_p ());
9413 tree ret
= tree_list_freelist ().alloc ();
9414 TREE_PURPOSE (ret
) = tldcl
;
9415 TREE_VALUE (ret
) = targs
;
9418 gcc_assert (tree_list_p ());
9422 const unsigned short tinst_level::refcount_infinity
;
9424 /* Increment OBJ's refcount unless it is already infinite. */
9425 static tinst_level
*
9426 inc_refcount_use (tinst_level
*obj
)
9428 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9433 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9435 tinst_level::free (tinst_level
*obj
)
9437 if (obj
->tree_list_p ())
9438 tree_list_freelist ().free (obj
->get_node ());
9439 tinst_level_freelist ().free (obj
);
9442 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9443 OBJ's DECL and OBJ, and start over with the tinst_level object that
9444 used to be referenced by OBJ's NEXT. */
9446 dec_refcount_use (tinst_level
*obj
)
9449 && obj
->refcount
!= tinst_level::refcount_infinity
9450 && !--obj
->refcount
)
9452 tinst_level
*next
= obj
->next
;
9453 tinst_level::free (obj
);
9458 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9459 and of the former PTR. Omitting the second argument is equivalent
9460 to passing (T*)NULL; this is allowed because passing the
9461 zero-valued integral constant NULL confuses type deduction and/or
9462 overload resolution. */
9463 template <typename T
>
9465 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9468 ptr
= inc_refcount_use (obj
);
9469 dec_refcount_use (save
);
9473 add_pending_template (tree d
)
9475 tree ti
= (TYPE_P (d
)
9476 ? CLASSTYPE_TEMPLATE_INFO (d
)
9477 : DECL_TEMPLATE_INFO (d
));
9478 struct pending_template
*pt
;
9481 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9484 /* We are called both from instantiate_decl, where we've already had a
9485 tinst_level pushed, and instantiate_template, where we haven't.
9487 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9488 level
= !current_tinst_level
9489 || current_tinst_level
->maybe_get_node () != d
;
9492 push_tinst_level (d
);
9494 pt
= pending_template_freelist ().alloc ();
9497 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9498 if (last_pending_template
)
9499 last_pending_template
->next
= pt
;
9501 pending_templates
= pt
;
9503 last_pending_template
= pt
;
9505 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9512 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9513 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9514 documentation for TEMPLATE_ID_EXPR. */
9517 lookup_template_function (tree fns
, tree arglist
)
9519 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9520 return error_mark_node
;
9522 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9524 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9526 error ("%q#D is not a function template", fns
);
9527 return error_mark_node
;
9530 if (BASELINK_P (fns
))
9532 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9534 BASELINK_FUNCTIONS (fns
),
9539 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9542 /* Within the scope of a template class S<T>, the name S gets bound
9543 (in build_self_reference) to a TYPE_DECL for the class, not a
9544 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9545 or one of its enclosing classes, and that type is a template,
9546 return the associated TEMPLATE_DECL. Otherwise, the original
9549 Also handle the case when DECL is a TREE_LIST of ambiguous
9550 injected-class-names from different bases. */
9553 maybe_get_template_decl_from_type_decl (tree decl
)
9555 if (decl
== NULL_TREE
)
9558 /* DR 176: A lookup that finds an injected-class-name (10.2
9559 [class.member.lookup]) can result in an ambiguity in certain cases
9560 (for example, if it is found in more than one base class). If all of
9561 the injected-class-names that are found refer to specializations of
9562 the same class template, and if the name is followed by a
9563 template-argument-list, the reference refers to the class template
9564 itself and not a specialization thereof, and is not ambiguous. */
9565 if (TREE_CODE (decl
) == TREE_LIST
)
9567 tree t
, tmpl
= NULL_TREE
;
9568 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9570 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9573 else if (tmpl
!= elt
)
9576 if (tmpl
&& t
== NULL_TREE
)
9582 return (decl
!= NULL_TREE
9583 && DECL_SELF_REFERENCE_P (decl
)
9584 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9585 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9588 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9589 parameters, find the desired type.
9591 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9593 IN_DECL, if non-NULL, is the template declaration we are trying to
9596 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9597 the class we are looking up.
9599 Issue error and warning messages under control of COMPLAIN.
9601 If the template class is really a local class in a template
9602 function, then the FUNCTION_CONTEXT is the function in which it is
9605 ??? Note that this function is currently called *twice* for each
9606 template-id: the first time from the parser, while creating the
9607 incomplete type (finish_template_type), and the second type during the
9608 real instantiation (instantiate_template_class). This is surely something
9609 that we want to avoid. It also causes some problems with argument
9610 coercion (see convert_nontype_argument for more information on this). */
9613 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9614 int entering_scope
, tsubst_flags_t complain
)
9616 tree templ
= NULL_TREE
, parmlist
;
9623 if (identifier_p (d1
))
9625 tree value
= innermost_non_namespace_value (d1
);
9626 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9631 push_decl_namespace (context
);
9632 templ
= lookup_name (d1
);
9633 templ
= maybe_get_template_decl_from_type_decl (templ
);
9635 pop_decl_namespace ();
9638 context
= DECL_CONTEXT (templ
);
9640 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9642 tree type
= TREE_TYPE (d1
);
9644 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9645 an implicit typename for the second A. Deal with it. */
9646 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9647 type
= TREE_TYPE (type
);
9649 if (CLASSTYPE_TEMPLATE_INFO (type
))
9651 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9652 d1
= DECL_NAME (templ
);
9655 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9656 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9658 templ
= TYPE_TI_TEMPLATE (d1
);
9659 d1
= DECL_NAME (templ
);
9661 else if (DECL_TYPE_TEMPLATE_P (d1
))
9664 d1
= DECL_NAME (templ
);
9665 context
= DECL_CONTEXT (templ
);
9667 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9670 d1
= DECL_NAME (templ
);
9673 /* Issue an error message if we didn't find a template. */
9676 if (complain
& tf_error
)
9677 error ("%qT is not a template", d1
);
9678 return error_mark_node
;
9681 if (TREE_CODE (templ
) != TEMPLATE_DECL
9682 /* Make sure it's a user visible template, if it was named by
9684 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9685 && !PRIMARY_TEMPLATE_P (templ
)))
9687 if (complain
& tf_error
)
9689 error ("non-template type %qT used as a template", d1
);
9691 error ("for template declaration %q+D", in_decl
);
9693 return error_mark_node
;
9696 complain
&= ~tf_user
;
9698 /* An alias that just changes the name of a template is equivalent to the
9699 other template, so if any of the arguments are pack expansions, strip
9700 the alias to avoid problems with a pack expansion passed to a non-pack
9701 alias template parameter (DR 1430). */
9702 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9703 templ
= get_underlying_template (templ
);
9705 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9708 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9709 if (arglist2
== error_mark_node
9710 || (!uses_template_parms (arglist2
)
9711 && check_instantiated_args (templ
, arglist2
, complain
)))
9712 return error_mark_node
;
9714 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9719 tree template_type
= TREE_TYPE (templ
);
9722 tree found
= NULL_TREE
;
9725 int is_dependent_type
;
9726 int use_partial_inst_tmpl
= false;
9728 if (template_type
== error_mark_node
)
9729 /* An error occurred while building the template TEMPL, and a
9730 diagnostic has most certainly been emitted for that
9731 already. Let's propagate that error. */
9732 return error_mark_node
;
9734 gen_tmpl
= most_general_template (templ
);
9735 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9736 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9737 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9739 if (arg_depth
== 1 && parm_depth
> 1)
9741 /* We've been given an incomplete set of template arguments.
9744 template <class T> struct S1 {
9745 template <class U> struct S2 {};
9746 template <class U> struct S2<U*> {};
9749 we will be called with an ARGLIST of `U*', but the
9750 TEMPLATE will be `template <class T> template
9751 <class U> struct S1<T>::S2'. We must fill in the missing
9753 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9754 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9755 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9758 /* Now we should have enough arguments. */
9759 gcc_assert (parm_depth
== arg_depth
);
9761 /* From here on, we're only interested in the most general
9764 /* Calculate the BOUND_ARGS. These will be the args that are
9765 actually tsubst'd into the definition to create the
9767 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9769 /*require_all_args=*/true,
9770 /*use_default_args=*/true);
9772 if (arglist
== error_mark_node
)
9773 /* We were unable to bind the arguments. */
9774 return error_mark_node
;
9776 /* In the scope of a template class, explicit references to the
9777 template class refer to the type of the template, not any
9778 instantiation of it. For example, in:
9780 template <class T> class C { void f(C<T>); }
9782 the `C<T>' is just the same as `C'. Outside of the
9783 class, however, such a reference is an instantiation. */
9785 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9786 || currently_open_class (template_type
))
9788 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9790 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9791 return template_type
;
9794 /* If we already have this specialization, return it. */
9795 elt
.tmpl
= gen_tmpl
;
9797 elt
.spec
= NULL_TREE
;
9798 hash
= spec_hasher::hash (&elt
);
9799 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9804 /* If the template's constraints are not satisfied,
9805 then we cannot form a valid type.
9807 Note that the check is deferred until after the hash
9808 lookup. This prevents redundant checks on previously
9809 instantiated specializations. */
9811 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9812 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9814 if (complain
& tf_error
)
9816 auto_diagnostic_group d
;
9817 error ("template constraint failure for %qD", gen_tmpl
);
9818 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9820 return error_mark_node
;
9823 is_dependent_type
= uses_template_parms (arglist
);
9825 /* If the deduced arguments are invalid, then the binding
9827 if (!is_dependent_type
9828 && check_instantiated_args (gen_tmpl
,
9829 INNERMOST_TEMPLATE_ARGS (arglist
),
9831 return error_mark_node
;
9833 if (!is_dependent_type
9834 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9835 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9836 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9838 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9839 DECL_NAME (gen_tmpl
),
9840 /*tag_scope=*/ts_global
);
9844 context
= DECL_CONTEXT (gen_tmpl
);
9845 if (context
&& TYPE_P (context
))
9847 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9848 context
= complete_type (context
);
9851 context
= tsubst (context
, arglist
, complain
, in_decl
);
9853 if (context
== error_mark_node
)
9854 return error_mark_node
;
9857 context
= global_namespace
;
9859 /* Create the type. */
9860 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9862 /* The user referred to a specialization of an alias
9863 template represented by GEN_TMPL.
9865 [temp.alias]/2 says:
9867 When a template-id refers to the specialization of an
9868 alias template, it is equivalent to the associated
9869 type obtained by substitution of its
9870 template-arguments for the template-parameters in the
9871 type-id of the alias template. */
9873 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9874 /* Note that the call above (by indirectly calling
9875 register_specialization in tsubst_decl) registers the
9876 TYPE_DECL representing the specialization of the alias
9877 template. So next time someone substitutes ARGLIST for
9878 the template parms into the alias template (GEN_TMPL),
9879 she'll get that TYPE_DECL back. */
9881 if (t
== error_mark_node
)
9884 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9886 if (!is_dependent_type
)
9888 set_current_access_from_decl (TYPE_NAME (template_type
));
9889 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9890 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9891 arglist
, complain
, in_decl
),
9892 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9893 arglist
, complain
, in_decl
),
9894 SCOPED_ENUM_P (template_type
), NULL
);
9896 if (t
== error_mark_node
)
9901 /* We don't want to call start_enum for this type, since
9902 the values for the enumeration constants may involve
9903 template parameters. And, no one should be interested
9904 in the enumeration constants for such a type. */
9905 t
= cxx_make_type (ENUMERAL_TYPE
);
9906 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9908 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9909 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9910 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9912 else if (CLASS_TYPE_P (template_type
))
9914 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9915 instantiated here. */
9916 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9918 t
= make_class_type (TREE_CODE (template_type
));
9919 CLASSTYPE_DECLARED_CLASS (t
)
9920 = CLASSTYPE_DECLARED_CLASS (template_type
);
9921 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9923 /* A local class. Make sure the decl gets registered properly. */
9924 if (context
== current_function_decl
)
9925 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9927 return error_mark_node
;
9929 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9930 /* This instantiation is another name for the primary
9931 template type. Set the TYPE_CANONICAL field
9933 TYPE_CANONICAL (t
) = template_type
;
9934 else if (any_template_arguments_need_structural_equality_p (arglist
))
9935 /* Some of the template arguments require structural
9936 equality testing, so this template class requires
9937 structural equality testing. */
9938 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9943 /* If we called start_enum or pushtag above, this information
9944 will already be set up. */
9945 type_decl
= TYPE_NAME (t
);
9948 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9950 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9951 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9952 DECL_SOURCE_LOCATION (type_decl
)
9953 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9956 if (CLASS_TYPE_P (template_type
))
9958 TREE_PRIVATE (type_decl
)
9959 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9960 TREE_PROTECTED (type_decl
)
9961 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9962 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9964 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9965 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9969 if (OVERLOAD_TYPE_P (t
)
9970 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9972 static const char *tags
[] = {"abi_tag", "may_alias"};
9974 for (unsigned ix
= 0; ix
!= 2; ix
++)
9977 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9981 = tree_cons (TREE_PURPOSE (attributes
),
9982 TREE_VALUE (attributes
),
9983 TYPE_ATTRIBUTES (t
));
9987 /* Let's consider the explicit specialization of a member
9988 of a class template specialization that is implicitly instantiated,
9993 template<class U> struct M {}; //#0
9998 struct S<int>::M<char> //#1
10002 [temp.expl.spec]/4 says this is valid.
10004 In this case, when we write:
10007 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10010 When we encounter #1, we want to store the partial instantiation
10011 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10013 For all cases other than this "explicit specialization of member of a
10014 class template", we just want to store the most general template into
10015 the CLASSTYPE_TI_TEMPLATE of M.
10017 This case of "explicit specialization of member of a class template"
10019 1/ the enclosing class is an instantiation of, and therefore not
10020 the same as, the context of the most general template, and
10021 2/ we aren't looking at the partial instantiation itself, i.e.
10022 the innermost arguments are not the same as the innermost parms of
10023 the most general template.
10025 So it's only when 1/ and 2/ happens that we want to use the partial
10026 instantiation of the member template in lieu of its most general
10029 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10030 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10031 /* the enclosing class must be an instantiation... */
10032 && CLASS_TYPE_P (context
)
10033 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10035 TREE_VEC_LENGTH (arglist
)--;
10036 ++processing_template_decl
;
10037 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10038 tree partial_inst_args
=
10039 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10040 arglist
, complain
, NULL_TREE
);
10041 --processing_template_decl
;
10042 TREE_VEC_LENGTH (arglist
)++;
10043 if (partial_inst_args
== error_mark_node
)
10044 return error_mark_node
;
10045 use_partial_inst_tmpl
=
10046 /*...and we must not be looking at the partial instantiation
10048 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10049 partial_inst_args
);
10052 if (!use_partial_inst_tmpl
)
10053 /* This case is easy; there are no member templates involved. */
10057 /* This is a full instantiation of a member template. Find
10058 the partial instantiation of which this is an instance. */
10060 /* Temporarily reduce by one the number of levels in the ARGLIST
10061 so as to avoid comparing the last set of arguments. */
10062 TREE_VEC_LENGTH (arglist
)--;
10063 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10064 TREE_VEC_LENGTH (arglist
)++;
10065 /* FOUND is either a proper class type, or an alias
10066 template specialization. In the later case, it's a
10067 TYPE_DECL, resulting from the substituting of arguments
10068 for parameters in the TYPE_DECL of the alias template
10069 done earlier. So be careful while getting the template
10071 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10073 : (TREE_CODE (found
) == TYPE_DECL
10074 ? DECL_TI_TEMPLATE (found
)
10075 : CLASSTYPE_TI_TEMPLATE (found
)));
10077 if (DECL_CLASS_TEMPLATE_P (found
)
10078 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10080 /* If this partial instantiation is specialized, we want to
10081 use it for hash table lookup. */
10083 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10084 hash
= spec_hasher::hash (&elt
);
10088 // Build template info for the new specialization.
10089 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10092 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10093 gcc_checking_assert (*slot
== NULL
);
10094 entry
= ggc_alloc
<spec_entry
> ();
10098 /* Note this use of the partial instantiation so we can check it
10099 later in maybe_process_partial_specialization. */
10100 DECL_TEMPLATE_INSTANTIATIONS (found
)
10101 = tree_cons (arglist
, t
,
10102 DECL_TEMPLATE_INSTANTIATIONS (found
));
10104 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10105 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10106 /* Now that the type has been registered on the instantiations
10107 list, we set up the enumerators. Because the enumeration
10108 constants may involve the enumeration type itself, we make
10109 sure to register the type first, and then create the
10110 constants. That way, doing tsubst_expr for the enumeration
10111 constants won't result in recursive calls here; we'll find
10112 the instantiation and exit above. */
10113 tsubst_enum (template_type
, t
, arglist
);
10115 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10116 /* If the type makes use of template parameters, the
10117 code that generates debugging information will crash. */
10118 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10120 /* Possibly limit visibility based on template args. */
10121 TREE_PUBLIC (type_decl
) = 1;
10122 determine_visibility (type_decl
);
10124 inherit_targ_abi_tags (t
);
10130 /* Wrapper for lookup_template_class_1. */
10133 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10134 int entering_scope
, tsubst_flags_t complain
)
10137 timevar_push (TV_TEMPLATE_INST
);
10138 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10139 entering_scope
, complain
);
10140 timevar_pop (TV_TEMPLATE_INST
);
10144 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10147 lookup_template_variable (tree templ
, tree arglist
)
10149 if (flag_concepts
&& variable_concept_p (templ
))
10150 return build_concept_check (templ
, arglist
, tf_none
);
10152 /* The type of the expression is NULL_TREE since the template-id could refer
10153 to an explicit or partial specialization. */
10154 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10157 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10160 finish_template_variable (tree var
, tsubst_flags_t complain
)
10162 tree templ
= TREE_OPERAND (var
, 0);
10163 tree arglist
= TREE_OPERAND (var
, 1);
10165 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10166 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10168 templ
= most_general_template (templ
);
10169 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10170 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10172 /*use_default*/true);
10173 if (arglist
== error_mark_node
)
10174 return error_mark_node
;
10176 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10178 if (complain
& tf_error
)
10180 auto_diagnostic_group d
;
10181 error ("use of invalid variable template %qE", var
);
10182 diagnose_constraints (location_of (var
), templ
, arglist
);
10184 return error_mark_node
;
10187 return instantiate_template (templ
, arglist
, complain
);
10190 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10191 TARGS template args, and instantiate it if it's not dependent. */
10194 lookup_and_finish_template_variable (tree templ
, tree targs
,
10195 tsubst_flags_t complain
)
10197 templ
= lookup_template_variable (templ
, targs
);
10198 if (!any_dependent_template_arguments_p (targs
))
10200 templ
= finish_template_variable (templ
, complain
);
10204 return convert_from_reference (templ
);
10208 struct pair_fn_data
10213 /* True when we should also visit template parameters that occur in
10214 non-deduced contexts. */
10215 bool include_nondeduced_p
;
10216 hash_set
<tree
> *visited
;
10219 /* Called from for_each_template_parm via walk_tree. */
10222 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10225 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10226 tree_fn_t fn
= pfd
->fn
;
10227 void *data
= pfd
->data
;
10228 tree result
= NULL_TREE
;
10230 #define WALK_SUBTREE(NODE) \
10233 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10234 pfd->include_nondeduced_p, \
10236 if (result) goto out; \
10240 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10244 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10245 WALK_SUBTREE (TYPE_CONTEXT (t
));
10247 switch (TREE_CODE (t
))
10250 if (TYPE_PTRMEMFUNC_P (t
))
10252 /* Fall through. */
10255 case ENUMERAL_TYPE
:
10256 if (!TYPE_TEMPLATE_INFO (t
))
10257 *walk_subtrees
= 0;
10259 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10263 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10264 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10268 /* Since we're not going to walk subtrees, we have to do this
10269 explicitly here. */
10270 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10271 /* Fall through. */
10273 case FUNCTION_TYPE
:
10274 /* Check the return type. */
10275 WALK_SUBTREE (TREE_TYPE (t
));
10277 /* Check the parameter types. Since default arguments are not
10278 instantiated until they are needed, the TYPE_ARG_TYPES may
10279 contain expressions that involve template parameters. But,
10280 no-one should be looking at them yet. And, once they're
10281 instantiated, they don't contain template parameters, so
10282 there's no point in looking at them then, either. */
10286 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10287 WALK_SUBTREE (TREE_VALUE (parm
));
10289 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10290 want walk_tree walking into them itself. */
10291 *walk_subtrees
= 0;
10294 if (flag_noexcept_type
)
10296 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10298 WALK_SUBTREE (TREE_PURPOSE (spec
));
10303 case DECLTYPE_TYPE
:
10304 case UNDERLYING_TYPE
:
10305 if (pfd
->include_nondeduced_p
10306 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10308 pfd
->include_nondeduced_p
,
10310 return error_mark_node
;
10311 *walk_subtrees
= false;
10314 case FUNCTION_DECL
:
10316 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10317 WALK_SUBTREE (DECL_TI_ARGS (t
));
10318 /* Fall through. */
10322 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10323 WALK_SUBTREE (DECL_INITIAL (t
));
10324 if (DECL_CONTEXT (t
)
10325 && pfd
->include_nondeduced_p
)
10326 WALK_SUBTREE (DECL_CONTEXT (t
));
10329 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10330 /* Record template parameters such as `T' inside `TT<T>'. */
10331 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10332 /* Fall through. */
10334 case TEMPLATE_TEMPLATE_PARM
:
10335 case TEMPLATE_TYPE_PARM
:
10336 case TEMPLATE_PARM_INDEX
:
10337 if (fn
&& (*fn
)(t
, data
))
10343 case TEMPLATE_DECL
:
10344 /* A template template parameter is encountered. */
10345 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10346 WALK_SUBTREE (TREE_TYPE (t
));
10348 /* Already substituted template template parameter */
10349 *walk_subtrees
= 0;
10352 case TYPENAME_TYPE
:
10353 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10354 partial instantiation. */
10355 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10356 *walk_subtrees
= 0;
10360 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10361 && pfd
->include_nondeduced_p
)
10362 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10366 case COMPONENT_REF
:
10367 /* If there's no type, then this thing must be some expression
10368 involving template parameters. */
10369 if (!fn
&& !TREE_TYPE (t
))
10370 return error_mark_node
;
10375 case IMPLICIT_CONV_EXPR
:
10376 case REINTERPRET_CAST_EXPR
:
10377 case CONST_CAST_EXPR
:
10378 case STATIC_CAST_EXPR
:
10379 case DYNAMIC_CAST_EXPR
:
10383 case PSEUDO_DTOR_EXPR
:
10385 return error_mark_node
;
10389 if (pfd
->include_nondeduced_p
)
10390 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10393 case REQUIRES_EXPR
:
10396 return error_mark_node
;
10398 /* Recursively walk the type of each constraint variable. */
10399 tree p
= TREE_OPERAND (t
, 0);
10402 WALK_SUBTREE (TREE_TYPE (p
));
10403 p
= TREE_CHAIN (p
);
10412 #undef WALK_SUBTREE
10414 /* We didn't find any template parameters we liked. */
10419 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10420 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10421 call FN with the parameter and the DATA.
10422 If FN returns nonzero, the iteration is terminated, and
10423 for_each_template_parm returns 1. Otherwise, the iteration
10424 continues. If FN never returns a nonzero value, the value
10425 returned by for_each_template_parm is 0. If FN is NULL, it is
10426 considered to be the function which always returns 1.
10428 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10429 parameters that occur in non-deduced contexts. When false, only
10430 visits those template parameters that can be deduced. */
10433 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10434 hash_set
<tree
> *visited
,
10435 bool include_nondeduced_p
,
10438 struct pair_fn_data pfd
;
10443 pfd
.any_fn
= any_fn
;
10445 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10447 /* Walk the tree. (Conceptually, we would like to walk without
10448 duplicates, but for_each_template_parm_r recursively calls
10449 for_each_template_parm, so we would need to reorganize a fair
10450 bit to use walk_tree_without_duplicates, so we keep our own
10453 pfd
.visited
= visited
;
10455 pfd
.visited
= new hash_set
<tree
>;
10456 result
= cp_walk_tree (&t
,
10457 for_each_template_parm_r
,
10464 delete pfd
.visited
;
10471 struct find_template_parameter_info
10473 explicit find_template_parameter_info (tree ctx_parms
)
10474 : parm_list (NULL_TREE
),
10475 ctx_parms (ctx_parms
),
10476 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10479 hash_set
<tree
> visited
;
10480 hash_set
<tree
> parms
;
10486 /* Appends the declaration of T to the list in DATA. */
10489 keep_template_parm (tree t
, void* data
)
10491 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10493 /* Template parameters declared within the expression are not part of
10494 the parameter mapping. For example, in this concept:
10496 template<typename T>
10497 concept C = requires { <expr> } -> same_as<int>;
10499 the return specifier same_as<int> declares a new decltype parameter
10500 that must not be part of the parameter mapping. The same is true
10501 for generic lambda parameters, lambda template parameters, etc. */
10504 template_parm_level_and_index (t
, &level
, &index
);
10505 if (level
> ftpi
->max_depth
)
10508 /* Arguments like const T yield parameters like const T. This means that
10509 a template-id like X<T, const T> would yield two distinct parameters:
10510 T and const T. Adjust types to their unqualified versions. */
10512 t
= TYPE_MAIN_VARIANT (t
);
10513 if (!ftpi
->parms
.add (t
))
10514 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10519 /* Ensure that we recursively examine certain terms that are not normally
10520 visited in for_each_template_parm_r. */
10523 any_template_parm_r (tree t
, void *data
)
10525 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10527 #define WALK_SUBTREE(NODE) \
10530 for_each_template_parm (NODE, keep_template_parm, data, \
10531 &ftpi->visited, true, \
10532 any_template_parm_r); \
10536 /* A mention of a member alias/typedef is a use of all of its template
10537 arguments, including those from the enclosing class, so we don't use
10538 alias_template_specialization_p here. */
10539 if (TYPE_P (t
) && typedef_variant_p (t
))
10540 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10541 WALK_SUBTREE (TI_ARGS (tinfo
));
10543 switch (TREE_CODE (t
))
10545 case TEMPLATE_TYPE_PARM
:
10546 /* Type constraints of a placeholder type may contain parameters. */
10548 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10549 WALK_SUBTREE (constr
);
10552 case TEMPLATE_ID_EXPR
:
10553 /* Search through references to variable templates. */
10554 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10555 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10558 case TEMPLATE_PARM_INDEX
:
10560 /* A parameter or constraint variable may also depend on a template
10561 parameter without explicitly naming it. */
10562 WALK_SUBTREE (TREE_TYPE (t
));
10565 case TEMPLATE_DECL
:
10567 /* If T is a member template that shares template parameters with
10568 ctx_parms, we need to mark all those parameters for mapping. */
10569 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10570 tree cparms
= ftpi
->ctx_parms
;
10571 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10572 dparms
= TREE_CHAIN (dparms
);
10573 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10574 cparms
= TREE_CHAIN (cparms
);
10576 && (TREE_TYPE (TREE_VALUE (dparms
))
10577 != TREE_TYPE (TREE_VALUE (cparms
))))
10578 dparms
= TREE_CHAIN (dparms
),
10579 cparms
= TREE_CHAIN (cparms
);
10582 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10583 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10584 for (int i
= 0; i
< ddepth
; ++i
)
10585 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10592 /* Look in the parms and body. */
10593 tree fn
= lambda_function (t
);
10594 WALK_SUBTREE (TREE_TYPE (fn
));
10595 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10599 case IDENTIFIER_NODE
:
10600 if (IDENTIFIER_CONV_OP_P (t
))
10601 /* The conversion-type-id of a conversion operator may be dependent. */
10602 WALK_SUBTREE (TREE_TYPE (t
));
10609 /* Keep walking. */
10613 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10614 are the template parameters in scope. */
10617 find_template_parameters (tree t
, tree ctx_parms
)
10622 find_template_parameter_info
ftpi (ctx_parms
);
10623 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10624 /*include_nondeduced*/true, any_template_parm_r
);
10625 return ftpi
.parm_list
;
10628 /* Returns true if T depends on any template parameter. */
10631 uses_template_parms (tree t
)
10633 if (t
== NULL_TREE
)
10637 int saved_processing_template_decl
;
10639 saved_processing_template_decl
= processing_template_decl
;
10640 if (!saved_processing_template_decl
)
10641 processing_template_decl
= 1;
10643 dependent_p
= dependent_type_p (t
);
10644 else if (TREE_CODE (t
) == TREE_VEC
)
10645 dependent_p
= any_dependent_template_arguments_p (t
);
10646 else if (TREE_CODE (t
) == TREE_LIST
)
10647 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10648 || uses_template_parms (TREE_CHAIN (t
)));
10649 else if (TREE_CODE (t
) == TYPE_DECL
)
10650 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10651 else if (t
== error_mark_node
)
10652 dependent_p
= false;
10654 dependent_p
= value_dependent_expression_p (t
);
10656 processing_template_decl
= saved_processing_template_decl
;
10658 return dependent_p
;
10661 /* Returns true iff current_function_decl is an incompletely instantiated
10662 template. Useful instead of processing_template_decl because the latter
10663 is set to 0 during instantiate_non_dependent_expr. */
10666 in_template_function (void)
10668 tree fn
= current_function_decl
;
10670 ++processing_template_decl
;
10671 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10672 && DECL_TEMPLATE_INFO (fn
)
10673 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10674 --processing_template_decl
;
10678 /* Returns true if T depends on any template parameter with level LEVEL. */
10681 uses_template_parms_level (tree t
, int level
)
10683 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10684 /*include_nondeduced_p=*/true);
10687 /* Returns true if the signature of DECL depends on any template parameter from
10688 its enclosing class. */
10691 uses_outer_template_parms (tree decl
)
10693 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10696 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10697 &depth
, NULL
, /*include_nondeduced_p=*/true))
10699 if (PRIMARY_TEMPLATE_P (decl
)
10700 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10701 (DECL_TEMPLATE_PARMS (decl
)),
10702 template_parm_outer_level
,
10703 &depth
, NULL
, /*include_nondeduced_p=*/true))
10705 tree ci
= get_constraints (decl
);
10707 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10708 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10709 &depth
, NULL
, /*nondeduced*/true))
10714 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10715 ill-formed translation unit, i.e. a variable or function that isn't
10716 usable in a constant expression. */
10719 neglectable_inst_p (tree d
)
10721 return (d
&& DECL_P (d
)
10722 && !undeduced_auto_decl (d
)
10723 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10724 : decl_maybe_constant_var_p (d
)));
10727 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10728 neglectable and instantiated from within an erroneous instantiation. */
10731 limit_bad_template_recursion (tree decl
)
10733 struct tinst_level
*lev
= current_tinst_level
;
10734 int errs
= errorcount
+ sorrycount
;
10735 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10738 for (; lev
; lev
= lev
->next
)
10739 if (neglectable_inst_p (lev
->maybe_get_node ()))
10742 return (lev
&& errs
> lev
->errors
);
10745 static int tinst_depth
;
10746 extern int max_tinst_depth
;
10749 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10751 /* We're starting to instantiate D; record the template instantiation context
10752 at LOC for diagnostics and to restore it later. */
10755 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10757 struct tinst_level
*new_level
;
10759 if (tinst_depth
>= max_tinst_depth
)
10761 /* Tell error.c not to try to instantiate any templates. */
10763 fatal_error (input_location
,
10764 "template instantiation depth exceeds maximum of %d"
10765 " (use %<-ftemplate-depth=%> to increase the maximum)",
10770 /* If the current instantiation caused problems, don't let it instantiate
10771 anything else. Do allow deduction substitution and decls usable in
10772 constant expressions. */
10773 if (!targs
&& limit_bad_template_recursion (tldcl
))
10775 /* Avoid no_linkage_errors and unused function warnings for this
10777 TREE_NO_WARNING (tldcl
) = 1;
10781 /* When not -quiet, dump template instantiations other than functions, since
10782 announce_function will take care of those. */
10783 if (!quiet_flag
&& !targs
10784 && TREE_CODE (tldcl
) != TREE_LIST
10785 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10786 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10788 new_level
= tinst_level_freelist ().alloc ();
10789 new_level
->tldcl
= tldcl
;
10790 new_level
->targs
= targs
;
10791 new_level
->locus
= loc
;
10792 new_level
->errors
= errorcount
+ sorrycount
;
10793 new_level
->next
= NULL
;
10794 new_level
->refcount
= 0;
10795 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10796 set_refcount_ptr (current_tinst_level
, new_level
);
10799 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10800 depth_reached
= tinst_depth
;
10805 /* We're starting substitution of TMPL<ARGS>; record the template
10806 substitution context for diagnostics and to restore it later. */
10809 push_tinst_level (tree tmpl
, tree args
)
10811 return push_tinst_level_loc (tmpl
, args
, input_location
);
10814 /* We're starting to instantiate D; record INPUT_LOCATION and the
10815 template instantiation context for diagnostics and to restore it
10819 push_tinst_level (tree d
)
10821 return push_tinst_level_loc (d
, input_location
);
10824 /* Likewise, but record LOC as the program location. */
10827 push_tinst_level_loc (tree d
, location_t loc
)
10829 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10830 return push_tinst_level_loc (d
, NULL
, loc
);
10833 /* We're done instantiating this template; return to the instantiation
10837 pop_tinst_level (void)
10839 /* Restore the filename and line number stashed away when we started
10840 this instantiation. */
10841 input_location
= current_tinst_level
->locus
;
10842 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10846 /* We're instantiating a deferred template; restore the template
10847 instantiation context in which the instantiation was requested, which
10848 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10851 reopen_tinst_level (struct tinst_level
*level
)
10853 struct tinst_level
*t
;
10856 for (t
= level
; t
; t
= t
->next
)
10859 set_refcount_ptr (current_tinst_level
, level
);
10860 pop_tinst_level ();
10861 if (current_tinst_level
)
10862 current_tinst_level
->errors
= errorcount
+sorrycount
;
10863 return level
->maybe_get_node ();
10866 /* Returns the TINST_LEVEL which gives the original instantiation
10869 struct tinst_level
*
10870 outermost_tinst_level (void)
10872 struct tinst_level
*level
= current_tinst_level
;
10874 while (level
->next
)
10875 level
= level
->next
;
10879 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10880 vector of template arguments, as for tsubst.
10882 Returns an appropriate tsubst'd friend declaration. */
10885 tsubst_friend_function (tree decl
, tree args
)
10889 if (TREE_CODE (decl
) == FUNCTION_DECL
10890 && DECL_TEMPLATE_INSTANTIATION (decl
)
10891 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10892 /* This was a friend declared with an explicit template
10893 argument list, e.g.:
10895 friend void f<>(T);
10897 to indicate that f was a template instantiation, not a new
10898 function declaration. Now, we have to figure out what
10899 instantiation of what template. */
10901 tree template_id
, arglist
, fns
;
10904 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10906 /* Friend functions are looked up in the containing namespace scope.
10907 We must enter that scope, to avoid finding member functions of the
10908 current class with same name. */
10909 push_nested_namespace (ns
);
10910 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10911 tf_warning_or_error
, NULL_TREE
,
10912 /*integral_constant_expression_p=*/false);
10913 pop_nested_namespace (ns
);
10914 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10915 tf_warning_or_error
, NULL_TREE
);
10916 template_id
= lookup_template_function (fns
, arglist
);
10918 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10919 tmpl
= determine_specialization (template_id
, new_friend
,
10921 /*need_member_template=*/0,
10922 TREE_VEC_LENGTH (args
),
10924 return instantiate_template (tmpl
, new_args
, tf_error
);
10927 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10928 if (new_friend
== error_mark_node
)
10929 return error_mark_node
;
10931 /* The NEW_FRIEND will look like an instantiation, to the
10932 compiler, but is not an instantiation from the point of view of
10933 the language. For example, we might have had:
10935 template <class T> struct S {
10936 template <class U> friend void f(T, U);
10939 Then, in S<int>, template <class U> void f(int, U) is not an
10940 instantiation of anything. */
10942 DECL_USE_TEMPLATE (new_friend
) = 0;
10943 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10945 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10946 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10947 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10949 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10950 match in decls_match. */
10951 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10952 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
10953 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
10954 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10957 /* The mangled name for the NEW_FRIEND is incorrect. The function
10958 is not a template instantiation and should not be mangled like
10959 one. Therefore, we forget the mangling here; we'll recompute it
10960 later if we need it. */
10961 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10963 SET_DECL_RTL (new_friend
, NULL
);
10964 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10967 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10972 /* We must save some information from NEW_FRIEND before calling
10973 duplicate decls since that function will free NEW_FRIEND if
10975 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10976 tree new_friend_result_template_info
= NULL_TREE
;
10977 bool new_friend_is_defn
=
10978 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10979 (template_for_substitution (new_friend
)))
10981 tree not_tmpl
= new_friend
;
10983 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10985 /* This declaration is a `primary' template. */
10986 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10988 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
10989 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
10992 /* Inside pushdecl_namespace_level, we will push into the
10993 current namespace. However, the friend function should go
10994 into the namespace of the template. */
10995 ns
= decl_namespace_context (new_friend
);
10996 push_nested_namespace (ns
);
10997 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10998 pop_nested_namespace (ns
);
11000 if (old_decl
== error_mark_node
)
11001 return error_mark_node
;
11003 if (old_decl
!= new_friend
)
11005 /* This new friend declaration matched an existing
11006 declaration. For example, given:
11008 template <class T> void f(T);
11009 template <class U> class C {
11010 template <class T> friend void f(T) {}
11013 the friend declaration actually provides the definition
11014 of `f', once C has been instantiated for some type. So,
11015 old_decl will be the out-of-class template declaration,
11016 while new_friend is the in-class definition.
11018 But, if `f' was called before this point, the
11019 instantiation of `f' will have DECL_TI_ARGS corresponding
11020 to `T' but not to `U', references to which might appear
11021 in the definition of `f'. Previously, the most general
11022 template for an instantiation of `f' was the out-of-class
11023 version; now it is the in-class version. Therefore, we
11024 run through all specialization of `f', adding to their
11025 DECL_TI_ARGS appropriately. In particular, they need a
11026 new set of outer arguments, corresponding to the
11027 arguments for this class instantiation.
11029 The same situation can arise with something like this:
11031 friend void f(int);
11032 template <class T> class C {
11033 friend void f(T) {}
11036 when `C<int>' is instantiated. Now, `f(int)' is defined
11039 if (!new_friend_is_defn
)
11040 /* On the other hand, if the in-class declaration does
11041 *not* provide a definition, then we don't want to alter
11042 existing definitions. We can just leave everything
11047 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11048 tree new_args
= TI_ARGS (new_friend_template_info
);
11050 /* Overwrite whatever template info was there before, if
11051 any, with the new template information pertaining to
11052 the declaration. */
11053 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11055 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11057 /* We should have called reregister_specialization in
11058 duplicate_decls. */
11059 gcc_assert (retrieve_specialization (new_template
,
11063 /* Instantiate it if the global has already been used. */
11064 if (DECL_ODR_USED (old_decl
))
11065 instantiate_decl (old_decl
, /*defer_ok=*/true,
11066 /*expl_inst_class_mem_p=*/false);
11072 /* Indicate that the old function template is a partial
11074 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11075 = new_friend_result_template_info
;
11077 gcc_assert (new_template
11078 == most_general_template (new_template
));
11079 gcc_assert (new_template
!= old_decl
);
11081 /* Reassign any specializations already in the hash table
11082 to the new more general template, and add the
11083 additional template args. */
11084 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11086 t
= TREE_CHAIN (t
))
11088 tree spec
= TREE_VALUE (t
);
11091 elt
.tmpl
= old_decl
;
11092 elt
.args
= DECL_TI_ARGS (spec
);
11093 elt
.spec
= NULL_TREE
;
11095 decl_specializations
->remove_elt (&elt
);
11097 DECL_TI_ARGS (spec
)
11098 = add_outermost_template_args (new_args
,
11099 DECL_TI_ARGS (spec
));
11101 register_specialization
11102 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11105 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11109 /* The information from NEW_FRIEND has been merged into OLD_DECL
11110 by duplicate_decls. */
11111 new_friend
= old_decl
;
11116 tree context
= DECL_CONTEXT (new_friend
);
11120 template <class T> class C {
11121 template <class U> friend void C1<U>::f (); // case 1
11122 friend void C2<T>::f (); // case 2
11124 we only need to make sure CONTEXT is a complete type for
11125 case 2. To distinguish between the two cases, we note that
11126 CONTEXT of case 1 remains dependent type after tsubst while
11127 this isn't true for case 2. */
11128 ++processing_template_decl
;
11129 dependent_p
= dependent_type_p (context
);
11130 --processing_template_decl
;
11133 && !complete_type_or_else (context
, NULL_TREE
))
11134 return error_mark_node
;
11136 if (COMPLETE_TYPE_P (context
))
11138 tree fn
= new_friend
;
11139 /* do_friend adds the TEMPLATE_DECL for any member friend
11140 template even if it isn't a member template, i.e.
11141 template <class T> friend A<T>::f();
11142 Look through it in that case. */
11143 if (TREE_CODE (fn
) == TEMPLATE_DECL
11144 && !PRIMARY_TEMPLATE_P (fn
))
11145 fn
= DECL_TEMPLATE_RESULT (fn
);
11146 /* Check to see that the declaration is really present, and,
11147 possibly obtain an improved declaration. */
11148 fn
= check_classfn (context
, fn
, NULL_TREE
);
11158 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11159 template arguments, as for tsubst.
11161 Returns an appropriate tsubst'd friend type or error_mark_node on
11165 tsubst_friend_class (tree friend_tmpl
, tree args
)
11169 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11171 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11172 return TREE_TYPE (tmpl
);
11175 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11176 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11177 push_nested_namespace (context
);
11180 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11181 push_nested_class (context
);
11184 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11185 /*non_class=*/false, /*block_p=*/false,
11186 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11188 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11190 /* The friend template has already been declared. Just
11191 check to see that the declarations match, and install any new
11192 default parameters. We must tsubst the default parameters,
11193 of course. We only need the innermost template parameters
11194 because that is all that redeclare_class_template will look
11196 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11197 > TMPL_ARGS_DEPTH (args
))
11199 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11200 args
, tf_warning_or_error
);
11201 location_t saved_input_location
= input_location
;
11202 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11203 tree cons
= get_constraints (tmpl
);
11204 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11205 input_location
= saved_input_location
;
11210 /* The friend template has not already been declared. In this
11211 case, the instantiation of the template class will cause the
11212 injection of this template into the namespace scope. */
11213 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11215 if (tmpl
!= error_mark_node
)
11217 /* The new TMPL is not an instantiation of anything, so we
11218 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11219 for the new type because that is supposed to be the
11220 corresponding template decl, i.e., TMPL. */
11221 DECL_USE_TEMPLATE (tmpl
) = 0;
11222 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11223 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11224 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11225 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11227 /* It is hidden. */
11228 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11229 DECL_ANTICIPATED (tmpl
)
11230 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11232 /* Substitute into and set the constraints on the new declaration. */
11233 if (tree ci
= get_constraints (friend_tmpl
))
11235 ++processing_template_decl
;
11236 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11237 DECL_FRIEND_CONTEXT (friend_tmpl
));
11238 --processing_template_decl
;
11239 set_constraints (tmpl
, ci
);
11242 /* Inject this template into the enclosing namspace scope. */
11243 tmpl
= pushdecl_namespace_level (tmpl
, true);
11247 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11248 pop_nested_namespace (context
);
11250 pop_nested_class ();
11252 return TREE_TYPE (tmpl
);
11255 /* Returns zero if TYPE cannot be completed later due to circularity.
11256 Otherwise returns one. */
11259 can_complete_type_without_circularity (tree type
)
11261 if (type
== NULL_TREE
|| type
== error_mark_node
)
11263 else if (COMPLETE_TYPE_P (type
))
11265 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11266 return can_complete_type_without_circularity (TREE_TYPE (type
));
11267 else if (CLASS_TYPE_P (type
)
11268 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11274 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11275 tsubst_flags_t
, tree
);
11277 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11278 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11281 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11282 tsubst_flags_t complain
, tree in_decl
)
11284 gcc_assert (ATTR_IS_DEPENDENT (t
));
11286 tree val
= TREE_VALUE (t
);
11287 if (val
== NULL_TREE
)
11288 /* Nothing to do. */;
11289 else if ((flag_openmp
|| flag_openmp_simd
)
11290 && is_attribute_p ("omp declare simd",
11291 get_attribute_name (t
)))
11293 tree clauses
= TREE_VALUE (val
);
11294 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11295 complain
, in_decl
);
11296 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11297 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11298 tree parms
= DECL_ARGUMENTS (*decl_p
);
11300 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11302 val
= build_tree_list (NULL_TREE
, clauses
);
11306 else if (flag_openmp
11307 && is_attribute_p ("omp declare variant base",
11308 get_attribute_name (t
)))
11310 ++cp_unevaluated_operand
;
11312 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11313 in_decl
, /*integral_constant_expression_p=*/false);
11314 --cp_unevaluated_operand
;
11315 tree chain
= TREE_CHAIN (val
);
11316 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11317 tree ctx
= copy_list (TREE_VALUE (val
));
11318 tree simd
= get_identifier ("simd");
11319 tree score
= get_identifier (" score");
11320 tree condition
= get_identifier ("condition");
11321 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11323 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11324 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11325 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11327 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11329 tree clauses
= TREE_VALUE (t2
);
11330 clauses
= tsubst_omp_clauses (clauses
,
11331 C_ORT_OMP_DECLARE_SIMD
, args
,
11332 complain
, in_decl
);
11333 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11334 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11335 TREE_VALUE (t2
) = clauses
;
11339 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11340 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11341 if (TREE_VALUE (t3
))
11344 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11345 && TREE_PURPOSE (t3
) != score
);
11346 tree v
= TREE_VALUE (t3
);
11347 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11349 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11350 v
= fold_non_dependent_expr (v
);
11351 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11352 || (TREE_PURPOSE (t3
) == score
11353 ? TREE_CODE (v
) != INTEGER_CST
11354 : !tree_fits_shwi_p (v
)))
11357 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11359 if (TREE_PURPOSE (t3
) == score
)
11360 error_at (loc
, "score argument must be "
11361 "constant integer expression");
11362 else if (allow_string
)
11363 error_at (loc
, "property must be constant "
11364 "integer expression or string "
11367 error_at (loc
, "property must be constant "
11368 "integer expression");
11371 else if (TREE_PURPOSE (t3
) == score
11372 && tree_int_cst_sgn (v
) < 0)
11375 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11377 error_at (loc
, "score argument must be "
11381 TREE_VALUE (t3
) = v
;
11386 val
= tree_cons (varid
, ctx
, chain
);
11388 /* If the first attribute argument is an identifier, don't
11389 pass it through tsubst. Attributes like mode, format,
11390 cleanup and several target specific attributes expect it
11392 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11395 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11396 /*integral_constant_expression_p=*/false);
11397 if (chain
!= TREE_CHAIN (val
))
11398 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11400 else if (PACK_EXPANSION_P (val
))
11402 /* An attribute pack expansion. */
11403 tree purp
= TREE_PURPOSE (t
);
11404 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11405 if (pack
== error_mark_node
)
11406 return error_mark_node
;
11407 int len
= TREE_VEC_LENGTH (pack
);
11408 tree list
= NULL_TREE
;
11410 for (int i
= 0; i
< len
; ++i
)
11412 tree elt
= TREE_VEC_ELT (pack
, i
);
11413 *q
= build_tree_list (purp
, elt
);
11414 q
= &TREE_CHAIN (*q
);
11419 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11420 /*integral_constant_expression_p=*/false);
11422 if (val
!= TREE_VALUE (t
))
11423 return build_tree_list (TREE_PURPOSE (t
), val
);
11427 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11428 unchanged or a new TREE_LIST chain. */
11431 tsubst_attributes (tree attributes
, tree args
,
11432 tsubst_flags_t complain
, tree in_decl
)
11434 tree last_dep
= NULL_TREE
;
11436 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11437 if (ATTR_IS_DEPENDENT (t
))
11440 attributes
= copy_list (attributes
);
11445 for (tree
*p
= &attributes
; *p
; )
11448 if (ATTR_IS_DEPENDENT (t
))
11450 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11455 p
= &TREE_CHAIN (*p
);
11456 *p
= TREE_CHAIN (t
);
11460 p
= &TREE_CHAIN (*p
);
11466 /* Apply any attributes which had to be deferred until instantiation
11467 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11468 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11471 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11472 tree args
, tsubst_flags_t complain
, tree in_decl
)
11474 tree last_dep
= NULL_TREE
;
11478 if (attributes
== NULL_TREE
)
11481 if (DECL_P (*decl_p
))
11483 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11485 p
= &DECL_ATTRIBUTES (*decl_p
);
11486 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11487 to our attributes parameter. */
11488 gcc_assert (*p
== attributes
);
11492 p
= &TYPE_ATTRIBUTES (*decl_p
);
11493 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11494 lookup_template_class_1, and should be preserved. */
11495 gcc_assert (*p
!= attributes
);
11497 p
= &TREE_CHAIN (*p
);
11500 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11501 if (ATTR_IS_DEPENDENT (t
))
11504 attributes
= copy_list (attributes
);
11511 tree late_attrs
= NULL_TREE
;
11512 tree
*q
= &late_attrs
;
11517 if (ATTR_IS_DEPENDENT (t
))
11519 *p
= TREE_CHAIN (t
);
11520 TREE_CHAIN (t
) = NULL_TREE
;
11521 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11523 q
= &TREE_CHAIN (*q
);
11526 p
= &TREE_CHAIN (t
);
11529 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11533 /* The template TMPL is being instantiated with the template arguments TARGS.
11534 Perform the access checks that we deferred when parsing the template. */
11537 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11540 deferred_access_check
*chk
;
11542 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11545 if (vec
<deferred_access_check
, va_gc
> *access_checks
11546 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11547 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11549 tree decl
= chk
->decl
;
11550 tree diag_decl
= chk
->diag_decl
;
11551 tree type_scope
= TREE_TYPE (chk
->binfo
);
11553 if (uses_template_parms (type_scope
))
11554 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11556 /* Make access check error messages point to the location
11557 of the use of the typedef. */
11558 iloc_sentinel
ils (chk
->loc
);
11559 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11560 decl
, diag_decl
, tf_warning_or_error
);
11565 instantiate_class_template_1 (tree type
)
11567 tree templ
, args
, pattern
, t
, member
;
11571 unsigned int saved_maximum_field_alignment
;
11574 if (type
== error_mark_node
)
11575 return error_mark_node
;
11577 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11578 || uses_template_parms (type
))
11581 /* Figure out which template is being instantiated. */
11582 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11583 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11585 /* Mark the type as in the process of being defined. */
11586 TYPE_BEING_DEFINED (type
) = 1;
11588 /* We may be in the middle of deferred access check. Disable
11590 deferring_access_check_sentinel
acs (dk_no_deferred
);
11592 /* Determine what specialization of the original template to
11594 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11595 if (t
== error_mark_node
)
11596 return error_mark_node
;
11599 /* This TYPE is actually an instantiation of a partial
11600 specialization. We replace the innermost set of ARGS with
11601 the arguments appropriate for substitution. For example,
11604 template <class T> struct S {};
11605 template <class T> struct S<T*> {};
11607 and supposing that we are instantiating S<int*>, ARGS will
11608 presently be {int*} -- but we need {int}. */
11609 pattern
= TREE_TYPE (t
);
11610 args
= TREE_PURPOSE (t
);
11614 pattern
= TREE_TYPE (templ
);
11615 args
= CLASSTYPE_TI_ARGS (type
);
11618 /* If the template we're instantiating is incomplete, then clearly
11619 there's nothing we can do. */
11620 if (!COMPLETE_TYPE_P (pattern
))
11622 /* We can try again later. */
11623 TYPE_BEING_DEFINED (type
) = 0;
11627 /* If we've recursively instantiated too many templates, stop. */
11628 if (! push_tinst_level (type
))
11631 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11632 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11634 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11635 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11636 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11637 fn_context
= error_mark_node
;
11639 push_to_top_level ();
11642 cp_unevaluated_operand
= 0;
11643 c_inhibit_evaluation_warnings
= 0;
11645 /* Use #pragma pack from the template context. */
11646 saved_maximum_field_alignment
= maximum_field_alignment
;
11647 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11649 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11651 /* Set the input location to the most specialized template definition.
11652 This is needed if tsubsting causes an error. */
11653 typedecl
= TYPE_MAIN_DECL (pattern
);
11654 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11655 DECL_SOURCE_LOCATION (typedecl
);
11657 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11658 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11659 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11660 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11661 if (ANON_AGGR_TYPE_P (pattern
))
11662 SET_ANON_AGGR_TYPE_P (type
);
11663 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11665 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11666 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11667 /* Adjust visibility for template arguments. */
11668 determine_visibility (TYPE_MAIN_DECL (type
));
11670 if (CLASS_TYPE_P (type
))
11671 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11673 pbinfo
= TYPE_BINFO (pattern
);
11675 /* We should never instantiate a nested class before its enclosing
11676 class; we need to look up the nested class by name before we can
11677 instantiate it, and that lookup should instantiate the enclosing
11679 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11680 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11682 base_list
= NULL_TREE
;
11683 if (BINFO_N_BASE_BINFOS (pbinfo
))
11689 /* We must enter the scope containing the type, as that is where
11690 the accessibility of types named in dependent bases are
11692 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11694 /* Substitute into each of the bases to determine the actual
11696 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11699 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11700 tree expanded_bases
= NULL_TREE
;
11703 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11706 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11707 args
, tf_error
, NULL_TREE
);
11708 if (expanded_bases
== error_mark_node
)
11711 len
= TREE_VEC_LENGTH (expanded_bases
);
11714 for (idx
= 0; idx
< len
; idx
++)
11716 if (expanded_bases
)
11717 /* Extract the already-expanded base class. */
11718 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11720 /* Substitute to figure out the base class. */
11721 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11724 if (base
== error_mark_node
)
11727 base_list
= tree_cons (access
, base
, base_list
);
11728 if (BINFO_VIRTUAL_P (pbase_binfo
))
11729 TREE_TYPE (base_list
) = integer_type_node
;
11733 /* The list is now in reverse order; correct that. */
11734 base_list
= nreverse (base_list
);
11737 pop_scope (pushed_scope
);
11739 /* Now call xref_basetypes to set up all the base-class
11741 xref_basetypes (type
, base_list
);
11743 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11744 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11745 args
, tf_error
, NULL_TREE
);
11746 fixup_attribute_variants (type
);
11748 /* Now that our base classes are set up, enter the scope of the
11749 class, so that name lookups into base classes, etc. will work
11750 correctly. This is precisely analogous to what we do in
11751 begin_class_definition when defining an ordinary non-template
11752 class, except we also need to push the enclosing classes. */
11753 push_nested_class (type
);
11755 /* Now members are processed in the order of declaration. */
11756 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11757 member
; member
= TREE_CHAIN (member
))
11759 tree t
= TREE_VALUE (member
);
11761 if (TREE_PURPOSE (member
))
11765 if (LAMBDA_TYPE_P (t
))
11766 /* A closure type for a lambda in an NSDMI or default argument.
11767 Ignore it; it will be regenerated when needed. */
11770 /* Build new CLASSTYPE_NESTED_UTDS. */
11771 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11772 && TYPE_LANG_SPECIFIC (t
)
11773 && CLASSTYPE_IS_TEMPLATE (t
));
11775 /* If the member is a class template, then -- even after
11776 substitution -- there may be dependent types in the
11777 template argument list for the class. We increment
11778 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11779 that function will assume that no types are dependent
11780 when outside of a template. */
11781 if (class_template_p
)
11782 ++processing_template_decl
;
11783 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11784 if (class_template_p
)
11785 --processing_template_decl
;
11786 if (newtag
== error_mark_node
)
11789 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11791 tree name
= TYPE_IDENTIFIER (t
);
11793 if (class_template_p
)
11794 /* Unfortunately, lookup_template_class sets
11795 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11796 instantiation (i.e., for the type of a member
11797 template class nested within a template class.)
11798 This behavior is required for
11799 maybe_process_partial_specialization to work
11800 correctly, but is not accurate in this case;
11801 the TAG is not an instantiation of anything.
11802 (The corresponding TEMPLATE_DECL is an
11803 instantiation, but the TYPE is not.) */
11804 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11806 /* Now, we call pushtag to put this NEWTAG into the scope of
11807 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11808 pushtag calling push_template_decl. We don't have to do
11809 this for enums because it will already have been done in
11812 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11813 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11816 else if (DECL_DECLARES_FUNCTION_P (t
))
11820 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11821 ++processing_template_decl
;
11822 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11823 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11824 --processing_template_decl
;
11825 set_current_access_from_decl (r
);
11826 finish_member_declaration (r
);
11827 /* Instantiate members marked with attribute used. */
11828 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11830 if (TREE_CODE (r
) == FUNCTION_DECL
11831 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11832 cp_check_omp_declare_reduction (r
);
11834 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11835 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11836 /* A closure type for a lambda in an NSDMI or default argument.
11837 Ignore it; it will be regenerated when needed. */;
11840 /* Build new TYPE_FIELDS. */
11841 if (TREE_CODE (t
) == STATIC_ASSERT
)
11842 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11843 /*integral_constant_expression_p=*/true);
11844 else if (TREE_CODE (t
) != CONST_DECL
)
11847 tree vec
= NULL_TREE
;
11850 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11851 /* The file and line for this declaration, to
11852 assist in error message reporting. Since we
11853 called push_tinst_level above, we don't need to
11855 input_location
= DECL_SOURCE_LOCATION (t
);
11857 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11858 ++processing_template_decl
;
11859 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11860 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11861 --processing_template_decl
;
11863 if (TREE_CODE (r
) == TREE_VEC
)
11865 /* A capture pack became multiple fields. */
11867 len
= TREE_VEC_LENGTH (vec
);
11870 for (int i
= 0; i
< len
; ++i
)
11873 r
= TREE_VEC_ELT (vec
, i
);
11878 [t]he initialization (and any associated
11879 side-effects) of a static data member does
11880 not occur unless the static data member is
11881 itself used in a way that requires the
11882 definition of the static data member to
11885 Therefore, we do not substitute into the
11886 initialized for the static data member here. */
11887 finish_static_data_member_decl
11889 /*init=*/NULL_TREE
,
11890 /*init_const_expr_p=*/false,
11891 /*asmspec_tree=*/NULL_TREE
,
11893 /* Instantiate members marked with attribute used. */
11894 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11897 else if (TREE_CODE (r
) == FIELD_DECL
)
11899 /* Determine whether R has a valid type and can be
11900 completed later. If R is invalid, then its type
11901 is replaced by error_mark_node. */
11902 tree rtype
= TREE_TYPE (r
);
11903 if (can_complete_type_without_circularity (rtype
))
11904 complete_type (rtype
);
11906 if (!complete_or_array_type_p (rtype
))
11908 /* If R's type couldn't be completed and
11909 it isn't a flexible array member (whose
11910 type is incomplete by definition) give
11912 cxx_incomplete_type_error (r
, rtype
);
11913 TREE_TYPE (r
) = error_mark_node
;
11915 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11916 && TYPE_DOMAIN (rtype
) == NULL_TREE
11917 && (TREE_CODE (type
) == UNION_TYPE
11918 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11920 error ("flexible array member %qD in union", r
);
11921 TREE_TYPE (r
) = error_mark_node
;
11923 else if (!verify_type_context (input_location
,
11924 TCTX_FIELD
, rtype
))
11925 TREE_TYPE (r
) = error_mark_node
;
11928 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11929 such a thing will already have been added to the field
11930 list by tsubst_enum in finish_member_declaration in the
11931 CLASSTYPE_NESTED_UTDS case above. */
11932 if (!(TREE_CODE (r
) == TYPE_DECL
11933 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11934 && DECL_ARTIFICIAL (r
)))
11936 set_current_access_from_decl (r
);
11937 finish_member_declaration (r
);
11945 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11946 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11948 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11950 tree friend_type
= t
;
11951 bool adjust_processing_template_decl
= false;
11953 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11955 /* template <class T> friend class C; */
11956 friend_type
= tsubst_friend_class (friend_type
, args
);
11957 adjust_processing_template_decl
= true;
11959 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11961 /* template <class T> friend class C::D; */
11962 friend_type
= tsubst (friend_type
, args
,
11963 tf_warning_or_error
, NULL_TREE
);
11964 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11965 friend_type
= TREE_TYPE (friend_type
);
11966 adjust_processing_template_decl
= true;
11968 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11969 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11971 /* This could be either
11975 when dependent_type_p is false or
11977 template <class U> friend class T::C;
11980 /* Bump processing_template_decl in case this is something like
11981 template <class T> friend struct A<T>::B. */
11982 ++processing_template_decl
;
11983 friend_type
= tsubst (friend_type
, args
,
11984 tf_warning_or_error
, NULL_TREE
);
11985 if (dependent_type_p (friend_type
))
11986 adjust_processing_template_decl
= true;
11987 --processing_template_decl
;
11989 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11990 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11991 && TYPE_HIDDEN_P (friend_type
))
11995 where C hasn't been declared yet. Let's lookup name
11996 from namespace scope directly, bypassing any name that
11997 come from dependent base class. */
11998 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
12000 /* The call to xref_tag_from_type does injection for friend
12002 push_nested_namespace (ns
);
12004 xref_tag_from_type (friend_type
, NULL_TREE
,
12005 /*tag_scope=*/ts_current
);
12006 pop_nested_namespace (ns
);
12008 else if (uses_template_parms (friend_type
))
12009 /* friend class C<T>; */
12010 friend_type
= tsubst (friend_type
, args
,
12011 tf_warning_or_error
, NULL_TREE
);
12016 where C is already declared or
12018 friend class C<int>;
12020 We don't have to do anything in these cases. */
12022 if (adjust_processing_template_decl
)
12023 /* Trick make_friend_class into realizing that the friend
12024 we're adding is a template, not an ordinary class. It's
12025 important that we use make_friend_class since it will
12026 perform some error-checking and output cross-reference
12028 ++processing_template_decl
;
12030 if (friend_type
!= error_mark_node
)
12031 make_friend_class (type
, friend_type
, /*complain=*/false);
12033 if (adjust_processing_template_decl
)
12034 --processing_template_decl
;
12038 /* Build new DECL_FRIENDLIST. */
12041 /* The file and line for this declaration, to
12042 assist in error message reporting. Since we
12043 called push_tinst_level above, we don't need to
12045 input_location
= DECL_SOURCE_LOCATION (t
);
12047 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12049 ++processing_template_decl
;
12050 push_deferring_access_checks (dk_no_check
);
12053 r
= tsubst_friend_function (t
, args
);
12054 add_friend (type
, r
, /*complain=*/false);
12055 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12057 pop_deferring_access_checks ();
12058 --processing_template_decl
;
12066 /* Restore these before substituting into the lambda capture
12068 cp_unevaluated_operand
= saved_unevaluated_operand
;
12069 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12072 /* Set the file and line number information to whatever is given for
12073 the class itself. This puts error messages involving generated
12074 implicit functions at a predictable point, and the same point
12075 that would be used for non-template classes. */
12076 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12078 unreverse_member_declarations (type
);
12079 finish_struct_1 (type
);
12080 TYPE_BEING_DEFINED (type
) = 0;
12082 /* We don't instantiate default arguments for member functions. 14.7.1:
12084 The implicit instantiation of a class template specialization causes
12085 the implicit instantiation of the declarations, but not of the
12086 definitions or default arguments, of the class member functions,
12087 member classes, static data members and member templates.... */
12089 perform_instantiation_time_access_checks (pattern
, args
);
12090 perform_deferred_access_checks (tf_warning_or_error
);
12091 pop_nested_class ();
12092 maximum_field_alignment
= saved_maximum_field_alignment
;
12094 pop_from_top_level ();
12095 pop_tinst_level ();
12097 /* The vtable for a template class can be emitted in any translation
12098 unit in which the class is instantiated. When there is no key
12099 method, however, finish_struct_1 will already have added TYPE to
12100 the keyed_classes. */
12101 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12102 vec_safe_push (keyed_classes
, type
);
12107 /* Wrapper for instantiate_class_template_1. */
12110 instantiate_class_template (tree type
)
12113 timevar_push (TV_TEMPLATE_INST
);
12114 ret
= instantiate_class_template_1 (type
);
12115 timevar_pop (TV_TEMPLATE_INST
);
12120 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12126 else if (TYPE_P (t
))
12127 r
= tsubst (t
, args
, complain
, in_decl
);
12130 if (!(complain
& tf_warning
))
12131 ++c_inhibit_evaluation_warnings
;
12132 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12133 /*integral_constant_expression_p=*/true);
12134 if (!(complain
& tf_warning
))
12135 --c_inhibit_evaluation_warnings
;
12141 /* Given a function parameter pack TMPL_PARM and some function parameters
12142 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12143 and set *SPEC_P to point at the next point in the list. */
12146 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12148 /* Collect all of the extra "packed" parameters into an
12151 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12152 tree spec_parm
= *spec_p
;
12155 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12157 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12160 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12161 parmvec
= make_tree_vec (len
);
12162 spec_parm
= *spec_p
;
12163 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12165 tree elt
= spec_parm
;
12166 if (DECL_PACK_P (elt
))
12167 elt
= make_pack_expansion (elt
);
12168 TREE_VEC_ELT (parmvec
, i
) = elt
;
12171 /* Build the argument packs. */
12172 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12173 *spec_p
= spec_parm
;
12178 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12179 NONTYPE_ARGUMENT_PACK. */
12182 make_fnparm_pack (tree spec_parm
)
12184 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12187 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12188 pack expansion with no extra args, 2 if it has extra args, or 0
12189 if it is not a pack expansion. */
12192 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12194 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12195 /* We're being called before this happens in tsubst_pack_expansion. */
12196 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12197 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12198 if (i
>= TREE_VEC_LENGTH (vec
))
12200 tree elt
= TREE_VEC_ELT (vec
, i
);
12202 /* A decl pack is itself an expansion. */
12203 elt
= TREE_TYPE (elt
);
12204 if (!PACK_EXPANSION_P (elt
))
12206 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12212 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12215 make_argument_pack_select (tree arg_pack
, unsigned index
)
12217 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12219 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12220 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12225 /* This is a subroutine of tsubst_pack_expansion.
12227 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12228 mechanism to store the (non complete list of) arguments of the
12229 substitution and return a non substituted pack expansion, in order
12230 to wait for when we have enough arguments to really perform the
12234 use_pack_expansion_extra_args_p (tree parm_packs
,
12236 bool has_empty_arg
)
12238 /* If one pack has an expansion and another pack has a normal
12239 argument or if one pack has an empty argument and an another
12240 one hasn't then tsubst_pack_expansion cannot perform the
12241 substitution and need to fall back on the
12242 PACK_EXPANSION_EXTRA mechanism. */
12243 if (parm_packs
== NULL_TREE
)
12245 else if (has_empty_arg
)
12247 /* If all the actual packs are pack expansions, we can still
12248 subsitute directly. */
12249 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12251 tree a
= TREE_VALUE (p
);
12252 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12253 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12254 a
= ARGUMENT_PACK_ARGS (a
);
12255 if (TREE_VEC_LENGTH (a
) == 1)
12256 a
= TREE_VEC_ELT (a
, 0);
12257 if (PACK_EXPANSION_P (a
))
12264 bool has_expansion_arg
= false;
12265 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12267 bool has_non_expansion_arg
= false;
12268 for (tree parm_pack
= parm_packs
;
12270 parm_pack
= TREE_CHAIN (parm_pack
))
12272 tree arg
= TREE_VALUE (parm_pack
);
12274 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12276 /* We can't substitute a pack expansion with extra args into
12280 has_expansion_arg
= true;
12282 has_non_expansion_arg
= true;
12285 if (has_expansion_arg
&& has_non_expansion_arg
)
12291 /* [temp.variadic]/6 says that:
12293 The instantiation of a pack expansion [...]
12294 produces a list E1,E2, ..., En, where N is the number of elements
12295 in the pack expansion parameters.
12297 This subroutine of tsubst_pack_expansion produces one of these Ei.
12299 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12300 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12301 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12302 INDEX is the index 'i' of the element Ei to produce. ARGS,
12303 COMPLAIN, and IN_DECL are the same parameters as for the
12304 tsubst_pack_expansion function.
12306 The function returns the resulting Ei upon successful completion,
12307 or error_mark_node.
12309 Note that this function possibly modifies the ARGS parameter, so
12310 it's the responsibility of the caller to restore it. */
12313 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12316 tree args
/* This parm gets
12318 tsubst_flags_t complain
,
12322 bool ith_elem_is_expansion
= false;
12324 /* For each parameter pack, change the substitution of the parameter
12325 pack to the ith argument in its argument pack, then expand the
12327 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12329 tree parm
= TREE_PURPOSE (pack
);
12330 tree arg_pack
= TREE_VALUE (pack
);
12331 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12333 ith_elem_is_expansion
|=
12334 argument_pack_element_is_expansion_p (arg_pack
, index
);
12336 /* Select the Ith argument from the pack. */
12337 if (TREE_CODE (parm
) == PARM_DECL
12339 || TREE_CODE (parm
) == FIELD_DECL
)
12343 aps
= make_argument_pack_select (arg_pack
, index
);
12344 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12345 return error_mark_node
;
12346 register_local_specialization (aps
, parm
);
12349 aps
= retrieve_local_specialization (parm
);
12354 template_parm_level_and_index (parm
, &level
, &idx
);
12358 aps
= make_argument_pack_select (arg_pack
, index
);
12359 /* Update the corresponding argument. */
12360 TMPL_ARG (args
, level
, idx
) = aps
;
12363 /* Re-use the ARGUMENT_PACK_SELECT. */
12364 aps
= TMPL_ARG (args
, level
, idx
);
12366 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12369 /* Substitute into the PATTERN with the (possibly altered)
12371 if (pattern
== in_decl
)
12372 /* Expanding a fixed parameter pack from
12373 coerce_template_parameter_pack. */
12374 t
= tsubst_decl (pattern
, args
, complain
);
12375 else if (pattern
== error_mark_node
)
12376 t
= error_mark_node
;
12377 else if (!TYPE_P (pattern
))
12378 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12379 /*integral_constant_expression_p=*/false);
12381 t
= tsubst (pattern
, args
, complain
, in_decl
);
12383 /* If the Ith argument pack element is a pack expansion, then
12384 the Ith element resulting from the substituting is going to
12385 be a pack expansion as well. */
12386 if (ith_elem_is_expansion
)
12387 t
= make_pack_expansion (t
, complain
);
12392 /* When the unexpanded parameter pack in a fold expression expands to an empty
12393 sequence, the value of the expression is as follows; the program is
12394 ill-formed if the operator is not listed in this table.
12401 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12403 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12404 if (!FOLD_EXPR_MODIFY_P (t
))
12407 case TRUTH_ANDIF_EXPR
:
12408 return boolean_true_node
;
12409 case TRUTH_ORIF_EXPR
:
12410 return boolean_false_node
;
12411 case COMPOUND_EXPR
:
12417 if (complain
& tf_error
)
12418 error_at (location_of (t
),
12419 "fold of empty expansion over %O", code
);
12420 return error_mark_node
;
12423 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12424 form an expression that combines the two terms using the
12428 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12430 tree op
= FOLD_EXPR_OP (t
);
12431 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12433 // Handle compound assignment operators.
12434 if (FOLD_EXPR_MODIFY_P (t
))
12435 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12437 warning_sentinel
s(warn_parentheses
);
12440 case COMPOUND_EXPR
:
12441 return build_x_compound_expr (input_location
, left
, right
, complain
);
12443 return build_x_binary_op (input_location
, code
,
12444 left
, TREE_CODE (left
),
12445 right
, TREE_CODE (right
),
12451 /* Substitute ARGS into the pack of a fold expression T. */
12454 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12456 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12459 /* Substitute ARGS into the pack of a fold expression T. */
12462 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12464 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12467 /* Expand a PACK of arguments into a grouped as left fold.
12468 Given a pack containing elements A0, A1, ..., An and an
12469 operator @, this builds the expression:
12471 ((A0 @ A1) @ A2) ... @ An
12473 Note that PACK must not be empty.
12475 The operator is defined by the original fold expression T. */
12478 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12480 tree left
= TREE_VEC_ELT (pack
, 0);
12481 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12483 tree right
= TREE_VEC_ELT (pack
, i
);
12484 left
= fold_expression (t
, left
, right
, complain
);
12489 /* Substitute into a unary left fold expression. */
12492 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12495 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12496 if (pack
== error_mark_node
)
12497 return error_mark_node
;
12498 if (PACK_EXPANSION_P (pack
))
12500 tree r
= copy_node (t
);
12501 FOLD_EXPR_PACK (r
) = pack
;
12504 if (TREE_VEC_LENGTH (pack
) == 0)
12505 return expand_empty_fold (t
, complain
);
12507 return expand_left_fold (t
, pack
, complain
);
12510 /* Substitute into a binary left fold expression.
12512 Do ths by building a single (non-empty) vector of argumnts and
12513 building the expression from those elements. */
12516 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12519 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12520 if (pack
== error_mark_node
)
12521 return error_mark_node
;
12522 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12523 if (init
== error_mark_node
)
12524 return error_mark_node
;
12526 if (PACK_EXPANSION_P (pack
))
12528 tree r
= copy_node (t
);
12529 FOLD_EXPR_PACK (r
) = pack
;
12530 FOLD_EXPR_INIT (r
) = init
;
12534 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12535 TREE_VEC_ELT (vec
, 0) = init
;
12536 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12537 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12539 return expand_left_fold (t
, vec
, complain
);
12542 /* Expand a PACK of arguments into a grouped as right fold.
12543 Given a pack containing elementns A0, A1, ..., and an
12544 operator @, this builds the expression:
12546 A0@ ... (An-2 @ (An-1 @ An))
12548 Note that PACK must not be empty.
12550 The operator is defined by the original fold expression T. */
12553 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12555 // Build the expression.
12556 int n
= TREE_VEC_LENGTH (pack
);
12557 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12558 for (--n
; n
!= 0; --n
)
12560 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12561 right
= fold_expression (t
, left
, right
, complain
);
12566 /* Substitute into a unary right fold expression. */
12569 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12572 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12573 if (pack
== error_mark_node
)
12574 return error_mark_node
;
12575 if (PACK_EXPANSION_P (pack
))
12577 tree r
= copy_node (t
);
12578 FOLD_EXPR_PACK (r
) = pack
;
12581 if (TREE_VEC_LENGTH (pack
) == 0)
12582 return expand_empty_fold (t
, complain
);
12584 return expand_right_fold (t
, pack
, complain
);
12587 /* Substitute into a binary right fold expression.
12589 Do ths by building a single (non-empty) vector of arguments and
12590 building the expression from those elements. */
12593 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12596 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12597 if (pack
== error_mark_node
)
12598 return error_mark_node
;
12599 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12600 if (init
== error_mark_node
)
12601 return error_mark_node
;
12603 if (PACK_EXPANSION_P (pack
))
12605 tree r
= copy_node (t
);
12606 FOLD_EXPR_PACK (r
) = pack
;
12607 FOLD_EXPR_INIT (r
) = init
;
12611 int n
= TREE_VEC_LENGTH (pack
);
12612 tree vec
= make_tree_vec (n
+ 1);
12613 for (int i
= 0; i
< n
; ++i
)
12614 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12615 TREE_VEC_ELT (vec
, n
) = init
;
12617 return expand_right_fold (t
, vec
, complain
);
12620 /* Walk through the pattern of a pack expansion, adding everything in
12621 local_specializations to a list. */
12626 hash_set
<tree
> internal
;
12628 tsubst_flags_t complain
;
12630 el_data (tsubst_flags_t c
)
12631 : extra (NULL_TREE
), complain (c
) {}
12634 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12636 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12637 tree
*extra
= &data
.extra
;
12638 tsubst_flags_t complain
= data
.complain
;
12640 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12641 /* Remember local typedefs (85214). */
12642 tp
= &TYPE_NAME (*tp
);
12644 if (TREE_CODE (*tp
) == DECL_EXPR
)
12645 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12646 else if (tree spec
= retrieve_local_specialization (*tp
))
12648 if (data
.internal
.contains (*tp
))
12649 /* Don't mess with variables declared within the pattern. */
12651 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12653 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12654 tree args
= ARGUMENT_PACK_ARGS (spec
);
12655 if (TREE_VEC_LENGTH (args
) == 1)
12657 tree elt
= TREE_VEC_ELT (args
, 0);
12658 if (PACK_EXPANSION_P (elt
))
12659 elt
= PACK_EXPANSION_PATTERN (elt
);
12660 if (DECL_PACK_P (elt
))
12663 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12665 /* Handle lambda capture here, since we aren't doing any
12666 substitution now, and so tsubst_copy won't call
12667 process_outer_var_ref. */
12668 tree args
= ARGUMENT_PACK_ARGS (spec
);
12669 int len
= TREE_VEC_LENGTH (args
);
12670 for (int i
= 0; i
< len
; ++i
)
12672 tree arg
= TREE_VEC_ELT (args
, i
);
12674 if (outer_automatic_var_p (arg
))
12675 carg
= process_outer_var_ref (arg
, complain
);
12678 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12682 spec
= copy_node (spec
);
12683 args
= copy_node (args
);
12684 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12685 register_local_specialization (spec
, *tp
);
12687 TREE_VEC_ELT (args
, i
) = carg
;
12692 if (outer_automatic_var_p (spec
))
12693 spec
= process_outer_var_ref (spec
, complain
);
12694 *extra
= tree_cons (*tp
, spec
, *extra
);
12699 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12701 el_data
data (complain
);
12702 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12706 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12707 for use in PACK_EXPANSION_EXTRA_ARGS. */
12710 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12713 if (local_specializations
)
12714 if (tree locals
= extract_local_specs (pattern
, complain
))
12715 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12719 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12720 normal template args to ARGS. */
12723 add_extra_args (tree extra
, tree args
)
12725 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12727 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12729 /* The partial instantiation involved local declarations collected in
12730 extract_local_specs; map from the general template to our local
12732 tree gen
= TREE_PURPOSE (elt
);
12733 tree inst
= TREE_VALUE (elt
);
12735 if (tree local
= retrieve_local_specialization (inst
))
12737 /* else inst is already a full instantiation of the pack. */
12738 register_local_specialization (inst
, gen
);
12740 gcc_assert (!TREE_PURPOSE (extra
));
12741 extra
= TREE_VALUE (extra
);
12744 /* I think we should always be able to substitute dependent args into the
12745 pattern. If that turns out to be incorrect in some cases, enable the
12746 alternate code (and add complain/in_decl parms to this function). */
12747 gcc_checking_assert (!uses_template_parms (extra
));
12749 if (!uses_template_parms (extra
))
12751 gcc_unreachable ();
12752 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12753 args
= add_outermost_template_args (args
, extra
);
12757 args
= add_to_template_args (extra
, args
);
12761 /* Substitute ARGS into T, which is an pack expansion
12762 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12763 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12764 (if only a partial substitution could be performed) or
12765 ERROR_MARK_NODE if there was an error. */
12767 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12771 tree pack
, packs
= NULL_TREE
;
12772 bool unsubstituted_packs
= false;
12775 bool need_local_specializations
= false;
12778 gcc_assert (PACK_EXPANSION_P (t
));
12779 pattern
= PACK_EXPANSION_PATTERN (t
);
12781 /* Add in any args remembered from an earlier partial instantiation. */
12782 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12784 levels
= TMPL_ARGS_DEPTH (args
);
12786 /* Determine the argument packs that will instantiate the parameter
12787 packs used in the expansion expression. While we're at it,
12788 compute the number of arguments to be expanded and make sure it
12790 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12791 pack
= TREE_CHAIN (pack
))
12793 tree parm_pack
= TREE_VALUE (pack
);
12794 tree arg_pack
= NULL_TREE
;
12795 tree orig_arg
= NULL_TREE
;
12798 if (TREE_CODE (parm_pack
) == BASES
)
12800 gcc_assert (parm_pack
== pattern
);
12801 if (BASES_DIRECT (parm_pack
))
12802 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12807 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12808 args
, complain
, in_decl
,
12811 else if (builtin_pack_call_p (parm_pack
))
12813 if (parm_pack
!= pattern
)
12815 if (complain
& tf_error
)
12816 sorry ("%qE is not the entire pattern of the pack expansion",
12818 return error_mark_node
;
12820 return expand_builtin_pack_call (parm_pack
, args
,
12821 complain
, in_decl
);
12823 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12825 /* We know we have correct local_specializations if this
12826 expansion is at function scope, or if we're dealing with a
12827 local parameter in a requires expression; for the latter,
12828 tsubst_requires_expr set it up appropriately. */
12829 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12830 arg_pack
= retrieve_local_specialization (parm_pack
);
12832 /* We can't rely on local_specializations for a parameter
12833 name used later in a function declaration (such as in a
12834 late-specified return type). Even if it exists, it might
12835 have the wrong value for a recursive call. */
12836 need_local_specializations
= true;
12840 /* This parameter pack was used in an unevaluated context. Just
12841 make a dummy decl, since it's only used for its type. */
12842 ++cp_unevaluated_operand
;
12843 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12844 --cp_unevaluated_operand
;
12845 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12846 /* Partial instantiation of the parm_pack, we can't build
12847 up an argument pack yet. */
12848 arg_pack
= NULL_TREE
;
12850 arg_pack
= make_fnparm_pack (arg_pack
);
12852 else if (DECL_PACK_P (arg_pack
))
12853 /* This argument pack isn't fully instantiated yet. */
12854 arg_pack
= NULL_TREE
;
12856 else if (is_capture_proxy (parm_pack
))
12858 arg_pack
= retrieve_local_specialization (parm_pack
);
12859 if (DECL_PACK_P (arg_pack
))
12860 arg_pack
= NULL_TREE
;
12865 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12866 if (level
<= levels
)
12867 arg_pack
= TMPL_ARG (args
, level
, idx
);
12869 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12870 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12871 arg_pack
= NULL_TREE
;
12874 orig_arg
= arg_pack
;
12875 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12876 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12878 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12879 /* This can only happen if we forget to expand an argument
12880 pack somewhere else. Just return an error, silently. */
12882 result
= make_tree_vec (1);
12883 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12890 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12892 /* Don't bother trying to do a partial substitution with
12893 incomplete packs; we'll try again after deduction. */
12894 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12899 else if (len
!= my_len
)
12901 if (!(complain
& tf_error
))
12902 /* Fail quietly. */;
12903 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12904 error ("mismatched argument pack lengths while expanding %qT",
12907 error ("mismatched argument pack lengths while expanding %qE",
12909 return error_mark_node
;
12912 /* Keep track of the parameter packs and their corresponding
12914 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12915 TREE_TYPE (packs
) = orig_arg
;
12919 /* We can't substitute for this parameter pack. We use a flag as
12920 well as the missing_level counter because function parameter
12921 packs don't have a level. */
12922 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12923 unsubstituted_packs
= true;
12927 /* If the expansion is just T..., return the matching argument pack, unless
12928 we need to call convert_from_reference on all the elements. This is an
12929 important optimization; see c++/68422. */
12930 if (!unsubstituted_packs
12931 && TREE_PURPOSE (packs
) == pattern
)
12933 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12935 /* If the argument pack is a single pack expansion, pull it out. */
12936 if (TREE_VEC_LENGTH (args
) == 1
12937 && pack_expansion_args_count (args
))
12938 return TREE_VEC_ELT (args
, 0);
12940 /* Types need no adjustment, nor does sizeof..., and if we still have
12941 some pack expansion args we won't do anything yet. */
12942 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12943 || PACK_EXPANSION_SIZEOF_P (t
)
12944 || pack_expansion_args_count (args
))
12946 /* Also optimize expression pack expansions if we can tell that the
12947 elements won't have reference type. */
12948 tree type
= TREE_TYPE (pattern
);
12949 if (type
&& !TYPE_REF_P (type
)
12950 && !PACK_EXPANSION_P (type
)
12951 && !WILDCARD_TYPE_P (type
))
12953 /* Otherwise use the normal path so we get convert_from_reference. */
12956 /* We cannot expand this expansion expression, because we don't have
12957 all of the argument packs we need. */
12958 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12960 /* We got some full packs, but we can't substitute them in until we
12961 have values for all the packs. So remember these until then. */
12963 t
= make_pack_expansion (pattern
, complain
);
12964 PACK_EXPANSION_EXTRA_ARGS (t
)
12965 = build_extra_args (pattern
, args
, complain
);
12969 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12970 type, so create our own local specializations map; the current map is
12971 either NULL or (in the case of recursive unification) might have
12972 bindings that we don't want to use or alter. */
12973 local_specialization_stack
lss (need_local_specializations
12974 ? lss_blank
: lss_nop
);
12976 if (unsubstituted_packs
)
12978 /* There were no real arguments, we're just replacing a parameter
12979 pack with another version of itself. Substitute into the
12980 pattern and return a PACK_EXPANSION_*. The caller will need to
12982 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12983 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12984 /*integral_constant_expression_p=*/false);
12986 t
= tsubst (pattern
, args
, complain
, in_decl
);
12987 t
= make_pack_expansion (t
, complain
);
12991 gcc_assert (len
>= 0);
12993 /* For each argument in each argument pack, substitute into the
12995 result
= make_tree_vec (len
);
12996 tree elem_args
= copy_template_args (args
);
12997 for (i
= 0; i
< len
; ++i
)
12999 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13001 elem_args
, complain
,
13003 TREE_VEC_ELT (result
, i
) = t
;
13004 if (t
== error_mark_node
)
13006 result
= error_mark_node
;
13011 /* Update ARGS to restore the substitution from parameter packs to
13012 their argument packs. */
13013 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13015 tree parm
= TREE_PURPOSE (pack
);
13017 if (TREE_CODE (parm
) == PARM_DECL
13019 || TREE_CODE (parm
) == FIELD_DECL
)
13020 register_local_specialization (TREE_TYPE (pack
), parm
);
13025 if (TREE_VALUE (pack
) == NULL_TREE
)
13028 template_parm_level_and_index (parm
, &level
, &idx
);
13030 /* Update the corresponding argument. */
13031 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13032 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13035 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13039 /* If the dependent pack arguments were such that we end up with only a
13040 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13041 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13042 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13043 return TREE_VEC_ELT (result
, 0);
13048 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13049 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13050 parameter packs; all parms generated from a function parameter pack will
13051 have the same DECL_PARM_INDEX. */
13054 get_pattern_parm (tree parm
, tree tmpl
)
13056 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13059 if (DECL_ARTIFICIAL (parm
))
13061 for (patparm
= DECL_ARGUMENTS (pattern
);
13062 patparm
; patparm
= DECL_CHAIN (patparm
))
13063 if (DECL_ARTIFICIAL (patparm
)
13064 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13069 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13070 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13071 gcc_assert (DECL_PARM_INDEX (patparm
)
13072 == DECL_PARM_INDEX (parm
));
13078 /* Make an argument pack out of the TREE_VEC VEC. */
13081 make_argument_pack (tree vec
)
13085 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13086 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13089 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13090 TREE_CONSTANT (pack
) = 1;
13092 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13096 /* Return an exact copy of template args T that can be modified
13100 copy_template_args (tree t
)
13102 if (t
== error_mark_node
)
13105 int len
= TREE_VEC_LENGTH (t
);
13106 tree new_vec
= make_tree_vec (len
);
13108 for (int i
= 0; i
< len
; ++i
)
13110 tree elt
= TREE_VEC_ELT (t
, i
);
13111 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13112 elt
= copy_template_args (elt
);
13113 TREE_VEC_ELT (new_vec
, i
) = elt
;
13116 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13117 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13122 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13125 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13128 /* Substitute into each of the arguments. */
13129 tree new_arg
= TYPE_P (orig_arg
)
13130 ? cxx_make_type (TREE_CODE (orig_arg
))
13131 : make_node (TREE_CODE (orig_arg
));
13133 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13134 args
, complain
, in_decl
);
13135 if (pack_args
== error_mark_node
)
13136 new_arg
= error_mark_node
;
13138 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13140 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13141 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13146 /* Substitute ARGS into the vector or list of template arguments T. */
13149 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13152 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13155 if (t
== error_mark_node
)
13156 return error_mark_node
;
13158 len
= TREE_VEC_LENGTH (t
);
13159 elts
= XALLOCAVEC (tree
, len
);
13161 for (i
= 0; i
< len
; i
++)
13163 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13166 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13167 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13168 else if (PACK_EXPANSION_P (orig_arg
))
13170 /* Substitute into an expansion expression. */
13171 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13173 if (TREE_CODE (new_arg
) == TREE_VEC
)
13174 /* Add to the expanded length adjustment the number of
13175 expanded arguments. We subtract one from this
13176 measurement, because the argument pack expression
13177 itself is already counted as 1 in
13178 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13179 the argument pack is empty. */
13180 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13182 else if (ARGUMENT_PACK_P (orig_arg
))
13183 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13185 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13187 if (new_arg
== error_mark_node
)
13188 return error_mark_node
;
13191 if (new_arg
!= orig_arg
)
13198 /* Make space for the expanded arguments coming from template
13200 t
= make_tree_vec (len
+ expanded_len_adjust
);
13201 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13202 arguments for a member template.
13203 In that case each TREE_VEC in ORIG_T represents a level of template
13204 arguments, and ORIG_T won't carry any non defaulted argument count.
13205 It will rather be the nested TREE_VECs that will carry one.
13206 In other words, ORIG_T carries a non defaulted argument count only
13207 if it doesn't contain any nested TREE_VEC. */
13208 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13210 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13211 count
+= expanded_len_adjust
;
13212 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13214 for (i
= 0, out
= 0; i
< len
; i
++)
13216 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13217 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13218 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13222 /* Now expand the template argument pack "in place". */
13223 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13224 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13228 TREE_VEC_ELT (t
, out
) = elts
[i
];
13236 /* Substitute ARGS into one level PARMS of template parameters. */
13239 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13241 if (parms
== error_mark_node
)
13242 return error_mark_node
;
13244 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13246 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13248 tree tuple
= TREE_VEC_ELT (parms
, i
);
13250 if (tuple
== error_mark_node
)
13253 TREE_VEC_ELT (new_vec
, i
) =
13254 tsubst_template_parm (tuple
, args
, complain
);
13260 /* Return the result of substituting ARGS into the template parameters
13261 given by PARMS. If there are m levels of ARGS and m + n levels of
13262 PARMS, then the result will contain n levels of PARMS. For
13263 example, if PARMS is `template <class T> template <class U>
13264 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13265 result will be `template <int*, double, class V>'. */
13268 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13270 tree r
= NULL_TREE
;
13273 /* When substituting into a template, we must set
13274 PROCESSING_TEMPLATE_DECL as the template parameters may be
13275 dependent if they are based on one-another, and the dependency
13276 predicates are short-circuit outside of templates. */
13277 ++processing_template_decl
;
13279 for (new_parms
= &r
;
13280 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13281 new_parms
= &(TREE_CHAIN (*new_parms
)),
13282 parms
= TREE_CHAIN (parms
))
13284 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13287 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13288 - TMPL_ARGS_DEPTH (args
)),
13289 new_vec
, NULL_TREE
);
13290 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13291 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13294 --processing_template_decl
;
13299 /* Return the result of substituting ARGS into one template parameter
13300 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13301 parameter and which TREE_PURPOSE is the default argument of the
13302 template parameter. */
13305 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13307 tree default_value
, parm_decl
;
13309 if (args
== NULL_TREE
13311 || t
== error_mark_node
)
13314 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13316 default_value
= TREE_PURPOSE (t
);
13317 parm_decl
= TREE_VALUE (t
);
13318 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13320 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13321 if (TREE_CODE (parm_decl
) == PARM_DECL
13322 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13323 parm_decl
= error_mark_node
;
13324 default_value
= tsubst_template_arg (default_value
, args
,
13325 complain
, NULL_TREE
);
13326 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13328 tree r
= build_tree_list (default_value
, parm_decl
);
13329 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13333 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13334 type T. If T is not an aggregate or enumeration type, it is
13335 handled as if by tsubst. IN_DECL is as for tsubst. If
13336 ENTERING_SCOPE is nonzero, T is the context for a template which
13337 we are presently tsubst'ing. Return the substituted value. */
13340 tsubst_aggr_type (tree t
,
13342 tsubst_flags_t complain
,
13344 int entering_scope
)
13346 if (t
== NULL_TREE
)
13349 switch (TREE_CODE (t
))
13352 if (TYPE_PTRMEMFUNC_P (t
))
13353 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13355 /* Fall through. */
13356 case ENUMERAL_TYPE
:
13358 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13364 /* In "sizeof(X<I>)" we need to evaluate "I". */
13367 /* First, determine the context for the type we are looking
13369 context
= TYPE_CONTEXT (t
);
13370 if (context
&& TYPE_P (context
))
13372 context
= tsubst_aggr_type (context
, args
, complain
,
13373 in_decl
, /*entering_scope=*/1);
13374 /* If context is a nested class inside a class template,
13375 it may still need to be instantiated (c++/33959). */
13376 context
= complete_type (context
);
13379 /* Then, figure out what arguments are appropriate for the
13380 type we are trying to find. For example, given:
13382 template <class T> struct S;
13383 template <class T, class U> void f(T, U) { S<U> su; }
13385 and supposing that we are instantiating f<int, double>,
13386 then our ARGS will be {int, double}, but, when looking up
13387 S we only want {double}. */
13388 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13389 complain
, in_decl
);
13390 if (argvec
== error_mark_node
)
13391 r
= error_mark_node
;
13394 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13395 entering_scope
, complain
);
13396 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13402 /* This is not a template type, so there's nothing to do. */
13406 return tsubst (t
, args
, complain
, in_decl
);
13410 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13412 /* Substitute into the default argument ARG (a default argument for
13413 FN), which has the indicated TYPE. */
13416 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13417 tsubst_flags_t complain
)
13419 int errs
= errorcount
+ sorrycount
;
13421 /* This can happen in invalid code. */
13422 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13426 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13427 && CONSTRUCTOR_NELTS (arg
) == 0)
13430 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13431 parm
= chain_index (parmnum
, parm
);
13432 tree parmtype
= TREE_TYPE (parm
);
13433 if (DECL_BY_REFERENCE (parm
))
13434 parmtype
= TREE_TYPE (parmtype
);
13435 if (parmtype
== error_mark_node
)
13436 return error_mark_node
;
13438 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13441 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13444 /* This default argument came from a template. Instantiate the
13445 default argument here, not in tsubst. In the case of
13454 we must be careful to do name lookup in the scope of S<T>,
13455 rather than in the current class. */
13456 push_to_top_level ();
13457 push_access_scope (fn
);
13458 push_deferring_access_checks (dk_no_deferred
);
13459 start_lambda_scope (parm
);
13461 /* The default argument expression may cause implicitly defined
13462 member functions to be synthesized, which will result in garbage
13463 collection. We must treat this situation as if we were within
13464 the body of function so as to avoid collecting live data on the
13467 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13468 complain
, NULL_TREE
,
13469 /*integral_constant_expression_p=*/false);
13472 finish_lambda_scope ();
13474 /* Make sure the default argument is reasonable. */
13475 arg
= check_default_argument (type
, arg
, complain
);
13477 if (errorcount
+sorrycount
> errs
13478 && (complain
& tf_warning_or_error
))
13479 inform (input_location
,
13480 " when instantiating default argument for call to %qD", fn
);
13482 pop_deferring_access_checks ();
13483 pop_access_scope (fn
);
13484 pop_from_top_level ();
13486 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13489 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13490 defarg_inst
->put (parm
, arg
);
13496 /* Substitute into all the default arguments for FN. */
13499 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13504 tmpl_args
= DECL_TI_ARGS (fn
);
13506 /* If this function is not yet instantiated, we certainly don't need
13507 its default arguments. */
13508 if (uses_template_parms (tmpl_args
))
13510 /* Don't do this again for clones. */
13511 if (DECL_CLONED_FUNCTION_P (fn
))
13515 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13517 arg
= TREE_CHAIN (arg
), ++i
)
13518 if (TREE_PURPOSE (arg
))
13519 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13521 TREE_PURPOSE (arg
),
13525 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13526 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13528 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13531 store_explicit_specifier (tree v
, tree t
)
13533 if (!explicit_specifier_map
)
13534 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13535 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13536 explicit_specifier_map
->put (v
, t
);
13539 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13542 lookup_explicit_specifier (tree v
)
13544 return *explicit_specifier_map
->get (v
);
13547 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13548 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13549 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13553 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13554 tree raises
, tsubst_flags_t complain
)
13556 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13559 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13561 new_type
= build_function_type (return_type
, arg_types
);
13562 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13566 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13567 /* Don't pick up extra function qualifiers from the basetype. */
13568 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13569 if (! MAYBE_CLASS_TYPE_P (r
))
13573 Type deduction may fail for any of the following
13576 -- Attempting to create "pointer to member of T" when T
13577 is not a class type. */
13578 if (complain
& tf_error
)
13579 error ("creating pointer to member function of non-class type %qT",
13581 return error_mark_node
;
13584 new_type
= build_method_type_directly (r
, return_type
,
13585 TREE_CHAIN (arg_types
));
13587 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13589 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13590 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13591 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13594 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13595 each of its formal parameters. If there is a disagreement then rebuild
13596 DECL's function type according to its formal parameter types, as part of a
13597 resolution for Core issues 1001/1322. */
13600 maybe_rebuild_function_decl_type (tree decl
)
13602 bool function_type_needs_rebuilding
= false;
13603 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13605 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13606 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13608 tree parm_type
= TREE_VALUE (parm_type_list
);
13609 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13610 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13612 function_type_needs_rebuilding
= true;
13616 parm_list
= DECL_CHAIN (parm_list
);
13617 parm_type_list
= TREE_CHAIN (parm_type_list
);
13621 if (!function_type_needs_rebuilding
)
13624 const tree fntype
= TREE_TYPE (decl
);
13625 tree parm_list
= DECL_ARGUMENTS (decl
);
13626 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13627 tree new_parm_type_list
= NULL_TREE
;
13628 tree
*q
= &new_parm_type_list
;
13629 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13631 *q
= copy_node (old_parm_type_list
);
13632 parm_list
= DECL_CHAIN (parm_list
);
13633 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13634 q
= &TREE_CHAIN (*q
);
13636 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13638 *q
= copy_node (old_parm_type_list
);
13639 tree
*new_parm_type
= &TREE_VALUE (*q
);
13640 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13641 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13642 *new_parm_type
= formal_parm_type_unqual
;
13644 parm_list
= DECL_CHAIN (parm_list
);
13645 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13646 q
= &TREE_CHAIN (*q
);
13648 if (old_parm_type_list
== void_list_node
)
13649 *q
= void_list_node
;
13652 = rebuild_function_or_method_type (fntype
,
13653 TREE_TYPE (fntype
), new_parm_type_list
,
13654 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13657 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13660 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13661 tree lambda_fntype
)
13663 tree gen_tmpl
, argvec
;
13664 hashval_t hash
= 0;
13667 /* Nobody should be tsubst'ing into non-template functions. */
13668 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13670 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13672 /* If T is not dependent, just return it. */
13673 if (!uses_template_parms (DECL_TI_ARGS (t
))
13674 && !LAMBDA_FUNCTION_P (t
))
13677 /* Calculate the most general template of which R is a
13679 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13681 /* We're substituting a lambda function under tsubst_lambda_expr but not
13682 directly from it; find the matching function we're already inside.
13683 But don't do this if T is a generic lambda with a single level of
13684 template parms, as in that case we're doing a normal instantiation. */
13685 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13686 && (!generic_lambda_fn_p (t
)
13687 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13688 return enclosing_instantiation_of (t
);
13690 /* Calculate the complete set of arguments used to
13692 argvec
= tsubst_template_args (DECL_TI_ARGS
13693 (DECL_TEMPLATE_RESULT
13694 (DECL_TI_TEMPLATE (t
))),
13695 args
, complain
, in_decl
);
13696 if (argvec
== error_mark_node
)
13697 return error_mark_node
;
13699 /* Check to see if we already have this specialization. */
13700 if (!lambda_fntype
)
13702 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13703 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13707 /* We can see more levels of arguments than parameters if
13708 there was a specialization of a member template, like
13711 template <class T> struct S { template <class U> void f(); }
13712 template <> template <class U> void S<int>::f(U);
13714 Here, we'll be substituting into the specialization,
13715 because that's where we can find the code we actually
13716 want to generate, but we'll have enough arguments for
13717 the most general template.
13719 We also deal with the peculiar case:
13721 template <class T> struct S {
13722 template <class U> friend void f();
13724 template <class U> void f() {}
13726 template void f<double>();
13728 Here, the ARGS for the instantiation of will be {int,
13729 double}. But, we only need as many ARGS as there are
13730 levels of template parameters in CODE_PATTERN. We are
13731 careful not to get fooled into reducing the ARGS in
13734 template <class T> struct S { template <class U> void f(U); }
13735 template <class T> template <> void S<T>::f(int) {}
13737 which we can spot because the pattern will be a
13738 specialization in this case. */
13739 int args_depth
= TMPL_ARGS_DEPTH (args
);
13741 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13743 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13744 args
= get_innermost_template_args (args
, parms_depth
);
13748 /* This special case arises when we have something like this:
13750 template <class T> struct S {
13751 friend void f<int>(int, double);
13754 Here, the DECL_TI_TEMPLATE for the friend declaration
13755 will be an IDENTIFIER_NODE. We are being called from
13756 tsubst_friend_function, and we want only to create a
13757 new decl (R) with appropriate types so that we can call
13758 determine_specialization. */
13759 gen_tmpl
= NULL_TREE
;
13760 argvec
= NULL_TREE
;
13763 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13765 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13766 bool member
= ctx
&& TYPE_P (ctx
);
13768 if (member
&& !closure
)
13769 ctx
= tsubst_aggr_type (ctx
, args
,
13770 complain
, t
, /*entering_scope=*/1);
13772 tree type
= (lambda_fntype
? lambda_fntype
13773 : tsubst (TREE_TYPE (t
), args
,
13774 complain
| tf_fndecl_type
, in_decl
));
13775 if (type
== error_mark_node
)
13776 return error_mark_node
;
13778 /* If we hit excessive deduction depth, the type is bogus even if
13779 it isn't error_mark_node, so don't build a decl. */
13780 if (excessive_deduction_depth
)
13781 return error_mark_node
;
13783 /* We do NOT check for matching decls pushed separately at this
13784 point, as they may not represent instantiations of this
13785 template, and in any case are considered separate under the
13787 tree r
= copy_decl (t
);
13788 DECL_USE_TEMPLATE (r
) = 0;
13789 TREE_TYPE (r
) = type
;
13790 /* Clear out the mangled name and RTL for the instantiation. */
13791 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13792 SET_DECL_RTL (r
, NULL
);
13793 /* Leave DECL_INITIAL set on deleted instantiations. */
13794 if (!DECL_DELETED_FN (r
))
13795 DECL_INITIAL (r
) = NULL_TREE
;
13796 DECL_CONTEXT (r
) = ctx
;
13798 /* Handle explicit(dependent-expr). */
13799 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13801 tree spec
= lookup_explicit_specifier (t
);
13802 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13803 /*function_p=*/false,
13805 spec
= build_explicit_specifier (spec
, complain
);
13806 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13809 /* OpenMP UDRs have the only argument a reference to the declared
13810 type. We want to diagnose if the declared type is a reference,
13811 which is invalid, but as references to references are usually
13812 quietly merged, diagnose it here. */
13813 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13816 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13817 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13818 if (TYPE_REF_P (argtype
))
13819 error_at (DECL_SOURCE_LOCATION (t
),
13820 "reference type %qT in "
13821 "%<#pragma omp declare reduction%>", argtype
);
13822 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13823 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13827 if (member
&& DECL_CONV_FN_P (r
))
13828 /* Type-conversion operator. Reconstruct the name, in
13829 case it's the name of one of the template's parameters. */
13830 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13832 tree parms
= DECL_ARGUMENTS (t
);
13834 parms
= DECL_CHAIN (parms
);
13835 parms
= tsubst (parms
, args
, complain
, t
);
13836 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13837 DECL_CONTEXT (parm
) = r
;
13840 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13841 DECL_NAME (tparm
) = closure_identifier
;
13842 DECL_CHAIN (tparm
) = parms
;
13845 DECL_ARGUMENTS (r
) = parms
;
13846 DECL_RESULT (r
) = NULL_TREE
;
13848 maybe_rebuild_function_decl_type (r
);
13850 TREE_STATIC (r
) = 0;
13851 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13852 DECL_EXTERNAL (r
) = 1;
13853 /* If this is an instantiation of a function with internal
13854 linkage, we already know what object file linkage will be
13855 assigned to the instantiation. */
13856 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13857 DECL_DEFER_OUTPUT (r
) = 0;
13858 DECL_CHAIN (r
) = NULL_TREE
;
13859 DECL_PENDING_INLINE_INFO (r
) = 0;
13860 DECL_PENDING_INLINE_P (r
) = 0;
13861 DECL_SAVED_TREE (r
) = NULL_TREE
;
13862 DECL_STRUCT_FUNCTION (r
) = NULL
;
13864 /* We'll re-clone as appropriate in instantiate_template. */
13865 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13867 /* If we aren't complaining now, return on error before we register
13868 the specialization so that we'll complain eventually. */
13869 if ((complain
& tf_error
) == 0
13870 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13871 && !grok_op_properties (r
, /*complain=*/false))
13872 return error_mark_node
;
13874 /* Associate the constraints directly with the instantiation. We
13875 don't substitute through the constraints; that's only done when
13876 they are checked. */
13877 if (tree ci
= get_constraints (t
))
13878 /* Unless we're regenerating a lambda, in which case we'll set the
13879 lambda's constraints in tsubst_lambda_expr. */
13880 if (!lambda_fntype
)
13881 set_constraints (r
, ci
);
13883 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13884 SET_DECL_FRIEND_CONTEXT (r
,
13885 tsubst (DECL_FRIEND_CONTEXT (t
),
13886 args
, complain
, in_decl
));
13888 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13889 this in the special friend case mentioned above where
13890 GEN_TMPL is NULL. */
13891 if (gen_tmpl
&& !closure
)
13893 DECL_TEMPLATE_INFO (r
)
13894 = build_template_info (gen_tmpl
, argvec
);
13895 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13898 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13900 /* We instantiated this while substituting into
13901 the type earlier (template/friend54.C). */
13904 /* We're not supposed to instantiate default arguments
13905 until they are called, for a template. But, for a
13908 template <class T> void f ()
13909 { extern void g(int i = T()); }
13911 we should do the substitution when the template is
13912 instantiated. We handle the member function case in
13913 instantiate_class_template since the default arguments
13914 might refer to other members of the class. */
13916 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13917 && !uses_template_parms (argvec
))
13918 tsubst_default_arguments (r
, complain
);
13921 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13923 /* Copy the list of befriending classes. */
13924 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13926 friends
= &TREE_CHAIN (*friends
))
13928 *friends
= copy_node (*friends
);
13929 TREE_VALUE (*friends
)
13930 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13933 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13935 maybe_retrofit_in_chrg (r
);
13936 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13937 return error_mark_node
;
13938 /* If this is an instantiation of a member template, clone it.
13939 If it isn't, that'll be handled by
13940 clone_constructors_and_destructors. */
13941 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13942 clone_cdtor (r
, /*update_methods=*/false);
13944 else if ((complain
& tf_error
) != 0
13945 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13946 && !grok_op_properties (r
, /*complain=*/true))
13947 return error_mark_node
;
13949 /* Possibly limit visibility based on template args. */
13950 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13951 if (DECL_VISIBILITY_SPECIFIED (t
))
13953 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13954 DECL_ATTRIBUTES (r
)
13955 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13957 determine_visibility (r
);
13958 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13959 && !processing_template_decl
)
13960 defaulted_late_check (r
);
13962 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13963 args
, complain
, in_decl
);
13965 if (tree attr
= lookup_attribute ("omp declare variant base",
13966 DECL_ATTRIBUTES (r
)))
13967 omp_declare_variant_finalize (r
, attr
);
13972 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13975 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13976 tree lambda_fntype
)
13978 /* We can get here when processing a member function template,
13979 member class template, or template template parameter. */
13980 tree decl
= DECL_TEMPLATE_RESULT (t
);
13986 hashval_t hash
= 0;
13988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13990 /* Template template parameter is treated here. */
13991 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13992 if (new_type
== error_mark_node
)
13993 r
= error_mark_node
;
13994 /* If we get a real template back, return it. This can happen in
13995 the context of most_specialized_partial_spec. */
13996 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13999 /* The new TEMPLATE_DECL was built in
14000 reduce_template_parm_level. */
14001 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14005 if (!lambda_fntype
)
14007 /* We might already have an instance of this template.
14008 The ARGS are for the surrounding class type, so the
14009 full args contain the tsubst'd args for the context,
14010 plus the innermost args from the template decl. */
14011 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14012 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14013 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14014 /* Because this is a template, the arguments will still be
14015 dependent, even after substitution. If
14016 PROCESSING_TEMPLATE_DECL is not set, the dependency
14017 predicates will short-circuit. */
14018 ++processing_template_decl
;
14019 full_args
= tsubst_template_args (tmpl_args
, args
,
14020 complain
, in_decl
);
14021 --processing_template_decl
;
14022 if (full_args
== error_mark_node
)
14023 return error_mark_node
;
14025 /* If this is a default template template argument,
14026 tsubst might not have changed anything. */
14027 if (full_args
== tmpl_args
)
14030 hash
= hash_tmpl_and_args (t
, full_args
);
14031 spec
= retrieve_specialization (t
, full_args
, hash
);
14032 if (spec
!= NULL_TREE
)
14035 /* Type partial instantiations are stored as the type by
14036 lookup_template_class_1, not here as the template. */
14037 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14042 /* Make a new template decl. It will be similar to the
14043 original, but will record the current template arguments.
14044 We also create a new function declaration, which is just
14045 like the old one, but points to this new template, rather
14046 than the old one. */
14048 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14049 DECL_CHAIN (r
) = NULL_TREE
;
14051 // Build new template info linking to the original template decl.
14052 if (!lambda_fntype
)
14054 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14055 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14058 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14060 /* The template parameters for this new template are all the
14061 template parameters for the old template, except the
14062 outermost level of parameters. */
14063 DECL_TEMPLATE_PARMS (r
)
14064 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14067 bool class_p
= false;
14069 ++processing_template_decl
;
14070 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14071 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14074 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14077 inner
= TREE_TYPE (inner
);
14079 inner
= tsubst (inner
, args
, complain
, in_decl
);
14081 --processing_template_decl
;
14082 if (inner
== error_mark_node
)
14083 return error_mark_node
;
14087 /* For a partial specialization, we need to keep pointing to
14088 the primary template. */
14089 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14090 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14092 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14093 inner
= TYPE_MAIN_DECL (inner
);
14095 else if (lambda_fntype
)
14097 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14098 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14102 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14103 DECL_TI_TEMPLATE (inner
) = r
;
14104 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14107 DECL_TEMPLATE_RESULT (r
) = inner
;
14108 TREE_TYPE (r
) = TREE_TYPE (inner
);
14109 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14111 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14112 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14114 if (PRIMARY_TEMPLATE_P (t
))
14115 DECL_PRIMARY_TEMPLATE (r
) = r
;
14117 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14119 /* Record this non-type partial instantiation. */
14120 register_specialization (r
, t
,
14121 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14127 /* True if FN is the op() for a lambda in an uninstantiated template. */
14130 lambda_fn_in_template_p (tree fn
)
14132 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14134 tree closure
= DECL_CONTEXT (fn
);
14135 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14138 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14139 which the above is true. */
14142 instantiated_lambda_fn_p (tree fn
)
14144 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14146 tree closure
= DECL_CONTEXT (fn
);
14147 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14148 return LAMBDA_EXPR_INSTANTIATED (lam
);
14151 /* We're instantiating a variable from template function TCTX. Return the
14152 corresponding current enclosing scope. This gets complicated because lambda
14153 functions in templates are regenerated rather than instantiated, but generic
14154 lambda functions are subsequently instantiated. */
14157 enclosing_instantiation_of (tree otctx
)
14160 tree fn
= current_function_decl
;
14161 int lambda_count
= 0;
14163 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14164 || instantiated_lambda_fn_p (tctx
));
14165 tctx
= decl_function_context (tctx
))
14167 for (; fn
; fn
= decl_function_context (fn
))
14170 int flambda_count
= 0;
14171 for (; fn
&& instantiated_lambda_fn_p (fn
);
14172 fn
= decl_function_context (fn
))
14174 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14175 ? most_general_template (fn
) != most_general_template (tctx
)
14178 if (flambda_count
!= lambda_count
)
14180 gcc_assert (flambda_count
> lambda_count
);
14181 for (; flambda_count
> lambda_count
; --flambda_count
)
14182 ofn
= decl_function_context (ofn
);
14184 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14185 || DECL_CONV_FN_P (ofn
));
14188 gcc_unreachable ();
14191 /* Substitute the ARGS into the T, which is a _DECL. Return the
14192 result of the substitution. Issue error and warning messages under
14193 control of COMPLAIN. */
14196 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14198 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14199 location_t saved_loc
;
14200 tree r
= NULL_TREE
;
14202 hashval_t hash
= 0;
14204 /* Set the filename and linenumber to improve error-reporting. */
14205 saved_loc
= input_location
;
14206 input_location
= DECL_SOURCE_LOCATION (t
);
14208 switch (TREE_CODE (t
))
14210 case TEMPLATE_DECL
:
14211 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14214 case FUNCTION_DECL
:
14215 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14220 tree type
= NULL_TREE
;
14222 tree expanded_types
= NULL_TREE
;
14223 tree prev_r
= NULL_TREE
;
14224 tree first_r
= NULL_TREE
;
14226 if (DECL_PACK_P (t
))
14228 /* If there is a local specialization that isn't a
14229 parameter pack, it means that we're doing a "simple"
14230 substitution from inside tsubst_pack_expansion. Just
14231 return the local specialization (which will be a single
14233 tree spec
= retrieve_local_specialization (t
);
14235 && TREE_CODE (spec
) == PARM_DECL
14236 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14239 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14240 the parameters in this function parameter pack. */
14241 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14242 complain
, in_decl
);
14243 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14245 len
= TREE_VEC_LENGTH (expanded_types
);
14247 /* Zero-length parameter packs are boring. Just substitute
14249 if (len
== 0 && !cp_unevaluated_operand
)
14250 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14255 /* All we did was update the type. Make a note of that. */
14256 type
= expanded_types
;
14257 expanded_types
= NULL_TREE
;
14261 /* Loop through all of the parameters we'll build. When T is
14262 a function parameter pack, LEN is the number of expanded
14263 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14265 for (i
= 0; i
< len
; ++i
)
14269 if (DECL_TEMPLATE_PARM_P (t
))
14270 SET_DECL_TEMPLATE_PARM_P (r
);
14272 if (expanded_types
)
14273 /* We're on the Ith parameter of the function parameter
14276 /* Get the Ith type. */
14277 type
= TREE_VEC_ELT (expanded_types
, i
);
14279 /* Rename the parameter to include the index. */
14281 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14284 /* We're dealing with a normal parameter. */
14285 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14287 type
= type_decays_to (type
);
14288 TREE_TYPE (r
) = type
;
14289 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14291 if (DECL_INITIAL (r
))
14293 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14294 DECL_INITIAL (r
) = TREE_TYPE (r
);
14296 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14297 complain
, in_decl
);
14300 DECL_CONTEXT (r
) = NULL_TREE
;
14302 if (!DECL_TEMPLATE_PARM_P (r
))
14303 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14305 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14306 args
, complain
, in_decl
);
14308 /* Keep track of the first new parameter we
14309 generate. That's what will be returned to the
14314 /* Build a proper chain of parameters when substituting
14315 into a function parameter pack. */
14317 DECL_CHAIN (prev_r
) = r
;
14320 /* If cp_unevaluated_operand is set, we're just looking for a
14321 single dummy parameter, so don't keep going. */
14322 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14323 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14324 complain
, DECL_CHAIN (t
));
14326 /* FIRST_R contains the start of the chain we've built. */
14333 tree type
= NULL_TREE
;
14334 tree vec
= NULL_TREE
;
14335 tree expanded_types
= NULL_TREE
;
14338 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14340 /* This field is a lambda capture pack. Return a TREE_VEC of
14341 the expanded fields to instantiate_class_template_1. */
14342 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14343 complain
, in_decl
);
14344 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14346 len
= TREE_VEC_LENGTH (expanded_types
);
14347 vec
= make_tree_vec (len
);
14351 /* All we did was update the type. Make a note of that. */
14352 type
= expanded_types
;
14353 expanded_types
= NULL_TREE
;
14357 for (int i
= 0; i
< len
; ++i
)
14360 if (expanded_types
)
14362 type
= TREE_VEC_ELT (expanded_types
, i
);
14364 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14367 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14369 if (type
== error_mark_node
)
14370 RETURN (error_mark_node
);
14371 TREE_TYPE (r
) = type
;
14372 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14374 if (DECL_C_BIT_FIELD (r
))
14375 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14377 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14378 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14380 /*integral_constant_expression_p=*/true);
14381 if (DECL_INITIAL (t
))
14383 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14384 NSDMI in perform_member_init. Still set DECL_INITIAL
14385 so that we know there is one. */
14386 DECL_INITIAL (r
) = void_node
;
14387 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14388 retrofit_lang_decl (r
);
14389 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14391 /* We don't have to set DECL_CONTEXT here; it is set by
14392 finish_member_declaration. */
14393 DECL_CHAIN (r
) = NULL_TREE
;
14395 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14396 args
, complain
, in_decl
);
14399 TREE_VEC_ELT (vec
, i
) = r
;
14408 /* We reach here only for member using decls. We also need to check
14409 uses_template_parms because DECL_DEPENDENT_P is not set for a
14410 using-declaration that designates a member of the current
14411 instantiation (c++/53549). */
14412 if (DECL_DEPENDENT_P (t
)
14413 || uses_template_parms (USING_DECL_SCOPE (t
)))
14415 tree scope
= USING_DECL_SCOPE (t
);
14416 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14417 if (PACK_EXPANSION_P (scope
))
14419 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14420 int len
= TREE_VEC_LENGTH (vec
);
14421 r
= make_tree_vec (len
);
14422 for (int i
= 0; i
< len
; ++i
)
14424 tree escope
= TREE_VEC_ELT (vec
, i
);
14425 tree elt
= do_class_using_decl (escope
, name
);
14428 r
= error_mark_node
;
14433 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14434 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14436 TREE_VEC_ELT (r
, i
) = elt
;
14441 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14442 complain
, in_decl
);
14443 r
= do_class_using_decl (inst_scope
, name
);
14445 r
= error_mark_node
;
14448 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14449 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14456 DECL_CHAIN (r
) = NULL_TREE
;
14463 tree argvec
= NULL_TREE
;
14464 tree gen_tmpl
= NULL_TREE
;
14466 tree tmpl
= NULL_TREE
;
14468 tree type
= NULL_TREE
;
14471 if (TREE_TYPE (t
) == error_mark_node
)
14472 RETURN (error_mark_node
);
14474 if (TREE_CODE (t
) == TYPE_DECL
14475 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14477 /* If this is the canonical decl, we don't have to
14478 mess with instantiations, and often we can't (for
14479 typename, template type parms and such). Note that
14480 TYPE_NAME is not correct for the above test if
14481 we've copied the type for a typedef. */
14482 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14483 if (type
== error_mark_node
)
14484 RETURN (error_mark_node
);
14485 r
= TYPE_NAME (type
);
14489 /* Check to see if we already have the specialization we
14492 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14494 /* T is a static data member or namespace-scope entity.
14495 We have to substitute into namespace-scope variables
14496 (not just variable templates) because of cases like:
14498 template <class T> void f() { extern T t; }
14500 where the entity referenced is not known until
14501 instantiation time. */
14503 ctx
= DECL_CONTEXT (t
);
14504 if (DECL_CLASS_SCOPE_P (t
))
14506 ctx
= tsubst_aggr_type (ctx
, args
,
14508 in_decl
, /*entering_scope=*/1);
14509 /* If CTX is unchanged, then T is in fact the
14510 specialization we want. That situation occurs when
14511 referencing a static data member within in its own
14512 class. We can use pointer equality, rather than
14513 same_type_p, because DECL_CONTEXT is always
14515 if (ctx
== DECL_CONTEXT (t
)
14516 /* ... unless T is a member template; in which
14517 case our caller can be willing to create a
14518 specialization of that template represented
14520 && !(DECL_TI_TEMPLATE (t
)
14521 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14527 tmpl
= DECL_TI_TEMPLATE (t
);
14528 gen_tmpl
= most_general_template (tmpl
);
14529 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14530 if (argvec
!= error_mark_node
)
14531 argvec
= (coerce_innermost_template_parms
14532 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14533 argvec
, t
, complain
,
14534 /*all*/true, /*defarg*/true));
14535 if (argvec
== error_mark_node
)
14536 RETURN (error_mark_node
);
14537 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14538 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14543 /* A local variable. */
14545 /* Subsequent calls to pushdecl will fill this in. */
14547 /* Unless this is a reference to a static variable from an
14548 enclosing function, in which case we need to fill it in now. */
14549 if (TREE_STATIC (t
))
14551 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14552 if (fn
!= current_function_decl
)
14555 spec
= retrieve_local_specialization (t
);
14557 /* If we already have the specialization we need, there is
14558 nothing more to do. */
14565 /* Create a new node for the specialization we need. */
14566 if (type
== NULL_TREE
)
14568 if (is_typedef_decl (t
))
14569 type
= DECL_ORIGINAL_TYPE (t
);
14571 type
= TREE_TYPE (t
);
14573 && VAR_HAD_UNKNOWN_BOUND (t
)
14574 && type
!= error_mark_node
)
14575 type
= strip_array_domain (type
);
14576 tree sub_args
= args
;
14577 if (tree auto_node
= type_uses_auto (type
))
14579 /* Mask off any template args past the variable's context so we
14580 don't replace the auto with an unrelated argument. */
14581 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14582 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14584 /* This should never happen with the new lambda instantiation
14585 model, but keep the handling just in case. */
14586 gcc_assert (!CHECKING_P
),
14587 sub_args
= strip_innermost_template_args (args
, extra
);
14589 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14590 /* Substituting the type might have recursively instantiated this
14591 same alias (c++/86171). */
14592 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14593 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14602 DECL_INITIALIZED_P (r
) = 0;
14603 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14604 if (type
== error_mark_node
)
14605 RETURN (error_mark_node
);
14606 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14608 /* It may seem that this case cannot occur, since:
14613 declares a function, not a variable. However:
14616 template <typename T> void g() { T t; }
14617 template void g<f>();
14619 is an attempt to declare a variable with function
14621 error ("variable %qD has function type",
14622 /* R is not yet sufficiently initialized, so we
14623 just use its name. */
14625 RETURN (error_mark_node
);
14627 type
= complete_type (type
);
14628 /* Wait until cp_finish_decl to set this again, to handle
14629 circular dependency (template/instantiate6.C). */
14630 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14631 type
= check_var_type (DECL_NAME (r
), type
,
14632 DECL_SOURCE_LOCATION (r
));
14633 if (DECL_HAS_VALUE_EXPR_P (t
))
14635 tree ve
= DECL_VALUE_EXPR (t
);
14636 /* If the DECL_VALUE_EXPR is converted to the declared type,
14637 preserve the identity so that gimplify_type_sizes works. */
14638 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14640 ve
= TREE_OPERAND (ve
, 0);
14641 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14642 /*constant_expression_p=*/false);
14643 if (REFERENCE_REF_P (ve
))
14645 gcc_assert (TYPE_REF_P (type
));
14646 ve
= TREE_OPERAND (ve
, 0);
14649 ve
= build_nop (type
, ve
);
14650 else if (DECL_LANG_SPECIFIC (t
)
14651 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14652 && TREE_CODE (ve
) == COMPONENT_REF
14653 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14654 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14655 type
= TREE_TYPE (ve
);
14657 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14658 == TYPE_MAIN_VARIANT (type
));
14659 SET_DECL_VALUE_EXPR (r
, ve
);
14661 if (CP_DECL_THREAD_LOCAL_P (r
)
14662 && !processing_template_decl
)
14663 set_decl_tls_model (r
, decl_default_tls_model (r
));
14665 else if (DECL_SELF_REFERENCE_P (t
))
14666 SET_DECL_SELF_REFERENCE_P (r
);
14667 TREE_TYPE (r
) = type
;
14668 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14669 DECL_CONTEXT (r
) = ctx
;
14670 /* Clear out the mangled name and RTL for the instantiation. */
14671 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14672 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14673 SET_DECL_RTL (r
, NULL
);
14674 /* The initializer must not be expanded until it is required;
14675 see [temp.inst]. */
14676 DECL_INITIAL (r
) = NULL_TREE
;
14677 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14680 if (DECL_LANG_SPECIFIC (r
))
14681 SET_DECL_DEPENDENT_INIT_P (r
, false);
14683 SET_DECL_MODE (r
, VOIDmode
);
14685 /* Possibly limit visibility based on template args. */
14686 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14687 if (DECL_VISIBILITY_SPECIFIED (t
))
14689 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14690 DECL_ATTRIBUTES (r
)
14691 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14693 determine_visibility (r
);
14698 /* A static data member declaration is always marked
14699 external when it is declared in-class, even if an
14700 initializer is present. We mimic the non-template
14701 processing here. */
14702 DECL_EXTERNAL (r
) = 1;
14703 if (DECL_NAMESPACE_SCOPE_P (t
))
14704 DECL_NOT_REALLY_EXTERN (r
) = 1;
14706 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14707 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14708 /* Remember whether we require constant initialization of
14709 a non-constant template variable. */
14710 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14711 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14712 if (!error_operand_p (r
) || (complain
& tf_error
))
14713 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14717 if (DECL_LANG_SPECIFIC (r
))
14718 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14719 if (!cp_unevaluated_operand
)
14720 register_local_specialization (r
, t
);
14723 DECL_CHAIN (r
) = NULL_TREE
;
14725 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14727 args
, complain
, in_decl
);
14729 /* Preserve a typedef that names a type. */
14730 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14732 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14733 set_underlying_type (r
);
14734 if (TYPE_DECL_ALIAS_P (r
))
14735 /* An alias template specialization can be dependent
14736 even if its underlying type is not. */
14737 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14740 layout_decl (r
, 0);
14745 gcc_unreachable ();
14750 /* Restore the file and line information. */
14751 input_location
= saved_loc
;
14756 /* Substitute into the complete parameter type list PARMS. */
14759 tsubst_function_parms (tree parms
,
14761 tsubst_flags_t complain
,
14764 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14767 /* Substitute into the ARG_TYPES of a function type.
14768 If END is a TREE_CHAIN, leave it and any following types
14772 tsubst_arg_types (tree arg_types
,
14775 tsubst_flags_t complain
,
14778 tree remaining_arg_types
;
14779 tree type
= NULL_TREE
;
14781 tree expanded_args
= NULL_TREE
;
14784 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14787 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14788 args
, end
, complain
, in_decl
);
14789 if (remaining_arg_types
== error_mark_node
)
14790 return error_mark_node
;
14792 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14794 /* For a pack expansion, perform substitution on the
14795 entire expression. Later on, we'll handle the arguments
14797 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14798 args
, complain
, in_decl
);
14800 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14801 /* So that we'll spin through the parameters, one by one. */
14802 i
= TREE_VEC_LENGTH (expanded_args
);
14805 /* We only partially substituted into the parameter
14806 pack. Our type is TYPE_PACK_EXPANSION. */
14807 type
= expanded_args
;
14808 expanded_args
= NULL_TREE
;
14816 type
= TREE_VEC_ELT (expanded_args
, i
);
14818 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14820 if (type
== error_mark_node
)
14821 return error_mark_node
;
14822 if (VOID_TYPE_P (type
))
14824 if (complain
& tf_error
)
14826 error ("invalid parameter type %qT", type
);
14828 error ("in declaration %q+D", in_decl
);
14830 return error_mark_node
;
14833 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14834 return error_mark_node
;
14836 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14837 top-level qualifiers as required. */
14838 type
= cv_unqualified (type_decays_to (type
));
14840 /* We do not substitute into default arguments here. The standard
14841 mandates that they be instantiated only when needed, which is
14842 done in build_over_call. */
14843 default_arg
= TREE_PURPOSE (arg_types
);
14845 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14846 since the new op() won't have any associated template arguments for us
14847 to refer to later. */
14848 if (lambda_fn_in_template_p (in_decl
))
14849 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14850 false/*fn*/, false/*constexpr*/);
14852 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14854 /* We've instantiated a template before its default arguments
14855 have been parsed. This can happen for a nested template
14856 class, and is not an error unless we require the default
14857 argument in a call of this function. */
14858 remaining_arg_types
=
14859 tree_cons (default_arg
, type
, remaining_arg_types
);
14860 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14861 remaining_arg_types
);
14864 remaining_arg_types
=
14865 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14868 return remaining_arg_types
;
14871 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14872 *not* handle the exception-specification for FNTYPE, because the
14873 initial substitution of explicitly provided template parameters
14874 during argument deduction forbids substitution into the
14875 exception-specification:
14879 All references in the function type of the function template to the
14880 corresponding template parameters are replaced by the specified tem-
14881 plate argument values. If a substitution in a template parameter or
14882 in the function type of the function template results in an invalid
14883 type, type deduction fails. [Note: The equivalent substitution in
14884 exception specifications is done only when the function is instanti-
14885 ated, at which point a program is ill-formed if the substitution
14886 results in an invalid type.] */
14889 tsubst_function_type (tree t
,
14891 tsubst_flags_t complain
,
14895 tree arg_types
= NULL_TREE
;
14897 /* The TYPE_CONTEXT is not used for function/method types. */
14898 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14900 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14902 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14904 if (late_return_type_p
)
14906 /* Substitute the argument types. */
14907 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14908 complain
, in_decl
);
14909 if (arg_types
== error_mark_node
)
14910 return error_mark_node
;
14912 tree save_ccp
= current_class_ptr
;
14913 tree save_ccr
= current_class_ref
;
14914 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14915 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14916 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14919 /* DR 1207: 'this' is in scope in the trailing return type. */
14920 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14923 /* Substitute the return type. */
14924 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14928 current_class_ptr
= save_ccp
;
14929 current_class_ref
= save_ccr
;
14933 /* Substitute the return type. */
14934 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14936 if (return_type
== error_mark_node
)
14937 return error_mark_node
;
14938 /* DR 486 clarifies that creation of a function type with an
14939 invalid return type is a deduction failure. */
14940 if (TREE_CODE (return_type
) == ARRAY_TYPE
14941 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14943 if (complain
& tf_error
)
14945 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14946 error ("function returning an array");
14948 error ("function returning a function");
14950 return error_mark_node
;
14953 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14954 return error_mark_node
;
14956 if (!late_return_type_p
)
14958 /* Substitute the argument types. */
14959 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14960 complain
, in_decl
);
14961 if (arg_types
== error_mark_node
)
14962 return error_mark_node
;
14965 /* Construct a new type node and return it. */
14966 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
14967 /*raises=*/NULL_TREE
, complain
);
14970 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14971 ARGS into that specification, and return the substituted
14972 specification. If there is no specification, return NULL_TREE. */
14975 tsubst_exception_specification (tree fntype
,
14977 tsubst_flags_t complain
,
14984 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14985 new_specs
= NULL_TREE
;
14986 if (specs
&& TREE_PURPOSE (specs
))
14988 /* A noexcept-specifier. */
14989 tree expr
= TREE_PURPOSE (specs
);
14990 if (TREE_CODE (expr
) == INTEGER_CST
)
14994 /* Defer instantiation of noexcept-specifiers to avoid
14995 excessive instantiations (c++/49107). */
14996 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14997 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14999 /* We already partially instantiated this member template,
15000 so combine the new args with the old. */
15001 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15002 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15003 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15004 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15008 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15009 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15014 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15016 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15018 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15020 new_specs
= tsubst_copy_and_build
15021 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15022 /*integral_constant_expression_p=*/true);
15024 new_specs
= build_noexcept_spec (new_specs
, complain
);
15028 if (! TREE_VALUE (specs
))
15035 tree expanded_specs
= NULL_TREE
;
15037 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15039 /* Expand the pack expansion type. */
15040 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15044 if (expanded_specs
== error_mark_node
)
15045 return error_mark_node
;
15046 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15047 len
= TREE_VEC_LENGTH (expanded_specs
);
15050 /* We're substituting into a member template, so
15051 we got a TYPE_PACK_EXPANSION back. Add that
15052 expansion and move on. */
15053 gcc_assert (TREE_CODE (expanded_specs
)
15054 == TYPE_PACK_EXPANSION
);
15055 new_specs
= add_exception_specifier (new_specs
,
15058 specs
= TREE_CHAIN (specs
);
15063 for (i
= 0; i
< len
; ++i
)
15065 if (expanded_specs
)
15066 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15068 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15069 if (spec
== error_mark_node
)
15071 new_specs
= add_exception_specifier (new_specs
, spec
,
15075 specs
= TREE_CHAIN (specs
);
15081 /* Substitute through a TREE_LIST of types or expressions, handling pack
15085 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15087 if (t
== void_list_node
)
15090 tree purpose
= TREE_PURPOSE (t
);
15091 tree purposevec
= NULL_TREE
;
15094 else if (PACK_EXPANSION_P (purpose
))
15096 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15097 if (TREE_CODE (purpose
) == TREE_VEC
)
15098 purposevec
= purpose
;
15100 else if (TYPE_P (purpose
))
15101 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15103 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15104 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15105 return error_mark_node
;
15107 tree value
= TREE_VALUE (t
);
15108 tree valuevec
= NULL_TREE
;
15111 else if (PACK_EXPANSION_P (value
))
15113 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15114 if (TREE_CODE (value
) == TREE_VEC
)
15117 else if (TYPE_P (value
))
15118 value
= tsubst (value
, args
, complain
, in_decl
);
15120 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15121 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15122 return error_mark_node
;
15124 tree chain
= TREE_CHAIN (t
);
15127 else if (TREE_CODE (chain
) == TREE_LIST
)
15128 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15129 else if (TYPE_P (chain
))
15130 chain
= tsubst (chain
, args
, complain
, in_decl
);
15132 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15133 if (chain
== error_mark_node
)
15134 return error_mark_node
;
15136 if (purpose
== TREE_PURPOSE (t
)
15137 && value
== TREE_VALUE (t
)
15138 && chain
== TREE_CHAIN (t
))
15142 /* Determine the number of arguments. */
15145 len
= TREE_VEC_LENGTH (purposevec
);
15146 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15149 len
= TREE_VEC_LENGTH (valuevec
);
15153 for (int i
= len
; i
-- > 0; )
15156 purpose
= TREE_VEC_ELT (purposevec
, i
);
15158 value
= TREE_VEC_ELT (valuevec
, i
);
15160 if (value
&& TYPE_P (value
))
15161 chain
= hash_tree_cons (purpose
, value
, chain
);
15163 chain
= tree_cons (purpose
, value
, chain
);
15169 /* Take the tree structure T and replace template parameters used
15170 therein with the argument vector ARGS. IN_DECL is an associated
15171 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15172 Issue error and warning messages under control of COMPLAIN. Note
15173 that we must be relatively non-tolerant of extensions here, in
15174 order to preserve conformance; if we allow substitutions that
15175 should not be allowed, we may allow argument deductions that should
15176 not succeed, and therefore report ambiguous overload situations
15177 where there are none. In theory, we could allow the substitution,
15178 but indicate that it should have failed, and allow our caller to
15179 make sure that the right thing happens, but we don't try to do this
15182 This function is used for dealing with types, decls and the like;
15183 for expressions, use tsubst_expr or tsubst_copy. */
15186 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15188 enum tree_code code
;
15189 tree type
, r
= NULL_TREE
;
15191 if (t
== NULL_TREE
|| t
== error_mark_node
15192 || t
== integer_type_node
15193 || t
== void_type_node
15194 || t
== char_type_node
15195 || t
== unknown_type_node
15196 || TREE_CODE (t
) == NAMESPACE_DECL
15197 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15201 return tsubst_decl (t
, args
, complain
);
15203 if (args
== NULL_TREE
)
15206 code
= TREE_CODE (t
);
15208 if (code
== IDENTIFIER_NODE
)
15209 type
= IDENTIFIER_TYPE_VALUE (t
);
15211 type
= TREE_TYPE (t
);
15213 gcc_assert (type
!= unknown_type_node
);
15215 /* Reuse typedefs. We need to do this to handle dependent attributes,
15216 such as attribute aligned. */
15218 && typedef_variant_p (t
))
15220 tree decl
= TYPE_NAME (t
);
15222 if (alias_template_specialization_p (t
, nt_opaque
))
15224 /* DECL represents an alias template and we want to
15226 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15227 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15228 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15230 else if (DECL_CLASS_SCOPE_P (decl
)
15231 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15232 && uses_template_parms (DECL_CONTEXT (decl
)))
15234 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15235 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15236 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15238 else if (DECL_FUNCTION_SCOPE_P (decl
)
15239 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15240 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15241 r
= retrieve_local_specialization (decl
);
15243 /* The typedef is from a non-template context. */
15249 r
= cp_build_qualified_type_real
15250 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15251 complain
| tf_ignore_bad_quals
);
15256 /* We don't have an instantiation yet, so drop the typedef. */
15257 int quals
= cp_type_quals (t
);
15258 t
= DECL_ORIGINAL_TYPE (decl
);
15259 t
= cp_build_qualified_type_real (t
, quals
,
15260 complain
| tf_ignore_bad_quals
);
15264 bool fndecl_type
= (complain
& tf_fndecl_type
);
15265 complain
&= ~tf_fndecl_type
;
15268 && code
!= TYPENAME_TYPE
15269 && code
!= TEMPLATE_TYPE_PARM
15270 && code
!= TEMPLATE_PARM_INDEX
15271 && code
!= IDENTIFIER_NODE
15272 && code
!= FUNCTION_TYPE
15273 && code
!= METHOD_TYPE
)
15274 type
= tsubst (type
, args
, complain
, in_decl
);
15275 if (type
== error_mark_node
)
15276 return error_mark_node
;
15282 case ENUMERAL_TYPE
:
15283 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15284 /*entering_scope=*/0);
15287 case IDENTIFIER_NODE
:
15298 if (t
== integer_type_node
)
15301 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15302 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15306 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15308 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15309 /*integral_constant_expression_p=*/false);
15311 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15313 if (TREE_CODE (max
) == NOP_EXPR
15314 && TREE_SIDE_EFFECTS (omax
)
15315 && !TREE_TYPE (max
))
15316 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15318 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15319 with TREE_SIDE_EFFECTS that indicates this is not an integral
15320 constant expression. */
15321 if (processing_template_decl
15322 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15324 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15325 TREE_SIDE_EFFECTS (max
) = 1;
15328 return compute_array_index_type (NULL_TREE
, max
, complain
);
15331 case TEMPLATE_TYPE_PARM
:
15332 case TEMPLATE_TEMPLATE_PARM
:
15333 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15334 case TEMPLATE_PARM_INDEX
:
15339 tree arg
= NULL_TREE
;
15343 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15344 template_parm_level_and_index (t
, &level
, &idx
);
15346 levels
= TMPL_ARGS_DEPTH (args
);
15347 if (level
<= levels
15348 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15350 arg
= TMPL_ARG (args
, level
, idx
);
15352 /* See through ARGUMENT_PACK_SELECT arguments. */
15353 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15354 arg
= argument_pack_select_arg (arg
);
15357 if (arg
== error_mark_node
)
15358 return error_mark_node
;
15359 else if (arg
!= NULL_TREE
)
15361 if (ARGUMENT_PACK_P (arg
))
15362 /* If ARG is an argument pack, we don't actually want to
15363 perform a substitution here, because substitutions
15364 for argument packs are only done
15365 element-by-element. We can get to this point when
15366 substituting the type of a non-type template
15367 parameter pack, when that type actually contains
15368 template parameter packs from an outer template, e.g.,
15370 template<typename... Types> struct A {
15371 template<Types... Values> struct B { };
15375 if (code
== TEMPLATE_TYPE_PARM
)
15379 /* When building concept checks for the purpose of
15380 deducing placeholders, we can end up with wildcards
15381 where types are expected. Adjust this to the deduced
15383 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15384 arg
= TREE_TYPE (TREE_TYPE (arg
));
15386 gcc_assert (TYPE_P (arg
));
15388 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15390 return cp_build_qualified_type_real
15391 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15393 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15395 /* We are processing a type constructed from a
15396 template template parameter. */
15397 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15398 args
, complain
, in_decl
);
15399 if (argvec
== error_mark_node
)
15400 return error_mark_node
;
15402 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15403 || TREE_CODE (arg
) == TEMPLATE_DECL
15404 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15406 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15407 /* Consider this code:
15409 template <template <class> class Template>
15411 template <class Arg> using Bind = Template<Arg>;
15414 template <template <class> class Template, class Arg>
15415 using Instantiate = Template<Arg>; //#0
15417 template <template <class> class Template,
15420 Instantiate<Internal<Template>::template Bind,
15423 When #1 is parsed, the
15424 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15425 parameter `Template' in #0 matches the
15426 UNBOUND_CLASS_TEMPLATE representing the argument
15427 `Internal<Template>::template Bind'; We then want
15428 to assemble the type `Bind<Argument>' that can't
15429 be fully created right now, because
15430 `Internal<Template>' not being complete, the Bind
15431 template cannot be looked up in that context. So
15432 we need to "store" `Bind<Argument>' for later
15433 when the context of Bind becomes complete. Let's
15434 store that in a TYPENAME_TYPE. */
15435 return make_typename_type (TYPE_CONTEXT (arg
),
15436 build_nt (TEMPLATE_ID_EXPR
,
15437 TYPE_IDENTIFIER (arg
),
15442 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15443 are resolving nested-types in the signature of a
15444 member function templates. Otherwise ARG is a
15445 TEMPLATE_DECL and is the real template to be
15447 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15448 arg
= TYPE_NAME (arg
);
15450 r
= lookup_template_class (arg
,
15452 DECL_CONTEXT (arg
),
15453 /*entering_scope=*/0,
15455 return cp_build_qualified_type_real
15456 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15458 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15461 /* TEMPLATE_PARM_INDEX. */
15462 return convert_from_reference (unshare_expr (arg
));
15466 /* This can happen during the attempted tsubst'ing in
15467 unify. This means that we don't yet have any information
15468 about the template parameter in question. */
15471 /* Early in template argument deduction substitution, we don't
15472 want to reduce the level of 'auto', or it will be confused
15473 with a normal template parm in subsequent deduction.
15474 Similarly, don't reduce the level of template parameters to
15475 avoid mismatches when deducing their types. */
15476 if (complain
& tf_partial
)
15479 /* If we get here, we must have been looking at a parm for a
15480 more deeply nested template. Make a new version of this
15481 template parameter, but with a lower level. */
15484 case TEMPLATE_TYPE_PARM
:
15485 case TEMPLATE_TEMPLATE_PARM
:
15486 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15487 if (cp_type_quals (t
))
15489 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15490 r
= cp_build_qualified_type_real
15491 (r
, cp_type_quals (t
),
15492 complain
| (code
== TEMPLATE_TYPE_PARM
15493 ? tf_ignore_bad_quals
: 0));
15495 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15496 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15497 && (r
= (TEMPLATE_PARM_DESCENDANTS
15498 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15499 && (r
= TREE_TYPE (r
))
15500 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15501 /* Break infinite recursion when substituting the constraints
15502 of a constrained placeholder. */;
15503 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15504 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15505 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15506 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15507 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15508 && (TEMPLATE_PARM_LEVEL (r
)
15509 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15510 /* Cache the simple case of lowering a type parameter. */
15515 TEMPLATE_TYPE_PARM_INDEX (r
)
15516 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15517 r
, levels
, args
, complain
);
15518 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15519 TYPE_MAIN_VARIANT (r
) = r
;
15520 TYPE_POINTER_TO (r
) = NULL_TREE
;
15521 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15523 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15525 /* Propagate constraints on placeholders since they are
15526 only instantiated during satisfaction. */
15527 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15528 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15529 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15531 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15532 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15536 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15537 /* We have reduced the level of the template
15538 template parameter, but not the levels of its
15539 template parameters, so canonical_type_parameter
15540 will not be able to find the canonical template
15541 template parameter for this level. Thus, we
15542 require structural equality checking to compare
15543 TEMPLATE_TEMPLATE_PARMs. */
15544 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15545 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15546 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15548 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15550 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15552 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15553 /* We might need to substitute into the types of non-type
15554 template parameters. */
15555 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15556 complain
, in_decl
);
15557 if (tmpl
== error_mark_node
)
15558 return error_mark_node
;
15559 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15560 complain
, in_decl
);
15561 if (argvec
== error_mark_node
)
15562 return error_mark_node
;
15564 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15565 = build_template_info (tmpl
, argvec
);
15570 case TEMPLATE_PARM_INDEX
:
15571 /* OK, now substitute the type of the non-type parameter. We
15572 couldn't do it earlier because it might be an auto parameter,
15573 and we wouldn't need to if we had an argument. */
15574 type
= tsubst (type
, args
, complain
, in_decl
);
15575 if (type
== error_mark_node
)
15576 return error_mark_node
;
15577 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15581 gcc_unreachable ();
15588 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15591 /* We should never be tsubsting a binfo. */
15592 gcc_unreachable ();
15595 /* A vector of template arguments. */
15596 gcc_assert (!type
);
15597 return tsubst_template_args (t
, args
, complain
, in_decl
);
15600 case REFERENCE_TYPE
:
15602 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15607 Type deduction may fail for any of the following
15610 -- Attempting to create a pointer to reference type.
15611 -- Attempting to create a reference to a reference type or
15612 a reference to void.
15614 Core issue 106 says that creating a reference to a reference
15615 during instantiation is no longer a cause for failure. We
15616 only enforce this check in strict C++98 mode. */
15617 if ((TYPE_REF_P (type
)
15618 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15619 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15621 static location_t last_loc
;
15623 /* We keep track of the last time we issued this error
15624 message to avoid spewing a ton of messages during a
15625 single bad template instantiation. */
15626 if (complain
& tf_error
15627 && last_loc
!= input_location
)
15629 if (VOID_TYPE_P (type
))
15630 error ("forming reference to void");
15631 else if (code
== POINTER_TYPE
)
15632 error ("forming pointer to reference type %qT", type
);
15634 error ("forming reference to reference type %qT", type
);
15635 last_loc
= input_location
;
15638 return error_mark_node
;
15640 else if (TREE_CODE (type
) == FUNCTION_TYPE
15641 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15642 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15644 if (complain
& tf_error
)
15646 if (code
== POINTER_TYPE
)
15647 error ("forming pointer to qualified function type %qT",
15650 error ("forming reference to qualified function type %qT",
15653 return error_mark_node
;
15655 else if (code
== POINTER_TYPE
)
15657 r
= build_pointer_type (type
);
15658 if (TREE_CODE (type
) == METHOD_TYPE
)
15659 r
= build_ptrmemfunc_type (r
);
15661 else if (TYPE_REF_P (type
))
15662 /* In C++0x, during template argument substitution, when there is an
15663 attempt to create a reference to a reference type, reference
15664 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15666 "If a template-argument for a template-parameter T names a type
15667 that is a reference to a type A, an attempt to create the type
15668 'lvalue reference to cv T' creates the type 'lvalue reference to
15669 A,' while an attempt to create the type type rvalue reference to
15670 cv T' creates the type T"
15672 r
= cp_build_reference_type
15674 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15676 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15677 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15679 if (r
!= error_mark_node
)
15680 /* Will this ever be needed for TYPE_..._TO values? */
15687 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15688 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15692 Type deduction may fail for any of the following
15695 -- Attempting to create "pointer to member of T" when T
15696 is not a class type. */
15697 if (complain
& tf_error
)
15698 error ("creating pointer to member of non-class type %qT", r
);
15699 return error_mark_node
;
15701 if (TYPE_REF_P (type
))
15703 if (complain
& tf_error
)
15704 error ("creating pointer to member reference type %qT", type
);
15705 return error_mark_node
;
15707 if (VOID_TYPE_P (type
))
15709 if (complain
& tf_error
)
15710 error ("creating pointer to member of type void");
15711 return error_mark_node
;
15713 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15714 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15716 /* The type of the implicit object parameter gets its
15717 cv-qualifiers from the FUNCTION_TYPE. */
15720 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15721 type_memfn_rqual (type
));
15722 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15723 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15727 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15731 case FUNCTION_TYPE
:
15736 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15737 if (fntype
== error_mark_node
)
15738 return error_mark_node
;
15740 /* Substitute the exception specification. */
15741 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15742 /*defer_ok*/fndecl_type
);
15743 if (specs
== error_mark_node
)
15744 return error_mark_node
;
15746 fntype
= build_exception_variant (fntype
, specs
);
15751 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15752 if (domain
== error_mark_node
)
15753 return error_mark_node
;
15755 /* As an optimization, we avoid regenerating the array type if
15756 it will obviously be the same as T. */
15757 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15760 /* These checks should match the ones in create_array_type_for_decl.
15764 The deduction may fail for any of the following reasons:
15766 -- Attempting to create an array with an element type that
15767 is void, a function type, or a reference type, or [DR337]
15768 an abstract class type. */
15769 if (VOID_TYPE_P (type
)
15770 || TREE_CODE (type
) == FUNCTION_TYPE
15771 || (TREE_CODE (type
) == ARRAY_TYPE
15772 && TYPE_DOMAIN (type
) == NULL_TREE
)
15773 || TYPE_REF_P (type
))
15775 if (complain
& tf_error
)
15776 error ("creating array of %qT", type
);
15777 return error_mark_node
;
15780 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15781 return error_mark_node
;
15783 r
= build_cplus_array_type (type
, domain
);
15785 if (!valid_array_size_p (input_location
, r
, in_decl
,
15786 (complain
& tf_error
)))
15787 return error_mark_node
;
15789 if (TYPE_USER_ALIGN (t
))
15791 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15792 TYPE_USER_ALIGN (r
) = 1;
15798 case TYPENAME_TYPE
:
15800 tree ctx
= TYPE_CONTEXT (t
);
15801 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15803 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15804 if (ctx
== error_mark_node
15805 || TREE_VEC_LENGTH (ctx
) > 1)
15806 return error_mark_node
;
15807 if (TREE_VEC_LENGTH (ctx
) == 0)
15809 if (complain
& tf_error
)
15810 error ("%qD is instantiated for an empty pack",
15811 TYPENAME_TYPE_FULLNAME (t
));
15812 return error_mark_node
;
15814 ctx
= TREE_VEC_ELT (ctx
, 0);
15817 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15818 /*entering_scope=*/1);
15819 if (ctx
== error_mark_node
)
15820 return error_mark_node
;
15822 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15823 complain
, in_decl
);
15824 if (f
== error_mark_node
)
15825 return error_mark_node
;
15827 if (!MAYBE_CLASS_TYPE_P (ctx
))
15829 if (complain
& tf_error
)
15830 error ("%qT is not a class, struct, or union type", ctx
);
15831 return error_mark_node
;
15833 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15835 /* Normally, make_typename_type does not require that the CTX
15836 have complete type in order to allow things like:
15838 template <class T> struct S { typename S<T>::X Y; };
15840 But, such constructs have already been resolved by this
15841 point, so here CTX really should have complete type, unless
15842 it's a partial instantiation. */
15843 ctx
= complete_type (ctx
);
15844 if (!COMPLETE_TYPE_P (ctx
))
15846 if (complain
& tf_error
)
15847 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15848 return error_mark_node
;
15852 f
= make_typename_type (ctx
, f
, typename_type
,
15853 complain
| tf_keep_type_decl
);
15854 if (f
== error_mark_node
)
15856 if (TREE_CODE (f
) == TYPE_DECL
)
15858 complain
|= tf_ignore_bad_quals
;
15862 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15864 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15866 if (complain
& tf_error
)
15867 error ("%qT resolves to %qT, which is not an enumeration type",
15870 return error_mark_node
;
15872 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15874 if (complain
& tf_error
)
15875 error ("%qT resolves to %qT, which is not a class type",
15878 return error_mark_node
;
15882 return cp_build_qualified_type_real
15883 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15886 case UNBOUND_CLASS_TEMPLATE
:
15888 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15889 in_decl
, /*entering_scope=*/1);
15890 tree name
= TYPE_IDENTIFIER (t
);
15891 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15893 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15894 return error_mark_node
;
15897 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15898 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15905 ++cp_unevaluated_operand
;
15906 ++c_inhibit_evaluation_warnings
;
15908 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15910 /*integral_constant_expression_p=*/false);
15912 --cp_unevaluated_operand
;
15913 --c_inhibit_evaluation_warnings
;
15915 type
= finish_typeof (type
);
15916 return cp_build_qualified_type_real (type
,
15918 | cp_type_quals (type
),
15922 case DECLTYPE_TYPE
:
15926 ++cp_unevaluated_operand
;
15927 ++c_inhibit_evaluation_warnings
;
15929 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15930 complain
|tf_decltype
, in_decl
,
15931 /*function_p*/false,
15932 /*integral_constant_expression*/false);
15934 --cp_unevaluated_operand
;
15935 --c_inhibit_evaluation_warnings
;
15937 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15938 type
= lambda_capture_field_type (type
,
15939 false /*explicit_init*/,
15940 DECLTYPE_FOR_REF_CAPTURE (t
));
15941 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15942 type
= lambda_proxy_type (type
);
15945 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15946 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15948 /* In a template ~id could be either a complement expression
15949 or an unqualified-id naming a destructor; if instantiating
15950 it produces an expression, it's not an id-expression or
15953 type
= finish_decltype_type (type
, id
, complain
);
15955 return cp_build_qualified_type_real (type
,
15957 | cp_type_quals (type
),
15958 complain
| tf_ignore_bad_quals
);
15961 case UNDERLYING_TYPE
:
15963 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15964 complain
, in_decl
);
15965 return finish_underlying_type (type
);
15968 case TYPE_ARGUMENT_PACK
:
15969 case NONTYPE_ARGUMENT_PACK
:
15973 if (code
== NONTYPE_ARGUMENT_PACK
)
15974 r
= make_node (code
);
15976 r
= cxx_make_type (code
);
15978 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15979 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15980 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15998 /* We should use one of the expression tsubsts for these codes. */
15999 gcc_unreachable ();
16002 sorry ("use of %qs in template", get_tree_code_name (code
));
16003 return error_mark_node
;
16007 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16008 expression on the left-hand side of the "." or "->" operator. We
16009 only do the lookup if we had a dependent BASELINK. Otherwise we
16010 adjust it onto the instantiated heirarchy. */
16013 tsubst_baselink (tree baselink
, tree object_type
,
16014 tree args
, tsubst_flags_t complain
, tree in_decl
)
16016 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16017 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16018 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16020 tree optype
= BASELINK_OPTYPE (baselink
);
16021 optype
= tsubst (optype
, args
, complain
, in_decl
);
16023 tree template_args
= NULL_TREE
;
16024 bool template_id_p
= false;
16025 tree fns
= BASELINK_FUNCTIONS (baselink
);
16026 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16028 template_id_p
= true;
16029 template_args
= TREE_OPERAND (fns
, 1);
16030 fns
= TREE_OPERAND (fns
, 0);
16032 template_args
= tsubst_template_args (template_args
, args
,
16033 complain
, in_decl
);
16036 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16037 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16038 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16042 tree name
= OVL_NAME (fns
);
16043 if (IDENTIFIER_CONV_OP_P (name
))
16044 name
= make_conv_op_name (optype
);
16046 if (name
== complete_dtor_identifier
)
16047 /* Treat as-if non-dependent below. */
16048 dependent_p
= false;
16050 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16054 if ((complain
& tf_error
)
16055 && constructor_name_p (name
, qualifying_scope
))
16056 error ("cannot call constructor %<%T::%D%> directly",
16057 qualifying_scope
, name
);
16058 return error_mark_node
;
16061 if (BASELINK_P (baselink
))
16062 fns
= BASELINK_FUNCTIONS (baselink
);
16065 /* We're going to overwrite pieces below, make a duplicate. */
16066 baselink
= copy_node (baselink
);
16068 /* If lookup found a single function, mark it as used at this point.
16069 (If lookup found multiple functions the one selected later by
16070 overload resolution will be marked as used at that point.) */
16071 if (!template_id_p
&& !really_overloaded_fn (fns
))
16073 tree fn
= OVL_FIRST (fns
);
16074 bool ok
= mark_used (fn
, complain
);
16075 if (!ok
&& !(complain
& tf_error
))
16076 return error_mark_node
;
16077 if (ok
&& BASELINK_P (baselink
))
16078 /* We might have instantiated an auto function. */
16079 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16082 if (BASELINK_P (baselink
))
16084 /* Add back the template arguments, if present. */
16086 BASELINK_FUNCTIONS (baselink
)
16087 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16089 /* Update the conversion operator type. */
16090 BASELINK_OPTYPE (baselink
) = optype
;
16094 object_type
= current_class_type
;
16096 if (qualified_p
|| !dependent_p
)
16098 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16102 /* We need to call adjust_result_of_qualified_name_lookup in case the
16103 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16104 so that we still get virtual function binding. */
16105 BASELINK_QUALIFIED_P (baselink
) = false;
16111 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16112 true if the qualified-id will be a postfix-expression in-and-of
16113 itself; false if more of the postfix-expression follows the
16114 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16118 tsubst_qualified_id (tree qualified_id
, tree args
,
16119 tsubst_flags_t complain
, tree in_decl
,
16120 bool done
, bool address_p
)
16126 tree template_args
;
16127 location_t loc
= UNKNOWN_LOCATION
;
16129 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16131 /* Figure out what name to look up. */
16132 name
= TREE_OPERAND (qualified_id
, 1);
16133 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16135 is_template
= true;
16136 loc
= EXPR_LOCATION (name
);
16137 template_args
= TREE_OPERAND (name
, 1);
16139 template_args
= tsubst_template_args (template_args
, args
,
16140 complain
, in_decl
);
16141 if (template_args
== error_mark_node
)
16142 return error_mark_node
;
16143 name
= TREE_OPERAND (name
, 0);
16147 is_template
= false;
16148 template_args
= NULL_TREE
;
16151 /* Substitute into the qualifying scope. When there are no ARGS, we
16152 are just trying to simplify a non-dependent expression. In that
16153 case the qualifying scope may be dependent, and, in any case,
16154 substituting will not help. */
16155 scope
= TREE_OPERAND (qualified_id
, 0);
16158 scope
= tsubst (scope
, args
, complain
, in_decl
);
16159 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16164 if (dependent_scope_p (scope
))
16167 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16168 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16169 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16170 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16174 if (!BASELINK_P (name
) && !DECL_P (expr
))
16176 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16178 /* A BIT_NOT_EXPR is used to represent a destructor. */
16179 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16181 error ("qualifying type %qT does not match destructor name ~%qT",
16182 scope
, TREE_OPERAND (expr
, 0));
16183 expr
= error_mark_node
;
16186 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16187 /*is_type_p=*/0, false);
16190 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
16191 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16192 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16194 if (complain
& tf_error
)
16196 error ("dependent-name %qE is parsed as a non-type, but "
16197 "instantiation yields a type", qualified_id
);
16198 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16200 return error_mark_node
;
16206 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16208 return error_mark_node
;
16209 /* Remember that there was a reference to this entity. */
16210 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16211 return error_mark_node
;
16214 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16216 if (complain
& tf_error
)
16217 qualified_name_lookup_error (scope
,
16218 TREE_OPERAND (qualified_id
, 1),
16219 expr
, input_location
);
16220 return error_mark_node
;
16225 /* We may be repeating a check already done during parsing, but
16226 if it was well-formed and passed then, it will pass again
16227 now, and if it didn't, we wouldn't have got here. The case
16228 we want to catch is when we couldn't tell then, and can now,
16229 namely when templ prior to substitution was an
16231 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16232 return error_mark_node
;
16234 if (variable_template_p (expr
))
16235 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16238 expr
= lookup_template_function (expr
, template_args
);
16241 if (expr
== error_mark_node
&& complain
& tf_error
)
16242 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16243 expr
, input_location
);
16244 else if (TYPE_P (scope
))
16246 expr
= (adjust_result_of_qualified_name_lookup
16247 (expr
, scope
, current_nonlambda_class_type ()));
16248 expr
= (finish_qualified_id_expr
16249 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16250 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16251 /*template_arg_p=*/false, complain
));
16254 /* Expressions do not generally have reference type. */
16255 if (TREE_CODE (expr
) != SCOPE_REF
16256 /* However, if we're about to form a pointer-to-member, we just
16257 want the referenced member referenced. */
16258 && TREE_CODE (expr
) != OFFSET_REF
)
16259 expr
= convert_from_reference (expr
);
16261 if (REF_PARENTHESIZED_P (qualified_id
))
16262 expr
= force_paren_expr (expr
);
16267 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16268 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16272 tsubst_init (tree init
, tree decl
, tree args
,
16273 tsubst_flags_t complain
, tree in_decl
)
16278 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16280 tree type
= TREE_TYPE (decl
);
16282 if (!init
&& type
!= error_mark_node
)
16284 if (tree auto_node
= type_uses_auto (type
))
16286 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16288 if (complain
& tf_error
)
16289 error ("initializer for %q#D expands to an empty list "
16290 "of expressions", decl
);
16291 return error_mark_node
;
16294 else if (!dependent_type_p (type
))
16296 /* If we had an initializer but it
16297 instantiated to nothing,
16298 value-initialize the object. This will
16299 only occur when the initializer was a
16300 pack expansion where the parameter packs
16301 used in that expansion were of length
16303 init
= build_value_init (type
, complain
);
16304 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16305 init
= get_target_expr_sfinae (init
, complain
);
16306 if (TREE_CODE (init
) == TARGET_EXPR
)
16307 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16314 /* Like tsubst, but deals with expressions. This function just replaces
16315 template parms; to finish processing the resultant expression, use
16316 tsubst_copy_and_build or tsubst_expr. */
16319 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16321 enum tree_code code
;
16324 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16327 code
= TREE_CODE (t
);
16332 r
= retrieve_local_specialization (t
);
16334 if (r
== NULL_TREE
)
16336 /* We get here for a use of 'this' in an NSDMI. */
16337 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16338 return current_class_ptr
;
16340 /* This can happen for a parameter name used later in a function
16341 declaration (such as in a late-specified return type). Just
16342 make a dummy decl, since it's only used for its type. */
16343 gcc_assert (cp_unevaluated_operand
!= 0);
16344 r
= tsubst_decl (t
, args
, complain
);
16345 /* Give it the template pattern as its context; its true context
16346 hasn't been instantiated yet and this is good enough for
16348 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16351 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16352 r
= argument_pack_select_arg (r
);
16353 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16354 return error_mark_node
;
16362 if (DECL_TEMPLATE_PARM_P (t
))
16363 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16364 /* There is no need to substitute into namespace-scope
16366 if (DECL_NAMESPACE_SCOPE_P (t
))
16368 /* If ARGS is NULL, then T is known to be non-dependent. */
16369 if (args
== NULL_TREE
)
16370 return scalar_constant_value (t
);
16372 /* Unfortunately, we cannot just call lookup_name here.
16375 template <int I> int f() {
16377 struct S { void g() { E e = a; } };
16380 When we instantiate f<7>::S::g(), say, lookup_name is not
16381 clever enough to find f<7>::a. */
16383 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16384 /*entering_scope=*/0);
16386 for (v
= TYPE_VALUES (enum_type
);
16388 v
= TREE_CHAIN (v
))
16389 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16390 return TREE_VALUE (v
);
16392 /* We didn't find the name. That should never happen; if
16393 name-lookup found it during preliminary parsing, we
16394 should find it again here during instantiation. */
16395 gcc_unreachable ();
16400 if (DECL_CONTEXT (t
))
16404 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16405 /*entering_scope=*/1);
16406 if (ctx
!= DECL_CONTEXT (t
))
16408 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16411 if (complain
& tf_error
)
16412 error ("using invalid field %qD", t
);
16413 return error_mark_node
;
16422 case FUNCTION_DECL
:
16423 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16424 r
= tsubst (t
, args
, complain
, in_decl
);
16425 else if (local_variable_p (t
)
16426 && uses_template_parms (DECL_CONTEXT (t
)))
16428 r
= retrieve_local_specialization (t
);
16429 if (r
== NULL_TREE
)
16431 /* First try name lookup to find the instantiation. */
16432 r
= lookup_name (DECL_NAME (t
));
16437 /* During error-recovery we may find a non-variable,
16438 even an OVERLOAD: just bail out and avoid ICEs and
16439 duplicate diagnostics (c++/62207). */
16440 gcc_assert (seen_error ());
16441 return error_mark_node
;
16443 if (!is_capture_proxy (r
))
16445 /* Make sure the one we found is the one we want. */
16446 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16447 if (ctx
!= DECL_CONTEXT (r
))
16456 /* This can happen for a variable used in a
16457 late-specified return type of a local lambda, or for a
16458 local static or constant. Building a new VAR_DECL
16459 should be OK in all those cases. */
16460 r
= tsubst_decl (t
, args
, complain
);
16461 if (local_specializations
)
16462 /* Avoid infinite recursion (79640). */
16463 register_local_specialization (r
, t
);
16464 if (decl_maybe_constant_var_p (r
))
16466 /* We can't call cp_finish_decl, so handle the
16467 initializer by hand. */
16468 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16469 complain
, in_decl
);
16470 if (!processing_template_decl
)
16471 init
= maybe_constant_init (init
);
16472 if (processing_template_decl
16473 ? potential_constant_expression (init
)
16474 : reduced_constant_expression_p (init
))
16475 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16476 = TREE_CONSTANT (r
) = true;
16477 DECL_INITIAL (r
) = init
;
16478 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16480 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16481 complain
, adc_variable_type
);
16483 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16484 || decl_constant_var_p (r
)
16486 if (!processing_template_decl
16487 && !TREE_STATIC (r
))
16488 r
= process_outer_var_ref (r
, complain
);
16490 /* Remember this for subsequent uses. */
16491 if (local_specializations
)
16492 register_local_specialization (r
, t
);
16494 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16495 r
= argument_pack_select_arg (r
);
16499 if (!mark_used (r
, complain
))
16500 return error_mark_node
;
16503 case NAMESPACE_DECL
:
16510 return tsubst_baselink (t
, current_nonlambda_class_type (),
16511 args
, complain
, in_decl
);
16513 case TEMPLATE_DECL
:
16514 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16515 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16516 args
, complain
, in_decl
);
16517 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16518 return tsubst (t
, args
, complain
, in_decl
);
16519 else if (DECL_CLASS_SCOPE_P (t
)
16520 && uses_template_parms (DECL_CONTEXT (t
)))
16522 /* Template template argument like the following example need
16525 template <template <class> class TT> struct C {};
16526 template <class T> struct D {
16527 template <class U> struct E {};
16532 We are processing the template argument `E' in #1 for
16533 the template instantiation #2. Originally, `E' is a
16534 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16535 have to substitute this with one having context `D<int>'. */
16537 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16538 if (dependent_scope_p (context
))
16540 /* When rewriting a constructor into a deduction guide, a
16541 non-dependent name can become dependent, so memtmpl<args>
16542 becomes context::template memtmpl<args>. */
16543 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16544 return build_qualified_name (type
, context
, DECL_NAME (t
),
16547 return lookup_field (context
, DECL_NAME(t
), 0, false);
16550 /* Ordinary template template argument. */
16553 case NON_LVALUE_EXPR
:
16554 case VIEW_CONVERT_EXPR
:
16556 /* Handle location wrappers by substituting the wrapped node
16557 first, *then* reusing the resulting type. Doing the type
16558 first ensures that we handle template parameters and
16559 parameter pack expansions. */
16560 if (location_wrapper_p (t
))
16562 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16563 complain
, in_decl
);
16564 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16566 tree op
= TREE_OPERAND (t
, 0);
16567 if (code
== VIEW_CONVERT_EXPR
16568 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16570 /* Wrapper to make a C++20 template parameter object const. */
16571 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16572 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16574 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16575 return build1 (code
, type
, op
);
16579 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16580 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16581 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16585 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16586 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16588 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16589 op
= build1 (code
, TREE_TYPE (op
), op
);
16590 REF_PARENTHESIZED_P (op
) = true;
16593 /* We shouldn't see any other uses of these in templates. */
16594 gcc_unreachable ();
16598 case REINTERPRET_CAST_EXPR
:
16599 case CONST_CAST_EXPR
:
16600 case STATIC_CAST_EXPR
:
16601 case DYNAMIC_CAST_EXPR
:
16602 case IMPLICIT_CONV_EXPR
:
16606 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16607 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16608 return build1 (code
, type
, op0
);
16612 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16613 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16615 tree expanded
, op
= TREE_OPERAND (t
, 0);
16618 if (SIZEOF_EXPR_TYPE_P (t
))
16619 op
= TREE_TYPE (op
);
16621 ++cp_unevaluated_operand
;
16622 ++c_inhibit_evaluation_warnings
;
16623 /* We only want to compute the number of arguments. */
16624 if (PACK_EXPANSION_P (op
))
16625 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16627 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16628 args
, complain
, in_decl
);
16629 --cp_unevaluated_operand
;
16630 --c_inhibit_evaluation_warnings
;
16632 if (TREE_CODE (expanded
) == TREE_VEC
)
16634 len
= TREE_VEC_LENGTH (expanded
);
16635 /* Set TREE_USED for the benefit of -Wunused. */
16636 for (int i
= 0; i
< len
; i
++)
16637 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16638 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16641 if (expanded
== error_mark_node
)
16642 return error_mark_node
;
16643 else if (PACK_EXPANSION_P (expanded
)
16644 || (TREE_CODE (expanded
) == TREE_VEC
16645 && pack_expansion_args_count (expanded
)))
16648 if (PACK_EXPANSION_P (expanded
))
16650 else if (TREE_VEC_LENGTH (expanded
) == 1)
16651 expanded
= TREE_VEC_ELT (expanded
, 0);
16653 expanded
= make_argument_pack (expanded
);
16655 if (TYPE_P (expanded
))
16656 return cxx_sizeof_or_alignof_type (input_location
,
16657 expanded
, SIZEOF_EXPR
,
16659 complain
& tf_error
);
16661 return cxx_sizeof_or_alignof_expr (input_location
,
16662 expanded
, SIZEOF_EXPR
,
16663 complain
& tf_error
);
16666 return build_int_cst (size_type_node
, len
);
16668 if (SIZEOF_EXPR_TYPE_P (t
))
16670 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16671 args
, complain
, in_decl
);
16672 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16673 r
= build1 (SIZEOF_EXPR
,
16674 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16675 SIZEOF_EXPR_TYPE_P (r
) = 1;
16682 case TRUTH_NOT_EXPR
:
16685 case UNARY_PLUS_EXPR
: /* Unary + */
16687 case AT_ENCODE_EXPR
:
16691 case REALPART_EXPR
:
16692 case IMAGPART_EXPR
:
16695 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16696 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16697 r
= build1 (code
, type
, op0
);
16698 if (code
== ALIGNOF_EXPR
)
16699 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16703 case COMPONENT_REF
:
16708 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16709 name
= TREE_OPERAND (t
, 1);
16710 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16712 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16713 complain
, in_decl
);
16714 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16716 else if (TREE_CODE (name
) == SCOPE_REF
16717 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16719 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16720 complain
, in_decl
);
16721 name
= TREE_OPERAND (name
, 1);
16722 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16723 complain
, in_decl
);
16724 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16725 name
= build_qualified_name (/*type=*/NULL_TREE
,
16727 /*template_p=*/false);
16729 else if (BASELINK_P (name
))
16730 name
= tsubst_baselink (name
,
16731 non_reference (TREE_TYPE (object
)),
16735 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16736 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16742 case TRUNC_DIV_EXPR
:
16743 case CEIL_DIV_EXPR
:
16744 case FLOOR_DIV_EXPR
:
16745 case ROUND_DIV_EXPR
:
16746 case EXACT_DIV_EXPR
:
16750 case TRUNC_MOD_EXPR
:
16751 case FLOOR_MOD_EXPR
:
16752 case TRUTH_ANDIF_EXPR
:
16753 case TRUTH_ORIF_EXPR
:
16754 case TRUTH_AND_EXPR
:
16755 case TRUTH_OR_EXPR
:
16766 case COMPOUND_EXPR
:
16769 case PREDECREMENT_EXPR
:
16770 case PREINCREMENT_EXPR
:
16771 case POSTDECREMENT_EXPR
:
16772 case POSTINCREMENT_EXPR
:
16774 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16775 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16776 return build_nt (code
, op0
, op1
);
16781 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16782 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16783 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16784 QUALIFIED_NAME_IS_TEMPLATE (t
));
16789 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16790 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16791 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16796 int n
= VL_EXP_OPERAND_LENGTH (t
);
16797 tree result
= build_vl_exp (CALL_EXPR
, n
);
16799 for (i
= 0; i
< n
; i
++)
16800 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16801 complain
, in_decl
);
16807 case PSEUDO_DTOR_EXPR
:
16808 case VEC_PERM_EXPR
:
16810 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16811 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16812 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16813 r
= build_nt (code
, op0
, op1
, op2
);
16814 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16820 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16821 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16822 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16823 r
= build_nt (code
, op0
, op1
, op2
);
16824 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16830 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16831 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16832 r
= build_nt (code
, op0
, op1
);
16833 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16834 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16838 case TEMPLATE_ID_EXPR
:
16840 /* Substituted template arguments */
16841 tree fn
= TREE_OPERAND (t
, 0);
16842 tree targs
= TREE_OPERAND (t
, 1);
16844 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16846 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16848 return lookup_template_function (fn
, targs
);
16853 tree purpose
, value
, chain
;
16855 if (t
== void_list_node
)
16858 purpose
= TREE_PURPOSE (t
);
16860 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16861 value
= TREE_VALUE (t
);
16863 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16864 chain
= TREE_CHAIN (t
);
16865 if (chain
&& chain
!= void_type_node
)
16866 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16867 if (purpose
== TREE_PURPOSE (t
)
16868 && value
== TREE_VALUE (t
)
16869 && chain
== TREE_CHAIN (t
))
16871 return tree_cons (purpose
, value
, chain
);
16876 case ENUMERAL_TYPE
:
16878 case TEMPLATE_TYPE_PARM
:
16879 case TEMPLATE_TEMPLATE_PARM
:
16880 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16881 case TEMPLATE_PARM_INDEX
:
16883 case REFERENCE_TYPE
:
16885 case FUNCTION_TYPE
:
16888 case TYPENAME_TYPE
:
16889 case UNBOUND_CLASS_TEMPLATE
:
16891 case DECLTYPE_TYPE
:
16893 return tsubst (t
, args
, complain
, in_decl
);
16897 /* Fall through. */
16898 case IDENTIFIER_NODE
:
16899 if (IDENTIFIER_CONV_OP_P (t
))
16901 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16902 return make_conv_op_name (new_type
);
16908 /* This is handled by tsubst_copy_and_build. */
16909 gcc_unreachable ();
16913 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16914 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16915 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16918 case CLEANUP_POINT_EXPR
:
16919 /* We shouldn't have built any of these during initial template
16920 generation. Instead, they should be built during instantiation
16921 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16922 gcc_unreachable ();
16926 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16927 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16928 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16929 r
= build2 (code
, type
, op0
, op1
);
16930 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16931 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16932 && !(complain
& tf_error
))
16933 return error_mark_node
;
16937 case EXPR_PACK_EXPANSION
:
16938 error ("invalid use of pack expansion expression");
16939 return error_mark_node
;
16941 case NONTYPE_ARGUMENT_PACK
:
16942 error ("use %<...%> to expand argument pack");
16943 return error_mark_node
;
16946 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16953 /* Instantiate any typedefs in the type. */
16954 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16955 r
= fold_convert (type
, t
);
16956 gcc_assert (TREE_CODE (r
) == code
);
16962 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16964 if (type
!= TREE_TYPE (t
))
16967 TREE_TYPE (r
) = type
;
16973 /* These can sometimes show up in a partial instantiation, but never
16974 involve template parms. */
16975 gcc_assert (!uses_template_parms (t
));
16978 case UNARY_LEFT_FOLD_EXPR
:
16979 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16980 case UNARY_RIGHT_FOLD_EXPR
:
16981 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16982 case BINARY_LEFT_FOLD_EXPR
:
16983 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16984 case BINARY_RIGHT_FOLD_EXPR
:
16985 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16989 case DEBUG_BEGIN_STMT
:
16990 /* ??? There's no point in copying it for now, but maybe some
16991 day it will contain more information, such as a pointer back
16992 to the containing function, inlined copy or so. */
16995 case CO_AWAIT_EXPR
:
16996 return tsubst_expr (t
, args
, complain
, in_decl
,
16997 /*integral_constant_expression_p=*/false);
17001 /* We shouldn't get here, but keep going if !flag_checking. */
17003 gcc_unreachable ();
17008 /* Helper function for tsubst_omp_clauses, used for instantiation of
17009 OMP_CLAUSE_DECL of clauses. */
17012 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17013 tree in_decl
, tree
*iterator_cache
)
17015 if (decl
== NULL_TREE
)
17018 /* Handle OpenMP iterators. */
17019 if (TREE_CODE (decl
) == TREE_LIST
17020 && TREE_PURPOSE (decl
)
17021 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17024 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17025 ret
= iterator_cache
[1];
17029 begin_scope (sk_omp
, NULL
);
17030 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17032 *tp
= copy_node (it
);
17033 TREE_VEC_ELT (*tp
, 0)
17034 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17035 TREE_VEC_ELT (*tp
, 1)
17036 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17037 /*integral_constant_expression_p=*/false);
17038 TREE_VEC_ELT (*tp
, 2)
17039 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17040 /*integral_constant_expression_p=*/false);
17041 TREE_VEC_ELT (*tp
, 3)
17042 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17043 /*integral_constant_expression_p=*/false);
17044 TREE_CHAIN (*tp
) = NULL_TREE
;
17045 tp
= &TREE_CHAIN (*tp
);
17047 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17048 iterator_cache
[0] = TREE_PURPOSE (decl
);
17049 iterator_cache
[1] = ret
;
17051 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17056 /* Handle an OpenMP array section represented as a TREE_LIST (or
17057 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17058 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17059 TREE_LIST. We can handle it exactly the same as an array section
17060 (purpose, value, and a chain), even though the nomenclature
17061 (low_bound, length, etc) is different. */
17062 if (TREE_CODE (decl
) == TREE_LIST
)
17065 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17066 /*integral_constant_expression_p=*/false);
17067 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17068 /*integral_constant_expression_p=*/false);
17069 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17071 if (TREE_PURPOSE (decl
) == low_bound
17072 && TREE_VALUE (decl
) == length
17073 && TREE_CHAIN (decl
) == chain
)
17075 tree ret
= tree_cons (low_bound
, length
, chain
);
17076 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17077 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17080 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17081 /*integral_constant_expression_p=*/false);
17082 /* Undo convert_from_reference tsubst_expr could have called. */
17084 && REFERENCE_REF_P (ret
)
17085 && !REFERENCE_REF_P (decl
))
17086 ret
= TREE_OPERAND (ret
, 0);
17090 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17093 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17094 tree args
, tsubst_flags_t complain
, tree in_decl
)
17096 tree new_clauses
= NULL_TREE
, nc
, oc
;
17097 tree linear_no_step
= NULL_TREE
;
17098 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17100 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17102 nc
= copy_node (oc
);
17103 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17106 switch (OMP_CLAUSE_CODE (nc
))
17108 case OMP_CLAUSE_LASTPRIVATE
:
17109 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17111 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17112 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17113 in_decl
, /*integral_constant_expression_p=*/false);
17114 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17115 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17118 case OMP_CLAUSE_PRIVATE
:
17119 case OMP_CLAUSE_SHARED
:
17120 case OMP_CLAUSE_FIRSTPRIVATE
:
17121 case OMP_CLAUSE_COPYIN
:
17122 case OMP_CLAUSE_COPYPRIVATE
:
17123 case OMP_CLAUSE_UNIFORM
:
17124 case OMP_CLAUSE_DEPEND
:
17125 case OMP_CLAUSE_FROM
:
17126 case OMP_CLAUSE_TO
:
17127 case OMP_CLAUSE_MAP
:
17128 case OMP_CLAUSE_NONTEMPORAL
:
17129 case OMP_CLAUSE_USE_DEVICE_PTR
:
17130 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17131 case OMP_CLAUSE_IS_DEVICE_PTR
:
17132 case OMP_CLAUSE_INCLUSIVE
:
17133 case OMP_CLAUSE_EXCLUSIVE
:
17134 OMP_CLAUSE_DECL (nc
)
17135 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17136 in_decl
, iterator_cache
);
17138 case OMP_CLAUSE_TILE
:
17139 case OMP_CLAUSE_IF
:
17140 case OMP_CLAUSE_NUM_THREADS
:
17141 case OMP_CLAUSE_SCHEDULE
:
17142 case OMP_CLAUSE_COLLAPSE
:
17143 case OMP_CLAUSE_FINAL
:
17144 case OMP_CLAUSE_DEVICE
:
17145 case OMP_CLAUSE_DIST_SCHEDULE
:
17146 case OMP_CLAUSE_NUM_TEAMS
:
17147 case OMP_CLAUSE_THREAD_LIMIT
:
17148 case OMP_CLAUSE_SAFELEN
:
17149 case OMP_CLAUSE_SIMDLEN
:
17150 case OMP_CLAUSE_NUM_TASKS
:
17151 case OMP_CLAUSE_GRAINSIZE
:
17152 case OMP_CLAUSE_PRIORITY
:
17153 case OMP_CLAUSE_ORDERED
:
17154 case OMP_CLAUSE_HINT
:
17155 case OMP_CLAUSE_NUM_GANGS
:
17156 case OMP_CLAUSE_NUM_WORKERS
:
17157 case OMP_CLAUSE_VECTOR_LENGTH
:
17158 case OMP_CLAUSE_WORKER
:
17159 case OMP_CLAUSE_VECTOR
:
17160 case OMP_CLAUSE_ASYNC
:
17161 case OMP_CLAUSE_WAIT
:
17162 OMP_CLAUSE_OPERAND (nc
, 0)
17163 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17164 in_decl
, /*integral_constant_expression_p=*/false);
17166 case OMP_CLAUSE_REDUCTION
:
17167 case OMP_CLAUSE_IN_REDUCTION
:
17168 case OMP_CLAUSE_TASK_REDUCTION
:
17169 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17171 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17172 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17174 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17175 complain
, in_decl
);
17176 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17177 = build_qualified_name (NULL_TREE
, scope
,
17178 TREE_OPERAND (placeholder
, 1),
17182 gcc_assert (identifier_p (placeholder
));
17184 OMP_CLAUSE_DECL (nc
)
17185 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17188 case OMP_CLAUSE_GANG
:
17189 case OMP_CLAUSE_ALIGNED
:
17190 OMP_CLAUSE_DECL (nc
)
17191 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17193 OMP_CLAUSE_OPERAND (nc
, 1)
17194 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17195 in_decl
, /*integral_constant_expression_p=*/false);
17197 case OMP_CLAUSE_LINEAR
:
17198 OMP_CLAUSE_DECL (nc
)
17199 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17201 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17203 gcc_assert (!linear_no_step
);
17204 linear_no_step
= nc
;
17206 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17207 OMP_CLAUSE_LINEAR_STEP (nc
)
17208 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17209 complain
, in_decl
, NULL
);
17211 OMP_CLAUSE_LINEAR_STEP (nc
)
17212 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17214 /*integral_constant_expression_p=*/false);
17216 case OMP_CLAUSE_NOWAIT
:
17217 case OMP_CLAUSE_DEFAULT
:
17218 case OMP_CLAUSE_UNTIED
:
17219 case OMP_CLAUSE_MERGEABLE
:
17220 case OMP_CLAUSE_INBRANCH
:
17221 case OMP_CLAUSE_NOTINBRANCH
:
17222 case OMP_CLAUSE_PROC_BIND
:
17223 case OMP_CLAUSE_FOR
:
17224 case OMP_CLAUSE_PARALLEL
:
17225 case OMP_CLAUSE_SECTIONS
:
17226 case OMP_CLAUSE_TASKGROUP
:
17227 case OMP_CLAUSE_NOGROUP
:
17228 case OMP_CLAUSE_THREADS
:
17229 case OMP_CLAUSE_SIMD
:
17230 case OMP_CLAUSE_DEFAULTMAP
:
17231 case OMP_CLAUSE_ORDER
:
17232 case OMP_CLAUSE_BIND
:
17233 case OMP_CLAUSE_INDEPENDENT
:
17234 case OMP_CLAUSE_AUTO
:
17235 case OMP_CLAUSE_SEQ
:
17236 case OMP_CLAUSE_IF_PRESENT
:
17237 case OMP_CLAUSE_FINALIZE
:
17240 gcc_unreachable ();
17242 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17243 switch (OMP_CLAUSE_CODE (nc
))
17245 case OMP_CLAUSE_SHARED
:
17246 case OMP_CLAUSE_PRIVATE
:
17247 case OMP_CLAUSE_FIRSTPRIVATE
:
17248 case OMP_CLAUSE_LASTPRIVATE
:
17249 case OMP_CLAUSE_COPYPRIVATE
:
17250 case OMP_CLAUSE_LINEAR
:
17251 case OMP_CLAUSE_REDUCTION
:
17252 case OMP_CLAUSE_IN_REDUCTION
:
17253 case OMP_CLAUSE_TASK_REDUCTION
:
17254 case OMP_CLAUSE_USE_DEVICE_PTR
:
17255 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17256 case OMP_CLAUSE_IS_DEVICE_PTR
:
17257 case OMP_CLAUSE_INCLUSIVE
:
17258 case OMP_CLAUSE_EXCLUSIVE
:
17259 /* tsubst_expr on SCOPE_REF results in returning
17260 finish_non_static_data_member result. Undo that here. */
17261 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17262 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17263 == IDENTIFIER_NODE
))
17265 tree t
= OMP_CLAUSE_DECL (nc
);
17268 switch (TREE_CODE (v
))
17270 case COMPONENT_REF
:
17274 case POINTER_PLUS_EXPR
:
17275 v
= TREE_OPERAND (v
, 0);
17278 if (DECL_CONTEXT (v
) == current_function_decl
17279 && DECL_ARTIFICIAL (v
)
17280 && DECL_NAME (v
) == this_identifier
)
17281 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17288 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17289 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17290 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17291 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17292 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17294 tree decl
= OMP_CLAUSE_DECL (nc
);
17297 retrofit_lang_decl (decl
);
17298 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17307 new_clauses
= nreverse (new_clauses
);
17308 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17310 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17311 if (linear_no_step
)
17312 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17313 if (nc
== linear_no_step
)
17315 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17319 return new_clauses
;
17322 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17325 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17328 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17330 tree purpose
, value
, chain
;
17335 if (TREE_CODE (t
) != TREE_LIST
)
17336 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17337 /*function_p=*/false,
17338 /*integral_constant_expression_p=*/false);
17340 if (t
== void_list_node
)
17343 purpose
= TREE_PURPOSE (t
);
17345 purpose
= RECUR (purpose
);
17346 value
= TREE_VALUE (t
);
17349 if (TREE_CODE (value
) != LABEL_DECL
)
17350 value
= RECUR (value
);
17353 value
= lookup_label (DECL_NAME (value
));
17354 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17355 TREE_USED (value
) = 1;
17358 chain
= TREE_CHAIN (t
);
17359 if (chain
&& chain
!= void_type_node
)
17360 chain
= RECUR (chain
);
17361 return tree_cons (purpose
, value
, chain
);
17365 /* Used to temporarily communicate the list of #pragma omp parallel
17366 clauses to #pragma omp for instantiation if they are combined
17369 static tree
*omp_parallel_combined_clauses
;
17371 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17372 tree
*, unsigned int *);
17374 /* Substitute one OMP_FOR iterator. */
17377 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17378 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17379 tree args
, tsubst_flags_t complain
, tree in_decl
,
17380 bool integral_constant_expression_p
)
17382 #define RECUR(NODE) \
17383 tsubst_expr ((NODE), args, complain, in_decl, \
17384 integral_constant_expression_p)
17385 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17388 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17389 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17391 decl
= TREE_OPERAND (init
, 0);
17392 init
= TREE_OPERAND (init
, 1);
17393 tree decl_expr
= NULL_TREE
;
17394 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17397 bool decomp
= false;
17398 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17400 tree v
= DECL_VALUE_EXPR (decl
);
17401 if (TREE_CODE (v
) == ARRAY_REF
17402 && VAR_P (TREE_OPERAND (v
, 0))
17403 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17405 tree decomp_first
= NULL_TREE
;
17406 unsigned decomp_cnt
= 0;
17407 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17408 maybe_push_decl (d
);
17409 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17410 in_decl
, &decomp_first
, &decomp_cnt
);
17412 if (d
== error_mark_node
)
17413 decl
= error_mark_node
;
17415 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17417 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17419 tree v
= build_nt (ARRAY_REF
, d
,
17420 size_int (decomp_cnt
- i
- 1),
17421 NULL_TREE
, NULL_TREE
);
17422 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17423 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17425 fit_decomposition_lang_decl (decomp_first
, d
);
17426 decomp_first
= DECL_CHAIN (decomp_first
);
17430 decl
= tsubst_decl (decl
, args
, complain
);
17432 maybe_push_decl (decl
);
17434 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17436 /* We need to jump through some hoops to handle declarations in the
17437 init-statement, since we might need to handle auto deduction,
17438 but we need to keep control of initialization. */
17440 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17441 decl
= tsubst_decl (decl
, args
, complain
);
17445 if (TREE_CODE (decl
) == SCOPE_REF
)
17447 decl
= RECUR (decl
);
17448 if (TREE_CODE (decl
) == COMPONENT_REF
)
17452 switch (TREE_CODE (v
))
17454 case COMPONENT_REF
:
17458 case POINTER_PLUS_EXPR
:
17459 v
= TREE_OPERAND (v
, 0);
17462 if (DECL_CONTEXT (v
) == current_function_decl
17463 && DECL_ARTIFICIAL (v
)
17464 && DECL_NAME (v
) == this_identifier
)
17466 decl
= TREE_OPERAND (decl
, 1);
17467 decl
= omp_privatize_field (decl
, false);
17477 decl
= RECUR (decl
);
17479 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17481 init
= copy_node (init
);
17482 TREE_VEC_ELT (init
, 0)
17483 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17484 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17485 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17488 init
= RECUR (init
);
17490 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17492 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17493 if (TREE_CODE (o
) == TREE_LIST
)
17494 TREE_VEC_ELT (orig_declv
, i
)
17495 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17496 RECUR (TREE_VALUE (o
)),
17499 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17504 tree this_pre_body
= NULL_TREE
;
17505 tree orig_init
= NULL_TREE
;
17506 tree orig_decl
= NULL_TREE
;
17507 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17508 orig_init
, cond
, incr
);
17511 if (orig_declv
== NULL_TREE
)
17512 orig_declv
= copy_node (declv
);
17513 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17516 else if (orig_declv
)
17517 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17520 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17521 if (!range_for
&& auto_node
&& init
)
17523 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17525 gcc_assert (!type_dependent_expression_p (decl
));
17527 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17531 /* Declare the variable, but don't let that initialize it. */
17532 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17533 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17535 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17540 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17541 if (COMPARISON_CLASS_P (cond
)
17542 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17544 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17545 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17546 TREE_VEC_ELT (rhs
, 0)
17547 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17548 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17549 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17550 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17554 cond
= RECUR (cond
);
17555 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17556 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17558 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17559 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17560 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17561 NOP_EXPR
, rhs
, complain
);
17564 incr
= RECUR (incr
);
17565 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17566 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17568 TREE_VEC_ELT (declv
, i
) = decl
;
17569 TREE_VEC_ELT (initv
, i
) = init
;
17570 TREE_VEC_ELT (condv
, i
) = cond
;
17571 TREE_VEC_ELT (incrv
, i
) = incr
;
17577 /* Declare and initialize the variable. */
17585 for (j
= ((omp_parallel_combined_clauses
== NULL
17586 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17588 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17590 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17591 && OMP_CLAUSE_DECL (*pc
) == decl
)
17593 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17594 && OMP_CLAUSE_DECL (*pc
) == decl
)
17598 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17600 *pc
= OMP_CLAUSE_CHAIN (c
);
17601 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17604 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17605 && OMP_CLAUSE_DECL (*pc
) == decl
)
17607 error ("iteration variable %qD should not be firstprivate",
17609 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17611 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17612 && OMP_CLAUSE_DECL (*pc
) == decl
)
17614 error ("iteration variable %qD should not be reduction",
17616 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17619 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17624 if (*pc
== NULL_TREE
)
17626 tree c
= build_omp_clause (input_location
,
17627 TREE_CODE (t
) == OMP_LOOP
17628 ? OMP_CLAUSE_LASTPRIVATE
17629 : OMP_CLAUSE_PRIVATE
);
17630 OMP_CLAUSE_DECL (c
) = decl
;
17631 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17634 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17639 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17640 if (COMPARISON_CLASS_P (cond
))
17642 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17643 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17644 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17647 cond
= RECUR (cond
);
17648 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17649 switch (TREE_CODE (incr
))
17651 case PREINCREMENT_EXPR
:
17652 case PREDECREMENT_EXPR
:
17653 case POSTINCREMENT_EXPR
:
17654 case POSTDECREMENT_EXPR
:
17655 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17656 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17659 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17660 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17662 tree rhs
= TREE_OPERAND (incr
, 1);
17663 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17664 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17665 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17666 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17667 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17671 incr
= RECUR (incr
);
17674 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17675 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17677 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17678 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17679 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17680 TREE_TYPE (decl
), lhs
,
17681 RECUR (TREE_OPERAND (incr
, 2))));
17683 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17684 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17685 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17687 tree rhs
= TREE_OPERAND (incr
, 2);
17688 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17689 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17690 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17691 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17692 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17696 incr
= RECUR (incr
);
17699 incr
= RECUR (incr
);
17703 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17704 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17705 TREE_VEC_ELT (declv
, i
) = decl
;
17706 TREE_VEC_ELT (initv
, i
) = init
;
17707 TREE_VEC_ELT (condv
, i
) = cond
;
17708 TREE_VEC_ELT (incrv
, i
) = incr
;
17713 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17714 of OMP_TARGET's body. */
17717 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17719 *walk_subtrees
= 0;
17720 switch (TREE_CODE (*tp
))
17725 case STATEMENT_LIST
:
17726 *walk_subtrees
= 1;
17734 /* Helper function for tsubst_expr. For decomposition declaration
17735 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17736 also the corresponding decls representing the identifiers
17737 of the decomposition declaration. Return DECL if successful
17738 or error_mark_node otherwise, set *FIRST to the first decl
17739 in the list chained through DECL_CHAIN and *CNT to the number
17743 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17744 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17747 tree decl2
, decl3
, prev
= decl
;
17749 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17750 for (decl2
= DECL_CHAIN (pattern_decl
);
17753 && DECL_DECOMPOSITION_P (decl2
)
17754 && DECL_NAME (decl2
);
17755 decl2
= DECL_CHAIN (decl2
))
17757 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17759 gcc_assert (errorcount
);
17760 return error_mark_node
;
17763 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17764 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17765 tree v
= DECL_VALUE_EXPR (decl2
);
17766 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17767 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17768 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17769 SET_DECL_VALUE_EXPR (decl2
, v
);
17770 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17772 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17775 gcc_assert (errorcount
);
17776 decl
= error_mark_node
;
17779 maybe_push_decl (decl3
);
17780 if (error_operand_p (decl3
))
17781 decl
= error_mark_node
;
17782 else if (decl
!= error_mark_node
17783 && DECL_CHAIN (decl3
) != prev
17786 gcc_assert (errorcount
);
17787 decl
= error_mark_node
;
17796 /* Return the proper local_specialization for init-capture pack DECL. */
17799 lookup_init_capture_pack (tree decl
)
17801 /* We handle normal pack captures by forwarding to the specialization of the
17802 captured parameter. We can't do that for pack init-captures; we need them
17803 to have their own local_specialization. We created the individual
17804 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17805 when we process the DECL_EXPR for the pack init-capture in the template.
17806 So, how do we find them? We don't know the capture proxy pack when
17807 building the individual resulting proxies, and we don't know the
17808 individual proxies when instantiating the pack. What we have in common is
17811 So...when we instantiate the FIELD_DECL, we stick the result in
17812 local_specializations. Then at the DECL_EXPR we look up that result, see
17813 how many elements it has, synthesize the names, and look them up. */
17815 tree cname
= DECL_NAME (decl
);
17816 tree val
= DECL_VALUE_EXPR (decl
);
17817 tree field
= TREE_OPERAND (val
, 1);
17818 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17819 tree fpack
= retrieve_local_specialization (field
);
17820 if (fpack
== error_mark_node
)
17821 return error_mark_node
;
17824 tree vec
= NULL_TREE
;
17825 tree r
= NULL_TREE
;
17826 if (TREE_CODE (fpack
) == TREE_VEC
)
17828 len
= TREE_VEC_LENGTH (fpack
);
17829 vec
= make_tree_vec (len
);
17830 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17831 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17833 for (int i
= 0; i
< len
; ++i
)
17835 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17836 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17838 TREE_VEC_ELT (vec
, i
) = elt
;
17845 /* Like tsubst_copy for expressions, etc. but also does semantic
17849 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17850 bool integral_constant_expression_p
)
17852 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17853 #define RECUR(NODE) \
17854 tsubst_expr ((NODE), args, complain, in_decl, \
17855 integral_constant_expression_p)
17861 if (t
== NULL_TREE
|| t
== error_mark_node
)
17864 loc
= input_location
;
17865 if (location_t eloc
= cp_expr_location (t
))
17866 input_location
= eloc
;
17867 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17868 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17870 switch (TREE_CODE (t
))
17872 case STATEMENT_LIST
:
17874 tree_stmt_iterator i
;
17875 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17876 RECUR (tsi_stmt (i
));
17880 case CTOR_INITIALIZER
:
17881 finish_mem_initializers (tsubst_initializer_list
17882 (TREE_OPERAND (t
, 0), args
));
17886 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17889 case CO_RETURN_EXPR
:
17890 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
17893 case CO_YIELD_EXPR
:
17894 stmt
= finish_co_yield_expr (input_location
,
17895 RECUR (TREE_OPERAND (t
, 0)));
17899 case CO_AWAIT_EXPR
:
17900 stmt
= finish_co_await_expr (input_location
,
17901 RECUR (TREE_OPERAND (t
, 0)));
17906 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17907 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17908 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17910 finish_expr_stmt (tmp
);
17914 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17919 tree decl
, pattern_decl
;
17922 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17923 if (TREE_CODE (decl
) == LABEL_DECL
)
17924 finish_label_decl (DECL_NAME (decl
));
17925 else if (TREE_CODE (decl
) == USING_DECL
)
17927 tree scope
= USING_DECL_SCOPE (decl
);
17928 tree name
= DECL_NAME (decl
);
17930 scope
= tsubst (scope
, args
, complain
, in_decl
);
17931 finish_nonmember_using_decl (scope
, name
);
17933 else if (is_capture_proxy (decl
)
17934 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17936 /* We're in tsubst_lambda_expr, we've already inserted a new
17937 capture proxy, so look it up and register it. */
17939 if (!DECL_PACK_P (decl
))
17941 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17942 /*nonclass*/1, /*block_p=*/true,
17943 /*ns_only*/0, LOOKUP_HIDDEN
);
17944 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17946 else if (is_normal_capture_proxy (decl
))
17948 inst
= (retrieve_local_specialization
17949 (DECL_CAPTURED_VARIABLE (decl
)));
17950 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
17951 || DECL_PACK_P (inst
));
17954 inst
= lookup_init_capture_pack (decl
);
17956 register_local_specialization (inst
, decl
);
17959 else if (DECL_PRETTY_FUNCTION_P (decl
))
17960 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17962 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17963 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17964 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17965 /* Don't copy the old closure; we'll create a new one in
17966 tsubst_lambda_expr. */
17970 init
= DECL_INITIAL (decl
);
17971 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17972 for local variables, so save if DECL was declared constinit. */
17973 const bool constinit_p
17975 && DECL_LANG_SPECIFIC (decl
)
17976 && DECL_TEMPLATE_INFO (decl
)
17977 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17978 decl
= tsubst (decl
, args
, complain
, in_decl
);
17979 if (decl
!= error_mark_node
)
17981 /* By marking the declaration as instantiated, we avoid
17982 trying to instantiate it. Since instantiate_decl can't
17983 handle local variables, and since we've already done
17984 all that needs to be done, that's the right thing to
17987 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17988 if (VAR_P (decl
) && !DECL_NAME (decl
)
17989 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17990 /* Anonymous aggregates are a special case. */
17991 finish_anon_union (decl
);
17992 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17994 DECL_CONTEXT (decl
) = current_function_decl
;
17995 if (DECL_NAME (decl
) == this_identifier
)
17997 tree lam
= DECL_CONTEXT (current_function_decl
);
17998 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17999 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18001 insert_capture_proxy (decl
);
18003 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18004 /* We already did a pushtag. */;
18005 else if (TREE_CODE (decl
) == FUNCTION_DECL
18006 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18007 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
18009 DECL_CONTEXT (decl
) = NULL_TREE
;
18011 DECL_CONTEXT (decl
) = current_function_decl
;
18012 cp_check_omp_declare_reduction (decl
);
18016 bool const_init
= false;
18017 unsigned int cnt
= 0;
18018 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18019 maybe_push_decl (decl
);
18022 && DECL_DECOMPOSITION_P (decl
)
18023 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18024 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18025 complain
, in_decl
, &first
,
18028 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18031 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18034 if (ndecl
!= error_mark_node
)
18035 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18037 /* In a non-template function, VLA type declarations are
18038 handled in grokdeclarator; for templates, handle them
18040 predeclare_vla (decl
);
18042 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
18043 constinit_p
? LOOKUP_CONSTINIT
: 0);
18045 if (ndecl
!= error_mark_node
)
18046 cp_finish_decomp (ndecl
, first
, cnt
);
18055 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18056 RECUR (FOR_INIT_STMT (t
));
18057 finish_init_stmt (stmt
);
18058 tmp
= RECUR (FOR_COND (t
));
18059 finish_for_cond (tmp
, stmt
, false, 0);
18060 tmp
= RECUR (FOR_EXPR (t
));
18061 finish_for_expr (tmp
, stmt
);
18063 bool prev
= note_iteration_stmt_body_start ();
18064 RECUR (FOR_BODY (t
));
18065 note_iteration_stmt_body_end (prev
);
18067 finish_for_stmt (stmt
);
18070 case RANGE_FOR_STMT
:
18072 /* Construct another range_for, if this is not a final
18073 substitution (for inside a generic lambda of a
18074 template). Otherwise convert to a regular for. */
18076 stmt
= (processing_template_decl
18077 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18078 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18079 RECUR (RANGE_FOR_INIT_STMT (t
));
18080 decl
= RANGE_FOR_DECL (t
);
18081 decl
= tsubst (decl
, args
, complain
, in_decl
);
18082 maybe_push_decl (decl
);
18083 expr
= RECUR (RANGE_FOR_EXPR (t
));
18085 tree decomp_first
= NULL_TREE
;
18086 unsigned decomp_cnt
= 0;
18087 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18088 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18090 &decomp_first
, &decomp_cnt
);
18092 if (processing_template_decl
)
18094 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18095 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18096 finish_range_for_decl (stmt
, decl
, expr
);
18097 if (decomp_first
&& decl
!= error_mark_node
)
18098 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18102 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18103 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18104 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18105 decomp_first
, decomp_cnt
,
18106 RANGE_FOR_IVDEP (t
), unroll
);
18109 bool prev
= note_iteration_stmt_body_start ();
18110 RECUR (RANGE_FOR_BODY (t
));
18111 note_iteration_stmt_body_end (prev
);
18112 finish_for_stmt (stmt
);
18117 stmt
= begin_while_stmt ();
18118 tmp
= RECUR (WHILE_COND (t
));
18119 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18121 bool prev
= note_iteration_stmt_body_start ();
18122 RECUR (WHILE_BODY (t
));
18123 note_iteration_stmt_body_end (prev
);
18125 finish_while_stmt (stmt
);
18129 stmt
= begin_do_stmt ();
18131 bool prev
= note_iteration_stmt_body_start ();
18132 RECUR (DO_BODY (t
));
18133 note_iteration_stmt_body_end (prev
);
18135 finish_do_body (stmt
);
18136 tmp
= RECUR (DO_COND (t
));
18137 finish_do_stmt (tmp
, stmt
, false, 0);
18141 stmt
= begin_if_stmt ();
18142 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18143 if (IF_STMT_CONSTEXPR_P (t
))
18144 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18145 tmp
= RECUR (IF_COND (t
));
18146 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18147 if (IF_STMT_CONSTEXPR_P (t
)
18148 && instantiation_dependent_expression_p (tmp
))
18150 /* We're partially instantiating a generic lambda, but the condition
18151 of the constexpr if is still dependent. Don't substitute into the
18152 branches now, just remember the template arguments. */
18153 do_poplevel (IF_SCOPE (stmt
));
18154 IF_COND (stmt
) = IF_COND (t
);
18155 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18156 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18157 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18161 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18162 /* Don't instantiate the THEN_CLAUSE. */;
18165 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18166 bool inhibit
= integer_zerop (folded
);
18168 ++c_inhibit_evaluation_warnings
;
18169 RECUR (THEN_CLAUSE (t
));
18171 --c_inhibit_evaluation_warnings
;
18173 finish_then_clause (stmt
);
18175 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18176 /* Don't instantiate the ELSE_CLAUSE. */;
18177 else if (ELSE_CLAUSE (t
))
18179 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18180 bool inhibit
= integer_nonzerop (folded
);
18181 begin_else_clause (stmt
);
18183 ++c_inhibit_evaluation_warnings
;
18184 RECUR (ELSE_CLAUSE (t
));
18186 --c_inhibit_evaluation_warnings
;
18187 finish_else_clause (stmt
);
18190 finish_if_stmt (stmt
);
18194 if (BIND_EXPR_BODY_BLOCK (t
))
18195 stmt
= begin_function_body ();
18197 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18198 ? BCS_TRY_BLOCK
: 0);
18200 RECUR (BIND_EXPR_BODY (t
));
18202 if (BIND_EXPR_BODY_BLOCK (t
))
18203 finish_function_body (stmt
);
18205 finish_compound_stmt (stmt
);
18209 finish_break_stmt ();
18212 case CONTINUE_STMT
:
18213 finish_continue_stmt ();
18217 stmt
= begin_switch_stmt ();
18218 tmp
= RECUR (SWITCH_STMT_COND (t
));
18219 finish_switch_cond (tmp
, stmt
);
18220 RECUR (SWITCH_STMT_BODY (t
));
18221 finish_switch_stmt (stmt
);
18224 case CASE_LABEL_EXPR
:
18226 tree decl
= CASE_LABEL (t
);
18227 tree low
= RECUR (CASE_LOW (t
));
18228 tree high
= RECUR (CASE_HIGH (t
));
18229 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18230 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18232 tree label
= CASE_LABEL (l
);
18233 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18234 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18235 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18242 tree decl
= LABEL_EXPR_LABEL (t
);
18245 label
= finish_label_stmt (DECL_NAME (decl
));
18246 if (TREE_CODE (label
) == LABEL_DECL
)
18247 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18248 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18249 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18254 tmp
= GOTO_DESTINATION (t
);
18255 if (TREE_CODE (tmp
) != LABEL_DECL
)
18256 /* Computed goto's must be tsubst'd into. On the other hand,
18257 non-computed gotos must not be; the identifier in question
18258 will have no binding. */
18261 tmp
= DECL_NAME (tmp
);
18262 finish_goto_stmt (tmp
);
18267 tree string
= RECUR (ASM_STRING (t
));
18268 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18269 complain
, in_decl
);
18270 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18271 complain
, in_decl
);
18272 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18273 complain
, in_decl
);
18274 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18275 complain
, in_decl
);
18276 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18277 outputs
, inputs
, clobbers
, labels
,
18279 tree asm_expr
= tmp
;
18280 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18281 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18282 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18289 stmt
= begin_try_block ();
18290 RECUR (TRY_STMTS (t
));
18291 finish_cleanup_try_block (stmt
);
18292 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18296 tree compound_stmt
= NULL_TREE
;
18298 if (FN_TRY_BLOCK_P (t
))
18299 stmt
= begin_function_try_block (&compound_stmt
);
18301 stmt
= begin_try_block ();
18303 RECUR (TRY_STMTS (t
));
18305 if (FN_TRY_BLOCK_P (t
))
18306 finish_function_try_block (stmt
);
18308 finish_try_block (stmt
);
18310 RECUR (TRY_HANDLERS (t
));
18311 if (FN_TRY_BLOCK_P (t
))
18312 finish_function_handler_sequence (stmt
, compound_stmt
);
18314 finish_handler_sequence (stmt
);
18320 tree decl
= HANDLER_PARMS (t
);
18324 decl
= tsubst (decl
, args
, complain
, in_decl
);
18325 /* Prevent instantiate_decl from trying to instantiate
18326 this variable. We've already done all that needs to be
18328 if (decl
!= error_mark_node
)
18329 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18331 stmt
= begin_handler ();
18332 finish_handler_parms (decl
, stmt
);
18333 RECUR (HANDLER_BODY (t
));
18334 finish_handler (stmt
);
18339 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18340 if (CLASS_TYPE_P (tmp
))
18342 /* Local classes are not independent templates; they are
18343 instantiated along with their containing function. And this
18344 way we don't have to deal with pushing out of one local class
18345 to instantiate a member of another local class. */
18346 /* Closures are handled by the LAMBDA_EXPR. */
18347 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18348 complete_type (tmp
);
18349 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18351 || (TREE_CODE (fld
) == FUNCTION_DECL
18352 && !DECL_ARTIFICIAL (fld
)))
18353 && DECL_TEMPLATE_INSTANTIATION (fld
))
18354 instantiate_decl (fld
, /*defer_ok=*/false,
18355 /*expl_inst_class=*/false);
18359 case STATIC_ASSERT
:
18363 ++c_inhibit_evaluation_warnings
;
18365 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18368 /*integral_constant_expression_p=*/true);
18369 --c_inhibit_evaluation_warnings
;
18371 finish_static_assert (condition
,
18372 STATIC_ASSERT_MESSAGE (t
),
18373 STATIC_ASSERT_SOURCE_LOCATION (t
),
18374 /*member_p=*/false);
18379 case OACC_PARALLEL
:
18381 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18383 stmt
= begin_omp_parallel ();
18384 RECUR (OMP_BODY (t
));
18385 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18389 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18390 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18391 complain
, in_decl
);
18392 if (OMP_PARALLEL_COMBINED (t
))
18393 omp_parallel_combined_clauses
= &tmp
;
18394 stmt
= begin_omp_parallel ();
18395 RECUR (OMP_PARALLEL_BODY (t
));
18396 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18397 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18398 = OMP_PARALLEL_COMBINED (t
);
18399 pop_omp_privatization_clauses (r
);
18403 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18405 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18406 complain
, in_decl
);
18408 OMP_TASK_CLAUSES (t
) = tmp
;
18412 r
= push_omp_privatization_clauses (false);
18413 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18414 complain
, in_decl
);
18415 stmt
= begin_omp_task ();
18416 RECUR (OMP_TASK_BODY (t
));
18417 finish_omp_task (tmp
, stmt
);
18418 pop_omp_privatization_clauses (r
);
18424 case OMP_DISTRIBUTE
:
18428 tree clauses
, body
, pre_body
;
18429 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18430 tree orig_declv
= NULL_TREE
;
18431 tree incrv
= NULL_TREE
;
18432 enum c_omp_region_type ort
= C_ORT_OMP
;
18433 bool any_range_for
= false;
18436 if (TREE_CODE (t
) == OACC_LOOP
)
18439 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18440 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18442 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18444 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18445 if (OMP_FOR_ORIG_DECLS (t
))
18446 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18447 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18448 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18449 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18452 keep_next_level (true);
18453 stmt
= begin_omp_structured_block ();
18455 pre_body
= push_stmt_list ();
18456 RECUR (OMP_FOR_PRE_BODY (t
));
18457 pre_body
= pop_stmt_list (pre_body
);
18459 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18460 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18462 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18463 condv
, incrv
, &clauses
, args
,
18465 integral_constant_expression_p
);
18466 omp_parallel_combined_clauses
= NULL
;
18470 gcc_assert (orig_declv
);
18471 body
= begin_omp_structured_block ();
18472 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18473 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18474 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18475 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18476 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18477 TREE_VEC_ELT (declv
, i
));
18480 body
= push_stmt_list ();
18481 RECUR (OMP_FOR_BODY (t
));
18483 body
= finish_omp_structured_block (body
);
18485 body
= pop_stmt_list (body
);
18487 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18488 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18489 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18493 t
= make_node (TREE_CODE (t
));
18494 TREE_TYPE (t
) = void_type_node
;
18495 OMP_FOR_BODY (t
) = body
;
18496 OMP_FOR_PRE_BODY (t
) = pre_body
;
18497 OMP_FOR_CLAUSES (t
) = clauses
;
18498 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18502 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18504 pop_omp_privatization_clauses (r
);
18509 omp_parallel_combined_clauses
= NULL
;
18514 case OMP_TASKGROUP
:
18516 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18517 && OMP_TEAMS_COMBINED (t
));
18518 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18520 if (TREE_CODE (t
) == OMP_TEAMS
)
18522 keep_next_level (true);
18523 stmt
= begin_omp_structured_block ();
18524 RECUR (OMP_BODY (t
));
18525 stmt
= finish_omp_structured_block (stmt
);
18529 stmt
= push_stmt_list ();
18530 RECUR (OMP_BODY (t
));
18531 stmt
= pop_stmt_list (stmt
);
18534 if (TREE_CODE (t
) == OMP_CRITICAL
18535 && tmp
!= NULL_TREE
18536 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18538 error_at (OMP_CLAUSE_LOCATION (tmp
),
18539 "%<#pragma omp critical%> with %<hint%> clause requires "
18540 "a name, except when %<omp_sync_hint_none%> is used");
18541 RETURN (error_mark_node
);
18544 OMP_BODY (t
) = stmt
;
18545 OMP_CLAUSES (t
) = tmp
;
18547 pop_omp_privatization_clauses (r
);
18551 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18552 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18554 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18555 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18557 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18558 args
, complain
, in_decl
);
18559 if (tmp
== NULL_TREE
)
18560 tmp
= error_mark_node
;
18564 kind
= (enum omp_clause_depend_kind
)
18565 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18568 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18571 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18572 OMP_CLAUSE_DEPEND_SOURCE
,
18573 OMP_DEPOBJ_CLAUSES (t
));
18577 case OMP_TARGET_DATA
:
18579 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18580 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18582 keep_next_level (true);
18583 stmt
= begin_omp_structured_block ();
18585 RECUR (OMP_BODY (t
));
18586 stmt
= finish_omp_structured_block (stmt
);
18589 OMP_BODY (t
) = stmt
;
18590 OMP_CLAUSES (t
) = tmp
;
18591 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18593 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18596 /* For combined target teams, ensure the num_teams and
18597 thread_limit clause expressions are evaluated on the host,
18598 before entering the target construct. */
18600 for (c
= OMP_TEAMS_CLAUSES (teams
);
18601 c
; c
= OMP_CLAUSE_CHAIN (c
))
18602 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18603 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18604 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18606 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18607 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18608 if (expr
== error_mark_node
)
18610 tmp
= TARGET_EXPR_SLOT (expr
);
18612 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18613 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18614 OMP_CLAUSE_FIRSTPRIVATE
);
18615 OMP_CLAUSE_DECL (tc
) = tmp
;
18616 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18617 OMP_TARGET_CLAUSES (t
) = tc
;
18626 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18627 complain
, in_decl
);
18628 OACC_DECLARE_CLAUSES (t
) = tmp
;
18632 case OMP_TARGET_UPDATE
:
18633 case OMP_TARGET_ENTER_DATA
:
18634 case OMP_TARGET_EXIT_DATA
:
18635 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18636 complain
, in_decl
);
18638 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18642 case OACC_ENTER_DATA
:
18643 case OACC_EXIT_DATA
:
18645 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18646 complain
, in_decl
);
18648 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18653 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18654 complain
, in_decl
);
18655 stmt
= push_stmt_list ();
18656 RECUR (OMP_BODY (t
));
18657 stmt
= pop_stmt_list (stmt
);
18660 OMP_BODY (t
) = stmt
;
18661 OMP_ORDERED_CLAUSES (t
) = tmp
;
18666 omp_parallel_combined_clauses
= NULL
;
18669 stmt
= push_stmt_list ();
18670 RECUR (OMP_BODY (t
));
18671 stmt
= pop_stmt_list (stmt
);
18674 OMP_BODY (t
) = stmt
;
18679 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18681 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18682 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18683 complain
, in_decl
);
18684 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18686 tree op1
= TREE_OPERAND (t
, 1);
18687 tree rhs1
= NULL_TREE
;
18689 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18691 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18692 op1
= TREE_OPERAND (op1
, 1);
18694 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18695 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18696 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18697 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18698 OMP_ATOMIC_MEMORY_ORDER (t
));
18702 tree op1
= TREE_OPERAND (t
, 1);
18703 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18704 tree rhs1
= NULL_TREE
;
18705 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18706 enum tree_code opcode
= NOP_EXPR
;
18707 if (code
== OMP_ATOMIC_READ
)
18709 v
= RECUR (TREE_OPERAND (op1
, 0));
18710 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18712 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18713 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18715 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18716 v
= RECUR (TREE_OPERAND (op1
, 0));
18717 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18718 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18720 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18721 op11
= TREE_OPERAND (op11
, 1);
18723 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18724 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18725 opcode
= TREE_CODE (op11
);
18726 if (opcode
== MODIFY_EXPR
)
18732 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18733 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18735 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18736 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18740 case TRANSACTION_EXPR
:
18743 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18744 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18746 if (TRANSACTION_EXPR_IS_STMT (t
))
18748 tree body
= TRANSACTION_EXPR_BODY (t
);
18749 tree noex
= NULL_TREE
;
18750 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18752 noex
= MUST_NOT_THROW_COND (body
);
18753 if (noex
== NULL_TREE
)
18754 noex
= boolean_true_node
;
18755 body
= TREE_OPERAND (body
, 0);
18757 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18759 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18763 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18764 RECUR (TRANSACTION_EXPR_BODY (t
)),
18771 case MUST_NOT_THROW_EXPR
:
18773 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18774 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18775 RETURN (build_must_not_throw_expr (op0
, cond
));
18778 case EXPR_PACK_EXPANSION
:
18779 error ("invalid use of pack expansion expression");
18780 RETURN (error_mark_node
);
18782 case NONTYPE_ARGUMENT_PACK
:
18783 error ("use %<...%> to expand argument pack");
18784 RETURN (error_mark_node
);
18786 case COMPOUND_EXPR
:
18787 tmp
= RECUR (TREE_OPERAND (t
, 0));
18788 if (tmp
== NULL_TREE
)
18789 /* If the first operand was a statement, we're done with it. */
18790 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18791 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18792 RECUR (TREE_OPERAND (t
, 1)),
18795 case ANNOTATE_EXPR
:
18796 tmp
= RECUR (TREE_OPERAND (t
, 0));
18797 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18798 TREE_TYPE (tmp
), tmp
,
18799 RECUR (TREE_OPERAND (t
, 1)),
18800 RECUR (TREE_OPERAND (t
, 2))));
18803 RETURN (add_stmt (copy_node (t
)));
18806 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18808 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18809 /*function_p=*/false,
18810 integral_constant_expression_p
));
18813 RETURN (NULL_TREE
);
18815 input_location
= loc
;
18821 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18822 function. For description of the body see comment above
18823 cp_parser_omp_declare_reduction_exprs. */
18826 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18828 if (t
== NULL_TREE
|| t
== error_mark_node
)
18831 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18833 tree_stmt_iterator tsi
;
18836 memset (stmts
, 0, sizeof stmts
);
18837 for (i
= 0, tsi
= tsi_start (t
);
18838 i
< 7 && !tsi_end_p (tsi
);
18839 i
++, tsi_next (&tsi
))
18840 stmts
[i
] = tsi_stmt (tsi
);
18841 gcc_assert (tsi_end_p (tsi
));
18845 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18846 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18847 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18848 args
, complain
, in_decl
);
18849 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18850 args
, complain
, in_decl
);
18851 DECL_CONTEXT (omp_out
) = current_function_decl
;
18852 DECL_CONTEXT (omp_in
) = current_function_decl
;
18853 keep_next_level (true);
18854 tree block
= begin_omp_structured_block ();
18855 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18856 block
= finish_omp_structured_block (block
);
18857 block
= maybe_cleanup_point_expr_void (block
);
18858 add_decl_expr (omp_out
);
18859 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18860 TREE_NO_WARNING (omp_out
) = 1;
18861 add_decl_expr (omp_in
);
18862 finish_expr_stmt (block
);
18866 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18867 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18868 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18869 args
, complain
, in_decl
);
18870 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18871 args
, complain
, in_decl
);
18872 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18873 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18874 keep_next_level (true);
18875 tree block
= begin_omp_structured_block ();
18876 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18877 block
= finish_omp_structured_block (block
);
18878 block
= maybe_cleanup_point_expr_void (block
);
18879 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18880 add_decl_expr (omp_priv
);
18881 add_decl_expr (omp_orig
);
18882 finish_expr_stmt (block
);
18884 add_decl_expr (omp_orig
);
18888 /* T is a postfix-expression that is not being used in a function
18889 call. Return the substituted version of T. */
18892 tsubst_non_call_postfix_expression (tree t
, tree args
,
18893 tsubst_flags_t complain
,
18896 if (TREE_CODE (t
) == SCOPE_REF
)
18897 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18898 /*done=*/false, /*address_p=*/false);
18900 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18901 /*function_p=*/false,
18902 /*integral_constant_expression_p=*/false);
18907 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18908 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18909 dependent init-capture. */
18912 prepend_one_capture (tree field
, tree init
, tree
&list
,
18913 tsubst_flags_t complain
)
18915 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18917 tree type
= NULL_TREE
;
18920 if (complain
& tf_error
)
18921 error ("empty initializer in lambda init-capture");
18922 init
= error_mark_node
;
18924 else if (TREE_CODE (init
) == TREE_LIST
)
18925 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18927 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18928 TREE_TYPE (field
) = type
;
18929 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18931 list
= tree_cons (field
, init
, list
);
18934 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18935 instantiation context. Instantiating a pack expansion containing a lambda
18936 might result in multiple lambdas all based on the same lambda in the
18940 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18942 tree oldfn
= lambda_function (t
);
18945 tree r
= build_lambda_expr ();
18947 LAMBDA_EXPR_LOCATION (r
)
18948 = LAMBDA_EXPR_LOCATION (t
);
18949 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18950 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18951 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18952 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18954 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18955 /* A lambda in a default argument outside a class gets no
18956 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18957 tsubst_default_argument calls start_lambda_scope, so we need to
18958 specifically ignore it here, and use the global scope. */
18959 record_null_lambda_scope (r
);
18961 record_lambda_scope (r
);
18963 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18964 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18966 vec
<tree
,va_gc
>* field_packs
= NULL
;
18968 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18969 cap
= TREE_CHAIN (cap
))
18971 tree ofield
= TREE_PURPOSE (cap
);
18972 tree init
= TREE_VALUE (cap
);
18973 if (PACK_EXPANSION_P (init
))
18974 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18976 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18977 /*fn*/false, /*constexpr*/false);
18979 if (init
== error_mark_node
)
18980 return error_mark_node
;
18982 if (init
&& TREE_CODE (init
) == TREE_LIST
)
18983 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18985 if (!processing_template_decl
18986 && init
&& TREE_CODE (init
) != TREE_VEC
18987 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
18989 /* For a VLA, simply tsubsting the field type won't work, we need to
18990 go through add_capture again. XXX do we want to do this for all
18992 tree name
= (get_identifier
18993 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
18994 tree ftype
= TREE_TYPE (ofield
);
18995 bool by_ref
= (TYPE_REF_P (ftype
)
18996 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
18997 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
18998 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19002 if (PACK_EXPANSION_P (ofield
))
19003 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19004 tree field
= tsubst_decl (ofield
, args
, complain
);
19006 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19008 /* Remember these for when we've pushed local_specializations. */
19009 vec_safe_push (field_packs
, ofield
);
19010 vec_safe_push (field_packs
, field
);
19013 if (field
== error_mark_node
)
19014 return error_mark_node
;
19016 if (TREE_CODE (field
) == TREE_VEC
)
19018 int len
= TREE_VEC_LENGTH (field
);
19019 gcc_assert (TREE_CODE (init
) == TREE_VEC
19020 && TREE_VEC_LENGTH (init
) == len
);
19021 for (int i
= 0; i
< len
; ++i
)
19022 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19023 TREE_VEC_ELT (init
, i
),
19024 LAMBDA_EXPR_CAPTURE_LIST (r
),
19029 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19032 if (id_equal (DECL_NAME (field
), "__this"))
19033 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19037 tree type
= begin_lambda_type (r
);
19038 if (type
== error_mark_node
)
19039 return error_mark_node
;
19041 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19042 determine_visibility (TYPE_NAME (type
));
19044 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19046 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19047 ? DECL_TI_TEMPLATE (oldfn
)
19050 tree fntype
= static_fn_type (oldfn
);
19052 ++processing_template_decl
;
19053 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19055 --processing_template_decl
;
19057 if (fntype
== error_mark_node
)
19058 r
= error_mark_node
;
19061 /* The body of a lambda-expression is not a subexpression of the
19062 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19063 which would be skipped if cp_unevaluated_operand. */
19066 /* Fix the type of 'this'. */
19067 fntype
= build_memfn_type (fntype
, type
,
19068 type_memfn_quals (fntype
),
19069 type_memfn_rqual (fntype
));
19073 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19074 if (tmpl
== error_mark_node
)
19076 r
= error_mark_node
;
19079 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19080 finish_member_declaration (tmpl
);
19085 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19086 if (fn
== error_mark_node
)
19088 r
= error_mark_node
;
19091 finish_member_declaration (fn
);
19094 if (tree ci
= get_constraints (oldfn
))
19096 /* Substitute into the lambda's constraints. */
19098 ++processing_template_decl
;
19099 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19101 --processing_template_decl
;
19102 set_constraints (fn
, ci
);
19105 /* Let finish_function set this. */
19106 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19108 bool nested
= cfun
;
19110 push_function_context ();
19112 /* Still increment function_depth so that we don't GC in the
19113 middle of an expression. */
19116 local_specialization_stack
s (lss_copy
);
19118 tree body
= start_lambda_function (fn
, r
);
19120 /* Now record them for lookup_init_capture_pack. */
19121 int fplen
= vec_safe_length (field_packs
);
19122 for (int i
= 0; i
< fplen
; )
19124 tree pack
= (*field_packs
)[i
++];
19125 tree inst
= (*field_packs
)[i
++];
19126 register_local_specialization (inst
, pack
);
19128 release_tree_vector (field_packs
);
19130 register_parameter_specializations (oldfn
, fn
);
19134 /* We might not partially instantiate some parts of the function, so
19135 copy these flags from the original template. */
19136 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19137 current_function_returns_value
= ol
->returns_value
;
19138 current_function_returns_null
= ol
->returns_null
;
19139 current_function_returns_abnormally
= ol
->returns_abnormally
;
19140 current_function_infinite_loop
= ol
->infinite_loop
;
19143 /* [temp.deduct] A lambda-expression appearing in a function type or a
19144 template parameter is not considered part of the immediate context for
19145 the purposes of template argument deduction. */
19146 complain
= tf_warning_or_error
;
19148 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19149 /*constexpr*/false);
19151 finish_lambda_function (body
);
19154 pop_function_context ();
19158 /* The capture list was built up in reverse order; fix that now. */
19159 LAMBDA_EXPR_CAPTURE_LIST (r
)
19160 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19162 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19164 maybe_add_lambda_conv_op (type
);
19168 finish_struct (type
, /*attr*/NULL_TREE
);
19170 insert_pending_capture_proxies ();
19175 /* Like tsubst but deals with expressions and performs semantic
19176 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19179 tsubst_copy_and_build (tree t
,
19181 tsubst_flags_t complain
,
19184 bool integral_constant_expression_p
)
19186 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19187 #define RECUR(NODE) \
19188 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19189 /*function_p=*/false, \
19190 integral_constant_expression_p)
19193 location_t save_loc
;
19195 if (t
== NULL_TREE
|| t
== error_mark_node
)
19198 save_loc
= input_location
;
19199 if (location_t eloc
= cp_expr_location (t
))
19200 input_location
= eloc
;
19202 /* N3276 decltype magic only applies to calls at the top level or on the
19203 right side of a comma. */
19204 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19205 complain
&= ~tf_decltype
;
19207 switch (TREE_CODE (t
))
19211 /* Fall through. */
19212 case IDENTIFIER_NODE
:
19216 bool non_integral_constant_expression_p
;
19217 const char *error_msg
;
19219 if (IDENTIFIER_CONV_OP_P (t
))
19221 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19222 t
= make_conv_op_name (new_type
);
19225 /* Look up the name. */
19226 decl
= lookup_name (t
);
19228 /* By convention, expressions use ERROR_MARK_NODE to indicate
19229 failure, not NULL_TREE. */
19230 if (decl
== NULL_TREE
)
19231 decl
= error_mark_node
;
19233 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19235 integral_constant_expression_p
,
19236 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19237 &non_integral_constant_expression_p
,
19238 /*template_p=*/false,
19240 /*address_p=*/false,
19241 /*template_arg_p=*/false,
19246 if (!function_p
&& identifier_p (decl
))
19248 if (complain
& tf_error
)
19249 unqualified_name_lookup_error (decl
);
19250 decl
= error_mark_node
;
19255 case TEMPLATE_ID_EXPR
:
19258 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19259 tree targs
= TREE_OPERAND (t
, 1);
19262 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19263 if (targs
== error_mark_node
)
19264 RETURN (error_mark_node
);
19266 if (TREE_CODE (templ
) == SCOPE_REF
)
19268 tree name
= TREE_OPERAND (templ
, 1);
19269 tree tid
= lookup_template_function (name
, targs
);
19270 TREE_OPERAND (templ
, 1) = tid
;
19274 if (concept_definition_p (templ
))
19276 tree check
= build_concept_check (templ
, targs
, complain
);
19277 if (check
== error_mark_node
)
19278 RETURN (error_mark_node
);
19280 tree id
= unpack_concept_check (check
);
19282 /* If we built a function concept check, return the underlying
19283 template-id. So we can evaluate it as a function call. */
19284 if (function_concept_p (TREE_OPERAND (id
, 0)))
19290 if (variable_template_p (templ
))
19292 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19294 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19298 if (TREE_CODE (templ
) == COMPONENT_REF
)
19300 object
= TREE_OPERAND (templ
, 0);
19301 templ
= TREE_OPERAND (templ
, 1);
19304 object
= NULL_TREE
;
19305 templ
= lookup_template_function (templ
, targs
);
19308 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19309 object
, templ
, NULL_TREE
));
19311 RETURN (baselink_for_fns (templ
));
19316 tree r
= RECUR (TREE_OPERAND (t
, 0));
19318 if (REFERENCE_REF_P (t
))
19320 /* A type conversion to reference type will be enclosed in
19321 such an indirect ref, but the substitution of the cast
19322 will have also added such an indirect ref. */
19323 r
= convert_from_reference (r
);
19326 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19327 complain
|decltype_flag
);
19329 if (REF_PARENTHESIZED_P (t
))
19330 r
= force_paren_expr (r
);
19337 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19338 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19339 RETURN (build_nop (type
, op0
));
19342 case IMPLICIT_CONV_EXPR
:
19344 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19345 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19346 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19348 retval
= copy_node (t
);
19349 TREE_TYPE (retval
) = type
;
19350 TREE_OPERAND (retval
, 0) = expr
;
19353 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19354 /* We'll pass this to convert_nontype_argument again, we don't need
19355 to actually perform any conversion here. */
19357 int flags
= LOOKUP_IMPLICIT
;
19358 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19359 flags
= LOOKUP_NORMAL
;
19360 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19361 flags
|= LOOKUP_NO_NARROWING
;
19362 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19368 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19369 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19370 if (op0
== error_mark_node
)
19371 RETURN (error_mark_node
);
19372 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19376 case REINTERPRET_CAST_EXPR
:
19377 case CONST_CAST_EXPR
:
19378 case DYNAMIC_CAST_EXPR
:
19379 case STATIC_CAST_EXPR
:
19382 tree op
, r
= NULL_TREE
;
19384 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19385 if (integral_constant_expression_p
19386 && !cast_valid_in_integral_constant_expression_p (type
))
19388 if (complain
& tf_error
)
19389 error ("a cast to a type other than an integral or "
19390 "enumeration type cannot appear in a constant-expression");
19391 RETURN (error_mark_node
);
19394 op
= RECUR (TREE_OPERAND (t
, 0));
19396 warning_sentinel
s(warn_useless_cast
);
19397 warning_sentinel
s2(warn_ignored_qualifiers
);
19398 switch (TREE_CODE (t
))
19401 r
= build_functional_cast (input_location
, type
, op
, complain
);
19403 case REINTERPRET_CAST_EXPR
:
19404 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19406 case CONST_CAST_EXPR
:
19407 r
= build_const_cast (input_location
, type
, op
, complain
);
19409 case DYNAMIC_CAST_EXPR
:
19410 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19412 case STATIC_CAST_EXPR
:
19413 r
= build_static_cast (input_location
, type
, op
, complain
);
19416 gcc_unreachable ();
19422 case POSTDECREMENT_EXPR
:
19423 case POSTINCREMENT_EXPR
:
19424 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19425 args
, complain
, in_decl
);
19426 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19427 complain
|decltype_flag
));
19429 case PREDECREMENT_EXPR
:
19430 case PREINCREMENT_EXPR
:
19434 case TRUTH_NOT_EXPR
:
19435 case UNARY_PLUS_EXPR
: /* Unary + */
19436 case REALPART_EXPR
:
19437 case IMAGPART_EXPR
:
19438 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19439 RECUR (TREE_OPERAND (t
, 0)),
19440 complain
|decltype_flag
));
19442 case FIX_TRUNC_EXPR
:
19443 gcc_unreachable ();
19446 op1
= TREE_OPERAND (t
, 0);
19447 if (TREE_CODE (op1
) == LABEL_DECL
)
19448 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19449 EXPR_LOCATION (op1
)));
19450 if (TREE_CODE (op1
) == SCOPE_REF
)
19451 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19452 /*done=*/true, /*address_p=*/true);
19454 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19456 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19457 complain
|decltype_flag
));
19462 case TRUNC_DIV_EXPR
:
19463 case CEIL_DIV_EXPR
:
19464 case FLOOR_DIV_EXPR
:
19465 case ROUND_DIV_EXPR
:
19466 case EXACT_DIV_EXPR
:
19470 case TRUNC_MOD_EXPR
:
19471 case FLOOR_MOD_EXPR
:
19472 case TRUTH_ANDIF_EXPR
:
19473 case TRUTH_ORIF_EXPR
:
19474 case TRUTH_AND_EXPR
:
19475 case TRUTH_OR_EXPR
:
19486 case SPACESHIP_EXPR
:
19490 /* If T was type-dependent, suppress warnings that depend on the range
19491 of the types involved. */
19492 bool was_dep
= type_dependent_expression_p_push (t
);
19494 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19495 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19497 warning_sentinel
s1(warn_type_limits
, was_dep
);
19498 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19499 warning_sentinel
s3(warn_logical_op
, was_dep
);
19500 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19502 tree r
= build_x_binary_op
19503 (input_location
, TREE_CODE (t
),
19505 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19507 : TREE_CODE (TREE_OPERAND (t
, 0))),
19509 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19511 : TREE_CODE (TREE_OPERAND (t
, 1))),
19513 complain
|decltype_flag
);
19514 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19515 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19520 case POINTER_PLUS_EXPR
:
19522 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19523 if (op0
== error_mark_node
)
19524 RETURN (error_mark_node
);
19525 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19526 if (op1
== error_mark_node
)
19527 RETURN (error_mark_node
);
19528 RETURN (fold_build_pointer_plus (op0
, op1
));
19532 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19533 /*address_p=*/false));
19535 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19536 args
, complain
, in_decl
);
19537 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19538 RECUR (TREE_OPERAND (t
, 1)),
19539 complain
|decltype_flag
));
19542 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19543 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19544 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19551 op1
= TREE_OPERAND (t
, 0);
19552 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19553 op1
= TREE_TYPE (op1
);
19554 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19555 && ALIGNOF_EXPR_STD_P (t
));
19558 /* When there are no ARGS, we are trying to evaluate a
19559 non-dependent expression from the parser. Trying to do
19560 the substitutions may not work. */
19562 op1
= TREE_TYPE (op1
);
19566 ++cp_unevaluated_operand
;
19567 ++c_inhibit_evaluation_warnings
;
19569 op1
= tsubst (op1
, args
, complain
, in_decl
);
19571 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19572 /*function_p=*/false,
19573 /*integral_constant_expression_p=*/
19575 --cp_unevaluated_operand
;
19576 --c_inhibit_evaluation_warnings
;
19579 r
= cxx_sizeof_or_alignof_type (input_location
,
19580 op1
, TREE_CODE (t
), std_alignof
,
19581 complain
& tf_error
);
19583 r
= cxx_sizeof_or_alignof_expr (input_location
,
19584 op1
, TREE_CODE (t
),
19585 complain
& tf_error
);
19586 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19588 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19590 if (!processing_template_decl
&& TYPE_P (op1
))
19592 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19593 build1 (NOP_EXPR
, op1
, error_mark_node
));
19594 SIZEOF_EXPR_TYPE_P (r
) = 1;
19597 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19598 TREE_SIDE_EFFECTS (r
) = 0;
19599 TREE_READONLY (r
) = 1;
19601 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19606 case AT_ENCODE_EXPR
:
19608 op1
= TREE_OPERAND (t
, 0);
19609 ++cp_unevaluated_operand
;
19610 ++c_inhibit_evaluation_warnings
;
19611 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19612 /*function_p=*/false,
19613 /*integral_constant_expression_p=*/false);
19614 --cp_unevaluated_operand
;
19615 --c_inhibit_evaluation_warnings
;
19616 RETURN (objc_build_encode_expr (op1
));
19619 case NOEXCEPT_EXPR
:
19620 op1
= TREE_OPERAND (t
, 0);
19621 ++cp_unevaluated_operand
;
19622 ++c_inhibit_evaluation_warnings
;
19623 ++cp_noexcept_operand
;
19624 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19625 /*function_p=*/false,
19626 /*integral_constant_expression_p=*/false);
19627 --cp_unevaluated_operand
;
19628 --c_inhibit_evaluation_warnings
;
19629 --cp_noexcept_operand
;
19630 RETURN (finish_noexcept_expr (op1
, complain
));
19634 warning_sentinel
s(warn_div_by_zero
);
19635 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19636 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19637 tree r
= build_x_modify_expr
19638 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19639 complain
|decltype_flag
);
19640 /* TREE_NO_WARNING must be set if either the expression was
19641 parenthesized or it uses an operator such as >>= rather
19642 than plain assignment. In the former case, it was already
19643 set and must be copied. In the latter case,
19644 build_x_modify_expr sets it and it must not be reset
19646 if (TREE_NO_WARNING (t
))
19647 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19653 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19654 args
, complain
, in_decl
);
19655 /* Remember that there was a reference to this entity. */
19657 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19658 RETURN (error_mark_node
);
19659 RETURN (build_x_arrow (input_location
, op1
, complain
));
19663 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19664 tree init
= RECUR (TREE_OPERAND (t
, 3));
19665 vec
<tree
, va_gc
> *placement_vec
;
19666 vec
<tree
, va_gc
> *init_vec
;
19668 location_t loc
= EXPR_LOCATION (t
);
19670 if (placement
== NULL_TREE
)
19671 placement_vec
= NULL
;
19672 else if (placement
== error_mark_node
)
19673 RETURN (error_mark_node
);
19676 placement_vec
= make_tree_vector ();
19677 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19678 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19681 /* If there was an initializer in the original tree, but it
19682 instantiated to an empty list, then we should pass a
19683 non-NULL empty vector to tell build_new that it was an
19684 empty initializer() rather than no initializer. This can
19685 only happen when the initializer is a pack expansion whose
19686 parameter packs are of length zero. */
19687 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19691 init_vec
= make_tree_vector ();
19692 if (init
== void_node
)
19693 gcc_assert (init_vec
!= NULL
);
19696 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19697 vec_safe_push (init_vec
, TREE_VALUE (init
));
19701 /* Avoid passing an enclosing decl to valid_array_size_p. */
19702 in_decl
= NULL_TREE
;
19704 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19705 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19706 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19707 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19710 if (placement_vec
!= NULL
)
19711 release_tree_vector (placement_vec
);
19712 if (init_vec
!= NULL
)
19713 release_tree_vector (init_vec
);
19720 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19721 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19722 RETURN (delete_sanity (input_location
, op0
, op1
,
19723 DELETE_EXPR_USE_VEC (t
),
19724 DELETE_EXPR_USE_GLOBAL (t
),
19728 case COMPOUND_EXPR
:
19730 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19731 complain
& ~tf_decltype
, in_decl
,
19732 /*function_p=*/false,
19733 integral_constant_expression_p
);
19734 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19736 RECUR (TREE_OPERAND (t
, 1)),
19737 complain
|decltype_flag
));
19743 unsigned int nargs
, i
;
19748 function
= CALL_EXPR_FN (t
);
19749 /* Internal function with no arguments. */
19750 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19753 /* When we parsed the expression, we determined whether or
19754 not Koenig lookup should be performed. */
19755 koenig_p
= KOENIG_LOOKUP_P (t
);
19756 if (function
== NULL_TREE
)
19759 qualified_p
= false;
19761 else if (TREE_CODE (function
) == SCOPE_REF
)
19763 qualified_p
= true;
19764 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19766 /*address_p=*/false);
19768 else if (koenig_p
&& identifier_p (function
))
19770 /* Do nothing; calling tsubst_copy_and_build on an identifier
19771 would incorrectly perform unqualified lookup again.
19773 Note that we can also have an IDENTIFIER_NODE if the earlier
19774 unqualified lookup found a member function; in that case
19775 koenig_p will be false and we do want to do the lookup
19776 again to find the instantiated member function.
19778 FIXME but doing that causes c++/15272, so we need to stop
19779 using IDENTIFIER_NODE in that situation. */
19780 qualified_p
= false;
19784 if (TREE_CODE (function
) == COMPONENT_REF
)
19786 tree op
= TREE_OPERAND (function
, 1);
19788 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19789 || (BASELINK_P (op
)
19790 && BASELINK_QUALIFIED_P (op
)));
19793 qualified_p
= false;
19795 if (TREE_CODE (function
) == ADDR_EXPR
19796 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19797 /* Avoid error about taking the address of a constructor. */
19798 function
= TREE_OPERAND (function
, 0);
19800 function
= tsubst_copy_and_build (function
, args
, complain
,
19803 integral_constant_expression_p
);
19805 if (BASELINK_P (function
))
19806 qualified_p
= true;
19809 nargs
= call_expr_nargs (t
);
19810 releasing_vec call_args
;
19811 for (i
= 0; i
< nargs
; ++i
)
19813 tree arg
= CALL_EXPR_ARG (t
, i
);
19815 if (!PACK_EXPANSION_P (arg
))
19816 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19819 /* Expand the pack expansion and push each entry onto
19821 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19822 if (TREE_CODE (arg
) == TREE_VEC
)
19824 unsigned int len
, j
;
19826 len
= TREE_VEC_LENGTH (arg
);
19827 for (j
= 0; j
< len
; ++j
)
19829 tree value
= TREE_VEC_ELT (arg
, j
);
19830 if (value
!= NULL_TREE
)
19831 value
= convert_from_reference (value
);
19832 vec_safe_push (call_args
, value
);
19837 /* A partial substitution. Add one entry. */
19838 vec_safe_push (call_args
, arg
);
19843 /* Stripped-down processing for a call in a thunk. Specifically, in
19844 the thunk template for a generic lambda. */
19845 if (CALL_FROM_THUNK_P (t
))
19847 /* Now that we've expanded any packs, the number of call args
19848 might be different. */
19849 unsigned int cargs
= call_args
->length ();
19850 tree thisarg
= NULL_TREE
;
19851 if (TREE_CODE (function
) == COMPONENT_REF
)
19853 thisarg
= TREE_OPERAND (function
, 0);
19854 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19855 thisarg
= TREE_OPERAND (thisarg
, 0);
19856 function
= TREE_OPERAND (function
, 1);
19857 if (TREE_CODE (function
) == BASELINK
)
19858 function
= BASELINK_FUNCTIONS (function
);
19860 /* We aren't going to do normal overload resolution, so force the
19861 template-id to resolve. */
19862 function
= resolve_nondeduced_context (function
, complain
);
19863 for (unsigned i
= 0; i
< cargs
; ++i
)
19865 /* In a thunk, pass through args directly, without any
19867 tree arg
= (*call_args
)[i
];
19868 while (TREE_CODE (arg
) != PARM_DECL
)
19869 arg
= TREE_OPERAND (arg
, 0);
19870 (*call_args
)[i
] = arg
;
19874 /* If there are no other args, just push 'this'. */
19876 vec_safe_push (call_args
, thisarg
);
19879 /* Otherwise, shift the other args over to make room. */
19880 tree last
= (*call_args
)[cargs
- 1];
19881 vec_safe_push (call_args
, last
);
19882 for (int i
= cargs
- 1; i
> 0; --i
)
19883 (*call_args
)[i
] = (*call_args
)[i
- 1];
19884 (*call_args
)[0] = thisarg
;
19887 ret
= build_call_a (function
, call_args
->length (),
19888 call_args
->address ());
19889 /* The thunk location is not interesting. */
19890 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19891 CALL_FROM_THUNK_P (ret
) = true;
19892 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19893 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19898 /* We do not perform argument-dependent lookup if normal
19899 lookup finds a non-function, in accordance with the
19900 expected resolution of DR 218. */
19902 && ((is_overloaded_fn (function
)
19903 /* If lookup found a member function, the Koenig lookup is
19904 not appropriate, even if an unqualified-name was used
19905 to denote the function. */
19906 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19907 || identifier_p (function
))
19908 /* Only do this when substitution turns a dependent call
19909 into a non-dependent call. */
19910 && type_dependent_expression_p_push (t
)
19911 && !any_type_dependent_arguments_p (call_args
))
19912 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19914 if (function
!= NULL_TREE
19915 && identifier_p (function
)
19916 && !any_type_dependent_arguments_p (call_args
))
19918 if (koenig_p
&& (complain
& tf_warning_or_error
))
19920 /* For backwards compatibility and good diagnostics, try
19921 the unqualified lookup again if we aren't in SFINAE
19923 tree unq
= (tsubst_copy_and_build
19924 (function
, args
, complain
, in_decl
, true,
19925 integral_constant_expression_p
));
19926 if (unq
== error_mark_node
)
19927 RETURN (error_mark_node
);
19929 if (unq
!= function
)
19931 /* In a lambda fn, we have to be careful to not
19932 introduce new this captures. Legacy code can't
19933 be using lambdas anyway, so it's ok to be
19935 bool in_lambda
= (current_class_type
19936 && LAMBDA_TYPE_P (current_class_type
));
19937 char const *const msg
19938 = G_("%qD was not declared in this scope, "
19939 "and no declarations were found by "
19940 "argument-dependent lookup at the point "
19941 "of instantiation");
19945 error_at (cp_expr_loc_or_input_loc (t
),
19948 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19954 if (INDIRECT_REF_P (fn
))
19955 fn
= TREE_OPERAND (fn
, 0);
19956 if (is_overloaded_fn (fn
))
19957 fn
= get_first_fn (fn
);
19960 /* Can't say anything more. */;
19961 else if (DECL_CLASS_SCOPE_P (fn
))
19963 location_t loc
= cp_expr_loc_or_input_loc (t
);
19965 "declarations in dependent base %qT are "
19966 "not found by unqualified lookup",
19967 DECL_CLASS_CONTEXT (fn
));
19968 if (current_class_ptr
)
19970 "use %<this->%D%> instead", function
);
19973 "use %<%T::%D%> instead",
19974 current_class_name
, function
);
19977 inform (DECL_SOURCE_LOCATION (fn
),
19978 "%qD declared here, later in the "
19979 "translation unit", fn
);
19981 RETURN (error_mark_node
);
19987 if (identifier_p (function
))
19989 if (complain
& tf_error
)
19990 unqualified_name_lookup_error (function
);
19991 RETURN (error_mark_node
);
19995 /* Remember that there was a reference to this entity. */
19996 if (function
!= NULL_TREE
19997 && DECL_P (function
)
19998 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19999 RETURN (error_mark_node
);
20001 /* Put back tf_decltype for the actual call. */
20002 complain
|= decltype_flag
;
20004 if (function
== NULL_TREE
)
20005 switch (CALL_EXPR_IFN (t
))
20008 gcc_assert (nargs
== 1);
20009 if (vec_safe_length (call_args
) != 1)
20011 error_at (cp_expr_loc_or_input_loc (t
),
20012 "wrong number of arguments to "
20013 "%<__builtin_launder%>");
20014 ret
= error_mark_node
;
20017 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20018 (*call_args
)[0], complain
);
20021 case IFN_VEC_CONVERT
:
20022 gcc_assert (nargs
== 1);
20023 if (vec_safe_length (call_args
) != 1)
20025 error_at (cp_expr_loc_or_input_loc (t
),
20026 "wrong number of arguments to "
20027 "%<__builtin_convertvector%>");
20028 ret
= error_mark_node
;
20031 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20032 tsubst (TREE_TYPE (t
), args
,
20033 complain
, in_decl
),
20035 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20040 /* Unsupported internal function with arguments. */
20041 gcc_unreachable ();
20043 else if (TREE_CODE (function
) == OFFSET_REF
20044 || TREE_CODE (function
) == DOTSTAR_EXPR
20045 || TREE_CODE (function
) == MEMBER_REF
)
20046 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20048 else if (TREE_CODE (function
) == COMPONENT_REF
)
20050 tree instance
= TREE_OPERAND (function
, 0);
20051 tree fn
= TREE_OPERAND (function
, 1);
20053 if (processing_template_decl
20054 && (type_dependent_expression_p (instance
)
20055 || (!BASELINK_P (fn
)
20056 && TREE_CODE (fn
) != FIELD_DECL
)
20057 || type_dependent_expression_p (fn
)
20058 || any_type_dependent_arguments_p (call_args
)))
20059 ret
= build_min_nt_call_vec (function
, call_args
);
20060 else if (!BASELINK_P (fn
))
20061 ret
= finish_call_expr (function
, &call_args
,
20062 /*disallow_virtual=*/false,
20063 /*koenig_p=*/false,
20066 ret
= (build_new_method_call
20068 &call_args
, NULL_TREE
,
20069 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20073 else if (concept_check_p (function
))
20075 /* FUNCTION is a template-id referring to a concept definition. */
20076 tree id
= unpack_concept_check (function
);
20077 tree tmpl
= TREE_OPERAND (id
, 0);
20078 tree args
= TREE_OPERAND (id
, 1);
20080 /* Calls to standard and variable concepts should have been
20081 previously diagnosed. */
20082 gcc_assert (function_concept_p (tmpl
));
20084 /* Ensure the result is wrapped as a call expression. */
20085 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20088 ret
= finish_call_expr (function
, &call_args
,
20089 /*disallow_virtual=*/qualified_p
,
20093 if (ret
!= error_mark_node
)
20095 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20096 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20097 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20098 if (op
|| ord
|| rev
)
20100 function
= extract_call_expr (ret
);
20101 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20102 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20103 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20112 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20113 cond
= mark_rvalue_use (cond
);
20114 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20117 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20119 if (integer_zerop (folded_cond
))
20121 ++c_inhibit_evaluation_warnings
;
20122 exp1
= RECUR (TREE_OPERAND (t
, 1));
20123 --c_inhibit_evaluation_warnings
;
20124 exp2
= RECUR (TREE_OPERAND (t
, 2));
20128 exp1
= RECUR (TREE_OPERAND (t
, 1));
20129 ++c_inhibit_evaluation_warnings
;
20130 exp2
= RECUR (TREE_OPERAND (t
, 2));
20131 --c_inhibit_evaluation_warnings
;
20133 cond
= folded_cond
;
20137 exp1
= RECUR (TREE_OPERAND (t
, 1));
20138 exp2
= RECUR (TREE_OPERAND (t
, 2));
20141 warning_sentinel
s(warn_duplicated_branches
);
20142 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20143 cond
, exp1
, exp2
, complain
));
20146 case PSEUDO_DTOR_EXPR
:
20148 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20149 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20150 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20151 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20156 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20158 case COMPONENT_REF
:
20165 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20166 args
, complain
, in_decl
);
20167 /* Remember that there was a reference to this entity. */
20168 if (DECL_P (object
)
20169 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20170 RETURN (error_mark_node
);
20171 object_type
= TREE_TYPE (object
);
20173 member
= TREE_OPERAND (t
, 1);
20174 if (BASELINK_P (member
))
20175 member
= tsubst_baselink (member
,
20176 non_reference (TREE_TYPE (object
)),
20177 args
, complain
, in_decl
);
20179 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20180 if (member
== error_mark_node
)
20181 RETURN (error_mark_node
);
20183 if (TREE_CODE (member
) == FIELD_DECL
)
20185 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20186 if (TREE_CODE (r
) == COMPONENT_REF
)
20187 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20190 else if (type_dependent_expression_p (object
))
20191 /* We can't do much here. */;
20192 else if (!CLASS_TYPE_P (object_type
))
20194 if (scalarish_type_p (object_type
))
20196 tree s
= NULL_TREE
;
20197 tree dtor
= member
;
20199 if (TREE_CODE (dtor
) == SCOPE_REF
)
20201 s
= TREE_OPERAND (dtor
, 0);
20202 dtor
= TREE_OPERAND (dtor
, 1);
20204 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20206 dtor
= TREE_OPERAND (dtor
, 0);
20208 RETURN (finish_pseudo_destructor_expr
20209 (object
, s
, dtor
, input_location
));
20213 else if (TREE_CODE (member
) == SCOPE_REF
20214 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20216 /* Lookup the template functions now that we know what the
20218 tree scope
= TREE_OPERAND (member
, 0);
20219 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20220 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20221 member
= lookup_qualified_name (scope
, tmpl
,
20222 /*is_type_p=*/false,
20223 /*complain=*/false);
20224 if (BASELINK_P (member
))
20226 BASELINK_FUNCTIONS (member
)
20227 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20229 member
= (adjust_result_of_qualified_name_lookup
20230 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20235 qualified_name_lookup_error (scope
, tmpl
, member
,
20237 RETURN (error_mark_node
);
20240 else if (TREE_CODE (member
) == SCOPE_REF
20241 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20242 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20244 if (complain
& tf_error
)
20246 if (TYPE_P (TREE_OPERAND (member
, 0)))
20247 error ("%qT is not a class or namespace",
20248 TREE_OPERAND (member
, 0));
20250 error ("%qD is not a class or namespace",
20251 TREE_OPERAND (member
, 0));
20253 RETURN (error_mark_node
);
20256 r
= finish_class_member_access_expr (object
, member
,
20257 /*template_p=*/false,
20259 if (TREE_CODE (r
) == COMPONENT_REF
)
20260 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20265 RETURN (build_throw
20266 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20270 vec
<constructor_elt
, va_gc
> *n
;
20271 constructor_elt
*ce
;
20272 unsigned HOST_WIDE_INT idx
;
20273 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20274 bool process_index_p
;
20276 bool need_copy_p
= false;
20279 if (type
== error_mark_node
)
20280 RETURN (error_mark_node
);
20282 /* We do not want to process the index of aggregate
20283 initializers as they are identifier nodes which will be
20284 looked up by digest_init. */
20285 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20287 if (null_member_pointer_value_p (t
))
20289 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20293 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20294 newlen
= vec_safe_length (n
);
20295 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20297 if (ce
->index
&& process_index_p
20298 /* An identifier index is looked up in the type
20299 being initialized, not the current scope. */
20300 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20301 ce
->index
= RECUR (ce
->index
);
20303 if (PACK_EXPANSION_P (ce
->value
))
20305 /* Substitute into the pack expansion. */
20306 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20309 if (ce
->value
== error_mark_node
20310 || PACK_EXPANSION_P (ce
->value
))
20312 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20313 /* Just move the argument into place. */
20314 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20317 /* Update the length of the final CONSTRUCTOR
20318 arguments vector, and note that we will need to
20320 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20321 need_copy_p
= true;
20325 ce
->value
= RECUR (ce
->value
);
20330 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20332 vec_alloc (n
, newlen
);
20333 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20335 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20337 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20338 for (i
= 0; i
< len
; ++i
)
20339 CONSTRUCTOR_APPEND_ELT (n
, 0,
20340 TREE_VEC_ELT (ce
->value
, i
));
20343 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20347 r
= build_constructor (init_list_type_node
, n
);
20348 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20349 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20350 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20352 if (TREE_HAS_CONSTRUCTOR (t
))
20354 fcl_t cl
= fcl_functional
;
20355 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20357 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20360 TREE_TYPE (r
) = type
;
20366 tree operand_0
= TREE_OPERAND (t
, 0);
20367 if (TYPE_P (operand_0
))
20369 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20370 RETURN (get_typeid (operand_0
, complain
));
20374 operand_0
= RECUR (operand_0
);
20375 RETURN (build_typeid (operand_0
, complain
));
20386 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20387 /* ??? We're doing a subset of finish_id_expression here. */
20388 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20389 /* Replace an evaluated use of the thread_local variable with
20390 a call to its wrapper. */
20392 else if (outer_automatic_var_p (r
))
20393 r
= process_outer_var_ref (r
, complain
);
20395 if (!TYPE_REF_P (TREE_TYPE (t
)))
20396 /* If the original type was a reference, we'll be wrapped in
20397 the appropriate INDIRECT_REF. */
20398 r
= convert_from_reference (r
);
20404 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20405 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20406 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20409 case OFFSETOF_EXPR
:
20412 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20413 in_decl
, /*function_p=*/false,
20414 /*integral_constant_expression_p=*/false);
20415 RETURN (finish_offsetof (object_ptr
,
20416 RECUR (TREE_OPERAND (t
, 0)),
20417 EXPR_LOCATION (t
)));
20420 case ADDRESSOF_EXPR
:
20421 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20422 RECUR (TREE_OPERAND (t
, 0)), complain
));
20426 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20427 complain
, in_decl
);
20428 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20429 complain
, in_decl
);
20430 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20431 TRAIT_EXPR_KIND (t
), type1
, type2
));
20436 tree old_stmt_expr
= cur_stmt_expr
;
20437 tree stmt_expr
= begin_stmt_expr ();
20439 cur_stmt_expr
= stmt_expr
;
20440 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20441 integral_constant_expression_p
);
20442 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20443 cur_stmt_expr
= old_stmt_expr
;
20445 /* If the resulting list of expression statement is empty,
20446 fold it further into void_node. */
20447 if (empty_expr_stmt_p (stmt_expr
))
20448 stmt_expr
= void_node
;
20450 RETURN (stmt_expr
);
20455 if (complain
& tf_partial
)
20457 /* We don't have a full set of template arguments yet; don't touch
20458 the lambda at all. */
20459 gcc_assert (processing_template_decl
);
20462 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20464 RETURN (build_lambda_object (r
));
20468 /* We can get here for a constant initializer of non-dependent type.
20469 FIXME stop folding in cp_parser_initializer_clause. */
20471 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20476 case TRANSACTION_EXPR
:
20477 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20478 integral_constant_expression_p
));
20481 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20483 case VEC_PERM_EXPR
:
20485 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20486 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20487 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20488 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20492 case REQUIRES_EXPR
:
20494 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20499 /* No need to substitute further, a RANGE_EXPR will always be built
20500 with constant operands. */
20503 case NON_LVALUE_EXPR
:
20504 case VIEW_CONVERT_EXPR
:
20505 if (location_wrapper_p (t
))
20506 /* We need to do this here as well as in tsubst_copy so we get the
20507 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20508 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20509 EXPR_LOCATION (t
)));
20513 /* Handle Objective-C++ constructs, if appropriate. */
20516 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20517 in_decl
, /*function_p=*/false);
20521 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20527 input_location
= save_loc
;
20531 /* Verify that the instantiated ARGS are valid. For type arguments,
20532 make sure that the type's linkage is ok. For non-type arguments,
20533 make sure they are constants if they are integral or enumerations.
20534 Emit an error under control of COMPLAIN, and return TRUE on error. */
20537 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20539 if (dependent_template_arg_p (t
))
20541 if (ARGUMENT_PACK_P (t
))
20543 tree vec
= ARGUMENT_PACK_ARGS (t
);
20544 int len
= TREE_VEC_LENGTH (vec
);
20545 bool result
= false;
20548 for (i
= 0; i
< len
; ++i
)
20549 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20553 else if (TYPE_P (t
))
20555 /* [basic.link]: A name with no linkage (notably, the name
20556 of a class or enumeration declared in a local scope)
20557 shall not be used to declare an entity with linkage.
20558 This implies that names with no linkage cannot be used as
20561 DR 757 relaxes this restriction for C++0x. */
20562 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20563 : no_linkage_check (t
, /*relaxed_p=*/false));
20567 /* DR 488 makes use of a type with no linkage cause
20568 type deduction to fail. */
20569 if (complain
& tf_error
)
20571 if (TYPE_UNNAMED_P (nt
))
20572 error ("%qT is/uses unnamed type", t
);
20574 error ("template argument for %qD uses local type %qT",
20579 /* In order to avoid all sorts of complications, we do not
20580 allow variably-modified types as template arguments. */
20581 else if (variably_modified_type_p (t
, NULL_TREE
))
20583 if (complain
& tf_error
)
20584 error ("%qT is a variably modified type", t
);
20588 /* Class template and alias template arguments should be OK. */
20589 else if (DECL_TYPE_TEMPLATE_P (t
))
20591 /* A non-type argument of integral or enumerated type must be a
20593 else if (TREE_TYPE (t
)
20594 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20595 && !REFERENCE_REF_P (t
)
20596 && !TREE_CONSTANT (t
))
20598 if (complain
& tf_error
)
20599 error ("integral expression %qE is not constant", t
);
20606 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20608 int ix
, len
= DECL_NTPARMS (tmpl
);
20609 bool result
= false;
20611 for (ix
= 0; ix
!= len
; ix
++)
20613 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20616 if (result
&& (complain
& tf_error
))
20617 error (" trying to instantiate %qD", tmpl
);
20621 /* We're out of SFINAE context now, so generate diagnostics for the access
20622 errors we saw earlier when instantiating D from TMPL and ARGS. */
20625 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20627 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20628 tree type
= TREE_TYPE (pattern
);
20629 location_t loc
= input_location
;
20631 push_access_scope (d
);
20632 push_deferring_access_checks (dk_no_deferred
);
20633 input_location
= DECL_SOURCE_LOCATION (pattern
);
20634 tsubst (type
, args
, tf_warning_or_error
, d
);
20635 input_location
= loc
;
20636 pop_deferring_access_checks ();
20637 pop_access_scope (d
);
20640 /* Instantiate the indicated variable, function, or alias template TMPL with
20641 the template arguments in TARG_PTR. */
20644 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20646 tree targ_ptr
= orig_args
;
20650 bool access_ok
= true;
20652 if (tmpl
== error_mark_node
)
20653 return error_mark_node
;
20655 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20657 /* If this function is a clone, handle it specially. */
20658 if (DECL_CLONED_FUNCTION_P (tmpl
))
20663 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20664 DECL_CLONED_FUNCTION. */
20665 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20666 targ_ptr
, complain
);
20667 if (spec
== error_mark_node
)
20668 return error_mark_node
;
20670 /* Look for the clone. */
20671 FOR_EACH_CLONE (clone
, spec
)
20672 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20674 /* We should always have found the clone by now. */
20675 gcc_unreachable ();
20679 if (targ_ptr
== error_mark_node
)
20680 return error_mark_node
;
20682 /* Check to see if we already have this specialization. */
20683 gen_tmpl
= most_general_template (tmpl
);
20684 if (TMPL_ARGS_DEPTH (targ_ptr
)
20685 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20686 /* targ_ptr only has the innermost template args, so add the outer ones
20687 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20688 the case of a non-dependent call within a template definition). */
20689 targ_ptr
= (add_outermost_template_args
20690 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20693 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20694 but it doesn't seem to be on the hot path. */
20695 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20697 gcc_checking_assert (tmpl
== gen_tmpl
20699 = retrieve_specialization (tmpl
, orig_args
, 0))
20701 || fndecl
== NULL_TREE
);
20703 if (spec
!= NULL_TREE
)
20705 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20707 if (complain
& tf_error
)
20708 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20709 return error_mark_node
;
20714 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20716 return error_mark_node
;
20718 /* We are building a FUNCTION_DECL, during which the access of its
20719 parameters and return types have to be checked. However this
20720 FUNCTION_DECL which is the desired context for access checking
20721 is not built yet. We solve this chicken-and-egg problem by
20722 deferring all checks until we have the FUNCTION_DECL. */
20723 push_deferring_access_checks (dk_deferred
);
20725 /* Instantiation of the function happens in the context of the function
20726 template, not the context of the overload resolution we're doing. */
20727 push_to_top_level ();
20728 /* If there are dependent arguments, e.g. because we're doing partial
20729 ordering, make sure processing_template_decl stays set. */
20730 if (uses_template_parms (targ_ptr
))
20731 ++processing_template_decl
;
20732 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20734 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20735 complain
, gen_tmpl
, true);
20736 push_nested_class (ctx
);
20739 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20741 fndecl
= NULL_TREE
;
20742 if (VAR_P (pattern
))
20744 /* We need to determine if we're using a partial or explicit
20745 specialization now, because the type of the variable could be
20747 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20748 tree elt
= most_specialized_partial_spec (tid
, complain
);
20749 if (elt
== error_mark_node
)
20750 pattern
= error_mark_node
;
20753 tree partial_tmpl
= TREE_VALUE (elt
);
20754 tree partial_args
= TREE_PURPOSE (elt
);
20755 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20756 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20760 /* Substitute template parameters to obtain the specialization. */
20761 if (fndecl
== NULL_TREE
)
20762 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20763 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20764 pop_nested_class ();
20765 pop_from_top_level ();
20767 if (fndecl
== error_mark_node
)
20769 pop_deferring_access_checks ();
20770 return error_mark_node
;
20773 /* The DECL_TI_TEMPLATE should always be the immediate parent
20774 template, not the most general template. */
20775 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20776 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20778 /* Now we know the specialization, compute access previously
20779 deferred. Do no access control for inheriting constructors,
20780 as we already checked access for the inherited constructor. */
20781 if (!(flag_new_inheriting_ctors
20782 && DECL_INHERITED_CTOR (fndecl
)))
20784 push_access_scope (fndecl
);
20785 if (!perform_deferred_access_checks (complain
))
20787 pop_access_scope (fndecl
);
20789 pop_deferring_access_checks ();
20791 /* If we've just instantiated the main entry point for a function,
20792 instantiate all the alternate entry points as well. We do this
20793 by cloning the instantiation of the main entry point, not by
20794 instantiating the template clones. */
20795 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20796 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20797 clone_cdtor (fndecl
, /*update_methods=*/false);
20801 if (!(complain
& tf_error
))
20803 /* Remember to reinstantiate when we're out of SFINAE so the user
20804 can see the errors. */
20805 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20807 return error_mark_node
;
20812 /* Wrapper for instantiate_template_1. */
20815 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20818 timevar_push (TV_TEMPLATE_INST
);
20819 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20820 timevar_pop (TV_TEMPLATE_INST
);
20824 /* Instantiate the alias template TMPL with ARGS. Also push a template
20825 instantiation level, which instantiate_template doesn't do because
20826 functions and variables have sufficient context established by the
20830 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20832 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20833 return error_mark_node
;
20836 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20837 args
, tmpl
, complain
,
20838 /*require_all_args=*/true,
20839 /*use_default_args=*/true);
20841 /* FIXME check for satisfaction in check_instantiated_args. */
20843 && !any_dependent_template_arguments_p (args
)
20844 && !constraints_satisfied_p (tmpl
, args
))
20846 if (complain
& tf_error
)
20848 auto_diagnostic_group d
;
20849 error ("template constraint failure for %qD", tmpl
);
20850 diagnose_constraints (input_location
, tmpl
, args
);
20852 return error_mark_node
;
20855 if (!push_tinst_level (tmpl
, args
))
20856 return error_mark_node
;
20857 tree r
= instantiate_template (tmpl
, args
, complain
);
20858 pop_tinst_level ();
20863 /* PARM is a template parameter pack for FN. Returns true iff
20864 PARM is used in a deducible way in the argument list of FN. */
20867 pack_deducible_p (tree parm
, tree fn
)
20869 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20870 for (; t
; t
= TREE_CHAIN (t
))
20872 tree type
= TREE_VALUE (t
);
20874 if (!PACK_EXPANSION_P (type
))
20876 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20877 packs
; packs
= TREE_CHAIN (packs
))
20878 if (template_args_equal (TREE_VALUE (packs
), parm
))
20880 /* The template parameter pack is used in a function parameter
20881 pack. If this is the end of the parameter list, the
20882 template parameter pack is deducible. */
20883 if (TREE_CHAIN (t
) == void_list_node
)
20886 /* Otherwise, not. Well, it could be deduced from
20887 a non-pack parameter, but doing so would end up with
20888 a deduction mismatch, so don't bother. */
20892 /* The template parameter pack isn't used in any function parameter
20893 packs, but it might be used deeper, e.g. tuple<Args...>. */
20897 /* Subroutine of fn_type_unification: check non-dependent parms for
20901 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20902 tree fn
, unification_kind_t strict
, int flags
,
20903 struct conversion
**convs
, bool explain_p
)
20905 /* Non-constructor methods need to leave a conversion for 'this', which
20906 isn't included in nargs here. */
20907 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20908 && !DECL_CONSTRUCTOR_P (fn
));
20910 for (unsigned ia
= 0;
20911 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20913 tree parm
= TREE_VALUE (parms
);
20915 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20916 && (!TREE_CHAIN (parms
)
20917 || TREE_CHAIN (parms
) == void_list_node
))
20918 /* For a function parameter pack that occurs at the end of the
20919 parameter-declaration-list, the type A of each remaining
20920 argument of the call is compared with the type P of the
20921 declarator-id of the function parameter pack. */
20924 parms
= TREE_CHAIN (parms
);
20926 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20927 /* For a function parameter pack that does not occur at the
20928 end of the parameter-declaration-list, the type of the
20929 parameter pack is a non-deduced context. */
20932 if (!uses_template_parms (parm
))
20934 tree arg
= args
[ia
];
20935 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20936 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20938 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20939 conv_p
, explain_p
))
20949 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20950 NARGS elements of the arguments that are being used when calling
20951 it. TARGS is a vector into which the deduced template arguments
20954 Returns either a FUNCTION_DECL for the matching specialization of FN or
20955 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20956 true, diagnostics will be printed to explain why it failed.
20958 If FN is a conversion operator, or we are trying to produce a specific
20959 specialization, RETURN_TYPE is the return type desired.
20961 The EXPLICIT_TARGS are explicit template arguments provided via a
20964 The parameter STRICT is one of:
20967 We are deducing arguments for a function call, as in
20968 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20969 deducing arguments for a call to the result of a conversion
20970 function template, as in [over.call.object].
20973 We are deducing arguments for a conversion function, as in
20974 [temp.deduct.conv].
20977 We are deducing arguments when doing an explicit instantiation
20978 as in [temp.explicit], when determining an explicit specialization
20979 as in [temp.expl.spec], or when taking the address of a function
20980 template, as in [temp.deduct.funcaddr]. */
20983 fn_type_unification (tree fn
,
20984 tree explicit_targs
,
20987 unsigned int nargs
,
20989 unification_kind_t strict
,
20991 struct conversion
**convs
,
20997 tree decl
= NULL_TREE
;
20998 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21000 static int deduction_depth
;
21001 /* type_unification_real will pass back any access checks from default
21002 template argument substitution. */
21003 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21004 /* We don't have all the template args yet. */
21005 bool incomplete
= true;
21008 if (flag_new_inheriting_ctors
)
21009 fn
= strip_inheriting_ctors (fn
);
21011 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21012 tree r
= error_mark_node
;
21014 tree full_targs
= targs
;
21015 if (TMPL_ARGS_DEPTH (targs
)
21016 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21017 full_targs
= (add_outermost_template_args
21018 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21022 complain
|= tf_decltype
;
21024 /* In C++0x, it's possible to have a function template whose type depends
21025 on itself recursively. This is most obvious with decltype, but can also
21026 occur with enumeration scope (c++/48969). So we need to catch infinite
21027 recursion and reject the substitution at deduction time; this function
21028 will return error_mark_node for any repeated substitution.
21030 This also catches excessive recursion such as when f<N> depends on
21031 f<N-1> across all integers, and returns error_mark_node for all the
21032 substitutions back up to the initial one.
21034 This is, of course, not reentrant. */
21035 if (excessive_deduction_depth
)
21036 return error_mark_node
;
21039 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21041 fntype
= TREE_TYPE (fn
);
21042 if (explicit_targs
)
21046 The specified template arguments must match the template
21047 parameters in kind (i.e., type, nontype, template), and there
21048 must not be more arguments than there are parameters;
21049 otherwise type deduction fails.
21051 Nontype arguments must match the types of the corresponding
21052 nontype template parameters, or must be convertible to the
21053 types of the corresponding nontype parameters as specified in
21054 _temp.arg.nontype_, otherwise type deduction fails.
21056 All references in the function type of the function template
21057 to the corresponding template parameters are replaced by the
21058 specified template argument values. If a substitution in a
21059 template parameter or in the function type of the function
21060 template results in an invalid type, type deduction fails. */
21061 int i
, len
= TREE_VEC_LENGTH (tparms
);
21062 location_t loc
= input_location
;
21063 incomplete
= false;
21065 if (explicit_targs
== error_mark_node
)
21068 if (TMPL_ARGS_DEPTH (explicit_targs
)
21069 < TMPL_ARGS_DEPTH (full_targs
))
21070 explicit_targs
= add_outermost_template_args (full_targs
,
21073 /* Adjust any explicit template arguments before entering the
21074 substitution context. */
21076 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21077 complain
|tf_partial
,
21078 /*require_all_args=*/false,
21079 /*use_default_args=*/false));
21080 if (explicit_targs
== error_mark_node
)
21083 /* Substitute the explicit args into the function type. This is
21084 necessary so that, for instance, explicitly declared function
21085 arguments can match null pointed constants. If we were given
21086 an incomplete set of explicit args, we must not do semantic
21087 processing during substitution as we could create partial
21089 for (i
= 0; i
< len
; i
++)
21091 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21092 bool parameter_pack
= false;
21093 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21095 /* Dig out the actual parm. */
21096 if (TREE_CODE (parm
) == TYPE_DECL
21097 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21099 parm
= TREE_TYPE (parm
);
21100 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21102 else if (TREE_CODE (parm
) == PARM_DECL
)
21104 parm
= DECL_INITIAL (parm
);
21105 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21108 if (targ
== NULL_TREE
)
21109 /* No explicit argument for this template parameter. */
21111 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21113 /* Mark the argument pack as "incomplete". We could
21114 still deduce more arguments during unification.
21115 We remove this mark in type_unification_real. */
21116 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21117 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21118 = ARGUMENT_PACK_ARGS (targ
);
21120 /* We have some incomplete argument packs. */
21127 if (!push_tinst_level (fn
, explicit_targs
))
21129 excessive_deduction_depth
= true;
21132 ++processing_template_decl
;
21133 input_location
= DECL_SOURCE_LOCATION (fn
);
21134 /* Ignore any access checks; we'll see them again in
21135 instantiate_template and they might have the wrong
21136 access path at this point. */
21137 push_deferring_access_checks (dk_deferred
);
21138 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21139 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21140 pop_deferring_access_checks ();
21141 input_location
= loc
;
21142 --processing_template_decl
;
21143 pop_tinst_level ();
21145 if (fntype
== error_mark_node
)
21149 /* Place the explicitly specified arguments in TARGS. */
21150 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21151 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21152 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21153 if (!incomplete
&& CHECKING_P
21154 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21156 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21159 if (return_type
&& strict
!= DEDUCE_CALL
)
21161 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21162 new_args
[0] = return_type
;
21163 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21171 /* Never do unification on the 'this' parameter. */
21172 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21174 if (return_type
&& strict
== DEDUCE_CALL
)
21176 /* We're deducing for a call to the result of a template conversion
21177 function. The parms we really want are in return_type. */
21178 if (INDIRECT_TYPE_P (return_type
))
21179 return_type
= TREE_TYPE (return_type
);
21180 parms
= TYPE_ARG_TYPES (return_type
);
21182 else if (return_type
)
21184 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21187 /* We allow incomplete unification without an error message here
21188 because the standard doesn't seem to explicitly prohibit it. Our
21189 callers must be ready to deal with unification failures in any
21192 /* If we aren't explaining yet, push tinst context so we can see where
21193 any errors (e.g. from class instantiations triggered by instantiation
21194 of default template arguments) come from. If we are explaining, this
21195 context is redundant. */
21196 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21198 excessive_deduction_depth
= true;
21202 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21203 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21204 strict
, &checks
, explain_p
);
21206 pop_tinst_level ();
21210 /* Now that we have bindings for all of the template arguments,
21211 ensure that the arguments deduced for the template template
21212 parameters have compatible template parameter lists. We cannot
21213 check this property before we have deduced all template
21214 arguments, because the template parameter types of a template
21215 template parameter might depend on prior template parameters
21216 deduced after the template template parameter. The following
21217 ill-formed example illustrates this issue:
21219 template<typename T, template<T> class C> void f(C<5>, T);
21221 template<int N> struct X {};
21224 f(X<5>(), 5l); // error: template argument deduction fails
21227 The template parameter list of 'C' depends on the template type
21228 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21229 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21230 time that we deduce 'C'. */
21231 if (!template_template_parm_bindings_ok_p
21232 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21234 unify_inconsistent_template_template_parameters (explain_p
);
21238 /* DR 1391: All parameters have args, now check non-dependent parms for
21240 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21245 /* All is well so far. Now, check:
21249 When all template arguments have been deduced, all uses of
21250 template parameters in nondeduced contexts are replaced with
21251 the corresponding deduced argument values. If the
21252 substitution results in an invalid type, as described above,
21253 type deduction fails. */
21254 if (!push_tinst_level (fn
, targs
))
21256 excessive_deduction_depth
= true;
21260 /* Also collect access checks from the instantiation. */
21261 reopen_deferring_access_checks (checks
);
21263 decl
= instantiate_template (fn
, targs
, complain
);
21265 checks
= get_deferred_access_checks ();
21266 pop_deferring_access_checks ();
21268 pop_tinst_level ();
21270 if (decl
== error_mark_node
)
21273 /* Now perform any access checks encountered during substitution. */
21274 push_access_scope (decl
);
21275 ok
= perform_access_checks (checks
, complain
);
21276 pop_access_scope (decl
);
21280 /* If we're looking for an exact match, check that what we got
21281 is indeed an exact match. It might not be if some template
21282 parameters are used in non-deduced contexts. But don't check
21283 for an exact match if we have dependent template arguments;
21284 in that case we're doing partial ordering, and we already know
21285 that we have two candidates that will provide the actual type. */
21286 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21288 tree substed
= TREE_TYPE (decl
);
21292 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21294 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21295 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21296 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21298 unify_type_mismatch (explain_p
, args
[i
],
21299 TREE_VALUE (sarg
));
21304 /* After doing deduction with the inherited constructor, actually return an
21305 instantiation of the inheriting constructor. */
21307 decl
= instantiate_template (orig_fn
, targs
, complain
);
21313 if (excessive_deduction_depth
)
21315 if (deduction_depth
== 0)
21316 /* Reset once we're all the way out. */
21317 excessive_deduction_depth
= false;
21323 /* Adjust types before performing type deduction, as described in
21324 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21325 sections are symmetric. PARM is the type of a function parameter
21326 or the return type of the conversion function. ARG is the type of
21327 the argument passed to the call, or the type of the value
21328 initialized with the result of the conversion function.
21329 ARG_EXPR is the original argument expression, which may be null. */
21332 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21345 /* Swap PARM and ARG throughout the remainder of this
21346 function; the handling is precisely symmetric since PARM
21347 will initialize ARG rather than vice versa. */
21348 std::swap (parm
, arg
);
21352 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21353 too, but here handle it by stripping the reference from PARM
21354 rather than by adding it to ARG. */
21355 if (TYPE_REF_P (*parm
)
21356 && TYPE_REF_IS_RVALUE (*parm
)
21357 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21358 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21359 && TYPE_REF_P (*arg
)
21360 && !TYPE_REF_IS_RVALUE (*arg
))
21361 *parm
= TREE_TYPE (*parm
);
21362 /* Nothing else to do in this case. */
21366 gcc_unreachable ();
21369 if (!TYPE_REF_P (*parm
))
21371 /* [temp.deduct.call]
21373 If P is not a reference type:
21375 --If A is an array type, the pointer type produced by the
21376 array-to-pointer standard conversion (_conv.array_) is
21377 used in place of A for type deduction; otherwise,
21379 --If A is a function type, the pointer type produced by
21380 the function-to-pointer standard conversion
21381 (_conv.func_) is used in place of A for type deduction;
21384 --If A is a cv-qualified type, the top level
21385 cv-qualifiers of A's type are ignored for type
21387 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21388 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21389 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21390 *arg
= build_pointer_type (*arg
);
21392 *arg
= TYPE_MAIN_VARIANT (*arg
);
21395 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21396 reference to a cv-unqualified template parameter that does not represent a
21397 template parameter of a class template (during class template argument
21398 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21399 an lvalue, the type "lvalue reference to A" is used in place of A for type
21401 if (TYPE_REF_P (*parm
)
21402 && TYPE_REF_IS_RVALUE (*parm
)
21403 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21404 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21405 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21406 && (arg_expr
? lvalue_p (arg_expr
)
21407 /* try_one_overload doesn't provide an arg_expr, but
21408 functions are always lvalues. */
21409 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21410 *arg
= build_reference_type (*arg
);
21412 /* [temp.deduct.call]
21414 If P is a cv-qualified type, the top level cv-qualifiers
21415 of P's type are ignored for type deduction. If P is a
21416 reference type, the type referred to by P is used for
21418 *parm
= TYPE_MAIN_VARIANT (*parm
);
21419 if (TYPE_REF_P (*parm
))
21421 *parm
= TREE_TYPE (*parm
);
21422 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21425 /* DR 322. For conversion deduction, remove a reference type on parm
21426 too (which has been swapped into ARG). */
21427 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21428 *arg
= TREE_TYPE (*arg
);
21433 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21434 template which doesn't contain any deducible template parameters; check if
21435 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21436 unify_one_argument. */
21439 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21440 int flags
, struct conversion
**conv_p
,
21446 type
= TREE_TYPE (arg
);
21450 if (same_type_p (parm
, type
))
21451 return unify_success (explain_p
);
21453 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21454 if (strict
== DEDUCE_CONV
)
21456 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21457 return unify_success (explain_p
);
21459 else if (strict
!= DEDUCE_EXACT
)
21462 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21464 /* Avoid recalculating this in add_function_candidate. */
21466 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21468 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21470 return unify_success (explain_p
);
21473 if (strict
== DEDUCE_EXACT
)
21474 return unify_type_mismatch (explain_p
, parm
, arg
);
21476 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21479 static bool uses_deducible_template_parms (tree type
);
21481 /* Returns true iff the expression EXPR is one from which a template
21482 argument can be deduced. In other words, if it's an undecorated
21483 use of a template non-type parameter. */
21486 deducible_expression (tree expr
)
21488 /* Strip implicit conversions. */
21489 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21490 expr
= TREE_OPERAND (expr
, 0);
21491 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21494 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21495 deducible way; that is, if it has a max value of <PARM> - 1. */
21498 deducible_array_bound (tree domain
)
21500 if (domain
== NULL_TREE
)
21503 tree max
= TYPE_MAX_VALUE (domain
);
21504 if (TREE_CODE (max
) != MINUS_EXPR
)
21507 return deducible_expression (TREE_OPERAND (max
, 0));
21510 /* Returns true iff the template arguments ARGS use a template parameter
21511 in a deducible way. */
21514 deducible_template_args (tree args
)
21516 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21519 tree elt
= TREE_VEC_ELT (args
, i
);
21520 if (ARGUMENT_PACK_P (elt
))
21521 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21524 if (PACK_EXPANSION_P (elt
))
21525 elt
= PACK_EXPANSION_PATTERN (elt
);
21526 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21528 else if (TYPE_P (elt
))
21529 deducible
= uses_deducible_template_parms (elt
);
21531 deducible
= deducible_expression (elt
);
21539 /* Returns true iff TYPE contains any deducible references to template
21540 parameters, as per 14.8.2.5. */
21543 uses_deducible_template_parms (tree type
)
21545 if (PACK_EXPANSION_P (type
))
21546 type
= PACK_EXPANSION_PATTERN (type
);
21553 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21554 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21560 if (INDIRECT_TYPE_P (type
))
21561 return uses_deducible_template_parms (TREE_TYPE (type
));
21563 /* T[integer-constant ]
21565 if (TREE_CODE (type
) == ARRAY_TYPE
)
21566 return (uses_deducible_template_parms (TREE_TYPE (type
))
21567 || deducible_array_bound (TYPE_DOMAIN (type
)));
21579 if (TYPE_PTRMEM_P (type
))
21580 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21581 || (uses_deducible_template_parms
21582 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21584 /* template-name <T> (where template-name refers to a class template)
21585 template-name <i> (where template-name refers to a class template) */
21586 if (CLASS_TYPE_P (type
)
21587 && CLASSTYPE_TEMPLATE_INFO (type
)
21588 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21589 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21590 (CLASSTYPE_TI_ARGS (type
)));
21595 if (FUNC_OR_METHOD_TYPE_P (type
))
21597 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21599 tree parm
= TYPE_ARG_TYPES (type
);
21600 if (TREE_CODE (type
) == METHOD_TYPE
)
21601 parm
= TREE_CHAIN (parm
);
21602 for (; parm
; parm
= TREE_CHAIN (parm
))
21603 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21610 /* Subroutine of type_unification_real and unify_pack_expansion to
21611 handle unification of a single P/A pair. Parameters are as
21612 for those functions. */
21615 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21616 int subr
, unification_kind_t strict
,
21619 tree arg_expr
= NULL_TREE
;
21622 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21623 return unify_invalid (explain_p
);
21624 if (arg
== unknown_type_node
)
21625 /* We can't deduce anything from this, but we might get all the
21626 template args from other function args. */
21627 return unify_success (explain_p
);
21629 /* Implicit conversions (Clause 4) will be performed on a function
21630 argument to convert it to the type of the corresponding function
21631 parameter if the parameter type contains no template-parameters that
21632 participate in template argument deduction. */
21633 if (strict
!= DEDUCE_EXACT
21634 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21635 /* For function parameters with no deducible template parameters,
21636 just return. We'll check non-dependent conversions later. */
21637 return unify_success (explain_p
);
21642 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21643 | UNIFY_ALLOW_MORE_CV_QUAL
21644 | UNIFY_ALLOW_DERIVED
);
21648 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21652 arg_strict
= UNIFY_ALLOW_NONE
;
21656 gcc_unreachable ();
21659 /* We only do these transformations if this is the top-level
21660 parameter_type_list in a call or declaration matching; in other
21661 situations (nested function declarators, template argument lists) we
21662 won't be comparing a type to an expression, and we don't do any type
21668 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21669 if (type_unknown_p (arg
))
21671 /* [temp.deduct.type] A template-argument can be
21672 deduced from a pointer to function or pointer
21673 to member function argument if the set of
21674 overloaded functions does not contain function
21675 templates and at most one of a set of
21676 overloaded functions provides a unique
21678 resolve_overloaded_unification (tparms
, targs
, parm
,
21680 arg_strict
, explain_p
);
21681 /* If a unique match was not found, this is a
21682 non-deduced context, so we still succeed. */
21683 return unify_success (explain_p
);
21687 arg
= unlowered_expr_type (arg
);
21688 if (arg
== error_mark_node
)
21689 return unify_invalid (explain_p
);
21693 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21696 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21697 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21698 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21700 /* For deduction from an init-list we need the actual list. */
21701 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21703 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21706 /* for_each_template_parm callback that always returns 0. */
21709 zero_r (tree
, void *)
21714 /* for_each_template_parm any_fn callback to handle deduction of a template
21715 type argument from the type of an array bound. */
21718 array_deduction_r (tree t
, void *data
)
21720 tree_pair_p d
= (tree_pair_p
)data
;
21721 tree
&tparms
= d
->purpose
;
21722 tree
&targs
= d
->value
;
21724 if (TREE_CODE (t
) == ARRAY_TYPE
)
21725 if (tree dom
= TYPE_DOMAIN (t
))
21726 if (tree max
= TYPE_MAX_VALUE (dom
))
21728 if (TREE_CODE (max
) == MINUS_EXPR
)
21729 max
= TREE_OPERAND (max
, 0);
21730 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21731 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21732 UNIFY_ALLOW_NONE
, /*explain*/false);
21735 /* Keep walking. */
21739 /* Try to deduce any not-yet-deduced template type arguments from the type of
21740 an array bound. This is handled separately from unify because 14.8.2.5 says
21741 "The type of a type parameter is only deduced from an array bound if it is
21742 not otherwise deduced." */
21745 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21747 tree_pair_s data
= { tparms
, targs
};
21748 hash_set
<tree
> visited
;
21749 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21750 /*nondeduced*/false, array_deduction_r
);
21753 /* Most parms like fn_type_unification.
21755 If SUBR is 1, we're being called recursively (to unify the
21756 arguments of a function or method parameter of a function
21759 CHECKS is a pointer to a vector of access checks encountered while
21760 substituting default template arguments. */
21763 type_unification_real (tree tparms
,
21767 unsigned int xnargs
,
21769 unification_kind_t strict
,
21770 vec
<deferred_access_check
, va_gc
> **checks
,
21775 int ntparms
= TREE_VEC_LENGTH (tparms
);
21776 int saw_undeduced
= 0;
21779 unsigned int nargs
;
21782 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21783 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21784 gcc_assert (ntparms
> 0);
21786 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21788 /* Reset the number of non-defaulted template arguments contained
21790 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21798 while (parms
&& parms
!= void_list_node
21801 parm
= TREE_VALUE (parms
);
21803 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21804 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21805 /* For a function parameter pack that occurs at the end of the
21806 parameter-declaration-list, the type A of each remaining
21807 argument of the call is compared with the type P of the
21808 declarator-id of the function parameter pack. */
21811 parms
= TREE_CHAIN (parms
);
21813 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21814 /* For a function parameter pack that does not occur at the
21815 end of the parameter-declaration-list, the type of the
21816 parameter pack is a non-deduced context. */
21822 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21828 && parms
!= void_list_node
21829 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21831 /* Unify the remaining arguments with the pack expansion type. */
21833 tree parmvec
= make_tree_vec (1);
21835 /* Allocate a TREE_VEC and copy in all of the arguments */
21836 argvec
= make_tree_vec (nargs
- ia
);
21837 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21838 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21840 /* Copy the parameter into parmvec. */
21841 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21842 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21843 /*subr=*/subr
, explain_p
))
21846 /* Advance to the end of the list of parameters. */
21847 parms
= TREE_CHAIN (parms
);
21850 /* Fail if we've reached the end of the parm list, and more args
21851 are present, and the parm list isn't variadic. */
21852 if (ia
< nargs
&& parms
== void_list_node
)
21853 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21854 /* Fail if parms are left and they don't have default values and
21855 they aren't all deduced as empty packs (c++/57397). This is
21856 consistent with sufficient_parms_p. */
21857 if (parms
&& parms
!= void_list_node
21858 && TREE_PURPOSE (parms
) == NULL_TREE
)
21860 unsigned int count
= nargs
;
21865 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21868 p
= TREE_CHAIN (p
);
21870 while (p
&& p
!= void_list_node
);
21871 if (count
!= nargs
)
21872 return unify_too_few_arguments (explain_p
, ia
, count
,
21878 tsubst_flags_t complain
= (explain_p
21879 ? tf_warning_or_error
21881 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21883 for (i
= 0; i
< ntparms
; i
++)
21885 tree targ
= TREE_VEC_ELT (targs
, i
);
21886 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21888 /* Clear the "incomplete" flags on all argument packs now so that
21889 substituting them into later default arguments works. */
21890 if (targ
&& ARGUMENT_PACK_P (targ
))
21892 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21893 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21896 if (targ
|| tparm
== error_mark_node
)
21898 tparm
= TREE_VALUE (tparm
);
21900 if (TREE_CODE (tparm
) == TYPE_DECL
21901 && !tried_array_deduction
)
21903 try_array_deduction (tparms
, targs
, xparms
);
21904 tried_array_deduction
= true;
21905 if (TREE_VEC_ELT (targs
, i
))
21909 /* If this is an undeduced nontype parameter that depends on
21910 a type parameter, try another pass; its type may have been
21911 deduced from a later argument than the one from which
21912 this parameter can be deduced. */
21913 if (TREE_CODE (tparm
) == PARM_DECL
21914 && uses_template_parms (TREE_TYPE (tparm
))
21915 && saw_undeduced
< 2)
21921 /* Core issue #226 (C++0x) [temp.deduct]:
21923 If a template argument has not been deduced, its
21924 default template argument, if any, is used.
21926 When we are in C++98 mode, TREE_PURPOSE will either
21927 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21928 to explicitly check cxx_dialect here. */
21929 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21930 /* OK, there is a default argument. Wait until after the
21931 conversion check to do substitution. */
21934 /* If the type parameter is a parameter pack, then it will
21935 be deduced to an empty parameter pack. */
21936 if (template_parameter_pack_p (tparm
))
21940 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21942 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21943 TREE_CONSTANT (arg
) = 1;
21946 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21948 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21950 TREE_VEC_ELT (targs
, i
) = arg
;
21954 return unify_parameter_deduction_failure (explain_p
, tparm
);
21957 /* Now substitute into the default template arguments. */
21958 for (i
= 0; i
< ntparms
; i
++)
21960 tree targ
= TREE_VEC_ELT (targs
, i
);
21961 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21963 if (targ
|| tparm
== error_mark_node
)
21965 tree parm
= TREE_VALUE (tparm
);
21966 tree arg
= TREE_PURPOSE (tparm
);
21967 reopen_deferring_access_checks (*checks
);
21968 location_t save_loc
= input_location
;
21970 input_location
= DECL_SOURCE_LOCATION (parm
);
21972 if (saw_undeduced
== 1
21973 && TREE_CODE (parm
) == PARM_DECL
21974 && uses_template_parms (TREE_TYPE (parm
)))
21976 /* The type of this non-type parameter depends on undeduced
21977 parameters. Don't try to use its default argument yet,
21978 since we might deduce an argument for it on the next pass,
21979 but do check whether the arguments we already have cause
21980 substitution failure, so that that happens before we try
21981 later default arguments (78489). */
21982 ++processing_template_decl
;
21983 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21985 --processing_template_decl
;
21986 if (type
== error_mark_node
)
21987 arg
= error_mark_node
;
21993 /* Even if the call is happening in template context, getting
21994 here means it's non-dependent, and a default argument is
21995 considered a separate definition under [temp.decls], so we can
21996 do this substitution without processing_template_decl. This
21997 is important if the default argument contains something that
21998 might be instantiation-dependent like access (87480). */
21999 processing_template_decl_sentinel s
;
22000 tree substed
= NULL_TREE
;
22001 if (saw_undeduced
== 1)
22003 /* First instatiate in template context, in case we still
22004 depend on undeduced template parameters. */
22005 ++processing_template_decl
;
22006 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22008 --processing_template_decl
;
22009 if (substed
!= error_mark_node
22010 && !uses_template_parms (substed
))
22011 /* We replaced all the tparms, substitute again out of
22012 template context. */
22013 substed
= NULL_TREE
;
22016 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22019 if (!uses_template_parms (substed
))
22020 arg
= convert_template_argument (parm
, substed
, full_targs
,
22021 complain
, i
, NULL_TREE
);
22022 else if (saw_undeduced
== 1)
22025 arg
= error_mark_node
;
22028 input_location
= save_loc
;
22029 *checks
= get_deferred_access_checks ();
22030 pop_deferring_access_checks ();
22032 if (arg
== error_mark_node
)
22036 TREE_VEC_ELT (targs
, i
) = arg
;
22037 /* The position of the first default template argument,
22038 is also the number of non-defaulted arguments in TARGS.
22040 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22041 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22045 if (saw_undeduced
++ == 1)
22049 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22050 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22052 return unify_success (explain_p
);
22055 /* Subroutine of type_unification_real. Args are like the variables
22056 at the call site. ARG is an overloaded function (or template-id);
22057 we try deducing template args from each of the overloads, and if
22058 only one succeeds, we go with that. Modifies TARGS and returns
22059 true on success. */
22062 resolve_overloaded_unification (tree tparms
,
22066 unification_kind_t strict
,
22070 tree tempargs
= copy_node (targs
);
22072 tree goodfn
= NULL_TREE
;
22075 if (TREE_CODE (arg
) == ADDR_EXPR
)
22077 arg
= TREE_OPERAND (arg
, 0);
22083 if (TREE_CODE (arg
) == COMPONENT_REF
)
22084 /* Handle `&x' where `x' is some static or non-static member
22086 arg
= TREE_OPERAND (arg
, 1);
22088 if (TREE_CODE (arg
) == OFFSET_REF
)
22089 arg
= TREE_OPERAND (arg
, 1);
22091 /* Strip baselink information. */
22092 if (BASELINK_P (arg
))
22093 arg
= BASELINK_FUNCTIONS (arg
);
22095 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22097 /* If we got some explicit template args, we need to plug them into
22098 the affected templates before we try to unify, in case the
22099 explicit args will completely resolve the templates in question. */
22102 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22103 arg
= TREE_OPERAND (arg
, 0);
22105 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22108 tree subargs
, elem
;
22110 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22113 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22114 expl_subargs
, NULL_TREE
, tf_none
,
22115 /*require_all_args=*/true,
22116 /*use_default_args=*/true);
22117 if (subargs
!= error_mark_node
22118 && !any_dependent_template_arguments_p (subargs
))
22120 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
22121 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22122 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22123 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22132 /* If no templates (or more than one) are fully resolved by the
22133 explicit arguments, this template-id is a non-deduced context; it
22134 could still be OK if we deduce all template arguments for the
22135 enclosing call through other arguments. */
22139 else if (!OVL_P (arg
))
22140 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22141 -- but the deduction does not succeed because the expression is
22142 not just the function on its own. */
22145 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22148 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22149 strict
, sub_strict
, addr_p
, explain_p
)
22150 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22157 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22158 to function or pointer to member function argument if the set of
22159 overloaded functions does not contain function templates and at most
22160 one of a set of overloaded functions provides a unique match.
22162 So if we found multiple possibilities, we return success but don't
22163 deduce anything. */
22167 int i
= TREE_VEC_LENGTH (targs
);
22169 if (TREE_VEC_ELT (tempargs
, i
))
22171 tree old
= TREE_VEC_ELT (targs
, i
);
22172 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22173 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22174 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22175 /* Don't forget explicit template arguments in a pack. */
22176 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22177 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22178 TREE_VEC_ELT (targs
, i
) = new_
;
22187 /* Core DR 115: In contexts where deduction is done and fails, or in
22188 contexts where deduction is not done, if a template argument list is
22189 specified and it, along with any default template arguments, identifies
22190 a single function template specialization, then the template-id is an
22191 lvalue for the function template specialization. */
22194 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22196 tree expr
, offset
, baselink
;
22199 if (!type_unknown_p (orig_expr
))
22204 offset
= NULL_TREE
;
22205 baselink
= NULL_TREE
;
22207 if (TREE_CODE (expr
) == ADDR_EXPR
)
22209 expr
= TREE_OPERAND (expr
, 0);
22212 if (TREE_CODE (expr
) == OFFSET_REF
)
22215 expr
= TREE_OPERAND (expr
, 1);
22217 if (BASELINK_P (expr
))
22220 expr
= BASELINK_FUNCTIONS (expr
);
22223 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22226 tree goodfn
= NULL_TREE
;
22228 /* If we got some explicit template args, we need to plug them into
22229 the affected templates before we try to unify, in case the
22230 explicit args will completely resolve the templates in question. */
22232 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22233 tree arg
= TREE_OPERAND (expr
, 0);
22234 tree badfn
= NULL_TREE
;
22235 tree badargs
= NULL_TREE
;
22237 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22240 tree subargs
, elem
;
22242 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22245 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22246 expl_subargs
, NULL_TREE
, tf_none
,
22247 /*require_all_args=*/true,
22248 /*use_default_args=*/true);
22249 if (subargs
!= error_mark_node
22250 && !any_dependent_template_arguments_p (subargs
))
22252 elem
= instantiate_template (fn
, subargs
, tf_none
);
22253 if (elem
== error_mark_node
)
22258 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
22267 mark_used (goodfn
);
22270 expr
= build_baselink (BASELINK_BINFO (baselink
),
22271 BASELINK_ACCESS_BINFO (baselink
),
22272 expr
, BASELINK_OPTYPE (baselink
));
22276 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22277 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22280 expr
= cp_build_addr_expr (expr
, complain
);
22283 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22284 /* There were no good options and at least one bad one, so let the
22285 user know what the problem is. */
22286 instantiate_template (badfn
, badargs
, complain
);
22291 /* As above, but error out if the expression remains overloaded. */
22294 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22296 exp
= resolve_nondeduced_context (exp
, complain
);
22297 if (type_unknown_p (exp
))
22299 if (complain
& tf_error
)
22300 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22301 return error_mark_node
;
22306 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22307 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22308 different overloads deduce different arguments for a given parm.
22309 ADDR_P is true if the expression for which deduction is being
22310 performed was of the form "& fn" rather than simply "fn".
22312 Returns 1 on success. */
22315 try_one_overload (tree tparms
,
22320 unification_kind_t strict
,
22329 if (arg
== error_mark_node
)
22332 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22333 to function or pointer to member function argument if the set of
22334 overloaded functions does not contain function templates and at most
22335 one of a set of overloaded functions provides a unique match.
22337 So if this is a template, just return success. */
22339 if (uses_template_parms (arg
))
22342 if (TREE_CODE (arg
) == METHOD_TYPE
)
22343 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22345 arg
= build_pointer_type (arg
);
22347 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22349 /* We don't copy orig_targs for this because if we have already deduced
22350 some template args from previous args, unify would complain when we
22351 try to deduce a template parameter for the same argument, even though
22352 there isn't really a conflict. */
22353 nargs
= TREE_VEC_LENGTH (targs
);
22354 tempargs
= make_tree_vec (nargs
);
22356 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22359 /* First make sure we didn't deduce anything that conflicts with
22360 explicitly specified args. */
22361 for (i
= nargs
; i
--; )
22363 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22364 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22368 else if (uses_template_parms (elt
))
22369 /* Since we're unifying against ourselves, we will fill in
22370 template args used in the function parm list with our own
22371 template parms. Discard them. */
22372 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22373 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22375 /* Check that the argument at each index of the deduced argument pack
22376 is equivalent to the corresponding explicitly specified argument.
22377 We may have deduced more arguments than were explicitly specified,
22380 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22381 that's wrong if we deduce the same argument pack from multiple
22382 function arguments: it's only incomplete the first time. */
22384 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22385 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22387 if (TREE_VEC_LENGTH (deduced_pack
)
22388 < TREE_VEC_LENGTH (explicit_pack
))
22391 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22392 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22393 TREE_VEC_ELT (deduced_pack
, j
)))
22396 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22400 for (i
= nargs
; i
--; )
22402 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22405 TREE_VEC_ELT (targs
, i
) = elt
;
22411 /* PARM is a template class (perhaps with unbound template
22412 parameters). ARG is a fully instantiated type. If ARG can be
22413 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22414 TARGS are as for unify. */
22417 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22420 tree copy_of_targs
;
22422 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22424 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22425 /* Matches anything. */;
22426 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22427 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22430 /* We need to make a new template argument vector for the call to
22431 unify. If we used TARGS, we'd clutter it up with the result of
22432 the attempted unification, even if this class didn't work out.
22433 We also don't want to commit ourselves to all the unifications
22434 we've already done, since unification is supposed to be done on
22435 an argument-by-argument basis. In other words, consider the
22436 following pathological case:
22438 template <int I, int J, int K>
22441 template <int I, int J>
22442 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22444 template <int I, int J, int K>
22445 void f(S<I, J, K>, S<I, I, I>);
22454 Now, by the time we consider the unification involving `s2', we
22455 already know that we must have `f<0, 0, 0>'. But, even though
22456 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22457 because there are two ways to unify base classes of S<0, 1, 2>
22458 with S<I, I, I>. If we kept the already deduced knowledge, we
22459 would reject the possibility I=1. */
22460 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22462 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22464 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22469 /* If unification failed, we're done. */
22470 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22471 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22477 /* Given a template type PARM and a class type ARG, find the unique
22478 base type in ARG that is an instance of PARM. We do not examine
22479 ARG itself; only its base-classes. If there is not exactly one
22480 appropriate base class, return NULL_TREE. PARM may be the type of
22481 a partial specialization, as well as a plain template type. Used
22484 static enum template_base_result
22485 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22486 bool explain_p
, tree
*result
)
22488 tree rval
= NULL_TREE
;
22491 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22493 binfo
= TYPE_BINFO (complete_type (arg
));
22496 /* The type could not be completed. */
22497 *result
= NULL_TREE
;
22498 return tbr_incomplete_type
;
22501 /* Walk in inheritance graph order. The search order is not
22502 important, and this avoids multiple walks of virtual bases. */
22503 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22505 tree r
= try_class_unification (tparms
, targs
, parm
,
22506 BINFO_TYPE (binfo
), explain_p
);
22510 /* If there is more than one satisfactory baseclass, then:
22514 If they yield more than one possible deduced A, the type
22518 if (rval
&& !same_type_p (r
, rval
))
22520 *result
= NULL_TREE
;
22521 return tbr_ambiguous_baseclass
;
22529 return tbr_success
;
22532 /* Returns the level of DECL, which declares a template parameter. */
22535 template_decl_level (tree decl
)
22537 switch (TREE_CODE (decl
))
22540 case TEMPLATE_DECL
:
22541 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22544 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22547 gcc_unreachable ();
22552 /* Decide whether ARG can be unified with PARM, considering only the
22553 cv-qualifiers of each type, given STRICT as documented for unify.
22554 Returns nonzero iff the unification is OK on that basis. */
22557 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22559 int arg_quals
= cp_type_quals (arg
);
22560 int parm_quals
= cp_type_quals (parm
);
22562 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22563 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22565 /* Although a CVR qualifier is ignored when being applied to a
22566 substituted template parameter ([8.3.2]/1 for example), that
22567 does not allow us to unify "const T" with "int&" because both
22568 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22569 It is ok when we're allowing additional CV qualifiers
22570 at the outer level [14.8.2.1]/3,1st bullet. */
22571 if ((TYPE_REF_P (arg
)
22572 || FUNC_OR_METHOD_TYPE_P (arg
))
22573 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22576 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22577 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22581 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22582 && (arg_quals
& parm_quals
) != parm_quals
)
22585 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22586 && (parm_quals
& arg_quals
) != arg_quals
)
22592 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22594 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22596 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22597 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22598 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22600 *index
= TEMPLATE_TYPE_IDX (parm
);
22601 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22605 *index
= TEMPLATE_PARM_IDX (parm
);
22606 *level
= TEMPLATE_PARM_LEVEL (parm
);
22610 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22612 if (unify (TP, TA, P, A, S, EP)) \
22616 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22617 expansion at the end of PACKED_PARMS. Returns 0 if the type
22618 deduction succeeds, 1 otherwise. STRICT is the same as in
22619 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22620 function call argument list. We'll need to adjust the arguments to make them
22621 types. SUBR tells us if this is from a recursive call to
22622 type_unification_real, or for comparing two template argument
22626 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22627 tree packed_args
, unification_kind_t strict
,
22628 bool subr
, bool explain_p
)
22631 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22632 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22633 tree pack
, packs
= NULL_TREE
;
22634 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22636 /* Add in any args remembered from an earlier partial instantiation. */
22637 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22638 int levels
= TMPL_ARGS_DEPTH (targs
);
22640 packed_args
= expand_template_argument_pack (packed_args
);
22642 int len
= TREE_VEC_LENGTH (packed_args
);
22644 /* Determine the parameter packs we will be deducing from the
22645 pattern, and record their current deductions. */
22646 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22647 pack
; pack
= TREE_CHAIN (pack
))
22649 tree parm_pack
= TREE_VALUE (pack
);
22652 /* Only template parameter packs can be deduced, not e.g. function
22653 parameter packs or __bases or __integer_pack. */
22654 if (!TEMPLATE_PARM_P (parm_pack
))
22657 /* Determine the index and level of this parameter pack. */
22658 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22659 if (level
< levels
)
22662 /* Keep track of the parameter packs and their corresponding
22664 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22665 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22668 /* Loop through all of the arguments that have not yet been
22669 unified and unify each with the pattern. */
22670 for (i
= start
; i
< len
; i
++)
22673 bool any_explicit
= false;
22674 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22676 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22677 or the element of its argument pack at the current index if
22678 this argument was explicitly specified. */
22679 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22683 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22686 if (TREE_VALUE (pack
)
22687 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22688 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22690 any_explicit
= true;
22691 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22693 TMPL_ARG (targs
, level
, idx
) = arg
;
22696 /* If we had explicit template arguments, substitute them into the
22697 pattern before deduction. */
22700 /* Some arguments might still be unspecified or dependent. */
22702 ++processing_template_decl
;
22703 dependent
= any_dependent_template_arguments_p (targs
);
22705 --processing_template_decl
;
22706 parm
= tsubst (pattern
, targs
,
22707 explain_p
? tf_warning_or_error
: tf_none
,
22710 --processing_template_decl
;
22711 if (parm
== error_mark_node
)
22717 /* Unify the pattern with the current argument. */
22718 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22722 /* For each parameter pack, collect the deduced value. */
22723 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22726 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22728 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22729 TMPL_ARG (targs
, level
, idx
);
22733 /* Verify that the results of unification with the parameter packs
22734 produce results consistent with what we've seen before, and make
22735 the deduced argument packs available. */
22736 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22738 tree old_pack
= TREE_VALUE (pack
);
22739 tree new_args
= TREE_TYPE (pack
);
22740 int i
, len
= TREE_VEC_LENGTH (new_args
);
22742 bool nondeduced_p
= false;
22744 /* By default keep the original deduced argument pack.
22745 If necessary, more specific code is going to update the
22746 resulting deduced argument later down in this function. */
22747 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22748 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22750 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22751 actually deduce anything. */
22752 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22753 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22754 nondeduced_p
= true;
22758 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22760 /* If we had fewer function args than explicit template args,
22761 just use the explicits. */
22762 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22763 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22764 if (len
< explicit_len
)
22765 new_args
= explicit_args
;
22771 /* Build the deduced *_ARGUMENT_PACK. */
22772 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22774 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22775 TREE_CONSTANT (result
) = 1;
22778 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22780 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22782 /* Note the deduced argument packs for this parameter
22784 TMPL_ARG (targs
, level
, idx
) = result
;
22786 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22787 && (ARGUMENT_PACK_ARGS (old_pack
)
22788 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22790 /* We only had the explicitly-provided arguments before, but
22791 now we have a complete set of arguments. */
22792 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22794 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22795 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22796 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22800 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22801 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22802 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22803 /* During template argument deduction for the aggregate deduction
22804 candidate, the number of elements in a trailing parameter pack
22805 is only deduced from the number of remaining function
22806 arguments if it is not otherwise deduced. */
22807 if (cxx_dialect
>= cxx20
22808 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
22809 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
22810 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
22811 if (!comp_template_args (old_args
, new_args
,
22812 &bad_old_arg
, &bad_new_arg
))
22813 /* Inconsistent unification of this parameter pack. */
22814 return unify_parameter_pack_inconsistent (explain_p
,
22820 return unify_success (explain_p
);
22823 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22824 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22825 parameters and return value are as for unify. */
22828 unify_array_domain (tree tparms
, tree targs
,
22829 tree parm_dom
, tree arg_dom
,
22837 /* Our representation of array types uses "N - 1" as the
22838 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22839 not an integer constant. We cannot unify arbitrarily
22840 complex expressions, so we eliminate the MINUS_EXPRs
22842 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22843 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22846 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22847 parm_max
= TREE_OPERAND (parm_max
, 0);
22849 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22850 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22853 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22854 trying to unify the type of a variable with the type
22855 of a template parameter. For example:
22857 template <unsigned int N>
22858 void f (char (&) [N]);
22865 Here, the type of the ARG will be "int [g(i)]", and
22866 may be a SAVE_EXPR, etc. */
22867 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22868 return unify_vla_arg (explain_p
, arg_dom
);
22869 arg_max
= TREE_OPERAND (arg_max
, 0);
22872 /* If only one of the bounds used a MINUS_EXPR, compensate
22873 by adding one to the other bound. */
22874 if (parm_cst
&& !arg_cst
)
22875 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22879 else if (arg_cst
&& !parm_cst
)
22880 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22885 return unify (tparms
, targs
, parm_max
, arg_max
,
22886 UNIFY_ALLOW_INTEGER
, explain_p
);
22889 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22891 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22896 if (PACK_EXPANSION_P (t
))
22897 t
= PACK_EXPANSION_PATTERN (t
);
22898 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22899 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22900 || DECL_TYPE_TEMPLATE_P (t
))
22902 else if (TYPE_P (t
))
22908 /* Deduce the value of template parameters. TPARMS is the (innermost)
22909 set of template parameters to a template. TARGS is the bindings
22910 for those template parameters, as determined thus far; TARGS may
22911 include template arguments for outer levels of template parameters
22912 as well. PARM is a parameter to a template function, or a
22913 subcomponent of that parameter; ARG is the corresponding argument.
22914 This function attempts to match PARM with ARG in a manner
22915 consistent with the existing assignments in TARGS. If more values
22916 are deduced, then TARGS is updated.
22918 Returns 0 if the type deduction succeeds, 1 otherwise. The
22919 parameter STRICT is a bitwise or of the following flags:
22922 Require an exact match between PARM and ARG.
22923 UNIFY_ALLOW_MORE_CV_QUAL:
22924 Allow the deduced ARG to be more cv-qualified (by qualification
22925 conversion) than ARG.
22926 UNIFY_ALLOW_LESS_CV_QUAL:
22927 Allow the deduced ARG to be less cv-qualified than ARG.
22928 UNIFY_ALLOW_DERIVED:
22929 Allow the deduced ARG to be a template base class of ARG,
22930 or a pointer to a template base class of the type pointed to by
22932 UNIFY_ALLOW_INTEGER:
22933 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22934 case for more information.
22935 UNIFY_ALLOW_OUTER_LEVEL:
22936 This is the outermost level of a deduction. Used to determine validity
22937 of qualification conversions. A valid qualification conversion must
22938 have const qualified pointers leading up to the inner type which
22939 requires additional CV quals, except at the outer level, where const
22940 is not required [conv.qual]. It would be normal to set this flag in
22941 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22942 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22943 This is the outermost level of a deduction, and PARM can be more CV
22944 qualified at this point.
22945 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22946 This is the outermost level of a deduction, and PARM can be less CV
22947 qualified at this point. */
22950 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22956 int strict_in
= strict
;
22957 tsubst_flags_t complain
= (explain_p
22958 ? tf_warning_or_error
22961 /* I don't think this will do the right thing with respect to types.
22962 But the only case I've seen it in so far has been array bounds, where
22963 signedness is the only information lost, and I think that will be
22964 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22965 finish_id_expression_1, and are also OK. */
22966 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22967 parm
= TREE_OPERAND (parm
, 0);
22969 if (arg
== error_mark_node
)
22970 return unify_invalid (explain_p
);
22971 if (arg
== unknown_type_node
22972 || arg
== init_list_type_node
)
22973 /* We can't deduce anything from this, but we might get all the
22974 template args from other function args. */
22975 return unify_success (explain_p
);
22977 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22978 return unify_success (explain_p
);
22980 /* If PARM uses template parameters, then we can't bail out here,
22981 even if ARG == PARM, since we won't record unifications for the
22982 template parameters. We might need them if we're trying to
22983 figure out which of two things is more specialized. */
22984 if (arg
== parm
&& !uses_template_parms (parm
))
22985 return unify_success (explain_p
);
22987 /* Handle init lists early, so the rest of the function can assume
22988 we're dealing with a type. */
22989 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22993 tree orig_parm
= parm
;
22995 if (!is_std_init_list (parm
)
22996 && TREE_CODE (parm
) != ARRAY_TYPE
)
22997 /* We can only deduce from an initializer list argument if the
22998 parameter is std::initializer_list or an array; otherwise this
22999 is a non-deduced context. */
23000 return unify_success (explain_p
);
23002 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23003 elttype
= TREE_TYPE (parm
);
23006 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23007 /* Deduction is defined in terms of a single type, so just punt
23008 on the (bizarre) std::initializer_list<T...>. */
23009 if (PACK_EXPANSION_P (elttype
))
23010 return unify_success (explain_p
);
23013 if (strict
!= DEDUCE_EXACT
23014 && TYPE_P (elttype
)
23015 && !uses_deducible_template_parms (elttype
))
23016 /* If ELTTYPE has no deducible template parms, skip deduction from
23017 the list elements. */;
23019 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23021 int elt_strict
= strict
;
23023 if (elt
== error_mark_node
)
23024 return unify_invalid (explain_p
);
23026 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23028 tree type
= TREE_TYPE (elt
);
23029 if (type
== error_mark_node
)
23030 return unify_invalid (explain_p
);
23031 /* It should only be possible to get here for a call. */
23032 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23033 elt_strict
|= maybe_adjust_types_for_deduction
23034 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23038 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23042 if (TREE_CODE (parm
) == ARRAY_TYPE
23043 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23045 /* Also deduce from the length of the initializer list. */
23046 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23047 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23048 if (idx
== error_mark_node
)
23049 return unify_invalid (explain_p
);
23050 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23054 /* If the std::initializer_list<T> deduction worked, replace the
23055 deduced A with std::initializer_list<A>. */
23056 if (orig_parm
!= parm
)
23058 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23059 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23060 targ
= listify (targ
);
23061 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23063 return unify_success (explain_p
);
23066 /* If parm and arg aren't the same kind of thing (template, type, or
23067 expression), fail early. */
23068 if (pa_kind (parm
) != pa_kind (arg
))
23069 return unify_invalid (explain_p
);
23071 /* Immediately reject some pairs that won't unify because of
23072 cv-qualification mismatches. */
23073 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23075 /* It is the elements of the array which hold the cv quals of an array
23076 type, and the elements might be template type parms. We'll check
23077 when we recurse. */
23078 && TREE_CODE (arg
) != ARRAY_TYPE
23079 /* We check the cv-qualifiers when unifying with template type
23080 parameters below. We want to allow ARG `const T' to unify with
23081 PARM `T' for example, when computing which of two templates
23082 is more specialized, for example. */
23083 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23084 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23085 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23087 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23088 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23089 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23090 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23091 strict
&= ~UNIFY_ALLOW_DERIVED
;
23092 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23093 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23095 switch (TREE_CODE (parm
))
23097 case TYPENAME_TYPE
:
23099 case UNBOUND_CLASS_TEMPLATE
:
23100 /* In a type which contains a nested-name-specifier, template
23101 argument values cannot be deduced for template parameters used
23102 within the nested-name-specifier. */
23103 return unify_success (explain_p
);
23105 case TEMPLATE_TYPE_PARM
:
23106 case TEMPLATE_TEMPLATE_PARM
:
23107 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23108 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23109 if (error_operand_p (tparm
))
23110 return unify_invalid (explain_p
);
23112 if (TEMPLATE_TYPE_LEVEL (parm
)
23113 != template_decl_level (tparm
))
23114 /* The PARM is not one we're trying to unify. Just check
23115 to see if it matches ARG. */
23117 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23118 && (is_auto (parm
) ? is_auto (arg
)
23119 : same_type_p (parm
, arg
)))
23120 return unify_success (explain_p
);
23122 return unify_type_mismatch (explain_p
, parm
, arg
);
23124 idx
= TEMPLATE_TYPE_IDX (parm
);
23125 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23126 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23127 if (error_operand_p (tparm
))
23128 return unify_invalid (explain_p
);
23130 /* Check for mixed types and values. */
23131 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23132 && TREE_CODE (tparm
) != TYPE_DECL
)
23133 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23134 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23135 gcc_unreachable ();
23137 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23139 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23140 && CLASS_TYPE_P (arg
))
23142 /* First try to match ARG directly. */
23143 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23147 /* Otherwise, look for a suitable base of ARG, as below. */
23148 enum template_base_result r
;
23149 r
= get_template_base (tparms
, targs
, parm
, arg
,
23152 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23156 /* ARG must be constructed from a template class or a template
23157 template parameter. */
23158 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23159 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23160 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23162 /* Deduce arguments T, i from TT<T> or TT<i>. */
23163 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23166 arg
= TYPE_TI_TEMPLATE (arg
);
23168 /* Fall through to deduce template name. */
23171 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23172 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23174 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23176 /* Simple cases: Value already set, does match or doesn't. */
23177 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23178 return unify_success (explain_p
);
23180 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23184 /* If PARM is `const T' and ARG is only `int', we don't have
23185 a match unless we are allowing additional qualification.
23186 If ARG is `const int' and PARM is just `T' that's OK;
23187 that binds `const int' to `T'. */
23188 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23190 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23192 /* Consider the case where ARG is `const volatile int' and
23193 PARM is `const T'. Then, T should be `volatile int'. */
23194 arg
= cp_build_qualified_type_real
23195 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23196 if (arg
== error_mark_node
)
23197 return unify_invalid (explain_p
);
23199 /* Simple cases: Value already set, does match or doesn't. */
23200 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23201 return unify_success (explain_p
);
23203 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23205 /* Make sure that ARG is not a variable-sized array. (Note
23206 that were talking about variable-sized arrays (like
23207 `int[n]'), rather than arrays of unknown size (like
23208 `int[]').) We'll get very confused by such a type since
23209 the bound of the array is not constant, and therefore
23210 not mangleable. Besides, such types are not allowed in
23211 ISO C++, so we can do as we please here. We do allow
23212 them for 'auto' deduction, since that isn't ABI-exposed. */
23213 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23214 return unify_vla_arg (explain_p
, arg
);
23216 /* Strip typedefs as in convert_template_argument. */
23217 arg
= canonicalize_type_argument (arg
, tf_none
);
23220 /* If ARG is a parameter pack or an expansion, we cannot unify
23221 against it unless PARM is also a parameter pack. */
23222 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23223 && !template_parameter_pack_p (parm
))
23224 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23226 /* If the argument deduction results is a METHOD_TYPE,
23227 then there is a problem.
23228 METHOD_TYPE doesn't map to any real C++ type the result of
23229 the deduction cannot be of that type. */
23230 if (TREE_CODE (arg
) == METHOD_TYPE
)
23231 return unify_method_type_error (explain_p
, arg
);
23233 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23234 return unify_success (explain_p
);
23236 case TEMPLATE_PARM_INDEX
:
23237 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23238 if (error_operand_p (tparm
))
23239 return unify_invalid (explain_p
);
23241 if (TEMPLATE_PARM_LEVEL (parm
)
23242 != template_decl_level (tparm
))
23244 /* The PARM is not one we're trying to unify. Just check
23245 to see if it matches ARG. */
23246 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23247 && cp_tree_equal (parm
, arg
));
23249 unify_expression_unequal (explain_p
, parm
, arg
);
23253 idx
= TEMPLATE_PARM_IDX (parm
);
23254 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23258 if ((strict
& UNIFY_ALLOW_INTEGER
)
23259 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23260 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23261 /* We're deducing from an array bound, the type doesn't matter. */
23262 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23263 int x
= !cp_tree_equal (targ
, arg
);
23265 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23269 /* [temp.deduct.type] If, in the declaration of a function template
23270 with a non-type template-parameter, the non-type
23271 template-parameter is used in an expression in the function
23272 parameter-list and, if the corresponding template-argument is
23273 deduced, the template-argument type shall match the type of the
23274 template-parameter exactly, except that a template-argument
23275 deduced from an array bound may be of any integral type.
23276 The non-type parameter might use already deduced type parameters. */
23277 tparm
= TREE_TYPE (parm
);
23278 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23279 /* We don't have enough levels of args to do any substitution. This
23280 can happen in the context of -fnew-ttp-matching. */;
23283 ++processing_template_decl
;
23284 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23285 --processing_template_decl
;
23287 if (tree a
= type_uses_auto (tparm
))
23289 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23290 if (tparm
== error_mark_node
)
23295 if (!TREE_TYPE (arg
))
23296 /* Template-parameter dependent expression. Just accept it for now.
23297 It will later be processed in convert_template_argument. */
23299 else if (same_type_ignoring_top_level_qualifiers_p
23300 (non_reference (TREE_TYPE (arg
)),
23301 non_reference (tparm
)))
23302 /* OK. Ignore top-level quals here because a class-type template
23303 parameter object is const. */;
23304 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23305 && CP_INTEGRAL_TYPE_P (tparm
))
23306 /* Convert the ARG to the type of PARM; the deduced non-type
23307 template argument must exactly match the types of the
23308 corresponding parameter. */
23309 arg
= fold (build_nop (tparm
, arg
));
23310 else if (uses_template_parms (tparm
))
23312 /* We haven't deduced the type of this parameter yet. */
23313 if (cxx_dialect
>= cxx17
23314 /* We deduce from array bounds in try_array_deduction. */
23315 && !(strict
& UNIFY_ALLOW_INTEGER
))
23317 /* Deduce it from the non-type argument. */
23318 tree atype
= TREE_TYPE (arg
);
23319 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23321 UNIFY_ALLOW_NONE
, explain_p
);
23324 /* Try again later. */
23325 return unify_success (explain_p
);
23328 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23330 /* If ARG is a parameter pack or an expansion, we cannot unify
23331 against it unless PARM is also a parameter pack. */
23332 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23333 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23334 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23337 bool removed_attr
= false;
23338 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23341 return unify_success (explain_p
);
23345 /* A pointer-to-member constant can be unified only with
23346 another constant. */
23347 if (TREE_CODE (arg
) != PTRMEM_CST
)
23348 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23350 /* Just unify the class member. It would be useless (and possibly
23351 wrong, depending on the strict flags) to unify also
23352 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23353 arg refer to the same variable, even if through different
23354 classes. For instance:
23356 struct A { int x; };
23359 Unification of &A::x and &B::x must succeed. */
23360 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23361 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23366 if (!TYPE_PTR_P (arg
))
23367 return unify_type_mismatch (explain_p
, parm
, arg
);
23369 /* [temp.deduct.call]
23371 A can be another pointer or pointer to member type that can
23372 be converted to the deduced A via a qualification
23373 conversion (_conv.qual_).
23375 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23376 This will allow for additional cv-qualification of the
23377 pointed-to types if appropriate. */
23379 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23380 /* The derived-to-base conversion only persists through one
23381 level of pointers. */
23382 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23384 return unify (tparms
, targs
, TREE_TYPE (parm
),
23385 TREE_TYPE (arg
), strict
, explain_p
);
23388 case REFERENCE_TYPE
:
23389 if (!TYPE_REF_P (arg
))
23390 return unify_type_mismatch (explain_p
, parm
, arg
);
23391 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23392 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23395 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23396 return unify_type_mismatch (explain_p
, parm
, arg
);
23397 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23398 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23399 return unify_type_mismatch (explain_p
, parm
, arg
);
23400 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23401 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23402 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23403 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23404 TYPE_DOMAIN (arg
), explain_p
);
23405 return unify_success (explain_p
);
23412 case ENUMERAL_TYPE
:
23415 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23416 return unify_type_mismatch (explain_p
, parm
, arg
);
23418 /* We have already checked cv-qualification at the top of the
23420 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23421 return unify_type_mismatch (explain_p
, parm
, arg
);
23423 /* As far as unification is concerned, this wins. Later checks
23424 will invalidate it if necessary. */
23425 return unify_success (explain_p
);
23427 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23428 /* Type INTEGER_CST can come from ordinary constant template args. */
23430 while (CONVERT_EXPR_P (arg
))
23431 arg
= TREE_OPERAND (arg
, 0);
23433 if (TREE_CODE (arg
) != INTEGER_CST
)
23434 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23435 return (tree_int_cst_equal (parm
, arg
)
23436 ? unify_success (explain_p
)
23437 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23441 int i
, len
, argslen
;
23442 int parm_variadic_p
= 0;
23444 if (TREE_CODE (arg
) != TREE_VEC
)
23445 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23447 len
= TREE_VEC_LENGTH (parm
);
23448 argslen
= TREE_VEC_LENGTH (arg
);
23450 /* Check for pack expansions in the parameters. */
23451 for (i
= 0; i
< len
; ++i
)
23453 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23456 /* We can unify against something with a trailing
23458 parm_variadic_p
= 1;
23460 /* [temp.deduct.type]/9: If the template argument list of
23461 P contains a pack expansion that is not the last
23462 template argument, the entire template argument list
23463 is a non-deduced context. */
23464 return unify_success (explain_p
);
23468 /* If we don't have enough arguments to satisfy the parameters
23469 (not counting the pack expression at the end), or we have
23470 too many arguments for a parameter list that doesn't end in
23471 a pack expression, we can't unify. */
23472 if (parm_variadic_p
23473 ? argslen
< len
- parm_variadic_p
23475 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23477 /* Unify all of the parameters that precede the (optional)
23478 pack expression. */
23479 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23481 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23482 TREE_VEC_ELT (parm
, i
),
23483 TREE_VEC_ELT (arg
, i
),
23484 UNIFY_ALLOW_NONE
, explain_p
);
23486 if (parm_variadic_p
)
23487 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23489 /*subr=*/true, explain_p
);
23490 return unify_success (explain_p
);
23495 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23496 return unify_type_mismatch (explain_p
, parm
, arg
);
23498 if (TYPE_PTRMEMFUNC_P (parm
))
23500 if (!TYPE_PTRMEMFUNC_P (arg
))
23501 return unify_type_mismatch (explain_p
, parm
, arg
);
23503 return unify (tparms
, targs
,
23504 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23505 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23506 strict
, explain_p
);
23508 else if (TYPE_PTRMEMFUNC_P (arg
))
23509 return unify_type_mismatch (explain_p
, parm
, arg
);
23511 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23513 tree t
= NULL_TREE
;
23515 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23517 /* First, we try to unify the PARM and ARG directly. */
23518 t
= try_class_unification (tparms
, targs
,
23519 parm
, arg
, explain_p
);
23523 /* Fallback to the special case allowed in
23524 [temp.deduct.call]:
23526 If P is a class, and P has the form
23527 template-id, then A can be a derived class of
23528 the deduced A. Likewise, if P is a pointer to
23529 a class of the form template-id, A can be a
23530 pointer to a derived class pointed to by the
23532 enum template_base_result r
;
23533 r
= get_template_base (tparms
, targs
, parm
, arg
,
23538 /* Don't give the derived diagnostic if we're
23539 already dealing with the same template. */
23541 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23542 && (CLASSTYPE_TI_TEMPLATE (parm
)
23543 == CLASSTYPE_TI_TEMPLATE (arg
)));
23544 return unify_no_common_base (explain_p
&& !same_template
,
23549 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23550 && (CLASSTYPE_TI_TEMPLATE (parm
)
23551 == CLASSTYPE_TI_TEMPLATE (arg
)))
23552 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23553 Then, we should unify `int' and `U'. */
23556 /* There's no chance of unification succeeding. */
23557 return unify_type_mismatch (explain_p
, parm
, arg
);
23559 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23560 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23562 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23563 return unify_type_mismatch (explain_p
, parm
, arg
);
23564 return unify_success (explain_p
);
23567 case FUNCTION_TYPE
:
23569 unsigned int nargs
;
23574 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23575 return unify_type_mismatch (explain_p
, parm
, arg
);
23577 /* CV qualifications for methods can never be deduced, they must
23578 match exactly. We need to check them explicitly here,
23579 because type_unification_real treats them as any other
23580 cv-qualified parameter. */
23581 if (TREE_CODE (parm
) == METHOD_TYPE
23582 && (!check_cv_quals_for_unify
23584 class_of_this_parm (arg
),
23585 class_of_this_parm (parm
))))
23586 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23587 if (TREE_CODE (arg
) == FUNCTION_TYPE
23588 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23589 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23590 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23591 return unify_type_mismatch (explain_p
, parm
, arg
);
23593 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23594 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23596 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23597 args
= XALLOCAVEC (tree
, nargs
);
23598 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23599 a
!= NULL_TREE
&& a
!= void_list_node
;
23600 a
= TREE_CHAIN (a
), ++i
)
23601 args
[i
] = TREE_VALUE (a
);
23604 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23605 args
, nargs
, 1, DEDUCE_EXACT
,
23609 if (flag_noexcept_type
)
23611 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23612 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23613 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23614 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23615 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23616 && uses_template_parms (TREE_PURPOSE (pspec
)))
23617 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23618 TREE_PURPOSE (aspec
),
23619 UNIFY_ALLOW_NONE
, explain_p
);
23620 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23621 return unify_type_mismatch (explain_p
, parm
, arg
);
23628 /* Unify a pointer to member with a pointer to member function, which
23629 deduces the type of the member as a function type. */
23630 if (TYPE_PTRMEMFUNC_P (arg
))
23632 /* Check top-level cv qualifiers */
23633 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23634 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23636 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23637 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23638 UNIFY_ALLOW_NONE
, explain_p
);
23640 /* Determine the type of the function we are unifying against. */
23641 tree fntype
= static_fn_type (arg
);
23643 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23646 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23647 return unify_type_mismatch (explain_p
, parm
, arg
);
23648 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23649 TYPE_OFFSET_BASETYPE (arg
),
23650 UNIFY_ALLOW_NONE
, explain_p
);
23651 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23652 strict
, explain_p
);
23655 if (DECL_TEMPLATE_PARM_P (parm
))
23656 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23657 if (arg
!= scalar_constant_value (parm
))
23658 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23659 return unify_success (explain_p
);
23662 case TEMPLATE_DECL
:
23663 /* Matched cases are handled by the ARG == PARM test above. */
23664 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23667 /* We might get a variable as a non-type template argument in parm if the
23668 corresponding parameter is type-dependent. Make any necessary
23669 adjustments based on whether arg is a reference. */
23670 if (CONSTANT_CLASS_P (arg
))
23671 parm
= fold_non_dependent_expr (parm
, complain
);
23672 else if (REFERENCE_REF_P (arg
))
23674 tree sub
= TREE_OPERAND (arg
, 0);
23676 if (TREE_CODE (sub
) == ADDR_EXPR
)
23677 arg
= TREE_OPERAND (sub
, 0);
23679 /* Now use the normal expression code to check whether they match. */
23682 case TYPE_ARGUMENT_PACK
:
23683 case NONTYPE_ARGUMENT_PACK
:
23684 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23685 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23688 case DECLTYPE_TYPE
:
23689 case UNDERLYING_TYPE
:
23690 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23691 or UNDERLYING_TYPE nodes. */
23692 return unify_success (explain_p
);
23695 /* Unification fails if we hit an error node. */
23696 return unify_invalid (explain_p
);
23699 if (REFERENCE_REF_P (parm
))
23701 bool pexp
= PACK_EXPANSION_P (arg
);
23703 arg
= PACK_EXPANSION_PATTERN (arg
);
23704 if (REFERENCE_REF_P (arg
))
23705 arg
= TREE_OPERAND (arg
, 0);
23707 arg
= make_pack_expansion (arg
, complain
);
23708 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23709 strict
, explain_p
);
23714 /* An unresolved overload is a nondeduced context. */
23715 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23716 return unify_success (explain_p
);
23717 gcc_assert (EXPR_P (parm
)
23718 || COMPOUND_LITERAL_P (parm
)
23719 || TREE_CODE (parm
) == TRAIT_EXPR
);
23721 /* We must be looking at an expression. This can happen with
23725 void foo(S<I>, S<I + 2>);
23729 template<typename T>
23730 void foo(A<T, T{}>);
23732 This is a "non-deduced context":
23736 The non-deduced contexts are:
23738 --A non-type template argument or an array bound in which
23739 a subexpression references a template parameter.
23741 In these cases, we assume deduction succeeded, but don't
23742 actually infer any unifications. */
23744 if (!uses_template_parms (parm
)
23745 && !template_args_equal (parm
, arg
))
23746 return unify_expression_unequal (explain_p
, parm
, arg
);
23748 return unify_success (explain_p
);
23751 #undef RECUR_AND_CHECK_FAILURE
23753 /* Note that DECL can be defined in this translation unit, if
23757 mark_definable (tree decl
)
23760 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23761 FOR_EACH_CLONE (clone
, decl
)
23762 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23765 /* Called if RESULT is explicitly instantiated, or is a member of an
23766 explicitly instantiated class. */
23769 mark_decl_instantiated (tree result
, int extern_p
)
23771 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23773 /* If this entity has already been written out, it's too late to
23774 make any modifications. */
23775 if (TREE_ASM_WRITTEN (result
))
23778 /* For anonymous namespace we don't need to do anything. */
23779 if (decl_anon_ns_mem_p (result
))
23781 gcc_assert (!TREE_PUBLIC (result
));
23785 if (TREE_CODE (result
) != FUNCTION_DECL
)
23786 /* The TREE_PUBLIC flag for function declarations will have been
23787 set correctly by tsubst. */
23788 TREE_PUBLIC (result
) = 1;
23790 /* This might have been set by an earlier implicit instantiation. */
23791 DECL_COMDAT (result
) = 0;
23794 DECL_NOT_REALLY_EXTERN (result
) = 0;
23797 mark_definable (result
);
23798 mark_needed (result
);
23799 /* Always make artificials weak. */
23800 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23801 comdat_linkage (result
);
23802 /* For WIN32 we also want to put explicit instantiations in
23803 linkonce sections. */
23804 else if (TREE_PUBLIC (result
))
23805 maybe_make_one_only (result
);
23806 if (TREE_CODE (result
) == FUNCTION_DECL
23807 && DECL_TEMPLATE_INSTANTIATED (result
))
23808 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23809 since start_preparsed_function wouldn't have if we had an earlier
23810 extern explicit instantiation. */
23811 DECL_EXTERNAL (result
) = 0;
23814 /* If EXTERN_P, then this function will not be emitted -- unless
23815 followed by an explicit instantiation, at which point its linkage
23816 will be adjusted. If !EXTERN_P, then this function will be
23817 emitted here. In neither circumstance do we want
23818 import_export_decl to adjust the linkage. */
23819 DECL_INTERFACE_KNOWN (result
) = 1;
23822 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23823 important template arguments. If any are missing, we check whether
23824 they're important by using error_mark_node for substituting into any
23825 args that were used for partial ordering (the ones between ARGS and END)
23826 and seeing if it bubbles up. */
23829 check_undeduced_parms (tree targs
, tree args
, tree end
)
23831 bool found
= false;
23833 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23834 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23837 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23841 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23842 if (substed
== error_mark_node
)
23848 /* Given two function templates PAT1 and PAT2, return:
23850 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23851 -1 if PAT2 is more specialized than PAT1.
23852 0 if neither is more specialized.
23854 LEN indicates the number of parameters we should consider
23855 (defaulted parameters should not be considered).
23857 The 1998 std underspecified function template partial ordering, and
23858 DR214 addresses the issue. We take pairs of arguments, one from
23859 each of the templates, and deduce them against each other. One of
23860 the templates will be more specialized if all the *other*
23861 template's arguments deduce against its arguments and at least one
23862 of its arguments *does* *not* deduce against the other template's
23863 corresponding argument. Deduction is done as for class templates.
23864 The arguments used in deduction have reference and top level cv
23865 qualifiers removed. Iff both arguments were originally reference
23866 types *and* deduction succeeds in both directions, an lvalue reference
23867 wins against an rvalue reference and otherwise the template
23868 with the more cv-qualified argument wins for that pairing (if
23869 neither is more cv-qualified, they both are equal). Unlike regular
23870 deduction, after all the arguments have been deduced in this way,
23871 we do *not* verify the deduced template argument values can be
23872 substituted into non-deduced contexts.
23874 The logic can be a bit confusing here, because we look at deduce1 and
23875 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23876 can find template arguments for pat1 to make arg1 look like arg2, that
23877 means that arg2 is at least as specialized as arg1. */
23880 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23882 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23883 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23884 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23885 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23886 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23887 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23888 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23889 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23890 tree origs1
, origs2
;
23891 bool lose1
= false;
23892 bool lose2
= false;
23894 /* Remove the this parameter from non-static member functions. If
23895 one is a non-static member function and the other is not a static
23896 member function, remove the first parameter from that function
23897 also. This situation occurs for operator functions where we
23898 locate both a member function (with this pointer) and non-member
23899 operator (with explicit first operand). */
23900 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23902 len
--; /* LEN is the number of significant arguments for DECL1 */
23903 args1
= TREE_CHAIN (args1
);
23904 if (!DECL_STATIC_FUNCTION_P (decl2
))
23905 args2
= TREE_CHAIN (args2
);
23907 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23909 args2
= TREE_CHAIN (args2
);
23910 if (!DECL_STATIC_FUNCTION_P (decl1
))
23913 args1
= TREE_CHAIN (args1
);
23917 /* If only one is a conversion operator, they are unordered. */
23918 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23921 /* Consider the return type for a conversion function */
23922 if (DECL_CONV_FN_P (decl1
))
23924 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23925 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23929 processing_template_decl
++;
23935 /* Stop when an ellipsis is seen. */
23936 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23938 tree arg1
= TREE_VALUE (args1
);
23939 tree arg2
= TREE_VALUE (args2
);
23940 int deduce1
, deduce2
;
23946 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23947 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23949 /* When both arguments are pack expansions, we need only
23950 unify the patterns themselves. */
23951 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23952 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23954 /* This is the last comparison we need to do. */
23958 if (TYPE_REF_P (arg1
))
23960 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23961 arg1
= TREE_TYPE (arg1
);
23962 quals1
= cp_type_quals (arg1
);
23965 if (TYPE_REF_P (arg2
))
23967 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23968 arg2
= TREE_TYPE (arg2
);
23969 quals2
= cp_type_quals (arg2
);
23972 arg1
= TYPE_MAIN_VARIANT (arg1
);
23973 arg2
= TYPE_MAIN_VARIANT (arg2
);
23975 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23977 int i
, len2
= remaining_arguments (args2
);
23978 tree parmvec
= make_tree_vec (1);
23979 tree argvec
= make_tree_vec (len2
);
23982 /* Setup the parameter vector, which contains only ARG1. */
23983 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23985 /* Setup the argument vector, which contains the remaining
23987 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23988 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23990 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23991 argvec
, DEDUCE_EXACT
,
23992 /*subr=*/true, /*explain_p=*/false)
23995 /* We cannot deduce in the other direction, because ARG1 is
23996 a pack expansion but ARG2 is not. */
23999 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24001 int i
, len1
= remaining_arguments (args1
);
24002 tree parmvec
= make_tree_vec (1);
24003 tree argvec
= make_tree_vec (len1
);
24006 /* Setup the parameter vector, which contains only ARG1. */
24007 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24009 /* Setup the argument vector, which contains the remaining
24011 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24012 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24014 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24015 argvec
, DEDUCE_EXACT
,
24016 /*subr=*/true, /*explain_p=*/false)
24019 /* We cannot deduce in the other direction, because ARG2 is
24020 a pack expansion but ARG1 is not.*/
24026 /* The normal case, where neither argument is a pack
24028 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24029 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24031 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24032 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24036 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24037 arg2, then arg2 is not as specialized as arg1. */
24043 /* "If, for a given type, deduction succeeds in both directions
24044 (i.e., the types are identical after the transformations above)
24045 and both P and A were reference types (before being replaced with
24046 the type referred to above):
24047 - if the type from the argument template was an lvalue reference and
24048 the type from the parameter template was not, the argument type is
24049 considered to be more specialized than the other; otherwise,
24050 - if the type from the argument template is more cv-qualified
24051 than the type from the parameter template (as described above),
24052 the argument type is considered to be more specialized than the other;
24054 - neither type is more specialized than the other." */
24056 if (deduce1
&& deduce2
)
24058 if (ref1
&& ref2
&& ref1
!= ref2
)
24065 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24067 if ((quals1
& quals2
) == quals2
)
24069 if ((quals1
& quals2
) == quals1
)
24074 if (lose1
&& lose2
)
24075 /* We've failed to deduce something in either direction.
24076 These must be unordered. */
24079 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24080 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24081 /* We have already processed all of the arguments in our
24082 handing of the pack expansion type. */
24085 args1
= TREE_CHAIN (args1
);
24086 args2
= TREE_CHAIN (args2
);
24089 /* "In most cases, all template parameters must have values in order for
24090 deduction to succeed, but for partial ordering purposes a template
24091 parameter may remain without a value provided it is not used in the
24092 types being used for partial ordering."
24094 Thus, if we are missing any of the targs1 we need to substitute into
24095 origs1, then pat2 is not as specialized as pat1. This can happen when
24096 there is a nondeduced context. */
24097 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24099 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24102 processing_template_decl
--;
24104 /* If both deductions succeed, the partial ordering selects the more
24105 constrained template. */
24106 /* P2113: If the corresponding template-parameters of the
24107 template-parameter-lists are not equivalent ([temp.over.link]) or if
24108 the function parameters that positionally correspond between the two
24109 templates are not of the same type, neither template is more
24110 specialized than the other. */
24111 if (!lose1
&& !lose2
24112 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24113 DECL_TEMPLATE_PARMS (pat2
))
24114 && compparms (origs1
, origs2
))
24116 int winner
= more_constrained (decl1
, decl2
);
24119 else if (winner
< 0)
24123 /* All things being equal, if the next argument is a pack expansion
24124 for one function but not for the other, prefer the
24125 non-variadic function. FIXME this is bogus; see c++/41958. */
24127 && args1
&& TREE_VALUE (args1
)
24128 && args2
&& TREE_VALUE (args2
))
24130 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24131 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24134 if (lose1
== lose2
)
24142 /* Determine which of two partial specializations of TMPL is more
24145 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24146 to the first partial specialization. The TREE_PURPOSE is the
24147 innermost set of template parameters for the partial
24148 specialization. PAT2 is similar, but for the second template.
24150 Return 1 if the first partial specialization is more specialized;
24151 -1 if the second is more specialized; 0 if neither is more
24154 See [temp.class.order] for information about determining which of
24155 two templates is more specialized. */
24158 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24162 bool any_deductions
= false;
24164 tree tmpl1
= TREE_VALUE (pat1
);
24165 tree tmpl2
= TREE_VALUE (pat2
);
24166 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24167 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24169 /* Just like what happens for functions, if we are ordering between
24170 different template specializations, we may encounter dependent
24171 types in the arguments, and we need our dependency check functions
24172 to behave correctly. */
24173 ++processing_template_decl
;
24174 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24178 any_deductions
= true;
24181 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24185 any_deductions
= true;
24187 --processing_template_decl
;
24189 /* If both deductions succeed, the partial ordering selects the more
24190 constrained template. */
24191 if (!winner
&& any_deductions
)
24192 winner
= more_constrained (tmpl1
, tmpl2
);
24194 /* In the case of a tie where at least one of the templates
24195 has a parameter pack at the end, the template with the most
24196 non-packed parameters wins. */
24199 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24200 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24202 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24203 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24204 int len1
= TREE_VEC_LENGTH (args1
);
24205 int len2
= TREE_VEC_LENGTH (args2
);
24207 /* We don't count the pack expansion at the end. */
24208 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24210 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24215 else if (len1
< len2
)
24222 /* Return the template arguments that will produce the function signature
24223 DECL from the function template FN, with the explicit template
24224 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24225 also match. Return NULL_TREE if no satisfactory arguments could be
24229 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24231 int ntparms
= DECL_NTPARMS (fn
);
24232 tree targs
= make_tree_vec (ntparms
);
24233 tree decl_type
= TREE_TYPE (decl
);
24234 tree decl_arg_types
;
24236 unsigned int nargs
, ix
;
24239 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24241 /* Never do unification on the 'this' parameter. */
24242 decl_arg_types
= skip_artificial_parms_for (decl
,
24243 TYPE_ARG_TYPES (decl_type
));
24245 nargs
= list_length (decl_arg_types
);
24246 args
= XALLOCAVEC (tree
, nargs
);
24247 for (arg
= decl_arg_types
, ix
= 0;
24248 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24249 arg
= TREE_CHAIN (arg
), ++ix
)
24250 args
[ix
] = TREE_VALUE (arg
);
24252 if (fn_type_unification (fn
, explicit_args
, targs
,
24254 (check_rettype
|| DECL_CONV_FN_P (fn
)
24255 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24256 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24257 /*explain_p=*/false,
24259 == error_mark_node
)
24265 /* Return the innermost template arguments that, when applied to a partial
24266 specialization SPEC_TMPL of TMPL, yield the ARGS.
24268 For example, suppose we have:
24270 template <class T, class U> struct S {};
24271 template <class T> struct S<T*, int> {};
24273 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24274 partial specialization and the ARGS will be {double*, int}. The resulting
24275 vector will be {double}, indicating that `T' is bound to `double'. */
24278 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24280 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24282 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24283 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24285 tree innermost_deduced_args
;
24287 innermost_deduced_args
= make_tree_vec (ntparms
);
24288 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24290 deduced_args
= copy_node (args
);
24291 SET_TMPL_ARGS_LEVEL (deduced_args
,
24292 TMPL_ARGS_DEPTH (deduced_args
),
24293 innermost_deduced_args
);
24296 deduced_args
= innermost_deduced_args
;
24298 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24300 if (unify (tparms
, deduced_args
,
24301 INNERMOST_TEMPLATE_ARGS (spec_args
),
24302 INNERMOST_TEMPLATE_ARGS (args
),
24303 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24306 for (i
= 0; i
< ntparms
; ++i
)
24307 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24309 if (!tried_array_deduction
)
24311 try_array_deduction (tparms
, innermost_deduced_args
,
24312 INNERMOST_TEMPLATE_ARGS (spec_args
));
24313 tried_array_deduction
= true;
24314 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24320 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24322 excessive_deduction_depth
= true;
24326 /* Verify that nondeduced template arguments agree with the type
24327 obtained from argument deduction.
24331 struct A { typedef int X; };
24332 template <class T, class U> struct C {};
24333 template <class T> struct C<T, typename T::X> {};
24335 Then with the instantiation `C<A, int>', we can deduce that
24336 `T' is `A' but unify () does not check whether `typename T::X'
24338 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24340 if (spec_args
!= error_mark_node
)
24341 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24342 INNERMOST_TEMPLATE_ARGS (spec_args
),
24343 tmpl
, tf_none
, false, false);
24345 pop_tinst_level ();
24347 if (spec_args
== error_mark_node
24348 /* We only need to check the innermost arguments; the other
24349 arguments will always agree. */
24350 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24351 INNERMOST_TEMPLATE_ARGS (args
)))
24354 /* Now that we have bindings for all of the template arguments,
24355 ensure that the arguments deduced for the template template
24356 parameters have compatible template parameter lists. See the use
24357 of template_template_parm_bindings_ok_p in fn_type_unification
24358 for more information. */
24359 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24362 return deduced_args
;
24365 // Compare two function templates T1 and T2 by deducing bindings
24366 // from one against the other. If both deductions succeed, compare
24367 // constraints to see which is more constrained.
24369 more_specialized_inst (tree t1
, tree t2
)
24374 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24380 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24386 // If both deductions succeed, then one may be more constrained.
24387 if (count
== 2 && fate
== 0)
24388 fate
= more_constrained (t1
, t2
);
24393 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24394 Return the TREE_LIST node with the most specialized template, if
24395 any. If there is no most specialized template, the error_mark_node
24398 Note that this function does not look at, or modify, the
24399 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24400 returned is one of the elements of INSTANTIATIONS, callers may
24401 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24402 and retrieve it from the value returned. */
24405 most_specialized_instantiation (tree templates
)
24409 ++processing_template_decl
;
24412 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24414 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24415 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24420 /* Equally specialized, move to next function. If there
24421 is no next function, nothing's most specialized. */
24422 fn
= TREE_CHAIN (fn
);
24430 /* Now verify that champ is better than everything earlier in the
24431 instantiation list. */
24432 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24433 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24440 processing_template_decl
--;
24443 return error_mark_node
;
24448 /* If DECL is a specialization of some template, return the most
24449 general such template. Otherwise, returns NULL_TREE.
24451 For example, given:
24453 template <class T> struct S { template <class U> void f(U); };
24455 if TMPL is `template <class U> void S<int>::f(U)' this will return
24456 the full template. This function will not trace past partial
24457 specializations, however. For example, given in addition:
24459 template <class T> struct S<T*> { template <class U> void f(U); };
24461 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24462 `template <class T> template <class U> S<T*>::f(U)'. */
24465 most_general_template (tree decl
)
24467 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24469 if (tree tinfo
= get_template_info (decl
))
24470 decl
= TI_TEMPLATE (tinfo
);
24471 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24472 template friend, or a FIELD_DECL for a capture pack. */
24473 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24477 /* Look for more and more general templates. */
24478 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24480 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24481 (See cp-tree.h for details.) */
24482 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24485 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24486 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24487 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24490 /* Stop if we run into an explicitly specialized class template. */
24491 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24492 && DECL_CONTEXT (decl
)
24493 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24496 decl
= DECL_TI_TEMPLATE (decl
);
24502 /* Return the most specialized of the template partial specializations
24503 which can produce TARGET, a specialization of some class or variable
24504 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24505 a TEMPLATE_DECL node corresponding to the partial specialization, while
24506 the TREE_PURPOSE is the set of template arguments that must be
24507 substituted into the template pattern in order to generate TARGET.
24509 If the choice of partial specialization is ambiguous, a diagnostic
24510 is issued, and the error_mark_node is returned. If there are no
24511 partial specializations matching TARGET, then NULL_TREE is
24512 returned, indicating that the primary template should be used. */
24515 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24517 tree list
= NULL_TREE
;
24522 tree outer_args
= NULL_TREE
;
24525 if (TYPE_P (target
))
24527 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24528 tmpl
= TI_TEMPLATE (tinfo
);
24529 args
= TI_ARGS (tinfo
);
24531 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24533 tmpl
= TREE_OPERAND (target
, 0);
24534 args
= TREE_OPERAND (target
, 1);
24536 else if (VAR_P (target
))
24538 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24539 tmpl
= TI_TEMPLATE (tinfo
);
24540 args
= TI_ARGS (tinfo
);
24543 gcc_unreachable ();
24545 tree main_tmpl
= most_general_template (tmpl
);
24547 /* For determining which partial specialization to use, only the
24548 innermost args are interesting. */
24549 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24551 outer_args
= strip_innermost_template_args (args
, 1);
24552 args
= INNERMOST_TEMPLATE_ARGS (args
);
24555 /* The caller hasn't called push_to_top_level yet, but we need
24556 get_partial_spec_bindings to be done in non-template context so that we'll
24557 fully resolve everything. */
24558 processing_template_decl_sentinel ptds
;
24560 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24562 const tree ospec_tmpl
= TREE_VALUE (t
);
24567 /* Substitute in the template args from the enclosing class. */
24568 ++processing_template_decl
;
24569 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24570 --processing_template_decl
;
24571 if (spec_tmpl
== error_mark_node
)
24572 return error_mark_node
;
24575 spec_tmpl
= ospec_tmpl
;
24577 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24581 spec_args
= add_to_template_args (outer_args
, spec_args
);
24583 /* Keep the candidate only if the constraints are satisfied,
24584 or if we're not compiling with concepts. */
24586 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24588 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24589 TREE_TYPE (list
) = TREE_TYPE (t
);
24597 ambiguous_p
= false;
24600 t
= TREE_CHAIN (t
);
24601 for (; t
; t
= TREE_CHAIN (t
))
24603 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24610 t
= TREE_CHAIN (t
);
24613 ambiguous_p
= true;
24622 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24624 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24627 ambiguous_p
= true;
24635 char *spaces
= NULL
;
24636 if (!(complain
& tf_error
))
24637 return error_mark_node
;
24638 if (TYPE_P (target
))
24639 error ("ambiguous template instantiation for %q#T", target
);
24641 error ("ambiguous template instantiation for %q#D", target
);
24642 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24643 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24645 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24646 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24647 "%s %#qS", spaces
? spaces
: str
, subst
);
24648 spaces
= spaces
? spaces
: get_spaces (str
);
24651 return error_mark_node
;
24657 /* Explicitly instantiate DECL. */
24660 do_decl_instantiation (tree decl
, tree storage
)
24662 tree result
= NULL_TREE
;
24665 if (!decl
|| decl
== error_mark_node
)
24666 /* An error occurred, for which grokdeclarator has already issued
24667 an appropriate message. */
24669 else if (! DECL_LANG_SPECIFIC (decl
))
24671 error ("explicit instantiation of non-template %q#D", decl
);
24674 else if (DECL_DECLARED_CONCEPT_P (decl
))
24677 error ("explicit instantiation of variable concept %q#D", decl
);
24679 error ("explicit instantiation of function concept %q#D", decl
);
24683 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24684 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24686 if (VAR_P (decl
) && !var_templ
)
24688 /* There is an asymmetry here in the way VAR_DECLs and
24689 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24690 the latter, the DECL we get back will be marked as a
24691 template instantiation, and the appropriate
24692 DECL_TEMPLATE_INFO will be set up. This does not happen for
24693 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24694 should handle VAR_DECLs as it currently handles
24696 if (!DECL_CLASS_SCOPE_P (decl
))
24698 error ("%qD is not a static data member of a class template", decl
);
24701 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24702 if (!result
|| !VAR_P (result
))
24704 error ("no matching template for %qD found", decl
);
24707 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24709 error ("type %qT for explicit instantiation %qD does not match "
24710 "declared type %qT", TREE_TYPE (result
), decl
,
24715 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24717 error ("explicit instantiation of %q#D", decl
);
24723 /* Check for various error cases. Note that if the explicit
24724 instantiation is valid the RESULT will currently be marked as an
24725 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24726 until we get here. */
24728 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24730 /* DR 259 [temp.spec].
24732 Both an explicit instantiation and a declaration of an explicit
24733 specialization shall not appear in a program unless the explicit
24734 instantiation follows a declaration of the explicit specialization.
24736 For a given set of template parameters, if an explicit
24737 instantiation of a template appears after a declaration of an
24738 explicit specialization for that template, the explicit
24739 instantiation has no effect. */
24742 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24746 No program shall explicitly instantiate any template more
24749 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24750 the first instantiation was `extern' and the second is not,
24751 and EXTERN_P for the opposite case. */
24752 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24753 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24754 /* If an "extern" explicit instantiation follows an ordinary
24755 explicit instantiation, the template is instantiated. */
24759 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24761 error ("no matching template for %qD found", result
);
24764 else if (!DECL_TEMPLATE_INFO (result
))
24766 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24770 if (storage
== NULL_TREE
)
24772 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24774 if (cxx_dialect
== cxx98
)
24775 pedwarn (input_location
, OPT_Wpedantic
,
24776 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24781 error ("storage class %qD applied to template instantiation", storage
);
24783 check_explicit_instantiation_namespace (result
);
24784 mark_decl_instantiated (result
, extern_p
);
24786 instantiate_decl (result
, /*defer_ok=*/true,
24787 /*expl_inst_class_mem_p=*/false);
24791 mark_class_instantiated (tree t
, int extern_p
)
24793 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24794 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24795 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24796 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24799 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24800 rest_of_type_compilation (t
, 1);
24804 /* Called from do_type_instantiation through binding_table_foreach to
24805 do recursive instantiation for the type bound in ENTRY. */
24807 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24809 tree storage
= *(tree
*) data
;
24811 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24812 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24813 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24814 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24817 /* Perform an explicit instantiation of template class T. STORAGE, if
24818 non-null, is the RID for extern, inline or static. COMPLAIN is
24819 nonzero if this is called from the parser, zero if called recursively,
24820 since the standard is unclear (as detailed below). */
24823 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24828 int previous_instantiation_extern_p
= 0;
24830 if (TREE_CODE (t
) == TYPE_DECL
)
24833 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24836 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24838 error ("explicit instantiation of non-class template %qD", tmpl
);
24840 error ("explicit instantiation of non-template type %qT", t
);
24846 if (!COMPLETE_TYPE_P (t
))
24848 if (complain
& tf_error
)
24849 error ("explicit instantiation of %q#T before definition of template",
24854 if (storage
!= NULL_TREE
)
24856 if (storage
== ridpointers
[(int) RID_EXTERN
])
24858 if (cxx_dialect
== cxx98
)
24859 pedwarn (input_location
, OPT_Wpedantic
,
24860 "ISO C++ 1998 forbids the use of %<extern%> on "
24861 "explicit instantiations");
24864 pedwarn (input_location
, OPT_Wpedantic
,
24865 "ISO C++ forbids the use of %qE"
24866 " on explicit instantiations", storage
);
24868 if (storage
== ridpointers
[(int) RID_INLINE
])
24870 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24872 else if (storage
== ridpointers
[(int) RID_STATIC
])
24876 error ("storage class %qD applied to template instantiation",
24882 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24884 /* DR 259 [temp.spec].
24886 Both an explicit instantiation and a declaration of an explicit
24887 specialization shall not appear in a program unless the explicit
24888 instantiation follows a declaration of the explicit specialization.
24890 For a given set of template parameters, if an explicit
24891 instantiation of a template appears after a declaration of an
24892 explicit specialization for that template, the explicit
24893 instantiation has no effect. */
24896 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24900 No program shall explicitly instantiate any template more
24903 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24904 instantiation was `extern'. If EXTERN_P then the second is.
24905 These cases are OK. */
24906 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24908 if (!previous_instantiation_extern_p
&& !extern_p
24909 && (complain
& tf_error
))
24910 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24912 /* If we've already instantiated the template, just return now. */
24913 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24917 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24918 mark_class_instantiated (t
, extern_p
);
24923 /* In contrast to implicit instantiation, where only the
24924 declarations, and not the definitions, of members are
24925 instantiated, we have here:
24929 The explicit instantiation of a class template specialization
24930 implies the instantiation of all of its members not
24931 previously explicitly specialized in the translation unit
24932 containing the explicit instantiation.
24934 Of course, we can't instantiate member template classes, since we
24935 don't have any arguments for them. Note that the standard is
24936 unclear on whether the instantiation of the members are
24937 *explicit* instantiations or not. However, the most natural
24938 interpretation is that it should be an explicit
24940 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24942 || (TREE_CODE (fld
) == FUNCTION_DECL
24944 && user_provided_p (fld
)))
24945 && DECL_TEMPLATE_INSTANTIATION (fld
))
24947 mark_decl_instantiated (fld
, extern_p
);
24949 instantiate_decl (fld
, /*defer_ok=*/true,
24950 /*expl_inst_class_mem_p=*/true);
24953 if (CLASSTYPE_NESTED_UTDS (t
))
24954 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24955 bt_instantiate_type_proc
, &storage
);
24958 /* Given a function DECL, which is a specialization of TMPL, modify
24959 DECL to be a re-instantiation of TMPL with the same template
24960 arguments. TMPL should be the template into which tsubst'ing
24961 should occur for DECL, not the most general template.
24963 One reason for doing this is a scenario like this:
24966 void f(const T&, int i);
24968 void g() { f(3, 7); }
24971 void f(const T& t, const int i) { }
24973 Note that when the template is first instantiated, with
24974 instantiate_template, the resulting DECL will have no name for the
24975 first parameter, and the wrong type for the second. So, when we go
24976 to instantiate the DECL, we regenerate it. */
24979 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24981 /* The arguments used to instantiate DECL, from the most general
24985 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24987 /* Make sure that we can see identifiers, and compute access
24989 push_access_scope (decl
);
24991 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24999 args_depth
= TMPL_ARGS_DEPTH (args
);
25000 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25001 if (args_depth
> parms_depth
)
25002 args
= get_innermost_template_args (args
, parms_depth
);
25004 /* Instantiate a dynamic exception-specification. noexcept will be
25006 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25007 if (TREE_VALUE (raises
))
25009 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25010 args
, tf_error
, NULL_TREE
,
25011 /*defer_ok*/false);
25012 if (specs
&& specs
!= error_mark_node
)
25013 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25017 /* Merge parameter declarations. */
25018 decl_parm
= skip_artificial_parms_for (decl
,
25019 DECL_ARGUMENTS (decl
));
25021 = skip_artificial_parms_for (code_pattern
,
25022 DECL_ARGUMENTS (code_pattern
));
25023 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25028 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25029 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25030 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25032 parm_type
= type_decays_to (parm_type
);
25033 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25034 TREE_TYPE (decl_parm
) = parm_type
;
25035 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25036 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25038 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25039 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25041 decl_parm
= DECL_CHAIN (decl_parm
);
25042 pattern_parm
= DECL_CHAIN (pattern_parm
);
25044 /* Merge any parameters that match with the function parameter
25046 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25049 tree expanded_types
;
25050 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25051 the parameters in this function parameter pack. */
25052 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25053 args
, tf_error
, NULL_TREE
);
25054 len
= TREE_VEC_LENGTH (expanded_types
);
25055 for (i
= 0; i
< len
; i
++)
25060 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25061 /* Rename the parameter to include the index. */
25062 DECL_NAME (decl_parm
) =
25063 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25064 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25065 parm_type
= type_decays_to (parm_type
);
25066 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25067 TREE_TYPE (decl_parm
) = parm_type
;
25068 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25069 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25071 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25072 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25074 decl_parm
= DECL_CHAIN (decl_parm
);
25077 /* Merge additional specifiers from the CODE_PATTERN. */
25078 if (DECL_DECLARED_INLINE_P (code_pattern
)
25079 && !DECL_DECLARED_INLINE_P (decl
))
25080 DECL_DECLARED_INLINE_P (decl
) = 1;
25082 maybe_instantiate_noexcept (decl
, tf_error
);
25084 else if (VAR_P (decl
))
25086 start_lambda_scope (decl
);
25087 DECL_INITIAL (decl
) =
25088 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25089 tf_error
, DECL_TI_TEMPLATE (decl
));
25090 finish_lambda_scope ();
25091 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25092 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25093 tf_error
, DECL_TI_TEMPLATE (decl
));
25096 gcc_unreachable ();
25098 pop_access_scope (decl
);
25101 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25102 substituted to get DECL. */
25105 template_for_substitution (tree decl
)
25107 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25109 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25110 for the instantiation. This is not always the most general
25111 template. Consider, for example:
25114 struct S { template <class U> void f();
25115 template <> void f<int>(); };
25117 and an instantiation of S<double>::f<int>. We want TD to be the
25118 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25119 while (/* An instantiation cannot have a definition, so we need a
25120 more general template. */
25121 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25122 /* We must also deal with friend templates. Given:
25124 template <class T> struct S {
25125 template <class U> friend void f() {};
25128 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25129 so far as the language is concerned, but that's still
25130 where we get the pattern for the instantiation from. On
25131 other hand, if the definition comes outside the class, say:
25133 template <class T> struct S {
25134 template <class U> friend void f();
25136 template <class U> friend void f() {}
25138 we don't need to look any further. That's what the check for
25139 DECL_INITIAL is for. */
25140 || (TREE_CODE (decl
) == FUNCTION_DECL
25141 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25142 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25144 /* The present template, TD, should not be a definition. If it
25145 were a definition, we should be using it! Note that we
25146 cannot restructure the loop to just keep going until we find
25147 a template with a definition, since that might go too far if
25148 a specialization was declared, but not defined. */
25150 /* Fetch the more general template. */
25151 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25157 /* Returns true if we need to instantiate this template instance even if we
25158 know we aren't going to emit it. */
25161 always_instantiate_p (tree decl
)
25163 /* We always instantiate inline functions so that we can inline them. An
25164 explicit instantiation declaration prohibits implicit instantiation of
25165 non-inline functions. With high levels of optimization, we would
25166 normally inline non-inline functions -- but we're not allowed to do
25167 that for "extern template" functions. Therefore, we check
25168 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25169 return ((TREE_CODE (decl
) == FUNCTION_DECL
25170 && (DECL_DECLARED_INLINE_P (decl
)
25171 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25172 /* And we need to instantiate static data members so that
25173 their initializers are available in integral constant
25176 && decl_maybe_constant_var_p (decl
)));
25179 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25180 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25181 error, true otherwise. */
25184 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25186 tree fntype
, spec
, noex
;
25188 /* Don't instantiate a noexcept-specification from template context. */
25189 if (processing_template_decl
25190 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25193 if (DECL_MAYBE_DELETED (fn
))
25195 if (fn
== current_function_decl
)
25196 /* We're in start_preparsed_function, keep going. */
25200 synthesize_method (fn
);
25202 return !DECL_MAYBE_DELETED (fn
);
25205 fntype
= TREE_TYPE (fn
);
25206 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25208 if (!spec
|| !TREE_PURPOSE (spec
))
25211 noex
= TREE_PURPOSE (spec
);
25212 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25213 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25216 tree orig_fn
= NULL_TREE
;
25217 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25218 its FUNCTION_DECL for the rest of this function -- push_access_scope
25219 doesn't accept TEMPLATE_DECLs. */
25220 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25223 fn
= DECL_TEMPLATE_RESULT (fn
);
25226 if (DECL_CLONED_FUNCTION_P (fn
))
25228 tree prime
= DECL_CLONED_FUNCTION (fn
);
25229 if (!maybe_instantiate_noexcept (prime
, complain
))
25231 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25233 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25235 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25236 bool added
= false;
25237 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25239 spec
= get_defaulted_eh_spec (fn
, complain
);
25240 if (spec
== error_mark_node
)
25241 /* This might have failed because of an unparsed DMI, so
25242 let's try again later. */
25245 else if (!(added
= !fns
->add (fn
)))
25247 /* If hash_set::add returns true, the element was already there. */
25248 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25249 DECL_SOURCE_LOCATION (fn
));
25251 "exception specification of %qD depends on itself",
25253 spec
= noexcept_false_spec
;
25255 else if (push_tinst_level (fn
))
25257 push_to_top_level ();
25258 push_access_scope (fn
);
25259 push_deferring_access_checks (dk_no_deferred
);
25260 input_location
= DECL_SOURCE_LOCATION (fn
);
25262 /* If needed, set current_class_ptr for the benefit of
25263 tsubst_copy/PARM_DECL. */
25264 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25265 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25267 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25268 current_class_ptr
= NULL_TREE
;
25269 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25270 current_class_ptr
= this_parm
;
25273 /* If this function is represented by a TEMPLATE_DECL, then
25274 the deferred noexcept-specification might still contain
25275 dependent types, even after substitution. And we need the
25276 dependency check functions to work in build_noexcept_spec. */
25278 ++processing_template_decl
;
25280 /* Do deferred instantiation of the noexcept-specifier. */
25281 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25282 DEFERRED_NOEXCEPT_ARGS (noex
),
25283 tf_warning_or_error
, fn
,
25284 /*function_p=*/false,
25287 /* Build up the noexcept-specification. */
25288 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25291 --processing_template_decl
;
25293 pop_deferring_access_checks ();
25294 pop_access_scope (fn
);
25295 pop_tinst_level ();
25296 pop_from_top_level ();
25299 spec
= noexcept_false_spec
;
25305 if (spec
== error_mark_node
)
25307 /* This failed with a hard error, so let's go with false. */
25308 gcc_assert (seen_error ());
25309 spec
= noexcept_false_spec
;
25312 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25314 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25319 /* We're starting to process the function INST, an instantiation of PATTERN;
25320 add their parameters to local_specializations. */
25323 register_parameter_specializations (tree pattern
, tree inst
)
25325 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25326 tree spec_parm
= DECL_ARGUMENTS (inst
);
25327 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25329 register_local_specialization (spec_parm
, tmpl_parm
);
25330 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25331 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25333 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25335 if (!DECL_PACK_P (tmpl_parm
)
25336 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25338 register_local_specialization (spec_parm
, tmpl_parm
);
25339 spec_parm
= DECL_CHAIN (spec_parm
);
25343 /* Register the (value) argument pack as a specialization of
25344 TMPL_PARM, then move on. */
25345 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25346 register_local_specialization (argpack
, tmpl_parm
);
25349 gcc_assert (!spec_parm
);
25352 /* Produce the definition of D, a _DECL generated from a template. If
25353 DEFER_OK is true, then we don't have to actually do the
25354 instantiation now; we just have to do it sometime. Normally it is
25355 an error if this is an explicit instantiation but D is undefined.
25356 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25357 instantiated class template. */
25360 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25362 tree tmpl
= DECL_TI_TEMPLATE (d
);
25369 bool pattern_defined
;
25370 location_t saved_loc
= input_location
;
25371 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25372 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25376 /* This function should only be used to instantiate templates for
25377 functions and static member variables. */
25378 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25380 /* A concept is never instantiated. */
25381 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25383 /* Variables are never deferred; if instantiation is required, they
25384 are instantiated right away. That allows for better code in the
25385 case that an expression refers to the value of the variable --
25386 if the variable has a constant value the referring expression can
25387 take advantage of that fact. */
25391 /* Don't instantiate cloned functions. Instead, instantiate the
25392 functions they cloned. */
25393 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25394 d
= DECL_CLONED_FUNCTION (d
);
25396 if (DECL_TEMPLATE_INSTANTIATED (d
)
25397 || TREE_TYPE (d
) == error_mark_node
25398 || (TREE_CODE (d
) == FUNCTION_DECL
25399 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25400 || DECL_TEMPLATE_SPECIALIZATION (d
))
25401 /* D has already been instantiated or explicitly specialized, so
25402 there's nothing for us to do here.
25404 It might seem reasonable to check whether or not D is an explicit
25405 instantiation, and, if so, stop here. But when an explicit
25406 instantiation is deferred until the end of the compilation,
25407 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25408 the instantiation. */
25411 /* Check to see whether we know that this template will be
25412 instantiated in some other file, as with "extern template"
25414 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25416 /* In general, we do not instantiate such templates. */
25417 if (external_p
&& !always_instantiate_p (d
))
25420 gen_tmpl
= most_general_template (tmpl
);
25421 gen_args
= DECL_TI_ARGS (d
);
25423 /* We should already have the extra args. */
25424 gcc_checking_assert (tmpl
== gen_tmpl
25425 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25426 == TMPL_ARGS_DEPTH (gen_args
)));
25427 /* And what's in the hash table should match D. */
25428 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25430 || spec
== NULL_TREE
);
25432 /* This needs to happen before any tsubsting. */
25433 if (! push_tinst_level (d
))
25436 timevar_push (TV_TEMPLATE_INST
);
25438 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25439 for the instantiation. */
25440 td
= template_for_substitution (d
);
25445 /* Look up an explicit specialization, if any. */
25446 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25447 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25448 if (elt
&& elt
!= error_mark_node
)
25450 td
= TREE_VALUE (elt
);
25451 args
= TREE_PURPOSE (elt
);
25455 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25457 /* We should never be trying to instantiate a member of a class
25458 template or partial specialization. */
25459 gcc_assert (d
!= code_pattern
);
25461 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25462 || DECL_TEMPLATE_SPECIALIZATION (td
))
25463 /* In the case of a friend template whose definition is provided
25464 outside the class, we may have too many arguments. Drop the
25465 ones we don't need. The same is true for specializations. */
25466 args
= get_innermost_template_args
25467 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25469 if (TREE_CODE (d
) == FUNCTION_DECL
)
25471 deleted_p
= DECL_DELETED_FN (code_pattern
);
25472 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25473 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25474 || DECL_DEFAULTED_FN (code_pattern
)
25480 if (DECL_CLASS_SCOPE_P (code_pattern
))
25481 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25483 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25486 /* We may be in the middle of deferred access check. Disable it now. */
25487 push_deferring_access_checks (dk_no_deferred
);
25489 /* Unless an explicit instantiation directive has already determined
25490 the linkage of D, remember that a definition is available for
25492 if (pattern_defined
25493 && !DECL_INTERFACE_KNOWN (d
)
25494 && !DECL_NOT_REALLY_EXTERN (d
))
25495 mark_definable (d
);
25497 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25498 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25499 input_location
= DECL_SOURCE_LOCATION (d
);
25501 /* If D is a member of an explicitly instantiated class template,
25502 and no definition is available, treat it like an implicit
25504 if (!pattern_defined
&& expl_inst_class_mem_p
25505 && DECL_EXPLICIT_INSTANTIATION (d
))
25507 /* Leave linkage flags alone on instantiations with anonymous
25509 if (TREE_PUBLIC (d
))
25511 DECL_NOT_REALLY_EXTERN (d
) = 0;
25512 DECL_INTERFACE_KNOWN (d
) = 0;
25514 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25517 /* Defer all other templates, unless we have been explicitly
25518 forbidden from doing so. */
25519 if (/* If there is no definition, we cannot instantiate the
25522 /* If it's OK to postpone instantiation, do so. */
25524 /* If this is a static data member that will be defined
25525 elsewhere, we don't want to instantiate the entire data
25526 member, but we do want to instantiate the initializer so that
25527 we can substitute that elsewhere. */
25528 || (external_p
&& VAR_P (d
))
25529 /* Handle here a deleted function too, avoid generating
25530 its body (c++/61080). */
25533 /* The definition of the static data member is now required so
25534 we must substitute the initializer. */
25536 && !DECL_INITIAL (d
)
25537 && DECL_INITIAL (code_pattern
))
25541 bool const_init
= false;
25542 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25544 ns
= decl_namespace_context (d
);
25545 push_nested_namespace (ns
);
25547 push_nested_class (DECL_CONTEXT (d
));
25548 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25550 tf_warning_or_error
, NULL_TREE
,
25551 /*integral_constant_expression_p=*/false);
25552 /* If instantiating the initializer involved instantiating this
25553 again, don't call cp_finish_decl twice. */
25554 if (!DECL_INITIAL (d
))
25556 /* Make sure the initializer is still constant, in case of
25557 circular dependency (template/instantiate6.C). */
25559 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25560 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25561 /*asmspec_tree=*/NULL_TREE
,
25562 LOOKUP_ONLYCONVERTING
);
25565 pop_nested_class ();
25566 pop_nested_namespace (ns
);
25569 /* We restore the source position here because it's used by
25570 add_pending_template. */
25571 input_location
= saved_loc
;
25573 if (at_eof
&& !pattern_defined
25574 && DECL_EXPLICIT_INSTANTIATION (d
)
25575 && DECL_NOT_REALLY_EXTERN (d
))
25578 The definition of a non-exported function template, a
25579 non-exported member function template, or a non-exported
25580 member function or static data member of a class template
25581 shall be present in every translation unit in which it is
25582 explicitly instantiated. */
25583 permerror (input_location
, "explicit instantiation of %qD "
25584 "but no definition available", d
);
25586 /* If we're in unevaluated context, we just wanted to get the
25587 constant value; this isn't an odr use, so don't queue
25588 a full instantiation. */
25589 if (cp_unevaluated_operand
!= 0)
25591 /* ??? Historically, we have instantiated inline functions, even
25592 when marked as "extern template". */
25593 if (!(external_p
&& VAR_P (d
)))
25594 add_pending_template (d
);
25598 bool push_to_top
, nested
;
25600 fn_context
= decl_function_context (d
);
25601 if (LAMBDA_FUNCTION_P (d
))
25602 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25603 fn_context
= NULL_TREE
;
25604 nested
= current_function_decl
!= NULL_TREE
;
25605 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25607 vec
<tree
> omp_privatization_save
;
25609 save_omp_privatization_clauses (omp_privatization_save
);
25612 push_to_top_level ();
25615 gcc_assert (!processing_template_decl
);
25616 push_function_context ();
25617 cp_unevaluated_operand
= 0;
25618 c_inhibit_evaluation_warnings
= 0;
25623 /* The variable might be a lambda's extra scope, and that
25624 lambda's visibility depends on D's. */
25625 maybe_commonize_var (d
);
25626 determine_visibility (d
);
25629 /* Mark D as instantiated so that recursive calls to
25630 instantiate_decl do not try to instantiate it again. */
25631 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25633 /* Regenerate the declaration in case the template has been modified
25634 by a subsequent redeclaration. */
25635 regenerate_decl_from_template (d
, td
, args
);
25637 /* We already set the file and line above. Reset them now in case
25638 they changed as a result of calling regenerate_decl_from_template. */
25639 input_location
= DECL_SOURCE_LOCATION (d
);
25644 bool const_init
= false;
25646 /* Clear out DECL_RTL; whatever was there before may not be right
25647 since we've reset the type of the declaration. */
25648 SET_DECL_RTL (d
, NULL
);
25649 DECL_IN_AGGR_P (d
) = 0;
25651 /* The initializer is placed in DECL_INITIAL by
25652 regenerate_decl_from_template so we don't need to
25653 push/pop_access_scope again here. Pull it out so that
25654 cp_finish_decl can process it. */
25655 init
= DECL_INITIAL (d
);
25656 DECL_INITIAL (d
) = NULL_TREE
;
25657 DECL_INITIALIZED_P (d
) = 0;
25659 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25660 initializer. That function will defer actual emission until
25661 we have a chance to determine linkage. */
25662 DECL_EXTERNAL (d
) = 0;
25664 /* Enter the scope of D so that access-checking works correctly. */
25665 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25667 push_nested_class (DECL_CONTEXT (d
));
25669 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25670 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25671 ? LOOKUP_CONSTINIT
: 0);
25672 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25675 pop_nested_class ();
25677 if (variable_template_p (gen_tmpl
))
25678 note_variable_template_instantiation (d
);
25680 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25681 synthesize_method (d
);
25682 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25684 /* Set up the list of local specializations. */
25685 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25686 tree block
= NULL_TREE
;
25688 /* Set up context. */
25689 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25690 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25691 block
= push_stmt_list ();
25693 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25695 perform_instantiation_time_access_checks (DECL_TEMPLATE_RESULT (td
),
25698 /* Create substitution entries for the parameters. */
25699 register_parameter_specializations (code_pattern
, d
);
25701 /* Substitute into the body of the function. */
25702 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25703 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25704 tf_warning_or_error
, tmpl
);
25707 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25708 tf_warning_or_error
, tmpl
,
25709 /*integral_constant_expression_p=*/false);
25711 /* Set the current input_location to the end of the function
25712 so that finish_function knows where we are. */
25714 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25716 /* Remember if we saw an infinite loop in the template. */
25717 current_function_infinite_loop
25718 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25721 /* Finish the function. */
25722 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25723 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25724 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25727 d
= finish_function (/*inline_p=*/false);
25728 expand_or_defer_fn (d
);
25731 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25732 cp_check_omp_declare_reduction (d
);
25735 /* We're not deferring instantiation any more. */
25736 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25739 pop_from_top_level ();
25741 pop_function_context ();
25744 restore_omp_privatization_clauses (omp_privatization_save
);
25747 pop_deferring_access_checks ();
25748 timevar_pop (TV_TEMPLATE_INST
);
25749 pop_tinst_level ();
25750 input_location
= saved_loc
;
25751 cp_unevaluated_operand
= saved_unevaluated_operand
;
25752 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25757 /* Run through the list of templates that we wish we could
25758 instantiate, and instantiate any we can. RETRIES is the
25759 number of times we retry pending template instantiation. */
25762 instantiate_pending_templates (int retries
)
25765 location_t saved_loc
= input_location
;
25767 /* Instantiating templates may trigger vtable generation. This in turn
25768 may require further template instantiations. We place a limit here
25769 to avoid infinite loop. */
25770 if (pending_templates
&& retries
>= max_tinst_depth
)
25772 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25774 fatal_error (input_location
,
25775 "template instantiation depth exceeds maximum of %d"
25776 " instantiating %q+D, possibly from virtual table generation"
25777 " (use %<-ftemplate-depth=%> to increase the maximum)",
25778 max_tinst_depth
, decl
);
25779 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25780 /* Pretend that we defined it. */
25781 DECL_INITIAL (decl
) = error_mark_node
;
25787 struct pending_template
**t
= &pending_templates
;
25788 struct pending_template
*last
= NULL
;
25792 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25793 bool complete
= false;
25795 if (TYPE_P (instantiation
))
25797 if (!COMPLETE_TYPE_P (instantiation
))
25799 instantiate_class_template (instantiation
);
25800 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25801 for (tree fld
= TYPE_FIELDS (instantiation
);
25802 fld
; fld
= TREE_CHAIN (fld
))
25804 || (TREE_CODE (fld
) == FUNCTION_DECL
25805 && !DECL_ARTIFICIAL (fld
)))
25806 && DECL_TEMPLATE_INSTANTIATION (fld
))
25807 instantiate_decl (fld
,
25808 /*defer_ok=*/false,
25809 /*expl_inst_class_mem_p=*/false);
25811 if (COMPLETE_TYPE_P (instantiation
))
25815 complete
= COMPLETE_TYPE_P (instantiation
);
25819 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25820 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25823 = instantiate_decl (instantiation
,
25824 /*defer_ok=*/false,
25825 /*expl_inst_class_mem_p=*/false);
25826 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25830 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25831 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25836 /* If INSTANTIATION has been instantiated, then we don't
25837 need to consider it again in the future. */
25838 struct pending_template
*drop
= *t
;
25840 set_refcount_ptr (drop
->tinst
);
25841 pending_template_freelist ().free (drop
);
25849 set_refcount_ptr (current_tinst_level
);
25851 last_pending_template
= last
;
25853 while (reconsider
);
25855 input_location
= saved_loc
;
25858 /* Substitute ARGVEC into T, which is a list of initializers for
25859 either base class or a non-static data member. The TREE_PURPOSEs
25860 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25861 instantiate_decl. */
25864 tsubst_initializer_list (tree t
, tree argvec
)
25866 tree inits
= NULL_TREE
;
25867 tree target_ctor
= error_mark_node
;
25869 for (; t
; t
= TREE_CHAIN (t
))
25873 tree expanded_bases
= NULL_TREE
;
25874 tree expanded_arguments
= NULL_TREE
;
25877 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25882 /* Expand the base class expansion type into separate base
25884 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25885 tf_warning_or_error
,
25887 if (expanded_bases
== error_mark_node
)
25890 /* We'll be building separate TREE_LISTs of arguments for
25892 len
= TREE_VEC_LENGTH (expanded_bases
);
25893 expanded_arguments
= make_tree_vec (len
);
25894 for (i
= 0; i
< len
; i
++)
25895 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25897 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25898 expand each argument in the TREE_VALUE of t. */
25899 expr
= make_node (EXPR_PACK_EXPANSION
);
25900 PACK_EXPANSION_LOCAL_P (expr
) = true;
25901 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25902 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25904 if (TREE_VALUE (t
) == void_type_node
)
25905 /* VOID_TYPE_NODE is used to indicate
25906 value-initialization. */
25908 for (i
= 0; i
< len
; i
++)
25909 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25913 /* Substitute parameter packs into each argument in the
25915 in_base_initializer
= 1;
25916 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25918 tree expanded_exprs
;
25920 /* Expand the argument. */
25921 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25923 = tsubst_pack_expansion (expr
, argvec
,
25924 tf_warning_or_error
,
25926 if (expanded_exprs
== error_mark_node
)
25929 /* Prepend each of the expanded expressions to the
25930 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25931 for (i
= 0; i
< len
; i
++)
25933 TREE_VEC_ELT (expanded_arguments
, i
) =
25934 tree_cons (NULL_TREE
,
25935 TREE_VEC_ELT (expanded_exprs
, i
),
25936 TREE_VEC_ELT (expanded_arguments
, i
));
25939 in_base_initializer
= 0;
25941 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25942 since we built them backwards. */
25943 for (i
= 0; i
< len
; i
++)
25945 TREE_VEC_ELT (expanded_arguments
, i
) =
25946 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25951 for (i
= 0; i
< len
; ++i
)
25953 if (expanded_bases
)
25955 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25956 decl
= expand_member_init (decl
);
25957 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25962 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25963 tf_warning_or_error
, NULL_TREE
);
25965 decl
= expand_member_init (decl
);
25966 if (decl
&& !DECL_P (decl
))
25967 in_base_initializer
= 1;
25969 init
= TREE_VALUE (t
);
25971 if (init
!= void_type_node
)
25972 init
= tsubst_expr (init
, argvec
,
25973 tf_warning_or_error
, NULL_TREE
,
25974 /*integral_constant_expression_p=*/false);
25975 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25976 /* If we had an initializer but it instantiated to nothing,
25977 value-initialize the object. This will only occur when
25978 the initializer was a pack expansion where the parameter
25979 packs used in that expansion were of length zero. */
25980 init
= void_type_node
;
25981 in_base_initializer
= 0;
25984 if (target_ctor
!= error_mark_node
25985 && init
!= error_mark_node
)
25987 error ("mem-initializer for %qD follows constructor delegation",
25991 /* Look for a target constructor. */
25992 if (init
!= error_mark_node
25993 && decl
&& CLASS_TYPE_P (decl
)
25994 && same_type_p (decl
, current_class_type
))
25996 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25999 error ("constructor delegation follows mem-initializer for %qD",
26000 TREE_PURPOSE (inits
));
26003 target_ctor
= init
;
26008 init
= build_tree_list (decl
, init
);
26009 TREE_CHAIN (init
) = inits
;
26017 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26020 set_current_access_from_decl (tree decl
)
26022 if (TREE_PRIVATE (decl
))
26023 current_access_specifier
= access_private_node
;
26024 else if (TREE_PROTECTED (decl
))
26025 current_access_specifier
= access_protected_node
;
26027 current_access_specifier
= access_public_node
;
26030 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26031 is the instantiation (which should have been created with
26032 start_enum) and ARGS are the template arguments to use. */
26035 tsubst_enum (tree tag
, tree newtag
, tree args
)
26039 if (SCOPED_ENUM_P (newtag
))
26040 begin_scope (sk_scoped_enum
, newtag
);
26042 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26047 decl
= TREE_VALUE (e
);
26048 /* Note that in a template enum, the TREE_VALUE is the
26049 CONST_DECL, not the corresponding INTEGER_CST. */
26050 value
= tsubst_expr (DECL_INITIAL (decl
),
26051 args
, tf_warning_or_error
, NULL_TREE
,
26052 /*integral_constant_expression_p=*/true);
26054 /* Give this enumeration constant the correct access. */
26055 set_current_access_from_decl (decl
);
26057 /* Actually build the enumerator itself. Here we're assuming that
26058 enumerators can't have dependent attributes. */
26059 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26060 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26063 if (SCOPED_ENUM_P (newtag
))
26066 finish_enum_value_list (newtag
);
26067 finish_enum (newtag
);
26069 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26070 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26073 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26074 its type -- but without substituting the innermost set of template
26075 arguments. So, innermost set of template parameters will appear in
26079 get_mostly_instantiated_function_type (tree decl
)
26081 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26082 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26085 /* Return truthvalue if we're processing a template different from
26086 the last one involved in diagnostics. */
26088 problematic_instantiation_changed (void)
26090 return current_tinst_level
!= last_error_tinst_level
;
26093 /* Remember current template involved in diagnostics. */
26095 record_last_problematic_instantiation (void)
26097 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26100 struct tinst_level
*
26101 current_instantiation (void)
26103 return current_tinst_level
;
26106 /* Return TRUE if current_function_decl is being instantiated, false
26110 instantiating_current_function_p (void)
26112 return (current_instantiation ()
26113 && (current_instantiation ()->maybe_get_node ()
26114 == current_function_decl
));
26117 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26118 type. Return false for ok, true for disallowed. Issue error and
26119 inform messages under control of COMPLAIN. */
26122 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26124 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26126 else if (TYPE_PTR_P (type
))
26128 else if (TYPE_REF_P (type
)
26129 && !TYPE_REF_IS_RVALUE (type
))
26131 else if (TYPE_PTRMEM_P (type
))
26133 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26135 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26137 if (complain
& tf_error
)
26138 error ("non-type template parameters of deduced class type only "
26139 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26144 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26146 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26148 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26150 /* A bound template template parm could later be instantiated to have a valid
26151 nontype parm type via an alias template. */
26152 else if (cxx_dialect
>= cxx11
26153 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26155 else if (VOID_TYPE_P (type
))
26156 /* Fall through. */;
26157 else if (cxx_dialect
>= cxx20
)
26159 if (dependent_type_p (type
))
26161 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26163 if (structural_type_p (type
))
26165 if (complain
& tf_error
)
26167 auto_diagnostic_group d
;
26168 error ("%qT is not a valid type for a template non-type "
26169 "parameter because it is not structural", type
);
26170 structural_type_p (type
, true);
26174 else if (CLASS_TYPE_P (type
))
26176 if (complain
& tf_error
)
26177 error ("non-type template parameters of class type only available "
26178 "with %<-std=c++20%> or %<-std=gnu++20%>");
26182 if (complain
& tf_error
)
26184 if (type
== error_mark_node
)
26185 inform (input_location
, "invalid template non-type parameter");
26187 error ("%q#T is not a valid type for a template non-type parameter",
26193 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26194 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26197 dependent_type_p_r (tree type
)
26203 A type is dependent if it is:
26205 -- a template parameter. Template template parameters are types
26206 for us (since TYPE_P holds true for them) so we handle
26208 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26209 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26211 /* -- a qualified-id with a nested-name-specifier which contains a
26212 class-name that names a dependent type or whose unqualified-id
26213 names a dependent type. */
26214 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26217 /* An alias template specialization can be dependent even if the
26218 resulting type is not. */
26219 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26222 /* -- a cv-qualified type where the cv-unqualified type is
26224 No code is necessary for this bullet; the code below handles
26225 cv-qualified types, and we don't want to strip aliases with
26226 TYPE_MAIN_VARIANT because of DR 1558. */
26227 /* -- a compound type constructed from any dependent type. */
26228 if (TYPE_PTRMEM_P (type
))
26229 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26230 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26232 else if (INDIRECT_TYPE_P (type
))
26233 return dependent_type_p (TREE_TYPE (type
));
26234 else if (FUNC_OR_METHOD_TYPE_P (type
))
26238 if (dependent_type_p (TREE_TYPE (type
)))
26240 for (arg_type
= TYPE_ARG_TYPES (type
);
26242 arg_type
= TREE_CHAIN (arg_type
))
26243 if (dependent_type_p (TREE_VALUE (arg_type
)))
26245 if (cxx_dialect
>= cxx17
)
26246 /* A value-dependent noexcept-specifier makes the type dependent. */
26247 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26248 if (tree noex
= TREE_PURPOSE (spec
))
26249 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26250 affect overload resolution and treating it as dependent breaks
26251 things. Same for an unparsed noexcept expression. */
26252 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26253 && TREE_CODE (noex
) != DEFERRED_PARSE
26254 && value_dependent_expression_p (noex
))
26258 /* -- an array type constructed from any dependent type or whose
26259 size is specified by a constant expression that is
26262 We checked for type- and value-dependence of the bounds in
26263 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26264 if (TREE_CODE (type
) == ARRAY_TYPE
)
26266 if (TYPE_DOMAIN (type
)
26267 && dependent_type_p (TYPE_DOMAIN (type
)))
26269 return dependent_type_p (TREE_TYPE (type
));
26272 /* -- a template-id in which either the template name is a template
26274 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26276 /* ... or any of the template arguments is a dependent type or
26277 an expression that is type-dependent or value-dependent. */
26278 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26279 && (any_dependent_template_arguments_p
26280 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26283 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26284 dependent; if the argument of the `typeof' expression is not
26285 type-dependent, then it should already been have resolved. */
26286 if (TREE_CODE (type
) == TYPEOF_TYPE
26287 || TREE_CODE (type
) == DECLTYPE_TYPE
26288 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26291 /* A template argument pack is dependent if any of its packed
26293 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26295 tree args
= ARGUMENT_PACK_ARGS (type
);
26296 int i
, len
= TREE_VEC_LENGTH (args
);
26297 for (i
= 0; i
< len
; ++i
)
26298 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26302 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26303 be template parameters. */
26304 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26307 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26310 /* The standard does not specifically mention types that are local
26311 to template functions or local classes, but they should be
26312 considered dependent too. For example:
26314 template <int I> void f() {
26319 The size of `E' cannot be known until the value of `I' has been
26320 determined. Therefore, `E' must be considered dependent. */
26321 scope
= TYPE_CONTEXT (type
);
26322 if (scope
&& TYPE_P (scope
))
26323 return dependent_type_p (scope
);
26324 /* Don't use type_dependent_expression_p here, as it can lead
26325 to infinite recursion trying to determine whether a lambda
26326 nested in a lambda is dependent (c++/47687). */
26327 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26328 && DECL_LANG_SPECIFIC (scope
)
26329 && DECL_TEMPLATE_INFO (scope
)
26330 && (any_dependent_template_arguments_p
26331 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26334 /* Other types are non-dependent. */
26338 /* Returns TRUE if TYPE is dependent, in the sense of
26339 [temp.dep.type]. Note that a NULL type is considered dependent. */
26342 dependent_type_p (tree type
)
26344 /* If there are no template parameters in scope, then there can't be
26345 any dependent types. */
26346 if (!processing_template_decl
)
26348 /* If we are not processing a template, then nobody should be
26349 providing us with a dependent type. */
26351 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26355 /* If the type is NULL, we have not computed a type for the entity
26356 in question; in that case, the type is dependent. */
26360 /* Erroneous types can be considered non-dependent. */
26361 if (type
== error_mark_node
)
26364 /* Getting here with global_type_node means we improperly called this
26365 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26366 gcc_checking_assert (type
!= global_type_node
);
26368 /* If we have not already computed the appropriate value for TYPE,
26370 if (!TYPE_DEPENDENT_P_VALID (type
))
26372 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26373 TYPE_DEPENDENT_P_VALID (type
) = 1;
26376 return TYPE_DEPENDENT_P (type
);
26379 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26380 lookup. In other words, a dependent type that is not the current
26384 dependent_scope_p (tree scope
)
26386 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26387 && !currently_open_class (scope
));
26390 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26391 an unknown base of 'this' (and is therefore instantiation-dependent). */
26394 unknown_base_ref_p (tree t
)
26396 if (!current_class_ptr
)
26399 tree mem
= TREE_OPERAND (t
, 1);
26400 if (shared_member_p (mem
))
26403 tree cur
= current_nonlambda_class_type ();
26404 if (!any_dependent_bases_p (cur
))
26407 tree ctx
= TREE_OPERAND (t
, 0);
26408 if (DERIVED_FROM_P (ctx
, cur
))
26414 /* T is a SCOPE_REF; return whether we need to consider it
26415 instantiation-dependent so that we can check access at instantiation
26416 time even though we know which member it resolves to. */
26419 instantiation_dependent_scope_ref_p (tree t
)
26421 if (DECL_P (TREE_OPERAND (t
, 1))
26422 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26423 && !unknown_base_ref_p (t
)
26424 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26425 TREE_OPERAND (t
, 1)))
26431 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26432 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26435 /* Note that this predicate is not appropriate for general expressions;
26436 only constant expressions (that satisfy potential_constant_expression)
26437 can be tested for value dependence. */
26440 value_dependent_expression_p (tree expression
)
26442 if (!processing_template_decl
|| expression
== NULL_TREE
)
26445 /* A type-dependent expression is also value-dependent. */
26446 if (type_dependent_expression_p (expression
))
26449 switch (TREE_CODE (expression
))
26452 /* A dependent member function of the current instantiation. */
26453 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26455 case FUNCTION_DECL
:
26456 /* A dependent member function of the current instantiation. */
26457 if (DECL_CLASS_SCOPE_P (expression
)
26458 && dependent_type_p (DECL_CONTEXT (expression
)))
26462 case IDENTIFIER_NODE
:
26463 /* A name that has not been looked up -- must be dependent. */
26466 case TEMPLATE_PARM_INDEX
:
26467 /* A non-type template parm. */
26471 /* A non-type template parm. */
26472 if (DECL_TEMPLATE_PARM_P (expression
))
26474 return value_dependent_expression_p (DECL_INITIAL (expression
));
26477 /* A constant with literal type and is initialized
26478 with an expression that is value-dependent. */
26479 if (DECL_DEPENDENT_INIT_P (expression
)
26480 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26481 || TYPE_REF_P (TREE_TYPE (expression
)))
26483 if (DECL_HAS_VALUE_EXPR_P (expression
))
26485 tree value_expr
= DECL_VALUE_EXPR (expression
);
26486 if (value_dependent_expression_p (value_expr
)
26487 /* __PRETTY_FUNCTION__ inside a template function is dependent
26488 on the name of the function. */
26489 || (DECL_PRETTY_FUNCTION_P (expression
)
26490 /* It might be used in a template, but not a template
26491 function, in which case its DECL_VALUE_EXPR will be
26493 && value_expr
== error_mark_node
))
26498 case DYNAMIC_CAST_EXPR
:
26499 case STATIC_CAST_EXPR
:
26500 case CONST_CAST_EXPR
:
26501 case REINTERPRET_CAST_EXPR
:
26503 case IMPLICIT_CONV_EXPR
:
26504 /* These expressions are value-dependent if the type to which
26505 the cast occurs is dependent or the expression being casted
26506 is value-dependent. */
26508 tree type
= TREE_TYPE (expression
);
26510 if (dependent_type_p (type
))
26513 /* A functional cast has a list of operands. */
26514 expression
= TREE_OPERAND (expression
, 0);
26517 /* If there are no operands, it must be an expression such
26518 as "int()". This should not happen for aggregate types
26519 because it would form non-constant expressions. */
26520 gcc_assert (cxx_dialect
>= cxx11
26521 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26526 if (TREE_CODE (expression
) == TREE_LIST
)
26527 return any_value_dependent_elements_p (expression
);
26529 return value_dependent_expression_p (expression
);
26533 if (SIZEOF_EXPR_TYPE_P (expression
))
26534 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26538 /* A `sizeof' expression is value-dependent if the operand is
26539 type-dependent or is a pack expansion. */
26540 expression
= TREE_OPERAND (expression
, 0);
26541 if (PACK_EXPANSION_P (expression
))
26543 else if (TYPE_P (expression
))
26544 return dependent_type_p (expression
);
26545 return instantiation_dependent_uneval_expression_p (expression
);
26547 case AT_ENCODE_EXPR
:
26548 /* An 'encode' expression is value-dependent if the operand is
26550 expression
= TREE_OPERAND (expression
, 0);
26551 return dependent_type_p (expression
);
26553 case NOEXCEPT_EXPR
:
26554 expression
= TREE_OPERAND (expression
, 0);
26555 return instantiation_dependent_uneval_expression_p (expression
);
26558 /* All instantiation-dependent expressions should also be considered
26559 value-dependent. */
26560 return instantiation_dependent_scope_ref_p (expression
);
26562 case COMPONENT_REF
:
26563 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26564 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26566 case NONTYPE_ARGUMENT_PACK
:
26567 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26568 is value-dependent. */
26570 tree values
= ARGUMENT_PACK_ARGS (expression
);
26571 int i
, len
= TREE_VEC_LENGTH (values
);
26573 for (i
= 0; i
< len
; ++i
)
26574 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26582 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26584 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26590 if (TREE_CODE (type2
) != TREE_LIST
)
26591 return dependent_type_p (type2
);
26593 for (; type2
; type2
= TREE_CHAIN (type2
))
26594 if (dependent_type_p (TREE_VALUE (type2
)))
26601 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26602 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26605 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26606 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26610 tree op
= TREE_OPERAND (expression
, 0);
26611 return (value_dependent_expression_p (op
)
26612 || has_value_dependent_address (op
));
26615 case REQUIRES_EXPR
:
26616 /* Treat all requires-expressions as value-dependent so
26617 we don't try to fold them. */
26621 return dependent_type_p (TREE_OPERAND (expression
, 0));
26625 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26627 tree fn
= get_callee_fndecl (expression
);
26629 nargs
= call_expr_nargs (expression
);
26630 for (i
= 0; i
< nargs
; ++i
)
26632 tree op
= CALL_EXPR_ARG (expression
, i
);
26633 /* In a call to a constexpr member function, look through the
26634 implicit ADDR_EXPR on the object argument so that it doesn't
26635 cause the call to be considered value-dependent. We also
26636 look through it in potential_constant_expression. */
26637 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26638 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26639 && TREE_CODE (op
) == ADDR_EXPR
)
26640 op
= TREE_OPERAND (op
, 0);
26641 if (value_dependent_expression_p (op
))
26647 case TEMPLATE_ID_EXPR
:
26648 return concept_definition_p (TREE_OPERAND (expression
, 0));
26654 if (dependent_type_p (TREE_TYPE (expression
)))
26656 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26657 if (value_dependent_expression_p (val
))
26663 /* Treat a GNU statement expression as dependent to avoid crashing
26664 under instantiate_non_dependent_expr; it can't be constant. */
26668 /* A constant expression is value-dependent if any subexpression is
26669 value-dependent. */
26670 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26672 case tcc_reference
:
26674 case tcc_comparison
:
26676 case tcc_expression
:
26679 int i
, len
= cp_tree_operand_length (expression
);
26681 for (i
= 0; i
< len
; i
++)
26683 tree t
= TREE_OPERAND (expression
, i
);
26685 /* In some cases, some of the operands may be missing.
26686 (For example, in the case of PREDECREMENT_EXPR, the
26687 amount to increment by may be missing.) That doesn't
26688 make the expression dependent. */
26689 if (t
&& value_dependent_expression_p (t
))
26700 /* The expression is not value-dependent. */
26704 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26705 [temp.dep.expr]. Note that an expression with no type is
26706 considered dependent. Other parts of the compiler arrange for an
26707 expression with type-dependent subexpressions to have no type, so
26708 this function doesn't have to be fully recursive. */
26711 type_dependent_expression_p (tree expression
)
26713 if (!processing_template_decl
)
26716 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26719 STRIP_ANY_LOCATION_WRAPPER (expression
);
26721 /* An unresolved name is always dependent. */
26722 if (identifier_p (expression
)
26723 || TREE_CODE (expression
) == USING_DECL
26724 || TREE_CODE (expression
) == WILDCARD_DECL
)
26727 /* A lambda-expression in template context is dependent. dependent_type_p is
26728 true for a lambda in the scope of a class or function template, but that
26729 doesn't cover all template contexts, like a default template argument. */
26730 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26733 /* A fold expression is type-dependent. */
26734 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26735 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26736 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26737 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26740 /* Some expression forms are never type-dependent. */
26741 if (TREE_CODE (expression
) == SIZEOF_EXPR
26742 || TREE_CODE (expression
) == ALIGNOF_EXPR
26743 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26744 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26745 || TREE_CODE (expression
) == TRAIT_EXPR
26746 || TREE_CODE (expression
) == TYPEID_EXPR
26747 || TREE_CODE (expression
) == DELETE_EXPR
26748 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26749 || TREE_CODE (expression
) == THROW_EXPR
26750 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26753 /* The types of these expressions depends only on the type to which
26754 the cast occurs. */
26755 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26756 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26757 || TREE_CODE (expression
) == CONST_CAST_EXPR
26758 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26759 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26760 || TREE_CODE (expression
) == CAST_EXPR
)
26761 return dependent_type_p (TREE_TYPE (expression
));
26763 /* The types of these expressions depends only on the type created
26764 by the expression. */
26765 if (TREE_CODE (expression
) == NEW_EXPR
26766 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26768 /* For NEW_EXPR tree nodes created inside a template, either
26769 the object type itself or a TREE_LIST may appear as the
26771 tree type
= TREE_OPERAND (expression
, 1);
26772 if (TREE_CODE (type
) == TREE_LIST
)
26773 /* This is an array type. We need to check array dimensions
26775 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26776 || value_dependent_expression_p
26777 (TREE_OPERAND (TREE_VALUE (type
), 1));
26779 return dependent_type_p (type
);
26782 if (TREE_CODE (expression
) == SCOPE_REF
)
26784 tree scope
= TREE_OPERAND (expression
, 0);
26785 tree name
= TREE_OPERAND (expression
, 1);
26787 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26788 contains an identifier associated by name lookup with one or more
26789 declarations declared with a dependent type, or...a
26790 nested-name-specifier or qualified-id that names a member of an
26791 unknown specialization. */
26792 return (type_dependent_expression_p (name
)
26793 || dependent_scope_p (scope
));
26796 if (TREE_CODE (expression
) == TEMPLATE_DECL
26797 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26798 return uses_outer_template_parms (expression
);
26800 if (TREE_CODE (expression
) == STMT_EXPR
)
26801 expression
= stmt_expr_value_expr (expression
);
26803 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26808 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26810 if (type_dependent_expression_p (elt
))
26816 /* A static data member of the current instantiation with incomplete
26817 array type is type-dependent, as the definition and specializations
26818 can have different bounds. */
26819 if (VAR_P (expression
)
26820 && DECL_CLASS_SCOPE_P (expression
)
26821 && dependent_type_p (DECL_CONTEXT (expression
))
26822 && VAR_HAD_UNKNOWN_BOUND (expression
))
26825 /* An array of unknown bound depending on a variadic parameter, eg:
26827 template<typename... Args>
26828 void foo (Args... args)
26830 int arr[] = { args... };
26833 template<int... vals>
26836 int arr[] = { vals... };
26839 If the array has no length and has an initializer, it must be that
26840 we couldn't determine its length in cp_complete_array_type because
26841 it is dependent. */
26842 if (VAR_P (expression
)
26843 && TREE_TYPE (expression
) != NULL_TREE
26844 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26845 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26846 && DECL_INITIAL (expression
))
26849 /* A function or variable template-id is type-dependent if it has any
26850 dependent template arguments. */
26851 if (VAR_OR_FUNCTION_DECL_P (expression
)
26852 && DECL_LANG_SPECIFIC (expression
)
26853 && DECL_TEMPLATE_INFO (expression
))
26855 /* Consider the innermost template arguments, since those are the ones
26856 that come from the template-id; the template arguments for the
26857 enclosing class do not make it type-dependent unless they are used in
26858 the type of the decl. */
26859 if (instantiates_primary_template_p (expression
)
26860 && (any_dependent_template_arguments_p
26861 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26865 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26866 type-dependent. Checking this is important for functions with auto return
26867 type, which looks like a dependent type. */
26868 if (TREE_CODE (expression
) == FUNCTION_DECL
26869 && !(DECL_CLASS_SCOPE_P (expression
)
26870 && dependent_type_p (DECL_CONTEXT (expression
)))
26871 && !(DECL_LANG_SPECIFIC (expression
)
26872 && DECL_FRIEND_P (expression
)
26873 && (!DECL_FRIEND_CONTEXT (expression
)
26874 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26875 && !DECL_LOCAL_FUNCTION_P (expression
))
26877 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26878 || undeduced_auto_decl (expression
));
26882 /* Always dependent, on the number of arguments if nothing else. */
26883 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26886 if (TREE_TYPE (expression
) == unknown_type_node
)
26888 if (TREE_CODE (expression
) == ADDR_EXPR
)
26889 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26890 if (TREE_CODE (expression
) == COMPONENT_REF
26891 || TREE_CODE (expression
) == OFFSET_REF
)
26893 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26895 expression
= TREE_OPERAND (expression
, 1);
26896 if (identifier_p (expression
))
26899 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26900 if (TREE_CODE (expression
) == SCOPE_REF
)
26903 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26904 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
26905 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
26908 if (BASELINK_P (expression
))
26910 if (BASELINK_OPTYPE (expression
)
26911 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26913 expression
= BASELINK_FUNCTIONS (expression
);
26916 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26918 if (any_dependent_template_arguments_p
26919 (TREE_OPERAND (expression
, 1)))
26921 expression
= TREE_OPERAND (expression
, 0);
26922 if (identifier_p (expression
))
26926 gcc_assert (OVL_P (expression
));
26928 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26929 if (type_dependent_expression_p (*iter
))
26935 /* The type of a non-type template parm declared with a placeholder type
26936 depends on the corresponding template argument, even though
26937 placeholders are not normally considered dependent. */
26938 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26939 && is_auto (TREE_TYPE (expression
)))
26942 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26944 /* Dependent type attributes might not have made it from the decl to
26946 if (DECL_P (expression
)
26947 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26950 return (dependent_type_p (TREE_TYPE (expression
)));
26953 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26954 type-dependent if the expression refers to a member of the current
26955 instantiation and the type of the referenced member is dependent, or the
26956 class member access expression refers to a member of an unknown
26959 This function returns true if the OBJECT in such a class member access
26960 expression is of an unknown specialization. */
26963 type_dependent_object_expression_p (tree object
)
26965 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26967 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26969 tree scope
= TREE_TYPE (object
);
26970 return (!scope
|| dependent_scope_p (scope
));
26973 /* walk_tree callback function for instantiation_dependent_expression_p,
26974 below. Returns non-zero if a dependent subexpression is found. */
26977 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26982 /* We don't have to worry about decltype currently because decltype
26983 of an instantiation-dependent expr is a dependent type. This
26984 might change depending on the resolution of DR 1172. */
26985 *walk_subtrees
= false;
26988 enum tree_code code
= TREE_CODE (*tp
);
26991 /* Don't treat an argument list as dependent just because it has no
26995 case NONTYPE_ARGUMENT_PACK
:
26998 case TEMPLATE_PARM_INDEX
:
26999 if (dependent_type_p (TREE_TYPE (*tp
)))
27001 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27003 /* We'll check value-dependence separately. */
27006 /* Handle expressions with type operands. */
27010 case AT_ENCODE_EXPR
:
27012 tree op
= TREE_OPERAND (*tp
, 0);
27013 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27014 op
= TREE_TYPE (op
);
27017 if (dependent_type_p (op
))
27021 *walk_subtrees
= false;
27028 case COMPONENT_REF
:
27029 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27030 /* In a template, finish_class_member_access_expr creates a
27031 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27032 type-dependent, so that we can check access control at
27033 instantiation time (PR 42277). See also Core issue 1273. */
27038 if (instantiation_dependent_scope_ref_p (*tp
))
27043 /* Treat statement-expressions as dependent. */
27047 /* Treat requires-expressions as dependent. */
27048 case REQUIRES_EXPR
:
27052 /* Treat concept checks as dependent. */
27053 if (concept_check_p (*tp
))
27057 case TEMPLATE_ID_EXPR
:
27058 /* Treat concept checks as dependent. */
27059 if (concept_check_p (*tp
))
27064 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27072 if (type_dependent_expression_p (*tp
))
27078 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27079 sense defined by the ABI:
27081 "An expression is instantiation-dependent if it is type-dependent
27082 or value-dependent, or it has a subexpression that is type-dependent
27083 or value-dependent."
27085 Except don't actually check value-dependence for unevaluated expressions,
27086 because in sizeof(i) we don't care about the value of i. Checking
27087 type-dependence will in turn check value-dependence of array bounds/template
27088 arguments as needed. */
27091 instantiation_dependent_uneval_expression_p (tree expression
)
27095 if (!processing_template_decl
)
27098 if (expression
== error_mark_node
)
27101 result
= cp_walk_tree_without_duplicates (&expression
,
27102 instantiation_dependent_r
, NULL
);
27103 return result
!= NULL_TREE
;
27106 /* As above, but also check value-dependence of the expression as a whole. */
27109 instantiation_dependent_expression_p (tree expression
)
27111 return (instantiation_dependent_uneval_expression_p (expression
)
27112 || value_dependent_expression_p (expression
));
27115 /* Like type_dependent_expression_p, but it also works while not processing
27116 a template definition, i.e. during substitution or mangling. */
27119 type_dependent_expression_p_push (tree expr
)
27122 ++processing_template_decl
;
27123 b
= type_dependent_expression_p (expr
);
27124 --processing_template_decl
;
27128 /* Returns TRUE if ARGS contains a type-dependent expression. */
27131 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27136 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27138 if (type_dependent_expression_p (arg
))
27144 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27145 expressions) contains any type-dependent expressions. */
27148 any_type_dependent_elements_p (const_tree list
)
27150 for (; list
; list
= TREE_CHAIN (list
))
27151 if (type_dependent_expression_p (TREE_VALUE (list
)))
27157 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27158 expressions) contains any value-dependent expressions. */
27161 any_value_dependent_elements_p (const_tree list
)
27163 for (; list
; list
= TREE_CHAIN (list
))
27164 if (value_dependent_expression_p (TREE_VALUE (list
)))
27170 /* Returns TRUE if the ARG (a template argument) is dependent. */
27173 dependent_template_arg_p (tree arg
)
27175 if (!processing_template_decl
)
27178 /* Assume a template argument that was wrongly written by the user
27179 is dependent. This is consistent with what
27180 any_dependent_template_arguments_p [that calls this function]
27182 if (!arg
|| arg
== error_mark_node
)
27185 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27186 arg
= argument_pack_select_arg (arg
);
27188 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27190 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27192 if (DECL_TEMPLATE_PARM_P (arg
))
27194 /* A member template of a dependent class is not necessarily
27195 type-dependent, but it is a dependent template argument because it
27196 will be a member of an unknown specialization to that template. */
27197 tree scope
= CP_DECL_CONTEXT (arg
);
27198 return TYPE_P (scope
) && dependent_type_p (scope
);
27200 else if (ARGUMENT_PACK_P (arg
))
27202 tree args
= ARGUMENT_PACK_ARGS (arg
);
27203 int i
, len
= TREE_VEC_LENGTH (args
);
27204 for (i
= 0; i
< len
; ++i
)
27206 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27212 else if (TYPE_P (arg
))
27213 return dependent_type_p (arg
);
27215 return value_dependent_expression_p (arg
);
27218 /* Returns true if ARGS (a collection of template arguments) contains
27219 any types that require structural equality testing. */
27222 any_template_arguments_need_structural_equality_p (tree args
)
27229 if (args
== error_mark_node
)
27232 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27234 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27235 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27237 tree arg
= TREE_VEC_ELT (level
, j
);
27238 tree packed_args
= NULL_TREE
;
27241 if (ARGUMENT_PACK_P (arg
))
27243 /* Look inside the argument pack. */
27244 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27245 len
= TREE_VEC_LENGTH (packed_args
);
27248 for (k
= 0; k
< len
; ++k
)
27251 arg
= TREE_VEC_ELT (packed_args
, k
);
27253 if (error_operand_p (arg
))
27255 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27257 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27259 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27260 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27269 /* Returns true if ARGS (a collection of template arguments) contains
27270 any dependent arguments. */
27273 any_dependent_template_arguments_p (const_tree args
)
27280 if (args
== error_mark_node
)
27283 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27285 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27286 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27287 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27294 /* Returns true if ARGS contains any errors. */
27297 any_erroneous_template_args_p (const_tree args
)
27302 if (args
== error_mark_node
)
27305 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27307 if (tree ti
= get_template_info (args
))
27308 args
= TI_ARGS (ti
);
27316 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27318 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27319 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27320 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27327 /* Returns TRUE if the template TMPL is type-dependent. */
27330 dependent_template_p (tree tmpl
)
27332 if (TREE_CODE (tmpl
) == OVERLOAD
)
27334 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27335 if (dependent_template_p (*iter
))
27340 /* Template template parameters are dependent. */
27341 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27342 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27344 /* So are names that have not been looked up. */
27345 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27350 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27353 dependent_template_id_p (tree tmpl
, tree args
)
27355 return (dependent_template_p (tmpl
)
27356 || any_dependent_template_arguments_p (args
));
27359 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27363 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27367 if (!processing_template_decl
)
27370 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27372 tree decl
= TREE_VEC_ELT (declv
, i
);
27373 tree init
= TREE_VEC_ELT (initv
, i
);
27374 tree cond
= TREE_VEC_ELT (condv
, i
);
27375 tree incr
= TREE_VEC_ELT (incrv
, i
);
27377 if (type_dependent_expression_p (decl
)
27378 || TREE_CODE (decl
) == SCOPE_REF
)
27381 if (init
&& type_dependent_expression_p (init
))
27384 if (cond
== global_namespace
)
27387 if (type_dependent_expression_p (cond
))
27390 if (COMPARISON_CLASS_P (cond
)
27391 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27392 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27395 if (TREE_CODE (incr
) == MODOP_EXPR
)
27397 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27398 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27401 else if (type_dependent_expression_p (incr
))
27403 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27405 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27407 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27409 tree t
= TREE_OPERAND (incr
, 1);
27410 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27411 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27414 /* If this loop has a class iterator with != comparison
27415 with increment other than i++/++i/i--/--i, make sure the
27416 increment is constant. */
27417 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27418 && TREE_CODE (cond
) == NE_EXPR
)
27420 if (TREE_OPERAND (t
, 0) == decl
)
27421 t
= TREE_OPERAND (t
, 1);
27423 t
= TREE_OPERAND (t
, 0);
27424 if (TREE_CODE (t
) != INTEGER_CST
)
27434 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27435 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27436 no such TYPE can be found. Note that this function peers inside
27437 uninstantiated templates and therefore should be used only in
27438 extremely limited situations. ONLY_CURRENT_P restricts this
27439 peering to the currently open classes hierarchy (which is required
27440 when comparing types). */
27443 resolve_typename_type (tree type
, bool only_current_p
)
27452 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27454 scope
= TYPE_CONTEXT (type
);
27455 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27456 gcc_checking_assert (uses_template_parms (scope
));
27458 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27459 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27460 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27461 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27462 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27463 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27464 the TYPENAME_TYPE instead, we avoid messing up with a possible
27465 typedef variant case. */
27466 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27468 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27469 it first before we can figure out what NAME refers to. */
27470 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27472 if (TYPENAME_IS_RESOLVING_P (scope
))
27473 /* Given a class template A with a dependent base with nested type C,
27474 typedef typename A::C::C C will land us here, as trying to resolve
27475 the initial A::C leads to the local C typedef, which leads back to
27476 A::C::C. So we break the recursion now. */
27479 scope
= resolve_typename_type (scope
, only_current_p
);
27481 /* If we don't know what SCOPE refers to, then we cannot resolve the
27483 if (!CLASS_TYPE_P (scope
))
27485 /* If this is a typedef, we don't want to look inside (c++/11987). */
27486 if (typedef_variant_p (type
))
27488 /* If SCOPE isn't the template itself, it will not have a valid
27489 TYPE_FIELDS list. */
27490 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27491 /* scope is either the template itself or a compatible instantiation
27492 like X<T>, so look up the name in the original template. */
27493 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27494 /* If scope has no fields, it can't be a current instantiation. Check this
27495 before currently_open_class to avoid infinite recursion (71515). */
27496 if (!TYPE_FIELDS (scope
))
27498 /* If the SCOPE is not the current instantiation, there's no reason
27499 to look inside it. */
27500 if (only_current_p
&& !currently_open_class (scope
))
27502 /* Enter the SCOPE so that name lookup will be resolved as if we
27503 were in the class definition. In particular, SCOPE will no
27504 longer be considered a dependent type. */
27505 pushed_scope
= push_scope (scope
);
27506 /* Look up the declaration. */
27507 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27508 tf_warning_or_error
);
27510 result
= NULL_TREE
;
27512 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27513 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27514 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27517 else if (identifier_p (fullname
)
27518 && TREE_CODE (decl
) == TYPE_DECL
)
27520 result
= TREE_TYPE (decl
);
27521 if (result
== error_mark_node
)
27522 result
= NULL_TREE
;
27524 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27525 && DECL_CLASS_TEMPLATE_P (decl
))
27527 /* Obtain the template and the arguments. */
27528 tree tmpl
= TREE_OPERAND (fullname
, 0);
27529 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27531 /* We get here with a plain identifier because a previous tentative
27532 parse of the nested-name-specifier as part of a ptr-operator saw
27533 ::template X<A>. The use of ::template is necessary in a
27534 ptr-operator, but wrong in a declarator-id.
27536 [temp.names]: In a qualified-id of a declarator-id, the keyword
27537 template shall not appear at the top level. */
27538 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27539 "keyword %<template%> not allowed in declarator-id");
27542 tree args
= TREE_OPERAND (fullname
, 1);
27543 /* Instantiate the template. */
27544 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27545 /*entering_scope=*/true,
27546 tf_error
| tf_user
);
27547 if (result
== error_mark_node
)
27548 result
= NULL_TREE
;
27551 /* Leave the SCOPE. */
27553 pop_scope (pushed_scope
);
27555 /* If we failed to resolve it, return the original typename. */
27559 /* If lookup found a typename type, resolve that too. */
27560 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27562 /* Ill-formed programs can cause infinite recursion here, so we
27563 must catch that. */
27564 TYPENAME_IS_RESOLVING_P (result
) = 1;
27565 result
= resolve_typename_type (result
, only_current_p
);
27566 TYPENAME_IS_RESOLVING_P (result
) = 0;
27569 /* Qualify the resulting type. */
27570 quals
= cp_type_quals (type
);
27572 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27577 /* EXPR is an expression which is not type-dependent. Return a proxy
27578 for EXPR that can be used to compute the types of larger
27579 expressions containing EXPR. */
27582 build_non_dependent_expr (tree expr
)
27584 tree orig_expr
= expr
;
27587 /* When checking, try to get a constant value for all non-dependent
27588 expressions in order to expose bugs in *_dependent_expression_p
27589 and constexpr. This can affect code generation, see PR70704, so
27590 only do this for -fchecking=2. */
27591 if (flag_checking
> 1
27592 && cxx_dialect
>= cxx11
27593 /* Don't do this during nsdmi parsing as it can lead to
27594 unexpected recursive instantiations. */
27595 && !parsing_nsdmi ()
27596 /* Don't do this during concept processing either and for
27597 the same reason. */
27598 && !processing_constraint_expression_p ())
27599 fold_non_dependent_expr (expr
, tf_none
);
27601 STRIP_ANY_LOCATION_WRAPPER (expr
);
27603 /* Preserve OVERLOADs; the functions must be available to resolve
27606 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27607 inner_expr
= stmt_expr_value_expr (inner_expr
);
27608 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27609 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27610 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27611 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27612 if (is_overloaded_fn (inner_expr
)
27613 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27615 /* There is no need to return a proxy for a variable or enumerator. */
27616 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27618 /* Preserve string constants; conversions from string constants to
27619 "char *" are allowed, even though normally a "const char *"
27620 cannot be used to initialize a "char *". */
27621 if (TREE_CODE (expr
) == STRING_CST
)
27623 /* Preserve void and arithmetic constants, as an optimization -- there is no
27624 reason to create a new node. */
27625 if (TREE_CODE (expr
) == VOID_CST
27626 || TREE_CODE (expr
) == INTEGER_CST
27627 || TREE_CODE (expr
) == REAL_CST
)
27629 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27630 There is at least one place where we want to know that a
27631 particular expression is a throw-expression: when checking a ?:
27632 expression, there are special rules if the second or third
27633 argument is a throw-expression. */
27634 if (TREE_CODE (expr
) == THROW_EXPR
)
27637 /* Don't wrap an initializer list, we need to be able to look inside. */
27638 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27641 /* Don't wrap a dummy object, we need to be able to test for it. */
27642 if (is_dummy_object (expr
))
27645 if (TREE_CODE (expr
) == COND_EXPR
)
27646 return build3 (COND_EXPR
,
27648 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27649 (TREE_OPERAND (expr
, 1)
27650 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27651 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27652 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27653 if (TREE_CODE (expr
) == COMPOUND_EXPR
27654 && !COMPOUND_EXPR_OVERLOADED (expr
))
27655 return build2 (COMPOUND_EXPR
,
27657 TREE_OPERAND (expr
, 0),
27658 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27660 /* If the type is unknown, it can't really be non-dependent */
27661 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27663 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27664 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27665 TREE_TYPE (expr
), expr
);
27668 /* ARGS is a vector of expressions as arguments to a function call.
27669 Replace the arguments with equivalent non-dependent expressions.
27670 This modifies ARGS in place. */
27673 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27678 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27680 tree newarg
= build_non_dependent_expr (arg
);
27682 (*args
)[ix
] = newarg
;
27686 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27687 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27688 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27691 make_auto_1 (tree name
, bool set_canonical
)
27693 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27694 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27695 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27696 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27697 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27698 TYPE_NAME (au
), NULL_TREE
);
27700 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27701 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27702 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27703 if (name
== decltype_auto_identifier
)
27704 AUTO_IS_DECLTYPE (au
) = true;
27710 make_decltype_auto (void)
27712 return make_auto_1 (decltype_auto_identifier
, true);
27718 return make_auto_1 (auto_identifier
, true);
27721 /* Return a C++17 deduction placeholder for class template TMPL. */
27724 make_template_placeholder (tree tmpl
)
27726 tree t
= make_auto_1 (auto_identifier
, false);
27727 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27728 /* Our canonical type depends on the placeholder. */
27729 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27733 /* True iff T is a C++17 class template deduction placeholder. */
27736 template_placeholder_p (tree t
)
27738 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27741 /* Make a "constrained auto" type-specifier. This is an auto or
27742 decltype(auto) type with constraints that must be associated after
27743 deduction. The constraint is formed from the given concept CON
27744 and its optional sequence of template arguments ARGS.
27746 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27749 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27751 /* Build the constraint. */
27752 tree tmpl
= DECL_TI_TEMPLATE (con
);
27754 if (TREE_CODE (con
) == FUNCTION_DECL
)
27755 expr
= ovl_make (tmpl
);
27756 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27758 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27760 /* Our canonical type depends on the constraint. */
27761 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27763 /* Attach the constraint to the type declaration. */
27764 return TYPE_NAME (type
);
27767 /* Make a "constrained auto" type-specifier. */
27770 make_constrained_auto (tree con
, tree args
)
27772 tree type
= make_auto_1 (auto_identifier
, false);
27773 return make_constrained_placeholder_type (type
, con
, args
);
27776 /* Make a "constrained decltype(auto)" type-specifier. */
27779 make_constrained_decltype_auto (tree con
, tree args
)
27781 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27782 return make_constrained_placeholder_type (type
, con
, args
);
27785 /* Build and return a concept definition. Like other templates, the
27786 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27787 the TEMPLATE_DECL. */
27790 finish_concept_definition (cp_expr id
, tree init
)
27792 gcc_assert (identifier_p (id
));
27793 gcc_assert (processing_template_decl
);
27795 location_t loc
= id
.get_location();
27797 /* A concept-definition shall not have associated constraints. */
27798 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27800 error_at (loc
, "a concept cannot be constrained");
27801 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27804 /* A concept-definition shall appear in namespace scope. Templates
27805 aren't allowed in block scope, so we only need to check for class
27807 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27809 error_at (loc
, "concept %qE not in namespace scope", *id
);
27810 return error_mark_node
;
27813 /* Initially build the concept declaration; its type is bool. */
27814 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27815 DECL_CONTEXT (decl
) = current_scope ();
27816 DECL_INITIAL (decl
) = init
;
27818 /* Push the enclosing template. */
27819 return push_template_decl (decl
);
27822 /* Given type ARG, return std::initializer_list<ARG>. */
27827 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27829 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27831 gcc_rich_location
richloc (input_location
);
27832 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27833 error_at (&richloc
,
27834 "deducing from brace-enclosed initializer list"
27835 " requires %<#include <initializer_list>%>");
27837 return error_mark_node
;
27839 tree argvec
= make_tree_vec (1);
27840 TREE_VEC_ELT (argvec
, 0) = arg
;
27842 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27843 NULL_TREE
, 0, tf_warning_or_error
);
27846 /* Replace auto in TYPE with std::initializer_list<auto>. */
27849 listify_autos (tree type
, tree auto_node
)
27851 tree init_auto
= listify (strip_top_quals (auto_node
));
27852 tree argvec
= make_tree_vec (1);
27853 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27854 if (processing_template_decl
)
27855 argvec
= add_to_template_args (current_template_args (), argvec
);
27856 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27859 /* Hash traits for hashing possibly constrained 'auto'
27860 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27862 struct auto_hash
: default_hash_traits
<tree
>
27864 static inline hashval_t
hash (tree
);
27865 static inline bool equal (tree
, tree
);
27868 /* Hash the 'auto' T. */
27871 auto_hash::hash (tree t
)
27873 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27874 /* Matching constrained-type-specifiers denote the same template
27875 parameter, so hash the constraint. */
27876 return hash_placeholder_constraint (c
);
27878 /* But unconstrained autos are all separate, so just hash the pointer. */
27879 return iterative_hash_object (t
, 0);
27882 /* Compare two 'auto's. */
27885 auto_hash::equal (tree t1
, tree t2
)
27890 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27891 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27893 /* Two unconstrained autos are distinct. */
27897 return equivalent_placeholder_constraints (c1
, c2
);
27900 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27901 constrained) auto, add it to the vector. */
27904 extract_autos_r (tree t
, void *data
)
27906 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27909 /* All the autos were built with index 0; fix that up now. */
27910 tree
*p
= hash
.find_slot (t
, INSERT
);
27913 /* If this is a repeated constrained-type-specifier, use the index we
27915 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27918 /* Otherwise this is new, so use the current count. */
27920 idx
= hash
.elements () - 1;
27922 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27925 /* Always keep walking. */
27929 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27930 says they can appear anywhere in the type. */
27933 extract_autos (tree type
)
27935 hash_set
<tree
> visited
;
27936 hash_table
<auto_hash
> hash (2);
27938 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27940 tree tree_vec
= make_tree_vec (hash
.elements());
27941 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27942 iter
!= hash
.end(); ++iter
)
27945 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27946 TREE_VEC_ELT (tree_vec
, i
)
27947 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27953 /* The stem for deduction guide names. */
27954 const char *const dguide_base
= "__dguide_";
27956 /* Return the name for a deduction guide for class template TMPL. */
27959 dguide_name (tree tmpl
)
27961 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27962 tree tname
= TYPE_IDENTIFIER (type
);
27963 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27964 + IDENTIFIER_LENGTH (tname
));
27965 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27966 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27967 IDENTIFIER_LENGTH (tname
) + 1);
27968 tree dname
= get_identifier (buf
);
27969 TREE_TYPE (dname
) = type
;
27973 /* True if NAME is the name of a deduction guide. */
27976 dguide_name_p (tree name
)
27978 return (TREE_CODE (name
) == IDENTIFIER_NODE
27979 && TREE_TYPE (name
)
27980 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27981 strlen (dguide_base
)));
27984 /* True if FN is a deduction guide. */
27987 deduction_guide_p (const_tree fn
)
27990 if (tree name
= DECL_NAME (fn
))
27991 return dguide_name_p (name
);
27995 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27998 copy_guide_p (const_tree fn
)
28000 gcc_assert (deduction_guide_p (fn
));
28001 if (!DECL_ARTIFICIAL (fn
))
28003 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28004 return (TREE_CHAIN (parms
) == void_list_node
28005 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28008 /* True if FN is a guide generated from a constructor template. */
28011 template_guide_p (const_tree fn
)
28013 gcc_assert (deduction_guide_p (fn
));
28014 if (!DECL_ARTIFICIAL (fn
))
28016 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28017 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28018 return PRIMARY_TEMPLATE_P (org
);
28022 /* True if FN is an aggregate initialization guide or the copy deduction
28026 builtin_guide_p (const_tree fn
)
28028 if (!deduction_guide_p (fn
))
28030 if (!DECL_ARTIFICIAL (fn
))
28031 /* Explicitly declared. */
28033 if (DECL_ABSTRACT_ORIGIN (fn
))
28034 /* Derived from a constructor. */
28039 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28040 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28041 template parameter types. Note that the handling of template template
28042 parameters relies on current_template_parms being set appropriately for the
28046 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28047 tree tsubst_args
, tsubst_flags_t complain
)
28049 if (olddecl
== error_mark_node
)
28050 return error_mark_node
;
28052 tree oldidx
= get_template_parm_index (olddecl
);
28055 if (TREE_CODE (olddecl
) == TYPE_DECL
28056 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28058 tree oldtype
= TREE_TYPE (olddecl
);
28059 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28060 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28061 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28062 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28063 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28067 newtype
= TREE_TYPE (olddecl
);
28068 if (type_uses_auto (newtype
))
28070 // Substitute once to fix references to other template parameters.
28071 newtype
= tsubst (newtype
, tsubst_args
,
28072 complain
|tf_partial
, NULL_TREE
);
28073 // Now substitute again to reduce the level of the auto.
28074 newtype
= tsubst (newtype
, current_template_args (),
28075 complain
, NULL_TREE
);
28078 newtype
= tsubst (newtype
, tsubst_args
,
28079 complain
, NULL_TREE
);
28083 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28084 DECL_NAME (olddecl
), newtype
);
28085 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28088 if (TREE_CODE (olddecl
) == TYPE_DECL
28089 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28091 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28092 = build_template_parm_index (index
, level
, level
,
28094 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28095 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28096 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28097 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28098 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28100 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28102 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28104 DECL_TEMPLATE_RESULT (newdecl
)
28105 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28106 DECL_NAME (olddecl
), newtype
);
28107 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28108 // First create a copy (ttargs) of tsubst_args with an
28109 // additional level for the template template parameter's own
28110 // template parameters (ttparms).
28111 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28112 (DECL_TEMPLATE_PARMS (olddecl
)));
28113 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28114 tree ttargs
= make_tree_vec (depth
+ 1);
28115 for (int i
= 0; i
< depth
; ++i
)
28116 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28117 TREE_VEC_ELT (ttargs
, depth
)
28118 = template_parms_level_to_args (ttparms
);
28119 // Substitute ttargs into ttparms to fix references to
28120 // other template parameters.
28121 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28122 complain
|tf_partial
);
28123 // Now substitute again with args based on tparms, to reduce
28124 // the level of the ttparms.
28125 ttargs
= current_template_args ();
28126 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28128 // Finally, tack the adjusted parms onto tparms.
28129 ttparms
= tree_cons (size_int (depth
), ttparms
,
28130 current_template_parms
);
28131 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28136 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28138 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28139 TREE_CODE (oldconst
),
28140 DECL_NAME (oldconst
), newtype
);
28141 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28142 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28143 SET_DECL_TEMPLATE_PARM_P (newconst
);
28144 newidx
= build_template_parm_index (index
, level
, level
,
28145 newconst
, newtype
);
28146 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28147 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28148 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28154 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28155 template parameter. */
28158 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28159 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28161 tree olddecl
= TREE_VALUE (oldelt
);
28162 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28164 if (newdecl
== error_mark_node
)
28165 return error_mark_node
;
28166 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28167 targs
, complain
, NULL_TREE
);
28168 tree list
= build_tree_list (newdef
, newdecl
);
28169 TEMPLATE_PARM_CONSTRAINTS (list
)
28170 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28171 targs
, complain
, NULL_TREE
);
28172 int depth
= TMPL_ARGS_DEPTH (targs
);
28173 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28177 /* Returns a C++17 class deduction guide template based on the constructor
28178 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28179 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28180 aggregate initialization guide. */
28183 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28185 tree tparms
, targs
, fparms
, fargs
, ci
;
28186 bool memtmpl
= false;
28189 tree fn_tmpl
= NULL_TREE
;
28193 ++processing_template_decl
;
28194 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28195 --processing_template_decl
;
28198 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28202 bool copy_p
= TYPE_REF_P (ctor
);
28204 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28206 fparms
= void_list_node
;
28208 else if (TREE_CODE (ctor
) == TREE_LIST
)
28211 gcc_unreachable ();
28213 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28214 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28215 targs
= CLASSTYPE_TI_ARGS (type
);
28218 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28219 explicit_p
= false;
28223 ++processing_template_decl
;
28227 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28228 : DECL_TI_TEMPLATE (ctor
));
28230 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28231 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28233 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28234 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28235 fully specialized args for the enclosing class. Strip those off, as
28236 the deduction guide won't have those template parameters. */
28237 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28238 TMPL_PARMS_DEPTH (tparms
));
28239 /* Discard the 'this' parameter. */
28240 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28241 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28242 ci
= get_constraints (ctor
);
28243 loc
= DECL_SOURCE_LOCATION (ctor
);
28244 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28246 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28250 /* For a member template constructor, we need to flatten the two
28251 template parameter lists into one, and then adjust the function
28252 signature accordingly. This gets...complicated. */
28253 tree save_parms
= current_template_parms
;
28255 /* For a member template we should have two levels of parms/args, one
28256 for the class and one for the constructor. We stripped
28257 specialized args for further enclosing classes above. */
28258 const int depth
= 2;
28259 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28261 /* Template args for translating references to the two-level template
28262 parameters into references to the one-level template parameters we
28264 tree tsubst_args
= copy_node (targs
);
28265 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28266 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28268 /* Template parms for the constructor template. */
28269 tree ftparms
= TREE_VALUE (tparms
);
28270 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28271 /* Template parms for the class template. */
28272 tparms
= TREE_CHAIN (tparms
);
28273 tree ctparms
= TREE_VALUE (tparms
);
28274 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28275 /* Template parms for the deduction guide start as a copy of the
28276 template parms for the class. We set current_template_parms for
28277 lookup_template_class_1. */
28278 current_template_parms
= tparms
= copy_node (tparms
);
28279 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28280 for (unsigned i
= 0; i
< clen
; ++i
)
28281 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28283 /* Now we need to rewrite the constructor parms to append them to the
28285 for (unsigned i
= 0; i
< flen
; ++i
)
28287 unsigned index
= i
+ clen
;
28288 unsigned level
= 1;
28289 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28291 = rewrite_tparm_list (oldelt
, index
, level
,
28292 tsubst_args
, i
, complain
);
28293 if (newelt
== error_mark_node
)
28295 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28298 /* Now we have a final set of template parms to substitute into the
28299 function signature. */
28300 targs
= template_parms_to_args (tparms
);
28301 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28303 if (fparms
== error_mark_node
)
28306 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28308 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28309 cp_unevaluated_operand. */
28311 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28312 current_template_parms
= save_parms
;
28315 --processing_template_decl
;
28317 return error_mark_node
;
28322 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28323 tparms
= copy_node (tparms
);
28324 INNERMOST_TEMPLATE_PARMS (tparms
)
28325 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28328 tree fntype
= build_function_type (type
, fparms
);
28329 tree ded_fn
= build_lang_decl_loc (loc
,
28331 dguide_name (type
), fntype
);
28332 DECL_ARGUMENTS (ded_fn
) = fargs
;
28333 DECL_ARTIFICIAL (ded_fn
) = true;
28334 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28335 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28336 DECL_ARTIFICIAL (ded_tmpl
) = true;
28337 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28338 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28340 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28342 set_constraints (ded_tmpl
, ci
);
28347 /* Add to LIST the member types for the reshaped initializer CTOR. */
28350 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28352 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28353 tree idx
, val
; unsigned i
;
28354 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28356 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28357 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28358 && CONSTRUCTOR_NELTS (val
)
28359 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28360 type gets a single initializer. */
28361 && CP_AGGREGATE_TYPE_P (ftype
)
28362 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28363 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28365 tree subelt
= NULL_TREE
;
28366 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28367 subelt
= TREE_TYPE (ftype
);
28368 list
= collect_ctor_idx_types (val
, list
, subelt
);
28371 tree arg
= NULL_TREE
;
28372 if (i
== v
->length() - 1
28373 && PACK_EXPANSION_P (ftype
))
28374 /* Give the trailing pack expansion parameter a default argument to
28375 match aggregate initialization behavior, even if we deduce the
28376 length of the pack separately to more than we have initializers. */
28377 arg
= build_constructor (init_list_type_node
, NULL
);
28378 /* if ei is of array type and xi is a braced-init-list or string literal,
28379 Ti is an rvalue reference to the declared type of ei */
28380 STRIP_ANY_LOCATION_WRAPPER (val
);
28381 if (TREE_CODE (ftype
) == ARRAY_TYPE
28382 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28383 || TREE_CODE (val
) == STRING_CST
))
28385 if (TREE_CODE (val
) == STRING_CST
)
28386 ftype
= cp_build_qualified_type
28387 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28388 ftype
= (cp_build_reference_type
28389 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28391 list
= tree_cons (arg
, ftype
, list
);
28397 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28400 is_spec_or_derived (tree etype
, tree tmpl
)
28402 if (!etype
|| !CLASS_TYPE_P (etype
))
28405 tree type
= TREE_TYPE (tmpl
);
28406 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28407 (DECL_TEMPLATE_PARMS (tmpl
)));
28408 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28409 int err
= unify (tparms
, targs
, type
, etype
,
28410 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28415 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28419 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28421 if (cxx_dialect
< cxx20
)
28424 if (init
== NULL_TREE
)
28427 tree type
= TREE_TYPE (tmpl
);
28428 if (!CP_AGGREGATE_TYPE_P (type
))
28431 /* No aggregate candidate for copy-initialization. */
28432 if (args
->length() == 1)
28434 tree val
= (*args
)[0];
28435 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28439 /* If we encounter a problem, we just won't add the candidate. */
28440 tsubst_flags_t complain
= tf_none
;
28442 tree parms
= NULL_TREE
;
28443 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28445 init
= reshape_init (type
, init
, complain
);
28446 if (init
== error_mark_node
)
28448 parms
= collect_ctor_idx_types (init
, parms
);
28450 else if (TREE_CODE (init
) == TREE_LIST
)
28452 int len
= list_length (init
);
28453 for (tree field
= TYPE_FIELDS (type
);
28455 --len
, field
= DECL_CHAIN (field
))
28457 field
= next_initializable_field (field
);
28460 tree ftype
= finish_decltype_type (field
, true, complain
);
28461 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28465 /* Aggregate initialization doesn't apply to an initializer expression. */
28471 parms
= nreverse (parms
);
28472 TREE_CHAIN (last
) = void_list_node
;
28473 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28480 /* UGUIDES are the deduction guides for the underlying template of alias
28481 template TMPL; adjust them to be deduction guides for TMPL. */
28484 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28486 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28487 class type (9.2.8.2) where the template-name names an alias template A,
28488 the defining-type-id of A must be of the form
28490 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28492 as specified in 9.2.8.2. The guides of A are the set of functions or
28493 function templates formed as follows. For each function or function
28494 template f in the guides of the template named by the simple-template-id
28495 of the defining-type-id, the template arguments of the return type of f
28496 are deduced from the defining-type-id of A according to the process in
28497 13.10.2.5 with the exception that deduction does not fail if not all
28498 template arguments are deduced. Let g denote the result of substituting
28499 these deductions into f. If substitution succeeds, form a function or
28500 function template f' with the following properties and add it to the set
28503 * The function type of f' is the function type of g.
28505 * If f is a function template, f' is a function template whose template
28506 parameter list consists of all the template parameters of A (including
28507 their default template arguments) that appear in the above deductions or
28508 (recursively) in their default template arguments, followed by the
28509 template parameters of f that were not deduced (including their default
28510 template arguments), otherwise f' is not a function template.
28512 * The associated constraints (13.5.2) are the conjunction of the
28513 associated constraints of g and a constraint that is satisfied if and only
28514 if the arguments of A are deducible (see below) from the return type.
28516 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28519 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28520 considered to be so as well.
28522 * The explicit-specifier of f' is the explicit-specifier of g (if
28525 /* This implementation differs from the above in two significant ways:
28527 1) We include all template parameters of A, not just some.
28528 2) The added constraint is same_type instead of deducible.
28530 I believe that while it's probably possible to construct a testcase that
28531 behaves differently with this simplification, it should have the same
28532 effect for real uses. Including all template parameters means that we
28533 deduce all parameters of A when resolving the call, so when we're in the
28534 constraint we don't need to deduce them again, we can just check whether
28535 the deduction produced the desired result. */
28537 tsubst_flags_t complain
= tf_warning_or_error
;
28538 tree atype
= TREE_TYPE (tmpl
);
28539 tree aguides
= NULL_TREE
;
28540 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28541 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28542 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28543 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28547 location_t loc
= DECL_SOURCE_LOCATION (f
);
28548 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28550 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28552 processing_template_decl_sentinel
ptds (/*reset*/false);
28553 ++processing_template_decl
;
28555 /* Deduce template arguments for f from the type-id of A. */
28556 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28557 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28558 tree targs
= make_tree_vec (len
);
28559 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28562 /* The number of parms for f' is the number of parms for A plus
28563 non-deduced parms of f. */
28564 unsigned ndlen
= 0;
28566 for (unsigned i
= 0; i
< len
; ++i
)
28567 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28569 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28571 /* First copy over the parms of A. */
28572 for (j
= 0; j
< natparms
; ++j
)
28573 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28574 /* Now rewrite the non-deduced parms of f. */
28575 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28576 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28579 unsigned index
= j
++;
28580 unsigned level
= 1;
28581 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28582 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28583 targs
, i
, complain
);
28584 TREE_VEC_ELT (gtparms
, index
) = list
;
28586 gtparms
= build_tree_list (size_one_node
, gtparms
);
28588 /* Substitute the deduced arguments plus the rewritten template
28589 parameters into f to get g. This covers the type, copyness,
28590 guideness, and explicit-specifier. */
28591 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28592 if (g
== error_mark_node
)
28593 return error_mark_node
;
28594 DECL_USE_TEMPLATE (g
) = 0;
28595 fprime
= build_template_decl (g
, gtparms
, false);
28596 DECL_TEMPLATE_RESULT (fprime
) = g
;
28597 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28598 tree gtargs
= template_parms_to_args (gtparms
);
28599 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28600 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28602 /* Substitute the associated constraints. */
28603 tree ci
= get_constraints (f
);
28605 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28606 if (ci
== error_mark_node
)
28607 return error_mark_node
;
28609 /* Add a constraint that the return type matches the instantiation of
28610 A with the same template arguments. */
28611 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28612 if (!same_type_p (atype
, ret
)
28613 /* FIXME this should mean they don't compare as equivalent. */
28614 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28616 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28617 ci
= append_constraint (ci
, same
);
28621 set_constraints (fprime
, ci
);
28625 /* For a non-template deduction guide, if the arguments of A aren't
28626 deducible from the return type, don't add the candidate. */
28627 tree targs
= make_tree_vec (natparms
);
28628 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28629 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28630 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28636 aguides
= lookup_add (fprime
, aguides
);
28642 /* Return artificial deduction guides built from the constructors of class
28646 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28648 tree type
= TREE_TYPE (tmpl
);
28649 tree outer_args
= NULL_TREE
;
28650 if (DECL_CLASS_SCOPE_P (tmpl
)
28651 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28653 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28654 type
= TREE_TYPE (most_general_template (tmpl
));
28657 tree cands
= NULL_TREE
;
28659 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28661 /* Skip inherited constructors. */
28662 if (iter
.using_p ())
28665 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28666 cands
= lookup_add (guide
, cands
);
28669 /* Add implicit default constructor deduction guide. */
28670 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28672 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28674 cands
= lookup_add (guide
, cands
);
28677 /* Add copy guide. */
28679 tree gtype
= build_reference_type (type
);
28680 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28682 cands
= lookup_add (guide
, cands
);
28688 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28690 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28691 aggregate candidate is added separately because it depends on the
28695 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28697 tree guides
= NULL_TREE
;
28698 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28700 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28701 tree tinfo
= get_template_info (under
);
28702 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28706 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28707 dguide_name (tmpl
),
28708 /*type*/false, /*complain*/false,
28710 if (guides
== error_mark_node
)
28711 guides
= NULL_TREE
;
28714 /* Cache the deduction guides for a template. We also remember the result of
28715 lookup, and rebuild everything if it changes; should be very rare. */
28716 tree_pair_p cache
= NULL
;
28718 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28721 if (cache
->purpose
== guides
)
28722 return cache
->value
;
28726 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28727 cache
->purpose
= guides
;
28730 tree cands
= NULL_TREE
;
28731 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28732 cands
= alias_ctad_tweaks (tmpl
, guides
);
28735 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28736 for (ovl_iterator
it (guides
); it
; ++it
)
28737 cands
= lookup_add (*it
, cands
);
28740 cache
->value
= cands
;
28744 /* Return whether TMPL is a (class template argument-) deducible template. */
28747 ctad_template_p (tree tmpl
)
28749 /* A deducible template is either a class template or is an alias template
28750 whose defining-type-id is of the form
28752 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28754 where the nested-name-specifier (if any) is non-dependent and the
28755 template-name of the simple-template-id names a deducible template. */
28757 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28758 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28760 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28762 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28763 if (tree tinfo
= get_template_info (orig
))
28764 return ctad_template_p (TI_TEMPLATE (tinfo
));
28768 /* Deduce template arguments for the class template placeholder PTYPE for
28769 template TMPL based on the initializer INIT, and return the resulting
28773 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28774 int flags
, tsubst_flags_t complain
)
28776 /* We should have handled this in the caller. */
28777 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28780 /* Look through alias templates that just rename another template. */
28781 tmpl
= get_underlying_template (tmpl
);
28782 if (!ctad_template_p (tmpl
))
28784 if (complain
& tf_error
)
28785 error ("non-deducible template %qT used without template arguments", tmpl
);
28786 return error_mark_node
;
28788 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28790 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28791 if (complain
& tf_warning_or_error
)
28792 pedwarn (input_location
, 0, "alias template deduction only available "
28793 "with %<-std=c++20%> or %<-std=gnu++20%>");
28796 if (init
&& TREE_TYPE (init
) == ptype
)
28797 /* Using the template parm as its own argument. */
28800 tree type
= TREE_TYPE (tmpl
);
28802 bool try_list_ctor
= false;
28804 releasing_vec rv_args
= NULL
;
28805 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28806 if (init
== NULL_TREE
)
28807 args
= make_tree_vector ();
28808 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28810 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28811 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28813 /* As an exception, the first phase in 16.3.1.7 (considering the
28814 initializer list as a single argument) is omitted if the
28815 initializer list consists of a single expression of type cv U,
28816 where U is a specialization of C or a class derived from a
28817 specialization of C. */
28818 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28819 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28820 try_list_ctor
= false;
28822 if (try_list_ctor
|| is_std_init_list (type
))
28823 args
= make_tree_vector_single (init
);
28825 args
= make_tree_vector_from_ctor (init
);
28827 else if (TREE_CODE (init
) == TREE_LIST
)
28828 args
= make_tree_vector_from_list (init
);
28830 args
= make_tree_vector_single (init
);
28832 /* Do this now to avoid problems with erroneous args later on. */
28833 args
= resolve_args (args
, complain
);
28835 return error_mark_node
;
28837 tree cands
= deduction_guides_for (tmpl
, complain
);
28838 if (cands
== error_mark_node
)
28839 return error_mark_node
;
28841 /* Prune explicit deduction guides in copy-initialization context. */
28842 bool elided
= false;
28843 if (flags
& LOOKUP_ONLYCONVERTING
)
28845 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28846 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28851 /* Found a nonconverting guide, prune the candidates. */
28852 tree pruned
= NULL_TREE
;
28853 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28854 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28855 pruned
= lookup_add (*iter
, pruned
);
28861 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
28862 cands
= lookup_add (guide
, cands
);
28864 tree call
= error_mark_node
;
28866 /* If this is list-initialization and the class has a list constructor, first
28867 try deducing from the list as a single argument, as [over.match.list]. */
28868 tree list_cands
= NULL_TREE
;
28869 if (try_list_ctor
&& cands
)
28870 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28873 if (is_list_ctor (dg
))
28874 list_cands
= lookup_add (dg
, list_cands
);
28878 ++cp_unevaluated_operand
;
28879 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28880 --cp_unevaluated_operand
;
28882 if (call
== error_mark_node
)
28884 /* That didn't work, now try treating the list as a sequence of
28886 release_tree_vector (args
);
28887 args
= make_tree_vector_from_ctor (init
);
28891 if (elided
&& !cands
)
28893 error ("cannot deduce template arguments for copy-initialization"
28894 " of %qT, as it has no non-explicit deduction guides or "
28895 "user-declared constructors", type
);
28896 return error_mark_node
;
28898 else if (!cands
&& call
== error_mark_node
)
28900 error ("cannot deduce template arguments of %qT, as it has no viable "
28901 "deduction guides", type
);
28902 return error_mark_node
;
28905 if (call
== error_mark_node
)
28907 ++cp_unevaluated_operand
;
28908 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28909 --cp_unevaluated_operand
;
28912 if (call
== error_mark_node
28913 && (complain
& tf_warning_or_error
))
28915 error ("class template argument deduction failed:");
28917 ++cp_unevaluated_operand
;
28918 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28919 --cp_unevaluated_operand
;
28922 inform (input_location
, "explicit deduction guides not considered "
28923 "for copy-initialization");
28926 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28929 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28930 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28931 The CONTEXT determines the context in which auto deduction is performed
28932 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28933 OUTER_TARGS are used during template argument deduction
28934 (context == adc_unify) to properly substitute the result, and is ignored
28937 For partial-concept-ids, extra args may be appended to the list of deduced
28938 template arguments prior to determining constraint satisfaction. */
28941 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28942 tsubst_flags_t complain
, auto_deduction_context context
,
28943 tree outer_targs
, int flags
)
28947 if (init
== error_mark_node
)
28948 return error_mark_node
;
28950 if (init
&& type_dependent_expression_p (init
)
28951 && context
!= adc_unify
)
28952 /* Defining a subset of type-dependent expressions that we can deduce
28953 from ahead of time isn't worth the trouble. */
28956 /* Similarly, we can't deduce from another undeduced decl. */
28957 if (init
&& undeduced_auto_decl (init
))
28960 /* We may be doing a partial substitution, but we still want to replace
28962 complain
&= ~tf_partial
;
28964 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28965 /* C++17 class template argument deduction. */
28966 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28968 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28969 /* Nothing we can do with this, even in deduction context. */
28972 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28973 with either a new invented type template parameter U or, if the
28974 initializer is a braced-init-list (8.5.4), with
28975 std::initializer_list<U>. */
28976 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28978 if (!DIRECT_LIST_INIT_P (init
))
28979 type
= listify_autos (type
, auto_node
);
28980 else if (CONSTRUCTOR_NELTS (init
) == 1)
28981 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28984 if (complain
& tf_warning_or_error
)
28986 if (permerror (input_location
, "direct-list-initialization of "
28987 "%<auto%> requires exactly one element"))
28988 inform (input_location
,
28989 "for deduction to %<std::initializer_list%>, use copy-"
28990 "list-initialization (i.e. add %<=%> before the %<{%>)");
28992 type
= listify_autos (type
, auto_node
);
28996 if (type
== error_mark_node
)
28997 return error_mark_node
;
28999 init
= resolve_nondeduced_context (init
, complain
);
29001 if (context
== adc_decomp_type
29002 && auto_node
== type
29003 && init
!= error_mark_node
29004 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29005 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29006 and initializer has array type, deduce cv-qualified array type. */
29007 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29009 else if (AUTO_IS_DECLTYPE (auto_node
))
29011 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29012 bool id
= (DECL_P (stripped_init
)
29013 || ((TREE_CODE (init
) == COMPONENT_REF
29014 || TREE_CODE (init
) == SCOPE_REF
)
29015 && !REF_PARENTHESIZED_P (init
)));
29016 targs
= make_tree_vec (1);
29017 TREE_VEC_ELT (targs
, 0)
29018 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29019 if (type
!= auto_node
)
29021 if (complain
& tf_error
)
29022 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29023 return error_mark_node
;
29025 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29027 if (complain
& tf_error
)
29028 error ("%<decltype(auto)%> cannot be cv-qualified");
29029 return error_mark_node
;
29034 if (error_operand_p (init
))
29035 return error_mark_node
;
29037 tree parms
= build_tree_list (NULL_TREE
, type
);
29041 tparms
= extract_autos (type
);
29044 tparms
= make_tree_vec (1);
29045 TREE_VEC_ELT (tparms
, 0)
29046 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29049 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29050 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29052 NULL
, /*explain_p=*/false);
29055 if (processing_template_decl
)
29056 /* Try again at instantiation time. */
29058 if (type
&& type
!= error_mark_node
29059 && (complain
& tf_error
))
29060 /* If type is error_mark_node a diagnostic must have been
29061 emitted by now. Also, having a mention to '<type error>'
29062 in the diagnostic is not really useful to the user. */
29065 && FNDECL_USED_AUTO (current_function_decl
)
29067 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29068 && LAMBDA_FUNCTION_P (current_function_decl
))
29069 error ("unable to deduce lambda return type from %qE", init
);
29071 error ("unable to deduce %qT from %qE", type
, init
);
29072 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29074 NULL
, /*explain_p=*/true);
29076 return error_mark_node
;
29080 /* Check any placeholder constraints against the deduced type. */
29081 if (flag_concepts
&& !processing_template_decl
)
29082 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29084 /* Use the deduced type to check the associated constraints. If we
29085 have a partial-concept-id, rebuild the argument list so that
29086 we check using the extra arguments. */
29087 check
= unpack_concept_check (check
);
29088 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29089 tree
cdecl = TREE_OPERAND (check
, 0);
29091 cdecl = OVL_FIRST (cdecl);
29092 tree cargs
= TREE_OPERAND (check
, 1);
29093 if (TREE_VEC_LENGTH (cargs
) > 1)
29095 cargs
= copy_node (cargs
);
29096 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29101 /* Rebuild the check using the deduced arguments. */
29102 check
= build_concept_check (cdecl, cargs
, tf_none
);
29104 if (!constraints_satisfied_p (check
))
29106 if (complain
& tf_warning_or_error
)
29108 auto_diagnostic_group d
;
29111 case adc_unspecified
:
29113 error("placeholder constraints not satisfied");
29115 case adc_variable_type
:
29116 case adc_decomp_type
:
29117 error ("deduced initializer does not satisfy "
29118 "placeholder constraints");
29120 case adc_return_type
:
29121 error ("deduced return type does not satisfy "
29122 "placeholder constraints");
29124 case adc_requirement
:
29125 error ("deduced expression type does not satisfy "
29126 "placeholder constraints");
29129 diagnose_constraints (input_location
, check
, targs
);
29131 return error_mark_node
;
29135 if (processing_template_decl
&& context
!= adc_unify
)
29136 outer_targs
= current_template_args ();
29137 targs
= add_to_template_args (outer_targs
, targs
);
29138 return tsubst (type
, targs
, complain
, NULL_TREE
);
29141 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29145 splice_late_return_type (tree type
, tree late_return_type
)
29147 if (late_return_type
)
29149 gcc_assert (is_auto (type
) || seen_error ());
29150 return late_return_type
;
29153 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29155 tree idx
= get_template_parm_index (*auto_node
);
29156 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29158 /* In an abbreviated function template we didn't know we were dealing
29159 with a function template when we saw the auto return type, so update
29160 it to have the correct level. */
29161 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29162 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29163 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29164 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29165 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29166 *auto_node
= new_auto
;
29172 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29173 'decltype(auto)' or a deduced class template. */
29176 is_auto (const_tree type
)
29178 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29179 && (TYPE_IDENTIFIER (type
) == auto_identifier
29180 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29186 /* for_each_template_parm callback for type_uses_auto. */
29189 is_auto_r (tree tp
, void */
*data*/
)
29191 return is_auto (tp
);
29194 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29195 a use of `auto'. Returns NULL_TREE otherwise. */
29198 type_uses_auto (tree type
)
29200 if (type
== NULL_TREE
)
29202 else if (flag_concepts
)
29204 /* The Concepts TS allows multiple autos in one type-specifier; just
29205 return the first one we find, do_auto_deduction will collect all of
29207 if (uses_template_parms (type
))
29208 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29209 /*visited*/NULL
, /*nondeduced*/false);
29213 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29219 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29220 concepts are enabled, auto is acceptable in template arguments, but
29221 only when TEMPL identifies a template class. Return TRUE if any
29222 such errors were reported. */
29225 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29227 /* If there were previous errors, nevermind. */
29228 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29231 /* If TMPL is an identifier, we're parsing and we can't tell yet
29232 whether TMPL is supposed to be a type, a function or a variable.
29233 We'll only be able to tell during template substitution, so we
29234 expect to be called again then. If concepts are enabled and we
29235 know we have a type, we're ok. */
29237 && (identifier_p (tmpl
)
29239 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29240 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29243 /* Quickly search for any occurrences of auto; usually there won't
29244 be any, and then we'll avoid allocating the vector. */
29245 if (!type_uses_auto (args
))
29248 bool errors
= false;
29250 tree vec
= extract_autos (args
);
29251 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29253 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29254 error_at (DECL_SOURCE_LOCATION (xauto
),
29255 "invalid use of %qT in template argument", xauto
);
29262 /* Recursively walk over && expressions searching for EXPR. Return a reference
29263 to that expression. */
29265 static tree
*find_template_requirement (tree
*t
, tree key
)
29269 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29271 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29273 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29279 /* Convert the generic type parameters in PARM that match the types given in the
29280 range [START_IDX, END_IDX) from the current_template_parms into generic type
29284 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29286 tree current
= current_template_parms
;
29287 int depth
= TMPL_PARMS_DEPTH (current
);
29288 current
= INNERMOST_TEMPLATE_PARMS (current
);
29289 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29291 for (int i
= 0; i
< start_idx
; ++i
)
29292 TREE_VEC_ELT (replacement
, i
)
29293 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29295 for (int i
= start_idx
; i
< end_idx
; ++i
)
29297 /* Create a distinct parameter pack type from the current parm and add it
29298 to the replacement args to tsubst below into the generic function
29300 tree node
= TREE_VEC_ELT (current
, i
);
29301 tree o
= TREE_TYPE (TREE_VALUE (node
));
29302 tree t
= copy_type (o
);
29303 TEMPLATE_TYPE_PARM_INDEX (t
)
29304 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29306 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29307 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29308 TYPE_MAIN_VARIANT (t
) = t
;
29309 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29310 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29311 TREE_VEC_ELT (replacement
, i
) = t
;
29313 /* Replace the current template parameter with new pack. */
29314 TREE_VALUE (node
) = TREE_CHAIN (t
);
29316 /* Surgically adjust the associated constraint of adjusted parameter
29317 and it's corresponding contribution to the current template
29319 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29321 tree id
= unpack_concept_check (constr
);
29322 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
29323 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29324 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29326 /* If there was a constraint, we also need to replace that in
29327 the template requirements, which we've already built. */
29328 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29329 reqs
= find_template_requirement (reqs
, constr
);
29334 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29335 TREE_VEC_ELT (replacement
, i
)
29336 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29338 /* If there are more levels then build up the replacement with the outer
29341 replacement
= add_to_template_args (template_parms_to_args
29342 (TREE_CHAIN (current_template_parms
)),
29345 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29348 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29352 declare_integer_pack (void)
29354 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29355 build_function_type_list (integer_type_node
,
29358 NULL_TREE
, ECF_CONST
);
29359 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29360 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29361 CP_BUILT_IN_INTEGER_PACK
);
29364 /* Set up the hash tables for template instantiations. */
29367 init_template_processing (void)
29369 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29370 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29372 if (cxx_dialect
>= cxx11
)
29373 declare_integer_pack ();
29376 /* Print stats about the template hash tables for -fstats. */
29379 print_template_statistics (void)
29381 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29382 "%f collisions\n", (long) decl_specializations
->size (),
29383 (long) decl_specializations
->elements (),
29384 decl_specializations
->collisions ());
29385 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29386 "%f collisions\n", (long) type_specializations
->size (),
29387 (long) type_specializations
->elements (),
29388 type_specializations
->collisions ());
29393 namespace selftest
{
29395 /* Verify that build_non_dependent_expr () works, for various expressions,
29396 and that location wrappers don't affect the results. */
29399 test_build_non_dependent_expr ()
29401 location_t loc
= BUILTINS_LOCATION
;
29403 /* Verify constants, without and with location wrappers. */
29404 tree int_cst
= build_int_cst (integer_type_node
, 42);
29405 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29407 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29408 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29409 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29411 tree string_lit
= build_string (4, "foo");
29412 TREE_TYPE (string_lit
) = char_array_type_node
;
29413 string_lit
= fix_string_type (string_lit
);
29414 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29416 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29417 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29418 ASSERT_EQ (wrapped_string_lit
,
29419 build_non_dependent_expr (wrapped_string_lit
));
29422 /* Verify that type_dependent_expression_p () works correctly, even
29423 in the presence of location wrapper nodes. */
29426 test_type_dependent_expression_p ()
29428 location_t loc
= BUILTINS_LOCATION
;
29430 tree name
= get_identifier ("foo");
29432 /* If no templates are involved, nothing is type-dependent. */
29433 gcc_assert (!processing_template_decl
);
29434 ASSERT_FALSE (type_dependent_expression_p (name
));
29436 ++processing_template_decl
;
29438 /* Within a template, an unresolved name is always type-dependent. */
29439 ASSERT_TRUE (type_dependent_expression_p (name
));
29441 /* Ensure it copes with NULL_TREE and errors. */
29442 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29443 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29445 /* A USING_DECL in a template should be type-dependent, even if wrapped
29446 with a location wrapper (PR c++/83799). */
29447 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29448 TREE_TYPE (using_decl
) = integer_type_node
;
29449 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29450 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29451 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29452 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29454 --processing_template_decl
;
29457 /* Run all of the selftests within this file. */
29462 test_build_non_dependent_expr ();
29463 test_type_dependent_expression_p ();
29466 } // namespace selftest
29468 #endif /* #if CHECKING_P */
29470 #include "gt-cp-pt.h"