1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2023 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".
27 Fixed by: C++20 modules. */
30 #define INCLUDE_ALGORITHM // for std::equal
32 #include "coretypes.h"
35 #include "stringpool.h"
38 #include "stor-layout.h"
40 #include "c-family/c-objc.h"
41 #include "cp-objcp-common.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
46 #include "gcc-rich-location.h"
50 /* The type of functions taking a tree, and some additional data, and
52 typedef int (*tree_fn_t
) (tree
, void*);
54 /* The PENDING_TEMPLATES is a list of templates whose instantiations
55 have been deferred, either because their definitions were not yet
56 available, or because we were putting off doing the work. */
57 struct GTY ((chain_next ("%h.next"))) pending_template
59 struct pending_template
*next
;
60 struct tinst_level
*tinst
;
63 static GTY(()) struct pending_template
*pending_templates
;
64 static GTY(()) struct pending_template
*last_pending_template
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static vec
<int> inline_parm_levels
;
71 static GTY(()) struct tinst_level
*current_tinst_level
;
73 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr
;
80 // -------------------------------------------------------------------------- //
81 // Local Specialization Stack
83 // Implementation of the RAII helper for creating new local
85 local_specialization_stack::local_specialization_stack (lss_policy policy
)
86 : saved (local_specializations
)
88 if (policy
== lss_nop
)
90 else if (policy
== lss_blank
|| !saved
)
91 local_specializations
= new hash_map
<tree
, tree
>;
93 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
96 local_specialization_stack::~local_specialization_stack ()
98 if (local_specializations
!= saved
)
100 delete local_specializations
;
101 local_specializations
= saved
;
105 /* True if we've recursed into fn_type_unification too many times. */
106 static bool excessive_deduction_depth
;
108 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
110 static hashval_t
hash (tree
, tree
);
111 static hashval_t
hash (spec_entry
*);
112 static bool equal (spec_entry
*, spec_entry
*);
115 /* The general template is not in these tables. */
116 typedef hash_table
<spec_hasher
> spec_hash_table
;
117 static GTY (()) spec_hash_table
*decl_specializations
;
118 static GTY (()) spec_hash_table
*type_specializations
;
120 /* Contains canonical template parameter types. The vector is indexed by
121 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
122 TREE_LIST, whose TREE_VALUEs contain the canonical template
123 parameters of various types and levels. */
124 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
126 #define UNIFY_ALLOW_NONE 0
127 #define UNIFY_ALLOW_MORE_CV_QUAL 1
128 #define UNIFY_ALLOW_LESS_CV_QUAL 2
129 #define UNIFY_ALLOW_DERIVED 4
130 #define UNIFY_ALLOW_INTEGER 8
131 #define UNIFY_ALLOW_OUTER_LEVEL 16
132 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
133 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
135 enum template_base_result
{
137 tbr_ambiguous_baseclass
,
141 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
142 unification_kind_t
, int,
144 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
145 unification_kind_t
, int, bool, bool);
146 static int unify (tree
, tree
, tree
, tree
, int, bool);
147 static void add_pending_template (tree
);
148 static tree
reopen_tinst_level (struct tinst_level
*);
149 static tree
tsubst_initializer_list (tree
, tree
);
150 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
151 static void tsubst_enum (tree
, tree
, tree
);
152 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
153 static int check_non_deducible_conversion (tree
, tree
, unification_kind_t
, int,
154 struct conversion
**, bool);
155 static int maybe_adjust_types_for_deduction (tree
, unification_kind_t
,
157 static int type_unification_real (tree
, tree
, tree
, const tree
*,
158 unsigned int, int, unification_kind_t
,
159 vec
<deferred_access_check
, va_gc
> **,
161 static void note_template_header (int);
162 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
163 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
164 static tree
convert_template_argument (tree
, tree
, tree
,
165 tsubst_flags_t
, int, tree
);
166 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
167 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
168 static tree
expand_template_argument_pack (tree
);
169 static tree
build_template_parm_index (int, int, int, tree
, tree
);
170 static bool inline_needs_template_parms (tree
, bool);
171 static void push_inline_template_parms_recursive (tree
, int);
172 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
173 static int mark_template_parm (tree
, void *);
174 static int template_parm_this_level_p (tree
, void *);
175 static tree
tsubst_friend_function (tree
, tree
);
176 static tree
tsubst_friend_class (tree
, tree
);
177 static int can_complete_type_without_circularity (tree
);
178 static tree
get_bindings (tree
, tree
, tree
, bool);
179 static int template_decl_level (tree
);
180 static int check_cv_quals_for_unify (int, tree
, tree
);
181 static int unify_pack_expansion (tree
, tree
, tree
,
182 tree
, unification_kind_t
, bool, bool);
183 static tree
copy_template_args (tree
);
184 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
185 static void tsubst_each_template_parm_constraints (tree
, tree
, tsubst_flags_t
);
186 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
187 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
188 static tree
tsubst_aggr_type_1 (tree
, tree
, tsubst_flags_t
, tree
, int);
189 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
190 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
191 static bool check_specialization_scope (void);
192 static tree
process_partial_specialization (tree
);
193 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
195 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
196 static bool class_nttp_const_wrapper_p (tree t
);
197 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
199 static bool template_template_parm_bindings_ok_p (tree
, tree
);
200 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
201 static tree
for_each_template_parm_r (tree
*, int *, void *);
202 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
203 static void copy_default_args_to_explicit_spec (tree
);
204 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
205 static bool dependent_template_arg_p (tree
);
206 static bool dependent_type_p_r (tree
);
207 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
208 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
209 static void perform_instantiation_time_access_checks (tree
, tree
);
210 static tree
listify (tree
);
211 static tree
listify_autos (tree
, tree
);
212 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
213 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
214 static bool complex_alias_template_p (const_tree tmpl
);
215 static tree
get_underlying_template (tree
);
216 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
218 static tree
make_argument_pack (tree
);
219 static tree
enclosing_instantiation_of (tree tctx
);
220 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
221 static tree
maybe_dependent_member_ref (tree
, tree
, tsubst_flags_t
, tree
);
223 /* Make the current scope suitable for access checking when we are
224 processing T. T can be FUNCTION_DECL for instantiated function
225 template, VAR_DECL for static member variable, or TYPE_DECL for
226 for a class or alias template (needed by instantiate_decl). */
229 push_access_scope (tree t
)
231 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
232 || TREE_CODE (t
) == TYPE_DECL
);
234 if (DECL_FRIEND_CONTEXT (t
))
235 push_nested_class (DECL_FRIEND_CONTEXT (t
));
236 else if (DECL_IMPLICIT_TYPEDEF_P (t
)
237 && CLASS_TYPE_P (TREE_TYPE (t
)))
238 push_nested_class (TREE_TYPE (t
));
239 else if (DECL_CLASS_SCOPE_P (t
))
240 push_nested_class (DECL_CONTEXT (t
));
241 else if (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
))
242 /* An artificial deduction guide should have the same access as
244 push_nested_class (TREE_TYPE (TREE_TYPE (t
)));
246 push_to_top_level ();
248 if (TREE_CODE (t
) == FUNCTION_DECL
)
250 vec_safe_push (saved_access_scope
, current_function_decl
);
251 current_function_decl
= t
;
255 /* Restore the scope set up by push_access_scope. T is the node we
259 pop_access_scope (tree t
)
261 if (TREE_CODE (t
) == FUNCTION_DECL
)
262 current_function_decl
= saved_access_scope
->pop();
264 if (DECL_FRIEND_CONTEXT (t
)
265 || (DECL_IMPLICIT_TYPEDEF_P (t
)
266 && CLASS_TYPE_P (TREE_TYPE (t
)))
267 || DECL_CLASS_SCOPE_P (t
)
268 || (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
)))
271 pop_from_top_level ();
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
280 finish_member_template_decl (tree decl
)
282 if (decl
== error_mark_node
)
283 return error_mark_node
;
285 gcc_assert (DECL_P (decl
));
287 if (TREE_CODE (decl
) == TYPE_DECL
)
291 type
= TREE_TYPE (decl
);
292 if (type
== error_mark_node
)
293 return error_mark_node
;
294 if (MAYBE_CLASS_TYPE_P (type
)
295 && CLASSTYPE_TEMPLATE_INFO (type
)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
298 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
299 check_member_template (tmpl
);
304 else if (TREE_CODE (decl
) == FIELD_DECL
)
305 error_at (DECL_SOURCE_LOCATION (decl
),
306 "data member %qD cannot be a member template", decl
);
307 else if (DECL_TEMPLATE_INFO (decl
))
309 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
311 check_member_template (DECL_TI_TEMPLATE (decl
));
312 return DECL_TI_TEMPLATE (decl
);
318 error_at (DECL_SOURCE_LOCATION (decl
),
319 "invalid member template declaration %qD", decl
);
321 return error_mark_node
;
324 /* Create a template info node. */
327 build_template_info (tree template_decl
, tree template_args
)
329 tree result
= make_node (TEMPLATE_INFO
);
330 TI_TEMPLATE (result
) = template_decl
;
331 TI_ARGS (result
) = template_args
;
335 /* Return the template info node corresponding to T, whatever T is. */
338 get_template_info (const_tree t
)
340 tree tinfo
= NULL_TREE
;
342 if (!t
|| t
== error_mark_node
)
345 if (TREE_CODE (t
) == NAMESPACE_DECL
346 || TREE_CODE (t
) == PARM_DECL
)
349 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
350 tinfo
= DECL_TEMPLATE_INFO (t
);
352 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
355 if (OVERLOAD_TYPE_P (t
))
356 tinfo
= TYPE_TEMPLATE_INFO (t
);
357 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
358 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
363 /* Returns the template nesting level of the indicated class TYPE.
373 A<T>::B<U> has depth two, while A<T> has depth one.
374 Both A<T>::B<int> and A<int>::B<U> have depth one, if
375 they are instantiations, not specializations.
377 This function is guaranteed to return 0 if passed NULL_TREE so
378 that, for example, `template_class_depth (current_class_type)' is
382 template_class_depth (tree type
)
386 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
388 tree tinfo
= get_template_info (type
);
391 && TREE_CODE (TI_TEMPLATE (tinfo
)) == TEMPLATE_DECL
392 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
393 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
398 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
401 type
= CP_DECL_CONTEXT (type
);
403 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
404 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
406 type
= CP_TYPE_CONTEXT (type
);
412 /* Return TRUE if NODE instantiates a template that has arguments of
413 its own, be it directly a primary template or indirectly through a
414 partial specializations. */
416 instantiates_primary_template_p (tree node
)
418 tree tinfo
= get_template_info (node
);
422 tree tmpl
= TI_TEMPLATE (tinfo
);
423 if (PRIMARY_TEMPLATE_P (tmpl
))
426 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
429 /* So now we know we have a specialization, but it could be a full
430 or a partial specialization. To tell which, compare the depth of
431 its template arguments with those of its context. */
433 tree ctxt
= DECL_CONTEXT (tmpl
);
434 tree ctinfo
= get_template_info (ctxt
);
438 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
439 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
442 /* Subroutine of maybe_begin_member_template_processing.
443 Returns true if processing DECL needs us to push template parms. */
446 inline_needs_template_parms (tree decl
, bool nsdmi
)
448 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
451 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
452 > (current_template_depth
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
455 /* Subroutine of maybe_begin_member_template_processing.
456 Push the template parms in PARMS, starting from LEVELS steps into the
457 chain, and ending at the beginning, since template parms are listed
461 push_inline_template_parms_recursive (tree parmlist
, int levels
)
463 tree parms
= TREE_VALUE (parmlist
);
467 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
469 ++processing_template_decl
;
470 current_template_parms
471 = tree_cons (size_int (current_template_depth
+ 1),
472 parms
, current_template_parms
);
473 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
475 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
477 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
479 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
481 if (error_operand_p (parm
))
484 gcc_assert (DECL_P (parm
));
486 switch (TREE_CODE (parm
))
494 /* Push the CONST_DECL. */
495 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
504 /* Restore the template parameter context for a member template, a
505 friend template defined in a class definition, or a non-template
506 member of template class. */
509 maybe_begin_member_template_processing (tree decl
)
513 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
517 tree ctx
= DECL_CONTEXT (decl
);
518 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
519 /* Disregard full specializations (c++/60999). */
520 && uses_template_parms (ctx
)
521 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
524 if (inline_needs_template_parms (decl
, nsdmi
))
526 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
527 levels
= TMPL_PARMS_DEPTH (parms
) - current_template_depth
;
529 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
532 parms
= TREE_CHAIN (parms
);
535 push_inline_template_parms_recursive (parms
, levels
);
538 /* Remember how many levels of template parameters we pushed so that
539 we can pop them later. */
540 inline_parm_levels
.safe_push (levels
);
543 /* Undo the effects of maybe_begin_member_template_processing. */
546 maybe_end_member_template_processing (void)
551 if (inline_parm_levels
.length () == 0)
554 last
= inline_parm_levels
.pop ();
555 for (i
= 0; i
< last
; ++i
)
557 --processing_template_decl
;
558 current_template_parms
= TREE_CHAIN (current_template_parms
);
563 /* Return a new template argument vector which contains all of ARGS,
564 but has as its innermost set of arguments the EXTRA_ARGS. */
567 add_to_template_args (tree args
, tree extra_args
)
574 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
577 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
578 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
580 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
581 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
583 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
584 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
589 /* Like add_to_template_args, but only the outermost ARGS are added to
590 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
591 (EXTRA_ARGS) levels are added. This function is used to combine
592 the template arguments from a partial instantiation with the
593 template arguments used to attain the full instantiation from the
594 partial instantiation.
596 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
599 add_outermost_template_args (tree args
, tree extra_args
)
605 if (TREE_CODE (args
) == TEMPLATE_DECL
)
607 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
611 /* If there are more levels of EXTRA_ARGS than there are ARGS,
612 something very fishy is going on. */
613 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
615 /* If *all* the new arguments will be the EXTRA_ARGS, just return
617 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
620 /* For the moment, we make ARGS look like it contains fewer levels. */
621 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
623 new_args
= add_to_template_args (args
, extra_args
);
625 /* Now, we restore ARGS to its full dimensions. */
626 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
631 /* Return the N levels of innermost template arguments from the ARGS. */
634 get_innermost_template_args (tree args
, int n
)
642 /* If N is 1, just return the innermost set of template arguments. */
644 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
646 /* If we're not removing anything, just return the arguments we were
648 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
649 gcc_assert (extra_levels
>= 0);
650 if (extra_levels
== 0)
653 /* Make a new set of arguments, not containing the outer arguments. */
654 new_args
= make_tree_vec (n
);
655 for (i
= 1; i
<= n
; ++i
)
656 SET_TMPL_ARGS_LEVEL (new_args
, i
,
657 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
662 /* The inverse of get_innermost_template_args: Return all but the innermost
663 EXTRA_LEVELS levels of template arguments from the ARGS. */
666 strip_innermost_template_args (tree args
, int extra_levels
)
669 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
674 /* If N is 1, just return the outermost set of template arguments. */
676 return TMPL_ARGS_LEVEL (args
, 1);
678 /* If we're not removing anything, just return the arguments we were
680 gcc_assert (extra_levels
>= 0);
681 if (extra_levels
== 0)
684 /* Make a new set of arguments, not containing the inner arguments. */
685 new_args
= make_tree_vec (n
);
686 for (i
= 1; i
<= n
; ++i
)
687 SET_TMPL_ARGS_LEVEL (new_args
, i
,
688 TMPL_ARGS_LEVEL (args
, i
));
693 /* We've got a template header coming up; push to a new level for storing
697 begin_template_parm_list (void)
699 /* We use a non-tag-transparent scope here, which causes pushtag to
700 put tags in this scope, rather than in the enclosing class or
701 namespace scope. This is the right thing, since we want
702 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
703 global template class, push_template_decl handles putting the
704 TEMPLATE_DECL into top-level scope. For a nested template class,
707 template <class T> struct S1 {
708 template <class T> struct S2 {};
711 pushtag contains special code to insert the TEMPLATE_DECL for S2
712 at the right scope. */
713 begin_scope (sk_template_parms
, NULL
);
714 ++processing_template_decl
;
715 ++processing_template_parmlist
;
716 note_template_header (0);
718 /* Add a dummy parameter level while we process the parameter list. */
719 current_template_parms
720 = tree_cons (size_int (current_template_depth
+ 1),
722 current_template_parms
);
725 /* This routine is called when a specialization is declared. If it is
726 invalid to declare a specialization here, an error is reported and
727 false is returned, otherwise this routine will return true. */
730 check_specialization_scope (void)
732 tree scope
= current_scope ();
736 An explicit specialization shall be declared in the namespace of
737 which the template is a member, or, for member templates, in the
738 namespace of which the enclosing class or enclosing class
739 template is a member. An explicit specialization of a member
740 function, member class or static data member of a class template
741 shall be declared in the namespace of which the class template
743 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
745 error ("explicit specialization in non-namespace scope %qD", scope
);
751 In an explicit specialization declaration for a member of a class
752 template or a member template that appears in namespace scope,
753 the member template and some of its enclosing class templates may
754 remain unspecialized, except that the declaration shall not
755 explicitly specialize a class member template if its enclosing
756 class templates are not explicitly specialized as well. */
757 if (current_template_parms
)
759 error ("enclosing class templates are not explicitly specialized");
766 /* We've just seen template <>. */
769 begin_specialization (void)
771 begin_scope (sk_template_spec
, NULL
);
772 note_template_header (1);
773 return check_specialization_scope ();
776 /* Called at then end of processing a declaration preceded by
780 end_specialization (void)
783 reset_specialization ();
786 /* Any template <>'s that we have seen thus far are not referring to a
787 function specialization. */
790 reset_specialization (void)
792 processing_specialization
= 0;
793 template_header_count
= 0;
796 /* We've just seen a template header. If SPECIALIZATION is nonzero,
797 it was of the form template <>. */
800 note_template_header (int specialization
)
802 processing_specialization
= specialization
;
803 template_header_count
++;
806 /* We're beginning an explicit instantiation. */
809 begin_explicit_instantiation (void)
811 gcc_assert (!processing_explicit_instantiation
);
812 processing_explicit_instantiation
= true;
817 end_explicit_instantiation (void)
819 gcc_assert (processing_explicit_instantiation
);
820 processing_explicit_instantiation
= false;
823 /* An explicit specialization or partial specialization of TMPL is being
824 declared. Check that the namespace in which the specialization is
825 occurring is permissible. Returns false iff it is invalid to
826 specialize TMPL in the current namespace. */
829 check_specialization_namespace (tree tmpl
)
831 tree tpl_ns
= decl_namespace_context (tmpl
);
835 An explicit specialization shall be declared in a namespace enclosing the
836 specialized template. An explicit specialization whose declarator-id is
837 not qualified shall be declared in the nearest enclosing namespace of the
838 template, or, if the namespace is inline (7.3.1), any namespace from its
839 enclosing namespace set. */
840 if (current_scope() != DECL_CONTEXT (tmpl
)
841 && !at_namespace_scope_p ())
843 error ("specialization of %qD must appear at namespace scope", tmpl
);
847 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
848 /* Same or enclosing namespace. */
852 auto_diagnostic_group d
;
853 if (permerror (input_location
,
854 "specialization of %qD in different namespace", tmpl
))
855 inform (DECL_SOURCE_LOCATION (tmpl
),
856 " from definition of %q#D", tmpl
);
861 /* SPEC is an explicit instantiation. Check that it is valid to
862 perform this explicit instantiation in the current namespace. */
865 check_explicit_instantiation_namespace (tree spec
)
869 /* DR 275: An explicit instantiation shall appear in an enclosing
870 namespace of its template. */
871 ns
= decl_namespace_context (spec
);
872 if (!is_nested_namespace (current_namespace
, ns
))
873 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
874 "(which does not enclose namespace %qD)",
875 spec
, current_namespace
, ns
);
878 /* Returns true if TYPE is a new partial specialization that needs to be
879 set up. This may also modify TYPE to point to the correct (new or
880 existing) constrained partial specialization. */
883 maybe_new_partial_specialization (tree
& type
)
885 /* An implicit instantiation of an incomplete type implies
886 the definition of a new class template.
894 Here, S<T*> is an implicit instantiation of S whose type
896 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
899 /* It can also be the case that TYPE is a completed specialization.
900 Continuing the previous example, suppose we also declare:
906 Here, S<T*> refers to the specialization S<T*> defined
907 above. However, we need to differentiate definitions because
908 we intend to define a new partial specialization. In this case,
909 we rely on the fact that the constraints are different for
910 this declaration than that above.
912 Note that we also get here for injected class names and
913 late-parsed template definitions. We must ensure that we
914 do not create new type declarations for those cases. */
915 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
917 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
918 tree args
= CLASSTYPE_TI_ARGS (type
);
920 /* If there are no template parameters, this cannot be a new
921 partial template specialization? */
922 if (!current_template_parms
)
925 /* The injected-class-name is not a new partial specialization. */
926 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
929 /* If the constraints are not the same as those of the primary
930 then, we can probably create a new specialization. */
931 tree type_constr
= current_template_constraints ();
933 if (type
== TREE_TYPE (tmpl
))
935 tree main_constr
= get_constraints (tmpl
);
936 if (equivalent_constraints (type_constr
, main_constr
))
940 /* Also, if there's a pre-existing specialization with matching
941 constraints, then this also isn't new. */
942 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
945 tree spec_tmpl
= TREE_VALUE (specs
);
946 tree spec_args
= TREE_PURPOSE (specs
);
947 tree spec_constr
= get_constraints (spec_tmpl
);
948 if (comp_template_args (args
, spec_args
)
949 && equivalent_constraints (type_constr
, spec_constr
))
951 type
= TREE_TYPE (spec_tmpl
);
954 specs
= TREE_CHAIN (specs
);
957 /* Create a new type node (and corresponding type decl)
958 for the newly declared specialization. */
959 tree t
= make_class_type (TREE_CODE (type
));
960 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
961 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
963 /* We only need a separate type node for storing the definition of this
964 partial specialization; uses of S<T*> are unconstrained, so all are
965 equivalent. So keep TYPE_CANONICAL the same. */
966 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
968 /* Build the corresponding type decl. */
969 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
970 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
971 DECL_SOURCE_LOCATION (d
) = input_location
;
972 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
973 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
975 set_instantiating_module (d
);
976 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
985 /* The TYPE is being declared. If it is a template type, that means it
986 is a partial specialization. Do appropriate error-checking. */
989 maybe_process_partial_specialization (tree type
)
993 if (type
== error_mark_node
)
994 return error_mark_node
;
996 /* A lambda that appears in specialization context is not itself a
998 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
1001 /* An injected-class-name is not a specialization. */
1002 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
1005 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1007 error ("name of class shadows template template parameter %qD",
1009 return error_mark_node
;
1012 context
= TYPE_CONTEXT (type
);
1014 if (TYPE_ALIAS_P (type
))
1016 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1018 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1019 error ("specialization of alias template %qD",
1020 TI_TEMPLATE (tinfo
));
1022 error ("explicit specialization of non-template %qT", type
);
1023 return error_mark_node
;
1025 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1027 /* This is for ordinary explicit specialization and partial
1028 specialization of a template class such as:
1030 template <> class C<int>;
1034 template <class T> class C<T*>;
1036 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1038 if (maybe_new_partial_specialization (type
))
1040 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
1041 && !at_namespace_scope_p ())
1042 return error_mark_node
;
1043 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1044 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1045 if (processing_template_decl
)
1047 tree decl
= push_template_decl (TYPE_MAIN_DECL (type
));
1048 if (decl
== error_mark_node
)
1049 return error_mark_node
;
1050 return TREE_TYPE (decl
);
1053 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1054 error ("specialization of %qT after instantiation", type
);
1055 else if (errorcount
&& !processing_specialization
1056 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1057 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1058 /* Trying to define a specialization either without a template<> header
1059 or in an inappropriate place. We've already given an error, so just
1060 bail now so we don't actually define the specialization. */
1061 return error_mark_node
;
1063 else if (CLASS_TYPE_P (type
)
1064 && !CLASSTYPE_USE_TEMPLATE (type
)
1065 && CLASSTYPE_TEMPLATE_INFO (type
)
1066 && context
&& CLASS_TYPE_P (context
)
1067 && CLASSTYPE_TEMPLATE_INFO (context
))
1069 /* This is for an explicit specialization of member class
1070 template according to [temp.expl.spec/18]:
1072 template <> template <class U> class C<int>::D;
1074 The context `C<int>' must be an implicit instantiation.
1075 Otherwise this is just a member class template declared
1078 template <> class C<int> { template <class U> class D; };
1079 template <> template <class U> class C<int>::D;
1081 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1082 while in the second case, `C<int>::D' is a primary template
1083 and `C<T>::D' may not exist. */
1085 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1086 && !COMPLETE_TYPE_P (type
))
1089 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1091 if (current_namespace
1092 != decl_namespace_context (tmpl
))
1094 if (permerror (input_location
,
1095 "specialization of %qD in different namespace",
1097 inform (DECL_SOURCE_LOCATION (tmpl
),
1098 "from definition of %q#D", tmpl
);
1101 /* Check for invalid specialization after instantiation:
1103 template <> template <> class C<int>::D<int>;
1104 template <> template <class U> class C<int>::D; */
1106 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1107 t
; t
= TREE_CHAIN (t
))
1109 tree inst
= TREE_VALUE (t
);
1110 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1111 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1113 /* We already have a full specialization of this partial
1114 instantiation, or a full specialization has been
1115 looked up but not instantiated. Reassign it to the
1116 new member specialization template. */
1120 elt
.tmpl
= most_general_template (tmpl
);
1121 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1124 type_specializations
->remove_elt (&elt
);
1127 CLASSTYPE_TI_ARGS (inst
)
1128 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1131 = type_specializations
->find_slot (&elt
, INSERT
);
1132 entry
= ggc_alloc
<spec_entry
> ();
1137 /* But if we've had an implicit instantiation, that's a
1138 problem ([temp.expl.spec]/6). */
1139 error ("specialization %qT after instantiation %qT",
1143 /* Mark TYPE as a specialization. And as a result, we only
1144 have one level of template argument for the innermost
1146 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1147 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1148 CLASSTYPE_TI_ARGS (type
)
1149 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1152 else if (processing_specialization
)
1154 /* Someday C++0x may allow for enum template specialization. */
1155 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1156 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1157 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1158 "of %qD not allowed by ISO C++", type
);
1161 error ("explicit specialization of non-template %qT", type
);
1162 return error_mark_node
;
1169 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1170 gone through coerce_template_parms by now. */
1173 verify_unstripped_args_1 (tree inner
)
1175 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1177 tree arg
= TREE_VEC_ELT (inner
, i
);
1178 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1180 else if (TYPE_P (arg
))
1181 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1182 else if (ARGUMENT_PACK_P (arg
))
1183 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1184 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1185 /* Allow typedefs on the type of a non-type argument, since a
1186 parameter can have them. */;
1188 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1193 verify_unstripped_args (tree args
)
1195 ++processing_template_decl
;
1196 if (!any_dependent_template_arguments_p (args
))
1197 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1198 --processing_template_decl
;
1201 /* Retrieve the specialization (in the sense of [temp.spec] - a
1202 specialization is either an instantiation or an explicit
1203 specialization) of TMPL for the given template ARGS. If there is
1204 no such specialization, return NULL_TREE. The ARGS are a vector of
1205 arguments, or a vector of vectors of arguments, in the case of
1206 templates with more than one level of parameters.
1208 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1209 then we search for a partial specialization matching ARGS. This
1210 parameter is ignored if TMPL is not a class template.
1212 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1213 result is a NONTYPE_ARGUMENT_PACK. */
1216 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1218 if (tmpl
== NULL_TREE
)
1221 if (args
== error_mark_node
)
1224 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1225 || TREE_CODE (tmpl
) == FIELD_DECL
);
1227 /* There should be as many levels of arguments as there are
1228 levels of parameters. */
1229 gcc_assert (TMPL_ARGS_DEPTH (args
)
1230 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1231 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1232 : template_class_depth (DECL_CONTEXT (tmpl
))));
1235 verify_unstripped_args (args
);
1237 /* Lambda functions in templates aren't instantiated normally, but through
1238 tsubst_lambda_expr. */
1239 if (lambda_fn_in_template_p (tmpl
))
1245 elt
.spec
= NULL_TREE
;
1247 spec_hash_table
*specializations
;
1248 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1249 specializations
= type_specializations
;
1251 specializations
= decl_specializations
;
1254 hash
= spec_hasher::hash (&elt
);
1255 if (spec_entry
*found
= specializations
->find_with_hash (&elt
, hash
))
1261 /* Like retrieve_specialization, but for local declarations. */
1264 retrieve_local_specialization (tree tmpl
)
1266 if (local_specializations
== NULL
)
1269 tree
*slot
= local_specializations
->get (tmpl
);
1270 return slot
? *slot
: NULL_TREE
;
1273 /* Returns nonzero iff DECL is a specialization of TMPL. */
1276 is_specialization_of (tree decl
, tree tmpl
)
1280 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1284 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1290 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1292 for (t
= TREE_TYPE (decl
);
1294 t
= CLASSTYPE_USE_TEMPLATE (t
)
1295 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1296 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1303 /* Returns nonzero iff DECL is a specialization of friend declaration
1304 FRIEND_DECL according to [temp.friend]. */
1307 is_specialization_of_friend (tree decl
, tree friend_decl
)
1309 bool need_template
= true;
1312 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1313 || TREE_CODE (decl
) == TYPE_DECL
);
1315 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1316 of a template class, we want to check if DECL is a specialization
1318 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1319 && DECL_TEMPLATE_INFO (friend_decl
)
1320 && !DECL_USE_TEMPLATE (friend_decl
))
1322 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1323 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1324 need_template
= false;
1326 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1327 && !PRIMARY_TEMPLATE_P (friend_decl
))
1328 need_template
= false;
1330 /* There is nothing to do if this is not a template friend. */
1331 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1334 if (is_specialization_of (decl
, friend_decl
))
1338 A member of a class template may be declared to be a friend of a
1339 non-template class. In this case, the corresponding member of
1340 every specialization of the class template is a friend of the
1341 class granting friendship.
1343 For example, given a template friend declaration
1345 template <class T> friend void A<T>::f();
1347 the member function below is considered a friend
1349 template <> struct A<int> {
1353 For this type of template friend, TEMPLATE_DEPTH below will be
1354 nonzero. To determine if DECL is a friend of FRIEND, we first
1355 check if the enclosing class is a specialization of another. */
1357 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1359 && DECL_CLASS_SCOPE_P (decl
)
1360 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1361 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1363 /* Next, we check the members themselves. In order to handle
1364 a few tricky cases, such as when FRIEND_DECL's are
1366 template <class T> friend void A<T>::g(T t);
1367 template <class T> template <T t> friend void A<T>::h();
1371 void A<int>::g(int);
1372 template <int> void A<int>::h();
1374 we need to figure out ARGS, the template arguments from
1375 the context of DECL. This is required for template substitution
1376 of `T' in the function parameter of `g' and template parameter
1377 of `h' in the above examples. Here ARGS corresponds to `int'. */
1379 tree context
= DECL_CONTEXT (decl
);
1380 tree args
= NULL_TREE
;
1381 int current_depth
= 0;
1383 while (current_depth
< template_depth
)
1385 if (CLASSTYPE_TEMPLATE_INFO (context
))
1387 if (current_depth
== 0)
1388 args
= TYPE_TI_ARGS (context
);
1390 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1393 context
= TYPE_CONTEXT (context
);
1396 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1401 tree friend_args_type
;
1402 tree decl_args_type
;
1404 /* Make sure that both DECL and FRIEND_DECL are templates or
1406 is_template
= DECL_TEMPLATE_INFO (decl
)
1407 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1408 if (need_template
^ is_template
)
1410 else if (is_template
)
1412 /* If both are templates, check template parameter list. */
1414 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1416 if (!comp_template_parms
1417 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1421 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1424 decl_type
= TREE_TYPE (decl
);
1426 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1427 tf_none
, NULL_TREE
);
1428 if (friend_type
== error_mark_node
)
1431 /* Check if return types match. */
1432 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1435 /* Check if function parameter types match, ignoring the
1436 `this' parameter. */
1437 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1438 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1440 friend_args_type
= TREE_CHAIN (friend_args_type
);
1441 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1442 decl_args_type
= TREE_CHAIN (decl_args_type
);
1444 return compparms (decl_args_type
, friend_args_type
);
1448 /* DECL is a TYPE_DECL */
1450 tree decl_type
= TREE_TYPE (decl
);
1452 /* Make sure that both DECL and FRIEND_DECL are templates or
1455 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1456 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1458 if (need_template
^ is_template
)
1460 else if (is_template
)
1463 /* If both are templates, check the name of the two
1464 TEMPLATE_DECL's first because is_friend didn't. */
1465 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1466 != DECL_NAME (friend_decl
))
1469 /* Now check template parameter list. */
1471 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1473 return comp_template_parms
1474 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1478 return (DECL_NAME (decl
)
1479 == DECL_NAME (friend_decl
));
1485 /* Register the specialization SPEC as a specialization of TMPL with
1486 the indicated ARGS. IS_FRIEND indicates whether the specialization
1487 is actually just a friend declaration. ATTRLIST is the list of
1488 attributes that the specialization is declared with or NULL when
1489 it isn't. Returns SPEC, or an equivalent prior declaration, if
1492 We also store instantiations of field packs in the hash table, even
1493 though they are not themselves templates, to make lookup easier. */
1496 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1501 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1502 || (TREE_CODE (tmpl
) == FIELD_DECL
1503 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1505 if (TREE_CODE (spec
) == FUNCTION_DECL
1506 && uses_template_parms (DECL_TI_ARGS (spec
)))
1507 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1508 register it; we want the corresponding TEMPLATE_DECL instead.
1509 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1510 the more obvious `uses_template_parms (spec)' to avoid problems
1511 with default function arguments. In particular, given
1512 something like this:
1514 template <class T> void f(T t1, T t = T())
1516 the default argument expression is not substituted for in an
1517 instantiation unless and until it is actually needed. */
1526 hash
= spec_hasher::hash (&elt
);
1528 spec_entry
**slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1534 /* We can sometimes try to re-register a specialization that we've
1535 already got. In particular, regenerate_decl_from_template calls
1536 duplicate_decls which will update the specialization list. But,
1537 we'll still get called again here anyhow. It's more convenient
1538 to simply allow this than to try to prevent it. */
1541 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1543 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1545 if (DECL_ODR_USED (fn
)
1546 || DECL_EXPLICIT_INSTANTIATION (fn
))
1548 error ("specialization of %qD after instantiation",
1550 return error_mark_node
;
1555 /* This situation should occur only if the first
1556 specialization is an implicit instantiation, the
1557 second is an explicit specialization, and the
1558 implicit instantiation has not yet been used. That
1559 situation can occur if we have implicitly
1560 instantiated a member function and then specialized
1563 We can also wind up here if a friend declaration that
1564 looked like an instantiation turns out to be a
1567 template <class T> void foo(T);
1568 class S { friend void foo<>(int) };
1569 template <> void foo(int);
1571 We transform the existing DECL in place so that any
1572 pointers to it become pointers to the updated
1575 If there was a definition for the template, but not
1576 for the specialization, we want this to look as if
1577 there were no definition, and vice versa. */
1578 DECL_INITIAL (fn
) = NULL_TREE
;
1579 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1581 /* The call to duplicate_decls will have applied
1584 An explicit specialization of a function template
1585 is inline only if it is explicitly declared to be,
1586 and independently of whether its function template
1589 to the primary function; now copy the inline bits to
1590 the various clones. */
1591 FOR_EACH_CLONE (clone
, fn
)
1593 DECL_DECLARED_INLINE_P (clone
)
1594 = DECL_DECLARED_INLINE_P (fn
);
1595 DECL_SOURCE_LOCATION (clone
)
1596 = DECL_SOURCE_LOCATION (fn
);
1597 DECL_DELETED_FN (clone
)
1598 = DECL_DELETED_FN (fn
);
1600 check_specialization_namespace (tmpl
);
1605 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1607 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1608 if (dd
== error_mark_node
)
1609 /* We've already complained in duplicate_decls. */
1610 return error_mark_node
;
1612 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1613 /* Dup decl failed, but this is a new definition. Set the
1614 line number so any errors match this new
1616 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1622 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1624 /* A specialization must be declared in the same namespace as the
1625 template it is specializing. */
1626 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1627 && !check_specialization_namespace (tmpl
))
1628 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1630 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1631 gcc_assert (tmpl
&& args
&& spec
);
1634 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1635 && PRIMARY_TEMPLATE_P (tmpl
)
1636 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1637 || variable_template_p (tmpl
))
1638 /* If TMPL is a forward declaration of a template function, keep a list
1639 of all specializations in case we need to reassign them to a friend
1640 template later in tsubst_friend_function.
1642 Also keep a list of all variable template instantiations so that
1643 process_partial_specialization can check whether a later partial
1644 specialization would have used it. */
1645 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1646 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1651 /* Restricts tree and type comparisons. */
1652 int comparing_specializations
;
1653 int comparing_dependent_aliases
;
1655 /* Returns true iff two spec_entry nodes are equivalent. */
1658 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1662 ++comparing_specializations
;
1663 ++comparing_dependent_aliases
;
1664 ++processing_template_decl
;
1665 equal
= (e1
->tmpl
== e2
->tmpl
1666 && comp_template_args (e1
->args
, e2
->args
));
1667 if (equal
&& flag_concepts
1668 /* tmpl could be a FIELD_DECL for a capture pack. */
1669 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1670 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1671 && uses_template_parms (e1
->args
))
1673 /* Partial specializations of a variable template can be distinguished by
1675 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1676 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1677 equal
= equivalent_constraints (c1
, c2
);
1679 --processing_template_decl
;
1680 --comparing_dependent_aliases
;
1681 --comparing_specializations
;
1686 /* Returns a hash for a template TMPL and template arguments ARGS. */
1689 hash_tmpl_and_args (tree tmpl
, tree args
)
1691 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1692 return iterative_hash_template_arg (args
, val
);
1696 spec_hasher::hash (tree tmpl
, tree args
)
1698 ++comparing_specializations
;
1699 hashval_t val
= hash_tmpl_and_args (tmpl
, args
);
1700 --comparing_specializations
;
1704 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1708 spec_hasher::hash (spec_entry
*e
)
1710 return spec_hasher::hash (e
->tmpl
, e
->args
);
1713 /* Recursively calculate a hash value for a template argument ARG, for use
1714 in the hash tables of template specializations. We must be
1715 careful to (at least) skip the same entities template_args_equal
1719 iterative_hash_template_arg (tree arg
, hashval_t val
)
1721 if (arg
== NULL_TREE
)
1722 return iterative_hash_object (arg
, val
);
1725 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1726 while (CONVERT_EXPR_P (arg
)
1727 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1728 || class_nttp_const_wrapper_p (arg
))
1729 arg
= TREE_OPERAND (arg
, 0);
1731 enum tree_code code
= TREE_CODE (arg
);
1733 val
= iterative_hash_object (code
, val
);
1737 case ARGUMENT_PACK_SELECT
:
1738 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1739 preserving it in a hash table, which is bad because it will change
1740 meaning when gen_elem_of_pack_expansion_instantiation changes the
1741 ARGUMENT_PACK_SELECT_INDEX. */
1747 case IDENTIFIER_NODE
:
1748 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1751 for (tree elt
: tree_vec_range (arg
))
1752 val
= iterative_hash_template_arg (elt
, val
);
1755 case TYPE_PACK_EXPANSION
:
1756 case EXPR_PACK_EXPANSION
:
1757 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1758 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1760 case TYPE_ARGUMENT_PACK
:
1761 case NONTYPE_ARGUMENT_PACK
:
1762 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1765 for (; arg
; arg
= TREE_CHAIN (arg
))
1766 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1770 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1771 val
= iterative_hash_template_arg (*iter
, val
);
1776 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1777 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
1779 val
= iterative_hash_template_arg (e
.index
, val
);
1780 val
= iterative_hash_template_arg (e
.value
, val
);
1786 if (!DECL_ARTIFICIAL (arg
))
1788 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1789 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1791 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1794 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1797 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1798 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1800 case TEMPLATE_PARM_INDEX
:
1801 val
= iterative_hash_template_arg
1802 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1803 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1804 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1807 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1808 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1809 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1812 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1814 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1818 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1819 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1820 val
= iterative_hash_object (code
, val
);
1821 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1824 /* [temp.over.link] Two lambda-expressions are never considered
1827 So just hash the closure type. */
1828 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1831 case IMPLICIT_CONV_EXPR
:
1832 case STATIC_CAST_EXPR
:
1833 case REINTERPRET_CAST_EXPR
:
1834 case CONST_CAST_EXPR
:
1835 case DYNAMIC_CAST_EXPR
:
1837 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1838 /* Now hash operands as usual. */
1843 tree fn
= CALL_EXPR_FN (arg
);
1844 if (tree name
= dependent_name (fn
))
1846 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1847 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1850 val
= iterative_hash_template_arg (fn
, val
);
1851 call_expr_arg_iterator ai
;
1852 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1853 x
= next_call_expr_arg (&ai
))
1854 val
= iterative_hash_template_arg (x
, val
);
1862 char tclass
= TREE_CODE_CLASS (code
);
1866 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1868 // We want an alias specialization that survived strip_typedefs
1869 // to hash differently from its TYPE_CANONICAL, to avoid hash
1870 // collisions that compare as different in template_args_equal.
1871 // These could be dependent specializations that strip_typedefs
1872 // left alone, or untouched specializations because
1873 // coerce_template_parms returns the unconverted template
1874 // arguments if it sees incomplete argument packs.
1875 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1876 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1879 switch (TREE_CODE (arg
))
1881 case TEMPLATE_TEMPLATE_PARM
:
1883 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1885 /* Do not recurse with TPI directly, as that is unbounded
1887 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1888 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1893 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1897 if (comparing_specializations
)
1899 /* Hash the components that are relevant to TYPENAME_TYPE
1900 equivalence as determined by structural_comptypes. We
1901 can only coherently do this when comparing_specializations
1902 is set, because otherwise structural_comptypes tries
1903 resolving TYPENAME_TYPE via the current instantiation. */
1904 tree context
= TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg
));
1905 tree fullname
= TYPENAME_TYPE_FULLNAME (arg
);
1906 val
= iterative_hash_template_arg (context
, val
);
1907 val
= iterative_hash_template_arg (fullname
, val
);
1912 if (tree canonical
= TYPE_CANONICAL (arg
))
1913 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1919 case tcc_declaration
:
1921 return iterative_hash_expr (arg
, val
);
1924 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1925 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1926 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1931 /* Unregister the specialization SPEC as a specialization of TMPL.
1932 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1933 if the SPEC was listed as a specialization of TMPL.
1935 Note that SPEC has been ggc_freed, so we can't look inside it. */
1938 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1943 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1944 elt
.args
= TI_ARGS (tinfo
);
1945 elt
.spec
= NULL_TREE
;
1947 entry
= decl_specializations
->find (&elt
);
1950 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1951 gcc_assert (new_spec
!= NULL_TREE
);
1952 entry
->spec
= new_spec
;
1959 /* Like register_specialization, but for local declarations. We are
1960 registering SPEC, an instantiation of TMPL. */
1963 register_local_specialization (tree spec
, tree tmpl
)
1965 gcc_assert (tmpl
!= spec
);
1966 local_specializations
->put (tmpl
, spec
);
1969 /* Registers T as a specialization of itself. This is used to preserve
1970 the references to already-parsed parameters when instantiating
1974 register_local_identity (tree t
)
1976 local_specializations
->put (t
, t
);
1979 /* TYPE is a class type. Returns true if TYPE is an explicitly
1980 specialized class. */
1983 explicit_class_specialization_p (tree type
)
1985 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1987 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1990 /* Print the list of functions at FNS, going through all the overloads
1991 for each element of the list. Alternatively, FNS cannot be a
1992 TREE_LIST, in which case it will be printed together with all the
1995 MORE and *STR should respectively be FALSE and NULL when the function
1996 is called from the outside. They are used internally on recursive
1997 calls. print_candidates manages the two parameters and leaves NULL
1998 in *STR when it ends. */
2001 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2003 if (TREE_CODE (fns
) == TREE_LIST
)
2004 for (; fns
; fns
= TREE_CHAIN (fns
))
2005 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2007 for (lkp_iterator
iter (fns
); iter
;)
2012 const char *pfx
= *str
;
2016 pfx
= _("candidates are:");
2018 pfx
= _("candidate is:");
2019 *str
= get_spaces (pfx
);
2021 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2025 /* Print the list of candidate FNS in an error message. FNS can also
2026 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2029 print_candidates (tree fns
)
2032 print_candidates_1 (fns
, &str
);
2036 /* Get a (possibly) constrained template declaration for the
2037 purpose of ordering candidates. */
2039 get_template_for_ordering (tree list
)
2041 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2042 tree f
= TREE_VALUE (list
);
2043 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2044 return TI_TEMPLATE (ti
);
2048 /* Among candidates having the same signature, return the
2049 most constrained or NULL_TREE if there is no best candidate.
2050 If the signatures of candidates vary (e.g., template
2051 specialization vs. member function), then there can be no
2054 Note that we don't compare constraints on the functions
2055 themselves, but rather those of their templates. */
2057 most_constrained_function (tree candidates
)
2059 // Try to find the best candidate in a first pass.
2060 tree champ
= candidates
;
2061 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2063 int winner
= more_constrained (get_template_for_ordering (champ
),
2064 get_template_for_ordering (c
));
2066 champ
= c
; // The candidate is more constrained
2067 else if (winner
== 0)
2068 return NULL_TREE
; // Neither is more constrained
2071 // Verify that the champ is better than previous candidates.
2072 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2073 if (!more_constrained (get_template_for_ordering (champ
),
2074 get_template_for_ordering (c
)))
2082 /* Returns the template (one of the functions given by TEMPLATE_ID)
2083 which can be specialized to match the indicated DECL with the
2084 explicit template args given in TEMPLATE_ID. The DECL may be
2085 NULL_TREE if none is available. In that case, the functions in
2086 TEMPLATE_ID are non-members.
2088 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2089 specialization of a member template.
2091 The TEMPLATE_COUNT is the number of references to qualifying
2092 template classes that appeared in the name of the function. See
2093 check_explicit_specialization for a more accurate description.
2095 TSK indicates what kind of template declaration (if any) is being
2096 declared. TSK_TEMPLATE indicates that the declaration given by
2097 DECL, though a FUNCTION_DECL, has template parameters, and is
2098 therefore a template function.
2100 The template args (those explicitly specified and those deduced)
2101 are output in a newly created vector *TARGS_OUT.
2103 If it is impossible to determine the result, an error message is
2104 issued. The error_mark_node is returned to indicate failure. */
2107 determine_specialization (tree template_id
,
2110 int need_member_template
,
2116 tree explicit_targs
;
2117 tree candidates
= NULL_TREE
;
2119 /* A TREE_LIST of templates of which DECL may be a specialization.
2120 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2121 corresponding TREE_PURPOSE is the set of template arguments that,
2122 when used to instantiate the template, would produce a function
2123 with the signature of DECL. */
2124 tree templates
= NULL_TREE
;
2126 cp_binding_level
*b
;
2128 *targs_out
= NULL_TREE
;
2130 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2131 return error_mark_node
;
2133 /* We shouldn't be specializing a member template of an
2134 unspecialized class template; we already gave an error in
2135 check_specialization_scope, now avoid crashing. */
2137 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2138 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2140 gcc_assert (errorcount
);
2141 return error_mark_node
;
2144 fns
= TREE_OPERAND (template_id
, 0);
2145 explicit_targs
= TREE_OPERAND (template_id
, 1);
2147 if (fns
== error_mark_node
)
2148 return error_mark_node
;
2150 /* Check for baselinks. */
2151 if (BASELINK_P (fns
))
2152 fns
= BASELINK_FUNCTIONS (fns
);
2154 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2156 error_at (DECL_SOURCE_LOCATION (decl
),
2157 "%qD is not a function template", fns
);
2158 return error_mark_node
;
2160 else if (VAR_P (decl
) && !variable_template_p (fns
))
2162 error ("%qD is not a variable template", fns
);
2163 return error_mark_node
;
2166 /* Count the number of template headers specified for this
2169 for (b
= current_binding_level
;
2170 b
->kind
== sk_template_parms
;
2174 tree orig_fns
= fns
;
2175 bool header_mismatch
= false;
2177 if (variable_template_p (fns
))
2179 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2180 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2181 tf_warning_or_error
);
2182 if (targs
!= error_mark_node
2183 && constraints_satisfied_p (fns
, targs
))
2184 templates
= tree_cons (targs
, fns
, templates
);
2186 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2190 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2192 tree decl_arg_types
;
2195 /* In case of explicit specialization, we need to check if
2196 the number of template headers appearing in the specialization
2197 is correct. This is usually done in check_explicit_specialization,
2198 but the check done there cannot be exhaustive when specializing
2199 member functions. Consider the following code:
2201 template <> void A<int>::f(int);
2202 template <> template <> void A<int>::f(int);
2204 Assuming that A<int> is not itself an explicit specialization
2205 already, the first line specializes "f" which is a non-template
2206 member function, whilst the second line specializes "f" which
2207 is a template member function. So both lines are syntactically
2208 correct, and check_explicit_specialization does not reject
2211 Here, we can do better, as we are matching the specialization
2212 against the declarations. We count the number of template
2213 headers, and we check if they match TEMPLATE_COUNT + 1
2214 (TEMPLATE_COUNT is the number of qualifying template classes,
2215 plus there must be another header for the member template
2218 Notice that if header_count is zero, this is not a
2219 specialization but rather a template instantiation, so there
2220 is no check we can perform here. */
2221 if (header_count
&& header_count
!= template_count
+ 1)
2223 header_mismatch
= true;
2227 /* Check that the number of template arguments at the
2228 innermost level for DECL is the same as for FN. */
2229 if (current_binding_level
->kind
== sk_template_parms
2230 && !current_binding_level
->explicit_spec_p
2231 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2232 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2233 (current_template_parms
))))
2236 /* DECL might be a specialization of FN. */
2237 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2238 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2240 /* For a non-static member function, we need to make sure
2241 that the const qualification is the same. Since
2242 get_bindings does not try to merge the "this" parameter,
2243 we must do the comparison explicitly. */
2244 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2246 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2247 TREE_VALUE (decl_arg_types
)))
2250 /* And the ref-qualification. */
2251 if (type_memfn_rqual (TREE_TYPE (decl
))
2252 != type_memfn_rqual (TREE_TYPE (fn
)))
2256 /* Skip the "this" parameter and, for constructors of
2257 classes with virtual bases, the VTT parameter. A
2258 full specialization of a constructor will have a VTT
2259 parameter, but a template never will. */
2261 = skip_artificial_parms_for (decl
, decl_arg_types
);
2263 = skip_artificial_parms_for (fn
, fn_arg_types
);
2265 /* Function templates cannot be specializations; there are
2266 no partial specializations of functions. Therefore, if
2267 the type of DECL does not match FN, there is no
2270 Note that it should never be the case that we have both
2271 candidates added here, and for regular member functions
2273 if (tsk
== tsk_template
)
2275 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2276 current_template_parms
))
2278 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2279 TREE_TYPE (TREE_TYPE (fn
))))
2281 if (!compparms (fn_arg_types
, decl_arg_types
))
2284 tree freq
= get_constraints (fn
);
2285 tree dreq
= get_constraints (decl
);
2290 /* C++20 CA104: Substitute directly into the
2291 constraint-expression. */
2292 tree fargs
= DECL_TI_ARGS (fn
);
2293 tsubst_flags_t complain
= tf_none
;
2294 freq
= tsubst_constraint_info (freq
, fargs
, complain
, fn
);
2295 if (!cp_tree_equal (freq
, dreq
))
2299 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2303 /* See whether this function might be a specialization of this
2304 template. Suppress access control because we might be trying
2305 to make this specialization a friend, and we have already done
2306 access control for the declaration of the specialization. */
2307 push_deferring_access_checks (dk_no_check
);
2308 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2309 pop_deferring_access_checks ();
2312 /* We cannot deduce template arguments that when used to
2313 specialize TMPL will produce DECL. */
2316 if (uses_template_parms (targs
))
2317 /* We deduced something involving 'auto', which isn't a valid
2318 template argument. */
2321 /* Save this template, and the arguments deduced. */
2322 templates
= tree_cons (targs
, fn
, templates
);
2324 else if (need_member_template
)
2325 /* FN is an ordinary member function, and we need a
2326 specialization of a member template. */
2328 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2329 /* We can get IDENTIFIER_NODEs here in certain erroneous
2332 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2333 /* This is just an ordinary non-member function. Nothing can
2334 be a specialization of that. */
2336 else if (DECL_ARTIFICIAL (fn
))
2337 /* Cannot specialize functions that are created implicitly. */
2341 tree decl_arg_types
;
2343 /* This is an ordinary member function. However, since
2344 we're here, we can assume its enclosing class is a
2345 template class. For example,
2347 template <typename T> struct S { void f(); };
2348 template <> void S<int>::f() {}
2350 Here, S<int>::f is a non-template, but S<int> is a
2351 template class. If FN has the same type as DECL, we
2352 might be in business. */
2354 if (!DECL_TEMPLATE_INFO (fn
))
2355 /* Its enclosing class is an explicit specialization
2356 of a template class. This is not a candidate. */
2359 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2360 TREE_TYPE (TREE_TYPE (fn
))))
2361 /* The return types differ. */
2364 /* Adjust the type of DECL in case FN is a static member. */
2365 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2366 if (DECL_STATIC_FUNCTION_P (fn
)
2367 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2368 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2370 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2374 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2375 && (type_memfn_rqual (TREE_TYPE (decl
))
2376 != type_memfn_rqual (TREE_TYPE (fn
))))
2379 // If the deduced arguments do not satisfy the constraints,
2380 // this is not a candidate.
2381 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2384 // Add the candidate.
2385 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2389 if (templates
&& TREE_CHAIN (templates
))
2395 It is possible for a specialization with a given function
2396 signature to be instantiated from more than one function
2397 template. In such cases, explicit specification of the
2398 template arguments must be used to uniquely identify the
2399 function template specialization being specialized.
2401 Note that here, there's no suggestion that we're supposed to
2402 determine which of the candidate templates is most
2403 specialized. However, we, also have:
2407 Partial ordering of overloaded function template
2408 declarations is used in the following contexts to select
2409 the function template to which a function template
2410 specialization refers:
2412 -- when an explicit specialization refers to a function
2415 So, we do use the partial ordering rules, at least for now.
2416 This extension can only serve to make invalid programs valid,
2417 so it's safe. And, there is strong anecdotal evidence that
2418 the committee intended the partial ordering rules to apply;
2419 the EDG front end has that behavior, and John Spicer claims
2420 that the committee simply forgot to delete the wording in
2421 [temp.expl.spec]. */
2422 tree tmpl
= most_specialized_instantiation (templates
);
2423 if (tmpl
!= error_mark_node
)
2426 TREE_CHAIN (templates
) = NULL_TREE
;
2430 // Concepts allows multiple declarations of member functions
2431 // with the same signature. Like above, we need to rely on
2432 // on the partial ordering of those candidates to determine which
2434 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2436 if (tree cand
= most_constrained_function (candidates
))
2439 TREE_CHAIN (cand
) = NULL_TREE
;
2443 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2445 error ("template-id %qD for %q+D does not match any template "
2446 "declaration", template_id
, decl
);
2447 if (header_mismatch
)
2448 inform (DECL_SOURCE_LOCATION (decl
),
2449 "saw %d %<template<>%>, need %d for "
2450 "specializing a member function template",
2451 header_count
, template_count
+ 1);
2452 print_candidates (orig_fns
);
2453 return error_mark_node
;
2455 else if ((templates
&& TREE_CHAIN (templates
))
2456 || (candidates
&& TREE_CHAIN (candidates
))
2457 || (templates
&& candidates
))
2459 error ("ambiguous template specialization %qD for %q+D",
2461 candidates
= chainon (candidates
, templates
);
2462 print_candidates (candidates
);
2463 return error_mark_node
;
2466 /* We have one, and exactly one, match. */
2469 tree fn
= TREE_VALUE (candidates
);
2470 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2472 /* Propagate the candidate's constraints to the declaration. */
2473 if (tsk
!= tsk_template
)
2474 set_constraints (decl
, get_constraints (fn
));
2476 /* DECL is a re-declaration or partial instantiation of a template
2478 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2480 /* It was a specialization of an ordinary member function in a
2482 return DECL_TI_TEMPLATE (fn
);
2485 /* It was a specialization of a template. */
2486 tree tmpl
= TREE_VALUE (templates
);
2487 *targs_out
= add_outermost_template_args (tmpl
, TREE_PURPOSE (templates
));
2489 /* Propagate the template's constraints to the declaration. */
2490 if (tsk
!= tsk_template
)
2491 set_constraints (decl
, get_constraints (tmpl
));
2496 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2497 but with the default argument values filled in from those in the
2501 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2504 tree new_spec_types
;
2509 if (spec_types
== void_list_node
)
2510 return void_list_node
;
2512 /* Substitute into the rest of the list. */
2514 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2515 TREE_CHAIN (tmpl_types
));
2517 /* Add the default argument for this parameter. */
2518 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2519 TREE_VALUE (spec_types
),
2523 /* DECL is an explicit specialization. Replicate default arguments
2524 from the template it specializes. (That way, code like:
2526 template <class T> void f(T = 3);
2527 template <> void f(double);
2530 works, as required.) An alternative approach would be to look up
2531 the correct default arguments at the call-site, but this approach
2532 is consistent with how implicit instantiations are handled. */
2535 copy_default_args_to_explicit_spec (tree decl
)
2540 tree new_spec_types
;
2544 tree object_type
= NULL_TREE
;
2545 tree in_charge
= NULL_TREE
;
2546 tree vtt
= NULL_TREE
;
2548 /* See if there's anything we need to do. */
2549 tmpl
= DECL_TI_TEMPLATE (decl
);
2550 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2551 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2552 if (TREE_PURPOSE (t
))
2557 old_type
= TREE_TYPE (decl
);
2558 spec_types
= TYPE_ARG_TYPES (old_type
);
2560 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2562 /* Remove the this pointer, but remember the object's type for
2564 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2565 spec_types
= TREE_CHAIN (spec_types
);
2566 tmpl_types
= TREE_CHAIN (tmpl_types
);
2568 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2570 /* DECL may contain more parameters than TMPL due to the extra
2571 in-charge parameter in constructors and destructors. */
2572 in_charge
= spec_types
;
2573 spec_types
= TREE_CHAIN (spec_types
);
2575 if (DECL_HAS_VTT_PARM_P (decl
))
2578 spec_types
= TREE_CHAIN (spec_types
);
2582 /* Compute the merged default arguments. */
2584 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2586 /* Compute the new FUNCTION_TYPE. */
2590 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2595 /* Put the in-charge parameter back. */
2596 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2597 TREE_VALUE (in_charge
),
2600 new_type
= build_method_type_directly (object_type
,
2601 TREE_TYPE (old_type
),
2605 new_type
= build_function_type (TREE_TYPE (old_type
),
2607 new_type
= cp_build_type_attribute_variant (new_type
,
2608 TYPE_ATTRIBUTES (old_type
));
2609 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2611 TREE_TYPE (decl
) = new_type
;
2614 /* Return the number of template headers we expect to see for a definition
2615 or specialization of CTYPE or one of its non-template members. */
2618 num_template_headers_for_class (tree ctype
)
2620 int num_templates
= 0;
2622 while (ctype
&& CLASS_TYPE_P (ctype
))
2624 /* You're supposed to have one `template <...>' for every
2625 template class, but you don't need one for a full
2626 specialization. For example:
2628 template <class T> struct S{};
2629 template <> struct S<int> { void f(); };
2630 void S<int>::f () {}
2632 is correct; there shouldn't be a `template <>' for the
2633 definition of `S<int>::f'. */
2634 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2635 /* If CTYPE does not have template information of any
2636 kind, then it is not a template, nor is it nested
2637 within a template. */
2639 if (explicit_class_specialization_p (ctype
))
2641 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2644 ctype
= TYPE_CONTEXT (ctype
);
2647 return num_templates
;
2650 /* Do a simple sanity check on the template headers that precede the
2651 variable declaration DECL. */
2654 check_template_variable (tree decl
)
2656 tree ctx
= CP_DECL_CONTEXT (decl
);
2657 int wanted
= num_template_headers_for_class (ctx
);
2658 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2659 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2661 if (cxx_dialect
< cxx14
)
2662 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wc__14_extensions
,
2663 "variable templates only available with "
2664 "%<-std=c++14%> or %<-std=gnu++14%>");
2666 // Namespace-scope variable templates should have a template header.
2669 if (template_header_count
> wanted
)
2671 auto_diagnostic_group d
;
2672 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2673 "too many template headers for %qD "
2676 if (warned
&& CLASS_TYPE_P (ctx
)
2677 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2678 inform (DECL_SOURCE_LOCATION (decl
),
2679 "members of an explicitly specialized class are defined "
2680 "without a template header");
2684 /* An explicit specialization whose declarator-id or class-head-name is not
2685 qualified shall be declared in the nearest enclosing namespace of the
2686 template, or, if the namespace is inline (7.3.1), any namespace from its
2687 enclosing namespace set.
2689 If the name declared in the explicit instantiation is an unqualified name,
2690 the explicit instantiation shall appear in the namespace where its template
2691 is declared or, if that namespace is inline (7.3.1), any namespace from its
2692 enclosing namespace set. */
2695 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2697 tree tmpl
= most_general_template (t
);
2698 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2699 && !is_nested_namespace (current_namespace
,
2700 CP_DECL_CONTEXT (tmpl
), true))
2702 if (processing_specialization
)
2703 permerror (loc
, "explicit specialization of %qD outside its "
2704 "namespace must use a nested-name-specifier", tmpl
);
2705 else if (processing_explicit_instantiation
2706 && cxx_dialect
>= cxx11
)
2707 /* This was allowed in C++98, so only pedwarn. */
2708 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2709 "outside its namespace must use a nested-name-"
2714 /* Warn for a template specialization SPEC that is missing some of a set
2715 of function or type attributes that the template TEMPL is declared with.
2716 ATTRLIST is a list of additional attributes that SPEC should be taken
2717 to ultimately be declared with. */
2720 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2722 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2723 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2725 /* Avoid warning if the difference between the primary and
2726 the specialization is not in one of the attributes below. */
2727 const char* const blacklist
[] = {
2728 "alloc_align", "alloc_size", "assume_aligned", "format",
2729 "format_arg", "malloc", "nonnull", NULL
2732 /* Put together a list of the black listed attributes that the primary
2733 template is declared with that the specialization is not, in case
2734 it's not apparent from the most recent declaration of the primary. */
2736 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2742 auto_diagnostic_group d
;
2743 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2744 "explicit specialization %q#D may be missing attributes",
2746 inform (DECL_SOURCE_LOCATION (tmpl
),
2748 ? G_("missing primary template attributes %s")
2749 : G_("missing primary template attribute %s"),
2750 pp_formatted_text (&str
));
2753 /* Check to see if the function just declared, as indicated in
2754 DECLARATOR, and in DECL, is a specialization of a function
2755 template. We may also discover that the declaration is an explicit
2756 instantiation at this point.
2758 Returns DECL, or an equivalent declaration that should be used
2759 instead if all goes well. Issues an error message if something is
2760 amiss. Returns error_mark_node if the error is not easily
2763 FLAGS is a bitmask consisting of the following flags:
2765 2: The function has a definition.
2766 4: The function is a friend.
2768 The TEMPLATE_COUNT is the number of references to qualifying
2769 template classes that appeared in the name of the function. For
2772 template <class T> struct S { void f(); };
2775 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2776 classes are not counted in the TEMPLATE_COUNT, so that in
2778 template <class T> struct S {};
2779 template <> struct S<int> { void f(); }
2780 template <> void S<int>::f();
2782 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2783 invalid; there should be no template <>.)
2785 If the function is a specialization, it is marked as such via
2786 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2787 is set up correctly, and it is added to the list of specializations
2788 for that template. */
2791 check_explicit_specialization (tree declarator
,
2797 int have_def
= flags
& 2;
2798 int is_friend
= flags
& 4;
2799 bool is_concept
= flags
& 8;
2800 int specialization
= 0;
2801 int explicit_instantiation
= 0;
2802 int member_specialization
= 0;
2803 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2804 tree dname
= DECL_NAME (decl
);
2809 if (!processing_specialization
)
2812 tsk
= tsk_excessive_parms
;
2815 tsk
= current_tmpl_spec_kind (template_count
);
2820 if (processing_specialization
&& !VAR_P (decl
))
2823 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2825 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
2826 || (DECL_LANG_SPECIFIC (decl
)
2827 && DECL_IMPLICIT_INSTANTIATION (decl
)))
2830 /* This could be something like:
2832 template <class T> void f(T);
2833 class S { friend void f<>(int); } */
2837 /* This case handles bogus declarations like template <>
2838 template <class T> void f<int>(); */
2840 error_at (cp_expr_loc_or_input_loc (declarator
),
2841 "template-id %qE in declaration of primary template",
2848 case tsk_invalid_member_spec
:
2849 /* The error has already been reported in
2850 check_specialization_scope. */
2851 return error_mark_node
;
2853 case tsk_invalid_expl_inst
:
2854 error ("template parameter list used in explicit instantiation");
2860 error ("definition provided for explicit instantiation");
2862 explicit_instantiation
= 1;
2865 case tsk_excessive_parms
:
2866 case tsk_insufficient_parms
:
2867 if (tsk
== tsk_excessive_parms
)
2868 error ("too many template parameter lists in declaration of %qD",
2870 else if (template_header_count
)
2871 error("too few template parameter lists in declaration of %qD", decl
);
2873 error("explicit specialization of %qD must be introduced by "
2874 "%<template <>%>", decl
);
2879 error ("explicit specialization declared %<concept%>");
2881 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2882 /* In cases like template<> constexpr bool v = true;
2883 We'll give an error in check_template_variable. */
2886 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2888 member_specialization
= 1;
2894 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2896 /* This case handles bogus declarations like template <>
2897 template <class T> void f<int>(); */
2899 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2900 error_at (cp_expr_loc_or_input_loc (declarator
),
2901 "template-id %qE in declaration of primary template",
2903 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2905 /* Partial specialization of variable template. */
2906 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2910 else if (cxx_dialect
< cxx14
)
2911 error_at (cp_expr_loc_or_input_loc (declarator
),
2912 "non-type partial specialization %qE "
2913 "is not allowed", declarator
);
2915 error_at (cp_expr_loc_or_input_loc (declarator
),
2916 "non-class, non-variable partial specialization %qE "
2917 "is not allowed", declarator
);
2922 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2923 /* This is a specialization of a member template, without
2924 specialization the containing class. Something like:
2926 template <class T> struct S {
2927 template <class U> void f (U);
2929 template <> template <class U> void S<int>::f(U) {}
2931 That's a specialization -- but of the entire template. */
2939 if ((specialization
|| member_specialization
)
2940 /* This doesn't apply to variable templates. */
2941 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2943 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2944 for (; t
; t
= TREE_CHAIN (t
))
2945 if (TREE_PURPOSE (t
))
2947 permerror (input_location
,
2948 "default argument specified in explicit specialization");
2953 if (specialization
|| member_specialization
|| explicit_instantiation
)
2955 tree tmpl
= NULL_TREE
;
2956 tree targs
= NULL_TREE
;
2957 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2958 bool found_hidden
= false;
2960 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2961 if (!was_template_id
)
2965 gcc_assert (identifier_p (declarator
));
2970 /* If there is no class context, the explicit instantiation
2971 must be at namespace scope. */
2972 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2974 /* Find the namespace binding, using the declaration
2976 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2977 LOOK_want::NORMAL
, true);
2978 if (fns
== error_mark_node
)
2980 /* If lookup fails, look for a friend declaration so we can
2981 give a better diagnostic. */
2982 fns
= (lookup_qualified_name
2983 (CP_DECL_CONTEXT (decl
), dname
,
2984 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
2986 found_hidden
= true;
2989 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2991 error ("%qD is not a template function", dname
);
2992 fns
= error_mark_node
;
2996 declarator
= lookup_template_function (fns
, NULL_TREE
);
2999 if (declarator
== error_mark_node
)
3000 return error_mark_node
;
3002 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3004 if (!explicit_instantiation
)
3005 /* A specialization in class scope. This is invalid,
3006 but the error will already have been flagged by
3007 check_specialization_scope. */
3008 return error_mark_node
;
3011 /* It's not valid to write an explicit instantiation in
3014 class C { template void f(); }
3016 This case is caught by the parser. However, on
3019 template class C { void f(); };
3021 (which is invalid) we can get here. The error will be
3028 else if (ctype
!= NULL_TREE
3029 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3031 // We'll match variable templates in start_decl.
3035 /* Find the list of functions in ctype that have the same
3036 name as the declared function. */
3037 tree name
= TREE_OPERAND (declarator
, 0);
3039 if (constructor_name_p (name
, ctype
))
3041 if (DECL_CONSTRUCTOR_P (decl
)
3042 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3043 : !CLASSTYPE_DESTRUCTOR (ctype
))
3045 /* From [temp.expl.spec]:
3047 If such an explicit specialization for the member
3048 of a class template names an implicitly-declared
3049 special member function (clause _special_), the
3050 program is ill-formed.
3052 Similar language is found in [temp.explicit]. */
3053 error ("specialization of implicitly-declared special member function");
3054 return error_mark_node
;
3057 name
= DECL_NAME (decl
);
3060 /* For a type-conversion operator, We might be looking for
3061 `operator int' which will be a specialization of
3062 `operator T'. Grab all the conversion operators, and
3063 then select from them. */
3064 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3065 ? conv_op_identifier
: name
);
3067 if (fns
== NULL_TREE
)
3069 error ("no member function %qD declared in %qT", name
, ctype
);
3070 return error_mark_node
;
3073 TREE_OPERAND (declarator
, 0) = fns
;
3076 /* Figure out what exactly is being specialized at this point.
3077 Note that for an explicit instantiation, even one for a
3078 member function, we cannot tell a priori whether the
3079 instantiation is for a member template, or just a member
3080 function of a template class. Even if a member template is
3081 being instantiated, the member template arguments may be
3082 elided if they can be deduced from the rest of the
3084 tmpl
= determine_specialization (declarator
, decl
,
3086 member_specialization
,
3090 if (!tmpl
|| tmpl
== error_mark_node
)
3091 /* We couldn't figure out what this declaration was
3093 return error_mark_node
;
3096 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3098 auto_diagnostic_group d
;
3099 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3100 "friend declaration %qD is not visible to "
3101 "explicit specialization", tmpl
))
3102 inform (DECL_SOURCE_LOCATION (tmpl
),
3103 "friend declaration here");
3106 if (!ctype
&& !is_friend
3107 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3108 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3110 tree gen_tmpl
= most_general_template (tmpl
);
3112 if (explicit_instantiation
)
3114 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3115 is done by do_decl_instantiation later. */
3117 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3118 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3120 if (arg_depth
> parm_depth
)
3122 /* If TMPL is not the most general template (for
3123 example, if TMPL is a friend template that is
3124 injected into namespace scope), then there will
3125 be too many levels of TARGS. Remove some of them
3130 new_targs
= make_tree_vec (parm_depth
);
3131 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3132 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3133 = TREE_VEC_ELT (targs
, i
);
3137 return instantiate_template (tmpl
, targs
, tf_error
);
3140 /* If we thought that the DECL was a member function, but it
3141 turns out to be specializing a static member function,
3142 make DECL a static member function as well. */
3143 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3144 && DECL_STATIC_FUNCTION_P (tmpl
)
3145 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3146 revert_static_member_fn (decl
);
3148 /* If this is a specialization of a member template of a
3149 template class, we want to return the TEMPLATE_DECL, not
3150 the specialization of it. */
3151 if (tsk
== tsk_template
&& !was_template_id
)
3153 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3154 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3155 DECL_INITIAL (result
) = NULL_TREE
;
3159 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3160 DECL_SOURCE_LOCATION (result
)
3161 = DECL_SOURCE_LOCATION (decl
);
3162 /* We want to use the argument list specified in the
3163 definition, not in the original declaration. */
3164 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3165 for (parm
= DECL_ARGUMENTS (result
); parm
;
3166 parm
= DECL_CHAIN (parm
))
3167 DECL_CONTEXT (parm
) = result
;
3169 decl
= register_specialization (tmpl
, gen_tmpl
, targs
,
3171 remove_contract_attributes (result
);
3175 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3176 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3178 if (was_template_id
)
3179 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3181 /* Inherit default function arguments from the template
3182 DECL is specializing. */
3183 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3184 copy_default_args_to_explicit_spec (decl
);
3186 /* This specialization has the same protection as the
3187 template it specializes. */
3188 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3189 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3191 /* 7.1.1-1 [dcl.stc]
3193 A storage-class-specifier shall not be specified in an
3194 explicit specialization...
3196 The parser rejects these, so unless action is taken here,
3197 explicit function specializations will always appear with
3200 The action recommended by the C++ CWG in response to C++
3201 defect report 605 is to make the storage class and linkage
3202 of the explicit specialization match the templated function:
3204 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3206 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3208 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3209 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3211 /* A concept cannot be specialized. */
3212 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3214 error ("explicit specialization of function concept %qD",
3216 return error_mark_node
;
3219 /* This specialization has the same linkage and visibility as
3220 the function template it specializes. */
3221 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3222 if (! TREE_PUBLIC (decl
))
3224 DECL_INTERFACE_KNOWN (decl
) = 1;
3225 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3227 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3228 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3230 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3231 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3235 /* If DECL is a friend declaration, declared using an
3236 unqualified name, the namespace associated with DECL may
3237 have been set incorrectly. For example, in:
3239 template <typename T> void f(T);
3241 struct S { friend void f<int>(int); }
3244 we will have set the DECL_CONTEXT for the friend
3245 declaration to N, rather than to the global namespace. */
3246 if (DECL_NAMESPACE_SCOPE_P (decl
))
3247 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3249 if (is_friend
&& !have_def
)
3250 /* This is not really a declaration of a specialization.
3251 It's just the name of an instantiation. But, it's not
3252 a request for an instantiation, either. */
3253 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3254 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3255 /* A specialization is not necessarily COMDAT. */
3256 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3257 && DECL_DECLARED_INLINE_P (decl
));
3258 else if (VAR_P (decl
))
3259 DECL_COMDAT (decl
) = false;
3261 /* If this is a full specialization, register it so that we can find
3262 it again. Partial specializations will be registered in
3263 process_partial_specialization. */
3264 if (!processing_template_decl
)
3266 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3268 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3272 /* If this is a specialization, splice any contracts that may have
3273 been inherited from the template, removing them. */
3274 if (decl
!= error_mark_node
&& DECL_TEMPLATE_SPECIALIZATION (decl
))
3275 remove_contract_attributes (decl
);
3277 /* A 'structor should already have clones. */
3278 gcc_assert (decl
== error_mark_node
3279 || variable_template_p (tmpl
)
3280 || !(DECL_CONSTRUCTOR_P (decl
)
3281 || DECL_DESTRUCTOR_P (decl
))
3282 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3289 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3290 parameters. These are represented in the same format used for
3291 DECL_TEMPLATE_PARMS. */
3294 comp_template_parms (const_tree parms1
, const_tree parms2
)
3299 if (parms1
== parms2
)
3302 for (p1
= parms1
, p2
= parms2
;
3303 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3304 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3306 tree t1
= TREE_VALUE (p1
);
3307 tree t2
= TREE_VALUE (p2
);
3310 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3311 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3313 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3316 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3318 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3319 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3321 /* If either of the template parameters are invalid, assume
3322 they match for the sake of error recovery. */
3323 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3326 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3329 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3330 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3331 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3333 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3338 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3339 /* One set of parameters has more parameters lists than the
3346 /* Returns true if two template parameters are declared with
3347 equivalent constraints. */
3350 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3352 tree req1
= TREE_TYPE (parm1
);
3353 tree req2
= TREE_TYPE (parm2
);
3357 return cp_tree_equal (req1
, req2
);
3361 /* Returns true when two template parameters are equivalent. */
3364 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3366 tree decl1
= TREE_VALUE (parm1
);
3367 tree decl2
= TREE_VALUE (parm2
);
3369 /* If either of the template parameters are invalid, assume
3370 they match for the sake of error recovery. */
3371 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3374 /* ... they declare parameters of the same kind. */
3375 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3378 /* ... one parameter was introduced by a parameter declaration, then
3379 both are. This case arises as a result of eagerly rewriting declarations
3381 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3384 /* ... if either declares a pack, they both do. */
3385 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3388 if (TREE_CODE (decl1
) == PARM_DECL
)
3390 /* ... if they declare non-type parameters, the types are equivalent. */
3391 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3394 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3396 /* ... if they declare template template parameters, their template
3397 parameter lists are equivalent. */
3398 if (!template_heads_equivalent_p (decl1
, decl2
))
3402 /* ... if they are declared with a qualified-concept name, they both
3403 are, and those names are equivalent. */
3404 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3407 /* Returns true if two template parameters lists are equivalent.
3408 Two template parameter lists are equivalent if they have the
3409 same length and their corresponding parameters are equivalent.
3411 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3412 data structure returned by DECL_TEMPLATE_PARMS.
3414 This is generally the same implementation as comp_template_parms
3415 except that it also the concept names and arguments used to
3416 introduce parameters. */
3419 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3421 if (parms1
== parms2
)
3424 const_tree p1
= parms1
;
3425 const_tree p2
= parms2
;
3426 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3428 tree list1
= TREE_VALUE (p1
);
3429 tree list2
= TREE_VALUE (p2
);
3431 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3434 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3436 tree parm1
= TREE_VEC_ELT (list1
, i
);
3437 tree parm2
= TREE_VEC_ELT (list2
, i
);
3438 if (!template_parameters_equivalent_p (parm1
, parm2
))
3442 p1
= TREE_CHAIN (p1
);
3443 p2
= TREE_CHAIN (p2
);
3446 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3452 /* Return true if the requires-clause of the template parameter lists are
3453 equivalent and false otherwise. */
3455 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3457 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3458 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3459 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3461 if (!cp_tree_equal (req1
, req2
))
3466 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3467 Two template heads are equivalent if their template parameter
3468 lists are equivalent and their requires clauses are equivalent.
3470 In pre-C++20, this is equivalent to calling comp_template_parms
3471 for the template parameters of TMPL1 and TMPL2. */
3474 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3476 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3477 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3479 /* Don't change the matching rules for pre-C++20. */
3480 if (cxx_dialect
< cxx20
)
3481 return comp_template_parms (parms1
, parms2
);
3483 /* ... have the same number of template parameters, and their
3484 corresponding parameters are equivalent. */
3485 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3488 /* ... if either has a requires-clause, they both do and their
3489 corresponding constraint-expressions are equivalent. */
3490 return template_requirements_equivalent_p (parms1
, parms2
);
3493 /* Determine whether PARM is a parameter pack. */
3496 template_parameter_pack_p (const_tree parm
)
3498 /* Determine if we have a non-type template parameter pack. */
3499 if (TREE_CODE (parm
) == PARM_DECL
)
3500 return (DECL_TEMPLATE_PARM_P (parm
)
3501 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3502 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3503 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3505 /* If this is a list of template parameters, we could get a
3506 TYPE_DECL or a TEMPLATE_DECL. */
3507 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3508 parm
= TREE_TYPE (parm
);
3510 /* Otherwise it must be a type template parameter. */
3511 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3512 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3513 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3516 /* Determine if T is a function parameter pack. */
3519 function_parameter_pack_p (const_tree t
)
3521 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3522 return DECL_PACK_P (t
);
3526 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3527 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3530 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3532 if (! primary_func_tmpl_inst
3533 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3534 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3537 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3540 /* Return true iff the function parameter PARAM_DECL was expanded
3541 from the function parameter pack PACK. */
3544 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3546 if (DECL_ARTIFICIAL (param_decl
)
3547 || !function_parameter_pack_p (pack
))
3550 /* The parameter pack and its pack arguments have the same
3552 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3555 /* Determine whether ARGS describes a variadic template args list,
3556 i.e., one that is terminated by a template argument pack. */
3559 template_args_variadic_p (tree args
)
3564 if (args
== NULL_TREE
)
3567 args
= INNERMOST_TEMPLATE_ARGS (args
);
3568 nargs
= TREE_VEC_LENGTH (args
);
3573 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3575 return ARGUMENT_PACK_P (last_parm
);
3578 /* Generate a new name for the parameter pack name NAME (an
3579 IDENTIFIER_NODE) that incorporates its */
3582 make_ith_pack_parameter_name (tree name
, int i
)
3584 /* Munge the name to include the parameter index. */
3585 #define NUMBUF_LEN 128
3586 char numbuf
[NUMBUF_LEN
];
3590 if (name
== NULL_TREE
)
3592 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3593 newname_len
= IDENTIFIER_LENGTH (name
)
3594 + strlen (numbuf
) + 2;
3595 newname
= (char*)alloca (newname_len
);
3596 snprintf (newname
, newname_len
,
3597 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3598 return get_identifier (newname
);
3601 /* Return true if T is a primary function, class or alias template
3602 specialization, not including the template pattern. */
3605 primary_template_specialization_p (const_tree t
)
3610 if (VAR_OR_FUNCTION_DECL_P (t
))
3611 return (DECL_LANG_SPECIFIC (t
)
3612 && DECL_USE_TEMPLATE (t
)
3613 && DECL_TEMPLATE_INFO (t
)
3614 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3615 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3616 return (CLASSTYPE_TEMPLATE_INFO (t
)
3617 && CLASSTYPE_USE_TEMPLATE (t
)
3618 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3619 else if (alias_template_specialization_p (t
, nt_transparent
))
3624 /* Return true if PARM is a template template parameter. */
3627 template_template_parameter_p (const_tree parm
)
3629 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3632 /* Return true iff PARM is a DECL representing a type template
3636 template_type_parameter_p (const_tree parm
)
3639 && (TREE_CODE (parm
) == TYPE_DECL
3640 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3641 && DECL_TEMPLATE_PARM_P (parm
));
3644 /* Return the template parameters of T if T is a
3645 primary template instantiation, NULL otherwise. */
3648 get_primary_template_innermost_parameters (const_tree t
)
3650 tree parms
= NULL
, template_info
= NULL
;
3652 if ((template_info
= get_template_info (t
))
3653 && primary_template_specialization_p (t
))
3654 parms
= INNERMOST_TEMPLATE_PARMS
3655 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3660 /* Returns the template arguments of T if T is a template instantiation,
3664 get_template_innermost_arguments (const_tree t
)
3666 tree args
= NULL
, template_info
= NULL
;
3668 if ((template_info
= get_template_info (t
))
3669 && TI_ARGS (template_info
))
3670 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3675 /* Return the argument pack elements of T if T is a template argument pack,
3679 get_template_argument_pack_elems (const_tree t
)
3681 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3682 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3685 return ARGUMENT_PACK_ARGS (t
);
3688 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3689 ARGUMENT_PACK_SELECT represents. */
3692 argument_pack_select_arg (tree t
)
3694 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3695 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3697 /* If the selected argument is an expansion E, that most likely means we were
3698 called from gen_elem_of_pack_expansion_instantiation during the
3699 substituting of an argument pack (of which the Ith element is a pack
3700 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3701 In this case, the Ith element resulting from this substituting is going to
3702 be a pack expansion, which pattern is the pattern of E. Let's return the
3703 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3704 resulting pack expansion from it. */
3705 if (PACK_EXPANSION_P (arg
))
3707 /* Make sure we aren't throwing away arg info. */
3708 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3709 arg
= PACK_EXPANSION_PATTERN (arg
);
3715 /* Return a modification of ARGS that's suitable for preserving inside a hash
3716 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3717 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3720 preserve_args (tree args
, bool cow_p
= true)
3725 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
3727 tree t
= TREE_VEC_ELT (args
, i
);
3731 else if (TREE_CODE (t
) == ARGUMENT_PACK_SELECT
)
3732 r
= argument_pack_select_arg (t
);
3733 else if (TREE_CODE (t
) == TREE_VEC
)
3734 r
= preserve_args (t
, cow_p
);
3741 args
= copy_template_args (args
);
3744 TREE_VEC_ELT (args
, i
) = r
;
3751 /* True iff FN is a function representing a built-in variadic parameter
3755 builtin_pack_fn_p (tree fn
)
3758 || TREE_CODE (fn
) != FUNCTION_DECL
3759 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3762 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3768 /* True iff CALL is a call to a function representing a built-in variadic
3772 builtin_pack_call_p (tree call
)
3774 if (TREE_CODE (call
) != CALL_EXPR
)
3776 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3779 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3782 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3785 tree ohi
= CALL_EXPR_ARG (call
, 0);
3786 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
);
3788 if (instantiation_dependent_expression_p (hi
))
3792 call
= copy_node (call
);
3793 CALL_EXPR_ARG (call
, 0) = hi
;
3795 tree ex
= make_pack_expansion (call
, complain
);
3796 tree vec
= make_tree_vec (1);
3797 TREE_VEC_ELT (vec
, 0) = ex
;
3802 hi
= instantiate_non_dependent_expr (hi
, complain
);
3803 hi
= cxx_constant_value (hi
, complain
);
3804 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3806 /* Calculate the largest value of len that won't make the size of the vec
3807 overflow an int. The compiler will exceed resource limits long before
3808 this, but it seems a decent place to diagnose. */
3809 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3811 if (len
< 0 || len
> max
)
3813 if ((complain
& tf_error
)
3814 && hi
!= error_mark_node
)
3815 error ("argument to %<__integer_pack%> must be between 0 and %d",
3817 return error_mark_node
;
3820 tree vec
= make_tree_vec (len
);
3822 for (int i
= 0; i
< len
; ++i
)
3823 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3829 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3833 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3836 if (!builtin_pack_call_p (call
))
3839 tree fn
= CALL_EXPR_FN (call
);
3841 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3842 return expand_integer_pack (call
, args
, complain
, in_decl
);
3847 /* Return true if the tree T has the extra args mechanism for
3848 avoiding partial instantiation. */
3851 has_extra_args_mechanism_p (const_tree t
)
3853 return (PACK_EXPANSION_P (t
) /* PACK_EXPANSION_EXTRA_ARGS */
3854 || TREE_CODE (t
) == REQUIRES_EXPR
/* REQUIRES_EXPR_EXTRA_ARGS */
3855 || (TREE_CODE (t
) == IF_STMT
3856 && IF_STMT_CONSTEXPR_P (t
))); /* IF_STMT_EXTRA_ARGS */
3859 /* Structure used to track the progress of find_parameter_packs_r. */
3860 struct find_parameter_pack_data
3862 /* TREE_LIST that will contain all of the parameter packs found by
3864 tree
* parameter_packs
;
3866 /* Set of AST nodes that have been visited by the traversal. */
3867 hash_set
<tree
> *visited
;
3869 /* True iff we're making a type pack expansion. */
3870 bool type_pack_expansion_p
;
3872 /* True iff we found a subtree that has the extra args mechanism. */
3873 bool found_extra_args_tree_p
= false;
3876 /* Identifies all of the argument packs that occur in a template
3877 argument and appends them to the TREE_LIST inside DATA, which is a
3878 find_parameter_pack_data structure. This is a subroutine of
3879 make_pack_expansion and uses_parameter_packs. */
3881 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3884 struct find_parameter_pack_data
* ppd
=
3885 (struct find_parameter_pack_data
*)data
;
3886 bool parameter_pack_p
= false;
3888 #define WALK_SUBTREE(NODE) \
3889 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3890 ppd, ppd->visited) \
3892 /* Don't look through typedefs; we are interested in whether a
3893 parameter pack is actually written in the expression/type we're
3894 looking at, not the target type. */
3895 if (TYPE_P (t
) && typedef_variant_p (t
))
3897 /* But do look at arguments for an alias template. */
3898 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3899 cp_walk_tree (&TI_ARGS (tinfo
),
3900 &find_parameter_packs_r
,
3906 /* Identify whether this is a parameter pack or not. */
3907 switch (TREE_CODE (t
))
3909 case TEMPLATE_PARM_INDEX
:
3910 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3911 parameter_pack_p
= true;
3914 case TEMPLATE_TYPE_PARM
:
3915 t
= TYPE_MAIN_VARIANT (t
);
3917 case TEMPLATE_TEMPLATE_PARM
:
3918 /* If the placeholder appears in the decl-specifier-seq of a function
3919 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3920 is a pack expansion, the invented template parameter is a template
3922 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3923 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3924 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3925 parameter_pack_p
= true;
3930 if (DECL_PACK_P (t
))
3932 /* We don't want to walk into the type of a PARM_DECL,
3933 because we don't want to see the type parameter pack. */
3935 parameter_pack_p
= true;
3940 if (DECL_PACK_P (t
))
3942 /* We don't want to walk into the type of a variadic capture proxy,
3943 because we don't want to see the type parameter pack. */
3945 parameter_pack_p
= true;
3947 else if (variable_template_specialization_p (t
))
3949 cp_walk_tree (&DECL_TI_ARGS (t
),
3950 find_parameter_packs_r
,
3957 if (builtin_pack_call_p (t
))
3958 parameter_pack_p
= true;
3962 parameter_pack_p
= true;
3965 /* Not a parameter pack. */
3969 if (parameter_pack_p
)
3971 /* Add this parameter pack to the list. */
3972 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3975 if (has_extra_args_mechanism_p (t
) && !PACK_EXPANSION_P (t
))
3976 ppd
->found_extra_args_tree_p
= true;
3979 cp_walk_tree (&TYPE_CONTEXT (t
),
3980 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3982 /* This switch statement will return immediately if we don't find a
3983 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3984 switch (TREE_CODE (t
))
3986 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3987 /* Check the template itself. */
3988 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3989 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3994 tree decl
= DECL_EXPR_DECL (t
);
3995 /* Ignore the declaration of a capture proxy for a parameter pack. */
3996 if (is_capture_proxy (decl
))
3998 if (is_typedef_decl (decl
))
3999 /* Since we stop at typedefs above, we need to look through them at
4000 the point of the DECL_EXPR. */
4001 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
4002 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4007 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4009 cp_walk_tree (&TREE_TYPE (t
),
4010 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4013 case TYPE_PACK_EXPANSION
:
4014 case EXPR_PACK_EXPANSION
:
4019 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4024 case IDENTIFIER_NODE
:
4025 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4032 /* Since we defer implicit capture, look in the parms and body. */
4033 tree fn
= lambda_function (t
);
4034 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4036 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4043 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4044 type_pack_expansion_p to false so that any placeholders
4045 within the expression don't get marked as parameter packs. */
4046 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4047 ppd
->type_pack_expansion_p
= false;
4048 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4050 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4056 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4058 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4060 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4062 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4068 if (CLASS_TYPE_P (t
))
4069 /* Local class, need to look through the whole definition. */
4070 for (tree bb
: BINFO_BASE_BINFOS (TYPE_BINFO (t
)))
4071 cp_walk_tree (&BINFO_TYPE (bb
), &find_parameter_packs_r
,
4074 /* Enum, look at the values. */
4075 for (tree l
= TYPE_VALUES (t
); l
; l
= TREE_CHAIN (l
))
4076 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l
)),
4077 &find_parameter_packs_r
,
4083 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t
));
4095 /* Determines if the expression or type T uses any parameter packs. */
4097 uses_parameter_packs (tree t
)
4099 tree parameter_packs
= NULL_TREE
;
4100 struct find_parameter_pack_data ppd
;
4101 ppd
.parameter_packs
= ¶meter_packs
;
4102 ppd
.visited
= new hash_set
<tree
>;
4103 ppd
.type_pack_expansion_p
= false;
4104 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4106 return parameter_packs
;
4109 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4110 representation a base-class initializer into a parameter pack
4111 expansion. If all goes well, the resulting node will be an
4112 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4115 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4118 tree parameter_packs
= NULL_TREE
;
4119 bool for_types
= false;
4120 struct find_parameter_pack_data ppd
;
4122 if (!arg
|| arg
== error_mark_node
)
4125 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4127 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4128 class initializer. In this case, the TREE_PURPOSE will be a
4129 _TYPE node (representing the base class expansion we're
4130 initializing) and the TREE_VALUE will be a TREE_LIST
4131 containing the initialization arguments.
4133 The resulting expansion looks somewhat different from most
4134 expansions. Rather than returning just one _EXPANSION, we
4135 return a TREE_LIST whose TREE_PURPOSE is a
4136 TYPE_PACK_EXPANSION containing the bases that will be
4137 initialized. The TREE_VALUE will be identical to the
4138 original TREE_VALUE, which is a list of arguments that will
4139 be passed to each base. We do not introduce any new pack
4140 expansion nodes into the TREE_VALUE (although it is possible
4141 that some already exist), because the TREE_PURPOSE and
4142 TREE_VALUE all need to be expanded together with the same
4143 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4144 resulting TREE_PURPOSE will mention the parameter packs in
4145 both the bases and the arguments to the bases. */
4148 tree parameter_packs
= NULL_TREE
;
4150 /* Determine which parameter packs will be used by the base
4152 ppd
.visited
= new hash_set
<tree
>;
4153 ppd
.parameter_packs
= ¶meter_packs
;
4154 ppd
.type_pack_expansion_p
= false;
4155 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4156 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4159 if (parameter_packs
== NULL_TREE
)
4161 if (complain
& tf_error
)
4162 error ("base initializer expansion %qT contains no parameter packs",
4165 return error_mark_node
;
4168 if (TREE_VALUE (arg
) != void_type_node
)
4170 /* Collect the sets of parameter packs used in each of the
4171 initialization arguments. */
4172 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4174 /* Determine which parameter packs will be expanded in this
4176 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4183 /* Create the pack expansion type for the base type. */
4184 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4185 PACK_EXPANSION_PATTERN (purpose
) = TREE_PURPOSE (arg
);
4186 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4187 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4189 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4190 they will rarely be compared to anything. */
4191 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4193 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4196 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4199 /* Build the PACK_EXPANSION_* node. */
4201 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4202 : make_node (EXPR_PACK_EXPANSION
);
4203 PACK_EXPANSION_PATTERN (result
) = arg
;
4204 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4206 /* Propagate type and const-expression information. */
4207 TREE_TYPE (result
) = TREE_TYPE (arg
);
4208 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4209 /* Mark this read now, since the expansion might be length 0. */
4210 mark_exp_read (arg
);
4213 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4214 they will rarely be compared to anything. */
4215 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4217 /* Determine which parameter packs will be expanded. */
4218 ppd
.parameter_packs
= ¶meter_packs
;
4219 ppd
.visited
= new hash_set
<tree
>;
4220 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4221 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4224 /* Make sure we found some parameter packs. */
4225 if (parameter_packs
== NULL_TREE
)
4227 if (complain
& tf_error
)
4230 error ("expansion pattern %qT contains no parameter packs", arg
);
4232 error ("expansion pattern %qE contains no parameter packs", arg
);
4234 return error_mark_node
;
4236 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4238 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4239 if (ppd
.found_extra_args_tree_p
)
4240 /* If the pattern of this pack expansion contains a subtree that has
4241 the extra args mechanism for avoiding partial instantiation, then
4242 force this pack expansion to also use extra args. Otherwise
4243 partial instantiation of this pack expansion may not lower the
4244 level of some parameter packs within the pattern, which would
4245 confuse tsubst_pack_expansion later (PR101764). */
4246 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result
) = true;
4251 /* Checks T for any "bare" parameter packs, which have not yet been
4252 expanded, and issues an error if any are found. This operation can
4253 only be done on full expressions or types (e.g., an expression
4254 statement, "if" condition, etc.), because we could have expressions like:
4256 foo(f(g(h(args)))...)
4258 where "args" is a parameter pack. check_for_bare_parameter_packs
4259 should not be called for the subexpressions args, h(args),
4260 g(h(args)), or f(g(h(args))), because we would produce erroneous
4263 Returns TRUE and emits an error if there were bare parameter packs,
4264 returns FALSE otherwise. */
4266 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4268 tree parameter_packs
= NULL_TREE
;
4269 struct find_parameter_pack_data ppd
;
4271 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4274 if (TREE_CODE (t
) == TYPE_DECL
)
4277 ppd
.parameter_packs
= ¶meter_packs
;
4278 ppd
.visited
= new hash_set
<tree
>;
4279 ppd
.type_pack_expansion_p
= false;
4280 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4283 if (!parameter_packs
)
4286 if (loc
== UNKNOWN_LOCATION
)
4287 loc
= cp_expr_loc_or_input_loc (t
);
4289 /* It's OK for a lambda to have an unexpanded parameter pack from the
4290 containing context, but do complain about unexpanded capture packs. */
4291 tree lam
= current_lambda_expr ();
4293 lam
= TREE_TYPE (lam
);
4295 if (lam
&& lam
!= current_class_type
)
4297 /* We're in a lambda, but it isn't the innermost class.
4298 This should work, but currently doesn't. */
4299 sorry_at (loc
, "unexpanded parameter pack in local class in lambda");
4303 if (lam
&& CLASSTYPE_TEMPLATE_INFO (lam
))
4304 for (; parameter_packs
;
4305 parameter_packs
= TREE_CHAIN (parameter_packs
))
4307 tree pack
= TREE_VALUE (parameter_packs
);
4308 if (is_capture_proxy (pack
)
4309 || (TREE_CODE (pack
) == PARM_DECL
4310 && DECL_CONTEXT (DECL_CONTEXT (pack
)) == lam
))
4314 if (parameter_packs
)
4316 error_at (loc
, "parameter packs not expanded with %<...%>:");
4317 while (parameter_packs
)
4319 tree pack
= TREE_VALUE (parameter_packs
);
4320 tree name
= NULL_TREE
;
4322 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4323 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4324 name
= TYPE_NAME (pack
);
4325 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4326 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4327 else if (TREE_CODE (pack
) == CALL_EXPR
)
4328 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4330 name
= DECL_NAME (pack
);
4333 inform (loc
, " %qD", name
);
4335 inform (loc
, " %s", "<anonymous>");
4337 parameter_packs
= TREE_CHAIN (parameter_packs
);
4346 /* Expand any parameter packs that occur in the template arguments in
4349 expand_template_argument_pack (tree args
)
4351 if (args
== error_mark_node
)
4352 return error_mark_node
;
4354 tree result_args
= NULL_TREE
;
4355 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4356 int num_result_args
= -1;
4357 int non_default_args_count
= -1;
4359 /* First, determine if we need to expand anything, and the number of
4360 slots we'll need. */
4361 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4363 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4364 if (arg
== NULL_TREE
)
4366 if (ARGUMENT_PACK_P (arg
))
4368 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4369 if (num_result_args
< 0)
4370 num_result_args
= in_arg
+ num_packed
;
4372 num_result_args
+= num_packed
;
4376 if (num_result_args
>= 0)
4381 /* If no expansion is necessary, we're done. */
4382 if (num_result_args
< 0)
4385 /* Expand arguments. */
4386 result_args
= make_tree_vec (num_result_args
);
4387 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4388 non_default_args_count
=
4389 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4390 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4392 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4393 if (ARGUMENT_PACK_P (arg
))
4395 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4396 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4397 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4398 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4399 if (non_default_args_count
> 0)
4400 non_default_args_count
+= num_packed
- 1;
4404 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4408 if (non_default_args_count
>= 0)
4409 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4413 /* Checks if DECL shadows a template parameter.
4415 [temp.local]: A template-parameter shall not be redeclared within its
4416 scope (including nested scopes).
4418 Emits an error and returns TRUE if the DECL shadows a parameter,
4419 returns FALSE otherwise. */
4422 check_template_shadow (tree decl
)
4426 /* If we're not in a template, we can't possibly shadow a template
4428 if (!current_template_parms
)
4431 /* Figure out what we're shadowing. */
4432 decl
= OVL_FIRST (decl
);
4433 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4435 /* If there's no previous binding for this name, we're not shadowing
4436 anything, let alone a template parameter. */
4440 /* If we're not shadowing a template parameter, we're done. Note
4441 that OLDDECL might be an OVERLOAD (or perhaps even an
4442 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4444 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4447 /* We check for decl != olddecl to avoid bogus errors for using a
4448 name inside a class. We check TPFI to avoid duplicate errors for
4449 inline member templates. */
4451 || (DECL_TEMPLATE_PARM_P (decl
)
4452 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4455 /* Don't complain about the injected class name, as we've already
4456 complained about the class itself. */
4457 if (DECL_SELF_REFERENCE_P (decl
))
4460 if (DECL_TEMPLATE_PARM_P (decl
))
4461 error ("declaration of template parameter %q+D shadows "
4462 "template parameter", decl
);
4464 error ("declaration of %q+#D shadows template parameter", decl
);
4465 inform (DECL_SOURCE_LOCATION (olddecl
),
4466 "template parameter %qD declared here", olddecl
);
4470 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4471 ORIG_LEVEL, DECL, and TYPE. */
4474 build_template_parm_index (int index
,
4480 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4481 TEMPLATE_PARM_IDX (t
) = index
;
4482 TEMPLATE_PARM_LEVEL (t
) = level
;
4483 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4484 TEMPLATE_PARM_DECL (t
) = decl
;
4485 TREE_TYPE (t
) = type
;
4486 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4487 TREE_READONLY (t
) = TREE_READONLY (decl
);
4492 struct ctp_hasher
: ggc_ptr_hash
<tree_node
>
4494 static hashval_t
hash (tree t
)
4496 ++comparing_specializations
;
4497 tree_code code
= TREE_CODE (t
);
4498 hashval_t val
= iterative_hash_object (code
, 0);
4499 val
= iterative_hash_object (TEMPLATE_TYPE_LEVEL (t
), val
);
4500 val
= iterative_hash_object (TEMPLATE_TYPE_IDX (t
), val
);
4501 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
4502 val
= iterative_hash_template_arg (TYPE_TI_ARGS (t
), val
);
4503 --comparing_specializations
;
4507 static bool equal (tree t
, tree u
)
4509 ++comparing_specializations
;
4510 bool eq
= comptypes (t
, u
, COMPARE_STRUCTURAL
);
4511 --comparing_specializations
;
4516 static GTY (()) hash_table
<ctp_hasher
> *ctp_table
;
4518 /* Find the canonical type parameter for the given template type
4519 parameter. Returns the canonical type parameter, which may be TYPE
4520 if no such parameter existed. */
4523 canonical_type_parameter (tree type
)
4525 if (ctp_table
== NULL
)
4526 ctp_table
= hash_table
<ctp_hasher
>::create_ggc (61);
4528 tree
& slot
= *ctp_table
->find_slot (type
, INSERT
);
4529 if (slot
== NULL_TREE
)
4534 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4535 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4536 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4537 new one is created. */
4540 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4541 tsubst_flags_t complain
)
4543 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4544 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4545 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4546 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4548 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4550 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4551 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4553 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4554 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4555 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4556 DECL_ARTIFICIAL (decl
) = 1;
4557 SET_DECL_TEMPLATE_PARM_P (decl
);
4559 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4560 TEMPLATE_PARM_LEVEL (index
) - levels
,
4561 TEMPLATE_PARM_ORIG_LEVEL (index
),
4563 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4564 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4565 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4567 /* Template template parameters need this. */
4569 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4571 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4572 TYPE_DECL
, DECL_NAME (decl
), type
);
4573 DECL_TEMPLATE_RESULT (decl
) = inner
;
4574 DECL_ARTIFICIAL (inner
) = true;
4575 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4576 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4579 /* Attach the TPI to the decl. */
4580 if (TREE_CODE (inner
) == TYPE_DECL
)
4581 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4583 DECL_INITIAL (decl
) = tpi
;
4586 return TEMPLATE_PARM_DESCENDANTS (index
);
4589 /* Process information from new template parameter PARM and append it
4590 to the LIST being built. This new parameter is a non-type
4591 parameter iff IS_NON_TYPE is true. This new parameter is a
4592 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4596 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4597 bool is_non_type
, bool is_parameter_pack
)
4599 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4600 tree prev
= NULL_TREE
;
4605 prev
= tree_last (list
);
4607 tree p
= TREE_VALUE (prev
);
4608 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4609 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4610 else if (TREE_CODE (p
) == PARM_DECL
)
4611 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4616 tree decl
= NULL_TREE
;
4617 tree defval
= TREE_PURPOSE (parm
);
4618 tree constr
= TREE_TYPE (parm
);
4622 parm
= TREE_VALUE (parm
);
4624 SET_DECL_TEMPLATE_PARM_P (parm
);
4626 if (TREE_TYPE (parm
) != error_mark_node
)
4630 The top-level cv-qualifiers on the template-parameter are
4631 ignored when determining its type. */
4632 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4633 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4634 TREE_TYPE (parm
) = error_mark_node
;
4635 else if (uses_parameter_packs (TREE_TYPE (parm
))
4636 && !is_parameter_pack
4637 /* If we're in a nested template parameter list, the template
4638 template parameter could be a parameter pack. */
4639 && processing_template_parmlist
== 1)
4641 /* This template parameter is not a parameter pack, but it
4642 should be. Complain about "bare" parameter packs. */
4643 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4645 /* Recover by calling this a parameter pack. */
4646 is_parameter_pack
= true;
4650 /* A template parameter is not modifiable. */
4651 TREE_CONSTANT (parm
) = 1;
4652 TREE_READONLY (parm
) = 1;
4653 decl
= build_decl (parm_loc
,
4654 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4655 TREE_CONSTANT (decl
) = 1;
4656 TREE_READONLY (decl
) = 1;
4657 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4658 = build_template_parm_index (idx
, current_template_depth
,
4659 current_template_depth
,
4660 decl
, TREE_TYPE (parm
));
4662 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4663 = is_parameter_pack
;
4668 parm
= TREE_VALUE (TREE_VALUE (parm
));
4670 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4672 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4673 /* This is for distinguishing between real templates and template
4674 template parameters */
4675 TREE_TYPE (parm
) = t
;
4677 /* any_template_parm_r expects to be able to get the targs of a
4678 DECL_TEMPLATE_RESULT. */
4679 tree result
= DECL_TEMPLATE_RESULT (parm
);
4680 TREE_TYPE (result
) = t
;
4681 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4682 tree tinfo
= build_template_info (parm
, args
);
4683 retrofit_lang_decl (result
);
4684 DECL_TEMPLATE_INFO (result
) = tinfo
;
4690 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4691 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4692 decl
= build_decl (parm_loc
,
4693 TYPE_DECL
, parm
, t
);
4696 TYPE_NAME (t
) = decl
;
4697 TYPE_STUB_DECL (t
) = decl
;
4699 TEMPLATE_TYPE_PARM_INDEX (t
)
4700 = build_template_parm_index (idx
, current_template_depth
,
4701 current_template_depth
,
4702 decl
, TREE_TYPE (parm
));
4703 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4704 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4706 DECL_ARTIFICIAL (decl
) = 1;
4707 SET_DECL_TEMPLATE_PARM_P (decl
);
4709 /* Build requirements for the type/template parameter.
4710 This must be done after SET_DECL_TEMPLATE_PARM_P or
4711 process_template_parm could fail. */
4712 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4714 decl
= pushdecl (decl
);
4718 /* Build the parameter node linking the parameter declaration,
4719 its default argument (if any), and its constraints (if any). */
4720 parm
= build_tree_list (defval
, parm
);
4721 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4724 TREE_CHAIN (prev
) = parm
;
4731 /* The end of a template parameter list has been reached. Process the
4732 tree list into a parameter vector, converting each parameter into a more
4733 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4737 end_template_parm_list (tree parms
)
4739 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4741 /* Pop the dummy parameter level and add the real one. We do not
4742 morph the dummy parameter in place, as it might have been
4743 captured by a (nested) template-template-parm. */
4744 current_template_parms
= TREE_CHAIN (current_template_parms
);
4746 current_template_parms
4747 = tree_cons (size_int (current_template_depth
+ 1),
4748 saved_parmlist
, current_template_parms
);
4750 for (unsigned ix
= 0; parms
; ix
++)
4753 parms
= TREE_CHAIN (parms
);
4754 TREE_CHAIN (parm
) = NULL_TREE
;
4756 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4759 --processing_template_parmlist
;
4761 return saved_parmlist
;
4764 // Explicitly indicate the end of the template parameter list. We assume
4765 // that the current template parameters have been constructed and/or
4766 // managed explicitly, as when creating new template template parameters
4767 // from a shorthand constraint.
4769 end_template_parm_list ()
4771 --processing_template_parmlist
;
4774 /* end_template_decl is called after a template declaration is seen. */
4777 end_template_decl (void)
4779 reset_specialization ();
4781 if (! processing_template_decl
)
4784 /* This matches the pushlevel in begin_template_parm_list. */
4787 --processing_template_decl
;
4788 current_template_parms
= TREE_CHAIN (current_template_parms
);
4791 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4792 thereof, and converts it into an argument suitable to be passed to
4793 the type substitution functions. Note that if the TREE_LIST contains
4794 an error_mark node, the returned argument is error_mark_node. */
4797 template_parm_to_arg (tree t
)
4802 if (TREE_CODE (t
) == TREE_LIST
)
4805 if (error_operand_p (t
))
4806 return error_mark_node
;
4808 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4810 if (TREE_CODE (t
) == TYPE_DECL
4811 || TREE_CODE (t
) == TEMPLATE_DECL
)
4814 t
= DECL_INITIAL (t
);
4817 gcc_assert (TEMPLATE_PARM_P (t
));
4819 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4820 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4822 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4824 /* Turn this argument into a TYPE_ARGUMENT_PACK
4825 with a single element, which expands T. */
4826 tree vec
= make_tree_vec (1);
4828 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4830 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4832 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4833 ARGUMENT_PACK_ARGS (t
) = vec
;
4838 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4840 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4841 with a single element, which expands T. */
4842 tree vec
= make_tree_vec (1);
4844 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4846 t
= convert_from_reference (t
);
4847 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4849 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4850 ARGUMENT_PACK_ARGS (t
) = vec
;
4853 t
= convert_from_reference (t
);
4858 /* If T looks like a generic template argument produced by template_parm_to_arg,
4859 return the corresponding template parameter, otherwise return NULL_TREE. */
4862 template_arg_to_parm (tree t
)
4867 if (ARGUMENT_PACK_P (t
))
4869 tree args
= ARGUMENT_PACK_ARGS (t
);
4870 if (TREE_VEC_LENGTH (args
) == 1
4871 && PACK_EXPANSION_P (TREE_VEC_ELT (args
, 0)))
4872 t
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args
, 0));
4875 if (REFERENCE_REF_P (t
))
4876 t
= TREE_OPERAND (t
, 0);
4878 if (TEMPLATE_PARM_P (t
))
4884 /* Given a single level of template parameters (a TREE_VEC), return it
4885 as a set of template arguments. */
4888 template_parms_level_to_args (tree parms
)
4890 parms
= copy_node (parms
);
4891 TREE_TYPE (parms
) = NULL_TREE
;
4892 for (tree
& parm
: tree_vec_range (parms
))
4893 parm
= template_parm_to_arg (parm
);
4896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms
, TREE_VEC_LENGTH (parms
));
4901 /* Given a set of template parameters, return them as a set of template
4902 arguments. The template parameters are represented as a TREE_VEC, in
4903 the form documented in cp-tree.h for template arguments. */
4906 template_parms_to_args (tree parms
)
4909 tree args
= NULL_TREE
;
4910 int length
= TMPL_PARMS_DEPTH (parms
);
4913 /* If there is only one level of template parameters, we do not
4914 create a TREE_VEC of TREE_VECs. Instead, we return a single
4915 TREE_VEC containing the arguments. */
4917 args
= make_tree_vec (length
);
4919 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4921 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4924 TREE_VEC_ELT (args
, --l
) = a
;
4932 /* Within the declaration of a template, return the currently active
4933 template parameters as an argument TREE_VEC. */
4936 current_template_args (void)
4938 return template_parms_to_args (current_template_parms
);
4941 /* Return the fully generic arguments for of TMPL, i.e. what
4942 current_template_args would be while parsing it. */
4945 generic_targs_for (tree tmpl
)
4947 if (tmpl
== NULL_TREE
)
4949 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4950 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4951 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4952 template parameter, it has no TEMPLATE_INFO; for a partial
4953 specialization, it has the arguments for the primary template, and we
4954 want the arguments for the partial specialization. */;
4955 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4956 if (tree ti
= get_template_info (result
))
4957 return TI_ARGS (ti
);
4958 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4961 /* Return the template arguments corresponding to the template parameters of
4962 TMPL's enclosing scope. When TMPL is a member of a partial specialization,
4963 this returns the arguments for the partial specialization as opposed to those
4964 for the primary template, which is the main difference between this function
4965 and simply using e.g. the TYPE_TI_ARGS of TMPL's DECL_CONTEXT. */
4968 outer_template_args (tree tmpl
)
4970 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (tmpl
));
4973 tree args
= TI_ARGS (ti
);
4974 if (!PRIMARY_TEMPLATE_P (tmpl
))
4976 if (TMPL_ARGS_DEPTH (args
) == 1)
4978 args
= copy_node (args
);
4979 --TREE_VEC_LENGTH (args
);
4983 /* Update the declared TYPE by doing any lookups which were thought to be
4984 dependent, but are not now that we know the SCOPE of the declarator. */
4987 maybe_update_decl_type (tree orig_type
, tree scope
)
4989 tree type
= orig_type
;
4991 if (type
== NULL_TREE
)
4994 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4995 type
= TREE_TYPE (type
);
4997 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4998 && dependent_type_p (type
)
4999 /* Don't bother building up the args in this case. */
5000 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
5002 /* tsubst in the args corresponding to the template parameters,
5003 including auto if present. Most things will be unchanged, but
5004 make_typename_type and tsubst_qualified_id will resolve
5005 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
5006 tree args
= current_template_args ();
5007 tree auto_node
= type_uses_auto (type
);
5011 tree auto_vec
= make_tree_vec (1);
5012 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
5013 args
= add_to_template_args (args
, auto_vec
);
5015 pushed
= push_scope (scope
);
5016 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
5021 if (type
== error_mark_node
)
5024 if (TREE_CODE (orig_type
) == TYPE_DECL
)
5026 if (same_type_p (type
, TREE_TYPE (orig_type
)))
5029 type
= TYPE_NAME (type
);
5034 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5035 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5036 the new template is a member template. */
5039 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
5041 gcc_checking_assert (TREE_CODE (decl
) != TEMPLATE_DECL
);
5043 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
5044 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
5045 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
5046 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5047 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
5048 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5049 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
5050 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
5052 /* Propagate module information from the decl. */
5053 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
5058 struct template_parm_data
5060 /* The level of the template parameters we are currently
5064 /* The index of the specialization argument we are currently
5068 /* An array whose size is the number of template parameters. The
5069 elements are nonzero if the parameter has been used in any one
5070 of the arguments processed so far. */
5073 /* An array whose size is the number of template arguments. The
5074 elements are nonzero if the argument makes use of template
5075 parameters of this level. */
5076 int* arg_uses_template_parms
;
5079 /* Subroutine of push_template_decl used to see if each template
5080 parameter in a partial specialization is used in the explicit
5081 argument list. If T is of the LEVEL given in DATA (which is
5082 treated as a template_parm_data*), then DATA->PARMS is marked
5086 mark_template_parm (tree t
, void* data
)
5090 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
5092 template_parm_level_and_index (t
, &level
, &idx
);
5094 if (level
== tpd
->level
)
5096 tpd
->parms
[idx
] = 1;
5097 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
5100 /* In C++17 the type of a non-type argument is a deduced context. */
5101 if (cxx_dialect
>= cxx17
5102 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5103 for_each_template_parm (TREE_TYPE (t
),
5104 &mark_template_parm
,
5107 /*include_nondeduced_p=*/false);
5109 /* Return zero so that for_each_template_parm will continue the
5110 traversal of the tree; we want to mark *every* template parm. */
5114 /* Process the partial specialization DECL. */
5117 process_partial_specialization (tree decl
)
5119 tree type
= TREE_TYPE (decl
);
5120 tree tinfo
= get_template_info (decl
);
5121 tree maintmpl
= TI_TEMPLATE (tinfo
);
5122 tree specargs
= TI_ARGS (tinfo
);
5123 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5124 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5127 int nargs
= TREE_VEC_LENGTH (inner_args
);
5130 bool did_error_intro
= false;
5131 struct template_parm_data tpd
;
5132 struct template_parm_data tpd2
;
5134 gcc_assert (current_template_parms
);
5136 /* A concept cannot be specialized. */
5137 if (flag_concepts
&& variable_concept_p (maintmpl
))
5139 error ("specialization of variable concept %q#D", maintmpl
);
5140 return error_mark_node
;
5143 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5144 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5146 /* We check that each of the template parameters given in the
5147 partial specialization is used in the argument list to the
5148 specialization. For example:
5150 template <class T> struct S;
5151 template <class T> struct S<T*>;
5153 The second declaration is OK because `T*' uses the template
5154 parameter T, whereas
5156 template <class T> struct S<int>;
5158 is no good. Even trickier is:
5169 The S2<T> declaration is actually invalid; it is a
5170 full-specialization. Of course,
5173 struct S2<T (*)(U)>;
5175 or some such would have been OK. */
5176 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5177 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5178 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5180 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5181 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5182 for (i
= 0; i
< nargs
; ++i
)
5184 tpd
.current_arg
= i
;
5185 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5186 &mark_template_parm
,
5189 /*include_nondeduced_p=*/false);
5191 for (i
= 0; i
< ntparms
; ++i
)
5192 if (tpd
.parms
[i
] == 0)
5194 /* One of the template parms was not used in a deduced context in the
5196 if (!did_error_intro
)
5198 error ("template parameters not deducible in "
5199 "partial specialization:");
5200 did_error_intro
= true;
5203 inform (input_location
, " %qD",
5204 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5207 if (did_error_intro
)
5208 return error_mark_node
;
5210 /* [temp.class.spec]
5212 The argument list of the specialization shall not be identical to
5213 the implicit argument list of the primary template. */
5215 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5216 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5218 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5221 error ("partial specialization %q+D does not specialize "
5222 "any template arguments; to define the primary template, "
5223 "remove the template argument list", decl
);
5225 error ("partial specialization %q+D does not specialize any "
5226 "template arguments and is not more constrained than "
5227 "the primary template; to define the primary template, "
5228 "remove the template argument list", decl
);
5229 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5232 /* A partial specialization that replaces multiple parameters of the
5233 primary template with a pack expansion is less specialized for those
5235 if (nargs
< DECL_NTPARMS (maintmpl
))
5237 error ("partial specialization is not more specialized than the "
5238 "primary template because it replaces multiple parameters "
5239 "with a pack expansion");
5240 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5241 /* Avoid crash in process_partial_specialization. */
5245 else if (nargs
> DECL_NTPARMS (maintmpl
))
5247 error ("too many arguments for partial specialization %qT", type
);
5248 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5249 /* Avoid crash below. */
5253 /* If we aren't in a dependent class, we can actually try deduction. */
5254 else if (tpd
.level
== 1
5255 /* FIXME we should be able to handle a partial specialization of a
5256 partial instantiation, but currently we can't (c++/41727). */
5257 && TMPL_ARGS_DEPTH (specargs
) == 1
5258 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5260 auto_diagnostic_group d
;
5261 if (pedwarn (input_location
, 0,
5262 "partial specialization %qD is not more specialized than",
5264 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5268 /* [temp.spec.partial]
5270 The type of a template parameter corresponding to a specialized
5271 non-type argument shall not be dependent on a parameter of the
5274 Also, we verify that pack expansions only occur at the
5275 end of the argument list. */
5277 for (i
= 0; i
< nargs
; ++i
)
5279 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5280 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5281 tree packed_args
= NULL_TREE
;
5284 if (ARGUMENT_PACK_P (arg
))
5286 /* Extract the arguments from the argument pack. We'll be
5287 iterating over these in the following loop. */
5288 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5289 len
= TREE_VEC_LENGTH (packed_args
);
5292 for (j
= 0; j
< len
; j
++)
5295 /* Get the Jth argument in the parameter pack. */
5296 arg
= TREE_VEC_ELT (packed_args
, j
);
5298 if (PACK_EXPANSION_P (arg
))
5300 /* Pack expansions must come at the end of the
5302 if ((packed_args
&& j
< len
- 1)
5303 || (!packed_args
&& i
< nargs
- 1))
5305 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5306 error ("parameter pack argument %qE must be at the "
5307 "end of the template argument list", arg
);
5309 error ("parameter pack argument %qT must be at the "
5310 "end of the template argument list", arg
);
5314 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5315 /* We only care about the pattern. */
5316 arg
= PACK_EXPANSION_PATTERN (arg
);
5318 if (/* These first two lines are the `non-type' bit. */
5320 && TREE_CODE (arg
) != TEMPLATE_DECL
5321 /* This next two lines are the `argument expression is not just a
5322 simple identifier' condition and also the `specialized
5323 non-type argument' bit. */
5324 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5325 && !((REFERENCE_REF_P (arg
)
5326 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5327 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5329 /* Look at the corresponding template parameter,
5330 marking which template parameters its type depends
5332 tree type
= TREE_TYPE (parm
);
5336 /* We haven't yet initialized TPD2. Do so now. */
5337 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5338 /* The number of parameters here is the number in the
5339 main template, which, as checked in the assertion
5341 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5343 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5346 /* Mark the template parameters. But this time, we're
5347 looking for the template parameters of the main
5348 template, not in the specialization. */
5349 tpd2
.current_arg
= i
;
5350 tpd2
.arg_uses_template_parms
[i
] = 0;
5351 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5352 for_each_template_parm (type
,
5353 &mark_template_parm
,
5356 /*include_nondeduced_p=*/false);
5358 if (tpd2
.arg_uses_template_parms
[i
])
5360 /* The type depended on some template parameters.
5361 If they are fully specialized in the
5362 specialization, that's OK. */
5365 for (j
= 0; j
< nargs
; ++j
)
5366 if (tpd2
.parms
[j
] != 0
5367 && tpd
.arg_uses_template_parms
[j
])
5370 error_n (input_location
, count
,
5371 "type %qT of template argument %qE depends "
5372 "on a template parameter",
5373 "type %qT of template argument %qE depends "
5374 "on template parameters",
5382 /* We should only get here once. */
5383 if (TREE_CODE (decl
) == TYPE_DECL
)
5384 gcc_assert (!COMPLETE_TYPE_P (type
));
5386 // Build the template decl.
5387 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5388 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5389 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5390 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5391 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5393 /* Give template template parms a DECL_CONTEXT of the template
5394 for which they are a parameter. */
5395 for (i
= 0; i
< ntparms
; ++i
)
5397 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5398 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5399 DECL_CONTEXT (parm
) = tmpl
;
5403 /* We didn't register this in check_explicit_specialization so we could
5404 wait until the constraints were set. */
5405 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5407 associate_classtype_constraints (type
);
5409 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5410 = tree_cons (specargs
, tmpl
,
5411 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5412 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5414 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5415 inst
= TREE_CHAIN (inst
))
5417 tree instance
= TREE_VALUE (inst
);
5418 if (TYPE_P (instance
)
5419 ? (COMPLETE_TYPE_P (instance
)
5420 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5421 : DECL_TEMPLATE_INSTANTIATION (instance
))
5423 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5424 tree inst_decl
= (DECL_P (instance
)
5425 ? instance
: TYPE_NAME (instance
));
5428 else if (spec
== error_mark_node
)
5429 permerror (input_location
,
5430 "declaration of %qD ambiguates earlier template "
5431 "instantiation for %qD", decl
, inst_decl
);
5432 else if (TREE_VALUE (spec
) == tmpl
)
5433 permerror (input_location
,
5434 "partial specialization of %qD after instantiation "
5435 "of %qD", decl
, inst_decl
);
5442 /* PARM is a template parameter of some form; return the corresponding
5443 TEMPLATE_PARM_INDEX. */
5446 get_template_parm_index (tree parm
)
5448 if (TREE_CODE (parm
) == PARM_DECL
5449 || TREE_CODE (parm
) == CONST_DECL
)
5450 parm
= DECL_INITIAL (parm
);
5451 else if (TREE_CODE (parm
) == TYPE_DECL
5452 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5453 parm
= TREE_TYPE (parm
);
5454 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5455 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5456 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5457 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5458 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5462 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5463 parameter packs used by the template parameter PARM. */
5466 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5468 /* A type parm can't refer to another parm. */
5469 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5471 else if (TREE_CODE (parm
) == PARM_DECL
)
5473 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5478 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5480 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5481 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5483 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5484 if (template_parameter_pack_p (p
))
5485 /* Any packs in the type are expanded by this parameter. */;
5487 fixed_parameter_pack_p_1 (p
, ppd
);
5491 /* PARM is a template parameter pack. Return any parameter packs used in
5492 its type or the type of any of its template parameters. If there are
5493 any such packs, it will be instantiated into a fixed template parameter
5494 list by partial instantiation rather than be fully deduced. */
5497 fixed_parameter_pack_p (tree parm
)
5499 /* This can only be true in a member template. */
5500 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5502 /* This can only be true for a parameter pack. */
5503 if (!template_parameter_pack_p (parm
))
5505 /* A type parm can't refer to another parm. */
5506 if (TREE_CODE (parm
) == TYPE_DECL
)
5509 tree parameter_packs
= NULL_TREE
;
5510 struct find_parameter_pack_data ppd
;
5511 ppd
.parameter_packs
= ¶meter_packs
;
5512 ppd
.visited
= new hash_set
<tree
>;
5513 ppd
.type_pack_expansion_p
= false;
5515 fixed_parameter_pack_p_1 (parm
, &ppd
);
5518 return parameter_packs
;
5521 /* Check that a template declaration's use of default arguments and
5522 parameter packs is not invalid. Here, PARMS are the template
5523 parameters. IS_PRIMARY is true if DECL is the thing declared by
5524 a primary template. IS_PARTIAL is true if DECL is a partial
5527 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5528 function template declaration or a friend class template
5529 declaration. In the function case, 1 indicates a declaration, 2
5530 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5531 emitted for extraneous default arguments.
5533 Returns TRUE if there were no errors found, FALSE otherwise. */
5536 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5537 bool is_partial
, int is_friend_decl
)
5540 int last_level_to_check
;
5542 bool no_errors
= true;
5546 A default template-argument shall not be specified in a
5547 function template declaration or a function template definition, nor
5548 in the template-parameter-list of the definition of a member of a
5551 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5552 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5553 /* You can't have a function template declaration in a local
5554 scope, nor you can you define a member of a class template in a
5558 if ((TREE_CODE (decl
) == TYPE_DECL
5560 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5561 || (TREE_CODE (decl
) == FUNCTION_DECL
5562 && LAMBDA_FUNCTION_P (decl
)))
5563 /* A lambda doesn't have an explicit declaration; don't complain
5564 about the parms of the enclosing class. */
5567 if (current_class_type
5568 && !TYPE_BEING_DEFINED (current_class_type
)
5569 && DECL_LANG_SPECIFIC (decl
)
5570 && DECL_DECLARES_FUNCTION_P (decl
)
5571 /* If this is either a friend defined in the scope of the class
5572 or a member function. */
5573 && (DECL_FUNCTION_MEMBER_P (decl
)
5574 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5575 : DECL_FRIEND_CONTEXT (decl
)
5576 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5578 /* And, if it was a member function, it really was defined in
5579 the scope of the class. */
5580 && (!DECL_FUNCTION_MEMBER_P (decl
)
5581 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5582 /* We already checked these parameters when the template was
5583 declared, so there's no need to do it again now. This function
5584 was defined in class scope, but we're processing its body now
5585 that the class is complete. */
5588 /* Core issue 226 (C++0x only): the following only applies to class
5591 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5595 If a template-parameter has a default template-argument, all
5596 subsequent template-parameters shall have a default
5597 template-argument supplied. */
5598 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5600 tree inner_parms
= TREE_VALUE (parm_level
);
5601 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5602 int seen_def_arg_p
= 0;
5605 for (i
= 0; i
< ntparms
; ++i
)
5607 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5609 if (parm
== error_mark_node
)
5612 if (TREE_PURPOSE (parm
))
5614 else if (seen_def_arg_p
5615 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5617 error ("no default argument for %qD", TREE_VALUE (parm
));
5618 /* For better subsequent error-recovery, we indicate that
5619 there should have been a default argument. */
5620 TREE_PURPOSE (parm
) = error_mark_node
;
5623 else if (!is_partial
5625 /* Don't complain about an enclosing partial
5627 && parm_level
== parms
5628 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5630 && template_parameter_pack_p (TREE_VALUE (parm
))
5631 /* A fixed parameter pack will be partially
5632 instantiated into a fixed length list. */
5633 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5635 /* A primary class template, primary variable template
5636 (DR 2032), or alias template can only have one
5637 parameter pack, at the end of the template
5640 error ("parameter pack %q+D must be at the end of the"
5641 " template parameter list", TREE_VALUE (parm
));
5643 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5651 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5655 /* For an ordinary class template, default template arguments are
5656 allowed at the innermost level, e.g.:
5657 template <class T = int>
5659 but, in a partial specialization, they're not allowed even
5660 there, as we have in [temp.class.spec]:
5662 The template parameter list of a specialization shall not
5663 contain default template argument values.
5665 So, for a partial specialization, or for a function template
5666 (in C++98/C++03), we look at all of them. */
5669 /* But, for a primary class template that is not a partial
5670 specialization we look at all template parameters except the
5672 parms
= TREE_CHAIN (parms
);
5674 /* Figure out what error message to issue. */
5675 if (is_friend_decl
== 2)
5676 msg
= G_("default template arguments may not be used in function template "
5677 "friend re-declaration");
5678 else if (is_friend_decl
)
5679 msg
= G_("default template arguments may not be used in template "
5680 "friend declarations");
5681 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5682 msg
= G_("default template arguments may not be used in function templates "
5683 "without %<-std=c++11%> or %<-std=gnu++11%>");
5684 else if (is_partial
)
5685 msg
= G_("default template arguments may not be used in "
5686 "partial specializations");
5687 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5688 msg
= G_("default argument for template parameter for class enclosing %qD");
5690 /* Per [temp.param]/9, "A default template-argument shall not be
5691 specified in the template-parameter-lists of the definition of
5692 a member of a class template that appears outside of the member's
5693 class.", thus if we aren't handling a member of a class template
5694 there is no need to examine the parameters. */
5697 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5698 /* If we're inside a class definition, there's no need to
5699 examine the parameters to the class itself. On the one
5700 hand, they will be checked when the class is defined, and,
5701 on the other, default arguments are valid in things like:
5702 template <class T = double>
5703 struct S { template <class U> void f(U); };
5704 Here the default argument for `S' has no bearing on the
5705 declaration of `f'. */
5706 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5708 /* Check everything. */
5709 last_level_to_check
= 0;
5711 for (parm_level
= parms
;
5712 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5713 parm_level
= TREE_CHAIN (parm_level
))
5715 tree inner_parms
= TREE_VALUE (parm_level
);
5719 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5720 for (i
= 0; i
< ntparms
; ++i
)
5722 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5725 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5730 if (is_friend_decl
== 2)
5737 /* Clear out the default argument so that we are not
5739 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5743 /* At this point, if we're still interested in issuing messages,
5744 they must apply to classes surrounding the object declared. */
5746 msg
= G_("default argument for template parameter for class "
5753 /* Worker for push_template_decl_real, called via
5754 for_each_template_parm. DATA is really an int, indicating the
5755 level of the parameters we are interested in. If T is a template
5756 parameter of that level, return nonzero. */
5759 template_parm_this_level_p (tree t
, void* data
)
5761 int this_level
= *(int *)data
;
5764 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5765 level
= TEMPLATE_PARM_LEVEL (t
);
5767 level
= TEMPLATE_TYPE_LEVEL (t
);
5768 return level
== this_level
;
5771 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5772 DATA is really an int, indicating the innermost outer level of parameters.
5773 If T is a template parameter of that level or further out, return
5777 template_parm_outer_level (tree t
, void *data
)
5779 int this_level
= *(int *)data
;
5782 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5783 level
= TEMPLATE_PARM_LEVEL (t
);
5785 level
= TEMPLATE_TYPE_LEVEL (t
);
5786 return level
<= this_level
;
5789 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5790 parameters given by current_template_args, or reuses a
5791 previously existing one, if appropriate. Returns the DECL, or an
5792 equivalent one, if it is replaced via a call to duplicate_decls.
5794 If IS_FRIEND is true, DECL is a friend declaration. */
5797 push_template_decl (tree decl
, bool is_friend
)
5799 if (decl
== error_mark_node
|| !current_template_parms
)
5800 return error_mark_node
;
5802 /* See if this is a partial specialization. */
5803 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5804 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5805 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5807 && DECL_LANG_SPECIFIC (decl
)
5808 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5809 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5811 /* No surprising friend functions. */
5812 gcc_checking_assert (is_friend
5813 || !(TREE_CODE (decl
) == FUNCTION_DECL
5814 && DECL_UNIQUE_FRIEND_P (decl
)));
5818 /* For a friend, we want the context of the friend, not
5819 the type of which it is a friend. */
5820 ctx
= CP_DECL_CONTEXT (decl
);
5821 else if (CP_DECL_CONTEXT (decl
)
5822 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5823 /* In the case of a virtual function, we want the class in which
5825 ctx
= CP_DECL_CONTEXT (decl
);
5827 /* Otherwise, if we're currently defining some class, the DECL
5828 is assumed to be a member of the class. */
5829 ctx
= current_scope ();
5831 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5834 if (!DECL_CONTEXT (decl
))
5835 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5837 /* See if this is a primary template. */
5838 bool is_primary
= false;
5839 if (is_friend
&& ctx
5840 && uses_template_parms_level (ctx
, current_template_depth
))
5841 /* A friend template that specifies a class context, i.e.
5842 template <typename T> friend void A<T>::f();
5845 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5846 /* Lambdas are not primary. */
5849 is_primary
= template_parm_scope_p ();
5851 /* True if the template is a member template, in the sense of
5853 bool member_template_p
= false;
5857 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5859 if (DECL_CLASS_SCOPE_P (decl
))
5860 member_template_p
= true;
5862 if (TREE_CODE (decl
) == TYPE_DECL
5863 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5865 error ("template class without a name");
5866 return error_mark_node
;
5868 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5870 if (member_template_p
)
5872 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5873 error ("member template %qD may not have virt-specifiers", decl
);
5875 if (DECL_DESTRUCTOR_P (decl
))
5879 A destructor shall not be a member template. */
5880 error_at (DECL_SOURCE_LOCATION (decl
),
5881 "destructor %qD declared as member template", decl
);
5882 return error_mark_node
;
5884 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5885 && (!prototype_p (TREE_TYPE (decl
))
5886 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5887 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5888 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5889 == void_list_node
)))
5891 /* [basic.stc.dynamic.allocation]
5893 An allocation function can be a function
5894 template. ... Template allocation functions shall
5895 have two or more parameters. */
5896 error ("invalid template declaration of %qD", decl
);
5897 return error_mark_node
;
5900 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5901 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5902 /* Class template. */;
5903 else if (TREE_CODE (decl
) == TYPE_DECL
5904 && TYPE_DECL_ALIAS_P (decl
))
5905 /* alias-declaration */
5906 gcc_assert (!DECL_ARTIFICIAL (decl
));
5907 else if (VAR_P (decl
))
5908 /* C++14 variable template. */;
5909 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5910 /* C++20 concept definitions. */;
5913 error ("template declaration of %q#D", decl
);
5914 return error_mark_node
;
5918 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5919 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5920 || (VAR_OR_FUNCTION_DECL_P (decl
)
5921 && DECL_LOCAL_DECL_P (decl
))));
5923 /* Check to see that the rules regarding the use of default
5924 arguments are not being violated. We check args for a friend
5925 functions when we know whether it's a definition, introducing
5926 declaration or re-declaration. */
5927 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5928 check_default_tmpl_args (decl
, current_template_parms
,
5929 is_primary
, is_partial
, is_friend
);
5931 /* Ensure that there are no parameter packs in the type of this
5932 declaration that have not been expanded. */
5933 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5935 /* Check each of the arguments individually to see if there are
5936 any bare parameter packs. */
5937 tree type
= TREE_TYPE (decl
);
5938 tree arg
= DECL_ARGUMENTS (decl
);
5939 tree argtype
= TYPE_ARG_TYPES (type
);
5941 while (arg
&& argtype
)
5943 if (!DECL_PACK_P (arg
)
5944 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5946 /* This is a PARM_DECL that contains unexpanded parameter
5947 packs. We have already complained about this in the
5948 check_for_bare_parameter_packs call, so just replace
5949 these types with ERROR_MARK_NODE. */
5950 TREE_TYPE (arg
) = error_mark_node
;
5951 TREE_VALUE (argtype
) = error_mark_node
;
5954 arg
= DECL_CHAIN (arg
);
5955 argtype
= TREE_CHAIN (argtype
);
5958 /* Check for bare parameter packs in the return type and the
5959 exception specifiers. */
5960 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5961 /* Errors were already issued, set return type to int
5962 as the frontend doesn't expect error_mark_node as
5964 TREE_TYPE (type
) = integer_type_node
;
5965 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5966 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5970 if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5971 ? DECL_ORIGINAL_TYPE (decl
)
5972 : TREE_TYPE (decl
)))
5974 TREE_TYPE (decl
) = error_mark_node
;
5975 return error_mark_node
;
5978 if (is_partial
&& VAR_P (decl
)
5979 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl
)))
5980 return error_mark_node
;
5984 return process_partial_specialization (decl
);
5986 tree args
= current_template_args ();
5987 tree tmpl
= NULL_TREE
;
5988 bool new_template_p
= false;
5991 /* Does not get a template head. */
5993 gcc_checking_assert (!is_primary
);
5996 || TREE_CODE (ctx
) == FUNCTION_DECL
5997 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5998 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5999 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
6000 && DECL_TEMPLATE_INFO (decl
))))
6002 if (DECL_LANG_SPECIFIC (decl
)
6003 && DECL_TEMPLATE_INFO (decl
)
6004 && DECL_TI_TEMPLATE (decl
))
6005 tmpl
= DECL_TI_TEMPLATE (decl
);
6006 /* If DECL is a TYPE_DECL for a class-template, then there won't
6007 be DECL_LANG_SPECIFIC. The information equivalent to
6008 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6009 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
6010 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
6011 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
6013 /* Since a template declaration already existed for this
6014 class-type, we must be redeclaring it here. Make sure
6015 that the redeclaration is valid. */
6016 redeclare_class_template (TREE_TYPE (decl
),
6017 current_template_parms
,
6018 current_template_constraints ());
6019 /* We don't need to create a new TEMPLATE_DECL; just use the
6020 one we already had. */
6021 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
6025 tmpl
= build_template_decl (decl
, current_template_parms
,
6027 new_template_p
= true;
6029 if (DECL_LANG_SPECIFIC (decl
)
6030 && DECL_TEMPLATE_SPECIALIZATION (decl
))
6032 /* A specialization of a member template of a template
6034 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
6035 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
6036 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
6042 tree a
, t
, current
, parms
;
6044 tree tinfo
= get_template_info (decl
);
6048 error ("template definition of non-template %q#D", decl
);
6049 return error_mark_node
;
6052 tmpl
= TI_TEMPLATE (tinfo
);
6054 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
6055 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
6056 && DECL_TEMPLATE_SPECIALIZATION (decl
)
6057 && DECL_MEMBER_TEMPLATE_P (tmpl
))
6059 /* The declaration is a specialization of a member
6060 template, declared outside the class. Therefore, the
6061 innermost template arguments will be NULL, so we
6062 replace them with the arguments determined by the
6063 earlier call to check_explicit_specialization. */
6064 args
= DECL_TI_ARGS (decl
);
6067 = build_template_decl (decl
, current_template_parms
,
6069 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
6070 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
6071 DECL_TEMPLATE_INFO (new_tmpl
)
6072 = build_template_info (tmpl
, args
);
6074 register_specialization (new_tmpl
,
6075 most_general_template (tmpl
),
6081 /* Make sure the template headers we got make sense. */
6083 parms
= DECL_TEMPLATE_PARMS (tmpl
);
6084 i
= TMPL_PARMS_DEPTH (parms
);
6085 if (TMPL_ARGS_DEPTH (args
) != i
)
6087 error ("expected %d levels of template parms for %q#D, got %d",
6088 i
, decl
, TMPL_ARGS_DEPTH (args
));
6089 DECL_INTERFACE_KNOWN (decl
) = 1;
6090 return error_mark_node
;
6093 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
6095 a
= TMPL_ARGS_LEVEL (args
, i
);
6096 t
= INNERMOST_TEMPLATE_PARMS (parms
);
6098 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
6100 if (current
== decl
)
6101 error ("got %d template parameters for %q#D",
6102 TREE_VEC_LENGTH (a
), decl
);
6104 error ("got %d template parameters for %q#T",
6105 TREE_VEC_LENGTH (a
), current
);
6106 error (" but %d required", TREE_VEC_LENGTH (t
));
6107 /* Avoid crash in import_export_decl. */
6108 DECL_INTERFACE_KNOWN (decl
) = 1;
6109 return error_mark_node
;
6112 if (current
== decl
)
6114 else if (current
== NULL_TREE
)
6115 /* Can happen in erroneous input. */
6118 current
= get_containing_scope (current
);
6121 /* Check that the parms are used in the appropriate qualifying scopes
6122 in the declarator. */
6123 if (!comp_template_args
6125 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6127 error ("template arguments to %qD do not match original "
6128 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6129 if (!uses_template_parms (TI_ARGS (tinfo
)))
6130 inform (input_location
, "use %<template<>%> for"
6131 " an explicit specialization");
6132 /* Avoid crash in import_export_decl. */
6133 DECL_INTERFACE_KNOWN (decl
) = 1;
6134 return error_mark_node
;
6138 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6142 /* Push template declarations for global functions and types.
6143 Note that we do not try to push a global template friend
6144 declared in a template class; such a thing may well depend on
6145 the template parameters of the class and we'll push it when
6146 instantiating the befriending class. */
6148 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6150 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6151 if (pushed
== error_mark_node
)
6152 return error_mark_node
;
6154 /* pushdecl may have found an existing template. */
6157 decl
= DECL_TEMPLATE_RESULT (pushed
);
6163 /* Record this decl as belonging to the current class. It's
6164 not chained onto anything else. */
6165 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6166 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6167 DECL_CHAIN (tmpl
) = current_scope ();
6171 /* The type may have been completed, or (erroneously) changed. */
6172 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6178 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6180 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6182 /* Give template template parms a DECL_CONTEXT of the template
6183 for which they are a parameter. */
6184 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6185 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6187 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6188 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6189 DECL_CONTEXT (parm
) = tmpl
;
6192 if (TREE_CODE (decl
) == TYPE_DECL
6193 && TYPE_DECL_ALIAS_P (decl
))
6196 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6198 /* ??? Why don't we do this here for all templates? */
6199 constr
= build_constraints (constr
, NULL_TREE
);
6200 set_constraints (decl
, constr
);
6202 if (complex_alias_template_p (tmpl
))
6203 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6207 /* The DECL_TI_ARGS of DECL contains full set of arguments
6208 referring wback to its most general template. If TMPL is a
6209 specialization, ARGS may only have the innermost set of
6210 arguments. Add the missing argument levels if necessary. */
6211 if (DECL_TEMPLATE_INFO (tmpl
))
6212 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6214 tree info
= build_template_info (tmpl
, args
);
6216 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6217 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6220 retrofit_lang_decl (decl
);
6221 DECL_TEMPLATE_INFO (decl
) = info
;
6225 if (flag_implicit_templates
6227 && TREE_PUBLIC (decl
)
6228 && VAR_OR_FUNCTION_DECL_P (decl
))
6229 /* Set DECL_COMDAT on template instantiations; if we force
6230 them to be emitted by explicit instantiation,
6231 mark_needed will tell cgraph to do the right thing. */
6232 DECL_COMDAT (decl
) = true;
6234 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6239 /* FN is an inheriting constructor that inherits from the constructor
6240 template INHERITED; turn FN into a constructor template with a matching
6244 add_inherited_template_parms (tree fn
, tree inherited
)
6247 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6248 inner_parms
= copy_node (inner_parms
);
6250 = tree_cons (size_int (current_template_depth
+ 1),
6251 inner_parms
, current_template_parms
);
6252 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6253 tree args
= template_parms_to_args (parms
);
6254 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6255 DECL_ARTIFICIAL (tmpl
) = true;
6256 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6260 /* Called when a class template TYPE is redeclared with the indicated
6261 template PARMS, e.g.:
6263 template <class T> struct S;
6264 template <class T> struct S {}; */
6267 redeclare_class_template (tree type
, tree parms
, tree cons
)
6273 if (!TYPE_TEMPLATE_INFO (type
))
6275 error ("%qT is not a template type", type
);
6279 tmpl
= TYPE_TI_TEMPLATE (type
);
6280 if (!PRIMARY_TEMPLATE_P (tmpl
))
6281 /* The type is nested in some template class. Nothing to worry
6282 about here; there are no new template parameters for the nested
6288 error ("template specifiers not specified in declaration of %qD",
6293 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6294 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6296 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6298 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6299 "redeclared with %d template parameter",
6300 "redeclared with %d template parameters",
6301 TREE_VEC_LENGTH (parms
));
6302 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6303 "previous declaration %qD used %d template parameter",
6304 "previous declaration %qD used %d template parameters",
6305 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6309 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6314 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6315 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6318 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6319 if (error_operand_p (tmpl_parm
))
6322 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6324 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6326 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6327 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6328 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6329 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6330 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6331 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6332 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6333 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6334 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6336 auto_diagnostic_group d
;
6337 error ("template parameter %q+#D", tmpl_parm
);
6339 inform (DECL_SOURCE_LOCATION (parm
), "redeclared here as %q#D", parm
);
6341 inform (input_location
, "redeclared here");
6345 /* The parameters can be declared to introduce different
6347 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6348 tree p2
= TREE_VEC_ELT (parms
, i
);
6349 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6351 auto_diagnostic_group d
;
6352 error ("declaration of template parameter %q+#D with different "
6353 "constraints", parm
);
6354 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6355 "original declaration appeared here");
6359 /* Give each template template parm in this redeclaration a
6360 DECL_CONTEXT of the template for which they are a parameter. */
6361 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6363 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6364 DECL_CONTEXT (parm
) = tmpl
;
6368 if (!merge_default_template_args (parms
, tmpl_parms
, /*class_p=*/true))
6371 tree ci
= get_constraints (tmpl
);
6372 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6373 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6375 /* Two classes with different constraints declare different entities. */
6376 if (!cp_tree_equal (req1
, req2
))
6378 auto_diagnostic_group d
;
6379 error_at (input_location
, "redeclaration %q#D with different "
6380 "constraints", tmpl
);
6381 inform (DECL_SOURCE_LOCATION (tmpl
),
6382 "original declaration appeared here");
6389 /* The actual substitution part of instantiate_non_dependent_expr,
6390 to be used when the caller has already checked
6391 !instantiation_dependent_uneval_expression_p (expr)
6392 and cleared processing_template_decl. */
6395 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6397 return tsubst_copy_and_build (expr
,
6400 /*in_decl=*/NULL_TREE
);
6403 /* Instantiate the non-dependent expression EXPR. */
6406 instantiate_non_dependent_expr (tree expr
,
6407 tsubst_flags_t complain
/* = tf_error */)
6409 if (expr
== NULL_TREE
)
6412 if (processing_template_decl
)
6414 /* The caller should have checked this already. */
6415 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr
));
6416 processing_template_decl_sentinel s
;
6417 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6422 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6423 expression is dependent or non-constant. */
6426 instantiate_non_dependent_or_null (tree expr
)
6428 if (expr
== NULL_TREE
)
6430 if (processing_template_decl
)
6432 if (!is_nondependent_constant_expression (expr
))
6436 processing_template_decl_sentinel s
;
6437 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6443 /* True iff T is a specialization of a variable template. */
6446 variable_template_specialization_p (tree t
)
6448 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6450 tree tmpl
= DECL_TI_TEMPLATE (t
);
6451 return variable_template_p (tmpl
);
6454 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6455 template declaration, or a TYPE_DECL for an alias declaration. */
6458 alias_type_or_template_p (tree t
)
6462 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6465 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6466 || DECL_ALIAS_TEMPLATE_P (t
));
6469 /* If T is a specialization of an alias template, return it; otherwise return
6470 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6473 alias_template_specialization_p (const_tree t
,
6474 bool transparent_typedefs
)
6479 /* It's an alias template specialization if it's an alias and its
6480 TYPE_NAME is a specialization of a primary template. */
6481 if (typedef_variant_p (t
))
6483 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6484 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6485 return CONST_CAST_TREE (t
);
6486 if (transparent_typedefs
)
6487 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6489 transparent_typedefs
);
6495 /* Data structure for complex_alias_template_*. */
6497 struct uses_all_template_parms_data
6503 /* walk_tree callback for complex_alias_template_p. */
6506 complex_alias_template_r (tree
*tp
, int *walk_subtrees
, void *data_
)
6509 auto &data
= *(struct uses_all_template_parms_data
*)data_
;
6511 switch (TREE_CODE (t
))
6513 case TEMPLATE_TYPE_PARM
:
6514 case TEMPLATE_PARM_INDEX
:
6515 case TEMPLATE_TEMPLATE_PARM
:
6516 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6518 tree idx
= get_template_parm_index (t
);
6519 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6520 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6526 if (!PACK_EXPANSION_P (t
))
6529 /* An alias template with a pack expansion that expands a pack from the
6530 enclosing class needs to be considered complex, to avoid confusion with
6531 the same pack being used as an argument to the alias's own template
6532 parameter (91966). */
6533 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6534 pack
= TREE_CHAIN (pack
))
6536 tree parm_pack
= TREE_VALUE (pack
);
6537 if (!TEMPLATE_PARM_P (parm_pack
))
6540 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6541 if (level
< data
.level
)
6544 /* Consider the expanded packs to be used outside the expansion... */
6545 data
.seen
[idx
] = true;
6548 /* ...but don't walk into the pattern. Consider PR104008:
6550 template <typename T, typename... Ts>
6551 using IsOneOf = disjunction<is_same<T, Ts>...>;
6553 where IsOneOf seemingly uses all of its template parameters in its
6554 expansion (and does not expand a pack from the enclosing class), so the
6555 alias was not marked as complex. However, if it is used like
6556 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6557 expansion. So only Ts is considered used by the pack expansion. */
6558 *walk_subtrees
= false;
6563 /* An alias template is complex from a SFINAE perspective if a template-id
6564 using that alias can be ill-formed when the expansion is not, as with
6565 the void_t template.
6567 Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
6568 template arguments are empty packs. */
6571 complex_alias_template_p (const_tree tmpl
)
6573 /* A renaming alias isn't complex. */
6574 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6577 /* Any other constrained alias is complex. */
6578 if (get_constraints (tmpl
))
6581 struct uses_all_template_parms_data data
;
6582 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6583 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6584 data
.level
= TMPL_PARMS_DEPTH (parms
);
6585 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6586 data
.seen
= XALLOCAVEC (bool, len
);
6587 for (int i
= 0; i
< len
; ++i
)
6588 data
.seen
[i
] = false;
6590 if (cp_walk_tree_without_duplicates (&pat
, complex_alias_template_r
, &data
))
6592 for (int i
= 0; i
< len
; ++i
)
6598 /* If T is a specialization of a complex alias template with dependent
6599 template-arguments, return it; otherwise return NULL_TREE. If T is a
6600 typedef to such a specialization, return the specialization. */
6603 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6605 if (t
== error_mark_node
)
6607 gcc_assert (TYPE_P (t
));
6609 if (!typedef_variant_p (t
))
6612 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6614 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6615 && (any_dependent_template_arguments_p
6616 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6617 return CONST_CAST_TREE (t
);
6619 if (transparent_typedefs
)
6621 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6622 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6628 /* Return the number of innermost template parameters in TMPL. */
6631 num_innermost_template_parms (const_tree tmpl
)
6633 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6634 return TREE_VEC_LENGTH (parms
);
6637 /* Return either TMPL or another template that it is equivalent to under DR
6638 1286: An alias that just changes the name of a template is equivalent to
6639 the other template. */
6642 get_underlying_template (tree tmpl
)
6644 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6645 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6647 /* Determine if the alias is equivalent to an underlying template. */
6648 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6649 /* The underlying type may have been ill-formed. Don't proceed. */
6652 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6656 tree underlying
= TI_TEMPLATE (tinfo
);
6657 if (!PRIMARY_TEMPLATE_P (underlying
)
6658 || (num_innermost_template_parms (tmpl
)
6659 != num_innermost_template_parms (underlying
)))
6662 /* Does the alias add cv-quals? */
6663 if (TYPE_QUALS (TREE_TYPE (underlying
)) != TYPE_QUALS (TREE_TYPE (tmpl
)))
6666 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6667 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6670 /* Are any default template arguments equivalent? */
6671 tree aparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6672 tree uparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying
));
6673 const int nparms
= TREE_VEC_LENGTH (aparms
);
6674 for (int i
= 0; i
< nparms
; ++i
)
6676 tree adefarg
= TREE_PURPOSE (TREE_VEC_ELT (aparms
, i
));
6677 tree udefarg
= TREE_PURPOSE (TREE_VEC_ELT (uparms
, i
));
6678 if (!template_args_equal (adefarg
, udefarg
))
6682 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6683 it's appropriate to treat a less-constrained alias as equivalent. */
6684 if (!at_least_as_constrained (underlying
, tmpl
))
6687 /* Alias is equivalent. Strip it and repeat. */
6695 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6696 must be a reference-to-function or a pointer-to-function type, as specified
6697 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6698 and check that the resulting function has external linkage. */
6701 convert_nontype_argument_function (tree type
, tree expr
,
6702 tsubst_flags_t complain
)
6706 linkage_kind linkage
;
6708 fn
= instantiate_type (type
, fns
, tf_none
);
6709 if (fn
== error_mark_node
)
6710 return error_mark_node
;
6712 if (value_dependent_expression_p (fn
))
6716 if (REFERENCE_REF_P (fn_no_ptr
))
6717 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6718 fn_no_ptr
= strip_fnptr_conv (fn_no_ptr
);
6719 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6720 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6721 if (BASELINK_P (fn_no_ptr
))
6722 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6724 /* [temp.arg.nontype]/1
6726 A template-argument for a non-type, non-template template-parameter
6729 -- the address of an object or function with external [C++11: or
6730 internal] linkage. */
6732 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6733 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6735 if (complain
& tf_error
)
6737 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6738 error_at (loc
, "%qE is not a valid template argument for type %qT",
6740 if (TYPE_PTR_P (type
))
6741 inform (loc
, "it must be the address of a function "
6742 "with external linkage");
6744 inform (loc
, "it must be the name of a function with "
6745 "external linkage");
6750 linkage
= decl_linkage (fn_no_ptr
);
6751 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6753 if (complain
& tf_error
)
6755 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6756 if (cxx_dialect
>= cxx11
)
6757 error_at (loc
, "%qE is not a valid template argument for type "
6758 "%qT because %qD has no linkage",
6759 expr
, type
, fn_no_ptr
);
6761 error_at (loc
, "%qE is not a valid template argument for type "
6762 "%qT because %qD does not have external linkage",
6763 expr
, type
, fn_no_ptr
);
6769 if (TYPE_REF_P (type
))
6771 if (REFERENCE_REF_P (fn
))
6772 fn
= TREE_OPERAND (fn
, 0);
6774 fn
= build_address (fn
);
6776 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6777 fn
= build_nop (type
, fn
);
6782 /* Subroutine of convert_nontype_argument.
6783 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6784 Emit an error otherwise. */
6787 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6788 tsubst_flags_t complain
)
6790 tree orig_expr
= expr
;
6792 if (null_ptr_cst_p (expr
))
6794 if (TREE_CODE (expr
) == PTRMEM_CST
6795 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6796 PTRMEM_CST_CLASS (expr
)))
6798 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6800 if (processing_template_decl
6801 && TREE_CODE (expr
) == ADDR_EXPR
6802 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6804 if (complain
& tf_error
)
6806 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6807 error_at (loc
, "%qE is not a valid template argument for type %qT",
6809 if (TREE_CODE (expr
) != PTRMEM_CST
)
6810 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6812 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6817 /* Returns TRUE iff the address of OP is value-dependent.
6819 14.6.2.4 [temp.dep.temp]:
6820 A non-integral non-type template-argument is dependent if its type is
6821 dependent or it has either of the following forms
6824 and contains a nested-name-specifier which specifies a class-name that
6825 names a dependent type.
6827 We generalize this to just say that the address of a member of a
6828 dependent class is value-dependent; the above doesn't cover the
6829 address of a static data member named with an unqualified-id. */
6832 has_value_dependent_address (tree op
)
6834 STRIP_ANY_LOCATION_WRAPPER (op
);
6836 /* We could use get_inner_reference here, but there's no need;
6837 this is only relevant for template non-type arguments, which
6838 can only be expressed as &id-expression. */
6841 tree ctx
= CP_DECL_CONTEXT (op
);
6843 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6848 && TREE_CODE (ctx
) == FUNCTION_DECL
6849 && type_dependent_expression_p (ctx
))
6856 /* The next set of functions are used for providing helpful explanatory
6857 diagnostics for failed overload resolution. Their messages should be
6858 indented by two spaces for consistency with the messages in
6862 unify_success (bool /*explain_p*/)
6867 /* Other failure functions should call this one, to provide a single function
6868 for setting a breakpoint on. */
6871 unify_invalid (bool /*explain_p*/)
6877 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6880 inform (input_location
,
6881 " couldn%'t deduce template parameter %qD", parm
);
6882 return unify_invalid (explain_p
);
6886 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6889 inform (input_location
,
6890 " types %qT and %qT have incompatible cv-qualifiers",
6892 return unify_invalid (explain_p
);
6896 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6899 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6900 return unify_invalid (explain_p
);
6904 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6907 inform (input_location
,
6908 " template parameter %qD is not a parameter pack, but "
6911 return unify_invalid (explain_p
);
6915 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6918 inform (input_location
,
6919 " template argument %qE does not match "
6920 "pointer-to-member constant %qE",
6922 return unify_invalid (explain_p
);
6926 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6929 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6930 return unify_invalid (explain_p
);
6934 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6937 inform (input_location
,
6938 " inconsistent parameter pack deduction with %qT and %qT",
6940 return unify_invalid (explain_p
);
6944 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6949 inform (input_location
,
6950 " deduced conflicting types for parameter %qT (%qT and %qT)",
6951 parm
, first
, second
);
6953 inform (input_location
,
6954 " deduced conflicting values for non-type parameter "
6955 "%qE (%qE and %qE)", parm
, first
, second
);
6957 return unify_invalid (explain_p
);
6961 unify_vla_arg (bool explain_p
, tree arg
)
6964 inform (input_location
,
6965 " variable-sized array type %qT is not "
6966 "a valid template argument",
6968 return unify_invalid (explain_p
);
6972 unify_method_type_error (bool explain_p
, tree arg
)
6975 inform (input_location
,
6976 " member function type %qT is not a valid template argument",
6978 return unify_invalid (explain_p
);
6982 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6987 inform_n (input_location
, wanted
,
6988 " candidate expects at least %d argument, %d provided",
6989 " candidate expects at least %d arguments, %d provided",
6992 inform_n (input_location
, wanted
,
6993 " candidate expects %d argument, %d provided",
6994 " candidate expects %d arguments, %d provided",
6997 return unify_invalid (explain_p
);
7001 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
7003 return unify_arity (explain_p
, have
, wanted
);
7007 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
7008 bool least_p
= false)
7010 return unify_arity (explain_p
, have
, wanted
, least_p
);
7014 unify_arg_conversion (bool explain_p
, tree to_type
,
7015 tree from_type
, tree arg
)
7018 inform (cp_expr_loc_or_input_loc (arg
),
7019 " cannot convert %qE (type %qT) to type %qT",
7020 arg
, from_type
, to_type
);
7021 return unify_invalid (explain_p
);
7025 unify_no_common_base (bool explain_p
, enum template_base_result r
,
7026 tree parm
, tree arg
)
7031 case tbr_ambiguous_baseclass
:
7032 inform (input_location
, " %qT is an ambiguous base class of %qT",
7036 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
7039 return unify_invalid (explain_p
);
7043 unify_inconsistent_template_template_parameters (bool explain_p
)
7046 inform (input_location
,
7047 " template parameters of a template template argument are "
7048 "inconsistent with other deduced template arguments");
7049 return unify_invalid (explain_p
);
7053 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
7056 inform (input_location
,
7057 " cannot deduce a template for %qT from non-template type %qT",
7059 return unify_invalid (explain_p
);
7063 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
7066 inform (input_location
,
7067 " template argument %qE does not match %qE", arg
, parm
);
7068 return unify_invalid (explain_p
);
7071 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7072 argument for TYPE, points to an unsuitable object.
7074 Also adjust the type of the index in C++20 array subobject references. */
7077 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
7079 switch (TREE_CODE (expr
))
7082 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
7086 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
7091 for (auto &e
: CONSTRUCTOR_ELTS (expr
))
7092 if (invalid_tparm_referent_p (TREE_TYPE (e
.value
), e
.value
, complain
))
7099 tree decl
= TREE_OPERAND (expr
, 0);
7101 if (cxx_dialect
>= cxx20
)
7102 while (TREE_CODE (decl
) == COMPONENT_REF
7103 || TREE_CODE (decl
) == ARRAY_REF
)
7105 tree
&op
= TREE_OPERAND (decl
, 1);
7106 if (TREE_CODE (decl
) == ARRAY_REF
7107 && TREE_CODE (op
) == INTEGER_CST
)
7108 /* Canonicalize array offsets to ptrdiff_t; how they were
7109 written doesn't matter for subobject identity. */
7110 op
= fold_convert (ptrdiff_type_node
, op
);
7111 decl
= TREE_OPERAND (decl
, 0);
7116 if (complain
& tf_error
)
7117 error_at (cp_expr_loc_or_input_loc (expr
),
7118 "%qE is not a valid template argument of type %qT "
7119 "because %qE is not a variable", expr
, type
, decl
);
7122 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7124 if (complain
& tf_error
)
7125 error_at (cp_expr_loc_or_input_loc (expr
),
7126 "%qE is not a valid template argument of type %qT "
7127 "in C++98 because %qD does not have external linkage",
7131 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7132 && decl_linkage (decl
) == lk_none
)
7134 if (complain
& tf_error
)
7135 error_at (cp_expr_loc_or_input_loc (expr
),
7136 "%qE is not a valid template argument of type %qT "
7137 "because %qD has no linkage", expr
, type
, decl
);
7140 /* C++17: For a non-type template-parameter of reference or pointer
7141 type, the value of the constant expression shall not refer to (or
7142 for a pointer type, shall not be the address of):
7143 * a subobject (4.5),
7144 * a temporary object (15.2),
7145 * a string literal (5.13.5),
7146 * the result of a typeid expression (8.2.8), or
7147 * a predefined __func__ variable (11.4.1). */
7148 else if (DECL_ARTIFICIAL (decl
))
7150 if (complain
& tf_error
)
7151 error ("the address of %qD is not a valid template argument",
7155 else if (cxx_dialect
< cxx20
7156 && !(same_type_ignoring_top_level_qualifiers_p
7157 (strip_array_types (TREE_TYPE (type
)),
7158 strip_array_types (TREE_TYPE (decl
)))))
7160 if (complain
& tf_error
)
7161 error ("the address of the %qT subobject of %qD is not a "
7162 "valid template argument", TREE_TYPE (type
), decl
);
7165 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7167 if (complain
& tf_error
)
7168 error ("the address of %qD is not a valid template argument "
7169 "because it does not have static storage duration",
7177 if (!INDIRECT_TYPE_P (type
))
7178 /* We're only concerned about pointers and references here. */;
7179 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7180 /* Null pointer values are OK in C++11. */;
7185 if (complain
& tf_error
)
7186 error ("%qD is not a valid template argument "
7187 "because %qD is a variable, not the address of "
7188 "a variable", expr
, expr
);
7193 if (complain
& tf_error
)
7194 error ("%qE is not a valid template argument for %qT "
7195 "because it is not the address of a variable",
7205 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7206 template argument EXPR. */
7209 create_template_parm_object (tree expr
, tsubst_flags_t complain
)
7211 if (TREE_CODE (expr
) == TARGET_EXPR
)
7212 expr
= TARGET_EXPR_INITIAL (expr
);
7214 if (!TREE_CONSTANT (expr
))
7216 if ((complain
& tf_error
)
7217 && require_rvalue_constant_expression (expr
))
7218 cxx_constant_value (expr
);
7219 return error_mark_node
;
7221 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7222 return error_mark_node
;
7224 /* This is no longer a compound literal. */
7225 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr
));
7227 return get_template_parm_object (expr
, mangle_template_parm_object (expr
));
7230 /* The template arguments corresponding to template parameter objects of types
7231 that contain pointers to members. */
7233 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7235 /* Find or build an nttp object for (already-validated) EXPR with name
7239 get_template_parm_object (tree expr
, tree name
)
7241 tree decl
= get_global_binding (name
);
7245 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7246 decl
= create_temporary_var (type
);
7247 DECL_NTTP_OBJECT_P (decl
) = true;
7248 DECL_CONTEXT (decl
) = NULL_TREE
;
7249 TREE_STATIC (decl
) = true;
7250 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7251 TREE_READONLY (decl
) = true;
7252 DECL_NAME (decl
) = name
;
7253 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7254 comdat_linkage (decl
);
7256 if (!zero_init_p (type
))
7258 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7259 lower_var_init before we're done mangling. So store the original
7261 tree copy
= unshare_constructor (expr
);
7262 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7265 pushdecl_top_level_and_finish (decl
, expr
);
7270 /* Return the actual template argument corresponding to template parameter
7274 tparm_object_argument (tree var
)
7276 if (zero_init_p (TREE_TYPE (var
)))
7277 return DECL_INITIAL (var
);
7278 return *(tparm_obj_values
->get (var
));
7281 /* Attempt to convert the non-type template parameter EXPR to the
7282 indicated TYPE. If the conversion is successful, return the
7283 converted value. If the conversion is unsuccessful, return
7284 NULL_TREE if we issued an error message, or error_mark_node if we
7285 did not. We issue error messages for out-and-out bad template
7286 parameters, but not simply because the conversion failed, since we
7287 might be just trying to do argument deduction. Both TYPE and EXPR
7288 must be non-dependent.
7290 The conversion follows the special rules described in
7291 [temp.arg.nontype], and it is much more strict than an implicit
7294 This function is called twice for each template argument (see
7295 lookup_template_class for a more accurate description of this
7296 problem). This means that we need to handle expressions which
7297 are not valid in a C++ source, but can be created from the
7298 first call (for instance, casts to perform conversions). These
7299 hacks can go away after we fix the double coercion problem. */
7302 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7305 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7307 /* Detect immediately string literals as invalid non-type argument.
7308 This special-case is not needed for correctness (we would easily
7309 catch this later), but only to provide better diagnostic for this
7310 common user mistake. As suggested by DR 100, we do not mention
7311 linkage issues in the diagnostic as this is not the point. */
7312 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7314 if (complain
& tf_error
)
7315 error ("%qE is not a valid template argument for type %qT "
7316 "because string literals can never be used in this context",
7321 /* Add the ADDR_EXPR now for the benefit of
7322 value_dependent_expression_p. */
7323 if (TYPE_PTROBV_P (type
)
7324 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7326 expr
= decay_conversion (expr
, complain
);
7327 if (expr
== error_mark_node
)
7328 return error_mark_node
;
7331 /* If we are in a template, EXPR may be non-dependent, but still
7332 have a syntactic, rather than semantic, form. For example, EXPR
7333 might be a SCOPE_REF, rather than the VAR_DECL to which the
7334 SCOPE_REF refers. Preserving the qualifying scope is necessary
7335 so that access checking can be performed when the template is
7336 instantiated -- but here we need the resolved form so that we can
7337 convert the argument. */
7338 bool non_dep
= false;
7339 if (TYPE_REF_OBJ_P (type
)
7340 && has_value_dependent_address (expr
))
7341 /* If we want the address and it's value-dependent, don't fold. */;
7342 else if (processing_template_decl
7343 && !instantiation_dependent_expression_p (expr
))
7345 if (error_operand_p (expr
))
7346 return error_mark_node
;
7347 expr_type
= TREE_TYPE (expr
);
7349 /* If the argument is non-dependent, perform any conversions in
7350 non-dependent context as well. */
7351 processing_template_decl_sentinel
s (non_dep
);
7353 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7355 bool val_dep_p
= value_dependent_expression_p (expr
);
7357 expr
= canonicalize_expr_argument (expr
, complain
);
7359 STRIP_ANY_LOCATION_WRAPPER (expr
);
7361 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7362 to a non-type argument of "nullptr". */
7363 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7364 expr
= fold_simple (convert (type
, expr
));
7366 /* In C++11, integral or enumeration non-type template arguments can be
7367 arbitrary constant expressions. Pointer and pointer to
7368 member arguments can be general constant expressions that evaluate
7369 to a null value, but otherwise still need to be of a specific form. */
7370 if (cxx_dialect
>= cxx11
)
7372 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7373 /* A PTRMEM_CST is already constant, and a valid template
7374 argument for a parameter of pointer to member type, we just want
7375 to leave it in that form rather than lower it to a
7377 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7378 || cxx_dialect
>= cxx17
)
7380 /* C++17: A template-argument for a non-type template-parameter shall
7381 be a converted constant expression (8.20) of the type of the
7382 template-parameter. */
7383 expr
= build_converted_constant_expr (type
, expr
, complain
);
7384 if (expr
== error_mark_node
)
7385 /* Make sure we return NULL_TREE only if we have really issued
7386 an error, as described above. */
7387 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7388 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7390 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7393 expr
= maybe_constant_value (expr
, NULL_TREE
,
7394 /*manifestly_const_eval=*/true);
7395 expr
= convert_from_reference (expr
);
7396 /* EXPR may have become value-dependent. */
7397 val_dep_p
= value_dependent_expression_p (expr
);
7399 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7401 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7402 /*manifestly_const_eval=*/true);
7403 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7404 : null_member_pointer_value_p (folded
))
7409 if (TYPE_REF_P (type
))
7410 expr
= mark_lvalue_use (expr
);
7412 expr
= mark_rvalue_use (expr
);
7414 /* HACK: Due to double coercion, we can get a
7415 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7416 which is the tree that we built on the first call (see
7417 below when coercing to reference to object or to reference to
7418 function). We just strip everything and get to the arg.
7419 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7421 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7423 /* Check this before we strip *& to avoid redundancy. */
7424 if (!mark_single_function (expr
, complain
))
7425 return error_mark_node
;
7427 tree probe_type
, probe
= expr
;
7428 if (REFERENCE_REF_P (probe
))
7429 probe
= TREE_OPERAND (probe
, 0);
7430 probe_type
= TREE_TYPE (probe
);
7431 if (TREE_CODE (probe
) == NOP_EXPR
)
7433 /* ??? Maybe we could use convert_from_reference here, but we
7434 would need to relax its constraints because the NOP_EXPR
7435 could actually change the type to something more cv-qualified,
7436 and this is not folded by convert_from_reference. */
7437 tree addr
= TREE_OPERAND (probe
, 0);
7438 if (TYPE_REF_P (probe_type
)
7439 && TREE_CODE (addr
) == ADDR_EXPR
7440 && TYPE_PTR_P (TREE_TYPE (addr
))
7441 && (same_type_ignoring_top_level_qualifiers_p
7442 (TREE_TYPE (probe_type
),
7443 TREE_TYPE (TREE_TYPE (addr
)))))
7445 expr
= TREE_OPERAND (addr
, 0);
7446 expr_type
= TREE_TYPE (probe_type
);
7451 /* [temp.arg.nontype]/5, bullet 1
7453 For a non-type template-parameter of integral or enumeration type,
7454 integral promotions (_conv.prom_) and integral conversions
7455 (_conv.integral_) are applied. */
7456 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7457 || TREE_CODE (type
) == REAL_TYPE
)
7459 if (cxx_dialect
< cxx11
)
7461 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7462 t
= maybe_constant_value (t
);
7463 if (t
!= error_mark_node
)
7467 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7468 return error_mark_node
;
7470 /* Notice that there are constant expressions like '4 % 0' which
7471 do not fold into integer constants. */
7472 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7474 if (complain
& tf_error
)
7476 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7477 if (!require_potential_constant_expression (expr
))
7478 expr
= error_mark_node
;
7480 expr
= cxx_constant_value (expr
);
7481 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7482 inform (loc
, "in template argument for type %qT", type
);
7483 if (expr
== error_mark_node
)
7485 /* else cxx_constant_value complained but gave us
7486 a real constant, so go ahead. */
7487 if (!CONSTANT_CLASS_P (expr
))
7489 /* Some assemble time constant expressions like
7490 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7491 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7492 as we can emit them into .rodata initializers of
7493 variables, yet they can't fold into an INTEGER_CST at
7494 compile time. Refuse them here. */
7495 gcc_checking_assert (reduced_constant_expression_p (expr
));
7496 error_at (loc
, "template argument %qE for type %qT not "
7497 "a compile-time constant", expr
, type
);
7505 /* Avoid typedef problems. */
7506 if (TREE_TYPE (expr
) != type
)
7507 expr
= fold_convert (type
, expr
);
7509 /* [temp.arg.nontype]/5, bullet 2
7511 For a non-type template-parameter of type pointer to object,
7512 qualification conversions (_conv.qual_) and the array-to-pointer
7513 conversion (_conv.array_) are applied. */
7514 else if (TYPE_PTROBV_P (type
))
7516 tree decayed
= expr
;
7518 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7519 decay_conversion or an explicit cast. If it's a problematic cast,
7520 we'll complain about it below. */
7521 if (TREE_CODE (expr
) == NOP_EXPR
)
7525 if (TREE_CODE (probe
) == ADDR_EXPR
7526 && TYPE_PTR_P (TREE_TYPE (probe
)))
7529 expr_type
= TREE_TYPE (expr
);
7533 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7535 A template-argument for a non-type, non-template template-parameter
7536 shall be one of: [...]
7538 -- the name of a non-type template-parameter;
7539 -- the address of an object or function with external linkage, [...]
7540 expressed as "& id-expression" where the & is optional if the name
7541 refers to a function or array, or if the corresponding
7542 template-parameter is a reference.
7544 Here, we do not care about functions, as they are invalid anyway
7545 for a parameter of type pointer-to-object. */
7548 /* Non-type template parameters are OK. */
7550 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7551 /* Null pointer values are OK in C++11. */;
7552 else if (TREE_CODE (expr
) != ADDR_EXPR
7553 && !INDIRECT_TYPE_P (expr_type
))
7554 /* Other values, like integer constants, might be valid
7555 non-type arguments of some other type. */
7556 return error_mark_node
;
7557 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7562 expr
= perform_qualification_conversions (type
, expr
);
7563 if (expr
== error_mark_node
)
7564 return error_mark_node
;
7566 /* [temp.arg.nontype]/5, bullet 3
7568 For a non-type template-parameter of type reference to object, no
7569 conversions apply. The type referred to by the reference may be more
7570 cv-qualified than the (otherwise identical) type of the
7571 template-argument. The template-parameter is bound directly to the
7572 template-argument, which must be an lvalue. */
7573 else if (TYPE_REF_OBJ_P (type
))
7575 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7577 return error_mark_node
;
7579 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7581 if (complain
& tf_error
)
7582 error ("%qE is not a valid template argument for type %qT "
7583 "because of conflicts in cv-qualification", expr
, type
);
7587 if (!lvalue_p (expr
))
7589 if (complain
& tf_error
)
7590 error ("%qE is not a valid template argument for type %qT "
7591 "because it is not an lvalue", expr
, type
);
7595 /* [temp.arg.nontype]/1
7597 A template-argument for a non-type, non-template template-parameter
7598 shall be one of: [...]
7600 -- the address of an object or function with external linkage. */
7601 if (INDIRECT_REF_P (expr
)
7602 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7604 expr
= TREE_OPERAND (expr
, 0);
7607 if (complain
& tf_error
)
7608 error ("%q#D is not a valid template argument for type %qT "
7609 "because a reference variable does not have a constant "
7610 "address", expr
, type
);
7615 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7616 /* OK, dependent reference. We don't want to ask whether a DECL is
7617 itself value-dependent, since what we want here is its address. */;
7620 expr
= build_address (expr
);
7622 if (invalid_tparm_referent_p (type
, expr
, complain
))
7626 if (!same_type_p (type
, TREE_TYPE (expr
)))
7627 expr
= build_nop (type
, expr
);
7629 /* [temp.arg.nontype]/5, bullet 4
7631 For a non-type template-parameter of type pointer to function, only
7632 the function-to-pointer conversion (_conv.func_) is applied. If the
7633 template-argument represents a set of overloaded functions (or a
7634 pointer to such), the matching function is selected from the set
7636 else if (TYPE_PTRFN_P (type
))
7638 /* If the argument is a template-id, we might not have enough
7639 context information to decay the pointer. */
7640 if (!type_unknown_p (expr_type
))
7642 expr
= decay_conversion (expr
, complain
);
7643 if (expr
== error_mark_node
)
7644 return error_mark_node
;
7647 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7648 /* Null pointer values are OK in C++11. */
7649 return perform_qualification_conversions (type
, expr
);
7651 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7652 if (!expr
|| expr
== error_mark_node
)
7655 /* [temp.arg.nontype]/5, bullet 5
7657 For a non-type template-parameter of type reference to function, no
7658 conversions apply. If the template-argument represents a set of
7659 overloaded functions, the matching function is selected from the set
7661 else if (TYPE_REFFN_P (type
))
7663 if (TREE_CODE (expr
) == ADDR_EXPR
)
7665 if (complain
& tf_error
)
7667 error ("%qE is not a valid template argument for type %qT "
7668 "because it is a pointer", expr
, type
);
7669 inform (input_location
, "try using %qE instead",
7670 TREE_OPERAND (expr
, 0));
7675 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7676 if (!expr
|| expr
== error_mark_node
)
7679 /* [temp.arg.nontype]/5, bullet 6
7681 For a non-type template-parameter of type pointer to member function,
7682 no conversions apply. If the template-argument represents a set of
7683 overloaded member functions, the matching member function is selected
7684 from the set (_over.over_). */
7685 else if (TYPE_PTRMEMFUNC_P (type
))
7687 expr
= instantiate_type (type
, expr
, tf_none
);
7688 if (expr
== error_mark_node
)
7689 return error_mark_node
;
7691 /* [temp.arg.nontype] bullet 1 says the pointer to member
7692 expression must be a pointer-to-member constant. */
7694 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7697 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7698 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7699 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7700 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7702 /* [temp.arg.nontype]/5, bullet 7
7704 For a non-type template-parameter of type pointer to data member,
7705 qualification conversions (_conv.qual_) are applied. */
7706 else if (TYPE_PTRDATAMEM_P (type
))
7708 /* [temp.arg.nontype] bullet 1 says the pointer to member
7709 expression must be a pointer-to-member constant. */
7711 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7714 expr
= perform_qualification_conversions (type
, expr
);
7715 if (expr
== error_mark_node
)
7718 else if (NULLPTR_TYPE_P (type
))
7720 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7722 if (complain
& tf_error
)
7723 error ("%qE is not a valid template argument for type %qT "
7724 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7729 else if (CLASS_TYPE_P (type
))
7731 /* Replace the argument with a reference to the corresponding template
7732 parameter object. */
7734 expr
= create_template_parm_object (expr
, complain
);
7735 if (expr
== error_mark_node
)
7738 /* A template non-type parameter must be one of the above. */
7742 /* Sanity check: did we actually convert the argument to the
7744 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7745 (type
, TREE_TYPE (expr
)));
7746 return convert_from_reference (expr
);
7749 /* Subroutine of coerce_template_template_parms, which returns 1 if
7750 PARM_PARM and ARG_PARM match using the rule for the template
7751 parameters of template template parameters. Both PARM and ARG are
7752 template parameters; the rest of the arguments are the same as for
7753 coerce_template_template_parms.
7756 coerce_template_template_parm (tree parm
,
7758 tsubst_flags_t complain
,
7762 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7763 || parm
== NULL_TREE
|| error_operand_p (parm
))
7766 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7769 switch (TREE_CODE (parm
))
7772 /* We encounter instantiations of templates like
7773 template <template <template <class> class> class TT>
7776 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
7777 tree argparm
= DECL_TEMPLATE_PARMS (arg
);
7779 if (!coerce_template_template_parms
7780 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7786 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7787 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7788 /* Argument is a parameter pack but parameter is not. */
7793 /* The tsubst call is used to handle cases such as
7795 template <int> class C {};
7796 template <class T, template <T> class TT> class D {};
7799 i.e. the parameter list of TT depends on earlier parameters. */
7800 if (!uses_template_parms (TREE_TYPE (arg
)))
7802 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7803 if (!uses_template_parms (t
)
7804 && !same_type_p (t
, TREE_TYPE (arg
)))
7808 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7809 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7810 /* Argument is a parameter pack but parameter is not. */
7822 /* Coerce template argument list ARGLIST for use with template
7823 template-parameter TEMPL. */
7826 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7827 tsubst_flags_t complain
)
7829 /* Consider an example where a template template parameter declared as
7831 template <class T, class U = std::allocator<T> > class TT
7833 The template parameter level of T and U are one level larger than
7834 of TT. To proper process the default argument of U, say when an
7835 instantiation `TT<int>' is seen, we need to build the full
7836 arguments containing {int} as the innermost level. Outer levels,
7837 available when not appearing as default template argument, can be
7838 obtained from the arguments of the enclosing template.
7840 Suppose that TT is later substituted with std::vector. The above
7841 instantiation is `TT<int, std::allocator<T> >' with TT at
7842 level 1, and T at level 2, while the template arguments at level 1
7843 becomes {std::vector} and the inner level 2 is {int}. */
7845 tree outer
= DECL_CONTEXT (templ
);
7847 outer
= generic_targs_for (outer
);
7848 else if (current_template_parms
)
7850 /* This is an argument of the current template, so we haven't set
7851 DECL_CONTEXT yet. */
7852 tree relevant_template_parms
;
7854 /* Parameter levels that are greater than the level of the given
7855 template template parm are irrelevant. */
7856 relevant_template_parms
= current_template_parms
;
7857 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7858 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7859 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7861 outer
= template_parms_to_args (relevant_template_parms
);
7865 arglist
= add_to_template_args (outer
, arglist
);
7867 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7868 return coerce_template_parms (parmlist
, arglist
, templ
, complain
);
7871 /* A cache of template template parameters with match-all default
7873 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7875 /* T is a bound template template-parameter. Copy its arguments into default
7876 arguments of the template template-parameter's template parameters. */
7879 add_defaults_to_ttp (tree otmpl
)
7881 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7884 tree ntmpl
= copy_node (otmpl
);
7886 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7887 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7888 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7889 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7890 TYPE_NAME (ntype
) = ntmpl
;
7891 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7893 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7894 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7895 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7896 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7898 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7899 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7900 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7901 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7902 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7904 tree o
= TREE_VEC_ELT (vec
, i
);
7905 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7907 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7908 TREE_PURPOSE (n
) = any_targ_node
;
7912 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7916 /* ARG is a bound potential template template-argument, and PARGS is a list
7917 of arguments for the corresponding template template-parameter. Adjust
7918 PARGS as appropriate for application to ARG's template, and if ARG is a
7919 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7920 arguments to the template template parameter. */
7923 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7925 ++processing_template_decl
;
7926 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7927 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7929 /* When comparing two template template-parameters in partial ordering,
7930 rewrite the one currently being used as an argument to have default
7931 arguments for all parameters. */
7932 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7933 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7934 if (pargs
!= error_mark_node
)
7935 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7936 TYPE_TI_ARGS (arg
));
7941 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7942 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
);
7944 --processing_template_decl
;
7948 /* Subroutine of unify for the case when PARM is a
7949 BOUND_TEMPLATE_TEMPLATE_PARM. */
7952 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7955 tree parmvec
= TYPE_TI_ARGS (parm
);
7956 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7958 /* The template template parm might be variadic and the argument
7959 not, so flatten both argument lists. */
7960 parmvec
= expand_template_argument_pack (parmvec
);
7961 argvec
= expand_template_argument_pack (argvec
);
7965 /* In keeping with P0522R0, adjust P's template arguments
7966 to apply to A's template; then flatten it again. */
7967 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7968 nparmvec
= expand_template_argument_pack (nparmvec
);
7970 if (unify (tparms
, targs
, nparmvec
, argvec
,
7971 UNIFY_ALLOW_NONE
, explain_p
))
7974 /* If the P0522 adjustment eliminated a pack expansion, deduce
7977 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7978 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7979 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7984 /* Deduce arguments T, i from TT<T> or TT<i>.
7985 We check each element of PARMVEC and ARGVEC individually
7986 rather than the whole TREE_VEC since they can have
7987 different number of elements, which is allowed under N2555. */
7989 int len
= TREE_VEC_LENGTH (parmvec
);
7991 /* Check if the parameters end in a pack, making them
7993 int parm_variadic_p
= 0;
7995 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7996 parm_variadic_p
= 1;
7998 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7999 /* If the template argument list of P contains a pack
8000 expansion that is not the last template argument, the
8001 entire template argument list is a non-deduced
8003 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
8004 return unify_success (explain_p
);
8006 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
8007 return unify_too_few_arguments (explain_p
,
8008 TREE_VEC_LENGTH (argvec
), len
);
8010 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
8011 if (unify (tparms
, targs
,
8012 TREE_VEC_ELT (parmvec
, i
),
8013 TREE_VEC_ELT (argvec
, i
),
8014 UNIFY_ALLOW_NONE
, explain_p
))
8018 && unify_pack_expansion (tparms
, targs
,
8021 /*subr=*/true, explain_p
))
8028 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
8029 template template parameters. Both PARM_PARMS and ARG_PARMS are
8030 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
8033 Consider the example:
8034 template <class T> class A;
8035 template<template <class U> class TT> class B;
8037 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
8038 the parameters to A, and OUTER_ARGS contains A. */
8041 coerce_template_template_parms (tree parm_parms_full
,
8042 tree arg_parms_full
,
8043 tsubst_flags_t complain
,
8047 int nparms
, nargs
, i
;
8051 tree parm_parms
= INNERMOST_TEMPLATE_PARMS (parm_parms_full
);
8052 tree arg_parms
= INNERMOST_TEMPLATE_PARMS (arg_parms_full
);
8054 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
8055 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
8057 nparms
= TREE_VEC_LENGTH (parm_parms
);
8058 nargs
= TREE_VEC_LENGTH (arg_parms
);
8062 /* P0522R0: A template template-parameter P is at least as specialized as
8063 a template template-argument A if, given the following rewrite to two
8064 function templates, the function template corresponding to P is at
8065 least as specialized as the function template corresponding to A
8066 according to the partial ordering rules for function templates
8067 ([temp.func.order]). Given an invented class template X with the
8068 template parameter list of A (including default arguments):
8070 * Each of the two function templates has the same template parameters,
8071 respectively, as P or A.
8073 * Each function template has a single function parameter whose type is
8074 a specialization of X with template arguments corresponding to the
8075 template parameters from the respective function template where, for
8076 each template parameter PP in the template parameter list of the
8077 function template, a corresponding template argument AA is formed. If
8078 PP declares a parameter pack, then AA is the pack expansion
8079 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8081 If the rewrite produces an invalid type, then P is not at least as
8082 specialized as A. */
8084 /* So coerce P's args to apply to A's parms, and then deduce between A's
8085 args and the converted args. If that succeeds, A is at least as
8086 specialized as P, so they match.*/
8087 processing_template_decl_sentinel
ptds (/*reset*/false);
8088 ++processing_template_decl
;
8090 tree pargs
= template_parms_level_to_args (parm_parms
);
8092 /* PARM, and thus the context in which we are passing ARG to it, may be
8093 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8094 want to provide the right number of levels, so we reduce the number of
8095 levels in OUTER_ARGS before prepending them. This is most important
8096 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8098 ARG might also be deeper than PARM (ttp23). In that case, we include
8099 all of OUTER_ARGS. The missing levels seem potentially problematic,
8100 but I can't come up with a testcase that breaks. */
8101 if (int arg_outer_levs
= TMPL_PARMS_DEPTH (arg_parms_full
) - 1)
8103 auto x
= make_temp_override (TREE_VEC_LENGTH (outer_args
));
8104 if (TMPL_ARGS_DEPTH (outer_args
) > arg_outer_levs
)
8105 TREE_VEC_LENGTH (outer_args
) = arg_outer_levs
;
8106 pargs
= add_to_template_args (outer_args
, pargs
);
8109 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
);
8110 if (pargs
!= error_mark_node
)
8112 tree targs
= make_tree_vec (nargs
);
8113 tree aargs
= template_parms_level_to_args (arg_parms
);
8114 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
8120 /* Determine whether we have a parameter pack at the end of the
8121 template template parameter's template parameter list. */
8122 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
8124 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
8126 if (error_operand_p (parm
))
8129 switch (TREE_CODE (parm
))
8133 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
8138 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8148 && !(variadic_p
&& nargs
>= nparms
- 1))
8151 /* Check all of the template parameters except the parameter pack at
8152 the end (if any). */
8153 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8155 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8156 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8159 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8160 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8162 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8170 /* Check each of the template parameters in the template
8171 argument against the template parameter pack at the end of
8172 the template template parameter. */
8173 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8176 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8178 for (; i
< nargs
; ++i
)
8180 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8183 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8185 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8194 /* Verifies that the deduced template arguments (in TARGS) for the
8195 template template parameters (in TPARMS) represent valid bindings,
8196 by comparing the template parameter list of each template argument
8197 to the template parameter list of its corresponding template
8198 template parameter, in accordance with DR150. This
8199 routine can only be called after all template arguments have been
8200 deduced. It will return TRUE if all of the template template
8201 parameter bindings are okay, FALSE otherwise. */
8203 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8205 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8208 /* We're dealing with template parms in this process. */
8209 ++processing_template_decl
;
8211 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8213 for (i
= 0; i
< ntparms
; ++i
)
8215 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8216 tree targ
= TREE_VEC_ELT (targs
, i
);
8218 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8220 tree packed_args
= NULL_TREE
;
8223 if (ARGUMENT_PACK_P (targ
))
8225 /* Look inside the argument pack. */
8226 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8227 len
= TREE_VEC_LENGTH (packed_args
);
8230 for (idx
= 0; idx
< len
; ++idx
)
8232 tree targ_parms
= NULL_TREE
;
8235 /* Extract the next argument from the argument
8237 targ
= TREE_VEC_ELT (packed_args
, idx
);
8239 if (PACK_EXPANSION_P (targ
))
8240 /* Look at the pattern of the pack expansion. */
8241 targ
= PACK_EXPANSION_PATTERN (targ
);
8243 /* Extract the template parameters from the template
8245 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8246 targ_parms
= DECL_TEMPLATE_PARMS (targ
);
8247 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8248 targ_parms
= DECL_TEMPLATE_PARMS (TYPE_NAME (targ
));
8250 /* Verify that we can coerce the template template
8251 parameters from the template argument to the template
8252 parameter. This requires an exact match. */
8254 && !coerce_template_template_parms
8255 (DECL_TEMPLATE_PARMS (tparm
),
8270 --processing_template_decl
;
8274 /* Since type attributes aren't mangled, we need to strip them from
8275 template type arguments. */
8278 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8280 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8282 bool removed_attributes
= false;
8283 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8284 if (removed_attributes
8285 && (complain
& tf_warning
))
8286 warning (OPT_Wignored_attributes
,
8287 "ignoring attributes on template argument %qT", arg
);
8291 /* And from inside dependent non-type arguments like sizeof(Type). */
8294 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8296 if (!arg
|| arg
== error_mark_node
)
8298 bool removed_attributes
= false;
8299 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8300 if (removed_attributes
8301 && (complain
& tf_warning
))
8302 warning (OPT_Wignored_attributes
,
8303 "ignoring attributes in template argument %qE", arg
);
8307 /* A template declaration can be substituted for a constrained
8308 template template parameter only when the argument is no more
8309 constrained than the parameter. */
8312 is_compatible_template_arg (tree parm
, tree arg
)
8314 tree parm_cons
= get_constraints (parm
);
8316 /* For now, allow constrained template template arguments
8317 and unconstrained template template parameters. */
8318 if (parm_cons
== NULL_TREE
)
8321 /* If the template parameter is constrained, we need to rewrite its
8322 constraints in terms of the ARG's template parameters. This ensures
8323 that all of the template parameter types will have the same depth.
8325 Note that this is only valid when coerce_template_template_parm is
8326 true for the innermost template parameters of PARM and ARG. In other
8327 words, because coercion is successful, this conversion will be valid. */
8328 tree new_args
= NULL_TREE
;
8331 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8332 new_args
= template_parms_level_to_args (aparms
);
8333 ++processing_template_decl
;
8334 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8335 tf_none
, NULL_TREE
);
8336 --processing_template_decl
;
8337 if (parm_cons
== error_mark_node
)
8341 return weakly_subsumes (parm_cons
, arg
);
8344 // Convert a placeholder argument into a binding to the original
8345 // parameter. The original parameter is saved as the TREE_TYPE of
8348 convert_wildcard_argument (tree parm
, tree arg
)
8350 TREE_TYPE (arg
) = parm
;
8354 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8355 because one of them is dependent. But we need to represent the
8356 conversion for the benefit of cp_tree_equal. */
8359 maybe_convert_nontype_argument (tree type
, tree arg
)
8361 /* Auto parms get no conversion. */
8362 if (type_uses_auto (type
))
8364 /* We don't need or want to add this conversion now if we're going to use the
8365 argument for deduction. */
8366 if (value_dependent_expression_p (arg
))
8369 type
= cv_unqualified (type
);
8370 tree argtype
= TREE_TYPE (arg
);
8371 if (same_type_p (type
, argtype
))
8374 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8375 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8379 /* Convert the indicated template ARG as necessary to match the
8380 indicated template PARM. Returns the converted ARG, or
8381 error_mark_node if the conversion was unsuccessful. Error and
8382 warning messages are issued under control of COMPLAIN. This
8383 conversion is for the Ith parameter in the parameter list. ARGS is
8384 the full set of template arguments deduced so far. */
8387 convert_template_argument (tree parm
,
8390 tsubst_flags_t complain
,
8396 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8398 if (parm
== error_mark_node
|| error_operand_p (arg
))
8399 return error_mark_node
;
8401 /* Trivially convert placeholders. */
8402 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8403 return convert_wildcard_argument (parm
, arg
);
8405 if (arg
== any_targ_node
)
8408 if (TREE_CODE (arg
) == TREE_LIST
8409 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8411 /* The template argument was the name of some
8412 member function. That's usually
8413 invalid, but static members are OK. In any
8414 case, grab the underlying fields/functions
8415 and issue an error later if required. */
8416 TREE_TYPE (arg
) = unknown_type_node
;
8421 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8422 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8423 || requires_tmpl_type
);
8425 /* When determining whether an argument pack expansion is a template,
8426 look at the pattern. */
8427 if (PACK_EXPANSION_P (arg
))
8428 arg
= PACK_EXPANSION_PATTERN (arg
);
8430 /* Deal with an injected-class-name used as a template template arg. */
8431 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8433 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8434 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8436 if (cxx_dialect
>= cxx11
)
8437 /* OK under DR 1004. */;
8438 else if (complain
& tf_warning_or_error
)
8439 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8440 " used as template template argument", TYPE_NAME (arg
));
8441 else if (flag_pedantic_errors
)
8449 ((TREE_CODE (arg
) == TEMPLATE_DECL
8450 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8451 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8452 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8453 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8456 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8457 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8458 arg
= TYPE_STUB_DECL (arg
);
8460 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8462 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8463 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8465 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8467 if (complain
& tf_error
)
8468 error ("invalid use of destructor %qE as a type", orig_arg
);
8469 return error_mark_node
;
8472 permerror (input_location
,
8473 "to refer to a type member of a template parameter, "
8474 "use %<typename %E%>", orig_arg
);
8476 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8477 TREE_OPERAND (arg
, 1),
8483 if (is_type
!= requires_type
)
8487 if (complain
& tf_error
)
8489 error ("type/value mismatch at argument %d in template "
8490 "parameter list for %qD",
8494 /* The template argument is a type, but we're expecting
8496 inform (input_location
,
8497 " expected a constant of type %qT, got %qT",
8499 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8500 /* [temp.arg]/2: "In a template-argument, an ambiguity
8501 between a type-id and an expression is resolved to a
8502 type-id, regardless of the form of the corresponding
8503 template-parameter." So give the user a clue. */
8504 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8505 inform (input_location
, " ambiguous template argument "
8506 "for non-type template parameter is treated as "
8509 else if (requires_tmpl_type
)
8510 inform (input_location
,
8511 " expected a class template, got %qE", orig_arg
);
8513 inform (input_location
,
8514 " expected a type, got %qE", orig_arg
);
8517 return error_mark_node
;
8519 if (is_tmpl_type
^ requires_tmpl_type
)
8521 if (in_decl
&& (complain
& tf_error
))
8523 error ("type/value mismatch at argument %d in template "
8524 "parameter list for %qD",
8527 inform (input_location
,
8528 " expected a type, got %qT", DECL_NAME (arg
));
8530 inform (input_location
,
8531 " expected a class template, got %qT", orig_arg
);
8533 return error_mark_node
;
8536 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8537 /* We already did the appropriate conversion when packing args. */
8541 if (requires_tmpl_type
)
8543 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8544 /* The number of argument required is not known yet.
8545 Just accept it for now. */
8549 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
8552 /* Strip alias templates that are equivalent to another
8554 arg
= get_underlying_template (arg
);
8555 argparm
= DECL_TEMPLATE_PARMS (arg
);
8557 if (coerce_template_template_parms (parmparm
, argparm
,
8563 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8565 if (val
!= error_mark_node
)
8567 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8568 val
= TREE_TYPE (val
);
8569 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8570 val
= make_pack_expansion (val
, complain
);
8575 if (in_decl
&& (complain
& tf_error
))
8577 error ("type/value mismatch at argument %d in "
8578 "template parameter list for %qD",
8580 inform (input_location
,
8581 " expected a template of type %qD, got %qT",
8585 val
= error_mark_node
;
8588 // Check that the constraints are compatible before allowing the
8590 if (val
!= error_mark_node
)
8591 if (!is_compatible_template_arg (parm
, arg
))
8593 if (in_decl
&& (complain
& tf_error
))
8595 error ("constraint mismatch at argument %d in "
8596 "template parameter list for %qD",
8598 inform (input_location
, " expected %qD but got %qD",
8601 val
= error_mark_node
;
8607 /* We only form one instance of each template specialization.
8608 Therefore, if we use a non-canonical variant (i.e., a
8609 typedef), any future messages referring to the type will use
8610 the typedef, which is confusing if those future uses do not
8611 themselves also use the typedef. */
8613 val
= canonicalize_type_argument (val
, complain
);
8617 tree t
= TREE_TYPE (parm
);
8619 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8620 > TMPL_ARGS_DEPTH (args
))
8621 /* We don't have enough levels of args to do any substitution. This
8622 can happen in the context of -fnew-ttp-matching. */;
8623 else if (tree a
= type_uses_auto (t
))
8625 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
,
8627 if (t
== error_mark_node
)
8628 return error_mark_node
;
8631 t
= tsubst (t
, args
, complain
, in_decl
);
8633 /* Perform array-to-pointer and function-to-pointer conversion
8634 as per [temp.param]/10. */
8635 t
= type_decays_to (t
);
8637 if (invalid_nontype_parm_type_p (t
, complain
))
8638 return error_mark_node
;
8640 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8641 this non-type template parameter, as per [temp.param]/6. */
8642 t
= cv_unqualified (t
);
8644 if (t
!= TREE_TYPE (parm
))
8645 t
= canonicalize_type_argument (t
, complain
);
8647 if (!type_dependent_expression_p (orig_arg
)
8648 && !uses_template_parms (t
))
8649 /* We used to call digest_init here. However, digest_init
8650 will report errors, which we don't want when complain
8651 is zero. More importantly, digest_init will try too
8652 hard to convert things: for example, `0' should not be
8653 converted to pointer type at this point according to
8654 the standard. Accepting this is not merely an
8655 extension, since deciding whether or not these
8656 conversions can occur is part of determining which
8657 function template to call, or whether a given explicit
8658 argument specification is valid. */
8659 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8662 val
= canonicalize_expr_argument (orig_arg
, complain
);
8663 val
= maybe_convert_nontype_argument (t
, val
);
8667 if (val
== NULL_TREE
)
8668 val
= error_mark_node
;
8669 else if (val
== error_mark_node
&& (complain
& tf_error
))
8670 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8671 "could not convert template argument %qE from %qT to %qT",
8672 orig_arg
, TREE_TYPE (orig_arg
), t
);
8674 if (INDIRECT_REF_P (val
))
8676 /* Reject template arguments that are references to built-in
8677 functions with no library fallbacks. */
8678 const_tree inner
= TREE_OPERAND (val
, 0);
8679 const_tree innertype
= TREE_TYPE (inner
);
8681 && TYPE_REF_P (innertype
)
8682 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8683 && TREE_OPERAND_LENGTH (inner
) > 0
8684 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8685 return error_mark_node
;
8688 if (TREE_CODE (val
) == SCOPE_REF
)
8690 /* Strip typedefs from the SCOPE_REF. */
8691 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8692 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8694 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8695 QUALIFIED_NAME_IS_TEMPLATE (val
));
8702 /* Coerces the remaining template arguments in INNER_ARGS (from
8703 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8704 Returns the coerced argument pack. PARM_IDX is the position of this
8705 parameter in the template parameter list. ARGS is the original
8706 template argument list. */
8708 coerce_template_parameter_pack (tree parms
,
8716 tsubst_flags_t complain
)
8718 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8719 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8722 tree packed_parms
= NULL_TREE
;
8724 if (arg_idx
> nargs
)
8727 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8729 /* When the template parameter is a non-type template parameter pack
8730 or template template parameter pack whose type or template
8731 parameters use parameter packs, we know exactly how many arguments
8732 we are looking for. Build a vector of the instantiated decls for
8733 these template parameters in PACKED_PARMS. */
8734 /* We can't use make_pack_expansion here because it would interpret a
8735 _DECL as a use rather than a declaration. */
8736 tree decl
= TREE_VALUE (parm
);
8737 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8738 PACK_EXPANSION_PATTERN (exp
) = decl
;
8739 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8740 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8742 TREE_VEC_LENGTH (args
)--;
8743 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8744 TREE_VEC_LENGTH (args
)++;
8746 if (packed_parms
== error_mark_node
)
8747 return error_mark_node
;
8749 /* If we're doing a partial instantiation of a member template,
8750 verify that all of the types used for the non-type
8751 template parameter pack are, in fact, valid for non-type
8752 template parameters. */
8754 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8756 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8757 for (j
= 0; j
< len
; ++j
)
8759 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8760 if (TREE_CODE (t
) == PARM_DECL
8761 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8762 return error_mark_node
;
8764 /* We don't know how many args we have yet, just
8765 use the unconverted ones for now. */
8769 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8771 /* Check if we have a placeholder pack, which indicates we're
8772 in the context of a introduction list. In that case we want
8773 to match this pack to the single placeholder. */
8774 else if (arg_idx
< nargs
8775 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8776 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8778 nargs
= arg_idx
+ 1;
8779 packed_args
= make_tree_vec (1);
8782 packed_args
= make_tree_vec (nargs
- arg_idx
);
8784 /* Convert the remaining arguments, which will be a part of the
8785 parameter pack "parm". */
8786 int first_pack_arg
= arg_idx
;
8787 for (; arg_idx
< nargs
; ++arg_idx
)
8789 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8790 tree actual_parm
= TREE_VALUE (parm
);
8791 int pack_idx
= arg_idx
- first_pack_arg
;
8795 /* Once we've packed as many args as we have types, stop. */
8796 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8798 else if (PACK_EXPANSION_P (arg
))
8799 /* We don't know how many args we have yet, just
8800 use the unconverted ones for now. */
8803 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8806 if (arg
== error_mark_node
)
8808 if (complain
& tf_error
)
8809 error ("template argument %d is invalid", arg_idx
+ 1);
8812 arg
= convert_template_argument (actual_parm
,
8813 arg
, new_args
, complain
, parm_idx
,
8815 if (arg
== error_mark_node
)
8817 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8820 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8821 && TREE_VEC_LENGTH (packed_args
) > 0)
8823 if (complain
& tf_error
)
8824 error ("wrong number of template arguments (%d, should be %d)",
8825 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8826 return error_mark_node
;
8829 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8830 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8831 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8834 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8835 TREE_CONSTANT (argument_pack
) = 1;
8838 ARGUMENT_PACK_ARGS (argument_pack
) = packed_args
;
8840 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8841 TREE_VEC_LENGTH (packed_args
));
8842 return argument_pack
;
8845 /* Returns the number of pack expansions in the template argument vector
8849 pack_expansion_args_count (tree args
)
8854 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8856 tree elt
= TREE_VEC_ELT (args
, i
);
8857 if (elt
&& PACK_EXPANSION_P (elt
))
8863 /* Convert all template arguments to their appropriate types, and
8864 return a vector containing the innermost resulting template
8865 arguments. If any error occurs, return error_mark_node. Error and
8866 warning messages are issued under control of COMPLAIN.
8868 If PARMS represents all template parameters levels, this function
8869 returns a vector of vectors representing all the resulting argument
8870 levels. Note that in this case, only the innermost arguments are
8871 coerced because the outermost ones are supposed to have been coerced
8872 already. Otherwise, if PARMS represents only (the innermost) vector
8873 of parameters, this function returns a vector containing just the
8874 innermost resulting arguments.
8876 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8877 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
8878 arguments not specified in ARGS must have default arguments which
8879 we'll use to fill in ARGS. */
8882 coerce_template_parms (tree parms
,
8885 tsubst_flags_t complain
,
8886 bool require_all_args
/* = true */)
8888 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8889 tree orig_inner_args
;
8892 /* When used as a boolean value, indicates whether this is a
8893 variadic template parameter list. Since it's an int, we can also
8894 subtract it from nparms to get the number of non-variadic
8897 int variadic_args_p
= 0;
8898 int post_variadic_parms
= 0;
8900 /* Adjustment to nparms for fixed parameter packs. */
8901 int fixed_pack_adjust
= 0;
8902 int fixed_packs
= 0;
8905 /* Likewise for parameters with default arguments. */
8908 if (args
== error_mark_node
)
8909 return error_mark_node
;
8911 bool return_full_args
= false;
8912 if (TREE_CODE (parms
) == TREE_LIST
)
8914 if (TMPL_PARMS_DEPTH (parms
) > 1)
8916 gcc_assert (TMPL_PARMS_DEPTH (parms
) == TMPL_ARGS_DEPTH (args
));
8917 return_full_args
= true;
8919 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
8922 nparms
= TREE_VEC_LENGTH (parms
);
8924 /* Determine if there are any parameter packs or default arguments. */
8925 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8927 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8929 ++post_variadic_parms
;
8930 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8932 if (TREE_PURPOSE (parm
))
8936 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8937 /* If there are no parameters that follow a parameter pack, we need to
8938 expand any argument packs so that we can deduce a parameter pack from
8939 some non-packed args followed by an argument pack, as in variadic85.C.
8940 If there are such parameters, we need to leave argument packs intact
8941 so the arguments are assigned properly. This can happen when dealing
8942 with a nested class inside a partial specialization of a class
8943 template, as in variadic92.C, or when deducing a template parameter pack
8944 from a sub-declarator, as in variadic114.C. */
8945 if (!post_variadic_parms
)
8946 inner_args
= expand_template_argument_pack (inner_args
);
8948 /* Count any pack expansion args. */
8949 variadic_args_p
= pack_expansion_args_count (inner_args
);
8951 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8952 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8953 || (nargs
< nparms
- variadic_p
8956 && (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8957 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)))))
8960 if (complain
& tf_error
)
8962 if (variadic_p
|| default_p
)
8964 nparms
-= variadic_p
+ default_p
;
8965 error ("wrong number of template arguments "
8966 "(%d, should be at least %d)", nargs
, nparms
);
8969 error ("wrong number of template arguments "
8970 "(%d, should be %d)", nargs
, nparms
);
8973 inform (DECL_SOURCE_LOCATION (in_decl
),
8974 "provided for %qD", in_decl
);
8977 return error_mark_node
;
8979 /* We can't pass a pack expansion to a non-pack parameter of an alias
8980 template (DR 1430). */
8982 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8983 || concept_definition_p (in_decl
))
8985 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8987 if (complain
& tf_error
)
8989 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8991 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8992 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8994 if (PACK_EXPANSION_P (arg
)
8995 && !template_parameter_pack_p (parm
))
8997 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8998 error_at (location_of (arg
),
8999 "pack expansion argument for non-pack parameter "
9000 "%qD of alias template %qD", parm
, in_decl
);
9002 error_at (location_of (arg
),
9003 "pack expansion argument for non-pack parameter "
9004 "%qD of concept %qD", parm
, in_decl
);
9005 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
9012 return error_mark_node
;
9015 /* We need to evaluate the template arguments, even though this
9016 template-id may be nested within a "sizeof". */
9019 tree new_args
= add_outermost_template_args (args
, make_tree_vec (nparms
));
9020 tree
& new_inner_args
= TMPL_ARGS_LEVEL (new_args
, TMPL_ARGS_DEPTH (new_args
));
9021 int pack_adjust
= 0;
9022 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
9027 /* Get the Ith template parameter. */
9028 parm
= TREE_VEC_ELT (parms
, parm_idx
);
9030 if (parm
== error_mark_node
)
9032 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
9036 /* Calculate the next argument. */
9037 if (arg_idx
< nargs
)
9038 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
9042 if (template_parameter_pack_p (TREE_VALUE (parm
))
9043 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
9044 && !(arg
&& ARGUMENT_PACK_P (arg
)))
9046 /* Some arguments will be placed in the
9047 template parameter pack PARM. */
9048 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
9049 inner_args
, arg_idx
,
9053 if (arg
== NULL_TREE
)
9055 /* We don't know how many args we have yet, just use the
9056 unconverted (and still packed) ones for now. */
9057 new_inner_args
= orig_inner_args
;
9062 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
9064 /* Store this argument. */
9065 if (arg
== error_mark_node
)
9068 /* We are done with all of the arguments. */
9074 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
9075 arg_idx
+= pack_adjust
;
9076 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
9079 fixed_pack_adjust
+= pack_adjust
;
9087 if (PACK_EXPANSION_P (arg
))
9089 /* "If every valid specialization of a variadic template
9090 requires an empty template parameter pack, the template is
9091 ill-formed, no diagnostic required." So check that the
9092 pattern works with this parameter. */
9093 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
9094 tree conv
= convert_template_argument (TREE_VALUE (parm
),
9098 if (conv
== error_mark_node
)
9100 if (complain
& tf_error
)
9101 inform (input_location
, "so any instantiation with a "
9102 "non-empty parameter pack would be ill-formed");
9105 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
9106 /* Recover from missing typename. */
9107 TREE_VEC_ELT (inner_args
, arg_idx
)
9108 = make_pack_expansion (conv
, complain
);
9110 /* We don't know how many args we have yet, just
9111 use the unconverted ones for now. */
9112 new_inner_args
= inner_args
;
9117 else if (require_all_args
)
9119 /* There must be a default arg in this case. */
9120 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
9122 /* The position of the first default template argument,
9123 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9125 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9126 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9127 arg_idx
- pack_adjust
);
9132 if (arg
== error_mark_node
)
9134 if (complain
& tf_error
)
9135 error ("template argument %d is invalid", arg_idx
+ 1);
9139 /* This can occur if there was an error in the template
9140 parameter list itself (which we would already have
9141 reported) that we are trying to recover from, e.g., a class
9142 template with a parameter list such as
9143 template<typename..., typename> (cpp0x/variadic150.C). */
9146 /* This can also happen with a fixed parameter pack (71834). */
9147 if (arg_idx
>= nargs
)
9151 arg
= convert_template_argument (TREE_VALUE (parm
),
9152 arg
, new_args
, complain
,
9155 if (arg
== error_mark_node
)
9158 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
9161 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
9163 /* If we had fixed parameter packs, we didn't know how many arguments we
9164 actually needed earlier; now we do. */
9165 nparms
+= fixed_pack_adjust
;
9166 variadic_p
-= fixed_packs
;
9170 if (arg_idx
< nargs
)
9172 /* We had some pack expansion arguments that will only work if the packs
9173 are empty, but wait until instantiation time to complain.
9174 See variadic-ttp3.C. */
9176 /* Except that we can't provide empty packs to alias templates or
9177 concepts when there are no corresponding parameters. Basically,
9178 we can get here with this:
9180 template<typename T> concept C = true;
9182 template<typename... Args>
9186 When parsing C<Args...>, we try to form a concept check of
9187 C<?, Args...>. Without the extra check for substituting an empty
9188 pack past the last parameter, we can accept the check as valid.
9190 FIXME: This may be valid for alias templates (but I doubt it).
9192 FIXME: The error could be better also. */
9193 if (in_decl
&& concept_definition_p (in_decl
))
9195 if (complain
& tf_error
)
9196 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9197 "too many arguments");
9198 return error_mark_node
;
9201 int len
= nparms
+ (nargs
- arg_idx
);
9202 tree args
= make_tree_vec (len
);
9204 for (; i
< nparms
; ++i
)
9205 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9206 for (; i
< len
; ++i
, ++arg_idx
)
9207 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9208 arg_idx
- pack_adjust
);
9209 new_inner_args
= args
;
9214 gcc_assert (!(complain
& tf_error
) || seen_error ());
9215 return error_mark_node
;
9218 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9219 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9220 TREE_VEC_LENGTH (new_inner_args
));
9222 return return_full_args
? new_args
: new_inner_args
;
9225 /* Returns true if T is a wrapper to make a C++20 template parameter
9229 class_nttp_const_wrapper_p (tree t
)
9231 if (cxx_dialect
< cxx20
)
9233 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9234 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9235 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9238 /* Returns 1 if template args OT and NT are equivalent. */
9241 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9245 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9247 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9250 if (class_nttp_const_wrapper_p (nt
))
9251 nt
= TREE_OPERAND (nt
, 0);
9252 if (class_nttp_const_wrapper_p (ot
))
9253 ot
= TREE_OPERAND (ot
, 0);
9255 /* DR 1558: Don't treat an alias template specialization with dependent
9256 arguments as equivalent to its underlying type when used as a template
9257 argument; we need them to be distinct so that we substitute into the
9258 specialization arguments at instantiation time. And aliases can't be
9259 equivalent without being ==, so we don't need to look any deeper.
9261 During partial ordering, however, we need to treat them normally so we can
9262 order uses of the same alias with different cv-qualification (79960). */
9263 auto cso
= make_temp_override (comparing_dependent_aliases
);
9265 ++comparing_dependent_aliases
;
9267 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9268 /* For member templates */
9269 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9270 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9271 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9272 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9273 PACK_EXPANSION_PATTERN (nt
))
9274 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9275 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9276 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9277 return cp_tree_equal (ot
, nt
);
9278 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9280 else if (TYPE_P (nt
) || TYPE_P (ot
))
9282 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9284 return same_type_p (ot
, nt
);
9288 /* Try to treat a template non-type argument that has been converted
9289 to the parameter type as equivalent to one that hasn't yet. */
9290 for (enum tree_code code1
= TREE_CODE (ot
);
9291 CONVERT_EXPR_CODE_P (code1
)
9292 || code1
== NON_LVALUE_EXPR
;
9293 code1
= TREE_CODE (ot
))
9294 ot
= TREE_OPERAND (ot
, 0);
9296 for (enum tree_code code2
= TREE_CODE (nt
);
9297 CONVERT_EXPR_CODE_P (code2
)
9298 || code2
== NON_LVALUE_EXPR
;
9299 code2
= TREE_CODE (nt
))
9300 nt
= TREE_OPERAND (nt
, 0);
9302 return cp_tree_equal (ot
, nt
);
9306 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9307 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9308 NEWARG_PTR with the offending arguments if they are non-NULL. */
9311 comp_template_args (tree oldargs
, tree newargs
,
9312 tree
*oldarg_ptr
/* = NULL */, tree
*newarg_ptr
/* = NULL */,
9313 bool partial_order
/* = false */)
9315 if (oldargs
== newargs
)
9318 if (!oldargs
|| !newargs
)
9321 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9324 for (int i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9326 tree nt
= TREE_VEC_ELT (newargs
, i
);
9327 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9329 if (! template_args_equal (ot
, nt
, partial_order
))
9331 if (oldarg_ptr
!= NULL
)
9333 if (newarg_ptr
!= NULL
)
9342 comp_template_args_porder (tree oargs
, tree nargs
)
9344 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9347 /* Implement a freelist interface for objects of type T.
9349 Head is a separate object, rather than a regular member, so that we
9350 can define it as a GTY deletable pointer, which is highly
9351 desirable. A data member could be declared that way, but then the
9352 containing object would implicitly get GTY((user)), which would
9353 prevent us from instantiating freelists as global objects.
9354 Although this way we can create freelist global objects, they're
9355 such thin wrappers that instantiating temporaries at every use
9356 loses nothing and saves permanent storage for the freelist object.
9358 Member functions next, anew, poison and reinit have default
9359 implementations that work for most of the types we're interested
9360 in, but if they don't work for some type, they should be explicitly
9361 specialized. See the comments before them for requirements, and
9362 the example specializations for the tree_list_freelist. */
9363 template <typename T
>
9366 /* Return the next object in a chain. We could just do type
9367 punning, but if we access the object with its underlying type, we
9368 avoid strict-aliasing trouble. This needs only work between
9369 poison and reinit. */
9370 static T
*&next (T
*obj
) { return obj
->next
; }
9372 /* Return a newly allocated, uninitialized or minimally-initialized
9373 object of type T. Any initialization performed by anew should
9374 either remain across the life of the object and the execution of
9375 poison, or be redone by reinit. */
9376 static T
*anew () { return ggc_alloc
<T
> (); }
9378 /* Optionally scribble all over the bits holding the object, so that
9379 they become (mostly?) uninitialized memory. This is called while
9380 preparing to make the object part of the free list. */
9381 static void poison (T
*obj
) {
9382 T
*p ATTRIBUTE_UNUSED
= obj
;
9383 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9385 #ifdef ENABLE_GC_CHECKING
9386 /* Poison the data, to indicate the data is garbage. */
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9388 memset (p
, 0xa5, sizeof (*p
));
9390 /* Let valgrind know the object is free. */
9391 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9393 /* Let valgrind know the next portion of the object is available,
9394 but uninitialized. */
9395 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9398 /* Bring an object that underwent at least one lifecycle after anew
9399 and before the most recent free and poison, back to a usable
9400 state, reinitializing whatever is needed for it to be
9401 functionally equivalent to an object just allocated and returned
9402 by anew. This may poison or clear the next field, used by
9403 freelist housekeeping after poison was called. */
9404 static void reinit (T
*obj
) {
9405 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9407 #ifdef ENABLE_GC_CHECKING
9408 memset (q
, 0xa5, sizeof (*q
));
9410 /* Let valgrind know the entire object is available, but
9412 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9415 /* Reference a GTY-deletable pointer that points to the first object
9416 in the free list proper. */
9419 /* Construct a freelist object chaining objects off of HEAD. */
9420 freelist (T
*&head
) : head(head
) {}
9422 /* Add OBJ to the free object list. The former head becomes OBJ's
9431 /* Take an object from the free list, if one is available, or
9432 allocate a new one. Objects taken from the free list should be
9433 regarded as filled with garbage, except for bits that are
9434 configured to be preserved across free and alloc. */
9449 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9450 want to allocate a TREE_LIST using the usual interface, and ensure
9451 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9452 build_tree_list logic in reinit, so this could go out of sync. */
9455 freelist
<tree_node
>::next (tree obj
)
9457 return TREE_CHAIN (obj
);
9461 freelist
<tree_node
>::anew ()
9463 return build_tree_list (NULL
, NULL
);
9467 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9469 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9470 tree p ATTRIBUTE_UNUSED
= obj
;
9471 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9472 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9474 #ifdef ENABLE_GC_CHECKING
9475 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9477 /* Poison the data, to indicate the data is garbage. */
9478 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9479 memset (p
, 0xa5, size
);
9481 /* Let valgrind know the object is free. */
9482 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9483 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9484 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9485 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9487 #ifdef ENABLE_GC_CHECKING
9488 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9489 /* Keep TREE_CHAIN functional. */
9490 TREE_SET_CODE (obj
, TREE_LIST
);
9492 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9497 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9499 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9501 #ifdef ENABLE_GC_CHECKING
9502 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9503 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9504 memset (obj
, 0, sizeof (tree_list
));
9507 /* Let valgrind know the entire object is available, but
9509 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9511 #ifdef ENABLE_GC_CHECKING
9512 TREE_SET_CODE (obj
, TREE_LIST
);
9514 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9518 /* Point to the first object in the TREE_LIST freelist. */
9519 static GTY((deletable
)) tree tree_list_freelist_head
;
9520 /* Return the/an actual TREE_LIST freelist. */
9521 static inline freelist
<tree_node
>
9522 tree_list_freelist ()
9524 return tree_list_freelist_head
;
9527 /* Point to the first object in the tinst_level freelist. */
9528 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9529 /* Return the/an actual tinst_level freelist. */
9530 static inline freelist
<tinst_level
>
9531 tinst_level_freelist ()
9533 return tinst_level_freelist_head
;
9536 /* Point to the first object in the pending_template freelist. */
9537 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9538 /* Return the/an actual pending_template freelist. */
9539 static inline freelist
<pending_template
>
9540 pending_template_freelist ()
9542 return pending_template_freelist_head
;
9545 /* Build the TREE_LIST object out of a split list, store it
9546 permanently, and return it. */
9548 tinst_level::to_list ()
9550 gcc_assert (split_list_p ());
9551 tree ret
= tree_list_freelist ().alloc ();
9552 TREE_PURPOSE (ret
) = tldcl
;
9553 TREE_VALUE (ret
) = targs
;
9556 gcc_assert (tree_list_p ());
9560 const unsigned short tinst_level::refcount_infinity
;
9562 /* Increment OBJ's refcount unless it is already infinite. */
9563 static tinst_level
*
9564 inc_refcount_use (tinst_level
*obj
)
9566 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9571 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9573 tinst_level::free (tinst_level
*obj
)
9575 if (obj
->tree_list_p ())
9576 tree_list_freelist ().free (obj
->get_node ());
9577 tinst_level_freelist ().free (obj
);
9580 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9581 OBJ's DECL and OBJ, and start over with the tinst_level object that
9582 used to be referenced by OBJ's NEXT. */
9584 dec_refcount_use (tinst_level
*obj
)
9587 && obj
->refcount
!= tinst_level::refcount_infinity
9588 && !--obj
->refcount
)
9590 tinst_level
*next
= obj
->next
;
9591 tinst_level::free (obj
);
9596 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9597 and of the former PTR. Omitting the second argument is equivalent
9598 to passing (T*)NULL; this is allowed because passing the
9599 zero-valued integral constant NULL confuses type deduction and/or
9600 overload resolution. */
9601 template <typename T
>
9603 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9606 ptr
= inc_refcount_use (obj
);
9607 dec_refcount_use (save
);
9611 add_pending_template (tree d
)
9613 tree ti
= (TYPE_P (d
)
9614 ? CLASSTYPE_TEMPLATE_INFO (d
)
9615 : DECL_TEMPLATE_INFO (d
));
9616 struct pending_template
*pt
;
9619 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9622 /* We are called both from instantiate_decl, where we've already had a
9623 tinst_level pushed, and instantiate_template, where we haven't.
9625 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9626 level
= !current_tinst_level
9627 || current_tinst_level
->maybe_get_node () != d
;
9630 push_tinst_level (d
);
9632 pt
= pending_template_freelist ().alloc ();
9635 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9636 if (last_pending_template
)
9637 last_pending_template
->next
= pt
;
9639 pending_templates
= pt
;
9641 last_pending_template
= pt
;
9643 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9650 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9651 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9652 documentation for TEMPLATE_ID_EXPR. */
9655 lookup_template_function (tree fns
, tree arglist
)
9657 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9658 return error_mark_node
;
9660 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9662 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9664 error ("%q#D is not a function template", fns
);
9665 return error_mark_node
;
9668 if (BASELINK_P (fns
))
9670 fns
= copy_node (fns
);
9671 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9673 BASELINK_FUNCTIONS (fns
),
9678 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9681 /* Within the scope of a template class S<T>, the name S gets bound
9682 (in build_self_reference) to a TYPE_DECL for the class, not a
9683 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9684 or one of its enclosing classes, and that type is a template,
9685 return the associated TEMPLATE_DECL. Otherwise, the original
9688 Also handle the case when DECL is a TREE_LIST of ambiguous
9689 injected-class-names from different bases. */
9692 maybe_get_template_decl_from_type_decl (tree decl
)
9694 if (decl
== NULL_TREE
)
9697 /* DR 176: A lookup that finds an injected-class-name (10.2
9698 [class.member.lookup]) can result in an ambiguity in certain cases
9699 (for example, if it is found in more than one base class). If all of
9700 the injected-class-names that are found refer to specializations of
9701 the same class template, and if the name is followed by a
9702 template-argument-list, the reference refers to the class template
9703 itself and not a specialization thereof, and is not ambiguous. */
9704 if (TREE_CODE (decl
) == TREE_LIST
)
9706 tree t
, tmpl
= NULL_TREE
;
9707 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9709 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9712 else if (tmpl
!= elt
)
9715 if (tmpl
&& t
== NULL_TREE
)
9721 return (decl
!= NULL_TREE
9722 && DECL_SELF_REFERENCE_P (decl
)
9723 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9724 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9727 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9728 parameters, find the desired type.
9730 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9732 IN_DECL, if non-NULL, is the template declaration we are trying to
9735 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9736 the class we are looking up.
9738 Issue error and warning messages under control of COMPLAIN.
9740 If the template class is really a local class in a template
9741 function, then the FUNCTION_CONTEXT is the function in which it is
9744 ??? Note that this function is currently called *twice* for each
9745 template-id: the first time from the parser, while creating the
9746 incomplete type (finish_template_type), and the second type during the
9747 real instantiation (instantiate_template_class). This is surely something
9748 that we want to avoid. It also causes some problems with argument
9749 coercion (see convert_nontype_argument for more information on this). */
9752 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9753 int entering_scope
, tsubst_flags_t complain
)
9755 auto_timevar
tv (TV_TEMPLATE_INST
);
9757 tree templ
= NULL_TREE
, parmlist
;
9764 if (identifier_p (d1
))
9766 tree value
= innermost_non_namespace_value (d1
);
9767 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9772 push_decl_namespace (context
);
9773 templ
= lookup_name (d1
);
9774 templ
= maybe_get_template_decl_from_type_decl (templ
);
9776 pop_decl_namespace ();
9779 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9781 tree type
= TREE_TYPE (d1
);
9783 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9784 an implicit typename for the second A. Deal with it. */
9785 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9786 type
= TREE_TYPE (type
);
9788 if (CLASSTYPE_TEMPLATE_INFO (type
))
9790 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9791 d1
= DECL_NAME (templ
);
9794 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9795 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9797 templ
= TYPE_TI_TEMPLATE (d1
);
9798 d1
= DECL_NAME (templ
);
9800 else if (DECL_TYPE_TEMPLATE_P (d1
))
9803 d1
= DECL_NAME (templ
);
9805 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9808 d1
= DECL_NAME (templ
);
9811 /* Issue an error message if we didn't find a template. */
9814 if (complain
& tf_error
)
9815 error ("%qT is not a template", d1
);
9816 return error_mark_node
;
9819 if (TREE_CODE (templ
) != TEMPLATE_DECL
9820 /* Make sure it's a user visible template, if it was named by
9822 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9823 && !PRIMARY_TEMPLATE_P (templ
)))
9825 if (complain
& tf_error
)
9827 error ("non-template type %qT used as a template", d1
);
9829 error ("for template declaration %q+D", in_decl
);
9831 return error_mark_node
;
9834 complain
&= ~tf_user
;
9836 /* An alias that just changes the name of a template is equivalent to the
9837 other template, so if any of the arguments are pack expansions, strip
9838 the alias to avoid problems with a pack expansion passed to a non-pack
9839 alias template parameter (DR 1430). */
9840 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9841 templ
= get_underlying_template (templ
);
9843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9846 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9847 if (arglist2
== error_mark_node
9848 || (!uses_template_parms (arglist2
)
9849 && check_instantiated_args (templ
, arglist2
, complain
)))
9850 return error_mark_node
;
9852 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9857 tree template_type
= TREE_TYPE (templ
);
9860 tree found
= NULL_TREE
;
9863 int is_dependent_type
;
9864 int use_partial_inst_tmpl
= false;
9866 if (template_type
== error_mark_node
)
9867 /* An error occurred while building the template TEMPL, and a
9868 diagnostic has most certainly been emitted for that
9869 already. Let's propagate that error. */
9870 return error_mark_node
;
9872 gen_tmpl
= most_general_template (templ
);
9874 lazy_load_pendings (gen_tmpl
);
9876 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9877 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9878 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9880 if (arg_depth
== 1 && parm_depth
> 1)
9882 /* We've been given an incomplete set of template arguments.
9885 template <class T> struct S1 {
9886 template <class U> struct S2 {};
9887 template <class U> struct S2<U*> {};
9890 we will be called with an ARGLIST of `U*', but the
9891 TEMPLATE will be `template <class T> template
9892 <class U> struct S1<T>::S2'. We must fill in the missing
9894 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9895 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9896 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9899 /* Now we should have enough arguments. */
9900 gcc_assert (parm_depth
== arg_depth
);
9902 /* From here on, we're only interested in the most general
9905 /* Shortcut looking up the current class scope again. */
9906 if (current_class_type
)
9907 if (tree ti
= CLASSTYPE_TEMPLATE_INFO (current_class_type
))
9908 if (gen_tmpl
== most_general_template (TI_TEMPLATE (ti
))
9909 && comp_template_args (arglist
, TI_ARGS (ti
)))
9910 return current_class_type
;
9912 /* Calculate the BOUND_ARGS. These will be the args that are
9913 actually tsubst'd into the definition to create the
9915 arglist
= coerce_template_parms (parmlist
, arglist
, gen_tmpl
, complain
);
9917 if (arglist
== error_mark_node
)
9918 /* We were unable to bind the arguments. */
9919 return error_mark_node
;
9921 /* In the scope of a template class, explicit references to the
9922 template class refer to the type of the template, not any
9923 instantiation of it. For example, in:
9925 template <class T> class C { void f(C<T>); }
9927 the `C<T>' is just the same as `C'. Outside of the
9928 class, however, such a reference is an instantiation. */
9930 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9931 || currently_open_class (template_type
))
9933 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9935 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9936 return template_type
;
9939 /* If we already have this specialization, return it. */
9940 elt
.tmpl
= gen_tmpl
;
9942 elt
.spec
= NULL_TREE
;
9943 hash
= spec_hasher::hash (&elt
);
9944 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9949 /* If the template's constraints are not satisfied,
9950 then we cannot form a valid type.
9952 Note that the check is deferred until after the hash
9953 lookup. This prevents redundant checks on previously
9954 instantiated specializations. */
9956 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9957 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9959 if (complain
& tf_error
)
9961 auto_diagnostic_group d
;
9962 error ("template constraint failure for %qD", gen_tmpl
);
9963 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9965 return error_mark_node
;
9968 is_dependent_type
= uses_template_parms (arglist
);
9970 /* If the deduced arguments are invalid, then the binding
9972 if (!is_dependent_type
9973 && check_instantiated_args (gen_tmpl
,
9974 INNERMOST_TEMPLATE_ARGS (arglist
),
9976 return error_mark_node
;
9978 if (!is_dependent_type
9979 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9980 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9981 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9982 /* This occurs when the user has tried to define a tagged type
9983 in a scope that forbids it. We emitted an error during the
9984 parse. We didn't complete the bail out then, so here we
9986 return error_mark_node
;
9988 context
= DECL_CONTEXT (gen_tmpl
);
9989 if (context
&& TYPE_P (context
))
9991 if (!uses_template_parms (DECL_CONTEXT (templ
)))
9992 /* If the context of the partially instantiated template is
9993 already non-dependent, then we might as well use it. */
9994 context
= DECL_CONTEXT (templ
);
9997 context
= tsubst_aggr_type (context
, arglist
,
9998 complain
, in_decl
, true);
9999 /* Try completing the enclosing context if it's not already so. */
10000 if (context
!= error_mark_node
10001 && !COMPLETE_TYPE_P (context
))
10003 context
= complete_type (context
);
10004 if (COMPLETE_TYPE_P (context
))
10006 /* Completion could have caused us to register the desired
10007 specialization already, so check the table again. */
10008 entry
= type_specializations
->find_with_hash (&elt
, hash
);
10010 return entry
->spec
;
10016 context
= tsubst (context
, arglist
, complain
, in_decl
);
10018 if (context
== error_mark_node
)
10019 return error_mark_node
;
10022 context
= global_namespace
;
10024 /* Create the type. */
10025 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10027 /* The user referred to a specialization of an alias
10028 template represented by GEN_TMPL.
10030 [temp.alias]/2 says:
10032 When a template-id refers to the specialization of an
10033 alias template, it is equivalent to the associated
10034 type obtained by substitution of its
10035 template-arguments for the template-parameters in the
10036 type-id of the alias template. */
10038 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
10039 /* Note that the call above (by indirectly calling
10040 register_specialization in tsubst_decl) registers the
10041 TYPE_DECL representing the specialization of the alias
10042 template. So next time someone substitutes ARGLIST for
10043 the template parms into the alias template (GEN_TMPL),
10044 she'll get that TYPE_DECL back. */
10046 if (t
== error_mark_node
)
10049 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
10051 if (!is_dependent_type
)
10053 set_current_access_from_decl (TYPE_NAME (template_type
));
10054 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
10055 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
10056 arglist
, complain
, in_decl
),
10057 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
10058 arglist
, complain
, in_decl
),
10059 SCOPED_ENUM_P (template_type
), NULL
);
10061 if (t
== error_mark_node
)
10066 /* We don't want to call start_enum for this type, since
10067 the values for the enumeration constants may involve
10068 template parameters. And, no one should be interested
10069 in the enumeration constants for such a type. */
10070 t
= cxx_make_type (ENUMERAL_TYPE
);
10071 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
10073 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
10074 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
10075 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
10077 else if (CLASS_TYPE_P (template_type
))
10079 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10080 instantiated here. */
10081 gcc_assert (!LAMBDA_TYPE_P (template_type
));
10083 t
= make_class_type (TREE_CODE (template_type
));
10084 CLASSTYPE_DECLARED_CLASS (t
)
10085 = CLASSTYPE_DECLARED_CLASS (template_type
);
10086 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
10088 /* A local class. Make sure the decl gets registered properly. */
10089 if (context
== current_function_decl
)
10090 if (pushtag (DECL_NAME (gen_tmpl
), t
)
10091 == error_mark_node
)
10092 return error_mark_node
;
10094 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
10095 /* This instantiation is another name for the primary
10096 template type. Set the TYPE_CANONICAL field
10098 TYPE_CANONICAL (t
) = template_type
;
10099 else if (any_template_arguments_need_structural_equality_p (arglist
))
10100 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10103 gcc_unreachable ();
10105 /* If we called start_enum or pushtag above, this information
10106 will already be set up. */
10107 type_decl
= TYPE_NAME (t
);
10110 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10112 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10113 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10114 DECL_SOURCE_LOCATION (type_decl
)
10115 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10118 set_instantiating_module (type_decl
);
10119 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10120 of export flag. We want to propagate this because it might
10121 be a friend declaration that pushes a new hidden binding. */
10122 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10124 if (CLASS_TYPE_P (template_type
))
10126 TREE_PRIVATE (type_decl
)
10127 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10128 TREE_PROTECTED (type_decl
)
10129 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10130 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10132 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10133 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10137 if (OVERLOAD_TYPE_P (t
)
10138 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10140 static const char *tags
[] = {"abi_tag", "may_alias"};
10142 for (unsigned ix
= 0; ix
!= 2; ix
++)
10145 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10148 TYPE_ATTRIBUTES (t
)
10149 = tree_cons (TREE_PURPOSE (attributes
),
10150 TREE_VALUE (attributes
),
10151 TYPE_ATTRIBUTES (t
));
10155 /* Let's consider the explicit specialization of a member
10156 of a class template specialization that is implicitly instantiated,
10161 template<class U> struct M {}; //#0
10166 struct S<int>::M<char> //#1
10170 [temp.expl.spec]/4 says this is valid.
10172 In this case, when we write:
10175 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10178 When we encounter #1, we want to store the partial instantiation
10179 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10181 For all cases other than this "explicit specialization of member of a
10182 class template", we just want to store the most general template into
10183 the CLASSTYPE_TI_TEMPLATE of M.
10185 This case of "explicit specialization of member of a class template"
10187 1/ the enclosing class is an instantiation of, and therefore not
10188 the same as, the context of the most general template, and
10189 2/ we aren't looking at the partial instantiation itself, i.e.
10190 the innermost arguments are not the same as the innermost parms of
10191 the most general template.
10193 So it's only when 1/ and 2/ happens that we want to use the partial
10194 instantiation of the member template in lieu of its most general
10197 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10198 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10199 /* the enclosing class must be an instantiation... */
10200 && CLASS_TYPE_P (context
)
10201 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10203 TREE_VEC_LENGTH (arglist
)--;
10204 ++processing_template_decl
;
10205 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10206 tree partial_inst_args
=
10207 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10208 arglist
, complain
, NULL_TREE
);
10209 --processing_template_decl
;
10210 TREE_VEC_LENGTH (arglist
)++;
10211 if (partial_inst_args
== error_mark_node
)
10212 return error_mark_node
;
10213 use_partial_inst_tmpl
=
10214 /*...and we must not be looking at the partial instantiation
10216 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10217 partial_inst_args
);
10220 if (!use_partial_inst_tmpl
)
10221 /* This case is easy; there are no member templates involved. */
10225 /* This is a full instantiation of a member template. Find
10226 the partial instantiation of which this is an instance. */
10228 /* Temporarily reduce by one the number of levels in the ARGLIST
10229 so as to avoid comparing the last set of arguments. */
10230 TREE_VEC_LENGTH (arglist
)--;
10231 /* We don't use COMPLAIN in the following call because this isn't
10232 the immediate context of deduction. For instance, tf_partial
10233 could be set here as we might be at the beginning of template
10234 argument deduction when any explicitly specified template
10235 arguments are substituted into the function type. tf_partial
10236 could lead into trouble because we wouldn't find the partial
10237 instantiation that might have been created outside tf_partial
10238 context, because the levels of template parameters wouldn't
10239 match, because in a tf_partial context, tsubst doesn't reduce
10240 TEMPLATE_PARM_LEVEL. */
10241 found
= tsubst (gen_tmpl
, arglist
, tf_none
, NULL_TREE
);
10242 TREE_VEC_LENGTH (arglist
)++;
10243 /* FOUND is either a proper class type, or an alias
10244 template specialization. In the later case, it's a
10245 TYPE_DECL, resulting from the substituting of arguments
10246 for parameters in the TYPE_DECL of the alias template
10247 done earlier. So be careful while getting the template
10249 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10251 : (TREE_CODE (found
) == TYPE_DECL
10252 ? DECL_TI_TEMPLATE (found
)
10253 : CLASSTYPE_TI_TEMPLATE (found
)));
10255 if (DECL_CLASS_TEMPLATE_P (found
)
10256 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10258 /* If this partial instantiation is specialized, we want to
10259 use it for hash table lookup. */
10261 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10262 hash
= spec_hasher::hash (&elt
);
10266 /* Build template info for the new specialization. */
10267 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10270 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10271 gcc_checking_assert (*slot
== NULL
);
10272 entry
= ggc_alloc
<spec_entry
> ();
10276 /* Note this use of the partial instantiation so we can check it
10277 later in maybe_process_partial_specialization. */
10278 DECL_TEMPLATE_INSTANTIATIONS (found
)
10279 = tree_cons (arglist
, t
,
10280 DECL_TEMPLATE_INSTANTIATIONS (found
));
10282 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
10283 && !uses_template_parms (current_nonlambda_scope ())
10284 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10285 /* Now that the type has been registered on the instantiations
10286 list, we set up the enumerators. Because the enumeration
10287 constants may involve the enumeration type itself, we make
10288 sure to register the type first, and then create the
10289 constants. That way, doing tsubst_expr for the enumeration
10290 constants won't result in recursive calls here; we'll find
10291 the instantiation and exit above. */
10292 tsubst_enum (template_type
, t
, arglist
);
10294 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10295 /* If the type makes use of template parameters, the
10296 code that generates debugging information will crash. */
10297 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10299 /* Possibly limit visibility based on template args. */
10300 TREE_PUBLIC (type_decl
) = 1;
10301 determine_visibility (type_decl
);
10303 inherit_targ_abi_tags (t
);
10309 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10312 lookup_template_variable (tree templ
, tree arglist
)
10314 if (flag_concepts
&& variable_concept_p (templ
))
10315 return build_concept_check (templ
, arglist
, tf_none
);
10317 /* The type of the expression is NULL_TREE since the template-id could refer
10318 to an explicit or partial specialization. */
10319 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10322 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10325 finish_template_variable (tree var
, tsubst_flags_t complain
)
10327 tree templ
= TREE_OPERAND (var
, 0);
10328 tree arglist
= TREE_OPERAND (var
, 1);
10330 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10331 arglist
= coerce_template_parms (parms
, arglist
, templ
, complain
);
10332 if (arglist
== error_mark_node
)
10333 return error_mark_node
;
10335 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10337 if (complain
& tf_error
)
10339 auto_diagnostic_group d
;
10340 error ("use of invalid variable template %qE", var
);
10341 diagnose_constraints (location_of (var
), templ
, arglist
);
10343 return error_mark_node
;
10346 return instantiate_template (templ
, arglist
, complain
);
10349 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10350 TARGS template args, and instantiate it if it's not dependent. */
10353 lookup_and_finish_template_variable (tree templ
, tree targs
,
10354 tsubst_flags_t complain
)
10356 tree var
= lookup_template_variable (templ
, targs
);
10357 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ
)) == 1
10358 && !any_dependent_template_arguments_p (targs
))
10360 var
= finish_template_variable (var
, complain
);
10364 return convert_from_reference (var
);
10367 /* If the set of template parameters PARMS contains a template parameter
10368 at the given LEVEL and INDEX, then return this parameter. Otherwise
10369 return NULL_TREE. */
10372 corresponding_template_parameter (tree parms
, int level
, int index
)
10374 while (TMPL_PARMS_DEPTH (parms
) > level
)
10375 parms
= TREE_CHAIN (parms
);
10377 if (TMPL_PARMS_DEPTH (parms
) != level
10378 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10381 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10382 /* As in template_parm_to_arg. */
10383 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10386 t
= DECL_INITIAL (t
);
10388 gcc_assert (TEMPLATE_PARM_P (t
));
10392 /* Return the template parameter from PARMS that positionally corresponds
10393 to the template parameter PARM, or else return NULL_TREE. */
10396 corresponding_template_parameter (tree parms
, tree parm
)
10399 template_parm_level_and_index (parm
, &level
, &index
);
10400 return corresponding_template_parameter (parms
, level
, index
);
10404 struct pair_fn_data
10409 /* True when we should also visit template parameters that occur in
10410 non-deduced contexts. */
10411 bool include_nondeduced_p
;
10412 hash_set
<tree
> *visited
;
10415 /* Called from for_each_template_parm via walk_tree. */
10418 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10421 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10422 tree_fn_t fn
= pfd
->fn
;
10423 void *data
= pfd
->data
;
10424 tree result
= NULL_TREE
;
10426 #define WALK_SUBTREE(NODE) \
10429 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10430 pfd->include_nondeduced_p, \
10432 if (result) goto out; \
10436 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10440 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10441 WALK_SUBTREE (TYPE_CONTEXT (t
));
10443 switch (TREE_CODE (t
))
10446 if (TYPE_PTRMEMFUNC_P (t
))
10448 /* Fall through. */
10451 case ENUMERAL_TYPE
:
10452 if (!TYPE_TEMPLATE_INFO (t
))
10453 *walk_subtrees
= 0;
10455 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10459 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10460 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10464 /* Since we're not going to walk subtrees, we have to do this
10465 explicitly here. */
10466 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10467 /* Fall through. */
10469 case FUNCTION_TYPE
:
10470 /* Check the return type. */
10471 WALK_SUBTREE (TREE_TYPE (t
));
10473 /* Check the parameter types. Since default arguments are not
10474 instantiated until they are needed, the TYPE_ARG_TYPES may
10475 contain expressions that involve template parameters. But,
10476 no-one should be looking at them yet. And, once they're
10477 instantiated, they don't contain template parameters, so
10478 there's no point in looking at them then, either. */
10482 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10483 WALK_SUBTREE (TREE_VALUE (parm
));
10485 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10486 want walk_tree walking into them itself. */
10487 *walk_subtrees
= 0;
10490 if (flag_noexcept_type
)
10492 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10494 WALK_SUBTREE (TREE_PURPOSE (spec
));
10499 case DECLTYPE_TYPE
:
10500 if (pfd
->include_nondeduced_p
10501 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10503 pfd
->include_nondeduced_p
,
10505 return error_mark_node
;
10506 *walk_subtrees
= false;
10510 if (pfd
->include_nondeduced_p
)
10512 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t
));
10513 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t
));
10515 *walk_subtrees
= false;
10518 case FUNCTION_DECL
:
10520 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10521 WALK_SUBTREE (DECL_TI_ARGS (t
));
10525 WALK_SUBTREE (TREE_TYPE (t
));
10529 if (DECL_TEMPLATE_PARM_P (t
))
10530 WALK_SUBTREE (DECL_INITIAL (t
));
10531 if (DECL_CONTEXT (t
)
10532 && pfd
->include_nondeduced_p
)
10533 WALK_SUBTREE (DECL_CONTEXT (t
));
10536 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10537 /* Record template parameters such as `T' inside `TT<T>'. */
10538 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10539 /* Fall through. */
10541 case TEMPLATE_TEMPLATE_PARM
:
10542 case TEMPLATE_TYPE_PARM
:
10543 case TEMPLATE_PARM_INDEX
:
10544 if (fn
&& (*fn
)(t
, data
))
10550 case TEMPLATE_DECL
:
10551 /* A template template parameter is encountered. */
10552 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10553 WALK_SUBTREE (TREE_TYPE (t
));
10555 /* Already substituted template template parameter */
10556 *walk_subtrees
= 0;
10559 case TYPENAME_TYPE
:
10560 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10561 partial instantiation. */
10562 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10563 *walk_subtrees
= 0;
10567 case COMPONENT_REF
:
10568 /* If there's no type, then this thing must be some expression
10569 involving template parameters. */
10570 if (!fn
&& !TREE_TYPE (t
))
10571 return error_mark_node
;
10579 /* These are non-deduced contexts. */
10580 if (!pfd
->include_nondeduced_p
)
10581 *walk_subtrees
= 0;
10586 case IMPLICIT_CONV_EXPR
:
10587 case REINTERPRET_CAST_EXPR
:
10588 case CONST_CAST_EXPR
:
10589 case STATIC_CAST_EXPR
:
10590 case DYNAMIC_CAST_EXPR
:
10594 case PSEUDO_DTOR_EXPR
:
10596 return error_mark_node
;
10603 #undef WALK_SUBTREE
10605 /* We didn't find any template parameters we liked. */
10610 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10611 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10612 call FN with the parameter and the DATA.
10613 If FN returns nonzero, the iteration is terminated, and
10614 for_each_template_parm returns 1. Otherwise, the iteration
10615 continues. If FN never returns a nonzero value, the value
10616 returned by for_each_template_parm is 0. If FN is NULL, it is
10617 considered to be the function which always returns 1.
10619 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10620 parameters that occur in non-deduced contexts. When false, only
10621 visits those template parameters that can be deduced. */
10624 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10625 hash_set
<tree
> *visited
,
10626 bool include_nondeduced_p
,
10629 struct pair_fn_data pfd
;
10634 pfd
.any_fn
= any_fn
;
10636 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10638 /* Walk the tree. (Conceptually, we would like to walk without
10639 duplicates, but for_each_template_parm_r recursively calls
10640 for_each_template_parm, so we would need to reorganize a fair
10641 bit to use walk_tree_without_duplicates, so we keep our own
10644 pfd
.visited
= visited
;
10646 pfd
.visited
= new hash_set
<tree
>;
10647 result
= cp_walk_tree (&t
,
10648 for_each_template_parm_r
,
10655 delete pfd
.visited
;
10662 struct find_template_parameter_info
10664 explicit find_template_parameter_info (tree ctx_parms
)
10665 : ctx_parms (ctx_parms
),
10666 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10669 hash_set
<tree
> visited
;
10670 hash_set
<tree
> parms
;
10671 tree parm_list
= NULL_TREE
;
10672 tree
*parm_list_tail
= &parm_list
;
10677 /* Appends the declaration of T to the list in DATA. */
10680 keep_template_parm (tree t
, void* data
)
10682 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10684 /* Template parameters declared within the expression are not part of
10685 the parameter mapping. For example, in this concept:
10687 template<typename T>
10688 concept C = requires { <expr> } -> same_as<int>;
10690 the return specifier same_as<int> declares a new decltype parameter
10691 that must not be part of the parameter mapping. The same is true
10692 for generic lambda parameters, lambda template parameters, etc. */
10695 template_parm_level_and_index (t
, &level
, &index
);
10696 if (level
== 0 || level
> ftpi
->max_depth
)
10699 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10700 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10701 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10702 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10704 /* This template parameter might be an argument to a cached dependent
10705 specalization that was formed earlier inside some other template, in
10706 which case the parameter is not among the ones that are in-scope.
10707 Look in CTX_PARMS to find the corresponding in-scope template
10708 parameter, and use it instead. */
10709 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10712 /* Arguments like const T yield parameters like const T. This means that
10713 a template-id like X<T, const T> would yield two distinct parameters:
10714 T and const T. Adjust types to their unqualified versions. */
10716 t
= TYPE_MAIN_VARIANT (t
);
10717 if (!ftpi
->parms
.add (t
))
10719 /* Append T to PARM_LIST. */
10720 tree node
= build_tree_list (NULL_TREE
, t
);
10721 *ftpi
->parm_list_tail
= node
;
10722 ftpi
->parm_list_tail
= &TREE_CHAIN (node
);
10725 /* Verify the parameter we found has a valid index. */
10728 tree parms
= ftpi
->ctx_parms
;
10729 while (TMPL_PARMS_DEPTH (parms
) > level
)
10730 parms
= TREE_CHAIN (parms
);
10731 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10732 gcc_assert (index
< len
);
10738 /* Ensure that we recursively examine certain terms that are not normally
10739 visited in for_each_template_parm_r. */
10742 any_template_parm_r (tree t
, void *data
)
10744 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10746 #define WALK_SUBTREE(NODE) \
10749 for_each_template_parm (NODE, keep_template_parm, data, \
10750 &ftpi->visited, true, \
10751 any_template_parm_r); \
10755 /* A mention of a member alias/typedef is a use of all of its template
10756 arguments, including those from the enclosing class, so we don't use
10757 alias_template_specialization_p here. */
10758 if (TYPE_P (t
) && typedef_variant_p (t
))
10759 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10760 WALK_SUBTREE (TI_ARGS (tinfo
));
10762 switch (TREE_CODE (t
))
10764 case TEMPLATE_TYPE_PARM
:
10765 /* Type constraints of a placeholder type may contain parameters. */
10767 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10768 WALK_SUBTREE (constr
);
10771 case TEMPLATE_ID_EXPR
:
10772 /* Search through references to variable templates. */
10773 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10774 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10777 case TEMPLATE_PARM_INDEX
:
10778 WALK_SUBTREE (TREE_TYPE (t
));
10781 case TEMPLATE_DECL
:
10782 /* If T is a member template that shares template parameters with
10783 ctx_parms, we need to mark all those parameters for mapping.
10784 To that end, it should suffice to just walk the DECL_CONTEXT of
10785 the template (assuming the template is not overly general). */
10786 WALK_SUBTREE (DECL_CONTEXT (t
));
10791 /* Look in the parms and body. */
10792 tree fn
= lambda_function (t
);
10793 WALK_SUBTREE (TREE_TYPE (fn
));
10794 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10798 case IDENTIFIER_NODE
:
10799 if (IDENTIFIER_CONV_OP_P (t
))
10800 /* The conversion-type-id of a conversion operator may be dependent. */
10801 WALK_SUBTREE (TREE_TYPE (t
));
10805 if (is_dummy_object (t
))
10806 WALK_SUBTREE (TREE_TYPE (t
));
10813 /* Keep walking. */
10817 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10818 are the template parameters in scope. */
10821 find_template_parameters (tree t
, tree ctx_parms
)
10826 find_template_parameter_info
ftpi (ctx_parms
);
10827 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10828 /*include_nondeduced*/true, any_template_parm_r
);
10829 return ftpi
.parm_list
;
10832 /* Returns true if T depends on any template parameter. */
10835 uses_template_parms (tree t
)
10837 if (t
== NULL_TREE
|| t
== error_mark_node
)
10840 /* Namespaces can't depend on any template parameters. */
10841 if (TREE_CODE (t
) == NAMESPACE_DECL
)
10844 processing_template_decl_sentinel
ptds (/*reset*/false);
10845 ++processing_template_decl
;
10848 return dependent_type_p (t
);
10849 else if (TREE_CODE (t
) == TREE_VEC
)
10850 return any_dependent_template_arguments_p (t
);
10851 else if (TREE_CODE (t
) == TREE_LIST
)
10852 return (uses_template_parms (TREE_VALUE (t
))
10853 || uses_template_parms (TREE_CHAIN (t
)));
10854 else if (TREE_CODE (t
) == TYPE_DECL
)
10855 return dependent_type_p (TREE_TYPE (t
));
10857 return instantiation_dependent_expression_p (t
);
10860 /* Returns true iff we're processing an incompletely instantiated function
10861 template. Useful instead of processing_template_decl because the latter
10862 is set to 0 during instantiate_non_dependent_expr. */
10865 in_template_function (void)
10867 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10868 the latter might get set for e.g. access checking during satisfaction. */
10869 tree fn
= cfun
? cfun
->decl
: NULL_TREE
;
10871 ++processing_template_decl
;
10872 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10873 && DECL_TEMPLATE_INFO (fn
)
10874 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10875 --processing_template_decl
;
10879 /* Returns true if T depends on any template parameter with level LEVEL. */
10882 uses_template_parms_level (tree t
, int level
)
10884 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10885 /*include_nondeduced_p=*/true);
10888 /* Returns true if the signature of DECL depends on any template parameter from
10889 its enclosing class. */
10892 uses_outer_template_parms (tree decl
)
10894 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10897 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10898 &depth
, NULL
, /*include_nondeduced_p=*/true))
10900 if (PRIMARY_TEMPLATE_P (decl
)
10901 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
10903 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl
));
10904 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
10906 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
10907 tree defarg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
10908 if (TREE_CODE (parm
) == PARM_DECL
10909 && for_each_template_parm (TREE_TYPE (parm
),
10910 template_parm_outer_level
,
10911 &depth
, NULL
, /*nondeduced*/true))
10913 if (TREE_CODE (parm
) == TEMPLATE_DECL
10914 && uses_outer_template_parms (parm
))
10917 && for_each_template_parm (defarg
, template_parm_outer_level
,
10918 &depth
, NULL
, /*nondeduced*/true))
10922 if (uses_outer_template_parms_in_constraints (decl
))
10927 /* Returns true if the constraints of DECL depend on any template parameters
10928 from its enclosing scope. */
10931 uses_outer_template_parms_in_constraints (tree decl
)
10933 tree ci
= get_constraints (decl
);
10935 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10938 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10941 return for_each_template_parm (ci
, template_parm_outer_level
,
10942 &depth
, NULL
, /*nondeduced*/true);
10945 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10946 ill-formed translation unit, i.e. a variable or function that isn't
10947 usable in a constant expression. */
10950 neglectable_inst_p (tree d
)
10952 return (d
&& DECL_P (d
)
10953 && !undeduced_auto_decl (d
)
10954 && !(TREE_CODE (d
) == FUNCTION_DECL
10955 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d
)
10956 : decl_maybe_constant_var_p (d
)));
10959 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10960 neglectable and instantiated from within an erroneous instantiation. */
10963 limit_bad_template_recursion (tree decl
)
10965 struct tinst_level
*lev
= current_tinst_level
;
10966 int errs
= errorcount
+ sorrycount
;
10967 if (errs
== 0 || !neglectable_inst_p (decl
))
10970 /* Avoid instantiating members of an ill-formed class. */
10972 = (DECL_CLASS_SCOPE_P (decl
)
10973 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl
)));
10977 for (; lev
; lev
= lev
->next
)
10978 if (neglectable_inst_p (lev
->maybe_get_node ()))
10980 refuse
= (lev
&& errs
> lev
->errors
);
10985 /* Don't warn about it not being defined. */
10986 suppress_warning (decl
, OPT_Wunused
);
10988 FOR_EACH_CLONE (clone
, decl
)
10989 suppress_warning (clone
, OPT_Wunused
);
10994 static int tinst_depth
;
10995 extern int max_tinst_depth
;
10998 static GTY(()) struct tinst_level
*last_error_tinst_level
;
11000 /* We're starting to instantiate D; record the template instantiation context
11001 at LOC for diagnostics and to restore it later. */
11004 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
11006 struct tinst_level
*new_level
;
11008 if (tinst_depth
>= max_tinst_depth
)
11010 /* Tell error.cc not to try to instantiate any templates. */
11012 fatal_error (input_location
,
11013 "template instantiation depth exceeds maximum of %d"
11014 " (use %<-ftemplate-depth=%> to increase the maximum)",
11019 /* If the current instantiation caused problems, don't let it instantiate
11020 anything else. Do allow deduction substitution and decls usable in
11021 constant expressions. */
11022 if (!targs
&& limit_bad_template_recursion (tldcl
))
11024 /* Avoid no_linkage_errors and unused function (and all other)
11025 warnings for this decl. */
11026 suppress_warning (tldcl
);
11030 /* When not -quiet, dump template instantiations other than functions, since
11031 announce_function will take care of those. */
11032 if (!quiet_flag
&& !targs
11033 && TREE_CODE (tldcl
) != TREE_LIST
11034 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
11035 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
11037 new_level
= tinst_level_freelist ().alloc ();
11038 new_level
->tldcl
= tldcl
;
11039 new_level
->targs
= targs
;
11040 new_level
->locus
= loc
;
11041 new_level
->errors
= errorcount
+ sorrycount
;
11042 new_level
->next
= NULL
;
11043 new_level
->refcount
= 0;
11044 new_level
->path
= new_level
->visible
= nullptr;
11045 set_refcount_ptr (new_level
->next
, current_tinst_level
);
11046 set_refcount_ptr (current_tinst_level
, new_level
);
11049 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
11050 depth_reached
= tinst_depth
;
11055 /* We're starting substitution of TMPL<ARGS>; record the template
11056 substitution context for diagnostics and to restore it later. */
11059 push_tinst_level (tree tmpl
, tree args
)
11061 return push_tinst_level_loc (tmpl
, args
, input_location
);
11064 /* We're starting to instantiate D; record INPUT_LOCATION and the
11065 template instantiation context for diagnostics and to restore it
11069 push_tinst_level (tree d
)
11071 return push_tinst_level_loc (d
, input_location
);
11074 /* Likewise, but record LOC as the program location. */
11077 push_tinst_level_loc (tree d
, location_t loc
)
11079 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
11080 return push_tinst_level_loc (d
, NULL
, loc
);
11083 /* We're done instantiating this template; return to the instantiation
11087 pop_tinst_level (void)
11089 /* Restore the filename and line number stashed away when we started
11090 this instantiation. */
11091 input_location
= current_tinst_level
->locus
;
11092 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
11096 /* We're instantiating a deferred template; restore the template
11097 instantiation context in which the instantiation was requested, which
11098 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11101 reopen_tinst_level (struct tinst_level
*level
)
11103 struct tinst_level
*t
;
11106 for (t
= level
; t
; t
= t
->next
)
11109 set_refcount_ptr (current_tinst_level
, level
);
11110 pop_tinst_level ();
11111 if (current_tinst_level
)
11112 current_tinst_level
->errors
= errorcount
+sorrycount
;
11113 return level
->maybe_get_node ();
11116 /* Returns the TINST_LEVEL which gives the original instantiation
11119 struct tinst_level
*
11120 outermost_tinst_level (void)
11122 struct tinst_level
*level
= current_tinst_level
;
11124 while (level
->next
)
11125 level
= level
->next
;
11129 /* True iff T is a friend function declaration that is not itself a template
11130 and is not defined in a class template. */
11133 non_templated_friend_p (tree t
)
11135 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
11136 && DECL_UNIQUE_FRIEND_P (t
))
11138 tree ti
= DECL_TEMPLATE_INFO (t
);
11141 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11142 if (DECL_FRIEND_CONTEXT (t
))
11144 /* Non-templated friends in a class template are still represented with a
11145 TEMPLATE_DECL; check that its primary template is the befriending
11146 class. Note that DECL_PRIMARY_TEMPLATE is null for
11147 template <class T> friend A<T>::f(); */
11148 tree tmpl
= TI_TEMPLATE (ti
);
11149 tree primary
= DECL_PRIMARY_TEMPLATE (tmpl
);
11150 return (primary
&& primary
!= tmpl
);
11156 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11157 vector of template arguments, as for tsubst.
11159 Returns an appropriate tsubst'd friend declaration. */
11162 tsubst_friend_function (tree decl
, tree args
)
11166 if (TREE_CODE (decl
) == FUNCTION_DECL
11167 && DECL_TEMPLATE_INSTANTIATION (decl
)
11168 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11169 /* This was a friend declared with an explicit template
11170 argument list, e.g.:
11172 friend void f<>(T);
11174 to indicate that f was a template instantiation, not a new
11175 function declaration. Now, we have to figure out what
11176 instantiation of what template. */
11178 tree template_id
, arglist
, fns
;
11181 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11183 /* Friend functions are looked up in the containing namespace scope.
11184 We must enter that scope, to avoid finding member functions of the
11185 current class with same name. */
11186 push_nested_namespace (ns
);
11187 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11188 tf_warning_or_error
, NULL_TREE
);
11189 pop_nested_namespace (ns
);
11190 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11191 tf_warning_or_error
, NULL_TREE
);
11192 template_id
= lookup_template_function (fns
, arglist
);
11194 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11195 tmpl
= determine_specialization (template_id
, new_friend
,
11197 /*need_member_template=*/0,
11198 TREE_VEC_LENGTH (args
),
11200 return instantiate_template (tmpl
, new_args
, tf_error
);
11203 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11204 if (new_friend
== error_mark_node
)
11205 return error_mark_node
;
11207 /* The NEW_FRIEND will look like an instantiation, to the
11208 compiler, but is not an instantiation from the point of view of
11209 the language. For example, we might have had:
11211 template <class T> struct S {
11212 template <class U> friend void f(T, U);
11215 Then, in S<int>, template <class U> void f(int, U) is not an
11216 instantiation of anything. */
11218 DECL_USE_TEMPLATE (new_friend
) = 0;
11219 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11221 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11222 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11223 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11224 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11226 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11227 match in decls_match. */
11228 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11229 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11230 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11231 if (treqs
!= TEMPLATE_PARMS_CONSTRAINTS (parms
))
11233 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11234 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11235 tsubst_each_template_parm_constraints (parms
, args
,
11236 tf_warning_or_error
);
11240 /* The mangled name for the NEW_FRIEND is incorrect. The function
11241 is not a template instantiation and should not be mangled like
11242 one. Therefore, we forget the mangling here; we'll recompute it
11243 later if we need it. */
11244 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11246 SET_DECL_RTL (new_friend
, NULL
);
11247 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11250 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11255 /* We must save some information from NEW_FRIEND before calling
11256 duplicate decls since that function will free NEW_FRIEND if
11258 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11259 tree new_friend_result_template_info
= NULL_TREE
;
11260 bool new_friend_is_defn
=
11261 (new_friend_template_info
11262 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11263 (template_for_substitution (new_friend
)))
11265 tree not_tmpl
= new_friend
;
11267 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11269 /* This declaration is a `primary' template. */
11270 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11272 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11273 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11275 else if (!constraints_satisfied_p (new_friend
))
11276 /* Only define a constrained hidden friend when satisfied. */
11277 return error_mark_node
;
11279 /* Inside pushdecl_namespace_level, we will push into the
11280 current namespace. However, the friend function should go
11281 into the namespace of the template. */
11282 ns
= decl_namespace_context (new_friend
);
11283 push_nested_namespace (ns
);
11284 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11285 pop_nested_namespace (ns
);
11287 if (old_decl
== error_mark_node
)
11288 return error_mark_node
;
11290 if (old_decl
!= new_friend
)
11292 /* This new friend declaration matched an existing
11293 declaration. For example, given:
11295 template <class T> void f(T);
11296 template <class U> class C {
11297 template <class T> friend void f(T) {}
11300 the friend declaration actually provides the definition
11301 of `f', once C has been instantiated for some type. So,
11302 old_decl will be the out-of-class template declaration,
11303 while new_friend is the in-class definition.
11305 But, if `f' was called before this point, the
11306 instantiation of `f' will have DECL_TI_ARGS corresponding
11307 to `T' but not to `U', references to which might appear
11308 in the definition of `f'. Previously, the most general
11309 template for an instantiation of `f' was the out-of-class
11310 version; now it is the in-class version. Therefore, we
11311 run through all specialization of `f', adding to their
11312 DECL_TI_ARGS appropriately. In particular, they need a
11313 new set of outer arguments, corresponding to the
11314 arguments for this class instantiation.
11316 The same situation can arise with something like this:
11318 friend void f(int);
11319 template <class T> class C {
11320 friend void f(T) {}
11323 when `C<int>' is instantiated. Now, `f(int)' is defined
11326 if (!new_friend_is_defn
)
11327 /* On the other hand, if the in-class declaration does
11328 *not* provide a definition, then we don't want to alter
11329 existing definitions. We can just leave everything
11334 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11335 tree new_args
= TI_ARGS (new_friend_template_info
);
11337 /* Overwrite whatever template info was there before, if
11338 any, with the new template information pertaining to
11339 the declaration. */
11340 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11342 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11344 /* We should have called reregister_specialization in
11345 duplicate_decls. */
11346 gcc_assert (retrieve_specialization (new_template
,
11350 /* Instantiate it if the global has already been used. */
11351 if (DECL_ODR_USED (old_decl
))
11352 instantiate_decl (old_decl
, /*defer_ok=*/true,
11353 /*expl_inst_class_mem_p=*/false);
11359 /* Indicate that the old function template is a partial
11361 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11362 = new_friend_result_template_info
;
11364 gcc_assert (new_template
11365 == most_general_template (new_template
));
11366 gcc_assert (new_template
!= old_decl
);
11368 /* Reassign any specializations already in the hash table
11369 to the new more general template, and add the
11370 additional template args. */
11371 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11373 t
= TREE_CHAIN (t
))
11375 tree spec
= TREE_VALUE (t
);
11378 elt
.tmpl
= old_decl
;
11379 elt
.args
= DECL_TI_ARGS (spec
);
11380 elt
.spec
= NULL_TREE
;
11382 decl_specializations
->remove_elt (&elt
);
11384 DECL_TI_ARGS (spec
)
11385 = add_outermost_template_args (new_args
,
11386 DECL_TI_ARGS (spec
));
11388 register_specialization
11389 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11392 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11396 /* The information from NEW_FRIEND has been merged into OLD_DECL
11397 by duplicate_decls. */
11398 new_friend
= old_decl
;
11401 /* We've just introduced a namespace-scope function in the purview
11402 without necessarily having opened the enclosing namespace, so
11403 make sure the namespace is in the purview now too. */
11405 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend
))
11406 && TREE_CODE (DECL_CONTEXT (new_friend
)) == NAMESPACE_DECL
)
11407 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend
)) = true;
11411 tree context
= DECL_CONTEXT (new_friend
);
11415 template <class T> class C {
11416 template <class U> friend void C1<U>::f (); // case 1
11417 friend void C2<T>::f (); // case 2
11419 we only need to make sure CONTEXT is a complete type for
11420 case 2. To distinguish between the two cases, we note that
11421 CONTEXT of case 1 remains dependent type after tsubst while
11422 this isn't true for case 2. */
11423 ++processing_template_decl
;
11424 dependent_p
= dependent_type_p (context
);
11425 --processing_template_decl
;
11428 && !complete_type_or_else (context
, NULL_TREE
))
11429 return error_mark_node
;
11431 if (COMPLETE_TYPE_P (context
))
11433 tree fn
= new_friend
;
11434 /* do_friend adds the TEMPLATE_DECL for any member friend
11435 template even if it isn't a member template, i.e.
11436 template <class T> friend A<T>::f();
11437 Look through it in that case. */
11438 if (TREE_CODE (fn
) == TEMPLATE_DECL
11439 && !PRIMARY_TEMPLATE_P (fn
))
11440 fn
= DECL_TEMPLATE_RESULT (fn
);
11441 /* Check to see that the declaration is really present, and,
11442 possibly obtain an improved declaration. */
11443 fn
= check_classfn (context
, fn
, NULL_TREE
);
11453 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11454 template arguments, as for tsubst.
11456 Returns an appropriate tsubst'd friend type or error_mark_node on
11460 tsubst_friend_class (tree friend_tmpl
, tree args
)
11464 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11466 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11467 return TREE_TYPE (tmpl
);
11470 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11471 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11472 push_nested_namespace (context
);
11475 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11476 push_nested_class (context
);
11479 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11480 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11482 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11484 /* The friend template has already been declared. Just
11485 check to see that the declarations match, and install any new
11486 default parameters. We must tsubst the default parameters,
11487 of course. We only need the innermost template parameters
11488 because that is all that redeclare_class_template will look
11490 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11491 > TMPL_ARGS_DEPTH (args
))
11493 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11494 args
, tf_warning_or_error
);
11495 tsubst_each_template_parm_constraints (parms
, args
,
11496 tf_warning_or_error
);
11497 location_t saved_input_location
= input_location
;
11498 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11499 tree cons
= get_constraints (tmpl
);
11500 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11501 input_location
= saved_input_location
;
11506 /* The friend template has not already been declared. In this
11507 case, the instantiation of the template class will cause the
11508 injection of this template into the namespace scope. */
11509 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11511 if (tmpl
!= error_mark_node
)
11513 /* The new TMPL is not an instantiation of anything, so we
11514 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11515 for the new type because that is supposed to be the
11516 corresponding template decl, i.e., TMPL. */
11517 DECL_USE_TEMPLATE (tmpl
) = 0;
11518 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11519 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11520 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11521 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11523 /* Substitute into and set the constraints on the new declaration. */
11524 if (tree ci
= get_constraints (friend_tmpl
))
11526 ++processing_template_decl
;
11527 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11528 DECL_FRIEND_CONTEXT (friend_tmpl
));
11529 --processing_template_decl
;
11530 set_constraints (tmpl
, ci
);
11531 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl
),
11532 args
, tf_warning_or_error
);
11535 /* Inject this template into the enclosing namspace scope. */
11536 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11540 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11541 pop_nested_namespace (context
);
11543 pop_nested_class ();
11545 return TREE_TYPE (tmpl
);
11548 /* Returns zero if TYPE cannot be completed later due to circularity.
11549 Otherwise returns one. */
11552 can_complete_type_without_circularity (tree type
)
11554 if (type
== NULL_TREE
|| type
== error_mark_node
)
11556 else if (COMPLETE_TYPE_P (type
))
11558 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11559 return can_complete_type_without_circularity (TREE_TYPE (type
));
11560 else if (CLASS_TYPE_P (type
)
11561 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11567 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11568 tsubst_flags_t
, tree
);
11570 /* Instantiate the contract statement. */
11573 tsubst_contract (tree decl
, tree t
, tree args
, tsubst_flags_t complain
,
11576 tree type
= decl
? TREE_TYPE (TREE_TYPE (decl
)) : NULL_TREE
;
11577 bool auto_p
= type_uses_auto (type
);
11579 tree r
= copy_node (t
);
11581 /* Rebuild the result variable. */
11582 if (type
&& POSTCONDITION_P (t
) && POSTCONDITION_IDENTIFIER (t
))
11584 tree oldvar
= POSTCONDITION_IDENTIFIER (t
);
11586 tree newvar
= copy_node (oldvar
);
11587 TREE_TYPE (newvar
) = type
;
11588 DECL_CONTEXT (newvar
) = decl
;
11589 POSTCONDITION_IDENTIFIER (r
) = newvar
;
11591 /* Make sure the postcondition is valid. */
11592 location_t loc
= DECL_SOURCE_LOCATION (oldvar
);
11594 if (!check_postcondition_result (decl
, type
, loc
))
11595 return invalidate_contract (r
);
11597 /* Make the variable available for lookup. */
11598 register_local_specialization (newvar
, oldvar
);
11601 /* Instantiate the condition. If the return type is undeduced, process
11602 the expression as if inside a template to avoid spurious type errors. */
11604 ++processing_template_decl
;
11605 ++processing_contract_condition
;
11606 CONTRACT_CONDITION (r
)
11607 = tsubst_expr (CONTRACT_CONDITION (t
), args
, complain
, in_decl
);
11608 --processing_contract_condition
;
11610 --processing_template_decl
;
11612 /* And the comment. */
11613 CONTRACT_COMMENT (r
)
11614 = tsubst_expr (CONTRACT_COMMENT (r
), args
, complain
, in_decl
);
11619 /* Update T by instantiating its contract attribute. */
11622 tsubst_contract_attribute (tree decl
, tree t
, tree args
,
11623 tsubst_flags_t complain
, tree in_decl
)
11625 /* For non-specializations, adjust the current declaration to the most general
11626 version of in_decl. Because we defer the instantiation of contracts as long
11627 as possible, they are still written in terms of the parameters (and return
11628 type) of the most general template. */
11629 tree tmpl
= DECL_TI_TEMPLATE (in_decl
);
11630 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
11631 in_decl
= DECL_TEMPLATE_RESULT (most_general_template (in_decl
));
11632 local_specialization_stack
specs (lss_copy
);
11633 register_parameter_specializations (in_decl
, decl
);
11635 /* Get the contract to be instantiated. */
11636 tree contract
= CONTRACT_STATEMENT (t
);
11638 /* Use the complete set of template arguments for instantiation. The
11639 contract may not have been instantiated and still refer to outer levels
11640 of template parameters. */
11641 args
= DECL_TI_ARGS (decl
);
11643 /* For member functions, make this available for semantic analysis. */
11644 tree save_ccp
= current_class_ptr
;
11645 tree save_ccr
= current_class_ref
;
11646 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
11648 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11649 tree this_type
= TREE_TYPE (TREE_VALUE (arg_types
));
11650 inject_this_parameter (this_type
, cp_type_quals (this_type
));
11653 contract
= tsubst_contract (decl
, contract
, args
, complain
, in_decl
);
11655 current_class_ptr
= save_ccp
;
11656 current_class_ref
= save_ccr
;
11658 /* Rebuild the attribute. */
11659 TREE_VALUE (t
) = build_tree_list (NULL_TREE
, contract
);
11662 /* Rebuild the attribute list for DECL, substituting into contracts
11666 tsubst_contract_attributes (tree decl
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11668 tree list
= copy_list (DECL_ATTRIBUTES (decl
));
11669 for (tree attr
= list
; attr
; attr
= CONTRACT_CHAIN (attr
))
11671 if (cxx_contract_attribute_p (attr
))
11672 tsubst_contract_attribute (decl
, attr
, args
, complain
, in_decl
);
11674 DECL_ATTRIBUTES (decl
) = list
;
11677 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11678 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11681 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11682 tsubst_flags_t complain
, tree in_decl
)
11684 gcc_assert (ATTR_IS_DEPENDENT (t
));
11686 /* Note that contract attributes are never substituted from this function.
11687 Their instantiation is triggered by regenerate_from_template_decl when
11688 we instantiate the body of the function. */
11690 tree val
= TREE_VALUE (t
);
11691 if (val
== NULL_TREE
)
11692 /* Nothing to do. */;
11693 else if ((flag_openmp
|| flag_openmp_simd
)
11694 && is_attribute_p ("omp declare simd",
11695 get_attribute_name (t
)))
11697 tree clauses
= TREE_VALUE (val
);
11698 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11699 complain
, in_decl
);
11700 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11701 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11702 tree parms
= DECL_ARGUMENTS (*decl_p
);
11704 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11706 val
= build_tree_list (NULL_TREE
, clauses
);
11710 else if (flag_openmp
11711 && is_attribute_p ("omp declare variant base",
11712 get_attribute_name (t
)))
11714 ++cp_unevaluated_operand
;
11715 tree varid
= tsubst_expr (TREE_PURPOSE (val
), args
, complain
, in_decl
);
11716 --cp_unevaluated_operand
;
11717 tree chain
= TREE_CHAIN (val
);
11718 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11719 tree ctx
= copy_list (TREE_VALUE (val
));
11720 tree simd
= get_identifier ("simd");
11721 tree score
= get_identifier (" score");
11722 tree condition
= get_identifier ("condition");
11723 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11725 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11726 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11727 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11729 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11731 tree clauses
= TREE_VALUE (t2
);
11732 clauses
= tsubst_omp_clauses (clauses
,
11733 C_ORT_OMP_DECLARE_SIMD
, args
,
11734 complain
, in_decl
);
11735 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11736 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11737 TREE_VALUE (t2
) = clauses
;
11741 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11742 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11743 if (TREE_VALUE (t3
))
11746 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11747 && TREE_PURPOSE (t3
) != score
);
11748 tree v
= TREE_VALUE (t3
);
11749 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11751 v
= tsubst_expr (v
, args
, complain
, in_decl
);
11752 v
= fold_non_dependent_expr (v
);
11753 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11754 || (TREE_PURPOSE (t3
) == score
11755 ? TREE_CODE (v
) != INTEGER_CST
11756 : !tree_fits_shwi_p (v
)))
11759 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11761 if (TREE_PURPOSE (t3
) == score
)
11762 error_at (loc
, "score argument must be "
11763 "constant integer expression");
11764 else if (allow_string
)
11765 error_at (loc
, "property must be constant "
11766 "integer expression or string "
11769 error_at (loc
, "property must be constant "
11770 "integer expression");
11773 else if (TREE_PURPOSE (t3
) == score
11774 && tree_int_cst_sgn (v
) < 0)
11777 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11779 error_at (loc
, "score argument must be "
11783 TREE_VALUE (t3
) = v
;
11788 val
= tree_cons (varid
, ctx
, chain
);
11790 /* If the first attribute argument is an identifier, don't
11791 pass it through tsubst. Attributes like mode, format,
11792 cleanup and several target specific attributes expect it
11794 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11797 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
);
11798 if (chain
!= TREE_CHAIN (val
))
11799 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11801 else if (PACK_EXPANSION_P (val
))
11803 /* An attribute pack expansion. */
11804 tree purp
= TREE_PURPOSE (t
);
11805 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11806 if (pack
== error_mark_node
)
11807 return error_mark_node
;
11808 int len
= TREE_VEC_LENGTH (pack
);
11809 tree list
= NULL_TREE
;
11811 for (int i
= 0; i
< len
; ++i
)
11813 tree elt
= TREE_VEC_ELT (pack
, i
);
11814 *q
= build_tree_list (purp
, elt
);
11815 q
= &TREE_CHAIN (*q
);
11820 val
= tsubst_expr (val
, args
, complain
, in_decl
);
11822 if (val
== error_mark_node
)
11823 return error_mark_node
;
11824 if (val
!= TREE_VALUE (t
))
11825 return build_tree_list (TREE_PURPOSE (t
), val
);
11829 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11830 unchanged or a new TREE_LIST chain. */
11833 tsubst_attributes (tree attributes
, tree args
,
11834 tsubst_flags_t complain
, tree in_decl
)
11836 tree last_dep
= NULL_TREE
;
11838 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11839 if (ATTR_IS_DEPENDENT (t
))
11842 attributes
= copy_list (attributes
);
11847 for (tree
*p
= &attributes
; *p
; )
11850 if (ATTR_IS_DEPENDENT (t
))
11852 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11857 p
= &TREE_CHAIN (*p
);
11858 *p
= TREE_CHAIN (t
);
11862 p
= &TREE_CHAIN (*p
);
11868 /* Apply any attributes which had to be deferred until instantiation
11869 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11870 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11874 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11875 tree args
, tsubst_flags_t complain
, tree in_decl
)
11880 if (attributes
== NULL_TREE
)
11883 if (DECL_P (*decl_p
))
11885 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11887 p
= &DECL_ATTRIBUTES (*decl_p
);
11888 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11889 to our attributes parameter. */
11890 gcc_assert (*p
== attributes
);
11894 p
= &TYPE_ATTRIBUTES (*decl_p
);
11895 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11896 lookup_template_class_1, and should be preserved. */
11897 gcc_assert (*p
!= attributes
);
11899 p
= &TREE_CHAIN (*p
);
11902 /* save_template_attributes puts the dependent attributes at the beginning of
11903 the list; find the non-dependent ones. */
11904 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11905 if (!ATTR_IS_DEPENDENT (t
))
11909 /* Apply any non-dependent attributes. */
11912 if (nondep
== attributes
)
11915 /* And then any dependent ones. */
11916 tree late_attrs
= NULL_TREE
;
11917 tree
*q
= &late_attrs
;
11918 for (t
= attributes
; t
!= nondep
; t
= TREE_CHAIN (t
))
11920 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11921 if (*q
== error_mark_node
)
11925 *q
= copy_node (t
);
11926 TREE_CHAIN (*q
) = NULL_TREE
;
11929 q
= &TREE_CHAIN (*q
);
11932 /* cplus_decl_attributes can add some attributes implicitly. For templates,
11933 those attributes should have been added already when those templates were
11934 parsed, and shouldn't be added based on from which context they are
11935 first time instantiated. */
11936 auto o1
= make_temp_override (current_optimize_pragma
, NULL_TREE
);
11937 auto o2
= make_temp_override (optimization_current_node
,
11938 optimization_default_node
);
11939 auto o3
= make_temp_override (current_target_pragma
, NULL_TREE
);
11940 auto o4
= make_temp_override (scope_chain
->omp_declare_target_attribute
,
11942 auto o5
= make_temp_override (scope_chain
->omp_begin_assumes
, NULL
);
11944 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11949 /* The template TMPL is being instantiated with the template arguments TARGS.
11950 Perform the access checks that we deferred when parsing the template. */
11953 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11956 deferred_access_check
*chk
;
11958 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11961 if (vec
<deferred_access_check
, va_gc
> *access_checks
11962 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11963 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11965 tree decl
= chk
->decl
;
11966 tree diag_decl
= chk
->diag_decl
;
11967 tree type_scope
= TREE_TYPE (chk
->binfo
);
11969 if (uses_template_parms (type_scope
))
11970 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11972 /* Make access check error messages point to the location
11973 of the use of the typedef. */
11974 iloc_sentinel
ils (chk
->loc
);
11975 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11976 decl
, diag_decl
, tf_warning_or_error
);
11981 instantiate_class_template (tree type
)
11983 auto_timevar
tv (TV_TEMPLATE_INST
);
11985 tree templ
, args
, pattern
, t
, member
;
11989 unsigned int saved_maximum_field_alignment
;
11992 if (type
== error_mark_node
)
11993 return error_mark_node
;
11995 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11996 || uses_template_parms (type
))
11999 /* Figure out which template is being instantiated. */
12000 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
12001 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
12003 /* Mark the type as in the process of being defined. */
12004 TYPE_BEING_DEFINED (type
) = 1;
12006 /* We may be in the middle of deferred access check. Disable
12008 deferring_access_check_sentinel
acs (dk_no_deferred
);
12010 /* Determine what specialization of the original template to
12012 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
12013 if (t
== error_mark_node
)
12014 return error_mark_node
;
12017 /* This TYPE is actually an instantiation of a partial
12018 specialization. We replace the innermost set of ARGS with
12019 the arguments appropriate for substitution. For example,
12022 template <class T> struct S {};
12023 template <class T> struct S<T*> {};
12025 and supposing that we are instantiating S<int*>, ARGS will
12026 presently be {int*} -- but we need {int}. */
12027 pattern
= TREE_TYPE (t
);
12028 args
= TREE_PURPOSE (t
);
12032 pattern
= TREE_TYPE (templ
);
12033 args
= CLASSTYPE_TI_ARGS (type
);
12036 /* If the template we're instantiating is incomplete, then clearly
12037 there's nothing we can do. */
12038 if (!COMPLETE_TYPE_P (pattern
))
12040 /* We can try again later. */
12041 TYPE_BEING_DEFINED (type
) = 0;
12045 /* If we've recursively instantiated too many templates, stop. */
12046 if (! push_tinst_level (type
))
12049 int saved_unevaluated_operand
= cp_unevaluated_operand
;
12050 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12052 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
12053 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12054 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
12055 fn_context
= error_mark_node
;
12057 push_to_top_level ();
12060 cp_unevaluated_operand
= 0;
12061 c_inhibit_evaluation_warnings
= 0;
12063 /* Use #pragma pack from the template context. */
12064 saved_maximum_field_alignment
= maximum_field_alignment
;
12065 maximum_field_alignment
= TYPE_PRECISION (pattern
);
12067 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
12069 /* Set the input location to the most specialized template definition.
12070 This is needed if tsubsting causes an error. */
12071 typedecl
= TYPE_MAIN_DECL (pattern
);
12072 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
12073 DECL_SOURCE_LOCATION (typedecl
);
12075 set_instantiating_module (TYPE_NAME (type
));
12077 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
12078 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
12079 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
12080 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
12081 if (ANON_AGGR_TYPE_P (pattern
))
12082 SET_ANON_AGGR_TYPE_P (type
);
12083 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
12085 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
12086 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
12087 /* Adjust visibility for template arguments. */
12088 determine_visibility (TYPE_MAIN_DECL (type
));
12090 if (CLASS_TYPE_P (type
))
12091 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
12093 pbinfo
= TYPE_BINFO (pattern
);
12095 /* We should never instantiate a nested class before its enclosing
12096 class; we need to look up the nested class by name before we can
12097 instantiate it, and that lookup should instantiate the enclosing
12099 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
12100 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
12102 base_list
= NULL_TREE
;
12103 /* Defer access checking while we substitute into the types named in
12104 the base-clause. */
12105 push_deferring_access_checks (dk_deferred
);
12106 if (BINFO_N_BASE_BINFOS (pbinfo
))
12111 /* Substitute into each of the bases to determine the actual
12113 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
12116 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
12117 tree expanded_bases
= NULL_TREE
;
12120 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
12123 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
12124 args
, tf_error
, NULL_TREE
);
12125 if (expanded_bases
== error_mark_node
)
12128 len
= TREE_VEC_LENGTH (expanded_bases
);
12131 for (idx
= 0; idx
< len
; idx
++)
12133 if (expanded_bases
)
12134 /* Extract the already-expanded base class. */
12135 base
= TREE_VEC_ELT (expanded_bases
, idx
);
12137 /* Substitute to figure out the base class. */
12138 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
12141 if (base
== error_mark_node
)
12144 base_list
= tree_cons (access
, base
, base_list
);
12145 if (BINFO_VIRTUAL_P (pbase_binfo
))
12146 TREE_TYPE (base_list
) = integer_type_node
;
12150 /* The list is now in reverse order; correct that. */
12151 base_list
= nreverse (base_list
);
12153 /* Now call xref_basetypes to set up all the base-class
12155 xref_basetypes (type
, base_list
);
12157 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
12158 (int) ATTR_FLAG_TYPE_IN_PLACE
,
12159 args
, tf_error
, NULL_TREE
);
12160 fixup_attribute_variants (type
);
12162 /* Now that our base classes are set up, enter the scope of the
12163 class, so that name lookups into base classes, etc. will work
12164 correctly. This is precisely analogous to what we do in
12165 begin_class_definition when defining an ordinary non-template
12166 class, except we also need to push the enclosing classes. */
12167 push_nested_class (type
);
12169 /* Now check accessibility of the types named in its base-clause,
12170 relative to the scope of the class. */
12171 pop_to_parent_deferring_access_checks ();
12173 /* A vector to hold members marked with attribute used. */
12174 auto_vec
<tree
> used
;
12176 /* Now members are processed in the order of declaration. */
12177 for (member
= CLASSTYPE_DECL_LIST (pattern
);
12178 member
; member
= TREE_CHAIN (member
))
12180 tree t
= TREE_VALUE (member
);
12182 if (TREE_PURPOSE (member
))
12186 if (LAMBDA_TYPE_P (t
))
12187 /* A closure type for a lambda in an NSDMI or default argument.
12188 Ignore it; it will be regenerated when needed. */
12191 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
12192 && TYPE_LANG_SPECIFIC (t
)
12193 && CLASSTYPE_IS_TEMPLATE (t
));
12195 /* If the member is a class template, then -- even after
12196 substitution -- there may be dependent types in the
12197 template argument list for the class. We increment
12198 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
12199 that function will assume that no types are dependent
12200 when outside of a template. */
12201 if (class_template_p
)
12202 ++processing_template_decl
;
12203 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12204 if (class_template_p
)
12205 --processing_template_decl
;
12206 if (newtag
== error_mark_node
)
12209 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
12211 tree name
= TYPE_IDENTIFIER (t
);
12213 if (class_template_p
)
12214 /* Unfortunately, lookup_template_class sets
12215 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12216 instantiation (i.e., for the type of a member
12217 template class nested within a template class.)
12218 This behavior is required for
12219 maybe_process_partial_specialization to work
12220 correctly, but is not accurate in this case;
12221 the TAG is not an instantiation of anything.
12222 (The corresponding TEMPLATE_DECL is an
12223 instantiation, but the TYPE is not.) */
12224 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
12226 /* Now, install the tag. We don't use pushtag
12227 because that does too much work -- creating an
12228 implicit typedef, which we've already done. */
12229 set_identifier_type_value (name
, TYPE_NAME (newtag
));
12230 maybe_add_class_template_decl_list (type
, newtag
, false);
12231 TREE_PUBLIC (TYPE_NAME (newtag
)) = true;
12232 determine_visibility (TYPE_NAME (newtag
));
12235 else if (DECL_DECLARES_FUNCTION_P (t
))
12239 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12240 ++processing_template_decl
;
12241 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12242 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12243 --processing_template_decl
;
12245 set_current_access_from_decl (r
);
12246 finish_member_declaration (r
);
12247 /* Instantiate members marked with attribute used. */
12248 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12249 used
.safe_push (r
);
12250 if (TREE_CODE (r
) == FUNCTION_DECL
12251 && DECL_OMP_DECLARE_REDUCTION_P (r
))
12252 cp_check_omp_declare_reduction (r
);
12254 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
12255 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
12256 /* A closure type for a lambda in an NSDMI or default argument.
12257 Ignore it; it will be regenerated when needed. */;
12260 /* Build new TYPE_FIELDS. */
12261 if (TREE_CODE (t
) == STATIC_ASSERT
)
12262 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
);
12263 else if (TREE_CODE (t
) != CONST_DECL
)
12266 tree vec
= NULL_TREE
;
12269 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
12270 /* The file and line for this declaration, to
12271 assist in error message reporting. Since we
12272 called push_tinst_level above, we don't need to
12274 input_location
= DECL_SOURCE_LOCATION (t
);
12276 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12277 ++processing_template_decl
;
12278 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12279 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12280 --processing_template_decl
;
12282 if (TREE_CODE (r
) == TREE_VEC
)
12284 /* A capture pack became multiple fields. */
12286 len
= TREE_VEC_LENGTH (vec
);
12289 for (int i
= 0; i
< len
; ++i
)
12292 r
= TREE_VEC_ELT (vec
, i
);
12297 [t]he initialization (and any associated
12298 side-effects) of a static data member does
12299 not occur unless the static data member is
12300 itself used in a way that requires the
12301 definition of the static data member to
12304 Therefore, we do not substitute into the
12305 initialized for the static data member here. */
12306 finish_static_data_member_decl
12308 /*init=*/NULL_TREE
,
12309 /*init_const_expr_p=*/false,
12310 /*asmspec_tree=*/NULL_TREE
,
12312 /* Instantiate members marked with attribute used. */
12313 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12314 used
.safe_push (r
);
12316 else if (TREE_CODE (r
) == FIELD_DECL
)
12318 /* Determine whether R has a valid type and can be
12319 completed later. If R is invalid, then its type
12320 is replaced by error_mark_node. */
12321 tree rtype
= TREE_TYPE (r
);
12322 if (can_complete_type_without_circularity (rtype
))
12323 complete_type (rtype
);
12325 if (!complete_or_array_type_p (rtype
))
12327 /* If R's type couldn't be completed and
12328 it isn't a flexible array member (whose
12329 type is incomplete by definition) give
12331 cxx_incomplete_type_error (r
, rtype
);
12332 TREE_TYPE (r
) = error_mark_node
;
12334 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12335 && TYPE_DOMAIN (rtype
) == NULL_TREE
12336 && (TREE_CODE (type
) == UNION_TYPE
12337 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12339 error ("flexible array member %qD in union", r
);
12340 TREE_TYPE (r
) = error_mark_node
;
12342 else if (!verify_type_context (input_location
,
12343 TCTX_FIELD
, rtype
))
12344 TREE_TYPE (r
) = error_mark_node
;
12347 /* If it is a TYPE_DECL for a class-scoped
12348 ENUMERAL_TYPE, such a thing will already have
12349 been added to the field list by tsubst_enum
12350 in finish_member_declaration case above. */
12351 if (!(TREE_CODE (r
) == TYPE_DECL
12352 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12353 && DECL_ARTIFICIAL (r
)))
12355 set_current_access_from_decl (r
);
12356 finish_member_declaration (r
);
12364 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12365 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12367 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12369 tree friend_type
= t
;
12370 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12372 /* template <class T> friend class C; */
12373 friend_type
= tsubst_friend_class (friend_type
, args
);
12375 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12377 /* template <class T> friend class C::D; */
12378 friend_type
= tsubst (friend_type
, args
,
12379 tf_warning_or_error
, NULL_TREE
);
12380 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12381 friend_type
= TREE_TYPE (friend_type
);
12383 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12384 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12386 /* This could be either
12390 when dependent_type_p is false or
12392 template <class U> friend class T::C;
12395 /* Bump processing_template_decl in case this is something like
12396 template <class T> friend struct A<T>::B. */
12397 ++processing_template_decl
;
12398 friend_type
= tsubst (friend_type
, args
,
12399 tf_warning_or_error
, NULL_TREE
);
12400 --processing_template_decl
;
12402 else if (uses_template_parms (friend_type
))
12403 /* friend class C<T>; */
12404 friend_type
= tsubst (friend_type
, args
,
12405 tf_warning_or_error
, NULL_TREE
);
12411 where C is already declared or
12413 friend class C<int>;
12415 We don't have to do anything in these cases. */
12417 if (friend_type
!= error_mark_node
)
12418 make_friend_class (type
, friend_type
, /*complain=*/false);
12422 /* Build new DECL_FRIENDLIST. */
12425 /* The file and line for this declaration, to
12426 assist in error message reporting. Since we
12427 called push_tinst_level above, we don't need to
12429 input_location
= DECL_SOURCE_LOCATION (t
);
12431 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12433 ++processing_template_decl
;
12434 push_deferring_access_checks (dk_no_check
);
12437 r
= tsubst_friend_function (t
, args
);
12438 add_friend (type
, r
, /*complain=*/false);
12439 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12441 pop_deferring_access_checks ();
12442 --processing_template_decl
;
12450 /* Restore these before substituting into the lambda capture
12452 cp_unevaluated_operand
= saved_unevaluated_operand
;
12453 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12456 /* Set the file and line number information to whatever is given for
12457 the class itself. This puts error messages involving generated
12458 implicit functions at a predictable point, and the same point
12459 that would be used for non-template classes. */
12460 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12462 unreverse_member_declarations (type
);
12463 finish_struct_1 (type
);
12464 TYPE_BEING_DEFINED (type
) = 0;
12466 /* Remember if instantiating this class ran into errors, so we can avoid
12467 instantiating member functions in limit_bad_template_recursion. We set
12468 this flag even if the problem was in another instantiation triggered by
12469 this one, as that will likely also cause trouble for member functions. */
12470 if (errorcount
+ sorrycount
> current_tinst_level
->errors
)
12471 CLASSTYPE_ERRONEOUS (type
) = true;
12473 /* We don't instantiate default arguments for member functions. 14.7.1:
12475 The implicit instantiation of a class template specialization causes
12476 the implicit instantiation of the declarations, but not of the
12477 definitions or default arguments, of the class member functions,
12478 member classes, static data members and member templates.... */
12480 perform_instantiation_time_access_checks (pattern
, args
);
12481 perform_deferred_access_checks (tf_warning_or_error
);
12483 /* Now that we've gone through all the members, instantiate those
12484 marked with attribute used. We must do this in the context of
12485 the class -- not the context we pushed from, as that might be
12486 inside a template and change the behaviour of mark_used. */
12487 for (tree x
: used
)
12490 pop_nested_class ();
12491 maximum_field_alignment
= saved_maximum_field_alignment
;
12493 pop_from_top_level ();
12494 pop_tinst_level ();
12496 /* The vtable for a template class can be emitted in any translation
12497 unit in which the class is instantiated. When there is no key
12498 method, however, finish_struct_1 will already have added TYPE to
12499 the keyed_classes. */
12500 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12501 vec_safe_push (keyed_classes
, type
);
12507 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12513 else if (TYPE_P (t
))
12514 r
= tsubst (t
, args
, complain
, in_decl
);
12517 if (!(complain
& tf_warning
))
12518 ++c_inhibit_evaluation_warnings
;
12519 r
= tsubst_expr (t
, args
, complain
, in_decl
);
12520 if (!(complain
& tf_warning
))
12521 --c_inhibit_evaluation_warnings
;
12527 /* Given a function parameter pack TMPL_PARM and some function parameters
12528 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12529 and set *SPEC_P to point at the next point in the list. */
12532 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12534 /* Collect all of the extra "packed" parameters into an
12537 tree spec_parm
= *spec_p
;
12540 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12542 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12545 spec_parm
= *spec_p
;
12546 if (len
== 1 && DECL_PACK_P (spec_parm
))
12548 /* The instantiation is still a parameter pack; don't wrap it in a
12549 NONTYPE_ARGUMENT_PACK. */
12550 argpack
= spec_parm
;
12551 spec_parm
= DECL_CHAIN (spec_parm
);
12555 /* Fill in PARMVEC with all of the parameters. */
12556 tree parmvec
= make_tree_vec (len
);
12557 argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12558 for (int i
= 0; i
< len
; i
++)
12560 tree elt
= spec_parm
;
12561 if (DECL_PACK_P (elt
))
12562 elt
= make_pack_expansion (elt
);
12563 TREE_VEC_ELT (parmvec
, i
) = elt
;
12564 spec_parm
= DECL_CHAIN (spec_parm
);
12567 /* Build the argument packs. */
12568 ARGUMENT_PACK_ARGS (argpack
) = parmvec
;
12570 *spec_p
= spec_parm
;
12575 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12576 NONTYPE_ARGUMENT_PACK. */
12579 make_fnparm_pack (tree spec_parm
)
12581 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12584 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12585 pack expansion with no extra args, 2 if it has extra args, or 0
12586 if it is not a pack expansion. */
12589 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12591 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12592 /* We're being called before this happens in tsubst_pack_expansion. */
12593 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12594 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12595 if (i
>= TREE_VEC_LENGTH (vec
))
12597 tree elt
= TREE_VEC_ELT (vec
, i
);
12599 /* A decl pack is itself an expansion. */
12600 elt
= TREE_TYPE (elt
);
12601 if (!PACK_EXPANSION_P (elt
))
12603 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12609 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12612 make_argument_pack_select (tree arg_pack
, unsigned index
)
12614 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12616 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12617 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12622 /* This is a subroutine of tsubst_pack_expansion.
12624 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12625 mechanism to store the (non complete list of) arguments of the
12626 substitution and return a non substituted pack expansion, in order
12627 to wait for when we have enough arguments to really perform the
12631 use_pack_expansion_extra_args_p (tree t
,
12634 bool has_empty_arg
)
12637 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t
))
12640 /* If one pack has an expansion and another pack has a normal
12641 argument or if one pack has an empty argument and an another
12642 one hasn't then tsubst_pack_expansion cannot perform the
12643 substitution and need to fall back on the
12644 PACK_EXPANSION_EXTRA mechanism. */
12645 if (parm_packs
== NULL_TREE
)
12647 else if (has_empty_arg
)
12649 /* If all the actual packs are pack expansions, we can still
12650 subsitute directly. */
12651 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12653 tree a
= TREE_VALUE (p
);
12654 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12655 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12656 a
= ARGUMENT_PACK_ARGS (a
);
12657 if (TREE_VEC_LENGTH (a
) == 1)
12658 a
= TREE_VEC_ELT (a
, 0);
12659 if (PACK_EXPANSION_P (a
))
12666 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12668 bool has_expansion_arg
= false;
12669 bool has_non_expansion_arg
= false;
12670 for (tree parm_pack
= parm_packs
;
12672 parm_pack
= TREE_CHAIN (parm_pack
))
12674 tree arg
= TREE_VALUE (parm_pack
);
12676 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12678 /* We can't substitute a pack expansion with extra args into
12682 has_expansion_arg
= true;
12684 has_non_expansion_arg
= true;
12687 if (has_expansion_arg
&& has_non_expansion_arg
)
12689 gcc_checking_assert (false);
12696 /* [temp.variadic]/6 says that:
12698 The instantiation of a pack expansion [...]
12699 produces a list E1,E2, ..., En, where N is the number of elements
12700 in the pack expansion parameters.
12702 This subroutine of tsubst_pack_expansion produces one of these Ei.
12704 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12705 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12706 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12707 INDEX is the index 'i' of the element Ei to produce. ARGS,
12708 COMPLAIN, and IN_DECL are the same parameters as for the
12709 tsubst_pack_expansion function.
12711 The function returns the resulting Ei upon successful completion,
12712 or error_mark_node.
12714 Note that this function possibly modifies the ARGS parameter, so
12715 it's the responsibility of the caller to restore it. */
12718 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12721 tree args
/* This parm gets
12723 tsubst_flags_t complain
,
12727 bool ith_elem_is_expansion
= false;
12729 /* For each parameter pack, change the substitution of the parameter
12730 pack to the ith argument in its argument pack, then expand the
12732 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12734 tree parm
= TREE_PURPOSE (pack
);
12735 tree arg_pack
= TREE_VALUE (pack
);
12736 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12738 ith_elem_is_expansion
|=
12739 argument_pack_element_is_expansion_p (arg_pack
, index
);
12741 /* Select the Ith argument from the pack. */
12742 if (TREE_CODE (parm
) == PARM_DECL
12744 || TREE_CODE (parm
) == FIELD_DECL
)
12748 aps
= make_argument_pack_select (arg_pack
, index
);
12749 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12750 return error_mark_node
;
12751 register_local_specialization (aps
, parm
);
12754 aps
= retrieve_local_specialization (parm
);
12759 template_parm_level_and_index (parm
, &level
, &idx
);
12763 aps
= make_argument_pack_select (arg_pack
, index
);
12764 /* Update the corresponding argument. */
12765 TMPL_ARG (args
, level
, idx
) = aps
;
12768 /* Re-use the ARGUMENT_PACK_SELECT. */
12769 aps
= TMPL_ARG (args
, level
, idx
);
12771 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12774 /* Substitute into the PATTERN with the (possibly altered)
12776 if (pattern
== in_decl
)
12777 /* Expanding a fixed parameter pack from
12778 coerce_template_parameter_pack. */
12779 t
= tsubst_decl (pattern
, args
, complain
);
12780 else if (pattern
== error_mark_node
)
12781 t
= error_mark_node
;
12782 else if (!TYPE_P (pattern
))
12783 t
= tsubst_expr (pattern
, args
, complain
, in_decl
);
12786 t
= tsubst (pattern
, args
, complain
, in_decl
);
12787 if (is_auto (t
) && !ith_elem_is_expansion
)
12788 /* When expanding the fake auto... pack expansion from add_capture, we
12789 need to mark that the expansion is no longer a pack. */
12790 TEMPLATE_TYPE_PARAMETER_PACK (t
) = false;
12793 /* If the Ith argument pack element is a pack expansion, then
12794 the Ith element resulting from the substituting is going to
12795 be a pack expansion as well. */
12796 if (ith_elem_is_expansion
)
12797 t
= make_pack_expansion (t
, complain
);
12802 /* When the unexpanded parameter pack in a fold expression expands to an empty
12803 sequence, the value of the expression is as follows; the program is
12804 ill-formed if the operator is not listed in this table.
12811 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12813 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12814 if (!FOLD_EXPR_MODIFY_P (t
))
12817 case TRUTH_ANDIF_EXPR
:
12818 return boolean_true_node
;
12819 case TRUTH_ORIF_EXPR
:
12820 return boolean_false_node
;
12821 case COMPOUND_EXPR
:
12827 if (complain
& tf_error
)
12828 error_at (location_of (t
),
12829 "fold of empty expansion over %O", code
);
12830 return error_mark_node
;
12833 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12834 form an expression that combines the two terms using the
12838 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12840 tree_code code
= FOLD_EXPR_OP (t
);
12842 tree lookups
= templated_operator_saved_lookups (t
);
12844 // Handle compound assignment operators.
12845 if (FOLD_EXPR_MODIFY_P (t
))
12846 return build_x_modify_expr (input_location
, left
, code
, right
,
12847 lookups
, complain
);
12849 warning_sentinel
s(warn_parentheses
);
12852 case COMPOUND_EXPR
:
12853 return build_x_compound_expr (input_location
, left
, right
,
12854 lookups
, complain
);
12856 return build_x_binary_op (input_location
, code
,
12857 left
, TREE_CODE (left
),
12858 right
, TREE_CODE (right
),
12859 lookups
, /*overload=*/NULL
,
12864 /* Substitute ARGS into the pack of a fold expression T. */
12867 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12869 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12872 /* Substitute ARGS into the pack of a fold expression T. */
12875 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12877 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
);
12880 /* Expand a PACK of arguments into a grouped as left fold.
12881 Given a pack containing elements A0, A1, ..., An and an
12882 operator @, this builds the expression:
12884 ((A0 @ A1) @ A2) ... @ An
12886 Note that PACK must not be empty.
12888 The operator is defined by the original fold expression T. */
12891 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12893 tree left
= TREE_VEC_ELT (pack
, 0);
12894 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12896 tree right
= TREE_VEC_ELT (pack
, i
);
12897 left
= fold_expression (t
, left
, right
, complain
);
12902 /* Substitute into a unary left fold expression. */
12905 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12908 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12909 if (pack
== error_mark_node
)
12910 return error_mark_node
;
12911 if (PACK_EXPANSION_P (pack
))
12913 tree r
= copy_node (t
);
12914 FOLD_EXPR_PACK (r
) = pack
;
12917 if (TREE_VEC_LENGTH (pack
) == 0)
12918 return expand_empty_fold (t
, complain
);
12920 return expand_left_fold (t
, pack
, complain
);
12923 /* Substitute into a binary left fold expression.
12925 Do ths by building a single (non-empty) vector of argumnts and
12926 building the expression from those elements. */
12929 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12932 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12933 if (pack
== error_mark_node
)
12934 return error_mark_node
;
12935 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12936 if (init
== error_mark_node
)
12937 return error_mark_node
;
12939 if (PACK_EXPANSION_P (pack
))
12941 tree r
= copy_node (t
);
12942 FOLD_EXPR_PACK (r
) = pack
;
12943 FOLD_EXPR_INIT (r
) = init
;
12947 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12948 TREE_VEC_ELT (vec
, 0) = init
;
12949 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12950 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12952 return expand_left_fold (t
, vec
, complain
);
12955 /* Expand a PACK of arguments into a grouped as right fold.
12956 Given a pack containing elementns A0, A1, ..., and an
12957 operator @, this builds the expression:
12959 A0@ ... (An-2 @ (An-1 @ An))
12961 Note that PACK must not be empty.
12963 The operator is defined by the original fold expression T. */
12966 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12968 // Build the expression.
12969 int n
= TREE_VEC_LENGTH (pack
);
12970 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12971 for (--n
; n
!= 0; --n
)
12973 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12974 right
= fold_expression (t
, left
, right
, complain
);
12979 /* Substitute into a unary right fold expression. */
12982 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12985 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12986 if (pack
== error_mark_node
)
12987 return error_mark_node
;
12988 if (PACK_EXPANSION_P (pack
))
12990 tree r
= copy_node (t
);
12991 FOLD_EXPR_PACK (r
) = pack
;
12994 if (TREE_VEC_LENGTH (pack
) == 0)
12995 return expand_empty_fold (t
, complain
);
12997 return expand_right_fold (t
, pack
, complain
);
13000 /* Substitute into a binary right fold expression.
13002 Do ths by building a single (non-empty) vector of arguments and
13003 building the expression from those elements. */
13006 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
13009 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13010 if (pack
== error_mark_node
)
13011 return error_mark_node
;
13012 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
13013 if (init
== error_mark_node
)
13014 return error_mark_node
;
13016 if (PACK_EXPANSION_P (pack
))
13018 tree r
= copy_node (t
);
13019 FOLD_EXPR_PACK (r
) = pack
;
13020 FOLD_EXPR_INIT (r
) = init
;
13024 int n
= TREE_VEC_LENGTH (pack
);
13025 tree vec
= make_tree_vec (n
+ 1);
13026 for (int i
= 0; i
< n
; ++i
)
13027 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
13028 TREE_VEC_ELT (vec
, n
) = init
;
13030 return expand_right_fold (t
, vec
, complain
);
13033 /* Walk through the pattern of a pack expansion, adding everything in
13034 local_specializations to a list. */
13039 /* Set of variables declared within the pattern. */
13040 hash_set
<tree
> internal
;
13041 /* Set of AST nodes that have been visited by the traversal. */
13042 hash_set
<tree
> visited
;
13043 /* List of local_specializations used within the pattern. */
13045 tsubst_flags_t complain
;
13046 /* True iff we don't want to walk into unevaluated contexts. */
13047 bool skip_unevaluated_operands
= false;
13049 el_data (tsubst_flags_t c
)
13050 : extra (NULL_TREE
), complain (c
) {}
13053 extract_locals_r (tree
*tp
, int *walk_subtrees
, void *data_
)
13055 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
13056 tree
*extra
= &data
.extra
;
13057 tsubst_flags_t complain
= data
.complain
;
13059 if (data
.skip_unevaluated_operands
13060 && unevaluated_p (TREE_CODE (*tp
)))
13062 *walk_subtrees
= 0;
13066 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
13067 /* Remember local typedefs (85214). */
13068 tp
= &TYPE_NAME (*tp
);
13070 if (TREE_CODE (*tp
) == DECL_EXPR
)
13072 tree decl
= DECL_EXPR_DECL (*tp
);
13073 data
.internal
.add (decl
);
13075 && DECL_DECOMPOSITION_P (decl
)
13076 && TREE_TYPE (decl
) != error_mark_node
)
13078 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
13079 for (tree decl2
= DECL_CHAIN (decl
);
13082 && DECL_DECOMPOSITION_P (decl2
)
13083 && DECL_NAME (decl2
)
13084 && TREE_TYPE (decl2
) != error_mark_node
;
13085 decl2
= DECL_CHAIN (decl2
))
13087 gcc_assert (DECL_DECOMP_BASE (decl2
) == decl
);
13088 data
.internal
.add (decl2
);
13092 else if (TREE_CODE (*tp
) == LAMBDA_EXPR
)
13094 /* Since we defer implicit capture, look in the parms and body. */
13095 tree fn
= lambda_function (*tp
);
13096 cp_walk_tree (&TREE_TYPE (fn
), &extract_locals_r
, &data
,
13098 cp_walk_tree (&DECL_SAVED_TREE (fn
), &extract_locals_r
, &data
,
13101 else if (tree spec
= retrieve_local_specialization (*tp
))
13103 if (data
.internal
.contains (*tp
))
13104 /* Don't mess with variables declared within the pattern. */
13106 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
13108 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13109 tree args
= ARGUMENT_PACK_ARGS (spec
);
13110 if (TREE_VEC_LENGTH (args
) == 1)
13112 tree elt
= TREE_VEC_ELT (args
, 0);
13113 if (PACK_EXPANSION_P (elt
))
13114 elt
= PACK_EXPANSION_PATTERN (elt
);
13115 if (DECL_PACK_P (elt
))
13118 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
13120 /* Handle lambda capture here, since we aren't doing any
13121 substitution now, and so tsubst_copy won't call
13122 process_outer_var_ref. */
13123 tree args
= ARGUMENT_PACK_ARGS (spec
);
13124 int len
= TREE_VEC_LENGTH (args
);
13125 for (int i
= 0; i
< len
; ++i
)
13127 tree arg
= TREE_VEC_ELT (args
, i
);
13129 if (outer_automatic_var_p (arg
))
13130 carg
= process_outer_var_ref (arg
, complain
);
13133 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13137 spec
= copy_node (spec
);
13138 args
= copy_node (args
);
13139 ARGUMENT_PACK_ARGS (spec
) = args
;
13140 register_local_specialization (spec
, *tp
);
13142 TREE_VEC_ELT (args
, i
) = carg
;
13147 if (outer_automatic_var_p (spec
))
13148 spec
= process_outer_var_ref (spec
, complain
);
13149 *extra
= tree_cons (*tp
, spec
, *extra
);
13154 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
13156 el_data
data (complain
);
13157 /* Walk the pattern twice, ignoring unevaluated operands the first time
13158 around, so that if a local specialization appears in both an evaluated
13159 and unevaluated context we prefer to process it in the evaluated context
13160 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13162 data
.skip_unevaluated_operands
= true;
13163 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
13164 data
.skip_unevaluated_operands
= false;
13165 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
13169 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13170 for use in PACK_EXPANSION_EXTRA_ARGS. */
13173 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
13175 /* Make a copy of the extra arguments so that they won't get changed
13176 out from under us. */
13177 tree extra
= preserve_args (copy_template_args (args
), /*cow_p=*/false);
13178 if (local_specializations
)
13179 if (tree locals
= extract_local_specs (pattern
, complain
))
13180 extra
= tree_cons (NULL_TREE
, extra
, locals
);
13184 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13185 normal template args to ARGS. */
13188 add_extra_args (tree extra
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13190 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
13192 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
13194 /* The partial instantiation involved local declarations collected in
13195 extract_local_specs; map from the general template to our local
13197 tree gen
= TREE_PURPOSE (elt
);
13198 tree inst
= TREE_VALUE (elt
);
13200 if (tree local
= retrieve_local_specialization (inst
))
13202 /* else inst is already a full instantiation of the pack. */
13203 register_local_specialization (inst
, gen
);
13205 gcc_assert (!TREE_PURPOSE (extra
));
13206 extra
= TREE_VALUE (extra
);
13208 if (uses_template_parms (extra
))
13210 /* This can happen after dependent substitution into a
13211 requires-expr or a lambda that uses constexpr if. */
13212 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
13213 args
= add_outermost_template_args (args
, extra
);
13216 args
= add_to_template_args (extra
, args
);
13220 /* Substitute ARGS into T, which is an pack expansion
13221 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13222 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13223 (if only a partial substitution could be performed) or
13224 ERROR_MARK_NODE if there was an error. */
13226 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
13230 tree pack
, packs
= NULL_TREE
;
13231 bool unsubstituted_packs
= false;
13234 bool need_local_specializations
= false;
13237 gcc_assert (PACK_EXPANSION_P (t
));
13238 pattern
= PACK_EXPANSION_PATTERN (t
);
13240 /* Add in any args remembered from an earlier partial instantiation. */
13241 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
, complain
, in_decl
);
13243 levels
= TMPL_ARGS_DEPTH (args
);
13245 /* Determine the argument packs that will instantiate the parameter
13246 packs used in the expansion expression. While we're at it,
13247 compute the number of arguments to be expanded and make sure it
13249 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
13250 pack
= TREE_CHAIN (pack
))
13252 tree parm_pack
= TREE_VALUE (pack
);
13253 tree arg_pack
= NULL_TREE
;
13254 tree orig_arg
= NULL_TREE
;
13257 if (TREE_CODE (parm_pack
) == BASES
)
13259 gcc_assert (parm_pack
== pattern
);
13260 if (BASES_DIRECT (parm_pack
))
13261 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13266 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13267 args
, complain
, in_decl
),
13270 else if (builtin_pack_call_p (parm_pack
))
13272 if (parm_pack
!= pattern
)
13274 if (complain
& tf_error
)
13275 sorry ("%qE is not the entire pattern of the pack expansion",
13277 return error_mark_node
;
13279 return expand_builtin_pack_call (parm_pack
, args
,
13280 complain
, in_decl
);
13282 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
13284 /* We know we have correct local_specializations if this
13285 expansion is at function scope, or if we're dealing with a
13286 local parameter in a requires expression; for the latter,
13287 tsubst_requires_expr set it up appropriately. */
13288 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
13289 arg_pack
= retrieve_local_specialization (parm_pack
);
13291 /* We can't rely on local_specializations for a parameter
13292 name used later in a function declaration (such as in a
13293 late-specified return type). Even if it exists, it might
13294 have the wrong value for a recursive call. */
13295 need_local_specializations
= true;
13299 /* This parameter pack was used in an unevaluated context. Just
13300 make a dummy decl, since it's only used for its type. */
13301 ++cp_unevaluated_operand
;
13302 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
13303 --cp_unevaluated_operand
;
13304 if (arg_pack
&& DECL_PACK_P (arg_pack
))
13305 /* Partial instantiation of the parm_pack, we can't build
13306 up an argument pack yet. */
13307 arg_pack
= NULL_TREE
;
13309 arg_pack
= make_fnparm_pack (arg_pack
);
13311 else if (DECL_PACK_P (arg_pack
))
13312 /* This argument pack isn't fully instantiated yet. */
13313 arg_pack
= NULL_TREE
;
13315 else if (is_capture_proxy (parm_pack
))
13317 arg_pack
= retrieve_local_specialization (parm_pack
);
13318 if (DECL_PACK_P (arg_pack
))
13319 arg_pack
= NULL_TREE
;
13324 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13325 if (level
<= levels
)
13326 arg_pack
= TMPL_ARG (args
, level
, idx
);
13328 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
13329 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
13330 arg_pack
= NULL_TREE
;
13333 orig_arg
= arg_pack
;
13334 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13335 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13337 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13338 /* This can only happen if we forget to expand an argument
13339 pack somewhere else. Just return an error, silently. */
13341 result
= make_tree_vec (1);
13342 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13349 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13351 /* Don't bother trying to do a partial substitution with
13352 incomplete packs; we'll try again after deduction. */
13353 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13358 else if (len
!= my_len
)
13360 if (!(complain
& tf_error
))
13361 /* Fail quietly. */;
13362 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13363 error ("mismatched argument pack lengths while expanding %qT",
13366 error ("mismatched argument pack lengths while expanding %qE",
13368 return error_mark_node
;
13371 /* Keep track of the parameter packs and their corresponding
13373 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13374 TREE_TYPE (packs
) = orig_arg
;
13378 /* We can't substitute for this parameter pack. We use a flag as
13379 well as the missing_level counter because function parameter
13380 packs don't have a level. */
13381 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13382 unsubstituted_packs
= true;
13386 /* If the expansion is just T..., return the matching argument pack, unless
13387 we need to call convert_from_reference on all the elements. This is an
13388 important optimization; see c++/68422. */
13389 if (!unsubstituted_packs
13390 && TREE_PURPOSE (packs
) == pattern
)
13392 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13394 /* If the argument pack is a single pack expansion, pull it out. */
13395 if (TREE_VEC_LENGTH (args
) == 1
13396 && pack_expansion_args_count (args
))
13397 return TREE_VEC_ELT (args
, 0);
13399 /* Types need no adjustment, nor does sizeof..., and if we still have
13400 some pack expansion args we won't do anything yet. */
13401 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13402 || PACK_EXPANSION_SIZEOF_P (t
)
13403 || pack_expansion_args_count (args
))
13405 /* Also optimize expression pack expansions if we can tell that the
13406 elements won't have reference type. */
13407 tree type
= TREE_TYPE (pattern
);
13408 if (type
&& !TYPE_REF_P (type
)
13409 && !PACK_EXPANSION_P (type
)
13410 && !WILDCARD_TYPE_P (type
))
13412 /* Otherwise use the normal path so we get convert_from_reference. */
13415 /* We cannot expand this expansion expression, because we don't have
13416 all of the argument packs we need. */
13417 if (use_pack_expansion_extra_args_p (t
, packs
, len
, unsubstituted_packs
))
13419 /* We got some full packs, but we can't substitute them in until we
13420 have values for all the packs. So remember these until then. */
13422 t
= make_pack_expansion (pattern
, complain
);
13423 PACK_EXPANSION_EXTRA_ARGS (t
)
13424 = build_extra_args (pattern
, args
, complain
);
13428 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13429 type, so create our own local specializations map; the current map is
13430 either NULL or (in the case of recursive unification) might have
13431 bindings that we don't want to use or alter. */
13432 local_specialization_stack
lss (need_local_specializations
13433 ? lss_blank
: lss_nop
);
13435 if (unsubstituted_packs
)
13437 /* There were no real arguments, we're just replacing a parameter
13438 pack with another version of itself. Substitute into the
13439 pattern and return a PACK_EXPANSION_*. The caller will need to
13441 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13442 result
= tsubst_expr (pattern
, args
, complain
, in_decl
);
13444 result
= tsubst (pattern
, args
, complain
, in_decl
);
13445 result
= make_pack_expansion (result
, complain
);
13446 PACK_EXPANSION_LOCAL_P (result
) = PACK_EXPANSION_LOCAL_P (t
);
13447 PACK_EXPANSION_SIZEOF_P (result
) = PACK_EXPANSION_SIZEOF_P (t
);
13448 if (PACK_EXPANSION_AUTO_P (t
))
13450 /* This is a fake auto... pack expansion created in add_capture with
13451 _PACKS that don't appear in the pattern. Copy one over. */
13452 packs
= PACK_EXPANSION_PARAMETER_PACKS (t
);
13453 pack
= retrieve_local_specialization (TREE_VALUE (packs
));
13454 gcc_checking_assert (DECL_PACK_P (pack
));
13455 PACK_EXPANSION_PARAMETER_PACKS (result
)
13456 = build_tree_list (NULL_TREE
, pack
);
13457 PACK_EXPANSION_AUTO_P (result
) = true;
13462 gcc_assert (len
>= 0);
13464 /* For each argument in each argument pack, substitute into the
13466 result
= make_tree_vec (len
);
13467 tree elem_args
= copy_template_args (args
);
13468 for (i
= 0; i
< len
; ++i
)
13470 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13472 elem_args
, complain
,
13474 TREE_VEC_ELT (result
, i
) = t
;
13475 if (t
== error_mark_node
)
13477 result
= error_mark_node
;
13482 /* Update ARGS to restore the substitution from parameter packs to
13483 their argument packs. */
13484 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13486 tree parm
= TREE_PURPOSE (pack
);
13488 if (TREE_CODE (parm
) == PARM_DECL
13490 || TREE_CODE (parm
) == FIELD_DECL
)
13491 register_local_specialization (TREE_TYPE (pack
), parm
);
13496 if (TREE_VALUE (pack
) == NULL_TREE
)
13499 template_parm_level_and_index (parm
, &level
, &idx
);
13501 /* Update the corresponding argument. */
13502 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13503 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13506 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13510 /* If the dependent pack arguments were such that we end up with only a
13511 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13512 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13513 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13514 return TREE_VEC_ELT (result
, 0);
13519 /* Make an argument pack out of the TREE_VEC VEC. */
13522 make_argument_pack (tree vec
)
13526 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13527 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13530 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13531 TREE_CONSTANT (pack
) = 1;
13533 ARGUMENT_PACK_ARGS (pack
) = vec
;
13537 /* Return an exact copy of template args T that can be modified
13541 copy_template_args (tree t
)
13543 if (t
== error_mark_node
)
13546 int len
= TREE_VEC_LENGTH (t
);
13547 tree new_vec
= make_tree_vec (len
);
13549 for (int i
= 0; i
< len
; ++i
)
13551 tree elt
= TREE_VEC_ELT (t
, i
);
13552 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13553 elt
= copy_template_args (elt
);
13554 TREE_VEC_ELT (new_vec
, i
) = elt
;
13557 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13558 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13563 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13566 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13569 /* This flag is used only during deduction, and we don't expect to
13570 substitute such ARGUMENT_PACKs. */
13571 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg
));
13573 /* Substitute into each of the arguments. */
13574 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13575 args
, complain
, in_decl
);
13576 if (pack_args
== error_mark_node
)
13577 return error_mark_node
;
13579 if (pack_args
== ARGUMENT_PACK_ARGS (orig_arg
))
13582 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
13583 template parameter, we might be able to avoid allocating a new
13584 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
13585 if the substituted result is identical to it. */
13586 if (tree parm
= template_arg_to_parm (orig_arg
))
13589 template_parm_level_and_index (parm
, &level
, &index
);
13590 if (TMPL_ARGS_DEPTH (args
) >= level
)
13591 if (tree arg
= TMPL_ARG (args
, level
, index
))
13592 if (TREE_CODE (arg
) == TREE_CODE (orig_arg
)
13593 && ARGUMENT_PACK_ARGS (arg
) == pack_args
)
13595 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg
));
13601 if (TYPE_P (orig_arg
))
13603 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13604 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13608 new_arg
= make_node (TREE_CODE (orig_arg
));
13609 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13611 ARGUMENT_PACK_ARGS (new_arg
) = pack_args
;
13615 /* Substitute ARGS into the vector or list of template arguments T. */
13618 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13620 if (t
== error_mark_node
)
13621 return error_mark_node
;
13623 /* In "sizeof(X<I>)" we need to evaluate "I". */
13626 const int len
= TREE_VEC_LENGTH (t
);
13627 tree
*elts
= XALLOCAVEC (tree
, len
);
13628 int expanded_len_adjust
= 0;
13630 /* True iff the substituted result is identical to T. */
13631 bool const_subst_p
= true;
13633 for (int i
= 0; i
< len
; i
++)
13635 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13639 new_arg
= NULL_TREE
;
13640 else if (TREE_CODE (orig_arg
) == TREE_VEC
)
13641 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13642 else if (PACK_EXPANSION_P (orig_arg
))
13644 /* Substitute into an expansion expression. */
13645 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13647 if (TREE_CODE (new_arg
) == TREE_VEC
)
13648 /* Add to the expanded length adjustment the number of
13649 expanded arguments. We subtract one from this
13650 measurement, because the argument pack expression
13651 itself is already counted as 1 in
13652 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13653 the argument pack is empty. */
13654 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13656 else if (ARGUMENT_PACK_P (orig_arg
))
13657 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13659 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13661 if (new_arg
== error_mark_node
)
13662 return error_mark_node
;
13665 if (new_arg
!= orig_arg
)
13666 const_subst_p
= false;
13672 tree maybe_reuse
= NULL_TREE
;
13674 /* If ARGS and T are both multi-level, the substituted result may be
13675 identical to ARGS. */
13676 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t
)
13677 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
13678 && TMPL_ARGS_DEPTH (t
) == TMPL_ARGS_DEPTH (args
))
13679 maybe_reuse
= args
;
13680 /* If T appears to be a vector of generic template arguments, the
13681 substituted result may be identical to the corresponding level
13683 else if (tree parm
= template_arg_to_parm (TREE_VEC_ELT (t
, 0)))
13686 template_parm_level_and_index (parm
, &level
, &index
);
13687 if (index
== 0 && TMPL_ARGS_DEPTH (args
) >= level
)
13688 maybe_reuse
= TMPL_ARGS_LEVEL (args
, level
);
13691 /* If the substituted result is identical to MAYBE_REUSE, return
13692 it and avoid allocating a new TREE_VEC, as an optimization. */
13693 if (maybe_reuse
!= NULL_TREE
13694 && TREE_VEC_LENGTH (maybe_reuse
) == len
13695 && std::equal (elts
, elts
+len
, TREE_VEC_BEGIN (maybe_reuse
)))
13696 return maybe_reuse
;
13698 /* If T consists of only a pack expansion for which substitution yielded
13699 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
13700 of effectively making a copy. */
13702 && PACK_EXPANSION_P (TREE_VEC_ELT (t
, 0))
13703 && TREE_CODE (elts
[0]) == TREE_VEC
)
13706 /* Make space for the expanded arguments coming from template
13708 tree r
= make_tree_vec (len
+ expanded_len_adjust
);
13709 /* T can contain TREE_VECs. That happens if T contains the
13710 arguments for a member template.
13711 In that case each TREE_VEC in T represents a level of template
13712 arguments, and T won't carry any non defaulted argument count.
13713 It will rather be the nested TREE_VECs that will carry one.
13714 In other words, T carries a non defaulted argument count only
13715 if it doesn't contain any nested TREE_VEC. */
13716 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
))
13718 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13719 count
+= expanded_len_adjust
;
13720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r
, count
);
13724 for (int i
= 0; i
< len
; i
++)
13726 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13728 && PACK_EXPANSION_P (orig_arg
)
13729 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13731 /* Now expand the template argument pack "in place". */
13732 for (int idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13733 TREE_VEC_ELT (r
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13737 TREE_VEC_ELT (r
, out
) = elts
[i
];
13741 gcc_assert (out
== TREE_VEC_LENGTH (r
));
13746 /* Substitute ARGS into one level PARMS of template parameters. */
13749 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13751 if (parms
== error_mark_node
)
13752 return error_mark_node
;
13754 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13756 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13758 tree tuple
= TREE_VEC_ELT (parms
, i
);
13760 if (tuple
== error_mark_node
)
13763 TREE_VEC_ELT (new_vec
, i
) =
13764 tsubst_template_parm (tuple
, args
, complain
);
13770 /* Return the result of substituting ARGS into the template parameters
13771 given by PARMS. If there are m levels of ARGS and m + n levels of
13772 PARMS, then the result will contain n levels of PARMS. For
13773 example, if PARMS is `template <class T> template <class U>
13774 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13775 result will be `template <int*, double, class V>'. */
13778 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13780 tree r
= NULL_TREE
;
13783 /* When substituting into a template, we must set
13784 PROCESSING_TEMPLATE_DECL as the template parameters may be
13785 dependent if they are based on one-another, and the dependency
13786 predicates are short-circuit outside of templates. */
13787 ++processing_template_decl
;
13789 for (new_parms
= &r
;
13790 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13791 new_parms
= &(TREE_CHAIN (*new_parms
)),
13792 parms
= TREE_CHAIN (parms
))
13794 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13797 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13798 - TMPL_ARGS_DEPTH (args
)),
13799 new_vec
, NULL_TREE
);
13800 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13801 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13804 --processing_template_decl
;
13809 /* Return the result of substituting ARGS into one template parameter
13810 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13811 parameter and which TREE_PURPOSE is the default argument of the
13812 template parameter. */
13815 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13817 tree default_value
, parm_decl
;
13819 if (args
== NULL_TREE
13821 || t
== error_mark_node
)
13824 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13826 default_value
= TREE_PURPOSE (t
);
13827 parm_decl
= TREE_VALUE (t
);
13829 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13830 if (TREE_CODE (parm_decl
) == PARM_DECL
13831 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13832 parm_decl
= error_mark_node
;
13833 default_value
= tsubst_template_arg (default_value
, args
,
13834 complain
, NULL_TREE
);
13836 tree r
= build_tree_list (default_value
, parm_decl
);
13837 TEMPLATE_PARM_CONSTRAINTS (r
) = TEMPLATE_PARM_CONSTRAINTS (t
);
13841 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
13842 parameter in PARMS for sake of declaration matching. */
13845 tsubst_each_template_parm_constraints (tree parms
, tree args
,
13846 tsubst_flags_t complain
)
13848 ++processing_template_decl
;
13849 for (; parms
; parms
= TREE_CHAIN (parms
))
13851 tree level
= TREE_VALUE (parms
);
13852 for (tree parm
: tree_vec_range (level
))
13853 TEMPLATE_PARM_CONSTRAINTS (parm
)
13854 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm
), args
,
13855 complain
, NULL_TREE
);
13857 --processing_template_decl
;
13860 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13861 type T. If T is not an aggregate or enumeration type, it is
13862 handled as if by tsubst. IN_DECL is as for tsubst. If
13863 ENTERING_SCOPE is nonzero, T is the context for a template which
13864 we are presently tsubst'ing. Return the substituted value. */
13867 tsubst_aggr_type (tree t
,
13869 tsubst_flags_t complain
,
13871 int entering_scope
)
13873 if (t
== NULL_TREE
)
13876 /* Handle typedefs via tsubst so that they get consistently reused. */
13877 if (typedef_variant_p (t
))
13879 t
= tsubst (t
, args
, complain
, in_decl
);
13880 if (t
== error_mark_node
)
13881 return error_mark_node
;
13883 /* The effect of entering_scope is that for a dependent specialization
13884 A<T>, lookup_template_class prefers to return A's primary template
13885 type instead of the implicit instantiation. So when entering_scope,
13886 we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
13887 taking advantage of the fact that lookup_template_class links the two
13888 types by setting TYPE_CANONICAL of the latter to the former. */
13890 && CLASS_TYPE_P (t
)
13891 && dependent_type_p (t
)
13892 && TYPE_CANONICAL (t
) == TREE_TYPE (TYPE_TI_TEMPLATE (t
)))
13893 t
= TYPE_CANONICAL (t
);
13898 switch (TREE_CODE (t
))
13901 case ENUMERAL_TYPE
:
13903 return tsubst_aggr_type_1 (t
, args
, complain
, in_decl
, entering_scope
);
13906 return tsubst (t
, args
, complain
, in_decl
);
13910 /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
13911 and ENUMERAL_TYPE cases of tsubst. */
13914 tsubst_aggr_type_1 (tree t
,
13916 tsubst_flags_t complain
,
13918 int entering_scope
)
13920 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13925 /* Figure out what arguments are appropriate for the
13926 type we are trying to find. For example, given:
13928 template <class T> struct S;
13929 template <class T, class U> void f(T, U) { S<U> su; }
13931 and supposing that we are instantiating f<int, double>,
13932 then our ARGS will be {int, double}, but, when looking up
13933 S we only want {double}. */
13934 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13935 complain
, in_decl
);
13936 if (argvec
== error_mark_node
)
13937 r
= error_mark_node
;
13940 r
= lookup_template_class (t
, argvec
, in_decl
, NULL_TREE
,
13941 entering_scope
, complain
);
13942 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
13948 /* This is not a template type, so there's nothing to do. */
13952 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13953 indexed in reverse order of the parameters. */
13955 static GTY((cache
)) hash_table
<tree_vec_map_cache_hasher
> *defarg_inst
;
13957 /* Return a reference to the vec* of defarg insts for FN. */
13959 static vec
<tree
,va_gc
> *&
13960 defarg_insts_for (tree fn
)
13963 defarg_inst
= hash_table
<tree_vec_map_cache_hasher
>::create_ggc (13);
13964 tree_vec_map in
= { { fn
}, nullptr };
13965 tree_vec_map
**slot
13966 = defarg_inst
->find_slot_with_hash (&in
, DECL_UID (fn
), INSERT
);
13969 *slot
= ggc_alloc
<tree_vec_map
> ();
13972 return (*slot
)->to
;
13975 /* Substitute into the default argument ARG (a default argument for
13976 FN), which has the indicated TYPE. */
13979 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13980 tsubst_flags_t complain
)
13982 int errs
= errorcount
+ sorrycount
;
13984 /* This can happen in invalid code. */
13985 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13989 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13990 && CONSTRUCTOR_NELTS (arg
) == 0)
13993 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13994 parm
= chain_index (parmnum
, parm
);
13995 tree parmtype
= TREE_TYPE (parm
);
13996 if (DECL_BY_REFERENCE (parm
))
13997 parmtype
= TREE_TYPE (parmtype
);
13998 if (parmtype
== error_mark_node
)
13999 return error_mark_node
;
14001 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
14003 /* Remember the location of the pointer to the vec rather than the location
14004 of the particular element, in case the vec grows in tsubst_expr. */
14005 vec
<tree
,va_gc
> *&defs
= defarg_insts_for (fn
);
14006 /* Index in reverse order to avoid allocating space for initial parameters
14007 that don't have default arguments. */
14008 unsigned ridx
= list_length (parm
);
14009 if (vec_safe_length (defs
) < ridx
)
14010 vec_safe_grow_cleared (defs
, ridx
);
14011 else if (tree inst
= (*defs
)[ridx
- 1])
14014 /* This default argument came from a template. Instantiate the
14015 default argument here, not in tsubst. In the case of
14024 we must be careful to do name lookup in the scope of S<T>,
14025 rather than in the current class. */
14026 push_to_top_level ();
14027 push_access_scope (fn
);
14028 push_deferring_access_checks (dk_no_deferred
);
14029 /* So in_immediate_context knows this is a default argument. */
14030 begin_scope (sk_function_parms
, fn
);
14031 start_lambda_scope (parm
);
14033 /* The default argument expression may cause implicitly defined
14034 member functions to be synthesized, which will result in garbage
14035 collection. We must treat this situation as if we were within
14036 the body of function so as to avoid collecting live data on the
14039 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), complain
, NULL_TREE
);
14042 finish_lambda_scope ();
14044 /* Make sure the default argument is reasonable. */
14045 arg
= check_default_argument (type
, arg
, complain
);
14047 if (errorcount
+sorrycount
> errs
14048 && (complain
& tf_warning_or_error
))
14049 inform (input_location
,
14050 " when instantiating default argument for call to %qD", fn
);
14053 pop_deferring_access_checks ();
14054 pop_access_scope (fn
);
14055 pop_from_top_level ();
14057 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
14058 (*defs
)[ridx
- 1] = arg
;
14063 /* Substitute into all the default arguments for FN. */
14066 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
14071 tmpl_args
= DECL_TI_ARGS (fn
);
14073 /* If this function is not yet instantiated, we certainly don't need
14074 its default arguments. */
14075 if (uses_template_parms (tmpl_args
))
14077 /* Don't do this again for clones. */
14078 if (DECL_CLONED_FUNCTION_P (fn
))
14082 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14084 arg
= TREE_CHAIN (arg
), ++i
)
14085 if (TREE_PURPOSE (arg
))
14086 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
14088 TREE_PURPOSE (arg
),
14092 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14093 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
14095 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14098 store_explicit_specifier (tree v
, tree t
)
14100 if (!explicit_specifier_map
)
14101 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
14102 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
14103 explicit_specifier_map
->put (v
, t
);
14106 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14109 lookup_explicit_specifier (tree v
)
14111 return *explicit_specifier_map
->get (v
);
14114 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14115 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14116 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14120 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
14121 tree raises
, tsubst_flags_t complain
)
14123 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
14126 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14128 new_type
= build_function_type (return_type
, arg_types
);
14129 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
14133 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14134 /* Don't pick up extra function qualifiers from the basetype. */
14135 r
= cp_build_qualified_type (r
, type_memfn_quals (t
), complain
);
14136 if (! MAYBE_CLASS_TYPE_P (r
))
14140 Type deduction may fail for any of the following
14143 -- Attempting to create "pointer to member of T" when T
14144 is not a class type. */
14145 if (complain
& tf_error
)
14146 error ("creating pointer to member function of non-class type %qT",
14148 return error_mark_node
;
14151 new_type
= build_method_type_directly (r
, return_type
,
14152 TREE_CHAIN (arg_types
));
14154 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
14156 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14157 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14158 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
14161 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14162 each of its formal parameters. If there is a disagreement then rebuild
14163 DECL's function type according to its formal parameter types, as part of a
14164 resolution for Core issues 1001/1322. */
14167 maybe_rebuild_function_decl_type (tree decl
)
14169 bool function_type_needs_rebuilding
= false;
14170 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
14172 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
14173 while (parm_type_list
&& parm_type_list
!= void_list_node
)
14175 tree parm_type
= TREE_VALUE (parm_type_list
);
14176 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
14177 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
14179 function_type_needs_rebuilding
= true;
14183 parm_list
= DECL_CHAIN (parm_list
);
14184 parm_type_list
= TREE_CHAIN (parm_type_list
);
14188 if (!function_type_needs_rebuilding
)
14191 const tree fntype
= TREE_TYPE (decl
);
14192 tree parm_list
= DECL_ARGUMENTS (decl
);
14193 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
14194 tree new_parm_type_list
= NULL_TREE
;
14195 tree
*q
= &new_parm_type_list
;
14196 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
14198 *q
= copy_node (old_parm_type_list
);
14199 parm_list
= DECL_CHAIN (parm_list
);
14200 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
14201 q
= &TREE_CHAIN (*q
);
14203 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
14205 *q
= copy_node (old_parm_type_list
);
14206 tree
*new_parm_type
= &TREE_VALUE (*q
);
14207 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
14208 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
14209 *new_parm_type
= formal_parm_type_unqual
;
14211 parm_list
= DECL_CHAIN (parm_list
);
14212 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
14213 q
= &TREE_CHAIN (*q
);
14215 if (old_parm_type_list
== void_list_node
)
14216 *q
= void_list_node
;
14219 = rebuild_function_or_method_type (fntype
,
14220 TREE_TYPE (fntype
), new_parm_type_list
,
14221 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
14224 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14227 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
14228 tree lambda_fntype
)
14230 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
14231 hashval_t hash
= 0;
14234 /* Nobody should be tsubst'ing into non-template functions. */
14235 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
14236 || DECL_LOCAL_DECL_P (t
));
14238 if (DECL_LOCAL_DECL_P (t
))
14240 if (tree spec
= retrieve_local_specialization (t
))
14243 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
14245 /* If T is not dependent, just return it. */
14246 if (!uses_template_parms (DECL_TI_ARGS (t
))
14247 && !LAMBDA_FUNCTION_P (t
))
14250 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14251 if (non_templated_friend_p (t
))
14254 /* Calculate the most general template of which R is a
14256 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
14258 /* We're substituting a lambda function under tsubst_lambda_expr but not
14259 directly from it; find the matching function we're already inside.
14260 But don't do this if T is a generic lambda with a single level of
14261 template parms, as in that case we're doing a normal instantiation. */
14262 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
14263 && (!generic_lambda_fn_p (t
)
14264 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
14265 return enclosing_instantiation_of (t
);
14267 /* Calculate the complete set of arguments used to
14269 argvec
= tsubst_template_args (DECL_TI_ARGS
14270 (DECL_TEMPLATE_RESULT
14271 (DECL_TI_TEMPLATE (t
))),
14272 args
, complain
, in_decl
);
14273 if (argvec
== error_mark_node
)
14274 return error_mark_node
;
14276 /* Check to see if we already have this specialization. */
14277 if (!lambda_fntype
)
14279 hash
= spec_hasher::hash (gen_tmpl
, argvec
);
14280 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
14281 /* The spec for these args might be a partial instantiation of the
14282 template, but here what we want is the FUNCTION_DECL. */
14283 return STRIP_TEMPLATE (spec
);
14288 /* This special case arises when we have something like this:
14290 template <class T> struct S {
14291 friend void f<int>(int, double);
14294 Here, the DECL_TI_TEMPLATE for the friend declaration
14295 will be an IDENTIFIER_NODE. We are being called from
14296 tsubst_friend_function, and we want only to create a
14297 new decl (R) with appropriate types so that we can call
14298 determine_specialization. */
14300 gen_tmpl
= NULL_TREE
;
14301 argvec
= NULL_TREE
;
14304 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
14306 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
14307 bool member
= ctx
&& TYPE_P (ctx
);
14309 if (member
&& !closure
)
14310 ctx
= tsubst_aggr_type (ctx
, args
,
14311 complain
, t
, /*entering_scope=*/1);
14313 tree type
= (lambda_fntype
? lambda_fntype
14314 : tsubst (TREE_TYPE (t
), args
,
14315 complain
| tf_fndecl_type
, in_decl
));
14316 if (type
== error_mark_node
)
14317 return error_mark_node
;
14319 /* If we hit excessive deduction depth, the type is bogus even if
14320 it isn't error_mark_node, so don't build a decl. */
14321 if (excessive_deduction_depth
)
14322 return error_mark_node
;
14324 /* We do NOT check for matching decls pushed separately at this
14325 point, as they may not represent instantiations of this
14326 template, and in any case are considered separate under the
14328 tree r
= copy_decl (t
);
14329 DECL_USE_TEMPLATE (r
) = 0;
14330 TREE_TYPE (r
) = type
;
14331 /* Clear out the mangled name and RTL for the instantiation. */
14332 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14333 SET_DECL_RTL (r
, NULL
);
14334 /* Leave DECL_INITIAL set on deleted instantiations. */
14335 if (!DECL_DELETED_FN (r
))
14336 DECL_INITIAL (r
) = NULL_TREE
;
14337 DECL_CONTEXT (r
) = ctx
;
14338 set_instantiating_module (r
);
14340 /* Handle explicit(dependent-expr). */
14341 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
14343 tree spec
= lookup_explicit_specifier (t
);
14344 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
);
14345 spec
= build_explicit_specifier (spec
, complain
);
14346 if (spec
== error_mark_node
)
14347 return error_mark_node
;
14348 if (instantiation_dependent_expression_p (spec
))
14349 store_explicit_specifier (r
, spec
);
14352 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
14353 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r
) = false;
14357 /* OpenMP UDRs have the only argument a reference to the declared
14358 type. We want to diagnose if the declared type is a reference,
14359 which is invalid, but as references to references are usually
14360 quietly merged, diagnose it here. */
14361 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
14364 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
14365 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
14366 if (TYPE_REF_P (argtype
))
14367 error_at (DECL_SOURCE_LOCATION (t
),
14368 "reference type %qT in "
14369 "%<#pragma omp declare reduction%>", argtype
);
14370 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
14371 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
14375 if (member
&& DECL_CONV_FN_P (r
))
14376 /* Type-conversion operator. Reconstruct the name, in
14377 case it's the name of one of the template's parameters. */
14378 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
14380 tree parms
= DECL_ARGUMENTS (t
);
14382 parms
= DECL_CHAIN (parms
);
14383 parms
= tsubst (parms
, args
, complain
, t
);
14384 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
14385 DECL_CONTEXT (parm
) = r
;
14388 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
14389 DECL_NAME (tparm
) = closure_identifier
;
14390 DECL_CHAIN (tparm
) = parms
;
14393 DECL_ARGUMENTS (r
) = parms
;
14394 DECL_RESULT (r
) = NULL_TREE
;
14396 maybe_rebuild_function_decl_type (r
);
14398 TREE_STATIC (r
) = 0;
14399 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14400 DECL_EXTERNAL (r
) = 1;
14401 /* If this is an instantiation of a function with internal
14402 linkage, we already know what object file linkage will be
14403 assigned to the instantiation. */
14404 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14405 DECL_DEFER_OUTPUT (r
) = 0;
14406 DECL_CHAIN (r
) = NULL_TREE
;
14407 DECL_PENDING_INLINE_INFO (r
) = 0;
14408 DECL_PENDING_INLINE_P (r
) = 0;
14409 DECL_SAVED_TREE (r
) = NULL_TREE
;
14410 DECL_STRUCT_FUNCTION (r
) = NULL
;
14412 /* We'll re-clone as appropriate in instantiate_template. */
14413 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14415 /* If we aren't complaining now, return on error before we register
14416 the specialization so that we'll complain eventually. */
14417 if ((complain
& tf_error
) == 0
14418 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14419 && !grok_op_properties (r
, /*complain=*/false))
14420 return error_mark_node
;
14422 /* Associate the constraints directly with the instantiation. We
14423 don't substitute through the constraints; that's only done when
14424 they are checked. */
14425 if (tree ci
= get_constraints (t
))
14426 set_constraints (r
, ci
);
14428 if (DECL_FRIEND_CONTEXT (t
))
14429 SET_DECL_FRIEND_CONTEXT (r
,
14430 tsubst (DECL_FRIEND_CONTEXT (t
),
14431 args
, complain
, in_decl
));
14433 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14434 args
, complain
, in_decl
))
14435 return error_mark_node
;
14437 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14438 this in the special friend case mentioned above where
14439 GEN_TMPL is NULL. */
14440 if (gen_tmpl
&& !closure
)
14442 DECL_TEMPLATE_INFO (r
)
14443 = build_template_info (gen_tmpl
, argvec
);
14444 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14447 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14449 /* We instantiated this while substituting into
14450 the type earlier (template/friend54.C). */
14453 /* We're not supposed to instantiate default arguments
14454 until they are called, for a template. But, for a
14457 template <class T> void f ()
14458 { extern void g(int i = T()); }
14460 we should do the substitution when the template is
14461 instantiated. We handle the member function case in
14462 instantiate_class_template since the default arguments
14463 might refer to other members of the class. */
14465 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14466 && !uses_template_parms (argvec
))
14467 tsubst_default_arguments (r
, complain
);
14469 else if (DECL_LOCAL_DECL_P (r
))
14471 if (!cp_unevaluated_operand
)
14472 register_local_specialization (r
, t
);
14475 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14477 /* Copy the list of befriending classes. */
14478 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14480 friends
= &TREE_CHAIN (*friends
))
14482 *friends
= copy_node (*friends
);
14483 TREE_VALUE (*friends
)
14484 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14487 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14489 maybe_retrofit_in_chrg (r
);
14490 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14491 return error_mark_node
;
14492 /* If this is an instantiation of a member template, clone it.
14493 If it isn't, that'll be handled by
14494 clone_constructors_and_destructors. */
14495 if (gen_tmpl
&& PRIMARY_TEMPLATE_P (gen_tmpl
))
14496 clone_cdtor (r
, /*update_methods=*/false);
14498 else if ((complain
& tf_error
) != 0
14499 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14500 && !grok_op_properties (r
, /*complain=*/true))
14501 return error_mark_node
;
14503 /* Possibly limit visibility based on template args. */
14504 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14505 if (DECL_VISIBILITY_SPECIFIED (t
))
14507 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14508 DECL_ATTRIBUTES (r
)
14509 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14511 determine_visibility (r
);
14512 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14513 && !processing_template_decl
)
14514 defaulted_late_check (r
);
14517 if (tree attr
= lookup_attribute ("omp declare variant base",
14518 DECL_ATTRIBUTES (r
)))
14519 omp_declare_variant_finalize (r
, attr
);
14524 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14527 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14528 tree lambda_fntype
)
14530 /* We can get here when processing a member function template,
14531 member class template, or template template parameter. */
14532 tree decl
= DECL_TEMPLATE_RESULT (t
);
14538 hashval_t hash
= 0;
14540 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14542 /* Template template parameter is treated here. */
14543 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14544 if (new_type
== error_mark_node
)
14545 r
= error_mark_node
;
14546 /* If we get a real template back, return it. This can happen in
14547 the context of most_specialized_partial_spec. */
14548 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14551 /* The new TEMPLATE_DECL was built in
14552 reduce_template_parm_level. */
14553 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14557 if (!lambda_fntype
)
14559 /* We might already have an instance of this template.
14560 The ARGS are for the surrounding class type, so the
14561 full args contain the tsubst'd args for the context,
14562 plus the innermost args from the template decl. */
14563 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14564 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14565 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14566 /* Because this is a template, the arguments will still be
14567 dependent, even after substitution. If
14568 PROCESSING_TEMPLATE_DECL is not set, the dependency
14569 predicates will short-circuit. */
14570 ++processing_template_decl
;
14571 full_args
= tsubst_template_args (tmpl_args
, args
,
14572 complain
, in_decl
);
14573 --processing_template_decl
;
14574 if (full_args
== error_mark_node
)
14575 return error_mark_node
;
14577 /* If this is a default template template argument,
14578 tsubst might not have changed anything. */
14579 if (full_args
== tmpl_args
)
14582 hash
= spec_hasher::hash (t
, full_args
);
14583 spec
= retrieve_specialization (t
, full_args
, hash
);
14584 if (spec
!= NULL_TREE
)
14587 /* Type partial instantiations are stored as the type by
14588 lookup_template_class_1, not here as the template. */
14589 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14594 /* Make a new template decl. It will be similar to the
14595 original, but will record the current template arguments.
14596 We also create a new function declaration, which is just
14597 like the old one, but points to this new template, rather
14598 than the old one. */
14600 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14601 DECL_CHAIN (r
) = NULL_TREE
;
14603 // Build new template info linking to the original template decl.
14604 if (!lambda_fntype
)
14606 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14607 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14610 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14612 /* The template parameters for this new template are all the
14613 template parameters for the old template, except the
14614 outermost level of parameters. */
14615 auto tparm_guard
= make_temp_override (current_template_parms
);
14616 DECL_TEMPLATE_PARMS (r
)
14617 = current_template_parms
14618 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14621 bool class_p
= false;
14623 ++processing_template_decl
;
14624 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14625 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14628 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14631 inner
= TREE_TYPE (inner
);
14634 inner
= tsubst_aggr_type (inner
, args
, complain
,
14635 in_decl
, /*entering*/1);
14637 inner
= tsubst (inner
, args
, complain
, in_decl
);
14639 --processing_template_decl
;
14640 if (inner
== error_mark_node
)
14641 return error_mark_node
;
14645 /* For a partial specialization, we need to keep pointing to
14646 the primary template. */
14647 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14648 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14650 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14651 inner
= TYPE_MAIN_DECL (inner
);
14653 else if (lambda_fntype
)
14655 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14656 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14660 DECL_TI_TEMPLATE (inner
) = r
;
14661 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14664 DECL_TEMPLATE_RESULT (r
) = inner
;
14665 TREE_TYPE (r
) = TREE_TYPE (inner
);
14666 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14670 /* Propagate module information from the decl. */
14671 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
14672 if (DECL_LANG_SPECIFIC (inner
))
14673 /* If this is a constrained template, the above tsubst of
14674 inner can find the unconstrained template, which may have
14675 come from an import. This is ok, because we don't
14676 register this instantiation (see below). */
14677 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
14678 || (TEMPLATE_PARMS_CONSTRAINTS
14679 (DECL_TEMPLATE_PARMS (t
))));
14682 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14683 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14685 if (PRIMARY_TEMPLATE_P (t
))
14686 DECL_PRIMARY_TEMPLATE (r
) = r
;
14688 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !lambda_fntype
)
14689 /* Record this non-type partial instantiation. */
14690 register_specialization (r
, t
,
14691 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14697 /* True if FN is the op() for a lambda in an uninstantiated template. */
14700 lambda_fn_in_template_p (tree fn
)
14702 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14704 tree closure
= DECL_CONTEXT (fn
);
14705 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14708 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14709 which the above is true. */
14712 regenerated_lambda_fn_p (tree fn
)
14714 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14716 tree closure
= DECL_CONTEXT (fn
);
14717 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14718 return LAMBDA_EXPR_REGEN_INFO (lam
) != NULL_TREE
;
14721 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14722 If T is not a regenerated LAMBDA_EXPR, return T. */
14725 most_general_lambda (tree t
)
14727 while (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14728 t
= TI_TEMPLATE (ti
);
14732 /* Return the set of template arguments used to regenerate the lambda T
14733 from its most general lambda. */
14736 lambda_regenerating_args (tree t
)
14738 if (LAMBDA_FUNCTION_P (t
))
14739 t
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t
));
14740 gcc_assert (TREE_CODE (t
) == LAMBDA_EXPR
);
14741 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14742 return TI_ARGS (ti
);
14747 /* We're instantiating a variable from template function TCTX. Return the
14748 corresponding current enclosing scope. We can match them up using
14749 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14750 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14751 the template definition in regenerate_decl_from_template. */
14754 enclosing_instantiation_of (tree tctx
)
14756 tree fn
= current_function_decl
;
14758 /* We shouldn't ever need to do this for other artificial functions. */
14759 gcc_assert (!DECL_ARTIFICIAL (tctx
) || LAMBDA_FUNCTION_P (tctx
));
14761 for (; fn
; fn
= decl_function_context (fn
))
14762 if (DECL_SOURCE_LOCATION (fn
) == DECL_SOURCE_LOCATION (tctx
))
14764 gcc_unreachable ();
14767 /* Substitute the ARGS into the T, which is a _DECL. Return the
14768 result of the substitution. Issue error and warning messages under
14769 control of COMPLAIN. */
14772 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14774 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14775 location_t saved_loc
;
14776 tree r
= NULL_TREE
;
14778 hashval_t hash
= 0;
14780 /* Set the filename and linenumber to improve error-reporting. */
14781 saved_loc
= input_location
;
14782 input_location
= DECL_SOURCE_LOCATION (t
);
14784 switch (TREE_CODE (t
))
14786 case TEMPLATE_DECL
:
14787 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14790 case FUNCTION_DECL
:
14791 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14796 tree type
= NULL_TREE
;
14798 tree expanded_types
= NULL_TREE
;
14799 tree prev_r
= NULL_TREE
;
14800 tree first_r
= NULL_TREE
;
14802 if (DECL_PACK_P (t
))
14804 /* If there is a local specialization that isn't a
14805 parameter pack, it means that we're doing a "simple"
14806 substitution from inside tsubst_pack_expansion. Just
14807 return the local specialization (which will be a single
14809 tree spec
= retrieve_local_specialization (t
);
14811 && TREE_CODE (spec
) == PARM_DECL
14812 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14815 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14816 the parameters in this function parameter pack. */
14817 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14818 complain
, in_decl
);
14819 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14821 len
= TREE_VEC_LENGTH (expanded_types
);
14823 /* Zero-length parameter packs are boring. Just substitute
14825 if (len
== 0 && !cp_unevaluated_operand
)
14826 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14831 /* All we did was update the type. Make a note of that. */
14832 type
= expanded_types
;
14833 expanded_types
= NULL_TREE
;
14837 /* Loop through all of the parameters we'll build. When T is
14838 a function parameter pack, LEN is the number of expanded
14839 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14841 for (i
= 0; i
< len
; ++i
)
14845 if (DECL_TEMPLATE_PARM_P (t
))
14846 SET_DECL_TEMPLATE_PARM_P (r
);
14848 if (expanded_types
)
14849 /* We're on the Ith parameter of the function parameter
14852 /* Get the Ith type. */
14853 type
= TREE_VEC_ELT (expanded_types
, i
);
14855 /* Rename the parameter to include the index. */
14857 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14860 /* We're dealing with a normal parameter. */
14861 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14863 type
= type_decays_to (type
);
14864 TREE_TYPE (r
) = type
;
14865 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14867 if (DECL_INITIAL (r
))
14869 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14870 DECL_INITIAL (r
) = TREE_TYPE (r
);
14872 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14873 complain
, in_decl
);
14876 DECL_CONTEXT (r
) = NULL_TREE
;
14878 if (!DECL_TEMPLATE_PARM_P (r
))
14879 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14881 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14882 args
, complain
, in_decl
))
14883 return error_mark_node
;
14885 /* Keep track of the first new parameter we
14886 generate. That's what will be returned to the
14891 /* Build a proper chain of parameters when substituting
14892 into a function parameter pack. */
14894 DECL_CHAIN (prev_r
) = r
;
14897 /* If cp_unevaluated_operand is set, we're just looking for a
14898 single dummy parameter, so don't keep going. */
14899 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14900 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14901 complain
, DECL_CHAIN (t
));
14903 /* FIRST_R contains the start of the chain we've built. */
14910 tree type
= NULL_TREE
;
14911 tree vec
= NULL_TREE
;
14912 tree expanded_types
= NULL_TREE
;
14915 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14917 /* This field is a lambda capture pack. Return a TREE_VEC of
14918 the expanded fields to instantiate_class_template_1. */
14919 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14920 complain
, in_decl
);
14921 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14923 len
= TREE_VEC_LENGTH (expanded_types
);
14924 vec
= make_tree_vec (len
);
14928 /* All we did was update the type. Make a note of that. */
14929 type
= expanded_types
;
14930 expanded_types
= NULL_TREE
;
14934 for (int i
= 0; i
< len
; ++i
)
14937 if (expanded_types
)
14939 type
= TREE_VEC_ELT (expanded_types
, i
);
14941 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14944 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14946 if (type
== error_mark_node
)
14947 RETURN (error_mark_node
);
14948 TREE_TYPE (r
) = type
;
14949 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14951 if (DECL_C_BIT_FIELD (r
))
14952 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14954 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14955 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14956 complain
, in_decl
);
14957 if (DECL_INITIAL (t
))
14959 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14960 NSDMI in perform_member_init. Still set DECL_INITIAL
14961 so that we know there is one. */
14962 DECL_INITIAL (r
) = void_node
;
14963 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14964 retrofit_lang_decl (r
);
14965 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14967 /* We don't have to set DECL_CONTEXT here; it is set by
14968 finish_member_declaration. */
14969 DECL_CHAIN (r
) = NULL_TREE
;
14971 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14972 args
, complain
, in_decl
))
14973 return error_mark_node
;
14976 TREE_VEC_ELT (vec
, i
) = r
;
14985 /* We reach here only for member using decls. We also need to check
14986 uses_template_parms because DECL_DEPENDENT_P is not set for a
14987 using-declaration that designates a member of the current
14988 instantiation (c++/53549). */
14989 if (DECL_DEPENDENT_P (t
)
14990 || uses_template_parms (USING_DECL_SCOPE (t
)))
14992 /* True iff this using-decl was written as a pack expansion
14993 (and a pack appeared in its scope or name). If a pack
14994 appeared in both, we expand the packs separately and
14995 manually merge them. */
14996 bool variadic_p
= false;
14998 tree scope
= USING_DECL_SCOPE (t
);
14999 if (PACK_EXPANSION_P (scope
))
15001 scope
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
15005 scope
= tsubst_copy (scope
, args
, complain
, in_decl
);
15007 tree name
= DECL_NAME (t
);
15008 if (IDENTIFIER_CONV_OP_P (name
)
15009 && PACK_EXPANSION_P (TREE_TYPE (name
)))
15011 name
= tsubst_pack_expansion (TREE_TYPE (name
), args
,
15012 complain
, in_decl
);
15013 if (name
== error_mark_node
)
15015 r
= error_mark_node
;
15018 for (tree
& elt
: tree_vec_range (name
))
15019 elt
= make_conv_op_name (elt
);
15023 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15028 else if (TREE_CODE (scope
) == TREE_VEC
15029 && TREE_CODE (name
) == TREE_VEC
)
15031 if (TREE_VEC_LENGTH (scope
) != TREE_VEC_LENGTH (name
))
15033 error ("mismatched argument pack lengths (%d vs %d)",
15034 TREE_VEC_LENGTH (scope
), TREE_VEC_LENGTH (name
));
15035 r
= error_mark_node
;
15038 len
= TREE_VEC_LENGTH (scope
);
15040 else if (TREE_CODE (scope
) == TREE_VEC
)
15041 len
= TREE_VEC_LENGTH (scope
);
15042 else /* TREE_CODE (name) == TREE_VEC */
15043 len
= TREE_VEC_LENGTH (name
);
15045 r
= make_tree_vec (len
);
15046 for (int i
= 0; i
< len
; ++i
)
15048 tree escope
= (TREE_CODE (scope
) == TREE_VEC
15049 ? TREE_VEC_ELT (scope
, i
)
15051 tree ename
= (TREE_CODE (name
) == TREE_VEC
15052 ? TREE_VEC_ELT (name
, i
)
15054 tree elt
= do_class_using_decl (escope
, ename
);
15057 r
= error_mark_node
;
15060 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
15061 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
15062 TREE_VEC_ELT (r
, i
) = elt
;
15065 if (!variadic_p
&& r
!= error_mark_node
)
15066 r
= TREE_VEC_ELT (r
, 0);
15071 DECL_CHAIN (r
) = NULL_TREE
;
15078 tree argvec
= NULL_TREE
;
15079 tree gen_tmpl
= NULL_TREE
;
15080 tree tmpl
= NULL_TREE
;
15081 tree type
= NULL_TREE
;
15083 if (TREE_TYPE (t
) == error_mark_node
)
15084 RETURN (error_mark_node
);
15086 if (TREE_CODE (t
) == TYPE_DECL
15087 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
15089 /* If this is the canonical decl, we don't have to
15090 mess with instantiations, and often we can't (for
15091 typename, template type parms and such). Note that
15092 TYPE_NAME is not correct for the above test if
15093 we've copied the type for a typedef. */
15094 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15095 if (type
== error_mark_node
)
15096 RETURN (error_mark_node
);
15097 r
= TYPE_NAME (type
);
15101 /* Check to see if we already have the specialization we
15103 tree spec
= NULL_TREE
;
15104 bool local_p
= false;
15105 tree ctx
= DECL_CONTEXT (t
);
15106 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
15107 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
15110 if (DECL_CLASS_SCOPE_P (t
))
15112 ctx
= tsubst_aggr_type (ctx
, args
,
15114 in_decl
, /*entering_scope=*/1);
15115 if (DECL_SELF_REFERENCE_P (t
))
15116 /* The context and type of an injected-class-name are
15117 the same, so we don't need to substitute both. */
15119 /* If CTX is unchanged, then T is in fact the
15120 specialization we want. That situation occurs when
15121 referencing a static data member within in its own
15122 class. We can use pointer equality, rather than
15123 same_type_p, because DECL_CONTEXT is always
15125 if (ctx
== DECL_CONTEXT (t
)
15126 /* ... unless T is a member template; in which
15127 case our caller can be willing to create a
15128 specialization of that template represented
15130 && !(DECL_TI_TEMPLATE (t
)
15131 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
15137 tmpl
= DECL_TI_TEMPLATE (t
);
15138 gen_tmpl
= most_general_template (tmpl
);
15139 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
15140 if (argvec
!= error_mark_node
15141 && PRIMARY_TEMPLATE_P (gen_tmpl
)
15142 && TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (argvec
))
15143 /* We're fully specializing a template declaration, so
15144 we need to coerce the innermost arguments corresponding to
15146 argvec
= (coerce_template_parms
15147 (DECL_TEMPLATE_PARMS (gen_tmpl
),
15148 argvec
, t
, complain
));
15149 if (argvec
== error_mark_node
)
15150 RETURN (error_mark_node
);
15151 hash
= spec_hasher::hash (gen_tmpl
, argvec
);
15152 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
15157 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
15158 /* Subsequent calls to pushdecl will fill this in. */
15160 /* A local variable. */
15162 /* Unless this is a reference to a static variable from an
15163 enclosing function, in which case we need to fill it in now. */
15164 if (TREE_STATIC (t
))
15166 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15167 if (fn
!= current_function_decl
)
15170 spec
= retrieve_local_specialization (t
);
15172 /* If we already have the specialization we need, there is
15173 nothing more to do. */
15180 /* Create a new node for the specialization we need. */
15181 if (type
== NULL_TREE
)
15183 if (is_typedef_decl (t
))
15184 type
= DECL_ORIGINAL_TYPE (t
);
15186 type
= TREE_TYPE (t
);
15188 && VAR_HAD_UNKNOWN_BOUND (t
)
15189 && type
!= error_mark_node
)
15190 type
= strip_array_domain (type
);
15191 tsubst_flags_t tcomplain
= complain
;
15193 tcomplain
|= tf_tst_ok
;
15194 type
= tsubst (type
, args
, tcomplain
, in_decl
);
15195 /* Substituting the type might have recursively instantiated this
15196 same alias (c++/86171). */
15197 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
15198 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
15207 DECL_INITIALIZED_P (r
) = 0;
15208 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
15209 if (type
== error_mark_node
)
15210 RETURN (error_mark_node
);
15211 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15213 /* It may seem that this case cannot occur, since:
15218 declares a function, not a variable. However:
15221 template <typename T> void g() { T t; }
15222 template void g<f>();
15224 is an attempt to declare a variable with function
15226 error ("variable %qD has function type",
15227 /* R is not yet sufficiently initialized, so we
15228 just use its name. */
15230 RETURN (error_mark_node
);
15232 type
= complete_type (type
);
15233 /* Wait until cp_finish_decl to set this again, to handle
15234 circular dependency (template/instantiate6.C). */
15235 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
15236 type
= check_var_type (DECL_NAME (r
), type
,
15237 DECL_SOURCE_LOCATION (r
));
15238 if (DECL_HAS_VALUE_EXPR_P (t
))
15240 tree ve
= DECL_VALUE_EXPR (t
);
15241 /* If the DECL_VALUE_EXPR is converted to the declared type,
15242 preserve the identity so that gimplify_type_sizes works. */
15243 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
15245 ve
= TREE_OPERAND (ve
, 0);
15246 ve
= tsubst_expr (ve
, args
, complain
, in_decl
);
15247 if (REFERENCE_REF_P (ve
))
15249 gcc_assert (TYPE_REF_P (type
));
15250 ve
= TREE_OPERAND (ve
, 0);
15253 ve
= build_nop (type
, ve
);
15254 else if (DECL_LANG_SPECIFIC (t
)
15255 && DECL_OMP_PRIVATIZED_MEMBER (t
)
15256 && TREE_CODE (ve
) == COMPONENT_REF
15257 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
15258 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
15259 type
= TREE_TYPE (ve
);
15261 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
15262 == TYPE_MAIN_VARIANT (type
));
15263 SET_DECL_VALUE_EXPR (r
, ve
);
15265 if (CP_DECL_THREAD_LOCAL_P (r
)
15266 && !processing_template_decl
)
15267 set_decl_tls_model (r
, decl_default_tls_model (r
));
15269 else if (DECL_SELF_REFERENCE_P (t
))
15270 SET_DECL_SELF_REFERENCE_P (r
);
15271 TREE_TYPE (r
) = type
;
15272 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15273 DECL_CONTEXT (r
) = ctx
;
15274 /* Clear out the mangled name and RTL for the instantiation. */
15275 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
15276 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
15277 SET_DECL_RTL (r
, NULL
);
15278 set_instantiating_module (r
);
15280 /* The initializer must not be expanded until it is required;
15281 see [temp.inst]. */
15282 DECL_INITIAL (r
) = NULL_TREE
;
15283 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
15286 if (DECL_LANG_SPECIFIC (r
))
15287 SET_DECL_DEPENDENT_INIT_P (r
, false);
15289 SET_DECL_MODE (r
, VOIDmode
);
15291 /* Possibly limit visibility based on template args. */
15292 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
15293 if (DECL_VISIBILITY_SPECIFIED (t
))
15295 DECL_VISIBILITY_SPECIFIED (r
) = 0;
15296 DECL_ATTRIBUTES (r
)
15297 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
15299 determine_visibility (r
);
15304 /* A static data member declaration is always marked
15305 external when it is declared in-class, even if an
15306 initializer is present. We mimic the non-template
15307 processing here. */
15308 DECL_EXTERNAL (r
) = 1;
15309 if (DECL_NAMESPACE_SCOPE_P (t
))
15310 DECL_NOT_REALLY_EXTERN (r
) = 1;
15312 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
15313 SET_DECL_IMPLICIT_INSTANTIATION (r
);
15314 if (!error_operand_p (r
) || (complain
& tf_error
))
15315 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
15319 if (DECL_LANG_SPECIFIC (r
))
15320 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
15321 if (!cp_unevaluated_operand
)
15322 register_local_specialization (r
, t
);
15325 DECL_CHAIN (r
) = NULL_TREE
;
15327 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
15329 args
, complain
, in_decl
))
15330 return error_mark_node
;
15332 /* Preserve a typedef that names a type. */
15333 if (is_typedef_decl (r
) && type
!= error_mark_node
)
15335 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
15336 set_underlying_type (r
);
15338 /* common_handle_aligned_attribute doesn't apply the alignment
15339 to DECL_ORIGINAL_TYPE. */
15340 if (TYPE_USER_ALIGN (TREE_TYPE (t
)))
15341 TREE_TYPE (r
) = build_aligned_type (TREE_TYPE (r
),
15342 TYPE_ALIGN (TREE_TYPE (t
)));
15345 layout_decl (r
, 0);
15350 gcc_unreachable ();
15355 /* Restore the file and line information. */
15356 input_location
= saved_loc
;
15361 /* Substitute into the complete parameter type list PARMS. */
15364 tsubst_function_parms (tree parms
,
15366 tsubst_flags_t complain
,
15369 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
15372 /* Substitute into the ARG_TYPES of a function type.
15373 If END is a TREE_CHAIN, leave it and any following types
15377 tsubst_arg_types (tree arg_types
,
15380 tsubst_flags_t complain
,
15383 tree type
= NULL_TREE
;
15385 tree expanded_args
= NULL_TREE
;
15387 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
15390 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
15392 /* For a pack expansion, perform substitution on the
15393 entire expression. Later on, we'll handle the arguments
15395 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
15396 args
, complain
, in_decl
);
15398 if (TREE_CODE (expanded_args
) == TREE_VEC
)
15399 /* So that we'll spin through the parameters, one by one. */
15400 len
= TREE_VEC_LENGTH (expanded_args
);
15403 /* We only partially substituted into the parameter
15404 pack. Our type is TYPE_PACK_EXPANSION. */
15405 type
= expanded_args
;
15406 expanded_args
= NULL_TREE
;
15410 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
15412 /* Check if a substituted type is erroneous before substituting into
15413 the rest of the chain. */
15414 for (int i
= 0; i
< len
; i
++)
15417 type
= TREE_VEC_ELT (expanded_args
, i
);
15419 if (type
== error_mark_node
)
15420 return error_mark_node
;
15421 if (VOID_TYPE_P (type
))
15423 if (complain
& tf_error
)
15425 error ("invalid parameter type %qT", type
);
15427 error ("in declaration %q+D", in_decl
);
15429 return error_mark_node
;
15433 /* We do not substitute into default arguments here. The standard
15434 mandates that they be instantiated only when needed, which is
15435 done in build_over_call. */
15436 tree default_arg
= TREE_PURPOSE (arg_types
);
15438 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15439 since the new op() won't have any associated template arguments for us
15440 to refer to later. */
15441 if (lambda_fn_in_template_p (in_decl
)
15442 || (in_decl
&& TREE_CODE (in_decl
) == FUNCTION_DECL
15443 && DECL_LOCAL_DECL_P (in_decl
)))
15444 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
);
15446 tree remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
15447 args
, end
, complain
, in_decl
);
15448 if (remaining_arg_types
== error_mark_node
)
15449 return error_mark_node
;
15451 for (int i
= len
-1; i
>= 0; i
--)
15454 type
= TREE_VEC_ELT (expanded_args
, i
);
15456 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15457 top-level qualifiers as required. */
15458 type
= cv_unqualified (type_decays_to (type
));
15460 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15462 /* We've instantiated a template before its default arguments
15463 have been parsed. This can happen for a nested template
15464 class, and is not an error unless we require the default
15465 argument in a call of this function. */
15466 remaining_arg_types
15467 = tree_cons (default_arg
, type
, remaining_arg_types
);
15468 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15469 remaining_arg_types
);
15472 remaining_arg_types
15473 = hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15476 return remaining_arg_types
;
15479 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15480 *not* handle the exception-specification for FNTYPE, because the
15481 initial substitution of explicitly provided template parameters
15482 during argument deduction forbids substitution into the
15483 exception-specification:
15487 All references in the function type of the function template to the
15488 corresponding template parameters are replaced by the specified tem-
15489 plate argument values. If a substitution in a template parameter or
15490 in the function type of the function template results in an invalid
15491 type, type deduction fails. [Note: The equivalent substitution in
15492 exception specifications is done only when the function is instanti-
15493 ated, at which point a program is ill-formed if the substitution
15494 results in an invalid type.] */
15497 tsubst_function_type (tree t
,
15499 tsubst_flags_t complain
,
15503 tree arg_types
= NULL_TREE
;
15505 /* The TYPE_CONTEXT is not used for function/method types. */
15506 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15508 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15510 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15512 if (late_return_type_p
)
15514 /* Substitute the argument types. */
15515 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15516 complain
, in_decl
);
15517 if (arg_types
== error_mark_node
)
15518 return error_mark_node
;
15520 tree save_ccp
= current_class_ptr
;
15521 tree save_ccr
= current_class_ref
;
15522 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15523 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15524 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15527 /* DR 1207: 'this' is in scope in the trailing return type. */
15528 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15531 /* Substitute the return type. */
15532 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15536 current_class_ptr
= save_ccp
;
15537 current_class_ref
= save_ccr
;
15541 /* Substitute the return type. */
15542 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15544 if (return_type
== error_mark_node
)
15545 return error_mark_node
;
15546 /* DR 486 clarifies that creation of a function type with an
15547 invalid return type is a deduction failure. */
15548 if (TREE_CODE (return_type
) == ARRAY_TYPE
15549 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15551 if (complain
& tf_error
)
15553 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15554 error ("function returning an array");
15556 error ("function returning a function");
15558 return error_mark_node
;
15561 if (!late_return_type_p
)
15563 /* Substitute the argument types. */
15564 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15565 complain
, in_decl
);
15566 if (arg_types
== error_mark_node
)
15567 return error_mark_node
;
15570 /* Construct a new type node and return it. */
15571 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15572 /*raises=*/NULL_TREE
, complain
);
15575 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15576 ARGS into that specification, and return the substituted
15577 specification. If there is no specification, return NULL_TREE. */
15580 tsubst_exception_specification (tree fntype
,
15582 tsubst_flags_t complain
,
15589 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15590 new_specs
= NULL_TREE
;
15591 if (specs
&& TREE_PURPOSE (specs
))
15593 /* A noexcept-specifier. */
15594 tree expr
= TREE_PURPOSE (specs
);
15595 if (TREE_CODE (expr
) == INTEGER_CST
)
15599 /* Defer instantiation of noexcept-specifiers to avoid
15600 excessive instantiations (c++/49107). */
15601 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15602 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15604 /* We already partially instantiated this member template,
15605 so combine the new args with the old. */
15606 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15607 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15608 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15609 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15613 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15614 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15619 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15621 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15623 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15625 new_specs
= tsubst_copy_and_build (expr
, args
, complain
, in_decl
);
15627 new_specs
= build_noexcept_spec (new_specs
, complain
);
15628 /* We've instantiated a template before a noexcept-specifier
15629 contained therein has been parsed. This can happen for
15630 a nested template class:
15633 template<typename> struct B { B() noexcept(...); };
15634 struct A : B<int> { ... use B() ... };
15637 where completing B<int> will trigger instantiating the
15638 noexcept, even though we only parse it at the end of S. */
15639 if (UNPARSED_NOEXCEPT_SPEC_P (specs
))
15641 gcc_checking_assert (defer_ok
);
15642 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr
), new_specs
);
15647 if (! TREE_VALUE (specs
))
15654 tree expanded_specs
= NULL_TREE
;
15656 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15658 /* Expand the pack expansion type. */
15659 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15663 if (expanded_specs
== error_mark_node
)
15664 return error_mark_node
;
15665 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15666 len
= TREE_VEC_LENGTH (expanded_specs
);
15669 /* We're substituting into a member template, so
15670 we got a TYPE_PACK_EXPANSION back. Add that
15671 expansion and move on. */
15672 gcc_assert (TREE_CODE (expanded_specs
)
15673 == TYPE_PACK_EXPANSION
);
15674 new_specs
= add_exception_specifier (new_specs
,
15677 specs
= TREE_CHAIN (specs
);
15682 for (i
= 0; i
< len
; ++i
)
15684 if (expanded_specs
)
15685 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15687 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15688 if (spec
== error_mark_node
)
15690 new_specs
= add_exception_specifier (new_specs
, spec
,
15694 specs
= TREE_CHAIN (specs
);
15700 /* Substitute through a TREE_LIST of types or expressions, handling pack
15704 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15706 if (t
== void_list_node
)
15709 tree purpose
= TREE_PURPOSE (t
);
15710 tree purposevec
= NULL_TREE
;
15713 else if (PACK_EXPANSION_P (purpose
))
15715 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15716 if (TREE_CODE (purpose
) == TREE_VEC
)
15717 purposevec
= purpose
;
15719 else if (TYPE_P (purpose
))
15720 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15722 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15723 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15724 return error_mark_node
;
15726 tree value
= TREE_VALUE (t
);
15727 tree valuevec
= NULL_TREE
;
15730 else if (PACK_EXPANSION_P (value
))
15732 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15733 if (TREE_CODE (value
) == TREE_VEC
)
15736 else if (TYPE_P (value
))
15737 value
= tsubst (value
, args
, complain
, in_decl
);
15739 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15740 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15741 return error_mark_node
;
15743 tree chain
= TREE_CHAIN (t
);
15746 else if (TREE_CODE (chain
) == TREE_LIST
)
15747 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15748 else if (TYPE_P (chain
))
15749 chain
= tsubst (chain
, args
, complain
, in_decl
);
15751 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15752 if (chain
== error_mark_node
)
15753 return error_mark_node
;
15755 if (purpose
== TREE_PURPOSE (t
)
15756 && value
== TREE_VALUE (t
)
15757 && chain
== TREE_CHAIN (t
))
15761 /* Determine the number of arguments. */
15764 len
= TREE_VEC_LENGTH (purposevec
);
15765 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15768 len
= TREE_VEC_LENGTH (valuevec
);
15772 for (int i
= len
; i
-- > 0; )
15775 purpose
= TREE_VEC_ELT (purposevec
, i
);
15777 value
= TREE_VEC_ELT (valuevec
, i
);
15779 if (value
&& TYPE_P (value
))
15780 chain
= hash_tree_cons (purpose
, value
, chain
);
15782 chain
= tree_cons (purpose
, value
, chain
);
15788 /* Take the tree structure T and replace template parameters used
15789 therein with the argument vector ARGS. IN_DECL is an associated
15790 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15791 Issue error and warning messages under control of COMPLAIN. Note
15792 that we must be relatively non-tolerant of extensions here, in
15793 order to preserve conformance; if we allow substitutions that
15794 should not be allowed, we may allow argument deductions that should
15795 not succeed, and therefore report ambiguous overload situations
15796 where there are none. In theory, we could allow the substitution,
15797 but indicate that it should have failed, and allow our caller to
15798 make sure that the right thing happens, but we don't try to do this
15801 This function is used for dealing with types, decls and the like;
15802 for expressions, use tsubst_expr or tsubst_copy. */
15805 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15807 enum tree_code code
;
15808 tree type
, r
= NULL_TREE
;
15810 if (t
== NULL_TREE
|| t
== error_mark_node
15811 || t
== integer_type_node
15812 || t
== void_type_node
15813 || t
== char_type_node
15814 || t
== unknown_type_node
15815 || TREE_CODE (t
) == NAMESPACE_DECL
15816 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15820 return tsubst_decl (t
, args
, complain
);
15822 if (args
== NULL_TREE
)
15825 code
= TREE_CODE (t
);
15827 gcc_assert (code
!= IDENTIFIER_NODE
);
15828 type
= TREE_TYPE (t
);
15830 gcc_assert (type
!= unknown_type_node
);
15832 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
15835 /* Reuse typedefs. We need to do this to handle dependent attributes,
15836 such as attribute aligned. */
15838 && typedef_variant_p (t
))
15840 tree decl
= TYPE_NAME (t
);
15842 if (alias_template_specialization_p (t
, nt_opaque
))
15844 /* DECL represents an alias template and we want to
15846 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15847 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15848 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15850 else if (DECL_CLASS_SCOPE_P (decl
)
15851 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15852 && uses_template_parms (DECL_CONTEXT (decl
)))
15854 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15855 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15856 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15858 else if (DECL_FUNCTION_SCOPE_P (decl
)
15859 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15860 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15861 r
= retrieve_local_specialization (decl
);
15863 /* The typedef is from a non-template context. */
15869 r
= cp_build_qualified_type
15870 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15871 complain
| tf_ignore_bad_quals
);
15876 /* We don't have an instantiation yet, so drop the typedef. */
15877 int quals
= cp_type_quals (t
);
15878 t
= DECL_ORIGINAL_TYPE (decl
);
15879 t
= cp_build_qualified_type (t
, quals
,
15880 complain
| tf_ignore_bad_quals
);
15884 bool fndecl_type
= (complain
& tf_fndecl_type
);
15885 complain
&= ~tf_fndecl_type
;
15887 bool tst_ok
= (complain
& tf_tst_ok
);
15888 complain
&= ~tf_tst_ok
;
15891 && code
!= TYPENAME_TYPE
15892 && code
!= TEMPLATE_TYPE_PARM
15893 && code
!= TEMPLATE_PARM_INDEX
15894 && code
!= IDENTIFIER_NODE
15895 && code
!= FUNCTION_TYPE
15896 && code
!= METHOD_TYPE
)
15897 type
= tsubst (type
, args
, complain
, in_decl
);
15898 if (type
== error_mark_node
)
15899 return error_mark_node
;
15904 if (TYPE_PTRMEMFUNC_P (t
))
15905 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
15906 /* Fall through. */
15908 case ENUMERAL_TYPE
:
15909 return tsubst_aggr_type_1 (t
, args
, complain
, in_decl
,
15910 /*entering_scope=*/0);
15913 case IDENTIFIER_NODE
:
15925 if (t
== integer_type_node
)
15928 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15929 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15933 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15935 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
15937 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15939 if (TREE_CODE (max
) == NOP_EXPR
15940 && TREE_SIDE_EFFECTS (omax
)
15941 && !TREE_TYPE (max
))
15942 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15944 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15945 with TREE_SIDE_EFFECTS that indicates this is not an integral
15946 constant expression. */
15947 if (processing_template_decl
15948 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15950 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15951 TREE_SIDE_EFFECTS (max
) = 1;
15954 return compute_array_index_type (NULL_TREE
, max
, complain
);
15957 case TEMPLATE_TYPE_PARM
:
15958 if (template_placeholder_p (t
))
15960 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (t
);
15961 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
15962 if (TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
15963 tmpl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl
);
15965 if (tmpl
!= CLASS_PLACEHOLDER_TEMPLATE (t
))
15966 return make_template_placeholder (tmpl
);
15970 /* Fall through. */
15971 case TEMPLATE_TEMPLATE_PARM
:
15972 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15973 case TEMPLATE_PARM_INDEX
:
15978 tree arg
= NULL_TREE
;
15982 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15983 template_parm_level_and_index (t
, &level
, &idx
);
15985 levels
= TMPL_ARGS_DEPTH (args
);
15986 if (level
<= levels
15987 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15989 arg
= TMPL_ARG (args
, level
, idx
);
15991 /* See through ARGUMENT_PACK_SELECT arguments. */
15992 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15993 arg
= argument_pack_select_arg (arg
);
15996 if (arg
== error_mark_node
)
15997 return error_mark_node
;
15998 else if (arg
!= NULL_TREE
)
16000 if (ARGUMENT_PACK_P (arg
))
16001 /* If ARG is an argument pack, we don't actually want to
16002 perform a substitution here, because substitutions
16003 for argument packs are only done
16004 element-by-element. We can get to this point when
16005 substituting the type of a non-type template
16006 parameter pack, when that type actually contains
16007 template parameter packs from an outer template, e.g.,
16009 template<typename... Types> struct A {
16010 template<Types... Values> struct B { };
16014 if (code
== TEMPLATE_TYPE_PARM
)
16018 /* When building concept checks for the purpose of
16019 deducing placeholders, we can end up with wildcards
16020 where types are expected. Adjust this to the deduced
16022 if (TREE_CODE (arg
) == WILDCARD_DECL
)
16023 arg
= TREE_TYPE (TREE_TYPE (arg
));
16025 gcc_assert (TYPE_P (arg
));
16027 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
16029 return cp_build_qualified_type
16030 (arg
, quals
, complain
| tf_ignore_bad_quals
);
16032 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
16034 /* We are processing a type constructed from a
16035 template template parameter. */
16036 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
16037 args
, complain
, in_decl
);
16038 if (argvec
== error_mark_node
)
16039 return error_mark_node
;
16041 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
16042 || TREE_CODE (arg
) == TEMPLATE_DECL
16043 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
16045 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
16046 /* Consider this code:
16048 template <template <class> class Template>
16050 template <class Arg> using Bind = Template<Arg>;
16053 template <template <class> class Template, class Arg>
16054 using Instantiate = Template<Arg>; //#0
16056 template <template <class> class Template,
16059 Instantiate<Internal<Template>::template Bind,
16062 When #1 is parsed, the
16063 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16064 parameter `Template' in #0 matches the
16065 UNBOUND_CLASS_TEMPLATE representing the argument
16066 `Internal<Template>::template Bind'; We then want
16067 to assemble the type `Bind<Argument>' that can't
16068 be fully created right now, because
16069 `Internal<Template>' not being complete, the Bind
16070 template cannot be looked up in that context. So
16071 we need to "store" `Bind<Argument>' for later
16072 when the context of Bind becomes complete. Let's
16073 store that in a TYPENAME_TYPE. */
16074 return make_typename_type (TYPE_CONTEXT (arg
),
16075 build_nt (TEMPLATE_ID_EXPR
,
16076 TYPE_IDENTIFIER (arg
),
16081 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16082 are resolving nested-types in the signature of a
16083 member function templates. Otherwise ARG is a
16084 TEMPLATE_DECL and is the real template to be
16086 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
16087 arg
= TYPE_NAME (arg
);
16089 r
= lookup_template_class (arg
,
16091 DECL_CONTEXT (arg
),
16092 /*entering_scope=*/0,
16094 return cp_build_qualified_type
16095 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
16097 else if (code
== TEMPLATE_TEMPLATE_PARM
)
16100 /* TEMPLATE_PARM_INDEX. */
16101 return convert_from_reference (unshare_expr (arg
));
16105 /* This can happen during the attempted tsubst'ing in
16106 unify. This means that we don't yet have any information
16107 about the template parameter in question. */
16110 /* Early in template argument deduction substitution, we don't
16111 want to reduce the level of 'auto', or it will be confused
16112 with a normal template parm in subsequent deduction.
16113 Similarly, don't reduce the level of template parameters to
16114 avoid mismatches when deducing their types. */
16115 if (complain
& tf_partial
)
16118 /* If we get here, we must have been looking at a parm for a
16119 more deeply nested template. Make a new version of this
16120 template parameter, but with a lower level. */
16123 case TEMPLATE_TYPE_PARM
:
16124 case TEMPLATE_TEMPLATE_PARM
:
16125 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16126 if (cp_type_quals (t
))
16128 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
16129 r
= cp_build_qualified_type
16130 (r
, cp_type_quals (t
),
16131 complain
| (code
== TEMPLATE_TYPE_PARM
16132 ? tf_ignore_bad_quals
: 0));
16134 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
16135 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
16136 && (r
= (TEMPLATE_PARM_DESCENDANTS
16137 (TEMPLATE_TYPE_PARM_INDEX (t
))))
16138 && (r
= TREE_TYPE (r
))
16139 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
))
16140 /* Break infinite recursion when substituting the constraints
16141 of a constrained placeholder. */;
16142 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
16143 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
16144 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
16145 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
16146 && (TEMPLATE_PARM_LEVEL (r
)
16147 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
16148 /* Cache the simple case of lowering a type parameter. */
16153 TEMPLATE_TYPE_PARM_INDEX (r
)
16154 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
16155 r
, levels
, args
, complain
);
16156 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
16157 TYPE_MAIN_VARIANT (r
) = r
;
16158 TYPE_POINTER_TO (r
) = NULL_TREE
;
16159 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
16161 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
16162 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
))
16163 /* Propagate constraints on placeholders since they are
16164 only instantiated during satisfaction. */
16165 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
) = ci
;
16167 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
16169 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
16170 /* We might need to substitute into the types of non-type
16171 template parameters. */
16172 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
16173 complain
, in_decl
);
16174 if (tmpl
== error_mark_node
)
16175 return error_mark_node
;
16176 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
16177 complain
, in_decl
);
16178 if (argvec
== error_mark_node
)
16179 return error_mark_node
;
16181 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
16182 = build_template_info (tmpl
, argvec
);
16185 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
16186 SET_TYPE_STRUCTURAL_EQUALITY (r
);
16188 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
16192 case TEMPLATE_PARM_INDEX
:
16193 /* OK, now substitute the type of the non-type parameter. We
16194 couldn't do it earlier because it might be an auto parameter,
16195 and we wouldn't need to if we had an argument. */
16196 type
= tsubst (type
, args
, complain
, in_decl
);
16197 if (type
== error_mark_node
)
16198 return error_mark_node
;
16199 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
16203 gcc_unreachable ();
16210 return tsubst_tree_list (t
, args
, complain
, in_decl
);
16213 /* We should never be tsubsting a binfo. */
16214 gcc_unreachable ();
16217 /* A vector of template arguments. */
16218 gcc_assert (!type
);
16219 return tsubst_template_args (t
, args
, complain
, in_decl
);
16222 case REFERENCE_TYPE
:
16224 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
16229 Type deduction may fail for any of the following
16232 -- Attempting to create a pointer to reference type.
16233 -- Attempting to create a reference to a reference type or
16234 a reference to void.
16236 Core issue 106 says that creating a reference to a reference
16237 during instantiation is no longer a cause for failure. We
16238 only enforce this check in strict C++98 mode. */
16239 if ((TYPE_REF_P (type
)
16240 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
16241 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
16243 static location_t last_loc
;
16245 /* We keep track of the last time we issued this error
16246 message to avoid spewing a ton of messages during a
16247 single bad template instantiation. */
16248 if (complain
& tf_error
16249 && last_loc
!= input_location
)
16251 if (VOID_TYPE_P (type
))
16252 error ("forming reference to void");
16253 else if (code
== POINTER_TYPE
)
16254 error ("forming pointer to reference type %qT", type
);
16256 error ("forming reference to reference type %qT", type
);
16257 last_loc
= input_location
;
16260 return error_mark_node
;
16262 else if (TREE_CODE (type
) == FUNCTION_TYPE
16263 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
16264 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
16266 if (complain
& tf_error
)
16268 if (code
== POINTER_TYPE
)
16269 error ("forming pointer to qualified function type %qT",
16272 error ("forming reference to qualified function type %qT",
16275 return error_mark_node
;
16277 else if (code
== POINTER_TYPE
)
16279 r
= build_pointer_type (type
);
16280 if (TREE_CODE (type
) == METHOD_TYPE
)
16281 r
= build_ptrmemfunc_type (r
);
16283 else if (TYPE_REF_P (type
))
16284 /* In C++0x, during template argument substitution, when there is an
16285 attempt to create a reference to a reference type, reference
16286 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16288 "If a template-argument for a template-parameter T names a type
16289 that is a reference to a type A, an attempt to create the type
16290 'lvalue reference to cv T' creates the type 'lvalue reference to
16291 A,' while an attempt to create the type type rvalue reference to
16292 cv T' creates the type T"
16294 r
= cp_build_reference_type
16296 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
16298 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
16299 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
16301 if (r
!= error_mark_node
)
16302 /* Will this ever be needed for TYPE_..._TO values? */
16309 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
16310 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
16314 Type deduction may fail for any of the following
16317 -- Attempting to create "pointer to member of T" when T
16318 is not a class type. */
16319 if (complain
& tf_error
)
16320 error ("creating pointer to member of non-class type %qT", r
);
16321 return error_mark_node
;
16323 if (TYPE_REF_P (type
))
16325 if (complain
& tf_error
)
16326 error ("creating pointer to member reference type %qT", type
);
16327 return error_mark_node
;
16329 if (VOID_TYPE_P (type
))
16331 if (complain
& tf_error
)
16332 error ("creating pointer to member of type void");
16333 return error_mark_node
;
16335 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
16336 if (TREE_CODE (type
) == FUNCTION_TYPE
)
16338 /* The type of the implicit object parameter gets its
16339 cv-qualifiers from the FUNCTION_TYPE. */
16342 = build_memfn_type (type
, r
, type_memfn_quals (type
),
16343 type_memfn_rqual (type
));
16344 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
16345 return cp_build_qualified_type (memptr
, cp_type_quals (t
),
16349 return cp_build_qualified_type (build_ptrmem_type (r
, type
),
16353 case FUNCTION_TYPE
:
16358 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
16359 if (fntype
== error_mark_node
)
16360 return error_mark_node
;
16362 /* Substitute the exception specification. */
16363 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
16364 /*defer_ok*/fndecl_type
);
16365 if (specs
== error_mark_node
)
16366 return error_mark_node
;
16368 fntype
= build_exception_variant (fntype
, specs
);
16373 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
16374 if (domain
== error_mark_node
)
16375 return error_mark_node
;
16377 /* As an optimization, we avoid regenerating the array type if
16378 it will obviously be the same as T. */
16379 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
16382 /* These checks should match the ones in create_array_type_for_decl.
16386 The deduction may fail for any of the following reasons:
16388 -- Attempting to create an array with an element type that
16389 is void, a function type, or a reference type, or [DR337]
16390 an abstract class type. */
16391 if (VOID_TYPE_P (type
)
16392 || TREE_CODE (type
) == FUNCTION_TYPE
16393 || (TREE_CODE (type
) == ARRAY_TYPE
16394 && TYPE_DOMAIN (type
) == NULL_TREE
)
16395 || TYPE_REF_P (type
))
16397 if (complain
& tf_error
)
16398 error ("creating array of %qT", type
);
16399 return error_mark_node
;
16402 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
16403 !(complain
& tf_error
)))
16404 return error_mark_node
;
16406 r
= build_cplus_array_type (type
, domain
);
16408 if (!valid_array_size_p (input_location
, r
, in_decl
,
16409 (complain
& tf_error
)))
16410 return error_mark_node
;
16412 if (TYPE_USER_ALIGN (t
))
16414 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
16415 TYPE_USER_ALIGN (r
) = 1;
16421 case TYPENAME_TYPE
:
16423 tree ctx
= TYPE_CONTEXT (t
);
16424 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
16426 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
16427 if (ctx
== error_mark_node
16428 || TREE_VEC_LENGTH (ctx
) > 1)
16429 return error_mark_node
;
16430 if (TREE_VEC_LENGTH (ctx
) == 0)
16432 if (complain
& tf_error
)
16433 error ("%qD is instantiated for an empty pack",
16434 TYPENAME_TYPE_FULLNAME (t
));
16435 return error_mark_node
;
16437 ctx
= TREE_VEC_ELT (ctx
, 0);
16440 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
16441 /*entering_scope=*/1);
16442 if (ctx
== error_mark_node
)
16443 return error_mark_node
;
16445 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
16446 complain
, in_decl
);
16447 if (f
== error_mark_node
)
16448 return error_mark_node
;
16450 if (!MAYBE_CLASS_TYPE_P (ctx
))
16452 if (complain
& tf_error
)
16453 error ("%qT is not a class, struct, or union type", ctx
);
16454 return error_mark_node
;
16456 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
16458 /* Normally, make_typename_type does not require that the CTX
16459 have complete type in order to allow things like:
16461 template <class T> struct S { typename S<T>::X Y; };
16463 But, such constructs have already been resolved by this
16464 point, so here CTX really should have complete type, unless
16465 it's a partial instantiation. */
16466 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16467 return error_mark_node
;
16470 tsubst_flags_t tcomplain
= complain
| tf_keep_type_decl
;
16472 tcomplain
|= tf_tst_ok
;
16473 f
= make_typename_type (ctx
, f
, typename_type
, tcomplain
);
16474 if (f
== error_mark_node
)
16476 if (TREE_CODE (f
) == TYPE_DECL
)
16478 complain
|= tf_ignore_bad_quals
;
16482 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16484 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16486 if (complain
& tf_error
)
16487 error ("%qT resolves to %qT, which is not an enumeration type",
16490 return error_mark_node
;
16492 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16494 if (complain
& tf_error
)
16495 error ("%qT resolves to %qT, which is not a class type",
16498 return error_mark_node
;
16502 return cp_build_qualified_type
16503 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16506 case UNBOUND_CLASS_TEMPLATE
:
16508 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16509 in_decl
, /*entering_scope=*/1);
16510 tree name
= TYPE_IDENTIFIER (t
);
16511 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16513 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16514 return error_mark_node
;
16517 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16518 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16525 ++cp_unevaluated_operand
;
16526 ++c_inhibit_evaluation_warnings
;
16528 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
, complain
, in_decl
);
16530 --cp_unevaluated_operand
;
16531 --c_inhibit_evaluation_warnings
;
16533 type
= finish_typeof (type
);
16534 return cp_build_qualified_type (type
,
16536 | cp_type_quals (type
),
16540 case DECLTYPE_TYPE
:
16544 ++cp_unevaluated_operand
;
16545 ++c_inhibit_evaluation_warnings
;
16547 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16548 complain
|tf_decltype
, in_decl
);
16550 --cp_unevaluated_operand
;
16551 --c_inhibit_evaluation_warnings
;
16553 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16554 type
= lambda_capture_field_type (type
,
16555 false /*explicit_init*/,
16556 DECLTYPE_FOR_REF_CAPTURE (t
));
16557 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16558 type
= lambda_proxy_type (type
);
16561 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16562 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16564 /* In a template ~id could be either a complement expression
16565 or an unqualified-id naming a destructor; if instantiating
16566 it produces an expression, it's not an id-expression or
16569 type
= finish_decltype_type (type
, id
, complain
);
16571 return cp_build_qualified_type (type
,
16573 | cp_type_quals (type
),
16574 complain
| tf_ignore_bad_quals
);
16579 tree type1
= tsubst (TRAIT_TYPE_TYPE1 (t
), args
, complain
, in_decl
);
16580 tree type2
= tsubst (TRAIT_TYPE_TYPE2 (t
), args
, complain
, in_decl
);
16581 type
= finish_trait_type (TRAIT_TYPE_KIND (t
), type1
, type2
);
16582 return cp_build_qualified_type (type
,
16583 cp_type_quals (t
) | cp_type_quals (type
),
16584 complain
| tf_ignore_bad_quals
);
16587 case TYPE_ARGUMENT_PACK
:
16588 case NONTYPE_ARGUMENT_PACK
:
16589 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16604 /* We should use one of the expression tsubsts for these codes. */
16605 gcc_unreachable ();
16608 sorry ("use of %qs in template", get_tree_code_name (code
));
16609 return error_mark_node
;
16613 /* OLDFNS is a lookup set of member functions from some class template, and
16614 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
16615 of that class template. Return the subset of NEWFNS which are
16616 specializations of a function from OLDFNS. */
16619 filter_memfn_lookup (tree oldfns
, tree newfns
, tree newtype
)
16621 /* Record all member functions from the old lookup set OLDFNS into
16623 hash_set
<tree
> visible_set
;
16624 bool seen_dep_using
= false;
16625 for (tree fn
: lkp_range (oldfns
))
16627 if (TREE_CODE (fn
) == USING_DECL
)
16629 /* Imprecisely handle dependent using-decl by keeping all members
16630 in the new lookup set that are defined in a base class, i.e.
16631 members that could plausibly have been introduced by this
16632 dependent using-decl.
16633 FIXME: Track which members are introduced by a dependent
16634 using-decl precisely, perhaps by performing another lookup
16635 from the substituted USING_DECL_SCOPE. */
16636 gcc_checking_assert (DECL_DEPENDENT_P (fn
));
16637 seen_dep_using
= true;
16640 visible_set
.add (fn
);
16643 /* Returns true iff (a less specialized version of) FN appeared in
16644 the old lookup set OLDFNS. */
16645 auto visible_p
= [newtype
, seen_dep_using
, &visible_set
] (tree fn
) {
16646 if (DECL_CONTEXT (fn
) != newtype
)
16647 /* FN is a member function from a base class, introduced via a
16648 using-decl; if it might have been introduced by a dependent
16649 using-decl then just conservatively keep it, otherwise look
16650 in the old lookup set for FN exactly. */
16651 return seen_dep_using
|| visible_set
.contains (fn
);
16652 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
16653 /* FN is a member function template from the current class;
16654 look in the old lookup set for the TEMPLATE_DECL from which
16655 it was specialized. */
16656 return visible_set
.contains (DECL_TI_TEMPLATE (fn
));
16658 /* FN is a non-template member function from the current class;
16659 look in the old lookup set for the FUNCTION_DECL from which
16660 it was specialized. */
16661 return visible_set
.contains (DECL_TEMPLATE_RESULT
16662 (DECL_TI_TEMPLATE (fn
)));
16665 bool lookup_changed_p
= false;
16666 for (tree fn
: lkp_range (newfns
))
16667 if (!visible_p (fn
))
16669 lookup_changed_p
= true;
16672 if (!lookup_changed_p
)
16675 /* Filter out from NEWFNS the member functions that weren't
16676 previously visible according to OLDFNS. */
16677 tree filtered_fns
= NULL_TREE
;
16678 unsigned filtered_size
= 0;
16679 for (tree fn
: lkp_range (newfns
))
16680 if (visible_p (fn
))
16682 filtered_fns
= lookup_add (fn
, filtered_fns
);
16685 gcc_checking_assert (seen_dep_using
16686 ? filtered_size
>= visible_set
.elements ()
16687 : filtered_size
== visible_set
.elements ());
16689 return filtered_fns
;
16692 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16693 expression on the left-hand side of the "." or "->" operator. We
16694 only do the lookup if we had a dependent BASELINK. Otherwise we
16695 adjust it onto the instantiated heirarchy. */
16698 tsubst_baselink (tree baselink
, tree object_type
,
16699 tree args
, tsubst_flags_t complain
, tree in_decl
)
16701 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16702 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16703 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16705 tree optype
= BASELINK_OPTYPE (baselink
);
16706 optype
= tsubst (optype
, args
, complain
, in_decl
);
16708 tree template_args
= NULL_TREE
;
16709 bool template_id_p
= false;
16710 tree fns
= BASELINK_FUNCTIONS (baselink
);
16711 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16713 template_id_p
= true;
16714 template_args
= TREE_OPERAND (fns
, 1);
16715 fns
= TREE_OPERAND (fns
, 0);
16717 template_args
= tsubst_template_args (template_args
, args
,
16718 complain
, in_decl
);
16721 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16722 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16723 bool dependent_p
= (binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
))
16724 || optype
!= BASELINK_OPTYPE (baselink
));
16728 tree name
= OVL_NAME (fns
);
16729 if (IDENTIFIER_CONV_OP_P (name
))
16730 name
= make_conv_op_name (optype
);
16732 /* See maybe_dependent_member_ref. */
16733 if ((complain
& tf_dguide
) && dependent_scope_p (qualifying_scope
))
16736 name
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, name
,
16738 return build_qualified_name (NULL_TREE
, qualifying_scope
, name
,
16739 /* ::template */false);
16742 if (name
== complete_dtor_identifier
)
16743 /* Treat as-if non-dependent below. */
16744 dependent_p
= false;
16746 bool maybe_incomplete
= BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
);
16747 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16749 if (maybe_incomplete
)
16751 /* Filter out from the new lookup set those functions which didn't
16752 appear in the original lookup set (in a less specialized form).
16753 This is needed to preserve the consistency of member lookup
16754 performed in an incomplete-class context, within which
16755 later-declared members ought to remain invisible. */
16756 BASELINK_FUNCTIONS (baselink
)
16757 = filter_memfn_lookup (fns
, BASELINK_FUNCTIONS (baselink
),
16759 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
) = true;
16764 if ((complain
& tf_error
)
16765 && constructor_name_p (name
, qualifying_scope
))
16766 error ("cannot call constructor %<%T::%D%> directly",
16767 qualifying_scope
, name
);
16768 return error_mark_node
;
16771 fns
= BASELINK_FUNCTIONS (baselink
);
16775 /* We're going to overwrite pieces below, make a duplicate. */
16776 baselink
= copy_node (baselink
);
16778 if (qualifying_scope
!= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
)))
16780 /* The decl we found was from non-dependent scope, but we still need
16781 to update the binfos for the instantiated qualifying_scope. */
16782 BASELINK_ACCESS_BINFO (baselink
) = TYPE_BINFO (qualifying_scope
);
16783 BASELINK_BINFO (baselink
) = lookup_base (qualifying_scope
, binfo_type
,
16784 ba_unique
, nullptr, complain
);
16788 /* If lookup found a single function, mark it as used at this point.
16789 (If lookup found multiple functions the one selected later by
16790 overload resolution will be marked as used at that point.) */
16791 if (!template_id_p
&& !really_overloaded_fn (fns
))
16793 tree fn
= OVL_FIRST (fns
);
16794 bool ok
= mark_used (fn
, complain
);
16795 if (!ok
&& !(complain
& tf_error
))
16796 return error_mark_node
;
16797 if (ok
&& BASELINK_P (baselink
))
16798 /* We might have instantiated an auto function. */
16799 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16802 if (BASELINK_P (baselink
))
16804 /* Add back the template arguments, if present. */
16806 BASELINK_FUNCTIONS (baselink
)
16807 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16809 /* Update the conversion operator type. */
16810 BASELINK_OPTYPE (baselink
) = optype
;
16814 object_type
= current_class_type
;
16816 if (qualified_p
|| !dependent_p
)
16818 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16822 /* We need to call adjust_result_of_qualified_name_lookup in case the
16823 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16824 so that we still get virtual function binding. */
16825 BASELINK_QUALIFIED_P (baselink
) = false;
16831 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16832 true if the qualified-id will be a postfix-expression in-and-of
16833 itself; false if more of the postfix-expression follows the
16834 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16838 tsubst_qualified_id (tree qualified_id
, tree args
,
16839 tsubst_flags_t complain
, tree in_decl
,
16840 bool done
, bool address_p
)
16846 tree template_args
;
16847 location_t loc
= EXPR_LOCATION (qualified_id
);
16849 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16851 /* Figure out what name to look up. */
16852 name
= TREE_OPERAND (qualified_id
, 1);
16853 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16855 is_template
= true;
16856 template_args
= TREE_OPERAND (name
, 1);
16858 template_args
= tsubst_template_args (template_args
, args
,
16859 complain
, in_decl
);
16860 if (template_args
== error_mark_node
)
16861 return error_mark_node
;
16862 name
= TREE_OPERAND (name
, 0);
16866 is_template
= false;
16867 template_args
= NULL_TREE
;
16870 /* Substitute into the qualifying scope. When there are no ARGS, we
16871 are just trying to simplify a non-dependent expression. In that
16872 case the qualifying scope may be dependent, and, in any case,
16873 substituting will not help. */
16874 scope
= TREE_OPERAND (qualified_id
, 0);
16877 scope
= tsubst (scope
, args
, complain
, in_decl
);
16878 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16883 if (dependent_scope_p (scope
))
16885 if (TREE_CODE (expr
) == SCOPE_REF
)
16886 /* We built one in tsubst_baselink. */
16887 gcc_checking_assert (same_type_p (scope
, TREE_OPERAND (expr
, 0)));
16891 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
,
16893 expr
= build_qualified_name (NULL_TREE
, scope
, expr
,
16894 QUALIFIED_NAME_IS_TEMPLATE
16897 REF_PARENTHESIZED_P (expr
) = REF_PARENTHESIZED_P (qualified_id
);
16901 if (!BASELINK_P (name
) && !DECL_P (expr
))
16903 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16905 /* A BIT_NOT_EXPR is used to represent a destructor. */
16906 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16908 error ("qualifying type %qT does not match destructor name ~%qT",
16909 scope
, TREE_OPERAND (expr
, 0));
16910 expr
= error_mark_node
;
16913 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16914 LOOK_want::NORMAL
, false);
16917 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16918 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16919 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16921 if (complain
& tf_error
)
16923 error ("dependent-name %qE is parsed as a non-type, but "
16924 "instantiation yields a type", qualified_id
);
16925 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16927 return error_mark_node
;
16933 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16935 return error_mark_node
;
16936 /* Remember that there was a reference to this entity. */
16937 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16938 return error_mark_node
;
16941 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16943 if (complain
& tf_error
)
16944 qualified_name_lookup_error (scope
,
16945 TREE_OPERAND (qualified_id
, 1),
16946 expr
, input_location
);
16947 return error_mark_node
;
16952 /* We may be repeating a check already done during parsing, but
16953 if it was well-formed and passed then, it will pass again
16954 now, and if it didn't, we wouldn't have got here. The case
16955 we want to catch is when we couldn't tell then, and can now,
16956 namely when templ prior to substitution was an
16958 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16959 return error_mark_node
;
16961 if (variable_template_p (expr
))
16962 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16965 expr
= lookup_template_function (expr
, template_args
);
16968 if (expr
== error_mark_node
&& complain
& tf_error
)
16969 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16970 expr
, input_location
);
16971 else if (TYPE_P (scope
))
16973 expr
= (adjust_result_of_qualified_name_lookup
16974 (expr
, scope
, current_nonlambda_class_type ()));
16975 expr
= (finish_qualified_id_expr
16976 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16977 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16978 /*template_arg_p=*/false, complain
));
16981 /* Expressions do not generally have reference type. */
16982 if (TREE_CODE (expr
) != SCOPE_REF
16983 /* However, if we're about to form a pointer-to-member, we just
16984 want the referenced member referenced. */
16985 && TREE_CODE (expr
) != OFFSET_REF
)
16986 expr
= convert_from_reference (expr
);
16988 if (REF_PARENTHESIZED_P (qualified_id
))
16989 expr
= force_paren_expr (expr
);
16991 expr
= maybe_wrap_with_location (expr
, loc
);
16996 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16997 initializer, DECL is the substituted VAR_DECL. Other arguments are as
17001 tsubst_init (tree init
, tree decl
, tree args
,
17002 tsubst_flags_t complain
, tree in_decl
)
17007 init
= tsubst_expr (init
, args
, complain
, in_decl
);
17009 tree type
= TREE_TYPE (decl
);
17011 if (!init
&& type
!= error_mark_node
)
17013 if (tree auto_node
= type_uses_auto (type
))
17015 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
17017 if (complain
& tf_error
)
17018 error ("initializer for %q#D expands to an empty list "
17019 "of expressions", decl
);
17020 return error_mark_node
;
17023 else if (!dependent_type_p (type
))
17025 /* If we had an initializer but it
17026 instantiated to nothing,
17027 value-initialize the object. This will
17028 only occur when the initializer was a
17029 pack expansion where the parameter packs
17030 used in that expansion were of length
17032 init
= build_value_init (type
, complain
);
17033 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
17034 init
= get_target_expr (init
, complain
);
17035 if (TREE_CODE (init
) == TARGET_EXPR
)
17036 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
17043 /* If T is a reference to a dependent member of the current instantiation C and
17044 we are trying to refer to that member in a partial instantiation of C,
17045 return a SCOPE_REF; otherwise, return NULL_TREE.
17047 This can happen when forming a C++17 deduction guide, as in PR96199. */
17050 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
17053 if (!(complain
& tf_dguide
))
17056 tree decl
= (t
&& TYPE_P (t
)) ? TYPE_NAME (t
) : t
;
17057 if (!decl
|| !DECL_P (decl
))
17060 tree ctx
= context_for_name_lookup (decl
);
17061 if (!CLASS_TYPE_P (ctx
))
17064 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
17065 if (!dependent_scope_p (ctx
))
17070 if (typedef_variant_p (t
))
17071 t
= strip_typedefs (t
);
17072 tree decl
= TYPE_NAME (t
);
17074 decl
= maybe_dependent_member_ref (decl
, args
, complain
, in_decl
);
17077 return cp_build_qualified_type (TREE_TYPE (decl
), cp_type_quals (t
),
17081 tree name
= DECL_NAME (t
);
17082 tree fullname
= name
;
17083 if (instantiates_primary_template_p (t
))
17085 tree tinfo
= get_template_info (t
);
17086 name
= DECL_NAME (TI_TEMPLATE (tinfo
));
17087 tree targs
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
17088 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17089 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
, targs
);
17092 if (TREE_CODE (t
) == TYPE_DECL
)
17094 if (TREE_CODE (TREE_TYPE (t
)) == TYPENAME_TYPE
17095 && TYPE_NAME (TREE_TYPE (t
)) == t
)
17096 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17097 scope, but it doesn't need to be rewritten again. */
17099 tree type
= build_typename_type (ctx
, name
, fullname
, typename_type
);
17100 return TYPE_NAME (type
);
17102 else if (DECL_TYPE_TEMPLATE_P (t
))
17103 return make_unbound_class_template (ctx
, name
,
17104 NULL_TREE
, complain
);
17106 return build_qualified_name (NULL_TREE
, ctx
, fullname
,
17107 TREE_CODE (t
) == TEMPLATE_DECL
);
17110 /* Like tsubst, but deals with expressions. This function just replaces
17111 template parms; to finish processing the resultant expression, use
17112 tsubst_copy_and_build or tsubst_expr. */
17115 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17117 enum tree_code code
;
17120 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
17123 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
17126 code
= TREE_CODE (t
);
17131 r
= retrieve_local_specialization (t
);
17133 if (r
== NULL_TREE
)
17135 /* We get here for a use of 'this' in an NSDMI. */
17136 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
17137 return current_class_ptr
;
17139 /* This can happen for a parameter name used later in a function
17140 declaration (such as in a late-specified return type). Just
17141 make a dummy decl, since it's only used for its type. */
17142 gcc_assert (cp_unevaluated_operand
);
17143 r
= tsubst_decl (t
, args
, complain
);
17144 /* Give it the template pattern as its context; its true context
17145 hasn't been instantiated yet and this is good enough for
17147 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
17150 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
17151 r
= argument_pack_select_arg (r
);
17152 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
17153 return error_mark_node
;
17161 if (DECL_TEMPLATE_PARM_P (t
))
17162 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
17163 if (!uses_template_parms (DECL_CONTEXT (t
)))
17166 /* Unfortunately, we cannot just call lookup_name here.
17169 template <int I> int f() {
17171 struct S { void g() { E e = a; } };
17174 When we instantiate f<7>::S::g(), say, lookup_name is not
17175 clever enough to find f<7>::a. */
17177 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
17178 /*entering_scope=*/0);
17180 for (v
= TYPE_VALUES (enum_type
);
17182 v
= TREE_CHAIN (v
))
17183 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
17184 return TREE_VALUE (v
);
17186 /* We didn't find the name. That should never happen; if
17187 name-lookup found it during preliminary parsing, we
17188 should find it again here during instantiation. */
17189 gcc_unreachable ();
17194 if (DECL_CONTEXT (t
))
17198 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
17199 /*entering_scope=*/1);
17200 if (ctx
!= DECL_CONTEXT (t
))
17202 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
17205 if (complain
& tf_error
)
17206 error ("using invalid field %qD", t
);
17207 return error_mark_node
;
17216 case FUNCTION_DECL
:
17217 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
17218 r
= tsubst (t
, args
, complain
, in_decl
);
17219 else if (DECL_LOCAL_DECL_P (t
))
17221 /* Local specialization will usually have been created when
17222 we instantiated the DECL_EXPR_DECL. */
17223 r
= retrieve_local_specialization (t
);
17226 /* We're in a generic lambda referencing a local extern
17227 from an outer block-scope of a non-template. */
17228 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl
));
17232 else if (local_variable_p (t
)
17233 && uses_template_parms (DECL_CONTEXT (t
)))
17235 r
= retrieve_local_specialization (t
);
17236 if (r
== NULL_TREE
)
17238 /* First try name lookup to find the instantiation. */
17239 r
= lookup_name (DECL_NAME (t
));
17244 /* During error-recovery we may find a non-variable,
17245 even an OVERLOAD: just bail out and avoid ICEs and
17246 duplicate diagnostics (c++/62207). */
17247 gcc_assert (seen_error ());
17248 return error_mark_node
;
17250 if (!is_capture_proxy (r
))
17252 /* Make sure the one we found is the one we want. */
17253 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
17254 if (ctx
!= DECL_CONTEXT (r
))
17263 /* This can happen for a variable used in a
17264 late-specified return type of a local lambda, or for a
17265 local static or constant. Building a new VAR_DECL
17266 should be OK in all those cases. */
17267 r
= tsubst_decl (t
, args
, complain
);
17268 if (local_specializations
)
17269 /* Avoid infinite recursion (79640). */
17270 register_local_specialization (r
, t
);
17271 if (decl_maybe_constant_var_p (r
))
17273 /* We can't call cp_finish_decl, so handle the
17274 initializer by hand. */
17275 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
17276 complain
, in_decl
);
17277 if (!processing_template_decl
)
17278 init
= maybe_constant_init (init
);
17279 if (processing_template_decl
17280 ? potential_constant_expression (init
)
17281 : reduced_constant_expression_p (init
))
17282 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
17283 = TREE_CONSTANT (r
) = true;
17284 DECL_INITIAL (r
) = init
;
17285 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
17287 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
17288 complain
, adc_variable_type
);
17290 gcc_assert (cp_unevaluated_operand
17291 || processing_contract_condition
17293 || decl_constant_var_p (r
)
17295 if (!processing_template_decl
17296 && !TREE_STATIC (r
))
17297 r
= process_outer_var_ref (r
, complain
);
17299 /* Remember this for subsequent uses. */
17300 if (local_specializations
)
17301 register_local_specialization (r
, t
);
17303 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
17304 r
= argument_pack_select_arg (r
);
17308 if (!mark_used (r
, complain
))
17309 return error_mark_node
;
17312 case NAMESPACE_DECL
:
17319 return tsubst_baselink (t
, current_nonlambda_class_type (),
17320 args
, complain
, in_decl
);
17322 case TEMPLATE_DECL
:
17323 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
17324 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
17325 args
, complain
, in_decl
);
17326 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
17327 return tsubst (t
, args
, complain
, in_decl
);
17328 else if (DECL_CLASS_SCOPE_P (t
)
17329 && uses_template_parms (DECL_CONTEXT (t
)))
17331 /* Template template argument like the following example need
17334 template <template <class> class TT> struct C {};
17335 template <class T> struct D {
17336 template <class U> struct E {};
17341 We are processing the template argument `E' in #1 for
17342 the template instantiation #2. Originally, `E' is a
17343 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
17344 have to substitute this with one having context `D<int>'. */
17346 tree context
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
,
17347 in_decl
, /*entering_scope=*/true);
17348 return lookup_field (context
, DECL_NAME(t
), 0, false);
17351 /* Ordinary template template argument. */
17354 case NON_LVALUE_EXPR
:
17355 case VIEW_CONVERT_EXPR
:
17357 /* Handle location wrappers by substituting the wrapped node
17358 first, *then* reusing the resulting type. Doing the type
17359 first ensures that we handle template parameters and
17360 parameter pack expansions. */
17361 if (location_wrapper_p (t
))
17363 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
17364 complain
, in_decl
);
17365 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
17367 tree op
= TREE_OPERAND (t
, 0);
17368 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
17369 if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
17371 op
= tsubst_copy (op
, args
, complain
, in_decl
);
17372 op
= build1 (code
, TREE_TYPE (op
), op
);
17373 REF_PARENTHESIZED_P (op
) = true;
17376 /* We shouldn't see any other uses of these in templates
17377 (tsubst_copy_and_build handles C++20 tparm object wrappers). */
17378 gcc_unreachable ();
17382 case REINTERPRET_CAST_EXPR
:
17383 case CONST_CAST_EXPR
:
17384 case STATIC_CAST_EXPR
:
17385 case DYNAMIC_CAST_EXPR
:
17386 case IMPLICIT_CONV_EXPR
:
17389 tsubst_flags_t tcomplain
= complain
;
17390 if (code
== CAST_EXPR
)
17391 tcomplain
|= tf_tst_ok
;
17392 tree type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
17393 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17394 return build1 (code
, type
, op0
);
17397 case BIT_CAST_EXPR
:
17399 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17400 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17401 r
= build_min (BIT_CAST_EXPR
, type
, op0
);
17402 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17407 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17408 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17410 tree expanded
, op
= TREE_OPERAND (t
, 0);
17413 if (SIZEOF_EXPR_TYPE_P (t
))
17414 op
= TREE_TYPE (op
);
17416 ++cp_unevaluated_operand
;
17417 ++c_inhibit_evaluation_warnings
;
17418 /* We only want to compute the number of arguments. */
17419 if (PACK_EXPANSION_P (op
))
17420 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
17422 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
17423 args
, complain
, in_decl
);
17424 --cp_unevaluated_operand
;
17425 --c_inhibit_evaluation_warnings
;
17427 if (TREE_CODE (expanded
) == TREE_VEC
)
17429 len
= TREE_VEC_LENGTH (expanded
);
17430 /* Set TREE_USED for the benefit of -Wunused. */
17431 for (int i
= 0; i
< len
; i
++)
17432 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
17433 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
17436 if (expanded
== error_mark_node
)
17437 return error_mark_node
;
17438 else if (PACK_EXPANSION_P (expanded
)
17439 || (TREE_CODE (expanded
) == TREE_VEC
17440 && pack_expansion_args_count (expanded
)))
17443 if (PACK_EXPANSION_P (expanded
))
17445 else if (TREE_VEC_LENGTH (expanded
) == 1)
17446 expanded
= TREE_VEC_ELT (expanded
, 0);
17448 expanded
= make_argument_pack (expanded
);
17450 if (TYPE_P (expanded
))
17451 return cxx_sizeof_or_alignof_type (input_location
,
17452 expanded
, SIZEOF_EXPR
,
17454 complain
& tf_error
);
17456 return cxx_sizeof_or_alignof_expr (input_location
,
17457 expanded
, SIZEOF_EXPR
,
17459 complain
& tf_error
);
17462 return build_int_cst (size_type_node
, len
);
17464 if (SIZEOF_EXPR_TYPE_P (t
))
17466 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
17467 args
, complain
, in_decl
);
17468 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
17469 r
= build1 (SIZEOF_EXPR
,
17470 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
17471 SIZEOF_EXPR_TYPE_P (r
) = 1;
17478 case TRUTH_NOT_EXPR
:
17481 case UNARY_PLUS_EXPR
: /* Unary + */
17483 case AT_ENCODE_EXPR
:
17487 case REALPART_EXPR
:
17488 case IMAGPART_EXPR
:
17491 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17492 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17493 r
= build1_loc (EXPR_LOCATION (t
), code
, type
, op0
);
17494 if (code
== ALIGNOF_EXPR
)
17495 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
17496 /* For addresses of immediate functions ensure we have EXPR_LOCATION
17497 set for possible later diagnostics. */
17498 if (code
== ADDR_EXPR
17499 && EXPR_LOCATION (r
) == UNKNOWN_LOCATION
17500 && TREE_CODE (op0
) == FUNCTION_DECL
17501 && DECL_IMMEDIATE_FUNCTION_P (op0
))
17502 SET_EXPR_LOCATION (r
, input_location
);
17506 case EXCESS_PRECISION_EXPR
:
17508 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17509 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17510 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
17512 gcc_checking_assert (same_type_p (type
, TREE_TYPE (op0
)));
17515 return build1_loc (EXPR_LOCATION (t
), code
, type
, op0
);
17518 case COMPONENT_REF
:
17523 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17524 name
= TREE_OPERAND (t
, 1);
17525 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
17527 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17528 complain
, in_decl
);
17529 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17531 else if (TREE_CODE (name
) == SCOPE_REF
17532 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
17534 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17535 complain
, in_decl
);
17536 name
= TREE_OPERAND (name
, 1);
17537 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17538 complain
, in_decl
);
17539 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17540 name
= build_qualified_name (/*type=*/NULL_TREE
,
17542 /*template_p=*/false);
17544 else if (BASELINK_P (name
))
17545 name
= tsubst_baselink (name
,
17546 non_reference (TREE_TYPE (object
)),
17550 name
= tsubst_copy (name
, args
, complain
, in_decl
);
17551 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
17557 case TRUNC_DIV_EXPR
:
17558 case CEIL_DIV_EXPR
:
17559 case FLOOR_DIV_EXPR
:
17560 case ROUND_DIV_EXPR
:
17561 case EXACT_DIV_EXPR
:
17565 case TRUNC_MOD_EXPR
:
17566 case FLOOR_MOD_EXPR
:
17567 case TRUTH_ANDIF_EXPR
:
17568 case TRUTH_ORIF_EXPR
:
17569 case TRUTH_AND_EXPR
:
17570 case TRUTH_OR_EXPR
:
17581 case COMPOUND_EXPR
:
17584 case PREDECREMENT_EXPR
:
17585 case PREINCREMENT_EXPR
:
17586 case POSTDECREMENT_EXPR
:
17587 case POSTINCREMENT_EXPR
:
17589 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17590 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17591 return build_nt (code
, op0
, op1
);
17596 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17597 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17598 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
17599 QUALIFIED_NAME_IS_TEMPLATE (t
));
17604 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17605 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17606 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
17611 int n
= VL_EXP_OPERAND_LENGTH (t
);
17612 tree result
= build_vl_exp (CALL_EXPR
, n
);
17614 for (i
= 0; i
< n
; i
++)
17615 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
17616 complain
, in_decl
);
17622 case PSEUDO_DTOR_EXPR
:
17623 case VEC_PERM_EXPR
:
17625 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17626 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17627 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17628 r
= build_nt (code
, op0
, op1
, op2
);
17629 copy_warning (r
, t
);
17635 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17636 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17637 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17638 r
= build_nt (code
, op0
, op1
, op2
);
17639 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
17645 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17646 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17647 r
= build_nt (code
, op0
, op1
);
17648 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
17649 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
17653 case TEMPLATE_ID_EXPR
:
17655 /* Substituted template arguments */
17656 tree tmpl
= TREE_OPERAND (t
, 0);
17657 tree targs
= TREE_OPERAND (t
, 1);
17659 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
17661 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17663 if (variable_template_p (tmpl
))
17664 return lookup_template_variable (tmpl
, targs
);
17666 return lookup_template_function (tmpl
, targs
);
17671 tree purpose
, value
, chain
;
17673 if (t
== void_list_node
)
17676 purpose
= TREE_PURPOSE (t
);
17678 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
17679 value
= TREE_VALUE (t
);
17681 value
= tsubst_copy (value
, args
, complain
, in_decl
);
17682 chain
= TREE_CHAIN (t
);
17683 if (chain
&& chain
!= void_type_node
)
17684 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
17685 if (purpose
== TREE_PURPOSE (t
)
17686 && value
== TREE_VALUE (t
)
17687 && chain
== TREE_CHAIN (t
))
17689 return tree_cons (purpose
, value
, chain
);
17694 case ENUMERAL_TYPE
:
17696 case TEMPLATE_TYPE_PARM
:
17697 case TEMPLATE_TEMPLATE_PARM
:
17698 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17699 case TEMPLATE_PARM_INDEX
:
17701 case REFERENCE_TYPE
:
17703 case FUNCTION_TYPE
:
17706 case TYPENAME_TYPE
:
17707 case UNBOUND_CLASS_TEMPLATE
:
17709 case DECLTYPE_TYPE
:
17711 return tsubst (t
, args
, complain
, in_decl
);
17715 /* Fall through. */
17716 case IDENTIFIER_NODE
:
17717 if (IDENTIFIER_CONV_OP_P (t
))
17719 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17720 return make_conv_op_name (new_type
);
17726 /* This is handled by tsubst_copy_and_build. */
17727 gcc_unreachable ();
17731 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17732 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17733 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17736 case CLEANUP_POINT_EXPR
:
17737 /* We shouldn't have built any of these during initial template
17738 generation. Instead, they should be built during instantiation
17739 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17740 gcc_unreachable ();
17744 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17745 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17746 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17747 r
= build2 (code
, type
, op0
, op1
);
17748 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17749 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17750 && !(complain
& tf_error
))
17751 return error_mark_node
;
17755 case EXPR_PACK_EXPANSION
:
17756 error ("invalid use of pack expansion expression");
17757 return error_mark_node
;
17759 case NONTYPE_ARGUMENT_PACK
:
17760 error ("use %<...%> to expand argument pack");
17761 return error_mark_node
;
17764 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17772 /* Instantiate any typedefs in the type. */
17773 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17774 r
= fold_convert (type
, t
);
17775 gcc_assert (TREE_CODE (r
) == code
);
17781 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17783 if (type
!= TREE_TYPE (t
))
17786 TREE_TYPE (r
) = type
;
17792 /* These can sometimes show up in a partial instantiation, but never
17793 involve template parms. */
17794 gcc_assert (!uses_template_parms (t
));
17797 case UNARY_LEFT_FOLD_EXPR
:
17798 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17799 case UNARY_RIGHT_FOLD_EXPR
:
17800 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17801 case BINARY_LEFT_FOLD_EXPR
:
17802 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17803 case BINARY_RIGHT_FOLD_EXPR
:
17804 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17808 case DEBUG_BEGIN_STMT
:
17809 /* ??? There's no point in copying it for now, but maybe some
17810 day it will contain more information, such as a pointer back
17811 to the containing function, inlined copy or so. */
17814 case CO_AWAIT_EXPR
:
17815 return tsubst_expr (t
, args
, complain
, in_decl
);
17818 /* We shouldn't get here, but keep going if !flag_checking. */
17820 gcc_unreachable ();
17825 /* Helper function for tsubst_omp_clauses, used for instantiation of
17826 OMP_CLAUSE_DECL of clauses. */
17829 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17830 tree in_decl
, tree
*iterator_cache
)
17832 if (decl
== NULL_TREE
|| decl
== ridpointers
[RID_OMP_ALL_MEMORY
])
17835 /* Handle OpenMP iterators. */
17836 if (TREE_CODE (decl
) == TREE_LIST
17837 && TREE_PURPOSE (decl
)
17838 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17841 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17842 ret
= iterator_cache
[1];
17846 begin_scope (sk_omp
, NULL
);
17847 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17849 *tp
= copy_node (it
);
17850 TREE_VEC_ELT (*tp
, 0)
17851 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17852 DECL_CONTEXT (TREE_VEC_ELT (*tp
, 0)) = current_function_decl
;
17853 pushdecl (TREE_VEC_ELT (*tp
, 0));
17854 TREE_VEC_ELT (*tp
, 1)
17855 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
);
17856 TREE_VEC_ELT (*tp
, 2)
17857 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
);
17858 TREE_VEC_ELT (*tp
, 3)
17859 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
);
17860 TREE_CHAIN (*tp
) = NULL_TREE
;
17861 tp
= &TREE_CHAIN (*tp
);
17863 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17864 iterator_cache
[0] = TREE_PURPOSE (decl
);
17865 iterator_cache
[1] = ret
;
17867 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17872 /* Handle an OpenMP array section represented as a TREE_LIST (or
17873 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17874 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17875 TREE_LIST. We can handle it exactly the same as an array section
17876 (purpose, value, and a chain), even though the nomenclature
17877 (low_bound, length, etc) is different. */
17878 if (TREE_CODE (decl
) == TREE_LIST
)
17881 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
);
17882 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
);
17883 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17885 if (TREE_PURPOSE (decl
) == low_bound
17886 && TREE_VALUE (decl
) == length
17887 && TREE_CHAIN (decl
) == chain
)
17889 tree ret
= tree_cons (low_bound
, length
, chain
);
17890 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret
)
17891 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl
);
17894 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
);
17895 /* Undo convert_from_reference tsubst_expr could have called. */
17897 && REFERENCE_REF_P (ret
)
17898 && !REFERENCE_REF_P (decl
))
17899 ret
= TREE_OPERAND (ret
, 0);
17903 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17906 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17907 tree args
, tsubst_flags_t complain
, tree in_decl
)
17909 tree new_clauses
= NULL_TREE
, nc
, oc
;
17910 tree linear_no_step
= NULL_TREE
;
17911 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17913 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17915 nc
= copy_node (oc
);
17916 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17919 switch (OMP_CLAUSE_CODE (nc
))
17921 case OMP_CLAUSE_LASTPRIVATE
:
17922 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17924 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17925 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
,
17926 complain
, in_decl
);
17927 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17928 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17931 case OMP_CLAUSE_PRIVATE
:
17932 case OMP_CLAUSE_SHARED
:
17933 case OMP_CLAUSE_FIRSTPRIVATE
:
17934 case OMP_CLAUSE_COPYIN
:
17935 case OMP_CLAUSE_COPYPRIVATE
:
17936 case OMP_CLAUSE_UNIFORM
:
17937 case OMP_CLAUSE_DEPEND
:
17938 case OMP_CLAUSE_DOACROSS
:
17939 case OMP_CLAUSE_AFFINITY
:
17940 case OMP_CLAUSE_FROM
:
17941 case OMP_CLAUSE_TO
:
17942 case OMP_CLAUSE_MAP
:
17943 case OMP_CLAUSE__CACHE_
:
17944 case OMP_CLAUSE_NONTEMPORAL
:
17945 case OMP_CLAUSE_USE_DEVICE_PTR
:
17946 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17947 case OMP_CLAUSE_IS_DEVICE_PTR
:
17948 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17949 case OMP_CLAUSE_INCLUSIVE
:
17950 case OMP_CLAUSE_EXCLUSIVE
:
17951 OMP_CLAUSE_DECL (nc
)
17952 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17953 in_decl
, iterator_cache
);
17955 case OMP_CLAUSE_NUM_TEAMS
:
17956 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
))
17957 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc
)
17958 = tsubst_expr (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
), args
,
17959 complain
, in_decl
);
17961 case OMP_CLAUSE_TILE
:
17962 case OMP_CLAUSE_IF
:
17963 case OMP_CLAUSE_NUM_THREADS
:
17964 case OMP_CLAUSE_SCHEDULE
:
17965 case OMP_CLAUSE_COLLAPSE
:
17966 case OMP_CLAUSE_FINAL
:
17967 case OMP_CLAUSE_DEVICE
:
17968 case OMP_CLAUSE_DIST_SCHEDULE
:
17969 case OMP_CLAUSE_THREAD_LIMIT
:
17970 case OMP_CLAUSE_SAFELEN
:
17971 case OMP_CLAUSE_SIMDLEN
:
17972 case OMP_CLAUSE_NUM_TASKS
:
17973 case OMP_CLAUSE_GRAINSIZE
:
17974 case OMP_CLAUSE_PRIORITY
:
17975 case OMP_CLAUSE_ORDERED
:
17976 case OMP_CLAUSE_HINT
:
17977 case OMP_CLAUSE_FILTER
:
17978 case OMP_CLAUSE_NUM_GANGS
:
17979 case OMP_CLAUSE_NUM_WORKERS
:
17980 case OMP_CLAUSE_VECTOR_LENGTH
:
17981 case OMP_CLAUSE_WORKER
:
17982 case OMP_CLAUSE_VECTOR
:
17983 case OMP_CLAUSE_ASYNC
:
17984 case OMP_CLAUSE_WAIT
:
17985 case OMP_CLAUSE_DETACH
:
17986 OMP_CLAUSE_OPERAND (nc
, 0)
17987 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
, in_decl
);
17989 case OMP_CLAUSE_REDUCTION
:
17990 case OMP_CLAUSE_IN_REDUCTION
:
17991 case OMP_CLAUSE_TASK_REDUCTION
:
17992 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17994 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17995 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17997 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17998 complain
, in_decl
);
17999 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
18000 = build_qualified_name (NULL_TREE
, scope
,
18001 TREE_OPERAND (placeholder
, 1),
18005 gcc_assert (identifier_p (placeholder
));
18007 OMP_CLAUSE_DECL (nc
)
18008 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
18011 case OMP_CLAUSE_GANG
:
18012 case OMP_CLAUSE_ALIGNED
:
18013 OMP_CLAUSE_DECL (nc
)
18014 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
18016 OMP_CLAUSE_OPERAND (nc
, 1)
18017 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
, in_decl
);
18019 case OMP_CLAUSE_ALLOCATE
:
18020 OMP_CLAUSE_DECL (nc
)
18021 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
18023 OMP_CLAUSE_OPERAND (nc
, 1)
18024 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
, in_decl
);
18025 OMP_CLAUSE_OPERAND (nc
, 2)
18026 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 2), args
, complain
, in_decl
);
18028 case OMP_CLAUSE_LINEAR
:
18029 OMP_CLAUSE_DECL (nc
)
18030 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
18032 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
18034 gcc_assert (!linear_no_step
);
18035 linear_no_step
= nc
;
18037 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
18038 OMP_CLAUSE_LINEAR_STEP (nc
)
18039 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
18040 complain
, in_decl
, NULL
);
18042 OMP_CLAUSE_LINEAR_STEP (nc
)
18043 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
18044 complain
, in_decl
);
18046 case OMP_CLAUSE_NOWAIT
:
18047 case OMP_CLAUSE_DEFAULT
:
18048 case OMP_CLAUSE_UNTIED
:
18049 case OMP_CLAUSE_MERGEABLE
:
18050 case OMP_CLAUSE_INBRANCH
:
18051 case OMP_CLAUSE_NOTINBRANCH
:
18052 case OMP_CLAUSE_PROC_BIND
:
18053 case OMP_CLAUSE_FOR
:
18054 case OMP_CLAUSE_PARALLEL
:
18055 case OMP_CLAUSE_SECTIONS
:
18056 case OMP_CLAUSE_TASKGROUP
:
18057 case OMP_CLAUSE_NOGROUP
:
18058 case OMP_CLAUSE_THREADS
:
18059 case OMP_CLAUSE_SIMD
:
18060 case OMP_CLAUSE_DEFAULTMAP
:
18061 case OMP_CLAUSE_ORDER
:
18062 case OMP_CLAUSE_BIND
:
18063 case OMP_CLAUSE_INDEPENDENT
:
18064 case OMP_CLAUSE_AUTO
:
18065 case OMP_CLAUSE_SEQ
:
18066 case OMP_CLAUSE_IF_PRESENT
:
18067 case OMP_CLAUSE_FINALIZE
:
18068 case OMP_CLAUSE_NOHOST
:
18071 gcc_unreachable ();
18073 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
18074 switch (OMP_CLAUSE_CODE (nc
))
18076 case OMP_CLAUSE_SHARED
:
18077 case OMP_CLAUSE_PRIVATE
:
18078 case OMP_CLAUSE_FIRSTPRIVATE
:
18079 case OMP_CLAUSE_LASTPRIVATE
:
18080 case OMP_CLAUSE_COPYPRIVATE
:
18081 case OMP_CLAUSE_LINEAR
:
18082 case OMP_CLAUSE_REDUCTION
:
18083 case OMP_CLAUSE_IN_REDUCTION
:
18084 case OMP_CLAUSE_TASK_REDUCTION
:
18085 case OMP_CLAUSE_USE_DEVICE_PTR
:
18086 case OMP_CLAUSE_USE_DEVICE_ADDR
:
18087 case OMP_CLAUSE_IS_DEVICE_PTR
:
18088 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
18089 case OMP_CLAUSE_INCLUSIVE
:
18090 case OMP_CLAUSE_EXCLUSIVE
:
18091 case OMP_CLAUSE_ALLOCATE
:
18092 /* tsubst_expr on SCOPE_REF results in returning
18093 finish_non_static_data_member result. Undo that here. */
18094 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
18095 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
18096 == IDENTIFIER_NODE
))
18098 tree t
= OMP_CLAUSE_DECL (nc
);
18101 switch (TREE_CODE (v
))
18103 case COMPONENT_REF
:
18107 case POINTER_PLUS_EXPR
:
18108 v
= TREE_OPERAND (v
, 0);
18111 if (DECL_CONTEXT (v
) == current_function_decl
18112 && DECL_ARTIFICIAL (v
)
18113 && DECL_NAME (v
) == this_identifier
)
18114 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
18121 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
18122 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
18123 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
18124 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
18125 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
18127 tree decl
= OMP_CLAUSE_DECL (nc
);
18130 retrofit_lang_decl (decl
);
18131 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
18140 new_clauses
= nreverse (new_clauses
);
18141 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
18143 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
18144 if (linear_no_step
)
18145 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
18146 if (nc
== linear_no_step
)
18148 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
18152 return new_clauses
;
18155 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
18158 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
18161 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
18163 tree purpose
, value
, chain
;
18168 if (TREE_CODE (t
) != TREE_LIST
)
18169 return tsubst_copy_and_build (t
, args
, complain
, in_decl
);
18171 if (t
== void_list_node
)
18174 purpose
= TREE_PURPOSE (t
);
18176 purpose
= RECUR (purpose
);
18177 value
= TREE_VALUE (t
);
18180 if (TREE_CODE (value
) != LABEL_DECL
)
18181 value
= RECUR (value
);
18184 value
= lookup_label (DECL_NAME (value
));
18185 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
18186 TREE_USED (value
) = 1;
18189 chain
= TREE_CHAIN (t
);
18190 if (chain
&& chain
!= void_type_node
)
18191 chain
= RECUR (chain
);
18192 return tree_cons (purpose
, value
, chain
);
18196 /* Used to temporarily communicate the list of #pragma omp parallel
18197 clauses to #pragma omp for instantiation if they are combined
18200 static tree
*omp_parallel_combined_clauses
;
18202 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
18203 tree
*, unsigned int *);
18205 /* Substitute one OMP_FOR iterator. */
18208 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
18209 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
18210 tree args
, tsubst_flags_t complain
, tree in_decl
)
18212 #define RECUR(NODE) \
18213 tsubst_expr ((NODE), args, complain, in_decl)
18214 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
18217 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
18218 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
18220 decl
= TREE_OPERAND (init
, 0);
18221 init
= TREE_OPERAND (init
, 1);
18222 tree decl_expr
= NULL_TREE
;
18223 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
18226 bool decomp
= false;
18227 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
18229 tree v
= DECL_VALUE_EXPR (decl
);
18230 if (TREE_CODE (v
) == ARRAY_REF
18231 && VAR_P (TREE_OPERAND (v
, 0))
18232 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
18234 tree decomp_first
= NULL_TREE
;
18235 unsigned decomp_cnt
= 0;
18236 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
18237 maybe_push_decl (d
);
18238 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
18239 in_decl
, &decomp_first
, &decomp_cnt
);
18241 if (d
== error_mark_node
)
18242 decl
= error_mark_node
;
18244 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
18246 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
18248 tree v
= build_nt (ARRAY_REF
, d
,
18249 size_int (decomp_cnt
- i
- 1),
18250 NULL_TREE
, NULL_TREE
);
18251 SET_DECL_VALUE_EXPR (decomp_first
, v
);
18252 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
18254 fit_decomposition_lang_decl (decomp_first
, d
);
18255 decomp_first
= DECL_CHAIN (decomp_first
);
18259 decl
= tsubst_decl (decl
, args
, complain
);
18261 maybe_push_decl (decl
);
18263 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
18265 /* We need to jump through some hoops to handle declarations in the
18266 init-statement, since we might need to handle auto deduction,
18267 but we need to keep control of initialization. */
18269 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
18270 decl
= tsubst_decl (decl
, args
, complain
);
18274 if (TREE_CODE (decl
) == SCOPE_REF
)
18276 decl
= RECUR (decl
);
18277 if (TREE_CODE (decl
) == COMPONENT_REF
)
18281 switch (TREE_CODE (v
))
18283 case COMPONENT_REF
:
18287 case POINTER_PLUS_EXPR
:
18288 v
= TREE_OPERAND (v
, 0);
18291 if (DECL_CONTEXT (v
) == current_function_decl
18292 && DECL_ARTIFICIAL (v
)
18293 && DECL_NAME (v
) == this_identifier
)
18295 decl
= TREE_OPERAND (decl
, 1);
18296 decl
= omp_privatize_field (decl
, false);
18306 decl
= RECUR (decl
);
18308 if (init
&& TREE_CODE (init
) == TREE_VEC
)
18310 init
= copy_node (init
);
18311 TREE_VEC_ELT (init
, 0)
18312 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
18313 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
18314 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
18317 init
= RECUR (init
);
18319 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
18321 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
18322 if (TREE_CODE (o
) == TREE_LIST
)
18323 TREE_VEC_ELT (orig_declv
, i
)
18324 = tree_cons (RECUR (TREE_PURPOSE (o
)),
18325 RECUR (TREE_VALUE (o
)),
18328 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
18333 tree this_pre_body
= NULL_TREE
;
18334 tree orig_init
= NULL_TREE
;
18335 tree orig_decl
= NULL_TREE
;
18336 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
18337 orig_init
, cond
, incr
);
18340 if (orig_declv
== NULL_TREE
)
18341 orig_declv
= copy_node (declv
);
18342 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
18345 else if (orig_declv
)
18346 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18349 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
18350 if (!range_for
&& auto_node
&& init
)
18352 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
18354 gcc_assert (!type_dependent_expression_p (decl
));
18356 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
18360 /* Declare the variable, but don't let that initialize it. */
18361 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
18362 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
18364 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
18369 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18370 if (COMPARISON_CLASS_P (cond
)
18371 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
18373 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
18374 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
18375 TREE_VEC_ELT (rhs
, 0)
18376 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
18377 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
18378 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
18379 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
18383 cond
= RECUR (cond
);
18384 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18385 if (TREE_CODE (incr
) == MODIFY_EXPR
)
18387 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18388 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
18389 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
18390 NOP_EXPR
, rhs
, NULL_TREE
, complain
);
18393 incr
= RECUR (incr
);
18394 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18395 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18397 TREE_VEC_ELT (declv
, i
) = decl
;
18398 TREE_VEC_ELT (initv
, i
) = init
;
18399 TREE_VEC_ELT (condv
, i
) = cond
;
18400 TREE_VEC_ELT (incrv
, i
) = incr
;
18406 /* Declare and initialize the variable. */
18414 for (j
= ((omp_parallel_combined_clauses
== NULL
18415 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
18417 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
18419 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
18420 && OMP_CLAUSE_DECL (*pc
) == decl
)
18422 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
18423 && OMP_CLAUSE_DECL (*pc
) == decl
)
18427 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18429 *pc
= OMP_CLAUSE_CHAIN (c
);
18430 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18433 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
18434 && OMP_CLAUSE_DECL (*pc
) == decl
)
18436 error ("iteration variable %qD should not be firstprivate",
18438 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18440 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
18441 && OMP_CLAUSE_DECL (*pc
) == decl
)
18443 error ("iteration variable %qD should not be reduction",
18445 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18448 pc
= &OMP_CLAUSE_CHAIN (*pc
);
18453 if (*pc
== NULL_TREE
)
18455 tree c
= build_omp_clause (input_location
,
18456 TREE_CODE (t
) == OMP_LOOP
18457 ? OMP_CLAUSE_LASTPRIVATE
18458 : OMP_CLAUSE_PRIVATE
);
18459 OMP_CLAUSE_DECL (c
) = decl
;
18460 c
= finish_omp_clauses (c
, C_ORT_OMP
);
18463 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18468 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18469 if (COMPARISON_CLASS_P (cond
))
18471 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
18472 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
18473 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
18476 cond
= RECUR (cond
);
18477 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18478 switch (TREE_CODE (incr
))
18480 case PREINCREMENT_EXPR
:
18481 case PREDECREMENT_EXPR
:
18482 case POSTINCREMENT_EXPR
:
18483 case POSTDECREMENT_EXPR
:
18484 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
18485 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
18488 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18489 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18491 tree rhs
= TREE_OPERAND (incr
, 1);
18492 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18493 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18494 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18495 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18496 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18500 incr
= RECUR (incr
);
18503 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18504 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18506 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18507 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18508 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
18509 TREE_TYPE (decl
), lhs
,
18510 RECUR (TREE_OPERAND (incr
, 2))));
18512 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
18513 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
18514 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
18516 tree rhs
= TREE_OPERAND (incr
, 2);
18517 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18518 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18519 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18520 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18521 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18525 incr
= RECUR (incr
);
18528 incr
= RECUR (incr
);
18532 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18533 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18534 TREE_VEC_ELT (declv
, i
) = decl
;
18535 TREE_VEC_ELT (initv
, i
) = init
;
18536 TREE_VEC_ELT (condv
, i
) = cond
;
18537 TREE_VEC_ELT (incrv
, i
) = incr
;
18542 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18543 of OMP_TARGET's body. */
18546 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
18548 *walk_subtrees
= 0;
18549 switch (TREE_CODE (*tp
))
18554 case STATEMENT_LIST
:
18555 *walk_subtrees
= 1;
18563 /* Helper function for tsubst_expr. For decomposition declaration
18564 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18565 also the corresponding decls representing the identifiers
18566 of the decomposition declaration. Return DECL if successful
18567 or error_mark_node otherwise, set *FIRST to the first decl
18568 in the list chained through DECL_CHAIN and *CNT to the number
18572 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
18573 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
18576 tree decl2
, decl3
, prev
= decl
;
18578 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
18579 for (decl2
= DECL_CHAIN (pattern_decl
);
18582 && DECL_DECOMPOSITION_P (decl2
)
18583 && DECL_NAME (decl2
);
18584 decl2
= DECL_CHAIN (decl2
))
18586 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
18588 gcc_assert (errorcount
);
18589 return error_mark_node
;
18592 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
18593 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
18594 tree v
= DECL_VALUE_EXPR (decl2
);
18595 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
18596 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
18597 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
18598 SET_DECL_VALUE_EXPR (decl2
, v
);
18599 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
18601 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
18604 gcc_assert (errorcount
);
18605 decl
= error_mark_node
;
18608 maybe_push_decl (decl3
);
18609 if (error_operand_p (decl3
))
18610 decl
= error_mark_node
;
18611 else if (decl
!= error_mark_node
18612 && DECL_CHAIN (decl3
) != prev
18615 gcc_assert (errorcount
);
18616 decl
= error_mark_node
;
18625 /* Return the proper local_specialization for init-capture pack DECL. */
18628 lookup_init_capture_pack (tree decl
)
18630 /* We handle normal pack captures by forwarding to the specialization of the
18631 captured parameter. We can't do that for pack init-captures; we need them
18632 to have their own local_specialization. We created the individual
18633 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18634 when we process the DECL_EXPR for the pack init-capture in the template.
18635 So, how do we find them? We don't know the capture proxy pack when
18636 building the individual resulting proxies, and we don't know the
18637 individual proxies when instantiating the pack. What we have in common is
18640 So...when we instantiate the FIELD_DECL, we stick the result in
18641 local_specializations. Then at the DECL_EXPR we look up that result, see
18642 how many elements it has, synthesize the names, and look them up. */
18644 tree cname
= DECL_NAME (decl
);
18645 tree val
= DECL_VALUE_EXPR (decl
);
18646 tree field
= TREE_OPERAND (val
, 1);
18647 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18648 tree fpack
= retrieve_local_specialization (field
);
18649 if (fpack
== error_mark_node
)
18650 return error_mark_node
;
18653 tree vec
= NULL_TREE
;
18654 tree r
= NULL_TREE
;
18655 if (TREE_CODE (fpack
) == TREE_VEC
)
18657 len
= TREE_VEC_LENGTH (fpack
);
18658 vec
= make_tree_vec (len
);
18659 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18660 ARGUMENT_PACK_ARGS (r
) = vec
;
18662 for (int i
= 0; i
< len
; ++i
)
18664 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18665 tree elt
= lookup_name (ename
);
18667 TREE_VEC_ELT (vec
, i
) = elt
;
18674 /* T is an operand of a template tree being substituted. Return whether
18675 T is dependent such that we should suppress some warnings that would
18676 make sense if the substituted expression were written directly, like
18677 template <int I> bool f() { return I == 2; }
18678 We don't want to warn when instantiating f that comparing two constants
18679 always has the same value.
18681 This is a more limited concept of dependence than instantiation-dependent;
18682 here we don't care whether substitution could fail. */
18685 dependent_operand_p (tree t
)
18687 while (TREE_CODE (t
) == IMPLICIT_CONV_EXPR
)
18688 t
= TREE_OPERAND (t
, 0);
18689 ++processing_template_decl
;
18690 bool r
= (potential_constant_expression (t
)
18691 ? value_dependent_expression_p (t
)
18692 : type_dependent_expression_p (t
));
18693 --processing_template_decl
;
18697 /* Like tsubst_copy for expressions, etc. but also does semantic
18701 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18703 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18704 #define RECUR(NODE) \
18705 tsubst_expr ((NODE), args, complain, in_decl)
18711 if (t
== NULL_TREE
|| t
== error_mark_node
)
18714 loc
= input_location
;
18715 if (location_t eloc
= cp_expr_location (t
))
18716 input_location
= eloc
;
18717 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18718 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18720 switch (TREE_CODE (t
))
18722 case STATEMENT_LIST
:
18724 for (tree stmt
: tsi_range (t
))
18729 case CTOR_INITIALIZER
:
18730 finish_mem_initializers (tsubst_initializer_list
18731 (TREE_OPERAND (t
, 0), args
));
18735 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18738 case CO_RETURN_EXPR
:
18739 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18742 case CO_YIELD_EXPR
:
18743 stmt
= finish_co_yield_expr (input_location
,
18744 RECUR (TREE_OPERAND (t
, 0)));
18747 case CO_AWAIT_EXPR
:
18748 stmt
= finish_co_await_expr (input_location
,
18749 RECUR (TREE_OPERAND (t
, 0)));
18753 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18754 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18755 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18757 finish_expr_stmt (tmp
);
18761 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18764 case PRECONDITION_STMT
:
18765 case POSTCONDITION_STMT
:
18766 gcc_unreachable ();
18768 case ASSERTION_STMT
:
18770 r
= tsubst_contract (NULL_TREE
, t
, args
, complain
, in_decl
);
18771 if (r
!= error_mark_node
)
18779 tree decl
, pattern_decl
;
18782 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18783 if (TREE_CODE (decl
) == LABEL_DECL
)
18784 finish_label_decl (DECL_NAME (decl
));
18785 else if (TREE_CODE (decl
) == USING_DECL
)
18787 tree scope
= USING_DECL_SCOPE (decl
);
18788 if (DECL_DEPENDENT_P (decl
))
18790 scope
= tsubst (scope
, args
, complain
, in_decl
);
18791 if (!MAYBE_CLASS_TYPE_P (scope
)
18792 && TREE_CODE (scope
) != ENUMERAL_TYPE
)
18794 if (complain
& tf_error
)
18795 error_at (DECL_SOURCE_LOCATION (decl
), "%qT is not a "
18796 "class, namespace, or enumeration", scope
);
18797 return error_mark_node
;
18799 finish_nonmember_using_decl (scope
, DECL_NAME (decl
));
18803 /* This is a non-dependent using-decl, and we'll have
18804 used the names it found during template parsing. We do
18805 not want to do the lookup again, because we might not
18806 find the things we found then. */
18807 gcc_checking_assert (scope
== tsubst (scope
, args
,
18808 complain
, in_decl
));
18809 /* We still need to push the bindings so that we can look up
18810 this name later. */
18811 push_using_decl_bindings (DECL_NAME (decl
),
18812 USING_DECL_DECLS (decl
));
18815 else if (is_capture_proxy (decl
)
18816 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18818 /* We're in tsubst_lambda_expr, we've already inserted a new
18819 capture proxy, so look it up and register it. */
18821 if (!DECL_PACK_P (decl
))
18823 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18824 LOOK_want::HIDDEN_LAMBDA
);
18825 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18827 else if (is_normal_capture_proxy (decl
))
18829 inst
= (retrieve_local_specialization
18830 (DECL_CAPTURED_VARIABLE (decl
)));
18831 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18832 || DECL_PACK_P (inst
));
18835 inst
= lookup_init_capture_pack (decl
);
18837 register_local_specialization (inst
, decl
);
18840 else if (DECL_PRETTY_FUNCTION_P (decl
))
18841 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18843 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18844 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18845 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18846 /* Don't copy the old closure; we'll create a new one in
18847 tsubst_lambda_expr. */
18851 init
= DECL_INITIAL (decl
);
18852 decl
= tsubst (decl
, args
, complain
, in_decl
);
18853 if (decl
!= error_mark_node
)
18855 /* By marking the declaration as instantiated, we avoid
18856 trying to instantiate it. Since instantiate_decl can't
18857 handle local variables, and since we've already done
18858 all that needs to be done, that's the right thing to
18861 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18862 if (VAR_P (decl
) && !DECL_NAME (decl
)
18863 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18864 /* Anonymous aggregates are a special case. */
18865 finish_anon_union (decl
);
18866 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18868 DECL_CONTEXT (decl
) = current_function_decl
;
18869 if (DECL_NAME (decl
) == this_identifier
)
18871 tree lam
= DECL_CONTEXT (current_function_decl
);
18872 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18873 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18875 insert_capture_proxy (decl
);
18877 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18878 /* We already did a pushtag. */;
18879 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18880 && DECL_LOCAL_DECL_P (decl
))
18882 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18883 DECL_CONTEXT (decl
) = NULL_TREE
;
18884 decl
= pushdecl (decl
);
18885 if (TREE_CODE (decl
) == FUNCTION_DECL
18886 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18887 && cp_check_omp_declare_reduction (decl
))
18888 instantiate_body (pattern_decl
, args
, decl
, true);
18892 bool const_init
= false;
18893 unsigned int cnt
= 0;
18894 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18895 tree asmspec_tree
= NULL_TREE
;
18896 maybe_push_decl (decl
);
18899 && DECL_LANG_SPECIFIC (decl
)
18900 && DECL_OMP_PRIVATIZED_MEMBER (decl
))
18904 && DECL_DECOMPOSITION_P (decl
)
18905 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18906 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18907 complain
, in_decl
, &first
,
18910 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18913 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18916 if (ndecl
!= error_mark_node
)
18917 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18919 /* In a non-template function, VLA type declarations are
18920 handled in grokdeclarator; for templates, handle them
18922 predeclare_vla (decl
);
18924 if (VAR_P (decl
) && DECL_HARD_REGISTER (pattern_decl
))
18926 tree id
= DECL_ASSEMBLER_NAME (pattern_decl
);
18927 const char *asmspec
= IDENTIFIER_POINTER (id
);
18928 gcc_assert (asmspec
[0] == '*');
18930 = build_string (IDENTIFIER_LENGTH (id
) - 1,
18932 TREE_TYPE (asmspec_tree
) = char_array_type_node
;
18935 cp_finish_decl (decl
, init
, const_init
, asmspec_tree
, 0);
18937 if (ndecl
!= error_mark_node
)
18938 cp_finish_decomp (ndecl
, first
, cnt
);
18947 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18948 RECUR (FOR_INIT_STMT (t
));
18949 finish_init_stmt (stmt
);
18950 tmp
= RECUR (FOR_COND (t
));
18951 finish_for_cond (tmp
, stmt
, false, 0);
18952 tmp
= RECUR (FOR_EXPR (t
));
18953 finish_for_expr (tmp
, stmt
);
18955 bool prev
= note_iteration_stmt_body_start ();
18956 RECUR (FOR_BODY (t
));
18957 note_iteration_stmt_body_end (prev
);
18959 finish_for_stmt (stmt
);
18962 case RANGE_FOR_STMT
:
18964 /* Construct another range_for, if this is not a final
18965 substitution (for inside a generic lambda of a
18966 template). Otherwise convert to a regular for. */
18968 stmt
= (processing_template_decl
18969 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18970 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18971 RECUR (RANGE_FOR_INIT_STMT (t
));
18972 decl
= RANGE_FOR_DECL (t
);
18973 decl
= tsubst (decl
, args
, complain
, in_decl
);
18974 maybe_push_decl (decl
);
18975 expr
= RECUR (RANGE_FOR_EXPR (t
));
18977 tree decomp_first
= NULL_TREE
;
18978 unsigned decomp_cnt
= 0;
18979 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18980 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18982 &decomp_first
, &decomp_cnt
);
18984 if (processing_template_decl
)
18986 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18987 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18988 finish_range_for_decl (stmt
, decl
, expr
);
18989 if (decomp_first
&& decl
!= error_mark_node
)
18990 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18994 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18995 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18996 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18997 decomp_first
, decomp_cnt
,
18998 RANGE_FOR_IVDEP (t
), unroll
);
19001 bool prev
= note_iteration_stmt_body_start ();
19002 RECUR (RANGE_FOR_BODY (t
));
19003 note_iteration_stmt_body_end (prev
);
19004 finish_for_stmt (stmt
);
19009 stmt
= begin_while_stmt ();
19010 tmp
= RECUR (WHILE_COND (t
));
19011 finish_while_stmt_cond (tmp
, stmt
, false, 0);
19013 bool prev
= note_iteration_stmt_body_start ();
19014 RECUR (WHILE_BODY (t
));
19015 note_iteration_stmt_body_end (prev
);
19017 finish_while_stmt (stmt
);
19021 stmt
= begin_do_stmt ();
19023 bool prev
= note_iteration_stmt_body_start ();
19024 RECUR (DO_BODY (t
));
19025 note_iteration_stmt_body_end (prev
);
19027 finish_do_body (stmt
);
19028 tmp
= RECUR (DO_COND (t
));
19029 finish_do_stmt (tmp
, stmt
, false, 0);
19033 stmt
= begin_if_stmt ();
19034 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
19035 IF_STMT_CONSTEVAL_P (stmt
) = IF_STMT_CONSTEVAL_P (t
);
19036 if (IF_STMT_CONSTEXPR_P (t
))
19037 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
, complain
, in_decl
);
19039 tree cond
= IF_COND (t
);
19040 bool was_dep
= dependent_operand_p (cond
);
19041 cond
= RECUR (cond
);
19042 warning_sentinel
s1(warn_address
, was_dep
);
19043 tmp
= finish_if_stmt_cond (cond
, stmt
);
19045 if (IF_STMT_CONSTEXPR_P (t
)
19046 && instantiation_dependent_expression_p (tmp
))
19048 /* We're partially instantiating a generic lambda, but the condition
19049 of the constexpr if is still dependent. Don't substitute into the
19050 branches now, just remember the template arguments. */
19051 do_poplevel (IF_SCOPE (stmt
));
19052 IF_COND (stmt
) = IF_COND (t
);
19053 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
19054 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
19055 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
19059 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
19060 /* Don't instantiate the THEN_CLAUSE. */;
19061 else if (IF_STMT_CONSTEVAL_P (t
))
19063 bool save_in_consteval_if_p
= in_consteval_if_p
;
19064 in_consteval_if_p
= true;
19065 RECUR (THEN_CLAUSE (t
));
19066 in_consteval_if_p
= save_in_consteval_if_p
;
19070 tree folded
= fold_non_dependent_expr (tmp
, complain
);
19071 bool inhibit
= integer_zerop (folded
);
19073 ++c_inhibit_evaluation_warnings
;
19074 RECUR (THEN_CLAUSE (t
));
19076 --c_inhibit_evaluation_warnings
;
19078 finish_then_clause (stmt
);
19080 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
19081 /* Don't instantiate the ELSE_CLAUSE. */;
19082 else if (ELSE_CLAUSE (t
))
19084 tree folded
= fold_non_dependent_expr (tmp
, complain
);
19085 bool inhibit
= integer_nonzerop (folded
);
19086 begin_else_clause (stmt
);
19088 ++c_inhibit_evaluation_warnings
;
19089 RECUR (ELSE_CLAUSE (t
));
19091 --c_inhibit_evaluation_warnings
;
19092 finish_else_clause (stmt
);
19095 finish_if_stmt (stmt
);
19099 if (BIND_EXPR_BODY_BLOCK (t
))
19100 stmt
= begin_function_body ();
19102 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
19103 ? BCS_TRY_BLOCK
: 0);
19105 RECUR (BIND_EXPR_BODY (t
));
19107 if (BIND_EXPR_BODY_BLOCK (t
))
19108 finish_function_body (stmt
);
19110 finish_compound_stmt (stmt
);
19114 finish_break_stmt ();
19117 case CONTINUE_STMT
:
19118 finish_continue_stmt ();
19122 stmt
= begin_switch_stmt ();
19123 tmp
= RECUR (SWITCH_STMT_COND (t
));
19124 finish_switch_cond (tmp
, stmt
);
19125 RECUR (SWITCH_STMT_BODY (t
));
19126 finish_switch_stmt (stmt
);
19129 case CASE_LABEL_EXPR
:
19131 tree decl
= CASE_LABEL (t
);
19132 tree low
= RECUR (CASE_LOW (t
));
19133 tree high
= RECUR (CASE_HIGH (t
));
19134 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
19135 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
19137 tree label
= CASE_LABEL (l
);
19138 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
19139 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
19140 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
19147 tree decl
= LABEL_EXPR_LABEL (t
);
19150 label
= finish_label_stmt (DECL_NAME (decl
));
19151 if (TREE_CODE (label
) == LABEL_DECL
)
19152 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
19153 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
19154 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
19159 tmp
= GOTO_DESTINATION (t
);
19160 if (TREE_CODE (tmp
) != LABEL_DECL
)
19161 /* Computed goto's must be tsubst'd into. On the other hand,
19162 non-computed gotos must not be; the identifier in question
19163 will have no binding. */
19166 tmp
= DECL_NAME (tmp
);
19167 finish_goto_stmt (tmp
);
19172 tree string
= RECUR (ASM_STRING (t
));
19173 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
19174 complain
, in_decl
);
19175 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
19176 complain
, in_decl
);
19177 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
19178 complain
, in_decl
);
19179 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
19180 complain
, in_decl
);
19181 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
19182 outputs
, inputs
, clobbers
, labels
,
19184 tree asm_expr
= tmp
;
19185 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
19186 asm_expr
= TREE_OPERAND (asm_expr
, 0);
19187 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
19194 stmt
= begin_try_block ();
19195 RECUR (TRY_STMTS (t
));
19196 finish_cleanup_try_block (stmt
);
19197 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
19201 tree compound_stmt
= NULL_TREE
;
19203 if (FN_TRY_BLOCK_P (t
))
19204 stmt
= begin_function_try_block (&compound_stmt
);
19206 stmt
= begin_try_block ();
19208 RECUR (TRY_STMTS (t
));
19210 if (FN_TRY_BLOCK_P (t
))
19211 finish_function_try_block (stmt
);
19213 finish_try_block (stmt
);
19215 RECUR (TRY_HANDLERS (t
));
19216 if (FN_TRY_BLOCK_P (t
))
19217 finish_function_handler_sequence (stmt
, compound_stmt
);
19219 finish_handler_sequence (stmt
);
19225 tree decl
= HANDLER_PARMS (t
);
19229 decl
= tsubst (decl
, args
, complain
, in_decl
);
19230 /* Prevent instantiate_decl from trying to instantiate
19231 this variable. We've already done all that needs to be
19233 if (decl
!= error_mark_node
)
19234 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
19236 stmt
= begin_handler ();
19237 finish_handler_parms (decl
, stmt
);
19238 RECUR (HANDLER_BODY (t
));
19239 finish_handler (stmt
);
19244 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
19245 if (CLASS_TYPE_P (tmp
))
19247 /* Local classes are not independent templates; they are
19248 instantiated along with their containing function. And this
19249 way we don't have to deal with pushing out of one local class
19250 to instantiate a member of another local class. */
19251 /* Closures are handled by the LAMBDA_EXPR. */
19252 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
19253 complete_type (tmp
);
19254 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
19256 || (TREE_CODE (fld
) == FUNCTION_DECL
19257 && !DECL_ARTIFICIAL (fld
)))
19258 && DECL_TEMPLATE_INSTANTIATION (fld
))
19259 instantiate_decl (fld
, /*defer_ok=*/false,
19260 /*expl_inst_class=*/false);
19264 case STATIC_ASSERT
:
19268 ++c_inhibit_evaluation_warnings
;
19269 condition
= tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
19270 complain
, in_decl
);
19271 --c_inhibit_evaluation_warnings
;
19273 finish_static_assert (condition
,
19274 STATIC_ASSERT_MESSAGE (t
),
19275 STATIC_ASSERT_SOURCE_LOCATION (t
),
19276 /*member_p=*/false, /*show_expr_p=*/true);
19281 case OACC_PARALLEL
:
19283 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
19285 stmt
= begin_omp_parallel ();
19286 RECUR (OMP_BODY (t
));
19287 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
19291 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
19292 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
19293 complain
, in_decl
);
19294 if (OMP_PARALLEL_COMBINED (t
))
19295 omp_parallel_combined_clauses
= &tmp
;
19296 stmt
= begin_omp_parallel ();
19297 RECUR (OMP_PARALLEL_BODY (t
));
19298 gcc_assert (omp_parallel_combined_clauses
== NULL
);
19299 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
19300 = OMP_PARALLEL_COMBINED (t
);
19301 pop_omp_privatization_clauses (r
);
19305 if (OMP_TASK_BODY (t
) == NULL_TREE
)
19307 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
19308 complain
, in_decl
);
19310 OMP_TASK_CLAUSES (t
) = tmp
;
19314 r
= push_omp_privatization_clauses (false);
19315 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
19316 complain
, in_decl
);
19317 stmt
= begin_omp_task ();
19318 RECUR (OMP_TASK_BODY (t
));
19319 finish_omp_task (tmp
, stmt
);
19320 pop_omp_privatization_clauses (r
);
19326 case OMP_DISTRIBUTE
:
19330 tree clauses
, body
, pre_body
;
19331 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
19332 tree orig_declv
= NULL_TREE
;
19333 tree incrv
= NULL_TREE
;
19334 enum c_omp_region_type ort
= C_ORT_OMP
;
19335 bool any_range_for
= false;
19338 if (TREE_CODE (t
) == OACC_LOOP
)
19341 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
19342 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
19344 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19346 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19347 if (OMP_FOR_ORIG_DECLS (t
))
19348 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19349 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19350 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19351 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19354 keep_next_level (true);
19355 stmt
= begin_omp_structured_block ();
19357 pre_body
= push_stmt_list ();
19358 RECUR (OMP_FOR_PRE_BODY (t
));
19359 pre_body
= pop_stmt_list (pre_body
);
19361 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19362 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19364 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
19365 condv
, incrv
, &clauses
, args
,
19366 complain
, in_decl
);
19367 omp_parallel_combined_clauses
= NULL
;
19371 gcc_assert (orig_declv
);
19372 body
= begin_omp_structured_block ();
19373 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19374 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
19375 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
19376 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
19377 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
19378 TREE_VEC_ELT (declv
, i
));
19381 body
= push_stmt_list ();
19382 RECUR (OMP_FOR_BODY (t
));
19384 body
= finish_omp_structured_block (body
);
19386 body
= pop_stmt_list (body
);
19388 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19389 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
19390 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
19394 t
= make_node (TREE_CODE (t
));
19395 TREE_TYPE (t
) = void_type_node
;
19396 OMP_FOR_BODY (t
) = body
;
19397 OMP_FOR_PRE_BODY (t
) = pre_body
;
19398 OMP_FOR_CLAUSES (t
) = clauses
;
19399 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
19403 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
19405 pop_omp_privatization_clauses (r
);
19411 omp_parallel_combined_clauses
= NULL
;
19417 case OMP_TASKGROUP
:
19419 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
19420 && OMP_TEAMS_COMBINED (t
));
19421 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
19423 if (TREE_CODE (t
) == OMP_TEAMS
)
19425 keep_next_level (true);
19426 stmt
= begin_omp_structured_block ();
19427 RECUR (OMP_BODY (t
));
19428 stmt
= finish_omp_structured_block (stmt
);
19432 stmt
= push_stmt_list ();
19433 RECUR (OMP_BODY (t
));
19434 stmt
= pop_stmt_list (stmt
);
19437 if (TREE_CODE (t
) == OMP_CRITICAL
19438 && tmp
!= NULL_TREE
19439 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
19441 error_at (OMP_CLAUSE_LOCATION (tmp
),
19442 "%<#pragma omp critical%> with %<hint%> clause requires "
19443 "a name, except when %<omp_sync_hint_none%> is used");
19444 RETURN (error_mark_node
);
19447 OMP_BODY (t
) = stmt
;
19448 OMP_CLAUSES (t
) = tmp
;
19450 pop_omp_privatization_clauses (r
);
19454 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
19455 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
19457 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INVALID
;
19458 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
19460 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
19461 args
, complain
, in_decl
);
19462 if (tmp
== NULL_TREE
)
19463 tmp
= error_mark_node
;
19467 kind
= (enum omp_clause_depend_kind
)
19468 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
19471 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
19474 finish_omp_depobj (EXPR_LOCATION (t
), r
,
19475 OMP_CLAUSE_DEPEND_INVALID
,
19476 OMP_DEPOBJ_CLAUSES (t
));
19480 case OMP_TARGET_DATA
:
19482 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
),
19483 TREE_CODE (t
) == OACC_DATA
19485 : TREE_CODE (t
) == OMP_TARGET
19486 ? C_ORT_OMP_TARGET
: C_ORT_OMP
,
19487 args
, complain
, in_decl
);
19488 keep_next_level (true);
19489 stmt
= begin_omp_structured_block ();
19491 RECUR (OMP_BODY (t
));
19492 stmt
= finish_omp_structured_block (stmt
);
19495 OMP_BODY (t
) = stmt
;
19496 OMP_CLAUSES (t
) = tmp
;
19498 if (TREE_CODE (t
) == OMP_TARGET
)
19499 finish_omp_target_clauses (EXPR_LOCATION (t
), OMP_BODY (t
),
19502 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
19504 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
19506 /* For combined target teams, ensure the num_teams and
19507 thread_limit clause expressions are evaluated on the host,
19508 before entering the target construct. */
19509 for (tree c
= OMP_TEAMS_CLAUSES (teams
);
19510 c
; c
= OMP_CLAUSE_CHAIN (c
))
19511 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
19512 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
19514 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
19515 if (OMP_CLAUSE_OPERAND (c
, i
)
19516 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
19518 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
19519 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
19521 if (expr
== error_mark_node
)
19523 tmp
= TARGET_EXPR_SLOT (expr
);
19525 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
19526 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
19527 OMP_CLAUSE_FIRSTPRIVATE
);
19528 OMP_CLAUSE_DECL (tc
) = tmp
;
19529 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
19530 OMP_TARGET_CLAUSES (t
) = tc
;
19538 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
19539 complain
, in_decl
);
19540 OACC_DECLARE_CLAUSES (t
) = tmp
;
19544 case OMP_TARGET_UPDATE
:
19545 case OMP_TARGET_ENTER_DATA
:
19546 case OMP_TARGET_EXIT_DATA
:
19547 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
19548 complain
, in_decl
);
19550 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19555 case OACC_ENTER_DATA
:
19556 case OACC_EXIT_DATA
:
19558 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
19559 complain
, in_decl
);
19561 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19566 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
19567 complain
, in_decl
);
19570 stmt
= push_stmt_list ();
19571 RECUR (OMP_BODY (t
));
19572 stmt
= pop_stmt_list (stmt
);
19578 OMP_BODY (t
) = stmt
;
19579 OMP_ORDERED_CLAUSES (t
) = tmp
;
19584 omp_parallel_combined_clauses
= NULL
;
19587 stmt
= push_stmt_list ();
19588 RECUR (OMP_BODY (t
));
19589 stmt
= pop_stmt_list (stmt
);
19592 OMP_BODY (t
) = stmt
;
19597 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
19599 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
19600 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
19601 complain
, in_decl
);
19602 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
19604 tree op1
= TREE_OPERAND (t
, 1);
19605 tree rhs1
= NULL_TREE
;
19606 tree r
= NULL_TREE
;
19608 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
19610 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
19611 op1
= TREE_OPERAND (op1
, 1);
19613 if (TREE_CODE (op1
) == COND_EXPR
)
19615 gcc_assert (rhs1
== NULL_TREE
);
19616 tree c
= TREE_OPERAND (op1
, 0);
19617 if (TREE_CODE (c
) == MODIFY_EXPR
)
19619 r
= RECUR (TREE_OPERAND (c
, 0));
19620 c
= TREE_OPERAND (c
, 1);
19622 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19623 rhs
= RECUR (TREE_OPERAND (c
, 1));
19624 lhs
= RECUR (TREE_OPERAND (op1
, 2));
19625 rhs1
= RECUR (TREE_OPERAND (op1
, 1));
19629 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19630 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19632 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
19633 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, r
,
19634 tmp
, OMP_ATOMIC_MEMORY_ORDER (t
),
19635 OMP_ATOMIC_WEAK (t
));
19639 tree op1
= TREE_OPERAND (t
, 1);
19640 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
19641 tree rhs1
= NULL_TREE
, r
= NULL_TREE
;
19642 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
19643 enum tree_code opcode
= NOP_EXPR
;
19644 if (code
== OMP_ATOMIC_READ
)
19646 v
= RECUR (TREE_OPERAND (op1
, 0));
19647 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19649 else if (code
== OMP_ATOMIC_CAPTURE_OLD
19650 || code
== OMP_ATOMIC_CAPTURE_NEW
)
19652 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
19653 v
= RECUR (TREE_OPERAND (op1
, 0));
19654 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19655 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
19657 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
19658 op11
= TREE_OPERAND (op11
, 1);
19660 if (TREE_CODE (op11
) == COND_EXPR
)
19662 gcc_assert (rhs1
== NULL_TREE
);
19663 tree c
= TREE_OPERAND (op11
, 0);
19664 if (TREE_CODE (c
) == MODIFY_EXPR
)
19666 r
= RECUR (TREE_OPERAND (c
, 0));
19667 c
= TREE_OPERAND (c
, 1);
19669 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19670 rhs
= RECUR (TREE_OPERAND (c
, 1));
19671 lhs
= RECUR (TREE_OPERAND (op11
, 2));
19672 rhs1
= RECUR (TREE_OPERAND (op11
, 1));
19676 lhs
= RECUR (TREE_OPERAND (op11
, 0));
19677 rhs
= RECUR (TREE_OPERAND (op11
, 1));
19679 opcode
= TREE_CODE (op11
);
19680 if (opcode
== MODIFY_EXPR
)
19686 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19687 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19689 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
19690 lhs1
, rhs1
, r
, tmp
,
19691 OMP_ATOMIC_MEMORY_ORDER (t
), OMP_ATOMIC_WEAK (t
));
19695 case TRANSACTION_EXPR
:
19698 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
19699 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
19701 if (TRANSACTION_EXPR_IS_STMT (t
))
19703 tree body
= TRANSACTION_EXPR_BODY (t
);
19704 tree noex
= NULL_TREE
;
19705 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
19707 noex
= MUST_NOT_THROW_COND (body
);
19708 if (noex
== NULL_TREE
)
19709 noex
= boolean_true_node
;
19710 body
= TREE_OPERAND (body
, 0);
19712 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
19714 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
19718 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
19719 RECUR (TRANSACTION_EXPR_BODY (t
)),
19726 case MUST_NOT_THROW_EXPR
:
19728 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19729 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
19730 RETURN (build_must_not_throw_expr (op0
, cond
));
19733 case EXPR_PACK_EXPANSION
:
19734 error ("invalid use of pack expansion expression");
19735 RETURN (error_mark_node
);
19737 case NONTYPE_ARGUMENT_PACK
:
19738 error ("use %<...%> to expand argument pack");
19739 RETURN (error_mark_node
);
19741 case COMPOUND_EXPR
:
19742 tmp
= RECUR (TREE_OPERAND (t
, 0));
19743 if (tmp
== NULL_TREE
)
19744 /* If the first operand was a statement, we're done with it. */
19745 RETURN (RECUR (TREE_OPERAND (t
, 1)));
19746 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
19747 RECUR (TREE_OPERAND (t
, 1)),
19748 templated_operator_saved_lookups (t
),
19751 case ANNOTATE_EXPR
:
19752 tmp
= RECUR (TREE_OPERAND (t
, 0));
19753 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
19754 TREE_TYPE (tmp
), tmp
,
19755 RECUR (TREE_OPERAND (t
, 1)),
19756 RECUR (TREE_OPERAND (t
, 2))));
19759 RETURN (add_stmt (copy_node (t
)));
19762 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19764 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
));
19767 RETURN (NULL_TREE
);
19769 input_location
= loc
;
19775 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19776 function. For description of the body see comment above
19777 cp_parser_omp_declare_reduction_exprs. */
19780 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19782 if (t
== NULL_TREE
|| t
== error_mark_node
)
19785 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19787 tree_stmt_iterator tsi
;
19790 memset (stmts
, 0, sizeof stmts
);
19791 for (i
= 0, tsi
= tsi_start (t
);
19792 i
< 7 && !tsi_end_p (tsi
);
19793 i
++, tsi_next (&tsi
))
19794 stmts
[i
] = tsi_stmt (tsi
);
19795 gcc_assert (tsi_end_p (tsi
));
19799 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19800 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19801 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19802 args
, complain
, in_decl
);
19803 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19804 args
, complain
, in_decl
);
19805 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19806 expect to be pushing it. */
19807 DECL_CONTEXT (omp_out
) = current_function_decl
;
19808 DECL_CONTEXT (omp_in
) = current_function_decl
;
19809 keep_next_level (true);
19810 tree block
= begin_omp_structured_block ();
19811 tsubst_expr (stmts
[2], args
, complain
, in_decl
);
19812 block
= finish_omp_structured_block (block
);
19813 block
= maybe_cleanup_point_expr_void (block
);
19814 add_decl_expr (omp_out
);
19815 copy_warning (omp_out
, DECL_EXPR_DECL (stmts
[0]));
19816 add_decl_expr (omp_in
);
19817 finish_expr_stmt (block
);
19821 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19822 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19823 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19824 args
, complain
, in_decl
);
19825 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19826 args
, complain
, in_decl
);
19827 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19828 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19829 keep_next_level (true);
19830 tree block
= begin_omp_structured_block ();
19831 tsubst_expr (stmts
[5], args
, complain
, in_decl
);
19832 block
= finish_omp_structured_block (block
);
19833 block
= maybe_cleanup_point_expr_void (block
);
19834 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19835 add_decl_expr (omp_priv
);
19836 add_decl_expr (omp_orig
);
19837 finish_expr_stmt (block
);
19839 add_decl_expr (omp_orig
);
19843 /* T is a postfix-expression that is not being used in a function
19844 call. Return the substituted version of T. */
19847 tsubst_non_call_postfix_expression (tree t
, tree args
,
19848 tsubst_flags_t complain
,
19851 if (TREE_CODE (t
) == SCOPE_REF
)
19852 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19853 /*done=*/false, /*address_p=*/false);
19855 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
);
19860 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19861 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19862 dependent init-capture. */
19865 prepend_one_capture (tree field
, tree init
, tree
&list
,
19866 tsubst_flags_t complain
)
19868 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19870 tree type
= NULL_TREE
;
19873 if (complain
& tf_error
)
19874 error ("empty initializer in lambda init-capture");
19875 init
= error_mark_node
;
19877 else if (TREE_CODE (init
) == TREE_LIST
)
19878 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19880 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19881 TREE_TYPE (field
) = type
;
19882 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19884 list
= tree_cons (field
, init
, list
);
19887 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19888 instantiation context. Instantiating a pack expansion containing a lambda
19889 might result in multiple lambdas all based on the same lambda in the
19893 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19895 tree oldfn
= lambda_function (t
);
19898 tree r
= build_lambda_expr ();
19900 LAMBDA_EXPR_LOCATION (r
)
19901 = LAMBDA_EXPR_LOCATION (t
);
19902 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19903 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19904 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19905 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
19906 LAMBDA_EXPR_REGEN_INFO (r
)
19907 = build_template_info (t
, add_to_template_args (TI_ARGS (ti
),
19908 preserve_args (args
)));
19910 LAMBDA_EXPR_REGEN_INFO (r
)
19911 = build_template_info (t
, preserve_args (args
));
19913 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19914 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19916 vec
<tree
,va_gc
>* field_packs
= NULL
;
19918 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19919 cap
= TREE_CHAIN (cap
))
19921 tree ofield
= TREE_PURPOSE (cap
);
19922 tree init
= TREE_VALUE (cap
);
19923 if (PACK_EXPANSION_P (init
))
19924 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19926 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
);
19928 if (init
== error_mark_node
)
19929 return error_mark_node
;
19931 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19932 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19934 if (!processing_template_decl
19935 && init
&& TREE_CODE (init
) != TREE_VEC
19936 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19938 /* For a VLA, simply tsubsting the field type won't work, we need to
19939 go through add_capture again. XXX do we want to do this for all
19941 tree name
= (get_identifier
19942 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19943 tree ftype
= TREE_TYPE (ofield
);
19944 bool by_ref
= (TYPE_REF_P (ftype
)
19945 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19946 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19947 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19951 if (PACK_EXPANSION_P (ofield
))
19952 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19953 tree field
= tsubst_decl (ofield
, args
, complain
);
19955 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19957 /* Remember these for when we've pushed local_specializations. */
19958 vec_safe_push (field_packs
, ofield
);
19959 vec_safe_push (field_packs
, field
);
19962 if (field
== error_mark_node
)
19963 return error_mark_node
;
19965 if (TREE_CODE (field
) == TREE_VEC
)
19967 int len
= TREE_VEC_LENGTH (field
);
19968 gcc_assert (TREE_CODE (init
) == TREE_VEC
19969 && TREE_VEC_LENGTH (init
) == len
);
19970 for (int i
= 0; i
< len
; ++i
)
19971 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19972 TREE_VEC_ELT (init
, i
),
19973 LAMBDA_EXPR_CAPTURE_LIST (r
),
19978 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19981 if (id_equal (DECL_NAME (field
), "__this"))
19982 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19986 tree type
= begin_lambda_type (r
);
19987 if (type
== error_mark_node
)
19988 return error_mark_node
;
19990 if (LAMBDA_EXPR_EXTRA_SCOPE (t
))
19991 record_lambda_scope (r
);
19992 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t
)))
19993 /* If we're pushed into another scope (PR105652), fix it. */
19994 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_NAME (type
))
19995 = TYPE_CONTEXT (TREE_TYPE (t
));
19996 record_lambda_scope_discriminator (r
);
19998 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19999 determine_visibility (TYPE_NAME (type
));
20001 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
20003 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
20004 ? DECL_TI_TEMPLATE (oldfn
)
20007 tree fntype
= static_fn_type (oldfn
);
20009 ++processing_template_decl
;
20010 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
20012 --processing_template_decl
;
20014 if (fntype
== error_mark_node
)
20015 r
= error_mark_node
;
20018 /* The body of a lambda-expression is not a subexpression of the
20019 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
20020 which would be skipped if cp_unevaluated_operand. */
20023 /* Fix the type of 'this'. */
20024 fntype
= build_memfn_type (fntype
, type
,
20025 type_memfn_quals (fntype
),
20026 type_memfn_rqual (fntype
));
20027 tree inst
= (oldtmpl
20028 ? tsubst_template_decl (oldtmpl
, args
, complain
, fntype
)
20029 : tsubst_function_decl (oldfn
, args
, complain
, fntype
));
20030 if (inst
== error_mark_node
)
20032 r
= error_mark_node
;
20035 finish_member_declaration (inst
);
20036 record_lambda_scope_sig_discriminator (r
, inst
);
20038 tree fn
= oldtmpl
? DECL_TEMPLATE_RESULT (inst
) : inst
;
20040 /* Let finish_function set this. */
20041 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
20043 bool nested
= cfun
;
20045 push_function_context ();
20047 /* Still increment function_depth so that we don't GC in the
20048 middle of an expression. */
20051 local_specialization_stack
s (lss_copy
);
20053 bool save_in_consteval_if_p
= in_consteval_if_p
;
20054 in_consteval_if_p
= false;
20056 tree body
= start_lambda_function (fn
, r
);
20058 /* Now record them for lookup_init_capture_pack. */
20059 int fplen
= vec_safe_length (field_packs
);
20060 for (int i
= 0; i
< fplen
; )
20062 tree pack
= (*field_packs
)[i
++];
20063 tree inst
= (*field_packs
)[i
++];
20064 register_local_specialization (inst
, pack
);
20066 release_tree_vector (field_packs
);
20068 register_parameter_specializations (oldfn
, fn
);
20072 /* We might not partially instantiate some parts of the function, so
20073 copy these flags from the original template. */
20074 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
20075 current_function_returns_value
= ol
->returns_value
;
20076 current_function_returns_null
= ol
->returns_null
;
20077 current_function_returns_abnormally
= ol
->returns_abnormally
;
20078 current_function_infinite_loop
= ol
->infinite_loop
;
20081 /* [temp.deduct] A lambda-expression appearing in a function type or a
20082 template parameter is not considered part of the immediate context for
20083 the purposes of template argument deduction. */
20084 complain
= tf_warning_or_error
;
20086 tree saved
= DECL_SAVED_TREE (oldfn
);
20087 if (TREE_CODE (saved
) == BIND_EXPR
&& BIND_EXPR_BODY_BLOCK (saved
))
20088 /* We already have a body block from start_lambda_function, we don't
20089 need another to confuse NRV (91217). */
20090 saved
= BIND_EXPR_BODY (saved
);
20092 tsubst_expr (saved
, args
, complain
, r
);
20094 finish_lambda_function (body
);
20096 in_consteval_if_p
= save_in_consteval_if_p
;
20099 pop_function_context ();
20103 /* The capture list was built up in reverse order; fix that now. */
20104 LAMBDA_EXPR_CAPTURE_LIST (r
)
20105 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
20107 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
20109 maybe_add_lambda_conv_op (type
);
20113 finish_struct (type
, /*attr*/NULL_TREE
);
20115 insert_pending_capture_proxies ();
20120 /* Subroutine of maybe_fold_fn_template_args. */
20123 fold_targs_r (tree targs
, tsubst_flags_t complain
)
20125 int len
= TREE_VEC_LENGTH (targs
);
20126 for (int i
= 0; i
< len
; ++i
)
20128 tree
&elt
= TREE_VEC_ELT (targs
, i
);
20129 if (!elt
|| TYPE_P (elt
)
20130 || TREE_CODE (elt
) == TEMPLATE_DECL
)
20132 if (TREE_CODE (elt
) == NONTYPE_ARGUMENT_PACK
)
20134 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt
), complain
))
20137 else if (/* We can only safely preevaluate scalar prvalues. */
20138 SCALAR_TYPE_P (TREE_TYPE (elt
))
20139 && !glvalue_p (elt
)
20140 && !TREE_CONSTANT (elt
))
20142 elt
= cxx_constant_value (elt
, complain
);
20143 if (elt
== error_mark_node
)
20151 /* Try to do constant evaluation of any explicit template arguments in FN
20152 before overload resolution, to get any errors only once. Return true iff
20153 we didn't have any problems folding. */
20156 maybe_fold_fn_template_args (tree fn
, tsubst_flags_t complain
)
20158 if (processing_template_decl
|| fn
== NULL_TREE
)
20160 if (fn
== error_mark_node
)
20162 if (TREE_CODE (fn
) == OFFSET_REF
20163 || TREE_CODE (fn
) == COMPONENT_REF
)
20164 fn
= TREE_OPERAND (fn
, 1);
20165 if (BASELINK_P (fn
))
20166 fn
= BASELINK_FUNCTIONS (fn
);
20167 if (TREE_CODE (fn
) != TEMPLATE_ID_EXPR
)
20169 tree targs
= TREE_OPERAND (fn
, 1);
20170 if (targs
== NULL_TREE
)
20172 if (targs
== error_mark_node
)
20174 return fold_targs_r (targs
, complain
);
20177 /* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
20181 tsubst_copy_and_build_call_args (tree t
, tree args
, tsubst_flags_t complain
,
20182 tree in_decl
, releasing_vec
&call_args
)
20184 unsigned int nargs
= call_expr_nargs (t
);
20185 for (unsigned int i
= 0; i
< nargs
; ++i
)
20187 tree arg
= CALL_EXPR_ARG (t
, i
);
20189 if (!PACK_EXPANSION_P (arg
))
20190 vec_safe_push (call_args
,
20191 tsubst_copy_and_build (arg
, args
, complain
, in_decl
));
20194 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
20195 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
20196 if (TREE_CODE (arg
) == TREE_VEC
)
20198 unsigned int len
, j
;
20200 len
= TREE_VEC_LENGTH (arg
);
20201 for (j
= 0; j
< len
; ++j
)
20203 tree value
= TREE_VEC_ELT (arg
, j
);
20204 if (value
!= NULL_TREE
)
20205 value
= convert_from_reference (value
);
20206 vec_safe_push (call_args
, value
);
20210 /* A partial substitution. Add one entry. */
20211 vec_safe_push (call_args
, arg
);
20216 /* Like tsubst but deals with expressions and performs semantic
20220 tsubst_copy_and_build (tree t
,
20222 tsubst_flags_t complain
,
20225 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
20226 #define RECUR(NODE) \
20227 tsubst_copy_and_build (NODE, args, complain, in_decl)
20230 location_t save_loc
;
20232 if (t
== NULL_TREE
|| t
== error_mark_node
)
20235 save_loc
= input_location
;
20236 if (location_t eloc
= cp_expr_location (t
))
20237 input_location
= eloc
;
20239 /* N3276 decltype magic only applies to calls at the top level or on the
20240 right side of a comma. */
20241 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
20242 complain
&= ~tf_decltype
;
20244 switch (TREE_CODE (t
))
20248 /* Fall through. */
20249 case IDENTIFIER_NODE
:
20253 const char *error_msg
;
20255 if (IDENTIFIER_CONV_OP_P (t
))
20257 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20258 t
= make_conv_op_name (new_type
);
20261 /* Look up the name. */
20262 decl
= lookup_name (t
);
20264 /* By convention, expressions use ERROR_MARK_NODE to indicate
20265 failure, not NULL_TREE. */
20266 if (decl
== NULL_TREE
)
20267 decl
= error_mark_node
;
20269 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
20272 /*allow_i_c_e_p=*/true,
20273 /*non_i_c_e_p=*/nullptr,
20274 /*template_p=*/false,
20276 /*address_p=*/false,
20277 /*template_arg_p=*/false,
20282 if (identifier_p (decl
))
20284 if (complain
& tf_error
)
20285 unqualified_name_lookup_error (decl
);
20286 decl
= error_mark_node
;
20291 case TEMPLATE_ID_EXPR
:
20294 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
20295 complain
, in_decl
);
20296 tree targs
= TREE_OPERAND (t
, 1);
20299 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
20300 if (targs
== error_mark_node
)
20301 RETURN (error_mark_node
);
20303 if (TREE_CODE (templ
) == SCOPE_REF
)
20305 tree name
= TREE_OPERAND (templ
, 1);
20306 tree tid
= lookup_template_function (name
, targs
);
20307 TREE_OPERAND (templ
, 1) = tid
;
20311 if (concept_definition_p (templ
))
20313 tree check
= build_concept_check (templ
, targs
, complain
);
20314 if (check
== error_mark_node
)
20315 RETURN (error_mark_node
);
20317 tree id
= unpack_concept_check (check
);
20319 /* If we built a function concept check, return the underlying
20320 template-id. So we can evaluate it as a function call. */
20321 if (function_concept_p (TREE_OPERAND (id
, 0)))
20327 if (variable_template_p (templ
))
20329 tree r
= lookup_and_finish_template_variable (templ
, targs
,
20331 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
20335 if (TREE_CODE (templ
) == COMPONENT_REF
)
20337 object
= TREE_OPERAND (templ
, 0);
20338 templ
= TREE_OPERAND (templ
, 1);
20341 object
= NULL_TREE
;
20343 tree tid
= lookup_template_function (templ
, targs
);
20344 protected_set_expr_location (tid
, EXPR_LOCATION (t
));
20347 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
20348 object
, tid
, NULL_TREE
));
20349 else if (identifier_p (templ
))
20351 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20352 name lookup found nothing when parsing the template name. */
20353 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
20357 RETURN (baselink_for_fns (tid
));
20362 tree r
= RECUR (TREE_OPERAND (t
, 0));
20364 if (REFERENCE_REF_P (t
))
20366 /* A type conversion to reference type will be enclosed in
20367 such an indirect ref, but the substitution of the cast
20368 will have also added such an indirect ref. */
20369 r
= convert_from_reference (r
);
20372 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
20373 templated_operator_saved_lookups (t
),
20374 complain
|decltype_flag
);
20376 if (REF_PARENTHESIZED_P (t
))
20377 r
= force_paren_expr (r
);
20384 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20385 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20386 RETURN (build_nop (type
, op0
));
20389 case IMPLICIT_CONV_EXPR
:
20391 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20392 tree expr
= RECUR (TREE_OPERAND (t
, 0));
20393 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
20395 retval
= copy_node (t
);
20396 TREE_TYPE (retval
) = type
;
20397 TREE_OPERAND (retval
, 0) = expr
;
20400 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
20401 /* We'll pass this to convert_nontype_argument again, we don't need
20402 to actually perform any conversion here. */
20404 int flags
= LOOKUP_IMPLICIT
;
20405 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
20406 flags
= LOOKUP_NORMAL
;
20407 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
20408 flags
|= LOOKUP_NO_NARROWING
;
20409 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
20415 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20416 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20417 if (op0
== error_mark_node
)
20418 RETURN (error_mark_node
);
20419 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
20423 case REINTERPRET_CAST_EXPR
:
20424 case CONST_CAST_EXPR
:
20425 case DYNAMIC_CAST_EXPR
:
20426 case STATIC_CAST_EXPR
:
20429 tree op
, r
= NULL_TREE
;
20431 tsubst_flags_t tcomplain
= complain
;
20432 if (TREE_CODE (t
) == CAST_EXPR
)
20433 tcomplain
|= tf_tst_ok
;
20434 type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
20436 op
= RECUR (TREE_OPERAND (t
, 0));
20438 warning_sentinel
s(warn_useless_cast
);
20439 warning_sentinel
s2(warn_ignored_qualifiers
);
20440 warning_sentinel
s3(warn_int_in_bool_context
);
20441 switch (TREE_CODE (t
))
20444 r
= build_functional_cast (input_location
, type
, op
, complain
);
20446 case REINTERPRET_CAST_EXPR
:
20447 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
20449 case CONST_CAST_EXPR
:
20450 r
= build_const_cast (input_location
, type
, op
, complain
);
20452 case DYNAMIC_CAST_EXPR
:
20453 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
20455 case STATIC_CAST_EXPR
:
20456 r
= build_static_cast (input_location
, type
, op
, complain
);
20457 if (IMPLICIT_RVALUE_P (t
))
20458 set_implicit_rvalue_p (r
);
20461 gcc_unreachable ();
20467 case BIT_CAST_EXPR
:
20469 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20470 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20471 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
20474 case POSTDECREMENT_EXPR
:
20475 case POSTINCREMENT_EXPR
:
20476 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20477 args
, complain
, in_decl
);
20478 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
20479 templated_operator_saved_lookups (t
),
20480 complain
|decltype_flag
));
20482 case PREDECREMENT_EXPR
:
20483 case PREINCREMENT_EXPR
:
20487 case TRUTH_NOT_EXPR
:
20488 case UNARY_PLUS_EXPR
: /* Unary + */
20489 case REALPART_EXPR
:
20490 case IMAGPART_EXPR
:
20491 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
20492 RECUR (TREE_OPERAND (t
, 0)),
20493 templated_operator_saved_lookups (t
),
20494 complain
|decltype_flag
));
20496 case EXCESS_PRECISION_EXPR
:
20498 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20499 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20500 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
20502 RETURN (build1_loc (EXPR_LOCATION (t
), EXCESS_PRECISION_EXPR
,
20506 case FIX_TRUNC_EXPR
:
20507 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20508 gcc_unreachable ();
20511 op1
= TREE_OPERAND (t
, 0);
20512 if (TREE_CODE (op1
) == LABEL_DECL
)
20513 RETURN (finish_label_address_expr (DECL_NAME (op1
),
20514 EXPR_LOCATION (op1
)));
20515 if (TREE_CODE (op1
) == SCOPE_REF
)
20516 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
20517 /*done=*/true, /*address_p=*/true);
20519 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
20521 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
20522 templated_operator_saved_lookups (t
),
20523 complain
|decltype_flag
));
20528 case TRUNC_DIV_EXPR
:
20529 case CEIL_DIV_EXPR
:
20530 case FLOOR_DIV_EXPR
:
20531 case ROUND_DIV_EXPR
:
20532 case EXACT_DIV_EXPR
:
20536 case TRUNC_MOD_EXPR
:
20537 case FLOOR_MOD_EXPR
:
20538 case TRUTH_ANDIF_EXPR
:
20539 case TRUTH_ORIF_EXPR
:
20540 case TRUTH_AND_EXPR
:
20541 case TRUTH_OR_EXPR
:
20552 case SPACESHIP_EXPR
:
20556 /* If either OP0 or OP1 was value- or type-dependent, suppress
20557 warnings that depend on the range of the types involved. */
20558 tree op0
= TREE_OPERAND (t
, 0);
20559 tree op1
= TREE_OPERAND (t
, 1);
20560 const bool was_dep
= (dependent_operand_p (op0
)
20561 || dependent_operand_p (op1
));
20565 warning_sentinel
s1(warn_type_limits
, was_dep
);
20566 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
20567 warning_sentinel
s3(warn_logical_op
, was_dep
);
20568 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
20569 warning_sentinel
s5(warn_address
, was_dep
);
20571 tree r
= build_x_binary_op
20572 (input_location
, TREE_CODE (t
),
20574 (warning_suppressed_p (TREE_OPERAND (t
, 0))
20576 : TREE_CODE (TREE_OPERAND (t
, 0))),
20578 (warning_suppressed_p (TREE_OPERAND (t
, 1))
20580 : TREE_CODE (TREE_OPERAND (t
, 1))),
20581 templated_operator_saved_lookups (t
),
20583 complain
|decltype_flag
);
20585 copy_warning (r
, t
);
20590 case POINTER_PLUS_EXPR
:
20592 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20593 if (op0
== error_mark_node
)
20594 RETURN (error_mark_node
);
20595 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20596 if (op1
== error_mark_node
)
20597 RETURN (error_mark_node
);
20598 RETURN (fold_build_pointer_plus (op0
, op1
));
20602 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
20603 /*address_p=*/false));
20606 RETURN (tsubst_baselink (t
, current_nonlambda_class_type (),
20607 args
, complain
, in_decl
));
20610 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20611 args
, complain
, in_decl
);
20612 if (TREE_CODE (TREE_OPERAND (t
, 1)) == CALL_EXPR
20613 && (CALL_EXPR_FN (TREE_OPERAND (t
, 1))
20614 == ovl_op_identifier (ARRAY_REF
)))
20616 tree c
= TREE_OPERAND (t
, 1);
20617 releasing_vec index_exp_list
;
20618 tsubst_copy_and_build_call_args (c
, args
, complain
, in_decl
,
20622 if (vec_safe_length (index_exp_list
) == 1
20623 && !PACK_EXPANSION_P (index_exp_list
[0]))
20624 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20625 index_exp_list
[0], NULL
,
20626 complain
| decltype_flag
);
20628 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20629 NULL_TREE
, &index_exp_list
,
20630 complain
| decltype_flag
);
20633 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
20634 RECUR (TREE_OPERAND (t
, 1)),
20635 complain
|decltype_flag
));
20638 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
20639 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
20640 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20647 op1
= TREE_OPERAND (t
, 0);
20648 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
20649 op1
= TREE_TYPE (op1
);
20650 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
20651 && ALIGNOF_EXPR_STD_P (t
));
20654 /* When there are no ARGS, we are trying to evaluate a
20655 non-dependent expression from the parser. Trying to do
20656 the substitutions may not work. */
20658 op1
= TREE_TYPE (op1
);
20662 ++cp_unevaluated_operand
;
20663 ++c_inhibit_evaluation_warnings
;
20665 op1
= tsubst (op1
, args
, complain
, in_decl
);
20667 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
);
20668 --cp_unevaluated_operand
;
20669 --c_inhibit_evaluation_warnings
;
20672 r
= cxx_sizeof_or_alignof_type (input_location
,
20673 op1
, TREE_CODE (t
), std_alignof
,
20674 complain
& tf_error
);
20676 r
= cxx_sizeof_or_alignof_expr (input_location
,
20677 op1
, TREE_CODE (t
), std_alignof
,
20678 complain
& tf_error
);
20679 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
20681 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
20683 if (!processing_template_decl
&& TYPE_P (op1
))
20685 r
= build_min (SIZEOF_EXPR
, size_type_node
,
20686 build1 (NOP_EXPR
, op1
, error_mark_node
));
20687 SIZEOF_EXPR_TYPE_P (r
) = 1;
20690 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
20691 TREE_SIDE_EFFECTS (r
) = 0;
20692 TREE_READONLY (r
) = 1;
20694 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
20699 case AT_ENCODE_EXPR
:
20701 op1
= TREE_OPERAND (t
, 0);
20702 ++cp_unevaluated_operand
;
20703 ++c_inhibit_evaluation_warnings
;
20704 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
);
20705 --cp_unevaluated_operand
;
20706 --c_inhibit_evaluation_warnings
;
20707 RETURN (objc_build_encode_expr (op1
));
20710 case NOEXCEPT_EXPR
:
20711 op1
= TREE_OPERAND (t
, 0);
20712 ++cp_unevaluated_operand
;
20713 ++c_inhibit_evaluation_warnings
;
20714 ++cp_noexcept_operand
;
20715 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
);
20716 --cp_unevaluated_operand
;
20717 --c_inhibit_evaluation_warnings
;
20718 --cp_noexcept_operand
;
20719 RETURN (finish_noexcept_expr (op1
, complain
));
20723 warning_sentinel
s(warn_div_by_zero
);
20724 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
20725 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
20727 tree r
= build_x_modify_expr
20728 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
20729 templated_operator_saved_lookups (t
),
20730 complain
|decltype_flag
);
20731 /* TREE_NO_WARNING must be set if either the expression was
20732 parenthesized or it uses an operator such as >>= rather
20733 than plain assignment. In the former case, it was already
20734 set and must be copied. In the latter case,
20735 build_x_modify_expr sets it and it must not be reset
20737 if (warning_suppressed_p (t
, OPT_Wparentheses
))
20738 suppress_warning (r
, OPT_Wparentheses
);
20744 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20745 args
, complain
, in_decl
);
20746 /* Remember that there was a reference to this entity. */
20748 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
20749 RETURN (error_mark_node
);
20750 RETURN (build_x_arrow (input_location
, op1
, complain
));
20754 tree placement
= RECUR (TREE_OPERAND (t
, 0));
20755 tree init
= RECUR (TREE_OPERAND (t
, 3));
20756 vec
<tree
, va_gc
> *placement_vec
;
20757 vec
<tree
, va_gc
> *init_vec
;
20759 location_t loc
= EXPR_LOCATION (t
);
20761 if (placement
== NULL_TREE
)
20762 placement_vec
= NULL
;
20763 else if (placement
== error_mark_node
)
20764 RETURN (error_mark_node
);
20767 placement_vec
= make_tree_vector ();
20768 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
20769 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
20772 /* If there was an initializer in the original tree, but it
20773 instantiated to an empty list, then we should pass a
20774 non-NULL empty vector to tell build_new that it was an
20775 empty initializer() rather than no initializer. This can
20776 only happen when the initializer is a pack expansion whose
20777 parameter packs are of length zero. */
20778 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
20780 else if (init
== error_mark_node
)
20781 RETURN (error_mark_node
);
20784 init_vec
= make_tree_vector ();
20785 if (init
== void_node
)
20786 gcc_assert (init_vec
!= NULL
);
20789 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
20790 vec_safe_push (init_vec
, TREE_VALUE (init
));
20794 /* Avoid passing an enclosing decl to valid_array_size_p. */
20795 in_decl
= NULL_TREE
;
20797 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20798 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20799 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
20800 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
20803 if (placement_vec
!= NULL
)
20804 release_tree_vector (placement_vec
);
20805 if (init_vec
!= NULL
)
20806 release_tree_vector (init_vec
);
20813 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20814 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20815 RETURN (delete_sanity (input_location
, op0
, op1
,
20816 DELETE_EXPR_USE_VEC (t
),
20817 DELETE_EXPR_USE_GLOBAL (t
),
20821 case COMPOUND_EXPR
:
20823 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
20824 complain
& ~tf_decltype
, in_decl
);
20825 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
20827 RECUR (TREE_OPERAND (t
, 1)),
20828 templated_operator_saved_lookups (t
),
20829 complain
|decltype_flag
));
20835 unsigned int nargs
;
20840 function
= CALL_EXPR_FN (t
);
20841 /* Internal function with no arguments. */
20842 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
20845 /* When we parsed the expression, we determined whether or
20846 not Koenig lookup should be performed. */
20847 koenig_p
= KOENIG_LOOKUP_P (t
);
20848 if (function
== NULL_TREE
)
20851 qualified_p
= false;
20853 else if (TREE_CODE (function
) == SCOPE_REF
)
20855 qualified_p
= true;
20856 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
20858 /*address_p=*/false);
20861 && (identifier_p (function
)
20862 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20863 && identifier_p (TREE_OPERAND (function
, 0)))))
20865 /* Do nothing; calling tsubst_copy_and_build on an identifier
20866 would incorrectly perform unqualified lookup again.
20868 Note that we can also have an IDENTIFIER_NODE if the earlier
20869 unqualified lookup found a dependent local extern declaration
20870 (as per finish_call_expr); in that case koenig_p will be false
20871 and we do want to do the lookup again to find the substituted
20873 qualified_p
= false;
20875 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20876 /* Use tsubst_copy to substitute through the template arguments
20877 of the template-id without performing unqualified lookup of
20878 the template name. */
20879 function
= tsubst_copy (function
, args
, complain
, in_decl
);
20883 if (TREE_CODE (function
) == COMPONENT_REF
)
20885 tree op
= TREE_OPERAND (function
, 1);
20887 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20888 || (BASELINK_P (op
)
20889 && BASELINK_QUALIFIED_P (op
)));
20892 qualified_p
= false;
20894 if (TREE_CODE (function
) == ADDR_EXPR
20895 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20896 /* Avoid error about taking the address of a constructor. */
20897 function
= TREE_OPERAND (function
, 0);
20899 tsubst_flags_t subcomplain
= complain
;
20900 if (koenig_p
&& TREE_CODE (function
) == FUNCTION_DECL
)
20901 /* When KOENIG_P, we don't want to mark_used the callee before
20902 augmenting the overload set via ADL, so during this initial
20903 substitution we disable mark_used by setting tf_conv (68942). */
20904 subcomplain
|= tf_conv
;
20905 function
= tsubst_copy_and_build (function
, args
, subcomplain
, in_decl
);
20907 if (BASELINK_P (function
))
20908 qualified_p
= true;
20911 nargs
= call_expr_nargs (t
);
20912 releasing_vec call_args
;
20913 tsubst_copy_and_build_call_args (t
, args
, complain
, in_decl
,
20916 /* Stripped-down processing for a call in a thunk. Specifically, in
20917 the thunk template for a generic lambda. */
20918 if (call_from_lambda_thunk_p (t
))
20920 /* Now that we've expanded any packs, the number of call args
20921 might be different. */
20922 unsigned int cargs
= call_args
->length ();
20923 tree thisarg
= NULL_TREE
;
20924 if (TREE_CODE (function
) == COMPONENT_REF
)
20926 thisarg
= TREE_OPERAND (function
, 0);
20927 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20928 thisarg
= TREE_OPERAND (thisarg
, 0);
20929 function
= TREE_OPERAND (function
, 1);
20930 if (TREE_CODE (function
) == BASELINK
)
20931 function
= BASELINK_FUNCTIONS (function
);
20933 /* We aren't going to do normal overload resolution, so force the
20934 template-id to resolve. */
20935 function
= resolve_nondeduced_context (function
, complain
);
20936 for (unsigned i
= 0; i
< cargs
; ++i
)
20938 /* In a thunk, pass through args directly, without any
20940 tree arg
= (*call_args
)[i
];
20941 while (TREE_CODE (arg
) != PARM_DECL
)
20942 arg
= TREE_OPERAND (arg
, 0);
20943 (*call_args
)[i
] = arg
;
20947 /* If there are no other args, just push 'this'. */
20949 vec_safe_push (call_args
, thisarg
);
20952 /* Otherwise, shift the other args over to make room. */
20953 tree last
= (*call_args
)[cargs
- 1];
20954 vec_safe_push (call_args
, last
);
20955 for (int i
= cargs
- 1; i
> 0; --i
)
20956 (*call_args
)[i
] = (*call_args
)[i
- 1];
20957 (*call_args
)[0] = thisarg
;
20960 ret
= build_call_a (function
, call_args
->length (),
20961 call_args
->address ());
20962 /* The thunk location is not interesting. */
20963 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20964 CALL_FROM_THUNK_P (ret
) = true;
20965 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20966 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20971 /* We do not perform argument-dependent lookup if normal
20972 lookup finds a non-function, in accordance with the
20973 resolution of DR 218. */
20975 && ((is_overloaded_fn (function
)
20976 /* If lookup found a member function, the Koenig lookup is
20977 not appropriate, even if an unqualified-name was used
20978 to denote the function. */
20979 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20980 || identifier_p (function
)
20981 /* C++20 P0846: Lookup found nothing. */
20982 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20983 && identifier_p (TREE_OPERAND (function
, 0))))
20984 /* Only do this when substitution turns a dependent call
20985 into a non-dependent call. */
20986 && type_dependent_expression_p_push (t
)
20987 && !any_type_dependent_arguments_p (call_args
))
20988 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20990 if (function
!= NULL_TREE
20991 && (identifier_p (function
)
20992 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20993 && identifier_p (TREE_OPERAND (function
, 0))
20994 && !any_dependent_template_arguments_p (TREE_OPERAND
20996 && !any_type_dependent_arguments_p (call_args
))
20998 bool template_id_p
= (TREE_CODE (function
) == TEMPLATE_ID_EXPR
);
21000 function
= TREE_OPERAND (function
, 0);
21001 if (koenig_p
&& (complain
& tf_warning_or_error
))
21003 /* For backwards compatibility and good diagnostics, try
21004 the unqualified lookup again if we aren't in SFINAE
21006 tree unq
= tsubst_copy_and_build (function
, args
,
21007 complain
, in_decl
);
21008 if (unq
== error_mark_node
)
21009 RETURN (error_mark_node
);
21011 if (unq
!= function
)
21013 char const *const msg
21014 = G_("%qD was not declared in this scope, "
21015 "and no declarations were found by "
21016 "argument-dependent lookup at the point "
21017 "of instantiation");
21019 bool in_lambda
= (current_class_type
21020 && LAMBDA_TYPE_P (current_class_type
));
21021 /* In a lambda fn, we have to be careful to not
21022 introduce new this captures. Legacy code can't
21023 be using lambdas anyway, so it's ok to be
21024 stricter. Be strict with C++20 template-id ADL too.
21025 And be strict if we're already failing anyway. */
21026 bool strict
= in_lambda
|| template_id_p
|| seen_error();
21029 error_at (cp_expr_loc_or_input_loc (t
),
21032 diag
= permerror (cp_expr_loc_or_input_loc (t
),
21038 if (INDIRECT_REF_P (fn
))
21039 fn
= TREE_OPERAND (fn
, 0);
21040 if (is_overloaded_fn (fn
))
21041 fn
= get_first_fn (fn
);
21044 /* Can't say anything more. */;
21045 else if (DECL_CLASS_SCOPE_P (fn
))
21047 location_t loc
= cp_expr_loc_or_input_loc (t
);
21049 "declarations in dependent base %qT are "
21050 "not found by unqualified lookup",
21051 DECL_CLASS_CONTEXT (fn
));
21052 if (current_class_ptr
)
21054 "use %<this->%D%> instead", function
);
21057 "use %<%T::%D%> instead",
21058 current_class_name
, function
);
21061 inform (DECL_SOURCE_LOCATION (fn
),
21062 "%qD declared here, later in the "
21063 "translation unit", fn
);
21065 RETURN (error_mark_node
);
21071 if (identifier_p (function
))
21073 if (complain
& tf_error
)
21074 unqualified_name_lookup_error (function
);
21075 RETURN (error_mark_node
);
21079 /* Remember that there was a reference to this entity. */
21080 if (function
!= NULL_TREE
)
21082 tree inner
= function
;
21083 if (TREE_CODE (inner
) == ADDR_EXPR
21084 && TREE_CODE (TREE_OPERAND (inner
, 0)) == FUNCTION_DECL
)
21085 /* We should already have called mark_used when taking the
21086 address of this function, but do so again anyway to make
21087 sure it's odr-used: at worst this is a no-op, but if we
21088 obtained this FUNCTION_DECL as part of ahead-of-time overload
21089 resolution then that call to mark_used wouldn't have marked it
21090 odr-used yet (53164). */
21091 inner
= TREE_OPERAND (inner
, 0);
21093 && !mark_used (inner
, complain
) && !(complain
& tf_error
))
21094 RETURN (error_mark_node
);
21097 if (!maybe_fold_fn_template_args (function
, complain
))
21098 return error_mark_node
;
21100 /* Put back tf_decltype for the actual call. */
21101 complain
|= decltype_flag
;
21103 if (function
== NULL_TREE
)
21104 switch (CALL_EXPR_IFN (t
))
21107 gcc_assert (nargs
== 1);
21108 if (vec_safe_length (call_args
) != 1)
21110 error_at (cp_expr_loc_or_input_loc (t
),
21111 "wrong number of arguments to "
21112 "%<__builtin_launder%>");
21113 ret
= error_mark_node
;
21116 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
21117 (*call_args
)[0], complain
);
21120 case IFN_VEC_CONVERT
:
21121 gcc_assert (nargs
== 1);
21122 if (vec_safe_length (call_args
) != 1)
21124 error_at (cp_expr_loc_or_input_loc (t
),
21125 "wrong number of arguments to "
21126 "%<__builtin_convertvector%>");
21127 ret
= error_mark_node
;
21130 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
21131 tsubst (TREE_TYPE (t
), args
,
21132 complain
, in_decl
),
21134 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
21138 case IFN_SHUFFLEVECTOR
:
21140 ret
= build_x_shufflevector (input_location
, call_args
,
21142 if (ret
!= error_mark_node
)
21148 gcc_assert (nargs
== 1);
21149 if (vec_safe_length (call_args
) != 1)
21151 error_at (cp_expr_loc_or_input_loc (t
),
21152 "wrong number of arguments to "
21153 "%<assume%> attribute");
21154 ret
= error_mark_node
;
21158 tree
&arg
= (*call_args
)[0];
21159 if (!type_dependent_expression_p (arg
))
21160 arg
= contextual_conv_bool (arg
, tf_warning_or_error
);
21161 if (error_operand_p (arg
))
21163 ret
= error_mark_node
;
21166 ret
= build_assume_call (EXPR_LOCATION (t
), arg
);
21172 /* Unsupported internal function with arguments. */
21173 gcc_unreachable ();
21175 else if (TREE_CODE (function
) == OFFSET_REF
21176 || TREE_CODE (function
) == DOTSTAR_EXPR
21177 || TREE_CODE (function
) == MEMBER_REF
)
21178 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
21180 else if (TREE_CODE (function
) == COMPONENT_REF
)
21182 tree instance
= TREE_OPERAND (function
, 0);
21183 tree fn
= TREE_OPERAND (function
, 1);
21185 if (processing_template_decl
21186 && (type_dependent_expression_p (instance
)
21187 || (!BASELINK_P (fn
)
21188 && TREE_CODE (fn
) != FIELD_DECL
)
21189 || type_dependent_expression_p (fn
)
21190 || any_type_dependent_arguments_p (call_args
)))
21191 ret
= build_min_nt_call_vec (function
, call_args
);
21192 else if (!BASELINK_P (fn
))
21193 ret
= finish_call_expr (function
, &call_args
,
21194 /*disallow_virtual=*/false,
21195 /*koenig_p=*/false,
21198 ret
= (build_new_method_call
21200 &call_args
, NULL_TREE
,
21201 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
21205 else if (concept_check_p (function
))
21207 /* FUNCTION is a template-id referring to a concept definition. */
21208 tree id
= unpack_concept_check (function
);
21209 tree tmpl
= TREE_OPERAND (id
, 0);
21210 tree args
= TREE_OPERAND (id
, 1);
21212 /* Calls to standard and variable concepts should have been
21213 previously diagnosed. */
21214 gcc_assert (function_concept_p (tmpl
));
21216 /* Ensure the result is wrapped as a call expression. */
21217 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
21220 ret
= finish_call_expr (function
, &call_args
,
21221 /*disallow_virtual=*/qualified_p
,
21225 if (ret
!= error_mark_node
)
21227 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
21228 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
21229 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
21230 if (op
|| ord
|| rev
)
21231 if (tree call
= extract_call_expr (ret
))
21233 CALL_EXPR_OPERATOR_SYNTAX (call
) = op
;
21234 CALL_EXPR_ORDERED_ARGS (call
) = ord
;
21235 CALL_EXPR_REVERSE_ARGS (call
) = rev
;
21237 if (warning_suppressed_p (t
, OPT_Wpessimizing_move
))
21238 /* This also suppresses -Wredundant-move. */
21239 suppress_warning (ret
, OPT_Wpessimizing_move
);
21247 tree cond
= RECUR (TREE_OPERAND (t
, 0));
21248 cond
= mark_rvalue_use (cond
);
21249 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
21252 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
21254 if (integer_zerop (folded_cond
))
21256 ++c_inhibit_evaluation_warnings
;
21257 exp1
= RECUR (TREE_OPERAND (t
, 1));
21258 --c_inhibit_evaluation_warnings
;
21259 exp2
= RECUR (TREE_OPERAND (t
, 2));
21263 exp1
= RECUR (TREE_OPERAND (t
, 1));
21264 ++c_inhibit_evaluation_warnings
;
21265 exp2
= RECUR (TREE_OPERAND (t
, 2));
21266 --c_inhibit_evaluation_warnings
;
21268 cond
= folded_cond
;
21272 exp1
= RECUR (TREE_OPERAND (t
, 1));
21273 exp2
= RECUR (TREE_OPERAND (t
, 2));
21276 warning_sentinel
s(warn_duplicated_branches
);
21277 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
21278 cond
, exp1
, exp2
, complain
));
21281 case PSEUDO_DTOR_EXPR
:
21283 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21284 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21285 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
21286 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
21291 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
21293 case COMPONENT_REF
:
21300 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
21301 args
, complain
, in_decl
);
21302 /* Remember that there was a reference to this entity. */
21303 if (DECL_P (object
)
21304 && !mark_used (object
, complain
) && !(complain
& tf_error
))
21305 RETURN (error_mark_node
);
21306 object_type
= TREE_TYPE (object
);
21308 member
= TREE_OPERAND (t
, 1);
21309 if (BASELINK_P (member
))
21310 member
= tsubst_baselink (member
,
21311 non_reference (TREE_TYPE (object
)),
21312 args
, complain
, in_decl
);
21314 member
= tsubst_copy (member
, args
, complain
, in_decl
);
21315 if (member
== error_mark_node
)
21316 RETURN (error_mark_node
);
21318 if (object_type
&& TYPE_PTRMEMFUNC_P (object_type
)
21319 && TREE_CODE (member
) == FIELD_DECL
)
21321 r
= build_ptrmemfunc_access_expr (object
, DECL_NAME (member
));
21324 else if (TREE_CODE (member
) == FIELD_DECL
)
21326 r
= finish_non_static_data_member (member
, object
, NULL_TREE
,
21328 if (TREE_CODE (r
) == COMPONENT_REF
)
21329 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21332 else if (type_dependent_expression_p (object
))
21333 /* We can't do much here. */;
21334 else if (!CLASS_TYPE_P (object_type
))
21336 if (scalarish_type_p (object_type
))
21338 tree s
= NULL_TREE
;
21339 tree dtor
= member
;
21341 if (TREE_CODE (dtor
) == SCOPE_REF
)
21343 s
= TREE_OPERAND (dtor
, 0);
21344 dtor
= TREE_OPERAND (dtor
, 1);
21346 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
21348 dtor
= TREE_OPERAND (dtor
, 0);
21350 RETURN (finish_pseudo_destructor_expr
21351 (object
, s
, dtor
, input_location
));
21355 else if (TREE_CODE (member
) == SCOPE_REF
21356 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
21358 /* Lookup the template functions now that we know what the
21360 tree scope
= TREE_OPERAND (member
, 0);
21361 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
21362 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
21363 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
21364 /*complain=*/false);
21365 if (BASELINK_P (member
))
21367 BASELINK_FUNCTIONS (member
)
21368 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
21370 member
= (adjust_result_of_qualified_name_lookup
21371 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
21376 qualified_name_lookup_error (scope
, tmpl
, member
,
21378 RETURN (error_mark_node
);
21381 else if (TREE_CODE (member
) == SCOPE_REF
21382 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
21383 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
21385 if (complain
& tf_error
)
21387 if (TYPE_P (TREE_OPERAND (member
, 0)))
21388 error ("%qT is not a class or namespace",
21389 TREE_OPERAND (member
, 0));
21391 error ("%qD is not a class or namespace",
21392 TREE_OPERAND (member
, 0));
21394 RETURN (error_mark_node
);
21397 r
= finish_class_member_access_expr (object
, member
,
21398 /*template_p=*/false,
21400 if (TREE_CODE (r
) == COMPONENT_REF
)
21401 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21406 RETURN (build_throw
21407 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
21411 vec
<constructor_elt
, va_gc
> *n
;
21412 constructor_elt
*ce
;
21413 unsigned HOST_WIDE_INT idx
;
21414 bool process_index_p
;
21416 bool need_copy_p
= false;
21419 tsubst_flags_t tcomplain
= complain
;
21420 if (COMPOUND_LITERAL_P (t
))
21421 tcomplain
|= tf_tst_ok
;
21422 tree type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
21423 if (type
== error_mark_node
)
21424 RETURN (error_mark_node
);
21426 /* We do not want to process the index of aggregate
21427 initializers as they are identifier nodes which will be
21428 looked up by digest_init. */
21429 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
21431 if (null_member_pointer_value_p (t
))
21433 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
21437 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
21438 newlen
= vec_safe_length (n
);
21439 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
21441 if (ce
->index
&& process_index_p
21442 /* An identifier index is looked up in the type
21443 being initialized, not the current scope. */
21444 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
21445 ce
->index
= RECUR (ce
->index
);
21447 if (PACK_EXPANSION_P (ce
->value
))
21449 /* Substitute into the pack expansion. */
21450 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
21453 if (ce
->value
== error_mark_node
21454 || PACK_EXPANSION_P (ce
->value
))
21456 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
21457 /* Just move the argument into place. */
21458 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
21461 /* Update the length of the final CONSTRUCTOR
21462 arguments vector, and note that we will need to
21464 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
21465 need_copy_p
= true;
21469 ce
->value
= RECUR (ce
->value
);
21474 vec
<constructor_elt
, va_gc
> *old_n
= n
;
21476 vec_alloc (n
, newlen
);
21477 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
21479 if (TREE_CODE (ce
->value
) == TREE_VEC
)
21481 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
21482 for (i
= 0; i
< len
; ++i
)
21483 CONSTRUCTOR_APPEND_ELT (n
, 0,
21484 TREE_VEC_ELT (ce
->value
, i
));
21487 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
21491 r
= build_constructor (init_list_type_node
, n
);
21492 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
21493 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
21494 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
21496 if (TREE_HAS_CONSTRUCTOR (t
))
21498 fcl_t cl
= fcl_functional
;
21499 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
21501 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
21504 TREE_TYPE (r
) = type
;
21510 tree operand_0
= TREE_OPERAND (t
, 0);
21511 if (TYPE_P (operand_0
))
21513 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
21514 RETURN (get_typeid (operand_0
, complain
));
21518 operand_0
= RECUR (operand_0
);
21519 RETURN (build_typeid (operand_0
, complain
));
21530 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
21531 /* ??? We're doing a subset of finish_id_expression here. */
21532 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
21533 /* Replace an evaluated use of the thread_local variable with
21534 a call to its wrapper. */
21536 else if (outer_automatic_var_p (r
))
21537 r
= process_outer_var_ref (r
, complain
);
21539 if (!TYPE_REF_P (TREE_TYPE (t
)))
21540 /* If the original type was a reference, we'll be wrapped in
21541 the appropriate INDIRECT_REF. */
21542 r
= convert_from_reference (r
);
21548 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21549 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21550 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
21553 case OFFSETOF_EXPR
:
21556 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
,
21557 complain
, in_decl
);
21558 RETURN (finish_offsetof (object_ptr
,
21559 RECUR (TREE_OPERAND (t
, 0)),
21560 EXPR_LOCATION (t
)));
21563 case ADDRESSOF_EXPR
:
21564 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
21565 RECUR (TREE_OPERAND (t
, 0)), complain
));
21569 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
21570 complain
, in_decl
);
21571 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
21572 complain
, in_decl
);
21573 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
21574 TRAIT_EXPR_KIND (t
), type1
, type2
));
21579 tree old_stmt_expr
= cur_stmt_expr
;
21580 tree stmt_expr
= begin_stmt_expr ();
21582 cur_stmt_expr
= stmt_expr
;
21583 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
);
21584 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
21585 cur_stmt_expr
= old_stmt_expr
;
21587 /* If the resulting list of expression statement is empty,
21588 fold it further into void_node. */
21589 if (empty_expr_stmt_p (stmt_expr
))
21590 stmt_expr
= void_node
;
21592 RETURN (stmt_expr
);
21597 if (complain
& tf_partial
)
21599 /* We don't have a full set of template arguments yet; don't touch
21600 the lambda at all. */
21601 gcc_assert (processing_template_decl
);
21604 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
21606 RETURN (build_lambda_object (r
));
21609 case TRANSACTION_EXPR
:
21610 RETURN (tsubst_expr (t
, args
, complain
, in_decl
));
21613 if (REF_PARENTHESIZED_P (t
))
21614 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
21616 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21618 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21619 RETURN (build1_loc (input_location
, PAREN_EXPR
,
21620 TREE_TYPE (op0
), op0
));
21623 case VEC_PERM_EXPR
:
21625 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21626 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21627 tree op2
= RECUR (TREE_OPERAND (t
, 2));
21628 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
21632 case REQUIRES_EXPR
:
21634 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
21639 /* No need to substitute further, a RANGE_EXPR will always be built
21640 with constant operands. */
21643 case NON_LVALUE_EXPR
:
21644 case VIEW_CONVERT_EXPR
:
21646 tree op
= RECUR (TREE_OPERAND (t
, 0));
21648 if (location_wrapper_p (t
))
21649 /* We need to do this here as well as in tsubst_copy so we get the
21650 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21651 RETURN (maybe_wrap_with_location (op
, EXPR_LOCATION (t
)));
21653 gcc_checking_assert (TREE_CODE (t
) == VIEW_CONVERT_EXPR
);
21654 if (REF_PARENTHESIZED_P (t
))
21655 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
21656 RETURN (finish_parenthesized_expr (op
));
21658 /* Otherwise, we're dealing with a wrapper to make a C++20 template
21659 parameter object const. */
21660 if (TREE_TYPE (op
) == NULL_TREE
21661 || !CP_TYPE_CONST_P (TREE_TYPE (op
)))
21663 /* The template argument is not const, presumably because
21664 it is still dependent, and so not the const template parm
21666 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21667 if (TREE_CODE (op
) == CONSTRUCTOR
21668 || TREE_CODE (op
) == IMPLICIT_CONV_EXPR
)
21670 /* Don't add a wrapper to these. */
21671 op
= copy_node (op
);
21672 TREE_TYPE (op
) = type
;
21675 /* Do add a wrapper otherwise (in particular, if op is
21676 another TEMPLATE_PARM_INDEX). */
21677 op
= build1 (VIEW_CONVERT_EXPR
, type
, op
);
21683 /* Handle Objective-C++ constructs, if appropriate. */
21686 = objcp_tsubst_copy_and_build (t
, args
, complain
, in_decl
);
21690 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
21696 input_location
= save_loc
;
21700 /* Verify that the instantiated ARGS are valid. For type arguments,
21701 make sure that the type's linkage is ok. For non-type arguments,
21702 make sure they are constants if they are integral or enumerations.
21703 Emit an error under control of COMPLAIN, and return TRUE on error. */
21706 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
21708 if (dependent_template_arg_p (t
))
21710 if (ARGUMENT_PACK_P (t
))
21712 tree vec
= ARGUMENT_PACK_ARGS (t
);
21713 int len
= TREE_VEC_LENGTH (vec
);
21714 bool result
= false;
21717 for (i
= 0; i
< len
; ++i
)
21718 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
21722 else if (TYPE_P (t
))
21724 /* [basic.link]: A name with no linkage (notably, the name
21725 of a class or enumeration declared in a local scope)
21726 shall not be used to declare an entity with linkage.
21727 This implies that names with no linkage cannot be used as
21730 DR 757 relaxes this restriction for C++0x. */
21731 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
21732 : no_linkage_check (t
, /*relaxed_p=*/false));
21736 /* DR 488 makes use of a type with no linkage cause
21737 type deduction to fail. */
21738 if (complain
& tf_error
)
21740 if (TYPE_UNNAMED_P (nt
))
21741 error ("%qT is/uses unnamed type", t
);
21743 error ("template argument for %qD uses local type %qT",
21748 /* In order to avoid all sorts of complications, we do not
21749 allow variably-modified types as template arguments. */
21750 else if (variably_modified_type_p (t
, NULL_TREE
))
21752 if (complain
& tf_error
)
21753 error ("%qT is a variably modified type", t
);
21757 /* Class template and alias template arguments should be OK. */
21758 else if (DECL_TYPE_TEMPLATE_P (t
))
21760 /* A non-type argument of integral or enumerated type must be a
21762 else if (TREE_TYPE (t
)
21763 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
21764 && !REFERENCE_REF_P (t
)
21765 && !TREE_CONSTANT (t
))
21767 if (complain
& tf_error
)
21768 error ("integral expression %qE is not constant", t
);
21775 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
21777 int ix
, len
= DECL_NTPARMS (tmpl
);
21778 bool result
= false;
21780 for (ix
= 0; ix
!= len
; ix
++)
21782 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
21785 if (result
&& (complain
& tf_error
))
21786 error (" trying to instantiate %qD", tmpl
);
21790 /* We're out of SFINAE context now, so generate diagnostics for the access
21791 errors we saw earlier when instantiating D from TMPL and ARGS. */
21794 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
21796 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21797 tree type
= TREE_TYPE (pattern
);
21798 location_t loc
= input_location
;
21800 push_access_scope (d
);
21801 push_deferring_access_checks (dk_no_deferred
);
21802 input_location
= DECL_SOURCE_LOCATION (pattern
);
21803 tsubst (type
, args
, tf_warning_or_error
, d
);
21804 input_location
= loc
;
21805 pop_deferring_access_checks ();
21806 pop_access_scope (d
);
21809 /* Instantiate the indicated variable, function, or alias template TMPL with
21810 the template arguments in TARG_PTR. */
21813 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21815 auto_timevar
tv (TV_TEMPLATE_INST
);
21817 tree targ_ptr
= orig_args
;
21821 bool access_ok
= true;
21823 if (tmpl
== error_mark_node
)
21824 return error_mark_node
;
21826 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
21829 lazy_load_pendings (tmpl
);
21831 /* If this function is a clone, handle it specially. */
21832 if (DECL_CLONED_FUNCTION_P (tmpl
))
21837 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21838 DECL_CLONED_FUNCTION. */
21839 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
21840 targ_ptr
, complain
);
21841 if (spec
== error_mark_node
)
21842 return error_mark_node
;
21844 /* Look for the clone. */
21845 FOR_EACH_CLONE (clone
, spec
)
21846 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
21848 /* We should always have found the clone by now. */
21849 gcc_unreachable ();
21853 if (targ_ptr
== error_mark_node
)
21854 return error_mark_node
;
21856 /* Check to see if we already have this specialization. */
21857 gen_tmpl
= most_general_template (tmpl
);
21858 if (TMPL_ARGS_DEPTH (targ_ptr
)
21859 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
21860 /* targ_ptr only has the innermost template args, so add the outer ones
21861 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21862 the case of a non-dependent call within a template definition). */
21863 targ_ptr
= (add_outermost_template_args
21864 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
21867 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21868 but it doesn't seem to be on the hot path. */
21869 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
21871 gcc_checking_assert (tmpl
== gen_tmpl
21873 = retrieve_specialization (tmpl
, orig_args
, 0))
21875 || fndecl
== NULL_TREE
);
21877 if (spec
!= NULL_TREE
)
21879 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
21881 if (complain
& tf_error
)
21882 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
21883 return error_mark_node
;
21888 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
21890 return error_mark_node
;
21892 /* We are building a FUNCTION_DECL, during which the access of its
21893 parameters and return types have to be checked. However this
21894 FUNCTION_DECL which is the desired context for access checking
21895 is not built yet. We solve this chicken-and-egg problem by
21896 deferring all checks until we have the FUNCTION_DECL. */
21897 push_deferring_access_checks (dk_deferred
);
21899 /* Instantiation of the function happens in the context of the function
21900 template, not the context of the overload resolution we're doing. */
21901 push_to_top_level ();
21902 /* If there are dependent arguments, e.g. because we're doing partial
21903 ordering, make sure processing_template_decl stays set. */
21904 if (uses_template_parms (targ_ptr
))
21905 ++processing_template_decl
;
21906 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21909 if (!uses_template_parms (DECL_CONTEXT (tmpl
)))
21910 /* If the context of the partially instantiated template is
21911 already non-dependent, then we might as well use it. */
21912 ctx
= DECL_CONTEXT (tmpl
);
21914 ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
21915 complain
, gen_tmpl
, true);
21916 push_nested_class (ctx
);
21919 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
21921 fndecl
= NULL_TREE
;
21922 if (VAR_P (pattern
))
21924 /* We need to determine if we're using a partial or explicit
21925 specialization now, because the type of the variable could be
21927 tree tid
= lookup_template_variable (tmpl
, targ_ptr
);
21928 tree elt
= most_specialized_partial_spec (tid
, complain
);
21929 if (elt
== error_mark_node
)
21930 pattern
= error_mark_node
;
21933 tree partial_tmpl
= TREE_VALUE (elt
);
21934 tree partial_args
= TREE_PURPOSE (elt
);
21935 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
21936 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
21940 /* Substitute template parameters to obtain the specialization. */
21941 if (fndecl
== NULL_TREE
)
21942 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
21943 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21944 pop_nested_class ();
21945 pop_from_top_level ();
21947 if (fndecl
== error_mark_node
)
21949 pop_deferring_access_checks ();
21950 return error_mark_node
;
21953 /* The DECL_TI_TEMPLATE should always be the immediate parent
21954 template, not the most general template. */
21955 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
21956 DECL_TI_ARGS (fndecl
) = targ_ptr
;
21958 set_instantiating_module (fndecl
);
21960 /* Now we know the specialization, compute access previously
21961 deferred. Do no access control for inheriting constructors,
21962 as we already checked access for the inherited constructor. */
21963 if (!(flag_new_inheriting_ctors
21964 && DECL_INHERITED_CTOR (fndecl
)))
21966 push_access_scope (fndecl
);
21967 if (!perform_deferred_access_checks (complain
))
21969 pop_access_scope (fndecl
);
21971 pop_deferring_access_checks ();
21973 /* If we've just instantiated the main entry point for a function,
21974 instantiate all the alternate entry points as well. We do this
21975 by cloning the instantiation of the main entry point, not by
21976 instantiating the template clones. */
21977 if (tree chain
= DECL_CHAIN (gen_tmpl
))
21978 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
21979 clone_cdtor (fndecl
, /*update_methods=*/false);
21983 if (!(complain
& tf_error
))
21985 /* Remember to reinstantiate when we're out of SFINAE so the user
21986 can see the errors. */
21987 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
21989 return error_mark_node
;
21995 /* Instantiate the alias template TMPL with ARGS. Also push a template
21996 instantiation level, which instantiate_template doesn't do because
21997 functions and variables have sufficient context established by the
22001 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
22003 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
22004 return error_mark_node
;
22006 args
= coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
22007 args
, tmpl
, complain
);
22009 /* FIXME check for satisfaction in check_instantiated_args. */
22011 && !any_dependent_template_arguments_p (args
)
22012 && !constraints_satisfied_p (tmpl
, args
))
22014 if (complain
& tf_error
)
22016 auto_diagnostic_group d
;
22017 error ("template constraint failure for %qD", tmpl
);
22018 diagnose_constraints (input_location
, tmpl
, args
);
22020 return error_mark_node
;
22023 if (!push_tinst_level (tmpl
, args
))
22024 return error_mark_node
;
22025 tree r
= instantiate_template (tmpl
, args
, complain
);
22026 pop_tinst_level ();
22028 if (tree d
= dependent_alias_template_spec_p (TREE_TYPE (r
), nt_opaque
))
22030 /* An alias template specialization can be dependent
22031 even if its underlying type is not. */
22032 TYPE_DEPENDENT_P (d
) = true;
22033 TYPE_DEPENDENT_P_VALID (d
) = true;
22034 /* Sometimes a dependent alias spec is equivalent to its expansion,
22035 sometimes not. So always use structural_comptypes. */
22036 SET_TYPE_STRUCTURAL_EQUALITY (d
);
22042 /* PARM is a template parameter pack for FN. Returns true iff
22043 PARM is used in a deducible way in the argument list of FN. */
22046 pack_deducible_p (tree parm
, tree fn
)
22048 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
22049 for (; t
; t
= TREE_CHAIN (t
))
22051 tree type
= TREE_VALUE (t
);
22053 if (!PACK_EXPANSION_P (type
))
22055 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
22056 packs
; packs
= TREE_CHAIN (packs
))
22057 if (template_args_equal (TREE_VALUE (packs
), parm
))
22059 /* The template parameter pack is used in a function parameter
22060 pack. If this is the end of the parameter list, the
22061 template parameter pack is deducible. */
22062 if (TREE_CHAIN (t
) == void_list_node
)
22065 /* Otherwise, not. Well, it could be deduced from
22066 a non-pack parameter, but doing so would end up with
22067 a deduction mismatch, so don't bother. */
22071 /* The template parameter pack isn't used in any function parameter
22072 packs, but it might be used deeper, e.g. tuple<Args...>. */
22076 /* Subroutine of fn_type_unification: check non-dependent parms for
22080 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
22081 tree fn
, unification_kind_t strict
, int flags
,
22082 struct conversion
**convs
, bool explain_p
)
22084 /* Non-constructor methods need to leave a conversion for 'this', which
22085 isn't included in nargs here. */
22086 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
22087 && !DECL_CONSTRUCTOR_P (fn
));
22089 for (unsigned ia
= 0;
22090 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
22092 tree parm
= TREE_VALUE (parms
);
22094 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22095 && (!TREE_CHAIN (parms
)
22096 || TREE_CHAIN (parms
) == void_list_node
))
22097 /* For a function parameter pack that occurs at the end of the
22098 parameter-declaration-list, the type A of each remaining
22099 argument of the call is compared with the type P of the
22100 declarator-id of the function parameter pack. */
22103 parms
= TREE_CHAIN (parms
);
22105 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22106 /* For a function parameter pack that does not occur at the
22107 end of the parameter-declaration-list, the type of the
22108 parameter pack is a non-deduced context. */
22111 if (!uses_template_parms (parm
))
22113 tree arg
= args
[ia
];
22114 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
22115 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
22117 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
22118 conv_p
, explain_p
))
22128 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
22129 NARGS elements of the arguments that are being used when calling
22130 it. TARGS is a vector into which the deduced template arguments
22133 Returns either a FUNCTION_DECL for the matching specialization of FN or
22134 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
22135 true, diagnostics will be printed to explain why it failed.
22137 If FN is a conversion operator, or we are trying to produce a specific
22138 specialization, RETURN_TYPE is the return type desired.
22140 The EXPLICIT_TARGS are explicit template arguments provided via a
22143 The parameter STRICT is one of:
22146 We are deducing arguments for a function call, as in
22147 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
22148 deducing arguments for a call to the result of a conversion
22149 function template, as in [over.call.object].
22152 We are deducing arguments for a conversion function, as in
22153 [temp.deduct.conv].
22156 We are deducing arguments when doing an explicit instantiation
22157 as in [temp.explicit], when determining an explicit specialization
22158 as in [temp.expl.spec], or when taking the address of a function
22159 template, as in [temp.deduct.funcaddr]. */
22162 fn_type_unification (tree fn
,
22163 tree explicit_targs
,
22166 unsigned int nargs
,
22168 unification_kind_t strict
,
22170 struct conversion
**convs
,
22176 tree decl
= NULL_TREE
;
22177 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22179 static int deduction_depth
;
22180 /* type_unification_real will pass back any access checks from default
22181 template argument substitution. */
22182 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
22183 /* We don't have all the template args yet. */
22184 bool incomplete
= true;
22187 if (flag_new_inheriting_ctors
)
22188 fn
= strip_inheriting_ctors (fn
);
22190 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
22191 tree r
= error_mark_node
;
22193 tree full_targs
= targs
;
22194 if (TMPL_ARGS_DEPTH (targs
)
22195 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
22196 full_targs
= (add_outermost_template_args
22197 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
22201 complain
|= tf_decltype
;
22203 /* In C++0x, it's possible to have a function template whose type depends
22204 on itself recursively. This is most obvious with decltype, but can also
22205 occur with enumeration scope (c++/48969). So we need to catch infinite
22206 recursion and reject the substitution at deduction time; this function
22207 will return error_mark_node for any repeated substitution.
22209 This also catches excessive recursion such as when f<N> depends on
22210 f<N-1> across all integers, and returns error_mark_node for all the
22211 substitutions back up to the initial one.
22213 This is, of course, not reentrant. */
22214 if (excessive_deduction_depth
)
22215 return error_mark_node
;
22218 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
22220 fntype
= TREE_TYPE (fn
);
22221 if (explicit_targs
)
22225 The specified template arguments must match the template
22226 parameters in kind (i.e., type, nontype, template), and there
22227 must not be more arguments than there are parameters;
22228 otherwise type deduction fails.
22230 Nontype arguments must match the types of the corresponding
22231 nontype template parameters, or must be convertible to the
22232 types of the corresponding nontype parameters as specified in
22233 _temp.arg.nontype_, otherwise type deduction fails.
22235 All references in the function type of the function template
22236 to the corresponding template parameters are replaced by the
22237 specified template argument values. If a substitution in a
22238 template parameter or in the function type of the function
22239 template results in an invalid type, type deduction fails. */
22240 int i
, len
= TREE_VEC_LENGTH (tparms
);
22241 location_t loc
= input_location
;
22242 incomplete
= false;
22244 if (explicit_targs
== error_mark_node
)
22247 if (TMPL_ARGS_DEPTH (explicit_targs
)
22248 < TMPL_ARGS_DEPTH (full_targs
))
22249 explicit_targs
= add_outermost_template_args (full_targs
,
22252 /* Adjust any explicit template arguments before entering the
22253 substitution context. */
22255 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
22256 complain
|tf_partial
,
22257 /*require_all_args=*/false));
22258 if (explicit_targs
== error_mark_node
)
22261 /* Substitute the explicit args into the function type. This is
22262 necessary so that, for instance, explicitly declared function
22263 arguments can match null pointed constants. If we were given
22264 an incomplete set of explicit args, we must not do semantic
22265 processing during substitution as we could create partial
22267 for (i
= 0; i
< len
; i
++)
22269 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
22270 bool parameter_pack
= false;
22271 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
22273 /* Dig out the actual parm. */
22274 if (TREE_CODE (parm
) == TYPE_DECL
22275 || TREE_CODE (parm
) == TEMPLATE_DECL
)
22277 parm
= TREE_TYPE (parm
);
22278 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
22280 else if (TREE_CODE (parm
) == PARM_DECL
)
22282 parm
= DECL_INITIAL (parm
);
22283 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
22286 if (targ
== NULL_TREE
)
22287 /* No explicit argument for this template parameter. */
22289 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
22291 /* Mark the argument pack as "incomplete". We could
22292 still deduce more arguments during unification.
22293 We remove this mark in type_unification_real. */
22294 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
22295 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
22296 = ARGUMENT_PACK_ARGS (targ
);
22298 /* We have some incomplete argument packs. */
22305 if (!push_tinst_level (fn
, explicit_targs
))
22307 excessive_deduction_depth
= true;
22310 ++processing_template_decl
;
22311 input_location
= DECL_SOURCE_LOCATION (fn
);
22312 /* Ignore any access checks; we'll see them again in
22313 instantiate_template and they might have the wrong
22314 access path at this point. */
22315 push_deferring_access_checks (dk_deferred
);
22316 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
22317 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
22318 pop_deferring_access_checks ();
22319 input_location
= loc
;
22320 --processing_template_decl
;
22321 pop_tinst_level ();
22323 if (fntype
== error_mark_node
)
22327 /* Place the explicitly specified arguments in TARGS. */
22328 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
22329 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
22330 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
22331 if (!incomplete
&& CHECKING_P
22332 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22333 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22334 (targs
, NUM_TMPL_ARGS (explicit_targs
));
22337 if (return_type
&& strict
!= DEDUCE_CALL
)
22339 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
22340 new_args
[0] = return_type
;
22341 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
22349 /* Never do unification on the 'this' parameter. */
22350 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
22352 if (return_type
&& strict
== DEDUCE_CALL
)
22354 /* We're deducing for a call to the result of a template conversion
22355 function. The parms we really want are in return_type. */
22356 if (INDIRECT_TYPE_P (return_type
))
22357 return_type
= TREE_TYPE (return_type
);
22358 parms
= TYPE_ARG_TYPES (return_type
);
22360 else if (return_type
)
22362 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
22365 /* We allow incomplete unification without an error message here
22366 because the standard doesn't seem to explicitly prohibit it. Our
22367 callers must be ready to deal with unification failures in any
22370 /* If we aren't explaining yet, push tinst context so we can see where
22371 any errors (e.g. from class instantiations triggered by instantiation
22372 of default template arguments) come from. If we are explaining, this
22373 context is redundant. */
22374 if (!explain_p
&& !push_tinst_level (fn
, targs
))
22376 excessive_deduction_depth
= true;
22380 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22381 full_targs
, parms
, args
, nargs
, /*subr=*/0,
22382 strict
, &checks
, explain_p
);
22384 pop_tinst_level ();
22388 /* Now that we have bindings for all of the template arguments,
22389 ensure that the arguments deduced for the template template
22390 parameters have compatible template parameter lists. We cannot
22391 check this property before we have deduced all template
22392 arguments, because the template parameter types of a template
22393 template parameter might depend on prior template parameters
22394 deduced after the template template parameter. The following
22395 ill-formed example illustrates this issue:
22397 template<typename T, template<T> class C> void f(C<5>, T);
22399 template<int N> struct X {};
22402 f(X<5>(), 5l); // error: template argument deduction fails
22405 The template parameter list of 'C' depends on the template type
22406 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22407 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22408 time that we deduce 'C'. */
22409 if (!template_template_parm_bindings_ok_p
22410 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
22412 unify_inconsistent_template_template_parameters (explain_p
);
22418 /* CWG2369: Check satisfaction before non-deducible conversions. */
22419 if (!constraints_satisfied_p (fn
, targs
))
22422 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
22426 /* DR 1391: All parameters have args, now check non-dependent parms for
22427 convertibility. We don't do this if all args were explicitly specified,
22428 as the standard says that we substitute explicit args immediately. */
22430 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22434 /* All is well so far. Now, check:
22438 When all template arguments have been deduced, all uses of
22439 template parameters in nondeduced contexts are replaced with
22440 the corresponding deduced argument values. If the
22441 substitution results in an invalid type, as described above,
22442 type deduction fails. */
22443 if (!push_tinst_level (fn
, targs
))
22445 excessive_deduction_depth
= true;
22449 /* Also collect access checks from the instantiation. */
22450 reopen_deferring_access_checks (checks
);
22452 decl
= instantiate_template (fn
, targs
, complain
);
22454 checks
= get_deferred_access_checks ();
22455 pop_deferring_access_checks ();
22457 pop_tinst_level ();
22459 if (decl
== error_mark_node
)
22462 /* Now perform any access checks encountered during substitution. */
22463 push_access_scope (decl
);
22464 ok
= perform_access_checks (checks
, complain
);
22465 pop_access_scope (decl
);
22469 /* If we're looking for an exact match, check that what we got
22470 is indeed an exact match. It might not be if some template
22471 parameters are used in non-deduced contexts. But don't check
22472 for an exact match if we have dependent template arguments;
22473 in that case we're doing partial ordering, and we already know
22474 that we have two candidates that will provide the actual type. */
22475 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
22477 tree substed
= TREE_TYPE (decl
);
22481 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
22483 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
22484 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
22485 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
22487 unify_type_mismatch (explain_p
, args
[i
],
22488 TREE_VALUE (sarg
));
22491 if ((i
< nargs
|| sarg
)
22492 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22493 doesn't contain the trailing void, and conv fns are always (). */
22494 && !DECL_CONV_FN_P (decl
))
22496 unsigned nsargs
= i
+ list_length (sarg
);
22497 unify_arity (explain_p
, nargs
, nsargs
);
22502 /* After doing deduction with the inherited constructor, actually return an
22503 instantiation of the inheriting constructor. */
22505 decl
= instantiate_template (orig_fn
, targs
, complain
);
22511 if (excessive_deduction_depth
)
22513 if (deduction_depth
== 0)
22514 /* Reset once we're all the way out. */
22515 excessive_deduction_depth
= false;
22521 /* Returns true iff PARM is a forwarding reference in the context of
22522 template argument deduction for TMPL. */
22525 forwarding_reference_p (tree parm
, tree tmpl
)
22527 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22528 cv-unqualified template parameter ..." */
22529 if (TYPE_REF_P (parm
)
22530 && TYPE_REF_IS_RVALUE (parm
)
22531 && TREE_CODE (TREE_TYPE (parm
)) == TEMPLATE_TYPE_PARM
22532 && cp_type_quals (TREE_TYPE (parm
)) == TYPE_UNQUALIFIED
)
22534 parm
= TREE_TYPE (parm
);
22535 /* [temp.deduct.call], "... that does not represent a template parameter
22536 of a class template (during class template argument deduction)." */
22538 && deduction_guide_p (tmpl
)
22539 && DECL_ARTIFICIAL (tmpl
))
22541 /* Since the template parameters of a synthesized guide consist of
22542 the template parameters of the class template followed by those of
22543 the constructor (if any), we can tell if PARM represents a template
22544 parameter of the class template by comparing its index with the
22545 arity of the class template. */
22546 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl
)));
22547 if (TEMPLATE_TYPE_IDX (parm
)
22548 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl
)))
22556 /* Adjust types before performing type deduction, as described in
22557 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22558 sections are symmetric. PARM is the type of a function parameter
22559 or the return type of the conversion function. ARG is the type of
22560 the argument passed to the call, or the type of the value
22561 initialized with the result of the conversion function.
22562 ARG_EXPR is the original argument expression, which may be null. */
22565 maybe_adjust_types_for_deduction (tree tparms
,
22566 unification_kind_t strict
,
22579 /* Swap PARM and ARG throughout the remainder of this
22580 function; the handling is precisely symmetric since PARM
22581 will initialize ARG rather than vice versa. */
22582 std::swap (parm
, arg
);
22586 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22587 too, but here handle it by stripping the reference from PARM
22588 rather than by adding it to ARG. */
22589 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22590 && TYPE_REF_P (*arg
)
22591 && !TYPE_REF_IS_RVALUE (*arg
))
22592 *parm
= TREE_TYPE (*parm
);
22593 /* Nothing else to do in this case. */
22597 gcc_unreachable ();
22600 if (!TYPE_REF_P (*parm
))
22602 /* [temp.deduct.call]
22604 If P is not a reference type:
22606 --If A is an array type, the pointer type produced by the
22607 array-to-pointer standard conversion (_conv.array_) is
22608 used in place of A for type deduction; otherwise,
22610 --If A is a function type, the pointer type produced by
22611 the function-to-pointer standard conversion
22612 (_conv.func_) is used in place of A for type deduction;
22615 --If A is a cv-qualified type, the top level
22616 cv-qualifiers of A's type are ignored for type
22618 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
22619 *arg
= build_pointer_type (TREE_TYPE (*arg
));
22620 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
22621 *arg
= build_pointer_type (*arg
);
22623 *arg
= TYPE_MAIN_VARIANT (*arg
);
22626 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22627 an lvalue, the type 'lvalue reference to A' is used in place of A for
22628 type deduction." */
22629 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22630 && (arg_expr
? lvalue_p (arg_expr
)
22631 /* try_one_overload doesn't provide an arg_expr, but
22632 functions are always lvalues. */
22633 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
22634 *arg
= build_reference_type (*arg
);
22636 /* [temp.deduct.call]
22638 If P is a cv-qualified type, the top level cv-qualifiers
22639 of P's type are ignored for type deduction. If P is a
22640 reference type, the type referred to by P is used for
22642 *parm
= TYPE_MAIN_VARIANT (*parm
);
22643 if (TYPE_REF_P (*parm
))
22645 *parm
= TREE_TYPE (*parm
);
22646 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22649 /* DR 322. For conversion deduction, remove a reference type on parm
22650 too (which has been swapped into ARG). */
22651 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
22652 *arg
= TREE_TYPE (*arg
);
22657 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22658 template which doesn't contain any deducible template parameters; check if
22659 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22660 unify_one_argument. */
22663 check_non_deducible_conversion (tree parm
, tree arg
, unification_kind_t strict
,
22664 int flags
, struct conversion
**conv_p
,
22670 type
= TREE_TYPE (arg
);
22674 if (same_type_p (parm
, type
))
22675 return unify_success (explain_p
);
22677 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22678 if (strict
== DEDUCE_CONV
)
22680 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
22681 return unify_success (explain_p
);
22683 else if (strict
== DEDUCE_CALL
)
22686 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
22688 /* Avoid recalculating this in add_function_candidate. */
22690 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
22692 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
22694 return unify_success (explain_p
);
22697 if (strict
== DEDUCE_EXACT
)
22698 return unify_type_mismatch (explain_p
, parm
, arg
);
22700 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
22703 static bool uses_deducible_template_parms (tree type
);
22705 /* Returns true iff the expression EXPR is one from which a template
22706 argument can be deduced. In other words, if it's an undecorated
22707 use of a template non-type parameter. */
22710 deducible_expression (tree expr
)
22712 /* Strip implicit conversions and implicit INDIRECT_REFs. */
22713 while (CONVERT_EXPR_P (expr
)
22714 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
22715 || REFERENCE_REF_P (expr
))
22716 expr
= TREE_OPERAND (expr
, 0);
22717 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
22720 /* Returns true iff the array domain DOMAIN uses a template parameter in a
22721 deducible way; that is, if it has a max value of <PARM> - 1. */
22724 deducible_array_bound (tree domain
)
22726 if (domain
== NULL_TREE
)
22729 tree max
= TYPE_MAX_VALUE (domain
);
22730 if (TREE_CODE (max
) != MINUS_EXPR
)
22733 return deducible_expression (TREE_OPERAND (max
, 0));
22736 /* Returns true iff the template arguments ARGS use a template parameter
22737 in a deducible way. */
22740 deducible_template_args (tree args
)
22742 for (tree elt
: tree_vec_range (args
))
22745 if (ARGUMENT_PACK_P (elt
))
22746 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
22749 if (PACK_EXPANSION_P (elt
))
22750 elt
= PACK_EXPANSION_PATTERN (elt
);
22751 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
22753 else if (TYPE_P (elt
))
22754 deducible
= uses_deducible_template_parms (elt
);
22756 deducible
= deducible_expression (elt
);
22764 /* Returns true iff TYPE contains any deducible references to template
22765 parameters, as per 14.8.2.5. */
22768 uses_deducible_template_parms (tree type
)
22770 if (PACK_EXPANSION_P (type
))
22771 type
= PACK_EXPANSION_PATTERN (type
);
22778 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22779 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22785 if (INDIRECT_TYPE_P (type
))
22786 return uses_deducible_template_parms (TREE_TYPE (type
));
22788 /* T[integer-constant ]
22790 if (TREE_CODE (type
) == ARRAY_TYPE
)
22791 return (uses_deducible_template_parms (TREE_TYPE (type
))
22792 || deducible_array_bound (TYPE_DOMAIN (type
)));
22804 if (TYPE_PTRMEM_P (type
))
22805 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
22806 || (uses_deducible_template_parms
22807 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
22809 /* template-name <T> (where template-name refers to a class template)
22810 template-name <i> (where template-name refers to a class template) */
22811 if (CLASS_TYPE_P (type
)
22812 && CLASSTYPE_TEMPLATE_INFO (type
)
22813 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
22814 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22815 (CLASSTYPE_TI_ARGS (type
)));
22820 if (FUNC_OR_METHOD_TYPE_P (type
))
22822 if (uses_deducible_template_parms (TREE_TYPE (type
)))
22824 tree parm
= TYPE_ARG_TYPES (type
);
22825 if (TREE_CODE (type
) == METHOD_TYPE
)
22826 parm
= TREE_CHAIN (parm
);
22827 for (; parm
; parm
= TREE_CHAIN (parm
))
22828 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
22830 if (flag_noexcept_type
22831 && TYPE_RAISES_EXCEPTIONS (type
)
22832 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))
22833 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))))
22840 /* Subroutine of type_unification_real and unify_pack_expansion to
22841 handle unification of a single P/A pair. Parameters are as
22842 for those functions. */
22845 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
22846 int subr
, unification_kind_t strict
,
22849 tree arg_expr
= NULL_TREE
;
22852 if (arg
== error_mark_node
|| parm
== error_mark_node
)
22853 return unify_invalid (explain_p
);
22854 if (arg
== unknown_type_node
)
22855 /* We can't deduce anything from this, but we might get all the
22856 template args from other function args. */
22857 return unify_success (explain_p
);
22859 /* Implicit conversions (Clause 4) will be performed on a function
22860 argument to convert it to the type of the corresponding function
22861 parameter if the parameter type contains no template-parameters that
22862 participate in template argument deduction. */
22863 if (strict
!= DEDUCE_EXACT
22864 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
22865 /* For function parameters with no deducible template parameters,
22866 just return. We'll check non-dependent conversions later. */
22867 return unify_success (explain_p
);
22872 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
22873 | UNIFY_ALLOW_MORE_CV_QUAL
22874 | UNIFY_ALLOW_DERIVED
);
22878 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
22882 arg_strict
= UNIFY_ALLOW_NONE
;
22886 gcc_unreachable ();
22889 /* We only do these transformations if this is the top-level
22890 parameter_type_list in a call or declaration matching; in other
22891 situations (nested function declarators, template argument lists) we
22892 won't be comparing a type to an expression, and we don't do any type
22898 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
22899 if (type_unknown_p (arg
))
22901 /* [temp.deduct.type] A template-argument can be
22902 deduced from a pointer to function or pointer
22903 to member function argument if the set of
22904 overloaded functions does not contain function
22905 templates and at most one of a set of
22906 overloaded functions provides a unique
22908 resolve_overloaded_unification (tparms
, targs
, parm
,
22910 arg_strict
, explain_p
);
22911 /* If a unique match was not found, this is a
22912 non-deduced context, so we still succeed. */
22913 return unify_success (explain_p
);
22917 arg
= unlowered_expr_type (arg
);
22918 if (arg
== error_mark_node
)
22919 return unify_invalid (explain_p
);
22922 arg_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
22923 &parm
, &arg
, arg_expr
);
22926 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
22927 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
22928 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22930 /* For deduction from an init-list we need the actual list. */
22931 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
22933 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
22936 /* for_each_template_parm callback that always returns 0. */
22939 zero_r (tree
, void *)
22944 /* for_each_template_parm any_fn callback to handle deduction of a template
22945 type argument from the type of an array bound. */
22948 array_deduction_r (tree t
, void *data
)
22950 tree_pair_p d
= (tree_pair_p
)data
;
22951 tree
&tparms
= d
->purpose
;
22952 tree
&targs
= d
->value
;
22954 if (TREE_CODE (t
) == ARRAY_TYPE
)
22955 if (tree dom
= TYPE_DOMAIN (t
))
22956 if (tree max
= TYPE_MAX_VALUE (dom
))
22958 if (TREE_CODE (max
) == MINUS_EXPR
)
22959 max
= TREE_OPERAND (max
, 0);
22960 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
22961 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
22962 UNIFY_ALLOW_NONE
, /*explain*/false);
22965 /* Keep walking. */
22969 /* Try to deduce any not-yet-deduced template type arguments from the type of
22970 an array bound. This is handled separately from unify because 14.8.2.5 says
22971 "The type of a type parameter is only deduced from an array bound if it is
22972 not otherwise deduced." */
22975 try_array_deduction (tree tparms
, tree targs
, tree parm
)
22977 tree_pair_s data
= { tparms
, targs
};
22978 hash_set
<tree
> visited
;
22979 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
22980 /*nondeduced*/false, array_deduction_r
);
22983 /* Most parms like fn_type_unification.
22985 If SUBR is 1, we're being called recursively (to unify the
22986 arguments of a function or method parameter of a function
22989 CHECKS is a pointer to a vector of access checks encountered while
22990 substituting default template arguments. */
22993 type_unification_real (tree tparms
,
22997 unsigned int xnargs
,
22999 unification_kind_t strict
,
23000 vec
<deferred_access_check
, va_gc
> **checks
,
23005 int ntparms
= TREE_VEC_LENGTH (tparms
);
23006 int saw_undeduced
= 0;
23009 unsigned int nargs
;
23012 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
23013 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
23014 gcc_assert (ntparms
> 0);
23016 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
23018 /* Reset the number of non-defaulted template arguments contained
23020 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
23027 /* Only fn_type_unification cares about terminal void. */
23028 if (nargs
&& args
[nargs
-1] == void_type_node
)
23032 while (parms
&& parms
!= void_list_node
23035 parm
= TREE_VALUE (parms
);
23037 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
23038 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
23039 /* For a function parameter pack that occurs at the end of the
23040 parameter-declaration-list, the type A of each remaining
23041 argument of the call is compared with the type P of the
23042 declarator-id of the function parameter pack. */
23045 parms
= TREE_CHAIN (parms
);
23047 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
23048 /* For a function parameter pack that does not occur at the
23049 end of the parameter-declaration-list, the type of the
23050 parameter pack is a non-deduced context. */
23056 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
23062 && parms
!= void_list_node
23063 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
23065 /* Unify the remaining arguments with the pack expansion type. */
23067 tree parmvec
= make_tree_vec (1);
23069 /* Allocate a TREE_VEC and copy in all of the arguments */
23070 argvec
= make_tree_vec (nargs
- ia
);
23071 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
23072 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
23074 /* Copy the parameter into parmvec. */
23075 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
23076 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
23077 /*subr=*/subr
, explain_p
))
23080 /* Advance to the end of the list of parameters. */
23081 parms
= TREE_CHAIN (parms
);
23084 /* Fail if we've reached the end of the parm list, and more args
23085 are present, and the parm list isn't variadic. */
23086 if (ia
< nargs
&& parms
== void_list_node
)
23087 return unify_too_many_arguments (explain_p
, nargs
, ia
);
23088 /* Fail if parms are left and they don't have default values and
23089 they aren't all deduced as empty packs (c++/57397). This is
23090 consistent with sufficient_parms_p. */
23091 if (parms
&& parms
!= void_list_node
23092 && TREE_PURPOSE (parms
) == NULL_TREE
)
23094 unsigned int count
= nargs
;
23099 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
23102 p
= TREE_CHAIN (p
);
23104 while (p
&& p
!= void_list_node
);
23105 if (count
!= nargs
)
23106 return unify_too_few_arguments (explain_p
, ia
, count
,
23112 tsubst_flags_t complain
= (explain_p
23113 ? tf_warning_or_error
23115 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23117 for (i
= 0; i
< ntparms
; i
++)
23119 tree targ
= TREE_VEC_ELT (targs
, i
);
23120 tree tparm
= TREE_VEC_ELT (tparms
, i
);
23122 /* Clear the "incomplete" flags on all argument packs now so that
23123 substituting them into later default arguments works. */
23124 if (targ
&& ARGUMENT_PACK_P (targ
))
23126 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
23127 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
23130 if (targ
|| tparm
== error_mark_node
)
23132 tparm
= TREE_VALUE (tparm
);
23134 if (TREE_CODE (tparm
) == TYPE_DECL
23135 && !tried_array_deduction
)
23137 try_array_deduction (tparms
, targs
, xparms
);
23138 tried_array_deduction
= true;
23139 if (TREE_VEC_ELT (targs
, i
))
23143 /* If this is an undeduced nontype parameter that depends on
23144 a type parameter, try another pass; its type may have been
23145 deduced from a later argument than the one from which
23146 this parameter can be deduced. */
23147 if (TREE_CODE (tparm
) == PARM_DECL
23148 && !is_auto (TREE_TYPE (tparm
))
23149 && uses_template_parms (TREE_TYPE (tparm
))
23150 && saw_undeduced
< 2)
23156 /* Core issue #226 (C++0x) [temp.deduct]:
23158 If a template argument has not been deduced, its
23159 default template argument, if any, is used.
23161 When we are in C++98 mode, TREE_PURPOSE will either
23162 be NULL_TREE or ERROR_MARK_NODE, so we do not need
23163 to explicitly check cxx_dialect here. */
23164 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
23165 /* OK, there is a default argument. Wait until after the
23166 conversion check to do substitution. */
23169 /* If the type parameter is a parameter pack, then it will
23170 be deduced to an empty parameter pack. */
23171 if (template_parameter_pack_p (tparm
))
23175 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
23177 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
23178 TREE_CONSTANT (arg
) = 1;
23181 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23183 ARGUMENT_PACK_ARGS (arg
) = make_tree_vec (0);
23185 TREE_VEC_ELT (targs
, i
) = arg
;
23189 return unify_parameter_deduction_failure (explain_p
, tparm
);
23192 /* Now substitute into the default template arguments. */
23193 for (i
= 0; i
< ntparms
; i
++)
23195 tree targ
= TREE_VEC_ELT (targs
, i
);
23196 tree tparm
= TREE_VEC_ELT (tparms
, i
);
23198 if (targ
|| tparm
== error_mark_node
)
23200 tree parm
= TREE_VALUE (tparm
);
23201 tree arg
= TREE_PURPOSE (tparm
);
23202 reopen_deferring_access_checks (*checks
);
23203 location_t save_loc
= input_location
;
23205 input_location
= DECL_SOURCE_LOCATION (parm
);
23207 if (saw_undeduced
== 1
23208 && TREE_CODE (parm
) == PARM_DECL
23209 && !is_auto (TREE_TYPE (parm
))
23210 && uses_template_parms (TREE_TYPE (parm
)))
23212 /* The type of this non-type parameter depends on undeduced
23213 parameters. Don't try to use its default argument yet,
23214 since we might deduce an argument for it on the next pass,
23215 but do check whether the arguments we already have cause
23216 substitution failure, so that that happens before we try
23217 later default arguments (78489). */
23218 ++processing_template_decl
;
23219 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
23221 --processing_template_decl
;
23222 if (type
== error_mark_node
)
23223 arg
= error_mark_node
;
23229 /* Even if the call is happening in template context, getting
23230 here means it's non-dependent, and a default argument is
23231 considered a separate definition under [temp.decls], so we can
23232 do this substitution without processing_template_decl. This
23233 is important if the default argument contains something that
23234 might be instantiation-dependent like access (87480). */
23235 processing_template_decl_sentinel s
;
23236 tree substed
= NULL_TREE
;
23237 if (saw_undeduced
== 1)
23239 /* First instatiate in template context, in case we still
23240 depend on undeduced template parameters. */
23241 ++processing_template_decl
;
23242 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
23244 --processing_template_decl
;
23245 if (substed
!= error_mark_node
23246 && !uses_template_parms (substed
))
23247 /* We replaced all the tparms, substitute again out of
23248 template context. */
23249 substed
= NULL_TREE
;
23252 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
23255 if (!uses_template_parms (substed
))
23256 arg
= convert_template_argument (parm
, substed
, full_targs
,
23257 complain
, i
, NULL_TREE
);
23258 else if (saw_undeduced
== 1)
23261 arg
= error_mark_node
;
23264 input_location
= save_loc
;
23265 *checks
= get_deferred_access_checks ();
23266 pop_deferring_access_checks ();
23268 if (arg
== error_mark_node
)
23272 TREE_VEC_ELT (targs
, i
) = arg
;
23273 /* The position of the first default template argument,
23274 is also the number of non-defaulted arguments in TARGS.
23276 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
23277 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
23281 if (saw_undeduced
++ == 1)
23285 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
23286 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
23288 return unify_success (explain_p
);
23291 /* Subroutine of type_unification_real. Args are like the variables
23292 at the call site. ARG is an overloaded function (or template-id);
23293 we try deducing template args from each of the overloads, and if
23294 only one succeeds, we go with that. Modifies TARGS and returns
23295 true on success. */
23298 resolve_overloaded_unification (tree tparms
,
23302 unification_kind_t strict
,
23306 tree tempargs
= copy_node (targs
);
23308 tree goodfn
= NULL_TREE
;
23311 if (TREE_CODE (arg
) == ADDR_EXPR
)
23313 arg
= TREE_OPERAND (arg
, 0);
23319 if (TREE_CODE (arg
) == COMPONENT_REF
)
23320 /* Handle `&x' where `x' is some static or non-static member
23322 arg
= TREE_OPERAND (arg
, 1);
23324 if (TREE_CODE (arg
) == OFFSET_REF
)
23325 arg
= TREE_OPERAND (arg
, 1);
23327 /* Strip baselink information. */
23328 if (BASELINK_P (arg
))
23329 arg
= BASELINK_FUNCTIONS (arg
);
23331 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
23333 /* If we got some explicit template args, we need to plug them into
23334 the affected templates before we try to unify, in case the
23335 explicit args will completely resolve the templates in question. */
23338 tree expl_subargs
= TREE_OPERAND (arg
, 1);
23339 arg
= TREE_OPERAND (arg
, 0);
23341 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23344 tree subargs
, elem
;
23346 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23349 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23350 expl_subargs
, NULL_TREE
, tf_none
);
23351 if (subargs
!= error_mark_node
23352 && !any_dependent_template_arguments_p (subargs
))
23354 fn
= instantiate_template (fn
, subargs
, tf_none
);
23355 if (!constraints_satisfied_p (fn
))
23357 if (undeduced_auto_decl (fn
))
23359 /* Instantiate the function to deduce its return type. */
23361 instantiate_decl (fn
, /*defer*/false, /*class*/false);
23365 if (flag_noexcept_type
)
23366 maybe_instantiate_noexcept (fn
, tf_none
);
23368 elem
= TREE_TYPE (fn
);
23369 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
23370 elem
, strict
, sub_strict
, addr_p
, explain_p
)
23371 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
23380 /* If no templates (or more than one) are fully resolved by the
23381 explicit arguments, this template-id is a non-deduced context; it
23382 could still be OK if we deduce all template arguments for the
23383 enclosing call through other arguments. */
23387 else if (!OVL_P (arg
))
23388 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23389 -- but the deduction does not succeed because the expression is
23390 not just the function on its own. */
23393 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23396 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
23397 strict
, sub_strict
, addr_p
, explain_p
)
23398 && (!goodfn
|| !decls_match (goodfn
, fn
)))
23405 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23406 to function or pointer to member function argument if the set of
23407 overloaded functions does not contain function templates and at most
23408 one of a set of overloaded functions provides a unique match.
23410 So if we found multiple possibilities, we return success but don't
23411 deduce anything. */
23415 int i
= TREE_VEC_LENGTH (targs
);
23417 if (TREE_VEC_ELT (tempargs
, i
))
23419 tree old
= TREE_VEC_ELT (targs
, i
);
23420 tree new_
= TREE_VEC_ELT (tempargs
, i
);
23421 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
23422 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
23423 /* Don't forget explicit template arguments in a pack. */
23424 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
23425 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
23426 TREE_VEC_ELT (targs
, i
) = new_
;
23435 /* Core DR 115: In contexts where deduction is done and fails, or in
23436 contexts where deduction is not done, if a template argument list is
23437 specified and it, along with any default template arguments, identifies
23438 a single function template specialization, then the template-id is an
23439 lvalue for the function template specialization. */
23442 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
23444 tree expr
, offset
, baselink
;
23447 if (!type_unknown_p (orig_expr
))
23452 offset
= NULL_TREE
;
23453 baselink
= NULL_TREE
;
23455 if (TREE_CODE (expr
) == ADDR_EXPR
)
23457 expr
= TREE_OPERAND (expr
, 0);
23460 if (TREE_CODE (expr
) == OFFSET_REF
)
23463 expr
= TREE_OPERAND (expr
, 1);
23465 if (BASELINK_P (expr
))
23468 expr
= BASELINK_FUNCTIONS (expr
);
23471 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
23474 tree goodfn
= NULL_TREE
;
23476 /* If we got some explicit template args, we need to plug them into
23477 the affected templates before we try to unify, in case the
23478 explicit args will completely resolve the templates in question. */
23480 tree expl_subargs
= TREE_OPERAND (expr
, 1);
23481 tree arg
= TREE_OPERAND (expr
, 0);
23482 tree badfn
= NULL_TREE
;
23483 tree badargs
= NULL_TREE
;
23485 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23488 tree subargs
, elem
;
23490 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23493 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23494 expl_subargs
, NULL_TREE
, tf_none
);
23495 if (subargs
!= error_mark_node
23496 && !any_dependent_template_arguments_p (subargs
))
23498 elem
= instantiate_template (fn
, subargs
, tf_none
);
23499 if (elem
== error_mark_node
)
23504 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
23505 && constraints_satisfied_p (elem
))
23514 mark_used (goodfn
);
23517 expr
= build_baselink (BASELINK_BINFO (baselink
),
23518 BASELINK_ACCESS_BINFO (baselink
),
23519 expr
, BASELINK_OPTYPE (baselink
));
23523 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
23524 expr
= build_offset_ref (base
, expr
, addr
, complain
);
23527 expr
= cp_build_addr_expr (expr
, complain
);
23530 else if (good
== 0 && badargs
&& (complain
& tf_error
))
23531 /* There were no good options and at least one bad one, so let the
23532 user know what the problem is. */
23533 instantiate_template (badfn
, badargs
, complain
);
23538 /* As above, but error out if the expression remains overloaded. */
23541 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
23543 exp
= resolve_nondeduced_context (exp
, complain
);
23544 if (type_unknown_p (exp
))
23546 if (complain
& tf_error
)
23547 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
23548 return error_mark_node
;
23553 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23554 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23555 different overloads deduce different arguments for a given parm.
23556 ADDR_P is true if the expression for which deduction is being
23557 performed was of the form "& fn" rather than simply "fn".
23559 Returns 1 on success. */
23562 try_one_overload (tree tparms
,
23567 unification_kind_t strict
,
23576 if (arg
== error_mark_node
)
23579 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23580 to function or pointer to member function argument if the set of
23581 overloaded functions does not contain function templates and at most
23582 one of a set of overloaded functions provides a unique match.
23584 So if this is a template, just return success. */
23586 if (uses_template_parms (arg
))
23589 if (TREE_CODE (arg
) == METHOD_TYPE
)
23590 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
23592 arg
= build_pointer_type (arg
);
23594 sub_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23595 &parm
, &arg
, NULL_TREE
);
23597 /* We don't copy orig_targs for this because if we have already deduced
23598 some template args from previous args, unify would complain when we
23599 try to deduce a template parameter for the same argument, even though
23600 there isn't really a conflict. */
23601 nargs
= TREE_VEC_LENGTH (targs
);
23602 tempargs
= make_tree_vec (nargs
);
23604 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
23607 /* First make sure we didn't deduce anything that conflicts with
23608 explicitly specified args. */
23609 for (i
= nargs
; i
--; )
23611 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23612 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
23616 else if (uses_template_parms (elt
))
23617 /* Since we're unifying against ourselves, we will fill in
23618 template args used in the function parm list with our own
23619 template parms. Discard them. */
23620 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
23621 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
23623 /* Check that the argument at each index of the deduced argument pack
23624 is equivalent to the corresponding explicitly specified argument.
23625 We may have deduced more arguments than were explicitly specified,
23628 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23629 that's wrong if we deduce the same argument pack from multiple
23630 function arguments: it's only incomplete the first time. */
23632 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
23633 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
23635 if (TREE_VEC_LENGTH (deduced_pack
)
23636 < TREE_VEC_LENGTH (explicit_pack
))
23639 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
23640 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
23641 TREE_VEC_ELT (deduced_pack
, j
)))
23644 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
23648 for (i
= nargs
; i
--; )
23650 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23653 TREE_VEC_ELT (targs
, i
) = elt
;
23659 /* PARM is a template class (perhaps with unbound template
23660 parameters). ARG is a fully instantiated type. If ARG can be
23661 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23662 TARGS are as for unify. */
23665 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
23668 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23670 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23671 /* Matches anything. */;
23672 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
23673 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
23676 /* We need to make a new template argument vector for the call to
23677 unify. If we used TARGS, we'd clutter it up with the result of
23678 the attempted unification, even if this class didn't work out.
23679 We also don't want to commit ourselves to all the unifications
23680 we've already done, since unification is supposed to be done on
23681 an argument-by-argument basis. In other words, consider the
23682 following pathological case:
23684 template <int I, int J, int K>
23687 template <int I, int J>
23688 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
23690 template <int I, int J, int K>
23691 void f(S<I, J, K>, S<I, I, I>);
23700 Now, by the time we consider the unification involving `s2', we
23701 already know that we must have `f<0, 0, 0>'. But, even though
23702 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
23703 because there are two ways to unify base classes of S<0, 1, 2>
23704 with S<I, I, I>. If we kept the already deduced knowledge, we
23705 would reject the possibility I=1. */
23706 targs
= copy_template_args (targs
);
23707 for (tree
& targ
: tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs
)))
23711 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23712 err
= unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
);
23714 err
= unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23715 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23717 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
23718 for (tree level
: tree_vec_range (targs
))
23722 return err
? NULL_TREE
: arg
;
23725 /* Given a template type PARM and a class type ARG, find the unique
23726 base type in ARG that is an instance of PARM. We do not examine
23727 ARG itself; only its base-classes. If there is not exactly one
23728 appropriate base class, return NULL_TREE. PARM may be the type of
23729 a partial specialization, as well as a plain template type. Used
23732 static enum template_base_result
23733 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
23734 bool explain_p
, tree
*result
)
23736 tree rval
= NULL_TREE
;
23739 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
23741 binfo
= TYPE_BINFO (complete_type (arg
));
23744 /* The type could not be completed. */
23745 *result
= NULL_TREE
;
23746 return tbr_incomplete_type
;
23749 /* Walk in inheritance graph order. The search order is not
23750 important, and this avoids multiple walks of virtual bases. */
23751 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
23753 tree r
= try_class_unification (tparms
, targs
, parm
,
23754 BINFO_TYPE (binfo
), explain_p
);
23758 /* If there is more than one satisfactory baseclass, then:
23762 If they yield more than one possible deduced A, the type
23766 if (rval
&& !same_type_p (r
, rval
))
23768 /* [temp.deduct.call]/4.3: If there is a class C that is a
23769 (direct or indirect) base class of D and derived (directly or
23770 indirectly) from a class B and that would be a valid deduced
23771 A, the deduced A cannot be B or pointer to B, respectively. */
23772 if (DERIVED_FROM_P (r
, rval
))
23775 else if (DERIVED_FROM_P (rval
, r
))
23776 /* Ignore rval. */;
23779 *result
= NULL_TREE
;
23780 return tbr_ambiguous_baseclass
;
23789 return tbr_success
;
23792 /* Returns the level of DECL, which declares a template parameter. */
23795 template_decl_level (tree decl
)
23797 switch (TREE_CODE (decl
))
23800 case TEMPLATE_DECL
:
23801 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
23804 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
23807 gcc_unreachable ();
23812 /* Decide whether ARG can be unified with PARM, considering only the
23813 cv-qualifiers of each type, given STRICT as documented for unify.
23814 Returns nonzero iff the unification is OK on that basis. */
23817 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
23819 int arg_quals
= cp_type_quals (arg
);
23820 int parm_quals
= cp_type_quals (parm
);
23822 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23823 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23825 /* Although a CVR qualifier is ignored when being applied to a
23826 substituted template parameter ([8.3.2]/1 for example), that
23827 does not allow us to unify "const T" with "int&" because both
23828 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23829 It is ok when we're allowing additional CV qualifiers
23830 at the outer level [14.8.2.1]/3,1st bullet. */
23831 if ((TYPE_REF_P (arg
)
23832 || FUNC_OR_METHOD_TYPE_P (arg
))
23833 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
23836 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
23837 && (parm_quals
& TYPE_QUAL_RESTRICT
))
23841 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23842 && (arg_quals
& parm_quals
) != parm_quals
)
23845 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
23846 && (parm_quals
& arg_quals
) != arg_quals
)
23852 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23854 template_parm_level_and_index (tree parm
, int* level
, int* index
)
23856 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23857 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23858 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23860 *index
= TEMPLATE_TYPE_IDX (parm
);
23861 *level
= TEMPLATE_TYPE_LEVEL (parm
);
23865 *index
= TEMPLATE_PARM_IDX (parm
);
23866 *level
= TEMPLATE_PARM_LEVEL (parm
);
23870 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23872 if (unify (TP, TA, P, A, S, EP)) \
23876 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23877 expansion at the end of PACKED_PARMS. Returns 0 if the type
23878 deduction succeeds, 1 otherwise. STRICT is the same as in
23879 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23880 function call argument list. We'll need to adjust the arguments to make them
23881 types. SUBR tells us if this is from a recursive call to
23882 type_unification_real, or for comparing two template argument
23886 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
23887 tree packed_args
, unification_kind_t strict
,
23888 bool subr
, bool explain_p
)
23891 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
23892 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
23893 tree pack
, packs
= NULL_TREE
;
23894 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
23896 /* Add in any args remembered from an earlier partial instantiation. */
23897 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
23898 int levels
= TMPL_ARGS_DEPTH (targs
);
23900 packed_args
= expand_template_argument_pack (packed_args
);
23902 int len
= TREE_VEC_LENGTH (packed_args
);
23904 /* Determine the parameter packs we will be deducing from the
23905 pattern, and record their current deductions. */
23906 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
23907 pack
; pack
= TREE_CHAIN (pack
))
23909 tree parm_pack
= TREE_VALUE (pack
);
23912 /* Only template parameter packs can be deduced, not e.g. function
23913 parameter packs or __bases or __integer_pack. */
23914 if (!TEMPLATE_PARM_P (parm_pack
))
23917 /* Determine the index and level of this parameter pack. */
23918 template_parm_level_and_index (parm_pack
, &level
, &idx
);
23919 if (level
> levels
)
23922 /* Keep track of the parameter packs and their corresponding
23924 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
23925 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
23928 /* Loop through all of the arguments that have not yet been
23929 unified and unify each with the pattern. */
23930 for (i
= start
; i
< len
; i
++)
23933 bool any_explicit
= false;
23934 tree arg
= TREE_VEC_ELT (packed_args
, i
);
23936 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23937 or the element of its argument pack at the current index if
23938 this argument was explicitly specified. */
23939 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23943 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23946 if (TREE_VALUE (pack
)
23947 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
23948 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
23950 any_explicit
= true;
23951 arg
= TREE_VEC_ELT (pargs
, i
- start
);
23953 TMPL_ARG (targs
, level
, idx
) = arg
;
23956 /* If we had explicit template arguments, substitute them into the
23957 pattern before deduction. */
23960 /* Some arguments might still be unspecified or dependent. */
23962 ++processing_template_decl
;
23963 dependent
= any_dependent_template_arguments_p (targs
);
23965 --processing_template_decl
;
23966 parm
= tsubst (pattern
, targs
,
23967 explain_p
? tf_warning_or_error
: tf_none
,
23970 --processing_template_decl
;
23971 if (parm
== error_mark_node
)
23977 /* Unify the pattern with the current argument. */
23978 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
23982 /* For each parameter pack, collect the deduced value. */
23983 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23986 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23988 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
23989 TMPL_ARG (targs
, level
, idx
);
23993 /* Verify that the results of unification with the parameter packs
23994 produce results consistent with what we've seen before, and make
23995 the deduced argument packs available. */
23996 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23998 tree old_pack
= TREE_VALUE (pack
);
23999 tree new_args
= TREE_TYPE (pack
);
24000 int i
, len
= TREE_VEC_LENGTH (new_args
);
24002 bool nondeduced_p
= false;
24004 /* By default keep the original deduced argument pack.
24005 If necessary, more specific code is going to update the
24006 resulting deduced argument later down in this function. */
24007 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24008 TMPL_ARG (targs
, level
, idx
) = old_pack
;
24010 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
24011 actually deduce anything. */
24012 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
24013 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
24014 nondeduced_p
= true;
24018 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
24020 /* If we had fewer function args than explicit template args,
24021 just use the explicits. */
24022 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
24023 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
24024 if (len
< explicit_len
)
24025 new_args
= explicit_args
;
24031 /* Build the deduced *_ARGUMENT_PACK. */
24032 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
24034 result
= make_node (NONTYPE_ARGUMENT_PACK
);
24035 TREE_CONSTANT (result
) = 1;
24038 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
24040 ARGUMENT_PACK_ARGS (result
) = new_args
;
24042 /* Note the deduced argument packs for this parameter
24044 TMPL_ARG (targs
, level
, idx
) = result
;
24046 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
24047 && (ARGUMENT_PACK_ARGS (old_pack
)
24048 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
24050 /* We only had the explicitly-provided arguments before, but
24051 now we have a complete set of arguments. */
24052 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
24054 ARGUMENT_PACK_ARGS (old_pack
) = new_args
;
24055 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
24056 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
24060 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
24061 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
24062 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
24063 /* During template argument deduction for the aggregate deduction
24064 candidate, the number of elements in a trailing parameter pack
24065 is only deduced from the number of remaining function
24066 arguments if it is not otherwise deduced. */
24067 if (cxx_dialect
>= cxx20
24068 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
24069 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
24070 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
24071 if (!comp_template_args (old_args
, new_args
,
24072 &bad_old_arg
, &bad_new_arg
))
24073 /* Inconsistent unification of this parameter pack. */
24074 return unify_parameter_pack_inconsistent (explain_p
,
24080 return unify_success (explain_p
);
24083 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
24084 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
24085 parameters and return value are as for unify. */
24088 unify_array_domain (tree tparms
, tree targs
,
24089 tree parm_dom
, tree arg_dom
,
24097 /* Our representation of array types uses "N - 1" as the
24098 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
24099 not an integer constant. We cannot unify arbitrarily
24100 complex expressions, so we eliminate the MINUS_EXPRs
24102 parm_max
= TYPE_MAX_VALUE (parm_dom
);
24103 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
24106 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
24107 parm_max
= TREE_OPERAND (parm_max
, 0);
24109 arg_max
= TYPE_MAX_VALUE (arg_dom
);
24110 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
24113 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
24114 trying to unify the type of a variable with the type
24115 of a template parameter. For example:
24117 template <unsigned int N>
24118 void f (char (&) [N]);
24125 Here, the type of the ARG will be "int [g(i)]", and
24126 may be a SAVE_EXPR, etc. */
24127 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
24128 return unify_vla_arg (explain_p
, arg_dom
);
24129 arg_max
= TREE_OPERAND (arg_max
, 0);
24132 /* If only one of the bounds used a MINUS_EXPR, compensate
24133 by adding one to the other bound. */
24134 if (parm_cst
&& !arg_cst
)
24135 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
24139 else if (arg_cst
&& !parm_cst
)
24140 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
24145 return unify (tparms
, targs
, parm_max
, arg_max
,
24146 UNIFY_ALLOW_INTEGER
, explain_p
);
24149 /* Returns whether T, a P or A in unify, is a type, template or expression. */
24151 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
24156 if (PACK_EXPANSION_P (t
))
24157 t
= PACK_EXPANSION_PATTERN (t
);
24158 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
24159 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
24160 || DECL_TYPE_TEMPLATE_P (t
))
24162 else if (TYPE_P (t
))
24168 /* Deduce the value of template parameters. TPARMS is the (innermost)
24169 set of template parameters to a template. TARGS is the bindings
24170 for those template parameters, as determined thus far; TARGS may
24171 include template arguments for outer levels of template parameters
24172 as well. PARM is a parameter to a template function, or a
24173 subcomponent of that parameter; ARG is the corresponding argument.
24174 This function attempts to match PARM with ARG in a manner
24175 consistent with the existing assignments in TARGS. If more values
24176 are deduced, then TARGS is updated.
24178 Returns 0 if the type deduction succeeds, 1 otherwise. The
24179 parameter STRICT is a bitwise or of the following flags:
24182 Require an exact match between PARM and ARG.
24183 UNIFY_ALLOW_MORE_CV_QUAL:
24184 Allow the deduced ARG to be more cv-qualified (by qualification
24185 conversion) than ARG.
24186 UNIFY_ALLOW_LESS_CV_QUAL:
24187 Allow the deduced ARG to be less cv-qualified than ARG.
24188 UNIFY_ALLOW_DERIVED:
24189 Allow the deduced ARG to be a template base class of ARG,
24190 or a pointer to a template base class of the type pointed to by
24192 UNIFY_ALLOW_INTEGER:
24193 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
24194 case for more information.
24195 UNIFY_ALLOW_OUTER_LEVEL:
24196 This is the outermost level of a deduction. Used to determine validity
24197 of qualification conversions. A valid qualification conversion must
24198 have const qualified pointers leading up to the inner type which
24199 requires additional CV quals, except at the outer level, where const
24200 is not required [conv.qual]. It would be normal to set this flag in
24201 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
24202 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
24203 This is the outermost level of a deduction, and PARM can be more CV
24204 qualified at this point.
24205 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
24206 This is the outermost level of a deduction, and PARM can be less CV
24207 qualified at this point. */
24210 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
24216 int strict_in
= strict
;
24217 tsubst_flags_t complain
= (explain_p
24218 ? tf_warning_or_error
24221 /* I don't think this will do the right thing with respect to types.
24222 But the only case I've seen it in so far has been array bounds, where
24223 signedness is the only information lost, and I think that will be
24224 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
24225 finish_id_expression_1, and are also OK. */
24226 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
24227 parm
= TREE_OPERAND (parm
, 0);
24229 if (arg
== error_mark_node
)
24230 return unify_invalid (explain_p
);
24231 if (arg
== unknown_type_node
24232 || arg
== init_list_type_node
)
24233 /* We can't deduce anything from this, but we might get all the
24234 template args from other function args. */
24235 return unify_success (explain_p
);
24237 if (parm
== any_targ_node
|| arg
== any_targ_node
)
24238 return unify_success (explain_p
);
24240 /* If PARM uses template parameters, then we can't bail out here,
24241 even if ARG == PARM, since we won't record unifications for the
24242 template parameters. We might need them if we're trying to
24243 figure out which of two things is more specialized. */
24244 if (arg
== parm
&& !uses_template_parms (parm
))
24245 return unify_success (explain_p
);
24247 /* Handle init lists early, so the rest of the function can assume
24248 we're dealing with a type. */
24249 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
24252 tree orig_parm
= parm
;
24254 if (!is_std_init_list (parm
)
24255 && TREE_CODE (parm
) != ARRAY_TYPE
)
24256 /* We can only deduce from an initializer list argument if the
24257 parameter is std::initializer_list or an array; otherwise this
24258 is a non-deduced context. */
24259 return unify_success (explain_p
);
24261 if (TREE_CODE (parm
) == ARRAY_TYPE
)
24262 elttype
= TREE_TYPE (parm
);
24265 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
24266 /* Deduction is defined in terms of a single type, so just punt
24267 on the (bizarre) std::initializer_list<T...>. */
24268 if (PACK_EXPANSION_P (elttype
))
24269 return unify_success (explain_p
);
24272 if (strict
!= DEDUCE_EXACT
24273 && TYPE_P (elttype
)
24274 && !uses_deducible_template_parms (elttype
))
24275 /* If ELTTYPE has no deducible template parms, skip deduction from
24276 the list elements. */;
24278 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
24280 tree elt
= e
.value
;
24281 int elt_strict
= strict
;
24283 if (elt
== error_mark_node
)
24284 return unify_invalid (explain_p
);
24286 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
24288 tree type
= TREE_TYPE (elt
);
24289 if (type
== error_mark_node
)
24290 return unify_invalid (explain_p
);
24291 /* It should only be possible to get here for a call. */
24292 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
24293 elt_strict
|= maybe_adjust_types_for_deduction
24294 (tparms
, DEDUCE_CALL
, &elttype
, &type
, elt
);
24298 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
24302 if (TREE_CODE (parm
) == ARRAY_TYPE
24303 && deducible_array_bound (TYPE_DOMAIN (parm
)))
24305 /* Also deduce from the length of the initializer list. */
24306 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
24307 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
24308 if (idx
== error_mark_node
)
24309 return unify_invalid (explain_p
);
24310 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24314 /* If the std::initializer_list<T> deduction worked, replace the
24315 deduced A with std::initializer_list<A>. */
24316 if (orig_parm
!= parm
)
24318 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
24319 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24320 targ
= listify (targ
);
24321 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
24323 return unify_success (explain_p
);
24326 /* If parm and arg aren't the same kind of thing (template, type, or
24327 expression), fail early. */
24328 if (pa_kind (parm
) != pa_kind (arg
))
24329 return unify_invalid (explain_p
);
24331 /* Immediately reject some pairs that won't unify because of
24332 cv-qualification mismatches. */
24333 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24335 /* It is the elements of the array which hold the cv quals of an array
24336 type, and the elements might be template type parms. We'll check
24337 when we recurse. */
24338 && TREE_CODE (arg
) != ARRAY_TYPE
24339 /* We check the cv-qualifiers when unifying with template type
24340 parameters below. We want to allow ARG `const T' to unify with
24341 PARM `T' for example, when computing which of two templates
24342 is more specialized, for example. */
24343 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
24344 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
24345 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24347 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
24348 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
24349 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
24350 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
24351 strict
&= ~UNIFY_ALLOW_DERIVED
;
24352 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
24353 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
24355 switch (TREE_CODE (parm
))
24357 case TYPENAME_TYPE
:
24359 case UNBOUND_CLASS_TEMPLATE
:
24360 /* In a type which contains a nested-name-specifier, template
24361 argument values cannot be deduced for template parameters used
24362 within the nested-name-specifier. */
24363 return unify_success (explain_p
);
24365 case TEMPLATE_TYPE_PARM
:
24366 case TEMPLATE_TEMPLATE_PARM
:
24367 case BOUND_TEMPLATE_TEMPLATE_PARM
:
24368 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24369 if (error_operand_p (tparm
))
24370 return unify_invalid (explain_p
);
24372 if (TEMPLATE_TYPE_LEVEL (parm
)
24373 != template_decl_level (tparm
))
24374 /* The PARM is not one we're trying to unify. Just check
24375 to see if it matches ARG. */
24377 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24378 && (is_auto (parm
) ? is_auto (arg
)
24379 : same_type_p (parm
, arg
)))
24380 return unify_success (explain_p
);
24382 return unify_type_mismatch (explain_p
, parm
, arg
);
24384 idx
= TEMPLATE_TYPE_IDX (parm
);
24385 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24386 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
24387 if (error_operand_p (tparm
))
24388 return unify_invalid (explain_p
);
24390 /* Check for mixed types and values. */
24391 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24392 && TREE_CODE (tparm
) != TYPE_DECL
)
24393 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24394 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
24395 gcc_unreachable ();
24397 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24399 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
24400 && CLASS_TYPE_P (arg
))
24402 /* First try to match ARG directly. */
24403 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
24407 /* Otherwise, look for a suitable base of ARG, as below. */
24408 enum template_base_result r
;
24409 r
= get_template_base (tparms
, targs
, parm
, arg
,
24412 return unify_no_common_base (explain_p
, r
, parm
, arg
);
24416 /* ARG must be constructed from a template class or a template
24417 template parameter. */
24418 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
24419 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
24420 return unify_template_deduction_failure (explain_p
, parm
, arg
);
24422 /* Deduce arguments T, i from TT<T> or TT<i>. */
24423 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
24426 arg
= TYPE_TI_TEMPLATE (arg
);
24427 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
24428 /* If the template is a template template parameter, use the
24429 TEMPLATE_TEMPLATE_PARM for matching. */
24430 arg
= TREE_TYPE (arg
);
24432 /* Fall through to deduce template name. */
24435 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24436 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24438 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24440 /* Simple cases: Value already set, does match or doesn't. */
24441 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
24442 return unify_success (explain_p
);
24444 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24448 /* If PARM is `const T' and ARG is only `int', we don't have
24449 a match unless we are allowing additional qualification.
24450 If ARG is `const int' and PARM is just `T' that's OK;
24451 that binds `const int' to `T'. */
24452 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
24454 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24456 /* Consider the case where ARG is `const volatile int' and
24457 PARM is `const T'. Then, T should be `volatile int'. */
24458 arg
= cp_build_qualified_type
24459 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
24460 if (arg
== error_mark_node
)
24461 return unify_invalid (explain_p
);
24463 /* Simple cases: Value already set, does match or doesn't. */
24464 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
24465 return unify_success (explain_p
);
24467 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24469 /* Make sure that ARG is not a variable-sized array. (Note
24470 that were talking about variable-sized arrays (like
24471 `int[n]'), rather than arrays of unknown size (like
24472 `int[]').) We'll get very confused by such a type since
24473 the bound of the array is not constant, and therefore
24474 not mangleable. Besides, such types are not allowed in
24475 ISO C++, so we can do as we please here. We do allow
24476 them for 'auto' deduction, since that isn't ABI-exposed. */
24477 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
24478 return unify_vla_arg (explain_p
, arg
);
24480 /* Strip typedefs as in convert_template_argument. */
24481 arg
= canonicalize_type_argument (arg
, tf_none
);
24484 /* If ARG is a parameter pack or an expansion, we cannot unify
24485 against it unless PARM is also a parameter pack. */
24486 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24487 && !template_parameter_pack_p (parm
))
24488 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24490 /* If the argument deduction results is a METHOD_TYPE,
24491 then there is a problem.
24492 METHOD_TYPE doesn't map to any real C++ type the result of
24493 the deduction cannot be of that type. */
24494 if (TREE_CODE (arg
) == METHOD_TYPE
)
24495 return unify_method_type_error (explain_p
, arg
);
24497 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24498 return unify_success (explain_p
);
24500 case TEMPLATE_PARM_INDEX
:
24501 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24502 if (error_operand_p (tparm
))
24503 return unify_invalid (explain_p
);
24505 if (TEMPLATE_PARM_LEVEL (parm
)
24506 != template_decl_level (tparm
))
24508 /* The PARM is not one we're trying to unify. Just check
24509 to see if it matches ARG. */
24510 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
24511 && cp_tree_equal (parm
, arg
));
24513 unify_expression_unequal (explain_p
, parm
, arg
);
24517 idx
= TEMPLATE_PARM_IDX (parm
);
24518 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24522 if ((strict
& UNIFY_ALLOW_INTEGER
)
24523 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
24524 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
24525 /* We're deducing from an array bound, the type doesn't matter. */
24526 arg
= fold_convert (TREE_TYPE (targ
), arg
);
24527 int x
= !cp_tree_equal (targ
, arg
);
24529 unify_inconsistency (explain_p
, parm
, targ
, arg
);
24533 /* [temp.deduct.type] If, in the declaration of a function template
24534 with a non-type template-parameter, the non-type
24535 template-parameter is used in an expression in the function
24536 parameter-list and, if the corresponding template-argument is
24537 deduced, the template-argument type shall match the type of the
24538 template-parameter exactly, except that a template-argument
24539 deduced from an array bound may be of any integral type.
24540 The non-type parameter might use already deduced type parameters. */
24541 tparm
= TREE_TYPE (parm
);
24542 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
24543 /* We don't have enough levels of args to do any substitution. This
24544 can happen in the context of -fnew-ttp-matching. */;
24547 ++processing_template_decl
;
24548 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
24549 --processing_template_decl
;
24551 if (tree a
= type_uses_auto (tparm
))
24553 tparm
= do_auto_deduction (tparm
, arg
, a
,
24554 complain
, adc_unify
, targs
);
24555 if (tparm
== error_mark_node
)
24560 if (!TREE_TYPE (arg
)
24561 || TREE_CODE (TREE_TYPE (arg
)) == DEPENDENT_OPERATOR_TYPE
)
24562 /* Template-parameter dependent expression. Just accept it for now.
24563 It will later be processed in convert_template_argument. */
24565 else if (same_type_ignoring_top_level_qualifiers_p
24566 (non_reference (TREE_TYPE (arg
)),
24567 non_reference (tparm
)))
24568 /* OK. Ignore top-level quals here because a class-type template
24569 parameter object is const. */;
24570 else if ((strict
& UNIFY_ALLOW_INTEGER
)
24571 && CP_INTEGRAL_TYPE_P (tparm
))
24572 /* Convert the ARG to the type of PARM; the deduced non-type
24573 template argument must exactly match the types of the
24574 corresponding parameter. */
24575 arg
= fold (build_nop (tparm
, arg
));
24576 else if (uses_template_parms (tparm
))
24578 /* We haven't deduced the type of this parameter yet. */
24579 if (cxx_dialect
>= cxx17
24580 /* We deduce from array bounds in try_array_deduction. */
24581 && !(strict
& UNIFY_ALLOW_INTEGER
)
24582 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
24584 /* Deduce it from the non-type argument. As above, ignore
24585 top-level quals here too. */
24586 tree atype
= cv_unqualified (TREE_TYPE (arg
));
24587 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24589 UNIFY_ALLOW_NONE
, explain_p
);
24590 /* Now check whether the type of this parameter is still
24591 dependent, and give up if so. */
24592 ++processing_template_decl
;
24593 tparm
= tsubst (TREE_TYPE (parm
), targs
, tf_none
, NULL_TREE
);
24594 --processing_template_decl
;
24595 if (uses_template_parms (tparm
))
24596 return unify_success (explain_p
);
24599 /* Try again later. */
24600 return unify_success (explain_p
);
24603 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
24605 /* If ARG is a parameter pack or an expansion, we cannot unify
24606 against it unless PARM is also a parameter pack. */
24607 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24608 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
24609 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24612 bool removed_attr
= false;
24613 arg
= strip_typedefs_expr (arg
, &removed_attr
);
24615 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24616 return unify_success (explain_p
);
24620 /* A pointer-to-member constant can be unified only with
24621 another constant. */
24622 if (TREE_CODE (arg
) != PTRMEM_CST
)
24623 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
24625 /* Just unify the class member. It would be useless (and possibly
24626 wrong, depending on the strict flags) to unify also
24627 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24628 arg refer to the same variable, even if through different
24629 classes. For instance:
24631 struct A { int x; };
24634 Unification of &A::x and &B::x must succeed. */
24635 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
24636 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
24641 if (!TYPE_PTR_P (arg
))
24642 return unify_type_mismatch (explain_p
, parm
, arg
);
24644 /* [temp.deduct.call]
24646 A can be another pointer or pointer to member type that can
24647 be converted to the deduced A via a qualification
24648 conversion (_conv.qual_).
24650 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24651 This will allow for additional cv-qualification of the
24652 pointed-to types if appropriate. */
24654 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
24655 /* The derived-to-base conversion only persists through one
24656 level of pointers. */
24657 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
24659 return unify (tparms
, targs
, TREE_TYPE (parm
),
24660 TREE_TYPE (arg
), strict
, explain_p
);
24663 case REFERENCE_TYPE
:
24664 if (!TYPE_REF_P (arg
))
24665 return unify_type_mismatch (explain_p
, parm
, arg
);
24666 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24667 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24670 if (TREE_CODE (arg
) != ARRAY_TYPE
)
24671 return unify_type_mismatch (explain_p
, parm
, arg
);
24672 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
24673 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
24674 return unify_type_mismatch (explain_p
, parm
, arg
);
24675 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24676 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24677 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
24678 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24679 TYPE_DOMAIN (arg
), explain_p
);
24680 return unify_success (explain_p
);
24687 case ENUMERAL_TYPE
:
24691 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24692 return unify_type_mismatch (explain_p
, parm
, arg
);
24694 /* We have already checked cv-qualification at the top of the
24696 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
24697 return unify_type_mismatch (explain_p
, parm
, arg
);
24699 /* As far as unification is concerned, this wins. Later checks
24700 will invalidate it if necessary. */
24701 return unify_success (explain_p
);
24703 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
24704 /* Type INTEGER_CST can come from ordinary constant template args. */
24706 while (CONVERT_EXPR_P (arg
))
24707 arg
= TREE_OPERAND (arg
, 0);
24709 if (TREE_CODE (arg
) != INTEGER_CST
)
24710 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24711 return (tree_int_cst_equal (parm
, arg
)
24712 ? unify_success (explain_p
)
24713 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
24717 int i
, len
, argslen
;
24718 int parm_variadic_p
= 0;
24720 if (TREE_CODE (arg
) != TREE_VEC
)
24721 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24723 len
= TREE_VEC_LENGTH (parm
);
24724 argslen
= TREE_VEC_LENGTH (arg
);
24726 /* Check for pack expansions in the parameters. */
24727 for (i
= 0; i
< len
; ++i
)
24729 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
24732 /* We can unify against something with a trailing
24734 parm_variadic_p
= 1;
24736 /* [temp.deduct.type]/9: If the template argument list of
24737 P contains a pack expansion that is not the last
24738 template argument, the entire template argument list
24739 is a non-deduced context. */
24740 return unify_success (explain_p
);
24744 /* If we don't have enough arguments to satisfy the parameters
24745 (not counting the pack expression at the end), or we have
24746 too many arguments for a parameter list that doesn't end in
24747 a pack expression, we can't unify. */
24748 if (parm_variadic_p
24749 ? argslen
< len
- parm_variadic_p
24751 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
24753 /* Unify all of the parameters that precede the (optional)
24754 pack expression. */
24755 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
24757 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24758 TREE_VEC_ELT (parm
, i
),
24759 TREE_VEC_ELT (arg
, i
),
24760 UNIFY_ALLOW_NONE
, explain_p
);
24762 if (parm_variadic_p
)
24763 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
24765 /*subr=*/true, explain_p
);
24766 return unify_success (explain_p
);
24771 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24772 return unify_type_mismatch (explain_p
, parm
, arg
);
24774 if (TYPE_PTRMEMFUNC_P (parm
))
24776 if (!TYPE_PTRMEMFUNC_P (arg
))
24777 return unify_type_mismatch (explain_p
, parm
, arg
);
24779 return unify (tparms
, targs
,
24780 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
24781 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
24782 strict
, explain_p
);
24784 else if (TYPE_PTRMEMFUNC_P (arg
))
24785 return unify_type_mismatch (explain_p
, parm
, arg
);
24787 if (CLASSTYPE_TEMPLATE_INFO (parm
))
24789 tree t
= NULL_TREE
;
24791 if (strict_in
& UNIFY_ALLOW_DERIVED
)
24793 /* First, we try to unify the PARM and ARG directly. */
24794 t
= try_class_unification (tparms
, targs
,
24795 parm
, arg
, explain_p
);
24799 /* Fallback to the special case allowed in
24800 [temp.deduct.call]:
24802 If P is a class, and P has the form
24803 template-id, then A can be a derived class of
24804 the deduced A. Likewise, if P is a pointer to
24805 a class of the form template-id, A can be a
24806 pointer to a derived class pointed to by the
24808 enum template_base_result r
;
24809 r
= get_template_base (tparms
, targs
, parm
, arg
,
24814 /* Don't give the derived diagnostic if we're
24815 already dealing with the same template. */
24817 = (CLASSTYPE_TEMPLATE_INFO (arg
)
24818 && (CLASSTYPE_TI_TEMPLATE (parm
)
24819 == CLASSTYPE_TI_TEMPLATE (arg
)));
24820 return unify_no_common_base (explain_p
&& !same_template
,
24825 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
24826 && (CLASSTYPE_TI_TEMPLATE (parm
)
24827 == CLASSTYPE_TI_TEMPLATE (arg
)))
24828 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24829 Then, we should unify `int' and `U'. */
24832 /* There's no chance of unification succeeding. */
24833 return unify_type_mismatch (explain_p
, parm
, arg
);
24835 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
24836 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
24838 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
24839 return unify_type_mismatch (explain_p
, parm
, arg
);
24840 return unify_success (explain_p
);
24843 case FUNCTION_TYPE
:
24845 unsigned int nargs
;
24850 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24851 return unify_type_mismatch (explain_p
, parm
, arg
);
24853 /* CV qualifications for methods can never be deduced, they must
24854 match exactly. We need to check them explicitly here,
24855 because type_unification_real treats them as any other
24856 cv-qualified parameter. */
24857 if (TREE_CODE (parm
) == METHOD_TYPE
24858 && (!check_cv_quals_for_unify
24860 class_of_this_parm (arg
),
24861 class_of_this_parm (parm
))))
24862 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24863 if (TREE_CODE (arg
) == FUNCTION_TYPE
24864 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
24865 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24866 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
24867 return unify_type_mismatch (explain_p
, parm
, arg
);
24869 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
24870 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
24872 nargs
= list_length (TYPE_ARG_TYPES (arg
));
24873 args
= XALLOCAVEC (tree
, nargs
);
24874 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
24875 a
!= NULL_TREE
&& a
!= void_list_node
;
24876 a
= TREE_CHAIN (a
), ++i
)
24877 args
[i
] = TREE_VALUE (a
);
24880 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
24881 args
, nargs
, 1, DEDUCE_EXACT
,
24885 if (flag_noexcept_type
)
24887 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
24888 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
24889 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
24890 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
24891 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
24892 && uses_template_parms (TREE_PURPOSE (pspec
)))
24893 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
24894 TREE_PURPOSE (aspec
),
24895 UNIFY_ALLOW_NONE
, explain_p
);
24896 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
24897 return unify_type_mismatch (explain_p
, parm
, arg
);
24904 /* Unify a pointer to member with a pointer to member function, which
24905 deduces the type of the member as a function type. */
24906 if (TYPE_PTRMEMFUNC_P (arg
))
24908 /* Check top-level cv qualifiers */
24909 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
24910 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24912 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24913 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
24914 UNIFY_ALLOW_NONE
, explain_p
);
24916 /* Determine the type of the function we are unifying against. */
24917 tree fntype
= static_fn_type (arg
);
24919 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
24922 if (TREE_CODE (arg
) != OFFSET_TYPE
)
24923 return unify_type_mismatch (explain_p
, parm
, arg
);
24924 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24925 TYPE_OFFSET_BASETYPE (arg
),
24926 UNIFY_ALLOW_NONE
, explain_p
);
24927 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24928 strict
, explain_p
);
24931 if (DECL_TEMPLATE_PARM_P (parm
))
24932 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
24933 if (arg
!= scalar_constant_value (parm
))
24934 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24935 return unify_success (explain_p
);
24938 case TEMPLATE_DECL
:
24939 /* Matched cases are handled by the ARG == PARM test above. */
24940 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24943 /* We might get a variable as a non-type template argument in parm if the
24944 corresponding parameter is type-dependent. Make any necessary
24945 adjustments based on whether arg is a reference. */
24946 if (CONSTANT_CLASS_P (arg
))
24947 parm
= fold_non_dependent_expr (parm
, complain
);
24948 else if (REFERENCE_REF_P (arg
))
24950 tree sub
= TREE_OPERAND (arg
, 0);
24952 if (TREE_CODE (sub
) == ADDR_EXPR
)
24953 arg
= TREE_OPERAND (sub
, 0);
24955 /* Now use the normal expression code to check whether they match. */
24958 case TYPE_ARGUMENT_PACK
:
24959 case NONTYPE_ARGUMENT_PACK
:
24960 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
24961 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
24964 case DECLTYPE_TYPE
:
24966 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24967 or TRAIT_TYPE nodes. */
24968 return unify_success (explain_p
);
24971 /* Unification fails if we hit an error node. */
24972 return unify_invalid (explain_p
);
24975 if (REFERENCE_REF_P (parm
))
24977 bool pexp
= PACK_EXPANSION_P (arg
);
24979 arg
= PACK_EXPANSION_PATTERN (arg
);
24980 if (REFERENCE_REF_P (arg
))
24981 arg
= TREE_OPERAND (arg
, 0);
24983 arg
= make_pack_expansion (arg
, complain
);
24984 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
24985 strict
, explain_p
);
24990 /* An unresolved overload is a nondeduced context. */
24991 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
24992 return unify_success (explain_p
);
24993 gcc_assert (EXPR_P (parm
)
24994 || TREE_CODE (parm
) == CONSTRUCTOR
24995 || TREE_CODE (parm
) == TRAIT_EXPR
);
24997 /* We must be looking at an expression. This can happen with
25001 void foo(S<I>, S<I + 2>);
25005 template<typename T>
25006 void foo(A<T, T{}>);
25008 This is a "non-deduced context":
25012 The non-deduced contexts are:
25014 --A non-type template argument or an array bound in which
25015 a subexpression references a template parameter.
25017 In these cases, we assume deduction succeeded, but don't
25018 actually infer any unifications. */
25020 if (!uses_template_parms (parm
)
25021 && !template_args_equal (parm
, arg
))
25022 return unify_expression_unequal (explain_p
, parm
, arg
);
25024 return unify_success (explain_p
);
25027 #undef RECUR_AND_CHECK_FAILURE
25029 /* Note that DECL can be defined in this translation unit, if
25033 mark_definable (tree decl
)
25036 DECL_NOT_REALLY_EXTERN (decl
) = 1;
25037 FOR_EACH_CLONE (clone
, decl
)
25038 DECL_NOT_REALLY_EXTERN (clone
) = 1;
25041 /* Called if RESULT is explicitly instantiated, or is a member of an
25042 explicitly instantiated class. */
25045 mark_decl_instantiated (tree result
, int extern_p
)
25047 SET_DECL_EXPLICIT_INSTANTIATION (result
);
25049 /* If this entity has already been written out, it's too late to
25050 make any modifications. */
25051 if (TREE_ASM_WRITTEN (result
))
25054 /* consteval functions are never emitted. */
25055 if (TREE_CODE (result
) == FUNCTION_DECL
25056 && DECL_IMMEDIATE_FUNCTION_P (result
))
25059 /* For anonymous namespace we don't need to do anything. */
25060 if (decl_internal_context_p (result
))
25062 gcc_assert (!TREE_PUBLIC (result
));
25066 if (TREE_CODE (result
) != FUNCTION_DECL
)
25067 /* The TREE_PUBLIC flag for function declarations will have been
25068 set correctly by tsubst. */
25069 TREE_PUBLIC (result
) = 1;
25073 DECL_EXTERNAL (result
) = 1;
25074 DECL_NOT_REALLY_EXTERN (result
) = 0;
25078 mark_definable (result
);
25079 mark_needed (result
);
25080 /* Always make artificials weak. */
25081 if (DECL_ARTIFICIAL (result
) && flag_weak
)
25082 comdat_linkage (result
);
25083 /* For WIN32 we also want to put explicit instantiations in
25084 linkonce sections. */
25085 else if (TREE_PUBLIC (result
))
25086 maybe_make_one_only (result
);
25087 if (TREE_CODE (result
) == FUNCTION_DECL
25088 && DECL_TEMPLATE_INSTANTIATED (result
))
25089 /* If the function has already been instantiated, clear DECL_EXTERNAL,
25090 since start_preparsed_function wouldn't have if we had an earlier
25091 extern explicit instantiation. */
25092 DECL_EXTERNAL (result
) = 0;
25095 /* If EXTERN_P, then this function will not be emitted -- unless
25096 followed by an explicit instantiation, at which point its linkage
25097 will be adjusted. If !EXTERN_P, then this function will be
25098 emitted here. In neither circumstance do we want
25099 import_export_decl to adjust the linkage. */
25100 DECL_INTERFACE_KNOWN (result
) = 1;
25103 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
25104 important template arguments. If any are missing, we check whether
25105 they're important by using error_mark_node for substituting into any
25106 args that were used for partial ordering (the ones between ARGS and END)
25107 and seeing if it bubbles up. */
25110 check_undeduced_parms (tree targs
, tree args
, tree end
)
25112 bool found
= false;
25113 for (tree
& targ
: tree_vec_range (targs
))
25114 if (targ
== NULL_TREE
)
25117 targ
= error_mark_node
;
25121 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
25122 if (substed
== error_mark_node
)
25128 /* Given two function templates PAT1 and PAT2, return:
25130 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
25131 -1 if PAT2 is more specialized than PAT1.
25132 0 if neither is more specialized.
25134 LEN indicates the number of parameters we should consider
25135 (defaulted parameters should not be considered).
25137 The 1998 std underspecified function template partial ordering, and
25138 DR214 addresses the issue. We take pairs of arguments, one from
25139 each of the templates, and deduce them against each other. One of
25140 the templates will be more specialized if all the *other*
25141 template's arguments deduce against its arguments and at least one
25142 of its arguments *does* *not* deduce against the other template's
25143 corresponding argument. Deduction is done as for class templates.
25144 The arguments used in deduction have reference and top level cv
25145 qualifiers removed. Iff both arguments were originally reference
25146 types *and* deduction succeeds in both directions, an lvalue reference
25147 wins against an rvalue reference and otherwise the template
25148 with the more cv-qualified argument wins for that pairing (if
25149 neither is more cv-qualified, they both are equal). Unlike regular
25150 deduction, after all the arguments have been deduced in this way,
25151 we do *not* verify the deduced template argument values can be
25152 substituted into non-deduced contexts.
25154 The logic can be a bit confusing here, because we look at deduce1 and
25155 targs1 to see if pat2 is at least as specialized, and vice versa; if we
25156 can find template arguments for pat1 to make arg1 look like arg2, that
25157 means that arg2 is at least as specialized as arg1. */
25160 more_specialized_fn (tree pat1
, tree pat2
, int len
)
25162 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
25163 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
25164 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
25165 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
25166 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
25167 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
25168 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
25169 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
25170 tree origs1
, origs2
;
25171 bool lose1
= false;
25172 bool lose2
= false;
25174 /* Remove the this parameter from non-static member functions. If
25175 one is a non-static member function and the other is not a static
25176 member function, remove the first parameter from that function
25177 also. This situation occurs for operator functions where we
25178 locate both a member function (with this pointer) and non-member
25179 operator (with explicit first operand). */
25180 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
25182 len
--; /* LEN is the number of significant arguments for DECL1 */
25183 args1
= TREE_CHAIN (args1
);
25184 if (!DECL_STATIC_FUNCTION_P (decl2
))
25185 args2
= TREE_CHAIN (args2
);
25187 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
25189 args2
= TREE_CHAIN (args2
);
25190 if (!DECL_STATIC_FUNCTION_P (decl1
))
25193 args1
= TREE_CHAIN (args1
);
25197 /* If only one is a conversion operator, they are unordered. */
25198 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
25201 /* Consider the return type for a conversion function */
25202 if (DECL_CONV_FN_P (decl1
))
25204 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
25205 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
25209 processing_template_decl
++;
25215 /* Stop when an ellipsis is seen. */
25216 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
25218 tree arg1
= TREE_VALUE (args1
);
25219 tree arg2
= TREE_VALUE (args2
);
25220 int deduce1
, deduce2
;
25226 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25227 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25229 /* When both arguments are pack expansions, we need only
25230 unify the patterns themselves. */
25231 arg1
= PACK_EXPANSION_PATTERN (arg1
);
25232 arg2
= PACK_EXPANSION_PATTERN (arg2
);
25234 /* This is the last comparison we need to do. */
25238 if (TYPE_REF_P (arg1
))
25240 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
25241 arg1
= TREE_TYPE (arg1
);
25242 quals1
= cp_type_quals (arg1
);
25245 if (TYPE_REF_P (arg2
))
25247 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
25248 arg2
= TREE_TYPE (arg2
);
25249 quals2
= cp_type_quals (arg2
);
25252 arg1
= TYPE_MAIN_VARIANT (arg1
);
25253 arg2
= TYPE_MAIN_VARIANT (arg2
);
25255 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
25257 int i
, len2
= remaining_arguments (args2
);
25258 tree parmvec
= make_tree_vec (1);
25259 tree argvec
= make_tree_vec (len2
);
25262 /* Setup the parameter vector, which contains only ARG1. */
25263 TREE_VEC_ELT (parmvec
, 0) = arg1
;
25265 /* Setup the argument vector, which contains the remaining
25267 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
25268 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25270 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
25271 argvec
, DEDUCE_EXACT
,
25272 /*subr=*/true, /*explain_p=*/false)
25275 /* We cannot deduce in the other direction, because ARG1 is
25276 a pack expansion but ARG2 is not. */
25279 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25281 int i
, len1
= remaining_arguments (args1
);
25282 tree parmvec
= make_tree_vec (1);
25283 tree argvec
= make_tree_vec (len1
);
25286 /* Setup the parameter vector, which contains only ARG1. */
25287 TREE_VEC_ELT (parmvec
, 0) = arg2
;
25289 /* Setup the argument vector, which contains the remaining
25291 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
25292 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25294 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
25295 argvec
, DEDUCE_EXACT
,
25296 /*subr=*/true, /*explain_p=*/false)
25299 /* We cannot deduce in the other direction, because ARG2 is
25300 a pack expansion but ARG1 is not.*/
25306 /* The normal case, where neither argument is a pack
25308 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
25309 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25311 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
25312 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25316 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25317 arg2, then arg2 is not as specialized as arg1. */
25323 /* "If, for a given type, deduction succeeds in both directions
25324 (i.e., the types are identical after the transformations above)
25325 and both P and A were reference types (before being replaced with
25326 the type referred to above):
25327 - if the type from the argument template was an lvalue reference and
25328 the type from the parameter template was not, the argument type is
25329 considered to be more specialized than the other; otherwise,
25330 - if the type from the argument template is more cv-qualified
25331 than the type from the parameter template (as described above),
25332 the argument type is considered to be more specialized than the other;
25334 - neither type is more specialized than the other." */
25336 if (deduce1
&& deduce2
)
25338 if (ref1
&& ref2
&& ref1
!= ref2
)
25345 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
25347 if ((quals1
& quals2
) == quals2
)
25349 if ((quals1
& quals2
) == quals1
)
25354 if (lose1
&& lose2
)
25355 /* We've failed to deduce something in either direction.
25356 These must be unordered. */
25359 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25360 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25361 /* We have already processed all of the arguments in our
25362 handing of the pack expansion type. */
25365 args1
= TREE_CHAIN (args1
);
25366 args2
= TREE_CHAIN (args2
);
25369 /* "In most cases, all template parameters must have values in order for
25370 deduction to succeed, but for partial ordering purposes a template
25371 parameter may remain without a value provided it is not used in the
25372 types being used for partial ordering."
25374 Thus, if we are missing any of the targs1 we need to substitute into
25375 origs1, then pat2 is not as specialized as pat1. This can happen when
25376 there is a nondeduced context. */
25377 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
25379 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
25382 processing_template_decl
--;
25384 /* If both deductions succeed, the partial ordering selects the more
25385 constrained template. */
25386 /* P2113: If the corresponding template-parameters of the
25387 template-parameter-lists are not equivalent ([temp.over.link]) or if
25388 the function parameters that positionally correspond between the two
25389 templates are not of the same type, neither template is more
25390 specialized than the other. */
25391 if (!lose1
&& !lose2
25392 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
25393 DECL_TEMPLATE_PARMS (pat2
))
25394 && compparms (origs1
, origs2
))
25396 int winner
= more_constrained (decl1
, decl2
);
25399 else if (winner
< 0)
25403 /* All things being equal, if the next argument is a pack expansion
25404 for one function but not for the other, prefer the
25405 non-variadic function. FIXME this is bogus; see c++/41958. */
25407 && args1
&& TREE_VALUE (args1
)
25408 && args2
&& TREE_VALUE (args2
))
25410 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
25411 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
25414 if (lose1
== lose2
)
25422 /* Determine which of two partial specializations of TMPL is more
25425 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25426 to the first partial specialization. The TREE_PURPOSE is the
25427 innermost set of template parameters for the partial
25428 specialization. PAT2 is similar, but for the second template.
25430 Return 1 if the first partial specialization is more specialized;
25431 -1 if the second is more specialized; 0 if neither is more
25434 See [temp.class.order] for information about determining which of
25435 two templates is more specialized. */
25438 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
25442 bool any_deductions
= false;
25444 tree tmpl1
= TREE_VALUE (pat1
);
25445 tree tmpl2
= TREE_VALUE (pat2
);
25446 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
25447 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
25449 /* Just like what happens for functions, if we are ordering between
25450 different template specializations, we may encounter dependent
25451 types in the arguments, and we need our dependency check functions
25452 to behave correctly. */
25453 ++processing_template_decl
;
25454 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
25458 any_deductions
= true;
25461 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
25465 any_deductions
= true;
25467 --processing_template_decl
;
25469 /* If both deductions succeed, the partial ordering selects the more
25470 constrained template. */
25471 if (!winner
&& any_deductions
)
25472 winner
= more_constrained (tmpl1
, tmpl2
);
25474 /* In the case of a tie where at least one of the templates
25475 has a parameter pack at the end, the template with the most
25476 non-packed parameters wins. */
25479 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
25480 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
25482 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
25483 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
25484 int len1
= TREE_VEC_LENGTH (args1
);
25485 int len2
= TREE_VEC_LENGTH (args2
);
25487 /* We don't count the pack expansion at the end. */
25488 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
25490 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
25495 else if (len1
< len2
)
25502 /* Return the template arguments that will produce the function signature
25503 DECL from the function template FN, with the explicit template
25504 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25505 also match. Return NULL_TREE if no satisfactory arguments could be
25509 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
25511 int ntparms
= DECL_NTPARMS (fn
);
25512 tree targs
= make_tree_vec (ntparms
);
25513 tree decl_type
= TREE_TYPE (decl
);
25514 tree decl_arg_types
;
25516 unsigned int nargs
, ix
;
25519 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
25521 /* Never do unification on the 'this' parameter. */
25522 decl_arg_types
= skip_artificial_parms_for (decl
,
25523 TYPE_ARG_TYPES (decl_type
));
25525 nargs
= list_length (decl_arg_types
);
25526 args
= XALLOCAVEC (tree
, nargs
);
25527 for (arg
= decl_arg_types
, ix
= 0;
25529 arg
= TREE_CHAIN (arg
), ++ix
)
25530 args
[ix
] = TREE_VALUE (arg
);
25532 if (fn_type_unification (fn
, explicit_args
, targs
,
25534 (check_rettype
|| DECL_CONV_FN_P (fn
)
25535 ? TREE_TYPE (decl_type
) : NULL_TREE
),
25536 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
25537 /*explain_p=*/false,
25539 == error_mark_node
)
25545 /* Return the innermost template arguments that, when applied to a partial
25546 specialization SPEC_TMPL of TMPL, yield the ARGS.
25548 For example, suppose we have:
25550 template <class T, class U> struct S {};
25551 template <class T> struct S<T*, int> {};
25553 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25554 partial specialization and the ARGS will be {double*, int}. The resulting
25555 vector will be {double}, indicating that `T' is bound to `double'. */
25558 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
25560 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
25562 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
25563 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
25565 tree innermost_deduced_args
;
25567 innermost_deduced_args
= make_tree_vec (ntparms
);
25568 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25570 deduced_args
= copy_node (args
);
25571 SET_TMPL_ARGS_LEVEL (deduced_args
,
25572 TMPL_ARGS_DEPTH (deduced_args
),
25573 innermost_deduced_args
);
25576 deduced_args
= innermost_deduced_args
;
25578 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
25580 if (unify (tparms
, deduced_args
,
25581 INNERMOST_TEMPLATE_ARGS (spec_args
),
25582 INNERMOST_TEMPLATE_ARGS (args
),
25583 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
25586 for (i
= 0; i
< ntparms
; ++i
)
25587 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
25589 if (!tried_array_deduction
)
25591 try_array_deduction (tparms
, innermost_deduced_args
,
25592 INNERMOST_TEMPLATE_ARGS (spec_args
));
25593 tried_array_deduction
= true;
25594 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
25600 if (!push_tinst_level (spec_tmpl
, deduced_args
))
25602 excessive_deduction_depth
= true;
25606 /* Verify that nondeduced template arguments agree with the type
25607 obtained from argument deduction.
25611 struct A { typedef int X; };
25612 template <class T, class U> struct C {};
25613 template <class T> struct C<T, typename T::X> {};
25615 Then with the instantiation `C<A, int>', we can deduce that
25616 `T' is `A' but unify () does not check whether `typename T::X'
25618 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
25620 if (spec_args
!= error_mark_node
)
25621 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
25622 INNERMOST_TEMPLATE_ARGS (spec_args
),
25623 tmpl
, tf_none
, false);
25625 pop_tinst_level ();
25627 if (spec_args
== error_mark_node
25628 /* We only need to check the innermost arguments; the other
25629 arguments will always agree. */
25630 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
25631 INNERMOST_TEMPLATE_ARGS (args
)))
25634 /* Now that we have bindings for all of the template arguments,
25635 ensure that the arguments deduced for the template template
25636 parameters have compatible template parameter lists. See the use
25637 of template_template_parm_bindings_ok_p in fn_type_unification
25638 for more information. */
25639 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
25642 return deduced_args
;
25645 // Compare two function templates T1 and T2 by deducing bindings
25646 // from one against the other. If both deductions succeed, compare
25647 // constraints to see which is more constrained.
25649 more_specialized_inst (tree t1
, tree t2
)
25654 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
25660 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
25666 // If both deductions succeed, then one may be more constrained.
25667 if (count
== 2 && fate
== 0)
25668 fate
= more_constrained (t1
, t2
);
25673 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
25674 Return the TREE_LIST node with the most specialized template, if
25675 any. If there is no most specialized template, the error_mark_node
25678 Note that this function does not look at, or modify, the
25679 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
25680 returned is one of the elements of INSTANTIATIONS, callers may
25681 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
25682 and retrieve it from the value returned. */
25685 most_specialized_instantiation (tree templates
)
25689 ++processing_template_decl
;
25692 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
25694 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
25695 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
25700 /* Equally specialized, move to next function. If there
25701 is no next function, nothing's most specialized. */
25702 fn
= TREE_CHAIN (fn
);
25710 /* Now verify that champ is better than everything earlier in the
25711 instantiation list. */
25712 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
25713 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
25720 processing_template_decl
--;
25723 return error_mark_node
;
25728 /* If DECL is a specialization of some template, return the most
25729 general such template. Otherwise, returns NULL_TREE.
25731 For example, given:
25733 template <class T> struct S { template <class U> void f(U); };
25735 if TMPL is `template <class U> void S<int>::f(U)' this will return
25736 the full template. This function will not trace past partial
25737 specializations, however. For example, given in addition:
25739 template <class T> struct S<T*> { template <class U> void f(U); };
25741 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25742 `template <class T> template <class U> S<T*>::f(U)'. */
25745 most_general_template (tree decl
)
25747 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25749 if (tree tinfo
= get_template_info (decl
))
25750 decl
= TI_TEMPLATE (tinfo
);
25751 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25752 template friend, or a FIELD_DECL for a capture pack. */
25753 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25757 /* Look for more and more general templates. */
25758 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
25760 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25761 (See cp-tree.h for details.) */
25762 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
25765 if (CLASS_TYPE_P (TREE_TYPE (decl
))
25766 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
25767 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
25770 /* Stop if we run into an explicitly specialized class template. */
25771 if (!DECL_NAMESPACE_SCOPE_P (decl
)
25772 && DECL_CONTEXT (decl
)
25773 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
25776 decl
= DECL_TI_TEMPLATE (decl
);
25782 /* Return the most specialized of the template partial specializations
25783 which can produce TARGET, a specialization of some class or variable
25784 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25785 a TEMPLATE_DECL node corresponding to the partial specialization, while
25786 the TREE_PURPOSE is the set of template arguments that must be
25787 substituted into the template pattern in order to generate TARGET.
25789 If the choice of partial specialization is ambiguous, a diagnostic
25790 is issued, and the error_mark_node is returned. If there are no
25791 partial specializations matching TARGET, then NULL_TREE is
25792 returned, indicating that the primary template should be used. */
25795 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
25797 tree list
= NULL_TREE
;
25802 tree outer_args
= NULL_TREE
;
25806 if (TYPE_P (target
))
25808 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
25809 tmpl
= TI_TEMPLATE (tinfo
);
25810 args
= TI_ARGS (tinfo
);
25811 decl
= TYPE_NAME (target
);
25813 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
25815 tmpl
= TREE_OPERAND (target
, 0);
25816 args
= TREE_OPERAND (target
, 1);
25817 decl
= DECL_TEMPLATE_RESULT (tmpl
);
25819 else if (VAR_P (target
))
25821 tree tinfo
= DECL_TEMPLATE_INFO (target
);
25822 tmpl
= TI_TEMPLATE (tinfo
);
25823 args
= TI_ARGS (tinfo
);
25827 gcc_unreachable ();
25829 push_access_scope_guard
pas (decl
);
25830 deferring_access_check_sentinel
acs (dk_no_deferred
);
25832 tree main_tmpl
= most_general_template (tmpl
);
25834 /* For determining which partial specialization to use, only the
25835 innermost args are interesting. */
25836 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25838 outer_args
= strip_innermost_template_args (args
, 1);
25839 args
= INNERMOST_TEMPLATE_ARGS (args
);
25842 /* The caller hasn't called push_to_top_level yet, but we need
25843 get_partial_spec_bindings to be done in non-template context so that we'll
25844 fully resolve everything. */
25845 processing_template_decl_sentinel ptds
;
25847 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
25849 const tree ospec_tmpl
= TREE_VALUE (t
);
25854 /* Substitute in the template args from the enclosing class. */
25855 ++processing_template_decl
;
25856 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
25857 --processing_template_decl
;
25858 if (spec_tmpl
== error_mark_node
)
25859 return error_mark_node
;
25862 spec_tmpl
= ospec_tmpl
;
25864 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
25868 spec_args
= add_to_template_args (outer_args
, spec_args
);
25870 /* Keep the candidate only if the constraints are satisfied,
25871 or if we're not compiling with concepts. */
25873 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
25875 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
25876 TREE_TYPE (list
) = TREE_TYPE (t
);
25884 ambiguous_p
= false;
25887 t
= TREE_CHAIN (t
);
25888 for (; t
; t
= TREE_CHAIN (t
))
25890 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25897 t
= TREE_CHAIN (t
);
25900 ambiguous_p
= true;
25909 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
25911 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25914 ambiguous_p
= true;
25922 char *spaces
= NULL
;
25923 if (!(complain
& tf_error
))
25924 return error_mark_node
;
25925 if (TYPE_P (target
))
25926 error ("ambiguous template instantiation for %q#T", target
);
25928 error ("ambiguous template instantiation for %q#D", target
);
25929 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
25930 for (t
= list
; t
; t
= TREE_CHAIN (t
))
25932 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
25933 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
25934 "%s %#qS", spaces
? spaces
: str
, subst
);
25935 spaces
= spaces
? spaces
: get_spaces (str
);
25938 return error_mark_node
;
25944 /* Explicitly instantiate DECL. */
25947 do_decl_instantiation (tree decl
, tree storage
)
25949 tree result
= NULL_TREE
;
25952 if (!decl
|| decl
== error_mark_node
)
25953 /* An error occurred, for which grokdeclarator has already issued
25954 an appropriate message. */
25956 else if (! DECL_LANG_SPECIFIC (decl
))
25958 error ("explicit instantiation of non-template %q#D", decl
);
25961 else if (DECL_DECLARED_CONCEPT_P (decl
))
25964 error ("explicit instantiation of variable concept %q#D", decl
);
25966 error ("explicit instantiation of function concept %q#D", decl
);
25970 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
25971 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
25973 if (VAR_P (decl
) && !var_templ
)
25975 /* There is an asymmetry here in the way VAR_DECLs and
25976 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25977 the latter, the DECL we get back will be marked as a
25978 template instantiation, and the appropriate
25979 DECL_TEMPLATE_INFO will be set up. This does not happen for
25980 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25981 should handle VAR_DECLs as it currently handles
25983 if (!DECL_CLASS_SCOPE_P (decl
))
25985 error ("%qD is not a static data member of a class template", decl
);
25988 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
25989 if (!result
|| !VAR_P (result
))
25991 error ("no matching template for %qD found", decl
);
25994 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
25996 error ("type %qT for explicit instantiation %qD does not match "
25997 "declared type %qT", TREE_TYPE (result
), decl
,
26002 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
26004 error ("explicit instantiation of %q#D", decl
);
26010 /* Check for various error cases. Note that if the explicit
26011 instantiation is valid the RESULT will currently be marked as an
26012 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
26013 until we get here. */
26015 if (DECL_TEMPLATE_SPECIALIZATION (result
))
26017 /* DR 259 [temp.spec].
26019 Both an explicit instantiation and a declaration of an explicit
26020 specialization shall not appear in a program unless the explicit
26021 instantiation follows a declaration of the explicit specialization.
26023 For a given set of template parameters, if an explicit
26024 instantiation of a template appears after a declaration of an
26025 explicit specialization for that template, the explicit
26026 instantiation has no effect. */
26029 else if (DECL_EXPLICIT_INSTANTIATION (result
))
26033 No program shall explicitly instantiate any template more
26036 We check DECL_NOT_REALLY_EXTERN so as not to complain when
26037 the first instantiation was `extern' and the second is not,
26038 and EXTERN_P for the opposite case. */
26039 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
26040 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
26041 /* If an "extern" explicit instantiation follows an ordinary
26042 explicit instantiation, the template is instantiated. */
26046 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
26048 error ("no matching template for %qD found", result
);
26051 else if (!DECL_TEMPLATE_INFO (result
))
26053 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
26057 if (storage
== NULL_TREE
)
26059 else if (storage
== ridpointers
[(int) RID_EXTERN
])
26061 if (cxx_dialect
== cxx98
)
26062 pedwarn (input_location
, OPT_Wpedantic
,
26063 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
26068 error ("storage class %qD applied to template instantiation", storage
);
26070 check_explicit_instantiation_namespace (result
);
26071 mark_decl_instantiated (result
, extern_p
);
26073 instantiate_decl (result
, /*defer_ok=*/true,
26074 /*expl_inst_class_mem_p=*/false);
26078 mark_class_instantiated (tree t
, int extern_p
)
26080 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
26081 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
26082 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
26083 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
26086 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
26087 rest_of_type_compilation (t
, 1);
26091 /* Perform an explicit instantiation of template class T. STORAGE, if
26092 non-null, is the RID for extern, inline or static. COMPLAIN is
26093 nonzero if this is called from the parser, zero if called recursively,
26094 since the standard is unclear (as detailed below). */
26097 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
26099 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
26101 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
26102 error ("explicit instantiation of non-class template %qD",
26105 error ("explicit instantiation of non-template type %qT", t
);
26111 if (!COMPLETE_TYPE_P (t
))
26113 if (complain
& tf_error
)
26114 error ("explicit instantiation of %q#T before definition of template",
26119 /* At most one of these will be true. */
26120 bool extern_p
= false;
26121 bool nomem_p
= false;
26122 bool static_p
= false;
26124 if (storage
!= NULL_TREE
)
26126 if (storage
== ridpointers
[(int) RID_EXTERN
])
26128 if (cxx_dialect
== cxx98
)
26129 pedwarn (input_location
, OPT_Wpedantic
,
26130 "ISO C++ 1998 forbids the use of %<extern%> on "
26131 "explicit instantiations");
26134 pedwarn (input_location
, OPT_Wpedantic
,
26135 "ISO C++ forbids the use of %qE"
26136 " on explicit instantiations", storage
);
26138 if (storage
== ridpointers
[(int) RID_INLINE
])
26140 else if (storage
== ridpointers
[(int) RID_EXTERN
])
26142 else if (storage
== ridpointers
[(int) RID_STATIC
])
26145 error ("storage class %qD applied to template instantiation",
26149 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
26150 /* DR 259 [temp.spec].
26152 Both an explicit instantiation and a declaration of an explicit
26153 specialization shall not appear in a program unless the
26154 explicit instantiation follows a declaration of the explicit
26157 For a given set of template parameters, if an explicit
26158 instantiation of a template appears after a declaration of an
26159 explicit specialization for that template, the explicit
26160 instantiation has no effect. */
26163 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
26165 /* We've already instantiated the template. */
26169 No program shall explicitly instantiate any template more
26172 If EXTERN_P then this is ok. */
26173 if (!extern_p
&& (complain
& tf_error
))
26174 permerror (input_location
,
26175 "duplicate explicit instantiation of %q#T", t
);
26180 check_explicit_instantiation_namespace (TYPE_NAME (t
));
26181 mark_class_instantiated (t
, extern_p
);
26186 /* In contrast to implicit instantiation, where only the
26187 declarations, and not the definitions, of members are
26188 instantiated, we have here:
26192 An explicit instantiation that names a class template
26193 specialization is also an explicit instantiation of the same
26194 kind (declaration or definition) of each of its members (not
26195 including members inherited from base classes and members
26196 that are templates) that has not been previously explicitly
26197 specialized in the translation unit containing the explicit
26198 instantiation, provided that the associated constraints, if
26199 any, of that member are satisfied by the template arguments
26200 of the explicit instantiation. */
26201 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
26203 || (TREE_CODE (fld
) == FUNCTION_DECL
26205 && user_provided_p (fld
)))
26206 && DECL_TEMPLATE_INSTANTIATION (fld
)
26207 && constraints_satisfied_p (fld
))
26209 mark_decl_instantiated (fld
, extern_p
);
26211 instantiate_decl (fld
, /*defer_ok=*/true,
26212 /*expl_inst_class_mem_p=*/true);
26214 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
26216 tree type
= TREE_TYPE (fld
);
26218 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26219 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
26220 do_type_instantiation (type
, storage
, 0);
26224 /* Given a function DECL, which is a specialization of TMPL, modify
26225 DECL to be a re-instantiation of TMPL with the same template
26226 arguments. TMPL should be the template into which tsubst'ing
26227 should occur for DECL, not the most general template.
26229 One reason for doing this is a scenario like this:
26232 void f(const T&, int i);
26234 void g() { f(3, 7); }
26237 void f(const T& t, const int i) { }
26239 Note that when the template is first instantiated, with
26240 instantiate_template, the resulting DECL will have no name for the
26241 first parameter, and the wrong type for the second. So, when we go
26242 to instantiate the DECL, we regenerate it. */
26245 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
26247 /* The arguments used to instantiate DECL, from the most general
26249 tree code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
26251 /* Make sure that we can see identifiers, and compute access correctly. */
26252 push_access_scope (decl
);
26254 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26260 /* Use the source location of the definition. */
26261 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (tmpl
);
26263 args_depth
= TMPL_ARGS_DEPTH (args
);
26264 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
26265 if (args_depth
> parms_depth
)
26266 args
= get_innermost_template_args (args
, parms_depth
);
26268 /* Instantiate a dynamic exception-specification. noexcept will be
26270 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
26271 if (TREE_VALUE (raises
))
26273 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
26274 args
, tf_error
, NULL_TREE
,
26275 /*defer_ok*/false);
26276 if (specs
&& specs
!= error_mark_node
)
26277 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
26281 /* Merge parameter declarations. */
26282 if (tree pattern_parm
26283 = skip_artificial_parms_for (code_pattern
,
26284 DECL_ARGUMENTS (code_pattern
)))
26286 tree
*p
= &DECL_ARGUMENTS (decl
);
26287 for (int skip
= num_artificial_parms_for (decl
); skip
; --skip
)
26288 p
= &DECL_CHAIN (*p
);
26289 *p
= tsubst_decl (pattern_parm
, args
, tf_error
);
26290 for (tree t
= *p
; t
; t
= DECL_CHAIN (t
))
26291 DECL_CONTEXT (t
) = decl
;
26294 if (DECL_CONTRACTS (decl
))
26296 /* If we're regenerating a specialization, the contracts will have
26297 been copied from the most general template. Replace those with
26298 the ones from the actual specialization. */
26299 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26300 if (DECL_TEMPLATE_SPECIALIZATION (tmpl
))
26302 remove_contract_attributes (decl
);
26303 copy_contract_attributes (decl
, code_pattern
);
26306 tsubst_contract_attributes (decl
, args
, tf_warning_or_error
, code_pattern
);
26309 /* Merge additional specifiers from the CODE_PATTERN. */
26310 if (DECL_DECLARED_INLINE_P (code_pattern
)
26311 && !DECL_DECLARED_INLINE_P (decl
))
26312 DECL_DECLARED_INLINE_P (decl
) = 1;
26314 maybe_instantiate_noexcept (decl
, tf_error
);
26316 else if (VAR_P (decl
))
26318 start_lambda_scope (decl
);
26319 DECL_INITIAL (decl
) =
26320 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
26321 tf_error
, DECL_TI_TEMPLATE (decl
));
26322 finish_lambda_scope ();
26323 if (VAR_HAD_UNKNOWN_BOUND (decl
))
26324 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
26325 tf_error
, DECL_TI_TEMPLATE (decl
));
26328 gcc_unreachable ();
26330 pop_access_scope (decl
);
26333 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26334 substituted to get DECL. */
26337 template_for_substitution (tree decl
)
26339 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26341 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26342 for the instantiation. This is not always the most general
26343 template. Consider, for example:
26346 struct S { template <class U> void f();
26347 template <> void f<int>(); };
26349 and an instantiation of S<double>::f<int>. We want TD to be the
26350 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26351 while (/* An instantiation cannot have a definition, so we need a
26352 more general template. */
26353 DECL_TEMPLATE_INSTANTIATION (tmpl
)
26354 /* We must also deal with friend templates. Given:
26356 template <class T> struct S {
26357 template <class U> friend void f() {};
26360 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26361 so far as the language is concerned, but that's still
26362 where we get the pattern for the instantiation from. On
26363 other hand, if the definition comes outside the class, say:
26365 template <class T> struct S {
26366 template <class U> friend void f();
26368 template <class U> friend void f() {}
26370 we don't need to look any further. That's what the check for
26371 DECL_INITIAL is for. */
26372 || (TREE_CODE (decl
) == FUNCTION_DECL
26373 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
26374 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
26376 /* The present template, TD, should not be a definition. If it
26377 were a definition, we should be using it! Note that we
26378 cannot restructure the loop to just keep going until we find
26379 a template with a definition, since that might go too far if
26380 a specialization was declared, but not defined. */
26382 /* Fetch the more general template. */
26383 tmpl
= DECL_TI_TEMPLATE (tmpl
);
26389 /* Returns true if we need to instantiate this template instance even if we
26390 know we aren't going to emit it. */
26393 always_instantiate_p (tree decl
)
26395 /* We always instantiate inline functions so that we can inline them. An
26396 explicit instantiation declaration prohibits implicit instantiation of
26397 non-inline functions. With high levels of optimization, we would
26398 normally inline non-inline functions -- but we're not allowed to do
26399 that for "extern template" functions. Therefore, we check
26400 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26401 return ((TREE_CODE (decl
) == FUNCTION_DECL
26402 && (DECL_DECLARED_INLINE_P (decl
)
26403 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
26404 /* And we need to instantiate static data members so that
26405 their initializers are available in integral constant
26408 && decl_maybe_constant_var_p (decl
)));
26411 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26412 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26413 error, true otherwise. */
26416 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
26418 if (fn
== error_mark_node
)
26421 /* Don't instantiate a noexcept-specification from template context. */
26422 if (processing_template_decl
26423 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
26426 tree fntype
= TREE_TYPE (fn
);
26427 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
26429 if ((!spec
|| UNEVALUATED_NOEXCEPT_SPEC_P (spec
))
26430 && DECL_MAYBE_DELETED (fn
))
26432 if (fn
== current_function_decl
)
26433 /* We're in start_preparsed_function, keep going. */
26437 maybe_synthesize_method (fn
);
26439 return !DECL_DELETED_FN (fn
);
26442 if (!spec
|| !TREE_PURPOSE (spec
))
26445 tree noex
= TREE_PURPOSE (spec
);
26446 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26447 && TREE_CODE (noex
) != DEFERRED_PARSE
)
26450 tree orig_fn
= NULL_TREE
;
26451 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26452 its FUNCTION_DECL for the rest of this function -- push_access_scope
26453 doesn't accept TEMPLATE_DECLs. */
26454 if (DECL_FUNCTION_TEMPLATE_P (fn
))
26457 fn
= DECL_TEMPLATE_RESULT (fn
);
26460 if (DECL_CLONED_FUNCTION_P (fn
))
26462 tree prime
= DECL_CLONED_FUNCTION (fn
);
26463 if (!maybe_instantiate_noexcept (prime
, complain
))
26465 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
26467 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
26469 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
26470 bool added
= false;
26471 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
26473 spec
= get_defaulted_eh_spec (fn
, complain
);
26474 if (spec
== error_mark_node
)
26475 /* This might have failed because of an unparsed DMI, so
26476 let's try again later. */
26479 else if (!(added
= !fns
->add (fn
)))
26481 /* If hash_set::add returns true, the element was already there. */
26482 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
26483 DECL_SOURCE_LOCATION (fn
));
26485 "exception specification of %qD depends on itself",
26487 spec
= noexcept_false_spec
;
26489 else if (push_tinst_level (fn
))
26491 push_to_top_level ();
26492 push_access_scope (fn
);
26493 push_deferring_access_checks (dk_no_deferred
);
26494 input_location
= DECL_SOURCE_LOCATION (fn
);
26496 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26497 && !DECL_LOCAL_DECL_P (fn
))
26499 /* If needed, set current_class_ptr for the benefit of
26500 tsubst_copy/PARM_DECL. */
26501 tree this_parm
= DECL_ARGUMENTS (fn
);
26502 current_class_ptr
= NULL_TREE
;
26503 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
26504 current_class_ptr
= this_parm
;
26507 /* If this function is represented by a TEMPLATE_DECL, then
26508 the deferred noexcept-specification might still contain
26509 dependent types, even after substitution. And we need the
26510 dependency check functions to work in build_noexcept_spec. */
26512 ++processing_template_decl
;
26514 /* Do deferred instantiation of the noexcept-specifier. */
26515 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
26516 DEFERRED_NOEXCEPT_ARGS (noex
),
26517 tf_warning_or_error
, fn
);
26519 /* Build up the noexcept-specification. */
26520 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
26523 --processing_template_decl
;
26525 pop_deferring_access_checks ();
26526 pop_access_scope (fn
);
26527 pop_tinst_level ();
26528 pop_from_top_level ();
26531 spec
= noexcept_false_spec
;
26537 if (spec
== error_mark_node
)
26539 /* This failed with a hard error, so let's go with false. */
26540 gcc_assert (seen_error ());
26541 spec
= noexcept_false_spec
;
26544 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
26546 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
26551 /* We're starting to process the function INST, an instantiation of PATTERN;
26552 add their parameters to local_specializations. */
26555 register_parameter_specializations (tree pattern
, tree inst
)
26557 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
26558 tree spec_parm
= DECL_ARGUMENTS (inst
);
26559 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
26561 register_local_specialization (spec_parm
, tmpl_parm
);
26562 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
26563 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
26565 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
26567 if (!DECL_PACK_P (tmpl_parm
))
26569 register_local_specialization (spec_parm
, tmpl_parm
);
26570 spec_parm
= DECL_CHAIN (spec_parm
);
26574 /* Register the (value) argument pack as a specialization of
26575 TMPL_PARM, then move on. */
26576 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
26577 register_local_specialization (argpack
, tmpl_parm
);
26580 gcc_assert (!spec_parm
);
26583 /* Instantiate the body of D using PATTERN with ARGS. We have
26584 already determined PATTERN is the correct template to use.
26585 NESTED_P is true if this is a nested function, in which case
26586 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
26589 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
26591 tree td
= NULL_TREE
;
26592 tree code_pattern
= pattern
;
26597 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26600 /* Only OMP reductions are nested. */
26601 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
26603 vec
<tree
> omp_privatization_save
;
26604 if (current_function_decl
)
26605 save_omp_privatization_clauses (omp_privatization_save
);
26608 = !(current_function_decl
26609 && !LAMBDA_FUNCTION_P (d
)
26610 && decl_function_context (d
) == current_function_decl
);
26613 push_to_top_level ();
26616 gcc_assert (!processing_template_decl
);
26617 push_function_context ();
26618 cp_unevaluated_operand
= 0;
26619 c_inhibit_evaluation_warnings
= 0;
26624 /* The variable might be a lambda's extra scope, and that
26625 lambda's visibility depends on D's. */
26626 maybe_commonize_var (d
);
26627 determine_visibility (d
);
26630 /* Mark D as instantiated so that recursive calls to
26631 instantiate_decl do not try to instantiate it again. */
26632 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
26635 /* Regenerate the declaration in case the template has been modified
26636 by a subsequent redeclaration. */
26637 regenerate_decl_from_template (d
, td
, args
);
26639 /* We already set the file and line above. Reset them now in case
26640 they changed as a result of calling regenerate_decl_from_template. */
26641 input_location
= DECL_SOURCE_LOCATION (d
);
26645 /* Clear out DECL_RTL; whatever was there before may not be right
26646 since we've reset the type of the declaration. */
26647 SET_DECL_RTL (d
, NULL
);
26648 DECL_IN_AGGR_P (d
) = 0;
26650 /* The initializer is placed in DECL_INITIAL by
26651 regenerate_decl_from_template so we don't need to
26652 push/pop_access_scope again here. Pull it out so that
26653 cp_finish_decl can process it. */
26654 bool const_init
= false;
26655 tree init
= DECL_INITIAL (d
);
26656 DECL_INITIAL (d
) = NULL_TREE
;
26657 DECL_INITIALIZED_P (d
) = 0;
26659 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
26660 initializer. That function will defer actual emission until
26661 we have a chance to determine linkage. */
26662 DECL_EXTERNAL (d
) = 0;
26664 /* Enter the scope of D so that access-checking works correctly. */
26665 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26667 push_nested_class (DECL_CONTEXT (d
));
26669 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26670 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
26673 pop_nested_class ();
26675 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
26676 synthesize_method (d
);
26677 else if (TREE_CODE (d
) == FUNCTION_DECL
)
26679 /* Set up the list of local specializations. */
26680 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
26681 tree block
= NULL_TREE
;
26683 /* Set up context. */
26685 block
= push_stmt_list ();
26688 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
26690 perform_instantiation_time_access_checks (code_pattern
, args
);
26693 /* Create substitution entries for the parameters. */
26694 register_parameter_specializations (code_pattern
, d
);
26696 /* Substitute into the body of the function. */
26697 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26698 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
26699 tf_warning_or_error
, d
);
26702 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
26703 tf_warning_or_error
, DECL_TI_TEMPLATE (d
));
26705 /* Set the current input_location to the end of the function
26706 so that finish_function knows where we are. */
26708 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
26710 /* Remember if we saw an infinite loop in the template. */
26711 current_function_infinite_loop
26712 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
26715 /* Finish the function. */
26717 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
26720 d
= finish_function (/*inline_p=*/false);
26721 expand_or_defer_fn (d
);
26724 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26725 cp_check_omp_declare_reduction (d
);
26728 /* We're not deferring instantiation any more. */
26730 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
26733 pop_from_top_level ();
26735 pop_function_context ();
26737 if (current_function_decl
)
26738 restore_omp_privatization_clauses (omp_privatization_save
);
26741 /* Produce the definition of D, a _DECL generated from a template. If
26742 DEFER_OK is true, then we don't have to actually do the
26743 instantiation now; we just have to do it sometime. Normally it is
26744 an error if this is an explicit instantiation but D is undefined.
26745 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
26746 instantiated class template. */
26749 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
26751 tree tmpl
= DECL_TI_TEMPLATE (d
);
26758 bool pattern_defined
;
26759 location_t saved_loc
= input_location
;
26760 int saved_unevaluated_operand
= cp_unevaluated_operand
;
26761 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26765 /* This function should only be used to instantiate templates for
26766 functions and static member variables. */
26767 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
26769 /* A concept is never instantiated. */
26770 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
26772 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
26775 /* We may have a pending instantiation of D itself. */
26776 lazy_load_pendings (d
);
26778 /* Variables are never deferred; if instantiation is required, they
26779 are instantiated right away. That allows for better code in the
26780 case that an expression refers to the value of the variable --
26781 if the variable has a constant value the referring expression can
26782 take advantage of that fact. */
26786 /* Don't instantiate cloned functions. Instead, instantiate the
26787 functions they cloned. */
26788 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
26789 d
= DECL_CLONED_FUNCTION (d
);
26791 if (DECL_TEMPLATE_INSTANTIATED (d
)
26792 || TREE_TYPE (d
) == error_mark_node
26793 || (TREE_CODE (d
) == FUNCTION_DECL
26794 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
26795 || DECL_TEMPLATE_SPECIALIZATION (d
))
26796 /* D has already been instantiated or explicitly specialized, so
26797 there's nothing for us to do here.
26799 It might seem reasonable to check whether or not D is an explicit
26800 instantiation, and, if so, stop here. But when an explicit
26801 instantiation is deferred until the end of the compilation,
26802 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26803 the instantiation. */
26806 /* Check to see whether we know that this template will be
26807 instantiated in some other file, as with "extern template"
26809 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
26811 /* In general, we do not instantiate such templates. */
26812 if (external_p
&& !always_instantiate_p (d
))
26815 gen_tmpl
= most_general_template (tmpl
);
26816 gen_args
= DECL_TI_ARGS (d
);
26818 /* We should already have the extra args. */
26819 gcc_checking_assert (tmpl
== gen_tmpl
26820 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
26821 == TMPL_ARGS_DEPTH (gen_args
)));
26822 /* And what's in the hash table should match D. */
26823 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
26825 || spec
== NULL_TREE
);
26827 /* This needs to happen before any tsubsting. */
26828 if (! push_tinst_level (d
))
26831 auto_timevar
tv (TV_TEMPLATE_INST
);
26833 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26834 for the instantiation. */
26835 td
= template_for_substitution (d
);
26838 if (variable_template_specialization_p (d
))
26840 /* Look up an explicit specialization, if any. */
26841 tree elt
= most_specialized_partial_spec (d
, tf_warning_or_error
);
26842 if (elt
&& elt
!= error_mark_node
)
26844 td
= TREE_VALUE (elt
);
26845 args
= TREE_PURPOSE (elt
);
26849 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26851 /* We should never be trying to instantiate a member of a class
26852 template or partial specialization. */
26853 gcc_assert (d
!= code_pattern
);
26855 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
26856 || DECL_TEMPLATE_SPECIALIZATION (td
))
26857 /* In the case of a friend template whose definition is provided
26858 outside the class, we may have too many arguments. Drop the
26859 ones we don't need. The same is true for specializations. */
26860 args
= get_innermost_template_args
26861 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
26863 if (TREE_CODE (d
) == FUNCTION_DECL
)
26865 deleted_p
= DECL_DELETED_FN (code_pattern
);
26866 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
26867 && DECL_INITIAL (code_pattern
) != error_mark_node
)
26868 || DECL_DEFAULTED_FN (code_pattern
)
26874 if (DECL_CLASS_SCOPE_P (code_pattern
))
26875 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
26877 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
26880 /* We may be in the middle of deferred access check. Disable it now. */
26881 push_deferring_access_checks (dk_no_deferred
);
26883 /* Unless an explicit instantiation directive has already determined
26884 the linkage of D, remember that a definition is available for
26886 if (pattern_defined
26887 && !DECL_INTERFACE_KNOWN (d
)
26888 && !DECL_NOT_REALLY_EXTERN (d
))
26889 mark_definable (d
);
26891 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
26892 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
26893 input_location
= DECL_SOURCE_LOCATION (d
);
26895 /* If D is a member of an explicitly instantiated class template,
26896 and no definition is available, treat it like an implicit
26898 if (!pattern_defined
&& expl_inst_class_mem_p
26899 && DECL_EXPLICIT_INSTANTIATION (d
))
26901 /* Leave linkage flags alone on instantiations with anonymous
26903 if (TREE_PUBLIC (d
))
26905 DECL_NOT_REALLY_EXTERN (d
) = 0;
26906 DECL_INTERFACE_KNOWN (d
) = 0;
26908 SET_DECL_IMPLICIT_INSTANTIATION (d
);
26911 /* Defer all other templates, unless we have been explicitly
26912 forbidden from doing so. */
26913 if (/* If there is no definition, we cannot instantiate the
26916 /* If it's OK to postpone instantiation, do so. */
26918 /* If this is a static data member that will be defined
26919 elsewhere, we don't want to instantiate the entire data
26920 member, but we do want to instantiate the initializer so that
26921 we can substitute that elsewhere. */
26922 || (external_p
&& VAR_P (d
))
26923 /* Handle here a deleted function too, avoid generating
26924 its body (c++/61080). */
26927 /* The definition of the static data member is now required so
26928 we must substitute the initializer. */
26930 && !DECL_INITIAL (d
)
26931 && DECL_INITIAL (code_pattern
))
26935 bool const_init
= false;
26936 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26938 ns
= decl_namespace_context (d
);
26939 push_nested_namespace (ns
);
26941 push_nested_class (DECL_CONTEXT (d
));
26942 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
26944 tf_warning_or_error
, NULL_TREE
);
26945 /* If instantiating the initializer involved instantiating this
26946 again, don't call cp_finish_decl twice. */
26947 if (!DECL_INITIAL (d
))
26949 /* Make sure the initializer is still constant, in case of
26950 circular dependency (template/instantiate6.C). */
26952 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26953 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
26954 /*asmspec_tree=*/NULL_TREE
, 0);
26957 pop_nested_class ();
26958 pop_nested_namespace (ns
);
26961 /* We restore the source position here because it's used by
26962 add_pending_template. */
26963 input_location
= saved_loc
;
26965 if (at_eof
&& !pattern_defined
26966 && DECL_EXPLICIT_INSTANTIATION (d
)
26967 && DECL_NOT_REALLY_EXTERN (d
))
26970 The definition of a non-exported function template, a
26971 non-exported member function template, or a non-exported
26972 member function or static data member of a class template
26973 shall be present in every translation unit in which it is
26974 explicitly instantiated. */
26975 permerror (input_location
, "explicit instantiation of %qD "
26976 "but no definition available", d
);
26978 /* If we're in unevaluated context, we just wanted to get the
26979 constant value; this isn't an odr use, so don't queue
26980 a full instantiation. */
26981 if (!cp_unevaluated_operand
26982 /* ??? Historically, we have instantiated inline functions, even
26983 when marked as "extern template". */
26984 && !(external_p
&& VAR_P (d
)))
26985 add_pending_template (d
);
26989 set_instantiating_module (d
);
26990 if (variable_template_p (gen_tmpl
))
26991 note_variable_template_instantiation (d
);
26992 instantiate_body (td
, args
, d
, false);
26995 pop_deferring_access_checks ();
26996 pop_tinst_level ();
26997 input_location
= saved_loc
;
26998 cp_unevaluated_operand
= saved_unevaluated_operand
;
26999 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27004 /* Run through the list of templates that we wish we could
27005 instantiate, and instantiate any we can. RETRIES is the
27006 number of times we retry pending template instantiation. */
27009 instantiate_pending_templates (int retries
)
27012 location_t saved_loc
= input_location
;
27014 /* Instantiating templates may trigger vtable generation. This in turn
27015 may require further template instantiations. We place a limit here
27016 to avoid infinite loop. */
27017 if (pending_templates
&& retries
>= max_tinst_depth
)
27019 tree decl
= pending_templates
->tinst
->maybe_get_node ();
27021 fatal_error (input_location
,
27022 "template instantiation depth exceeds maximum of %d"
27023 " instantiating %q+D, possibly from virtual table generation"
27024 " (use %<-ftemplate-depth=%> to increase the maximum)",
27025 max_tinst_depth
, decl
);
27026 if (TREE_CODE (decl
) == FUNCTION_DECL
)
27027 /* Pretend that we defined it. */
27028 DECL_INITIAL (decl
) = error_mark_node
;
27034 struct pending_template
**t
= &pending_templates
;
27035 struct pending_template
*last
= NULL
;
27039 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
27040 bool complete
= false;
27042 if (TYPE_P (instantiation
))
27044 if (!COMPLETE_TYPE_P (instantiation
))
27046 instantiate_class_template (instantiation
);
27047 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
27048 for (tree fld
= TYPE_FIELDS (instantiation
);
27049 fld
; fld
= TREE_CHAIN (fld
))
27051 || (TREE_CODE (fld
) == FUNCTION_DECL
27052 && !DECL_ARTIFICIAL (fld
)))
27053 && DECL_TEMPLATE_INSTANTIATION (fld
))
27054 instantiate_decl (fld
,
27055 /*defer_ok=*/false,
27056 /*expl_inst_class_mem_p=*/false);
27058 if (COMPLETE_TYPE_P (instantiation
))
27062 complete
= COMPLETE_TYPE_P (instantiation
);
27066 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
27067 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
27070 = instantiate_decl (instantiation
,
27071 /*defer_ok=*/false,
27072 /*expl_inst_class_mem_p=*/false);
27073 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
27077 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
27078 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
27083 /* If INSTANTIATION has been instantiated, then we don't
27084 need to consider it again in the future. */
27085 struct pending_template
*drop
= *t
;
27087 set_refcount_ptr (drop
->tinst
);
27088 pending_template_freelist ().free (drop
);
27096 set_refcount_ptr (current_tinst_level
);
27098 last_pending_template
= last
;
27100 while (reconsider
);
27102 input_location
= saved_loc
;
27105 /* Substitute ARGVEC into T, which is a list of initializers for
27106 either base class or a non-static data member. The TREE_PURPOSEs
27107 are DECLs, and the TREE_VALUEs are the initializer values. Used by
27108 instantiate_decl. */
27111 tsubst_initializer_list (tree t
, tree argvec
)
27113 tree inits
= NULL_TREE
;
27114 tree target_ctor
= error_mark_node
;
27116 for (; t
; t
= TREE_CHAIN (t
))
27120 tree expanded_bases
= NULL_TREE
;
27121 tree expanded_arguments
= NULL_TREE
;
27124 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
27129 /* Expand the base class expansion type into separate base
27131 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
27132 tf_warning_or_error
,
27134 if (expanded_bases
== error_mark_node
)
27137 /* We'll be building separate TREE_LISTs of arguments for
27139 len
= TREE_VEC_LENGTH (expanded_bases
);
27140 expanded_arguments
= make_tree_vec (len
);
27141 for (i
= 0; i
< len
; i
++)
27142 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
27144 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
27145 expand each argument in the TREE_VALUE of t. */
27146 expr
= make_node (EXPR_PACK_EXPANSION
);
27147 PACK_EXPANSION_LOCAL_P (expr
) = true;
27148 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
27149 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
27151 if (TREE_VALUE (t
) == void_type_node
)
27152 /* VOID_TYPE_NODE is used to indicate
27153 value-initialization. */
27155 for (i
= 0; i
< len
; i
++)
27156 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
27160 /* Substitute parameter packs into each argument in the
27162 in_base_initializer
= 1;
27163 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
27165 tree expanded_exprs
;
27167 /* Expand the argument. */
27169 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
27170 value
= TREE_VALUE (arg
);
27174 PACK_EXPANSION_PATTERN (value
) = TREE_VALUE (arg
);
27177 = tsubst_pack_expansion (value
, argvec
,
27178 tf_warning_or_error
,
27180 if (expanded_exprs
== error_mark_node
)
27183 /* Prepend each of the expanded expressions to the
27184 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
27185 for (i
= 0; i
< len
; i
++)
27186 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
27187 for (int j
= 0; j
< TREE_VEC_LENGTH (expanded_exprs
); j
++)
27188 TREE_VEC_ELT (expanded_arguments
, i
)
27189 = tree_cons (NULL_TREE
,
27190 TREE_VEC_ELT (expanded_exprs
, j
),
27191 TREE_VEC_ELT (expanded_arguments
, i
));
27193 TREE_VEC_ELT (expanded_arguments
, i
)
27194 = tree_cons (NULL_TREE
,
27195 TREE_VEC_ELT (expanded_exprs
, i
),
27196 TREE_VEC_ELT (expanded_arguments
, i
));
27198 in_base_initializer
= 0;
27200 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
27201 since we built them backwards. */
27202 for (i
= 0; i
< len
; i
++)
27204 TREE_VEC_ELT (expanded_arguments
, i
) =
27205 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
27210 for (i
= 0; i
< len
; ++i
)
27212 if (expanded_bases
)
27214 decl
= TREE_VEC_ELT (expanded_bases
, i
);
27215 decl
= expand_member_init (decl
);
27216 init
= TREE_VEC_ELT (expanded_arguments
, i
);
27221 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
27222 tf_warning_or_error
, NULL_TREE
);
27224 decl
= expand_member_init (decl
);
27225 if (decl
&& !DECL_P (decl
))
27226 in_base_initializer
= 1;
27228 init
= TREE_VALUE (t
);
27230 if (init
!= void_type_node
)
27231 init
= tsubst_expr (init
, argvec
,
27232 tf_warning_or_error
, NULL_TREE
);
27233 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
27234 /* If we had an initializer but it instantiated to nothing,
27235 value-initialize the object. This will only occur when
27236 the initializer was a pack expansion where the parameter
27237 packs used in that expansion were of length zero. */
27238 init
= void_type_node
;
27239 in_base_initializer
= 0;
27242 if (target_ctor
!= error_mark_node
27243 && init
!= error_mark_node
)
27245 error ("mem-initializer for %qD follows constructor delegation",
27249 /* Look for a target constructor. */
27250 if (init
!= error_mark_node
27251 && decl
&& CLASS_TYPE_P (decl
)
27252 && same_type_p (decl
, current_class_type
))
27254 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
27257 error ("constructor delegation follows mem-initializer for %qD",
27258 TREE_PURPOSE (inits
));
27261 target_ctor
= init
;
27266 init
= build_tree_list (decl
, init
);
27267 /* Carry over the dummy TREE_TYPE node containing the source
27269 TREE_TYPE (init
) = TREE_TYPE (t
);
27270 TREE_CHAIN (init
) = inits
;
27278 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
27279 is the instantiation (which should have been created with
27280 start_enum) and ARGS are the template arguments to use. */
27283 tsubst_enum (tree tag
, tree newtag
, tree args
)
27287 if (SCOPED_ENUM_P (newtag
))
27288 begin_scope (sk_scoped_enum
, newtag
);
27290 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
27293 tree decl
= TREE_VALUE (e
);
27295 /* Note that in a template enum, the TREE_VALUE is the
27296 CONST_DECL, not the corresponding INTEGER_CST. */
27297 value
= tsubst_expr (DECL_INITIAL (decl
),
27298 args
, tf_warning_or_error
, NULL_TREE
);
27300 /* Give this enumeration constant the correct access. */
27301 set_current_access_from_decl (decl
);
27303 /* Actually build the enumerator itself. Here we're assuming that
27304 enumerators can't have dependent attributes. */
27305 tree newdecl
= build_enumerator (DECL_NAME (decl
), value
, newtag
,
27306 DECL_ATTRIBUTES (decl
),
27307 DECL_SOURCE_LOCATION (decl
));
27308 /* Attribute deprecated without an argument isn't sticky: it'll
27309 melt into a tree flag, so we need to propagate the flag here,
27310 since we just created a new enumerator. */
27311 TREE_DEPRECATED (newdecl
) = TREE_DEPRECATED (decl
);
27312 TREE_UNAVAILABLE (newdecl
) = TREE_UNAVAILABLE (decl
);
27315 if (SCOPED_ENUM_P (newtag
))
27318 finish_enum_value_list (newtag
);
27319 finish_enum (newtag
);
27321 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
27322 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
27323 TREE_DEPRECATED (newtag
) = TREE_DEPRECATED (tag
);
27324 TREE_UNAVAILABLE (newtag
) = TREE_UNAVAILABLE (tag
);
27327 /* DECL is a FUNCTION_DECL that is a template specialization. Return
27328 its type -- but without substituting the innermost set of template
27329 arguments. So, innermost set of template parameters will appear in
27333 get_mostly_instantiated_function_type (tree decl
)
27335 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27336 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
27339 /* Return truthvalue if we're processing a template different from
27340 the last one involved in diagnostics. */
27342 problematic_instantiation_changed (void)
27344 return current_tinst_level
!= last_error_tinst_level
;
27347 /* Remember current template involved in diagnostics. */
27349 record_last_problematic_instantiation (void)
27351 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
27354 struct tinst_level
*
27355 current_instantiation (void)
27357 return current_tinst_level
;
27360 /* Return TRUE if current_function_decl is being instantiated, false
27364 instantiating_current_function_p (void)
27366 return (current_instantiation ()
27367 && (current_instantiation ()->maybe_get_node ()
27368 == current_function_decl
));
27371 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27372 type. Return false for ok, true for disallowed. Issue error and
27373 inform messages under control of COMPLAIN. */
27376 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
27378 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
27380 else if (TYPE_PTR_P (type
))
27382 else if (TYPE_REF_P (type
)
27383 && !TYPE_REF_IS_RVALUE (type
))
27385 else if (TYPE_PTRMEM_P (type
))
27387 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
27389 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
27391 if (complain
& tf_error
)
27392 error ("non-type template parameters of deduced class type only "
27393 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27398 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
27400 else if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
27401 && cxx_dialect
< cxx11
)
27402 /* Fall through; before C++11 alias templates, a bound ttp
27403 always instantiates into a class type. */;
27404 else if (WILDCARD_TYPE_P (type
))
27405 /* Any other wildcard type not already handled above is allowed. */
27407 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
27408 /* Fall through. */;
27409 else if (VOID_TYPE_P (type
))
27410 /* Fall through. */;
27411 else if (cxx_dialect
>= cxx20
)
27413 if (dependent_type_p (type
))
27415 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
27417 if (structural_type_p (type
))
27419 if (complain
& tf_error
)
27421 auto_diagnostic_group d
;
27422 error ("%qT is not a valid type for a template non-type "
27423 "parameter because it is not structural", type
);
27424 structural_type_p (type
, true);
27428 else if (CLASS_TYPE_P (type
))
27430 if (complain
& tf_error
)
27431 error ("non-type template parameters of class type only available "
27432 "with %<-std=c++20%> or %<-std=gnu++20%>");
27436 if (complain
& tf_error
)
27438 if (type
== error_mark_node
)
27439 inform (input_location
, "invalid template non-type parameter");
27441 error ("%q#T is not a valid type for a template non-type parameter",
27447 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27450 value_dependent_noexcept_spec_p (tree type
)
27452 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
27453 if (tree noex
= TREE_PURPOSE (spec
))
27454 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27455 affect overload resolution and treating it as dependent breaks
27456 things. Same for an unparsed noexcept expression. */
27457 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
27458 && TREE_CODE (noex
) != DEFERRED_PARSE
27459 && value_dependent_expression_p (noex
))
27465 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27466 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27469 dependent_type_p_r (tree type
)
27475 A type is dependent if it is:
27477 -- a template parameter. Template template parameters are types
27478 for us (since TYPE_P holds true for them) so we handle
27480 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27481 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
27483 /* -- a qualified-id with a nested-name-specifier which contains a
27484 class-name that names a dependent type or whose unqualified-id
27485 names a dependent type. */
27486 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27489 /* An alias template specialization can be dependent even if the
27490 resulting type is not. */
27491 if (dependent_alias_template_spec_p (type
, nt_transparent
))
27494 /* -- a cv-qualified type where the cv-unqualified type is
27496 No code is necessary for this bullet; the code below handles
27497 cv-qualified types, and we don't want to strip aliases with
27498 TYPE_MAIN_VARIANT because of DR 1558. */
27499 /* -- a compound type constructed from any dependent type. */
27500 if (TYPE_PTRMEM_P (type
))
27501 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
27502 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27504 else if (INDIRECT_TYPE_P (type
))
27505 return dependent_type_p (TREE_TYPE (type
));
27506 else if (FUNC_OR_METHOD_TYPE_P (type
))
27510 if (dependent_type_p (TREE_TYPE (type
)))
27512 for (arg_type
= TYPE_ARG_TYPES (type
);
27514 arg_type
= TREE_CHAIN (arg_type
))
27515 if (dependent_type_p (TREE_VALUE (arg_type
)))
27517 if (cxx_dialect
>= cxx17
27518 && value_dependent_noexcept_spec_p (type
))
27519 /* A value-dependent noexcept-specifier makes the type dependent. */
27523 /* -- an array type constructed from any dependent type or whose
27524 size is specified by a constant expression that is
27527 We checked for type- and value-dependence of the bounds in
27528 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27529 if (TREE_CODE (type
) == ARRAY_TYPE
)
27531 if (TYPE_DOMAIN (type
)
27532 && dependent_type_p (TYPE_DOMAIN (type
)))
27534 return dependent_type_p (TREE_TYPE (type
));
27537 /* -- a template-id in which either the template name is a template
27539 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
27541 /* ... or any of the template arguments is a dependent type or
27542 an expression that is type-dependent or value-dependent. */
27543 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
27544 && (any_dependent_template_arguments_p
27545 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
27548 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
27549 dependent; if the argument of the `typeof' expression is not
27550 type-dependent, then it should already been have resolved. */
27551 if (TREE_CODE (type
) == TYPEOF_TYPE
27552 || TREE_CODE (type
) == DECLTYPE_TYPE
27553 || TREE_CODE (type
) == TRAIT_TYPE
)
27556 /* A template argument pack is dependent if any of its packed
27558 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
27560 tree args
= ARGUMENT_PACK_ARGS (type
);
27561 for (tree arg
: tree_vec_range (args
))
27562 if (dependent_template_arg_p (arg
))
27566 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27567 be template parameters. */
27568 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
27571 if (TREE_CODE (type
) == DEPENDENT_OPERATOR_TYPE
)
27574 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
27577 /* The standard does not specifically mention types that are local
27578 to template functions or local classes, but they should be
27579 considered dependent too. For example:
27581 template <int I> void f() {
27586 The size of `E' cannot be known until the value of `I' has been
27587 determined. Therefore, `E' must be considered dependent. */
27588 scope
= TYPE_CONTEXT (type
);
27589 if (scope
&& TYPE_P (scope
))
27590 return dependent_type_p (scope
);
27591 /* Don't use type_dependent_expression_p here, as it can lead
27592 to infinite recursion trying to determine whether a lambda
27593 nested in a lambda is dependent (c++/47687). */
27594 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
27595 && DECL_LANG_SPECIFIC (scope
)
27596 && DECL_TEMPLATE_INFO (scope
)
27597 && (any_dependent_template_arguments_p
27598 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
27601 /* Other types are non-dependent. */
27605 /* Returns TRUE if TYPE is dependent, in the sense of
27606 [temp.dep.type]. Note that a NULL type is considered dependent. */
27609 dependent_type_p (tree type
)
27611 /* If there are no template parameters in scope, then there can't be
27612 any dependent types. */
27613 if (!processing_template_decl
)
27615 /* If we are not processing a template, then nobody should be
27616 providing us with a dependent type. */
27618 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
27622 /* If the type is NULL, we have not computed a type for the entity
27623 in question; in that case, the type is dependent. */
27627 /* Erroneous types can be considered non-dependent. */
27628 if (type
== error_mark_node
)
27631 /* If we have not already computed the appropriate value for TYPE,
27633 if (!TYPE_DEPENDENT_P_VALID (type
))
27635 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
27636 TYPE_DEPENDENT_P_VALID (type
) = 1;
27639 return TYPE_DEPENDENT_P (type
);
27642 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
27643 lookup. In other words, a dependent type that is not the current
27647 dependent_scope_p (tree scope
)
27649 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
27650 && !currently_open_class (scope
));
27653 /* True if we might find more declarations in SCOPE during instantiation than
27654 we can when parsing the template. */
27657 dependentish_scope_p (tree scope
)
27659 return dependent_scope_p (scope
) || any_dependent_bases_p (scope
);
27662 /* T is a SCOPE_REF. Return whether it represents a non-static member of
27663 an unknown base of 'this' (and is therefore instantiation-dependent). */
27666 unknown_base_ref_p (tree t
)
27668 if (!current_class_ptr
)
27671 tree mem
= TREE_OPERAND (t
, 1);
27672 if (shared_member_p (mem
))
27675 tree cur
= current_nonlambda_class_type ();
27676 if (!any_dependent_bases_p (cur
))
27679 tree ctx
= TREE_OPERAND (t
, 0);
27680 if (DERIVED_FROM_P (ctx
, cur
))
27686 /* T is a SCOPE_REF; return whether we need to consider it
27687 instantiation-dependent so that we can check access at instantiation
27688 time even though we know which member it resolves to. */
27691 instantiation_dependent_scope_ref_p (tree t
)
27693 if (DECL_P (TREE_OPERAND (t
, 1))
27694 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
27695 && !dependent_scope_p (TREE_OPERAND (t
, 0))
27696 && !unknown_base_ref_p (t
)
27697 && accessible_in_template_p (TREE_OPERAND (t
, 0),
27698 TREE_OPERAND (t
, 1)))
27704 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
27705 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
27708 /* Note that this predicate is not appropriate for general expressions;
27709 only constant expressions (that satisfy potential_constant_expression)
27710 can be tested for value dependence. */
27713 value_dependent_expression_p (tree expression
)
27715 if (!processing_template_decl
|| expression
== NULL_TREE
)
27718 /* A type-dependent expression is also value-dependent. */
27719 if (type_dependent_expression_p (expression
))
27722 switch (TREE_CODE (expression
))
27725 /* A dependent member function of the current instantiation. */
27726 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
27728 case FUNCTION_DECL
:
27729 /* A dependent member function of the current instantiation. */
27730 if (DECL_CLASS_SCOPE_P (expression
)
27731 && dependent_type_p (DECL_CONTEXT (expression
)))
27735 case IDENTIFIER_NODE
:
27736 /* A name that has not been looked up -- must be dependent. */
27739 case TEMPLATE_PARM_INDEX
:
27740 /* A non-type template parm. */
27744 /* A non-type template parm. */
27745 if (DECL_TEMPLATE_PARM_P (expression
))
27747 return value_dependent_expression_p (DECL_INITIAL (expression
));
27750 /* A constant with literal type and is initialized
27751 with an expression that is value-dependent. */
27752 if (DECL_DEPENDENT_INIT_P (expression
)
27753 /* FIXME cp_finish_decl doesn't fold reference initializers. */
27754 || TYPE_REF_P (TREE_TYPE (expression
)))
27756 if (DECL_HAS_VALUE_EXPR_P (expression
))
27758 tree value_expr
= DECL_VALUE_EXPR (expression
);
27759 if (value_dependent_expression_p (value_expr
)
27760 /* __PRETTY_FUNCTION__ inside a template function is dependent
27761 on the name of the function. */
27762 || (DECL_PRETTY_FUNCTION_P (expression
)
27763 /* It might be used in a template, but not a template
27764 function, in which case its DECL_VALUE_EXPR will be
27766 && value_expr
== error_mark_node
))
27771 case DYNAMIC_CAST_EXPR
:
27772 case STATIC_CAST_EXPR
:
27773 case CONST_CAST_EXPR
:
27774 case REINTERPRET_CAST_EXPR
:
27776 case IMPLICIT_CONV_EXPR
:
27777 /* These expressions are value-dependent if the type to which
27778 the cast occurs is dependent or the expression being casted
27779 is value-dependent. */
27781 tree type
= TREE_TYPE (expression
);
27783 if (dependent_type_p (type
))
27786 /* A functional cast has a list of operands. */
27787 expression
= TREE_OPERAND (expression
, 0);
27790 /* If there are no operands, it must be an expression such
27791 as "int()". This should not happen for aggregate types
27792 because it would form non-constant expressions. */
27793 gcc_assert (cxx_dialect
>= cxx11
27794 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
27799 if (TREE_CODE (expression
) == TREE_LIST
)
27800 return any_value_dependent_elements_p (expression
);
27802 if (TREE_CODE (type
) == REFERENCE_TYPE
27803 && has_value_dependent_address (expression
))
27806 return value_dependent_expression_p (expression
);
27810 if (SIZEOF_EXPR_TYPE_P (expression
))
27811 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
27815 /* A `sizeof' expression is value-dependent if the operand is
27816 type-dependent or is a pack expansion. */
27817 expression
= TREE_OPERAND (expression
, 0);
27818 if (PACK_EXPANSION_P (expression
))
27820 else if (TYPE_P (expression
))
27821 return dependent_type_p (expression
);
27822 return instantiation_dependent_uneval_expression_p (expression
);
27824 case AT_ENCODE_EXPR
:
27825 /* An 'encode' expression is value-dependent if the operand is
27827 expression
= TREE_OPERAND (expression
, 0);
27828 return dependent_type_p (expression
);
27830 case NOEXCEPT_EXPR
:
27831 expression
= TREE_OPERAND (expression
, 0);
27832 return instantiation_dependent_uneval_expression_p (expression
);
27835 /* All instantiation-dependent expressions should also be considered
27836 value-dependent. */
27837 return instantiation_dependent_scope_ref_p (expression
);
27839 case COMPONENT_REF
:
27840 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
27841 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
27843 case NONTYPE_ARGUMENT_PACK
:
27844 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27845 is value-dependent. */
27846 for (tree arg
: tree_vec_range (ARGUMENT_PACK_ARGS (expression
)))
27847 if (value_dependent_expression_p (arg
))
27853 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
27855 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
27861 if (TREE_CODE (type2
) != TREE_LIST
)
27862 return dependent_type_p (type2
);
27864 for (; type2
; type2
= TREE_CHAIN (type2
))
27865 if (dependent_type_p (TREE_VALUE (type2
)))
27872 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27873 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
27876 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27877 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
27881 tree op
= TREE_OPERAND (expression
, 0);
27882 return (value_dependent_expression_p (op
)
27883 || has_value_dependent_address (op
));
27886 case REQUIRES_EXPR
:
27887 /* Treat all requires-expressions as value-dependent so
27888 we don't try to fold them. */
27892 return dependent_type_p (TREE_OPERAND (expression
, 0));
27896 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
27898 tree fn
= get_callee_fndecl (expression
);
27900 nargs
= call_expr_nargs (expression
);
27901 for (i
= 0; i
< nargs
; ++i
)
27903 tree op
= CALL_EXPR_ARG (expression
, i
);
27904 /* In a call to a constexpr member function, look through the
27905 implicit ADDR_EXPR on the object argument so that it doesn't
27906 cause the call to be considered value-dependent. We also
27907 look through it in potential_constant_expression. */
27908 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
27909 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
27910 && TREE_CODE (op
) == ADDR_EXPR
)
27911 op
= TREE_OPERAND (op
, 0);
27912 if (value_dependent_expression_p (op
))
27918 case TEMPLATE_ID_EXPR
:
27919 return concept_definition_p (TREE_OPERAND (expression
, 0))
27920 && any_dependent_template_arguments_p (TREE_OPERAND (expression
, 1));
27926 if (dependent_type_p (TREE_TYPE (expression
)))
27928 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
27929 if (value_dependent_expression_p (val
))
27935 /* Treat a GNU statement expression as dependent to avoid crashing
27936 under instantiate_non_dependent_expr; it can't be constant. */
27941 /* The second operand is a type, which type_dependent_expression_p
27942 (and therefore value_dependent_expression_p) doesn't want to see. */
27943 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
27944 || value_dependent_expression_p (TREE_OPERAND (expression
, 2))
27945 || value_dependent_expression_p (TREE_OPERAND (expression
, 3)));
27948 /* A constant expression is value-dependent if any subexpression is
27949 value-dependent. */
27950 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
27952 case tcc_reference
:
27954 case tcc_comparison
:
27956 case tcc_expression
:
27959 int i
, len
= cp_tree_operand_length (expression
);
27961 for (i
= 0; i
< len
; i
++)
27963 tree t
= TREE_OPERAND (expression
, i
);
27965 /* In some cases, some of the operands may be missing.
27966 (For example, in the case of PREDECREMENT_EXPR, the
27967 amount to increment by may be missing.) That doesn't
27968 make the expression dependent. */
27969 if (t
&& value_dependent_expression_p (t
))
27980 /* The expression is not value-dependent. */
27984 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27985 [temp.dep.expr]. Note that an expression with no type is
27986 considered dependent. Other parts of the compiler arrange for an
27987 expression with type-dependent subexpressions to have no type, so
27988 this function doesn't have to be fully recursive. */
27991 type_dependent_expression_p (tree expression
)
27993 if (!processing_template_decl
)
27996 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
27999 gcc_checking_assert (!TYPE_P (expression
));
28001 STRIP_ANY_LOCATION_WRAPPER (expression
);
28003 /* An unresolved name is always dependent. */
28004 if (identifier_p (expression
)
28005 || TREE_CODE (expression
) == USING_DECL
28006 || TREE_CODE (expression
) == WILDCARD_DECL
)
28009 /* A lambda-expression in template context is dependent. dependent_type_p is
28010 true for a lambda in the scope of a class or function template, but that
28011 doesn't cover all template contexts, like a default template argument. */
28012 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
28015 /* A fold expression is type-dependent. */
28016 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
28017 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
28018 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
28019 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
28022 /* Some expression forms are never type-dependent. */
28023 if (TREE_CODE (expression
) == SIZEOF_EXPR
28024 || TREE_CODE (expression
) == ALIGNOF_EXPR
28025 || TREE_CODE (expression
) == AT_ENCODE_EXPR
28026 || TREE_CODE (expression
) == NOEXCEPT_EXPR
28027 || TREE_CODE (expression
) == TRAIT_EXPR
28028 || TREE_CODE (expression
) == TYPEID_EXPR
28029 || TREE_CODE (expression
) == DELETE_EXPR
28030 || TREE_CODE (expression
) == VEC_DELETE_EXPR
28031 || TREE_CODE (expression
) == THROW_EXPR
28032 || TREE_CODE (expression
) == REQUIRES_EXPR
)
28035 /* The types of these expressions depends only on the type to which
28036 the cast occurs. */
28037 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
28038 || TREE_CODE (expression
) == STATIC_CAST_EXPR
28039 || TREE_CODE (expression
) == CONST_CAST_EXPR
28040 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
28041 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
28042 || TREE_CODE (expression
) == CAST_EXPR
)
28043 return dependent_type_p (TREE_TYPE (expression
));
28045 /* The types of these expressions depends only on the type created
28046 by the expression. */
28047 if (TREE_CODE (expression
) == NEW_EXPR
28048 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
28050 /* For NEW_EXPR tree nodes created inside a template, either
28051 the object type itself or a TREE_LIST may appear as the
28053 tree type
= TREE_OPERAND (expression
, 1);
28054 if (TREE_CODE (type
) == TREE_LIST
)
28055 /* This is an array type. We need to check array dimensions
28057 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
28058 || value_dependent_expression_p
28059 (TREE_OPERAND (TREE_VALUE (type
), 1));
28060 /* Array type whose dimension has to be deduced. */
28061 else if (TREE_CODE (type
) == ARRAY_TYPE
28062 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
28065 return dependent_type_p (type
);
28068 if (TREE_CODE (expression
) == SCOPE_REF
)
28070 tree scope
= TREE_OPERAND (expression
, 0);
28071 tree name
= TREE_OPERAND (expression
, 1);
28073 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
28074 contains an identifier associated by name lookup with one or more
28075 declarations declared with a dependent type, or...a
28076 nested-name-specifier or qualified-id that names a member of an
28077 unknown specialization. */
28078 return (type_dependent_expression_p (name
)
28079 || dependent_scope_p (scope
));
28082 if (TREE_CODE (expression
) == TEMPLATE_DECL
28083 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
28084 return uses_outer_template_parms (expression
);
28086 if (TREE_CODE (expression
) == STMT_EXPR
)
28087 expression
= stmt_expr_value_expr (expression
);
28089 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
28091 for (auto &elt
: CONSTRUCTOR_ELTS (expression
))
28092 if (type_dependent_expression_p (elt
.value
))
28097 /* A static data member of the current instantiation with incomplete
28098 array type is type-dependent, as the definition and specializations
28099 can have different bounds. */
28100 if (VAR_P (expression
)
28101 && DECL_CLASS_SCOPE_P (expression
)
28102 && dependent_type_p (DECL_CONTEXT (expression
))
28103 && VAR_HAD_UNKNOWN_BOUND (expression
))
28106 /* An array of unknown bound depending on a variadic parameter, eg:
28108 template<typename... Args>
28109 void foo (Args... args)
28111 int arr[] = { args... };
28114 template<int... vals>
28117 int arr[] = { vals... };
28120 If the array has no length and has an initializer, it must be that
28121 we couldn't determine its length in cp_complete_array_type because
28122 it is dependent. */
28123 if (((VAR_P (expression
) && DECL_INITIAL (expression
))
28124 || COMPOUND_LITERAL_P (expression
))
28125 && TREE_TYPE (expression
) != NULL_TREE
28126 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
28127 && !TYPE_DOMAIN (TREE_TYPE (expression
)))
28130 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
28131 if (BASELINK_P (expression
))
28133 if (BASELINK_OPTYPE (expression
)
28134 && dependent_type_p (BASELINK_OPTYPE (expression
)))
28136 expression
= BASELINK_FUNCTIONS (expression
);
28139 /* A function or variable template-id is type-dependent if it has any
28140 dependent template arguments. */
28141 if (VAR_OR_FUNCTION_DECL_P (expression
)
28142 && DECL_LANG_SPECIFIC (expression
)
28143 && DECL_TEMPLATE_INFO (expression
))
28145 /* Consider the innermost template arguments, since those are the ones
28146 that come from the template-id; the template arguments for the
28147 enclosing class do not make it type-dependent unless they are used in
28148 the type of the decl. */
28149 if (instantiates_primary_template_p (expression
)
28150 && (any_dependent_template_arguments_p
28151 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
28155 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
28156 type-dependent. Checking this is important for functions with auto return
28157 type, which looks like a dependent type. */
28158 if (TREE_CODE (expression
) == FUNCTION_DECL
28159 && !(DECL_CLASS_SCOPE_P (expression
)
28160 && dependent_type_p (DECL_CONTEXT (expression
)))
28161 && !(DECL_LANG_SPECIFIC (expression
)
28162 && DECL_UNIQUE_FRIEND_P (expression
)
28163 && (!DECL_FRIEND_CONTEXT (expression
)
28164 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
28165 && !DECL_LOCAL_DECL_P (expression
))
28167 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
28168 || undeduced_auto_decl (expression
));
28172 /* Otherwise, its constraints could still depend on outer template parameters
28173 from its (dependent) scope. */
28174 if (TREE_CODE (expression
) == FUNCTION_DECL
28175 /* As an optimization, check this cheaper sufficient condition first.
28176 (At this point we've established that we're looking at a member of
28177 a dependent class, so it makes sense to start treating say undeduced
28178 auto as dependent.) */
28179 && !dependent_type_p (TREE_TYPE (expression
))
28180 && uses_outer_template_parms_in_constraints (expression
))
28183 /* Always dependent, on the number of arguments if nothing else. */
28184 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
28187 if (TREE_TYPE (expression
) == unknown_type_node
)
28189 if (TREE_CODE (expression
) == ADDR_EXPR
)
28190 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
28191 if (TREE_CODE (expression
) == COMPONENT_REF
28192 || TREE_CODE (expression
) == OFFSET_REF
)
28194 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
28196 expression
= TREE_OPERAND (expression
, 1);
28197 if (identifier_p (expression
))
28200 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
28201 if (TREE_CODE (expression
) == SCOPE_REF
)
28204 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
28205 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
28206 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
28209 if (BASELINK_P (expression
))
28211 if (BASELINK_OPTYPE (expression
)
28212 && dependent_type_p (BASELINK_OPTYPE (expression
)))
28214 expression
= BASELINK_FUNCTIONS (expression
);
28217 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
28219 if (any_dependent_template_arguments_p
28220 (TREE_OPERAND (expression
, 1)))
28222 expression
= TREE_OPERAND (expression
, 0);
28223 if (identifier_p (expression
))
28227 gcc_assert (OVL_P (expression
));
28229 for (lkp_iterator
iter (expression
); iter
; ++iter
)
28230 if (type_dependent_expression_p (*iter
))
28236 /* The type of a non-type template parm declared with a placeholder type
28237 depends on the corresponding template argument, even though
28238 placeholders are not normally considered dependent. */
28239 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
28240 && is_auto (TREE_TYPE (expression
)))
28243 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
28245 /* Dependent type attributes might not have made it from the decl to
28247 if (DECL_P (expression
)
28248 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
28251 return (dependent_type_p (TREE_TYPE (expression
)));
28254 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
28255 type-dependent if the expression refers to a member of the current
28256 instantiation and the type of the referenced member is dependent, or the
28257 class member access expression refers to a member of an unknown
28260 This function returns true if the OBJECT in such a class member access
28261 expression is of an unknown specialization. */
28264 type_dependent_object_expression_p (tree object
)
28266 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
28268 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
28270 tree scope
= TREE_TYPE (object
);
28271 return (!scope
|| dependent_scope_p (scope
));
28274 /* walk_tree callback function for instantiation_dependent_expression_p,
28275 below. Returns non-zero if a dependent subexpression is found. */
28278 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
28283 /* We don't have to worry about decltype currently because decltype
28284 of an instantiation-dependent expr is a dependent type. This
28285 might change depending on the resolution of DR 1172. */
28286 *walk_subtrees
= false;
28289 enum tree_code code
= TREE_CODE (*tp
);
28292 /* Don't treat an argument list as dependent just because it has no
28296 case NONTYPE_ARGUMENT_PACK
:
28299 case TEMPLATE_PARM_INDEX
:
28300 if (dependent_type_p (TREE_TYPE (*tp
)))
28302 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
28304 /* We'll check value-dependence separately. */
28307 /* Handle expressions with type operands. */
28311 case AT_ENCODE_EXPR
:
28313 tree op
= TREE_OPERAND (*tp
, 0);
28314 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
28315 op
= TREE_TYPE (op
);
28318 if (dependent_type_p (op
))
28322 *walk_subtrees
= false;
28329 case COMPONENT_REF
:
28330 if (identifier_p (TREE_OPERAND (*tp
, 1)))
28331 /* In a template, finish_class_member_access_expr creates a
28332 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28333 type-dependent, so that we can check access control at
28334 instantiation time (PR 42277). See also Core issue 1273. */
28339 if (instantiation_dependent_scope_ref_p (*tp
))
28344 /* Treat statement-expressions as dependent. */
28348 /* Treat requires-expressions as dependent. */
28349 case REQUIRES_EXPR
:
28353 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
28357 case TEMPLATE_DECL
:
28358 case FUNCTION_DECL
:
28359 /* Before C++17, a noexcept-specifier isn't part of the function type
28360 so it doesn't affect type dependence, but we still want to consider it
28361 for instantiation dependence. */
28362 if (cxx_dialect
< cxx17
28363 && DECL_DECLARES_FUNCTION_P (*tp
)
28364 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp
)))
28372 if (type_dependent_expression_p (*tp
))
28378 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28379 sense defined by the ABI:
28381 "An expression is instantiation-dependent if it is type-dependent
28382 or value-dependent, or it has a subexpression that is type-dependent
28383 or value-dependent."
28385 Except don't actually check value-dependence for unevaluated expressions,
28386 because in sizeof(i) we don't care about the value of i. Checking
28387 type-dependence will in turn check value-dependence of array bounds/template
28388 arguments as needed. */
28391 instantiation_dependent_uneval_expression_p (tree expression
)
28395 if (!processing_template_decl
)
28398 if (expression
== error_mark_node
)
28401 result
= cp_walk_tree_without_duplicates (&expression
,
28402 instantiation_dependent_r
, NULL
);
28403 return result
!= NULL_TREE
;
28406 /* As above, but also check value-dependence of the expression as a whole. */
28409 instantiation_dependent_expression_p (tree expression
)
28411 return (instantiation_dependent_uneval_expression_p (expression
)
28412 || (processing_template_decl
28413 && potential_constant_expression (expression
)
28414 && value_dependent_expression_p (expression
)));
28417 /* Like type_dependent_expression_p, but it also works while not processing
28418 a template definition, i.e. during substitution or mangling. */
28421 type_dependent_expression_p_push (tree expr
)
28424 ++processing_template_decl
;
28425 b
= type_dependent_expression_p (expr
);
28426 --processing_template_decl
;
28430 /* Returns TRUE if ARGS contains a type-dependent expression. */
28433 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
28438 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
28440 if (type_dependent_expression_p (arg
))
28446 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28447 expressions) contains any type-dependent expressions. */
28450 any_type_dependent_elements_p (const_tree list
)
28452 for (; list
; list
= TREE_CHAIN (list
))
28453 if (type_dependent_expression_p (TREE_VALUE (list
)))
28459 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28460 expressions) contains any value-dependent expressions. */
28463 any_value_dependent_elements_p (const_tree list
)
28465 for (; list
; list
= TREE_CHAIN (list
))
28466 if (value_dependent_expression_p (TREE_VALUE (list
)))
28472 /* Returns TRUE if the ARG (a template argument) is dependent. */
28475 dependent_template_arg_p (tree arg
)
28477 if (!processing_template_decl
)
28480 /* Assume a template argument that was wrongly written by the user
28481 is dependent. This is consistent with what
28482 any_dependent_template_arguments_p [that calls this function]
28484 if (!arg
|| arg
== error_mark_node
)
28487 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
28488 arg
= argument_pack_select_arg (arg
);
28490 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
28492 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28494 if (DECL_TEMPLATE_PARM_P (arg
))
28496 /* A member template of a dependent class is not necessarily
28497 type-dependent, but it is a dependent template argument because it
28498 will be a member of an unknown specialization to that template. */
28499 tree scope
= CP_DECL_CONTEXT (arg
);
28500 return TYPE_P (scope
) && dependent_type_p (scope
);
28502 else if (ARGUMENT_PACK_P (arg
))
28504 tree args
= ARGUMENT_PACK_ARGS (arg
);
28505 for (tree arg
: tree_vec_range (args
))
28506 if (dependent_template_arg_p (arg
))
28510 else if (TYPE_P (arg
))
28511 return dependent_type_p (arg
);
28513 return value_dependent_expression_p (arg
);
28516 /* Identify any expressions that use function parms. */
28519 find_parm_usage_r (tree
*tp
, int *walk_subtrees
, void*)
28522 if (TREE_CODE (t
) == PARM_DECL
)
28524 *walk_subtrees
= 0;
28530 /* Returns true if a type specialization formed using the template
28531 arguments ARGS needs to use structural equality. */
28534 any_template_arguments_need_structural_equality_p (tree args
)
28541 if (args
== error_mark_node
)
28544 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28546 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28547 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28549 tree arg
= TREE_VEC_ELT (level
, j
);
28550 tree packed_args
= NULL_TREE
;
28553 if (ARGUMENT_PACK_P (arg
))
28555 /* Look inside the argument pack. */
28556 packed_args
= ARGUMENT_PACK_ARGS (arg
);
28557 len
= TREE_VEC_LENGTH (packed_args
);
28560 for (k
= 0; k
< len
; ++k
)
28563 arg
= TREE_VEC_ELT (packed_args
, k
);
28565 if (error_operand_p (arg
))
28567 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28569 else if (arg
== any_targ_node
)
28570 /* An any_targ_node argument (added by add_defaults_to_ttp)
28571 makes the corresponding specialization not canonicalizable,
28572 since template_args_equal always return true for it. We
28573 may see this when called from bind_template_template_parm. */
28575 /* Checking current_function_decl because this structural
28576 comparison is only necessary for redeclaration. */
28577 else if (!current_function_decl
28578 && dependent_template_arg_p (arg
)
28579 && (cp_walk_tree_without_duplicates
28580 (&arg
, find_parm_usage_r
, NULL
)))
28581 /* The identity of a class template specialization that uses
28582 a function parameter depends on the identity of the function.
28583 And if this specialization appeared in the trailing return
28584 type thereof, we don't know the identity of the function
28585 (e.g. if it's a redeclaration or a new function) until we
28586 form its signature and go through duplicate_decls. Thus
28587 it's unsafe to decide on a canonical type now (which depends
28588 on the DECL_CONTEXT of the function parameter, which can get
28589 mutated after the fact by duplicate_decls), so just require
28590 structural equality in this case (PR52830). */
28599 /* Returns true if ARGS (a collection of template arguments) contains
28600 any dependent arguments. */
28603 any_dependent_template_arguments_p (const_tree args
)
28610 if (args
== error_mark_node
)
28613 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28615 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28616 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28617 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
28624 /* Returns true if ARGS contains any errors. */
28627 any_erroneous_template_args_p (const_tree args
)
28632 if (args
== error_mark_node
)
28635 if (args
&& TREE_CODE (args
) != TREE_VEC
)
28637 if (tree ti
= get_template_info (args
))
28638 args
= TI_ARGS (ti
);
28646 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28648 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28649 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28650 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
28657 /* Returns TRUE if the template TMPL is type-dependent. */
28660 dependent_template_p (tree tmpl
)
28662 if (TREE_CODE (tmpl
) == OVERLOAD
)
28664 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
28665 if (dependent_template_p (*iter
))
28670 /* Template template parameters are dependent. */
28671 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
28672 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
28674 /* So are names that have not been looked up. */
28675 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
28680 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
28683 dependent_template_id_p (tree tmpl
, tree args
)
28685 return (dependent_template_p (tmpl
)
28686 || any_dependent_template_arguments_p (args
));
28689 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
28693 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
28697 if (!processing_template_decl
)
28700 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
28702 tree decl
= TREE_VEC_ELT (declv
, i
);
28703 tree init
= TREE_VEC_ELT (initv
, i
);
28704 tree cond
= TREE_VEC_ELT (condv
, i
);
28705 tree incr
= TREE_VEC_ELT (incrv
, i
);
28707 if (type_dependent_expression_p (decl
)
28708 || TREE_CODE (decl
) == SCOPE_REF
)
28711 if (init
&& type_dependent_expression_p (init
))
28714 if (cond
== global_namespace
)
28717 if (type_dependent_expression_p (cond
))
28720 if (COMPARISON_CLASS_P (cond
)
28721 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
28722 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
28725 if (TREE_CODE (incr
) == MODOP_EXPR
)
28727 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
28728 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
28731 else if (type_dependent_expression_p (incr
))
28733 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
28735 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
28737 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
28739 tree t
= TREE_OPERAND (incr
, 1);
28740 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
28741 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
28744 /* If this loop has a class iterator with != comparison
28745 with increment other than i++/++i/i--/--i, make sure the
28746 increment is constant. */
28747 if (CLASS_TYPE_P (TREE_TYPE (decl
))
28748 && TREE_CODE (cond
) == NE_EXPR
)
28750 if (TREE_OPERAND (t
, 0) == decl
)
28751 t
= TREE_OPERAND (t
, 1);
28753 t
= TREE_OPERAND (t
, 0);
28754 if (TREE_CODE (t
) != INTEGER_CST
)
28764 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
28765 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
28766 no such TYPE can be found. Note that this function peers inside
28767 uninstantiated templates and therefore should be used only in
28768 extremely limited situations. ONLY_CURRENT_P restricts this
28769 peering to the currently open classes hierarchy (which is required
28770 when comparing types). */
28773 resolve_typename_type (tree type
, bool only_current_p
)
28782 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
28784 scope
= TYPE_CONTEXT (type
);
28785 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
28786 gcc_checking_assert (uses_template_parms (scope
));
28788 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
28789 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
28790 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
28791 representing the typedef. In that case TYPE_IDENTIFIER (type) is
28792 not the non-qualified identifier of the TYPENAME_TYPE anymore.
28793 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
28794 the TYPENAME_TYPE instead, we avoid messing up with a possible
28795 typedef variant case. */
28796 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
28798 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
28799 it first before we can figure out what NAME refers to. */
28800 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
28802 if (TYPENAME_IS_RESOLVING_P (scope
))
28803 /* Given a class template A with a dependent base with nested type C,
28804 typedef typename A::C::C C will land us here, as trying to resolve
28805 the initial A::C leads to the local C typedef, which leads back to
28806 A::C::C. So we break the recursion now. */
28809 scope
= resolve_typename_type (scope
, only_current_p
);
28811 /* If we don't know what SCOPE refers to, then we cannot resolve the
28813 if (!CLASS_TYPE_P (scope
))
28815 /* If this is a typedef, we don't want to look inside (c++/11987). */
28816 if (typedef_variant_p (type
))
28818 /* If SCOPE isn't the template itself, it will not have a valid
28819 TYPE_FIELDS list. */
28820 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
28821 /* scope is either the template itself or a compatible instantiation
28822 like X<T>, so look up the name in the original template. */
28823 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
28824 /* If scope has no fields, it can't be a current instantiation. Check this
28825 before currently_open_class to avoid infinite recursion (71515). */
28826 if (!TYPE_FIELDS (scope
))
28828 /* If the SCOPE is not the current instantiation, there's no reason
28829 to look inside it. */
28830 if (only_current_p
&& !currently_open_class (scope
))
28832 /* Enter the SCOPE so that name lookup will be resolved as if we
28833 were in the class definition. In particular, SCOPE will no
28834 longer be considered a dependent type. */
28835 pushed_scope
= push_scope (scope
);
28836 /* Look up the declaration. */
28837 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
28838 tf_warning_or_error
);
28840 result
= NULL_TREE
;
28842 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28843 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28844 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
28847 else if (identifier_p (fullname
)
28848 && TREE_CODE (decl
) == TYPE_DECL
)
28850 result
= TREE_TYPE (decl
);
28851 if (result
== error_mark_node
)
28852 result
= NULL_TREE
;
28854 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
28855 && DECL_CLASS_TEMPLATE_P (decl
))
28857 /* Obtain the template and the arguments. */
28858 tree tmpl
= TREE_OPERAND (fullname
, 0);
28859 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
28861 /* We get here with a plain identifier because a previous tentative
28862 parse of the nested-name-specifier as part of a ptr-operator saw
28863 ::template X<A>. The use of ::template is necessary in a
28864 ptr-operator, but wrong in a declarator-id.
28866 [temp.names]: In a qualified-id of a declarator-id, the keyword
28867 template shall not appear at the top level. */
28868 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
28869 "keyword %<template%> not allowed in declarator-id");
28872 tree args
= TREE_OPERAND (fullname
, 1);
28873 /* Instantiate the template. */
28874 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
28875 /*entering_scope=*/true,
28876 tf_error
| tf_user
);
28877 if (result
== error_mark_node
)
28878 result
= NULL_TREE
;
28881 /* Leave the SCOPE. */
28883 pop_scope (pushed_scope
);
28885 /* If we failed to resolve it, return the original typename. */
28889 /* If lookup found a typename type, resolve that too. */
28890 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
28892 /* Ill-formed programs can cause infinite recursion here, so we
28893 must catch that. */
28894 TYPENAME_IS_RESOLVING_P (result
) = 1;
28895 result
= resolve_typename_type (result
, only_current_p
);
28896 TYPENAME_IS_RESOLVING_P (result
) = 0;
28899 /* Qualify the resulting type. */
28900 quals
= cp_type_quals (type
);
28902 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
28907 /* EXPR is an expression which is not type-dependent. Return a proxy
28908 for EXPR that can be used to compute the types of larger
28909 expressions containing EXPR. */
28912 build_non_dependent_expr (tree expr
)
28914 tree orig_expr
= expr
;
28917 /* When checking, try to get a constant value for all non-dependent
28918 expressions in order to expose bugs in *_dependent_expression_p
28919 and constexpr. This can affect code generation, see PR70704, so
28920 only do this for -fchecking=2. */
28921 if (flag_checking
> 1
28922 && cxx_dialect
>= cxx11
28923 /* Don't do this during nsdmi parsing as it can lead to
28924 unexpected recursive instantiations. */
28925 && !parsing_nsdmi ()
28926 /* Don't do this during concept processing either and for
28927 the same reason. */
28928 && !processing_constraint_expression_p ())
28929 fold_non_dependent_expr (expr
, tf_none
);
28931 STRIP_ANY_LOCATION_WRAPPER (expr
);
28933 /* Preserve OVERLOADs; the functions must be available to resolve
28936 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
28937 inner_expr
= stmt_expr_value_expr (inner_expr
);
28938 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
28939 inner_expr
= TREE_OPERAND (inner_expr
, 0);
28940 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
28941 inner_expr
= TREE_OPERAND (inner_expr
, 1);
28942 if (is_overloaded_fn (inner_expr
)
28943 || TREE_CODE (inner_expr
) == OFFSET_REF
)
28945 /* There is no need to return a proxy for a variable, parameter
28947 if (VAR_P (expr
) || TREE_CODE (expr
) == PARM_DECL
28948 || TREE_CODE (expr
) == CONST_DECL
)
28950 /* Preserve string constants; conversions from string constants to
28951 "char *" are allowed, even though normally a "const char *"
28952 cannot be used to initialize a "char *". */
28953 if (TREE_CODE (expr
) == STRING_CST
)
28955 /* Preserve void and arithmetic constants, as an optimization -- there is no
28956 reason to create a new node. */
28957 if (TREE_CODE (expr
) == VOID_CST
28958 || TREE_CODE (expr
) == INTEGER_CST
28959 || TREE_CODE (expr
) == REAL_CST
)
28961 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28962 There is at least one place where we want to know that a
28963 particular expression is a throw-expression: when checking a ?:
28964 expression, there are special rules if the second or third
28965 argument is a throw-expression. */
28966 if (TREE_CODE (expr
) == THROW_EXPR
)
28969 /* Don't wrap an initializer list, we need to be able to look inside. */
28970 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
28973 /* Don't wrap a dummy object, we need to be able to test for it. */
28974 if (is_dummy_object (expr
))
28977 if (TREE_CODE (expr
) == COND_EXPR
)
28978 return build3 (COND_EXPR
,
28980 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
28981 (TREE_OPERAND (expr
, 1)
28982 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
28983 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
28984 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
28985 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
28986 return build2 (COMPOUND_EXPR
,
28988 TREE_OPERAND (expr
, 0),
28989 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
28991 /* If the type is unknown, it can't really be non-dependent */
28992 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
28994 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28995 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
28996 TREE_TYPE (expr
), expr
);
28999 /* ARGS is a vector of expressions as arguments to a function call.
29000 Replace the arguments with equivalent non-dependent expressions.
29001 This modifies ARGS in place. */
29004 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
29009 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
29011 tree newarg
= build_non_dependent_expr (arg
);
29013 (*args
)[ix
] = newarg
;
29017 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
29018 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
29019 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
29022 make_auto_1 (tree name
, bool set_canonical
, int level
= -1)
29025 level
= current_template_depth
+ 1;
29026 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
29027 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
29028 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
29029 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
29030 (0, level
, level
, TYPE_NAME (au
), NULL_TREE
);
29032 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
29033 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
29034 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
29035 if (name
== decltype_auto_identifier
)
29036 AUTO_IS_DECLTYPE (au
) = true;
29042 make_decltype_auto (void)
29044 return make_auto_1 (decltype_auto_identifier
, true);
29050 return make_auto_1 (auto_identifier
, true);
29053 /* Return a C++17 deduction placeholder for class template TMPL.
29054 There are represented as an 'auto' with the special level 0 and
29055 CLASS_PLACEHOLDER_TEMPLATE set. */
29058 make_template_placeholder (tree tmpl
)
29060 tree t
= make_auto_1 (auto_identifier
, false, /*level=*/0);
29061 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
29062 /* Our canonical type depends on the placeholder. */
29063 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29067 /* True iff T is a C++17 class template deduction placeholder. */
29070 template_placeholder_p (tree t
)
29072 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
29075 /* Make a "constrained auto" type-specifier. This is an auto or
29076 decltype(auto) type with constraints that must be associated after
29077 deduction. The constraint is formed from the given concept CON
29078 and its optional sequence of template arguments ARGS.
29080 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
29083 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
29085 /* Build the constraint. */
29086 tree tmpl
= DECL_TI_TEMPLATE (con
);
29088 if (TREE_CODE (con
) == FUNCTION_DECL
)
29089 expr
= ovl_make (tmpl
);
29090 ++processing_template_decl
;
29091 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
29092 --processing_template_decl
;
29094 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
)
29095 = build_tree_list (current_template_parms
, expr
);
29097 /* Our canonical type depends on the constraint. */
29098 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
29100 /* Attach the constraint to the type declaration. */
29101 return TYPE_NAME (type
);
29104 /* Make a "constrained auto" type-specifier. */
29107 make_constrained_auto (tree con
, tree args
)
29109 tree type
= make_auto_1 (auto_identifier
, false);
29110 return make_constrained_placeholder_type (type
, con
, args
);
29113 /* Make a "constrained decltype(auto)" type-specifier. */
29116 make_constrained_decltype_auto (tree con
, tree args
)
29118 tree type
= make_auto_1 (decltype_auto_identifier
, false);
29119 return make_constrained_placeholder_type (type
, con
, args
);
29122 /* Returns true if the placeholder type constraint T has any dependent
29123 (explicit) template arguments. */
29126 placeholder_type_constraint_dependent_p (tree t
)
29128 tree id
= unpack_concept_check (t
);
29129 tree args
= TREE_OPERAND (id
, 1);
29130 tree first
= TREE_VEC_ELT (args
, 0);
29131 if (ARGUMENT_PACK_P (first
))
29133 args
= expand_template_argument_pack (args
);
29134 first
= TREE_VEC_ELT (args
, 0);
29136 gcc_checking_assert (TREE_CODE (first
) == WILDCARD_DECL
29137 || is_auto (first
));
29138 for (int i
= 1; i
< TREE_VEC_LENGTH (args
); ++i
)
29139 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
29144 /* Build and return a concept definition. Like other templates, the
29145 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
29146 the TEMPLATE_DECL. */
29149 finish_concept_definition (cp_expr id
, tree init
, tree attrs
)
29151 gcc_assert (identifier_p (id
));
29152 gcc_assert (processing_template_decl
);
29154 location_t loc
= id
.get_location();
29156 /* A concept-definition shall not have associated constraints. */
29157 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
29159 error_at (loc
, "a concept cannot be constrained");
29160 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
29163 /* A concept-definition shall appear in namespace scope. Templates
29164 aren't allowed in block scope, so we only need to check for class
29166 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
29168 error_at (loc
, "concept %qE not in namespace scope", *id
);
29169 return error_mark_node
;
29172 if (current_template_depth
> 1)
29174 error_at (loc
, "concept %qE has multiple template parameter lists", *id
);
29175 return error_mark_node
;
29178 /* Initially build the concept declaration; its type is bool. */
29179 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
29180 DECL_CONTEXT (decl
) = current_scope ();
29181 DECL_INITIAL (decl
) = init
;
29184 cplus_decl_attributes (&decl
, attrs
, 0);
29186 set_originating_module (decl
, false);
29188 /* Push the enclosing template. */
29189 return push_template_decl (decl
);
29192 /* Given type ARG, return std::initializer_list<ARG>. */
29197 tree std_init_list
= lookup_qualified_name (std_node
, init_list_identifier
);
29199 if (std_init_list
== error_mark_node
29200 || !DECL_CLASS_TEMPLATE_P (std_init_list
))
29202 gcc_rich_location
richloc (input_location
);
29203 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
29204 error_at (&richloc
,
29205 "deducing from brace-enclosed initializer list"
29206 " requires %<#include <initializer_list>%>");
29208 return error_mark_node
;
29210 tree argvec
= make_tree_vec (1);
29211 TREE_VEC_ELT (argvec
, 0) = arg
;
29213 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
29214 NULL_TREE
, 0, tf_warning_or_error
);
29217 /* Replace auto in TYPE with std::initializer_list<auto>. */
29220 listify_autos (tree type
, tree auto_node
)
29222 tree init_auto
= listify (strip_top_quals (auto_node
));
29223 tree argvec
= make_tree_vec (1);
29224 TREE_VEC_ELT (argvec
, 0) = init_auto
;
29225 if (processing_template_decl
)
29226 argvec
= add_to_template_args (current_template_args (), argvec
);
29227 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
29230 /* Hash traits for hashing possibly constrained 'auto'
29231 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
29233 struct auto_hash
: default_hash_traits
<tree
>
29235 static inline hashval_t
hash (tree
);
29236 static inline bool equal (tree
, tree
);
29239 /* Hash the 'auto' T. */
29242 auto_hash::hash (tree t
)
29244 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
29245 /* Matching constrained-type-specifiers denote the same template
29246 parameter, so hash the constraint. */
29247 return hash_placeholder_constraint (c
);
29249 /* But unconstrained autos are all separate, so just hash the pointer. */
29250 return iterative_hash_object (t
, 0);
29253 /* Compare two 'auto's. */
29256 auto_hash::equal (tree t1
, tree t2
)
29261 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
29262 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
29264 /* Two unconstrained autos are distinct. */
29268 return equivalent_placeholder_constraints (c1
, c2
);
29271 /* for_each_template_parm callback for extract_autos: if t is a (possibly
29272 constrained) auto, add it to the vector. */
29275 extract_autos_r (tree t
, void *data
)
29277 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
29278 if (is_auto (t
) && !template_placeholder_p (t
))
29280 /* All the autos were built with index 0; fix that up now. */
29281 tree
*p
= hash
.find_slot (t
, INSERT
);
29284 /* If this is a repeated constrained-type-specifier, use the index we
29286 idx
= TEMPLATE_TYPE_IDX (*p
);
29289 /* Otherwise this is new, so use the current count. */
29291 idx
= hash
.elements () - 1;
29293 if (idx
!= TEMPLATE_TYPE_IDX (t
))
29295 gcc_checking_assert (TEMPLATE_TYPE_IDX (t
) == 0);
29296 gcc_checking_assert (TYPE_CANONICAL (t
) != t
);
29297 TEMPLATE_TYPE_IDX (t
) = idx
;
29298 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29302 /* Always keep walking. */
29306 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
29307 says they can appear anywhere in the type. */
29310 extract_autos (tree type
)
29312 hash_set
<tree
> visited
;
29313 hash_table
<auto_hash
> hash (2);
29315 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
29317 tree tree_vec
= make_tree_vec (hash
.elements());
29318 for (tree elt
: hash
)
29320 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
29321 TREE_VEC_ELT (tree_vec
, i
)
29322 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
29328 /* The stem for deduction guide names. */
29329 const char *const dguide_base
= "__dguide_";
29331 /* Return the name for a deduction guide for class template TMPL. */
29334 dguide_name (tree tmpl
)
29336 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
29337 tree tname
= TYPE_IDENTIFIER (type
);
29338 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
29339 + IDENTIFIER_LENGTH (tname
));
29340 memcpy (buf
, dguide_base
, strlen (dguide_base
));
29341 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
29342 IDENTIFIER_LENGTH (tname
) + 1);
29343 tree dname
= get_identifier (buf
);
29344 TREE_TYPE (dname
) = type
;
29348 /* True if NAME is the name of a deduction guide. */
29351 dguide_name_p (tree name
)
29353 return (TREE_CODE (name
) == IDENTIFIER_NODE
29354 && TREE_TYPE (name
)
29355 && startswith (IDENTIFIER_POINTER (name
), dguide_base
));
29358 /* True if FN is a deduction guide. */
29361 deduction_guide_p (const_tree fn
)
29364 if (tree name
= DECL_NAME (fn
))
29365 return dguide_name_p (name
);
29369 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
29372 copy_guide_p (const_tree fn
)
29374 gcc_assert (deduction_guide_p (fn
));
29375 if (!DECL_ARTIFICIAL (fn
))
29377 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
29378 return (TREE_CHAIN (parms
) == void_list_node
29379 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
29382 /* True if FN is a guide generated from a constructor template. */
29385 template_guide_p (const_tree fn
)
29387 gcc_assert (deduction_guide_p (fn
));
29388 if (!DECL_ARTIFICIAL (fn
))
29390 tree tmpl
= DECL_TI_TEMPLATE (fn
);
29391 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
29392 return PRIMARY_TEMPLATE_P (org
);
29396 /* True if FN is an aggregate initialization guide or the copy deduction
29400 builtin_guide_p (const_tree fn
)
29402 if (!deduction_guide_p (fn
))
29404 if (!DECL_ARTIFICIAL (fn
))
29405 /* Explicitly declared. */
29407 if (DECL_ABSTRACT_ORIGIN (fn
))
29408 /* Derived from a constructor. */
29413 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29414 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29415 template parameter types. Note that the handling of template template
29416 parameters relies on current_template_parms being set appropriately for the
29420 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
29421 tree tsubst_args
, tsubst_flags_t complain
)
29423 if (olddecl
== error_mark_node
)
29424 return error_mark_node
;
29426 tree oldidx
= get_template_parm_index (olddecl
);
29429 if (TREE_CODE (olddecl
) == TYPE_DECL
29430 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29432 tree oldtype
= TREE_TYPE (olddecl
);
29433 newtype
= cxx_make_type (TREE_CODE (oldtype
));
29434 TYPE_MAIN_VARIANT (newtype
) = newtype
;
29438 newtype
= TREE_TYPE (olddecl
);
29439 if (type_uses_auto (newtype
))
29441 // Substitute once to fix references to other template parameters.
29442 newtype
= tsubst (newtype
, tsubst_args
,
29443 complain
|tf_partial
, NULL_TREE
);
29444 // Now substitute again to reduce the level of the auto.
29445 newtype
= tsubst (newtype
, current_template_args (),
29446 complain
, NULL_TREE
);
29449 newtype
= tsubst (newtype
, tsubst_args
,
29450 complain
, NULL_TREE
);
29454 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
29455 DECL_NAME (olddecl
), newtype
);
29456 SET_DECL_TEMPLATE_PARM_P (newdecl
);
29459 if (TREE_CODE (olddecl
) == TYPE_DECL
29460 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29462 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
29463 = build_template_parm_index (index
, level
, level
,
29465 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29466 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29467 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
29469 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29471 DECL_TEMPLATE_RESULT (newdecl
)
29472 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
29473 DECL_NAME (olddecl
), newtype
);
29474 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
29475 // First create a copy (ttargs) of tsubst_args with an
29476 // additional level for the template template parameter's own
29477 // template parameters (ttparms).
29478 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
29479 (DECL_TEMPLATE_PARMS (olddecl
)));
29480 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
29481 tree ttargs
= make_tree_vec (depth
+ 1);
29482 for (int i
= 0; i
< depth
; ++i
)
29483 TREE_VEC_ELT (ttargs
, i
) = TMPL_ARGS_LEVEL (tsubst_args
, i
+ 1);
29484 TREE_VEC_ELT (ttargs
, depth
)
29485 = template_parms_level_to_args (ttparms
);
29486 // Substitute ttargs into ttparms to fix references to
29487 // other template parameters.
29488 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29489 complain
|tf_partial
);
29490 // Now substitute again with args based on tparms, to reduce
29491 // the level of the ttparms.
29492 ttargs
= current_template_args ();
29493 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29495 // Finally, tack the adjusted parms onto tparms.
29496 ttparms
= tree_cons (size_int (level
+ 1), ttparms
,
29497 copy_node (current_template_parms
));
29498 // As with all template template parms, the parameter list captured
29499 // by this template template parm that corresponds to its own level
29500 // should be empty. This avoids infinite recursion when structurally
29501 // comparing two such rewritten template template parms (PR102479).
29502 gcc_assert (!TREE_VEC_LENGTH
29503 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl
)))));
29504 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms
)) == level
);
29505 TREE_VALUE (TREE_CHAIN (ttparms
)) = make_tree_vec (0);
29507 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
29510 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
29511 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
29513 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
29517 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
29519 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
29520 TREE_CODE (oldconst
),
29521 DECL_NAME (oldconst
), newtype
);
29522 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
29523 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
29524 SET_DECL_TEMPLATE_PARM_P (newconst
);
29525 newidx
= build_template_parm_index (index
, level
, level
,
29526 newconst
, newtype
);
29527 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29528 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29529 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
29535 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29536 template parameter. */
29539 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
29540 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
29542 tree olddecl
= TREE_VALUE (oldelt
);
29543 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
29545 if (newdecl
== error_mark_node
)
29546 return error_mark_node
;
29547 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
29548 targs
, complain
, NULL_TREE
);
29549 tree list
= build_tree_list (newdef
, newdecl
);
29550 TEMPLATE_PARM_CONSTRAINTS (list
)
29551 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
29552 targs
, complain
, NULL_TREE
);
29553 int depth
= TMPL_ARGS_DEPTH (targs
);
29554 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
29558 /* Returns a C++17 class deduction guide template based on the constructor
29559 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29560 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29561 aggregate initialization guide. OUTER_ARGS are the template arguments
29562 for the enclosing scope of the class. */
29565 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
29567 tree tparms
, targs
, fparms
, fargs
, ci
;
29568 bool memtmpl
= false;
29571 tree fn_tmpl
= NULL_TREE
;
29575 ++processing_template_decl
;
29576 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
29577 --processing_template_decl
;
29580 if (!DECL_DECLARES_FUNCTION_P (ctor
))
29584 bool copy_p
= TYPE_REF_P (ctor
);
29586 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
29588 fparms
= void_list_node
;
29590 else if (TREE_CODE (ctor
) == TREE_LIST
)
29593 gcc_unreachable ();
29595 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
29596 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
29597 targs
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
29600 loc
= DECL_SOURCE_LOCATION (ctmpl
);
29601 explicit_p
= false;
29605 ++processing_template_decl
;
29608 complain
|= tf_dguide
;
29611 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
29612 : DECL_TI_TEMPLATE (ctor
));
29614 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
29615 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
29617 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
29618 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29619 fully specialized args for the enclosing class. Strip those off, as
29620 the deduction guide won't have those template parameters. */
29621 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
29622 TMPL_PARMS_DEPTH (tparms
));
29623 /* Discard the 'this' parameter. */
29624 fparms
= FUNCTION_ARG_CHAIN (ctor
);
29625 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
29626 ci
= get_constraints (ctor
);
29627 loc
= DECL_SOURCE_LOCATION (ctor
);
29628 explicit_p
= DECL_NONCONVERTING_P (ctor
);
29630 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
29634 /* For a member template constructor, we need to flatten the two
29635 template parameter lists into one, and then adjust the function
29636 signature accordingly. This gets...complicated. */
29637 tree save_parms
= current_template_parms
;
29639 /* For a member template we should have two levels of parms/args, one
29640 for the class and one for the constructor. We stripped
29641 specialized args for further enclosing classes above. */
29642 const int depth
= 2;
29643 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
29645 /* Template args for translating references to the two-level template
29646 parameters into references to the one-level template parameters we
29648 tree tsubst_args
= copy_node (targs
);
29649 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
29650 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
29652 /* Template parms for the constructor template. */
29653 tree ftparms
= TREE_VALUE (tparms
);
29654 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
29655 /* Template parms for the class template. */
29656 tparms
= TREE_CHAIN (tparms
);
29657 tree ctparms
= TREE_VALUE (tparms
);
29658 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
29659 /* Template parms for the deduction guide start as a copy of the
29660 template parms for the class. We set current_template_parms for
29661 lookup_template_class_1. */
29662 current_template_parms
= tparms
= copy_node (tparms
);
29663 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
29664 for (unsigned i
= 0; i
< clen
; ++i
)
29665 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
29667 /* Now we need to rewrite the constructor parms to append them to the
29669 for (unsigned i
= 0; i
< flen
; ++i
)
29671 unsigned index
= i
+ clen
;
29672 unsigned level
= 1;
29673 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
29675 = rewrite_tparm_list (oldelt
, index
, level
,
29676 tsubst_args
, i
, complain
);
29677 if (newelt
== error_mark_node
)
29679 TREE_VEC_ELT (new_vec
, index
) = newelt
;
29682 /* Now we have a final set of template parms to substitute into the
29683 function signature. */
29684 targs
= template_parms_to_args (tparms
);
29685 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
29687 if (fparms
== error_mark_node
)
29692 /* FIXME: We'd like to avoid substituting outer template
29693 arguments into the constraint ahead of time, but the
29694 construction of tsubst_args assumes that outer arguments
29695 are already substituted in. */
29696 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29697 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
29700 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
29701 cp_unevaluated_operand. */
29703 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
29704 current_template_parms
= save_parms
;
29708 /* Substitute in the same arguments to rewrite class members into
29709 references to members of an unknown specialization. */
29711 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
29712 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
29716 /* FIXME: As above. */
29717 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29718 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
29722 --processing_template_decl
;
29724 return error_mark_node
;
29729 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
29730 tparms
= copy_node (tparms
);
29731 INNERMOST_TEMPLATE_PARMS (tparms
)
29732 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
29735 tree fntype
= build_function_type (type
, fparms
);
29736 tree ded_fn
= build_lang_decl_loc (loc
,
29738 dguide_name (type
), fntype
);
29739 DECL_ARGUMENTS (ded_fn
) = fargs
;
29740 DECL_ARTIFICIAL (ded_fn
) = true;
29741 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
29742 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
29743 DECL_ARTIFICIAL (ded_tmpl
) = true;
29744 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
29745 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
29747 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
29749 set_constraints (ded_tmpl
, ci
);
29754 /* Add to LIST the member types for the reshaped initializer CTOR. */
29757 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
29759 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
29760 tree idx
, val
; unsigned i
;
29761 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
29763 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
29764 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
29765 && CONSTRUCTOR_BRACES_ELIDED_P (val
))
29767 tree subelt
= NULL_TREE
;
29768 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
29769 subelt
= TREE_TYPE (ftype
);
29770 list
= collect_ctor_idx_types (val
, list
, subelt
);
29773 tree arg
= NULL_TREE
;
29774 if (i
== v
->length() - 1
29775 && PACK_EXPANSION_P (ftype
))
29776 /* Give the trailing pack expansion parameter a default argument to
29777 match aggregate initialization behavior, even if we deduce the
29778 length of the pack separately to more than we have initializers. */
29779 arg
= build_constructor (init_list_type_node
, NULL
);
29780 /* if ei is of array type and xi is a braced-init-list or string literal,
29781 Ti is an rvalue reference to the declared type of ei */
29782 STRIP_ANY_LOCATION_WRAPPER (val
);
29783 if (TREE_CODE (ftype
) == ARRAY_TYPE
29784 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
29785 || TREE_CODE (val
) == STRING_CST
))
29787 if (TREE_CODE (val
) == STRING_CST
)
29788 ftype
= cp_build_qualified_type
29789 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
29790 ftype
= (cp_build_reference_type
29791 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
29793 list
= tree_cons (arg
, ftype
, list
);
29799 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
29802 is_spec_or_derived (tree etype
, tree tmpl
)
29804 if (!etype
|| !CLASS_TYPE_P (etype
))
29807 etype
= cv_unqualified (etype
);
29808 tree type
= TREE_TYPE (tmpl
);
29809 tree tparms
= (INNERMOST_TEMPLATE_PARMS
29810 (DECL_TEMPLATE_PARMS (tmpl
)));
29811 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29812 int err
= unify (tparms
, targs
, type
, etype
,
29813 UNIFY_ALLOW_DERIVED
, /*explain*/false);
29818 static tree
alias_ctad_tweaks (tree
, tree
);
29820 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
29824 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
29826 if (cxx_dialect
< cxx20
)
29829 if (init
== NULL_TREE
)
29832 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29834 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29835 tree tinfo
= get_template_info (under
);
29836 if (tree guide
= maybe_aggr_guide (TI_TEMPLATE (tinfo
), init
, args
))
29837 return alias_ctad_tweaks (tmpl
, guide
);
29841 /* We might be creating a guide for a class member template, e.g.,
29843 template<typename U> struct A {
29844 template<typename T> struct B { T t; };
29847 At this point, A will have been instantiated. Below, we need to
29848 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
29849 const bool member_template_p
29850 = (DECL_TEMPLATE_INFO (tmpl
)
29851 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl
)));
29852 tree type
= TREE_TYPE (tmpl
);
29853 tree template_type
= (member_template_p
29854 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl
))
29856 if (!CP_AGGREGATE_TYPE_P (template_type
))
29859 /* No aggregate candidate for copy-initialization. */
29860 if (args
->length() == 1)
29862 tree val
= (*args
)[0];
29863 if (is_spec_or_derived (TREE_TYPE (val
), tmpl
))
29867 /* If we encounter a problem, we just won't add the candidate. */
29868 tsubst_flags_t complain
= tf_none
;
29870 tree parms
= NULL_TREE
;
29871 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29873 init
= reshape_init (template_type
, init
, complain
);
29874 if (init
== error_mark_node
)
29876 parms
= collect_ctor_idx_types (init
, parms
);
29877 /* If we're creating a deduction guide for a member class template,
29878 we've used the original template pattern type for the reshape_init
29879 above; this is done because we want PARMS to be a template parameter
29880 type, something that can be deduced when used as a function template
29881 parameter. At this point the outer class template has already been
29882 partially instantiated (we deferred the deduction until the enclosing
29883 scope is non-dependent). Therefore we have to partially instantiate
29884 PARMS, so that its template level is properly reduced and we don't get
29885 mismatches when deducing types using the guide with PARMS. */
29886 if (member_template_p
)
29888 ++processing_template_decl
;
29889 parms
= tsubst (parms
, DECL_TI_ARGS (tmpl
), complain
, init
);
29890 --processing_template_decl
;
29893 else if (TREE_CODE (init
) == TREE_LIST
)
29895 int len
= list_length (init
);
29896 for (tree field
= TYPE_FIELDS (type
);
29898 --len
, field
= DECL_CHAIN (field
))
29900 field
= next_aggregate_field (field
);
29903 tree ftype
= finish_decltype_type (field
, true, complain
);
29904 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
29908 /* Aggregate initialization doesn't apply to an initializer expression. */
29914 parms
= nreverse (parms
);
29915 TREE_CHAIN (last
) = void_list_node
;
29916 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
29923 /* UGUIDES are the deduction guides for the underlying template of alias
29924 template TMPL; adjust them to be deduction guides for TMPL. */
29927 alias_ctad_tweaks (tree tmpl
, tree uguides
)
29929 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29930 class type (9.2.8.2) where the template-name names an alias template A,
29931 the defining-type-id of A must be of the form
29933 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29935 as specified in 9.2.8.2. The guides of A are the set of functions or
29936 function templates formed as follows. For each function or function
29937 template f in the guides of the template named by the simple-template-id
29938 of the defining-type-id, the template arguments of the return type of f
29939 are deduced from the defining-type-id of A according to the process in
29940 13.10.2.5 with the exception that deduction does not fail if not all
29941 template arguments are deduced. Let g denote the result of substituting
29942 these deductions into f. If substitution succeeds, form a function or
29943 function template f' with the following properties and add it to the set
29946 * The function type of f' is the function type of g.
29948 * If f is a function template, f' is a function template whose template
29949 parameter list consists of all the template parameters of A (including
29950 their default template arguments) that appear in the above deductions or
29951 (recursively) in their default template arguments, followed by the
29952 template parameters of f that were not deduced (including their default
29953 template arguments), otherwise f' is not a function template.
29955 * The associated constraints (13.5.2) are the conjunction of the
29956 associated constraints of g and a constraint that is satisfied if and only
29957 if the arguments of A are deducible (see below) from the return type.
29959 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29962 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29963 considered to be so as well.
29965 * The explicit-specifier of f' is the explicit-specifier of g (if
29968 /* This implementation differs from the above in two significant ways:
29970 1) We include all template parameters of A, not just some.
29971 2) The added constraint is same_type instead of deducible.
29973 I believe that while it's probably possible to construct a testcase that
29974 behaves differently with this simplification, it should have the same
29975 effect for real uses. Including all template parameters means that we
29976 deduce all parameters of A when resolving the call, so when we're in the
29977 constraint we don't need to deduce them again, we can just check whether
29978 the deduction produced the desired result. */
29980 tsubst_flags_t complain
= tf_warning_or_error
;
29981 tree atype
= TREE_TYPE (tmpl
);
29982 tree aguides
= NULL_TREE
;
29983 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
29984 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
29985 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29986 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
29990 location_t loc
= DECL_SOURCE_LOCATION (f
);
29991 tree ret
= TREE_TYPE (TREE_TYPE (f
));
29993 if (TREE_CODE (f
) == TEMPLATE_DECL
)
29995 processing_template_decl_sentinel
ptds (/*reset*/false);
29996 ++processing_template_decl
;
29998 /* Deduce template arguments for f from the type-id of A. */
29999 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
30000 unsigned len
= TREE_VEC_LENGTH (ftparms
);
30001 tree targs
= make_tree_vec (len
);
30002 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
30006 /* The number of parms for f' is the number of parms for A plus
30007 non-deduced parms of f. */
30008 unsigned ndlen
= 0;
30010 for (unsigned i
= 0; i
< len
; ++i
)
30011 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30013 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
30015 /* Set current_template_parms as in build_deduction_guide. */
30016 auto ctp
= make_temp_override (current_template_parms
);
30017 current_template_parms
= copy_node (DECL_TEMPLATE_PARMS (tmpl
));
30018 TREE_VALUE (current_template_parms
) = gtparms
;
30020 /* First copy over the parms of A. */
30021 for (j
= 0; j
< natparms
; ++j
)
30022 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
30023 /* Now rewrite the non-deduced parms of f. */
30024 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
30025 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30028 unsigned index
= j
++;
30029 unsigned level
= 1;
30030 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
30031 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
30032 targs
, i
, complain
);
30033 TREE_VEC_ELT (gtparms
, index
) = list
;
30035 gtparms
= build_tree_list (size_one_node
, gtparms
);
30037 /* Substitute the deduced arguments plus the rewritten template
30038 parameters into f to get g. This covers the type, copyness,
30039 guideness, and explicit-specifier. */
30042 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
30043 if cp_unevaluated_operand. */
30045 g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
30047 if (g
== error_mark_node
)
30049 DECL_USE_TEMPLATE (g
) = 0;
30050 fprime
= build_template_decl (g
, gtparms
, false);
30051 DECL_TEMPLATE_RESULT (fprime
) = g
;
30052 TREE_TYPE (fprime
) = TREE_TYPE (g
);
30053 tree gtargs
= template_parms_to_args (gtparms
);
30054 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
30055 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
30057 /* Substitute the associated constraints. */
30058 tree ci
= get_constraints (f
);
30060 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
30061 if (ci
== error_mark_node
)
30064 /* Add a constraint that the return type matches the instantiation of
30065 A with the same template arguments. */
30066 ret
= TREE_TYPE (TREE_TYPE (fprime
));
30067 if (!same_type_p (atype
, ret
)
30068 /* FIXME this should mean they don't compare as equivalent. */
30069 || dependent_alias_template_spec_p (atype
, nt_opaque
))
30071 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME
, atype
, ret
);
30072 ci
= append_constraint (ci
, same
);
30077 remove_constraints (fprime
);
30078 set_constraints (fprime
, ci
);
30083 /* For a non-template deduction guide, if the arguments of A aren't
30084 deducible from the return type, don't add the candidate. */
30085 tree targs
= make_tree_vec (natparms
);
30086 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
30087 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
30088 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30094 aguides
= lookup_add (fprime
, aguides
);
30100 /* Return artificial deduction guides built from the constructors of class
30104 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
30106 tree outer_args
= outer_template_args (tmpl
);
30107 tree type
= TREE_TYPE (most_general_template (tmpl
));
30109 tree cands
= NULL_TREE
;
30111 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
30113 /* Skip inherited constructors. */
30114 if (iter
.using_p ())
30117 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
30118 cands
= lookup_add (guide
, cands
);
30121 /* Add implicit default constructor deduction guide. */
30122 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
30124 tree guide
= build_deduction_guide (type
, type
, outer_args
,
30126 cands
= lookup_add (guide
, cands
);
30129 /* Add copy guide. */
30131 tree gtype
= build_reference_type (type
);
30132 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
30134 cands
= lookup_add (guide
, cands
);
30140 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
30142 /* Return the non-aggregate deduction guides for deducible template TMPL. The
30143 aggregate candidate is added separately because it depends on the
30144 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
30148 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
30150 tree guides
= NULL_TREE
;
30151 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30153 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30154 tree tinfo
= get_template_info (under
);
30155 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
30160 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
30161 dguide_name (tmpl
),
30162 LOOK_want::NORMAL
, /*complain*/false);
30163 if (guides
== error_mark_node
)
30164 guides
= NULL_TREE
;
30166 any_dguides_p
= true;
30169 /* Cache the deduction guides for a template. We also remember the result of
30170 lookup, and rebuild everything if it changes; should be very rare. */
30171 tree_pair_p cache
= NULL
;
30173 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
30176 if (cache
->purpose
== guides
)
30177 return cache
->value
;
30181 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
30182 cache
->purpose
= guides
;
30185 tree cands
= NULL_TREE
;
30186 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30187 cands
= alias_ctad_tweaks (tmpl
, guides
);
30190 cands
= ctor_deduction_guides_for (tmpl
, complain
);
30191 for (ovl_iterator
it (guides
); it
; ++it
)
30192 cands
= lookup_add (*it
, cands
);
30195 cache
->value
= cands
;
30199 /* Return whether TMPL is a (class template argument-) deducible template. */
30202 ctad_template_p (tree tmpl
)
30204 /* A deducible template is either a class template or is an alias template
30205 whose defining-type-id is of the form
30207 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30209 where the nested-name-specifier (if any) is non-dependent and the
30210 template-name of the simple-template-id names a deducible template. */
30212 if (DECL_CLASS_TEMPLATE_P (tmpl
)
30213 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30215 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
30217 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30218 if (tree tinfo
= get_template_info (orig
))
30219 return ctad_template_p (TI_TEMPLATE (tinfo
));
30223 /* Deduce template arguments for the class template placeholder PTYPE for
30224 template TMPL based on the initializer INIT, and return the resulting
30228 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
30229 int flags
, tsubst_flags_t complain
)
30231 /* We should have handled this in the caller. */
30232 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30235 /* If the class was erroneous, don't try to deduce, because that
30236 can generate a lot of diagnostic. */
30237 if (TREE_TYPE (tmpl
)
30238 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl
))
30239 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl
)))
30242 /* Wait until the enclosing scope is non-dependent. */
30243 if (DECL_CLASS_SCOPE_P (tmpl
)
30244 && dependent_type_p (DECL_CONTEXT (tmpl
)))
30247 /* Initializing one placeholder from another. */
30249 && (TREE_CODE (init
) == TEMPLATE_PARM_INDEX
30250 || (TREE_CODE (init
) == EXPR_PACK_EXPANSION
30251 && (TREE_CODE (PACK_EXPANSION_PATTERN (init
))
30252 == TEMPLATE_PARM_INDEX
)))
30253 && is_auto (TREE_TYPE (init
))
30254 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
30255 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
30257 if (!ctad_template_p (tmpl
))
30259 if (complain
& tf_error
)
30260 error ("non-deducible template %qT used without template arguments", tmpl
);
30261 return error_mark_node
;
30263 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
30265 if (complain
& tf_error
)
30267 /* Be permissive with equivalent alias templates. */
30268 tree u
= get_underlying_template (tmpl
);
30269 diagnostic_t dk
= (u
== tmpl
) ? DK_ERROR
: DK_PEDWARN
;
30271 = emit_diagnostic (dk
, input_location
, 0,
30272 "alias template deduction only available "
30273 "with %<-std=c++20%> or %<-std=gnu++20%>");
30275 return error_mark_node
;
30276 else if (complained
)
30278 inform (input_location
, "use %qD directly instead", u
);
30283 return error_mark_node
;
30286 /* Wait until the initializer is non-dependent. */
30287 if (type_dependent_expression_p (init
))
30290 /* Don't bother with the alias rules for an equivalent template. */
30291 tmpl
= get_underlying_template (tmpl
);
30293 tree type
= TREE_TYPE (tmpl
);
30295 bool try_list_cand
= false;
30296 bool list_init_p
= false;
30298 releasing_vec rv_args
= NULL
;
30299 vec
<tree
,va_gc
> *&args
= *&rv_args
;
30300 if (init
== NULL_TREE
)
30301 args
= make_tree_vector ();
30302 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30304 list_init_p
= true;
30305 try_list_cand
= true;
30306 if (CONSTRUCTOR_NELTS (init
) == 1
30307 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init
))
30309 /* As an exception, the first phase in 16.3.1.7 (considering the
30310 initializer list as a single argument) is omitted if the
30311 initializer list consists of a single expression of type cv U,
30312 where U is a specialization of C or a class derived from a
30313 specialization of C. */
30314 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
30315 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
30316 try_list_cand
= false;
30318 if (try_list_cand
|| is_std_init_list (type
))
30319 args
= make_tree_vector_single (init
);
30321 args
= make_tree_vector_from_ctor (init
);
30323 else if (TREE_CODE (init
) == TREE_LIST
)
30324 args
= make_tree_vector_from_list (init
);
30326 args
= make_tree_vector_single (init
);
30328 /* Do this now to avoid problems with erroneous args later on. */
30329 args
= resolve_args (args
, complain
);
30331 return error_mark_node
;
30333 bool any_dguides_p
= false;
30334 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
30335 if (cands
== error_mark_node
)
30336 return error_mark_node
;
30338 /* Prune explicit deduction guides in copy-initialization context (but
30339 not copy-list-initialization). */
30340 bool elided
= false;
30341 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
30343 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
30344 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30349 /* Found a nonconverting guide, prune the candidates. */
30350 tree pruned
= NULL_TREE
;
30351 for (lkp_iterator
iter (cands
); iter
; ++iter
)
30352 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30353 pruned
= lookup_add (*iter
, pruned
);
30359 if (!any_dguides_p
)
30360 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
30361 cands
= lookup_add (guide
, cands
);
30363 tree fndecl
= error_mark_node
;
30365 /* If this is list-initialization and the class has a list guide, first
30366 try deducing from the list as a single argument, as [over.match.list]. */
30369 tree list_cands
= NULL_TREE
;
30370 for (tree dg
: lkp_range (cands
))
30371 if (is_list_ctor (dg
))
30372 list_cands
= lookup_add (dg
, list_cands
);
30374 fndecl
= perform_dguide_overload_resolution (list_cands
, args
, tf_none
);
30375 if (fndecl
== error_mark_node
)
30377 /* That didn't work, now try treating the list as a sequence of
30379 release_tree_vector (args
);
30380 args
= make_tree_vector_from_ctor (init
);
30381 args
= resolve_args (args
, complain
);
30383 return error_mark_node
;
30387 if (elided
&& !cands
)
30389 error ("cannot deduce template arguments for copy-initialization"
30390 " of %qT, as it has no non-explicit deduction guides or "
30391 "user-declared constructors", type
);
30392 return error_mark_node
;
30394 else if (!cands
&& fndecl
== error_mark_node
)
30396 error ("cannot deduce template arguments of %qT, as it has no viable "
30397 "deduction guides", type
);
30398 return error_mark_node
;
30401 if (fndecl
== error_mark_node
)
30402 fndecl
= perform_dguide_overload_resolution (cands
, args
, tf_none
);
30404 if (fndecl
== error_mark_node
)
30406 if (complain
& tf_warning_or_error
)
30408 error ("class template argument deduction failed:");
30409 perform_dguide_overload_resolution (cands
, args
, complain
);
30411 inform (input_location
, "explicit deduction guides not considered "
30412 "for copy-initialization");
30414 return error_mark_node
;
30416 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30417 constructor is chosen, the initialization is ill-formed. */
30418 else if (flags
& LOOKUP_ONLYCONVERTING
)
30420 if (DECL_NONCONVERTING_P (fndecl
))
30422 if (complain
& tf_warning_or_error
)
30424 // TODO: Pass down location from cp_finish_decl.
30425 error ("class template argument deduction for %qT failed: "
30426 "explicit deduction guide selected in "
30427 "copy-list-initialization", type
);
30428 inform (DECL_SOURCE_LOCATION (fndecl
),
30429 "explicit deduction guide declared here");
30432 return error_mark_node
;
30436 /* If CTAD succeeded but the type doesn't have any explicit deduction
30437 guides, this deduction might not be what the user intended. */
30438 if (fndecl
!= error_mark_node
&& !any_dguides_p
&& (complain
& tf_warning
))
30440 if ((!DECL_IN_SYSTEM_HEADER (fndecl
)
30441 || global_dc
->dc_warn_system_headers
)
30442 && warning (OPT_Wctad_maybe_unsupported
,
30443 "%qT may not intend to support class template argument "
30444 "deduction", type
))
30445 inform (input_location
, "add a deduction guide to suppress this "
30449 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl
)),
30450 cp_type_quals (ptype
));
30453 /* Return true if INIT is an unparenthesized id-expression or an
30454 unparenthesized class member access. Used for the argument of
30458 unparenthesized_id_or_class_member_access_p (tree init
)
30460 STRIP_ANY_LOCATION_WRAPPER (init
);
30462 /* We need to be able to tell '(r)' and 'r' apart (when it's of
30463 reference type). Only the latter is an id-expression. */
30464 if (REFERENCE_REF_P (init
)
30465 && !REF_PARENTHESIZED_P (init
))
30466 init
= TREE_OPERAND (init
, 0);
30467 return (DECL_P (init
)
30468 || ((TREE_CODE (init
) == COMPONENT_REF
30469 || TREE_CODE (init
) == SCOPE_REF
)
30470 && !REF_PARENTHESIZED_P (init
)));
30473 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
30474 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
30475 The CONTEXT determines the context in which auto deduction is performed
30476 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
30478 OUTER_TARGS is used during template argument deduction (context == adc_unify)
30479 to properly substitute the result. It's also used in the adc_unify and
30480 adc_requirement contexts to communicate the necessary template arguments
30481 to satisfaction. OUTER_TARGS is ignored in other contexts.
30483 For partial-concept-ids, extra args may be appended to the list of deduced
30484 template arguments prior to determining constraint satisfaction. */
30487 do_auto_deduction (tree type
, tree init
, tree auto_node
,
30488 tsubst_flags_t complain
, auto_deduction_context context
,
30489 tree outer_targs
, int flags
)
30491 if (init
== error_mark_node
)
30492 return error_mark_node
;
30494 if (init
&& type_dependent_expression_p (init
)
30495 && context
!= adc_unify
)
30496 /* Defining a subset of type-dependent expressions that we can deduce
30497 from ahead of time isn't worth the trouble. */
30500 /* Similarly, we can't deduce from another undeduced decl. */
30501 if (init
&& undeduced_auto_decl (init
))
30504 /* We may be doing a partial substitution, but we still want to replace
30506 complain
&= ~tf_partial
;
30508 /* In C++23, we must deduce the type to int&& for code like
30509 decltype(auto) f(int&& x) { return (x); }
30511 auto&& f(int x) { return x; }
30512 so we use treat_lvalue_as_rvalue_p. But don't do it for
30513 decltype(auto) f(int x) { return x; }
30514 where we should deduce 'int' rather than 'int&&'; transmogrifying
30515 INIT to an rvalue would break that. */
30517 if (cxx_dialect
>= cxx23
30518 && context
== adc_return_type
30519 && (!AUTO_IS_DECLTYPE (auto_node
)
30520 || !unparenthesized_id_or_class_member_access_p (init
))
30521 && (r
= treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init
),
30525 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
30526 /* C++17 class template argument deduction. */
30527 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
30529 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
30530 /* Nothing we can do with this, even in deduction context. */
30533 location_t loc
= cp_expr_loc_or_input_loc (init
);
30535 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
30536 with either a new invented type template parameter U or, if the
30537 initializer is a braced-init-list (8.5.4), with
30538 std::initializer_list<U>. */
30539 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30541 if (!DIRECT_LIST_INIT_P (init
))
30542 type
= listify_autos (type
, auto_node
);
30543 else if (CONSTRUCTOR_NELTS (init
) == 1)
30544 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
30547 if (complain
& tf_warning_or_error
)
30549 if (permerror (loc
, "direct-list-initialization of "
30550 "%<auto%> requires exactly one element"))
30552 "for deduction to %<std::initializer_list%>, use copy-"
30553 "list-initialization (i.e. add %<=%> before the %<{%>)");
30555 type
= listify_autos (type
, auto_node
);
30559 if (type
== error_mark_node
)
30560 return error_mark_node
;
30562 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30564 /* We don't recurse here because we can't deduce from a nested
30565 initializer_list. */
30566 if (CONSTRUCTOR_ELTS (init
))
30567 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
30568 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
30571 init
= resolve_nondeduced_context (init
, complain
);
30574 if (context
== adc_decomp_type
30575 && auto_node
== type
30576 && init
!= error_mark_node
30577 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
30579 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
30580 and initializer has array type, deduce cv-qualified array type. */
30581 targs
= make_tree_vec (1);
30582 TREE_VEC_ELT (targs
, 0) = TREE_TYPE (init
);
30584 else if (AUTO_IS_DECLTYPE (auto_node
))
30586 const bool id
= unparenthesized_id_or_class_member_access_p (init
);
30587 tree deduced
= finish_decltype_type (init
, id
, complain
);
30588 deduced
= canonicalize_type_argument (deduced
, complain
);
30589 if (deduced
== error_mark_node
)
30590 return error_mark_node
;
30591 targs
= make_tree_vec (1);
30592 TREE_VEC_ELT (targs
, 0) = deduced
;
30596 if (error_operand_p (init
))
30597 return error_mark_node
;
30599 tree parms
= build_tree_list (NULL_TREE
, type
);
30602 if (flag_concepts_ts
)
30603 tparms
= extract_autos (type
);
30606 tparms
= make_tree_vec (1);
30607 TREE_VEC_ELT (tparms
, 0)
30608 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
30611 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
30612 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
30614 NULL
, /*explain_p=*/false);
30617 if (processing_template_decl
)
30618 /* Try again at instantiation time. */
30620 if (type
&& type
!= error_mark_node
30621 && (complain
& tf_error
))
30622 /* If type is error_mark_node a diagnostic must have been
30623 emitted by now. Also, having a mention to '<type error>'
30624 in the diagnostic is not really useful to the user. */
30627 && FNDECL_USED_AUTO (current_function_decl
)
30629 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
30630 && LAMBDA_FUNCTION_P (current_function_decl
))
30631 error_at (loc
, "unable to deduce lambda return type from %qE",
30634 error_at (loc
, "unable to deduce %qT from %qE", type
, init
);
30635 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
30637 NULL
, /*explain_p=*/true);
30639 return error_mark_node
;
30643 /* Check any placeholder constraints against the deduced type. */
30644 if (processing_template_decl
&& context
== adc_unify
)
30645 /* Constraints will be checked after deduction. */;
30646 else if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
30648 if (processing_template_decl
)
30650 gcc_checking_assert (context
== adc_variable_type
30651 || context
== adc_return_type
30652 || context
== adc_decomp_type
);
30653 gcc_checking_assert (!type_dependent_expression_p (init
));
30654 /* If the constraint is dependent, we need to wait until
30655 instantiation time to resolve the placeholder. */
30656 if (placeholder_type_constraint_dependent_p (constr
))
30660 if (context
== adc_return_type
30661 || context
== adc_variable_type
30662 || context
== adc_decomp_type
)
30663 if (tree fn
= current_function_decl
)
30664 if (DECL_TEMPLATE_INFO (fn
) || LAMBDA_FUNCTION_P (fn
))
30666 outer_targs
= DECL_TEMPLATE_INFO (fn
)
30667 ? DECL_TI_ARGS (fn
) : NULL_TREE
;
30668 if (LAMBDA_FUNCTION_P (fn
))
30670 /* As in satisfy_declaration_constraints. */
30671 tree regen_args
= lambda_regenerating_args (fn
);
30673 outer_targs
= add_to_template_args (regen_args
, outer_targs
);
30675 outer_targs
= regen_args
;
30679 tree full_targs
= add_to_template_args (outer_targs
, targs
);
30681 /* HACK: Compensate for callers not always communicating all levels of
30682 outer template arguments by filling in the outermost missing levels
30683 with dummy levels before checking satisfaction. We'll still crash
30684 if the constraint depends on a template argument belonging to one of
30685 these missing levels, but this hack otherwise allows us to handle a
30686 large subset of possible constraints (including all non-dependent
30688 if (int missing_levels
= (TEMPLATE_TYPE_ORIG_LEVEL (auto_node
)
30689 - TMPL_ARGS_DEPTH (full_targs
)))
30691 tree dummy_levels
= make_tree_vec (missing_levels
);
30692 for (int i
= 0; i
< missing_levels
; ++i
)
30693 TREE_VEC_ELT (dummy_levels
, i
) = make_tree_vec (0);
30694 full_targs
= add_to_template_args (dummy_levels
, full_targs
);
30697 if (!constraints_satisfied_p (auto_node
, full_targs
))
30699 if (complain
& tf_warning_or_error
)
30701 auto_diagnostic_group d
;
30704 case adc_unspecified
:
30706 error_at (loc
, "placeholder constraints not satisfied");
30708 case adc_variable_type
:
30709 case adc_decomp_type
:
30710 error_at (loc
, "deduced initializer does not satisfy "
30711 "placeholder constraints");
30713 case adc_return_type
:
30714 error_at (loc
, "deduced return type does not satisfy "
30715 "placeholder constraints");
30717 case adc_requirement
:
30718 error_at (loc
, "deduced expression type does not satisfy "
30719 "placeholder constraints");
30722 diagnose_constraints (loc
, auto_node
, full_targs
);
30724 return error_mark_node
;
30728 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 1)
30729 /* The outer template arguments are already substituted into type
30730 (but we still may have used them for constraint checking above). */;
30731 else if (context
== adc_unify
)
30732 targs
= add_to_template_args (outer_targs
, targs
);
30733 else if (processing_template_decl
)
30734 targs
= add_to_template_args (current_template_args (), targs
);
30735 return tsubst (type
, targs
, complain
, NULL_TREE
);
30738 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
30742 splice_late_return_type (tree type
, tree late_return_type
)
30744 if (late_return_type
)
30746 gcc_assert (is_auto (type
) || seen_error ());
30747 return late_return_type
;
30750 if (tree auto_node
= find_type_usage (type
, is_auto
))
30751 if (TEMPLATE_TYPE_LEVEL (auto_node
) <= current_template_depth
)
30753 /* In an abbreviated function template we didn't know we were dealing
30754 with a function template when we saw the auto return type, so rebuild
30755 the return type using an auto with the correct level. */
30756 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (auto_node
), false);
30757 tree auto_vec
= make_tree_vec (1);
30758 TREE_VEC_ELT (auto_vec
, 0) = new_auto
;
30759 tree targs
= add_outermost_template_args (current_template_args (),
30761 /* Also rebuild the constraint info in terms of the new auto. */
30762 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
))
30763 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto
)
30764 = build_tree_list (current_template_parms
,
30765 tsubst_constraint (TREE_VALUE (ci
), targs
,
30766 tf_none
, NULL_TREE
));
30767 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
30768 return tsubst (type
, targs
, tf_none
, NULL_TREE
);
30773 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
30774 'decltype(auto)' or a deduced class template. */
30777 is_auto (const_tree type
)
30779 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
30780 && (TYPE_IDENTIFIER (type
) == auto_identifier
30781 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
30787 /* for_each_template_parm callback for type_uses_auto. */
30790 is_auto_r (tree tp
, void */
*data*/
)
30792 return is_auto (tp
);
30795 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
30796 a use of `auto'. Returns NULL_TREE otherwise. */
30799 type_uses_auto (tree type
)
30801 if (type
== NULL_TREE
)
30803 else if (flag_concepts_ts
)
30805 /* The Concepts TS allows multiple autos in one type-specifier; just
30806 return the first one we find, do_auto_deduction will collect all of
30808 if (uses_template_parms (type
))
30809 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
30810 /*visited*/NULL
, /*nondeduced*/false);
30815 return find_type_usage (type
, is_auto
);
30818 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
30819 concepts are enabled, auto is acceptable in template arguments, but
30820 only when TEMPL identifies a template class. Return TRUE if any
30821 such errors were reported. */
30824 check_auto_in_tmpl_args (tree tmpl
, tree args
)
30826 if (!flag_concepts_ts
)
30827 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
30828 have already been rejected by the parser more generally. */
30831 /* If there were previous errors, nevermind. */
30832 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
30835 /* If TMPL is an identifier, we're parsing and we can't tell yet
30836 whether TMPL is supposed to be a type, a function or a variable.
30837 We'll only be able to tell during template substitution, so we
30838 expect to be called again then. If concepts are enabled and we
30839 know we have a type, we're ok. */
30840 if (identifier_p (tmpl
)
30842 && (DECL_TYPE_TEMPLATE_P (tmpl
)
30843 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))))
30846 /* Quickly search for any occurrences of auto; usually there won't
30847 be any, and then we'll avoid allocating the vector. */
30848 if (!type_uses_auto (args
))
30851 bool errors
= false;
30853 tree vec
= extract_autos (args
);
30854 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
30856 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
30857 error_at (DECL_SOURCE_LOCATION (xauto
),
30858 "invalid use of %qT in template argument", xauto
);
30865 /* Recursively walk over && expressions searching for EXPR. Return a reference
30866 to that expression. */
30868 static tree
*find_template_requirement (tree
*t
, tree key
)
30872 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
30874 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
30876 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
30882 /* Convert the generic type parameters in PARM that match the types given in the
30883 range [START_IDX, END_IDX) from the current_template_parms into generic type
30887 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
30889 tree current
= current_template_parms
;
30890 int depth
= TMPL_PARMS_DEPTH (current
);
30891 current
= INNERMOST_TEMPLATE_PARMS (current
);
30892 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
30894 for (int i
= 0; i
< start_idx
; ++i
)
30895 TREE_VEC_ELT (replacement
, i
)
30896 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30898 for (int i
= start_idx
; i
< end_idx
; ++i
)
30900 /* Create a distinct parameter pack type from the current parm and add it
30901 to the replacement args to tsubst below into the generic function
30903 tree node
= TREE_VEC_ELT (current
, i
);
30904 tree o
= TREE_TYPE (TREE_VALUE (node
));
30905 tree t
= copy_type (o
);
30906 TEMPLATE_TYPE_PARM_INDEX (t
)
30907 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
30909 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
30910 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
30911 TYPE_MAIN_VARIANT (t
) = t
;
30912 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
30913 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
30914 TREE_VEC_ELT (replacement
, i
) = t
;
30916 /* Replace the current template parameter with new pack. */
30917 TREE_VALUE (node
) = TREE_CHAIN (t
);
30919 /* Surgically adjust the associated constraint of adjusted parameter
30920 and it's corresponding contribution to the current template
30922 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
30924 tree id
= unpack_concept_check (constr
);
30925 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
30926 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
30927 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
30929 /* If there was a constraint, we also need to replace that in
30930 the template requirements, which we've already built. */
30931 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
30932 reqs
= find_template_requirement (reqs
, constr
);
30937 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
30938 TREE_VEC_ELT (replacement
, i
)
30939 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30941 /* If there are more levels then build up the replacement with the outer
30944 replacement
= add_to_template_args (template_parms_to_args
30945 (TREE_CHAIN (current_template_parms
)),
30948 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
30951 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30955 declare_integer_pack (void)
30957 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
30958 build_function_type_list (integer_type_node
,
30961 NULL_TREE
, ECF_CONST
);
30962 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
30963 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
30964 CP_BUILT_IN_INTEGER_PACK
);
30967 /* Walk the decl or type specialization table calling FN on each
30971 walk_specializations (bool decls_p
,
30972 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
30975 spec_hash_table
*table
= decls_p
? decl_specializations
30976 : type_specializations
;
30977 spec_hash_table::iterator
end (table
->end ());
30978 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
30979 fn (decls_p
, *iter
, data
);
30982 /* Lookup the specialization of *ELT, in the decl or type
30983 specialization table. Return the SPEC that's already there, or
30984 NULL if nothing. */
30987 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
)
30989 hash_table
<spec_hasher
> *specializations
30990 = decl_p
? decl_specializations
: type_specializations
;
30991 hashval_t hash
= spec_hasher::hash (elt
);
30992 auto *slot
= specializations
->find_slot_with_hash (elt
, hash
, NO_INSERT
);
30995 return (*slot
)->spec
;
31000 /* Return flags encoding whether SPEC is on the instantiation and/or
31001 specialization lists of TMPL. */
31004 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
31006 unsigned flags
= 0;
31008 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
31009 inst
; inst
= TREE_CHAIN (inst
))
31010 if (TREE_VALUE (inst
) == decl
)
31016 if (CLASS_TYPE_P (TREE_TYPE (decl
))
31017 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
31018 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
31019 /* Only need to search if DECL is a partial specialization. */
31020 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
31021 part
; part
= TREE_CHAIN (part
))
31022 if (TREE_VALUE (part
) == decl
)
31031 /* Add a new specialization described by SPEC. DECL is the
31032 maybe-template decl and FLAGS is as returned from
31033 get_mergeable_specialization_flags. */
31036 add_mergeable_specialization (bool decl_p
, bool alias_p
, spec_entry
*elt
,
31037 tree decl
, unsigned flags
)
31039 hashval_t hash
= spec_hasher::hash (elt
);
31042 auto *slot
= decl_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
31044 gcc_checking_assert (!*slot
);
31045 auto entry
= ggc_alloc
<spec_entry
> ();
31051 elt
->spec
= TREE_TYPE (elt
->spec
);
31052 gcc_checking_assert (elt
->spec
);
31056 if (!decl_p
|| alias_p
)
31058 auto *slot
= type_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
31060 /* We don't distinguish different constrained partial type
31061 specializations, so there could be duplicates. Everything else
31063 if (!(flags
& 2 && *slot
))
31065 gcc_checking_assert (!*slot
);
31067 auto entry
= ggc_alloc
<spec_entry
> ();
31074 DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
)
31075 = tree_cons (elt
->args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
));
31079 /* A partial specialization. */
31080 tree cons
= tree_cons (elt
->args
, decl
,
31081 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
));
31082 TREE_TYPE (cons
) = decl_p
? TREE_TYPE (elt
->spec
) : elt
->spec
;
31083 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
) = cons
;
31087 /* Set up the hash tables for template instantiations. */
31090 init_template_processing (void)
31092 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
31093 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
31095 if (cxx_dialect
>= cxx11
)
31096 declare_integer_pack ();
31099 /* Print stats about the template hash tables for -fstats. */
31102 print_template_statistics (void)
31104 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
31105 "%f collisions\n", (long) decl_specializations
->size (),
31106 (long) decl_specializations
->elements (),
31107 decl_specializations
->collisions ());
31108 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
31109 "%f collisions\n", (long) type_specializations
->size (),
31110 (long) type_specializations
->elements (),
31111 type_specializations
->collisions ());
31116 namespace selftest
{
31118 /* Verify that build_non_dependent_expr () works, for various expressions,
31119 and that location wrappers don't affect the results. */
31122 test_build_non_dependent_expr ()
31124 location_t loc
= BUILTINS_LOCATION
;
31126 /* Verify constants, without and with location wrappers. */
31127 tree int_cst
= build_int_cst (integer_type_node
, 42);
31128 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
31130 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
31131 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
31132 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
31134 tree string_lit
= build_string (4, "foo");
31135 TREE_TYPE (string_lit
) = char_array_type_node
;
31136 string_lit
= fix_string_type (string_lit
);
31137 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
31139 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
31140 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
31141 ASSERT_EQ (wrapped_string_lit
,
31142 build_non_dependent_expr (wrapped_string_lit
));
31145 /* Verify that type_dependent_expression_p () works correctly, even
31146 in the presence of location wrapper nodes. */
31149 test_type_dependent_expression_p ()
31151 location_t loc
= BUILTINS_LOCATION
;
31153 tree name
= get_identifier ("foo");
31155 /* If no templates are involved, nothing is type-dependent. */
31156 gcc_assert (!processing_template_decl
);
31157 ASSERT_FALSE (type_dependent_expression_p (name
));
31159 ++processing_template_decl
;
31161 /* Within a template, an unresolved name is always type-dependent. */
31162 ASSERT_TRUE (type_dependent_expression_p (name
));
31164 /* Ensure it copes with NULL_TREE and errors. */
31165 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
31166 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
31168 /* A USING_DECL in a template should be type-dependent, even if wrapped
31169 with a location wrapper (PR c++/83799). */
31170 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
31171 TREE_TYPE (using_decl
) = integer_type_node
;
31172 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
31173 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
31174 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
31175 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
31177 --processing_template_decl
;
31180 /* Run all of the selftests within this file. */
31185 test_build_non_dependent_expr ();
31186 test_type_dependent_expression_p ();
31189 } // namespace selftest
31191 #endif /* #if CHECKING_P */
31193 #include "gt-cp-pt.h"