1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_blank
|| !saved
)
87 local_specializations
= new hash_map
<tree
, tree
>;
89 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
92 local_specialization_stack::~local_specialization_stack ()
94 delete local_specializations
;
95 local_specializations
= saved
;
98 /* True if we've recursed into fn_type_unification too many times. */
99 static bool excessive_deduction_depth
;
101 struct GTY((for_user
)) spec_entry
108 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
110 static hashval_t
hash (spec_entry
*);
111 static bool equal (spec_entry
*, spec_entry
*);
114 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
116 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133 enum template_base_result
{
135 tbr_ambiguous_baseclass
,
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
153 static void tsubst_enum (tree
, tree
, tree
);
154 static tree
add_to_template_args (tree
, tree
);
155 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
156 static int check_non_deducible_conversion (tree
, tree
, int, int,
157 struct conversion
**, bool);
158 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
160 static int type_unification_real (tree
, tree
, tree
, const tree
*,
161 unsigned int, int, unification_kind_t
,
162 vec
<deferred_access_check
, va_gc
> **,
164 static void note_template_header (int);
165 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
167 static tree
convert_template_argument (tree
, tree
, tree
,
168 tsubst_flags_t
, int, tree
);
169 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
170 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
171 static tree
expand_template_argument_pack (tree
);
172 static tree
build_template_parm_index (int, int, int, tree
, tree
);
173 static bool inline_needs_template_parms (tree
, bool);
174 static void push_inline_template_parms_recursive (tree
, int);
175 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
176 static int mark_template_parm (tree
, void *);
177 static int template_parm_this_level_p (tree
, void *);
178 static tree
tsubst_friend_function (tree
, tree
);
179 static tree
tsubst_friend_class (tree
, tree
);
180 static int can_complete_type_without_circularity (tree
);
181 static tree
get_bindings (tree
, tree
, tree
, bool);
182 static int template_decl_level (tree
);
183 static int check_cv_quals_for_unify (int, tree
, tree
);
184 static int unify_pack_expansion (tree
, tree
, tree
,
185 tree
, unification_kind_t
, bool, bool);
186 static tree
copy_template_args (tree
);
187 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
188 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
189 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
190 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
192 static bool check_specialization_scope (void);
193 static tree
process_partial_specialization (tree
);
194 static void set_current_access_from_decl (tree
);
195 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
197 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
198 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
200 static bool template_template_parm_bindings_ok_p (tree
, tree
);
201 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
202 static tree
for_each_template_parm_r (tree
*, int *, void *);
203 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
204 static void copy_default_args_to_explicit_spec (tree
);
205 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
206 static bool dependent_template_arg_p (tree
);
207 static bool any_template_arguments_need_structural_equality_p (tree
);
208 static bool dependent_type_p_r (tree
);
209 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
210 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
211 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
212 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
214 static tree
listify (tree
);
215 static tree
listify_autos (tree
, tree
);
216 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
217 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
218 static bool complex_alias_template_p (const_tree tmpl
);
219 static tree
get_underlying_template (tree
);
220 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
221 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
222 static tree
make_argument_pack (tree
);
223 static void register_parameter_specializations (tree
, tree
);
224 static tree
enclosing_instantiation_of (tree tctx
);
226 /* Make the current scope suitable for access checking when we are
227 processing T. T can be FUNCTION_DECL for instantiated function
228 template, VAR_DECL for static member variable, or TYPE_DECL for
229 alias template (needed by instantiate_decl). */
232 push_access_scope (tree t
)
234 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
235 || TREE_CODE (t
) == TYPE_DECL
);
237 if (DECL_FRIEND_CONTEXT (t
))
238 push_nested_class (DECL_FRIEND_CONTEXT (t
));
239 else if (DECL_CLASS_SCOPE_P (t
))
240 push_nested_class (DECL_CONTEXT (t
));
242 push_to_top_level ();
244 if (TREE_CODE (t
) == FUNCTION_DECL
)
246 vec_safe_push (saved_access_scope
, current_function_decl
);
247 current_function_decl
= t
;
251 /* Restore the scope set up by push_access_scope. T is the node we
255 pop_access_scope (tree t
)
257 if (TREE_CODE (t
) == FUNCTION_DECL
)
258 current_function_decl
= saved_access_scope
->pop();
260 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
263 pop_from_top_level ();
266 /* Do any processing required when DECL (a member template
267 declaration) is finished. Returns the TEMPLATE_DECL corresponding
268 to DECL, unless it is a specialization, in which case the DECL
269 itself is returned. */
272 finish_member_template_decl (tree decl
)
274 if (decl
== error_mark_node
)
275 return error_mark_node
;
277 gcc_assert (DECL_P (decl
));
279 if (TREE_CODE (decl
) == TYPE_DECL
)
283 type
= TREE_TYPE (decl
);
284 if (type
== error_mark_node
)
285 return error_mark_node
;
286 if (MAYBE_CLASS_TYPE_P (type
)
287 && CLASSTYPE_TEMPLATE_INFO (type
)
288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
290 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
291 check_member_template (tmpl
);
296 else if (TREE_CODE (decl
) == FIELD_DECL
)
297 error_at (DECL_SOURCE_LOCATION (decl
),
298 "data member %qD cannot be a member template", decl
);
299 else if (DECL_TEMPLATE_INFO (decl
))
301 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
303 check_member_template (DECL_TI_TEMPLATE (decl
));
304 return DECL_TI_TEMPLATE (decl
);
310 error_at (DECL_SOURCE_LOCATION (decl
),
311 "invalid member template declaration %qD", decl
);
313 return error_mark_node
;
316 /* Create a template info node. */
319 build_template_info (tree template_decl
, tree template_args
)
321 tree result
= make_node (TEMPLATE_INFO
);
322 TI_TEMPLATE (result
) = template_decl
;
323 TI_ARGS (result
) = template_args
;
327 /* Return the template info node corresponding to T, whatever T is. */
330 get_template_info (const_tree t
)
332 tree tinfo
= NULL_TREE
;
334 if (!t
|| t
== error_mark_node
)
337 if (TREE_CODE (t
) == NAMESPACE_DECL
338 || TREE_CODE (t
) == PARM_DECL
)
341 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
342 tinfo
= DECL_TEMPLATE_INFO (t
);
344 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
347 if (OVERLOAD_TYPE_P (t
))
348 tinfo
= TYPE_TEMPLATE_INFO (t
);
349 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
350 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
355 /* Returns the template nesting level of the indicated class TYPE.
365 A<T>::B<U> has depth two, while A<T> has depth one.
366 Both A<T>::B<int> and A<int>::B<U> have depth one, if
367 they are instantiations, not specializations.
369 This function is guaranteed to return 0 if passed NULL_TREE so
370 that, for example, `template_class_depth (current_class_type)' is
374 template_class_depth (tree type
)
378 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
380 tree tinfo
= get_template_info (type
);
382 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
383 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
387 type
= CP_DECL_CONTEXT (type
);
388 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
389 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
391 type
= CP_TYPE_CONTEXT (type
);
397 /* Return TRUE if NODE instantiates a template that has arguments of
398 its own, be it directly a primary template or indirectly through a
399 partial specializations. */
401 instantiates_primary_template_p (tree node
)
403 tree tinfo
= get_template_info (node
);
407 tree tmpl
= TI_TEMPLATE (tinfo
);
408 if (PRIMARY_TEMPLATE_P (tmpl
))
411 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
414 /* So now we know we have a specialization, but it could be a full
415 or a partial specialization. To tell which, compare the depth of
416 its template arguments with those of its context. */
418 tree ctxt
= DECL_CONTEXT (tmpl
);
419 tree ctinfo
= get_template_info (ctxt
);
423 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
424 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
427 /* Subroutine of maybe_begin_member_template_processing.
428 Returns true if processing DECL needs us to push template parms. */
431 inline_needs_template_parms (tree decl
, bool nsdmi
)
433 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
436 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
437 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
440 /* Subroutine of maybe_begin_member_template_processing.
441 Push the template parms in PARMS, starting from LEVELS steps into the
442 chain, and ending at the beginning, since template parms are listed
446 push_inline_template_parms_recursive (tree parmlist
, int levels
)
448 tree parms
= TREE_VALUE (parmlist
);
452 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
454 ++processing_template_decl
;
455 current_template_parms
456 = tree_cons (size_int (processing_template_decl
),
457 parms
, current_template_parms
);
458 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
460 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
462 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
464 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
466 if (error_operand_p (parm
))
469 gcc_assert (DECL_P (parm
));
471 switch (TREE_CODE (parm
))
479 /* Push the CONST_DECL. */
480 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
489 /* Restore the template parameter context for a member template, a
490 friend template defined in a class definition, or a non-template
491 member of template class. */
494 maybe_begin_member_template_processing (tree decl
)
498 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
502 tree ctx
= DECL_CONTEXT (decl
);
503 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
504 /* Disregard full specializations (c++/60999). */
505 && uses_template_parms (ctx
)
506 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
509 if (inline_needs_template_parms (decl
, nsdmi
))
511 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
512 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
514 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
517 parms
= TREE_CHAIN (parms
);
520 push_inline_template_parms_recursive (parms
, levels
);
523 /* Remember how many levels of template parameters we pushed so that
524 we can pop them later. */
525 inline_parm_levels
.safe_push (levels
);
528 /* Undo the effects of maybe_begin_member_template_processing. */
531 maybe_end_member_template_processing (void)
536 if (inline_parm_levels
.length () == 0)
539 last
= inline_parm_levels
.pop ();
540 for (i
= 0; i
< last
; ++i
)
542 --processing_template_decl
;
543 current_template_parms
= TREE_CHAIN (current_template_parms
);
548 /* Return a new template argument vector which contains all of ARGS,
549 but has as its innermost set of arguments the EXTRA_ARGS. */
552 add_to_template_args (tree args
, tree extra_args
)
559 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
562 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
563 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
565 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
566 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
568 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
569 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
574 /* Like add_to_template_args, but only the outermost ARGS are added to
575 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
576 (EXTRA_ARGS) levels are added. This function is used to combine
577 the template arguments from a partial instantiation with the
578 template arguments used to attain the full instantiation from the
579 partial instantiation. */
582 add_outermost_template_args (tree args
, tree extra_args
)
586 /* If there are more levels of EXTRA_ARGS than there are ARGS,
587 something very fishy is going on. */
588 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
590 /* If *all* the new arguments will be the EXTRA_ARGS, just return
592 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
595 /* For the moment, we make ARGS look like it contains fewer levels. */
596 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
598 new_args
= add_to_template_args (args
, extra_args
);
600 /* Now, we restore ARGS to its full dimensions. */
601 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
606 /* Return the N levels of innermost template arguments from the ARGS. */
609 get_innermost_template_args (tree args
, int n
)
617 /* If N is 1, just return the innermost set of template arguments. */
619 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
621 /* If we're not removing anything, just return the arguments we were
623 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
624 gcc_assert (extra_levels
>= 0);
625 if (extra_levels
== 0)
628 /* Make a new set of arguments, not containing the outer arguments. */
629 new_args
= make_tree_vec (n
);
630 for (i
= 1; i
<= n
; ++i
)
631 SET_TMPL_ARGS_LEVEL (new_args
, i
,
632 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
637 /* The inverse of get_innermost_template_args: Return all but the innermost
638 EXTRA_LEVELS levels of template arguments from the ARGS. */
641 strip_innermost_template_args (tree args
, int extra_levels
)
644 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
649 /* If N is 1, just return the outermost set of template arguments. */
651 return TMPL_ARGS_LEVEL (args
, 1);
653 /* If we're not removing anything, just return the arguments we were
655 gcc_assert (extra_levels
>= 0);
656 if (extra_levels
== 0)
659 /* Make a new set of arguments, not containing the inner arguments. */
660 new_args
= make_tree_vec (n
);
661 for (i
= 1; i
<= n
; ++i
)
662 SET_TMPL_ARGS_LEVEL (new_args
, i
,
663 TMPL_ARGS_LEVEL (args
, i
));
668 /* We've got a template header coming up; push to a new level for storing
672 begin_template_parm_list (void)
674 /* We use a non-tag-transparent scope here, which causes pushtag to
675 put tags in this scope, rather than in the enclosing class or
676 namespace scope. This is the right thing, since we want
677 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
678 global template class, push_template_decl handles putting the
679 TEMPLATE_DECL into top-level scope. For a nested template class,
682 template <class T> struct S1 {
683 template <class T> struct S2 {};
686 pushtag contains special code to insert the TEMPLATE_DECL for S2
687 at the right scope. */
688 begin_scope (sk_template_parms
, NULL
);
689 ++processing_template_decl
;
690 ++processing_template_parmlist
;
691 note_template_header (0);
693 /* Add a dummy parameter level while we process the parameter list. */
694 current_template_parms
695 = tree_cons (size_int (processing_template_decl
),
697 current_template_parms
);
700 /* This routine is called when a specialization is declared. If it is
701 invalid to declare a specialization here, an error is reported and
702 false is returned, otherwise this routine will return true. */
705 check_specialization_scope (void)
707 tree scope
= current_scope ();
711 An explicit specialization shall be declared in the namespace of
712 which the template is a member, or, for member templates, in the
713 namespace of which the enclosing class or enclosing class
714 template is a member. An explicit specialization of a member
715 function, member class or static data member of a class template
716 shall be declared in the namespace of which the class template
718 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
720 error ("explicit specialization in non-namespace scope %qD", scope
);
726 In an explicit specialization declaration for a member of a class
727 template or a member template that appears in namespace scope,
728 the member template and some of its enclosing class templates may
729 remain unspecialized, except that the declaration shall not
730 explicitly specialize a class member template if its enclosing
731 class templates are not explicitly specialized as well. */
732 if (current_template_parms
)
734 error ("enclosing class templates are not explicitly specialized");
741 /* We've just seen template <>. */
744 begin_specialization (void)
746 begin_scope (sk_template_spec
, NULL
);
747 note_template_header (1);
748 return check_specialization_scope ();
751 /* Called at then end of processing a declaration preceded by
755 end_specialization (void)
758 reset_specialization ();
761 /* Any template <>'s that we have seen thus far are not referring to a
762 function specialization. */
765 reset_specialization (void)
767 processing_specialization
= 0;
768 template_header_count
= 0;
771 /* We've just seen a template header. If SPECIALIZATION is nonzero,
772 it was of the form template <>. */
775 note_template_header (int specialization
)
777 processing_specialization
= specialization
;
778 template_header_count
++;
781 /* We're beginning an explicit instantiation. */
784 begin_explicit_instantiation (void)
786 gcc_assert (!processing_explicit_instantiation
);
787 processing_explicit_instantiation
= true;
792 end_explicit_instantiation (void)
794 gcc_assert (processing_explicit_instantiation
);
795 processing_explicit_instantiation
= false;
798 /* An explicit specialization or partial specialization of TMPL is being
799 declared. Check that the namespace in which the specialization is
800 occurring is permissible. Returns false iff it is invalid to
801 specialize TMPL in the current namespace. */
804 check_specialization_namespace (tree tmpl
)
806 tree tpl_ns
= decl_namespace_context (tmpl
);
810 An explicit specialization shall be declared in a namespace enclosing the
811 specialized template. An explicit specialization whose declarator-id is
812 not qualified shall be declared in the nearest enclosing namespace of the
813 template, or, if the namespace is inline (7.3.1), any namespace from its
814 enclosing namespace set. */
815 if (current_scope() != DECL_CONTEXT (tmpl
)
816 && !at_namespace_scope_p ())
818 error ("specialization of %qD must appear at namespace scope", tmpl
);
822 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
823 /* Same or enclosing namespace. */
827 auto_diagnostic_group d
;
828 if (permerror (input_location
,
829 "specialization of %qD in different namespace", tmpl
))
830 inform (DECL_SOURCE_LOCATION (tmpl
),
831 " from definition of %q#D", tmpl
);
836 /* SPEC is an explicit instantiation. Check that it is valid to
837 perform this explicit instantiation in the current namespace. */
840 check_explicit_instantiation_namespace (tree spec
)
844 /* DR 275: An explicit instantiation shall appear in an enclosing
845 namespace of its template. */
846 ns
= decl_namespace_context (spec
);
847 if (!is_nested_namespace (current_namespace
, ns
))
848 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
849 "(which does not enclose namespace %qD)",
850 spec
, current_namespace
, ns
);
853 /* Returns the type of a template specialization only if that
854 specialization needs to be defined. Otherwise (e.g., if the type has
855 already been defined), the function returns NULL_TREE. */
858 maybe_new_partial_specialization (tree type
)
860 /* An implicit instantiation of an incomplete type implies
861 the definition of a new class template.
869 Here, S<T*> is an implicit instantiation of S whose type
871 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
874 /* It can also be the case that TYPE is a completed specialization.
875 Continuing the previous example, suppose we also declare:
881 Here, S<T*> refers to the specialization S<T*> defined
882 above. However, we need to differentiate definitions because
883 we intend to define a new partial specialization. In this case,
884 we rely on the fact that the constraints are different for
885 this declaration than that above.
887 Note that we also get here for injected class names and
888 late-parsed template definitions. We must ensure that we
889 do not create new type declarations for those cases. */
890 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
892 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
893 tree args
= CLASSTYPE_TI_ARGS (type
);
895 /* If there are no template parameters, this cannot be a new
896 partial template specialization? */
897 if (!current_template_parms
)
900 /* The injected-class-name is not a new partial specialization. */
901 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
904 /* If the constraints are not the same as those of the primary
905 then, we can probably create a new specialization. */
906 tree type_constr
= current_template_constraints ();
908 if (type
== TREE_TYPE (tmpl
))
910 tree main_constr
= get_constraints (tmpl
);
911 if (equivalent_constraints (type_constr
, main_constr
))
915 /* Also, if there's a pre-existing specialization with matching
916 constraints, then this also isn't new. */
917 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
920 tree spec_tmpl
= TREE_VALUE (specs
);
921 tree spec_args
= TREE_PURPOSE (specs
);
922 tree spec_constr
= get_constraints (spec_tmpl
);
923 if (comp_template_args (args
, spec_args
)
924 && equivalent_constraints (type_constr
, spec_constr
))
926 specs
= TREE_CHAIN (specs
);
929 /* Create a new type node (and corresponding type decl)
930 for the newly declared specialization. */
931 tree t
= make_class_type (TREE_CODE (type
));
932 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
933 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
935 /* We only need a separate type node for storing the definition of this
936 partial specialization; uses of S<T*> are unconstrained, so all are
937 equivalent. So keep TYPE_CANONICAL the same. */
938 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
940 /* Build the corresponding type decl. */
941 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
942 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
943 DECL_SOURCE_LOCATION (d
) = input_location
;
944 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
945 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
953 /* The TYPE is being declared. If it is a template type, that means it
954 is a partial specialization. Do appropriate error-checking. */
957 maybe_process_partial_specialization (tree type
)
961 if (type
== error_mark_node
)
962 return error_mark_node
;
964 /* A lambda that appears in specialization context is not itself a
966 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
969 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
971 error ("name of class shadows template template parameter %qD",
973 return error_mark_node
;
976 context
= TYPE_CONTEXT (type
);
978 if (TYPE_ALIAS_P (type
))
980 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
982 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
983 error ("specialization of alias template %qD",
984 TI_TEMPLATE (tinfo
));
986 error ("explicit specialization of non-template %qT", type
);
987 return error_mark_node
;
989 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
991 /* This is for ordinary explicit specialization and partial
992 specialization of a template class such as:
994 template <> class C<int>;
998 template <class T> class C<T*>;
1000 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1002 if (tree t
= maybe_new_partial_specialization (type
))
1004 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1005 && !at_namespace_scope_p ())
1006 return error_mark_node
;
1007 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1008 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1009 if (processing_template_decl
)
1011 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1012 if (decl
== error_mark_node
)
1013 return error_mark_node
;
1014 return TREE_TYPE (decl
);
1017 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1018 error ("specialization of %qT after instantiation", type
);
1019 else if (errorcount
&& !processing_specialization
1020 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1021 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1022 /* Trying to define a specialization either without a template<> header
1023 or in an inappropriate place. We've already given an error, so just
1024 bail now so we don't actually define the specialization. */
1025 return error_mark_node
;
1027 else if (CLASS_TYPE_P (type
)
1028 && !CLASSTYPE_USE_TEMPLATE (type
)
1029 && CLASSTYPE_TEMPLATE_INFO (type
)
1030 && context
&& CLASS_TYPE_P (context
)
1031 && CLASSTYPE_TEMPLATE_INFO (context
))
1033 /* This is for an explicit specialization of member class
1034 template according to [temp.expl.spec/18]:
1036 template <> template <class U> class C<int>::D;
1038 The context `C<int>' must be an implicit instantiation.
1039 Otherwise this is just a member class template declared
1042 template <> class C<int> { template <class U> class D; };
1043 template <> template <class U> class C<int>::D;
1045 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1046 while in the second case, `C<int>::D' is a primary template
1047 and `C<T>::D' may not exist. */
1049 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1050 && !COMPLETE_TYPE_P (type
))
1053 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1055 if (current_namespace
1056 != decl_namespace_context (tmpl
))
1058 if (permerror (input_location
,
1059 "specialization of %qD in different namespace",
1061 inform (DECL_SOURCE_LOCATION (tmpl
),
1062 "from definition of %q#D", tmpl
);
1065 /* Check for invalid specialization after instantiation:
1067 template <> template <> class C<int>::D<int>;
1068 template <> template <class U> class C<int>::D; */
1070 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1071 t
; t
= TREE_CHAIN (t
))
1073 tree inst
= TREE_VALUE (t
);
1074 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1075 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1077 /* We already have a full specialization of this partial
1078 instantiation, or a full specialization has been
1079 looked up but not instantiated. Reassign it to the
1080 new member specialization template. */
1084 elt
.tmpl
= most_general_template (tmpl
);
1085 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1088 type_specializations
->remove_elt (&elt
);
1091 CLASSTYPE_TI_ARGS (inst
)
1092 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1095 = type_specializations
->find_slot (&elt
, INSERT
);
1096 entry
= ggc_alloc
<spec_entry
> ();
1101 /* But if we've had an implicit instantiation, that's a
1102 problem ([temp.expl.spec]/6). */
1103 error ("specialization %qT after instantiation %qT",
1107 /* Mark TYPE as a specialization. And as a result, we only
1108 have one level of template argument for the innermost
1110 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1111 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1112 CLASSTYPE_TI_ARGS (type
)
1113 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1116 else if (processing_specialization
)
1118 /* Someday C++0x may allow for enum template specialization. */
1119 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1120 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1121 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1122 "of %qD not allowed by ISO C++", type
);
1125 error ("explicit specialization of non-template %qT", type
);
1126 return error_mark_node
;
1133 /* Returns nonzero if we can optimize the retrieval of specializations
1134 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1135 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1138 optimize_specialization_lookup_p (tree tmpl
)
1140 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1141 && DECL_CLASS_SCOPE_P (tmpl
)
1142 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1144 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1145 /* The optimized lookup depends on the fact that the
1146 template arguments for the member function template apply
1147 purely to the containing class, which is not true if the
1148 containing class is an explicit or partial
1150 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1151 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1152 && !DECL_CONV_FN_P (tmpl
)
1153 /* It is possible to have a template that is not a member
1154 template and is not a member of a template class:
1156 template <typename T>
1157 struct S { friend A::f(); };
1159 Here, the friend function is a template, but the context does
1160 not have template information. The optimized lookup relies
1161 on having ARGS be the template arguments for both the class
1162 and the function template. */
1163 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1166 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1167 gone through coerce_template_parms by now. */
1170 verify_unstripped_args_1 (tree inner
)
1172 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1174 tree arg
= TREE_VEC_ELT (inner
, i
);
1175 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1177 else if (TYPE_P (arg
))
1178 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1179 else if (ARGUMENT_PACK_P (arg
))
1180 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1181 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1182 /* Allow typedefs on the type of a non-type argument, since a
1183 parameter can have them. */;
1185 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1190 verify_unstripped_args (tree args
)
1192 ++processing_template_decl
;
1193 if (!any_dependent_template_arguments_p (args
))
1194 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1195 --processing_template_decl
;
1198 /* Retrieve the specialization (in the sense of [temp.spec] - a
1199 specialization is either an instantiation or an explicit
1200 specialization) of TMPL for the given template ARGS. If there is
1201 no such specialization, return NULL_TREE. The ARGS are a vector of
1202 arguments, or a vector of vectors of arguments, in the case of
1203 templates with more than one level of parameters.
1205 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1206 then we search for a partial specialization matching ARGS. This
1207 parameter is ignored if TMPL is not a class template.
1209 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1210 result is a NONTYPE_ARGUMENT_PACK. */
1213 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1215 if (tmpl
== NULL_TREE
)
1218 if (args
== error_mark_node
)
1221 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1222 || TREE_CODE (tmpl
) == FIELD_DECL
);
1224 /* There should be as many levels of arguments as there are
1225 levels of parameters. */
1226 gcc_assert (TMPL_ARGS_DEPTH (args
)
1227 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1228 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1229 : template_class_depth (DECL_CONTEXT (tmpl
))));
1232 verify_unstripped_args (args
);
1234 /* Lambda functions in templates aren't instantiated normally, but through
1235 tsubst_lambda_expr. */
1236 if (lambda_fn_in_template_p (tmpl
))
1239 if (optimize_specialization_lookup_p (tmpl
))
1241 /* The template arguments actually apply to the containing
1242 class. Find the class specialization with those
1244 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1245 tree class_specialization
1246 = retrieve_specialization (class_template
, args
, 0);
1247 if (!class_specialization
)
1250 /* Find the instance of TMPL. */
1251 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1252 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1255 if (tree ti
= get_template_info (fn
))
1256 if (TI_TEMPLATE (ti
) == tmpl
1257 /* using-declarations can bring in a different
1258 instantiation of tmpl as a member of a different
1259 instantiation of tmpl's class. We don't want those
1261 && DECL_CONTEXT (fn
) == class_specialization
)
1270 hash_table
<spec_hasher
> *specializations
;
1274 elt
.spec
= NULL_TREE
;
1276 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1277 specializations
= type_specializations
;
1279 specializations
= decl_specializations
;
1282 hash
= spec_hasher::hash (&elt
);
1283 found
= specializations
->find_with_hash (&elt
, hash
);
1291 /* Like retrieve_specialization, but for local declarations. */
1294 retrieve_local_specialization (tree tmpl
)
1296 if (local_specializations
== NULL
)
1299 tree
*slot
= local_specializations
->get (tmpl
);
1300 return slot
? *slot
: NULL_TREE
;
1303 /* Returns nonzero iff DECL is a specialization of TMPL. */
1306 is_specialization_of (tree decl
, tree tmpl
)
1310 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1314 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1320 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1322 for (t
= TREE_TYPE (decl
);
1324 t
= CLASSTYPE_USE_TEMPLATE (t
)
1325 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1326 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1333 /* Returns nonzero iff DECL is a specialization of friend declaration
1334 FRIEND_DECL according to [temp.friend]. */
1337 is_specialization_of_friend (tree decl
, tree friend_decl
)
1339 bool need_template
= true;
1342 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1343 || TREE_CODE (decl
) == TYPE_DECL
);
1345 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1346 of a template class, we want to check if DECL is a specialization
1348 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1349 && DECL_TEMPLATE_INFO (friend_decl
)
1350 && !DECL_USE_TEMPLATE (friend_decl
))
1352 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1353 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1354 need_template
= false;
1356 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1357 && !PRIMARY_TEMPLATE_P (friend_decl
))
1358 need_template
= false;
1360 /* There is nothing to do if this is not a template friend. */
1361 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1364 if (is_specialization_of (decl
, friend_decl
))
1368 A member of a class template may be declared to be a friend of a
1369 non-template class. In this case, the corresponding member of
1370 every specialization of the class template is a friend of the
1371 class granting friendship.
1373 For example, given a template friend declaration
1375 template <class T> friend void A<T>::f();
1377 the member function below is considered a friend
1379 template <> struct A<int> {
1383 For this type of template friend, TEMPLATE_DEPTH below will be
1384 nonzero. To determine if DECL is a friend of FRIEND, we first
1385 check if the enclosing class is a specialization of another. */
1387 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1389 && DECL_CLASS_SCOPE_P (decl
)
1390 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1391 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1393 /* Next, we check the members themselves. In order to handle
1394 a few tricky cases, such as when FRIEND_DECL's are
1396 template <class T> friend void A<T>::g(T t);
1397 template <class T> template <T t> friend void A<T>::h();
1401 void A<int>::g(int);
1402 template <int> void A<int>::h();
1404 we need to figure out ARGS, the template arguments from
1405 the context of DECL. This is required for template substitution
1406 of `T' in the function parameter of `g' and template parameter
1407 of `h' in the above examples. Here ARGS corresponds to `int'. */
1409 tree context
= DECL_CONTEXT (decl
);
1410 tree args
= NULL_TREE
;
1411 int current_depth
= 0;
1413 while (current_depth
< template_depth
)
1415 if (CLASSTYPE_TEMPLATE_INFO (context
))
1417 if (current_depth
== 0)
1418 args
= TYPE_TI_ARGS (context
);
1420 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1423 context
= TYPE_CONTEXT (context
);
1426 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1431 tree friend_args_type
;
1432 tree decl_args_type
;
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1436 is_template
= DECL_TEMPLATE_INFO (decl
)
1437 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1438 if (need_template
^ is_template
)
1440 else if (is_template
)
1442 /* If both are templates, check template parameter list. */
1444 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1446 if (!comp_template_parms
1447 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1451 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1454 decl_type
= TREE_TYPE (decl
);
1456 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1457 tf_none
, NULL_TREE
);
1458 if (friend_type
== error_mark_node
)
1461 /* Check if return types match. */
1462 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1465 /* Check if function parameter types match, ignoring the
1466 `this' parameter. */
1467 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1468 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1470 friend_args_type
= TREE_CHAIN (friend_args_type
);
1471 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1472 decl_args_type
= TREE_CHAIN (decl_args_type
);
1474 return compparms (decl_args_type
, friend_args_type
);
1478 /* DECL is a TYPE_DECL */
1480 tree decl_type
= TREE_TYPE (decl
);
1482 /* Make sure that both DECL and FRIEND_DECL are templates or
1485 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1486 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1488 if (need_template
^ is_template
)
1490 else if (is_template
)
1493 /* If both are templates, check the name of the two
1494 TEMPLATE_DECL's first because is_friend didn't. */
1495 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1496 != DECL_NAME (friend_decl
))
1499 /* Now check template parameter list. */
1501 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1503 return comp_template_parms
1504 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1508 return (DECL_NAME (decl
)
1509 == DECL_NAME (friend_decl
));
1515 /* Register the specialization SPEC as a specialization of TMPL with
1516 the indicated ARGS. IS_FRIEND indicates whether the specialization
1517 is actually just a friend declaration. ATTRLIST is the list of
1518 attributes that the specialization is declared with or NULL when
1519 it isn't. Returns SPEC, or an equivalent prior declaration, if
1522 We also store instantiations of field packs in the hash table, even
1523 though they are not themselves templates, to make lookup easier. */
1526 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1530 spec_entry
**slot
= NULL
;
1533 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1534 || (TREE_CODE (tmpl
) == FIELD_DECL
1535 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1537 if (TREE_CODE (spec
) == FUNCTION_DECL
1538 && uses_template_parms (DECL_TI_ARGS (spec
)))
1539 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1540 register it; we want the corresponding TEMPLATE_DECL instead.
1541 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1542 the more obvious `uses_template_parms (spec)' to avoid problems
1543 with default function arguments. In particular, given
1544 something like this:
1546 template <class T> void f(T t1, T t = T())
1548 the default argument expression is not substituted for in an
1549 instantiation unless and until it is actually needed. */
1552 if (optimize_specialization_lookup_p (tmpl
))
1553 /* We don't put these specializations in the hash table, but we might
1554 want to give an error about a mismatch. */
1555 fn
= retrieve_specialization (tmpl
, args
, 0);
1563 hash
= spec_hasher::hash (&elt
);
1566 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1568 fn
= ((spec_entry
*) *slot
)->spec
;
1573 /* We can sometimes try to re-register a specialization that we've
1574 already got. In particular, regenerate_decl_from_template calls
1575 duplicate_decls which will update the specialization list. But,
1576 we'll still get called again here anyhow. It's more convenient
1577 to simply allow this than to try to prevent it. */
1580 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1582 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1584 if (DECL_ODR_USED (fn
)
1585 || DECL_EXPLICIT_INSTANTIATION (fn
))
1587 error ("specialization of %qD after instantiation",
1589 return error_mark_node
;
1594 /* This situation should occur only if the first
1595 specialization is an implicit instantiation, the
1596 second is an explicit specialization, and the
1597 implicit instantiation has not yet been used. That
1598 situation can occur if we have implicitly
1599 instantiated a member function and then specialized
1602 We can also wind up here if a friend declaration that
1603 looked like an instantiation turns out to be a
1606 template <class T> void foo(T);
1607 class S { friend void foo<>(int) };
1608 template <> void foo(int);
1610 We transform the existing DECL in place so that any
1611 pointers to it become pointers to the updated
1614 If there was a definition for the template, but not
1615 for the specialization, we want this to look as if
1616 there were no definition, and vice versa. */
1617 DECL_INITIAL (fn
) = NULL_TREE
;
1618 duplicate_decls (spec
, fn
, is_friend
);
1619 /* The call to duplicate_decls will have applied
1622 An explicit specialization of a function template
1623 is inline only if it is explicitly declared to be,
1624 and independently of whether its function template
1627 to the primary function; now copy the inline bits to
1628 the various clones. */
1629 FOR_EACH_CLONE (clone
, fn
)
1631 DECL_DECLARED_INLINE_P (clone
)
1632 = DECL_DECLARED_INLINE_P (fn
);
1633 DECL_SOURCE_LOCATION (clone
)
1634 = DECL_SOURCE_LOCATION (fn
);
1635 DECL_DELETED_FN (clone
)
1636 = DECL_DELETED_FN (fn
);
1638 check_specialization_namespace (tmpl
);
1643 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1645 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1646 if (dd
== error_mark_node
)
1647 /* We've already complained in duplicate_decls. */
1648 return error_mark_node
;
1650 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1651 /* Dup decl failed, but this is a new definition. Set the
1652 line number so any errors match this new
1654 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1660 return duplicate_decls (spec
, fn
, is_friend
);
1662 /* A specialization must be declared in the same namespace as the
1663 template it is specializing. */
1664 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1665 && !check_specialization_namespace (tmpl
))
1666 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1668 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1670 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1671 gcc_assert (tmpl
&& args
&& spec
);
1674 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1675 && PRIMARY_TEMPLATE_P (tmpl
)
1676 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1677 || variable_template_p (tmpl
))
1678 /* If TMPL is a forward declaration of a template function, keep a list
1679 of all specializations in case we need to reassign them to a friend
1680 template later in tsubst_friend_function.
1682 Also keep a list of all variable template instantiations so that
1683 process_partial_specialization can check whether a later partial
1684 specialization would have used it. */
1685 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1686 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1692 /* Returns true iff two spec_entry nodes are equivalent. */
1694 int comparing_specializations
;
1697 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1701 ++comparing_specializations
;
1702 equal
= (e1
->tmpl
== e2
->tmpl
1703 && comp_template_args (e1
->args
, e2
->args
));
1704 if (equal
&& flag_concepts
1705 /* tmpl could be a FIELD_DECL for a capture pack. */
1706 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1707 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1708 && uses_template_parms (e1
->args
))
1710 /* Partial specializations of a variable template can be distinguished by
1712 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1713 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1714 equal
= equivalent_constraints (c1
, c2
);
1716 --comparing_specializations
;
1721 /* Returns a hash for a template TMPL and template arguments ARGS. */
1724 hash_tmpl_and_args (tree tmpl
, tree args
)
1726 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1727 return iterative_hash_template_arg (args
, val
);
1730 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1734 spec_hasher::hash (spec_entry
*e
)
1736 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1739 /* Recursively calculate a hash value for a template argument ARG, for use
1740 in the hash tables of template specializations. */
1743 iterative_hash_template_arg (tree arg
, hashval_t val
)
1745 unsigned HOST_WIDE_INT i
;
1746 enum tree_code code
;
1749 if (arg
== NULL_TREE
)
1750 return iterative_hash_object (arg
, val
);
1755 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1758 code
= TREE_CODE (arg
);
1759 tclass
= TREE_CODE_CLASS (code
);
1761 val
= iterative_hash_object (code
, val
);
1768 case IDENTIFIER_NODE
:
1769 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1773 int i
, len
= TREE_VEC_LENGTH (arg
);
1774 for (i
= 0; i
< len
; ++i
)
1775 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1779 case TYPE_PACK_EXPANSION
:
1780 case EXPR_PACK_EXPANSION
:
1781 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1782 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1784 case TYPE_ARGUMENT_PACK
:
1785 case NONTYPE_ARGUMENT_PACK
:
1786 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1789 for (; arg
; arg
= TREE_CHAIN (arg
))
1790 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1794 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1795 val
= iterative_hash_template_arg (*iter
, val
);
1801 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1802 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1804 val
= iterative_hash_template_arg (field
, val
);
1805 val
= iterative_hash_template_arg (value
, val
);
1811 if (!DECL_ARTIFICIAL (arg
))
1813 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1814 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1816 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1819 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1822 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1823 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1825 case TEMPLATE_PARM_INDEX
:
1826 val
= iterative_hash_template_arg
1827 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1828 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1829 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1832 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1833 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1834 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1837 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1839 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1843 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1844 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1845 val
= iterative_hash_object (code
, val
);
1846 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1849 /* [temp.over.link] Two lambda-expressions are never considered
1852 So just hash the closure type. */
1853 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1856 case IMPLICIT_CONV_EXPR
:
1857 case STATIC_CAST_EXPR
:
1858 case REINTERPRET_CAST_EXPR
:
1859 case CONST_CAST_EXPR
:
1860 case DYNAMIC_CAST_EXPR
:
1862 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1863 /* Now hash operands as usual. */
1868 tree fn
= CALL_EXPR_FN (arg
);
1869 if (tree name
= dependent_name (fn
))
1871 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1872 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1875 val
= iterative_hash_template_arg (fn
, val
);
1876 call_expr_arg_iterator ai
;
1877 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1878 x
= next_call_expr_arg (&ai
))
1879 val
= iterative_hash_template_arg (x
, val
);
1890 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1892 // We want an alias specialization that survived strip_typedefs
1893 // to hash differently from its TYPE_CANONICAL, to avoid hash
1894 // collisions that compare as different in template_args_equal.
1895 // These could be dependent specializations that strip_typedefs
1896 // left alone, or untouched specializations because
1897 // coerce_template_parms returns the unconverted template
1898 // arguments if it sees incomplete argument packs.
1899 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1900 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1902 if (TYPE_CANONICAL (arg
))
1903 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1905 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1906 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1907 /* Otherwise just compare the types during lookup. */
1910 case tcc_declaration
:
1912 return iterative_hash_expr (arg
, val
);
1915 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1917 unsigned n
= cp_tree_operand_length (arg
);
1918 for (i
= 0; i
< n
; ++i
)
1919 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1927 /* Unregister the specialization SPEC as a specialization of TMPL.
1928 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1929 if the SPEC was listed as a specialization of TMPL.
1931 Note that SPEC has been ggc_freed, so we can't look inside it. */
1934 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1939 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1940 elt
.args
= TI_ARGS (tinfo
);
1941 elt
.spec
= NULL_TREE
;
1943 entry
= decl_specializations
->find (&elt
);
1946 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1947 gcc_assert (new_spec
!= NULL_TREE
);
1948 entry
->spec
= new_spec
;
1955 /* Like register_specialization, but for local declarations. We are
1956 registering SPEC, an instantiation of TMPL. */
1959 register_local_specialization (tree spec
, tree tmpl
)
1961 gcc_assert (tmpl
!= spec
);
1962 local_specializations
->put (tmpl
, spec
);
1965 /* TYPE is a class type. Returns true if TYPE is an explicitly
1966 specialized class. */
1969 explicit_class_specialization_p (tree type
)
1971 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1973 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1976 /* Print the list of functions at FNS, going through all the overloads
1977 for each element of the list. Alternatively, FNS cannot be a
1978 TREE_LIST, in which case it will be printed together with all the
1981 MORE and *STR should respectively be FALSE and NULL when the function
1982 is called from the outside. They are used internally on recursive
1983 calls. print_candidates manages the two parameters and leaves NULL
1984 in *STR when it ends. */
1987 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1989 if (TREE_CODE (fns
) == TREE_LIST
)
1990 for (; fns
; fns
= TREE_CHAIN (fns
))
1991 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1993 for (lkp_iterator
iter (fns
); iter
;)
1998 const char *pfx
= *str
;
2002 pfx
= _("candidates are:");
2004 pfx
= _("candidate is:");
2005 *str
= get_spaces (pfx
);
2007 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2011 /* Print the list of candidate FNS in an error message. FNS can also
2012 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2015 print_candidates (tree fns
)
2018 print_candidates_1 (fns
, &str
);
2022 /* Get a (possibly) constrained template declaration for the
2023 purpose of ordering candidates. */
2025 get_template_for_ordering (tree list
)
2027 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2028 tree f
= TREE_VALUE (list
);
2029 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2030 return TI_TEMPLATE (ti
);
2034 /* Among candidates having the same signature, return the
2035 most constrained or NULL_TREE if there is no best candidate.
2036 If the signatures of candidates vary (e.g., template
2037 specialization vs. member function), then there can be no
2040 Note that we don't compare constraints on the functions
2041 themselves, but rather those of their templates. */
2043 most_constrained_function (tree candidates
)
2045 // Try to find the best candidate in a first pass.
2046 tree champ
= candidates
;
2047 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2049 int winner
= more_constrained (get_template_for_ordering (champ
),
2050 get_template_for_ordering (c
));
2052 champ
= c
; // The candidate is more constrained
2053 else if (winner
== 0)
2054 return NULL_TREE
; // Neither is more constrained
2057 // Verify that the champ is better than previous candidates.
2058 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2059 if (!more_constrained (get_template_for_ordering (champ
),
2060 get_template_for_ordering (c
)))
2068 /* Returns the template (one of the functions given by TEMPLATE_ID)
2069 which can be specialized to match the indicated DECL with the
2070 explicit template args given in TEMPLATE_ID. The DECL may be
2071 NULL_TREE if none is available. In that case, the functions in
2072 TEMPLATE_ID are non-members.
2074 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2075 specialization of a member template.
2077 The TEMPLATE_COUNT is the number of references to qualifying
2078 template classes that appeared in the name of the function. See
2079 check_explicit_specialization for a more accurate description.
2081 TSK indicates what kind of template declaration (if any) is being
2082 declared. TSK_TEMPLATE indicates that the declaration given by
2083 DECL, though a FUNCTION_DECL, has template parameters, and is
2084 therefore a template function.
2086 The template args (those explicitly specified and those deduced)
2087 are output in a newly created vector *TARGS_OUT.
2089 If it is impossible to determine the result, an error message is
2090 issued. The error_mark_node is returned to indicate failure. */
2093 determine_specialization (tree template_id
,
2096 int need_member_template
,
2102 tree explicit_targs
;
2103 tree candidates
= NULL_TREE
;
2105 /* A TREE_LIST of templates of which DECL may be a specialization.
2106 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2107 corresponding TREE_PURPOSE is the set of template arguments that,
2108 when used to instantiate the template, would produce a function
2109 with the signature of DECL. */
2110 tree templates
= NULL_TREE
;
2112 cp_binding_level
*b
;
2114 *targs_out
= NULL_TREE
;
2116 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2117 return error_mark_node
;
2119 /* We shouldn't be specializing a member template of an
2120 unspecialized class template; we already gave an error in
2121 check_specialization_scope, now avoid crashing. */
2123 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2124 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2126 gcc_assert (errorcount
);
2127 return error_mark_node
;
2130 fns
= TREE_OPERAND (template_id
, 0);
2131 explicit_targs
= TREE_OPERAND (template_id
, 1);
2133 if (fns
== error_mark_node
)
2134 return error_mark_node
;
2136 /* Check for baselinks. */
2137 if (BASELINK_P (fns
))
2138 fns
= BASELINK_FUNCTIONS (fns
);
2140 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2142 error_at (DECL_SOURCE_LOCATION (decl
),
2143 "%qD is not a function template", fns
);
2144 return error_mark_node
;
2146 else if (VAR_P (decl
) && !variable_template_p (fns
))
2148 error ("%qD is not a variable template", fns
);
2149 return error_mark_node
;
2152 /* Count the number of template headers specified for this
2155 for (b
= current_binding_level
;
2156 b
->kind
== sk_template_parms
;
2160 tree orig_fns
= fns
;
2162 if (variable_template_p (fns
))
2164 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2165 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2166 tf_warning_or_error
,
2167 /*req_all*/true, /*use_defarg*/true);
2168 if (targs
!= error_mark_node
)
2169 templates
= tree_cons (targs
, fns
, templates
);
2171 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2175 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2177 tree decl_arg_types
;
2181 /* In case of explicit specialization, we need to check if
2182 the number of template headers appearing in the specialization
2183 is correct. This is usually done in check_explicit_specialization,
2184 but the check done there cannot be exhaustive when specializing
2185 member functions. Consider the following code:
2187 template <> void A<int>::f(int);
2188 template <> template <> void A<int>::f(int);
2190 Assuming that A<int> is not itself an explicit specialization
2191 already, the first line specializes "f" which is a non-template
2192 member function, whilst the second line specializes "f" which
2193 is a template member function. So both lines are syntactically
2194 correct, and check_explicit_specialization does not reject
2197 Here, we can do better, as we are matching the specialization
2198 against the declarations. We count the number of template
2199 headers, and we check if they match TEMPLATE_COUNT + 1
2200 (TEMPLATE_COUNT is the number of qualifying template classes,
2201 plus there must be another header for the member template
2204 Notice that if header_count is zero, this is not a
2205 specialization but rather a template instantiation, so there
2206 is no check we can perform here. */
2207 if (header_count
&& header_count
!= template_count
+ 1)
2210 /* Check that the number of template arguments at the
2211 innermost level for DECL is the same as for FN. */
2212 if (current_binding_level
->kind
== sk_template_parms
2213 && !current_binding_level
->explicit_spec_p
2214 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2215 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2216 (current_template_parms
))))
2219 /* DECL might be a specialization of FN. */
2220 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2221 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2223 /* For a non-static member function, we need to make sure
2224 that the const qualification is the same. Since
2225 get_bindings does not try to merge the "this" parameter,
2226 we must do the comparison explicitly. */
2227 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2229 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2230 TREE_VALUE (decl_arg_types
)))
2233 /* And the ref-qualification. */
2234 if (type_memfn_rqual (TREE_TYPE (decl
))
2235 != type_memfn_rqual (TREE_TYPE (fn
)))
2239 /* Skip the "this" parameter and, for constructors of
2240 classes with virtual bases, the VTT parameter. A
2241 full specialization of a constructor will have a VTT
2242 parameter, but a template never will. */
2244 = skip_artificial_parms_for (decl
, decl_arg_types
);
2246 = skip_artificial_parms_for (fn
, fn_arg_types
);
2248 /* Function templates cannot be specializations; there are
2249 no partial specializations of functions. Therefore, if
2250 the type of DECL does not match FN, there is no
2253 Note that it should never be the case that we have both
2254 candidates added here, and for regular member functions
2256 if (tsk
== tsk_template
)
2258 if (compparms (fn_arg_types
, decl_arg_types
))
2259 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2263 /* See whether this function might be a specialization of this
2264 template. Suppress access control because we might be trying
2265 to make this specialization a friend, and we have already done
2266 access control for the declaration of the specialization. */
2267 push_deferring_access_checks (dk_no_check
);
2268 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2269 pop_deferring_access_checks ();
2272 /* We cannot deduce template arguments that when used to
2273 specialize TMPL will produce DECL. */
2276 if (uses_template_parms (targs
))
2277 /* We deduced something involving 'auto', which isn't a valid
2278 template argument. */
2281 /* Remove, from the set of candidates, all those functions
2282 whose constraints are not satisfied. */
2283 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2286 // Then, try to form the new function type.
2287 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2288 if (insttype
== error_mark_node
)
2291 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2292 if (!compparms (fn_arg_types
, decl_arg_types
))
2295 /* Save this template, and the arguments deduced. */
2296 templates
= tree_cons (targs
, fn
, templates
);
2298 else if (need_member_template
)
2299 /* FN is an ordinary member function, and we need a
2300 specialization of a member template. */
2302 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2303 /* We can get IDENTIFIER_NODEs here in certain erroneous
2306 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2307 /* This is just an ordinary non-member function. Nothing can
2308 be a specialization of that. */
2310 else if (DECL_ARTIFICIAL (fn
))
2311 /* Cannot specialize functions that are created implicitly. */
2315 tree decl_arg_types
;
2317 /* This is an ordinary member function. However, since
2318 we're here, we can assume its enclosing class is a
2319 template class. For example,
2321 template <typename T> struct S { void f(); };
2322 template <> void S<int>::f() {}
2324 Here, S<int>::f is a non-template, but S<int> is a
2325 template class. If FN has the same type as DECL, we
2326 might be in business. */
2328 if (!DECL_TEMPLATE_INFO (fn
))
2329 /* Its enclosing class is an explicit specialization
2330 of a template class. This is not a candidate. */
2333 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2334 TREE_TYPE (TREE_TYPE (fn
))))
2335 /* The return types differ. */
2338 /* Adjust the type of DECL in case FN is a static member. */
2339 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2340 if (DECL_STATIC_FUNCTION_P (fn
)
2341 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2342 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2344 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2348 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2349 && (type_memfn_rqual (TREE_TYPE (decl
))
2350 != type_memfn_rqual (TREE_TYPE (fn
))))
2353 // If the deduced arguments do not satisfy the constraints,
2354 // this is not a candidate.
2355 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2358 // Add the candidate.
2359 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2363 if (templates
&& TREE_CHAIN (templates
))
2369 It is possible for a specialization with a given function
2370 signature to be instantiated from more than one function
2371 template. In such cases, explicit specification of the
2372 template arguments must be used to uniquely identify the
2373 function template specialization being specialized.
2375 Note that here, there's no suggestion that we're supposed to
2376 determine which of the candidate templates is most
2377 specialized. However, we, also have:
2381 Partial ordering of overloaded function template
2382 declarations is used in the following contexts to select
2383 the function template to which a function template
2384 specialization refers:
2386 -- when an explicit specialization refers to a function
2389 So, we do use the partial ordering rules, at least for now.
2390 This extension can only serve to make invalid programs valid,
2391 so it's safe. And, there is strong anecdotal evidence that
2392 the committee intended the partial ordering rules to apply;
2393 the EDG front end has that behavior, and John Spicer claims
2394 that the committee simply forgot to delete the wording in
2395 [temp.expl.spec]. */
2396 tree tmpl
= most_specialized_instantiation (templates
);
2397 if (tmpl
!= error_mark_node
)
2400 TREE_CHAIN (templates
) = NULL_TREE
;
2404 // Concepts allows multiple declarations of member functions
2405 // with the same signature. Like above, we need to rely on
2406 // on the partial ordering of those candidates to determine which
2408 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2410 if (tree cand
= most_constrained_function (candidates
))
2413 TREE_CHAIN (cand
) = NULL_TREE
;
2417 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2419 error ("template-id %qD for %q+D does not match any template "
2420 "declaration", template_id
, decl
);
2421 if (header_count
&& header_count
!= template_count
+ 1)
2422 inform (DECL_SOURCE_LOCATION (decl
),
2423 "saw %d %<template<>%>, need %d for "
2424 "specializing a member function template",
2425 header_count
, template_count
+ 1);
2427 print_candidates (orig_fns
);
2428 return error_mark_node
;
2430 else if ((templates
&& TREE_CHAIN (templates
))
2431 || (candidates
&& TREE_CHAIN (candidates
))
2432 || (templates
&& candidates
))
2434 error ("ambiguous template specialization %qD for %q+D",
2436 candidates
= chainon (candidates
, templates
);
2437 print_candidates (candidates
);
2438 return error_mark_node
;
2441 /* We have one, and exactly one, match. */
2444 tree fn
= TREE_VALUE (candidates
);
2445 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2447 /* Propagate the candidate's constraints to the declaration. */
2448 set_constraints (decl
, get_constraints (fn
));
2450 /* DECL is a re-declaration or partial instantiation of a template
2452 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2454 /* It was a specialization of an ordinary member function in a
2456 return DECL_TI_TEMPLATE (fn
);
2459 /* It was a specialization of a template. */
2460 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2461 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2463 *targs_out
= copy_node (targs
);
2464 SET_TMPL_ARGS_LEVEL (*targs_out
,
2465 TMPL_ARGS_DEPTH (*targs_out
),
2466 TREE_PURPOSE (templates
));
2469 *targs_out
= TREE_PURPOSE (templates
);
2470 return TREE_VALUE (templates
);
2473 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2474 but with the default argument values filled in from those in the
2478 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2481 tree new_spec_types
;
2486 if (spec_types
== void_list_node
)
2487 return void_list_node
;
2489 /* Substitute into the rest of the list. */
2491 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2492 TREE_CHAIN (tmpl_types
));
2494 /* Add the default argument for this parameter. */
2495 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2496 TREE_VALUE (spec_types
),
2500 /* DECL is an explicit specialization. Replicate default arguments
2501 from the template it specializes. (That way, code like:
2503 template <class T> void f(T = 3);
2504 template <> void f(double);
2507 works, as required.) An alternative approach would be to look up
2508 the correct default arguments at the call-site, but this approach
2509 is consistent with how implicit instantiations are handled. */
2512 copy_default_args_to_explicit_spec (tree decl
)
2517 tree new_spec_types
;
2521 tree object_type
= NULL_TREE
;
2522 tree in_charge
= NULL_TREE
;
2523 tree vtt
= NULL_TREE
;
2525 /* See if there's anything we need to do. */
2526 tmpl
= DECL_TI_TEMPLATE (decl
);
2527 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2528 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2529 if (TREE_PURPOSE (t
))
2534 old_type
= TREE_TYPE (decl
);
2535 spec_types
= TYPE_ARG_TYPES (old_type
);
2537 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2539 /* Remove the this pointer, but remember the object's type for
2541 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2542 spec_types
= TREE_CHAIN (spec_types
);
2543 tmpl_types
= TREE_CHAIN (tmpl_types
);
2545 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2547 /* DECL may contain more parameters than TMPL due to the extra
2548 in-charge parameter in constructors and destructors. */
2549 in_charge
= spec_types
;
2550 spec_types
= TREE_CHAIN (spec_types
);
2552 if (DECL_HAS_VTT_PARM_P (decl
))
2555 spec_types
= TREE_CHAIN (spec_types
);
2559 /* Compute the merged default arguments. */
2561 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2563 /* Compute the new FUNCTION_TYPE. */
2567 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2572 /* Put the in-charge parameter back. */
2573 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2574 TREE_VALUE (in_charge
),
2577 new_type
= build_method_type_directly (object_type
,
2578 TREE_TYPE (old_type
),
2582 new_type
= build_function_type (TREE_TYPE (old_type
),
2584 new_type
= cp_build_type_attribute_variant (new_type
,
2585 TYPE_ATTRIBUTES (old_type
));
2586 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2588 TREE_TYPE (decl
) = new_type
;
2591 /* Return the number of template headers we expect to see for a definition
2592 or specialization of CTYPE or one of its non-template members. */
2595 num_template_headers_for_class (tree ctype
)
2597 int num_templates
= 0;
2599 while (ctype
&& CLASS_TYPE_P (ctype
))
2601 /* You're supposed to have one `template <...>' for every
2602 template class, but you don't need one for a full
2603 specialization. For example:
2605 template <class T> struct S{};
2606 template <> struct S<int> { void f(); };
2607 void S<int>::f () {}
2609 is correct; there shouldn't be a `template <>' for the
2610 definition of `S<int>::f'. */
2611 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2612 /* If CTYPE does not have template information of any
2613 kind, then it is not a template, nor is it nested
2614 within a template. */
2616 if (explicit_class_specialization_p (ctype
))
2618 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2621 ctype
= TYPE_CONTEXT (ctype
);
2624 return num_templates
;
2627 /* Do a simple sanity check on the template headers that precede the
2628 variable declaration DECL. */
2631 check_template_variable (tree decl
)
2633 tree ctx
= CP_DECL_CONTEXT (decl
);
2634 int wanted
= num_template_headers_for_class (ctx
);
2635 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2636 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2638 if (cxx_dialect
< cxx14
)
2639 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2640 "variable templates only available with "
2641 "%<-std=c++14%> or %<-std=gnu++14%>");
2643 // Namespace-scope variable templates should have a template header.
2646 if (template_header_count
> wanted
)
2648 auto_diagnostic_group d
;
2649 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2650 "too many template headers for %qD "
2653 if (warned
&& CLASS_TYPE_P (ctx
)
2654 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2655 inform (DECL_SOURCE_LOCATION (decl
),
2656 "members of an explicitly specialized class are defined "
2657 "without a template header");
2661 /* An explicit specialization whose declarator-id or class-head-name is not
2662 qualified shall be declared in the nearest enclosing namespace of the
2663 template, or, if the namespace is inline (7.3.1), any namespace from its
2664 enclosing namespace set.
2666 If the name declared in the explicit instantiation is an unqualified name,
2667 the explicit instantiation shall appear in the namespace where its template
2668 is declared or, if that namespace is inline (7.3.1), any namespace from its
2669 enclosing namespace set. */
2672 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2674 tree tmpl
= most_general_template (t
);
2675 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2676 && !is_nested_namespace (current_namespace
,
2677 CP_DECL_CONTEXT (tmpl
), true))
2679 if (processing_specialization
)
2680 permerror (loc
, "explicit specialization of %qD outside its "
2681 "namespace must use a nested-name-specifier", tmpl
);
2682 else if (processing_explicit_instantiation
2683 && cxx_dialect
>= cxx11
)
2684 /* This was allowed in C++98, so only pedwarn. */
2685 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2686 "outside its namespace must use a nested-name-"
2691 /* Warn for a template specialization SPEC that is missing some of a set
2692 of function or type attributes that the template TEMPL is declared with.
2693 ATTRLIST is a list of additional attributes that SPEC should be taken
2694 to ultimately be declared with. */
2697 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2699 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2700 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2702 /* Avoid warning if the difference between the primary and
2703 the specialization is not in one of the attributes below. */
2704 const char* const blacklist
[] = {
2705 "alloc_align", "alloc_size", "assume_aligned", "format",
2706 "format_arg", "malloc", "nonnull", NULL
2709 /* Put together a list of the black listed attributes that the primary
2710 template is declared with that the specialization is not, in case
2711 it's not apparent from the most recent declaration of the primary. */
2713 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2719 auto_diagnostic_group d
;
2720 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2721 "explicit specialization %q#D may be missing attributes",
2723 inform (DECL_SOURCE_LOCATION (tmpl
),
2725 ? G_("missing primary template attributes %s")
2726 : G_("missing primary template attribute %s"),
2727 pp_formatted_text (&str
));
2730 /* Check to see if the function just declared, as indicated in
2731 DECLARATOR, and in DECL, is a specialization of a function
2732 template. We may also discover that the declaration is an explicit
2733 instantiation at this point.
2735 Returns DECL, or an equivalent declaration that should be used
2736 instead if all goes well. Issues an error message if something is
2737 amiss. Returns error_mark_node if the error is not easily
2740 FLAGS is a bitmask consisting of the following flags:
2742 2: The function has a definition.
2743 4: The function is a friend.
2745 The TEMPLATE_COUNT is the number of references to qualifying
2746 template classes that appeared in the name of the function. For
2749 template <class T> struct S { void f(); };
2752 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2753 classes are not counted in the TEMPLATE_COUNT, so that in
2755 template <class T> struct S {};
2756 template <> struct S<int> { void f(); }
2757 template <> void S<int>::f();
2759 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2760 invalid; there should be no template <>.)
2762 If the function is a specialization, it is marked as such via
2763 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2764 is set up correctly, and it is added to the list of specializations
2765 for that template. */
2768 check_explicit_specialization (tree declarator
,
2774 int have_def
= flags
& 2;
2775 int is_friend
= flags
& 4;
2776 bool is_concept
= flags
& 8;
2777 int specialization
= 0;
2778 int explicit_instantiation
= 0;
2779 int member_specialization
= 0;
2780 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2781 tree dname
= DECL_NAME (decl
);
2786 if (!processing_specialization
)
2789 tsk
= tsk_excessive_parms
;
2792 tsk
= current_tmpl_spec_kind (template_count
);
2797 if (processing_specialization
&& !VAR_P (decl
))
2800 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2802 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2805 /* This could be something like:
2807 template <class T> void f(T);
2808 class S { friend void f<>(int); } */
2812 /* This case handles bogus declarations like template <>
2813 template <class T> void f<int>(); */
2815 error_at (cp_expr_loc_or_input_loc (declarator
),
2816 "template-id %qE in declaration of primary template",
2823 case tsk_invalid_member_spec
:
2824 /* The error has already been reported in
2825 check_specialization_scope. */
2826 return error_mark_node
;
2828 case tsk_invalid_expl_inst
:
2829 error ("template parameter list used in explicit instantiation");
2835 error ("definition provided for explicit instantiation");
2837 explicit_instantiation
= 1;
2840 case tsk_excessive_parms
:
2841 case tsk_insufficient_parms
:
2842 if (tsk
== tsk_excessive_parms
)
2843 error ("too many template parameter lists in declaration of %qD",
2845 else if (template_header_count
)
2846 error("too few template parameter lists in declaration of %qD", decl
);
2848 error("explicit specialization of %qD must be introduced by "
2849 "%<template <>%>", decl
);
2854 error ("explicit specialization declared %<concept%>");
2856 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2857 /* In cases like template<> constexpr bool v = true;
2858 We'll give an error in check_template_variable. */
2861 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2863 member_specialization
= 1;
2869 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2871 /* This case handles bogus declarations like template <>
2872 template <class T> void f<int>(); */
2874 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2875 error_at (cp_expr_loc_or_input_loc (declarator
),
2876 "template-id %qE in declaration of primary template",
2878 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2880 /* Partial specialization of variable template. */
2881 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2885 else if (cxx_dialect
< cxx14
)
2886 error_at (cp_expr_loc_or_input_loc (declarator
),
2887 "non-type partial specialization %qE "
2888 "is not allowed", declarator
);
2890 error_at (cp_expr_loc_or_input_loc (declarator
),
2891 "non-class, non-variable partial specialization %qE "
2892 "is not allowed", declarator
);
2897 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2898 /* This is a specialization of a member template, without
2899 specialization the containing class. Something like:
2901 template <class T> struct S {
2902 template <class U> void f (U);
2904 template <> template <class U> void S<int>::f(U) {}
2906 That's a specialization -- but of the entire template. */
2914 if ((specialization
|| member_specialization
)
2915 /* This doesn't apply to variable templates. */
2916 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2918 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2919 for (; t
; t
= TREE_CHAIN (t
))
2920 if (TREE_PURPOSE (t
))
2922 permerror (input_location
,
2923 "default argument specified in explicit specialization");
2928 if (specialization
|| member_specialization
|| explicit_instantiation
)
2930 tree tmpl
= NULL_TREE
;
2931 tree targs
= NULL_TREE
;
2932 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2934 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2935 if (!was_template_id
)
2939 gcc_assert (identifier_p (declarator
));
2944 /* If there is no class context, the explicit instantiation
2945 must be at namespace scope. */
2946 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2948 /* Find the namespace binding, using the declaration
2950 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2952 if (fns
== error_mark_node
)
2953 /* If lookup fails, look for a friend declaration so we can
2954 give a better diagnostic. */
2955 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2956 /*type*/false, /*complain*/true,
2959 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2961 error ("%qD is not a template function", dname
);
2962 fns
= error_mark_node
;
2966 declarator
= lookup_template_function (fns
, NULL_TREE
);
2969 if (declarator
== error_mark_node
)
2970 return error_mark_node
;
2972 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2974 if (!explicit_instantiation
)
2975 /* A specialization in class scope. This is invalid,
2976 but the error will already have been flagged by
2977 check_specialization_scope. */
2978 return error_mark_node
;
2981 /* It's not valid to write an explicit instantiation in
2984 class C { template void f(); }
2986 This case is caught by the parser. However, on
2989 template class C { void f(); };
2991 (which is invalid) we can get here. The error will be
2998 else if (ctype
!= NULL_TREE
2999 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3001 // We'll match variable templates in start_decl.
3005 /* Find the list of functions in ctype that have the same
3006 name as the declared function. */
3007 tree name
= TREE_OPERAND (declarator
, 0);
3009 if (constructor_name_p (name
, ctype
))
3011 if (DECL_CONSTRUCTOR_P (decl
)
3012 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3013 : !CLASSTYPE_DESTRUCTOR (ctype
))
3015 /* From [temp.expl.spec]:
3017 If such an explicit specialization for the member
3018 of a class template names an implicitly-declared
3019 special member function (clause _special_), the
3020 program is ill-formed.
3022 Similar language is found in [temp.explicit]. */
3023 error ("specialization of implicitly-declared special member function");
3024 return error_mark_node
;
3027 name
= DECL_NAME (decl
);
3030 /* For a type-conversion operator, We might be looking for
3031 `operator int' which will be a specialization of
3032 `operator T'. Grab all the conversion operators, and
3033 then select from them. */
3034 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3035 ? conv_op_identifier
: name
);
3037 if (fns
== NULL_TREE
)
3039 error ("no member function %qD declared in %qT", name
, ctype
);
3040 return error_mark_node
;
3043 TREE_OPERAND (declarator
, 0) = fns
;
3046 /* Figure out what exactly is being specialized at this point.
3047 Note that for an explicit instantiation, even one for a
3048 member function, we cannot tell a priori whether the
3049 instantiation is for a member template, or just a member
3050 function of a template class. Even if a member template is
3051 being instantiated, the member template arguments may be
3052 elided if they can be deduced from the rest of the
3054 tmpl
= determine_specialization (declarator
, decl
,
3056 member_specialization
,
3060 if (!tmpl
|| tmpl
== error_mark_node
)
3061 /* We couldn't figure out what this declaration was
3063 return error_mark_node
;
3066 if (TREE_CODE (decl
) == FUNCTION_DECL
3067 && DECL_HIDDEN_FRIEND_P (tmpl
))
3069 auto_diagnostic_group d
;
3070 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3071 "friend declaration %qD is not visible to "
3072 "explicit specialization", tmpl
))
3073 inform (DECL_SOURCE_LOCATION (tmpl
),
3074 "friend declaration here");
3076 else if (!ctype
&& !is_friend
3077 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3078 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3080 tree gen_tmpl
= most_general_template (tmpl
);
3082 if (explicit_instantiation
)
3084 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3085 is done by do_decl_instantiation later. */
3087 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3088 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3090 if (arg_depth
> parm_depth
)
3092 /* If TMPL is not the most general template (for
3093 example, if TMPL is a friend template that is
3094 injected into namespace scope), then there will
3095 be too many levels of TARGS. Remove some of them
3100 new_targs
= make_tree_vec (parm_depth
);
3101 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3102 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3103 = TREE_VEC_ELT (targs
, i
);
3107 return instantiate_template (tmpl
, targs
, tf_error
);
3110 /* If we thought that the DECL was a member function, but it
3111 turns out to be specializing a static member function,
3112 make DECL a static member function as well. */
3113 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3114 && DECL_STATIC_FUNCTION_P (tmpl
)
3115 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3116 revert_static_member_fn (decl
);
3118 /* If this is a specialization of a member template of a
3119 template class, we want to return the TEMPLATE_DECL, not
3120 the specialization of it. */
3121 if (tsk
== tsk_template
&& !was_template_id
)
3123 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3124 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3125 DECL_INITIAL (result
) = NULL_TREE
;
3129 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3130 DECL_SOURCE_LOCATION (result
)
3131 = DECL_SOURCE_LOCATION (decl
);
3132 /* We want to use the argument list specified in the
3133 definition, not in the original declaration. */
3134 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3135 for (parm
= DECL_ARGUMENTS (result
); parm
;
3136 parm
= DECL_CHAIN (parm
))
3137 DECL_CONTEXT (parm
) = result
;
3139 return register_specialization (tmpl
, gen_tmpl
, targs
,
3143 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3144 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3146 if (was_template_id
)
3147 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3149 /* Inherit default function arguments from the template
3150 DECL is specializing. */
3151 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3152 copy_default_args_to_explicit_spec (decl
);
3154 /* This specialization has the same protection as the
3155 template it specializes. */
3156 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3157 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3159 /* 7.1.1-1 [dcl.stc]
3161 A storage-class-specifier shall not be specified in an
3162 explicit specialization...
3164 The parser rejects these, so unless action is taken here,
3165 explicit function specializations will always appear with
3168 The action recommended by the C++ CWG in response to C++
3169 defect report 605 is to make the storage class and linkage
3170 of the explicit specialization match the templated function:
3172 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3174 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3176 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3177 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3179 /* A concept cannot be specialized. */
3180 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3182 error ("explicit specialization of function concept %qD",
3184 return error_mark_node
;
3187 /* This specialization has the same linkage and visibility as
3188 the function template it specializes. */
3189 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3190 if (! TREE_PUBLIC (decl
))
3192 DECL_INTERFACE_KNOWN (decl
) = 1;
3193 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3195 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3196 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3198 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3199 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3203 /* If DECL is a friend declaration, declared using an
3204 unqualified name, the namespace associated with DECL may
3205 have been set incorrectly. For example, in:
3207 template <typename T> void f(T);
3209 struct S { friend void f<int>(int); }
3212 we will have set the DECL_CONTEXT for the friend
3213 declaration to N, rather than to the global namespace. */
3214 if (DECL_NAMESPACE_SCOPE_P (decl
))
3215 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3217 if (is_friend
&& !have_def
)
3218 /* This is not really a declaration of a specialization.
3219 It's just the name of an instantiation. But, it's not
3220 a request for an instantiation, either. */
3221 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3222 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3223 /* A specialization is not necessarily COMDAT. */
3224 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3225 && DECL_DECLARED_INLINE_P (decl
));
3226 else if (VAR_P (decl
))
3227 DECL_COMDAT (decl
) = false;
3229 /* If this is a full specialization, register it so that we can find
3230 it again. Partial specializations will be registered in
3231 process_partial_specialization. */
3232 if (!processing_template_decl
)
3234 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3236 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3241 /* A 'structor should already have clones. */
3242 gcc_assert (decl
== error_mark_node
3243 || variable_template_p (tmpl
)
3244 || !(DECL_CONSTRUCTOR_P (decl
)
3245 || DECL_DESTRUCTOR_P (decl
))
3246 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3253 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3254 parameters. These are represented in the same format used for
3255 DECL_TEMPLATE_PARMS. */
3258 comp_template_parms (const_tree parms1
, const_tree parms2
)
3263 if (parms1
== parms2
)
3266 for (p1
= parms1
, p2
= parms2
;
3267 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3268 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3270 tree t1
= TREE_VALUE (p1
);
3271 tree t2
= TREE_VALUE (p2
);
3274 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3275 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3277 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3280 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3282 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3283 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3285 /* If either of the template parameters are invalid, assume
3286 they match for the sake of error recovery. */
3287 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3290 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3293 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3294 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3295 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3297 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3302 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3303 /* One set of parameters has more parameters lists than the
3310 /* Returns true if two template parameters are declared with
3311 equivalent constraints. */
3314 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3316 tree req1
= TREE_TYPE (parm1
);
3317 tree req2
= TREE_TYPE (parm2
);
3321 return cp_tree_equal (req1
, req2
);
3325 /* Returns true when two template parameters are equivalent. */
3328 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3330 tree decl1
= TREE_VALUE (parm1
);
3331 tree decl2
= TREE_VALUE (parm2
);
3333 /* If either of the template parameters are invalid, assume
3334 they match for the sake of error recovery. */
3335 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3338 /* ... they declare parameters of the same kind. */
3339 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3342 /* ... one parameter was introduced by a parameter declaration, then
3343 both are. This case arises as a result of eagerly rewriting declarations
3345 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3348 /* ... if either declares a pack, they both do. */
3349 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3352 if (TREE_CODE (decl1
) == PARM_DECL
)
3354 /* ... if they declare non-type parameters, the types are equivalent. */
3355 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3358 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3360 /* ... if they declare template template parameters, their template
3361 parameter lists are equivalent. */
3362 if (!template_heads_equivalent_p (decl1
, decl2
))
3366 /* ... if they are declared with a qualified-concept name, they both
3367 are, and those names are equivalent. */
3368 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3371 /* Returns true if two template parameters lists are equivalent.
3372 Two template parameter lists are equivalent if they have the
3373 same length and their corresponding parameters are equivalent.
3375 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3376 data structure returned by DECL_TEMPLATE_PARMS.
3378 This is generally the same implementation as comp_template_parms
3379 except that it also the concept names and arguments used to
3380 introduce parameters. */
3383 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3385 if (parms1
== parms2
)
3388 const_tree p1
= parms1
;
3389 const_tree p2
= parms2
;
3390 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3392 tree list1
= TREE_VALUE (p1
);
3393 tree list2
= TREE_VALUE (p2
);
3395 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3398 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3400 tree parm1
= TREE_VEC_ELT (list1
, i
);
3401 tree parm2
= TREE_VEC_ELT (list2
, i
);
3402 if (!template_parameters_equivalent_p (parm1
, parm2
))
3406 p1
= TREE_CHAIN (p1
);
3407 p2
= TREE_CHAIN (p2
);
3410 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3416 /* Return true if the requires-clause of the template parameter lists are
3417 equivalent and false otherwise. */
3419 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3421 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3422 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3423 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3425 if (!cp_tree_equal (req1
, req2
))
3430 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3431 Two template heads are equivalent if their template parameter
3432 lists are equivalent and their requires clauses are equivalent.
3434 In pre-C++20, this is equivalent to calling comp_template_parms
3435 for the template parameters of TMPL1 and TMPL2. */
3438 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3440 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3441 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3443 /* Don't change the matching rules for pre-C++20. */
3444 if (cxx_dialect
< cxx2a
)
3445 return comp_template_parms (parms1
, parms2
);
3447 /* ... have the same number of template parameters, and their
3448 corresponding parameters are equivalent. */
3449 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3452 /* ... if either has a requires-clause, they both do and their
3453 corresponding constraint-expressions are equivalent. */
3454 return template_requirements_equivalent_p (parms1
, parms2
);
3457 /* Determine whether PARM is a parameter pack. */
3460 template_parameter_pack_p (const_tree parm
)
3462 /* Determine if we have a non-type template parameter pack. */
3463 if (TREE_CODE (parm
) == PARM_DECL
)
3464 return (DECL_TEMPLATE_PARM_P (parm
)
3465 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3466 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3467 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3469 /* If this is a list of template parameters, we could get a
3470 TYPE_DECL or a TEMPLATE_DECL. */
3471 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3472 parm
= TREE_TYPE (parm
);
3474 /* Otherwise it must be a type template parameter. */
3475 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3476 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3477 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3480 /* Determine if T is a function parameter pack. */
3483 function_parameter_pack_p (const_tree t
)
3485 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3486 return DECL_PACK_P (t
);
3490 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3491 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3494 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3496 if (! primary_func_tmpl_inst
3497 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3498 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3501 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3504 /* Return true iff the function parameter PARAM_DECL was expanded
3505 from the function parameter pack PACK. */
3508 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3510 if (DECL_ARTIFICIAL (param_decl
)
3511 || !function_parameter_pack_p (pack
))
3514 /* The parameter pack and its pack arguments have the same
3516 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3519 /* Determine whether ARGS describes a variadic template args list,
3520 i.e., one that is terminated by a template argument pack. */
3523 template_args_variadic_p (tree args
)
3528 if (args
== NULL_TREE
)
3531 args
= INNERMOST_TEMPLATE_ARGS (args
);
3532 nargs
= TREE_VEC_LENGTH (args
);
3537 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3539 return ARGUMENT_PACK_P (last_parm
);
3542 /* Generate a new name for the parameter pack name NAME (an
3543 IDENTIFIER_NODE) that incorporates its */
3546 make_ith_pack_parameter_name (tree name
, int i
)
3548 /* Munge the name to include the parameter index. */
3549 #define NUMBUF_LEN 128
3550 char numbuf
[NUMBUF_LEN
];
3554 if (name
== NULL_TREE
)
3556 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3557 newname_len
= IDENTIFIER_LENGTH (name
)
3558 + strlen (numbuf
) + 2;
3559 newname
= (char*)alloca (newname_len
);
3560 snprintf (newname
, newname_len
,
3561 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3562 return get_identifier (newname
);
3565 /* Return true if T is a primary function, class or alias template
3566 specialization, not including the template pattern. */
3569 primary_template_specialization_p (const_tree t
)
3574 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3575 return (DECL_LANG_SPECIFIC (t
)
3576 && DECL_USE_TEMPLATE (t
)
3577 && DECL_TEMPLATE_INFO (t
)
3578 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3579 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3580 return (CLASSTYPE_TEMPLATE_INFO (t
)
3581 && CLASSTYPE_USE_TEMPLATE (t
)
3582 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3583 else if (alias_template_specialization_p (t
, nt_transparent
))
3588 /* Return true if PARM is a template template parameter. */
3591 template_template_parameter_p (const_tree parm
)
3593 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3596 /* Return true iff PARM is a DECL representing a type template
3600 template_type_parameter_p (const_tree parm
)
3603 && (TREE_CODE (parm
) == TYPE_DECL
3604 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3605 && DECL_TEMPLATE_PARM_P (parm
));
3608 /* Return the template parameters of T if T is a
3609 primary template instantiation, NULL otherwise. */
3612 get_primary_template_innermost_parameters (const_tree t
)
3614 tree parms
= NULL
, template_info
= NULL
;
3616 if ((template_info
= get_template_info (t
))
3617 && primary_template_specialization_p (t
))
3618 parms
= INNERMOST_TEMPLATE_PARMS
3619 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3624 /* Return the template parameters of the LEVELth level from the full list
3625 of template parameters PARMS. */
3628 get_template_parms_at_level (tree parms
, int level
)
3632 || TREE_CODE (parms
) != TREE_LIST
3633 || level
> TMPL_PARMS_DEPTH (parms
))
3636 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3637 if (TMPL_PARMS_DEPTH (p
) == level
)
3643 /* Returns the template arguments of T if T is a template instantiation,
3647 get_template_innermost_arguments (const_tree t
)
3649 tree args
= NULL
, template_info
= NULL
;
3651 if ((template_info
= get_template_info (t
))
3652 && TI_ARGS (template_info
))
3653 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3658 /* Return the argument pack elements of T if T is a template argument pack,
3662 get_template_argument_pack_elems (const_tree t
)
3664 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3665 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3668 return ARGUMENT_PACK_ARGS (t
);
3671 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3672 ARGUMENT_PACK_SELECT represents. */
3675 argument_pack_select_arg (tree t
)
3677 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3678 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3680 /* If the selected argument is an expansion E, that most likely means we were
3681 called from gen_elem_of_pack_expansion_instantiation during the
3682 substituting of an argument pack (of which the Ith element is a pack
3683 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3684 In this case, the Ith element resulting from this substituting is going to
3685 be a pack expansion, which pattern is the pattern of E. Let's return the
3686 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3687 resulting pack expansion from it. */
3688 if (PACK_EXPANSION_P (arg
))
3690 /* Make sure we aren't throwing away arg info. */
3691 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3692 arg
= PACK_EXPANSION_PATTERN (arg
);
3699 /* True iff FN is a function representing a built-in variadic parameter
3703 builtin_pack_fn_p (tree fn
)
3706 || TREE_CODE (fn
) != FUNCTION_DECL
3707 || !DECL_IS_BUILTIN (fn
))
3710 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3716 /* True iff CALL is a call to a function representing a built-in variadic
3720 builtin_pack_call_p (tree call
)
3722 if (TREE_CODE (call
) != CALL_EXPR
)
3724 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3727 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3730 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3733 tree ohi
= CALL_EXPR_ARG (call
, 0);
3734 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3735 false/*fn*/, true/*int_cst*/);
3737 if (value_dependent_expression_p (hi
))
3741 call
= copy_node (call
);
3742 CALL_EXPR_ARG (call
, 0) = hi
;
3744 tree ex
= make_pack_expansion (call
, complain
);
3745 tree vec
= make_tree_vec (1);
3746 TREE_VEC_ELT (vec
, 0) = ex
;
3751 hi
= cxx_constant_value (hi
);
3752 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3754 /* Calculate the largest value of len that won't make the size of the vec
3755 overflow an int. The compiler will exceed resource limits long before
3756 this, but it seems a decent place to diagnose. */
3757 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3759 if (len
< 0 || len
> max
)
3761 if ((complain
& tf_error
)
3762 && hi
!= error_mark_node
)
3763 error ("argument to %<__integer_pack%> must be between 0 and %d",
3765 return error_mark_node
;
3768 tree vec
= make_tree_vec (len
);
3770 for (int i
= 0; i
< len
; ++i
)
3771 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3777 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3781 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3784 if (!builtin_pack_call_p (call
))
3787 tree fn
= CALL_EXPR_FN (call
);
3789 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3790 return expand_integer_pack (call
, args
, complain
, in_decl
);
3795 /* Structure used to track the progress of find_parameter_packs_r. */
3796 struct find_parameter_pack_data
3798 /* TREE_LIST that will contain all of the parameter packs found by
3800 tree
* parameter_packs
;
3802 /* Set of AST nodes that have been visited by the traversal. */
3803 hash_set
<tree
> *visited
;
3805 /* True iff we're making a type pack expansion. */
3806 bool type_pack_expansion_p
;
3809 /* Identifies all of the argument packs that occur in a template
3810 argument and appends them to the TREE_LIST inside DATA, which is a
3811 find_parameter_pack_data structure. This is a subroutine of
3812 make_pack_expansion and uses_parameter_packs. */
3814 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3817 struct find_parameter_pack_data
* ppd
=
3818 (struct find_parameter_pack_data
*)data
;
3819 bool parameter_pack_p
= false;
3821 /* Don't look through typedefs; we are interested in whether a
3822 parameter pack is actually written in the expression/type we're
3823 looking at, not the target type. */
3824 if (TYPE_P (t
) && typedef_variant_p (t
))
3826 /* But do look at arguments for an alias template. */
3827 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3828 cp_walk_tree (&TI_ARGS (tinfo
),
3829 &find_parameter_packs_r
,
3835 /* Identify whether this is a parameter pack or not. */
3836 switch (TREE_CODE (t
))
3838 case TEMPLATE_PARM_INDEX
:
3839 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3840 parameter_pack_p
= true;
3843 case TEMPLATE_TYPE_PARM
:
3844 t
= TYPE_MAIN_VARIANT (t
);
3846 case TEMPLATE_TEMPLATE_PARM
:
3847 /* If the placeholder appears in the decl-specifier-seq of a function
3848 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3849 is a pack expansion, the invented template parameter is a template
3851 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3852 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3853 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3854 parameter_pack_p
= true;
3859 if (DECL_PACK_P (t
))
3861 /* We don't want to walk into the type of a PARM_DECL,
3862 because we don't want to see the type parameter pack. */
3864 parameter_pack_p
= true;
3869 if (DECL_PACK_P (t
))
3871 /* We don't want to walk into the type of a variadic capture proxy,
3872 because we don't want to see the type parameter pack. */
3874 parameter_pack_p
= true;
3876 else if (variable_template_specialization_p (t
))
3878 cp_walk_tree (&DECL_TI_ARGS (t
),
3879 find_parameter_packs_r
,
3886 if (builtin_pack_call_p (t
))
3887 parameter_pack_p
= true;
3891 parameter_pack_p
= true;
3894 /* Not a parameter pack. */
3898 if (parameter_pack_p
)
3900 /* Add this parameter pack to the list. */
3901 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3905 cp_walk_tree (&TYPE_CONTEXT (t
),
3906 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3908 /* This switch statement will return immediately if we don't find a
3909 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3910 switch (TREE_CODE (t
))
3912 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3913 /* Check the template itself. */
3914 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3915 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3920 tree decl
= DECL_EXPR_DECL (t
);
3921 /* Ignore the declaration of a capture proxy for a parameter pack. */
3922 if (is_capture_proxy (decl
))
3924 if (is_typedef_decl (decl
))
3925 /* Since we stop at typedefs above, we need to look through them at
3926 the point of the DECL_EXPR. */
3927 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3928 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3933 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3935 cp_walk_tree (&TREE_TYPE (t
),
3936 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3940 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3945 case TYPE_PACK_EXPANSION
:
3946 case EXPR_PACK_EXPANSION
:
3951 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3956 case IDENTIFIER_NODE
:
3957 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3964 /* Look at explicit captures. */
3965 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3966 cap
; cap
= TREE_CHAIN (cap
))
3967 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3969 /* Since we defer implicit capture, look in the parms and body. */
3970 tree fn
= lambda_function (t
);
3971 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3973 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3981 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3982 type_pack_expansion_p to false so that any placeholders
3983 within the expression don't get marked as parameter packs. */
3984 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3985 ppd
->type_pack_expansion_p
= false;
3986 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3988 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3994 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3996 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3998 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4000 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4011 /* Determines if the expression or type T uses any parameter packs. */
4013 uses_parameter_packs (tree t
)
4015 tree parameter_packs
= NULL_TREE
;
4016 struct find_parameter_pack_data ppd
;
4017 ppd
.parameter_packs
= ¶meter_packs
;
4018 ppd
.visited
= new hash_set
<tree
>;
4019 ppd
.type_pack_expansion_p
= false;
4020 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4022 return parameter_packs
;
4025 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4026 representation a base-class initializer into a parameter pack
4027 expansion. If all goes well, the resulting node will be an
4028 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4031 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4034 tree parameter_packs
= NULL_TREE
;
4035 bool for_types
= false;
4036 struct find_parameter_pack_data ppd
;
4038 if (!arg
|| arg
== error_mark_node
)
4041 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4043 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4044 class initializer. In this case, the TREE_PURPOSE will be a
4045 _TYPE node (representing the base class expansion we're
4046 initializing) and the TREE_VALUE will be a TREE_LIST
4047 containing the initialization arguments.
4049 The resulting expansion looks somewhat different from most
4050 expansions. Rather than returning just one _EXPANSION, we
4051 return a TREE_LIST whose TREE_PURPOSE is a
4052 TYPE_PACK_EXPANSION containing the bases that will be
4053 initialized. The TREE_VALUE will be identical to the
4054 original TREE_VALUE, which is a list of arguments that will
4055 be passed to each base. We do not introduce any new pack
4056 expansion nodes into the TREE_VALUE (although it is possible
4057 that some already exist), because the TREE_PURPOSE and
4058 TREE_VALUE all need to be expanded together with the same
4059 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4060 resulting TREE_PURPOSE will mention the parameter packs in
4061 both the bases and the arguments to the bases. */
4064 tree parameter_packs
= NULL_TREE
;
4066 /* Determine which parameter packs will be used by the base
4068 ppd
.visited
= new hash_set
<tree
>;
4069 ppd
.parameter_packs
= ¶meter_packs
;
4070 ppd
.type_pack_expansion_p
= false;
4071 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4072 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4075 if (parameter_packs
== NULL_TREE
)
4077 if (complain
& tf_error
)
4078 error ("base initializer expansion %qT contains no parameter packs",
4081 return error_mark_node
;
4084 if (TREE_VALUE (arg
) != void_type_node
)
4086 /* Collect the sets of parameter packs used in each of the
4087 initialization arguments. */
4088 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4090 /* Determine which parameter packs will be expanded in this
4092 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4099 /* Create the pack expansion type for the base type. */
4100 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4101 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4102 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4103 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4105 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4106 they will rarely be compared to anything. */
4107 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4109 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4112 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4115 /* Build the PACK_EXPANSION_* node. */
4117 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4118 : make_node (EXPR_PACK_EXPANSION
);
4119 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4120 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4122 /* Propagate type and const-expression information. */
4123 TREE_TYPE (result
) = TREE_TYPE (arg
);
4124 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4125 /* Mark this read now, since the expansion might be length 0. */
4126 mark_exp_read (arg
);
4129 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4130 they will rarely be compared to anything. */
4131 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4133 /* Determine which parameter packs will be expanded. */
4134 ppd
.parameter_packs
= ¶meter_packs
;
4135 ppd
.visited
= new hash_set
<tree
>;
4136 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4137 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4140 /* Make sure we found some parameter packs. */
4141 if (parameter_packs
== NULL_TREE
)
4143 if (complain
& tf_error
)
4146 error ("expansion pattern %qT contains no parameter packs", arg
);
4148 error ("expansion pattern %qE contains no parameter packs", arg
);
4150 return error_mark_node
;
4152 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4154 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4159 /* Checks T for any "bare" parameter packs, which have not yet been
4160 expanded, and issues an error if any are found. This operation can
4161 only be done on full expressions or types (e.g., an expression
4162 statement, "if" condition, etc.), because we could have expressions like:
4164 foo(f(g(h(args)))...)
4166 where "args" is a parameter pack. check_for_bare_parameter_packs
4167 should not be called for the subexpressions args, h(args),
4168 g(h(args)), or f(g(h(args))), because we would produce erroneous
4171 Returns TRUE and emits an error if there were bare parameter packs,
4172 returns FALSE otherwise. */
4174 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4176 tree parameter_packs
= NULL_TREE
;
4177 struct find_parameter_pack_data ppd
;
4179 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4182 /* A lambda might use a parameter pack from the containing context. */
4183 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4184 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4187 if (TREE_CODE (t
) == TYPE_DECL
)
4190 ppd
.parameter_packs
= ¶meter_packs
;
4191 ppd
.visited
= new hash_set
<tree
>;
4192 ppd
.type_pack_expansion_p
= false;
4193 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4196 if (parameter_packs
)
4198 if (loc
== UNKNOWN_LOCATION
)
4199 loc
= cp_expr_loc_or_input_loc (t
);
4200 error_at (loc
, "parameter packs not expanded with %<...%>:");
4201 while (parameter_packs
)
4203 tree pack
= TREE_VALUE (parameter_packs
);
4204 tree name
= NULL_TREE
;
4206 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4207 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4208 name
= TYPE_NAME (pack
);
4209 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4210 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4211 else if (TREE_CODE (pack
) == CALL_EXPR
)
4212 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4214 name
= DECL_NAME (pack
);
4217 inform (loc
, " %qD", name
);
4219 inform (loc
, " %s", "<anonymous>");
4221 parameter_packs
= TREE_CHAIN (parameter_packs
);
4230 /* Expand any parameter packs that occur in the template arguments in
4233 expand_template_argument_pack (tree args
)
4235 if (args
== error_mark_node
)
4236 return error_mark_node
;
4238 tree result_args
= NULL_TREE
;
4239 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4240 int num_result_args
= -1;
4241 int non_default_args_count
= -1;
4243 /* First, determine if we need to expand anything, and the number of
4244 slots we'll need. */
4245 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4247 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4248 if (arg
== NULL_TREE
)
4250 if (ARGUMENT_PACK_P (arg
))
4252 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4253 if (num_result_args
< 0)
4254 num_result_args
= in_arg
+ num_packed
;
4256 num_result_args
+= num_packed
;
4260 if (num_result_args
>= 0)
4265 /* If no expansion is necessary, we're done. */
4266 if (num_result_args
< 0)
4269 /* Expand arguments. */
4270 result_args
= make_tree_vec (num_result_args
);
4271 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4272 non_default_args_count
=
4273 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4274 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4276 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4277 if (ARGUMENT_PACK_P (arg
))
4279 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4280 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4281 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4282 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4283 if (non_default_args_count
> 0)
4284 non_default_args_count
+= num_packed
- 1;
4288 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4292 if (non_default_args_count
>= 0)
4293 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4297 /* Checks if DECL shadows a template parameter.
4299 [temp.local]: A template-parameter shall not be redeclared within its
4300 scope (including nested scopes).
4302 Emits an error and returns TRUE if the DECL shadows a parameter,
4303 returns FALSE otherwise. */
4306 check_template_shadow (tree decl
)
4310 /* If we're not in a template, we can't possibly shadow a template
4312 if (!current_template_parms
)
4315 /* Figure out what we're shadowing. */
4316 decl
= OVL_FIRST (decl
);
4317 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4319 /* If there's no previous binding for this name, we're not shadowing
4320 anything, let alone a template parameter. */
4324 /* If we're not shadowing a template parameter, we're done. Note
4325 that OLDDECL might be an OVERLOAD (or perhaps even an
4326 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4328 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4331 /* We check for decl != olddecl to avoid bogus errors for using a
4332 name inside a class. We check TPFI to avoid duplicate errors for
4333 inline member templates. */
4335 || (DECL_TEMPLATE_PARM_P (decl
)
4336 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4339 /* Don't complain about the injected class name, as we've already
4340 complained about the class itself. */
4341 if (DECL_SELF_REFERENCE_P (decl
))
4344 if (DECL_TEMPLATE_PARM_P (decl
))
4345 error ("declaration of template parameter %q+D shadows "
4346 "template parameter", decl
);
4348 error ("declaration of %q+#D shadows template parameter", decl
);
4349 inform (DECL_SOURCE_LOCATION (olddecl
),
4350 "template parameter %qD declared here", olddecl
);
4354 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4355 ORIG_LEVEL, DECL, and TYPE. */
4358 build_template_parm_index (int index
,
4364 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4365 TEMPLATE_PARM_IDX (t
) = index
;
4366 TEMPLATE_PARM_LEVEL (t
) = level
;
4367 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4368 TEMPLATE_PARM_DECL (t
) = decl
;
4369 TREE_TYPE (t
) = type
;
4370 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4371 TREE_READONLY (t
) = TREE_READONLY (decl
);
4376 /* Find the canonical type parameter for the given template type
4377 parameter. Returns the canonical type parameter, which may be TYPE
4378 if no such parameter existed. */
4381 canonical_type_parameter (tree type
)
4384 int idx
= TEMPLATE_TYPE_IDX (type
);
4385 if (!canonical_template_parms
)
4386 vec_alloc (canonical_template_parms
, idx
+ 1);
4388 if (canonical_template_parms
->length () <= (unsigned) idx
)
4389 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4391 list
= (*canonical_template_parms
)[idx
];
4392 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4393 list
= TREE_CHAIN (list
);
4396 return TREE_VALUE (list
);
4399 (*canonical_template_parms
)[idx
]
4400 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4405 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4406 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4407 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4408 new one is created. */
4411 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4412 tsubst_flags_t complain
)
4414 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4415 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4416 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4417 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4419 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4421 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4422 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4424 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4425 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4426 DECL_ARTIFICIAL (decl
) = 1;
4427 SET_DECL_TEMPLATE_PARM_P (decl
);
4429 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4430 TEMPLATE_PARM_LEVEL (index
) - levels
,
4431 TEMPLATE_PARM_ORIG_LEVEL (index
),
4433 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4434 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4435 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4437 /* Template template parameters need this. */
4439 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4441 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4442 TYPE_DECL
, DECL_NAME (decl
), type
);
4443 DECL_TEMPLATE_RESULT (decl
) = inner
;
4444 DECL_ARTIFICIAL (inner
) = true;
4445 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4446 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4449 /* Attach the TPI to the decl. */
4450 if (TREE_CODE (inner
) == TYPE_DECL
)
4451 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4453 DECL_INITIAL (decl
) = tpi
;
4456 return TEMPLATE_PARM_DESCENDANTS (index
);
4459 /* Process information from new template parameter PARM and append it
4460 to the LIST being built. This new parameter is a non-type
4461 parameter iff IS_NON_TYPE is true. This new parameter is a
4462 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4466 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4467 bool is_non_type
, bool is_parameter_pack
)
4472 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4473 tree defval
= TREE_PURPOSE (parm
);
4474 tree constr
= TREE_TYPE (parm
);
4478 tree p
= tree_last (list
);
4480 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4483 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4484 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4486 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4494 parm
= TREE_VALUE (parm
);
4496 SET_DECL_TEMPLATE_PARM_P (parm
);
4498 if (TREE_TYPE (parm
) != error_mark_node
)
4502 The top-level cv-qualifiers on the template-parameter are
4503 ignored when determining its type. */
4504 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4505 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4506 TREE_TYPE (parm
) = error_mark_node
;
4507 else if (uses_parameter_packs (TREE_TYPE (parm
))
4508 && !is_parameter_pack
4509 /* If we're in a nested template parameter list, the template
4510 template parameter could be a parameter pack. */
4511 && processing_template_parmlist
== 1)
4513 /* This template parameter is not a parameter pack, but it
4514 should be. Complain about "bare" parameter packs. */
4515 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4517 /* Recover by calling this a parameter pack. */
4518 is_parameter_pack
= true;
4522 /* A template parameter is not modifiable. */
4523 TREE_CONSTANT (parm
) = 1;
4524 TREE_READONLY (parm
) = 1;
4525 decl
= build_decl (parm_loc
,
4526 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4527 TREE_CONSTANT (decl
) = 1;
4528 TREE_READONLY (decl
) = 1;
4529 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4530 = build_template_parm_index (idx
, processing_template_decl
,
4531 processing_template_decl
,
4532 decl
, TREE_TYPE (parm
));
4534 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4535 = is_parameter_pack
;
4540 parm
= TREE_VALUE (TREE_VALUE (parm
));
4542 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4544 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4545 /* This is for distinguishing between real templates and template
4546 template parameters */
4547 TREE_TYPE (parm
) = t
;
4548 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4553 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4554 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4555 decl
= build_decl (parm_loc
,
4556 TYPE_DECL
, parm
, t
);
4559 TYPE_NAME (t
) = decl
;
4560 TYPE_STUB_DECL (t
) = decl
;
4562 TEMPLATE_TYPE_PARM_INDEX (t
)
4563 = build_template_parm_index (idx
, processing_template_decl
,
4564 processing_template_decl
,
4565 decl
, TREE_TYPE (parm
));
4566 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4567 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4569 DECL_ARTIFICIAL (decl
) = 1;
4570 SET_DECL_TEMPLATE_PARM_P (decl
);
4572 /* Build requirements for the type/template parameter.
4573 This must be done after SET_DECL_TEMPLATE_PARM_P or
4574 process_template_parm could fail. */
4575 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4577 decl
= pushdecl (decl
);
4581 /* Build the parameter node linking the parameter declaration,
4582 its default argument (if any), and its constraints (if any). */
4583 parm
= build_tree_list (defval
, parm
);
4584 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4586 return chainon (list
, parm
);
4589 /* The end of a template parameter list has been reached. Process the
4590 tree list into a parameter vector, converting each parameter into a more
4591 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4595 end_template_parm_list (tree parms
)
4599 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4601 /* Pop the dummy parameter level and add the real one. */
4602 current_template_parms
= TREE_CHAIN (current_template_parms
);
4604 current_template_parms
4605 = tree_cons (size_int (processing_template_decl
),
4606 saved_parmlist
, current_template_parms
);
4608 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4610 next
= TREE_CHAIN (parm
);
4611 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4612 TREE_CHAIN (parm
) = NULL_TREE
;
4615 --processing_template_parmlist
;
4617 return saved_parmlist
;
4620 // Explicitly indicate the end of the template parameter list. We assume
4621 // that the current template parameters have been constructed and/or
4622 // managed explicitly, as when creating new template template parameters
4623 // from a shorthand constraint.
4625 end_template_parm_list ()
4627 --processing_template_parmlist
;
4630 /* end_template_decl is called after a template declaration is seen. */
4633 end_template_decl (void)
4635 reset_specialization ();
4637 if (! processing_template_decl
)
4640 /* This matches the pushlevel in begin_template_parm_list. */
4643 --processing_template_decl
;
4644 current_template_parms
= TREE_CHAIN (current_template_parms
);
4647 /* Takes a TREE_LIST representing a template parameter and convert it
4648 into an argument suitable to be passed to the type substitution
4649 functions. Note that If the TREE_LIST contains an error_mark
4650 node, the returned argument is error_mark_node. */
4653 template_parm_to_arg (tree t
)
4657 || TREE_CODE (t
) != TREE_LIST
)
4660 if (error_operand_p (TREE_VALUE (t
)))
4661 return error_mark_node
;
4665 if (TREE_CODE (t
) == TYPE_DECL
4666 || TREE_CODE (t
) == TEMPLATE_DECL
)
4670 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4672 /* Turn this argument into a TYPE_ARGUMENT_PACK
4673 with a single element, which expands T. */
4674 tree vec
= make_tree_vec (1);
4676 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4678 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4680 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4681 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4686 t
= DECL_INITIAL (t
);
4688 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4690 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4691 with a single element, which expands T. */
4692 tree vec
= make_tree_vec (1);
4694 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4696 t
= convert_from_reference (t
);
4697 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4699 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4700 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4703 t
= convert_from_reference (t
);
4708 /* Given a single level of template parameters (a TREE_VEC), return it
4709 as a set of template arguments. */
4712 template_parms_level_to_args (tree parms
)
4714 tree a
= copy_node (parms
);
4715 TREE_TYPE (a
) = NULL_TREE
;
4716 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4717 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4725 /* Given a set of template parameters, return them as a set of template
4726 arguments. The template parameters are represented as a TREE_VEC, in
4727 the form documented in cp-tree.h for template arguments. */
4730 template_parms_to_args (tree parms
)
4733 tree args
= NULL_TREE
;
4734 int length
= TMPL_PARMS_DEPTH (parms
);
4737 /* If there is only one level of template parameters, we do not
4738 create a TREE_VEC of TREE_VECs. Instead, we return a single
4739 TREE_VEC containing the arguments. */
4741 args
= make_tree_vec (length
);
4743 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4745 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4748 TREE_VEC_ELT (args
, --l
) = a
;
4756 /* Within the declaration of a template, return the currently active
4757 template parameters as an argument TREE_VEC. */
4760 current_template_args (void)
4762 return template_parms_to_args (current_template_parms
);
4765 /* Return the fully generic arguments for of TMPL, i.e. what
4766 current_template_args would be while parsing it. */
4769 generic_targs_for (tree tmpl
)
4771 if (tmpl
== NULL_TREE
)
4773 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4774 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4775 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4776 template parameter, it has no TEMPLATE_INFO; for a partial
4777 specialization, it has the arguments for the primary template, and we
4778 want the arguments for the partial specialization. */;
4779 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4780 if (tree ti
= get_template_info (result
))
4781 return TI_ARGS (ti
);
4782 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4785 /* Update the declared TYPE by doing any lookups which were thought to be
4786 dependent, but are not now that we know the SCOPE of the declarator. */
4789 maybe_update_decl_type (tree orig_type
, tree scope
)
4791 tree type
= orig_type
;
4793 if (type
== NULL_TREE
)
4796 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4797 type
= TREE_TYPE (type
);
4799 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4800 && dependent_type_p (type
)
4801 /* Don't bother building up the args in this case. */
4802 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4804 /* tsubst in the args corresponding to the template parameters,
4805 including auto if present. Most things will be unchanged, but
4806 make_typename_type and tsubst_qualified_id will resolve
4807 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4808 tree args
= current_template_args ();
4809 tree auto_node
= type_uses_auto (type
);
4813 tree auto_vec
= make_tree_vec (1);
4814 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4815 args
= add_to_template_args (args
, auto_vec
);
4817 pushed
= push_scope (scope
);
4818 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4823 if (type
== error_mark_node
)
4826 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4828 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4831 type
= TYPE_NAME (type
);
4836 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4837 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4838 the new template is a member template. */
4841 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4843 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4844 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4845 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4846 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4847 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4848 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4853 struct template_parm_data
4855 /* The level of the template parameters we are currently
4859 /* The index of the specialization argument we are currently
4863 /* An array whose size is the number of template parameters. The
4864 elements are nonzero if the parameter has been used in any one
4865 of the arguments processed so far. */
4868 /* An array whose size is the number of template arguments. The
4869 elements are nonzero if the argument makes use of template
4870 parameters of this level. */
4871 int* arg_uses_template_parms
;
4874 /* Subroutine of push_template_decl used to see if each template
4875 parameter in a partial specialization is used in the explicit
4876 argument list. If T is of the LEVEL given in DATA (which is
4877 treated as a template_parm_data*), then DATA->PARMS is marked
4881 mark_template_parm (tree t
, void* data
)
4885 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4887 template_parm_level_and_index (t
, &level
, &idx
);
4889 if (level
== tpd
->level
)
4891 tpd
->parms
[idx
] = 1;
4892 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4895 /* In C++17 the type of a non-type argument is a deduced context. */
4896 if (cxx_dialect
>= cxx17
4897 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4898 for_each_template_parm (TREE_TYPE (t
),
4899 &mark_template_parm
,
4902 /*include_nondeduced_p=*/false);
4904 /* Return zero so that for_each_template_parm will continue the
4905 traversal of the tree; we want to mark *every* template parm. */
4909 /* Process the partial specialization DECL. */
4912 process_partial_specialization (tree decl
)
4914 tree type
= TREE_TYPE (decl
);
4915 tree tinfo
= get_template_info (decl
);
4916 tree maintmpl
= TI_TEMPLATE (tinfo
);
4917 tree specargs
= TI_ARGS (tinfo
);
4918 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4919 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4922 int nargs
= TREE_VEC_LENGTH (inner_args
);
4925 bool did_error_intro
= false;
4926 struct template_parm_data tpd
;
4927 struct template_parm_data tpd2
;
4929 gcc_assert (current_template_parms
);
4931 /* A concept cannot be specialized. */
4932 if (flag_concepts
&& variable_concept_p (maintmpl
))
4934 error ("specialization of variable concept %q#D", maintmpl
);
4935 return error_mark_node
;
4938 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4939 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4941 /* We check that each of the template parameters given in the
4942 partial specialization is used in the argument list to the
4943 specialization. For example:
4945 template <class T> struct S;
4946 template <class T> struct S<T*>;
4948 The second declaration is OK because `T*' uses the template
4949 parameter T, whereas
4951 template <class T> struct S<int>;
4953 is no good. Even trickier is:
4964 The S2<T> declaration is actually invalid; it is a
4965 full-specialization. Of course,
4968 struct S2<T (*)(U)>;
4970 or some such would have been OK. */
4971 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4972 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4973 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4975 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4976 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4977 for (i
= 0; i
< nargs
; ++i
)
4979 tpd
.current_arg
= i
;
4980 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4981 &mark_template_parm
,
4984 /*include_nondeduced_p=*/false);
4986 for (i
= 0; i
< ntparms
; ++i
)
4987 if (tpd
.parms
[i
] == 0)
4989 /* One of the template parms was not used in a deduced context in the
4991 if (!did_error_intro
)
4993 error ("template parameters not deducible in "
4994 "partial specialization:");
4995 did_error_intro
= true;
4998 inform (input_location
, " %qD",
4999 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5002 if (did_error_intro
)
5003 return error_mark_node
;
5005 /* [temp.class.spec]
5007 The argument list of the specialization shall not be identical to
5008 the implicit argument list of the primary template. */
5010 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5011 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5013 || !strictly_subsumes (current_template_constraints (),
5014 inner_args
, maintmpl
)))
5017 error ("partial specialization %q+D does not specialize "
5018 "any template arguments; to define the primary template, "
5019 "remove the template argument list", decl
);
5021 error ("partial specialization %q+D does not specialize any "
5022 "template arguments and is not more constrained than "
5023 "the primary template; to define the primary template, "
5024 "remove the template argument list", decl
);
5025 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5028 /* A partial specialization that replaces multiple parameters of the
5029 primary template with a pack expansion is less specialized for those
5031 if (nargs
< DECL_NTPARMS (maintmpl
))
5033 error ("partial specialization is not more specialized than the "
5034 "primary template because it replaces multiple parameters "
5035 "with a pack expansion");
5036 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5037 /* Avoid crash in process_partial_specialization. */
5041 else if (nargs
> DECL_NTPARMS (maintmpl
))
5043 error ("too many arguments for partial specialization %qT", type
);
5044 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5045 /* Avoid crash below. */
5049 /* If we aren't in a dependent class, we can actually try deduction. */
5050 else if (tpd
.level
== 1
5051 /* FIXME we should be able to handle a partial specialization of a
5052 partial instantiation, but currently we can't (c++/41727). */
5053 && TMPL_ARGS_DEPTH (specargs
) == 1
5054 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5056 auto_diagnostic_group d
;
5057 if (permerror (input_location
, "partial specialization %qD is not "
5058 "more specialized than", decl
))
5059 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5063 /* [temp.class.spec]
5065 A partially specialized non-type argument expression shall not
5066 involve template parameters of the partial specialization except
5067 when the argument expression is a simple identifier.
5069 The type of a template parameter corresponding to a specialized
5070 non-type argument shall not be dependent on a parameter of the
5073 Also, we verify that pack expansions only occur at the
5074 end of the argument list. */
5076 for (i
= 0; i
< nargs
; ++i
)
5078 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5079 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5080 tree packed_args
= NULL_TREE
;
5083 if (ARGUMENT_PACK_P (arg
))
5085 /* Extract the arguments from the argument pack. We'll be
5086 iterating over these in the following loop. */
5087 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5088 len
= TREE_VEC_LENGTH (packed_args
);
5091 for (j
= 0; j
< len
; j
++)
5094 /* Get the Jth argument in the parameter pack. */
5095 arg
= TREE_VEC_ELT (packed_args
, j
);
5097 if (PACK_EXPANSION_P (arg
))
5099 /* Pack expansions must come at the end of the
5101 if ((packed_args
&& j
< len
- 1)
5102 || (!packed_args
&& i
< nargs
- 1))
5104 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5105 error ("parameter pack argument %qE must be at the "
5106 "end of the template argument list", arg
);
5108 error ("parameter pack argument %qT must be at the "
5109 "end of the template argument list", arg
);
5113 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5114 /* We only care about the pattern. */
5115 arg
= PACK_EXPANSION_PATTERN (arg
);
5117 if (/* These first two lines are the `non-type' bit. */
5119 && TREE_CODE (arg
) != TEMPLATE_DECL
5120 /* This next two lines are the `argument expression is not just a
5121 simple identifier' condition and also the `specialized
5122 non-type argument' bit. */
5123 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5124 && !((REFERENCE_REF_P (arg
)
5125 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5126 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5128 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5129 || (packed_args
&& uses_template_parms (arg
)))
5130 error_at (cp_expr_loc_or_input_loc (arg
),
5131 "template argument %qE involves template "
5132 "parameter(s)", arg
);
5135 /* Look at the corresponding template parameter,
5136 marking which template parameters its type depends
5138 tree type
= TREE_TYPE (parm
);
5142 /* We haven't yet initialized TPD2. Do so now. */
5143 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5144 /* The number of parameters here is the number in the
5145 main template, which, as checked in the assertion
5147 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5149 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5152 /* Mark the template parameters. But this time, we're
5153 looking for the template parameters of the main
5154 template, not in the specialization. */
5155 tpd2
.current_arg
= i
;
5156 tpd2
.arg_uses_template_parms
[i
] = 0;
5157 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5158 for_each_template_parm (type
,
5159 &mark_template_parm
,
5162 /*include_nondeduced_p=*/false);
5164 if (tpd2
.arg_uses_template_parms
[i
])
5166 /* The type depended on some template parameters.
5167 If they are fully specialized in the
5168 specialization, that's OK. */
5171 for (j
= 0; j
< nargs
; ++j
)
5172 if (tpd2
.parms
[j
] != 0
5173 && tpd
.arg_uses_template_parms
[j
])
5176 error_n (input_location
, count
,
5177 "type %qT of template argument %qE depends "
5178 "on a template parameter",
5179 "type %qT of template argument %qE depends "
5180 "on template parameters",
5189 /* We should only get here once. */
5190 if (TREE_CODE (decl
) == TYPE_DECL
)
5191 gcc_assert (!COMPLETE_TYPE_P (type
));
5193 // Build the template decl.
5194 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5195 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5196 TREE_TYPE (tmpl
) = type
;
5197 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5198 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5199 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5200 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5202 /* Give template template parms a DECL_CONTEXT of the template
5203 for which they are a parameter. */
5204 for (i
= 0; i
< ntparms
; ++i
)
5206 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5207 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5208 DECL_CONTEXT (parm
) = tmpl
;
5212 /* We didn't register this in check_explicit_specialization so we could
5213 wait until the constraints were set. */
5214 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5216 associate_classtype_constraints (type
);
5218 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5219 = tree_cons (specargs
, tmpl
,
5220 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5221 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5223 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5224 inst
= TREE_CHAIN (inst
))
5226 tree instance
= TREE_VALUE (inst
);
5227 if (TYPE_P (instance
)
5228 ? (COMPLETE_TYPE_P (instance
)
5229 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5230 : DECL_TEMPLATE_INSTANTIATION (instance
))
5232 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5233 tree inst_decl
= (DECL_P (instance
)
5234 ? instance
: TYPE_NAME (instance
));
5237 else if (spec
== error_mark_node
)
5238 permerror (input_location
,
5239 "declaration of %qD ambiguates earlier template "
5240 "instantiation for %qD", decl
, inst_decl
);
5241 else if (TREE_VALUE (spec
) == tmpl
)
5242 permerror (input_location
,
5243 "partial specialization of %qD after instantiation "
5244 "of %qD", decl
, inst_decl
);
5251 /* PARM is a template parameter of some form; return the corresponding
5252 TEMPLATE_PARM_INDEX. */
5255 get_template_parm_index (tree parm
)
5257 if (TREE_CODE (parm
) == PARM_DECL
5258 || TREE_CODE (parm
) == CONST_DECL
)
5259 parm
= DECL_INITIAL (parm
);
5260 else if (TREE_CODE (parm
) == TYPE_DECL
5261 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5262 parm
= TREE_TYPE (parm
);
5263 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5264 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5265 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5266 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5267 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5271 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5272 parameter packs used by the template parameter PARM. */
5275 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5277 /* A type parm can't refer to another parm. */
5278 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5280 else if (TREE_CODE (parm
) == PARM_DECL
)
5282 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5287 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5289 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5290 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5292 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5293 if (template_parameter_pack_p (p
))
5294 /* Any packs in the type are expanded by this parameter. */;
5296 fixed_parameter_pack_p_1 (p
, ppd
);
5300 /* PARM is a template parameter pack. Return any parameter packs used in
5301 its type or the type of any of its template parameters. If there are
5302 any such packs, it will be instantiated into a fixed template parameter
5303 list by partial instantiation rather than be fully deduced. */
5306 fixed_parameter_pack_p (tree parm
)
5308 /* This can only be true in a member template. */
5309 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5311 /* This can only be true for a parameter pack. */
5312 if (!template_parameter_pack_p (parm
))
5314 /* A type parm can't refer to another parm. */
5315 if (TREE_CODE (parm
) == TYPE_DECL
)
5318 tree parameter_packs
= NULL_TREE
;
5319 struct find_parameter_pack_data ppd
;
5320 ppd
.parameter_packs
= ¶meter_packs
;
5321 ppd
.visited
= new hash_set
<tree
>;
5322 ppd
.type_pack_expansion_p
= false;
5324 fixed_parameter_pack_p_1 (parm
, &ppd
);
5327 return parameter_packs
;
5330 /* Check that a template declaration's use of default arguments and
5331 parameter packs is not invalid. Here, PARMS are the template
5332 parameters. IS_PRIMARY is true if DECL is the thing declared by
5333 a primary template. IS_PARTIAL is true if DECL is a partial
5336 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5337 function template declaration or a friend class template
5338 declaration. In the function case, 1 indicates a declaration, 2
5339 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5340 emitted for extraneous default arguments.
5342 Returns TRUE if there were no errors found, FALSE otherwise. */
5345 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5346 bool is_partial
, int is_friend_decl
)
5349 int last_level_to_check
;
5351 bool no_errors
= true;
5355 A default template-argument shall not be specified in a
5356 function template declaration or a function template definition, nor
5357 in the template-parameter-list of the definition of a member of a
5360 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5361 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5362 /* You can't have a function template declaration in a local
5363 scope, nor you can you define a member of a class template in a
5367 if ((TREE_CODE (decl
) == TYPE_DECL
5369 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5370 || (TREE_CODE (decl
) == FUNCTION_DECL
5371 && LAMBDA_FUNCTION_P (decl
)))
5372 /* A lambda doesn't have an explicit declaration; don't complain
5373 about the parms of the enclosing class. */
5376 if (current_class_type
5377 && !TYPE_BEING_DEFINED (current_class_type
)
5378 && DECL_LANG_SPECIFIC (decl
)
5379 && DECL_DECLARES_FUNCTION_P (decl
)
5380 /* If this is either a friend defined in the scope of the class
5381 or a member function. */
5382 && (DECL_FUNCTION_MEMBER_P (decl
)
5383 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5384 : DECL_FRIEND_CONTEXT (decl
)
5385 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5387 /* And, if it was a member function, it really was defined in
5388 the scope of the class. */
5389 && (!DECL_FUNCTION_MEMBER_P (decl
)
5390 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5391 /* We already checked these parameters when the template was
5392 declared, so there's no need to do it again now. This function
5393 was defined in class scope, but we're processing its body now
5394 that the class is complete. */
5397 /* Core issue 226 (C++0x only): the following only applies to class
5400 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5404 If a template-parameter has a default template-argument, all
5405 subsequent template-parameters shall have a default
5406 template-argument supplied. */
5407 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5409 tree inner_parms
= TREE_VALUE (parm_level
);
5410 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5411 int seen_def_arg_p
= 0;
5414 for (i
= 0; i
< ntparms
; ++i
)
5416 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5418 if (parm
== error_mark_node
)
5421 if (TREE_PURPOSE (parm
))
5423 else if (seen_def_arg_p
5424 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5426 error ("no default argument for %qD", TREE_VALUE (parm
));
5427 /* For better subsequent error-recovery, we indicate that
5428 there should have been a default argument. */
5429 TREE_PURPOSE (parm
) = error_mark_node
;
5432 else if (!is_partial
5434 /* Don't complain about an enclosing partial
5436 && parm_level
== parms
5437 && TREE_CODE (decl
) == TYPE_DECL
5439 && template_parameter_pack_p (TREE_VALUE (parm
))
5440 /* A fixed parameter pack will be partially
5441 instantiated into a fixed length list. */
5442 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5444 /* A primary class template can only have one
5445 parameter pack, at the end of the template
5448 error ("parameter pack %q+D must be at the end of the"
5449 " template parameter list", TREE_VALUE (parm
));
5451 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5459 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5463 /* For an ordinary class template, default template arguments are
5464 allowed at the innermost level, e.g.:
5465 template <class T = int>
5467 but, in a partial specialization, they're not allowed even
5468 there, as we have in [temp.class.spec]:
5470 The template parameter list of a specialization shall not
5471 contain default template argument values.
5473 So, for a partial specialization, or for a function template
5474 (in C++98/C++03), we look at all of them. */
5477 /* But, for a primary class template that is not a partial
5478 specialization we look at all template parameters except the
5480 parms
= TREE_CHAIN (parms
);
5482 /* Figure out what error message to issue. */
5483 if (is_friend_decl
== 2)
5484 msg
= G_("default template arguments may not be used in function template "
5485 "friend re-declaration");
5486 else if (is_friend_decl
)
5487 msg
= G_("default template arguments may not be used in template "
5488 "friend declarations");
5489 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5490 msg
= G_("default template arguments may not be used in function templates "
5491 "without %<-std=c++11%> or %<-std=gnu++11%>");
5492 else if (is_partial
)
5493 msg
= G_("default template arguments may not be used in "
5494 "partial specializations");
5495 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5496 msg
= G_("default argument for template parameter for class enclosing %qD");
5498 /* Per [temp.param]/9, "A default template-argument shall not be
5499 specified in the template-parameter-lists of the definition of
5500 a member of a class template that appears outside of the member's
5501 class.", thus if we aren't handling a member of a class template
5502 there is no need to examine the parameters. */
5505 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5506 /* If we're inside a class definition, there's no need to
5507 examine the parameters to the class itself. On the one
5508 hand, they will be checked when the class is defined, and,
5509 on the other, default arguments are valid in things like:
5510 template <class T = double>
5511 struct S { template <class U> void f(U); };
5512 Here the default argument for `S' has no bearing on the
5513 declaration of `f'. */
5514 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5516 /* Check everything. */
5517 last_level_to_check
= 0;
5519 for (parm_level
= parms
;
5520 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5521 parm_level
= TREE_CHAIN (parm_level
))
5523 tree inner_parms
= TREE_VALUE (parm_level
);
5527 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5528 for (i
= 0; i
< ntparms
; ++i
)
5530 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5533 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5538 if (is_friend_decl
== 2)
5545 /* Clear out the default argument so that we are not
5547 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5551 /* At this point, if we're still interested in issuing messages,
5552 they must apply to classes surrounding the object declared. */
5554 msg
= G_("default argument for template parameter for class "
5561 /* Worker for push_template_decl_real, called via
5562 for_each_template_parm. DATA is really an int, indicating the
5563 level of the parameters we are interested in. If T is a template
5564 parameter of that level, return nonzero. */
5567 template_parm_this_level_p (tree t
, void* data
)
5569 int this_level
= *(int *)data
;
5572 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5573 level
= TEMPLATE_PARM_LEVEL (t
);
5575 level
= TEMPLATE_TYPE_LEVEL (t
);
5576 return level
== this_level
;
5579 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5580 DATA is really an int, indicating the innermost outer level of parameters.
5581 If T is a template parameter of that level or further out, return
5585 template_parm_outer_level (tree t
, void *data
)
5587 int this_level
= *(int *)data
;
5590 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5591 level
= TEMPLATE_PARM_LEVEL (t
);
5593 level
= TEMPLATE_TYPE_LEVEL (t
);
5594 return level
<= this_level
;
5597 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5598 parameters given by current_template_args, or reuses a
5599 previously existing one, if appropriate. Returns the DECL, or an
5600 equivalent one, if it is replaced via a call to duplicate_decls.
5602 If IS_FRIEND is true, DECL is a friend declaration. */
5605 push_template_decl_real (tree decl
, bool is_friend
)
5613 int new_template_p
= 0;
5614 /* True if the template is a member template, in the sense of
5616 bool member_template_p
= false;
5618 if (decl
== error_mark_node
|| !current_template_parms
)
5619 return error_mark_node
;
5621 /* See if this is a partial specialization. */
5622 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5623 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5624 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5626 && DECL_LANG_SPECIFIC (decl
)
5627 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5628 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5630 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5634 /* For a friend, we want the context of the friend, not
5635 the type of which it is a friend. */
5636 ctx
= CP_DECL_CONTEXT (decl
);
5637 else if (CP_DECL_CONTEXT (decl
)
5638 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5639 /* In the case of a virtual function, we want the class in which
5641 ctx
= CP_DECL_CONTEXT (decl
);
5643 /* Otherwise, if we're currently defining some class, the DECL
5644 is assumed to be a member of the class. */
5645 ctx
= current_scope ();
5647 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5650 if (!DECL_CONTEXT (decl
))
5651 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5653 /* See if this is a primary template. */
5654 if (is_friend
&& ctx
5655 && uses_template_parms_level (ctx
, processing_template_decl
))
5656 /* A friend template that specifies a class context, i.e.
5657 template <typename T> friend void A<T>::f();
5660 else if (TREE_CODE (decl
) == TYPE_DECL
5661 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5664 is_primary
= template_parm_scope_p ();
5668 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5670 if (DECL_CLASS_SCOPE_P (decl
))
5671 member_template_p
= true;
5672 if (TREE_CODE (decl
) == TYPE_DECL
5673 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5675 error ("template class without a name");
5676 return error_mark_node
;
5678 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5680 if (member_template_p
)
5682 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5683 error ("member template %qD may not have virt-specifiers", decl
);
5685 if (DECL_DESTRUCTOR_P (decl
))
5689 A destructor shall not be a member template. */
5690 error_at (DECL_SOURCE_LOCATION (decl
),
5691 "destructor %qD declared as member template", decl
);
5692 return error_mark_node
;
5694 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5695 && (!prototype_p (TREE_TYPE (decl
))
5696 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5697 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5698 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5699 == void_list_node
)))
5701 /* [basic.stc.dynamic.allocation]
5703 An allocation function can be a function
5704 template. ... Template allocation functions shall
5705 have two or more parameters. */
5706 error ("invalid template declaration of %qD", decl
);
5707 return error_mark_node
;
5710 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5711 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5713 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5714 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5715 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5717 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5718 if (TREE_CODE (t
) == TYPE_DECL
)
5720 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5721 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5724 else if (TREE_CODE (decl
) == TYPE_DECL
5725 && TYPE_DECL_ALIAS_P (decl
))
5726 /* alias-declaration */
5727 gcc_assert (!DECL_ARTIFICIAL (decl
));
5728 else if (VAR_P (decl
))
5729 /* C++14 variable template. */;
5730 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5731 /* C++2a concept definitions. */;
5734 error ("template declaration of %q#D", decl
);
5735 return error_mark_node
;
5739 /* Check to see that the rules regarding the use of default
5740 arguments are not being violated. We check args for a friend
5741 functions when we know whether it's a definition, introducing
5742 declaration or re-declaration. */
5743 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5744 check_default_tmpl_args (decl
, current_template_parms
,
5745 is_primary
, is_partial
, is_friend
);
5747 /* Ensure that there are no parameter packs in the type of this
5748 declaration that have not been expanded. */
5749 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5751 /* Check each of the arguments individually to see if there are
5752 any bare parameter packs. */
5753 tree type
= TREE_TYPE (decl
);
5754 tree arg
= DECL_ARGUMENTS (decl
);
5755 tree argtype
= TYPE_ARG_TYPES (type
);
5757 while (arg
&& argtype
)
5759 if (!DECL_PACK_P (arg
)
5760 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5762 /* This is a PARM_DECL that contains unexpanded parameter
5763 packs. We have already complained about this in the
5764 check_for_bare_parameter_packs call, so just replace
5765 these types with ERROR_MARK_NODE. */
5766 TREE_TYPE (arg
) = error_mark_node
;
5767 TREE_VALUE (argtype
) = error_mark_node
;
5770 arg
= DECL_CHAIN (arg
);
5771 argtype
= TREE_CHAIN (argtype
);
5774 /* Check for bare parameter packs in the return type and the
5775 exception specifiers. */
5776 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5777 /* Errors were already issued, set return type to int
5778 as the frontend doesn't expect error_mark_node as
5780 TREE_TYPE (type
) = integer_type_node
;
5781 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5782 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5784 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5785 ? DECL_ORIGINAL_TYPE (decl
)
5786 : TREE_TYPE (decl
)))
5788 TREE_TYPE (decl
) = error_mark_node
;
5789 return error_mark_node
;
5793 return process_partial_specialization (decl
);
5795 args
= current_template_args ();
5798 || TREE_CODE (ctx
) == FUNCTION_DECL
5799 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5800 || (TREE_CODE (decl
) == TYPE_DECL
5801 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5802 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5804 if (DECL_LANG_SPECIFIC (decl
)
5805 && DECL_TEMPLATE_INFO (decl
)
5806 && DECL_TI_TEMPLATE (decl
))
5807 tmpl
= DECL_TI_TEMPLATE (decl
);
5808 /* If DECL is a TYPE_DECL for a class-template, then there won't
5809 be DECL_LANG_SPECIFIC. The information equivalent to
5810 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5811 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5812 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5813 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5815 /* Since a template declaration already existed for this
5816 class-type, we must be redeclaring it here. Make sure
5817 that the redeclaration is valid. */
5818 redeclare_class_template (TREE_TYPE (decl
),
5819 current_template_parms
,
5820 current_template_constraints ());
5821 /* We don't need to create a new TEMPLATE_DECL; just use the
5822 one we already had. */
5823 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5827 tmpl
= build_template_decl (decl
, current_template_parms
,
5831 if (DECL_LANG_SPECIFIC (decl
)
5832 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5834 /* A specialization of a member template of a template
5836 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5837 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5838 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5844 tree a
, t
, current
, parms
;
5846 tree tinfo
= get_template_info (decl
);
5850 error ("template definition of non-template %q#D", decl
);
5851 return error_mark_node
;
5854 tmpl
= TI_TEMPLATE (tinfo
);
5856 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5857 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5858 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5859 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5863 /* The declaration is a specialization of a member
5864 template, declared outside the class. Therefore, the
5865 innermost template arguments will be NULL, so we
5866 replace them with the arguments determined by the
5867 earlier call to check_explicit_specialization. */
5868 args
= DECL_TI_ARGS (decl
);
5871 = build_template_decl (decl
, current_template_parms
,
5873 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5874 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5875 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5876 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5877 DECL_TEMPLATE_INFO (new_tmpl
)
5878 = build_template_info (tmpl
, args
);
5880 register_specialization (new_tmpl
,
5881 most_general_template (tmpl
),
5887 /* Make sure the template headers we got make sense. */
5889 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5890 i
= TMPL_PARMS_DEPTH (parms
);
5891 if (TMPL_ARGS_DEPTH (args
) != i
)
5893 error ("expected %d levels of template parms for %q#D, got %d",
5894 i
, decl
, TMPL_ARGS_DEPTH (args
));
5895 DECL_INTERFACE_KNOWN (decl
) = 1;
5896 return error_mark_node
;
5899 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5901 a
= TMPL_ARGS_LEVEL (args
, i
);
5902 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5904 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5906 if (current
== decl
)
5907 error ("got %d template parameters for %q#D",
5908 TREE_VEC_LENGTH (a
), decl
);
5910 error ("got %d template parameters for %q#T",
5911 TREE_VEC_LENGTH (a
), current
);
5912 error (" but %d required", TREE_VEC_LENGTH (t
));
5913 /* Avoid crash in import_export_decl. */
5914 DECL_INTERFACE_KNOWN (decl
) = 1;
5915 return error_mark_node
;
5918 if (current
== decl
)
5920 else if (current
== NULL_TREE
)
5921 /* Can happen in erroneous input. */
5924 current
= get_containing_scope (current
);
5927 /* Check that the parms are used in the appropriate qualifying scopes
5928 in the declarator. */
5929 if (!comp_template_args
5931 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5933 error ("template arguments to %qD do not match original "
5934 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5935 if (!uses_template_parms (TI_ARGS (tinfo
)))
5936 inform (input_location
, "use %<template<>%> for"
5937 " an explicit specialization");
5938 /* Avoid crash in import_export_decl. */
5939 DECL_INTERFACE_KNOWN (decl
) = 1;
5940 return error_mark_node
;
5944 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5945 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5947 /* Push template declarations for global functions and types. Note
5948 that we do not try to push a global template friend declared in a
5949 template class; such a thing may well depend on the template
5950 parameters of the class. */
5951 if (new_template_p
&& !ctx
5952 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5954 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5955 if (tmpl
== error_mark_node
)
5956 return error_mark_node
;
5958 /* Hide template friend classes that haven't been declared yet. */
5959 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5961 DECL_ANTICIPATED (tmpl
) = 1;
5962 DECL_FRIEND_P (tmpl
) = 1;
5968 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5970 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5972 /* Give template template parms a DECL_CONTEXT of the template
5973 for which they are a parameter. */
5974 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5975 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5977 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5978 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5979 DECL_CONTEXT (parm
) = tmpl
;
5982 if (TREE_CODE (decl
) == TYPE_DECL
5983 && TYPE_DECL_ALIAS_P (decl
))
5986 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
5988 /* ??? Why don't we do this here for all templates? */
5989 constr
= build_constraints (constr
, NULL_TREE
);
5990 set_constraints (decl
, constr
);
5992 if (complex_alias_template_p (tmpl
))
5993 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5997 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5998 back to its most general template. If TMPL is a specialization,
5999 ARGS may only have the innermost set of arguments. Add the missing
6000 argument levels if necessary. */
6001 if (DECL_TEMPLATE_INFO (tmpl
))
6002 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6004 info
= build_template_info (tmpl
, args
);
6006 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6007 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6011 retrofit_lang_decl (decl
);
6012 if (DECL_LANG_SPECIFIC (decl
))
6013 DECL_TEMPLATE_INFO (decl
) = info
;
6016 if (flag_implicit_templates
6018 && TREE_PUBLIC (decl
)
6019 && VAR_OR_FUNCTION_DECL_P (decl
))
6020 /* Set DECL_COMDAT on template instantiations; if we force
6021 them to be emitted by explicit instantiation,
6022 mark_needed will tell cgraph to do the right thing. */
6023 DECL_COMDAT (decl
) = true;
6025 return DECL_TEMPLATE_RESULT (tmpl
);
6029 push_template_decl (tree decl
)
6031 return push_template_decl_real (decl
, false);
6034 /* FN is an inheriting constructor that inherits from the constructor
6035 template INHERITED; turn FN into a constructor template with a matching
6039 add_inherited_template_parms (tree fn
, tree inherited
)
6042 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6043 inner_parms
= copy_node (inner_parms
);
6045 = tree_cons (size_int (processing_template_decl
+ 1),
6046 inner_parms
, current_template_parms
);
6047 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6048 tree args
= template_parms_to_args (parms
);
6049 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6050 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
6051 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
6052 DECL_ARTIFICIAL (tmpl
) = true;
6053 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6057 /* Called when a class template TYPE is redeclared with the indicated
6058 template PARMS, e.g.:
6060 template <class T> struct S;
6061 template <class T> struct S {}; */
6064 redeclare_class_template (tree type
, tree parms
, tree cons
)
6070 if (!TYPE_TEMPLATE_INFO (type
))
6072 error ("%qT is not a template type", type
);
6076 tmpl
= TYPE_TI_TEMPLATE (type
);
6077 if (!PRIMARY_TEMPLATE_P (tmpl
))
6078 /* The type is nested in some template class. Nothing to worry
6079 about here; there are no new template parameters for the nested
6085 error ("template specifiers not specified in declaration of %qD",
6090 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6091 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6093 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6095 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6096 "redeclared with %d template parameter",
6097 "redeclared with %d template parameters",
6098 TREE_VEC_LENGTH (parms
));
6099 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6100 "previous declaration %qD used %d template parameter",
6101 "previous declaration %qD used %d template parameters",
6102 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6106 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6113 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6114 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6117 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6118 if (error_operand_p (tmpl_parm
))
6121 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6122 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6123 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6125 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6127 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6128 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6129 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6130 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6131 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6132 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6133 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6134 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6135 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6137 auto_diagnostic_group d
;
6138 error ("template parameter %q+#D", tmpl_parm
);
6139 inform (input_location
, "redeclared here as %q#D", parm
);
6143 /* The parameters can be declared to introduce different
6145 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6146 tree p2
= TREE_VEC_ELT (parms
, i
);
6147 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6149 auto_diagnostic_group d
;
6150 error ("declaration of template parameter %q+#D with different "
6151 "constraints", parm
);
6152 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6153 "original declaration appeared here");
6157 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6159 /* We have in [temp.param]:
6161 A template-parameter may not be given default arguments
6162 by two different declarations in the same scope. */
6163 auto_diagnostic_group d
;
6164 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6165 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6166 "original definition appeared here");
6170 if (parm_default
!= NULL_TREE
)
6171 /* Update the previous template parameters (which are the ones
6172 that will really count) with the new default value. */
6173 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6174 else if (tmpl_default
!= NULL_TREE
)
6175 /* Update the new parameters, too; they'll be used as the
6176 parameters for any members. */
6177 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6179 /* Give each template template parm in this redeclaration a
6180 DECL_CONTEXT of the template for which they are a parameter. */
6181 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6183 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6184 DECL_CONTEXT (parm
) = tmpl
;
6187 if (TREE_CODE (parm
) == TYPE_DECL
)
6188 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6191 tree ci
= get_constraints (tmpl
);
6192 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6193 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6195 /* Two classes with different constraints declare different entities. */
6196 if (!cp_tree_equal (req1
, req2
))
6198 auto_diagnostic_group d
;
6199 error_at (input_location
, "redeclaration %q#D with different "
6200 "constraints", tmpl
);
6201 inform (DECL_SOURCE_LOCATION (tmpl
),
6202 "original declaration appeared here");
6209 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6210 to be used when the caller has already checked
6211 (processing_template_decl
6212 && !instantiation_dependent_expression_p (expr)
6213 && potential_constant_expression (expr))
6214 and cleared processing_template_decl. */
6217 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6219 return tsubst_copy_and_build (expr
,
6222 /*in_decl=*/NULL_TREE
,
6223 /*function_p=*/false,
6224 /*integral_constant_expression_p=*/true);
6227 /* Simplify EXPR if it is a non-dependent expression. Returns the
6228 (possibly simplified) expression. */
6231 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6233 if (expr
== NULL_TREE
)
6236 /* If we're in a template, but EXPR isn't value dependent, simplify
6237 it. We're supposed to treat:
6239 template <typename T> void f(T[1 + 1]);
6240 template <typename T> void f(T[2]);
6242 as two declarations of the same function, for example. */
6243 if (processing_template_decl
6244 && is_nondependent_constant_expression (expr
))
6246 processing_template_decl_sentinel s
;
6247 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6253 instantiate_non_dependent_expr (tree expr
)
6255 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6258 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6259 an uninstantiated expression. */
6262 instantiate_non_dependent_or_null (tree expr
)
6264 if (expr
== NULL_TREE
)
6266 if (processing_template_decl
)
6268 if (!is_nondependent_constant_expression (expr
))
6272 processing_template_decl_sentinel s
;
6273 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6279 /* True iff T is a specialization of a variable template. */
6282 variable_template_specialization_p (tree t
)
6284 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6286 tree tmpl
= DECL_TI_TEMPLATE (t
);
6287 return variable_template_p (tmpl
);
6290 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6291 template declaration, or a TYPE_DECL for an alias declaration. */
6294 alias_type_or_template_p (tree t
)
6298 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6301 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6302 || DECL_ALIAS_TEMPLATE_P (t
));
6305 /* If T is a specialization of an alias template, return it; otherwise return
6306 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6309 alias_template_specialization_p (const_tree t
,
6310 bool transparent_typedefs
)
6315 /* It's an alias template specialization if it's an alias and its
6316 TYPE_NAME is a specialization of a primary template. */
6317 if (typedef_variant_p (t
))
6319 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6320 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6321 return CONST_CAST_TREE (t
);
6322 if (transparent_typedefs
)
6323 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6325 transparent_typedefs
);
6331 /* An alias template is complex from a SFINAE perspective if a template-id
6332 using that alias can be ill-formed when the expansion is not, as with
6333 the void_t template. We determine this by checking whether the
6334 expansion for the alias template uses all its template parameters. */
6336 struct uses_all_template_parms_data
6343 uses_all_template_parms_r (tree t
, void *data_
)
6345 struct uses_all_template_parms_data
&data
6346 = *(struct uses_all_template_parms_data
*)data_
;
6347 tree idx
= get_template_parm_index (t
);
6349 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6350 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6354 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6357 complex_pack_expansion_r (tree t
, void *data_
)
6359 /* An alias template with a pack expansion that expands a pack from the
6360 enclosing class needs to be considered complex, to avoid confusion with
6361 the same pack being used as an argument to the alias's own template
6362 parameter (91966). */
6363 if (!PACK_EXPANSION_P (t
))
6365 struct uses_all_template_parms_data
&data
6366 = *(struct uses_all_template_parms_data
*)data_
;
6367 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6368 pack
= TREE_CHAIN (pack
))
6370 tree parm_pack
= TREE_VALUE (pack
);
6371 if (!TEMPLATE_PARM_P (parm_pack
))
6374 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6375 if (level
< data
.level
)
6382 complex_alias_template_p (const_tree tmpl
)
6384 /* A renaming alias isn't complex. */
6385 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6388 /* Any other constrained alias is complex. */
6389 if (get_constraints (tmpl
))
6392 struct uses_all_template_parms_data data
;
6393 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6394 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6395 data
.level
= TMPL_PARMS_DEPTH (parms
);
6396 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6397 data
.seen
= XALLOCAVEC (bool, len
);
6398 for (int i
= 0; i
< len
; ++i
)
6399 data
.seen
[i
] = false;
6401 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6402 NULL
, true, complex_pack_expansion_r
))
6404 for (int i
= 0; i
< len
; ++i
)
6410 /* If T is a specialization of a complex alias template with dependent
6411 template-arguments, return it; otherwise return NULL_TREE. If T is a
6412 typedef to such a specialization, return the specialization. */
6415 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6417 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6420 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6422 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6423 && (any_dependent_template_arguments_p
6424 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6425 return CONST_CAST_TREE (t
);
6427 if (transparent_typedefs
)
6429 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6430 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6436 /* Return the number of innermost template parameters in TMPL. */
6439 num_innermost_template_parms (const_tree tmpl
)
6441 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6442 return TREE_VEC_LENGTH (parms
);
6445 /* Return either TMPL or another template that it is equivalent to under DR
6446 1286: An alias that just changes the name of a template is equivalent to
6447 the other template. */
6450 get_underlying_template (tree tmpl
)
6452 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6453 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6455 /* Determine if the alias is equivalent to an underlying template. */
6456 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6457 /* The underlying type may have been ill-formed. Don't proceed. */
6460 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6464 tree underlying
= TI_TEMPLATE (tinfo
);
6465 if (!PRIMARY_TEMPLATE_P (underlying
)
6466 || (num_innermost_template_parms (tmpl
)
6467 != num_innermost_template_parms (underlying
)))
6470 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6471 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6474 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6475 it's appropriate to treat a less-constrained alias as equivalent. */
6476 if (!at_least_as_constrained (underlying
, tmpl
))
6479 /* Alias is equivalent. Strip it and repeat. */
6486 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6487 must be a reference-to-function or a pointer-to-function type, as specified
6488 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6489 and check that the resulting function has external linkage. */
6492 convert_nontype_argument_function (tree type
, tree expr
,
6493 tsubst_flags_t complain
)
6497 linkage_kind linkage
;
6499 fn
= instantiate_type (type
, fns
, tf_none
);
6500 if (fn
== error_mark_node
)
6501 return error_mark_node
;
6503 if (value_dependent_expression_p (fn
))
6506 fn_no_ptr
= strip_fnptr_conv (fn
);
6507 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6508 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6509 if (BASELINK_P (fn_no_ptr
))
6510 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6512 /* [temp.arg.nontype]/1
6514 A template-argument for a non-type, non-template template-parameter
6517 -- the address of an object or function with external [C++11: or
6518 internal] linkage. */
6520 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6521 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6523 if (complain
& tf_error
)
6525 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6526 error_at (loc
, "%qE is not a valid template argument for type %qT",
6528 if (TYPE_PTR_P (type
))
6529 inform (loc
, "it must be the address of a function "
6530 "with external linkage");
6532 inform (loc
, "it must be the name of a function with "
6533 "external linkage");
6538 linkage
= decl_linkage (fn_no_ptr
);
6539 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6541 if (complain
& tf_error
)
6543 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6544 if (cxx_dialect
>= cxx11
)
6545 error_at (loc
, "%qE is not a valid template argument for type "
6546 "%qT because %qD has no linkage",
6547 expr
, type
, fn_no_ptr
);
6549 error_at (loc
, "%qE is not a valid template argument for type "
6550 "%qT because %qD does not have external linkage",
6551 expr
, type
, fn_no_ptr
);
6557 if (TYPE_REF_P (type
))
6559 if (REFERENCE_REF_P (fn
))
6560 fn
= TREE_OPERAND (fn
, 0);
6562 fn
= build_address (fn
);
6564 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6565 fn
= build_nop (type
, fn
);
6570 /* Subroutine of convert_nontype_argument.
6571 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6572 Emit an error otherwise. */
6575 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6576 tsubst_flags_t complain
)
6578 tree orig_expr
= expr
;
6580 if (null_ptr_cst_p (expr
))
6582 if (TREE_CODE (expr
) == PTRMEM_CST
6583 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6584 PTRMEM_CST_CLASS (expr
)))
6586 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6588 if (processing_template_decl
6589 && TREE_CODE (expr
) == ADDR_EXPR
6590 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6592 if (complain
& tf_error
)
6594 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6595 error_at (loc
, "%qE is not a valid template argument for type %qT",
6597 if (TREE_CODE (expr
) != PTRMEM_CST
)
6598 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6600 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6605 /* Returns TRUE iff the address of OP is value-dependent.
6607 14.6.2.4 [temp.dep.temp]:
6608 A non-integral non-type template-argument is dependent if its type is
6609 dependent or it has either of the following forms
6612 and contains a nested-name-specifier which specifies a class-name that
6613 names a dependent type.
6615 We generalize this to just say that the address of a member of a
6616 dependent class is value-dependent; the above doesn't cover the
6617 address of a static data member named with an unqualified-id. */
6620 has_value_dependent_address (tree op
)
6622 STRIP_ANY_LOCATION_WRAPPER (op
);
6624 /* We could use get_inner_reference here, but there's no need;
6625 this is only relevant for template non-type arguments, which
6626 can only be expressed as &id-expression. */
6629 tree ctx
= CP_DECL_CONTEXT (op
);
6630 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6637 /* The next set of functions are used for providing helpful explanatory
6638 diagnostics for failed overload resolution. Their messages should be
6639 indented by two spaces for consistency with the messages in
6643 unify_success (bool /*explain_p*/)
6648 /* Other failure functions should call this one, to provide a single function
6649 for setting a breakpoint on. */
6652 unify_invalid (bool /*explain_p*/)
6658 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6661 inform (input_location
,
6662 " couldn%'t deduce template parameter %qD", parm
);
6663 return unify_invalid (explain_p
);
6667 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6670 inform (input_location
,
6671 " types %qT and %qT have incompatible cv-qualifiers",
6673 return unify_invalid (explain_p
);
6677 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6680 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6681 return unify_invalid (explain_p
);
6685 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6688 inform (input_location
,
6689 " template parameter %qD is not a parameter pack, but "
6692 return unify_invalid (explain_p
);
6696 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6699 inform (input_location
,
6700 " template argument %qE does not match "
6701 "pointer-to-member constant %qE",
6703 return unify_invalid (explain_p
);
6707 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6710 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6711 return unify_invalid (explain_p
);
6715 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6718 inform (input_location
,
6719 " inconsistent parameter pack deduction with %qT and %qT",
6721 return unify_invalid (explain_p
);
6725 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6730 inform (input_location
,
6731 " deduced conflicting types for parameter %qT (%qT and %qT)",
6732 parm
, first
, second
);
6734 inform (input_location
,
6735 " deduced conflicting values for non-type parameter "
6736 "%qE (%qE and %qE)", parm
, first
, second
);
6738 return unify_invalid (explain_p
);
6742 unify_vla_arg (bool explain_p
, tree arg
)
6745 inform (input_location
,
6746 " variable-sized array type %qT is not "
6747 "a valid template argument",
6749 return unify_invalid (explain_p
);
6753 unify_method_type_error (bool explain_p
, tree arg
)
6756 inform (input_location
,
6757 " member function type %qT is not a valid template argument",
6759 return unify_invalid (explain_p
);
6763 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6768 inform_n (input_location
, wanted
,
6769 " candidate expects at least %d argument, %d provided",
6770 " candidate expects at least %d arguments, %d provided",
6773 inform_n (input_location
, wanted
,
6774 " candidate expects %d argument, %d provided",
6775 " candidate expects %d arguments, %d provided",
6778 return unify_invalid (explain_p
);
6782 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6784 return unify_arity (explain_p
, have
, wanted
);
6788 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6789 bool least_p
= false)
6791 return unify_arity (explain_p
, have
, wanted
, least_p
);
6795 unify_arg_conversion (bool explain_p
, tree to_type
,
6796 tree from_type
, tree arg
)
6799 inform (cp_expr_loc_or_input_loc (arg
),
6800 " cannot convert %qE (type %qT) to type %qT",
6801 arg
, from_type
, to_type
);
6802 return unify_invalid (explain_p
);
6806 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6807 tree parm
, tree arg
)
6812 case tbr_ambiguous_baseclass
:
6813 inform (input_location
, " %qT is an ambiguous base class of %qT",
6817 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6820 return unify_invalid (explain_p
);
6824 unify_inconsistent_template_template_parameters (bool explain_p
)
6827 inform (input_location
,
6828 " template parameters of a template template argument are "
6829 "inconsistent with other deduced template arguments");
6830 return unify_invalid (explain_p
);
6834 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6837 inform (input_location
,
6838 " cannot deduce a template for %qT from non-template type %qT",
6840 return unify_invalid (explain_p
);
6844 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6847 inform (input_location
,
6848 " template argument %qE does not match %qE", arg
, parm
);
6849 return unify_invalid (explain_p
);
6852 /* True if T is a C++20 template parameter object to store the argument for a
6853 template parameter of class type. */
6856 template_parm_object_p (const_tree t
)
6858 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6859 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6862 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6863 argument for TYPE, points to an unsuitable object. */
6866 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6868 switch (TREE_CODE (expr
))
6871 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6875 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6880 unsigned i
; tree elt
;
6881 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6882 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6889 tree decl
= TREE_OPERAND (expr
, 0);
6893 if (complain
& tf_error
)
6894 error_at (cp_expr_loc_or_input_loc (expr
),
6895 "%qE is not a valid template argument of type %qT "
6896 "because %qE is not a variable", expr
, type
, decl
);
6899 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6901 if (complain
& tf_error
)
6902 error_at (cp_expr_loc_or_input_loc (expr
),
6903 "%qE is not a valid template argument of type %qT "
6904 "in C++98 because %qD does not have external linkage",
6908 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6909 && decl_linkage (decl
) == lk_none
)
6911 if (complain
& tf_error
)
6912 error_at (cp_expr_loc_or_input_loc (expr
),
6913 "%qE is not a valid template argument of type %qT "
6914 "because %qD has no linkage", expr
, type
, decl
);
6917 /* C++17: For a non-type template-parameter of reference or pointer
6918 type, the value of the constant expression shall not refer to (or
6919 for a pointer type, shall not be the address of):
6920 * a subobject (4.5),
6921 * a temporary object (15.2),
6922 * a string literal (5.13.5),
6923 * the result of a typeid expression (8.2.8), or
6924 * a predefined __func__ variable (11.4.1). */
6925 else if (DECL_ARTIFICIAL (decl
))
6927 if (complain
& tf_error
)
6928 error ("the address of %qD is not a valid template argument",
6932 else if (!same_type_ignoring_top_level_qualifiers_p
6933 (strip_array_types (TREE_TYPE (type
)),
6934 strip_array_types (TREE_TYPE (decl
))))
6936 if (complain
& tf_error
)
6937 error ("the address of the %qT subobject of %qD is not a "
6938 "valid template argument", TREE_TYPE (type
), decl
);
6941 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6943 if (complain
& tf_error
)
6944 error ("the address of %qD is not a valid template argument "
6945 "because it does not have static storage duration",
6953 if (!INDIRECT_TYPE_P (type
))
6954 /* We're only concerned about pointers and references here. */;
6955 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6956 /* Null pointer values are OK in C++11. */;
6961 if (complain
& tf_error
)
6962 error ("%qD is not a valid template argument "
6963 "because %qD is a variable, not the address of "
6964 "a variable", expr
, expr
);
6969 if (complain
& tf_error
)
6970 error ("%qE is not a valid template argument for %qT "
6971 "because it is not the address of a variable",
6981 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6982 template argument EXPR. */
6985 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6987 if (TREE_CODE (expr
) == TARGET_EXPR
)
6988 expr
= TARGET_EXPR_INITIAL (expr
);
6990 if (!TREE_CONSTANT (expr
))
6992 if ((complain
& tf_error
)
6993 && require_rvalue_constant_expression (expr
))
6994 cxx_constant_value (expr
);
6995 return error_mark_node
;
6997 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6998 return error_mark_node
;
7000 tree name
= mangle_template_parm_object (expr
);
7001 tree decl
= get_global_binding (name
);
7005 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7006 decl
= create_temporary_var (type
);
7007 TREE_STATIC (decl
) = true;
7008 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7009 TREE_READONLY (decl
) = true;
7010 DECL_NAME (decl
) = name
;
7011 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7012 DECL_CONTEXT (decl
) = global_namespace
;
7013 comdat_linkage (decl
);
7014 pushdecl_top_level_and_finish (decl
, expr
);
7018 /* Attempt to convert the non-type template parameter EXPR to the
7019 indicated TYPE. If the conversion is successful, return the
7020 converted value. If the conversion is unsuccessful, return
7021 NULL_TREE if we issued an error message, or error_mark_node if we
7022 did not. We issue error messages for out-and-out bad template
7023 parameters, but not simply because the conversion failed, since we
7024 might be just trying to do argument deduction. Both TYPE and EXPR
7025 must be non-dependent.
7027 The conversion follows the special rules described in
7028 [temp.arg.nontype], and it is much more strict than an implicit
7031 This function is called twice for each template argument (see
7032 lookup_template_class for a more accurate description of this
7033 problem). This means that we need to handle expressions which
7034 are not valid in a C++ source, but can be created from the
7035 first call (for instance, casts to perform conversions). These
7036 hacks can go away after we fix the double coercion problem. */
7039 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7042 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7044 /* Detect immediately string literals as invalid non-type argument.
7045 This special-case is not needed for correctness (we would easily
7046 catch this later), but only to provide better diagnostic for this
7047 common user mistake. As suggested by DR 100, we do not mention
7048 linkage issues in the diagnostic as this is not the point. */
7049 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7051 if (complain
& tf_error
)
7052 error ("%qE is not a valid template argument for type %qT "
7053 "because string literals can never be used in this context",
7058 /* Add the ADDR_EXPR now for the benefit of
7059 value_dependent_expression_p. */
7060 if (TYPE_PTROBV_P (type
)
7061 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7063 expr
= decay_conversion (expr
, complain
);
7064 if (expr
== error_mark_node
)
7065 return error_mark_node
;
7068 /* If we are in a template, EXPR may be non-dependent, but still
7069 have a syntactic, rather than semantic, form. For example, EXPR
7070 might be a SCOPE_REF, rather than the VAR_DECL to which the
7071 SCOPE_REF refers. Preserving the qualifying scope is necessary
7072 so that access checking can be performed when the template is
7073 instantiated -- but here we need the resolved form so that we can
7074 convert the argument. */
7075 bool non_dep
= false;
7076 if (TYPE_REF_OBJ_P (type
)
7077 && has_value_dependent_address (expr
))
7078 /* If we want the address and it's value-dependent, don't fold. */;
7079 else if (processing_template_decl
7080 && is_nondependent_constant_expression (expr
))
7082 if (error_operand_p (expr
))
7083 return error_mark_node
;
7084 expr_type
= TREE_TYPE (expr
);
7086 /* If the argument is non-dependent, perform any conversions in
7087 non-dependent context as well. */
7088 processing_template_decl_sentinel
s (non_dep
);
7090 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7092 const bool val_dep_p
= value_dependent_expression_p (expr
);
7094 expr
= canonicalize_expr_argument (expr
, complain
);
7096 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7097 to a non-type argument of "nullptr". */
7098 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7099 expr
= fold_simple (convert (type
, expr
));
7101 /* In C++11, integral or enumeration non-type template arguments can be
7102 arbitrary constant expressions. Pointer and pointer to
7103 member arguments can be general constant expressions that evaluate
7104 to a null value, but otherwise still need to be of a specific form. */
7105 if (cxx_dialect
>= cxx11
)
7107 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7108 /* A PTRMEM_CST is already constant, and a valid template
7109 argument for a parameter of pointer to member type, we just want
7110 to leave it in that form rather than lower it to a
7112 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7113 || cxx_dialect
>= cxx17
)
7115 /* C++17: A template-argument for a non-type template-parameter shall
7116 be a converted constant expression (8.20) of the type of the
7117 template-parameter. */
7118 expr
= build_converted_constant_expr (type
, expr
, complain
);
7119 if (expr
== error_mark_node
)
7120 /* Make sure we return NULL_TREE only if we have really issued
7121 an error, as described above. */
7122 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7123 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7125 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7128 expr
= maybe_constant_value (expr
, NULL_TREE
,
7129 /*manifestly_const_eval=*/true);
7130 expr
= convert_from_reference (expr
);
7132 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7134 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7135 /*manifestly_const_eval=*/true);
7136 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7137 : null_member_pointer_value_p (folded
))
7142 if (TYPE_REF_P (type
))
7143 expr
= mark_lvalue_use (expr
);
7145 expr
= mark_rvalue_use (expr
);
7147 /* HACK: Due to double coercion, we can get a
7148 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7149 which is the tree that we built on the first call (see
7150 below when coercing to reference to object or to reference to
7151 function). We just strip everything and get to the arg.
7152 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7154 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7156 tree probe_type
, probe
= expr
;
7157 if (REFERENCE_REF_P (probe
))
7158 probe
= TREE_OPERAND (probe
, 0);
7159 probe_type
= TREE_TYPE (probe
);
7160 if (TREE_CODE (probe
) == NOP_EXPR
)
7162 /* ??? Maybe we could use convert_from_reference here, but we
7163 would need to relax its constraints because the NOP_EXPR
7164 could actually change the type to something more cv-qualified,
7165 and this is not folded by convert_from_reference. */
7166 tree addr
= TREE_OPERAND (probe
, 0);
7167 if (TYPE_REF_P (probe_type
)
7168 && TREE_CODE (addr
) == ADDR_EXPR
7169 && TYPE_PTR_P (TREE_TYPE (addr
))
7170 && (same_type_ignoring_top_level_qualifiers_p
7171 (TREE_TYPE (probe_type
),
7172 TREE_TYPE (TREE_TYPE (addr
)))))
7174 expr
= TREE_OPERAND (addr
, 0);
7175 expr_type
= TREE_TYPE (probe_type
);
7180 /* [temp.arg.nontype]/5, bullet 1
7182 For a non-type template-parameter of integral or enumeration type,
7183 integral promotions (_conv.prom_) and integral conversions
7184 (_conv.integral_) are applied. */
7185 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7187 if (cxx_dialect
< cxx11
)
7189 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7190 t
= maybe_constant_value (t
);
7191 if (t
!= error_mark_node
)
7195 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7196 return error_mark_node
;
7198 /* Notice that there are constant expressions like '4 % 0' which
7199 do not fold into integer constants. */
7200 if (TREE_CODE (expr
) != INTEGER_CST
&& !val_dep_p
)
7202 if (complain
& tf_error
)
7204 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7205 if (!require_potential_constant_expression (expr
))
7206 expr
= error_mark_node
;
7208 expr
= cxx_constant_value (expr
);
7209 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7210 inform (loc
, "in template argument for type %qT", type
);
7211 if (expr
== error_mark_node
)
7213 /* else cxx_constant_value complained but gave us
7214 a real constant, so go ahead. */
7215 if (TREE_CODE (expr
) != INTEGER_CST
)
7217 /* Some assemble time constant expressions like
7218 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7219 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7220 as we can emit them into .rodata initializers of
7221 variables, yet they can't fold into an INTEGER_CST at
7222 compile time. Refuse them here. */
7223 gcc_checking_assert (reduced_constant_expression_p (expr
));
7224 error_at (loc
, "template argument %qE for type %qT not "
7225 "a constant integer", expr
, type
);
7233 /* Avoid typedef problems. */
7234 if (TREE_TYPE (expr
) != type
)
7235 expr
= fold_convert (type
, expr
);
7237 /* [temp.arg.nontype]/5, bullet 2
7239 For a non-type template-parameter of type pointer to object,
7240 qualification conversions (_conv.qual_) and the array-to-pointer
7241 conversion (_conv.array_) are applied. */
7242 else if (TYPE_PTROBV_P (type
))
7244 tree decayed
= expr
;
7246 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7247 decay_conversion or an explicit cast. If it's a problematic cast,
7248 we'll complain about it below. */
7249 if (TREE_CODE (expr
) == NOP_EXPR
)
7253 if (TREE_CODE (probe
) == ADDR_EXPR
7254 && TYPE_PTR_P (TREE_TYPE (probe
)))
7257 expr_type
= TREE_TYPE (expr
);
7261 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7263 A template-argument for a non-type, non-template template-parameter
7264 shall be one of: [...]
7266 -- the name of a non-type template-parameter;
7267 -- the address of an object or function with external linkage, [...]
7268 expressed as "& id-expression" where the & is optional if the name
7269 refers to a function or array, or if the corresponding
7270 template-parameter is a reference.
7272 Here, we do not care about functions, as they are invalid anyway
7273 for a parameter of type pointer-to-object. */
7276 /* Non-type template parameters are OK. */
7278 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7279 /* Null pointer values are OK in C++11. */;
7280 else if (TREE_CODE (expr
) != ADDR_EXPR
7281 && !INDIRECT_TYPE_P (expr_type
))
7282 /* Other values, like integer constants, might be valid
7283 non-type arguments of some other type. */
7284 return error_mark_node
;
7285 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7290 expr
= perform_qualification_conversions (type
, expr
);
7291 if (expr
== error_mark_node
)
7292 return error_mark_node
;
7294 /* [temp.arg.nontype]/5, bullet 3
7296 For a non-type template-parameter of type reference to object, no
7297 conversions apply. The type referred to by the reference may be more
7298 cv-qualified than the (otherwise identical) type of the
7299 template-argument. The template-parameter is bound directly to the
7300 template-argument, which must be an lvalue. */
7301 else if (TYPE_REF_OBJ_P (type
))
7303 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7305 return error_mark_node
;
7307 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7309 if (complain
& tf_error
)
7310 error ("%qE is not a valid template argument for type %qT "
7311 "because of conflicts in cv-qualification", expr
, type
);
7315 if (!lvalue_p (expr
))
7317 if (complain
& tf_error
)
7318 error ("%qE is not a valid template argument for type %qT "
7319 "because it is not an lvalue", expr
, type
);
7323 /* [temp.arg.nontype]/1
7325 A template-argument for a non-type, non-template template-parameter
7326 shall be one of: [...]
7328 -- the address of an object or function with external linkage. */
7329 if (INDIRECT_REF_P (expr
)
7330 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7332 expr
= TREE_OPERAND (expr
, 0);
7335 if (complain
& tf_error
)
7336 error ("%q#D is not a valid template argument for type %qT "
7337 "because a reference variable does not have a constant "
7338 "address", expr
, type
);
7343 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7344 /* OK, dependent reference. We don't want to ask whether a DECL is
7345 itself value-dependent, since what we want here is its address. */;
7348 expr
= build_address (expr
);
7350 if (invalid_tparm_referent_p (type
, expr
, complain
))
7354 if (!same_type_p (type
, TREE_TYPE (expr
)))
7355 expr
= build_nop (type
, expr
);
7357 /* [temp.arg.nontype]/5, bullet 4
7359 For a non-type template-parameter of type pointer to function, only
7360 the function-to-pointer conversion (_conv.func_) is applied. If the
7361 template-argument represents a set of overloaded functions (or a
7362 pointer to such), the matching function is selected from the set
7364 else if (TYPE_PTRFN_P (type
))
7366 /* If the argument is a template-id, we might not have enough
7367 context information to decay the pointer. */
7368 if (!type_unknown_p (expr_type
))
7370 expr
= decay_conversion (expr
, complain
);
7371 if (expr
== error_mark_node
)
7372 return error_mark_node
;
7375 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7376 /* Null pointer values are OK in C++11. */
7377 return perform_qualification_conversions (type
, expr
);
7379 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7380 if (!expr
|| expr
== error_mark_node
)
7383 /* [temp.arg.nontype]/5, bullet 5
7385 For a non-type template-parameter of type reference to function, no
7386 conversions apply. If the template-argument represents a set of
7387 overloaded functions, the matching function is selected from the set
7389 else if (TYPE_REFFN_P (type
))
7391 if (TREE_CODE (expr
) == ADDR_EXPR
)
7393 if (complain
& tf_error
)
7395 error ("%qE is not a valid template argument for type %qT "
7396 "because it is a pointer", expr
, type
);
7397 inform (input_location
, "try using %qE instead",
7398 TREE_OPERAND (expr
, 0));
7403 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7404 if (!expr
|| expr
== error_mark_node
)
7407 /* [temp.arg.nontype]/5, bullet 6
7409 For a non-type template-parameter of type pointer to member function,
7410 no conversions apply. If the template-argument represents a set of
7411 overloaded member functions, the matching member function is selected
7412 from the set (_over.over_). */
7413 else if (TYPE_PTRMEMFUNC_P (type
))
7415 expr
= instantiate_type (type
, expr
, tf_none
);
7416 if (expr
== error_mark_node
)
7417 return error_mark_node
;
7419 /* [temp.arg.nontype] bullet 1 says the pointer to member
7420 expression must be a pointer-to-member constant. */
7422 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7425 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7426 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7427 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7428 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7430 /* [temp.arg.nontype]/5, bullet 7
7432 For a non-type template-parameter of type pointer to data member,
7433 qualification conversions (_conv.qual_) are applied. */
7434 else if (TYPE_PTRDATAMEM_P (type
))
7436 /* [temp.arg.nontype] bullet 1 says the pointer to member
7437 expression must be a pointer-to-member constant. */
7439 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7442 expr
= perform_qualification_conversions (type
, expr
);
7443 if (expr
== error_mark_node
)
7446 else if (NULLPTR_TYPE_P (type
))
7448 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7450 if (complain
& tf_error
)
7451 error ("%qE is not a valid template argument for type %qT "
7452 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7457 else if (CLASS_TYPE_P (type
))
7459 /* Replace the argument with a reference to the corresponding template
7460 parameter object. */
7462 expr
= get_template_parm_object (expr
, complain
);
7463 if (expr
== error_mark_node
)
7466 /* A template non-type parameter must be one of the above. */
7470 /* Sanity check: did we actually convert the argument to the
7472 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7473 (type
, TREE_TYPE (expr
)));
7474 return convert_from_reference (expr
);
7477 /* Subroutine of coerce_template_template_parms, which returns 1 if
7478 PARM_PARM and ARG_PARM match using the rule for the template
7479 parameters of template template parameters. Both PARM and ARG are
7480 template parameters; the rest of the arguments are the same as for
7481 coerce_template_template_parms.
7484 coerce_template_template_parm (tree parm
,
7486 tsubst_flags_t complain
,
7490 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7491 || parm
== NULL_TREE
|| error_operand_p (parm
))
7494 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7497 switch (TREE_CODE (parm
))
7500 /* We encounter instantiations of templates like
7501 template <template <template <class> class> class TT>
7504 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7505 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7507 if (!coerce_template_template_parms
7508 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7514 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7515 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7516 /* Argument is a parameter pack but parameter is not. */
7521 /* The tsubst call is used to handle cases such as
7523 template <int> class C {};
7524 template <class T, template <T> class TT> class D {};
7527 i.e. the parameter list of TT depends on earlier parameters. */
7528 if (!uses_template_parms (TREE_TYPE (arg
)))
7530 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7531 if (!uses_template_parms (t
)
7532 && !same_type_p (t
, TREE_TYPE (arg
)))
7536 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7537 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7538 /* Argument is a parameter pack but parameter is not. */
7550 /* Coerce template argument list ARGLIST for use with template
7551 template-parameter TEMPL. */
7554 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7555 tsubst_flags_t complain
)
7557 /* Consider an example where a template template parameter declared as
7559 template <class T, class U = std::allocator<T> > class TT
7561 The template parameter level of T and U are one level larger than
7562 of TT. To proper process the default argument of U, say when an
7563 instantiation `TT<int>' is seen, we need to build the full
7564 arguments containing {int} as the innermost level. Outer levels,
7565 available when not appearing as default template argument, can be
7566 obtained from the arguments of the enclosing template.
7568 Suppose that TT is later substituted with std::vector. The above
7569 instantiation is `TT<int, std::allocator<T> >' with TT at
7570 level 1, and T at level 2, while the template arguments at level 1
7571 becomes {std::vector} and the inner level 2 is {int}. */
7573 tree outer
= DECL_CONTEXT (templ
);
7575 outer
= generic_targs_for (outer
);
7576 else if (current_template_parms
)
7578 /* This is an argument of the current template, so we haven't set
7579 DECL_CONTEXT yet. */
7580 tree relevant_template_parms
;
7582 /* Parameter levels that are greater than the level of the given
7583 template template parm are irrelevant. */
7584 relevant_template_parms
= current_template_parms
;
7585 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7586 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7587 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7589 outer
= template_parms_to_args (relevant_template_parms
);
7593 arglist
= add_to_template_args (outer
, arglist
);
7595 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7596 return coerce_template_parms (parmlist
, arglist
, templ
,
7598 /*require_all_args=*/true,
7599 /*use_default_args=*/true);
7602 /* A cache of template template parameters with match-all default
7604 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7606 /* T is a bound template template-parameter. Copy its arguments into default
7607 arguments of the template template-parameter's template parameters. */
7610 add_defaults_to_ttp (tree otmpl
)
7612 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7615 tree ntmpl
= copy_node (otmpl
);
7617 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7618 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7619 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7620 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7621 TYPE_NAME (ntype
) = ntmpl
;
7622 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7624 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7625 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7626 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7627 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7629 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7630 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7631 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7632 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7633 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7635 tree o
= TREE_VEC_ELT (vec
, i
);
7636 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7638 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7639 TREE_PURPOSE (n
) = any_targ_node
;
7643 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7647 /* ARG is a bound potential template template-argument, and PARGS is a list
7648 of arguments for the corresponding template template-parameter. Adjust
7649 PARGS as appropriate for application to ARG's template, and if ARG is a
7650 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7651 arguments to the template template parameter. */
7654 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7656 ++processing_template_decl
;
7657 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7658 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7660 /* When comparing two template template-parameters in partial ordering,
7661 rewrite the one currently being used as an argument to have default
7662 arguments for all parameters. */
7663 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7664 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7665 if (pargs
!= error_mark_node
)
7666 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7667 TYPE_TI_ARGS (arg
));
7672 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7673 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7674 /*require_all*/true,
7675 /*use_default*/true);
7677 --processing_template_decl
;
7681 /* Subroutine of unify for the case when PARM is a
7682 BOUND_TEMPLATE_TEMPLATE_PARM. */
7685 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7688 tree parmvec
= TYPE_TI_ARGS (parm
);
7689 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7691 /* The template template parm might be variadic and the argument
7692 not, so flatten both argument lists. */
7693 parmvec
= expand_template_argument_pack (parmvec
);
7694 argvec
= expand_template_argument_pack (argvec
);
7698 /* In keeping with P0522R0, adjust P's template arguments
7699 to apply to A's template; then flatten it again. */
7700 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7701 nparmvec
= expand_template_argument_pack (nparmvec
);
7703 if (unify (tparms
, targs
, nparmvec
, argvec
,
7704 UNIFY_ALLOW_NONE
, explain_p
))
7707 /* If the P0522 adjustment eliminated a pack expansion, deduce
7710 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7711 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7712 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7717 /* Deduce arguments T, i from TT<T> or TT<i>.
7718 We check each element of PARMVEC and ARGVEC individually
7719 rather than the whole TREE_VEC since they can have
7720 different number of elements, which is allowed under N2555. */
7722 int len
= TREE_VEC_LENGTH (parmvec
);
7724 /* Check if the parameters end in a pack, making them
7726 int parm_variadic_p
= 0;
7728 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7729 parm_variadic_p
= 1;
7731 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7732 /* If the template argument list of P contains a pack
7733 expansion that is not the last template argument, the
7734 entire template argument list is a non-deduced
7736 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7737 return unify_success (explain_p
);
7739 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7740 return unify_too_few_arguments (explain_p
,
7741 TREE_VEC_LENGTH (argvec
), len
);
7743 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7744 if (unify (tparms
, targs
,
7745 TREE_VEC_ELT (parmvec
, i
),
7746 TREE_VEC_ELT (argvec
, i
),
7747 UNIFY_ALLOW_NONE
, explain_p
))
7751 && unify_pack_expansion (tparms
, targs
,
7754 /*subr=*/true, explain_p
))
7761 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7762 template template parameters. Both PARM_PARMS and ARG_PARMS are
7763 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7766 Consider the example:
7767 template <class T> class A;
7768 template<template <class U> class TT> class B;
7770 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7771 the parameters to A, and OUTER_ARGS contains A. */
7774 coerce_template_template_parms (tree parm_parms
,
7776 tsubst_flags_t complain
,
7780 int nparms
, nargs
, i
;
7784 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7785 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7787 nparms
= TREE_VEC_LENGTH (parm_parms
);
7788 nargs
= TREE_VEC_LENGTH (arg_parms
);
7792 /* P0522R0: A template template-parameter P is at least as specialized as
7793 a template template-argument A if, given the following rewrite to two
7794 function templates, the function template corresponding to P is at
7795 least as specialized as the function template corresponding to A
7796 according to the partial ordering rules for function templates
7797 ([temp.func.order]). Given an invented class template X with the
7798 template parameter list of A (including default arguments):
7800 * Each of the two function templates has the same template parameters,
7801 respectively, as P or A.
7803 * Each function template has a single function parameter whose type is
7804 a specialization of X with template arguments corresponding to the
7805 template parameters from the respective function template where, for
7806 each template parameter PP in the template parameter list of the
7807 function template, a corresponding template argument AA is formed. If
7808 PP declares a parameter pack, then AA is the pack expansion
7809 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7811 If the rewrite produces an invalid type, then P is not at least as
7812 specialized as A. */
7814 /* So coerce P's args to apply to A's parms, and then deduce between A's
7815 args and the converted args. If that succeeds, A is at least as
7816 specialized as P, so they match.*/
7817 tree pargs
= template_parms_level_to_args (parm_parms
);
7818 pargs
= add_outermost_template_args (outer_args
, pargs
);
7819 ++processing_template_decl
;
7820 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7821 /*require_all*/true, /*use_default*/true);
7822 --processing_template_decl
;
7823 if (pargs
!= error_mark_node
)
7825 tree targs
= make_tree_vec (nargs
);
7826 tree aargs
= template_parms_level_to_args (arg_parms
);
7827 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7833 /* Determine whether we have a parameter pack at the end of the
7834 template template parameter's template parameter list. */
7835 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7837 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7839 if (error_operand_p (parm
))
7842 switch (TREE_CODE (parm
))
7846 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7851 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7861 && !(variadic_p
&& nargs
>= nparms
- 1))
7864 /* Check all of the template parameters except the parameter pack at
7865 the end (if any). */
7866 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7868 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7869 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7872 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7873 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7875 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7883 /* Check each of the template parameters in the template
7884 argument against the template parameter pack at the end of
7885 the template template parameter. */
7886 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7889 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7891 for (; i
< nargs
; ++i
)
7893 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7896 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7898 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7907 /* Verifies that the deduced template arguments (in TARGS) for the
7908 template template parameters (in TPARMS) represent valid bindings,
7909 by comparing the template parameter list of each template argument
7910 to the template parameter list of its corresponding template
7911 template parameter, in accordance with DR150. This
7912 routine can only be called after all template arguments have been
7913 deduced. It will return TRUE if all of the template template
7914 parameter bindings are okay, FALSE otherwise. */
7916 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7918 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7921 /* We're dealing with template parms in this process. */
7922 ++processing_template_decl
;
7924 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7926 for (i
= 0; i
< ntparms
; ++i
)
7928 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7929 tree targ
= TREE_VEC_ELT (targs
, i
);
7931 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7933 tree packed_args
= NULL_TREE
;
7936 if (ARGUMENT_PACK_P (targ
))
7938 /* Look inside the argument pack. */
7939 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7940 len
= TREE_VEC_LENGTH (packed_args
);
7943 for (idx
= 0; idx
< len
; ++idx
)
7945 tree targ_parms
= NULL_TREE
;
7948 /* Extract the next argument from the argument
7950 targ
= TREE_VEC_ELT (packed_args
, idx
);
7952 if (PACK_EXPANSION_P (targ
))
7953 /* Look at the pattern of the pack expansion. */
7954 targ
= PACK_EXPANSION_PATTERN (targ
);
7956 /* Extract the template parameters from the template
7958 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7959 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7960 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7961 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7963 /* Verify that we can coerce the template template
7964 parameters from the template argument to the template
7965 parameter. This requires an exact match. */
7967 && !coerce_template_template_parms
7968 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7983 --processing_template_decl
;
7987 /* Since type attributes aren't mangled, we need to strip them from
7988 template type arguments. */
7991 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7993 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7995 bool removed_attributes
= false;
7996 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7997 if (removed_attributes
7998 && (complain
& tf_warning
))
7999 warning (OPT_Wignored_attributes
,
8000 "ignoring attributes on template argument %qT", arg
);
8004 /* And from inside dependent non-type arguments like sizeof(Type). */
8007 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8009 if (!arg
|| arg
== error_mark_node
)
8011 bool removed_attributes
= false;
8012 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8013 if (removed_attributes
8014 && (complain
& tf_warning
))
8015 warning (OPT_Wignored_attributes
,
8016 "ignoring attributes in template argument %qE", arg
);
8020 // A template declaration can be substituted for a constrained
8021 // template template parameter only when the argument is more
8022 // constrained than the parameter.
8024 is_compatible_template_arg (tree parm
, tree arg
)
8026 tree parm_cons
= get_constraints (parm
);
8028 /* For now, allow constrained template template arguments
8029 and unconstrained template template parameters. */
8030 if (parm_cons
== NULL_TREE
)
8033 /* If the template parameter is constrained, we need to rewrite its
8034 constraints in terms of the ARG's template parameters. This ensures
8035 that all of the template parameter types will have the same depth.
8037 Note that this is only valid when coerce_template_template_parm is
8038 true for the innermost template parameters of PARM and ARG. In other
8039 words, because coercion is successful, this conversion will be valid. */
8040 tree new_args
= NULL_TREE
;
8043 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8044 new_args
= template_parms_level_to_args (aparms
);
8045 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8046 tf_none
, NULL_TREE
);
8047 if (parm_cons
== error_mark_node
)
8051 return weakly_subsumes (parm_cons
, new_args
, arg
);
8054 // Convert a placeholder argument into a binding to the original
8055 // parameter. The original parameter is saved as the TREE_TYPE of
8058 convert_wildcard_argument (tree parm
, tree arg
)
8060 TREE_TYPE (arg
) = parm
;
8064 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8065 because one of them is dependent. But we need to represent the
8066 conversion for the benefit of cp_tree_equal. */
8069 maybe_convert_nontype_argument (tree type
, tree arg
)
8071 /* Auto parms get no conversion. */
8072 if (type_uses_auto (type
))
8074 /* We don't need or want to add this conversion now if we're going to use the
8075 argument for deduction. */
8076 if (value_dependent_expression_p (arg
))
8079 type
= cv_unqualified (type
);
8080 tree argtype
= TREE_TYPE (arg
);
8081 if (same_type_p (type
, argtype
))
8084 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8085 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8089 /* Convert the indicated template ARG as necessary to match the
8090 indicated template PARM. Returns the converted ARG, or
8091 error_mark_node if the conversion was unsuccessful. Error and
8092 warning messages are issued under control of COMPLAIN. This
8093 conversion is for the Ith parameter in the parameter list. ARGS is
8094 the full set of template arguments deduced so far. */
8097 convert_template_argument (tree parm
,
8100 tsubst_flags_t complain
,
8106 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8108 if (parm
== error_mark_node
|| error_operand_p (arg
))
8109 return error_mark_node
;
8111 /* Trivially convert placeholders. */
8112 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8113 return convert_wildcard_argument (parm
, arg
);
8115 if (arg
== any_targ_node
)
8118 if (TREE_CODE (arg
) == TREE_LIST
8119 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8121 /* The template argument was the name of some
8122 member function. That's usually
8123 invalid, but static members are OK. In any
8124 case, grab the underlying fields/functions
8125 and issue an error later if required. */
8126 TREE_TYPE (arg
) = unknown_type_node
;
8131 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8132 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8133 || requires_tmpl_type
);
8135 /* When determining whether an argument pack expansion is a template,
8136 look at the pattern. */
8137 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8138 arg
= PACK_EXPANSION_PATTERN (arg
);
8140 /* Deal with an injected-class-name used as a template template arg. */
8141 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8143 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8144 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8146 if (cxx_dialect
>= cxx11
)
8147 /* OK under DR 1004. */;
8148 else if (complain
& tf_warning_or_error
)
8149 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8150 " used as template template argument", TYPE_NAME (arg
));
8151 else if (flag_pedantic_errors
)
8159 ((TREE_CODE (arg
) == TEMPLATE_DECL
8160 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8161 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8162 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8163 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8166 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8167 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8168 arg
= TYPE_STUB_DECL (arg
);
8170 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8172 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8173 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8175 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8177 if (complain
& tf_error
)
8178 error ("invalid use of destructor %qE as a type", orig_arg
);
8179 return error_mark_node
;
8182 permerror (input_location
,
8183 "to refer to a type member of a template parameter, "
8184 "use %<typename %E%>", orig_arg
);
8186 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8187 TREE_OPERAND (arg
, 1),
8193 if (is_type
!= requires_type
)
8197 if (complain
& tf_error
)
8199 error ("type/value mismatch at argument %d in template "
8200 "parameter list for %qD",
8204 /* The template argument is a type, but we're expecting
8206 inform (input_location
,
8207 " expected a constant of type %qT, got %qT",
8209 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8210 /* [temp.arg]/2: "In a template-argument, an ambiguity
8211 between a type-id and an expression is resolved to a
8212 type-id, regardless of the form of the corresponding
8213 template-parameter." So give the user a clue. */
8214 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8215 inform (input_location
, " ambiguous template argument "
8216 "for non-type template parameter is treated as "
8219 else if (requires_tmpl_type
)
8220 inform (input_location
,
8221 " expected a class template, got %qE", orig_arg
);
8223 inform (input_location
,
8224 " expected a type, got %qE", orig_arg
);
8227 return error_mark_node
;
8229 if (is_tmpl_type
^ requires_tmpl_type
)
8231 if (in_decl
&& (complain
& tf_error
))
8233 error ("type/value mismatch at argument %d in template "
8234 "parameter list for %qD",
8237 inform (input_location
,
8238 " expected a type, got %qT", DECL_NAME (arg
));
8240 inform (input_location
,
8241 " expected a class template, got %qT", orig_arg
);
8243 return error_mark_node
;
8246 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8247 /* We already did the appropriate conversion when packing args. */
8251 if (requires_tmpl_type
)
8253 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8254 /* The number of argument required is not known yet.
8255 Just accept it for now. */
8259 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8262 /* Strip alias templates that are equivalent to another
8264 arg
= get_underlying_template (arg
);
8265 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8267 if (coerce_template_template_parms (parmparm
, argparm
,
8273 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8275 if (val
!= error_mark_node
)
8277 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8278 val
= TREE_TYPE (val
);
8279 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8280 val
= make_pack_expansion (val
, complain
);
8285 if (in_decl
&& (complain
& tf_error
))
8287 error ("type/value mismatch at argument %d in "
8288 "template parameter list for %qD",
8290 inform (input_location
,
8291 " expected a template of type %qD, got %qT",
8295 val
= error_mark_node
;
8298 // Check that the constraints are compatible before allowing the
8300 if (val
!= error_mark_node
)
8301 if (!is_compatible_template_arg (parm
, arg
))
8303 if (in_decl
&& (complain
& tf_error
))
8305 error ("constraint mismatch at argument %d in "
8306 "template parameter list for %qD",
8308 inform (input_location
, " expected %qD but got %qD",
8311 val
= error_mark_node
;
8317 /* We only form one instance of each template specialization.
8318 Therefore, if we use a non-canonical variant (i.e., a
8319 typedef), any future messages referring to the type will use
8320 the typedef, which is confusing if those future uses do not
8321 themselves also use the typedef. */
8323 val
= canonicalize_type_argument (val
, complain
);
8327 tree t
= TREE_TYPE (parm
);
8329 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8330 > TMPL_ARGS_DEPTH (args
))
8331 /* We don't have enough levels of args to do any substitution. This
8332 can happen in the context of -fnew-ttp-matching. */;
8333 else if (tree a
= type_uses_auto (t
))
8335 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8336 if (t
== error_mark_node
)
8337 return error_mark_node
;
8340 t
= tsubst (t
, args
, complain
, in_decl
);
8342 if (invalid_nontype_parm_type_p (t
, complain
))
8343 return error_mark_node
;
8345 if (t
!= TREE_TYPE (parm
))
8346 t
= canonicalize_type_argument (t
, complain
);
8348 if (!type_dependent_expression_p (orig_arg
)
8349 && !uses_template_parms (t
))
8350 /* We used to call digest_init here. However, digest_init
8351 will report errors, which we don't want when complain
8352 is zero. More importantly, digest_init will try too
8353 hard to convert things: for example, `0' should not be
8354 converted to pointer type at this point according to
8355 the standard. Accepting this is not merely an
8356 extension, since deciding whether or not these
8357 conversions can occur is part of determining which
8358 function template to call, or whether a given explicit
8359 argument specification is valid. */
8360 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8363 val
= canonicalize_expr_argument (orig_arg
, complain
);
8364 val
= maybe_convert_nontype_argument (t
, val
);
8368 if (val
== NULL_TREE
)
8369 val
= error_mark_node
;
8370 else if (val
== error_mark_node
&& (complain
& tf_error
))
8371 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8372 "could not convert template argument %qE from %qT to %qT",
8373 orig_arg
, TREE_TYPE (orig_arg
), t
);
8375 if (INDIRECT_REF_P (val
))
8377 /* Reject template arguments that are references to built-in
8378 functions with no library fallbacks. */
8379 const_tree inner
= TREE_OPERAND (val
, 0);
8380 const_tree innertype
= TREE_TYPE (inner
);
8382 && TYPE_REF_P (innertype
)
8383 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8384 && TREE_OPERAND_LENGTH (inner
) > 0
8385 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8386 return error_mark_node
;
8389 if (TREE_CODE (val
) == SCOPE_REF
)
8391 /* Strip typedefs from the SCOPE_REF. */
8392 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8393 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8395 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8396 QUALIFIED_NAME_IS_TEMPLATE (val
));
8403 /* Coerces the remaining template arguments in INNER_ARGS (from
8404 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8405 Returns the coerced argument pack. PARM_IDX is the position of this
8406 parameter in the template parameter list. ARGS is the original
8407 template argument list. */
8409 coerce_template_parameter_pack (tree parms
,
8417 tsubst_flags_t complain
)
8419 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8420 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8423 tree packed_parms
= NULL_TREE
;
8425 if (arg_idx
> nargs
)
8428 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8430 /* When the template parameter is a non-type template parameter pack
8431 or template template parameter pack whose type or template
8432 parameters use parameter packs, we know exactly how many arguments
8433 we are looking for. Build a vector of the instantiated decls for
8434 these template parameters in PACKED_PARMS. */
8435 /* We can't use make_pack_expansion here because it would interpret a
8436 _DECL as a use rather than a declaration. */
8437 tree decl
= TREE_VALUE (parm
);
8438 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8439 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8440 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8441 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8443 TREE_VEC_LENGTH (args
)--;
8444 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8445 TREE_VEC_LENGTH (args
)++;
8447 if (packed_parms
== error_mark_node
)
8448 return error_mark_node
;
8450 /* If we're doing a partial instantiation of a member template,
8451 verify that all of the types used for the non-type
8452 template parameter pack are, in fact, valid for non-type
8453 template parameters. */
8455 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8457 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8458 for (j
= 0; j
< len
; ++j
)
8460 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8461 if (TREE_CODE (t
) == PARM_DECL
8462 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8463 return error_mark_node
;
8465 /* We don't know how many args we have yet, just
8466 use the unconverted ones for now. */
8470 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8472 /* Check if we have a placeholder pack, which indicates we're
8473 in the context of a introduction list. In that case we want
8474 to match this pack to the single placeholder. */
8475 else if (arg_idx
< nargs
8476 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8477 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8479 nargs
= arg_idx
+ 1;
8480 packed_args
= make_tree_vec (1);
8483 packed_args
= make_tree_vec (nargs
- arg_idx
);
8485 /* Convert the remaining arguments, which will be a part of the
8486 parameter pack "parm". */
8487 int first_pack_arg
= arg_idx
;
8488 for (; arg_idx
< nargs
; ++arg_idx
)
8490 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8491 tree actual_parm
= TREE_VALUE (parm
);
8492 int pack_idx
= arg_idx
- first_pack_arg
;
8496 /* Once we've packed as many args as we have types, stop. */
8497 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8499 else if (PACK_EXPANSION_P (arg
))
8500 /* We don't know how many args we have yet, just
8501 use the unconverted ones for now. */
8504 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8507 if (arg
== error_mark_node
)
8509 if (complain
& tf_error
)
8510 error ("template argument %d is invalid", arg_idx
+ 1);
8513 arg
= convert_template_argument (actual_parm
,
8514 arg
, new_args
, complain
, parm_idx
,
8516 if (arg
== error_mark_node
)
8518 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8521 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8522 && TREE_VEC_LENGTH (packed_args
) > 0)
8524 if (complain
& tf_error
)
8525 error ("wrong number of template arguments (%d, should be %d)",
8526 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8527 return error_mark_node
;
8530 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8531 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8532 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8535 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8536 TREE_CONSTANT (argument_pack
) = 1;
8539 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8541 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8542 TREE_VEC_LENGTH (packed_args
));
8543 return argument_pack
;
8546 /* Returns the number of pack expansions in the template argument vector
8550 pack_expansion_args_count (tree args
)
8555 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8557 tree elt
= TREE_VEC_ELT (args
, i
);
8558 if (elt
&& PACK_EXPANSION_P (elt
))
8564 /* Convert all template arguments to their appropriate types, and
8565 return a vector containing the innermost resulting template
8566 arguments. If any error occurs, return error_mark_node. Error and
8567 warning messages are issued under control of COMPLAIN.
8569 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8570 for arguments not specified in ARGS. Otherwise, if
8571 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8572 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8573 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8577 coerce_template_parms (tree parms
,
8580 tsubst_flags_t complain
,
8581 bool require_all_args
,
8582 bool use_default_args
)
8584 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8585 tree orig_inner_args
;
8588 tree new_inner_args
;
8590 /* When used as a boolean value, indicates whether this is a
8591 variadic template parameter list. Since it's an int, we can also
8592 subtract it from nparms to get the number of non-variadic
8595 int variadic_args_p
= 0;
8596 int post_variadic_parms
= 0;
8598 /* Adjustment to nparms for fixed parameter packs. */
8599 int fixed_pack_adjust
= 0;
8600 int fixed_packs
= 0;
8603 /* Likewise for parameters with default arguments. */
8606 if (args
== error_mark_node
)
8607 return error_mark_node
;
8609 nparms
= TREE_VEC_LENGTH (parms
);
8611 /* Determine if there are any parameter packs or default arguments. */
8612 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8614 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8616 ++post_variadic_parms
;
8617 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8619 if (TREE_PURPOSE (parm
))
8623 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8624 /* If there are no parameters that follow a parameter pack, we need to
8625 expand any argument packs so that we can deduce a parameter pack from
8626 some non-packed args followed by an argument pack, as in variadic85.C.
8627 If there are such parameters, we need to leave argument packs intact
8628 so the arguments are assigned properly. This can happen when dealing
8629 with a nested class inside a partial specialization of a class
8630 template, as in variadic92.C, or when deducing a template parameter pack
8631 from a sub-declarator, as in variadic114.C. */
8632 if (!post_variadic_parms
)
8633 inner_args
= expand_template_argument_pack (inner_args
);
8635 /* Count any pack expansion args. */
8636 variadic_args_p
= pack_expansion_args_count (inner_args
);
8638 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8639 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8640 || (nargs
< nparms
- variadic_p
8643 && (!use_default_args
8644 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8645 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8648 if (complain
& tf_error
)
8650 if (variadic_p
|| default_p
)
8652 nparms
-= variadic_p
+ default_p
;
8653 error ("wrong number of template arguments "
8654 "(%d, should be at least %d)", nargs
, nparms
);
8657 error ("wrong number of template arguments "
8658 "(%d, should be %d)", nargs
, nparms
);
8661 inform (DECL_SOURCE_LOCATION (in_decl
),
8662 "provided for %qD", in_decl
);
8665 return error_mark_node
;
8667 /* We can't pass a pack expansion to a non-pack parameter of an alias
8668 template (DR 1430). */
8670 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8671 || concept_definition_p (in_decl
))
8673 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8675 if (complain
& tf_error
)
8677 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8679 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8680 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8682 if (PACK_EXPANSION_P (arg
)
8683 && !template_parameter_pack_p (parm
))
8685 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8686 error_at (location_of (arg
),
8687 "pack expansion argument for non-pack parameter "
8688 "%qD of alias template %qD", parm
, in_decl
);
8690 error_at (location_of (arg
),
8691 "pack expansion argument for non-pack parameter "
8692 "%qD of concept %qD", parm
, in_decl
);
8693 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8700 return error_mark_node
;
8703 /* We need to evaluate the template arguments, even though this
8704 template-id may be nested within a "sizeof". */
8707 new_inner_args
= make_tree_vec (nparms
);
8708 new_args
= add_outermost_template_args (args
, new_inner_args
);
8709 int pack_adjust
= 0;
8710 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8715 /* Get the Ith template parameter. */
8716 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8718 if (parm
== error_mark_node
)
8720 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8724 /* Calculate the next argument. */
8725 if (arg_idx
< nargs
)
8726 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8730 if (template_parameter_pack_p (TREE_VALUE (parm
))
8731 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8732 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8734 /* Some arguments will be placed in the
8735 template parameter pack PARM. */
8736 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8737 inner_args
, arg_idx
,
8741 if (arg
== NULL_TREE
)
8743 /* We don't know how many args we have yet, just use the
8744 unconverted (and still packed) ones for now. */
8745 new_inner_args
= orig_inner_args
;
8750 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8752 /* Store this argument. */
8753 if (arg
== error_mark_node
)
8756 /* We are done with all of the arguments. */
8762 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8763 arg_idx
+= pack_adjust
;
8764 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8767 fixed_pack_adjust
+= pack_adjust
;
8775 if (PACK_EXPANSION_P (arg
))
8777 /* "If every valid specialization of a variadic template
8778 requires an empty template parameter pack, the template is
8779 ill-formed, no diagnostic required." So check that the
8780 pattern works with this parameter. */
8781 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8782 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8786 if (conv
== error_mark_node
)
8788 if (complain
& tf_error
)
8789 inform (input_location
, "so any instantiation with a "
8790 "non-empty parameter pack would be ill-formed");
8793 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8794 /* Recover from missing typename. */
8795 TREE_VEC_ELT (inner_args
, arg_idx
)
8796 = make_pack_expansion (conv
, complain
);
8798 /* We don't know how many args we have yet, just
8799 use the unconverted ones for now. */
8800 new_inner_args
= inner_args
;
8805 else if (require_all_args
)
8807 /* There must be a default arg in this case. */
8808 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8810 /* The position of the first default template argument,
8811 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8813 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8815 arg_idx
- pack_adjust
);
8820 if (arg
== error_mark_node
)
8822 if (complain
& tf_error
)
8823 error ("template argument %d is invalid", arg_idx
+ 1);
8827 /* This can occur if there was an error in the template
8828 parameter list itself (which we would already have
8829 reported) that we are trying to recover from, e.g., a class
8830 template with a parameter list such as
8831 template<typename..., typename> (cpp0x/variadic150.C). */
8834 /* This can also happen with a fixed parameter pack (71834). */
8835 if (arg_idx
>= nargs
)
8839 arg
= convert_template_argument (TREE_VALUE (parm
),
8840 arg
, new_args
, complain
,
8843 if (arg
== error_mark_node
)
8846 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8849 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8851 /* If we had fixed parameter packs, we didn't know how many arguments we
8852 actually needed earlier; now we do. */
8853 nparms
+= fixed_pack_adjust
;
8854 variadic_p
-= fixed_packs
;
8858 if (arg_idx
< nargs
)
8860 /* We had some pack expansion arguments that will only work if the packs
8861 are empty, but wait until instantiation time to complain.
8862 See variadic-ttp3.C. */
8864 /* Except that we can't provide empty packs to alias templates or
8865 concepts when there are no corresponding parameters. Basically,
8866 we can get here with this:
8868 template<typename T> concept C = true;
8870 template<typename... Args>
8874 When parsing C<Args...>, we try to form a concept check of
8875 C<?, Args...>. Without the extra check for substituting an empty
8876 pack past the last parameter, we can accept the check as valid.
8878 FIXME: This may be valid for alias templates (but I doubt it).
8880 FIXME: The error could be better also. */
8881 if (in_decl
&& concept_definition_p (in_decl
))
8883 if (complain
& tf_error
)
8884 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8885 "too many arguments");
8886 return error_mark_node
;
8889 int len
= nparms
+ (nargs
- arg_idx
);
8890 tree args
= make_tree_vec (len
);
8892 for (; i
< nparms
; ++i
)
8893 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8894 for (; i
< len
; ++i
, ++arg_idx
)
8895 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8896 arg_idx
- pack_adjust
);
8897 new_inner_args
= args
;
8902 gcc_assert (!(complain
& tf_error
) || seen_error ());
8903 return error_mark_node
;
8906 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8907 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8908 TREE_VEC_LENGTH (new_inner_args
));
8910 return new_inner_args
;
8913 /* Convert all template arguments to their appropriate types, and
8914 return a vector containing the innermost resulting template
8915 arguments. If any error occurs, return error_mark_node. Error and
8916 warning messages are not issued.
8918 Note that no function argument deduction is performed, and default
8919 arguments are used to fill in unspecified arguments. */
8921 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8923 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8926 /* Convert all template arguments to their appropriate type, and
8927 instantiate default arguments as needed. This returns a vector
8928 containing the innermost resulting template arguments, or
8929 error_mark_node if unsuccessful. */
8931 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8932 tsubst_flags_t complain
)
8934 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8937 /* Like coerce_template_parms. If PARMS represents all template
8938 parameters levels, this function returns a vector of vectors
8939 representing all the resulting argument levels. Note that in this
8940 case, only the innermost arguments are coerced because the
8941 outermost ones are supposed to have been coerced already.
8943 Otherwise, if PARMS represents only (the innermost) vector of
8944 parameters, this function returns a vector containing just the
8945 innermost resulting arguments. */
8948 coerce_innermost_template_parms (tree parms
,
8951 tsubst_flags_t complain
,
8952 bool require_all_args
,
8953 bool use_default_args
)
8955 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8956 int args_depth
= TMPL_ARGS_DEPTH (args
);
8959 if (parms_depth
> 1)
8961 coerced_args
= make_tree_vec (parms_depth
);
8965 for (level
= parms
, cur_depth
= parms_depth
;
8966 parms_depth
> 0 && level
!= NULL_TREE
;
8967 level
= TREE_CHAIN (level
), --cur_depth
)
8970 if (cur_depth
== args_depth
)
8971 l
= coerce_template_parms (TREE_VALUE (level
),
8972 args
, in_decl
, complain
,
8976 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8978 if (l
== error_mark_node
)
8979 return error_mark_node
;
8981 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8985 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8986 args
, in_decl
, complain
,
8989 return coerced_args
;
8992 /* Returns true if T is a wrapper to make a C++20 template parameter
8996 class_nttp_const_wrapper_p (tree t
)
8998 if (cxx_dialect
< cxx2a
)
9000 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9001 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9002 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9005 /* Returns 1 if template args OT and NT are equivalent. */
9008 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9012 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9014 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9017 if (class_nttp_const_wrapper_p (nt
))
9018 nt
= TREE_OPERAND (nt
, 0);
9019 if (class_nttp_const_wrapper_p (ot
))
9020 ot
= TREE_OPERAND (ot
, 0);
9022 if (TREE_CODE (nt
) == TREE_VEC
)
9023 /* For member templates */
9024 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
9025 else if (PACK_EXPANSION_P (ot
))
9026 return (PACK_EXPANSION_P (nt
)
9027 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9028 PACK_EXPANSION_PATTERN (nt
))
9029 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9030 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9031 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9032 return cp_tree_equal (ot
, nt
);
9033 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9035 else if (TYPE_P (nt
))
9039 /* Don't treat an alias template specialization with dependent
9040 arguments as equivalent to its underlying type when used as a
9041 template argument; we need them to be distinct so that we
9042 substitute into the specialization arguments at instantiation
9043 time. And aliases can't be equivalent without being ==, so
9044 we don't need to look any deeper.
9046 During partial ordering, however, we need to treat them normally so
9047 that we can order uses of the same alias with different
9048 cv-qualification (79960). */
9050 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9053 return same_type_p (ot
, nt
);
9055 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
9059 /* Try to treat a template non-type argument that has been converted
9060 to the parameter type as equivalent to one that hasn't yet. */
9061 for (enum tree_code code1
= TREE_CODE (ot
);
9062 CONVERT_EXPR_CODE_P (code1
)
9063 || code1
== NON_LVALUE_EXPR
;
9064 code1
= TREE_CODE (ot
))
9065 ot
= TREE_OPERAND (ot
, 0);
9066 for (enum tree_code code2
= TREE_CODE (nt
);
9067 CONVERT_EXPR_CODE_P (code2
)
9068 || code2
== NON_LVALUE_EXPR
;
9069 code2
= TREE_CODE (nt
))
9070 nt
= TREE_OPERAND (nt
, 0);
9072 return cp_tree_equal (ot
, nt
);
9076 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9077 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9078 NEWARG_PTR with the offending arguments if they are non-NULL. */
9081 comp_template_args (tree oldargs
, tree newargs
,
9082 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9087 if (oldargs
== newargs
)
9090 if (!oldargs
|| !newargs
)
9093 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9096 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9098 tree nt
= TREE_VEC_ELT (newargs
, i
);
9099 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9101 if (! template_args_equal (ot
, nt
, partial_order
))
9103 if (oldarg_ptr
!= NULL
)
9105 if (newarg_ptr
!= NULL
)
9114 comp_template_args_porder (tree oargs
, tree nargs
)
9116 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9119 /* Implement a freelist interface for objects of type T.
9121 Head is a separate object, rather than a regular member, so that we
9122 can define it as a GTY deletable pointer, which is highly
9123 desirable. A data member could be declared that way, but then the
9124 containing object would implicitly get GTY((user)), which would
9125 prevent us from instantiating freelists as global objects.
9126 Although this way we can create freelist global objects, they're
9127 such thin wrappers that instantiating temporaries at every use
9128 loses nothing and saves permanent storage for the freelist object.
9130 Member functions next, anew, poison and reinit have default
9131 implementations that work for most of the types we're interested
9132 in, but if they don't work for some type, they should be explicitly
9133 specialized. See the comments before them for requirements, and
9134 the example specializations for the tree_list_freelist. */
9135 template <typename T
>
9138 /* Return the next object in a chain. We could just do type
9139 punning, but if we access the object with its underlying type, we
9140 avoid strict-aliasing trouble. This needs only work between
9141 poison and reinit. */
9142 static T
*&next (T
*obj
) { return obj
->next
; }
9144 /* Return a newly allocated, uninitialized or minimally-initialized
9145 object of type T. Any initialization performed by anew should
9146 either remain across the life of the object and the execution of
9147 poison, or be redone by reinit. */
9148 static T
*anew () { return ggc_alloc
<T
> (); }
9150 /* Optionally scribble all over the bits holding the object, so that
9151 they become (mostly?) uninitialized memory. This is called while
9152 preparing to make the object part of the free list. */
9153 static void poison (T
*obj
) {
9154 T
*p ATTRIBUTE_UNUSED
= obj
;
9155 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9157 #ifdef ENABLE_GC_CHECKING
9158 /* Poison the data, to indicate the data is garbage. */
9159 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9160 memset (p
, 0xa5, sizeof (*p
));
9162 /* Let valgrind know the object is free. */
9163 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9165 /* Let valgrind know the next portion of the object is available,
9166 but uninitialized. */
9167 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9170 /* Bring an object that underwent at least one lifecycle after anew
9171 and before the most recent free and poison, back to a usable
9172 state, reinitializing whatever is needed for it to be
9173 functionally equivalent to an object just allocated and returned
9174 by anew. This may poison or clear the next field, used by
9175 freelist housekeeping after poison was called. */
9176 static void reinit (T
*obj
) {
9177 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9179 #ifdef ENABLE_GC_CHECKING
9180 memset (q
, 0xa5, sizeof (*q
));
9182 /* Let valgrind know the entire object is available, but
9184 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9187 /* Reference a GTY-deletable pointer that points to the first object
9188 in the free list proper. */
9191 /* Construct a freelist object chaining objects off of HEAD. */
9192 freelist (T
*&head
) : head(head
) {}
9194 /* Add OBJ to the free object list. The former head becomes OBJ's
9203 /* Take an object from the free list, if one is available, or
9204 allocate a new one. Objects taken from the free list should be
9205 regarded as filled with garbage, except for bits that are
9206 configured to be preserved across free and alloc. */
9221 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9222 want to allocate a TREE_LIST using the usual interface, and ensure
9223 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9224 build_tree_list logic in reinit, so this could go out of sync. */
9227 freelist
<tree_node
>::next (tree obj
)
9229 return TREE_CHAIN (obj
);
9233 freelist
<tree_node
>::anew ()
9235 return build_tree_list (NULL
, NULL
);
9239 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9241 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9242 tree p ATTRIBUTE_UNUSED
= obj
;
9243 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9244 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9246 #ifdef ENABLE_GC_CHECKING
9247 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9249 /* Poison the data, to indicate the data is garbage. */
9250 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9251 memset (p
, 0xa5, size
);
9253 /* Let valgrind know the object is free. */
9254 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9255 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9256 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9257 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9259 #ifdef ENABLE_GC_CHECKING
9260 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9261 /* Keep TREE_CHAIN functional. */
9262 TREE_SET_CODE (obj
, TREE_LIST
);
9264 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9269 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9271 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9273 #ifdef ENABLE_GC_CHECKING
9274 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9276 memset (obj
, 0, sizeof (tree_list
));
9279 /* Let valgrind know the entire object is available, but
9281 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9283 #ifdef ENABLE_GC_CHECKING
9284 TREE_SET_CODE (obj
, TREE_LIST
);
9286 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9290 /* Point to the first object in the TREE_LIST freelist. */
9291 static GTY((deletable
)) tree tree_list_freelist_head
;
9292 /* Return the/an actual TREE_LIST freelist. */
9293 static inline freelist
<tree_node
>
9294 tree_list_freelist ()
9296 return tree_list_freelist_head
;
9299 /* Point to the first object in the tinst_level freelist. */
9300 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9301 /* Return the/an actual tinst_level freelist. */
9302 static inline freelist
<tinst_level
>
9303 tinst_level_freelist ()
9305 return tinst_level_freelist_head
;
9308 /* Point to the first object in the pending_template freelist. */
9309 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9310 /* Return the/an actual pending_template freelist. */
9311 static inline freelist
<pending_template
>
9312 pending_template_freelist ()
9314 return pending_template_freelist_head
;
9317 /* Build the TREE_LIST object out of a split list, store it
9318 permanently, and return it. */
9320 tinst_level::to_list ()
9322 gcc_assert (split_list_p ());
9323 tree ret
= tree_list_freelist ().alloc ();
9324 TREE_PURPOSE (ret
) = tldcl
;
9325 TREE_VALUE (ret
) = targs
;
9328 gcc_assert (tree_list_p ());
9332 const unsigned short tinst_level::refcount_infinity
;
9334 /* Increment OBJ's refcount unless it is already infinite. */
9335 static tinst_level
*
9336 inc_refcount_use (tinst_level
*obj
)
9338 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9343 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9345 tinst_level::free (tinst_level
*obj
)
9347 if (obj
->tree_list_p ())
9348 tree_list_freelist ().free (obj
->get_node ());
9349 tinst_level_freelist ().free (obj
);
9352 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9353 OBJ's DECL and OBJ, and start over with the tinst_level object that
9354 used to be referenced by OBJ's NEXT. */
9356 dec_refcount_use (tinst_level
*obj
)
9359 && obj
->refcount
!= tinst_level::refcount_infinity
9360 && !--obj
->refcount
)
9362 tinst_level
*next
= obj
->next
;
9363 tinst_level::free (obj
);
9368 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9369 and of the former PTR. Omitting the second argument is equivalent
9370 to passing (T*)NULL; this is allowed because passing the
9371 zero-valued integral constant NULL confuses type deduction and/or
9372 overload resolution. */
9373 template <typename T
>
9375 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9378 ptr
= inc_refcount_use (obj
);
9379 dec_refcount_use (save
);
9383 add_pending_template (tree d
)
9385 tree ti
= (TYPE_P (d
)
9386 ? CLASSTYPE_TEMPLATE_INFO (d
)
9387 : DECL_TEMPLATE_INFO (d
));
9388 struct pending_template
*pt
;
9391 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9394 /* We are called both from instantiate_decl, where we've already had a
9395 tinst_level pushed, and instantiate_template, where we haven't.
9397 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9398 level
= !current_tinst_level
9399 || current_tinst_level
->maybe_get_node () != d
;
9402 push_tinst_level (d
);
9404 pt
= pending_template_freelist ().alloc ();
9407 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9408 if (last_pending_template
)
9409 last_pending_template
->next
= pt
;
9411 pending_templates
= pt
;
9413 last_pending_template
= pt
;
9415 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9422 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9423 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9424 documentation for TEMPLATE_ID_EXPR. */
9427 lookup_template_function (tree fns
, tree arglist
)
9429 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9430 return error_mark_node
;
9432 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9434 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9436 error ("%q#D is not a function template", fns
);
9437 return error_mark_node
;
9440 if (BASELINK_P (fns
))
9442 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9444 BASELINK_FUNCTIONS (fns
),
9449 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9452 /* Within the scope of a template class S<T>, the name S gets bound
9453 (in build_self_reference) to a TYPE_DECL for the class, not a
9454 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9455 or one of its enclosing classes, and that type is a template,
9456 return the associated TEMPLATE_DECL. Otherwise, the original
9459 Also handle the case when DECL is a TREE_LIST of ambiguous
9460 injected-class-names from different bases. */
9463 maybe_get_template_decl_from_type_decl (tree decl
)
9465 if (decl
== NULL_TREE
)
9468 /* DR 176: A lookup that finds an injected-class-name (10.2
9469 [class.member.lookup]) can result in an ambiguity in certain cases
9470 (for example, if it is found in more than one base class). If all of
9471 the injected-class-names that are found refer to specializations of
9472 the same class template, and if the name is followed by a
9473 template-argument-list, the reference refers to the class template
9474 itself and not a specialization thereof, and is not ambiguous. */
9475 if (TREE_CODE (decl
) == TREE_LIST
)
9477 tree t
, tmpl
= NULL_TREE
;
9478 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9480 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9483 else if (tmpl
!= elt
)
9486 if (tmpl
&& t
== NULL_TREE
)
9492 return (decl
!= NULL_TREE
9493 && DECL_SELF_REFERENCE_P (decl
)
9494 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9495 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9498 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9499 parameters, find the desired type.
9501 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9503 IN_DECL, if non-NULL, is the template declaration we are trying to
9506 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9507 the class we are looking up.
9509 Issue error and warning messages under control of COMPLAIN.
9511 If the template class is really a local class in a template
9512 function, then the FUNCTION_CONTEXT is the function in which it is
9515 ??? Note that this function is currently called *twice* for each
9516 template-id: the first time from the parser, while creating the
9517 incomplete type (finish_template_type), and the second type during the
9518 real instantiation (instantiate_template_class). This is surely something
9519 that we want to avoid. It also causes some problems with argument
9520 coercion (see convert_nontype_argument for more information on this). */
9523 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9524 int entering_scope
, tsubst_flags_t complain
)
9526 tree templ
= NULL_TREE
, parmlist
;
9533 if (identifier_p (d1
))
9535 tree value
= innermost_non_namespace_value (d1
);
9536 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9541 push_decl_namespace (context
);
9542 templ
= lookup_name (d1
);
9543 templ
= maybe_get_template_decl_from_type_decl (templ
);
9545 pop_decl_namespace ();
9548 context
= DECL_CONTEXT (templ
);
9550 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9552 tree type
= TREE_TYPE (d1
);
9554 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9555 an implicit typename for the second A. Deal with it. */
9556 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9557 type
= TREE_TYPE (type
);
9559 if (CLASSTYPE_TEMPLATE_INFO (type
))
9561 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9562 d1
= DECL_NAME (templ
);
9565 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9566 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9568 templ
= TYPE_TI_TEMPLATE (d1
);
9569 d1
= DECL_NAME (templ
);
9571 else if (DECL_TYPE_TEMPLATE_P (d1
))
9574 d1
= DECL_NAME (templ
);
9575 context
= DECL_CONTEXT (templ
);
9577 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9580 d1
= DECL_NAME (templ
);
9583 /* Issue an error message if we didn't find a template. */
9586 if (complain
& tf_error
)
9587 error ("%qT is not a template", d1
);
9588 return error_mark_node
;
9591 if (TREE_CODE (templ
) != TEMPLATE_DECL
9592 /* Make sure it's a user visible template, if it was named by
9594 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9595 && !PRIMARY_TEMPLATE_P (templ
)))
9597 if (complain
& tf_error
)
9599 error ("non-template type %qT used as a template", d1
);
9601 error ("for template declaration %q+D", in_decl
);
9603 return error_mark_node
;
9606 complain
&= ~tf_user
;
9608 /* An alias that just changes the name of a template is equivalent to the
9609 other template, so if any of the arguments are pack expansions, strip
9610 the alias to avoid problems with a pack expansion passed to a non-pack
9611 alias template parameter (DR 1430). */
9612 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9613 templ
= get_underlying_template (templ
);
9615 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9618 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9619 if (arglist2
== error_mark_node
9620 || (!uses_template_parms (arglist2
)
9621 && check_instantiated_args (templ
, arglist2
, complain
)))
9622 return error_mark_node
;
9624 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9629 tree template_type
= TREE_TYPE (templ
);
9632 tree found
= NULL_TREE
;
9635 int is_dependent_type
;
9636 int use_partial_inst_tmpl
= false;
9638 if (template_type
== error_mark_node
)
9639 /* An error occurred while building the template TEMPL, and a
9640 diagnostic has most certainly been emitted for that
9641 already. Let's propagate that error. */
9642 return error_mark_node
;
9644 gen_tmpl
= most_general_template (templ
);
9645 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9646 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9647 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9649 if (arg_depth
== 1 && parm_depth
> 1)
9651 /* We've been given an incomplete set of template arguments.
9654 template <class T> struct S1 {
9655 template <class U> struct S2 {};
9656 template <class U> struct S2<U*> {};
9659 we will be called with an ARGLIST of `U*', but the
9660 TEMPLATE will be `template <class T> template
9661 <class U> struct S1<T>::S2'. We must fill in the missing
9663 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9664 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9665 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9668 /* Now we should have enough arguments. */
9669 gcc_assert (parm_depth
== arg_depth
);
9671 /* From here on, we're only interested in the most general
9674 /* Calculate the BOUND_ARGS. These will be the args that are
9675 actually tsubst'd into the definition to create the
9677 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9679 /*require_all_args=*/true,
9680 /*use_default_args=*/true);
9682 if (arglist
== error_mark_node
)
9683 /* We were unable to bind the arguments. */
9684 return error_mark_node
;
9686 /* In the scope of a template class, explicit references to the
9687 template class refer to the type of the template, not any
9688 instantiation of it. For example, in:
9690 template <class T> class C { void f(C<T>); }
9692 the `C<T>' is just the same as `C'. Outside of the
9693 class, however, such a reference is an instantiation. */
9695 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9696 || currently_open_class (template_type
))
9698 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9700 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9701 return template_type
;
9704 /* If we already have this specialization, return it. */
9705 elt
.tmpl
= gen_tmpl
;
9707 elt
.spec
= NULL_TREE
;
9708 hash
= spec_hasher::hash (&elt
);
9709 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9714 /* If the template's constraints are not satisfied,
9715 then we cannot form a valid type.
9717 Note that the check is deferred until after the hash
9718 lookup. This prevents redundant checks on previously
9719 instantiated specializations. */
9721 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9722 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9724 if (complain
& tf_error
)
9726 auto_diagnostic_group d
;
9727 error ("template constraint failure for %qD", gen_tmpl
);
9728 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9730 return error_mark_node
;
9733 is_dependent_type
= uses_template_parms (arglist
);
9735 /* If the deduced arguments are invalid, then the binding
9737 if (!is_dependent_type
9738 && check_instantiated_args (gen_tmpl
,
9739 INNERMOST_TEMPLATE_ARGS (arglist
),
9741 return error_mark_node
;
9743 if (!is_dependent_type
9744 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9745 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9746 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9748 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9749 DECL_NAME (gen_tmpl
),
9750 /*tag_scope=*/ts_global
);
9754 context
= DECL_CONTEXT (gen_tmpl
);
9755 if (context
&& TYPE_P (context
))
9757 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9758 context
= complete_type (context
);
9761 context
= tsubst (context
, arglist
, complain
, in_decl
);
9763 if (context
== error_mark_node
)
9764 return error_mark_node
;
9767 context
= global_namespace
;
9769 /* Create the type. */
9770 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9772 /* The user referred to a specialization of an alias
9773 template represented by GEN_TMPL.
9775 [temp.alias]/2 says:
9777 When a template-id refers to the specialization of an
9778 alias template, it is equivalent to the associated
9779 type obtained by substitution of its
9780 template-arguments for the template-parameters in the
9781 type-id of the alias template. */
9783 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9784 /* Note that the call above (by indirectly calling
9785 register_specialization in tsubst_decl) registers the
9786 TYPE_DECL representing the specialization of the alias
9787 template. So next time someone substitutes ARGLIST for
9788 the template parms into the alias template (GEN_TMPL),
9789 she'll get that TYPE_DECL back. */
9791 if (t
== error_mark_node
)
9794 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9796 if (!is_dependent_type
)
9798 set_current_access_from_decl (TYPE_NAME (template_type
));
9799 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9800 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9801 arglist
, complain
, in_decl
),
9802 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9803 arglist
, complain
, in_decl
),
9804 SCOPED_ENUM_P (template_type
), NULL
);
9806 if (t
== error_mark_node
)
9811 /* We don't want to call start_enum for this type, since
9812 the values for the enumeration constants may involve
9813 template parameters. And, no one should be interested
9814 in the enumeration constants for such a type. */
9815 t
= cxx_make_type (ENUMERAL_TYPE
);
9816 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9818 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9819 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9820 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9822 else if (CLASS_TYPE_P (template_type
))
9824 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9825 instantiated here. */
9826 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9828 t
= make_class_type (TREE_CODE (template_type
));
9829 CLASSTYPE_DECLARED_CLASS (t
)
9830 = CLASSTYPE_DECLARED_CLASS (template_type
);
9831 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9833 /* A local class. Make sure the decl gets registered properly. */
9834 if (context
== current_function_decl
)
9835 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9837 return error_mark_node
;
9839 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9840 /* This instantiation is another name for the primary
9841 template type. Set the TYPE_CANONICAL field
9843 TYPE_CANONICAL (t
) = template_type
;
9844 else if (any_template_arguments_need_structural_equality_p (arglist
))
9845 /* Some of the template arguments require structural
9846 equality testing, so this template class requires
9847 structural equality testing. */
9848 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9853 /* If we called start_enum or pushtag above, this information
9854 will already be set up. */
9857 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9859 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9860 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9861 DECL_SOURCE_LOCATION (type_decl
)
9862 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9865 type_decl
= TYPE_NAME (t
);
9867 if (CLASS_TYPE_P (template_type
))
9869 TREE_PRIVATE (type_decl
)
9870 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9871 TREE_PROTECTED (type_decl
)
9872 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9873 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9875 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9876 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9880 if (OVERLOAD_TYPE_P (t
)
9881 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9883 static const char *tags
[] = {"abi_tag", "may_alias"};
9885 for (unsigned ix
= 0; ix
!= 2; ix
++)
9888 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9892 = tree_cons (TREE_PURPOSE (attributes
),
9893 TREE_VALUE (attributes
),
9894 TYPE_ATTRIBUTES (t
));
9898 /* Let's consider the explicit specialization of a member
9899 of a class template specialization that is implicitly instantiated,
9904 template<class U> struct M {}; //#0
9909 struct S<int>::M<char> //#1
9913 [temp.expl.spec]/4 says this is valid.
9915 In this case, when we write:
9918 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9921 When we encounter #1, we want to store the partial instantiation
9922 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9924 For all cases other than this "explicit specialization of member of a
9925 class template", we just want to store the most general template into
9926 the CLASSTYPE_TI_TEMPLATE of M.
9928 This case of "explicit specialization of member of a class template"
9930 1/ the enclosing class is an instantiation of, and therefore not
9931 the same as, the context of the most general template, and
9932 2/ we aren't looking at the partial instantiation itself, i.e.
9933 the innermost arguments are not the same as the innermost parms of
9934 the most general template.
9936 So it's only when 1/ and 2/ happens that we want to use the partial
9937 instantiation of the member template in lieu of its most general
9940 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9941 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9942 /* the enclosing class must be an instantiation... */
9943 && CLASS_TYPE_P (context
)
9944 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9946 TREE_VEC_LENGTH (arglist
)--;
9947 ++processing_template_decl
;
9948 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9949 tree partial_inst_args
=
9950 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9951 arglist
, complain
, NULL_TREE
);
9952 --processing_template_decl
;
9953 TREE_VEC_LENGTH (arglist
)++;
9954 if (partial_inst_args
== error_mark_node
)
9955 return error_mark_node
;
9956 use_partial_inst_tmpl
=
9957 /*...and we must not be looking at the partial instantiation
9959 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9963 if (!use_partial_inst_tmpl
)
9964 /* This case is easy; there are no member templates involved. */
9968 /* This is a full instantiation of a member template. Find
9969 the partial instantiation of which this is an instance. */
9971 /* Temporarily reduce by one the number of levels in the ARGLIST
9972 so as to avoid comparing the last set of arguments. */
9973 TREE_VEC_LENGTH (arglist
)--;
9974 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9975 TREE_VEC_LENGTH (arglist
)++;
9976 /* FOUND is either a proper class type, or an alias
9977 template specialization. In the later case, it's a
9978 TYPE_DECL, resulting from the substituting of arguments
9979 for parameters in the TYPE_DECL of the alias template
9980 done earlier. So be careful while getting the template
9982 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9984 : (TREE_CODE (found
) == TYPE_DECL
9985 ? DECL_TI_TEMPLATE (found
)
9986 : CLASSTYPE_TI_TEMPLATE (found
)));
9988 if (DECL_CLASS_TEMPLATE_P (found
)
9989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
9991 /* If this partial instantiation is specialized, we want to
9992 use it for hash table lookup. */
9994 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
9995 hash
= spec_hasher::hash (&elt
);
9999 // Build template info for the new specialization.
10000 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10003 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10004 gcc_checking_assert (*slot
== NULL
);
10005 entry
= ggc_alloc
<spec_entry
> ();
10009 /* Note this use of the partial instantiation so we can check it
10010 later in maybe_process_partial_specialization. */
10011 DECL_TEMPLATE_INSTANTIATIONS (found
)
10012 = tree_cons (arglist
, t
,
10013 DECL_TEMPLATE_INSTANTIATIONS (found
));
10015 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10016 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10017 /* Now that the type has been registered on the instantiations
10018 list, we set up the enumerators. Because the enumeration
10019 constants may involve the enumeration type itself, we make
10020 sure to register the type first, and then create the
10021 constants. That way, doing tsubst_expr for the enumeration
10022 constants won't result in recursive calls here; we'll find
10023 the instantiation and exit above. */
10024 tsubst_enum (template_type
, t
, arglist
);
10026 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10027 /* If the type makes use of template parameters, the
10028 code that generates debugging information will crash. */
10029 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10031 /* Possibly limit visibility based on template args. */
10032 TREE_PUBLIC (type_decl
) = 1;
10033 determine_visibility (type_decl
);
10035 inherit_targ_abi_tags (t
);
10041 /* Wrapper for lookup_template_class_1. */
10044 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10045 int entering_scope
, tsubst_flags_t complain
)
10048 timevar_push (TV_TEMPLATE_INST
);
10049 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10050 entering_scope
, complain
);
10051 timevar_pop (TV_TEMPLATE_INST
);
10055 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10058 lookup_template_variable (tree templ
, tree arglist
)
10060 if (flag_concepts
&& variable_concept_p (templ
))
10061 return build_concept_check (templ
, arglist
, tf_none
);
10063 /* The type of the expression is NULL_TREE since the template-id could refer
10064 to an explicit or partial specialization. */
10065 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10068 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10071 finish_template_variable (tree var
, tsubst_flags_t complain
)
10073 tree templ
= TREE_OPERAND (var
, 0);
10074 tree arglist
= TREE_OPERAND (var
, 1);
10076 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10077 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10079 templ
= most_general_template (templ
);
10080 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10081 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10083 /*use_default*/true);
10085 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10087 if (complain
& tf_error
)
10089 auto_diagnostic_group d
;
10090 error ("use of invalid variable template %qE", var
);
10091 diagnose_constraints (location_of (var
), templ
, arglist
);
10093 return error_mark_node
;
10096 return instantiate_template (templ
, arglist
, complain
);
10099 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10100 TARGS template args, and instantiate it if it's not dependent. */
10103 lookup_and_finish_template_variable (tree templ
, tree targs
,
10104 tsubst_flags_t complain
)
10106 templ
= lookup_template_variable (templ
, targs
);
10107 if (!any_dependent_template_arguments_p (targs
))
10109 templ
= finish_template_variable (templ
, complain
);
10113 return convert_from_reference (templ
);
10117 struct pair_fn_data
10122 /* True when we should also visit template parameters that occur in
10123 non-deduced contexts. */
10124 bool include_nondeduced_p
;
10125 hash_set
<tree
> *visited
;
10128 /* Called from for_each_template_parm via walk_tree. */
10131 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10134 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10135 tree_fn_t fn
= pfd
->fn
;
10136 void *data
= pfd
->data
;
10137 tree result
= NULL_TREE
;
10139 #define WALK_SUBTREE(NODE) \
10142 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10143 pfd->include_nondeduced_p, \
10145 if (result) goto out; \
10149 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10153 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10154 WALK_SUBTREE (TYPE_CONTEXT (t
));
10156 switch (TREE_CODE (t
))
10159 if (TYPE_PTRMEMFUNC_P (t
))
10161 /* Fall through. */
10164 case ENUMERAL_TYPE
:
10165 if (!TYPE_TEMPLATE_INFO (t
))
10166 *walk_subtrees
= 0;
10168 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10172 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10173 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10177 /* Since we're not going to walk subtrees, we have to do this
10178 explicitly here. */
10179 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10180 /* Fall through. */
10182 case FUNCTION_TYPE
:
10183 /* Check the return type. */
10184 WALK_SUBTREE (TREE_TYPE (t
));
10186 /* Check the parameter types. Since default arguments are not
10187 instantiated until they are needed, the TYPE_ARG_TYPES may
10188 contain expressions that involve template parameters. But,
10189 no-one should be looking at them yet. And, once they're
10190 instantiated, they don't contain template parameters, so
10191 there's no point in looking at them then, either. */
10195 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10196 WALK_SUBTREE (TREE_VALUE (parm
));
10198 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10199 want walk_tree walking into them itself. */
10200 *walk_subtrees
= 0;
10203 if (flag_noexcept_type
)
10205 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10207 WALK_SUBTREE (TREE_PURPOSE (spec
));
10212 case DECLTYPE_TYPE
:
10213 case UNDERLYING_TYPE
:
10214 if (pfd
->include_nondeduced_p
10215 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10217 pfd
->include_nondeduced_p
,
10219 return error_mark_node
;
10220 *walk_subtrees
= false;
10223 case FUNCTION_DECL
:
10225 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10226 WALK_SUBTREE (DECL_TI_ARGS (t
));
10227 /* Fall through. */
10231 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10232 WALK_SUBTREE (DECL_INITIAL (t
));
10233 if (DECL_CONTEXT (t
)
10234 && pfd
->include_nondeduced_p
)
10235 WALK_SUBTREE (DECL_CONTEXT (t
));
10238 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10239 /* Record template parameters such as `T' inside `TT<T>'. */
10240 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10241 /* Fall through. */
10243 case TEMPLATE_TEMPLATE_PARM
:
10244 case TEMPLATE_TYPE_PARM
:
10245 case TEMPLATE_PARM_INDEX
:
10246 if (fn
&& (*fn
)(t
, data
))
10252 case TEMPLATE_DECL
:
10253 /* A template template parameter is encountered. */
10254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10255 WALK_SUBTREE (TREE_TYPE (t
));
10257 /* Already substituted template template parameter */
10258 *walk_subtrees
= 0;
10261 case TYPENAME_TYPE
:
10262 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10263 partial instantiation. */
10264 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10268 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10269 && pfd
->include_nondeduced_p
)
10270 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10274 case COMPONENT_REF
:
10275 /* If there's no type, then this thing must be some expression
10276 involving template parameters. */
10277 if (!fn
&& !TREE_TYPE (t
))
10278 return error_mark_node
;
10283 case IMPLICIT_CONV_EXPR
:
10284 case REINTERPRET_CAST_EXPR
:
10285 case CONST_CAST_EXPR
:
10286 case STATIC_CAST_EXPR
:
10287 case DYNAMIC_CAST_EXPR
:
10291 case PSEUDO_DTOR_EXPR
:
10293 return error_mark_node
;
10297 if (pfd
->include_nondeduced_p
)
10298 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10301 case REQUIRES_EXPR
:
10304 return error_mark_node
;
10306 /* Recursively walk the type of each constraint variable. */
10307 tree p
= TREE_OPERAND (t
, 0);
10310 WALK_SUBTREE (TREE_TYPE (p
));
10311 p
= TREE_CHAIN (p
);
10320 #undef WALK_SUBTREE
10322 /* We didn't find any template parameters we liked. */
10327 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10328 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10329 call FN with the parameter and the DATA.
10330 If FN returns nonzero, the iteration is terminated, and
10331 for_each_template_parm returns 1. Otherwise, the iteration
10332 continues. If FN never returns a nonzero value, the value
10333 returned by for_each_template_parm is 0. If FN is NULL, it is
10334 considered to be the function which always returns 1.
10336 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10337 parameters that occur in non-deduced contexts. When false, only
10338 visits those template parameters that can be deduced. */
10341 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10342 hash_set
<tree
> *visited
,
10343 bool include_nondeduced_p
,
10346 struct pair_fn_data pfd
;
10351 pfd
.any_fn
= any_fn
;
10353 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10355 /* Walk the tree. (Conceptually, we would like to walk without
10356 duplicates, but for_each_template_parm_r recursively calls
10357 for_each_template_parm, so we would need to reorganize a fair
10358 bit to use walk_tree_without_duplicates, so we keep our own
10361 pfd
.visited
= visited
;
10363 pfd
.visited
= new hash_set
<tree
>;
10364 result
= cp_walk_tree (&t
,
10365 for_each_template_parm_r
,
10372 delete pfd
.visited
;
10379 struct find_template_parameter_info
10381 explicit find_template_parameter_info (tree ctx_parms
)
10382 : ctx_parms (ctx_parms
),
10383 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10386 hash_set
<tree
> visited
;
10387 hash_set
<tree
> parms
;
10392 /* Appends the declaration of T to the list in DATA. */
10395 keep_template_parm (tree t
, void* data
)
10397 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10399 /* Template parameters declared within the expression are not part of
10400 the parameter mapping. For example, in this concept:
10402 template<typename T>
10403 concept C = requires { <expr> } -> same_as<int>;
10405 the return specifier same_as<int> declares a new decltype parameter
10406 that must not be part of the parameter mapping. The same is true
10407 for generic lambda parameters, lambda template parameters, etc. */
10410 template_parm_level_and_index (t
, &level
, &index
);
10411 if (level
> ftpi
->max_depth
)
10414 /* Arguments like const T yield parameters like const T. This means that
10415 a template-id like X<T, const T> would yield two distinct parameters:
10416 T and const T. Adjust types to their unqualified versions. */
10418 t
= TYPE_MAIN_VARIANT (t
);
10419 ftpi
->parms
.add (t
);
10424 /* Ensure that we recursively examine certain terms that are not normally
10425 visited in for_each_template_parm_r. */
10428 any_template_parm_r (tree t
, void *data
)
10430 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10432 #define WALK_SUBTREE(NODE) \
10435 for_each_template_parm (NODE, keep_template_parm, data, \
10436 &ftpi->visited, true, \
10437 any_template_parm_r); \
10441 /* A mention of a member alias/typedef is a use of all of its template
10442 arguments, including those from the enclosing class, so we don't use
10443 alias_template_specialization_p here. */
10444 if (TYPE_P (t
) && typedef_variant_p (t
))
10445 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10446 WALK_SUBTREE (TI_ARGS (tinfo
));
10448 switch (TREE_CODE (t
))
10450 case TEMPLATE_TYPE_PARM
:
10451 /* Type constraints of a placeholder type may contain parameters. */
10453 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10454 WALK_SUBTREE (constr
);
10457 case TEMPLATE_ID_EXPR
:
10458 /* Search through references to variable templates. */
10459 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10460 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10463 case TEMPLATE_PARM_INDEX
:
10465 /* A parameter or constraint variable may also depend on a template
10466 parameter without explicitly naming it. */
10467 WALK_SUBTREE (TREE_TYPE (t
));
10470 case TEMPLATE_DECL
:
10472 /* If T is a member template that shares template parameters with
10473 ctx_parms, we need to mark all those parameters for mapping. */
10474 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10475 tree cparms
= ftpi
->ctx_parms
;
10476 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10477 dparms
= TREE_CHAIN (dparms
);
10478 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10479 cparms
= TREE_CHAIN (cparms
);
10481 && (TREE_TYPE (TREE_VALUE (dparms
))
10482 != TREE_TYPE (TREE_VALUE (cparms
))))
10483 dparms
= TREE_CHAIN (dparms
),
10484 cparms
= TREE_CHAIN (cparms
);
10487 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10488 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10489 for (int i
= 0; i
< ddepth
; ++i
)
10490 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10497 /* Look in the parms and body. */
10498 tree fn
= lambda_function (t
);
10499 WALK_SUBTREE (TREE_TYPE (fn
));
10500 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10508 /* Keep walking. */
10512 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10513 are the template parameters in scope. */
10516 find_template_parameters (tree t
, tree ctx_parms
)
10521 find_template_parameter_info
ftpi (ctx_parms
);
10522 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10523 /*include_nondeduced*/true, any_template_parm_r
);
10524 tree list
= NULL_TREE
;
10525 for (hash_set
<tree
>::iterator iter
= ftpi
.parms
.begin();
10526 iter
!= ftpi
.parms
.end(); ++iter
)
10527 list
= tree_cons (NULL_TREE
, *iter
, list
);
10531 /* Returns true if T depends on any template parameter. */
10534 uses_template_parms (tree t
)
10536 if (t
== NULL_TREE
)
10540 int saved_processing_template_decl
;
10542 saved_processing_template_decl
= processing_template_decl
;
10543 if (!saved_processing_template_decl
)
10544 processing_template_decl
= 1;
10546 dependent_p
= dependent_type_p (t
);
10547 else if (TREE_CODE (t
) == TREE_VEC
)
10548 dependent_p
= any_dependent_template_arguments_p (t
);
10549 else if (TREE_CODE (t
) == TREE_LIST
)
10550 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10551 || uses_template_parms (TREE_CHAIN (t
)));
10552 else if (TREE_CODE (t
) == TYPE_DECL
)
10553 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10554 else if (t
== error_mark_node
)
10555 dependent_p
= false;
10557 dependent_p
= value_dependent_expression_p (t
);
10559 processing_template_decl
= saved_processing_template_decl
;
10561 return dependent_p
;
10564 /* Returns true iff current_function_decl is an incompletely instantiated
10565 template. Useful instead of processing_template_decl because the latter
10566 is set to 0 during instantiate_non_dependent_expr. */
10569 in_template_function (void)
10571 tree fn
= current_function_decl
;
10573 ++processing_template_decl
;
10574 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10575 && DECL_TEMPLATE_INFO (fn
)
10576 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10577 --processing_template_decl
;
10581 /* Returns true if T depends on any template parameter with level LEVEL. */
10584 uses_template_parms_level (tree t
, int level
)
10586 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10587 /*include_nondeduced_p=*/true);
10590 /* Returns true if the signature of DECL depends on any template parameter from
10591 its enclosing class. */
10594 uses_outer_template_parms (tree decl
)
10596 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10599 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10600 &depth
, NULL
, /*include_nondeduced_p=*/true))
10602 if (PRIMARY_TEMPLATE_P (decl
)
10603 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10604 (DECL_TEMPLATE_PARMS (decl
)),
10605 template_parm_outer_level
,
10606 &depth
, NULL
, /*include_nondeduced_p=*/true))
10608 tree ci
= get_constraints (decl
);
10610 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10611 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10612 &depth
, NULL
, /*nondeduced*/true))
10617 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10618 ill-formed translation unit, i.e. a variable or function that isn't
10619 usable in a constant expression. */
10622 neglectable_inst_p (tree d
)
10624 return (d
&& DECL_P (d
)
10625 && !undeduced_auto_decl (d
)
10626 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10627 : decl_maybe_constant_var_p (d
)));
10630 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10631 neglectable and instantiated from within an erroneous instantiation. */
10634 limit_bad_template_recursion (tree decl
)
10636 struct tinst_level
*lev
= current_tinst_level
;
10637 int errs
= errorcount
+ sorrycount
;
10638 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10641 for (; lev
; lev
= lev
->next
)
10642 if (neglectable_inst_p (lev
->maybe_get_node ()))
10645 return (lev
&& errs
> lev
->errors
);
10648 static int tinst_depth
;
10649 extern int max_tinst_depth
;
10652 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10654 /* We're starting to instantiate D; record the template instantiation context
10655 at LOC for diagnostics and to restore it later. */
10658 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10660 struct tinst_level
*new_level
;
10662 if (tinst_depth
>= max_tinst_depth
)
10664 /* Tell error.c not to try to instantiate any templates. */
10666 fatal_error (input_location
,
10667 "template instantiation depth exceeds maximum of %d"
10668 " (use %<-ftemplate-depth=%> to increase the maximum)",
10673 /* If the current instantiation caused problems, don't let it instantiate
10674 anything else. Do allow deduction substitution and decls usable in
10675 constant expressions. */
10676 if (!targs
&& limit_bad_template_recursion (tldcl
))
10678 /* Avoid no_linkage_errors and unused function warnings for this
10680 TREE_NO_WARNING (tldcl
) = 1;
10684 /* When not -quiet, dump template instantiations other than functions, since
10685 announce_function will take care of those. */
10686 if (!quiet_flag
&& !targs
10687 && TREE_CODE (tldcl
) != TREE_LIST
10688 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10689 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10691 new_level
= tinst_level_freelist ().alloc ();
10692 new_level
->tldcl
= tldcl
;
10693 new_level
->targs
= targs
;
10694 new_level
->locus
= loc
;
10695 new_level
->errors
= errorcount
+ sorrycount
;
10696 new_level
->next
= NULL
;
10697 new_level
->refcount
= 0;
10698 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10699 set_refcount_ptr (current_tinst_level
, new_level
);
10702 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10703 depth_reached
= tinst_depth
;
10708 /* We're starting substitution of TMPL<ARGS>; record the template
10709 substitution context for diagnostics and to restore it later. */
10712 push_tinst_level (tree tmpl
, tree args
)
10714 return push_tinst_level_loc (tmpl
, args
, input_location
);
10717 /* We're starting to instantiate D; record INPUT_LOCATION and the
10718 template instantiation context for diagnostics and to restore it
10722 push_tinst_level (tree d
)
10724 return push_tinst_level_loc (d
, input_location
);
10727 /* Likewise, but record LOC as the program location. */
10730 push_tinst_level_loc (tree d
, location_t loc
)
10732 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10733 return push_tinst_level_loc (d
, NULL
, loc
);
10736 /* We're done instantiating this template; return to the instantiation
10740 pop_tinst_level (void)
10742 /* Restore the filename and line number stashed away when we started
10743 this instantiation. */
10744 input_location
= current_tinst_level
->locus
;
10745 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10749 /* We're instantiating a deferred template; restore the template
10750 instantiation context in which the instantiation was requested, which
10751 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10754 reopen_tinst_level (struct tinst_level
*level
)
10756 struct tinst_level
*t
;
10759 for (t
= level
; t
; t
= t
->next
)
10762 set_refcount_ptr (current_tinst_level
, level
);
10763 pop_tinst_level ();
10764 if (current_tinst_level
)
10765 current_tinst_level
->errors
= errorcount
+sorrycount
;
10766 return level
->maybe_get_node ();
10769 /* Returns the TINST_LEVEL which gives the original instantiation
10772 struct tinst_level
*
10773 outermost_tinst_level (void)
10775 struct tinst_level
*level
= current_tinst_level
;
10777 while (level
->next
)
10778 level
= level
->next
;
10782 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10783 vector of template arguments, as for tsubst.
10785 Returns an appropriate tsubst'd friend declaration. */
10788 tsubst_friend_function (tree decl
, tree args
)
10792 if (TREE_CODE (decl
) == FUNCTION_DECL
10793 && DECL_TEMPLATE_INSTANTIATION (decl
)
10794 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10795 /* This was a friend declared with an explicit template
10796 argument list, e.g.:
10798 friend void f<>(T);
10800 to indicate that f was a template instantiation, not a new
10801 function declaration. Now, we have to figure out what
10802 instantiation of what template. */
10804 tree template_id
, arglist
, fns
;
10807 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10809 /* Friend functions are looked up in the containing namespace scope.
10810 We must enter that scope, to avoid finding member functions of the
10811 current class with same name. */
10812 push_nested_namespace (ns
);
10813 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10814 tf_warning_or_error
, NULL_TREE
,
10815 /*integral_constant_expression_p=*/false);
10816 pop_nested_namespace (ns
);
10817 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10818 tf_warning_or_error
, NULL_TREE
);
10819 template_id
= lookup_template_function (fns
, arglist
);
10821 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10822 tmpl
= determine_specialization (template_id
, new_friend
,
10824 /*need_member_template=*/0,
10825 TREE_VEC_LENGTH (args
),
10827 return instantiate_template (tmpl
, new_args
, tf_error
);
10830 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10832 /* The NEW_FRIEND will look like an instantiation, to the
10833 compiler, but is not an instantiation from the point of view of
10834 the language. For example, we might have had:
10836 template <class T> struct S {
10837 template <class U> friend void f(T, U);
10840 Then, in S<int>, template <class U> void f(int, U) is not an
10841 instantiation of anything. */
10842 if (new_friend
== error_mark_node
)
10843 return error_mark_node
;
10845 DECL_USE_TEMPLATE (new_friend
) = 0;
10846 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10848 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10849 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10850 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10852 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10853 match in decls_match. */
10854 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10855 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
10856 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
10857 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10860 /* The mangled name for the NEW_FRIEND is incorrect. The function
10861 is not a template instantiation and should not be mangled like
10862 one. Therefore, we forget the mangling here; we'll recompute it
10863 later if we need it. */
10864 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10866 SET_DECL_RTL (new_friend
, NULL
);
10867 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10870 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10873 tree new_friend_template_info
;
10874 tree new_friend_result_template_info
;
10876 int new_friend_is_defn
;
10878 /* We must save some information from NEW_FRIEND before calling
10879 duplicate decls since that function will free NEW_FRIEND if
10881 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10882 new_friend_is_defn
=
10883 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10884 (template_for_substitution (new_friend
)))
10886 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10888 /* This declaration is a `primary' template. */
10889 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10891 new_friend_result_template_info
10892 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10895 new_friend_result_template_info
= NULL_TREE
;
10897 /* Inside pushdecl_namespace_level, we will push into the
10898 current namespace. However, the friend function should go
10899 into the namespace of the template. */
10900 ns
= decl_namespace_context (new_friend
);
10901 push_nested_namespace (ns
);
10902 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10903 pop_nested_namespace (ns
);
10905 if (old_decl
== error_mark_node
)
10906 return error_mark_node
;
10908 if (old_decl
!= new_friend
)
10910 /* This new friend declaration matched an existing
10911 declaration. For example, given:
10913 template <class T> void f(T);
10914 template <class U> class C {
10915 template <class T> friend void f(T) {}
10918 the friend declaration actually provides the definition
10919 of `f', once C has been instantiated for some type. So,
10920 old_decl will be the out-of-class template declaration,
10921 while new_friend is the in-class definition.
10923 But, if `f' was called before this point, the
10924 instantiation of `f' will have DECL_TI_ARGS corresponding
10925 to `T' but not to `U', references to which might appear
10926 in the definition of `f'. Previously, the most general
10927 template for an instantiation of `f' was the out-of-class
10928 version; now it is the in-class version. Therefore, we
10929 run through all specialization of `f', adding to their
10930 DECL_TI_ARGS appropriately. In particular, they need a
10931 new set of outer arguments, corresponding to the
10932 arguments for this class instantiation.
10934 The same situation can arise with something like this:
10936 friend void f(int);
10937 template <class T> class C {
10938 friend void f(T) {}
10941 when `C<int>' is instantiated. Now, `f(int)' is defined
10944 if (!new_friend_is_defn
)
10945 /* On the other hand, if the in-class declaration does
10946 *not* provide a definition, then we don't want to alter
10947 existing definitions. We can just leave everything
10952 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10953 tree new_args
= TI_ARGS (new_friend_template_info
);
10955 /* Overwrite whatever template info was there before, if
10956 any, with the new template information pertaining to
10957 the declaration. */
10958 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10960 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10962 /* We should have called reregister_specialization in
10963 duplicate_decls. */
10964 gcc_assert (retrieve_specialization (new_template
,
10968 /* Instantiate it if the global has already been used. */
10969 if (DECL_ODR_USED (old_decl
))
10970 instantiate_decl (old_decl
, /*defer_ok=*/true,
10971 /*expl_inst_class_mem_p=*/false);
10977 /* Indicate that the old function template is a partial
10979 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10980 = new_friend_result_template_info
;
10982 gcc_assert (new_template
10983 == most_general_template (new_template
));
10984 gcc_assert (new_template
!= old_decl
);
10986 /* Reassign any specializations already in the hash table
10987 to the new more general template, and add the
10988 additional template args. */
10989 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10991 t
= TREE_CHAIN (t
))
10993 tree spec
= TREE_VALUE (t
);
10996 elt
.tmpl
= old_decl
;
10997 elt
.args
= DECL_TI_ARGS (spec
);
10998 elt
.spec
= NULL_TREE
;
11000 decl_specializations
->remove_elt (&elt
);
11002 DECL_TI_ARGS (spec
)
11003 = add_outermost_template_args (new_args
,
11004 DECL_TI_ARGS (spec
));
11006 register_specialization
11007 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11010 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11014 /* The information from NEW_FRIEND has been merged into OLD_DECL
11015 by duplicate_decls. */
11016 new_friend
= old_decl
;
11021 tree context
= DECL_CONTEXT (new_friend
);
11025 template <class T> class C {
11026 template <class U> friend void C1<U>::f (); // case 1
11027 friend void C2<T>::f (); // case 2
11029 we only need to make sure CONTEXT is a complete type for
11030 case 2. To distinguish between the two cases, we note that
11031 CONTEXT of case 1 remains dependent type after tsubst while
11032 this isn't true for case 2. */
11033 ++processing_template_decl
;
11034 dependent_p
= dependent_type_p (context
);
11035 --processing_template_decl
;
11038 && !complete_type_or_else (context
, NULL_TREE
))
11039 return error_mark_node
;
11041 if (COMPLETE_TYPE_P (context
))
11043 tree fn
= new_friend
;
11044 /* do_friend adds the TEMPLATE_DECL for any member friend
11045 template even if it isn't a member template, i.e.
11046 template <class T> friend A<T>::f();
11047 Look through it in that case. */
11048 if (TREE_CODE (fn
) == TEMPLATE_DECL
11049 && !PRIMARY_TEMPLATE_P (fn
))
11050 fn
= DECL_TEMPLATE_RESULT (fn
);
11051 /* Check to see that the declaration is really present, and,
11052 possibly obtain an improved declaration. */
11053 fn
= check_classfn (context
, fn
, NULL_TREE
);
11063 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11064 template arguments, as for tsubst.
11066 Returns an appropriate tsubst'd friend type or error_mark_node on
11070 tsubst_friend_class (tree friend_tmpl
, tree args
)
11074 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11076 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11077 return TREE_TYPE (tmpl
);
11080 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11081 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11082 push_nested_namespace (context
);
11085 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11086 push_nested_class (context
);
11089 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11090 /*non_class=*/false, /*block_p=*/false,
11091 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11093 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11095 /* The friend template has already been declared. Just
11096 check to see that the declarations match, and install any new
11097 default parameters. We must tsubst the default parameters,
11098 of course. We only need the innermost template parameters
11099 because that is all that redeclare_class_template will look
11101 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11102 > TMPL_ARGS_DEPTH (args
))
11104 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11105 args
, tf_warning_or_error
);
11106 location_t saved_input_location
= input_location
;
11107 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11108 tree cons
= get_constraints (tmpl
);
11109 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11110 input_location
= saved_input_location
;
11115 /* The friend template has not already been declared. In this
11116 case, the instantiation of the template class will cause the
11117 injection of this template into the namespace scope. */
11118 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11120 if (tmpl
!= error_mark_node
)
11122 /* The new TMPL is not an instantiation of anything, so we
11123 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11124 for the new type because that is supposed to be the
11125 corresponding template decl, i.e., TMPL. */
11126 DECL_USE_TEMPLATE (tmpl
) = 0;
11127 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11128 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11129 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11130 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11132 /* It is hidden. */
11133 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11134 DECL_ANTICIPATED (tmpl
)
11135 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11137 /* Inject this template into the enclosing namspace scope. */
11138 tmpl
= pushdecl_namespace_level (tmpl
, true);
11142 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11143 pop_nested_namespace (context
);
11145 pop_nested_class ();
11147 return TREE_TYPE (tmpl
);
11150 /* Returns zero if TYPE cannot be completed later due to circularity.
11151 Otherwise returns one. */
11154 can_complete_type_without_circularity (tree type
)
11156 if (type
== NULL_TREE
|| type
== error_mark_node
)
11158 else if (COMPLETE_TYPE_P (type
))
11160 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11161 return can_complete_type_without_circularity (TREE_TYPE (type
));
11162 else if (CLASS_TYPE_P (type
)
11163 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11169 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11170 tsubst_flags_t
, tree
);
11172 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11173 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11176 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11177 tsubst_flags_t complain
, tree in_decl
)
11179 gcc_assert (ATTR_IS_DEPENDENT (t
));
11181 tree val
= TREE_VALUE (t
);
11182 if (val
== NULL_TREE
)
11183 /* Nothing to do. */;
11184 else if ((flag_openmp
|| flag_openmp_simd
)
11185 && is_attribute_p ("omp declare simd",
11186 get_attribute_name (t
)))
11188 tree clauses
= TREE_VALUE (val
);
11189 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11190 complain
, in_decl
);
11191 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11192 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11193 tree parms
= DECL_ARGUMENTS (*decl_p
);
11195 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11197 val
= build_tree_list (NULL_TREE
, clauses
);
11201 else if (flag_openmp
11202 && is_attribute_p ("omp declare variant base",
11203 get_attribute_name (t
)))
11205 ++cp_unevaluated_operand
;
11207 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11208 in_decl
, /*integral_constant_expression_p=*/false);
11209 --cp_unevaluated_operand
;
11210 tree chain
= TREE_CHAIN (val
);
11211 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11212 tree ctx
= copy_list (TREE_VALUE (val
));
11213 tree simd
= get_identifier ("simd");
11214 tree score
= get_identifier (" score");
11215 tree condition
= get_identifier ("condition");
11216 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11218 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11219 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11220 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11222 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11224 tree clauses
= TREE_VALUE (t2
);
11225 clauses
= tsubst_omp_clauses (clauses
,
11226 C_ORT_OMP_DECLARE_SIMD
, args
,
11227 complain
, in_decl
);
11228 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11229 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11230 TREE_VALUE (t2
) = clauses
;
11234 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11235 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11236 if (TREE_VALUE (t3
))
11239 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11240 && TREE_PURPOSE (t3
) != score
);
11241 tree v
= TREE_VALUE (t3
);
11242 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11244 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11245 v
= fold_non_dependent_expr (v
);
11246 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11247 || (TREE_PURPOSE (t3
) == score
11248 ? TREE_CODE (v
) != INTEGER_CST
11249 : !tree_fits_shwi_p (v
)))
11252 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11254 if (TREE_PURPOSE (t3
) == score
)
11255 error_at (loc
, "score argument must be "
11256 "constant integer expression");
11257 else if (allow_string
)
11258 error_at (loc
, "property must be constant "
11259 "integer expression or string "
11262 error_at (loc
, "property must be constant "
11263 "integer expression");
11266 else if (TREE_PURPOSE (t3
) == score
11267 && tree_int_cst_sgn (v
) < 0)
11270 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11272 error_at (loc
, "score argument must be "
11276 TREE_VALUE (t3
) = v
;
11281 val
= tree_cons (varid
, ctx
, chain
);
11283 /* If the first attribute argument is an identifier, don't
11284 pass it through tsubst. Attributes like mode, format,
11285 cleanup and several target specific attributes expect it
11287 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11290 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11291 /*integral_constant_expression_p=*/false);
11292 if (chain
!= TREE_CHAIN (val
))
11293 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11295 else if (PACK_EXPANSION_P (val
))
11297 /* An attribute pack expansion. */
11298 tree purp
= TREE_PURPOSE (t
);
11299 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11300 if (pack
== error_mark_node
)
11301 return error_mark_node
;
11302 int len
= TREE_VEC_LENGTH (pack
);
11303 tree list
= NULL_TREE
;
11305 for (int i
= 0; i
< len
; ++i
)
11307 tree elt
= TREE_VEC_ELT (pack
, i
);
11308 *q
= build_tree_list (purp
, elt
);
11309 q
= &TREE_CHAIN (*q
);
11314 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11315 /*integral_constant_expression_p=*/false);
11317 if (val
!= TREE_VALUE (t
))
11318 return build_tree_list (TREE_PURPOSE (t
), val
);
11322 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11323 unchanged or a new TREE_LIST chain. */
11326 tsubst_attributes (tree attributes
, tree args
,
11327 tsubst_flags_t complain
, tree in_decl
)
11329 tree last_dep
= NULL_TREE
;
11331 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11332 if (ATTR_IS_DEPENDENT (t
))
11335 attributes
= copy_list (attributes
);
11340 for (tree
*p
= &attributes
; *p
; )
11343 if (ATTR_IS_DEPENDENT (t
))
11345 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11350 p
= &TREE_CHAIN (*p
);
11351 *p
= TREE_CHAIN (t
);
11355 p
= &TREE_CHAIN (*p
);
11361 /* Apply any attributes which had to be deferred until instantiation
11362 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11363 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11366 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11367 tree args
, tsubst_flags_t complain
, tree in_decl
)
11369 tree last_dep
= NULL_TREE
;
11373 if (attributes
== NULL_TREE
)
11376 if (DECL_P (*decl_p
))
11378 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11380 p
= &DECL_ATTRIBUTES (*decl_p
);
11381 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11382 to our attributes parameter. */
11383 gcc_assert (*p
== attributes
);
11387 p
= &TYPE_ATTRIBUTES (*decl_p
);
11388 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11389 lookup_template_class_1, and should be preserved. */
11390 gcc_assert (*p
!= attributes
);
11392 p
= &TREE_CHAIN (*p
);
11395 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11396 if (ATTR_IS_DEPENDENT (t
))
11399 attributes
= copy_list (attributes
);
11406 tree late_attrs
= NULL_TREE
;
11407 tree
*q
= &late_attrs
;
11412 if (ATTR_IS_DEPENDENT (t
))
11414 *p
= TREE_CHAIN (t
);
11415 TREE_CHAIN (t
) = NULL_TREE
;
11416 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11418 q
= &TREE_CHAIN (*q
);
11421 p
= &TREE_CHAIN (t
);
11424 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11428 /* Perform (or defer) access check for typedefs that were referenced
11429 from within the template TMPL code.
11430 This is a subroutine of instantiate_decl and instantiate_class_template.
11431 TMPL is the template to consider and TARGS is the list of arguments of
11435 perform_typedefs_access_check (tree tmpl
, tree targs
)
11438 qualified_typedef_usage_t
*iter
;
11441 || (!CLASS_TYPE_P (tmpl
)
11442 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
11445 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
11447 tree type_decl
= iter
->typedef_decl
;
11448 tree type_scope
= iter
->context
;
11450 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
11453 if (uses_template_parms (type_decl
))
11454 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
11455 if (uses_template_parms (type_scope
))
11456 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11458 /* Make access check error messages point to the location
11459 of the use of the typedef. */
11460 iloc_sentinel
ils (iter
->locus
);
11461 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11462 type_decl
, type_decl
,
11463 tf_warning_or_error
);
11468 instantiate_class_template_1 (tree type
)
11470 tree templ
, args
, pattern
, t
, member
;
11474 unsigned int saved_maximum_field_alignment
;
11477 if (type
== error_mark_node
)
11478 return error_mark_node
;
11480 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11481 || uses_template_parms (type
))
11484 /* Figure out which template is being instantiated. */
11485 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11486 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11488 /* Mark the type as in the process of being defined. */
11489 TYPE_BEING_DEFINED (type
) = 1;
11491 /* We may be in the middle of deferred access check. Disable
11493 deferring_access_check_sentinel
acs (dk_no_deferred
);
11495 /* Determine what specialization of the original template to
11497 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11498 if (t
== error_mark_node
)
11499 return error_mark_node
;
11502 /* This TYPE is actually an instantiation of a partial
11503 specialization. We replace the innermost set of ARGS with
11504 the arguments appropriate for substitution. For example,
11507 template <class T> struct S {};
11508 template <class T> struct S<T*> {};
11510 and supposing that we are instantiating S<int*>, ARGS will
11511 presently be {int*} -- but we need {int}. */
11512 pattern
= TREE_TYPE (t
);
11513 args
= TREE_PURPOSE (t
);
11517 pattern
= TREE_TYPE (templ
);
11518 args
= CLASSTYPE_TI_ARGS (type
);
11521 /* If the template we're instantiating is incomplete, then clearly
11522 there's nothing we can do. */
11523 if (!COMPLETE_TYPE_P (pattern
))
11525 /* We can try again later. */
11526 TYPE_BEING_DEFINED (type
) = 0;
11530 /* If we've recursively instantiated too many templates, stop. */
11531 if (! push_tinst_level (type
))
11534 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11535 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11537 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11538 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11539 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11540 fn_context
= error_mark_node
;
11542 push_to_top_level ();
11545 cp_unevaluated_operand
= 0;
11546 c_inhibit_evaluation_warnings
= 0;
11548 /* Use #pragma pack from the template context. */
11549 saved_maximum_field_alignment
= maximum_field_alignment
;
11550 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11552 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11554 /* Set the input location to the most specialized template definition.
11555 This is needed if tsubsting causes an error. */
11556 typedecl
= TYPE_MAIN_DECL (pattern
);
11557 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11558 DECL_SOURCE_LOCATION (typedecl
);
11560 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11561 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11562 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11563 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11564 if (ANON_AGGR_TYPE_P (pattern
))
11565 SET_ANON_AGGR_TYPE_P (type
);
11566 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11568 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11569 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11570 /* Adjust visibility for template arguments. */
11571 determine_visibility (TYPE_MAIN_DECL (type
));
11573 if (CLASS_TYPE_P (type
))
11574 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11576 pbinfo
= TYPE_BINFO (pattern
);
11578 /* We should never instantiate a nested class before its enclosing
11579 class; we need to look up the nested class by name before we can
11580 instantiate it, and that lookup should instantiate the enclosing
11582 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11583 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11585 base_list
= NULL_TREE
;
11586 if (BINFO_N_BASE_BINFOS (pbinfo
))
11592 /* We must enter the scope containing the type, as that is where
11593 the accessibility of types named in dependent bases are
11595 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11597 /* Substitute into each of the bases to determine the actual
11599 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11602 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11603 tree expanded_bases
= NULL_TREE
;
11606 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11609 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11610 args
, tf_error
, NULL_TREE
);
11611 if (expanded_bases
== error_mark_node
)
11614 len
= TREE_VEC_LENGTH (expanded_bases
);
11617 for (idx
= 0; idx
< len
; idx
++)
11619 if (expanded_bases
)
11620 /* Extract the already-expanded base class. */
11621 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11623 /* Substitute to figure out the base class. */
11624 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11627 if (base
== error_mark_node
)
11630 base_list
= tree_cons (access
, base
, base_list
);
11631 if (BINFO_VIRTUAL_P (pbase_binfo
))
11632 TREE_TYPE (base_list
) = integer_type_node
;
11636 /* The list is now in reverse order; correct that. */
11637 base_list
= nreverse (base_list
);
11640 pop_scope (pushed_scope
);
11642 /* Now call xref_basetypes to set up all the base-class
11644 xref_basetypes (type
, base_list
);
11646 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11647 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11648 args
, tf_error
, NULL_TREE
);
11649 fixup_attribute_variants (type
);
11651 /* Now that our base classes are set up, enter the scope of the
11652 class, so that name lookups into base classes, etc. will work
11653 correctly. This is precisely analogous to what we do in
11654 begin_class_definition when defining an ordinary non-template
11655 class, except we also need to push the enclosing classes. */
11656 push_nested_class (type
);
11658 /* Now members are processed in the order of declaration. */
11659 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11660 member
; member
= TREE_CHAIN (member
))
11662 tree t
= TREE_VALUE (member
);
11664 if (TREE_PURPOSE (member
))
11668 if (LAMBDA_TYPE_P (t
))
11669 /* A closure type for a lambda in an NSDMI or default argument.
11670 Ignore it; it will be regenerated when needed. */
11673 /* Build new CLASSTYPE_NESTED_UTDS. */
11676 bool class_template_p
;
11678 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11679 && TYPE_LANG_SPECIFIC (t
)
11680 && CLASSTYPE_IS_TEMPLATE (t
));
11681 /* If the member is a class template, then -- even after
11682 substitution -- there may be dependent types in the
11683 template argument list for the class. We increment
11684 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11685 that function will assume that no types are dependent
11686 when outside of a template. */
11687 if (class_template_p
)
11688 ++processing_template_decl
;
11689 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11690 if (class_template_p
)
11691 --processing_template_decl
;
11692 if (newtag
== error_mark_node
)
11695 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11697 tree name
= TYPE_IDENTIFIER (t
);
11699 if (class_template_p
)
11700 /* Unfortunately, lookup_template_class sets
11701 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11702 instantiation (i.e., for the type of a member
11703 template class nested within a template class.)
11704 This behavior is required for
11705 maybe_process_partial_specialization to work
11706 correctly, but is not accurate in this case;
11707 the TAG is not an instantiation of anything.
11708 (The corresponding TEMPLATE_DECL is an
11709 instantiation, but the TYPE is not.) */
11710 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11712 /* Now, we call pushtag to put this NEWTAG into the scope of
11713 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11714 pushtag calling push_template_decl. We don't have to do
11715 this for enums because it will already have been done in
11718 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11719 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11722 else if (DECL_DECLARES_FUNCTION_P (t
))
11726 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11727 ++processing_template_decl
;
11728 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11729 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11730 --processing_template_decl
;
11731 set_current_access_from_decl (r
);
11732 finish_member_declaration (r
);
11733 /* Instantiate members marked with attribute used. */
11734 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11736 if (TREE_CODE (r
) == FUNCTION_DECL
11737 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11738 cp_check_omp_declare_reduction (r
);
11740 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11741 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11742 /* A closure type for a lambda in an NSDMI or default argument.
11743 Ignore it; it will be regenerated when needed. */;
11746 /* Build new TYPE_FIELDS. */
11747 if (TREE_CODE (t
) == STATIC_ASSERT
)
11751 ++c_inhibit_evaluation_warnings
;
11753 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11754 tf_warning_or_error
, NULL_TREE
,
11755 /*integral_constant_expression_p=*/true);
11756 --c_inhibit_evaluation_warnings
;
11758 finish_static_assert (condition
,
11759 STATIC_ASSERT_MESSAGE (t
),
11760 STATIC_ASSERT_SOURCE_LOCATION (t
),
11761 /*member_p=*/true);
11763 else if (TREE_CODE (t
) != CONST_DECL
)
11766 tree vec
= NULL_TREE
;
11769 /* The file and line for this declaration, to
11770 assist in error message reporting. Since we
11771 called push_tinst_level above, we don't need to
11773 input_location
= DECL_SOURCE_LOCATION (t
);
11775 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11776 ++processing_template_decl
;
11777 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11778 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11779 --processing_template_decl
;
11781 if (TREE_CODE (r
) == TREE_VEC
)
11783 /* A capture pack became multiple fields. */
11785 len
= TREE_VEC_LENGTH (vec
);
11788 for (int i
= 0; i
< len
; ++i
)
11791 r
= TREE_VEC_ELT (vec
, i
);
11796 [t]he initialization (and any associated
11797 side-effects) of a static data member does
11798 not occur unless the static data member is
11799 itself used in a way that requires the
11800 definition of the static data member to
11803 Therefore, we do not substitute into the
11804 initialized for the static data member here. */
11805 finish_static_data_member_decl
11807 /*init=*/NULL_TREE
,
11808 /*init_const_expr_p=*/false,
11809 /*asmspec_tree=*/NULL_TREE
,
11811 /* Instantiate members marked with attribute used. */
11812 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11815 else if (TREE_CODE (r
) == FIELD_DECL
)
11817 /* Determine whether R has a valid type and can be
11818 completed later. If R is invalid, then its type
11819 is replaced by error_mark_node. */
11820 tree rtype
= TREE_TYPE (r
);
11821 if (can_complete_type_without_circularity (rtype
))
11822 complete_type (rtype
);
11824 if (!complete_or_array_type_p (rtype
))
11826 /* If R's type couldn't be completed and
11827 it isn't a flexible array member (whose
11828 type is incomplete by definition) give
11830 cxx_incomplete_type_error (r
, rtype
);
11831 TREE_TYPE (r
) = error_mark_node
;
11833 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11834 && TYPE_DOMAIN (rtype
) == NULL_TREE
11835 && (TREE_CODE (type
) == UNION_TYPE
11836 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11838 error ("flexible array member %qD in union", r
);
11839 TREE_TYPE (r
) = error_mark_node
;
11841 else if (!verify_type_context (input_location
,
11842 TCTX_FIELD
, rtype
))
11843 TREE_TYPE (r
) = error_mark_node
;
11846 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11847 such a thing will already have been added to the field
11848 list by tsubst_enum in finish_member_declaration in the
11849 CLASSTYPE_NESTED_UTDS case above. */
11850 if (!(TREE_CODE (r
) == TYPE_DECL
11851 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11852 && DECL_ARTIFICIAL (r
)))
11854 set_current_access_from_decl (r
);
11855 finish_member_declaration (r
);
11863 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11864 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11866 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11868 tree friend_type
= t
;
11869 bool adjust_processing_template_decl
= false;
11871 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11873 /* template <class T> friend class C; */
11874 friend_type
= tsubst_friend_class (friend_type
, args
);
11875 adjust_processing_template_decl
= true;
11877 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11879 /* template <class T> friend class C::D; */
11880 friend_type
= tsubst (friend_type
, args
,
11881 tf_warning_or_error
, NULL_TREE
);
11882 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11883 friend_type
= TREE_TYPE (friend_type
);
11884 adjust_processing_template_decl
= true;
11886 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11887 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11889 /* This could be either
11893 when dependent_type_p is false or
11895 template <class U> friend class T::C;
11898 /* Bump processing_template_decl in case this is something like
11899 template <class T> friend struct A<T>::B. */
11900 ++processing_template_decl
;
11901 friend_type
= tsubst (friend_type
, args
,
11902 tf_warning_or_error
, NULL_TREE
);
11903 if (dependent_type_p (friend_type
))
11904 adjust_processing_template_decl
= true;
11905 --processing_template_decl
;
11907 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11908 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11909 && TYPE_HIDDEN_P (friend_type
))
11913 where C hasn't been declared yet. Let's lookup name
11914 from namespace scope directly, bypassing any name that
11915 come from dependent base class. */
11916 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11918 /* The call to xref_tag_from_type does injection for friend
11920 push_nested_namespace (ns
);
11922 xref_tag_from_type (friend_type
, NULL_TREE
,
11923 /*tag_scope=*/ts_current
);
11924 pop_nested_namespace (ns
);
11926 else if (uses_template_parms (friend_type
))
11927 /* friend class C<T>; */
11928 friend_type
= tsubst (friend_type
, args
,
11929 tf_warning_or_error
, NULL_TREE
);
11934 where C is already declared or
11936 friend class C<int>;
11938 We don't have to do anything in these cases. */
11940 if (adjust_processing_template_decl
)
11941 /* Trick make_friend_class into realizing that the friend
11942 we're adding is a template, not an ordinary class. It's
11943 important that we use make_friend_class since it will
11944 perform some error-checking and output cross-reference
11946 ++processing_template_decl
;
11948 if (friend_type
!= error_mark_node
)
11949 make_friend_class (type
, friend_type
, /*complain=*/false);
11951 if (adjust_processing_template_decl
)
11952 --processing_template_decl
;
11956 /* Build new DECL_FRIENDLIST. */
11959 /* The file and line for this declaration, to
11960 assist in error message reporting. Since we
11961 called push_tinst_level above, we don't need to
11963 input_location
= DECL_SOURCE_LOCATION (t
);
11965 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11967 ++processing_template_decl
;
11968 push_deferring_access_checks (dk_no_check
);
11971 r
= tsubst_friend_function (t
, args
);
11972 add_friend (type
, r
, /*complain=*/false);
11973 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11975 pop_deferring_access_checks ();
11976 --processing_template_decl
;
11984 /* Restore these before substituting into the lambda capture
11986 cp_unevaluated_operand
= saved_unevaluated_operand
;
11987 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11990 /* Set the file and line number information to whatever is given for
11991 the class itself. This puts error messages involving generated
11992 implicit functions at a predictable point, and the same point
11993 that would be used for non-template classes. */
11994 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11996 unreverse_member_declarations (type
);
11997 finish_struct_1 (type
);
11998 TYPE_BEING_DEFINED (type
) = 0;
12000 /* We don't instantiate default arguments for member functions. 14.7.1:
12002 The implicit instantiation of a class template specialization causes
12003 the implicit instantiation of the declarations, but not of the
12004 definitions or default arguments, of the class member functions,
12005 member classes, static data members and member templates.... */
12007 /* Some typedefs referenced from within the template code need to be access
12008 checked at template instantiation time, i.e now. These types were
12009 added to the template at parsing time. Let's get those and perform
12010 the access checks then. */
12011 perform_typedefs_access_check (pattern
, args
);
12012 perform_deferred_access_checks (tf_warning_or_error
);
12013 pop_nested_class ();
12014 maximum_field_alignment
= saved_maximum_field_alignment
;
12016 pop_from_top_level ();
12017 pop_tinst_level ();
12019 /* The vtable for a template class can be emitted in any translation
12020 unit in which the class is instantiated. When there is no key
12021 method, however, finish_struct_1 will already have added TYPE to
12022 the keyed_classes. */
12023 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12024 vec_safe_push (keyed_classes
, type
);
12029 /* Wrapper for instantiate_class_template_1. */
12032 instantiate_class_template (tree type
)
12035 timevar_push (TV_TEMPLATE_INST
);
12036 ret
= instantiate_class_template_1 (type
);
12037 timevar_pop (TV_TEMPLATE_INST
);
12042 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12048 else if (TYPE_P (t
))
12049 r
= tsubst (t
, args
, complain
, in_decl
);
12052 if (!(complain
& tf_warning
))
12053 ++c_inhibit_evaluation_warnings
;
12054 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12055 /*integral_constant_expression_p=*/true);
12056 if (!(complain
& tf_warning
))
12057 --c_inhibit_evaluation_warnings
;
12063 /* Given a function parameter pack TMPL_PARM and some function parameters
12064 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12065 and set *SPEC_P to point at the next point in the list. */
12068 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12070 /* Collect all of the extra "packed" parameters into an
12073 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12074 tree spec_parm
= *spec_p
;
12077 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12079 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12082 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12083 parmvec
= make_tree_vec (len
);
12084 spec_parm
= *spec_p
;
12085 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12087 tree elt
= spec_parm
;
12088 if (DECL_PACK_P (elt
))
12089 elt
= make_pack_expansion (elt
);
12090 TREE_VEC_ELT (parmvec
, i
) = elt
;
12093 /* Build the argument packs. */
12094 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12095 *spec_p
= spec_parm
;
12100 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12101 NONTYPE_ARGUMENT_PACK. */
12104 make_fnparm_pack (tree spec_parm
)
12106 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12109 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12110 pack expansion with no extra args, 2 if it has extra args, or 0
12111 if it is not a pack expansion. */
12114 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12116 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12117 /* We're being called before this happens in tsubst_pack_expansion. */
12118 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12119 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12120 if (i
>= TREE_VEC_LENGTH (vec
))
12122 tree elt
= TREE_VEC_ELT (vec
, i
);
12124 /* A decl pack is itself an expansion. */
12125 elt
= TREE_TYPE (elt
);
12126 if (!PACK_EXPANSION_P (elt
))
12128 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12134 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12137 make_argument_pack_select (tree arg_pack
, unsigned index
)
12139 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12141 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12142 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12147 /* This is a subroutine of tsubst_pack_expansion.
12149 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12150 mechanism to store the (non complete list of) arguments of the
12151 substitution and return a non substituted pack expansion, in order
12152 to wait for when we have enough arguments to really perform the
12156 use_pack_expansion_extra_args_p (tree parm_packs
,
12158 bool has_empty_arg
)
12160 /* If one pack has an expansion and another pack has a normal
12161 argument or if one pack has an empty argument and an another
12162 one hasn't then tsubst_pack_expansion cannot perform the
12163 substitution and need to fall back on the
12164 PACK_EXPANSION_EXTRA mechanism. */
12165 if (parm_packs
== NULL_TREE
)
12167 else if (has_empty_arg
)
12169 /* If all the actual packs are pack expansions, we can still
12170 subsitute directly. */
12171 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12173 tree a
= TREE_VALUE (p
);
12174 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12175 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12176 a
= ARGUMENT_PACK_ARGS (a
);
12177 if (TREE_VEC_LENGTH (a
) == 1)
12178 a
= TREE_VEC_ELT (a
, 0);
12179 if (PACK_EXPANSION_P (a
))
12186 bool has_expansion_arg
= false;
12187 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12189 bool has_non_expansion_arg
= false;
12190 for (tree parm_pack
= parm_packs
;
12192 parm_pack
= TREE_CHAIN (parm_pack
))
12194 tree arg
= TREE_VALUE (parm_pack
);
12196 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12198 /* We can't substitute a pack expansion with extra args into
12202 has_expansion_arg
= true;
12204 has_non_expansion_arg
= true;
12207 if (has_expansion_arg
&& has_non_expansion_arg
)
12213 /* [temp.variadic]/6 says that:
12215 The instantiation of a pack expansion [...]
12216 produces a list E1,E2, ..., En, where N is the number of elements
12217 in the pack expansion parameters.
12219 This subroutine of tsubst_pack_expansion produces one of these Ei.
12221 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12222 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12223 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12224 INDEX is the index 'i' of the element Ei to produce. ARGS,
12225 COMPLAIN, and IN_DECL are the same parameters as for the
12226 tsubst_pack_expansion function.
12228 The function returns the resulting Ei upon successful completion,
12229 or error_mark_node.
12231 Note that this function possibly modifies the ARGS parameter, so
12232 it's the responsibility of the caller to restore it. */
12235 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12238 tree args
/* This parm gets
12240 tsubst_flags_t complain
,
12244 bool ith_elem_is_expansion
= false;
12246 /* For each parameter pack, change the substitution of the parameter
12247 pack to the ith argument in its argument pack, then expand the
12249 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12251 tree parm
= TREE_PURPOSE (pack
);
12252 tree arg_pack
= TREE_VALUE (pack
);
12253 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12255 ith_elem_is_expansion
|=
12256 argument_pack_element_is_expansion_p (arg_pack
, index
);
12258 /* Select the Ith argument from the pack. */
12259 if (TREE_CODE (parm
) == PARM_DECL
12261 || TREE_CODE (parm
) == FIELD_DECL
)
12265 aps
= make_argument_pack_select (arg_pack
, index
);
12266 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12267 return error_mark_node
;
12268 register_local_specialization (aps
, parm
);
12271 aps
= retrieve_local_specialization (parm
);
12276 template_parm_level_and_index (parm
, &level
, &idx
);
12280 aps
= make_argument_pack_select (arg_pack
, index
);
12281 /* Update the corresponding argument. */
12282 TMPL_ARG (args
, level
, idx
) = aps
;
12285 /* Re-use the ARGUMENT_PACK_SELECT. */
12286 aps
= TMPL_ARG (args
, level
, idx
);
12288 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12291 /* Substitute into the PATTERN with the (possibly altered)
12293 if (pattern
== in_decl
)
12294 /* Expanding a fixed parameter pack from
12295 coerce_template_parameter_pack. */
12296 t
= tsubst_decl (pattern
, args
, complain
);
12297 else if (pattern
== error_mark_node
)
12298 t
= error_mark_node
;
12299 else if (!TYPE_P (pattern
))
12300 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12301 /*integral_constant_expression_p=*/false);
12303 t
= tsubst (pattern
, args
, complain
, in_decl
);
12305 /* If the Ith argument pack element is a pack expansion, then
12306 the Ith element resulting from the substituting is going to
12307 be a pack expansion as well. */
12308 if (ith_elem_is_expansion
)
12309 t
= make_pack_expansion (t
, complain
);
12314 /* When the unexpanded parameter pack in a fold expression expands to an empty
12315 sequence, the value of the expression is as follows; the program is
12316 ill-formed if the operator is not listed in this table.
12323 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12325 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12326 if (!FOLD_EXPR_MODIFY_P (t
))
12329 case TRUTH_ANDIF_EXPR
:
12330 return boolean_true_node
;
12331 case TRUTH_ORIF_EXPR
:
12332 return boolean_false_node
;
12333 case COMPOUND_EXPR
:
12339 if (complain
& tf_error
)
12340 error_at (location_of (t
),
12341 "fold of empty expansion over %O", code
);
12342 return error_mark_node
;
12345 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12346 form an expression that combines the two terms using the
12350 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12352 tree op
= FOLD_EXPR_OP (t
);
12353 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12355 // Handle compound assignment operators.
12356 if (FOLD_EXPR_MODIFY_P (t
))
12357 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12361 case COMPOUND_EXPR
:
12362 return build_x_compound_expr (input_location
, left
, right
, complain
);
12364 return build_x_binary_op (input_location
, code
,
12365 left
, TREE_CODE (left
),
12366 right
, TREE_CODE (right
),
12372 /* Substitute ARGS into the pack of a fold expression T. */
12375 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12377 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12380 /* Substitute ARGS into the pack of a fold expression T. */
12383 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12385 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12388 /* Expand a PACK of arguments into a grouped as left fold.
12389 Given a pack containing elements A0, A1, ..., An and an
12390 operator @, this builds the expression:
12392 ((A0 @ A1) @ A2) ... @ An
12394 Note that PACK must not be empty.
12396 The operator is defined by the original fold expression T. */
12399 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12401 tree left
= TREE_VEC_ELT (pack
, 0);
12402 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12404 tree right
= TREE_VEC_ELT (pack
, i
);
12405 left
= fold_expression (t
, left
, right
, complain
);
12410 /* Substitute into a unary left fold expression. */
12413 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12416 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12417 if (pack
== error_mark_node
)
12418 return error_mark_node
;
12419 if (PACK_EXPANSION_P (pack
))
12421 tree r
= copy_node (t
);
12422 FOLD_EXPR_PACK (r
) = pack
;
12425 if (TREE_VEC_LENGTH (pack
) == 0)
12426 return expand_empty_fold (t
, complain
);
12428 return expand_left_fold (t
, pack
, complain
);
12431 /* Substitute into a binary left fold expression.
12433 Do ths by building a single (non-empty) vector of argumnts and
12434 building the expression from those elements. */
12437 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12440 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12441 if (pack
== error_mark_node
)
12442 return error_mark_node
;
12443 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12444 if (init
== error_mark_node
)
12445 return error_mark_node
;
12447 if (PACK_EXPANSION_P (pack
))
12449 tree r
= copy_node (t
);
12450 FOLD_EXPR_PACK (r
) = pack
;
12451 FOLD_EXPR_INIT (r
) = init
;
12455 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12456 TREE_VEC_ELT (vec
, 0) = init
;
12457 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12458 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12460 return expand_left_fold (t
, vec
, complain
);
12463 /* Expand a PACK of arguments into a grouped as right fold.
12464 Given a pack containing elementns A0, A1, ..., and an
12465 operator @, this builds the expression:
12467 A0@ ... (An-2 @ (An-1 @ An))
12469 Note that PACK must not be empty.
12471 The operator is defined by the original fold expression T. */
12474 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12476 // Build the expression.
12477 int n
= TREE_VEC_LENGTH (pack
);
12478 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12479 for (--n
; n
!= 0; --n
)
12481 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12482 right
= fold_expression (t
, left
, right
, complain
);
12487 /* Substitute into a unary right fold expression. */
12490 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12493 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12494 if (pack
== error_mark_node
)
12495 return error_mark_node
;
12496 if (PACK_EXPANSION_P (pack
))
12498 tree r
= copy_node (t
);
12499 FOLD_EXPR_PACK (r
) = pack
;
12502 if (TREE_VEC_LENGTH (pack
) == 0)
12503 return expand_empty_fold (t
, complain
);
12505 return expand_right_fold (t
, pack
, complain
);
12508 /* Substitute into a binary right fold expression.
12510 Do ths by building a single (non-empty) vector of arguments and
12511 building the expression from those elements. */
12514 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12517 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12518 if (pack
== error_mark_node
)
12519 return error_mark_node
;
12520 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12521 if (init
== error_mark_node
)
12522 return error_mark_node
;
12524 if (PACK_EXPANSION_P (pack
))
12526 tree r
= copy_node (t
);
12527 FOLD_EXPR_PACK (r
) = pack
;
12528 FOLD_EXPR_INIT (r
) = init
;
12532 int n
= TREE_VEC_LENGTH (pack
);
12533 tree vec
= make_tree_vec (n
+ 1);
12534 for (int i
= 0; i
< n
; ++i
)
12535 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12536 TREE_VEC_ELT (vec
, n
) = init
;
12538 return expand_right_fold (t
, vec
, complain
);
12541 /* Walk through the pattern of a pack expansion, adding everything in
12542 local_specializations to a list. */
12547 hash_set
<tree
> internal
;
12549 tsubst_flags_t complain
;
12551 el_data (tsubst_flags_t c
)
12552 : extra (NULL_TREE
), complain (c
) {}
12555 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12557 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12558 tree
*extra
= &data
.extra
;
12559 tsubst_flags_t complain
= data
.complain
;
12561 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12562 /* Remember local typedefs (85214). */
12563 tp
= &TYPE_NAME (*tp
);
12565 if (TREE_CODE (*tp
) == DECL_EXPR
)
12566 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12567 else if (tree spec
= retrieve_local_specialization (*tp
))
12569 if (data
.internal
.contains (*tp
))
12570 /* Don't mess with variables declared within the pattern. */
12572 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12574 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12575 tree args
= ARGUMENT_PACK_ARGS (spec
);
12576 if (TREE_VEC_LENGTH (args
) == 1)
12578 tree elt
= TREE_VEC_ELT (args
, 0);
12579 if (PACK_EXPANSION_P (elt
))
12580 elt
= PACK_EXPANSION_PATTERN (elt
);
12581 if (DECL_PACK_P (elt
))
12584 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12586 /* Handle lambda capture here, since we aren't doing any
12587 substitution now, and so tsubst_copy won't call
12588 process_outer_var_ref. */
12589 tree args
= ARGUMENT_PACK_ARGS (spec
);
12590 int len
= TREE_VEC_LENGTH (args
);
12591 for (int i
= 0; i
< len
; ++i
)
12593 tree arg
= TREE_VEC_ELT (args
, i
);
12595 if (outer_automatic_var_p (arg
))
12596 carg
= process_outer_var_ref (arg
, complain
);
12599 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12603 spec
= copy_node (spec
);
12604 args
= copy_node (args
);
12605 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12606 register_local_specialization (spec
, *tp
);
12608 TREE_VEC_ELT (args
, i
) = carg
;
12613 if (outer_automatic_var_p (spec
))
12614 spec
= process_outer_var_ref (spec
, complain
);
12615 *extra
= tree_cons (*tp
, spec
, *extra
);
12620 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12622 el_data
data (complain
);
12623 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12627 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12628 for use in PACK_EXPANSION_EXTRA_ARGS. */
12631 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12634 if (local_specializations
)
12635 if (tree locals
= extract_local_specs (pattern
, complain
))
12636 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12640 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12641 normal template args to ARGS. */
12644 add_extra_args (tree extra
, tree args
)
12646 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12648 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12650 /* The partial instantiation involved local declarations collected in
12651 extract_local_specs; map from the general template to our local
12653 tree gen
= TREE_PURPOSE (elt
);
12654 tree inst
= TREE_VALUE (elt
);
12656 if (tree local
= retrieve_local_specialization (inst
))
12658 /* else inst is already a full instantiation of the pack. */
12659 register_local_specialization (inst
, gen
);
12661 gcc_assert (!TREE_PURPOSE (extra
));
12662 extra
= TREE_VALUE (extra
);
12665 /* I think we should always be able to substitute dependent args into the
12666 pattern. If that turns out to be incorrect in some cases, enable the
12667 alternate code (and add complain/in_decl parms to this function). */
12668 gcc_checking_assert (!uses_template_parms (extra
));
12670 if (!uses_template_parms (extra
))
12672 gcc_unreachable ();
12673 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12674 args
= add_outermost_template_args (args
, extra
);
12678 args
= add_to_template_args (extra
, args
);
12682 /* Substitute ARGS into T, which is an pack expansion
12683 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12684 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12685 (if only a partial substitution could be performed) or
12686 ERROR_MARK_NODE if there was an error. */
12688 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12692 tree pack
, packs
= NULL_TREE
;
12693 bool unsubstituted_packs
= false;
12694 bool unsubstituted_fn_pack
= false;
12697 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12698 bool need_local_specializations
= false;
12701 gcc_assert (PACK_EXPANSION_P (t
));
12702 pattern
= PACK_EXPANSION_PATTERN (t
);
12704 /* Add in any args remembered from an earlier partial instantiation. */
12705 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12707 levels
= TMPL_ARGS_DEPTH (args
);
12709 /* Determine the argument packs that will instantiate the parameter
12710 packs used in the expansion expression. While we're at it,
12711 compute the number of arguments to be expanded and make sure it
12713 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12714 pack
= TREE_CHAIN (pack
))
12716 tree parm_pack
= TREE_VALUE (pack
);
12717 tree arg_pack
= NULL_TREE
;
12718 tree orig_arg
= NULL_TREE
;
12721 if (TREE_CODE (parm_pack
) == BASES
)
12723 gcc_assert (parm_pack
== pattern
);
12724 if (BASES_DIRECT (parm_pack
))
12725 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12730 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12731 args
, complain
, in_decl
,
12734 else if (builtin_pack_call_p (parm_pack
))
12736 if (parm_pack
!= pattern
)
12738 if (complain
& tf_error
)
12739 sorry ("%qE is not the entire pattern of the pack expansion",
12741 return error_mark_node
;
12743 return expand_builtin_pack_call (parm_pack
, args
,
12744 complain
, in_decl
);
12746 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12748 /* We know we have correct local_specializations if this
12749 expansion is at function scope, or if we're dealing with a
12750 local parameter in a requires expression; for the latter,
12751 tsubst_requires_expr set it up appropriately. */
12752 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12753 arg_pack
= retrieve_local_specialization (parm_pack
);
12755 /* We can't rely on local_specializations for a parameter
12756 name used later in a function declaration (such as in a
12757 late-specified return type). Even if it exists, it might
12758 have the wrong value for a recursive call. */
12759 need_local_specializations
= true;
12763 /* This parameter pack was used in an unevaluated context. Just
12764 make a dummy decl, since it's only used for its type. */
12765 ++cp_unevaluated_operand
;
12766 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12767 --cp_unevaluated_operand
;
12768 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12769 /* Partial instantiation of the parm_pack, we can't build
12770 up an argument pack yet. */
12771 arg_pack
= NULL_TREE
;
12773 arg_pack
= make_fnparm_pack (arg_pack
);
12775 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12776 /* This argument pack isn't fully instantiated yet. We set this
12777 flag rather than clear arg_pack because we do want to do the
12778 optimization below, and we don't want to substitute directly
12779 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12780 where it isn't expected). */
12781 unsubstituted_fn_pack
= true;
12783 else if (is_capture_proxy (parm_pack
))
12785 arg_pack
= retrieve_local_specialization (parm_pack
);
12786 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12787 unsubstituted_fn_pack
= true;
12792 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12793 if (level
<= levels
)
12794 arg_pack
= TMPL_ARG (args
, level
, idx
);
12797 orig_arg
= arg_pack
;
12798 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12799 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12801 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12802 /* This can only happen if we forget to expand an argument
12803 pack somewhere else. Just return an error, silently. */
12805 result
= make_tree_vec (1);
12806 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12813 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12815 /* Don't bother trying to do a partial substitution with
12816 incomplete packs; we'll try again after deduction. */
12817 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12822 else if (len
!= my_len
12823 && !unsubstituted_fn_pack
)
12825 if (!(complain
& tf_error
))
12826 /* Fail quietly. */;
12827 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12828 error ("mismatched argument pack lengths while expanding %qT",
12831 error ("mismatched argument pack lengths while expanding %qE",
12833 return error_mark_node
;
12836 /* Keep track of the parameter packs and their corresponding
12838 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12839 TREE_TYPE (packs
) = orig_arg
;
12843 /* We can't substitute for this parameter pack. We use a flag as
12844 well as the missing_level counter because function parameter
12845 packs don't have a level. */
12846 if (!(processing_template_decl
|| is_auto (parm_pack
)))
12848 gcc_unreachable ();
12850 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12851 unsubstituted_packs
= true;
12855 /* If the expansion is just T..., return the matching argument pack, unless
12856 we need to call convert_from_reference on all the elements. This is an
12857 important optimization; see c++/68422. */
12858 if (!unsubstituted_packs
12859 && TREE_PURPOSE (packs
) == pattern
)
12861 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12863 /* If the argument pack is a single pack expansion, pull it out. */
12864 if (TREE_VEC_LENGTH (args
) == 1
12865 && pack_expansion_args_count (args
))
12866 return TREE_VEC_ELT (args
, 0);
12868 /* Types need no adjustment, nor does sizeof..., and if we still have
12869 some pack expansion args we won't do anything yet. */
12870 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12871 || PACK_EXPANSION_SIZEOF_P (t
)
12872 || pack_expansion_args_count (args
))
12874 /* Also optimize expression pack expansions if we can tell that the
12875 elements won't have reference type. */
12876 tree type
= TREE_TYPE (pattern
);
12877 if (type
&& !TYPE_REF_P (type
)
12878 && !PACK_EXPANSION_P (type
)
12879 && !WILDCARD_TYPE_P (type
))
12881 /* Otherwise use the normal path so we get convert_from_reference. */
12884 /* We cannot expand this expansion expression, because we don't have
12885 all of the argument packs we need. */
12886 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12888 /* We got some full packs, but we can't substitute them in until we
12889 have values for all the packs. So remember these until then. */
12891 t
= make_pack_expansion (pattern
, complain
);
12892 PACK_EXPANSION_EXTRA_ARGS (t
)
12893 = build_extra_args (pattern
, args
, complain
);
12896 else if (unsubstituted_packs
)
12898 /* There were no real arguments, we're just replacing a parameter
12899 pack with another version of itself. Substitute into the
12900 pattern and return a PACK_EXPANSION_*. The caller will need to
12902 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12903 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12904 /*integral_constant_expression_p=*/false);
12906 t
= tsubst (pattern
, args
, complain
, in_decl
);
12907 t
= make_pack_expansion (t
, complain
);
12911 gcc_assert (len
>= 0);
12913 if (need_local_specializations
)
12915 /* We're in a late-specified return type, so create our own local
12916 specializations map; the current map is either NULL or (in the
12917 case of recursive unification) might have bindings that we don't
12918 want to use or alter. */
12919 saved_local_specializations
= local_specializations
;
12920 local_specializations
= new hash_map
<tree
, tree
>;
12923 /* For each argument in each argument pack, substitute into the
12925 result
= make_tree_vec (len
);
12926 tree elem_args
= copy_template_args (args
);
12927 for (i
= 0; i
< len
; ++i
)
12929 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12931 elem_args
, complain
,
12933 TREE_VEC_ELT (result
, i
) = t
;
12934 if (t
== error_mark_node
)
12936 result
= error_mark_node
;
12941 /* Update ARGS to restore the substitution from parameter packs to
12942 their argument packs. */
12943 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12945 tree parm
= TREE_PURPOSE (pack
);
12947 if (TREE_CODE (parm
) == PARM_DECL
12949 || TREE_CODE (parm
) == FIELD_DECL
)
12950 register_local_specialization (TREE_TYPE (pack
), parm
);
12955 if (TREE_VALUE (pack
) == NULL_TREE
)
12958 template_parm_level_and_index (parm
, &level
, &idx
);
12960 /* Update the corresponding argument. */
12961 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12962 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12965 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12969 if (need_local_specializations
)
12971 delete local_specializations
;
12972 local_specializations
= saved_local_specializations
;
12975 /* If the dependent pack arguments were such that we end up with only a
12976 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12977 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12978 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12979 return TREE_VEC_ELT (result
, 0);
12984 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12985 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12986 parameter packs; all parms generated from a function parameter pack will
12987 have the same DECL_PARM_INDEX. */
12990 get_pattern_parm (tree parm
, tree tmpl
)
12992 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12995 if (DECL_ARTIFICIAL (parm
))
12997 for (patparm
= DECL_ARGUMENTS (pattern
);
12998 patparm
; patparm
= DECL_CHAIN (patparm
))
12999 if (DECL_ARTIFICIAL (patparm
)
13000 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13005 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13006 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13007 gcc_assert (DECL_PARM_INDEX (patparm
)
13008 == DECL_PARM_INDEX (parm
));
13014 /* Make an argument pack out of the TREE_VEC VEC. */
13017 make_argument_pack (tree vec
)
13020 tree elt
= TREE_VEC_ELT (vec
, 0);
13022 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13025 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13026 TREE_CONSTANT (pack
) = 1;
13028 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13032 /* Return an exact copy of template args T that can be modified
13036 copy_template_args (tree t
)
13038 if (t
== error_mark_node
)
13041 int len
= TREE_VEC_LENGTH (t
);
13042 tree new_vec
= make_tree_vec (len
);
13044 for (int i
= 0; i
< len
; ++i
)
13046 tree elt
= TREE_VEC_ELT (t
, i
);
13047 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13048 elt
= copy_template_args (elt
);
13049 TREE_VEC_ELT (new_vec
, i
) = elt
;
13052 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13053 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13058 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13061 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13064 /* Substitute into each of the arguments. */
13065 tree new_arg
= TYPE_P (orig_arg
)
13066 ? cxx_make_type (TREE_CODE (orig_arg
))
13067 : make_node (TREE_CODE (orig_arg
));
13069 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13070 args
, complain
, in_decl
);
13071 if (pack_args
== error_mark_node
)
13072 new_arg
= error_mark_node
;
13074 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13076 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13077 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13082 /* Substitute ARGS into the vector or list of template arguments T. */
13085 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13088 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13091 if (t
== error_mark_node
)
13092 return error_mark_node
;
13094 len
= TREE_VEC_LENGTH (t
);
13095 elts
= XALLOCAVEC (tree
, len
);
13097 for (i
= 0; i
< len
; i
++)
13099 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13102 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13103 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13104 else if (PACK_EXPANSION_P (orig_arg
))
13106 /* Substitute into an expansion expression. */
13107 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13109 if (TREE_CODE (new_arg
) == TREE_VEC
)
13110 /* Add to the expanded length adjustment the number of
13111 expanded arguments. We subtract one from this
13112 measurement, because the argument pack expression
13113 itself is already counted as 1 in
13114 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13115 the argument pack is empty. */
13116 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13118 else if (ARGUMENT_PACK_P (orig_arg
))
13119 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13121 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13123 if (new_arg
== error_mark_node
)
13124 return error_mark_node
;
13127 if (new_arg
!= orig_arg
)
13134 /* Make space for the expanded arguments coming from template
13136 t
= make_tree_vec (len
+ expanded_len_adjust
);
13137 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13138 arguments for a member template.
13139 In that case each TREE_VEC in ORIG_T represents a level of template
13140 arguments, and ORIG_T won't carry any non defaulted argument count.
13141 It will rather be the nested TREE_VECs that will carry one.
13142 In other words, ORIG_T carries a non defaulted argument count only
13143 if it doesn't contain any nested TREE_VEC. */
13144 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13146 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13147 count
+= expanded_len_adjust
;
13148 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13150 for (i
= 0, out
= 0; i
< len
; i
++)
13152 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13153 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13154 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13158 /* Now expand the template argument pack "in place". */
13159 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13160 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13164 TREE_VEC_ELT (t
, out
) = elts
[i
];
13172 /* Substitute ARGS into one level PARMS of template parameters. */
13175 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13177 if (parms
== error_mark_node
)
13178 return error_mark_node
;
13180 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13182 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13184 tree tuple
= TREE_VEC_ELT (parms
, i
);
13186 if (tuple
== error_mark_node
)
13189 TREE_VEC_ELT (new_vec
, i
) =
13190 tsubst_template_parm (tuple
, args
, complain
);
13196 /* Return the result of substituting ARGS into the template parameters
13197 given by PARMS. If there are m levels of ARGS and m + n levels of
13198 PARMS, then the result will contain n levels of PARMS. For
13199 example, if PARMS is `template <class T> template <class U>
13200 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13201 result will be `template <int*, double, class V>'. */
13204 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13206 tree r
= NULL_TREE
;
13209 /* When substituting into a template, we must set
13210 PROCESSING_TEMPLATE_DECL as the template parameters may be
13211 dependent if they are based on one-another, and the dependency
13212 predicates are short-circuit outside of templates. */
13213 ++processing_template_decl
;
13215 for (new_parms
= &r
;
13216 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13217 new_parms
= &(TREE_CHAIN (*new_parms
)),
13218 parms
= TREE_CHAIN (parms
))
13220 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13223 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13224 - TMPL_ARGS_DEPTH (args
)),
13225 new_vec
, NULL_TREE
);
13226 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13227 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13230 --processing_template_decl
;
13235 /* Return the result of substituting ARGS into one template parameter
13236 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13237 parameter and which TREE_PURPOSE is the default argument of the
13238 template parameter. */
13241 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13243 tree default_value
, parm_decl
;
13245 if (args
== NULL_TREE
13247 || t
== error_mark_node
)
13250 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13252 default_value
= TREE_PURPOSE (t
);
13253 parm_decl
= TREE_VALUE (t
);
13254 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13256 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13257 if (TREE_CODE (parm_decl
) == PARM_DECL
13258 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13259 parm_decl
= error_mark_node
;
13260 default_value
= tsubst_template_arg (default_value
, args
,
13261 complain
, NULL_TREE
);
13262 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13264 tree r
= build_tree_list (default_value
, parm_decl
);
13265 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13269 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13270 type T. If T is not an aggregate or enumeration type, it is
13271 handled as if by tsubst. IN_DECL is as for tsubst. If
13272 ENTERING_SCOPE is nonzero, T is the context for a template which
13273 we are presently tsubst'ing. Return the substituted value. */
13276 tsubst_aggr_type (tree t
,
13278 tsubst_flags_t complain
,
13280 int entering_scope
)
13282 if (t
== NULL_TREE
)
13285 switch (TREE_CODE (t
))
13288 if (TYPE_PTRMEMFUNC_P (t
))
13289 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13291 /* Fall through. */
13292 case ENUMERAL_TYPE
:
13294 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13300 /* In "sizeof(X<I>)" we need to evaluate "I". */
13303 /* First, determine the context for the type we are looking
13305 context
= TYPE_CONTEXT (t
);
13306 if (context
&& TYPE_P (context
))
13308 context
= tsubst_aggr_type (context
, args
, complain
,
13309 in_decl
, /*entering_scope=*/1);
13310 /* If context is a nested class inside a class template,
13311 it may still need to be instantiated (c++/33959). */
13312 context
= complete_type (context
);
13315 /* Then, figure out what arguments are appropriate for the
13316 type we are trying to find. For example, given:
13318 template <class T> struct S;
13319 template <class T, class U> void f(T, U) { S<U> su; }
13321 and supposing that we are instantiating f<int, double>,
13322 then our ARGS will be {int, double}, but, when looking up
13323 S we only want {double}. */
13324 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13325 complain
, in_decl
);
13326 if (argvec
== error_mark_node
)
13327 r
= error_mark_node
;
13330 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13331 entering_scope
, complain
);
13332 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13338 /* This is not a template type, so there's nothing to do. */
13342 return tsubst (t
, args
, complain
, in_decl
);
13346 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13348 /* Substitute into the default argument ARG (a default argument for
13349 FN), which has the indicated TYPE. */
13352 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13353 tsubst_flags_t complain
)
13355 int errs
= errorcount
+ sorrycount
;
13357 /* This can happen in invalid code. */
13358 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13361 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13362 parm
= chain_index (parmnum
, parm
);
13363 tree parmtype
= TREE_TYPE (parm
);
13364 if (DECL_BY_REFERENCE (parm
))
13365 parmtype
= TREE_TYPE (parmtype
);
13366 if (parmtype
== error_mark_node
)
13367 return error_mark_node
;
13369 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13372 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13375 /* This default argument came from a template. Instantiate the
13376 default argument here, not in tsubst. In the case of
13385 we must be careful to do name lookup in the scope of S<T>,
13386 rather than in the current class. */
13387 push_to_top_level ();
13388 push_access_scope (fn
);
13389 push_deferring_access_checks (dk_no_deferred
);
13390 start_lambda_scope (parm
);
13392 /* The default argument expression may cause implicitly defined
13393 member functions to be synthesized, which will result in garbage
13394 collection. We must treat this situation as if we were within
13395 the body of function so as to avoid collecting live data on the
13398 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13399 complain
, NULL_TREE
,
13400 /*integral_constant_expression_p=*/false);
13403 finish_lambda_scope ();
13405 /* Make sure the default argument is reasonable. */
13406 arg
= check_default_argument (type
, arg
, complain
);
13408 if (errorcount
+sorrycount
> errs
13409 && (complain
& tf_warning_or_error
))
13410 inform (input_location
,
13411 " when instantiating default argument for call to %qD", fn
);
13413 pop_deferring_access_checks ();
13414 pop_access_scope (fn
);
13415 pop_from_top_level ();
13417 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13420 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13421 defarg_inst
->put (parm
, arg
);
13427 /* Substitute into all the default arguments for FN. */
13430 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13435 tmpl_args
= DECL_TI_ARGS (fn
);
13437 /* If this function is not yet instantiated, we certainly don't need
13438 its default arguments. */
13439 if (uses_template_parms (tmpl_args
))
13441 /* Don't do this again for clones. */
13442 if (DECL_CLONED_FUNCTION_P (fn
))
13446 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13448 arg
= TREE_CHAIN (arg
), ++i
)
13449 if (TREE_PURPOSE (arg
))
13450 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13452 TREE_PURPOSE (arg
),
13456 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13457 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13459 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13462 store_explicit_specifier (tree v
, tree t
)
13464 if (!explicit_specifier_map
)
13465 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13466 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13467 explicit_specifier_map
->put (v
, t
);
13470 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13473 lookup_explicit_specifier (tree v
)
13475 return *explicit_specifier_map
->get (v
);
13478 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13481 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13482 tree lambda_fntype
)
13484 tree gen_tmpl
, argvec
;
13485 hashval_t hash
= 0;
13488 /* Nobody should be tsubst'ing into non-template functions. */
13489 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13491 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13493 /* If T is not dependent, just return it. */
13494 if (!uses_template_parms (DECL_TI_ARGS (t
))
13495 && !LAMBDA_FUNCTION_P (t
))
13498 /* Calculate the most general template of which R is a
13500 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13502 /* We're substituting a lambda function under tsubst_lambda_expr but not
13503 directly from it; find the matching function we're already inside.
13504 But don't do this if T is a generic lambda with a single level of
13505 template parms, as in that case we're doing a normal instantiation. */
13506 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13507 && (!generic_lambda_fn_p (t
)
13508 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13509 return enclosing_instantiation_of (t
);
13511 /* Calculate the complete set of arguments used to
13513 argvec
= tsubst_template_args (DECL_TI_ARGS
13514 (DECL_TEMPLATE_RESULT
13515 (DECL_TI_TEMPLATE (t
))),
13516 args
, complain
, in_decl
);
13517 if (argvec
== error_mark_node
)
13518 return error_mark_node
;
13520 /* Check to see if we already have this specialization. */
13521 if (!lambda_fntype
)
13523 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13524 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13528 /* We can see more levels of arguments than parameters if
13529 there was a specialization of a member template, like
13532 template <class T> struct S { template <class U> void f(); }
13533 template <> template <class U> void S<int>::f(U);
13535 Here, we'll be substituting into the specialization,
13536 because that's where we can find the code we actually
13537 want to generate, but we'll have enough arguments for
13538 the most general template.
13540 We also deal with the peculiar case:
13542 template <class T> struct S {
13543 template <class U> friend void f();
13545 template <class U> void f() {}
13547 template void f<double>();
13549 Here, the ARGS for the instantiation of will be {int,
13550 double}. But, we only need as many ARGS as there are
13551 levels of template parameters in CODE_PATTERN. We are
13552 careful not to get fooled into reducing the ARGS in
13555 template <class T> struct S { template <class U> void f(U); }
13556 template <class T> template <> void S<T>::f(int) {}
13558 which we can spot because the pattern will be a
13559 specialization in this case. */
13560 int args_depth
= TMPL_ARGS_DEPTH (args
);
13562 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13564 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13565 args
= get_innermost_template_args (args
, parms_depth
);
13569 /* This special case arises when we have something like this:
13571 template <class T> struct S {
13572 friend void f<int>(int, double);
13575 Here, the DECL_TI_TEMPLATE for the friend declaration
13576 will be an IDENTIFIER_NODE. We are being called from
13577 tsubst_friend_function, and we want only to create a
13578 new decl (R) with appropriate types so that we can call
13579 determine_specialization. */
13580 gen_tmpl
= NULL_TREE
;
13581 argvec
= NULL_TREE
;
13584 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13586 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13587 bool member
= ctx
&& TYPE_P (ctx
);
13589 if (member
&& !closure
)
13590 ctx
= tsubst_aggr_type (ctx
, args
,
13591 complain
, t
, /*entering_scope=*/1);
13593 tree type
= (lambda_fntype
? lambda_fntype
13594 : tsubst (TREE_TYPE (t
), args
,
13595 complain
| tf_fndecl_type
, in_decl
));
13596 if (type
== error_mark_node
)
13597 return error_mark_node
;
13599 /* If we hit excessive deduction depth, the type is bogus even if
13600 it isn't error_mark_node, so don't build a decl. */
13601 if (excessive_deduction_depth
)
13602 return error_mark_node
;
13604 /* We do NOT check for matching decls pushed separately at this
13605 point, as they may not represent instantiations of this
13606 template, and in any case are considered separate under the
13608 tree r
= copy_decl (t
);
13609 DECL_USE_TEMPLATE (r
) = 0;
13610 TREE_TYPE (r
) = type
;
13611 /* Clear out the mangled name and RTL for the instantiation. */
13612 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13613 SET_DECL_RTL (r
, NULL
);
13614 /* Leave DECL_INITIAL set on deleted instantiations. */
13615 if (!DECL_DELETED_FN (r
))
13616 DECL_INITIAL (r
) = NULL_TREE
;
13617 DECL_CONTEXT (r
) = ctx
;
13619 /* Handle explicit(dependent-expr). */
13620 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13622 tree spec
= lookup_explicit_specifier (t
);
13623 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13624 /*function_p=*/false,
13626 spec
= build_explicit_specifier (spec
, complain
);
13627 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13630 /* OpenMP UDRs have the only argument a reference to the declared
13631 type. We want to diagnose if the declared type is a reference,
13632 which is invalid, but as references to references are usually
13633 quietly merged, diagnose it here. */
13634 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13637 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13638 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13639 if (TYPE_REF_P (argtype
))
13640 error_at (DECL_SOURCE_LOCATION (t
),
13641 "reference type %qT in "
13642 "%<#pragma omp declare reduction%>", argtype
);
13643 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13644 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13648 if (member
&& DECL_CONV_FN_P (r
))
13649 /* Type-conversion operator. Reconstruct the name, in
13650 case it's the name of one of the template's parameters. */
13651 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13653 tree parms
= DECL_ARGUMENTS (t
);
13655 parms
= DECL_CHAIN (parms
);
13656 parms
= tsubst (parms
, args
, complain
, t
);
13657 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13658 DECL_CONTEXT (parm
) = r
;
13661 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13662 DECL_CHAIN (tparm
) = parms
;
13665 DECL_ARGUMENTS (r
) = parms
;
13666 DECL_RESULT (r
) = NULL_TREE
;
13668 TREE_STATIC (r
) = 0;
13669 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13670 DECL_EXTERNAL (r
) = 1;
13671 /* If this is an instantiation of a function with internal
13672 linkage, we already know what object file linkage will be
13673 assigned to the instantiation. */
13674 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13675 DECL_DEFER_OUTPUT (r
) = 0;
13676 DECL_CHAIN (r
) = NULL_TREE
;
13677 DECL_PENDING_INLINE_INFO (r
) = 0;
13678 DECL_PENDING_INLINE_P (r
) = 0;
13679 DECL_SAVED_TREE (r
) = NULL_TREE
;
13680 DECL_STRUCT_FUNCTION (r
) = NULL
;
13682 /* We'll re-clone as appropriate in instantiate_template. */
13683 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13685 /* If we aren't complaining now, return on error before we register
13686 the specialization so that we'll complain eventually. */
13687 if ((complain
& tf_error
) == 0
13688 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13689 && !grok_op_properties (r
, /*complain=*/false))
13690 return error_mark_node
;
13692 /* Associate the constraints directly with the instantiation. We
13693 don't substitute through the constraints; that's only done when
13694 they are checked. */
13695 if (tree ci
= get_constraints (t
))
13696 set_constraints (r
, ci
);
13698 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13699 SET_DECL_FRIEND_CONTEXT (r
,
13700 tsubst (DECL_FRIEND_CONTEXT (t
),
13701 args
, complain
, in_decl
));
13703 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13704 this in the special friend case mentioned above where
13705 GEN_TMPL is NULL. */
13706 if (gen_tmpl
&& !closure
)
13708 DECL_TEMPLATE_INFO (r
)
13709 = build_template_info (gen_tmpl
, argvec
);
13710 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13713 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13715 /* We instantiated this while substituting into
13716 the type earlier (template/friend54.C). */
13719 /* We're not supposed to instantiate default arguments
13720 until they are called, for a template. But, for a
13723 template <class T> void f ()
13724 { extern void g(int i = T()); }
13726 we should do the substitution when the template is
13727 instantiated. We handle the member function case in
13728 instantiate_class_template since the default arguments
13729 might refer to other members of the class. */
13731 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13732 && !uses_template_parms (argvec
))
13733 tsubst_default_arguments (r
, complain
);
13736 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13738 /* Copy the list of befriending classes. */
13739 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13741 friends
= &TREE_CHAIN (*friends
))
13743 *friends
= copy_node (*friends
);
13744 TREE_VALUE (*friends
)
13745 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13748 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13750 maybe_retrofit_in_chrg (r
);
13751 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13752 return error_mark_node
;
13753 /* If this is an instantiation of a member template, clone it.
13754 If it isn't, that'll be handled by
13755 clone_constructors_and_destructors. */
13756 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13757 clone_function_decl (r
, /*update_methods=*/false);
13759 else if ((complain
& tf_error
) != 0
13760 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13761 && !grok_op_properties (r
, /*complain=*/true))
13762 return error_mark_node
;
13764 /* Possibly limit visibility based on template args. */
13765 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13766 if (DECL_VISIBILITY_SPECIFIED (t
))
13768 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13769 DECL_ATTRIBUTES (r
)
13770 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13772 determine_visibility (r
);
13773 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13774 && !processing_template_decl
)
13775 defaulted_late_check (r
);
13777 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13778 args
, complain
, in_decl
);
13780 if (tree attr
= lookup_attribute ("omp declare variant base",
13781 DECL_ATTRIBUTES (r
)))
13782 omp_declare_variant_finalize (r
, attr
);
13787 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13790 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13791 tree lambda_fntype
)
13793 /* We can get here when processing a member function template,
13794 member class template, or template template parameter. */
13795 tree decl
= DECL_TEMPLATE_RESULT (t
);
13801 hashval_t hash
= 0;
13803 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13805 /* Template template parameter is treated here. */
13806 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13807 if (new_type
== error_mark_node
)
13808 r
= error_mark_node
;
13809 /* If we get a real template back, return it. This can happen in
13810 the context of most_specialized_partial_spec. */
13811 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13814 /* The new TEMPLATE_DECL was built in
13815 reduce_template_parm_level. */
13816 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13820 if (!lambda_fntype
)
13822 /* We might already have an instance of this template.
13823 The ARGS are for the surrounding class type, so the
13824 full args contain the tsubst'd args for the context,
13825 plus the innermost args from the template decl. */
13826 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13827 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13828 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13829 /* Because this is a template, the arguments will still be
13830 dependent, even after substitution. If
13831 PROCESSING_TEMPLATE_DECL is not set, the dependency
13832 predicates will short-circuit. */
13833 ++processing_template_decl
;
13834 full_args
= tsubst_template_args (tmpl_args
, args
,
13835 complain
, in_decl
);
13836 --processing_template_decl
;
13837 if (full_args
== error_mark_node
)
13838 return error_mark_node
;
13840 /* If this is a default template template argument,
13841 tsubst might not have changed anything. */
13842 if (full_args
== tmpl_args
)
13845 hash
= hash_tmpl_and_args (t
, full_args
);
13846 spec
= retrieve_specialization (t
, full_args
, hash
);
13847 if (spec
!= NULL_TREE
)
13850 /* Type partial instantiations are stored as the type by
13851 lookup_template_class_1, not here as the template. */
13852 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
13857 /* Make a new template decl. It will be similar to the
13858 original, but will record the current template arguments.
13859 We also create a new function declaration, which is just
13860 like the old one, but points to this new template, rather
13861 than the old one. */
13863 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13864 DECL_CHAIN (r
) = NULL_TREE
;
13866 // Build new template info linking to the original template decl.
13867 if (!lambda_fntype
)
13869 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13870 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13873 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13875 /* The template parameters for this new template are all the
13876 template parameters for the old template, except the
13877 outermost level of parameters. */
13878 DECL_TEMPLATE_PARMS (r
)
13879 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13882 if (TREE_CODE (decl
) == TYPE_DECL
13883 && !TYPE_DECL_ALIAS_P (decl
))
13886 ++processing_template_decl
;
13887 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13888 --processing_template_decl
;
13889 if (new_type
== error_mark_node
)
13890 return error_mark_node
;
13892 TREE_TYPE (r
) = new_type
;
13893 /* For a partial specialization, we need to keep pointing to
13894 the primary template. */
13895 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13896 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13897 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13898 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13899 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13904 ++processing_template_decl
;
13905 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13906 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13908 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13909 --processing_template_decl
;
13910 if (new_decl
== error_mark_node
)
13911 return error_mark_node
;
13913 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13914 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13915 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13918 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13919 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13923 DECL_TI_TEMPLATE (new_decl
) = r
;
13924 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13928 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13929 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13931 if (PRIMARY_TEMPLATE_P (t
))
13932 DECL_PRIMARY_TEMPLATE (r
) = r
;
13934 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13936 /* Record this non-type partial instantiation. */
13937 register_specialization (r
, t
,
13938 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13944 /* True if FN is the op() for a lambda in an uninstantiated template. */
13947 lambda_fn_in_template_p (tree fn
)
13949 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13951 tree closure
= DECL_CONTEXT (fn
);
13952 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13955 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13956 which the above is true. */
13959 instantiated_lambda_fn_p (tree fn
)
13961 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13963 tree closure
= DECL_CONTEXT (fn
);
13964 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13965 return LAMBDA_EXPR_INSTANTIATED (lam
);
13968 /* We're instantiating a variable from template function TCTX. Return the
13969 corresponding current enclosing scope. This gets complicated because lambda
13970 functions in templates are regenerated rather than instantiated, but generic
13971 lambda functions are subsequently instantiated. */
13974 enclosing_instantiation_of (tree otctx
)
13977 tree fn
= current_function_decl
;
13978 int lambda_count
= 0;
13980 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
13981 || instantiated_lambda_fn_p (tctx
));
13982 tctx
= decl_function_context (tctx
))
13984 for (; fn
; fn
= decl_function_context (fn
))
13987 int flambda_count
= 0;
13988 for (; fn
&& instantiated_lambda_fn_p (fn
);
13989 fn
= decl_function_context (fn
))
13991 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13992 ? most_general_template (fn
) != most_general_template (tctx
)
13995 if (flambda_count
!= lambda_count
)
13997 gcc_assert (flambda_count
> lambda_count
);
13998 for (; flambda_count
> lambda_count
; --flambda_count
)
13999 ofn
= decl_function_context (ofn
);
14001 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14002 || DECL_CONV_FN_P (ofn
));
14005 gcc_unreachable ();
14008 /* Substitute the ARGS into the T, which is a _DECL. Return the
14009 result of the substitution. Issue error and warning messages under
14010 control of COMPLAIN. */
14013 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14015 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14016 location_t saved_loc
;
14017 tree r
= NULL_TREE
;
14019 hashval_t hash
= 0;
14021 /* Set the filename and linenumber to improve error-reporting. */
14022 saved_loc
= input_location
;
14023 input_location
= DECL_SOURCE_LOCATION (t
);
14025 switch (TREE_CODE (t
))
14027 case TEMPLATE_DECL
:
14028 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14031 case FUNCTION_DECL
:
14032 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14037 tree type
= NULL_TREE
;
14039 tree expanded_types
= NULL_TREE
;
14040 tree prev_r
= NULL_TREE
;
14041 tree first_r
= NULL_TREE
;
14043 if (DECL_PACK_P (t
))
14045 /* If there is a local specialization that isn't a
14046 parameter pack, it means that we're doing a "simple"
14047 substitution from inside tsubst_pack_expansion. Just
14048 return the local specialization (which will be a single
14050 tree spec
= retrieve_local_specialization (t
);
14052 && TREE_CODE (spec
) == PARM_DECL
14053 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14056 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14057 the parameters in this function parameter pack. */
14058 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14059 complain
, in_decl
);
14060 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14062 len
= TREE_VEC_LENGTH (expanded_types
);
14064 /* Zero-length parameter packs are boring. Just substitute
14066 if (len
== 0 && !cp_unevaluated_operand
)
14067 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14072 /* All we did was update the type. Make a note of that. */
14073 type
= expanded_types
;
14074 expanded_types
= NULL_TREE
;
14078 /* Loop through all of the parameters we'll build. When T is
14079 a function parameter pack, LEN is the number of expanded
14080 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14082 for (i
= 0; i
< len
; ++i
)
14086 if (DECL_TEMPLATE_PARM_P (t
))
14087 SET_DECL_TEMPLATE_PARM_P (r
);
14089 if (expanded_types
)
14090 /* We're on the Ith parameter of the function parameter
14093 /* Get the Ith type. */
14094 type
= TREE_VEC_ELT (expanded_types
, i
);
14096 /* Rename the parameter to include the index. */
14098 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14101 /* We're dealing with a normal parameter. */
14102 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14104 type
= type_decays_to (type
);
14105 TREE_TYPE (r
) = type
;
14106 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14108 if (DECL_INITIAL (r
))
14110 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14111 DECL_INITIAL (r
) = TREE_TYPE (r
);
14113 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14114 complain
, in_decl
);
14117 DECL_CONTEXT (r
) = NULL_TREE
;
14119 if (!DECL_TEMPLATE_PARM_P (r
))
14120 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14122 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14123 args
, complain
, in_decl
);
14125 /* Keep track of the first new parameter we
14126 generate. That's what will be returned to the
14131 /* Build a proper chain of parameters when substituting
14132 into a function parameter pack. */
14134 DECL_CHAIN (prev_r
) = r
;
14137 /* If cp_unevaluated_operand is set, we're just looking for a
14138 single dummy parameter, so don't keep going. */
14139 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14140 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14141 complain
, DECL_CHAIN (t
));
14143 /* FIRST_R contains the start of the chain we've built. */
14150 tree type
= NULL_TREE
;
14151 tree vec
= NULL_TREE
;
14152 tree expanded_types
= NULL_TREE
;
14155 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14157 /* This field is a lambda capture pack. Return a TREE_VEC of
14158 the expanded fields to instantiate_class_template_1. */
14159 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14160 complain
, in_decl
);
14161 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14163 len
= TREE_VEC_LENGTH (expanded_types
);
14164 vec
= make_tree_vec (len
);
14168 /* All we did was update the type. Make a note of that. */
14169 type
= expanded_types
;
14170 expanded_types
= NULL_TREE
;
14174 for (int i
= 0; i
< len
; ++i
)
14177 if (expanded_types
)
14179 type
= TREE_VEC_ELT (expanded_types
, i
);
14181 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14184 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14186 if (type
== error_mark_node
)
14187 RETURN (error_mark_node
);
14188 TREE_TYPE (r
) = type
;
14189 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14191 if (DECL_C_BIT_FIELD (r
))
14192 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14194 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14195 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14197 /*integral_constant_expression_p=*/true);
14198 if (DECL_INITIAL (t
))
14200 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14201 NSDMI in perform_member_init. Still set DECL_INITIAL
14202 so that we know there is one. */
14203 DECL_INITIAL (r
) = void_node
;
14204 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14205 retrofit_lang_decl (r
);
14206 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14208 /* We don't have to set DECL_CONTEXT here; it is set by
14209 finish_member_declaration. */
14210 DECL_CHAIN (r
) = NULL_TREE
;
14212 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14213 args
, complain
, in_decl
);
14216 TREE_VEC_ELT (vec
, i
) = r
;
14225 /* We reach here only for member using decls. We also need to check
14226 uses_template_parms because DECL_DEPENDENT_P is not set for a
14227 using-declaration that designates a member of the current
14228 instantiation (c++/53549). */
14229 if (DECL_DEPENDENT_P (t
)
14230 || uses_template_parms (USING_DECL_SCOPE (t
)))
14232 tree scope
= USING_DECL_SCOPE (t
);
14233 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14234 if (PACK_EXPANSION_P (scope
))
14236 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14237 int len
= TREE_VEC_LENGTH (vec
);
14238 r
= make_tree_vec (len
);
14239 for (int i
= 0; i
< len
; ++i
)
14241 tree escope
= TREE_VEC_ELT (vec
, i
);
14242 tree elt
= do_class_using_decl (escope
, name
);
14245 r
= error_mark_node
;
14250 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14251 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14253 TREE_VEC_ELT (r
, i
) = elt
;
14258 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14259 complain
, in_decl
);
14260 r
= do_class_using_decl (inst_scope
, name
);
14262 r
= error_mark_node
;
14265 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14266 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14273 DECL_CHAIN (r
) = NULL_TREE
;
14280 tree argvec
= NULL_TREE
;
14281 tree gen_tmpl
= NULL_TREE
;
14283 tree tmpl
= NULL_TREE
;
14285 tree type
= NULL_TREE
;
14288 if (TREE_TYPE (t
) == error_mark_node
)
14289 RETURN (error_mark_node
);
14291 if (TREE_CODE (t
) == TYPE_DECL
14292 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14294 /* If this is the canonical decl, we don't have to
14295 mess with instantiations, and often we can't (for
14296 typename, template type parms and such). Note that
14297 TYPE_NAME is not correct for the above test if
14298 we've copied the type for a typedef. */
14299 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14300 if (type
== error_mark_node
)
14301 RETURN (error_mark_node
);
14302 r
= TYPE_NAME (type
);
14306 /* Check to see if we already have the specialization we
14309 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14311 /* T is a static data member or namespace-scope entity.
14312 We have to substitute into namespace-scope variables
14313 (not just variable templates) because of cases like:
14315 template <class T> void f() { extern T t; }
14317 where the entity referenced is not known until
14318 instantiation time. */
14320 ctx
= DECL_CONTEXT (t
);
14321 if (DECL_CLASS_SCOPE_P (t
))
14323 ctx
= tsubst_aggr_type (ctx
, args
,
14325 in_decl
, /*entering_scope=*/1);
14326 /* If CTX is unchanged, then T is in fact the
14327 specialization we want. That situation occurs when
14328 referencing a static data member within in its own
14329 class. We can use pointer equality, rather than
14330 same_type_p, because DECL_CONTEXT is always
14332 if (ctx
== DECL_CONTEXT (t
)
14333 /* ... unless T is a member template; in which
14334 case our caller can be willing to create a
14335 specialization of that template represented
14337 && !(DECL_TI_TEMPLATE (t
)
14338 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14344 tmpl
= DECL_TI_TEMPLATE (t
);
14345 gen_tmpl
= most_general_template (tmpl
);
14346 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14347 if (argvec
!= error_mark_node
)
14348 argvec
= (coerce_innermost_template_parms
14349 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14350 argvec
, t
, complain
,
14351 /*all*/true, /*defarg*/true));
14352 if (argvec
== error_mark_node
)
14353 RETURN (error_mark_node
);
14354 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14355 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14360 /* A local variable. */
14362 /* Subsequent calls to pushdecl will fill this in. */
14364 /* Unless this is a reference to a static variable from an
14365 enclosing function, in which case we need to fill it in now. */
14366 if (TREE_STATIC (t
))
14368 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14369 if (fn
!= current_function_decl
)
14372 spec
= retrieve_local_specialization (t
);
14374 /* If we already have the specialization we need, there is
14375 nothing more to do. */
14382 /* Create a new node for the specialization we need. */
14383 if (type
== NULL_TREE
)
14385 if (is_typedef_decl (t
))
14386 type
= DECL_ORIGINAL_TYPE (t
);
14388 type
= TREE_TYPE (t
);
14390 && VAR_HAD_UNKNOWN_BOUND (t
)
14391 && type
!= error_mark_node
)
14392 type
= strip_array_domain (type
);
14393 tree sub_args
= args
;
14394 if (tree auto_node
= type_uses_auto (type
))
14396 /* Mask off any template args past the variable's context so we
14397 don't replace the auto with an unrelated argument. */
14398 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14399 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14401 /* This should never happen with the new lambda instantiation
14402 model, but keep the handling just in case. */
14403 gcc_assert (!CHECKING_P
),
14404 sub_args
= strip_innermost_template_args (args
, extra
);
14406 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14407 /* Substituting the type might have recursively instantiated this
14408 same alias (c++/86171). */
14409 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14410 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14419 DECL_INITIALIZED_P (r
) = 0;
14420 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14421 if (type
== error_mark_node
)
14422 RETURN (error_mark_node
);
14423 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14425 /* It may seem that this case cannot occur, since:
14430 declares a function, not a variable. However:
14433 template <typename T> void g() { T t; }
14434 template void g<f>();
14436 is an attempt to declare a variable with function
14438 error ("variable %qD has function type",
14439 /* R is not yet sufficiently initialized, so we
14440 just use its name. */
14442 RETURN (error_mark_node
);
14444 type
= complete_type (type
);
14445 /* Wait until cp_finish_decl to set this again, to handle
14446 circular dependency (template/instantiate6.C). */
14447 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14448 type
= check_var_type (DECL_NAME (r
), type
,
14449 DECL_SOURCE_LOCATION (r
));
14450 if (DECL_HAS_VALUE_EXPR_P (t
))
14452 tree ve
= DECL_VALUE_EXPR (t
);
14453 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14454 /*constant_expression_p=*/false);
14455 if (REFERENCE_REF_P (ve
))
14457 gcc_assert (TYPE_REF_P (type
));
14458 ve
= TREE_OPERAND (ve
, 0);
14460 SET_DECL_VALUE_EXPR (r
, ve
);
14462 if (CP_DECL_THREAD_LOCAL_P (r
)
14463 && !processing_template_decl
)
14464 set_decl_tls_model (r
, decl_default_tls_model (r
));
14466 else if (DECL_SELF_REFERENCE_P (t
))
14467 SET_DECL_SELF_REFERENCE_P (r
);
14468 TREE_TYPE (r
) = type
;
14469 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14470 DECL_CONTEXT (r
) = ctx
;
14471 /* Clear out the mangled name and RTL for the instantiation. */
14472 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14473 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14474 SET_DECL_RTL (r
, NULL
);
14475 /* The initializer must not be expanded until it is required;
14476 see [temp.inst]. */
14477 DECL_INITIAL (r
) = NULL_TREE
;
14478 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14481 if (DECL_LANG_SPECIFIC (r
))
14482 SET_DECL_DEPENDENT_INIT_P (r
, false);
14484 SET_DECL_MODE (r
, VOIDmode
);
14486 /* Possibly limit visibility based on template args. */
14487 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14488 if (DECL_VISIBILITY_SPECIFIED (t
))
14490 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14491 DECL_ATTRIBUTES (r
)
14492 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14494 determine_visibility (r
);
14499 /* A static data member declaration is always marked
14500 external when it is declared in-class, even if an
14501 initializer is present. We mimic the non-template
14502 processing here. */
14503 DECL_EXTERNAL (r
) = 1;
14504 if (DECL_NAMESPACE_SCOPE_P (t
))
14505 DECL_NOT_REALLY_EXTERN (r
) = 1;
14507 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14508 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14509 /* Remember whether we require constant initialization of
14510 a non-constant template variable. */
14511 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14512 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14513 if (!error_operand_p (r
) || (complain
& tf_error
))
14514 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14518 if (DECL_LANG_SPECIFIC (r
))
14519 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14520 if (!cp_unevaluated_operand
)
14521 register_local_specialization (r
, t
);
14524 DECL_CHAIN (r
) = NULL_TREE
;
14526 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14528 args
, complain
, in_decl
);
14530 /* Preserve a typedef that names a type. */
14531 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14533 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14534 set_underlying_type (r
);
14535 if (TYPE_DECL_ALIAS_P (r
))
14536 /* An alias template specialization can be dependent
14537 even if its underlying type is not. */
14538 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14541 layout_decl (r
, 0);
14546 gcc_unreachable ();
14551 /* Restore the file and line information. */
14552 input_location
= saved_loc
;
14557 /* Substitute into the complete parameter type list PARMS. */
14560 tsubst_function_parms (tree parms
,
14562 tsubst_flags_t complain
,
14565 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14568 /* Substitute into the ARG_TYPES of a function type.
14569 If END is a TREE_CHAIN, leave it and any following types
14573 tsubst_arg_types (tree arg_types
,
14576 tsubst_flags_t complain
,
14579 tree remaining_arg_types
;
14580 tree type
= NULL_TREE
;
14582 tree expanded_args
= NULL_TREE
;
14585 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14588 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14589 args
, end
, complain
, in_decl
);
14590 if (remaining_arg_types
== error_mark_node
)
14591 return error_mark_node
;
14593 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14595 /* For a pack expansion, perform substitution on the
14596 entire expression. Later on, we'll handle the arguments
14598 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14599 args
, complain
, in_decl
);
14601 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14602 /* So that we'll spin through the parameters, one by one. */
14603 i
= TREE_VEC_LENGTH (expanded_args
);
14606 /* We only partially substituted into the parameter
14607 pack. Our type is TYPE_PACK_EXPANSION. */
14608 type
= expanded_args
;
14609 expanded_args
= NULL_TREE
;
14617 type
= TREE_VEC_ELT (expanded_args
, i
);
14619 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14621 if (type
== error_mark_node
)
14622 return error_mark_node
;
14623 if (VOID_TYPE_P (type
))
14625 if (complain
& tf_error
)
14627 error ("invalid parameter type %qT", type
);
14629 error ("in declaration %q+D", in_decl
);
14631 return error_mark_node
;
14634 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14635 return error_mark_node
;
14637 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14638 top-level qualifiers as required. */
14639 type
= cv_unqualified (type_decays_to (type
));
14641 /* We do not substitute into default arguments here. The standard
14642 mandates that they be instantiated only when needed, which is
14643 done in build_over_call. */
14644 default_arg
= TREE_PURPOSE (arg_types
);
14646 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14647 since the new op() won't have any associated template arguments for us
14648 to refer to later. */
14649 if (lambda_fn_in_template_p (in_decl
))
14650 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14651 false/*fn*/, false/*constexpr*/);
14653 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14655 /* We've instantiated a template before its default arguments
14656 have been parsed. This can happen for a nested template
14657 class, and is not an error unless we require the default
14658 argument in a call of this function. */
14659 remaining_arg_types
=
14660 tree_cons (default_arg
, type
, remaining_arg_types
);
14661 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14662 remaining_arg_types
);
14665 remaining_arg_types
=
14666 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14669 return remaining_arg_types
;
14672 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14673 *not* handle the exception-specification for FNTYPE, because the
14674 initial substitution of explicitly provided template parameters
14675 during argument deduction forbids substitution into the
14676 exception-specification:
14680 All references in the function type of the function template to the
14681 corresponding template parameters are replaced by the specified tem-
14682 plate argument values. If a substitution in a template parameter or
14683 in the function type of the function template results in an invalid
14684 type, type deduction fails. [Note: The equivalent substitution in
14685 exception specifications is done only when the function is instanti-
14686 ated, at which point a program is ill-formed if the substitution
14687 results in an invalid type.] */
14690 tsubst_function_type (tree t
,
14692 tsubst_flags_t complain
,
14696 tree arg_types
= NULL_TREE
;
14699 /* The TYPE_CONTEXT is not used for function/method types. */
14700 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14702 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14704 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14706 if (late_return_type_p
)
14708 /* Substitute the argument types. */
14709 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14710 complain
, in_decl
);
14711 if (arg_types
== error_mark_node
)
14712 return error_mark_node
;
14714 tree save_ccp
= current_class_ptr
;
14715 tree save_ccr
= current_class_ref
;
14716 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14717 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14718 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14721 /* DR 1207: 'this' is in scope in the trailing return type. */
14722 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14725 /* Substitute the return type. */
14726 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14730 current_class_ptr
= save_ccp
;
14731 current_class_ref
= save_ccr
;
14735 /* Substitute the return type. */
14736 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14738 if (return_type
== error_mark_node
)
14739 return error_mark_node
;
14740 /* DR 486 clarifies that creation of a function type with an
14741 invalid return type is a deduction failure. */
14742 if (TREE_CODE (return_type
) == ARRAY_TYPE
14743 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14745 if (complain
& tf_error
)
14747 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14748 error ("function returning an array");
14750 error ("function returning a function");
14752 return error_mark_node
;
14755 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14756 return error_mark_node
;
14758 if (!late_return_type_p
)
14760 /* Substitute the argument types. */
14761 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14762 complain
, in_decl
);
14763 if (arg_types
== error_mark_node
)
14764 return error_mark_node
;
14767 /* Construct a new type node and return it. */
14768 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14770 fntype
= build_function_type (return_type
, arg_types
);
14771 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14775 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14776 /* Don't pick up extra function qualifiers from the basetype. */
14777 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14778 if (! MAYBE_CLASS_TYPE_P (r
))
14782 Type deduction may fail for any of the following
14785 -- Attempting to create "pointer to member of T" when T
14786 is not a class type. */
14787 if (complain
& tf_error
)
14788 error ("creating pointer to member function of non-class type %qT",
14790 return error_mark_node
;
14793 fntype
= build_method_type_directly (r
, return_type
,
14794 TREE_CHAIN (arg_types
));
14796 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14798 /* See comment above. */
14799 tree raises
= NULL_TREE
;
14800 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14801 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14806 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14807 ARGS into that specification, and return the substituted
14808 specification. If there is no specification, return NULL_TREE. */
14811 tsubst_exception_specification (tree fntype
,
14813 tsubst_flags_t complain
,
14820 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14821 new_specs
= NULL_TREE
;
14822 if (specs
&& TREE_PURPOSE (specs
))
14824 /* A noexcept-specifier. */
14825 tree expr
= TREE_PURPOSE (specs
);
14826 if (TREE_CODE (expr
) == INTEGER_CST
)
14830 /* Defer instantiation of noexcept-specifiers to avoid
14831 excessive instantiations (c++/49107). */
14832 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14833 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14835 /* We already partially instantiated this member template,
14836 so combine the new args with the old. */
14837 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14838 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14839 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14840 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14844 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14845 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14850 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14852 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14854 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14856 new_specs
= tsubst_copy_and_build
14857 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14858 /*integral_constant_expression_p=*/true);
14860 new_specs
= build_noexcept_spec (new_specs
, complain
);
14864 if (! TREE_VALUE (specs
))
14871 tree expanded_specs
= NULL_TREE
;
14873 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14875 /* Expand the pack expansion type. */
14876 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14880 if (expanded_specs
== error_mark_node
)
14881 return error_mark_node
;
14882 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14883 len
= TREE_VEC_LENGTH (expanded_specs
);
14886 /* We're substituting into a member template, so
14887 we got a TYPE_PACK_EXPANSION back. Add that
14888 expansion and move on. */
14889 gcc_assert (TREE_CODE (expanded_specs
)
14890 == TYPE_PACK_EXPANSION
);
14891 new_specs
= add_exception_specifier (new_specs
,
14894 specs
= TREE_CHAIN (specs
);
14899 for (i
= 0; i
< len
; ++i
)
14901 if (expanded_specs
)
14902 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14904 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14905 if (spec
== error_mark_node
)
14907 new_specs
= add_exception_specifier (new_specs
, spec
,
14911 specs
= TREE_CHAIN (specs
);
14917 /* Take the tree structure T and replace template parameters used
14918 therein with the argument vector ARGS. IN_DECL is an associated
14919 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14920 Issue error and warning messages under control of COMPLAIN. Note
14921 that we must be relatively non-tolerant of extensions here, in
14922 order to preserve conformance; if we allow substitutions that
14923 should not be allowed, we may allow argument deductions that should
14924 not succeed, and therefore report ambiguous overload situations
14925 where there are none. In theory, we could allow the substitution,
14926 but indicate that it should have failed, and allow our caller to
14927 make sure that the right thing happens, but we don't try to do this
14930 This function is used for dealing with types, decls and the like;
14931 for expressions, use tsubst_expr or tsubst_copy. */
14934 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14936 enum tree_code code
;
14937 tree type
, r
= NULL_TREE
;
14939 if (t
== NULL_TREE
|| t
== error_mark_node
14940 || t
== integer_type_node
14941 || t
== void_type_node
14942 || t
== char_type_node
14943 || t
== unknown_type_node
14944 || TREE_CODE (t
) == NAMESPACE_DECL
14945 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14949 return tsubst_decl (t
, args
, complain
);
14951 if (args
== NULL_TREE
)
14954 code
= TREE_CODE (t
);
14956 if (code
== IDENTIFIER_NODE
)
14957 type
= IDENTIFIER_TYPE_VALUE (t
);
14959 type
= TREE_TYPE (t
);
14961 gcc_assert (type
!= unknown_type_node
);
14963 /* Reuse typedefs. We need to do this to handle dependent attributes,
14964 such as attribute aligned. */
14966 && typedef_variant_p (t
))
14968 tree decl
= TYPE_NAME (t
);
14970 if (alias_template_specialization_p (t
, nt_opaque
))
14972 /* DECL represents an alias template and we want to
14974 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14975 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14976 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14978 else if (DECL_CLASS_SCOPE_P (decl
)
14979 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14980 && uses_template_parms (DECL_CONTEXT (decl
)))
14982 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14983 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14984 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14986 else if (DECL_FUNCTION_SCOPE_P (decl
)
14987 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14988 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14989 r
= retrieve_local_specialization (decl
);
14991 /* The typedef is from a non-template context. */
14997 r
= cp_build_qualified_type_real
14998 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14999 complain
| tf_ignore_bad_quals
);
15004 /* We don't have an instantiation yet, so drop the typedef. */
15005 int quals
= cp_type_quals (t
);
15006 t
= DECL_ORIGINAL_TYPE (decl
);
15007 t
= cp_build_qualified_type_real (t
, quals
,
15008 complain
| tf_ignore_bad_quals
);
15012 bool fndecl_type
= (complain
& tf_fndecl_type
);
15013 complain
&= ~tf_fndecl_type
;
15016 && code
!= TYPENAME_TYPE
15017 && code
!= TEMPLATE_TYPE_PARM
15018 && code
!= TEMPLATE_PARM_INDEX
15019 && code
!= IDENTIFIER_NODE
15020 && code
!= FUNCTION_TYPE
15021 && code
!= METHOD_TYPE
)
15022 type
= tsubst (type
, args
, complain
, in_decl
);
15023 if (type
== error_mark_node
)
15024 return error_mark_node
;
15030 case ENUMERAL_TYPE
:
15031 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15032 /*entering_scope=*/0);
15035 case IDENTIFIER_NODE
:
15046 if (t
== integer_type_node
)
15049 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15050 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15054 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15056 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15057 /*integral_constant_expression_p=*/false);
15059 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15061 if (TREE_CODE (max
) == NOP_EXPR
15062 && TREE_SIDE_EFFECTS (omax
)
15063 && !TREE_TYPE (max
))
15064 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15066 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15067 with TREE_SIDE_EFFECTS that indicates this is not an integral
15068 constant expression. */
15069 if (processing_template_decl
15070 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15072 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15073 TREE_SIDE_EFFECTS (max
) = 1;
15076 return compute_array_index_type (NULL_TREE
, max
, complain
);
15079 case TEMPLATE_TYPE_PARM
:
15080 case TEMPLATE_TEMPLATE_PARM
:
15081 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15082 case TEMPLATE_PARM_INDEX
:
15087 tree arg
= NULL_TREE
;
15091 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15092 template_parm_level_and_index (t
, &level
, &idx
);
15094 levels
= TMPL_ARGS_DEPTH (args
);
15095 if (level
<= levels
15096 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15098 arg
= TMPL_ARG (args
, level
, idx
);
15100 /* See through ARGUMENT_PACK_SELECT arguments. */
15101 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15102 arg
= argument_pack_select_arg (arg
);
15105 if (arg
== error_mark_node
)
15106 return error_mark_node
;
15107 else if (arg
!= NULL_TREE
)
15109 if (ARGUMENT_PACK_P (arg
))
15110 /* If ARG is an argument pack, we don't actually want to
15111 perform a substitution here, because substitutions
15112 for argument packs are only done
15113 element-by-element. We can get to this point when
15114 substituting the type of a non-type template
15115 parameter pack, when that type actually contains
15116 template parameter packs from an outer template, e.g.,
15118 template<typename... Types> struct A {
15119 template<Types... Values> struct B { };
15123 if (code
== TEMPLATE_TYPE_PARM
)
15127 /* When building concept checks for the purpose of
15128 deducing placeholders, we can end up with wildcards
15129 where types are expected. Adjust this to the deduced
15131 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15132 arg
= TREE_TYPE (TREE_TYPE (arg
));
15134 gcc_assert (TYPE_P (arg
));
15136 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15138 return cp_build_qualified_type_real
15139 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15141 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15143 /* We are processing a type constructed from a
15144 template template parameter. */
15145 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15146 args
, complain
, in_decl
);
15147 if (argvec
== error_mark_node
)
15148 return error_mark_node
;
15150 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15151 || TREE_CODE (arg
) == TEMPLATE_DECL
15152 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15154 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15155 /* Consider this code:
15157 template <template <class> class Template>
15159 template <class Arg> using Bind = Template<Arg>;
15162 template <template <class> class Template, class Arg>
15163 using Instantiate = Template<Arg>; //#0
15165 template <template <class> class Template,
15168 Instantiate<Internal<Template>::template Bind,
15171 When #1 is parsed, the
15172 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15173 parameter `Template' in #0 matches the
15174 UNBOUND_CLASS_TEMPLATE representing the argument
15175 `Internal<Template>::template Bind'; We then want
15176 to assemble the type `Bind<Argument>' that can't
15177 be fully created right now, because
15178 `Internal<Template>' not being complete, the Bind
15179 template cannot be looked up in that context. So
15180 we need to "store" `Bind<Argument>' for later
15181 when the context of Bind becomes complete. Let's
15182 store that in a TYPENAME_TYPE. */
15183 return make_typename_type (TYPE_CONTEXT (arg
),
15184 build_nt (TEMPLATE_ID_EXPR
,
15185 TYPE_IDENTIFIER (arg
),
15190 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15191 are resolving nested-types in the signature of a
15192 member function templates. Otherwise ARG is a
15193 TEMPLATE_DECL and is the real template to be
15195 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15196 arg
= TYPE_NAME (arg
);
15198 r
= lookup_template_class (arg
,
15200 DECL_CONTEXT (arg
),
15201 /*entering_scope=*/0,
15203 return cp_build_qualified_type_real
15204 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15206 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15209 /* TEMPLATE_PARM_INDEX. */
15210 return convert_from_reference (unshare_expr (arg
));
15214 /* This can happen during the attempted tsubst'ing in
15215 unify. This means that we don't yet have any information
15216 about the template parameter in question. */
15219 /* Early in template argument deduction substitution, we don't
15220 want to reduce the level of 'auto', or it will be confused
15221 with a normal template parm in subsequent deduction.
15222 Similarly, don't reduce the level of template parameters to
15223 avoid mismatches when deducing their types. */
15224 if (complain
& tf_partial
)
15227 /* If we get here, we must have been looking at a parm for a
15228 more deeply nested template. Make a new version of this
15229 template parameter, but with a lower level. */
15232 case TEMPLATE_TYPE_PARM
:
15233 case TEMPLATE_TEMPLATE_PARM
:
15234 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15235 if (cp_type_quals (t
))
15237 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15238 r
= cp_build_qualified_type_real
15239 (r
, cp_type_quals (t
),
15240 complain
| (code
== TEMPLATE_TYPE_PARM
15241 ? tf_ignore_bad_quals
: 0));
15243 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15244 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15245 && (r
= (TEMPLATE_PARM_DESCENDANTS
15246 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15247 && (r
= TREE_TYPE (r
))
15248 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15249 /* Break infinite recursion when substituting the constraints
15250 of a constrained placeholder. */;
15251 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15252 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15253 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15254 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15255 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15256 && (TEMPLATE_PARM_LEVEL (r
)
15257 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15258 /* Cache the simple case of lowering a type parameter. */
15263 TEMPLATE_TYPE_PARM_INDEX (r
)
15264 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15265 r
, levels
, args
, complain
);
15266 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15267 TYPE_MAIN_VARIANT (r
) = r
;
15268 TYPE_POINTER_TO (r
) = NULL_TREE
;
15269 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15271 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15273 /* Propagate constraints on placeholders since they are
15274 only instantiated during satisfaction. */
15275 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15276 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15277 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15279 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15280 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15284 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15285 /* We have reduced the level of the template
15286 template parameter, but not the levels of its
15287 template parameters, so canonical_type_parameter
15288 will not be able to find the canonical template
15289 template parameter for this level. Thus, we
15290 require structural equality checking to compare
15291 TEMPLATE_TEMPLATE_PARMs. */
15292 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15293 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15294 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15296 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15298 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15300 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15301 /* We might need to substitute into the types of non-type
15302 template parameters. */
15303 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15304 complain
, in_decl
);
15305 if (tmpl
== error_mark_node
)
15306 return error_mark_node
;
15307 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15308 complain
, in_decl
);
15309 if (argvec
== error_mark_node
)
15310 return error_mark_node
;
15312 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15313 = build_template_info (tmpl
, argvec
);
15318 case TEMPLATE_PARM_INDEX
:
15319 /* OK, now substitute the type of the non-type parameter. We
15320 couldn't do it earlier because it might be an auto parameter,
15321 and we wouldn't need to if we had an argument. */
15322 type
= tsubst (type
, args
, complain
, in_decl
);
15323 if (type
== error_mark_node
)
15324 return error_mark_node
;
15325 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15329 gcc_unreachable ();
15337 tree purpose
, value
, chain
;
15339 if (t
== void_list_node
)
15342 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
15343 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
15345 /* We have pack expansions, so expand those and
15346 create a new list out of it. */
15348 /* Expand the argument expressions. */
15349 tree purposevec
= NULL_TREE
;
15350 if (TREE_PURPOSE (t
))
15351 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
15352 complain
, in_decl
);
15353 if (purposevec
== error_mark_node
)
15354 return error_mark_node
;
15356 tree valuevec
= NULL_TREE
;
15357 if (TREE_VALUE (t
))
15358 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
15359 complain
, in_decl
);
15360 if (valuevec
== error_mark_node
)
15361 return error_mark_node
;
15363 /* Build the rest of the list. */
15364 tree chain
= TREE_CHAIN (t
);
15365 if (chain
&& chain
!= void_type_node
)
15366 chain
= tsubst (chain
, args
, complain
, in_decl
);
15367 if (chain
== error_mark_node
)
15368 return error_mark_node
;
15370 /* Determine the number of arguments. */
15372 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
15374 len
= TREE_VEC_LENGTH (purposevec
);
15375 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15377 else if (TREE_CODE (valuevec
) == TREE_VEC
)
15378 len
= TREE_VEC_LENGTH (valuevec
);
15381 /* Since we only performed a partial substitution into
15382 the argument pack, we only RETURN (a single list
15384 if (purposevec
== TREE_PURPOSE (t
)
15385 && valuevec
== TREE_VALUE (t
)
15386 && chain
== TREE_CHAIN (t
))
15389 return tree_cons (purposevec
, valuevec
, chain
);
15392 /* Convert the argument vectors into a TREE_LIST. */
15393 for (int i
= len
; i
-- > 0; )
15395 purpose
= (purposevec
? TREE_VEC_ELT (purposevec
, i
)
15397 value
= (valuevec
? TREE_VEC_ELT (valuevec
, i
)
15400 /* Build the list (backwards). */
15401 chain
= hash_tree_cons (purpose
, value
, chain
);
15407 purpose
= TREE_PURPOSE (t
);
15410 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15411 if (purpose
== error_mark_node
)
15412 return error_mark_node
;
15414 value
= TREE_VALUE (t
);
15417 value
= tsubst (value
, args
, complain
, in_decl
);
15418 if (value
== error_mark_node
)
15419 return error_mark_node
;
15421 chain
= TREE_CHAIN (t
);
15422 if (chain
&& chain
!= void_type_node
)
15424 chain
= tsubst (chain
, args
, complain
, in_decl
);
15425 if (chain
== error_mark_node
)
15426 return error_mark_node
;
15428 if (purpose
== TREE_PURPOSE (t
)
15429 && value
== TREE_VALUE (t
)
15430 && chain
== TREE_CHAIN (t
))
15432 return hash_tree_cons (purpose
, value
, chain
);
15436 /* We should never be tsubsting a binfo. */
15437 gcc_unreachable ();
15440 /* A vector of template arguments. */
15441 gcc_assert (!type
);
15442 return tsubst_template_args (t
, args
, complain
, in_decl
);
15445 case REFERENCE_TYPE
:
15447 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15452 Type deduction may fail for any of the following
15455 -- Attempting to create a pointer to reference type.
15456 -- Attempting to create a reference to a reference type or
15457 a reference to void.
15459 Core issue 106 says that creating a reference to a reference
15460 during instantiation is no longer a cause for failure. We
15461 only enforce this check in strict C++98 mode. */
15462 if ((TYPE_REF_P (type
)
15463 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15464 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15466 static location_t last_loc
;
15468 /* We keep track of the last time we issued this error
15469 message to avoid spewing a ton of messages during a
15470 single bad template instantiation. */
15471 if (complain
& tf_error
15472 && last_loc
!= input_location
)
15474 if (VOID_TYPE_P (type
))
15475 error ("forming reference to void");
15476 else if (code
== POINTER_TYPE
)
15477 error ("forming pointer to reference type %qT", type
);
15479 error ("forming reference to reference type %qT", type
);
15480 last_loc
= input_location
;
15483 return error_mark_node
;
15485 else if (TREE_CODE (type
) == FUNCTION_TYPE
15486 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15487 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15489 if (complain
& tf_error
)
15491 if (code
== POINTER_TYPE
)
15492 error ("forming pointer to qualified function type %qT",
15495 error ("forming reference to qualified function type %qT",
15498 return error_mark_node
;
15500 else if (code
== POINTER_TYPE
)
15502 r
= build_pointer_type (type
);
15503 if (TREE_CODE (type
) == METHOD_TYPE
)
15504 r
= build_ptrmemfunc_type (r
);
15506 else if (TYPE_REF_P (type
))
15507 /* In C++0x, during template argument substitution, when there is an
15508 attempt to create a reference to a reference type, reference
15509 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15511 "If a template-argument for a template-parameter T names a type
15512 that is a reference to a type A, an attempt to create the type
15513 'lvalue reference to cv T' creates the type 'lvalue reference to
15514 A,' while an attempt to create the type type rvalue reference to
15515 cv T' creates the type T"
15517 r
= cp_build_reference_type
15519 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15521 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15522 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15524 if (r
!= error_mark_node
)
15525 /* Will this ever be needed for TYPE_..._TO values? */
15532 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15533 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15537 Type deduction may fail for any of the following
15540 -- Attempting to create "pointer to member of T" when T
15541 is not a class type. */
15542 if (complain
& tf_error
)
15543 error ("creating pointer to member of non-class type %qT", r
);
15544 return error_mark_node
;
15546 if (TYPE_REF_P (type
))
15548 if (complain
& tf_error
)
15549 error ("creating pointer to member reference type %qT", type
);
15550 return error_mark_node
;
15552 if (VOID_TYPE_P (type
))
15554 if (complain
& tf_error
)
15555 error ("creating pointer to member of type void");
15556 return error_mark_node
;
15558 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15559 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15561 /* The type of the implicit object parameter gets its
15562 cv-qualifiers from the FUNCTION_TYPE. */
15565 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15566 type_memfn_rqual (type
));
15567 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15568 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15572 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15576 case FUNCTION_TYPE
:
15581 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15582 if (fntype
== error_mark_node
)
15583 return error_mark_node
;
15585 /* Substitute the exception specification. */
15586 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15587 /*defer_ok*/fndecl_type
);
15588 if (specs
== error_mark_node
)
15589 return error_mark_node
;
15591 fntype
= build_exception_variant (fntype
, specs
);
15596 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15597 if (domain
== error_mark_node
)
15598 return error_mark_node
;
15600 /* As an optimization, we avoid regenerating the array type if
15601 it will obviously be the same as T. */
15602 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15605 /* These checks should match the ones in create_array_type_for_decl.
15609 The deduction may fail for any of the following reasons:
15611 -- Attempting to create an array with an element type that
15612 is void, a function type, or a reference type, or [DR337]
15613 an abstract class type. */
15614 if (VOID_TYPE_P (type
)
15615 || TREE_CODE (type
) == FUNCTION_TYPE
15616 || (TREE_CODE (type
) == ARRAY_TYPE
15617 && TYPE_DOMAIN (type
) == NULL_TREE
)
15618 || TYPE_REF_P (type
))
15620 if (complain
& tf_error
)
15621 error ("creating array of %qT", type
);
15622 return error_mark_node
;
15625 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15626 return error_mark_node
;
15628 r
= build_cplus_array_type (type
, domain
);
15630 if (!valid_array_size_p (input_location
, r
, in_decl
,
15631 (complain
& tf_error
)))
15632 return error_mark_node
;
15634 if (TYPE_USER_ALIGN (t
))
15636 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15637 TYPE_USER_ALIGN (r
) = 1;
15643 case TYPENAME_TYPE
:
15645 tree ctx
= TYPE_CONTEXT (t
);
15646 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15648 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15649 if (ctx
== error_mark_node
15650 || TREE_VEC_LENGTH (ctx
) > 1)
15651 return error_mark_node
;
15652 if (TREE_VEC_LENGTH (ctx
) == 0)
15654 if (complain
& tf_error
)
15655 error ("%qD is instantiated for an empty pack",
15656 TYPENAME_TYPE_FULLNAME (t
));
15657 return error_mark_node
;
15659 ctx
= TREE_VEC_ELT (ctx
, 0);
15662 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15663 /*entering_scope=*/1);
15664 if (ctx
== error_mark_node
)
15665 return error_mark_node
;
15667 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15668 complain
, in_decl
);
15669 if (f
== error_mark_node
)
15670 return error_mark_node
;
15672 if (!MAYBE_CLASS_TYPE_P (ctx
))
15674 if (complain
& tf_error
)
15675 error ("%qT is not a class, struct, or union type", ctx
);
15676 return error_mark_node
;
15678 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15680 /* Normally, make_typename_type does not require that the CTX
15681 have complete type in order to allow things like:
15683 template <class T> struct S { typename S<T>::X Y; };
15685 But, such constructs have already been resolved by this
15686 point, so here CTX really should have complete type, unless
15687 it's a partial instantiation. */
15688 ctx
= complete_type (ctx
);
15689 if (!COMPLETE_TYPE_P (ctx
))
15691 if (complain
& tf_error
)
15692 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15693 return error_mark_node
;
15697 f
= make_typename_type (ctx
, f
, typename_type
,
15698 complain
| tf_keep_type_decl
);
15699 if (f
== error_mark_node
)
15701 if (TREE_CODE (f
) == TYPE_DECL
)
15703 complain
|= tf_ignore_bad_quals
;
15707 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15709 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15711 if (complain
& tf_error
)
15712 error ("%qT resolves to %qT, which is not an enumeration type",
15715 return error_mark_node
;
15717 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15719 if (complain
& tf_error
)
15720 error ("%qT resolves to %qT, which is not a class type",
15723 return error_mark_node
;
15727 return cp_build_qualified_type_real
15728 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15731 case UNBOUND_CLASS_TEMPLATE
:
15733 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15734 in_decl
, /*entering_scope=*/1);
15735 tree name
= TYPE_IDENTIFIER (t
);
15736 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15738 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15739 return error_mark_node
;
15742 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15743 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15750 ++cp_unevaluated_operand
;
15751 ++c_inhibit_evaluation_warnings
;
15753 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15755 /*integral_constant_expression_p=*/false);
15757 --cp_unevaluated_operand
;
15758 --c_inhibit_evaluation_warnings
;
15760 type
= finish_typeof (type
);
15761 return cp_build_qualified_type_real (type
,
15763 | cp_type_quals (type
),
15767 case DECLTYPE_TYPE
:
15771 ++cp_unevaluated_operand
;
15772 ++c_inhibit_evaluation_warnings
;
15774 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15775 complain
|tf_decltype
, in_decl
,
15776 /*function_p*/false,
15777 /*integral_constant_expression*/false);
15779 --cp_unevaluated_operand
;
15780 --c_inhibit_evaluation_warnings
;
15782 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15783 type
= lambda_capture_field_type (type
,
15784 false /*explicit_init*/,
15785 DECLTYPE_FOR_REF_CAPTURE (t
));
15786 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15787 type
= lambda_proxy_type (type
);
15790 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15791 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15793 /* In a template ~id could be either a complement expression
15794 or an unqualified-id naming a destructor; if instantiating
15795 it produces an expression, it's not an id-expression or
15798 type
= finish_decltype_type (type
, id
, complain
);
15800 return cp_build_qualified_type_real (type
,
15802 | cp_type_quals (type
),
15803 complain
| tf_ignore_bad_quals
);
15806 case UNDERLYING_TYPE
:
15808 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15809 complain
, in_decl
);
15810 return finish_underlying_type (type
);
15813 case TYPE_ARGUMENT_PACK
:
15814 case NONTYPE_ARGUMENT_PACK
:
15818 if (code
== NONTYPE_ARGUMENT_PACK
)
15819 r
= make_node (code
);
15821 r
= cxx_make_type (code
);
15823 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15824 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15825 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15843 /* We should use one of the expression tsubsts for these codes. */
15844 gcc_unreachable ();
15847 sorry ("use of %qs in template", get_tree_code_name (code
));
15848 return error_mark_node
;
15852 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15853 expression on the left-hand side of the "." or "->" operator. We
15854 only do the lookup if we had a dependent BASELINK. Otherwise we
15855 adjust it onto the instantiated heirarchy. */
15858 tsubst_baselink (tree baselink
, tree object_type
,
15859 tree args
, tsubst_flags_t complain
, tree in_decl
)
15861 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15862 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15863 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15865 tree optype
= BASELINK_OPTYPE (baselink
);
15866 optype
= tsubst (optype
, args
, complain
, in_decl
);
15868 tree template_args
= NULL_TREE
;
15869 bool template_id_p
= false;
15870 tree fns
= BASELINK_FUNCTIONS (baselink
);
15871 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15873 template_id_p
= true;
15874 template_args
= TREE_OPERAND (fns
, 1);
15875 fns
= TREE_OPERAND (fns
, 0);
15877 template_args
= tsubst_template_args (template_args
, args
,
15878 complain
, in_decl
);
15881 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15882 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15883 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15887 tree name
= OVL_NAME (fns
);
15888 if (IDENTIFIER_CONV_OP_P (name
))
15889 name
= make_conv_op_name (optype
);
15891 if (name
== complete_dtor_identifier
)
15892 /* Treat as-if non-dependent below. */
15893 dependent_p
= false;
15895 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15898 if ((complain
& tf_error
)
15899 && constructor_name_p (name
, qualifying_scope
))
15900 error ("cannot call constructor %<%T::%D%> directly",
15901 qualifying_scope
, name
);
15902 return error_mark_node
;
15905 if (BASELINK_P (baselink
))
15906 fns
= BASELINK_FUNCTIONS (baselink
);
15909 /* We're going to overwrite pieces below, make a duplicate. */
15910 baselink
= copy_node (baselink
);
15912 /* If lookup found a single function, mark it as used at this point.
15913 (If lookup found multiple functions the one selected later by
15914 overload resolution will be marked as used at that point.) */
15915 if (!template_id_p
&& !really_overloaded_fn (fns
))
15917 tree fn
= OVL_FIRST (fns
);
15918 bool ok
= mark_used (fn
, complain
);
15919 if (!ok
&& !(complain
& tf_error
))
15920 return error_mark_node
;
15921 if (ok
&& BASELINK_P (baselink
))
15922 /* We might have instantiated an auto function. */
15923 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15926 if (BASELINK_P (baselink
))
15928 /* Add back the template arguments, if present. */
15930 BASELINK_FUNCTIONS (baselink
)
15931 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15933 /* Update the conversion operator type. */
15934 BASELINK_OPTYPE (baselink
) = optype
;
15938 object_type
= current_class_type
;
15940 if (qualified_p
|| !dependent_p
)
15942 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15946 /* We need to call adjust_result_of_qualified_name_lookup in case the
15947 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15948 so that we still get virtual function binding. */
15949 BASELINK_QUALIFIED_P (baselink
) = false;
15955 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15956 true if the qualified-id will be a postfix-expression in-and-of
15957 itself; false if more of the postfix-expression follows the
15958 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15962 tsubst_qualified_id (tree qualified_id
, tree args
,
15963 tsubst_flags_t complain
, tree in_decl
,
15964 bool done
, bool address_p
)
15970 tree template_args
;
15971 location_t loc
= UNKNOWN_LOCATION
;
15973 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15975 /* Figure out what name to look up. */
15976 name
= TREE_OPERAND (qualified_id
, 1);
15977 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15979 is_template
= true;
15980 loc
= EXPR_LOCATION (name
);
15981 template_args
= TREE_OPERAND (name
, 1);
15983 template_args
= tsubst_template_args (template_args
, args
,
15984 complain
, in_decl
);
15985 if (template_args
== error_mark_node
)
15986 return error_mark_node
;
15987 name
= TREE_OPERAND (name
, 0);
15991 is_template
= false;
15992 template_args
= NULL_TREE
;
15995 /* Substitute into the qualifying scope. When there are no ARGS, we
15996 are just trying to simplify a non-dependent expression. In that
15997 case the qualifying scope may be dependent, and, in any case,
15998 substituting will not help. */
15999 scope
= TREE_OPERAND (qualified_id
, 0);
16002 scope
= tsubst (scope
, args
, complain
, in_decl
);
16003 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16008 if (dependent_scope_p (scope
))
16011 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16012 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16013 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16014 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16018 if (!BASELINK_P (name
) && !DECL_P (expr
))
16020 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16022 /* A BIT_NOT_EXPR is used to represent a destructor. */
16023 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16025 error ("qualifying type %qT does not match destructor name ~%qT",
16026 scope
, TREE_OPERAND (expr
, 0));
16027 expr
= error_mark_node
;
16030 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16031 /*is_type_p=*/0, false);
16034 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
16035 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16036 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16038 if (complain
& tf_error
)
16040 error ("dependent-name %qE is parsed as a non-type, but "
16041 "instantiation yields a type", qualified_id
);
16042 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16044 return error_mark_node
;
16050 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16052 /* Remember that there was a reference to this entity. */
16053 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16054 return error_mark_node
;
16057 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16059 if (complain
& tf_error
)
16060 qualified_name_lookup_error (scope
,
16061 TREE_OPERAND (qualified_id
, 1),
16062 expr
, input_location
);
16063 return error_mark_node
;
16068 /* We may be repeating a check already done during parsing, but
16069 if it was well-formed and passed then, it will pass again
16070 now, and if it didn't, we wouldn't have got here. The case
16071 we want to catch is when we couldn't tell then, and can now,
16072 namely when templ prior to substitution was an
16074 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16075 return error_mark_node
;
16077 if (variable_template_p (expr
))
16078 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16081 expr
= lookup_template_function (expr
, template_args
);
16084 if (expr
== error_mark_node
&& complain
& tf_error
)
16085 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16086 expr
, input_location
);
16087 else if (TYPE_P (scope
))
16089 expr
= (adjust_result_of_qualified_name_lookup
16090 (expr
, scope
, current_nonlambda_class_type ()));
16091 expr
= (finish_qualified_id_expr
16092 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16093 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16094 /*template_arg_p=*/false, complain
));
16097 /* Expressions do not generally have reference type. */
16098 if (TREE_CODE (expr
) != SCOPE_REF
16099 /* However, if we're about to form a pointer-to-member, we just
16100 want the referenced member referenced. */
16101 && TREE_CODE (expr
) != OFFSET_REF
)
16102 expr
= convert_from_reference (expr
);
16104 if (REF_PARENTHESIZED_P (qualified_id
))
16105 expr
= force_paren_expr (expr
);
16110 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16111 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16115 tsubst_init (tree init
, tree decl
, tree args
,
16116 tsubst_flags_t complain
, tree in_decl
)
16121 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16123 tree type
= TREE_TYPE (decl
);
16125 if (!init
&& type
!= error_mark_node
)
16127 if (tree auto_node
= type_uses_auto (type
))
16129 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16131 if (complain
& tf_error
)
16132 error ("initializer for %q#D expands to an empty list "
16133 "of expressions", decl
);
16134 return error_mark_node
;
16137 else if (!dependent_type_p (type
))
16139 /* If we had an initializer but it
16140 instantiated to nothing,
16141 value-initialize the object. This will
16142 only occur when the initializer was a
16143 pack expansion where the parameter packs
16144 used in that expansion were of length
16146 init
= build_value_init (type
, complain
);
16147 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16148 init
= get_target_expr_sfinae (init
, complain
);
16149 if (TREE_CODE (init
) == TARGET_EXPR
)
16150 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16157 /* Like tsubst, but deals with expressions. This function just replaces
16158 template parms; to finish processing the resultant expression, use
16159 tsubst_copy_and_build or tsubst_expr. */
16162 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16164 enum tree_code code
;
16167 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16170 code
= TREE_CODE (t
);
16175 r
= retrieve_local_specialization (t
);
16177 if (r
== NULL_TREE
)
16179 /* We get here for a use of 'this' in an NSDMI. */
16180 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16181 return current_class_ptr
;
16183 /* This can happen for a parameter name used later in a function
16184 declaration (such as in a late-specified return type). Just
16185 make a dummy decl, since it's only used for its type. */
16186 gcc_assert (cp_unevaluated_operand
!= 0);
16187 r
= tsubst_decl (t
, args
, complain
);
16188 /* Give it the template pattern as its context; its true context
16189 hasn't been instantiated yet and this is good enough for
16191 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16194 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16195 r
= argument_pack_select_arg (r
);
16196 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16197 return error_mark_node
;
16205 if (DECL_TEMPLATE_PARM_P (t
))
16206 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16207 /* There is no need to substitute into namespace-scope
16209 if (DECL_NAMESPACE_SCOPE_P (t
))
16211 /* If ARGS is NULL, then T is known to be non-dependent. */
16212 if (args
== NULL_TREE
)
16213 return scalar_constant_value (t
);
16215 /* Unfortunately, we cannot just call lookup_name here.
16218 template <int I> int f() {
16220 struct S { void g() { E e = a; } };
16223 When we instantiate f<7>::S::g(), say, lookup_name is not
16224 clever enough to find f<7>::a. */
16226 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16227 /*entering_scope=*/0);
16229 for (v
= TYPE_VALUES (enum_type
);
16231 v
= TREE_CHAIN (v
))
16232 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16233 return TREE_VALUE (v
);
16235 /* We didn't find the name. That should never happen; if
16236 name-lookup found it during preliminary parsing, we
16237 should find it again here during instantiation. */
16238 gcc_unreachable ();
16243 if (DECL_CONTEXT (t
))
16247 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16248 /*entering_scope=*/1);
16249 if (ctx
!= DECL_CONTEXT (t
))
16251 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16254 if (complain
& tf_error
)
16255 error ("using invalid field %qD", t
);
16256 return error_mark_node
;
16265 case FUNCTION_DECL
:
16266 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16267 r
= tsubst (t
, args
, complain
, in_decl
);
16268 else if (local_variable_p (t
)
16269 && uses_template_parms (DECL_CONTEXT (t
)))
16271 r
= retrieve_local_specialization (t
);
16272 if (r
== NULL_TREE
)
16274 /* First try name lookup to find the instantiation. */
16275 r
= lookup_name (DECL_NAME (t
));
16280 /* During error-recovery we may find a non-variable,
16281 even an OVERLOAD: just bail out and avoid ICEs and
16282 duplicate diagnostics (c++/62207). */
16283 gcc_assert (seen_error ());
16284 return error_mark_node
;
16286 if (!is_capture_proxy (r
))
16288 /* Make sure the one we found is the one we want. */
16289 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16290 if (ctx
!= DECL_CONTEXT (r
))
16299 /* This can happen for a variable used in a
16300 late-specified return type of a local lambda, or for a
16301 local static or constant. Building a new VAR_DECL
16302 should be OK in all those cases. */
16303 r
= tsubst_decl (t
, args
, complain
);
16304 if (local_specializations
)
16305 /* Avoid infinite recursion (79640). */
16306 register_local_specialization (r
, t
);
16307 if (decl_maybe_constant_var_p (r
))
16309 /* We can't call cp_finish_decl, so handle the
16310 initializer by hand. */
16311 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16312 complain
, in_decl
);
16313 if (!processing_template_decl
)
16314 init
= maybe_constant_init (init
);
16315 if (processing_template_decl
16316 ? potential_constant_expression (init
)
16317 : reduced_constant_expression_p (init
))
16318 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16319 = TREE_CONSTANT (r
) = true;
16320 DECL_INITIAL (r
) = init
;
16321 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16323 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16324 complain
, adc_variable_type
);
16326 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16327 || decl_constant_var_p (r
)
16329 if (!processing_template_decl
16330 && !TREE_STATIC (r
))
16331 r
= process_outer_var_ref (r
, complain
);
16333 /* Remember this for subsequent uses. */
16334 if (local_specializations
)
16335 register_local_specialization (r
, t
);
16337 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16338 r
= argument_pack_select_arg (r
);
16342 if (!mark_used (r
, complain
))
16343 return error_mark_node
;
16346 case NAMESPACE_DECL
:
16353 return tsubst_baselink (t
, current_nonlambda_class_type (),
16354 args
, complain
, in_decl
);
16356 case TEMPLATE_DECL
:
16357 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16358 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16359 args
, complain
, in_decl
);
16360 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16361 return tsubst (t
, args
, complain
, in_decl
);
16362 else if (DECL_CLASS_SCOPE_P (t
)
16363 && uses_template_parms (DECL_CONTEXT (t
)))
16365 /* Template template argument like the following example need
16368 template <template <class> class TT> struct C {};
16369 template <class T> struct D {
16370 template <class U> struct E {};
16375 We are processing the template argument `E' in #1 for
16376 the template instantiation #2. Originally, `E' is a
16377 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16378 have to substitute this with one having context `D<int>'. */
16380 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16381 if (dependent_scope_p (context
))
16383 /* When rewriting a constructor into a deduction guide, a
16384 non-dependent name can become dependent, so memtmpl<args>
16385 becomes context::template memtmpl<args>. */
16386 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16387 return build_qualified_name (type
, context
, DECL_NAME (t
),
16390 return lookup_field (context
, DECL_NAME(t
), 0, false);
16393 /* Ordinary template template argument. */
16396 case NON_LVALUE_EXPR
:
16397 case VIEW_CONVERT_EXPR
:
16399 /* Handle location wrappers by substituting the wrapped node
16400 first, *then* reusing the resulting type. Doing the type
16401 first ensures that we handle template parameters and
16402 parameter pack expansions. */
16403 if (location_wrapper_p (t
))
16405 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16406 complain
, in_decl
);
16407 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16409 tree op
= TREE_OPERAND (t
, 0);
16410 if (code
== VIEW_CONVERT_EXPR
16411 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16413 /* Wrapper to make a C++20 template parameter object const. */
16414 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16415 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16417 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16418 return build1 (code
, type
, op
);
16422 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16423 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16424 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16428 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16429 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16431 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16432 op
= build1 (code
, TREE_TYPE (op
), op
);
16433 REF_PARENTHESIZED_P (op
) = true;
16436 /* We shouldn't see any other uses of these in templates. */
16437 gcc_unreachable ();
16441 case REINTERPRET_CAST_EXPR
:
16442 case CONST_CAST_EXPR
:
16443 case STATIC_CAST_EXPR
:
16444 case DYNAMIC_CAST_EXPR
:
16445 case IMPLICIT_CONV_EXPR
:
16449 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16450 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16451 return build1 (code
, type
, op0
);
16455 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16456 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16458 tree expanded
, op
= TREE_OPERAND (t
, 0);
16461 if (SIZEOF_EXPR_TYPE_P (t
))
16462 op
= TREE_TYPE (op
);
16464 ++cp_unevaluated_operand
;
16465 ++c_inhibit_evaluation_warnings
;
16466 /* We only want to compute the number of arguments. */
16467 if (PACK_EXPANSION_P (op
))
16468 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16470 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16471 args
, complain
, in_decl
);
16472 --cp_unevaluated_operand
;
16473 --c_inhibit_evaluation_warnings
;
16475 if (TREE_CODE (expanded
) == TREE_VEC
)
16477 len
= TREE_VEC_LENGTH (expanded
);
16478 /* Set TREE_USED for the benefit of -Wunused. */
16479 for (int i
= 0; i
< len
; i
++)
16480 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16481 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16484 if (expanded
== error_mark_node
)
16485 return error_mark_node
;
16486 else if (PACK_EXPANSION_P (expanded
)
16487 || (TREE_CODE (expanded
) == TREE_VEC
16488 && pack_expansion_args_count (expanded
)))
16491 if (PACK_EXPANSION_P (expanded
))
16493 else if (TREE_VEC_LENGTH (expanded
) == 1)
16494 expanded
= TREE_VEC_ELT (expanded
, 0);
16496 expanded
= make_argument_pack (expanded
);
16498 if (TYPE_P (expanded
))
16499 return cxx_sizeof_or_alignof_type (input_location
,
16500 expanded
, SIZEOF_EXPR
,
16502 complain
& tf_error
);
16504 return cxx_sizeof_or_alignof_expr (input_location
,
16505 expanded
, SIZEOF_EXPR
,
16506 complain
& tf_error
);
16509 return build_int_cst (size_type_node
, len
);
16511 if (SIZEOF_EXPR_TYPE_P (t
))
16513 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16514 args
, complain
, in_decl
);
16515 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16516 r
= build1 (SIZEOF_EXPR
,
16517 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16518 SIZEOF_EXPR_TYPE_P (r
) = 1;
16525 case TRUTH_NOT_EXPR
:
16528 case UNARY_PLUS_EXPR
: /* Unary + */
16530 case AT_ENCODE_EXPR
:
16534 case REALPART_EXPR
:
16535 case IMAGPART_EXPR
:
16538 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16539 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16540 r
= build1 (code
, type
, op0
);
16541 if (code
== ALIGNOF_EXPR
)
16542 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16546 case COMPONENT_REF
:
16551 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16552 name
= TREE_OPERAND (t
, 1);
16553 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16555 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16556 complain
, in_decl
);
16557 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16559 else if (TREE_CODE (name
) == SCOPE_REF
16560 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16562 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16563 complain
, in_decl
);
16564 name
= TREE_OPERAND (name
, 1);
16565 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16566 complain
, in_decl
);
16567 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16568 name
= build_qualified_name (/*type=*/NULL_TREE
,
16570 /*template_p=*/false);
16572 else if (BASELINK_P (name
))
16573 name
= tsubst_baselink (name
,
16574 non_reference (TREE_TYPE (object
)),
16578 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16579 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16585 case TRUNC_DIV_EXPR
:
16586 case CEIL_DIV_EXPR
:
16587 case FLOOR_DIV_EXPR
:
16588 case ROUND_DIV_EXPR
:
16589 case EXACT_DIV_EXPR
:
16593 case TRUNC_MOD_EXPR
:
16594 case FLOOR_MOD_EXPR
:
16595 case TRUTH_ANDIF_EXPR
:
16596 case TRUTH_ORIF_EXPR
:
16597 case TRUTH_AND_EXPR
:
16598 case TRUTH_OR_EXPR
:
16609 case COMPOUND_EXPR
:
16612 case PREDECREMENT_EXPR
:
16613 case PREINCREMENT_EXPR
:
16614 case POSTDECREMENT_EXPR
:
16615 case POSTINCREMENT_EXPR
:
16617 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16618 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16619 return build_nt (code
, op0
, op1
);
16624 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16625 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16626 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16627 QUALIFIED_NAME_IS_TEMPLATE (t
));
16632 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16633 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16634 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16639 int n
= VL_EXP_OPERAND_LENGTH (t
);
16640 tree result
= build_vl_exp (CALL_EXPR
, n
);
16642 for (i
= 0; i
< n
; i
++)
16643 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16644 complain
, in_decl
);
16650 case PSEUDO_DTOR_EXPR
:
16651 case VEC_PERM_EXPR
:
16653 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16654 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16655 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16656 r
= build_nt (code
, op0
, op1
, op2
);
16657 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16663 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16664 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16665 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16666 r
= build_nt (code
, op0
, op1
, op2
);
16667 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16673 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16674 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16675 r
= build_nt (code
, op0
, op1
);
16676 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16677 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16681 case TEMPLATE_ID_EXPR
:
16683 /* Substituted template arguments */
16684 tree fn
= TREE_OPERAND (t
, 0);
16685 tree targs
= TREE_OPERAND (t
, 1);
16687 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16689 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16691 return lookup_template_function (fn
, targs
);
16696 tree purpose
, value
, chain
;
16698 if (t
== void_list_node
)
16701 purpose
= TREE_PURPOSE (t
);
16703 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16704 value
= TREE_VALUE (t
);
16706 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16707 chain
= TREE_CHAIN (t
);
16708 if (chain
&& chain
!= void_type_node
)
16709 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16710 if (purpose
== TREE_PURPOSE (t
)
16711 && value
== TREE_VALUE (t
)
16712 && chain
== TREE_CHAIN (t
))
16714 return tree_cons (purpose
, value
, chain
);
16719 case ENUMERAL_TYPE
:
16721 case TEMPLATE_TYPE_PARM
:
16722 case TEMPLATE_TEMPLATE_PARM
:
16723 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16724 case TEMPLATE_PARM_INDEX
:
16726 case REFERENCE_TYPE
:
16728 case FUNCTION_TYPE
:
16731 case TYPENAME_TYPE
:
16732 case UNBOUND_CLASS_TEMPLATE
:
16734 case DECLTYPE_TYPE
:
16736 return tsubst (t
, args
, complain
, in_decl
);
16740 /* Fall through. */
16741 case IDENTIFIER_NODE
:
16742 if (IDENTIFIER_CONV_OP_P (t
))
16744 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16745 return make_conv_op_name (new_type
);
16751 /* This is handled by tsubst_copy_and_build. */
16752 gcc_unreachable ();
16756 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16757 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16758 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16761 case CLEANUP_POINT_EXPR
:
16762 /* We shouldn't have built any of these during initial template
16763 generation. Instead, they should be built during instantiation
16764 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16765 gcc_unreachable ();
16769 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16770 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16771 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16772 r
= build2 (code
, type
, op0
, op1
);
16773 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16774 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16775 && !(complain
& tf_error
))
16776 return error_mark_node
;
16780 case EXPR_PACK_EXPANSION
:
16781 error ("invalid use of pack expansion expression");
16782 return error_mark_node
;
16784 case NONTYPE_ARGUMENT_PACK
:
16785 error ("use %<...%> to expand argument pack");
16786 return error_mark_node
;
16789 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16796 /* Instantiate any typedefs in the type. */
16797 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16798 r
= fold_convert (type
, t
);
16799 gcc_assert (TREE_CODE (r
) == code
);
16805 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16807 if (type
!= TREE_TYPE (t
))
16810 TREE_TYPE (r
) = type
;
16816 /* These can sometimes show up in a partial instantiation, but never
16817 involve template parms. */
16818 gcc_assert (!uses_template_parms (t
));
16821 case UNARY_LEFT_FOLD_EXPR
:
16822 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16823 case UNARY_RIGHT_FOLD_EXPR
:
16824 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16825 case BINARY_LEFT_FOLD_EXPR
:
16826 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16827 case BINARY_RIGHT_FOLD_EXPR
:
16828 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16832 case DEBUG_BEGIN_STMT
:
16833 /* ??? There's no point in copying it for now, but maybe some
16834 day it will contain more information, such as a pointer back
16835 to the containing function, inlined copy or so. */
16838 case CO_AWAIT_EXPR
:
16839 return tsubst_expr (t
, args
, complain
, in_decl
,
16840 /*integral_constant_expression_p=*/false);
16844 /* We shouldn't get here, but keep going if !flag_checking. */
16846 gcc_unreachable ();
16851 /* Helper function for tsubst_omp_clauses, used for instantiation of
16852 OMP_CLAUSE_DECL of clauses. */
16855 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16856 tree in_decl
, tree
*iterator_cache
)
16858 if (decl
== NULL_TREE
)
16861 /* Handle OpenMP iterators. */
16862 if (TREE_CODE (decl
) == TREE_LIST
16863 && TREE_PURPOSE (decl
)
16864 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16867 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16868 ret
= iterator_cache
[1];
16872 begin_scope (sk_omp
, NULL
);
16873 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16875 *tp
= copy_node (it
);
16876 TREE_VEC_ELT (*tp
, 0)
16877 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16878 TREE_VEC_ELT (*tp
, 1)
16879 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16880 /*integral_constant_expression_p=*/false);
16881 TREE_VEC_ELT (*tp
, 2)
16882 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16883 /*integral_constant_expression_p=*/false);
16884 TREE_VEC_ELT (*tp
, 3)
16885 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16886 /*integral_constant_expression_p=*/false);
16887 TREE_CHAIN (*tp
) = NULL_TREE
;
16888 tp
= &TREE_CHAIN (*tp
);
16890 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16891 iterator_cache
[0] = TREE_PURPOSE (decl
);
16892 iterator_cache
[1] = ret
;
16894 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16899 /* Handle an OpenMP array section represented as a TREE_LIST (or
16900 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16901 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16902 TREE_LIST. We can handle it exactly the same as an array section
16903 (purpose, value, and a chain), even though the nomenclature
16904 (low_bound, length, etc) is different. */
16905 if (TREE_CODE (decl
) == TREE_LIST
)
16908 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16909 /*integral_constant_expression_p=*/false);
16910 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16911 /*integral_constant_expression_p=*/false);
16912 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16914 if (TREE_PURPOSE (decl
) == low_bound
16915 && TREE_VALUE (decl
) == length
16916 && TREE_CHAIN (decl
) == chain
)
16918 tree ret
= tree_cons (low_bound
, length
, chain
);
16919 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16920 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16923 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16924 /*integral_constant_expression_p=*/false);
16925 /* Undo convert_from_reference tsubst_expr could have called. */
16927 && REFERENCE_REF_P (ret
)
16928 && !REFERENCE_REF_P (decl
))
16929 ret
= TREE_OPERAND (ret
, 0);
16933 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16936 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16937 tree args
, tsubst_flags_t complain
, tree in_decl
)
16939 tree new_clauses
= NULL_TREE
, nc
, oc
;
16940 tree linear_no_step
= NULL_TREE
;
16941 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16943 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16945 nc
= copy_node (oc
);
16946 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16949 switch (OMP_CLAUSE_CODE (nc
))
16951 case OMP_CLAUSE_LASTPRIVATE
:
16952 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16954 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16955 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16956 in_decl
, /*integral_constant_expression_p=*/false);
16957 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16958 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16961 case OMP_CLAUSE_PRIVATE
:
16962 case OMP_CLAUSE_SHARED
:
16963 case OMP_CLAUSE_FIRSTPRIVATE
:
16964 case OMP_CLAUSE_COPYIN
:
16965 case OMP_CLAUSE_COPYPRIVATE
:
16966 case OMP_CLAUSE_UNIFORM
:
16967 case OMP_CLAUSE_DEPEND
:
16968 case OMP_CLAUSE_FROM
:
16969 case OMP_CLAUSE_TO
:
16970 case OMP_CLAUSE_MAP
:
16971 case OMP_CLAUSE_NONTEMPORAL
:
16972 case OMP_CLAUSE_USE_DEVICE_PTR
:
16973 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16974 case OMP_CLAUSE_IS_DEVICE_PTR
:
16975 case OMP_CLAUSE_INCLUSIVE
:
16976 case OMP_CLAUSE_EXCLUSIVE
:
16977 OMP_CLAUSE_DECL (nc
)
16978 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16979 in_decl
, iterator_cache
);
16981 case OMP_CLAUSE_TILE
:
16982 case OMP_CLAUSE_IF
:
16983 case OMP_CLAUSE_NUM_THREADS
:
16984 case OMP_CLAUSE_SCHEDULE
:
16985 case OMP_CLAUSE_COLLAPSE
:
16986 case OMP_CLAUSE_FINAL
:
16987 case OMP_CLAUSE_DEVICE
:
16988 case OMP_CLAUSE_DIST_SCHEDULE
:
16989 case OMP_CLAUSE_NUM_TEAMS
:
16990 case OMP_CLAUSE_THREAD_LIMIT
:
16991 case OMP_CLAUSE_SAFELEN
:
16992 case OMP_CLAUSE_SIMDLEN
:
16993 case OMP_CLAUSE_NUM_TASKS
:
16994 case OMP_CLAUSE_GRAINSIZE
:
16995 case OMP_CLAUSE_PRIORITY
:
16996 case OMP_CLAUSE_ORDERED
:
16997 case OMP_CLAUSE_HINT
:
16998 case OMP_CLAUSE_NUM_GANGS
:
16999 case OMP_CLAUSE_NUM_WORKERS
:
17000 case OMP_CLAUSE_VECTOR_LENGTH
:
17001 case OMP_CLAUSE_WORKER
:
17002 case OMP_CLAUSE_VECTOR
:
17003 case OMP_CLAUSE_ASYNC
:
17004 case OMP_CLAUSE_WAIT
:
17005 OMP_CLAUSE_OPERAND (nc
, 0)
17006 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17007 in_decl
, /*integral_constant_expression_p=*/false);
17009 case OMP_CLAUSE_REDUCTION
:
17010 case OMP_CLAUSE_IN_REDUCTION
:
17011 case OMP_CLAUSE_TASK_REDUCTION
:
17012 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17014 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17015 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17017 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17018 complain
, in_decl
);
17019 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17020 = build_qualified_name (NULL_TREE
, scope
,
17021 TREE_OPERAND (placeholder
, 1),
17025 gcc_assert (identifier_p (placeholder
));
17027 OMP_CLAUSE_DECL (nc
)
17028 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17031 case OMP_CLAUSE_GANG
:
17032 case OMP_CLAUSE_ALIGNED
:
17033 OMP_CLAUSE_DECL (nc
)
17034 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17036 OMP_CLAUSE_OPERAND (nc
, 1)
17037 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17038 in_decl
, /*integral_constant_expression_p=*/false);
17040 case OMP_CLAUSE_LINEAR
:
17041 OMP_CLAUSE_DECL (nc
)
17042 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17044 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17046 gcc_assert (!linear_no_step
);
17047 linear_no_step
= nc
;
17049 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17050 OMP_CLAUSE_LINEAR_STEP (nc
)
17051 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17052 complain
, in_decl
, NULL
);
17054 OMP_CLAUSE_LINEAR_STEP (nc
)
17055 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17057 /*integral_constant_expression_p=*/false);
17059 case OMP_CLAUSE_NOWAIT
:
17060 case OMP_CLAUSE_DEFAULT
:
17061 case OMP_CLAUSE_UNTIED
:
17062 case OMP_CLAUSE_MERGEABLE
:
17063 case OMP_CLAUSE_INBRANCH
:
17064 case OMP_CLAUSE_NOTINBRANCH
:
17065 case OMP_CLAUSE_PROC_BIND
:
17066 case OMP_CLAUSE_FOR
:
17067 case OMP_CLAUSE_PARALLEL
:
17068 case OMP_CLAUSE_SECTIONS
:
17069 case OMP_CLAUSE_TASKGROUP
:
17070 case OMP_CLAUSE_NOGROUP
:
17071 case OMP_CLAUSE_THREADS
:
17072 case OMP_CLAUSE_SIMD
:
17073 case OMP_CLAUSE_DEFAULTMAP
:
17074 case OMP_CLAUSE_ORDER
:
17075 case OMP_CLAUSE_BIND
:
17076 case OMP_CLAUSE_INDEPENDENT
:
17077 case OMP_CLAUSE_AUTO
:
17078 case OMP_CLAUSE_SEQ
:
17079 case OMP_CLAUSE_IF_PRESENT
:
17080 case OMP_CLAUSE_FINALIZE
:
17083 gcc_unreachable ();
17085 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17086 switch (OMP_CLAUSE_CODE (nc
))
17088 case OMP_CLAUSE_SHARED
:
17089 case OMP_CLAUSE_PRIVATE
:
17090 case OMP_CLAUSE_FIRSTPRIVATE
:
17091 case OMP_CLAUSE_LASTPRIVATE
:
17092 case OMP_CLAUSE_COPYPRIVATE
:
17093 case OMP_CLAUSE_LINEAR
:
17094 case OMP_CLAUSE_REDUCTION
:
17095 case OMP_CLAUSE_IN_REDUCTION
:
17096 case OMP_CLAUSE_TASK_REDUCTION
:
17097 case OMP_CLAUSE_USE_DEVICE_PTR
:
17098 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17099 case OMP_CLAUSE_IS_DEVICE_PTR
:
17100 case OMP_CLAUSE_INCLUSIVE
:
17101 case OMP_CLAUSE_EXCLUSIVE
:
17102 /* tsubst_expr on SCOPE_REF results in returning
17103 finish_non_static_data_member result. Undo that here. */
17104 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17105 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17106 == IDENTIFIER_NODE
))
17108 tree t
= OMP_CLAUSE_DECL (nc
);
17111 switch (TREE_CODE (v
))
17113 case COMPONENT_REF
:
17117 case POINTER_PLUS_EXPR
:
17118 v
= TREE_OPERAND (v
, 0);
17121 if (DECL_CONTEXT (v
) == current_function_decl
17122 && DECL_ARTIFICIAL (v
)
17123 && DECL_NAME (v
) == this_identifier
)
17124 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17131 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17132 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17133 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17134 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17135 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17137 tree decl
= OMP_CLAUSE_DECL (nc
);
17140 retrofit_lang_decl (decl
);
17141 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17150 new_clauses
= nreverse (new_clauses
);
17151 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17153 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17154 if (linear_no_step
)
17155 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17156 if (nc
== linear_no_step
)
17158 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17162 return new_clauses
;
17165 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17168 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17171 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17173 tree purpose
, value
, chain
;
17178 if (TREE_CODE (t
) != TREE_LIST
)
17179 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17180 /*function_p=*/false,
17181 /*integral_constant_expression_p=*/false);
17183 if (t
== void_list_node
)
17186 purpose
= TREE_PURPOSE (t
);
17188 purpose
= RECUR (purpose
);
17189 value
= TREE_VALUE (t
);
17192 if (TREE_CODE (value
) != LABEL_DECL
)
17193 value
= RECUR (value
);
17196 value
= lookup_label (DECL_NAME (value
));
17197 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17198 TREE_USED (value
) = 1;
17201 chain
= TREE_CHAIN (t
);
17202 if (chain
&& chain
!= void_type_node
)
17203 chain
= RECUR (chain
);
17204 return tree_cons (purpose
, value
, chain
);
17208 /* Used to temporarily communicate the list of #pragma omp parallel
17209 clauses to #pragma omp for instantiation if they are combined
17212 static tree
*omp_parallel_combined_clauses
;
17214 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17215 tree
*, unsigned int *);
17217 /* Substitute one OMP_FOR iterator. */
17220 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17221 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17222 tree args
, tsubst_flags_t complain
, tree in_decl
,
17223 bool integral_constant_expression_p
)
17225 #define RECUR(NODE) \
17226 tsubst_expr ((NODE), args, complain, in_decl, \
17227 integral_constant_expression_p)
17228 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17231 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17232 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17234 decl
= TREE_OPERAND (init
, 0);
17235 init
= TREE_OPERAND (init
, 1);
17236 tree decl_expr
= NULL_TREE
;
17237 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17240 bool decomp
= false;
17241 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17243 tree v
= DECL_VALUE_EXPR (decl
);
17244 if (TREE_CODE (v
) == ARRAY_REF
17245 && VAR_P (TREE_OPERAND (v
, 0))
17246 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17248 tree decomp_first
= NULL_TREE
;
17249 unsigned decomp_cnt
= 0;
17250 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17251 maybe_push_decl (d
);
17252 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17253 in_decl
, &decomp_first
, &decomp_cnt
);
17255 if (d
== error_mark_node
)
17256 decl
= error_mark_node
;
17258 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17260 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17262 tree v
= build_nt (ARRAY_REF
, d
,
17263 size_int (decomp_cnt
- i
- 1),
17264 NULL_TREE
, NULL_TREE
);
17265 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17266 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17268 fit_decomposition_lang_decl (decomp_first
, d
);
17269 decomp_first
= DECL_CHAIN (decomp_first
);
17273 decl
= tsubst_decl (decl
, args
, complain
);
17275 maybe_push_decl (decl
);
17277 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17279 /* We need to jump through some hoops to handle declarations in the
17280 init-statement, since we might need to handle auto deduction,
17281 but we need to keep control of initialization. */
17283 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17284 decl
= tsubst_decl (decl
, args
, complain
);
17288 if (TREE_CODE (decl
) == SCOPE_REF
)
17290 decl
= RECUR (decl
);
17291 if (TREE_CODE (decl
) == COMPONENT_REF
)
17295 switch (TREE_CODE (v
))
17297 case COMPONENT_REF
:
17301 case POINTER_PLUS_EXPR
:
17302 v
= TREE_OPERAND (v
, 0);
17305 if (DECL_CONTEXT (v
) == current_function_decl
17306 && DECL_ARTIFICIAL (v
)
17307 && DECL_NAME (v
) == this_identifier
)
17309 decl
= TREE_OPERAND (decl
, 1);
17310 decl
= omp_privatize_field (decl
, false);
17320 decl
= RECUR (decl
);
17322 init
= RECUR (init
);
17324 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17326 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17327 if (TREE_CODE (o
) == TREE_LIST
)
17328 TREE_VEC_ELT (orig_declv
, i
)
17329 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17330 RECUR (TREE_VALUE (o
)),
17333 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17338 tree this_pre_body
= NULL_TREE
;
17339 tree orig_init
= NULL_TREE
;
17340 tree orig_decl
= NULL_TREE
;
17341 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17342 orig_init
, cond
, incr
);
17345 if (orig_declv
== NULL_TREE
)
17346 orig_declv
= copy_node (declv
);
17347 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17350 else if (orig_declv
)
17351 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17354 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17355 if (!range_for
&& auto_node
&& init
)
17357 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17359 gcc_assert (!type_dependent_expression_p (decl
));
17361 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17365 /* Declare the variable, but don't let that initialize it. */
17366 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17367 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17369 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17374 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
17375 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17376 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17378 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17379 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17380 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17381 NOP_EXPR
, rhs
, complain
);
17384 incr
= RECUR (incr
);
17385 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17386 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17388 TREE_VEC_ELT (declv
, i
) = decl
;
17389 TREE_VEC_ELT (initv
, i
) = init
;
17390 TREE_VEC_ELT (condv
, i
) = cond
;
17391 TREE_VEC_ELT (incrv
, i
) = incr
;
17397 /* Declare and initialize the variable. */
17405 for (j
= ((omp_parallel_combined_clauses
== NULL
17406 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17408 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17410 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17411 && OMP_CLAUSE_DECL (*pc
) == decl
)
17413 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17414 && OMP_CLAUSE_DECL (*pc
) == decl
)
17418 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17420 *pc
= OMP_CLAUSE_CHAIN (c
);
17421 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17424 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17425 && OMP_CLAUSE_DECL (*pc
) == decl
)
17427 error ("iteration variable %qD should not be firstprivate",
17429 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17431 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17432 && OMP_CLAUSE_DECL (*pc
) == decl
)
17434 error ("iteration variable %qD should not be reduction",
17436 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17439 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17444 if (*pc
== NULL_TREE
)
17446 tree c
= build_omp_clause (input_location
,
17447 TREE_CODE (t
) == OMP_LOOP
17448 ? OMP_CLAUSE_LASTPRIVATE
17449 : OMP_CLAUSE_PRIVATE
);
17450 OMP_CLAUSE_DECL (c
) = decl
;
17451 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17454 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17459 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17460 if (COMPARISON_CLASS_P (cond
))
17462 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17463 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17464 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17467 cond
= RECUR (cond
);
17468 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17469 switch (TREE_CODE (incr
))
17471 case PREINCREMENT_EXPR
:
17472 case PREDECREMENT_EXPR
:
17473 case POSTINCREMENT_EXPR
:
17474 case POSTDECREMENT_EXPR
:
17475 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17476 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17479 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17480 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17482 tree rhs
= TREE_OPERAND (incr
, 1);
17483 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17484 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17485 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17486 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17487 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17491 incr
= RECUR (incr
);
17494 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17495 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17497 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17498 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17499 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17500 TREE_TYPE (decl
), lhs
,
17501 RECUR (TREE_OPERAND (incr
, 2))));
17503 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17504 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17505 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17507 tree rhs
= TREE_OPERAND (incr
, 2);
17508 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17509 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17510 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17511 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17512 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17516 incr
= RECUR (incr
);
17519 incr
= RECUR (incr
);
17523 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17524 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17525 TREE_VEC_ELT (declv
, i
) = decl
;
17526 TREE_VEC_ELT (initv
, i
) = init
;
17527 TREE_VEC_ELT (condv
, i
) = cond
;
17528 TREE_VEC_ELT (incrv
, i
) = incr
;
17533 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17534 of OMP_TARGET's body. */
17537 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17539 *walk_subtrees
= 0;
17540 switch (TREE_CODE (*tp
))
17545 case STATEMENT_LIST
:
17546 *walk_subtrees
= 1;
17554 /* Helper function for tsubst_expr. For decomposition declaration
17555 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17556 also the corresponding decls representing the identifiers
17557 of the decomposition declaration. Return DECL if successful
17558 or error_mark_node otherwise, set *FIRST to the first decl
17559 in the list chained through DECL_CHAIN and *CNT to the number
17563 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17564 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17567 tree decl2
, decl3
, prev
= decl
;
17569 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17570 for (decl2
= DECL_CHAIN (pattern_decl
);
17573 && DECL_DECOMPOSITION_P (decl2
)
17574 && DECL_NAME (decl2
);
17575 decl2
= DECL_CHAIN (decl2
))
17577 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17579 gcc_assert (errorcount
);
17580 return error_mark_node
;
17583 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17584 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17585 tree v
= DECL_VALUE_EXPR (decl2
);
17586 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17587 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17588 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17589 SET_DECL_VALUE_EXPR (decl2
, v
);
17590 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17592 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17595 gcc_assert (errorcount
);
17596 decl
= error_mark_node
;
17599 maybe_push_decl (decl3
);
17600 if (error_operand_p (decl3
))
17601 decl
= error_mark_node
;
17602 else if (decl
!= error_mark_node
17603 && DECL_CHAIN (decl3
) != prev
17606 gcc_assert (errorcount
);
17607 decl
= error_mark_node
;
17616 /* Return the proper local_specialization for init-capture pack DECL. */
17619 lookup_init_capture_pack (tree decl
)
17621 /* We handle normal pack captures by forwarding to the specialization of the
17622 captured parameter. We can't do that for pack init-captures; we need them
17623 to have their own local_specialization. We created the individual
17624 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17625 when we process the DECL_EXPR for the pack init-capture in the template.
17626 So, how do we find them? We don't know the capture proxy pack when
17627 building the individual resulting proxies, and we don't know the
17628 individual proxies when instantiating the pack. What we have in common is
17631 So...when we instantiate the FIELD_DECL, we stick the result in
17632 local_specializations. Then at the DECL_EXPR we look up that result, see
17633 how many elements it has, synthesize the names, and look them up. */
17635 tree cname
= DECL_NAME (decl
);
17636 tree val
= DECL_VALUE_EXPR (decl
);
17637 tree field
= TREE_OPERAND (val
, 1);
17638 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17639 tree fpack
= retrieve_local_specialization (field
);
17640 if (fpack
== error_mark_node
)
17641 return error_mark_node
;
17644 tree vec
= NULL_TREE
;
17645 tree r
= NULL_TREE
;
17646 if (TREE_CODE (fpack
) == TREE_VEC
)
17648 len
= TREE_VEC_LENGTH (fpack
);
17649 vec
= make_tree_vec (len
);
17650 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17651 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17653 for (int i
= 0; i
< len
; ++i
)
17655 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17656 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17658 TREE_VEC_ELT (vec
, i
) = elt
;
17665 /* Like tsubst_copy for expressions, etc. but also does semantic
17669 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17670 bool integral_constant_expression_p
)
17672 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17673 #define RECUR(NODE) \
17674 tsubst_expr ((NODE), args, complain, in_decl, \
17675 integral_constant_expression_p)
17681 if (t
== NULL_TREE
|| t
== error_mark_node
)
17684 loc
= input_location
;
17685 if (location_t eloc
= cp_expr_location (t
))
17686 input_location
= eloc
;
17687 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17688 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17690 switch (TREE_CODE (t
))
17692 case STATEMENT_LIST
:
17694 tree_stmt_iterator i
;
17695 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17696 RECUR (tsi_stmt (i
));
17700 case CTOR_INITIALIZER
:
17701 finish_mem_initializers (tsubst_initializer_list
17702 (TREE_OPERAND (t
, 0), args
));
17706 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17709 case CO_RETURN_EXPR
:
17710 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
17713 case CO_YIELD_EXPR
:
17714 stmt
= finish_co_yield_expr (input_location
,
17715 RECUR (TREE_OPERAND (t
, 0)));
17719 case CO_AWAIT_EXPR
:
17720 stmt
= finish_co_await_expr (input_location
,
17721 RECUR (TREE_OPERAND (t
, 0)));
17726 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17727 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17728 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17730 finish_expr_stmt (tmp
);
17734 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17739 tree decl
, pattern_decl
;
17742 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17743 if (TREE_CODE (decl
) == LABEL_DECL
)
17744 finish_label_decl (DECL_NAME (decl
));
17745 else if (TREE_CODE (decl
) == USING_DECL
)
17747 tree scope
= USING_DECL_SCOPE (decl
);
17748 tree name
= DECL_NAME (decl
);
17750 scope
= tsubst (scope
, args
, complain
, in_decl
);
17751 finish_nonmember_using_decl (scope
, name
);
17753 else if (is_capture_proxy (decl
)
17754 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17756 /* We're in tsubst_lambda_expr, we've already inserted a new
17757 capture proxy, so look it up and register it. */
17759 if (!DECL_PACK_P (decl
))
17761 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17762 /*nonclass*/1, /*block_p=*/true,
17763 /*ns_only*/0, LOOKUP_HIDDEN
);
17764 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17766 else if (is_normal_capture_proxy (decl
))
17768 inst
= (retrieve_local_specialization
17769 (DECL_CAPTURED_VARIABLE (decl
)));
17770 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
17773 inst
= lookup_init_capture_pack (decl
);
17775 register_local_specialization (inst
, decl
);
17778 else if (DECL_PRETTY_FUNCTION_P (decl
))
17779 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17781 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17782 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17783 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17784 /* Don't copy the old closure; we'll create a new one in
17785 tsubst_lambda_expr. */
17789 init
= DECL_INITIAL (decl
);
17790 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17791 for local variables, so save if DECL was declared constinit. */
17792 const bool constinit_p
17794 && DECL_LANG_SPECIFIC (decl
)
17795 && DECL_TEMPLATE_INFO (decl
)
17796 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17797 decl
= tsubst (decl
, args
, complain
, in_decl
);
17798 if (decl
!= error_mark_node
)
17800 /* By marking the declaration as instantiated, we avoid
17801 trying to instantiate it. Since instantiate_decl can't
17802 handle local variables, and since we've already done
17803 all that needs to be done, that's the right thing to
17806 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17807 if (VAR_P (decl
) && !DECL_NAME (decl
)
17808 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17809 /* Anonymous aggregates are a special case. */
17810 finish_anon_union (decl
);
17811 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17813 DECL_CONTEXT (decl
) = current_function_decl
;
17814 if (DECL_NAME (decl
) == this_identifier
)
17816 tree lam
= DECL_CONTEXT (current_function_decl
);
17817 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17818 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17820 insert_capture_proxy (decl
);
17822 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17823 /* We already did a pushtag. */;
17824 else if (TREE_CODE (decl
) == FUNCTION_DECL
17825 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17826 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17828 DECL_CONTEXT (decl
) = NULL_TREE
;
17830 DECL_CONTEXT (decl
) = current_function_decl
;
17831 cp_check_omp_declare_reduction (decl
);
17835 bool const_init
= false;
17836 unsigned int cnt
= 0;
17837 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17838 maybe_push_decl (decl
);
17841 && DECL_DECOMPOSITION_P (decl
)
17842 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17843 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17844 complain
, in_decl
, &first
,
17847 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17850 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17853 if (ndecl
!= error_mark_node
)
17854 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17856 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17857 constinit_p
? LOOKUP_CONSTINIT
: 0);
17859 if (ndecl
!= error_mark_node
)
17860 cp_finish_decomp (ndecl
, first
, cnt
);
17869 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17870 RECUR (FOR_INIT_STMT (t
));
17871 finish_init_stmt (stmt
);
17872 tmp
= RECUR (FOR_COND (t
));
17873 finish_for_cond (tmp
, stmt
, false, 0);
17874 tmp
= RECUR (FOR_EXPR (t
));
17875 finish_for_expr (tmp
, stmt
);
17877 bool prev
= note_iteration_stmt_body_start ();
17878 RECUR (FOR_BODY (t
));
17879 note_iteration_stmt_body_end (prev
);
17881 finish_for_stmt (stmt
);
17884 case RANGE_FOR_STMT
:
17886 /* Construct another range_for, if this is not a final
17887 substitution (for inside a generic lambda of a
17888 template). Otherwise convert to a regular for. */
17890 stmt
= (processing_template_decl
17891 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17892 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17893 RECUR (RANGE_FOR_INIT_STMT (t
));
17894 decl
= RANGE_FOR_DECL (t
);
17895 decl
= tsubst (decl
, args
, complain
, in_decl
);
17896 maybe_push_decl (decl
);
17897 expr
= RECUR (RANGE_FOR_EXPR (t
));
17899 tree decomp_first
= NULL_TREE
;
17900 unsigned decomp_cnt
= 0;
17901 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17902 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17904 &decomp_first
, &decomp_cnt
);
17906 if (processing_template_decl
)
17908 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17909 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17910 finish_range_for_decl (stmt
, decl
, expr
);
17911 if (decomp_first
&& decl
!= error_mark_node
)
17912 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17916 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17917 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17918 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17919 decomp_first
, decomp_cnt
,
17920 RANGE_FOR_IVDEP (t
), unroll
);
17923 bool prev
= note_iteration_stmt_body_start ();
17924 RECUR (RANGE_FOR_BODY (t
));
17925 note_iteration_stmt_body_end (prev
);
17926 finish_for_stmt (stmt
);
17931 stmt
= begin_while_stmt ();
17932 tmp
= RECUR (WHILE_COND (t
));
17933 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17935 bool prev
= note_iteration_stmt_body_start ();
17936 RECUR (WHILE_BODY (t
));
17937 note_iteration_stmt_body_end (prev
);
17939 finish_while_stmt (stmt
);
17943 stmt
= begin_do_stmt ();
17945 bool prev
= note_iteration_stmt_body_start ();
17946 RECUR (DO_BODY (t
));
17947 note_iteration_stmt_body_end (prev
);
17949 finish_do_body (stmt
);
17950 tmp
= RECUR (DO_COND (t
));
17951 finish_do_stmt (tmp
, stmt
, false, 0);
17955 stmt
= begin_if_stmt ();
17956 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17957 if (IF_STMT_CONSTEXPR_P (t
))
17958 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17959 tmp
= RECUR (IF_COND (t
));
17960 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17961 if (IF_STMT_CONSTEXPR_P (t
)
17962 && instantiation_dependent_expression_p (tmp
))
17964 /* We're partially instantiating a generic lambda, but the condition
17965 of the constexpr if is still dependent. Don't substitute into the
17966 branches now, just remember the template arguments. */
17967 do_poplevel (IF_SCOPE (stmt
));
17968 IF_COND (stmt
) = IF_COND (t
);
17969 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17970 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17971 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17975 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17976 /* Don't instantiate the THEN_CLAUSE. */;
17979 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17980 bool inhibit
= integer_zerop (folded
);
17982 ++c_inhibit_evaluation_warnings
;
17983 RECUR (THEN_CLAUSE (t
));
17985 --c_inhibit_evaluation_warnings
;
17987 finish_then_clause (stmt
);
17989 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17990 /* Don't instantiate the ELSE_CLAUSE. */;
17991 else if (ELSE_CLAUSE (t
))
17993 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17994 bool inhibit
= integer_nonzerop (folded
);
17995 begin_else_clause (stmt
);
17997 ++c_inhibit_evaluation_warnings
;
17998 RECUR (ELSE_CLAUSE (t
));
18000 --c_inhibit_evaluation_warnings
;
18001 finish_else_clause (stmt
);
18004 finish_if_stmt (stmt
);
18008 if (BIND_EXPR_BODY_BLOCK (t
))
18009 stmt
= begin_function_body ();
18011 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18012 ? BCS_TRY_BLOCK
: 0);
18014 RECUR (BIND_EXPR_BODY (t
));
18016 if (BIND_EXPR_BODY_BLOCK (t
))
18017 finish_function_body (stmt
);
18019 finish_compound_stmt (stmt
);
18023 finish_break_stmt ();
18026 case CONTINUE_STMT
:
18027 finish_continue_stmt ();
18031 stmt
= begin_switch_stmt ();
18032 tmp
= RECUR (SWITCH_STMT_COND (t
));
18033 finish_switch_cond (tmp
, stmt
);
18034 RECUR (SWITCH_STMT_BODY (t
));
18035 finish_switch_stmt (stmt
);
18038 case CASE_LABEL_EXPR
:
18040 tree decl
= CASE_LABEL (t
);
18041 tree low
= RECUR (CASE_LOW (t
));
18042 tree high
= RECUR (CASE_HIGH (t
));
18043 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18044 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18046 tree label
= CASE_LABEL (l
);
18047 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18048 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18049 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18056 tree decl
= LABEL_EXPR_LABEL (t
);
18059 label
= finish_label_stmt (DECL_NAME (decl
));
18060 if (TREE_CODE (label
) == LABEL_DECL
)
18061 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18062 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18063 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18068 tmp
= GOTO_DESTINATION (t
);
18069 if (TREE_CODE (tmp
) != LABEL_DECL
)
18070 /* Computed goto's must be tsubst'd into. On the other hand,
18071 non-computed gotos must not be; the identifier in question
18072 will have no binding. */
18075 tmp
= DECL_NAME (tmp
);
18076 finish_goto_stmt (tmp
);
18081 tree string
= RECUR (ASM_STRING (t
));
18082 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18083 complain
, in_decl
);
18084 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18085 complain
, in_decl
);
18086 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18087 complain
, in_decl
);
18088 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18089 complain
, in_decl
);
18090 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18091 outputs
, inputs
, clobbers
, labels
,
18093 tree asm_expr
= tmp
;
18094 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18095 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18096 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18103 stmt
= begin_try_block ();
18104 RECUR (TRY_STMTS (t
));
18105 finish_cleanup_try_block (stmt
);
18106 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18110 tree compound_stmt
= NULL_TREE
;
18112 if (FN_TRY_BLOCK_P (t
))
18113 stmt
= begin_function_try_block (&compound_stmt
);
18115 stmt
= begin_try_block ();
18117 RECUR (TRY_STMTS (t
));
18119 if (FN_TRY_BLOCK_P (t
))
18120 finish_function_try_block (stmt
);
18122 finish_try_block (stmt
);
18124 RECUR (TRY_HANDLERS (t
));
18125 if (FN_TRY_BLOCK_P (t
))
18126 finish_function_handler_sequence (stmt
, compound_stmt
);
18128 finish_handler_sequence (stmt
);
18134 tree decl
= HANDLER_PARMS (t
);
18138 decl
= tsubst (decl
, args
, complain
, in_decl
);
18139 /* Prevent instantiate_decl from trying to instantiate
18140 this variable. We've already done all that needs to be
18142 if (decl
!= error_mark_node
)
18143 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18145 stmt
= begin_handler ();
18146 finish_handler_parms (decl
, stmt
);
18147 RECUR (HANDLER_BODY (t
));
18148 finish_handler (stmt
);
18153 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18154 if (CLASS_TYPE_P (tmp
))
18156 /* Local classes are not independent templates; they are
18157 instantiated along with their containing function. And this
18158 way we don't have to deal with pushing out of one local class
18159 to instantiate a member of another local class. */
18160 /* Closures are handled by the LAMBDA_EXPR. */
18161 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18162 complete_type (tmp
);
18163 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18165 || (TREE_CODE (fld
) == FUNCTION_DECL
18166 && !DECL_ARTIFICIAL (fld
)))
18167 && DECL_TEMPLATE_INSTANTIATION (fld
))
18168 instantiate_decl (fld
, /*defer_ok=*/false,
18169 /*expl_inst_class=*/false);
18173 case STATIC_ASSERT
:
18177 ++c_inhibit_evaluation_warnings
;
18179 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18182 /*integral_constant_expression_p=*/true);
18183 --c_inhibit_evaluation_warnings
;
18185 finish_static_assert (condition
,
18186 STATIC_ASSERT_MESSAGE (t
),
18187 STATIC_ASSERT_SOURCE_LOCATION (t
),
18188 /*member_p=*/false);
18193 case OACC_PARALLEL
:
18195 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18197 stmt
= begin_omp_parallel ();
18198 RECUR (OMP_BODY (t
));
18199 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18203 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18204 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18205 complain
, in_decl
);
18206 if (OMP_PARALLEL_COMBINED (t
))
18207 omp_parallel_combined_clauses
= &tmp
;
18208 stmt
= begin_omp_parallel ();
18209 RECUR (OMP_PARALLEL_BODY (t
));
18210 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18211 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18212 = OMP_PARALLEL_COMBINED (t
);
18213 pop_omp_privatization_clauses (r
);
18217 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18219 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18220 complain
, in_decl
);
18222 OMP_TASK_CLAUSES (t
) = tmp
;
18226 r
= push_omp_privatization_clauses (false);
18227 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18228 complain
, in_decl
);
18229 stmt
= begin_omp_task ();
18230 RECUR (OMP_TASK_BODY (t
));
18231 finish_omp_task (tmp
, stmt
);
18232 pop_omp_privatization_clauses (r
);
18238 case OMP_DISTRIBUTE
:
18242 tree clauses
, body
, pre_body
;
18243 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18244 tree orig_declv
= NULL_TREE
;
18245 tree incrv
= NULL_TREE
;
18246 enum c_omp_region_type ort
= C_ORT_OMP
;
18247 bool any_range_for
= false;
18250 if (TREE_CODE (t
) == OACC_LOOP
)
18253 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18254 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18256 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18258 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18259 if (OMP_FOR_ORIG_DECLS (t
))
18260 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18261 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18262 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18263 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18266 keep_next_level (true);
18267 stmt
= begin_omp_structured_block ();
18269 pre_body
= push_stmt_list ();
18270 RECUR (OMP_FOR_PRE_BODY (t
));
18271 pre_body
= pop_stmt_list (pre_body
);
18273 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18274 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18276 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18277 condv
, incrv
, &clauses
, args
,
18279 integral_constant_expression_p
);
18280 omp_parallel_combined_clauses
= NULL
;
18284 gcc_assert (orig_declv
);
18285 body
= begin_omp_structured_block ();
18286 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18287 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18288 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18289 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18290 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18291 TREE_VEC_ELT (declv
, i
));
18294 body
= push_stmt_list ();
18295 RECUR (OMP_FOR_BODY (t
));
18297 body
= finish_omp_structured_block (body
);
18299 body
= pop_stmt_list (body
);
18301 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18302 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18303 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18307 t
= make_node (TREE_CODE (t
));
18308 TREE_TYPE (t
) = void_type_node
;
18309 OMP_FOR_BODY (t
) = body
;
18310 OMP_FOR_PRE_BODY (t
) = pre_body
;
18311 OMP_FOR_CLAUSES (t
) = clauses
;
18312 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18316 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18318 pop_omp_privatization_clauses (r
);
18323 omp_parallel_combined_clauses
= NULL
;
18328 case OMP_TASKGROUP
:
18330 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18331 && OMP_TEAMS_COMBINED (t
));
18332 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18334 if (TREE_CODE (t
) == OMP_TEAMS
)
18336 keep_next_level (true);
18337 stmt
= begin_omp_structured_block ();
18338 RECUR (OMP_BODY (t
));
18339 stmt
= finish_omp_structured_block (stmt
);
18343 stmt
= push_stmt_list ();
18344 RECUR (OMP_BODY (t
));
18345 stmt
= pop_stmt_list (stmt
);
18349 OMP_BODY (t
) = stmt
;
18350 OMP_CLAUSES (t
) = tmp
;
18352 pop_omp_privatization_clauses (r
);
18356 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18357 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18359 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18360 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18362 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18363 args
, complain
, in_decl
);
18364 if (tmp
== NULL_TREE
)
18365 tmp
= error_mark_node
;
18369 kind
= (enum omp_clause_depend_kind
)
18370 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18373 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18376 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18377 OMP_CLAUSE_DEPEND_SOURCE
,
18378 OMP_DEPOBJ_CLAUSES (t
));
18382 case OMP_TARGET_DATA
:
18384 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18385 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18387 keep_next_level (true);
18388 stmt
= begin_omp_structured_block ();
18390 RECUR (OMP_BODY (t
));
18391 stmt
= finish_omp_structured_block (stmt
);
18394 OMP_BODY (t
) = stmt
;
18395 OMP_CLAUSES (t
) = tmp
;
18396 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18398 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18401 /* For combined target teams, ensure the num_teams and
18402 thread_limit clause expressions are evaluated on the host,
18403 before entering the target construct. */
18405 for (c
= OMP_TEAMS_CLAUSES (teams
);
18406 c
; c
= OMP_CLAUSE_CHAIN (c
))
18407 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18408 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18409 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18411 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18412 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18413 if (expr
== error_mark_node
)
18415 tmp
= TARGET_EXPR_SLOT (expr
);
18417 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18418 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18419 OMP_CLAUSE_FIRSTPRIVATE
);
18420 OMP_CLAUSE_DECL (tc
) = tmp
;
18421 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18422 OMP_TARGET_CLAUSES (t
) = tc
;
18431 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18432 complain
, in_decl
);
18433 OACC_DECLARE_CLAUSES (t
) = tmp
;
18437 case OMP_TARGET_UPDATE
:
18438 case OMP_TARGET_ENTER_DATA
:
18439 case OMP_TARGET_EXIT_DATA
:
18440 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18441 complain
, in_decl
);
18443 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18447 case OACC_ENTER_DATA
:
18448 case OACC_EXIT_DATA
:
18450 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18451 complain
, in_decl
);
18453 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18458 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18459 complain
, in_decl
);
18460 stmt
= push_stmt_list ();
18461 RECUR (OMP_BODY (t
));
18462 stmt
= pop_stmt_list (stmt
);
18465 OMP_BODY (t
) = stmt
;
18466 OMP_ORDERED_CLAUSES (t
) = tmp
;
18472 stmt
= push_stmt_list ();
18473 RECUR (OMP_BODY (t
));
18474 stmt
= pop_stmt_list (stmt
);
18477 OMP_BODY (t
) = stmt
;
18482 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18484 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18485 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18486 complain
, in_decl
);
18487 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18489 tree op1
= TREE_OPERAND (t
, 1);
18490 tree rhs1
= NULL_TREE
;
18492 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18494 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18495 op1
= TREE_OPERAND (op1
, 1);
18497 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18498 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18499 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18500 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18501 OMP_ATOMIC_MEMORY_ORDER (t
));
18505 tree op1
= TREE_OPERAND (t
, 1);
18506 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18507 tree rhs1
= NULL_TREE
;
18508 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18509 enum tree_code opcode
= NOP_EXPR
;
18510 if (code
== OMP_ATOMIC_READ
)
18512 v
= RECUR (TREE_OPERAND (op1
, 0));
18513 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18515 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18516 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18518 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18519 v
= RECUR (TREE_OPERAND (op1
, 0));
18520 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18521 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18523 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18524 op11
= TREE_OPERAND (op11
, 1);
18526 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18527 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18528 opcode
= TREE_CODE (op11
);
18529 if (opcode
== MODIFY_EXPR
)
18535 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18536 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18538 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18539 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18543 case TRANSACTION_EXPR
:
18546 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18547 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18549 if (TRANSACTION_EXPR_IS_STMT (t
))
18551 tree body
= TRANSACTION_EXPR_BODY (t
);
18552 tree noex
= NULL_TREE
;
18553 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18555 noex
= MUST_NOT_THROW_COND (body
);
18556 if (noex
== NULL_TREE
)
18557 noex
= boolean_true_node
;
18558 body
= TREE_OPERAND (body
, 0);
18560 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18562 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18566 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18567 RECUR (TRANSACTION_EXPR_BODY (t
)),
18574 case MUST_NOT_THROW_EXPR
:
18576 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18577 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18578 RETURN (build_must_not_throw_expr (op0
, cond
));
18581 case EXPR_PACK_EXPANSION
:
18582 error ("invalid use of pack expansion expression");
18583 RETURN (error_mark_node
);
18585 case NONTYPE_ARGUMENT_PACK
:
18586 error ("use %<...%> to expand argument pack");
18587 RETURN (error_mark_node
);
18589 case COMPOUND_EXPR
:
18590 tmp
= RECUR (TREE_OPERAND (t
, 0));
18591 if (tmp
== NULL_TREE
)
18592 /* If the first operand was a statement, we're done with it. */
18593 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18594 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18595 RECUR (TREE_OPERAND (t
, 1)),
18598 case ANNOTATE_EXPR
:
18599 tmp
= RECUR (TREE_OPERAND (t
, 0));
18600 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18601 TREE_TYPE (tmp
), tmp
,
18602 RECUR (TREE_OPERAND (t
, 1)),
18603 RECUR (TREE_OPERAND (t
, 2))));
18606 RETURN (add_stmt (copy_node (t
)));
18609 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18611 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18612 /*function_p=*/false,
18613 integral_constant_expression_p
));
18616 RETURN (NULL_TREE
);
18618 input_location
= loc
;
18624 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18625 function. For description of the body see comment above
18626 cp_parser_omp_declare_reduction_exprs. */
18629 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18631 if (t
== NULL_TREE
|| t
== error_mark_node
)
18634 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18636 tree_stmt_iterator tsi
;
18639 memset (stmts
, 0, sizeof stmts
);
18640 for (i
= 0, tsi
= tsi_start (t
);
18641 i
< 7 && !tsi_end_p (tsi
);
18642 i
++, tsi_next (&tsi
))
18643 stmts
[i
] = tsi_stmt (tsi
);
18644 gcc_assert (tsi_end_p (tsi
));
18648 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18649 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18650 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18651 args
, complain
, in_decl
);
18652 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18653 args
, complain
, in_decl
);
18654 DECL_CONTEXT (omp_out
) = current_function_decl
;
18655 DECL_CONTEXT (omp_in
) = current_function_decl
;
18656 keep_next_level (true);
18657 tree block
= begin_omp_structured_block ();
18658 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18659 block
= finish_omp_structured_block (block
);
18660 block
= maybe_cleanup_point_expr_void (block
);
18661 add_decl_expr (omp_out
);
18662 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18663 TREE_NO_WARNING (omp_out
) = 1;
18664 add_decl_expr (omp_in
);
18665 finish_expr_stmt (block
);
18669 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18670 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18671 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18672 args
, complain
, in_decl
);
18673 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18674 args
, complain
, in_decl
);
18675 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18676 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18677 keep_next_level (true);
18678 tree block
= begin_omp_structured_block ();
18679 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18680 block
= finish_omp_structured_block (block
);
18681 block
= maybe_cleanup_point_expr_void (block
);
18682 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18683 add_decl_expr (omp_priv
);
18684 add_decl_expr (omp_orig
);
18685 finish_expr_stmt (block
);
18687 add_decl_expr (omp_orig
);
18691 /* T is a postfix-expression that is not being used in a function
18692 call. Return the substituted version of T. */
18695 tsubst_non_call_postfix_expression (tree t
, tree args
,
18696 tsubst_flags_t complain
,
18699 if (TREE_CODE (t
) == SCOPE_REF
)
18700 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18701 /*done=*/false, /*address_p=*/false);
18703 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18704 /*function_p=*/false,
18705 /*integral_constant_expression_p=*/false);
18710 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18711 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18712 dependent init-capture. */
18715 prepend_one_capture (tree field
, tree init
, tree
&list
,
18716 tsubst_flags_t complain
)
18718 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18720 tree type
= NULL_TREE
;
18723 if (complain
& tf_error
)
18724 error ("empty initializer in lambda init-capture");
18725 init
= error_mark_node
;
18727 else if (TREE_CODE (init
) == TREE_LIST
)
18728 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18730 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18731 TREE_TYPE (field
) = type
;
18732 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18734 list
= tree_cons (field
, init
, list
);
18737 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18738 instantiation context. Instantiating a pack expansion containing a lambda
18739 might result in multiple lambdas all based on the same lambda in the
18743 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18745 tree oldfn
= lambda_function (t
);
18748 tree r
= build_lambda_expr ();
18750 LAMBDA_EXPR_LOCATION (r
)
18751 = LAMBDA_EXPR_LOCATION (t
);
18752 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18753 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18754 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18755 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18757 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18758 /* A lambda in a default argument outside a class gets no
18759 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18760 tsubst_default_argument calls start_lambda_scope, so we need to
18761 specifically ignore it here, and use the global scope. */
18762 record_null_lambda_scope (r
);
18764 record_lambda_scope (r
);
18766 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18767 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18769 vec
<tree
,va_gc
>* field_packs
= NULL
;
18771 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18772 cap
= TREE_CHAIN (cap
))
18774 tree ofield
= TREE_PURPOSE (cap
);
18775 tree init
= TREE_VALUE (cap
);
18776 if (PACK_EXPANSION_P (init
))
18777 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18779 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18780 /*fn*/false, /*constexpr*/false);
18782 if (init
== error_mark_node
)
18783 return error_mark_node
;
18785 if (init
&& TREE_CODE (init
) == TREE_LIST
)
18786 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18788 if (!processing_template_decl
18789 && init
&& TREE_CODE (init
) != TREE_VEC
18790 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
18792 /* For a VLA, simply tsubsting the field type won't work, we need to
18793 go through add_capture again. XXX do we want to do this for all
18795 tree name
= (get_identifier
18796 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
18797 tree ftype
= TREE_TYPE (ofield
);
18798 bool by_ref
= (TYPE_REF_P (ftype
)
18799 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
18800 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
18801 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
18805 if (PACK_EXPANSION_P (ofield
))
18806 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18807 tree field
= tsubst_decl (ofield
, args
, complain
);
18809 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18811 /* Remember these for when we've pushed local_specializations. */
18812 vec_safe_push (field_packs
, ofield
);
18813 vec_safe_push (field_packs
, field
);
18816 if (field
== error_mark_node
)
18817 return error_mark_node
;
18819 if (TREE_CODE (field
) == TREE_VEC
)
18821 int len
= TREE_VEC_LENGTH (field
);
18822 gcc_assert (TREE_CODE (init
) == TREE_VEC
18823 && TREE_VEC_LENGTH (init
) == len
);
18824 for (int i
= 0; i
< len
; ++i
)
18825 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18826 TREE_VEC_ELT (init
, i
),
18827 LAMBDA_EXPR_CAPTURE_LIST (r
),
18832 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18835 if (id_equal (DECL_NAME (field
), "__this"))
18836 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18840 tree type
= begin_lambda_type (r
);
18841 if (type
== error_mark_node
)
18842 return error_mark_node
;
18844 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18845 determine_visibility (TYPE_NAME (type
));
18847 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18849 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18850 ? DECL_TI_TEMPLATE (oldfn
)
18853 tree fntype
= static_fn_type (oldfn
);
18855 ++processing_template_decl
;
18856 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18858 --processing_template_decl
;
18860 if (fntype
== error_mark_node
)
18861 r
= error_mark_node
;
18864 /* The body of a lambda-expression is not a subexpression of the
18865 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18866 which would be skipped if cp_unevaluated_operand. */
18869 /* Fix the type of 'this'. */
18870 fntype
= build_memfn_type (fntype
, type
,
18871 type_memfn_quals (fntype
),
18872 type_memfn_rqual (fntype
));
18876 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18877 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18878 finish_member_declaration (tmpl
);
18883 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18884 finish_member_declaration (fn
);
18887 /* Let finish_function set this. */
18888 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18890 bool nested
= cfun
;
18892 push_function_context ();
18894 /* Still increment function_depth so that we don't GC in the
18895 middle of an expression. */
18898 local_specialization_stack
s (lss_copy
);
18900 tree body
= start_lambda_function (fn
, r
);
18902 /* Now record them for lookup_init_capture_pack. */
18903 int fplen
= vec_safe_length (field_packs
);
18904 for (int i
= 0; i
< fplen
; )
18906 tree pack
= (*field_packs
)[i
++];
18907 tree inst
= (*field_packs
)[i
++];
18908 register_local_specialization (inst
, pack
);
18910 release_tree_vector (field_packs
);
18912 register_parameter_specializations (oldfn
, fn
);
18916 /* We might not partially instantiate some parts of the function, so
18917 copy these flags from the original template. */
18918 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18919 current_function_returns_value
= ol
->returns_value
;
18920 current_function_returns_null
= ol
->returns_null
;
18921 current_function_returns_abnormally
= ol
->returns_abnormally
;
18922 current_function_infinite_loop
= ol
->infinite_loop
;
18925 /* [temp.deduct] A lambda-expression appearing in a function type or a
18926 template parameter is not considered part of the immediate context for
18927 the purposes of template argument deduction. */
18928 complain
= tf_warning_or_error
;
18930 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18931 /*constexpr*/false);
18933 finish_lambda_function (body
);
18936 pop_function_context ();
18940 /* The capture list was built up in reverse order; fix that now. */
18941 LAMBDA_EXPR_CAPTURE_LIST (r
)
18942 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18944 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18946 maybe_add_lambda_conv_op (type
);
18949 finish_struct (type
, /*attr*/NULL_TREE
);
18951 insert_pending_capture_proxies ();
18956 /* Like tsubst but deals with expressions and performs semantic
18957 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18960 tsubst_copy_and_build (tree t
,
18962 tsubst_flags_t complain
,
18965 bool integral_constant_expression_p
)
18967 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18968 #define RECUR(NODE) \
18969 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18970 /*function_p=*/false, \
18971 integral_constant_expression_p)
18974 location_t save_loc
;
18976 if (t
== NULL_TREE
|| t
== error_mark_node
)
18979 save_loc
= input_location
;
18980 if (location_t eloc
= cp_expr_location (t
))
18981 input_location
= eloc
;
18983 /* N3276 decltype magic only applies to calls at the top level or on the
18984 right side of a comma. */
18985 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18986 complain
&= ~tf_decltype
;
18988 switch (TREE_CODE (t
))
18992 /* Fall through. */
18993 case IDENTIFIER_NODE
:
18997 bool non_integral_constant_expression_p
;
18998 const char *error_msg
;
19000 if (IDENTIFIER_CONV_OP_P (t
))
19002 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19003 t
= make_conv_op_name (new_type
);
19006 /* Look up the name. */
19007 decl
= lookup_name (t
);
19009 /* By convention, expressions use ERROR_MARK_NODE to indicate
19010 failure, not NULL_TREE. */
19011 if (decl
== NULL_TREE
)
19012 decl
= error_mark_node
;
19014 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19016 integral_constant_expression_p
,
19017 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19018 &non_integral_constant_expression_p
,
19019 /*template_p=*/false,
19021 /*address_p=*/false,
19022 /*template_arg_p=*/false,
19027 if (!function_p
&& identifier_p (decl
))
19029 if (complain
& tf_error
)
19030 unqualified_name_lookup_error (decl
);
19031 decl
= error_mark_node
;
19036 case TEMPLATE_ID_EXPR
:
19039 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19040 tree targs
= TREE_OPERAND (t
, 1);
19043 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19044 if (targs
== error_mark_node
)
19045 RETURN (error_mark_node
);
19047 if (TREE_CODE (templ
) == SCOPE_REF
)
19049 tree name
= TREE_OPERAND (templ
, 1);
19050 tree tid
= lookup_template_function (name
, targs
);
19051 TREE_OPERAND (templ
, 1) = tid
;
19055 if (concept_definition_p (templ
))
19057 tree check
= build_concept_check (templ
, targs
, complain
);
19058 if (check
== error_mark_node
)
19059 RETURN (error_mark_node
);
19061 tree id
= unpack_concept_check (check
);
19063 /* If we built a function concept check, return the underlying
19064 template-id. So we can evaluate it as a function call. */
19065 if (function_concept_p (TREE_OPERAND (id
, 0)))
19071 if (variable_template_p (templ
))
19073 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19075 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19079 if (TREE_CODE (templ
) == COMPONENT_REF
)
19081 object
= TREE_OPERAND (templ
, 0);
19082 templ
= TREE_OPERAND (templ
, 1);
19085 object
= NULL_TREE
;
19086 templ
= lookup_template_function (templ
, targs
);
19089 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19090 object
, templ
, NULL_TREE
));
19092 RETURN (baselink_for_fns (templ
));
19097 tree r
= RECUR (TREE_OPERAND (t
, 0));
19099 if (REFERENCE_REF_P (t
))
19101 /* A type conversion to reference type will be enclosed in
19102 such an indirect ref, but the substitution of the cast
19103 will have also added such an indirect ref. */
19104 r
= convert_from_reference (r
);
19107 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19108 complain
|decltype_flag
);
19110 if (REF_PARENTHESIZED_P (t
))
19111 r
= force_paren_expr (r
);
19118 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19119 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19120 RETURN (build_nop (type
, op0
));
19123 case IMPLICIT_CONV_EXPR
:
19125 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19126 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19127 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19129 retval
= copy_node (t
);
19130 TREE_TYPE (retval
) = type
;
19131 TREE_OPERAND (retval
, 0) = expr
;
19134 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19135 /* We'll pass this to convert_nontype_argument again, we don't need
19136 to actually perform any conversion here. */
19138 int flags
= LOOKUP_IMPLICIT
;
19139 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19140 flags
= LOOKUP_NORMAL
;
19141 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19142 flags
|= LOOKUP_NO_NARROWING
;
19143 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19149 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19150 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19151 if (op0
== error_mark_node
)
19152 RETURN (error_mark_node
);
19153 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19157 case REINTERPRET_CAST_EXPR
:
19158 case CONST_CAST_EXPR
:
19159 case DYNAMIC_CAST_EXPR
:
19160 case STATIC_CAST_EXPR
:
19163 tree op
, r
= NULL_TREE
;
19165 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19166 if (integral_constant_expression_p
19167 && !cast_valid_in_integral_constant_expression_p (type
))
19169 if (complain
& tf_error
)
19170 error ("a cast to a type other than an integral or "
19171 "enumeration type cannot appear in a constant-expression");
19172 RETURN (error_mark_node
);
19175 op
= RECUR (TREE_OPERAND (t
, 0));
19177 warning_sentinel
s(warn_useless_cast
);
19178 warning_sentinel
s2(warn_ignored_qualifiers
);
19179 switch (TREE_CODE (t
))
19182 r
= build_functional_cast (input_location
, type
, op
, complain
);
19184 case REINTERPRET_CAST_EXPR
:
19185 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19187 case CONST_CAST_EXPR
:
19188 r
= build_const_cast (input_location
, type
, op
, complain
);
19190 case DYNAMIC_CAST_EXPR
:
19191 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19193 case STATIC_CAST_EXPR
:
19194 r
= build_static_cast (input_location
, type
, op
, complain
);
19197 gcc_unreachable ();
19203 case POSTDECREMENT_EXPR
:
19204 case POSTINCREMENT_EXPR
:
19205 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19206 args
, complain
, in_decl
);
19207 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19208 complain
|decltype_flag
));
19210 case PREDECREMENT_EXPR
:
19211 case PREINCREMENT_EXPR
:
19215 case TRUTH_NOT_EXPR
:
19216 case UNARY_PLUS_EXPR
: /* Unary + */
19217 case REALPART_EXPR
:
19218 case IMAGPART_EXPR
:
19219 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19220 RECUR (TREE_OPERAND (t
, 0)),
19221 complain
|decltype_flag
));
19223 case FIX_TRUNC_EXPR
:
19224 gcc_unreachable ();
19227 op1
= TREE_OPERAND (t
, 0);
19228 if (TREE_CODE (op1
) == LABEL_DECL
)
19229 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19230 EXPR_LOCATION (op1
)));
19231 if (TREE_CODE (op1
) == SCOPE_REF
)
19232 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19233 /*done=*/true, /*address_p=*/true);
19235 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19237 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19238 complain
|decltype_flag
));
19243 case TRUNC_DIV_EXPR
:
19244 case CEIL_DIV_EXPR
:
19245 case FLOOR_DIV_EXPR
:
19246 case ROUND_DIV_EXPR
:
19247 case EXACT_DIV_EXPR
:
19251 case TRUNC_MOD_EXPR
:
19252 case FLOOR_MOD_EXPR
:
19253 case TRUTH_ANDIF_EXPR
:
19254 case TRUTH_ORIF_EXPR
:
19255 case TRUTH_AND_EXPR
:
19256 case TRUTH_OR_EXPR
:
19267 case SPACESHIP_EXPR
:
19271 /* If T was type-dependent, suppress warnings that depend on the range
19272 of the types involved. */
19273 bool was_dep
= uses_template_parms (t
);
19274 warning_sentinel
s1(warn_type_limits
, was_dep
);
19275 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19276 warning_sentinel
s3(warn_logical_op
, was_dep
);
19277 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19279 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19280 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19281 tree r
= build_x_binary_op
19282 (input_location
, TREE_CODE (t
),
19284 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19286 : TREE_CODE (TREE_OPERAND (t
, 0))),
19288 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19290 : TREE_CODE (TREE_OPERAND (t
, 1))),
19292 complain
|decltype_flag
);
19293 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19294 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19299 case POINTER_PLUS_EXPR
:
19301 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19302 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19303 RETURN (fold_build_pointer_plus (op0
, op1
));
19307 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19308 /*address_p=*/false));
19310 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19311 args
, complain
, in_decl
);
19312 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19313 RECUR (TREE_OPERAND (t
, 1)),
19314 complain
|decltype_flag
));
19317 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19318 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19319 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19326 op1
= TREE_OPERAND (t
, 0);
19327 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19328 op1
= TREE_TYPE (op1
);
19329 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19330 && ALIGNOF_EXPR_STD_P (t
));
19333 /* When there are no ARGS, we are trying to evaluate a
19334 non-dependent expression from the parser. Trying to do
19335 the substitutions may not work. */
19337 op1
= TREE_TYPE (op1
);
19341 ++cp_unevaluated_operand
;
19342 ++c_inhibit_evaluation_warnings
;
19344 op1
= tsubst (op1
, args
, complain
, in_decl
);
19346 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19347 /*function_p=*/false,
19348 /*integral_constant_expression_p=*/
19350 --cp_unevaluated_operand
;
19351 --c_inhibit_evaluation_warnings
;
19354 r
= cxx_sizeof_or_alignof_type (input_location
,
19355 op1
, TREE_CODE (t
), std_alignof
,
19356 complain
& tf_error
);
19358 r
= cxx_sizeof_or_alignof_expr (input_location
,
19359 op1
, TREE_CODE (t
),
19360 complain
& tf_error
);
19361 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19363 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19365 if (!processing_template_decl
&& TYPE_P (op1
))
19367 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19368 build1 (NOP_EXPR
, op1
, error_mark_node
));
19369 SIZEOF_EXPR_TYPE_P (r
) = 1;
19372 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19373 TREE_SIDE_EFFECTS (r
) = 0;
19374 TREE_READONLY (r
) = 1;
19376 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19381 case AT_ENCODE_EXPR
:
19383 op1
= TREE_OPERAND (t
, 0);
19384 ++cp_unevaluated_operand
;
19385 ++c_inhibit_evaluation_warnings
;
19386 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19387 /*function_p=*/false,
19388 /*integral_constant_expression_p=*/false);
19389 --cp_unevaluated_operand
;
19390 --c_inhibit_evaluation_warnings
;
19391 RETURN (objc_build_encode_expr (op1
));
19394 case NOEXCEPT_EXPR
:
19395 op1
= TREE_OPERAND (t
, 0);
19396 ++cp_unevaluated_operand
;
19397 ++c_inhibit_evaluation_warnings
;
19398 ++cp_noexcept_operand
;
19399 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19400 /*function_p=*/false,
19401 /*integral_constant_expression_p=*/false);
19402 --cp_unevaluated_operand
;
19403 --c_inhibit_evaluation_warnings
;
19404 --cp_noexcept_operand
;
19405 RETURN (finish_noexcept_expr (op1
, complain
));
19409 warning_sentinel
s(warn_div_by_zero
);
19410 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19411 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19412 tree r
= build_x_modify_expr
19413 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19414 complain
|decltype_flag
);
19415 /* TREE_NO_WARNING must be set if either the expression was
19416 parenthesized or it uses an operator such as >>= rather
19417 than plain assignment. In the former case, it was already
19418 set and must be copied. In the latter case,
19419 build_x_modify_expr sets it and it must not be reset
19421 if (TREE_NO_WARNING (t
))
19422 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19428 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19429 args
, complain
, in_decl
);
19430 /* Remember that there was a reference to this entity. */
19432 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19433 RETURN (error_mark_node
);
19434 RETURN (build_x_arrow (input_location
, op1
, complain
));
19438 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19439 tree init
= RECUR (TREE_OPERAND (t
, 3));
19440 vec
<tree
, va_gc
> *placement_vec
;
19441 vec
<tree
, va_gc
> *init_vec
;
19443 location_t loc
= EXPR_LOCATION (t
);
19445 if (placement
== NULL_TREE
)
19446 placement_vec
= NULL
;
19449 placement_vec
= make_tree_vector ();
19450 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19451 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19454 /* If there was an initializer in the original tree, but it
19455 instantiated to an empty list, then we should pass a
19456 non-NULL empty vector to tell build_new that it was an
19457 empty initializer() rather than no initializer. This can
19458 only happen when the initializer is a pack expansion whose
19459 parameter packs are of length zero. */
19460 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19464 init_vec
= make_tree_vector ();
19465 if (init
== void_node
)
19466 gcc_assert (init_vec
!= NULL
);
19469 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19470 vec_safe_push (init_vec
, TREE_VALUE (init
));
19474 /* Avoid passing an enclosing decl to valid_array_size_p. */
19475 in_decl
= NULL_TREE
;
19477 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19478 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19479 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19480 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19483 if (placement_vec
!= NULL
)
19484 release_tree_vector (placement_vec
);
19485 if (init_vec
!= NULL
)
19486 release_tree_vector (init_vec
);
19493 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19494 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19495 RETURN (delete_sanity (input_location
, op0
, op1
,
19496 DELETE_EXPR_USE_VEC (t
),
19497 DELETE_EXPR_USE_GLOBAL (t
),
19501 case COMPOUND_EXPR
:
19503 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19504 complain
& ~tf_decltype
, in_decl
,
19505 /*function_p=*/false,
19506 integral_constant_expression_p
);
19507 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19509 RECUR (TREE_OPERAND (t
, 1)),
19510 complain
|decltype_flag
));
19516 unsigned int nargs
, i
;
19521 function
= CALL_EXPR_FN (t
);
19522 /* Internal function with no arguments. */
19523 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19526 /* When we parsed the expression, we determined whether or
19527 not Koenig lookup should be performed. */
19528 koenig_p
= KOENIG_LOOKUP_P (t
);
19529 if (function
== NULL_TREE
)
19532 qualified_p
= false;
19534 else if (TREE_CODE (function
) == SCOPE_REF
)
19536 qualified_p
= true;
19537 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19539 /*address_p=*/false);
19541 else if (koenig_p
&& identifier_p (function
))
19543 /* Do nothing; calling tsubst_copy_and_build on an identifier
19544 would incorrectly perform unqualified lookup again.
19546 Note that we can also have an IDENTIFIER_NODE if the earlier
19547 unqualified lookup found a member function; in that case
19548 koenig_p will be false and we do want to do the lookup
19549 again to find the instantiated member function.
19551 FIXME but doing that causes c++/15272, so we need to stop
19552 using IDENTIFIER_NODE in that situation. */
19553 qualified_p
= false;
19557 if (TREE_CODE (function
) == COMPONENT_REF
)
19559 tree op
= TREE_OPERAND (function
, 1);
19561 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19562 || (BASELINK_P (op
)
19563 && BASELINK_QUALIFIED_P (op
)));
19566 qualified_p
= false;
19568 if (TREE_CODE (function
) == ADDR_EXPR
19569 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19570 /* Avoid error about taking the address of a constructor. */
19571 function
= TREE_OPERAND (function
, 0);
19573 function
= tsubst_copy_and_build (function
, args
, complain
,
19576 integral_constant_expression_p
);
19578 if (BASELINK_P (function
))
19579 qualified_p
= true;
19582 nargs
= call_expr_nargs (t
);
19583 releasing_vec call_args
;
19584 for (i
= 0; i
< nargs
; ++i
)
19586 tree arg
= CALL_EXPR_ARG (t
, i
);
19588 if (!PACK_EXPANSION_P (arg
))
19589 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19592 /* Expand the pack expansion and push each entry onto
19594 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19595 if (TREE_CODE (arg
) == TREE_VEC
)
19597 unsigned int len
, j
;
19599 len
= TREE_VEC_LENGTH (arg
);
19600 for (j
= 0; j
< len
; ++j
)
19602 tree value
= TREE_VEC_ELT (arg
, j
);
19603 if (value
!= NULL_TREE
)
19604 value
= convert_from_reference (value
);
19605 vec_safe_push (call_args
, value
);
19610 /* A partial substitution. Add one entry. */
19611 vec_safe_push (call_args
, arg
);
19616 /* Stripped-down processing for a call in a thunk. Specifically, in
19617 the thunk template for a generic lambda. */
19618 if (CALL_FROM_THUNK_P (t
))
19620 /* Now that we've expanded any packs, the number of call args
19621 might be different. */
19622 unsigned int cargs
= call_args
->length ();
19623 tree thisarg
= NULL_TREE
;
19624 if (TREE_CODE (function
) == COMPONENT_REF
)
19626 thisarg
= TREE_OPERAND (function
, 0);
19627 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19628 thisarg
= TREE_OPERAND (thisarg
, 0);
19629 function
= TREE_OPERAND (function
, 1);
19630 if (TREE_CODE (function
) == BASELINK
)
19631 function
= BASELINK_FUNCTIONS (function
);
19633 /* We aren't going to do normal overload resolution, so force the
19634 template-id to resolve. */
19635 function
= resolve_nondeduced_context (function
, complain
);
19636 for (unsigned i
= 0; i
< cargs
; ++i
)
19638 /* In a thunk, pass through args directly, without any
19640 tree arg
= (*call_args
)[i
];
19641 while (TREE_CODE (arg
) != PARM_DECL
)
19642 arg
= TREE_OPERAND (arg
, 0);
19643 (*call_args
)[i
] = arg
;
19647 /* If there are no other args, just push 'this'. */
19649 vec_safe_push (call_args
, thisarg
);
19652 /* Otherwise, shift the other args over to make room. */
19653 tree last
= (*call_args
)[cargs
- 1];
19654 vec_safe_push (call_args
, last
);
19655 for (int i
= cargs
- 1; i
> 0; --i
)
19656 (*call_args
)[i
] = (*call_args
)[i
- 1];
19657 (*call_args
)[0] = thisarg
;
19660 ret
= build_call_a (function
, call_args
->length (),
19661 call_args
->address ());
19662 /* The thunk location is not interesting. */
19663 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19664 CALL_FROM_THUNK_P (ret
) = true;
19665 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19666 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19671 /* We do not perform argument-dependent lookup if normal
19672 lookup finds a non-function, in accordance with the
19673 expected resolution of DR 218. */
19675 && ((is_overloaded_fn (function
)
19676 /* If lookup found a member function, the Koenig lookup is
19677 not appropriate, even if an unqualified-name was used
19678 to denote the function. */
19679 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19680 || identifier_p (function
))
19681 /* Only do this when substitution turns a dependent call
19682 into a non-dependent call. */
19683 && type_dependent_expression_p_push (t
)
19684 && !any_type_dependent_arguments_p (call_args
))
19685 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19687 if (function
!= NULL_TREE
19688 && identifier_p (function
)
19689 && !any_type_dependent_arguments_p (call_args
))
19691 if (koenig_p
&& (complain
& tf_warning_or_error
))
19693 /* For backwards compatibility and good diagnostics, try
19694 the unqualified lookup again if we aren't in SFINAE
19696 tree unq
= (tsubst_copy_and_build
19697 (function
, args
, complain
, in_decl
, true,
19698 integral_constant_expression_p
));
19699 if (unq
== error_mark_node
)
19700 RETURN (error_mark_node
);
19702 if (unq
!= function
)
19704 /* In a lambda fn, we have to be careful to not
19705 introduce new this captures. Legacy code can't
19706 be using lambdas anyway, so it's ok to be
19708 bool in_lambda
= (current_class_type
19709 && LAMBDA_TYPE_P (current_class_type
));
19710 char const *const msg
19711 = G_("%qD was not declared in this scope, "
19712 "and no declarations were found by "
19713 "argument-dependent lookup at the point "
19714 "of instantiation");
19718 error_at (cp_expr_loc_or_input_loc (t
),
19721 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19727 if (INDIRECT_REF_P (fn
))
19728 fn
= TREE_OPERAND (fn
, 0);
19729 if (is_overloaded_fn (fn
))
19730 fn
= get_first_fn (fn
);
19733 /* Can't say anything more. */;
19734 else if (DECL_CLASS_SCOPE_P (fn
))
19736 location_t loc
= cp_expr_loc_or_input_loc (t
);
19738 "declarations in dependent base %qT are "
19739 "not found by unqualified lookup",
19740 DECL_CLASS_CONTEXT (fn
));
19741 if (current_class_ptr
)
19743 "use %<this->%D%> instead", function
);
19746 "use %<%T::%D%> instead",
19747 current_class_name
, function
);
19750 inform (DECL_SOURCE_LOCATION (fn
),
19751 "%qD declared here, later in the "
19752 "translation unit", fn
);
19754 RETURN (error_mark_node
);
19760 if (identifier_p (function
))
19762 if (complain
& tf_error
)
19763 unqualified_name_lookup_error (function
);
19764 RETURN (error_mark_node
);
19768 /* Remember that there was a reference to this entity. */
19769 if (function
!= NULL_TREE
19770 && DECL_P (function
)
19771 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19772 RETURN (error_mark_node
);
19774 /* Put back tf_decltype for the actual call. */
19775 complain
|= decltype_flag
;
19777 if (function
== NULL_TREE
)
19778 switch (CALL_EXPR_IFN (t
))
19781 gcc_assert (nargs
== 1);
19782 if (vec_safe_length (call_args
) != 1)
19784 error_at (cp_expr_loc_or_input_loc (t
),
19785 "wrong number of arguments to "
19786 "%<__builtin_launder%>");
19787 ret
= error_mark_node
;
19790 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19791 (*call_args
)[0], complain
);
19794 case IFN_VEC_CONVERT
:
19795 gcc_assert (nargs
== 1);
19796 if (vec_safe_length (call_args
) != 1)
19798 error_at (cp_expr_loc_or_input_loc (t
),
19799 "wrong number of arguments to "
19800 "%<__builtin_convertvector%>");
19801 ret
= error_mark_node
;
19804 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19805 tsubst (TREE_TYPE (t
), args
,
19806 complain
, in_decl
),
19808 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19813 /* Unsupported internal function with arguments. */
19814 gcc_unreachable ();
19816 else if (TREE_CODE (function
) == OFFSET_REF
19817 || TREE_CODE (function
) == DOTSTAR_EXPR
19818 || TREE_CODE (function
) == MEMBER_REF
)
19819 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19821 else if (TREE_CODE (function
) == COMPONENT_REF
)
19823 tree instance
= TREE_OPERAND (function
, 0);
19824 tree fn
= TREE_OPERAND (function
, 1);
19826 if (processing_template_decl
19827 && (type_dependent_expression_p (instance
)
19828 || (!BASELINK_P (fn
)
19829 && TREE_CODE (fn
) != FIELD_DECL
)
19830 || type_dependent_expression_p (fn
)
19831 || any_type_dependent_arguments_p (call_args
)))
19832 ret
= build_min_nt_call_vec (function
, call_args
);
19833 else if (!BASELINK_P (fn
))
19834 ret
= finish_call_expr (function
, &call_args
,
19835 /*disallow_virtual=*/false,
19836 /*koenig_p=*/false,
19839 ret
= (build_new_method_call
19841 &call_args
, NULL_TREE
,
19842 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19846 else if (concept_check_p (function
))
19848 /* FUNCTION is a template-id referring to a concept definition. */
19849 tree id
= unpack_concept_check (function
);
19850 tree tmpl
= TREE_OPERAND (id
, 0);
19851 tree args
= TREE_OPERAND (id
, 1);
19853 /* Calls to standard and variable concepts should have been
19854 previously diagnosed. */
19855 gcc_assert (function_concept_p (tmpl
));
19857 /* Ensure the result is wrapped as a call expression. */
19858 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
19861 ret
= finish_call_expr (function
, &call_args
,
19862 /*disallow_virtual=*/qualified_p
,
19866 if (ret
!= error_mark_node
)
19868 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
19869 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
19870 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
19871 if (op
|| ord
|| rev
)
19873 function
= extract_call_expr (ret
);
19874 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
19875 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
19876 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
19885 tree cond
= RECUR (TREE_OPERAND (t
, 0));
19886 cond
= mark_rvalue_use (cond
);
19887 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19890 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19892 if (integer_zerop (folded_cond
))
19894 ++c_inhibit_evaluation_warnings
;
19895 exp1
= RECUR (TREE_OPERAND (t
, 1));
19896 --c_inhibit_evaluation_warnings
;
19897 exp2
= RECUR (TREE_OPERAND (t
, 2));
19901 exp1
= RECUR (TREE_OPERAND (t
, 1));
19902 ++c_inhibit_evaluation_warnings
;
19903 exp2
= RECUR (TREE_OPERAND (t
, 2));
19904 --c_inhibit_evaluation_warnings
;
19906 cond
= folded_cond
;
19910 exp1
= RECUR (TREE_OPERAND (t
, 1));
19911 exp2
= RECUR (TREE_OPERAND (t
, 2));
19914 warning_sentinel
s(warn_duplicated_branches
);
19915 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19916 cond
, exp1
, exp2
, complain
));
19919 case PSEUDO_DTOR_EXPR
:
19921 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19922 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19923 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19924 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19930 tree purpose
, value
, chain
;
19932 if (t
== void_list_node
)
19935 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19936 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19938 /* We have pack expansions, so expand those and
19939 create a new list out of it. */
19940 tree purposevec
= NULL_TREE
;
19941 tree valuevec
= NULL_TREE
;
19945 /* Expand the argument expressions. */
19946 if (TREE_PURPOSE (t
))
19947 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19948 complain
, in_decl
);
19949 if (TREE_VALUE (t
))
19950 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19951 complain
, in_decl
);
19953 /* Build the rest of the list. */
19954 chain
= TREE_CHAIN (t
);
19955 if (chain
&& chain
!= void_type_node
)
19956 chain
= RECUR (chain
);
19958 /* Determine the number of arguments. */
19959 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19961 len
= TREE_VEC_LENGTH (purposevec
);
19962 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19964 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19965 len
= TREE_VEC_LENGTH (valuevec
);
19968 /* Since we only performed a partial substitution into
19969 the argument pack, we only RETURN (a single list
19971 if (purposevec
== TREE_PURPOSE (t
)
19972 && valuevec
== TREE_VALUE (t
)
19973 && chain
== TREE_CHAIN (t
))
19976 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19979 /* Convert the argument vectors into a TREE_LIST */
19983 /* Grab the Ith values. */
19985 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19988 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19991 /* Build the list (backwards). */
19992 chain
= tree_cons (purpose
, value
, chain
);
19998 purpose
= TREE_PURPOSE (t
);
20000 purpose
= RECUR (purpose
);
20001 value
= TREE_VALUE (t
);
20003 value
= RECUR (value
);
20004 chain
= TREE_CHAIN (t
);
20005 if (chain
&& chain
!= void_type_node
)
20006 chain
= RECUR (chain
);
20007 if (purpose
== TREE_PURPOSE (t
)
20008 && value
== TREE_VALUE (t
)
20009 && chain
== TREE_CHAIN (t
))
20011 RETURN (tree_cons (purpose
, value
, chain
));
20014 case COMPONENT_REF
:
20021 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20022 args
, complain
, in_decl
);
20023 /* Remember that there was a reference to this entity. */
20024 if (DECL_P (object
)
20025 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20026 RETURN (error_mark_node
);
20027 object_type
= TREE_TYPE (object
);
20029 member
= TREE_OPERAND (t
, 1);
20030 if (BASELINK_P (member
))
20031 member
= tsubst_baselink (member
,
20032 non_reference (TREE_TYPE (object
)),
20033 args
, complain
, in_decl
);
20035 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20036 if (member
== error_mark_node
)
20037 RETURN (error_mark_node
);
20039 if (TREE_CODE (member
) == FIELD_DECL
)
20041 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20042 if (TREE_CODE (r
) == COMPONENT_REF
)
20043 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20046 else if (type_dependent_expression_p (object
))
20047 /* We can't do much here. */;
20048 else if (!CLASS_TYPE_P (object_type
))
20050 if (scalarish_type_p (object_type
))
20052 tree s
= NULL_TREE
;
20053 tree dtor
= member
;
20055 if (TREE_CODE (dtor
) == SCOPE_REF
)
20057 s
= TREE_OPERAND (dtor
, 0);
20058 dtor
= TREE_OPERAND (dtor
, 1);
20060 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20062 dtor
= TREE_OPERAND (dtor
, 0);
20064 RETURN (finish_pseudo_destructor_expr
20065 (object
, s
, dtor
, input_location
));
20069 else if (TREE_CODE (member
) == SCOPE_REF
20070 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20072 /* Lookup the template functions now that we know what the
20074 tree scope
= TREE_OPERAND (member
, 0);
20075 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20076 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20077 member
= lookup_qualified_name (scope
, tmpl
,
20078 /*is_type_p=*/false,
20079 /*complain=*/false);
20080 if (BASELINK_P (member
))
20082 BASELINK_FUNCTIONS (member
)
20083 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20085 member
= (adjust_result_of_qualified_name_lookup
20086 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20091 qualified_name_lookup_error (scope
, tmpl
, member
,
20093 RETURN (error_mark_node
);
20096 else if (TREE_CODE (member
) == SCOPE_REF
20097 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20098 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20100 if (complain
& tf_error
)
20102 if (TYPE_P (TREE_OPERAND (member
, 0)))
20103 error ("%qT is not a class or namespace",
20104 TREE_OPERAND (member
, 0));
20106 error ("%qD is not a class or namespace",
20107 TREE_OPERAND (member
, 0));
20109 RETURN (error_mark_node
);
20112 r
= finish_class_member_access_expr (object
, member
,
20113 /*template_p=*/false,
20115 if (TREE_CODE (r
) == COMPONENT_REF
)
20116 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20121 RETURN (build_throw
20122 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20126 vec
<constructor_elt
, va_gc
> *n
;
20127 constructor_elt
*ce
;
20128 unsigned HOST_WIDE_INT idx
;
20129 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20130 bool process_index_p
;
20132 bool need_copy_p
= false;
20135 if (type
== error_mark_node
)
20136 RETURN (error_mark_node
);
20138 /* We do not want to process the index of aggregate
20139 initializers as they are identifier nodes which will be
20140 looked up by digest_init. */
20141 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20143 if (null_member_pointer_value_p (t
))
20145 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20149 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20150 newlen
= vec_safe_length (n
);
20151 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20153 if (ce
->index
&& process_index_p
20154 /* An identifier index is looked up in the type
20155 being initialized, not the current scope. */
20156 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20157 ce
->index
= RECUR (ce
->index
);
20159 if (PACK_EXPANSION_P (ce
->value
))
20161 /* Substitute into the pack expansion. */
20162 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20165 if (ce
->value
== error_mark_node
20166 || PACK_EXPANSION_P (ce
->value
))
20168 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20169 /* Just move the argument into place. */
20170 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20173 /* Update the length of the final CONSTRUCTOR
20174 arguments vector, and note that we will need to
20176 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20177 need_copy_p
= true;
20181 ce
->value
= RECUR (ce
->value
);
20186 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20188 vec_alloc (n
, newlen
);
20189 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20191 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20193 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20194 for (i
= 0; i
< len
; ++i
)
20195 CONSTRUCTOR_APPEND_ELT (n
, 0,
20196 TREE_VEC_ELT (ce
->value
, i
));
20199 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20203 r
= build_constructor (init_list_type_node
, n
);
20204 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20205 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20206 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20208 if (TREE_HAS_CONSTRUCTOR (t
))
20210 fcl_t cl
= fcl_functional
;
20211 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20213 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20216 TREE_TYPE (r
) = type
;
20222 tree operand_0
= TREE_OPERAND (t
, 0);
20223 if (TYPE_P (operand_0
))
20225 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20226 RETURN (get_typeid (operand_0
, complain
));
20230 operand_0
= RECUR (operand_0
);
20231 RETURN (build_typeid (operand_0
, complain
));
20242 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20243 /* ??? We're doing a subset of finish_id_expression here. */
20244 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20245 /* Replace an evaluated use of the thread_local variable with
20246 a call to its wrapper. */
20248 else if (outer_automatic_var_p (r
))
20249 r
= process_outer_var_ref (r
, complain
);
20251 if (!TYPE_REF_P (TREE_TYPE (t
)))
20252 /* If the original type was a reference, we'll be wrapped in
20253 the appropriate INDIRECT_REF. */
20254 r
= convert_from_reference (r
);
20260 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20261 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20262 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20265 case OFFSETOF_EXPR
:
20268 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20269 in_decl
, /*function_p=*/false,
20270 /*integral_constant_expression_p=*/false);
20271 RETURN (finish_offsetof (object_ptr
,
20272 RECUR (TREE_OPERAND (t
, 0)),
20273 EXPR_LOCATION (t
)));
20276 case ADDRESSOF_EXPR
:
20277 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20278 RECUR (TREE_OPERAND (t
, 0)), complain
));
20282 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20283 complain
, in_decl
);
20284 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20285 complain
, in_decl
);
20286 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20287 TRAIT_EXPR_KIND (t
), type1
, type2
));
20292 tree old_stmt_expr
= cur_stmt_expr
;
20293 tree stmt_expr
= begin_stmt_expr ();
20295 cur_stmt_expr
= stmt_expr
;
20296 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20297 integral_constant_expression_p
);
20298 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20299 cur_stmt_expr
= old_stmt_expr
;
20301 /* If the resulting list of expression statement is empty,
20302 fold it further into void_node. */
20303 if (empty_expr_stmt_p (stmt_expr
))
20304 stmt_expr
= void_node
;
20306 RETURN (stmt_expr
);
20311 if (complain
& tf_partial
)
20313 /* We don't have a full set of template arguments yet; don't touch
20314 the lambda at all. */
20315 gcc_assert (processing_template_decl
);
20318 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20320 RETURN (build_lambda_object (r
));
20324 /* We can get here for a constant initializer of non-dependent type.
20325 FIXME stop folding in cp_parser_initializer_clause. */
20327 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20332 case TRANSACTION_EXPR
:
20333 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20334 integral_constant_expression_p
));
20337 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20339 case VEC_PERM_EXPR
:
20341 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20342 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20343 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20344 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20348 case REQUIRES_EXPR
:
20350 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20355 /* No need to substitute further, a RANGE_EXPR will always be built
20356 with constant operands. */
20359 case NON_LVALUE_EXPR
:
20360 case VIEW_CONVERT_EXPR
:
20361 if (location_wrapper_p (t
))
20362 /* We need to do this here as well as in tsubst_copy so we get the
20363 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20364 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20365 EXPR_LOCATION (t
)));
20369 /* Handle Objective-C++ constructs, if appropriate. */
20372 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20373 in_decl
, /*function_p=*/false);
20377 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20383 input_location
= save_loc
;
20387 /* Verify that the instantiated ARGS are valid. For type arguments,
20388 make sure that the type's linkage is ok. For non-type arguments,
20389 make sure they are constants if they are integral or enumerations.
20390 Emit an error under control of COMPLAIN, and return TRUE on error. */
20393 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20395 if (dependent_template_arg_p (t
))
20397 if (ARGUMENT_PACK_P (t
))
20399 tree vec
= ARGUMENT_PACK_ARGS (t
);
20400 int len
= TREE_VEC_LENGTH (vec
);
20401 bool result
= false;
20404 for (i
= 0; i
< len
; ++i
)
20405 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20409 else if (TYPE_P (t
))
20411 /* [basic.link]: A name with no linkage (notably, the name
20412 of a class or enumeration declared in a local scope)
20413 shall not be used to declare an entity with linkage.
20414 This implies that names with no linkage cannot be used as
20417 DR 757 relaxes this restriction for C++0x. */
20418 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20419 : no_linkage_check (t
, /*relaxed_p=*/false));
20423 /* DR 488 makes use of a type with no linkage cause
20424 type deduction to fail. */
20425 if (complain
& tf_error
)
20427 if (TYPE_UNNAMED_P (nt
))
20428 error ("%qT is/uses unnamed type", t
);
20430 error ("template argument for %qD uses local type %qT",
20435 /* In order to avoid all sorts of complications, we do not
20436 allow variably-modified types as template arguments. */
20437 else if (variably_modified_type_p (t
, NULL_TREE
))
20439 if (complain
& tf_error
)
20440 error ("%qT is a variably modified type", t
);
20444 /* Class template and alias template arguments should be OK. */
20445 else if (DECL_TYPE_TEMPLATE_P (t
))
20447 /* A non-type argument of integral or enumerated type must be a
20449 else if (TREE_TYPE (t
)
20450 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20451 && !REFERENCE_REF_P (t
)
20452 && !TREE_CONSTANT (t
))
20454 if (complain
& tf_error
)
20455 error ("integral expression %qE is not constant", t
);
20462 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20464 int ix
, len
= DECL_NTPARMS (tmpl
);
20465 bool result
= false;
20467 for (ix
= 0; ix
!= len
; ix
++)
20469 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20472 if (result
&& (complain
& tf_error
))
20473 error (" trying to instantiate %qD", tmpl
);
20477 /* We're out of SFINAE context now, so generate diagnostics for the access
20478 errors we saw earlier when instantiating D from TMPL and ARGS. */
20481 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20483 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20484 tree type
= TREE_TYPE (pattern
);
20485 location_t loc
= input_location
;
20487 push_access_scope (d
);
20488 push_deferring_access_checks (dk_no_deferred
);
20489 input_location
= DECL_SOURCE_LOCATION (pattern
);
20490 tsubst (type
, args
, tf_warning_or_error
, d
);
20491 input_location
= loc
;
20492 pop_deferring_access_checks ();
20493 pop_access_scope (d
);
20496 /* Instantiate the indicated variable, function, or alias template TMPL with
20497 the template arguments in TARG_PTR. */
20500 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20502 tree targ_ptr
= orig_args
;
20506 bool access_ok
= true;
20508 if (tmpl
== error_mark_node
)
20509 return error_mark_node
;
20511 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20513 /* If this function is a clone, handle it specially. */
20514 if (DECL_CLONED_FUNCTION_P (tmpl
))
20519 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20520 DECL_CLONED_FUNCTION. */
20521 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20522 targ_ptr
, complain
);
20523 if (spec
== error_mark_node
)
20524 return error_mark_node
;
20526 /* Look for the clone. */
20527 FOR_EACH_CLONE (clone
, spec
)
20528 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20530 /* We should always have found the clone by now. */
20531 gcc_unreachable ();
20535 if (targ_ptr
== error_mark_node
)
20536 return error_mark_node
;
20538 /* Check to see if we already have this specialization. */
20539 gen_tmpl
= most_general_template (tmpl
);
20540 if (TMPL_ARGS_DEPTH (targ_ptr
)
20541 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20542 /* targ_ptr only has the innermost template args, so add the outer ones
20543 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20544 the case of a non-dependent call within a template definition). */
20545 targ_ptr
= (add_outermost_template_args
20546 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20549 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20550 but it doesn't seem to be on the hot path. */
20551 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20553 gcc_assert (tmpl
== gen_tmpl
20554 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
20556 || fndecl
== NULL_TREE
);
20558 if (spec
!= NULL_TREE
)
20560 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20562 if (complain
& tf_error
)
20563 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20564 return error_mark_node
;
20569 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20571 return error_mark_node
;
20573 /* We are building a FUNCTION_DECL, during which the access of its
20574 parameters and return types have to be checked. However this
20575 FUNCTION_DECL which is the desired context for access checking
20576 is not built yet. We solve this chicken-and-egg problem by
20577 deferring all checks until we have the FUNCTION_DECL. */
20578 push_deferring_access_checks (dk_deferred
);
20580 /* Instantiation of the function happens in the context of the function
20581 template, not the context of the overload resolution we're doing. */
20582 push_to_top_level ();
20583 /* If there are dependent arguments, e.g. because we're doing partial
20584 ordering, make sure processing_template_decl stays set. */
20585 if (uses_template_parms (targ_ptr
))
20586 ++processing_template_decl
;
20587 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20589 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20590 complain
, gen_tmpl
, true);
20591 push_nested_class (ctx
);
20594 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20596 fndecl
= NULL_TREE
;
20597 if (VAR_P (pattern
))
20599 /* We need to determine if we're using a partial or explicit
20600 specialization now, because the type of the variable could be
20602 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20603 tree elt
= most_specialized_partial_spec (tid
, complain
);
20604 if (elt
== error_mark_node
)
20605 pattern
= error_mark_node
;
20608 tree partial_tmpl
= TREE_VALUE (elt
);
20609 tree partial_args
= TREE_PURPOSE (elt
);
20610 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20611 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20615 /* Substitute template parameters to obtain the specialization. */
20616 if (fndecl
== NULL_TREE
)
20617 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20618 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20619 pop_nested_class ();
20620 pop_from_top_level ();
20622 if (fndecl
== error_mark_node
)
20624 pop_deferring_access_checks ();
20625 return error_mark_node
;
20628 /* The DECL_TI_TEMPLATE should always be the immediate parent
20629 template, not the most general template. */
20630 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20631 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20633 /* Now we know the specialization, compute access previously
20634 deferred. Do no access control for inheriting constructors,
20635 as we already checked access for the inherited constructor. */
20636 if (!(flag_new_inheriting_ctors
20637 && DECL_INHERITED_CTOR (fndecl
)))
20639 push_access_scope (fndecl
);
20640 if (!perform_deferred_access_checks (complain
))
20642 pop_access_scope (fndecl
);
20644 pop_deferring_access_checks ();
20646 /* If we've just instantiated the main entry point for a function,
20647 instantiate all the alternate entry points as well. We do this
20648 by cloning the instantiation of the main entry point, not by
20649 instantiating the template clones. */
20650 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20651 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20652 clone_function_decl (fndecl
, /*update_methods=*/false);
20656 if (!(complain
& tf_error
))
20658 /* Remember to reinstantiate when we're out of SFINAE so the user
20659 can see the errors. */
20660 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20662 return error_mark_node
;
20667 /* Wrapper for instantiate_template_1. */
20670 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20673 timevar_push (TV_TEMPLATE_INST
);
20674 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20675 timevar_pop (TV_TEMPLATE_INST
);
20679 /* Instantiate the alias template TMPL with ARGS. Also push a template
20680 instantiation level, which instantiate_template doesn't do because
20681 functions and variables have sufficient context established by the
20685 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20687 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20688 return error_mark_node
;
20691 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20692 args
, tmpl
, complain
,
20693 /*require_all_args=*/true,
20694 /*use_default_args=*/true);
20696 /* FIXME check for satisfaction in check_instantiated_args. */
20698 && !any_dependent_template_arguments_p (args
)
20699 && !constraints_satisfied_p (tmpl
, args
))
20701 if (complain
& tf_error
)
20703 auto_diagnostic_group d
;
20704 error ("template constraint failure for %qD", tmpl
);
20705 diagnose_constraints (input_location
, tmpl
, args
);
20707 return error_mark_node
;
20710 if (!push_tinst_level (tmpl
, args
))
20711 return error_mark_node
;
20712 tree r
= instantiate_template (tmpl
, args
, complain
);
20713 pop_tinst_level ();
20718 /* PARM is a template parameter pack for FN. Returns true iff
20719 PARM is used in a deducible way in the argument list of FN. */
20722 pack_deducible_p (tree parm
, tree fn
)
20724 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20725 for (; t
; t
= TREE_CHAIN (t
))
20727 tree type
= TREE_VALUE (t
);
20729 if (!PACK_EXPANSION_P (type
))
20731 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20732 packs
; packs
= TREE_CHAIN (packs
))
20733 if (template_args_equal (TREE_VALUE (packs
), parm
))
20735 /* The template parameter pack is used in a function parameter
20736 pack. If this is the end of the parameter list, the
20737 template parameter pack is deducible. */
20738 if (TREE_CHAIN (t
) == void_list_node
)
20741 /* Otherwise, not. Well, it could be deduced from
20742 a non-pack parameter, but doing so would end up with
20743 a deduction mismatch, so don't bother. */
20747 /* The template parameter pack isn't used in any function parameter
20748 packs, but it might be used deeper, e.g. tuple<Args...>. */
20752 /* Subroutine of fn_type_unification: check non-dependent parms for
20756 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20757 tree fn
, unification_kind_t strict
, int flags
,
20758 struct conversion
**convs
, bool explain_p
)
20760 /* Non-constructor methods need to leave a conversion for 'this', which
20761 isn't included in nargs here. */
20762 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20763 && !DECL_CONSTRUCTOR_P (fn
));
20765 for (unsigned ia
= 0;
20766 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20768 tree parm
= TREE_VALUE (parms
);
20770 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20771 && (!TREE_CHAIN (parms
)
20772 || TREE_CHAIN (parms
) == void_list_node
))
20773 /* For a function parameter pack that occurs at the end of the
20774 parameter-declaration-list, the type A of each remaining
20775 argument of the call is compared with the type P of the
20776 declarator-id of the function parameter pack. */
20779 parms
= TREE_CHAIN (parms
);
20781 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20782 /* For a function parameter pack that does not occur at the
20783 end of the parameter-declaration-list, the type of the
20784 parameter pack is a non-deduced context. */
20787 if (!uses_template_parms (parm
))
20789 tree arg
= args
[ia
];
20790 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20791 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20793 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20794 conv_p
, explain_p
))
20804 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20805 NARGS elements of the arguments that are being used when calling
20806 it. TARGS is a vector into which the deduced template arguments
20809 Returns either a FUNCTION_DECL for the matching specialization of FN or
20810 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20811 true, diagnostics will be printed to explain why it failed.
20813 If FN is a conversion operator, or we are trying to produce a specific
20814 specialization, RETURN_TYPE is the return type desired.
20816 The EXPLICIT_TARGS are explicit template arguments provided via a
20819 The parameter STRICT is one of:
20822 We are deducing arguments for a function call, as in
20823 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20824 deducing arguments for a call to the result of a conversion
20825 function template, as in [over.call.object].
20828 We are deducing arguments for a conversion function, as in
20829 [temp.deduct.conv].
20832 We are deducing arguments when doing an explicit instantiation
20833 as in [temp.explicit], when determining an explicit specialization
20834 as in [temp.expl.spec], or when taking the address of a function
20835 template, as in [temp.deduct.funcaddr]. */
20838 fn_type_unification (tree fn
,
20839 tree explicit_targs
,
20842 unsigned int nargs
,
20844 unification_kind_t strict
,
20846 struct conversion
**convs
,
20852 tree decl
= NULL_TREE
;
20853 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20855 static int deduction_depth
;
20856 /* type_unification_real will pass back any access checks from default
20857 template argument substitution. */
20858 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20859 /* We don't have all the template args yet. */
20860 bool incomplete
= true;
20863 if (flag_new_inheriting_ctors
)
20864 fn
= strip_inheriting_ctors (fn
);
20866 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20867 tree r
= error_mark_node
;
20869 tree full_targs
= targs
;
20870 if (TMPL_ARGS_DEPTH (targs
)
20871 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20872 full_targs
= (add_outermost_template_args
20873 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20877 complain
|= tf_decltype
;
20879 /* In C++0x, it's possible to have a function template whose type depends
20880 on itself recursively. This is most obvious with decltype, but can also
20881 occur with enumeration scope (c++/48969). So we need to catch infinite
20882 recursion and reject the substitution at deduction time; this function
20883 will return error_mark_node for any repeated substitution.
20885 This also catches excessive recursion such as when f<N> depends on
20886 f<N-1> across all integers, and returns error_mark_node for all the
20887 substitutions back up to the initial one.
20889 This is, of course, not reentrant. */
20890 if (excessive_deduction_depth
)
20891 return error_mark_node
;
20894 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20896 fntype
= TREE_TYPE (fn
);
20897 if (explicit_targs
)
20901 The specified template arguments must match the template
20902 parameters in kind (i.e., type, nontype, template), and there
20903 must not be more arguments than there are parameters;
20904 otherwise type deduction fails.
20906 Nontype arguments must match the types of the corresponding
20907 nontype template parameters, or must be convertible to the
20908 types of the corresponding nontype parameters as specified in
20909 _temp.arg.nontype_, otherwise type deduction fails.
20911 All references in the function type of the function template
20912 to the corresponding template parameters are replaced by the
20913 specified template argument values. If a substitution in a
20914 template parameter or in the function type of the function
20915 template results in an invalid type, type deduction fails. */
20916 int i
, len
= TREE_VEC_LENGTH (tparms
);
20917 location_t loc
= input_location
;
20918 incomplete
= false;
20920 if (explicit_targs
== error_mark_node
)
20923 if (TMPL_ARGS_DEPTH (explicit_targs
)
20924 < TMPL_ARGS_DEPTH (full_targs
))
20925 explicit_targs
= add_outermost_template_args (full_targs
,
20928 /* Adjust any explicit template arguments before entering the
20929 substitution context. */
20931 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20932 complain
|tf_partial
,
20933 /*require_all_args=*/false,
20934 /*use_default_args=*/false));
20935 if (explicit_targs
== error_mark_node
)
20938 /* Substitute the explicit args into the function type. This is
20939 necessary so that, for instance, explicitly declared function
20940 arguments can match null pointed constants. If we were given
20941 an incomplete set of explicit args, we must not do semantic
20942 processing during substitution as we could create partial
20944 for (i
= 0; i
< len
; i
++)
20946 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20947 bool parameter_pack
= false;
20948 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20950 /* Dig out the actual parm. */
20951 if (TREE_CODE (parm
) == TYPE_DECL
20952 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20954 parm
= TREE_TYPE (parm
);
20955 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20957 else if (TREE_CODE (parm
) == PARM_DECL
)
20959 parm
= DECL_INITIAL (parm
);
20960 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20963 if (targ
== NULL_TREE
)
20964 /* No explicit argument for this template parameter. */
20966 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20968 /* Mark the argument pack as "incomplete". We could
20969 still deduce more arguments during unification.
20970 We remove this mark in type_unification_real. */
20971 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20972 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20973 = ARGUMENT_PACK_ARGS (targ
);
20975 /* We have some incomplete argument packs. */
20982 if (!push_tinst_level (fn
, explicit_targs
))
20984 excessive_deduction_depth
= true;
20987 ++processing_template_decl
;
20988 input_location
= DECL_SOURCE_LOCATION (fn
);
20989 /* Ignore any access checks; we'll see them again in
20990 instantiate_template and they might have the wrong
20991 access path at this point. */
20992 push_deferring_access_checks (dk_deferred
);
20993 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20994 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20995 pop_deferring_access_checks ();
20996 input_location
= loc
;
20997 --processing_template_decl
;
20998 pop_tinst_level ();
21000 if (fntype
== error_mark_node
)
21004 /* Place the explicitly specified arguments in TARGS. */
21005 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21006 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21007 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21008 if (!incomplete
&& CHECKING_P
21009 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21010 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21011 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21014 if (return_type
&& strict
!= DEDUCE_CALL
)
21016 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21017 new_args
[0] = return_type
;
21018 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21026 /* Never do unification on the 'this' parameter. */
21027 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21029 if (return_type
&& strict
== DEDUCE_CALL
)
21031 /* We're deducing for a call to the result of a template conversion
21032 function. The parms we really want are in return_type. */
21033 if (INDIRECT_TYPE_P (return_type
))
21034 return_type
= TREE_TYPE (return_type
);
21035 parms
= TYPE_ARG_TYPES (return_type
);
21037 else if (return_type
)
21039 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21042 /* We allow incomplete unification without an error message here
21043 because the standard doesn't seem to explicitly prohibit it. Our
21044 callers must be ready to deal with unification failures in any
21047 /* If we aren't explaining yet, push tinst context so we can see where
21048 any errors (e.g. from class instantiations triggered by instantiation
21049 of default template arguments) come from. If we are explaining, this
21050 context is redundant. */
21051 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21053 excessive_deduction_depth
= true;
21057 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21058 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21059 strict
, &checks
, explain_p
);
21061 pop_tinst_level ();
21065 /* Now that we have bindings for all of the template arguments,
21066 ensure that the arguments deduced for the template template
21067 parameters have compatible template parameter lists. We cannot
21068 check this property before we have deduced all template
21069 arguments, because the template parameter types of a template
21070 template parameter might depend on prior template parameters
21071 deduced after the template template parameter. The following
21072 ill-formed example illustrates this issue:
21074 template<typename T, template<T> class C> void f(C<5>, T);
21076 template<int N> struct X {};
21079 f(X<5>(), 5l); // error: template argument deduction fails
21082 The template parameter list of 'C' depends on the template type
21083 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21084 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21085 time that we deduce 'C'. */
21086 if (!template_template_parm_bindings_ok_p
21087 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21089 unify_inconsistent_template_template_parameters (explain_p
);
21093 /* DR 1391: All parameters have args, now check non-dependent parms for
21095 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21100 /* All is well so far. Now, check:
21104 When all template arguments have been deduced, all uses of
21105 template parameters in nondeduced contexts are replaced with
21106 the corresponding deduced argument values. If the
21107 substitution results in an invalid type, as described above,
21108 type deduction fails. */
21109 if (!push_tinst_level (fn
, targs
))
21111 excessive_deduction_depth
= true;
21115 /* Also collect access checks from the instantiation. */
21116 reopen_deferring_access_checks (checks
);
21118 decl
= instantiate_template (fn
, targs
, complain
);
21120 checks
= get_deferred_access_checks ();
21121 pop_deferring_access_checks ();
21123 pop_tinst_level ();
21125 if (decl
== error_mark_node
)
21128 /* Now perform any access checks encountered during substitution. */
21129 push_access_scope (decl
);
21130 ok
= perform_access_checks (checks
, complain
);
21131 pop_access_scope (decl
);
21135 /* If we're looking for an exact match, check that what we got
21136 is indeed an exact match. It might not be if some template
21137 parameters are used in non-deduced contexts. But don't check
21138 for an exact match if we have dependent template arguments;
21139 in that case we're doing partial ordering, and we already know
21140 that we have two candidates that will provide the actual type. */
21141 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21143 tree substed
= TREE_TYPE (decl
);
21147 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21149 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21150 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21151 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21153 unify_type_mismatch (explain_p
, args
[i
],
21154 TREE_VALUE (sarg
));
21159 /* After doing deduction with the inherited constructor, actually return an
21160 instantiation of the inheriting constructor. */
21162 decl
= instantiate_template (orig_fn
, targs
, complain
);
21168 if (excessive_deduction_depth
)
21170 if (deduction_depth
== 0)
21171 /* Reset once we're all the way out. */
21172 excessive_deduction_depth
= false;
21178 /* Adjust types before performing type deduction, as described in
21179 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21180 sections are symmetric. PARM is the type of a function parameter
21181 or the return type of the conversion function. ARG is the type of
21182 the argument passed to the call, or the type of the value
21183 initialized with the result of the conversion function.
21184 ARG_EXPR is the original argument expression, which may be null. */
21187 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21200 /* Swap PARM and ARG throughout the remainder of this
21201 function; the handling is precisely symmetric since PARM
21202 will initialize ARG rather than vice versa. */
21203 std::swap (parm
, arg
);
21207 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21208 too, but here handle it by stripping the reference from PARM
21209 rather than by adding it to ARG. */
21210 if (TYPE_REF_P (*parm
)
21211 && TYPE_REF_IS_RVALUE (*parm
)
21212 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21213 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21214 && TYPE_REF_P (*arg
)
21215 && !TYPE_REF_IS_RVALUE (*arg
))
21216 *parm
= TREE_TYPE (*parm
);
21217 /* Nothing else to do in this case. */
21221 gcc_unreachable ();
21224 if (!TYPE_REF_P (*parm
))
21226 /* [temp.deduct.call]
21228 If P is not a reference type:
21230 --If A is an array type, the pointer type produced by the
21231 array-to-pointer standard conversion (_conv.array_) is
21232 used in place of A for type deduction; otherwise,
21234 --If A is a function type, the pointer type produced by
21235 the function-to-pointer standard conversion
21236 (_conv.func_) is used in place of A for type deduction;
21239 --If A is a cv-qualified type, the top level
21240 cv-qualifiers of A's type are ignored for type
21242 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21243 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21244 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21245 *arg
= build_pointer_type (*arg
);
21247 *arg
= TYPE_MAIN_VARIANT (*arg
);
21250 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21251 reference to a cv-unqualified template parameter that does not represent a
21252 template parameter of a class template (during class template argument
21253 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21254 an lvalue, the type "lvalue reference to A" is used in place of A for type
21256 if (TYPE_REF_P (*parm
)
21257 && TYPE_REF_IS_RVALUE (*parm
)
21258 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21259 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21260 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21261 && (arg_expr
? lvalue_p (arg_expr
)
21262 /* try_one_overload doesn't provide an arg_expr, but
21263 functions are always lvalues. */
21264 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21265 *arg
= build_reference_type (*arg
);
21267 /* [temp.deduct.call]
21269 If P is a cv-qualified type, the top level cv-qualifiers
21270 of P's type are ignored for type deduction. If P is a
21271 reference type, the type referred to by P is used for
21273 *parm
= TYPE_MAIN_VARIANT (*parm
);
21274 if (TYPE_REF_P (*parm
))
21276 *parm
= TREE_TYPE (*parm
);
21277 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21280 /* DR 322. For conversion deduction, remove a reference type on parm
21281 too (which has been swapped into ARG). */
21282 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21283 *arg
= TREE_TYPE (*arg
);
21288 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21289 template which doesn't contain any deducible template parameters; check if
21290 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21291 unify_one_argument. */
21294 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21295 int flags
, struct conversion
**conv_p
,
21301 type
= TREE_TYPE (arg
);
21305 if (same_type_p (parm
, type
))
21306 return unify_success (explain_p
);
21308 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21309 if (strict
== DEDUCE_CONV
)
21311 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21312 return unify_success (explain_p
);
21314 else if (strict
!= DEDUCE_EXACT
)
21317 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21319 /* Avoid recalculating this in add_function_candidate. */
21321 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21323 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21325 return unify_success (explain_p
);
21328 if (strict
== DEDUCE_EXACT
)
21329 return unify_type_mismatch (explain_p
, parm
, arg
);
21331 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21334 static bool uses_deducible_template_parms (tree type
);
21336 /* Returns true iff the expression EXPR is one from which a template
21337 argument can be deduced. In other words, if it's an undecorated
21338 use of a template non-type parameter. */
21341 deducible_expression (tree expr
)
21343 /* Strip implicit conversions. */
21344 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21345 expr
= TREE_OPERAND (expr
, 0);
21346 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21349 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21350 deducible way; that is, if it has a max value of <PARM> - 1. */
21353 deducible_array_bound (tree domain
)
21355 if (domain
== NULL_TREE
)
21358 tree max
= TYPE_MAX_VALUE (domain
);
21359 if (TREE_CODE (max
) != MINUS_EXPR
)
21362 return deducible_expression (TREE_OPERAND (max
, 0));
21365 /* Returns true iff the template arguments ARGS use a template parameter
21366 in a deducible way. */
21369 deducible_template_args (tree args
)
21371 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21374 tree elt
= TREE_VEC_ELT (args
, i
);
21375 if (ARGUMENT_PACK_P (elt
))
21376 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21379 if (PACK_EXPANSION_P (elt
))
21380 elt
= PACK_EXPANSION_PATTERN (elt
);
21381 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21383 else if (TYPE_P (elt
))
21384 deducible
= uses_deducible_template_parms (elt
);
21386 deducible
= deducible_expression (elt
);
21394 /* Returns true iff TYPE contains any deducible references to template
21395 parameters, as per 14.8.2.5. */
21398 uses_deducible_template_parms (tree type
)
21400 if (PACK_EXPANSION_P (type
))
21401 type
= PACK_EXPANSION_PATTERN (type
);
21408 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21409 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21415 if (INDIRECT_TYPE_P (type
))
21416 return uses_deducible_template_parms (TREE_TYPE (type
));
21418 /* T[integer-constant ]
21420 if (TREE_CODE (type
) == ARRAY_TYPE
)
21421 return (uses_deducible_template_parms (TREE_TYPE (type
))
21422 || deducible_array_bound (TYPE_DOMAIN (type
)));
21434 if (TYPE_PTRMEM_P (type
))
21435 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21436 || (uses_deducible_template_parms
21437 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21439 /* template-name <T> (where template-name refers to a class template)
21440 template-name <i> (where template-name refers to a class template) */
21441 if (CLASS_TYPE_P (type
)
21442 && CLASSTYPE_TEMPLATE_INFO (type
)
21443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21444 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21445 (CLASSTYPE_TI_ARGS (type
)));
21450 if (FUNC_OR_METHOD_TYPE_P (type
))
21452 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21454 tree parm
= TYPE_ARG_TYPES (type
);
21455 if (TREE_CODE (type
) == METHOD_TYPE
)
21456 parm
= TREE_CHAIN (parm
);
21457 for (; parm
; parm
= TREE_CHAIN (parm
))
21458 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21465 /* Subroutine of type_unification_real and unify_pack_expansion to
21466 handle unification of a single P/A pair. Parameters are as
21467 for those functions. */
21470 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21471 int subr
, unification_kind_t strict
,
21474 tree arg_expr
= NULL_TREE
;
21477 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21478 return unify_invalid (explain_p
);
21479 if (arg
== unknown_type_node
)
21480 /* We can't deduce anything from this, but we might get all the
21481 template args from other function args. */
21482 return unify_success (explain_p
);
21484 /* Implicit conversions (Clause 4) will be performed on a function
21485 argument to convert it to the type of the corresponding function
21486 parameter if the parameter type contains no template-parameters that
21487 participate in template argument deduction. */
21488 if (strict
!= DEDUCE_EXACT
21489 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21490 /* For function parameters with no deducible template parameters,
21491 just return. We'll check non-dependent conversions later. */
21492 return unify_success (explain_p
);
21497 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21498 | UNIFY_ALLOW_MORE_CV_QUAL
21499 | UNIFY_ALLOW_DERIVED
);
21503 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21507 arg_strict
= UNIFY_ALLOW_NONE
;
21511 gcc_unreachable ();
21514 /* We only do these transformations if this is the top-level
21515 parameter_type_list in a call or declaration matching; in other
21516 situations (nested function declarators, template argument lists) we
21517 won't be comparing a type to an expression, and we don't do any type
21523 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21524 if (type_unknown_p (arg
))
21526 /* [temp.deduct.type] A template-argument can be
21527 deduced from a pointer to function or pointer
21528 to member function argument if the set of
21529 overloaded functions does not contain function
21530 templates and at most one of a set of
21531 overloaded functions provides a unique
21533 resolve_overloaded_unification (tparms
, targs
, parm
,
21535 arg_strict
, explain_p
);
21536 /* If a unique match was not found, this is a
21537 non-deduced context, so we still succeed. */
21538 return unify_success (explain_p
);
21542 arg
= unlowered_expr_type (arg
);
21543 if (arg
== error_mark_node
)
21544 return unify_invalid (explain_p
);
21548 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21551 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21552 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21553 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21555 /* For deduction from an init-list we need the actual list. */
21556 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21558 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21561 /* for_each_template_parm callback that always returns 0. */
21564 zero_r (tree
, void *)
21569 /* for_each_template_parm any_fn callback to handle deduction of a template
21570 type argument from the type of an array bound. */
21573 array_deduction_r (tree t
, void *data
)
21575 tree_pair_p d
= (tree_pair_p
)data
;
21576 tree
&tparms
= d
->purpose
;
21577 tree
&targs
= d
->value
;
21579 if (TREE_CODE (t
) == ARRAY_TYPE
)
21580 if (tree dom
= TYPE_DOMAIN (t
))
21581 if (tree max
= TYPE_MAX_VALUE (dom
))
21583 if (TREE_CODE (max
) == MINUS_EXPR
)
21584 max
= TREE_OPERAND (max
, 0);
21585 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21586 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21587 UNIFY_ALLOW_NONE
, /*explain*/false);
21590 /* Keep walking. */
21594 /* Try to deduce any not-yet-deduced template type arguments from the type of
21595 an array bound. This is handled separately from unify because 14.8.2.5 says
21596 "The type of a type parameter is only deduced from an array bound if it is
21597 not otherwise deduced." */
21600 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21602 tree_pair_s data
= { tparms
, targs
};
21603 hash_set
<tree
> visited
;
21604 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21605 /*nondeduced*/false, array_deduction_r
);
21608 /* Most parms like fn_type_unification.
21610 If SUBR is 1, we're being called recursively (to unify the
21611 arguments of a function or method parameter of a function
21614 CHECKS is a pointer to a vector of access checks encountered while
21615 substituting default template arguments. */
21618 type_unification_real (tree tparms
,
21622 unsigned int xnargs
,
21624 unification_kind_t strict
,
21625 vec
<deferred_access_check
, va_gc
> **checks
,
21630 int ntparms
= TREE_VEC_LENGTH (tparms
);
21631 int saw_undeduced
= 0;
21634 unsigned int nargs
;
21637 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21638 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21639 gcc_assert (ntparms
> 0);
21641 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21643 /* Reset the number of non-defaulted template arguments contained
21645 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21653 while (parms
&& parms
!= void_list_node
21656 parm
= TREE_VALUE (parms
);
21658 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21659 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21660 /* For a function parameter pack that occurs at the end of the
21661 parameter-declaration-list, the type A of each remaining
21662 argument of the call is compared with the type P of the
21663 declarator-id of the function parameter pack. */
21666 parms
= TREE_CHAIN (parms
);
21668 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21669 /* For a function parameter pack that does not occur at the
21670 end of the parameter-declaration-list, the type of the
21671 parameter pack is a non-deduced context. */
21677 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21683 && parms
!= void_list_node
21684 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21686 /* Unify the remaining arguments with the pack expansion type. */
21688 tree parmvec
= make_tree_vec (1);
21690 /* Allocate a TREE_VEC and copy in all of the arguments */
21691 argvec
= make_tree_vec (nargs
- ia
);
21692 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21693 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21695 /* Copy the parameter into parmvec. */
21696 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21697 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21698 /*subr=*/subr
, explain_p
))
21701 /* Advance to the end of the list of parameters. */
21702 parms
= TREE_CHAIN (parms
);
21705 /* Fail if we've reached the end of the parm list, and more args
21706 are present, and the parm list isn't variadic. */
21707 if (ia
< nargs
&& parms
== void_list_node
)
21708 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21709 /* Fail if parms are left and they don't have default values and
21710 they aren't all deduced as empty packs (c++/57397). This is
21711 consistent with sufficient_parms_p. */
21712 if (parms
&& parms
!= void_list_node
21713 && TREE_PURPOSE (parms
) == NULL_TREE
)
21715 unsigned int count
= nargs
;
21720 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21723 p
= TREE_CHAIN (p
);
21725 while (p
&& p
!= void_list_node
);
21726 if (count
!= nargs
)
21727 return unify_too_few_arguments (explain_p
, ia
, count
,
21733 tsubst_flags_t complain
= (explain_p
21734 ? tf_warning_or_error
21736 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21738 for (i
= 0; i
< ntparms
; i
++)
21740 tree targ
= TREE_VEC_ELT (targs
, i
);
21741 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21743 /* Clear the "incomplete" flags on all argument packs now so that
21744 substituting them into later default arguments works. */
21745 if (targ
&& ARGUMENT_PACK_P (targ
))
21747 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21748 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21751 if (targ
|| tparm
== error_mark_node
)
21753 tparm
= TREE_VALUE (tparm
);
21755 if (TREE_CODE (tparm
) == TYPE_DECL
21756 && !tried_array_deduction
)
21758 try_array_deduction (tparms
, targs
, xparms
);
21759 tried_array_deduction
= true;
21760 if (TREE_VEC_ELT (targs
, i
))
21764 /* If this is an undeduced nontype parameter that depends on
21765 a type parameter, try another pass; its type may have been
21766 deduced from a later argument than the one from which
21767 this parameter can be deduced. */
21768 if (TREE_CODE (tparm
) == PARM_DECL
21769 && uses_template_parms (TREE_TYPE (tparm
))
21770 && saw_undeduced
< 2)
21776 /* Core issue #226 (C++0x) [temp.deduct]:
21778 If a template argument has not been deduced, its
21779 default template argument, if any, is used.
21781 When we are in C++98 mode, TREE_PURPOSE will either
21782 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21783 to explicitly check cxx_dialect here. */
21784 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21785 /* OK, there is a default argument. Wait until after the
21786 conversion check to do substitution. */
21789 /* If the type parameter is a parameter pack, then it will
21790 be deduced to an empty parameter pack. */
21791 if (template_parameter_pack_p (tparm
))
21795 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21797 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21798 TREE_CONSTANT (arg
) = 1;
21801 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21803 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21805 TREE_VEC_ELT (targs
, i
) = arg
;
21809 return unify_parameter_deduction_failure (explain_p
, tparm
);
21812 /* Now substitute into the default template arguments. */
21813 for (i
= 0; i
< ntparms
; i
++)
21815 tree targ
= TREE_VEC_ELT (targs
, i
);
21816 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21818 if (targ
|| tparm
== error_mark_node
)
21820 tree parm
= TREE_VALUE (tparm
);
21821 tree arg
= TREE_PURPOSE (tparm
);
21822 reopen_deferring_access_checks (*checks
);
21823 location_t save_loc
= input_location
;
21825 input_location
= DECL_SOURCE_LOCATION (parm
);
21827 if (saw_undeduced
== 1
21828 && TREE_CODE (parm
) == PARM_DECL
21829 && uses_template_parms (TREE_TYPE (parm
)))
21831 /* The type of this non-type parameter depends on undeduced
21832 parameters. Don't try to use its default argument yet,
21833 since we might deduce an argument for it on the next pass,
21834 but do check whether the arguments we already have cause
21835 substitution failure, so that that happens before we try
21836 later default arguments (78489). */
21837 ++processing_template_decl
;
21838 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21840 --processing_template_decl
;
21841 if (type
== error_mark_node
)
21842 arg
= error_mark_node
;
21848 /* Even if the call is happening in template context, getting
21849 here means it's non-dependent, and a default argument is
21850 considered a separate definition under [temp.decls], so we can
21851 do this substitution without processing_template_decl. This
21852 is important if the default argument contains something that
21853 might be instantiation-dependent like access (87480). */
21854 processing_template_decl_sentinel s
;
21855 tree substed
= NULL_TREE
;
21856 if (saw_undeduced
== 1)
21858 /* First instatiate in template context, in case we still
21859 depend on undeduced template parameters. */
21860 ++processing_template_decl
;
21861 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21863 --processing_template_decl
;
21864 if (substed
!= error_mark_node
21865 && !uses_template_parms (substed
))
21866 /* We replaced all the tparms, substitute again out of
21867 template context. */
21868 substed
= NULL_TREE
;
21871 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21874 if (!uses_template_parms (substed
))
21875 arg
= convert_template_argument (parm
, substed
, full_targs
,
21876 complain
, i
, NULL_TREE
);
21877 else if (saw_undeduced
== 1)
21880 arg
= error_mark_node
;
21883 input_location
= save_loc
;
21884 *checks
= get_deferred_access_checks ();
21885 pop_deferring_access_checks ();
21887 if (arg
== error_mark_node
)
21891 TREE_VEC_ELT (targs
, i
) = arg
;
21892 /* The position of the first default template argument,
21893 is also the number of non-defaulted arguments in TARGS.
21895 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21900 if (saw_undeduced
++ == 1)
21904 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21905 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21907 return unify_success (explain_p
);
21910 /* Subroutine of type_unification_real. Args are like the variables
21911 at the call site. ARG is an overloaded function (or template-id);
21912 we try deducing template args from each of the overloads, and if
21913 only one succeeds, we go with that. Modifies TARGS and returns
21914 true on success. */
21917 resolve_overloaded_unification (tree tparms
,
21921 unification_kind_t strict
,
21925 tree tempargs
= copy_node (targs
);
21927 tree goodfn
= NULL_TREE
;
21930 if (TREE_CODE (arg
) == ADDR_EXPR
)
21932 arg
= TREE_OPERAND (arg
, 0);
21938 if (TREE_CODE (arg
) == COMPONENT_REF
)
21939 /* Handle `&x' where `x' is some static or non-static member
21941 arg
= TREE_OPERAND (arg
, 1);
21943 if (TREE_CODE (arg
) == OFFSET_REF
)
21944 arg
= TREE_OPERAND (arg
, 1);
21946 /* Strip baselink information. */
21947 if (BASELINK_P (arg
))
21948 arg
= BASELINK_FUNCTIONS (arg
);
21950 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21952 /* If we got some explicit template args, we need to plug them into
21953 the affected templates before we try to unify, in case the
21954 explicit args will completely resolve the templates in question. */
21957 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21958 arg
= TREE_OPERAND (arg
, 0);
21960 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21963 tree subargs
, elem
;
21965 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21968 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21969 expl_subargs
, NULL_TREE
, tf_none
,
21970 /*require_all_args=*/true,
21971 /*use_default_args=*/true);
21972 if (subargs
!= error_mark_node
21973 && !any_dependent_template_arguments_p (subargs
))
21975 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21976 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21977 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21978 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21987 /* If no templates (or more than one) are fully resolved by the
21988 explicit arguments, this template-id is a non-deduced context; it
21989 could still be OK if we deduce all template arguments for the
21990 enclosing call through other arguments. */
21994 else if (!OVL_P (arg
))
21995 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21996 -- but the deduction does not succeed because the expression is
21997 not just the function on its own. */
22000 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22003 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22004 strict
, sub_strict
, addr_p
, explain_p
)
22005 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22012 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22013 to function or pointer to member function argument if the set of
22014 overloaded functions does not contain function templates and at most
22015 one of a set of overloaded functions provides a unique match.
22017 So if we found multiple possibilities, we return success but don't
22018 deduce anything. */
22022 int i
= TREE_VEC_LENGTH (targs
);
22024 if (TREE_VEC_ELT (tempargs
, i
))
22026 tree old
= TREE_VEC_ELT (targs
, i
);
22027 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22028 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22029 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22030 /* Don't forget explicit template arguments in a pack. */
22031 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22032 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22033 TREE_VEC_ELT (targs
, i
) = new_
;
22042 /* Core DR 115: In contexts where deduction is done and fails, or in
22043 contexts where deduction is not done, if a template argument list is
22044 specified and it, along with any default template arguments, identifies
22045 a single function template specialization, then the template-id is an
22046 lvalue for the function template specialization. */
22049 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22051 tree expr
, offset
, baselink
;
22054 if (!type_unknown_p (orig_expr
))
22059 offset
= NULL_TREE
;
22060 baselink
= NULL_TREE
;
22062 if (TREE_CODE (expr
) == ADDR_EXPR
)
22064 expr
= TREE_OPERAND (expr
, 0);
22067 if (TREE_CODE (expr
) == OFFSET_REF
)
22070 expr
= TREE_OPERAND (expr
, 1);
22072 if (BASELINK_P (expr
))
22075 expr
= BASELINK_FUNCTIONS (expr
);
22078 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22081 tree goodfn
= NULL_TREE
;
22083 /* If we got some explicit template args, we need to plug them into
22084 the affected templates before we try to unify, in case the
22085 explicit args will completely resolve the templates in question. */
22087 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22088 tree arg
= TREE_OPERAND (expr
, 0);
22089 tree badfn
= NULL_TREE
;
22090 tree badargs
= NULL_TREE
;
22092 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22095 tree subargs
, elem
;
22097 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22100 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22101 expl_subargs
, NULL_TREE
, tf_none
,
22102 /*require_all_args=*/true,
22103 /*use_default_args=*/true);
22104 if (subargs
!= error_mark_node
22105 && !any_dependent_template_arguments_p (subargs
))
22107 elem
= instantiate_template (fn
, subargs
, tf_none
);
22108 if (elem
== error_mark_node
)
22113 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
22122 mark_used (goodfn
);
22125 expr
= build_baselink (BASELINK_BINFO (baselink
),
22126 BASELINK_ACCESS_BINFO (baselink
),
22127 expr
, BASELINK_OPTYPE (baselink
));
22131 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22132 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22135 expr
= cp_build_addr_expr (expr
, complain
);
22138 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22139 /* There were no good options and at least one bad one, so let the
22140 user know what the problem is. */
22141 instantiate_template (badfn
, badargs
, complain
);
22146 /* As above, but error out if the expression remains overloaded. */
22149 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22151 exp
= resolve_nondeduced_context (exp
, complain
);
22152 if (type_unknown_p (exp
))
22154 if (complain
& tf_error
)
22155 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22156 return error_mark_node
;
22161 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22162 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22163 different overloads deduce different arguments for a given parm.
22164 ADDR_P is true if the expression for which deduction is being
22165 performed was of the form "& fn" rather than simply "fn".
22167 Returns 1 on success. */
22170 try_one_overload (tree tparms
,
22175 unification_kind_t strict
,
22184 if (arg
== error_mark_node
)
22187 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22188 to function or pointer to member function argument if the set of
22189 overloaded functions does not contain function templates and at most
22190 one of a set of overloaded functions provides a unique match.
22192 So if this is a template, just return success. */
22194 if (uses_template_parms (arg
))
22197 if (TREE_CODE (arg
) == METHOD_TYPE
)
22198 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22200 arg
= build_pointer_type (arg
);
22202 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22204 /* We don't copy orig_targs for this because if we have already deduced
22205 some template args from previous args, unify would complain when we
22206 try to deduce a template parameter for the same argument, even though
22207 there isn't really a conflict. */
22208 nargs
= TREE_VEC_LENGTH (targs
);
22209 tempargs
= make_tree_vec (nargs
);
22211 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22214 /* First make sure we didn't deduce anything that conflicts with
22215 explicitly specified args. */
22216 for (i
= nargs
; i
--; )
22218 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22219 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22223 else if (uses_template_parms (elt
))
22224 /* Since we're unifying against ourselves, we will fill in
22225 template args used in the function parm list with our own
22226 template parms. Discard them. */
22227 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22228 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22230 /* Check that the argument at each index of the deduced argument pack
22231 is equivalent to the corresponding explicitly specified argument.
22232 We may have deduced more arguments than were explicitly specified,
22235 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22236 that's wrong if we deduce the same argument pack from multiple
22237 function arguments: it's only incomplete the first time. */
22239 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22240 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22242 if (TREE_VEC_LENGTH (deduced_pack
)
22243 < TREE_VEC_LENGTH (explicit_pack
))
22246 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22247 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22248 TREE_VEC_ELT (deduced_pack
, j
)))
22251 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22255 for (i
= nargs
; i
--; )
22257 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22260 TREE_VEC_ELT (targs
, i
) = elt
;
22266 /* PARM is a template class (perhaps with unbound template
22267 parameters). ARG is a fully instantiated type. If ARG can be
22268 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22269 TARGS are as for unify. */
22272 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22275 tree copy_of_targs
;
22277 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22279 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22280 /* Matches anything. */;
22281 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22282 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22285 /* We need to make a new template argument vector for the call to
22286 unify. If we used TARGS, we'd clutter it up with the result of
22287 the attempted unification, even if this class didn't work out.
22288 We also don't want to commit ourselves to all the unifications
22289 we've already done, since unification is supposed to be done on
22290 an argument-by-argument basis. In other words, consider the
22291 following pathological case:
22293 template <int I, int J, int K>
22296 template <int I, int J>
22297 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22299 template <int I, int J, int K>
22300 void f(S<I, J, K>, S<I, I, I>);
22309 Now, by the time we consider the unification involving `s2', we
22310 already know that we must have `f<0, 0, 0>'. But, even though
22311 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22312 because there are two ways to unify base classes of S<0, 1, 2>
22313 with S<I, I, I>. If we kept the already deduced knowledge, we
22314 would reject the possibility I=1. */
22315 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22317 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22319 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22324 /* If unification failed, we're done. */
22325 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22326 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22332 /* Given a template type PARM and a class type ARG, find the unique
22333 base type in ARG that is an instance of PARM. We do not examine
22334 ARG itself; only its base-classes. If there is not exactly one
22335 appropriate base class, return NULL_TREE. PARM may be the type of
22336 a partial specialization, as well as a plain template type. Used
22339 static enum template_base_result
22340 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22341 bool explain_p
, tree
*result
)
22343 tree rval
= NULL_TREE
;
22346 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22348 binfo
= TYPE_BINFO (complete_type (arg
));
22351 /* The type could not be completed. */
22352 *result
= NULL_TREE
;
22353 return tbr_incomplete_type
;
22356 /* Walk in inheritance graph order. The search order is not
22357 important, and this avoids multiple walks of virtual bases. */
22358 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22360 tree r
= try_class_unification (tparms
, targs
, parm
,
22361 BINFO_TYPE (binfo
), explain_p
);
22365 /* If there is more than one satisfactory baseclass, then:
22369 If they yield more than one possible deduced A, the type
22373 if (rval
&& !same_type_p (r
, rval
))
22375 *result
= NULL_TREE
;
22376 return tbr_ambiguous_baseclass
;
22384 return tbr_success
;
22387 /* Returns the level of DECL, which declares a template parameter. */
22390 template_decl_level (tree decl
)
22392 switch (TREE_CODE (decl
))
22395 case TEMPLATE_DECL
:
22396 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22399 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22402 gcc_unreachable ();
22407 /* Decide whether ARG can be unified with PARM, considering only the
22408 cv-qualifiers of each type, given STRICT as documented for unify.
22409 Returns nonzero iff the unification is OK on that basis. */
22412 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22414 int arg_quals
= cp_type_quals (arg
);
22415 int parm_quals
= cp_type_quals (parm
);
22417 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22418 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22420 /* Although a CVR qualifier is ignored when being applied to a
22421 substituted template parameter ([8.3.2]/1 for example), that
22422 does not allow us to unify "const T" with "int&" because both
22423 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22424 It is ok when we're allowing additional CV qualifiers
22425 at the outer level [14.8.2.1]/3,1st bullet. */
22426 if ((TYPE_REF_P (arg
)
22427 || FUNC_OR_METHOD_TYPE_P (arg
))
22428 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22431 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22432 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22436 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22437 && (arg_quals
& parm_quals
) != parm_quals
)
22440 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22441 && (parm_quals
& arg_quals
) != arg_quals
)
22447 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22449 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22451 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22452 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22453 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22455 *index
= TEMPLATE_TYPE_IDX (parm
);
22456 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22460 *index
= TEMPLATE_PARM_IDX (parm
);
22461 *level
= TEMPLATE_PARM_LEVEL (parm
);
22465 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22467 if (unify (TP, TA, P, A, S, EP)) \
22471 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22472 expansion at the end of PACKED_PARMS. Returns 0 if the type
22473 deduction succeeds, 1 otherwise. STRICT is the same as in
22474 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22475 function call argument list. We'll need to adjust the arguments to make them
22476 types. SUBR tells us if this is from a recursive call to
22477 type_unification_real, or for comparing two template argument
22481 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22482 tree packed_args
, unification_kind_t strict
,
22483 bool subr
, bool explain_p
)
22486 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22487 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22488 tree pack
, packs
= NULL_TREE
;
22489 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22491 /* Add in any args remembered from an earlier partial instantiation. */
22492 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22493 int levels
= TMPL_ARGS_DEPTH (targs
);
22495 packed_args
= expand_template_argument_pack (packed_args
);
22497 int len
= TREE_VEC_LENGTH (packed_args
);
22499 /* Determine the parameter packs we will be deducing from the
22500 pattern, and record their current deductions. */
22501 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22502 pack
; pack
= TREE_CHAIN (pack
))
22504 tree parm_pack
= TREE_VALUE (pack
);
22507 /* Only template parameter packs can be deduced, not e.g. function
22508 parameter packs or __bases or __integer_pack. */
22509 if (!TEMPLATE_PARM_P (parm_pack
))
22512 /* Determine the index and level of this parameter pack. */
22513 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22514 if (level
< levels
)
22517 /* Keep track of the parameter packs and their corresponding
22519 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22520 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22523 /* Loop through all of the arguments that have not yet been
22524 unified and unify each with the pattern. */
22525 for (i
= start
; i
< len
; i
++)
22528 bool any_explicit
= false;
22529 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22531 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22532 or the element of its argument pack at the current index if
22533 this argument was explicitly specified. */
22534 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22538 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22541 if (TREE_VALUE (pack
)
22542 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22543 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22545 any_explicit
= true;
22546 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22548 TMPL_ARG (targs
, level
, idx
) = arg
;
22551 /* If we had explicit template arguments, substitute them into the
22552 pattern before deduction. */
22555 /* Some arguments might still be unspecified or dependent. */
22557 ++processing_template_decl
;
22558 dependent
= any_dependent_template_arguments_p (targs
);
22560 --processing_template_decl
;
22561 parm
= tsubst (pattern
, targs
,
22562 explain_p
? tf_warning_or_error
: tf_none
,
22565 --processing_template_decl
;
22566 if (parm
== error_mark_node
)
22572 /* Unify the pattern with the current argument. */
22573 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22577 /* For each parameter pack, collect the deduced value. */
22578 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22581 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22583 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22584 TMPL_ARG (targs
, level
, idx
);
22588 /* Verify that the results of unification with the parameter packs
22589 produce results consistent with what we've seen before, and make
22590 the deduced argument packs available. */
22591 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22593 tree old_pack
= TREE_VALUE (pack
);
22594 tree new_args
= TREE_TYPE (pack
);
22595 int i
, len
= TREE_VEC_LENGTH (new_args
);
22597 bool nondeduced_p
= false;
22599 /* By default keep the original deduced argument pack.
22600 If necessary, more specific code is going to update the
22601 resulting deduced argument later down in this function. */
22602 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22603 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22605 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22606 actually deduce anything. */
22607 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22608 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22609 nondeduced_p
= true;
22613 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22615 /* If we had fewer function args than explicit template args,
22616 just use the explicits. */
22617 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22618 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22619 if (len
< explicit_len
)
22620 new_args
= explicit_args
;
22626 /* Build the deduced *_ARGUMENT_PACK. */
22627 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22629 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22630 TREE_CONSTANT (result
) = 1;
22633 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22635 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22637 /* Note the deduced argument packs for this parameter
22639 TMPL_ARG (targs
, level
, idx
) = result
;
22641 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22642 && (ARGUMENT_PACK_ARGS (old_pack
)
22643 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22645 /* We only had the explicitly-provided arguments before, but
22646 now we have a complete set of arguments. */
22647 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22649 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22650 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22651 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22655 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22656 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22658 if (!comp_template_args (old_args
, new_args
,
22659 &bad_old_arg
, &bad_new_arg
))
22660 /* Inconsistent unification of this parameter pack. */
22661 return unify_parameter_pack_inconsistent (explain_p
,
22667 return unify_success (explain_p
);
22670 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22671 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22672 parameters and return value are as for unify. */
22675 unify_array_domain (tree tparms
, tree targs
,
22676 tree parm_dom
, tree arg_dom
,
22684 /* Our representation of array types uses "N - 1" as the
22685 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22686 not an integer constant. We cannot unify arbitrarily
22687 complex expressions, so we eliminate the MINUS_EXPRs
22689 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22690 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22693 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22694 parm_max
= TREE_OPERAND (parm_max
, 0);
22696 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22697 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22700 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22701 trying to unify the type of a variable with the type
22702 of a template parameter. For example:
22704 template <unsigned int N>
22705 void f (char (&) [N]);
22712 Here, the type of the ARG will be "int [g(i)]", and
22713 may be a SAVE_EXPR, etc. */
22714 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22715 return unify_vla_arg (explain_p
, arg_dom
);
22716 arg_max
= TREE_OPERAND (arg_max
, 0);
22719 /* If only one of the bounds used a MINUS_EXPR, compensate
22720 by adding one to the other bound. */
22721 if (parm_cst
&& !arg_cst
)
22722 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22726 else if (arg_cst
&& !parm_cst
)
22727 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22732 return unify (tparms
, targs
, parm_max
, arg_max
,
22733 UNIFY_ALLOW_INTEGER
, explain_p
);
22736 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22738 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22743 if (PACK_EXPANSION_P (t
))
22744 t
= PACK_EXPANSION_PATTERN (t
);
22745 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22746 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22747 || DECL_TYPE_TEMPLATE_P (t
))
22749 else if (TYPE_P (t
))
22755 /* Deduce the value of template parameters. TPARMS is the (innermost)
22756 set of template parameters to a template. TARGS is the bindings
22757 for those template parameters, as determined thus far; TARGS may
22758 include template arguments for outer levels of template parameters
22759 as well. PARM is a parameter to a template function, or a
22760 subcomponent of that parameter; ARG is the corresponding argument.
22761 This function attempts to match PARM with ARG in a manner
22762 consistent with the existing assignments in TARGS. If more values
22763 are deduced, then TARGS is updated.
22765 Returns 0 if the type deduction succeeds, 1 otherwise. The
22766 parameter STRICT is a bitwise or of the following flags:
22769 Require an exact match between PARM and ARG.
22770 UNIFY_ALLOW_MORE_CV_QUAL:
22771 Allow the deduced ARG to be more cv-qualified (by qualification
22772 conversion) than ARG.
22773 UNIFY_ALLOW_LESS_CV_QUAL:
22774 Allow the deduced ARG to be less cv-qualified than ARG.
22775 UNIFY_ALLOW_DERIVED:
22776 Allow the deduced ARG to be a template base class of ARG,
22777 or a pointer to a template base class of the type pointed to by
22779 UNIFY_ALLOW_INTEGER:
22780 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22781 case for more information.
22782 UNIFY_ALLOW_OUTER_LEVEL:
22783 This is the outermost level of a deduction. Used to determine validity
22784 of qualification conversions. A valid qualification conversion must
22785 have const qualified pointers leading up to the inner type which
22786 requires additional CV quals, except at the outer level, where const
22787 is not required [conv.qual]. It would be normal to set this flag in
22788 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22789 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22790 This is the outermost level of a deduction, and PARM can be more CV
22791 qualified at this point.
22792 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22793 This is the outermost level of a deduction, and PARM can be less CV
22794 qualified at this point. */
22797 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22803 int strict_in
= strict
;
22804 tsubst_flags_t complain
= (explain_p
22805 ? tf_warning_or_error
22808 /* I don't think this will do the right thing with respect to types.
22809 But the only case I've seen it in so far has been array bounds, where
22810 signedness is the only information lost, and I think that will be
22811 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22812 finish_id_expression_1, and are also OK. */
22813 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22814 parm
= TREE_OPERAND (parm
, 0);
22816 if (arg
== error_mark_node
)
22817 return unify_invalid (explain_p
);
22818 if (arg
== unknown_type_node
22819 || arg
== init_list_type_node
)
22820 /* We can't deduce anything from this, but we might get all the
22821 template args from other function args. */
22822 return unify_success (explain_p
);
22824 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22825 return unify_success (explain_p
);
22827 /* If PARM uses template parameters, then we can't bail out here,
22828 even if ARG == PARM, since we won't record unifications for the
22829 template parameters. We might need them if we're trying to
22830 figure out which of two things is more specialized. */
22831 if (arg
== parm
&& !uses_template_parms (parm
))
22832 return unify_success (explain_p
);
22834 /* Handle init lists early, so the rest of the function can assume
22835 we're dealing with a type. */
22836 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22840 tree orig_parm
= parm
;
22842 if (!is_std_init_list (parm
)
22843 && TREE_CODE (parm
) != ARRAY_TYPE
)
22844 /* We can only deduce from an initializer list argument if the
22845 parameter is std::initializer_list or an array; otherwise this
22846 is a non-deduced context. */
22847 return unify_success (explain_p
);
22849 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22850 elttype
= TREE_TYPE (parm
);
22853 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22854 /* Deduction is defined in terms of a single type, so just punt
22855 on the (bizarre) std::initializer_list<T...>. */
22856 if (PACK_EXPANSION_P (elttype
))
22857 return unify_success (explain_p
);
22860 if (strict
!= DEDUCE_EXACT
22861 && TYPE_P (elttype
)
22862 && !uses_deducible_template_parms (elttype
))
22863 /* If ELTTYPE has no deducible template parms, skip deduction from
22864 the list elements. */;
22866 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22868 int elt_strict
= strict
;
22870 if (elt
== error_mark_node
)
22871 return unify_invalid (explain_p
);
22873 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22875 tree type
= TREE_TYPE (elt
);
22876 if (type
== error_mark_node
)
22877 return unify_invalid (explain_p
);
22878 /* It should only be possible to get here for a call. */
22879 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22880 elt_strict
|= maybe_adjust_types_for_deduction
22881 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22885 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22889 if (TREE_CODE (parm
) == ARRAY_TYPE
22890 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22892 /* Also deduce from the length of the initializer list. */
22893 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22894 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22895 if (idx
== error_mark_node
)
22896 return unify_invalid (explain_p
);
22897 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22901 /* If the std::initializer_list<T> deduction worked, replace the
22902 deduced A with std::initializer_list<A>. */
22903 if (orig_parm
!= parm
)
22905 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22906 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22907 targ
= listify (targ
);
22908 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22910 return unify_success (explain_p
);
22913 /* If parm and arg aren't the same kind of thing (template, type, or
22914 expression), fail early. */
22915 if (pa_kind (parm
) != pa_kind (arg
))
22916 return unify_invalid (explain_p
);
22918 /* Immediately reject some pairs that won't unify because of
22919 cv-qualification mismatches. */
22920 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22922 /* It is the elements of the array which hold the cv quals of an array
22923 type, and the elements might be template type parms. We'll check
22924 when we recurse. */
22925 && TREE_CODE (arg
) != ARRAY_TYPE
22926 /* We check the cv-qualifiers when unifying with template type
22927 parameters below. We want to allow ARG `const T' to unify with
22928 PARM `T' for example, when computing which of two templates
22929 is more specialized, for example. */
22930 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22931 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22932 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22934 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22935 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22936 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22937 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22938 strict
&= ~UNIFY_ALLOW_DERIVED
;
22939 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22940 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22942 switch (TREE_CODE (parm
))
22944 case TYPENAME_TYPE
:
22946 case UNBOUND_CLASS_TEMPLATE
:
22947 /* In a type which contains a nested-name-specifier, template
22948 argument values cannot be deduced for template parameters used
22949 within the nested-name-specifier. */
22950 return unify_success (explain_p
);
22952 case TEMPLATE_TYPE_PARM
:
22953 case TEMPLATE_TEMPLATE_PARM
:
22954 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22955 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22956 if (error_operand_p (tparm
))
22957 return unify_invalid (explain_p
);
22959 if (TEMPLATE_TYPE_LEVEL (parm
)
22960 != template_decl_level (tparm
))
22961 /* The PARM is not one we're trying to unify. Just check
22962 to see if it matches ARG. */
22964 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22965 && (is_auto (parm
) ? is_auto (arg
)
22966 : same_type_p (parm
, arg
)))
22967 return unify_success (explain_p
);
22969 return unify_type_mismatch (explain_p
, parm
, arg
);
22971 idx
= TEMPLATE_TYPE_IDX (parm
);
22972 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22973 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22974 if (error_operand_p (tparm
))
22975 return unify_invalid (explain_p
);
22977 /* Check for mixed types and values. */
22978 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22979 && TREE_CODE (tparm
) != TYPE_DECL
)
22980 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22981 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22982 gcc_unreachable ();
22984 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22986 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22987 && CLASS_TYPE_P (arg
))
22989 /* First try to match ARG directly. */
22990 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22994 /* Otherwise, look for a suitable base of ARG, as below. */
22995 enum template_base_result r
;
22996 r
= get_template_base (tparms
, targs
, parm
, arg
,
22999 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23003 /* ARG must be constructed from a template class or a template
23004 template parameter. */
23005 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23006 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23007 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23009 /* Deduce arguments T, i from TT<T> or TT<i>. */
23010 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23013 arg
= TYPE_TI_TEMPLATE (arg
);
23015 /* Fall through to deduce template name. */
23018 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23019 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23021 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23023 /* Simple cases: Value already set, does match or doesn't. */
23024 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23025 return unify_success (explain_p
);
23027 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23031 /* If PARM is `const T' and ARG is only `int', we don't have
23032 a match unless we are allowing additional qualification.
23033 If ARG is `const int' and PARM is just `T' that's OK;
23034 that binds `const int' to `T'. */
23035 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23037 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23039 /* Consider the case where ARG is `const volatile int' and
23040 PARM is `const T'. Then, T should be `volatile int'. */
23041 arg
= cp_build_qualified_type_real
23042 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23043 if (arg
== error_mark_node
)
23044 return unify_invalid (explain_p
);
23046 /* Simple cases: Value already set, does match or doesn't. */
23047 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23048 return unify_success (explain_p
);
23050 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23052 /* Make sure that ARG is not a variable-sized array. (Note
23053 that were talking about variable-sized arrays (like
23054 `int[n]'), rather than arrays of unknown size (like
23055 `int[]').) We'll get very confused by such a type since
23056 the bound of the array is not constant, and therefore
23057 not mangleable. Besides, such types are not allowed in
23058 ISO C++, so we can do as we please here. We do allow
23059 them for 'auto' deduction, since that isn't ABI-exposed. */
23060 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23061 return unify_vla_arg (explain_p
, arg
);
23063 /* Strip typedefs as in convert_template_argument. */
23064 arg
= canonicalize_type_argument (arg
, tf_none
);
23067 /* If ARG is a parameter pack or an expansion, we cannot unify
23068 against it unless PARM is also a parameter pack. */
23069 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23070 && !template_parameter_pack_p (parm
))
23071 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23073 /* If the argument deduction results is a METHOD_TYPE,
23074 then there is a problem.
23075 METHOD_TYPE doesn't map to any real C++ type the result of
23076 the deduction cannot be of that type. */
23077 if (TREE_CODE (arg
) == METHOD_TYPE
)
23078 return unify_method_type_error (explain_p
, arg
);
23080 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23081 return unify_success (explain_p
);
23083 case TEMPLATE_PARM_INDEX
:
23084 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23085 if (error_operand_p (tparm
))
23086 return unify_invalid (explain_p
);
23088 if (TEMPLATE_PARM_LEVEL (parm
)
23089 != template_decl_level (tparm
))
23091 /* The PARM is not one we're trying to unify. Just check
23092 to see if it matches ARG. */
23093 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23094 && cp_tree_equal (parm
, arg
));
23096 unify_expression_unequal (explain_p
, parm
, arg
);
23100 idx
= TEMPLATE_PARM_IDX (parm
);
23101 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23105 if ((strict
& UNIFY_ALLOW_INTEGER
)
23106 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23107 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23108 /* We're deducing from an array bound, the type doesn't matter. */
23109 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23110 int x
= !cp_tree_equal (targ
, arg
);
23112 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23116 /* [temp.deduct.type] If, in the declaration of a function template
23117 with a non-type template-parameter, the non-type
23118 template-parameter is used in an expression in the function
23119 parameter-list and, if the corresponding template-argument is
23120 deduced, the template-argument type shall match the type of the
23121 template-parameter exactly, except that a template-argument
23122 deduced from an array bound may be of any integral type.
23123 The non-type parameter might use already deduced type parameters. */
23124 tparm
= TREE_TYPE (parm
);
23125 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23126 /* We don't have enough levels of args to do any substitution. This
23127 can happen in the context of -fnew-ttp-matching. */;
23130 ++processing_template_decl
;
23131 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23132 --processing_template_decl
;
23134 if (tree a
= type_uses_auto (tparm
))
23136 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23137 if (tparm
== error_mark_node
)
23142 if (!TREE_TYPE (arg
))
23143 /* Template-parameter dependent expression. Just accept it for now.
23144 It will later be processed in convert_template_argument. */
23146 else if (same_type_ignoring_top_level_qualifiers_p
23147 (non_reference (TREE_TYPE (arg
)),
23148 non_reference (tparm
)))
23149 /* OK. Ignore top-level quals here because a class-type template
23150 parameter object is const. */;
23151 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23152 && CP_INTEGRAL_TYPE_P (tparm
))
23153 /* Convert the ARG to the type of PARM; the deduced non-type
23154 template argument must exactly match the types of the
23155 corresponding parameter. */
23156 arg
= fold (build_nop (tparm
, arg
));
23157 else if (uses_template_parms (tparm
))
23159 /* We haven't deduced the type of this parameter yet. */
23160 if (cxx_dialect
>= cxx17
23161 /* We deduce from array bounds in try_array_deduction. */
23162 && !(strict
& UNIFY_ALLOW_INTEGER
))
23164 /* Deduce it from the non-type argument. */
23165 tree atype
= TREE_TYPE (arg
);
23166 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23168 UNIFY_ALLOW_NONE
, explain_p
);
23171 /* Try again later. */
23172 return unify_success (explain_p
);
23175 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23177 /* If ARG is a parameter pack or an expansion, we cannot unify
23178 against it unless PARM is also a parameter pack. */
23179 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23180 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23181 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23184 bool removed_attr
= false;
23185 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23187 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23188 return unify_success (explain_p
);
23192 /* A pointer-to-member constant can be unified only with
23193 another constant. */
23194 if (TREE_CODE (arg
) != PTRMEM_CST
)
23195 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23197 /* Just unify the class member. It would be useless (and possibly
23198 wrong, depending on the strict flags) to unify also
23199 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23200 arg refer to the same variable, even if through different
23201 classes. For instance:
23203 struct A { int x; };
23206 Unification of &A::x and &B::x must succeed. */
23207 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23208 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23213 if (!TYPE_PTR_P (arg
))
23214 return unify_type_mismatch (explain_p
, parm
, arg
);
23216 /* [temp.deduct.call]
23218 A can be another pointer or pointer to member type that can
23219 be converted to the deduced A via a qualification
23220 conversion (_conv.qual_).
23222 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23223 This will allow for additional cv-qualification of the
23224 pointed-to types if appropriate. */
23226 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23227 /* The derived-to-base conversion only persists through one
23228 level of pointers. */
23229 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23231 return unify (tparms
, targs
, TREE_TYPE (parm
),
23232 TREE_TYPE (arg
), strict
, explain_p
);
23235 case REFERENCE_TYPE
:
23236 if (!TYPE_REF_P (arg
))
23237 return unify_type_mismatch (explain_p
, parm
, arg
);
23238 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23239 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23242 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23243 return unify_type_mismatch (explain_p
, parm
, arg
);
23244 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23245 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23246 return unify_type_mismatch (explain_p
, parm
, arg
);
23247 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23248 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23249 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23250 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23251 TYPE_DOMAIN (arg
), explain_p
);
23252 return unify_success (explain_p
);
23259 case ENUMERAL_TYPE
:
23262 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23263 return unify_type_mismatch (explain_p
, parm
, arg
);
23265 /* We have already checked cv-qualification at the top of the
23267 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23268 return unify_type_mismatch (explain_p
, parm
, arg
);
23270 /* As far as unification is concerned, this wins. Later checks
23271 will invalidate it if necessary. */
23272 return unify_success (explain_p
);
23274 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23275 /* Type INTEGER_CST can come from ordinary constant template args. */
23277 while (CONVERT_EXPR_P (arg
))
23278 arg
= TREE_OPERAND (arg
, 0);
23280 if (TREE_CODE (arg
) != INTEGER_CST
)
23281 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23282 return (tree_int_cst_equal (parm
, arg
)
23283 ? unify_success (explain_p
)
23284 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23288 int i
, len
, argslen
;
23289 int parm_variadic_p
= 0;
23291 if (TREE_CODE (arg
) != TREE_VEC
)
23292 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23294 len
= TREE_VEC_LENGTH (parm
);
23295 argslen
= TREE_VEC_LENGTH (arg
);
23297 /* Check for pack expansions in the parameters. */
23298 for (i
= 0; i
< len
; ++i
)
23300 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23303 /* We can unify against something with a trailing
23305 parm_variadic_p
= 1;
23307 /* [temp.deduct.type]/9: If the template argument list of
23308 P contains a pack expansion that is not the last
23309 template argument, the entire template argument list
23310 is a non-deduced context. */
23311 return unify_success (explain_p
);
23315 /* If we don't have enough arguments to satisfy the parameters
23316 (not counting the pack expression at the end), or we have
23317 too many arguments for a parameter list that doesn't end in
23318 a pack expression, we can't unify. */
23319 if (parm_variadic_p
23320 ? argslen
< len
- parm_variadic_p
23322 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23324 /* Unify all of the parameters that precede the (optional)
23325 pack expression. */
23326 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23328 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23329 TREE_VEC_ELT (parm
, i
),
23330 TREE_VEC_ELT (arg
, i
),
23331 UNIFY_ALLOW_NONE
, explain_p
);
23333 if (parm_variadic_p
)
23334 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23336 /*subr=*/true, explain_p
);
23337 return unify_success (explain_p
);
23342 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23343 return unify_type_mismatch (explain_p
, parm
, arg
);
23345 if (TYPE_PTRMEMFUNC_P (parm
))
23347 if (!TYPE_PTRMEMFUNC_P (arg
))
23348 return unify_type_mismatch (explain_p
, parm
, arg
);
23350 return unify (tparms
, targs
,
23351 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23352 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23353 strict
, explain_p
);
23355 else if (TYPE_PTRMEMFUNC_P (arg
))
23356 return unify_type_mismatch (explain_p
, parm
, arg
);
23358 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23360 tree t
= NULL_TREE
;
23362 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23364 /* First, we try to unify the PARM and ARG directly. */
23365 t
= try_class_unification (tparms
, targs
,
23366 parm
, arg
, explain_p
);
23370 /* Fallback to the special case allowed in
23371 [temp.deduct.call]:
23373 If P is a class, and P has the form
23374 template-id, then A can be a derived class of
23375 the deduced A. Likewise, if P is a pointer to
23376 a class of the form template-id, A can be a
23377 pointer to a derived class pointed to by the
23379 enum template_base_result r
;
23380 r
= get_template_base (tparms
, targs
, parm
, arg
,
23385 /* Don't give the derived diagnostic if we're
23386 already dealing with the same template. */
23388 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23389 && (CLASSTYPE_TI_TEMPLATE (parm
)
23390 == CLASSTYPE_TI_TEMPLATE (arg
)));
23391 return unify_no_common_base (explain_p
&& !same_template
,
23396 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23397 && (CLASSTYPE_TI_TEMPLATE (parm
)
23398 == CLASSTYPE_TI_TEMPLATE (arg
)))
23399 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23400 Then, we should unify `int' and `U'. */
23403 /* There's no chance of unification succeeding. */
23404 return unify_type_mismatch (explain_p
, parm
, arg
);
23406 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23407 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23409 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23410 return unify_type_mismatch (explain_p
, parm
, arg
);
23411 return unify_success (explain_p
);
23414 case FUNCTION_TYPE
:
23416 unsigned int nargs
;
23421 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23422 return unify_type_mismatch (explain_p
, parm
, arg
);
23424 /* CV qualifications for methods can never be deduced, they must
23425 match exactly. We need to check them explicitly here,
23426 because type_unification_real treats them as any other
23427 cv-qualified parameter. */
23428 if (TREE_CODE (parm
) == METHOD_TYPE
23429 && (!check_cv_quals_for_unify
23431 class_of_this_parm (arg
),
23432 class_of_this_parm (parm
))))
23433 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23434 if (TREE_CODE (arg
) == FUNCTION_TYPE
23435 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23436 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23437 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23438 return unify_type_mismatch (explain_p
, parm
, arg
);
23440 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23441 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23443 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23444 args
= XALLOCAVEC (tree
, nargs
);
23445 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23446 a
!= NULL_TREE
&& a
!= void_list_node
;
23447 a
= TREE_CHAIN (a
), ++i
)
23448 args
[i
] = TREE_VALUE (a
);
23451 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23452 args
, nargs
, 1, DEDUCE_EXACT
,
23456 if (flag_noexcept_type
)
23458 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23459 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23460 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23461 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23462 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23463 && uses_template_parms (TREE_PURPOSE (pspec
)))
23464 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23465 TREE_PURPOSE (aspec
),
23466 UNIFY_ALLOW_NONE
, explain_p
);
23467 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23468 return unify_type_mismatch (explain_p
, parm
, arg
);
23475 /* Unify a pointer to member with a pointer to member function, which
23476 deduces the type of the member as a function type. */
23477 if (TYPE_PTRMEMFUNC_P (arg
))
23479 /* Check top-level cv qualifiers */
23480 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23481 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23483 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23484 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23485 UNIFY_ALLOW_NONE
, explain_p
);
23487 /* Determine the type of the function we are unifying against. */
23488 tree fntype
= static_fn_type (arg
);
23490 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23493 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23494 return unify_type_mismatch (explain_p
, parm
, arg
);
23495 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23496 TYPE_OFFSET_BASETYPE (arg
),
23497 UNIFY_ALLOW_NONE
, explain_p
);
23498 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23499 strict
, explain_p
);
23502 if (DECL_TEMPLATE_PARM_P (parm
))
23503 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23504 if (arg
!= scalar_constant_value (parm
))
23505 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23506 return unify_success (explain_p
);
23509 case TEMPLATE_DECL
:
23510 /* Matched cases are handled by the ARG == PARM test above. */
23511 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23514 /* We might get a variable as a non-type template argument in parm if the
23515 corresponding parameter is type-dependent. Make any necessary
23516 adjustments based on whether arg is a reference. */
23517 if (CONSTANT_CLASS_P (arg
))
23518 parm
= fold_non_dependent_expr (parm
, complain
);
23519 else if (REFERENCE_REF_P (arg
))
23521 tree sub
= TREE_OPERAND (arg
, 0);
23523 if (TREE_CODE (sub
) == ADDR_EXPR
)
23524 arg
= TREE_OPERAND (sub
, 0);
23526 /* Now use the normal expression code to check whether they match. */
23529 case TYPE_ARGUMENT_PACK
:
23530 case NONTYPE_ARGUMENT_PACK
:
23531 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23532 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23535 case DECLTYPE_TYPE
:
23536 case UNDERLYING_TYPE
:
23537 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23538 or UNDERLYING_TYPE nodes. */
23539 return unify_success (explain_p
);
23542 /* Unification fails if we hit an error node. */
23543 return unify_invalid (explain_p
);
23546 if (REFERENCE_REF_P (parm
))
23548 bool pexp
= PACK_EXPANSION_P (arg
);
23550 arg
= PACK_EXPANSION_PATTERN (arg
);
23551 if (REFERENCE_REF_P (arg
))
23552 arg
= TREE_OPERAND (arg
, 0);
23554 arg
= make_pack_expansion (arg
, complain
);
23555 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23556 strict
, explain_p
);
23561 /* An unresolved overload is a nondeduced context. */
23562 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23563 return unify_success (explain_p
);
23564 gcc_assert (EXPR_P (parm
)
23565 || COMPOUND_LITERAL_P (parm
)
23566 || TREE_CODE (parm
) == TRAIT_EXPR
);
23568 /* We must be looking at an expression. This can happen with
23572 void foo(S<I>, S<I + 2>);
23576 template<typename T>
23577 void foo(A<T, T{}>);
23579 This is a "non-deduced context":
23583 The non-deduced contexts are:
23585 --A non-type template argument or an array bound in which
23586 a subexpression references a template parameter.
23588 In these cases, we assume deduction succeeded, but don't
23589 actually infer any unifications. */
23591 if (!uses_template_parms (parm
)
23592 && !template_args_equal (parm
, arg
))
23593 return unify_expression_unequal (explain_p
, parm
, arg
);
23595 return unify_success (explain_p
);
23598 #undef RECUR_AND_CHECK_FAILURE
23600 /* Note that DECL can be defined in this translation unit, if
23604 mark_definable (tree decl
)
23607 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23608 FOR_EACH_CLONE (clone
, decl
)
23609 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23612 /* Called if RESULT is explicitly instantiated, or is a member of an
23613 explicitly instantiated class. */
23616 mark_decl_instantiated (tree result
, int extern_p
)
23618 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23620 /* If this entity has already been written out, it's too late to
23621 make any modifications. */
23622 if (TREE_ASM_WRITTEN (result
))
23625 /* For anonymous namespace we don't need to do anything. */
23626 if (decl_anon_ns_mem_p (result
))
23628 gcc_assert (!TREE_PUBLIC (result
));
23632 if (TREE_CODE (result
) != FUNCTION_DECL
)
23633 /* The TREE_PUBLIC flag for function declarations will have been
23634 set correctly by tsubst. */
23635 TREE_PUBLIC (result
) = 1;
23637 /* This might have been set by an earlier implicit instantiation. */
23638 DECL_COMDAT (result
) = 0;
23641 DECL_NOT_REALLY_EXTERN (result
) = 0;
23644 mark_definable (result
);
23645 mark_needed (result
);
23646 /* Always make artificials weak. */
23647 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23648 comdat_linkage (result
);
23649 /* For WIN32 we also want to put explicit instantiations in
23650 linkonce sections. */
23651 else if (TREE_PUBLIC (result
))
23652 maybe_make_one_only (result
);
23653 if (TREE_CODE (result
) == FUNCTION_DECL
23654 && DECL_TEMPLATE_INSTANTIATED (result
))
23655 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23656 since start_preparsed_function wouldn't have if we had an earlier
23657 extern explicit instantiation. */
23658 DECL_EXTERNAL (result
) = 0;
23661 /* If EXTERN_P, then this function will not be emitted -- unless
23662 followed by an explicit instantiation, at which point its linkage
23663 will be adjusted. If !EXTERN_P, then this function will be
23664 emitted here. In neither circumstance do we want
23665 import_export_decl to adjust the linkage. */
23666 DECL_INTERFACE_KNOWN (result
) = 1;
23669 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23670 important template arguments. If any are missing, we check whether
23671 they're important by using error_mark_node for substituting into any
23672 args that were used for partial ordering (the ones between ARGS and END)
23673 and seeing if it bubbles up. */
23676 check_undeduced_parms (tree targs
, tree args
, tree end
)
23678 bool found
= false;
23680 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23681 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23684 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23688 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23689 if (substed
== error_mark_node
)
23695 /* Given two function templates PAT1 and PAT2, return:
23697 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23698 -1 if PAT2 is more specialized than PAT1.
23699 0 if neither is more specialized.
23701 LEN indicates the number of parameters we should consider
23702 (defaulted parameters should not be considered).
23704 The 1998 std underspecified function template partial ordering, and
23705 DR214 addresses the issue. We take pairs of arguments, one from
23706 each of the templates, and deduce them against each other. One of
23707 the templates will be more specialized if all the *other*
23708 template's arguments deduce against its arguments and at least one
23709 of its arguments *does* *not* deduce against the other template's
23710 corresponding argument. Deduction is done as for class templates.
23711 The arguments used in deduction have reference and top level cv
23712 qualifiers removed. Iff both arguments were originally reference
23713 types *and* deduction succeeds in both directions, an lvalue reference
23714 wins against an rvalue reference and otherwise the template
23715 with the more cv-qualified argument wins for that pairing (if
23716 neither is more cv-qualified, they both are equal). Unlike regular
23717 deduction, after all the arguments have been deduced in this way,
23718 we do *not* verify the deduced template argument values can be
23719 substituted into non-deduced contexts.
23721 The logic can be a bit confusing here, because we look at deduce1 and
23722 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23723 can find template arguments for pat1 to make arg1 look like arg2, that
23724 means that arg2 is at least as specialized as arg1. */
23727 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23729 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23730 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23731 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23732 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23733 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23734 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23735 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23736 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23737 tree origs1
, origs2
;
23738 bool lose1
= false;
23739 bool lose2
= false;
23741 /* Remove the this parameter from non-static member functions. If
23742 one is a non-static member function and the other is not a static
23743 member function, remove the first parameter from that function
23744 also. This situation occurs for operator functions where we
23745 locate both a member function (with this pointer) and non-member
23746 operator (with explicit first operand). */
23747 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23749 len
--; /* LEN is the number of significant arguments for DECL1 */
23750 args1
= TREE_CHAIN (args1
);
23751 if (!DECL_STATIC_FUNCTION_P (decl2
))
23752 args2
= TREE_CHAIN (args2
);
23754 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23756 args2
= TREE_CHAIN (args2
);
23757 if (!DECL_STATIC_FUNCTION_P (decl1
))
23760 args1
= TREE_CHAIN (args1
);
23764 /* If only one is a conversion operator, they are unordered. */
23765 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23768 /* Consider the return type for a conversion function */
23769 if (DECL_CONV_FN_P (decl1
))
23771 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23772 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23776 processing_template_decl
++;
23782 /* Stop when an ellipsis is seen. */
23783 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23785 tree arg1
= TREE_VALUE (args1
);
23786 tree arg2
= TREE_VALUE (args2
);
23787 int deduce1
, deduce2
;
23793 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23794 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23796 /* When both arguments are pack expansions, we need only
23797 unify the patterns themselves. */
23798 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23799 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23801 /* This is the last comparison we need to do. */
23805 /* DR 1847: If a particular P contains no template-parameters that
23806 participate in template argument deduction, that P is not used to
23807 determine the ordering. */
23808 if (!uses_deducible_template_parms (arg1
)
23809 && !uses_deducible_template_parms (arg2
))
23812 if (TYPE_REF_P (arg1
))
23814 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23815 arg1
= TREE_TYPE (arg1
);
23816 quals1
= cp_type_quals (arg1
);
23819 if (TYPE_REF_P (arg2
))
23821 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23822 arg2
= TREE_TYPE (arg2
);
23823 quals2
= cp_type_quals (arg2
);
23826 arg1
= TYPE_MAIN_VARIANT (arg1
);
23827 arg2
= TYPE_MAIN_VARIANT (arg2
);
23829 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23831 int i
, len2
= remaining_arguments (args2
);
23832 tree parmvec
= make_tree_vec (1);
23833 tree argvec
= make_tree_vec (len2
);
23836 /* Setup the parameter vector, which contains only ARG1. */
23837 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23839 /* Setup the argument vector, which contains the remaining
23841 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23842 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23844 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23845 argvec
, DEDUCE_EXACT
,
23846 /*subr=*/true, /*explain_p=*/false)
23849 /* We cannot deduce in the other direction, because ARG1 is
23850 a pack expansion but ARG2 is not. */
23853 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23855 int i
, len1
= remaining_arguments (args1
);
23856 tree parmvec
= make_tree_vec (1);
23857 tree argvec
= make_tree_vec (len1
);
23860 /* Setup the parameter vector, which contains only ARG1. */
23861 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23863 /* Setup the argument vector, which contains the remaining
23865 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23866 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23868 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23869 argvec
, DEDUCE_EXACT
,
23870 /*subr=*/true, /*explain_p=*/false)
23873 /* We cannot deduce in the other direction, because ARG2 is
23874 a pack expansion but ARG1 is not.*/
23880 /* The normal case, where neither argument is a pack
23882 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23883 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23885 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23886 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23890 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23891 arg2, then arg2 is not as specialized as arg1. */
23897 /* "If, for a given type, deduction succeeds in both directions
23898 (i.e., the types are identical after the transformations above)
23899 and both P and A were reference types (before being replaced with
23900 the type referred to above):
23901 - if the type from the argument template was an lvalue reference and
23902 the type from the parameter template was not, the argument type is
23903 considered to be more specialized than the other; otherwise,
23904 - if the type from the argument template is more cv-qualified
23905 than the type from the parameter template (as described above),
23906 the argument type is considered to be more specialized than the other;
23908 - neither type is more specialized than the other." */
23910 if (deduce1
&& deduce2
)
23912 if (ref1
&& ref2
&& ref1
!= ref2
)
23919 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23921 if ((quals1
& quals2
) == quals2
)
23923 if ((quals1
& quals2
) == quals1
)
23928 if (lose1
&& lose2
)
23929 /* We've failed to deduce something in either direction.
23930 These must be unordered. */
23935 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23936 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23937 /* We have already processed all of the arguments in our
23938 handing of the pack expansion type. */
23941 args1
= TREE_CHAIN (args1
);
23942 args2
= TREE_CHAIN (args2
);
23945 /* "In most cases, all template parameters must have values in order for
23946 deduction to succeed, but for partial ordering purposes a template
23947 parameter may remain without a value provided it is not used in the
23948 types being used for partial ordering."
23950 Thus, if we are missing any of the targs1 we need to substitute into
23951 origs1, then pat2 is not as specialized as pat1. This can happen when
23952 there is a nondeduced context. */
23953 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23955 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23958 processing_template_decl
--;
23960 /* If both deductions succeed, the partial ordering selects the more
23961 constrained template. */
23962 if (!lose1
&& !lose2
)
23964 int winner
= more_constrained (decl1
, decl2
);
23967 else if (winner
< 0)
23971 /* All things being equal, if the next argument is a pack expansion
23972 for one function but not for the other, prefer the
23973 non-variadic function. FIXME this is bogus; see c++/41958. */
23975 && args1
&& TREE_VALUE (args1
)
23976 && args2
&& TREE_VALUE (args2
))
23978 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23979 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23982 if (lose1
== lose2
)
23990 /* Determine which of two partial specializations of TMPL is more
23993 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23994 to the first partial specialization. The TREE_PURPOSE is the
23995 innermost set of template parameters for the partial
23996 specialization. PAT2 is similar, but for the second template.
23998 Return 1 if the first partial specialization is more specialized;
23999 -1 if the second is more specialized; 0 if neither is more
24002 See [temp.class.order] for information about determining which of
24003 two templates is more specialized. */
24006 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24010 bool any_deductions
= false;
24012 tree tmpl1
= TREE_VALUE (pat1
);
24013 tree tmpl2
= TREE_VALUE (pat2
);
24014 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24015 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24017 /* Just like what happens for functions, if we are ordering between
24018 different template specializations, we may encounter dependent
24019 types in the arguments, and we need our dependency check functions
24020 to behave correctly. */
24021 ++processing_template_decl
;
24022 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24026 any_deductions
= true;
24029 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24033 any_deductions
= true;
24035 --processing_template_decl
;
24037 /* If both deductions succeed, the partial ordering selects the more
24038 constrained template. */
24039 if (!winner
&& any_deductions
)
24040 winner
= more_constrained (tmpl1
, tmpl2
);
24042 /* In the case of a tie where at least one of the templates
24043 has a parameter pack at the end, the template with the most
24044 non-packed parameters wins. */
24047 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24048 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24050 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24051 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24052 int len1
= TREE_VEC_LENGTH (args1
);
24053 int len2
= TREE_VEC_LENGTH (args2
);
24055 /* We don't count the pack expansion at the end. */
24056 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24058 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24063 else if (len1
< len2
)
24070 /* Return the template arguments that will produce the function signature
24071 DECL from the function template FN, with the explicit template
24072 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24073 also match. Return NULL_TREE if no satisfactory arguments could be
24077 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24079 int ntparms
= DECL_NTPARMS (fn
);
24080 tree targs
= make_tree_vec (ntparms
);
24081 tree decl_type
= TREE_TYPE (decl
);
24082 tree decl_arg_types
;
24084 unsigned int nargs
, ix
;
24087 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24089 /* Never do unification on the 'this' parameter. */
24090 decl_arg_types
= skip_artificial_parms_for (decl
,
24091 TYPE_ARG_TYPES (decl_type
));
24093 nargs
= list_length (decl_arg_types
);
24094 args
= XALLOCAVEC (tree
, nargs
);
24095 for (arg
= decl_arg_types
, ix
= 0;
24096 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24097 arg
= TREE_CHAIN (arg
), ++ix
)
24098 args
[ix
] = TREE_VALUE (arg
);
24100 if (fn_type_unification (fn
, explicit_args
, targs
,
24102 (check_rettype
|| DECL_CONV_FN_P (fn
)
24103 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24104 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24105 /*explain_p=*/false,
24107 == error_mark_node
)
24113 /* Return the innermost template arguments that, when applied to a partial
24114 specialization SPEC_TMPL of TMPL, yield the ARGS.
24116 For example, suppose we have:
24118 template <class T, class U> struct S {};
24119 template <class T> struct S<T*, int> {};
24121 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24122 partial specialization and the ARGS will be {double*, int}. The resulting
24123 vector will be {double}, indicating that `T' is bound to `double'. */
24126 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24128 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24130 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24131 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24133 tree innermost_deduced_args
;
24135 innermost_deduced_args
= make_tree_vec (ntparms
);
24136 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24138 deduced_args
= copy_node (args
);
24139 SET_TMPL_ARGS_LEVEL (deduced_args
,
24140 TMPL_ARGS_DEPTH (deduced_args
),
24141 innermost_deduced_args
);
24144 deduced_args
= innermost_deduced_args
;
24146 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24148 if (unify (tparms
, deduced_args
,
24149 INNERMOST_TEMPLATE_ARGS (spec_args
),
24150 INNERMOST_TEMPLATE_ARGS (args
),
24151 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24154 for (i
= 0; i
< ntparms
; ++i
)
24155 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24157 if (!tried_array_deduction
)
24159 try_array_deduction (tparms
, innermost_deduced_args
,
24160 INNERMOST_TEMPLATE_ARGS (spec_args
));
24161 tried_array_deduction
= true;
24162 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24168 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24170 excessive_deduction_depth
= true;
24174 /* Verify that nondeduced template arguments agree with the type
24175 obtained from argument deduction.
24179 struct A { typedef int X; };
24180 template <class T, class U> struct C {};
24181 template <class T> struct C<T, typename T::X> {};
24183 Then with the instantiation `C<A, int>', we can deduce that
24184 `T' is `A' but unify () does not check whether `typename T::X'
24186 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24188 if (spec_args
!= error_mark_node
)
24189 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24190 INNERMOST_TEMPLATE_ARGS (spec_args
),
24191 tmpl
, tf_none
, false, false);
24193 pop_tinst_level ();
24195 if (spec_args
== error_mark_node
24196 /* We only need to check the innermost arguments; the other
24197 arguments will always agree. */
24198 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24199 INNERMOST_TEMPLATE_ARGS (args
)))
24202 /* Now that we have bindings for all of the template arguments,
24203 ensure that the arguments deduced for the template template
24204 parameters have compatible template parameter lists. See the use
24205 of template_template_parm_bindings_ok_p in fn_type_unification
24206 for more information. */
24207 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24210 return deduced_args
;
24213 // Compare two function templates T1 and T2 by deducing bindings
24214 // from one against the other. If both deductions succeed, compare
24215 // constraints to see which is more constrained.
24217 more_specialized_inst (tree t1
, tree t2
)
24222 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24228 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24234 // If both deductions succeed, then one may be more constrained.
24235 if (count
== 2 && fate
== 0)
24236 fate
= more_constrained (t1
, t2
);
24241 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24242 Return the TREE_LIST node with the most specialized template, if
24243 any. If there is no most specialized template, the error_mark_node
24246 Note that this function does not look at, or modify, the
24247 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24248 returned is one of the elements of INSTANTIATIONS, callers may
24249 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24250 and retrieve it from the value returned. */
24253 most_specialized_instantiation (tree templates
)
24257 ++processing_template_decl
;
24260 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24262 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24263 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24268 /* Equally specialized, move to next function. If there
24269 is no next function, nothing's most specialized. */
24270 fn
= TREE_CHAIN (fn
);
24278 /* Now verify that champ is better than everything earlier in the
24279 instantiation list. */
24280 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24281 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24288 processing_template_decl
--;
24291 return error_mark_node
;
24296 /* If DECL is a specialization of some template, return the most
24297 general such template. Otherwise, returns NULL_TREE.
24299 For example, given:
24301 template <class T> struct S { template <class U> void f(U); };
24303 if TMPL is `template <class U> void S<int>::f(U)' this will return
24304 the full template. This function will not trace past partial
24305 specializations, however. For example, given in addition:
24307 template <class T> struct S<T*> { template <class U> void f(U); };
24309 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24310 `template <class T> template <class U> S<T*>::f(U)'. */
24313 most_general_template (tree decl
)
24315 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24317 if (tree tinfo
= get_template_info (decl
))
24318 decl
= TI_TEMPLATE (tinfo
);
24319 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24320 template friend, or a FIELD_DECL for a capture pack. */
24321 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24325 /* Look for more and more general templates. */
24326 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24328 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24329 (See cp-tree.h for details.) */
24330 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24333 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24334 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24335 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24338 /* Stop if we run into an explicitly specialized class template. */
24339 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24340 && DECL_CONTEXT (decl
)
24341 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24344 decl
= DECL_TI_TEMPLATE (decl
);
24350 /* Return the most specialized of the template partial specializations
24351 which can produce TARGET, a specialization of some class or variable
24352 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24353 a TEMPLATE_DECL node corresponding to the partial specialization, while
24354 the TREE_PURPOSE is the set of template arguments that must be
24355 substituted into the template pattern in order to generate TARGET.
24357 If the choice of partial specialization is ambiguous, a diagnostic
24358 is issued, and the error_mark_node is returned. If there are no
24359 partial specializations matching TARGET, then NULL_TREE is
24360 returned, indicating that the primary template should be used. */
24363 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24365 tree list
= NULL_TREE
;
24370 tree outer_args
= NULL_TREE
;
24373 if (TYPE_P (target
))
24375 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24376 tmpl
= TI_TEMPLATE (tinfo
);
24377 args
= TI_ARGS (tinfo
);
24379 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24381 tmpl
= TREE_OPERAND (target
, 0);
24382 args
= TREE_OPERAND (target
, 1);
24384 else if (VAR_P (target
))
24386 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24387 tmpl
= TI_TEMPLATE (tinfo
);
24388 args
= TI_ARGS (tinfo
);
24391 gcc_unreachable ();
24393 tree main_tmpl
= most_general_template (tmpl
);
24395 /* For determining which partial specialization to use, only the
24396 innermost args are interesting. */
24397 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24399 outer_args
= strip_innermost_template_args (args
, 1);
24400 args
= INNERMOST_TEMPLATE_ARGS (args
);
24403 /* The caller hasn't called push_to_top_level yet, but we need
24404 get_partial_spec_bindings to be done in non-template context so that we'll
24405 fully resolve everything. */
24406 processing_template_decl_sentinel ptds
;
24408 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24411 tree spec_tmpl
= TREE_VALUE (t
);
24415 /* Substitute in the template args from the enclosing class. */
24416 ++processing_template_decl
;
24417 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24418 --processing_template_decl
;
24421 if (spec_tmpl
== error_mark_node
)
24422 return error_mark_node
;
24424 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24428 spec_args
= add_to_template_args (outer_args
, spec_args
);
24430 /* Keep the candidate only if the constraints are satisfied,
24431 or if we're not compiling with concepts. */
24433 || constraints_satisfied_p (spec_tmpl
, spec_args
))
24435 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
24436 TREE_TYPE (list
) = TREE_TYPE (t
);
24444 ambiguous_p
= false;
24447 t
= TREE_CHAIN (t
);
24448 for (; t
; t
= TREE_CHAIN (t
))
24450 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24457 t
= TREE_CHAIN (t
);
24460 ambiguous_p
= true;
24469 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24471 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24474 ambiguous_p
= true;
24482 char *spaces
= NULL
;
24483 if (!(complain
& tf_error
))
24484 return error_mark_node
;
24485 if (TYPE_P (target
))
24486 error ("ambiguous template instantiation for %q#T", target
);
24488 error ("ambiguous template instantiation for %q#D", target
);
24489 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24490 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24492 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24493 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24494 "%s %#qS", spaces
? spaces
: str
, subst
);
24495 spaces
= spaces
? spaces
: get_spaces (str
);
24498 return error_mark_node
;
24504 /* Explicitly instantiate DECL. */
24507 do_decl_instantiation (tree decl
, tree storage
)
24509 tree result
= NULL_TREE
;
24512 if (!decl
|| decl
== error_mark_node
)
24513 /* An error occurred, for which grokdeclarator has already issued
24514 an appropriate message. */
24516 else if (! DECL_LANG_SPECIFIC (decl
))
24518 error ("explicit instantiation of non-template %q#D", decl
);
24521 else if (DECL_DECLARED_CONCEPT_P (decl
))
24524 error ("explicit instantiation of variable concept %q#D", decl
);
24526 error ("explicit instantiation of function concept %q#D", decl
);
24530 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24531 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24533 if (VAR_P (decl
) && !var_templ
)
24535 /* There is an asymmetry here in the way VAR_DECLs and
24536 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24537 the latter, the DECL we get back will be marked as a
24538 template instantiation, and the appropriate
24539 DECL_TEMPLATE_INFO will be set up. This does not happen for
24540 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24541 should handle VAR_DECLs as it currently handles
24543 if (!DECL_CLASS_SCOPE_P (decl
))
24545 error ("%qD is not a static data member of a class template", decl
);
24548 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24549 if (!result
|| !VAR_P (result
))
24551 error ("no matching template for %qD found", decl
);
24554 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24556 error ("type %qT for explicit instantiation %qD does not match "
24557 "declared type %qT", TREE_TYPE (result
), decl
,
24562 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24564 error ("explicit instantiation of %q#D", decl
);
24570 /* Check for various error cases. Note that if the explicit
24571 instantiation is valid the RESULT will currently be marked as an
24572 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24573 until we get here. */
24575 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24577 /* DR 259 [temp.spec].
24579 Both an explicit instantiation and a declaration of an explicit
24580 specialization shall not appear in a program unless the explicit
24581 instantiation follows a declaration of the explicit specialization.
24583 For a given set of template parameters, if an explicit
24584 instantiation of a template appears after a declaration of an
24585 explicit specialization for that template, the explicit
24586 instantiation has no effect. */
24589 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24593 No program shall explicitly instantiate any template more
24596 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24597 the first instantiation was `extern' and the second is not,
24598 and EXTERN_P for the opposite case. */
24599 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24600 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24601 /* If an "extern" explicit instantiation follows an ordinary
24602 explicit instantiation, the template is instantiated. */
24606 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24608 error ("no matching template for %qD found", result
);
24611 else if (!DECL_TEMPLATE_INFO (result
))
24613 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24617 if (storage
== NULL_TREE
)
24619 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24621 if (cxx_dialect
== cxx98
)
24622 pedwarn (input_location
, OPT_Wpedantic
,
24623 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24628 error ("storage class %qD applied to template instantiation", storage
);
24630 check_explicit_instantiation_namespace (result
);
24631 mark_decl_instantiated (result
, extern_p
);
24633 instantiate_decl (result
, /*defer_ok=*/true,
24634 /*expl_inst_class_mem_p=*/false);
24638 mark_class_instantiated (tree t
, int extern_p
)
24640 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24641 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24642 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24643 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24646 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24647 rest_of_type_compilation (t
, 1);
24651 /* Called from do_type_instantiation through binding_table_foreach to
24652 do recursive instantiation for the type bound in ENTRY. */
24654 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24656 tree storage
= *(tree
*) data
;
24658 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24659 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24660 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24661 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24664 /* Perform an explicit instantiation of template class T. STORAGE, if
24665 non-null, is the RID for extern, inline or static. COMPLAIN is
24666 nonzero if this is called from the parser, zero if called recursively,
24667 since the standard is unclear (as detailed below). */
24670 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24675 int previous_instantiation_extern_p
= 0;
24677 if (TREE_CODE (t
) == TYPE_DECL
)
24680 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24683 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24685 error ("explicit instantiation of non-class template %qD", tmpl
);
24687 error ("explicit instantiation of non-template type %qT", t
);
24693 if (!COMPLETE_TYPE_P (t
))
24695 if (complain
& tf_error
)
24696 error ("explicit instantiation of %q#T before definition of template",
24701 if (storage
!= NULL_TREE
)
24703 if (storage
== ridpointers
[(int) RID_EXTERN
])
24705 if (cxx_dialect
== cxx98
)
24706 pedwarn (input_location
, OPT_Wpedantic
,
24707 "ISO C++ 1998 forbids the use of %<extern%> on "
24708 "explicit instantiations");
24711 pedwarn (input_location
, OPT_Wpedantic
,
24712 "ISO C++ forbids the use of %qE"
24713 " on explicit instantiations", storage
);
24715 if (storage
== ridpointers
[(int) RID_INLINE
])
24717 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24719 else if (storage
== ridpointers
[(int) RID_STATIC
])
24723 error ("storage class %qD applied to template instantiation",
24729 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24731 /* DR 259 [temp.spec].
24733 Both an explicit instantiation and a declaration of an explicit
24734 specialization shall not appear in a program unless the explicit
24735 instantiation follows a declaration of the explicit specialization.
24737 For a given set of template parameters, if an explicit
24738 instantiation of a template appears after a declaration of an
24739 explicit specialization for that template, the explicit
24740 instantiation has no effect. */
24743 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24747 No program shall explicitly instantiate any template more
24750 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24751 instantiation was `extern'. If EXTERN_P then the second is.
24752 These cases are OK. */
24753 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24755 if (!previous_instantiation_extern_p
&& !extern_p
24756 && (complain
& tf_error
))
24757 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24759 /* If we've already instantiated the template, just return now. */
24760 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24764 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24765 mark_class_instantiated (t
, extern_p
);
24770 /* In contrast to implicit instantiation, where only the
24771 declarations, and not the definitions, of members are
24772 instantiated, we have here:
24776 The explicit instantiation of a class template specialization
24777 implies the instantiation of all of its members not
24778 previously explicitly specialized in the translation unit
24779 containing the explicit instantiation.
24781 Of course, we can't instantiate member template classes, since we
24782 don't have any arguments for them. Note that the standard is
24783 unclear on whether the instantiation of the members are
24784 *explicit* instantiations or not. However, the most natural
24785 interpretation is that it should be an explicit
24787 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24789 || (TREE_CODE (fld
) == FUNCTION_DECL
24791 && user_provided_p (fld
)))
24792 && DECL_TEMPLATE_INSTANTIATION (fld
))
24794 mark_decl_instantiated (fld
, extern_p
);
24796 instantiate_decl (fld
, /*defer_ok=*/true,
24797 /*expl_inst_class_mem_p=*/true);
24800 if (CLASSTYPE_NESTED_UTDS (t
))
24801 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24802 bt_instantiate_type_proc
, &storage
);
24805 /* Given a function DECL, which is a specialization of TMPL, modify
24806 DECL to be a re-instantiation of TMPL with the same template
24807 arguments. TMPL should be the template into which tsubst'ing
24808 should occur for DECL, not the most general template.
24810 One reason for doing this is a scenario like this:
24813 void f(const T&, int i);
24815 void g() { f(3, 7); }
24818 void f(const T& t, const int i) { }
24820 Note that when the template is first instantiated, with
24821 instantiate_template, the resulting DECL will have no name for the
24822 first parameter, and the wrong type for the second. So, when we go
24823 to instantiate the DECL, we regenerate it. */
24826 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24828 /* The arguments used to instantiate DECL, from the most general
24832 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24834 /* Make sure that we can see identifiers, and compute access
24836 push_access_scope (decl
);
24838 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24846 args_depth
= TMPL_ARGS_DEPTH (args
);
24847 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24848 if (args_depth
> parms_depth
)
24849 args
= get_innermost_template_args (args
, parms_depth
);
24851 /* Instantiate a dynamic exception-specification. noexcept will be
24853 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24854 if (TREE_VALUE (raises
))
24856 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24857 args
, tf_error
, NULL_TREE
,
24858 /*defer_ok*/false);
24859 if (specs
&& specs
!= error_mark_node
)
24860 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24864 /* Merge parameter declarations. */
24865 decl_parm
= skip_artificial_parms_for (decl
,
24866 DECL_ARGUMENTS (decl
));
24868 = skip_artificial_parms_for (code_pattern
,
24869 DECL_ARGUMENTS (code_pattern
));
24870 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24875 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24876 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24877 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24879 parm_type
= type_decays_to (parm_type
);
24880 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24881 TREE_TYPE (decl_parm
) = parm_type
;
24882 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24883 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24885 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24886 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24888 decl_parm
= DECL_CHAIN (decl_parm
);
24889 pattern_parm
= DECL_CHAIN (pattern_parm
);
24891 /* Merge any parameters that match with the function parameter
24893 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24896 tree expanded_types
;
24897 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24898 the parameters in this function parameter pack. */
24899 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24900 args
, tf_error
, NULL_TREE
);
24901 len
= TREE_VEC_LENGTH (expanded_types
);
24902 for (i
= 0; i
< len
; i
++)
24907 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24908 /* Rename the parameter to include the index. */
24909 DECL_NAME (decl_parm
) =
24910 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24911 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24912 parm_type
= type_decays_to (parm_type
);
24913 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24914 TREE_TYPE (decl_parm
) = parm_type
;
24915 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24916 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24918 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24919 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24921 decl_parm
= DECL_CHAIN (decl_parm
);
24924 /* Merge additional specifiers from the CODE_PATTERN. */
24925 if (DECL_DECLARED_INLINE_P (code_pattern
)
24926 && !DECL_DECLARED_INLINE_P (decl
))
24927 DECL_DECLARED_INLINE_P (decl
) = 1;
24929 maybe_instantiate_noexcept (decl
, tf_error
);
24931 else if (VAR_P (decl
))
24933 start_lambda_scope (decl
);
24934 DECL_INITIAL (decl
) =
24935 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
24936 tf_error
, DECL_TI_TEMPLATE (decl
));
24937 finish_lambda_scope ();
24938 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24939 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24940 tf_error
, DECL_TI_TEMPLATE (decl
));
24943 gcc_unreachable ();
24945 pop_access_scope (decl
);
24948 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24949 substituted to get DECL. */
24952 template_for_substitution (tree decl
)
24954 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24956 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24957 for the instantiation. This is not always the most general
24958 template. Consider, for example:
24961 struct S { template <class U> void f();
24962 template <> void f<int>(); };
24964 and an instantiation of S<double>::f<int>. We want TD to be the
24965 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24966 while (/* An instantiation cannot have a definition, so we need a
24967 more general template. */
24968 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24969 /* We must also deal with friend templates. Given:
24971 template <class T> struct S {
24972 template <class U> friend void f() {};
24975 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24976 so far as the language is concerned, but that's still
24977 where we get the pattern for the instantiation from. On
24978 other hand, if the definition comes outside the class, say:
24980 template <class T> struct S {
24981 template <class U> friend void f();
24983 template <class U> friend void f() {}
24985 we don't need to look any further. That's what the check for
24986 DECL_INITIAL is for. */
24987 || (TREE_CODE (decl
) == FUNCTION_DECL
24988 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24989 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24991 /* The present template, TD, should not be a definition. If it
24992 were a definition, we should be using it! Note that we
24993 cannot restructure the loop to just keep going until we find
24994 a template with a definition, since that might go too far if
24995 a specialization was declared, but not defined. */
24997 /* Fetch the more general template. */
24998 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25004 /* Returns true if we need to instantiate this template instance even if we
25005 know we aren't going to emit it. */
25008 always_instantiate_p (tree decl
)
25010 /* We always instantiate inline functions so that we can inline them. An
25011 explicit instantiation declaration prohibits implicit instantiation of
25012 non-inline functions. With high levels of optimization, we would
25013 normally inline non-inline functions -- but we're not allowed to do
25014 that for "extern template" functions. Therefore, we check
25015 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25016 return ((TREE_CODE (decl
) == FUNCTION_DECL
25017 && (DECL_DECLARED_INLINE_P (decl
)
25018 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25019 /* And we need to instantiate static data members so that
25020 their initializers are available in integral constant
25023 && decl_maybe_constant_var_p (decl
)));
25026 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25027 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25028 error, true otherwise. */
25031 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25033 tree fntype
, spec
, noex
, clone
;
25035 /* Don't instantiate a noexcept-specification from template context. */
25036 if (processing_template_decl
25037 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25040 if (DECL_CLONED_FUNCTION_P (fn
))
25041 fn
= DECL_CLONED_FUNCTION (fn
);
25043 tree orig_fn
= NULL_TREE
;
25044 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25045 its FUNCTION_DECL for the rest of this function -- push_access_scope
25046 doesn't accept TEMPLATE_DECLs. */
25047 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25050 fn
= DECL_TEMPLATE_RESULT (fn
);
25053 fntype
= TREE_TYPE (fn
);
25054 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25056 if (!spec
|| !TREE_PURPOSE (spec
))
25059 noex
= TREE_PURPOSE (spec
);
25061 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25063 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25064 bool added
= false;
25065 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25067 spec
= get_defaulted_eh_spec (fn
, complain
);
25068 if (spec
== error_mark_node
)
25069 /* This might have failed because of an unparsed DMI, so
25070 let's try again later. */
25073 else if (!(added
= !fns
->add (fn
)))
25075 /* If hash_set::add returns true, the element was already there. */
25076 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25077 DECL_SOURCE_LOCATION (fn
));
25079 "exception specification of %qD depends on itself",
25081 spec
= noexcept_false_spec
;
25083 else if (push_tinst_level (fn
))
25085 push_to_top_level ();
25086 push_access_scope (fn
);
25087 push_deferring_access_checks (dk_no_deferred
);
25088 input_location
= DECL_SOURCE_LOCATION (fn
);
25090 /* If needed, set current_class_ptr for the benefit of
25091 tsubst_copy/PARM_DECL. */
25092 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25093 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25095 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25096 current_class_ptr
= NULL_TREE
;
25097 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25098 current_class_ptr
= this_parm
;
25101 /* If this function is represented by a TEMPLATE_DECL, then
25102 the deferred noexcept-specification might still contain
25103 dependent types, even after substitution. And we need the
25104 dependency check functions to work in build_noexcept_spec. */
25106 ++processing_template_decl
;
25108 /* Do deferred instantiation of the noexcept-specifier. */
25109 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25110 DEFERRED_NOEXCEPT_ARGS (noex
),
25111 tf_warning_or_error
, fn
,
25112 /*function_p=*/false,
25115 /* Build up the noexcept-specification. */
25116 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25119 --processing_template_decl
;
25121 pop_deferring_access_checks ();
25122 pop_access_scope (fn
);
25123 pop_tinst_level ();
25124 pop_from_top_level ();
25127 spec
= noexcept_false_spec
;
25132 if (spec
== error_mark_node
)
25134 /* This failed with a hard error, so let's go with false. */
25135 gcc_assert (seen_error ());
25136 spec
= noexcept_false_spec
;
25139 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25141 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25144 FOR_EACH_CLONE (clone
, fn
)
25146 if (TREE_TYPE (clone
) == fntype
)
25147 TREE_TYPE (clone
) = TREE_TYPE (fn
);
25149 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
25155 /* We're starting to process the function INST, an instantiation of PATTERN;
25156 add their parameters to local_specializations. */
25159 register_parameter_specializations (tree pattern
, tree inst
)
25161 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25162 tree spec_parm
= DECL_ARGUMENTS (inst
);
25163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25165 register_local_specialization (spec_parm
, tmpl_parm
);
25166 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25167 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25169 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25171 if (!DECL_PACK_P (tmpl_parm
))
25173 register_local_specialization (spec_parm
, tmpl_parm
);
25174 spec_parm
= DECL_CHAIN (spec_parm
);
25178 /* Register the (value) argument pack as a specialization of
25179 TMPL_PARM, then move on. */
25180 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25181 register_local_specialization (argpack
, tmpl_parm
);
25184 gcc_assert (!spec_parm
);
25187 /* Produce the definition of D, a _DECL generated from a template. If
25188 DEFER_OK is true, then we don't have to actually do the
25189 instantiation now; we just have to do it sometime. Normally it is
25190 an error if this is an explicit instantiation but D is undefined.
25191 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25192 instantiated class template. */
25195 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25197 tree tmpl
= DECL_TI_TEMPLATE (d
);
25204 bool pattern_defined
;
25205 location_t saved_loc
= input_location
;
25206 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25207 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25211 /* This function should only be used to instantiate templates for
25212 functions and static member variables. */
25213 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25215 /* A concept is never instantiated. */
25216 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25218 /* Variables are never deferred; if instantiation is required, they
25219 are instantiated right away. That allows for better code in the
25220 case that an expression refers to the value of the variable --
25221 if the variable has a constant value the referring expression can
25222 take advantage of that fact. */
25226 /* Don't instantiate cloned functions. Instead, instantiate the
25227 functions they cloned. */
25228 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25229 d
= DECL_CLONED_FUNCTION (d
);
25231 if (DECL_TEMPLATE_INSTANTIATED (d
)
25232 || (TREE_CODE (d
) == FUNCTION_DECL
25233 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25234 || DECL_TEMPLATE_SPECIALIZATION (d
))
25235 /* D has already been instantiated or explicitly specialized, so
25236 there's nothing for us to do here.
25238 It might seem reasonable to check whether or not D is an explicit
25239 instantiation, and, if so, stop here. But when an explicit
25240 instantiation is deferred until the end of the compilation,
25241 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25242 the instantiation. */
25245 /* Check to see whether we know that this template will be
25246 instantiated in some other file, as with "extern template"
25248 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25250 /* In general, we do not instantiate such templates. */
25251 if (external_p
&& !always_instantiate_p (d
))
25254 gen_tmpl
= most_general_template (tmpl
);
25255 gen_args
= DECL_TI_ARGS (d
);
25257 if (tmpl
!= gen_tmpl
)
25258 /* We should already have the extra args. */
25259 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25260 == TMPL_ARGS_DEPTH (gen_args
));
25261 /* And what's in the hash table should match D. */
25262 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
25263 || spec
== NULL_TREE
);
25265 /* This needs to happen before any tsubsting. */
25266 if (! push_tinst_level (d
))
25269 timevar_push (TV_TEMPLATE_INST
);
25271 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25272 for the instantiation. */
25273 td
= template_for_substitution (d
);
25278 /* Look up an explicit specialization, if any. */
25279 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25280 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25281 if (elt
&& elt
!= error_mark_node
)
25283 td
= TREE_VALUE (elt
);
25284 args
= TREE_PURPOSE (elt
);
25288 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25290 /* We should never be trying to instantiate a member of a class
25291 template or partial specialization. */
25292 gcc_assert (d
!= code_pattern
);
25294 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25295 || DECL_TEMPLATE_SPECIALIZATION (td
))
25296 /* In the case of a friend template whose definition is provided
25297 outside the class, we may have too many arguments. Drop the
25298 ones we don't need. The same is true for specializations. */
25299 args
= get_innermost_template_args
25300 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25302 if (TREE_CODE (d
) == FUNCTION_DECL
)
25304 deleted_p
= DECL_DELETED_FN (code_pattern
);
25305 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25306 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25307 || DECL_DEFAULTED_FN (code_pattern
)
25313 if (DECL_CLASS_SCOPE_P (code_pattern
))
25314 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25316 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25319 /* We may be in the middle of deferred access check. Disable it now. */
25320 push_deferring_access_checks (dk_no_deferred
);
25322 /* Unless an explicit instantiation directive has already determined
25323 the linkage of D, remember that a definition is available for
25325 if (pattern_defined
25326 && !DECL_INTERFACE_KNOWN (d
)
25327 && !DECL_NOT_REALLY_EXTERN (d
))
25328 mark_definable (d
);
25330 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25331 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25332 input_location
= DECL_SOURCE_LOCATION (d
);
25334 /* If D is a member of an explicitly instantiated class template,
25335 and no definition is available, treat it like an implicit
25337 if (!pattern_defined
&& expl_inst_class_mem_p
25338 && DECL_EXPLICIT_INSTANTIATION (d
))
25340 /* Leave linkage flags alone on instantiations with anonymous
25342 if (TREE_PUBLIC (d
))
25344 DECL_NOT_REALLY_EXTERN (d
) = 0;
25345 DECL_INTERFACE_KNOWN (d
) = 0;
25347 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25350 /* Defer all other templates, unless we have been explicitly
25351 forbidden from doing so. */
25352 if (/* If there is no definition, we cannot instantiate the
25355 /* If it's OK to postpone instantiation, do so. */
25357 /* If this is a static data member that will be defined
25358 elsewhere, we don't want to instantiate the entire data
25359 member, but we do want to instantiate the initializer so that
25360 we can substitute that elsewhere. */
25361 || (external_p
&& VAR_P (d
))
25362 /* Handle here a deleted function too, avoid generating
25363 its body (c++/61080). */
25366 /* The definition of the static data member is now required so
25367 we must substitute the initializer. */
25369 && !DECL_INITIAL (d
)
25370 && DECL_INITIAL (code_pattern
))
25374 bool const_init
= false;
25375 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25377 ns
= decl_namespace_context (d
);
25378 push_nested_namespace (ns
);
25380 push_nested_class (DECL_CONTEXT (d
));
25381 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25383 tf_warning_or_error
, NULL_TREE
,
25384 /*integral_constant_expression_p=*/false);
25385 /* If instantiating the initializer involved instantiating this
25386 again, don't call cp_finish_decl twice. */
25387 if (!DECL_INITIAL (d
))
25389 /* Make sure the initializer is still constant, in case of
25390 circular dependency (template/instantiate6.C). */
25392 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25393 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25394 /*asmspec_tree=*/NULL_TREE
,
25395 LOOKUP_ONLYCONVERTING
);
25398 pop_nested_class ();
25399 pop_nested_namespace (ns
);
25402 /* We restore the source position here because it's used by
25403 add_pending_template. */
25404 input_location
= saved_loc
;
25406 if (at_eof
&& !pattern_defined
25407 && DECL_EXPLICIT_INSTANTIATION (d
)
25408 && DECL_NOT_REALLY_EXTERN (d
))
25411 The definition of a non-exported function template, a
25412 non-exported member function template, or a non-exported
25413 member function or static data member of a class template
25414 shall be present in every translation unit in which it is
25415 explicitly instantiated. */
25416 permerror (input_location
, "explicit instantiation of %qD "
25417 "but no definition available", d
);
25419 /* If we're in unevaluated context, we just wanted to get the
25420 constant value; this isn't an odr use, so don't queue
25421 a full instantiation. */
25422 if (cp_unevaluated_operand
!= 0)
25424 /* ??? Historically, we have instantiated inline functions, even
25425 when marked as "extern template". */
25426 if (!(external_p
&& VAR_P (d
)))
25427 add_pending_template (d
);
25431 bool push_to_top
, nested
;
25433 fn_context
= decl_function_context (d
);
25434 if (LAMBDA_FUNCTION_P (d
))
25435 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25436 fn_context
= NULL_TREE
;
25437 nested
= current_function_decl
!= NULL_TREE
;
25438 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25440 vec
<tree
> omp_privatization_save
;
25442 save_omp_privatization_clauses (omp_privatization_save
);
25445 push_to_top_level ();
25448 gcc_assert (!processing_template_decl
);
25449 push_function_context ();
25450 cp_unevaluated_operand
= 0;
25451 c_inhibit_evaluation_warnings
= 0;
25454 /* Mark D as instantiated so that recursive calls to
25455 instantiate_decl do not try to instantiate it again. */
25456 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25458 /* Regenerate the declaration in case the template has been modified
25459 by a subsequent redeclaration. */
25460 regenerate_decl_from_template (d
, td
, args
);
25462 /* We already set the file and line above. Reset them now in case
25463 they changed as a result of calling regenerate_decl_from_template. */
25464 input_location
= DECL_SOURCE_LOCATION (d
);
25469 bool const_init
= false;
25471 /* Clear out DECL_RTL; whatever was there before may not be right
25472 since we've reset the type of the declaration. */
25473 SET_DECL_RTL (d
, NULL
);
25474 DECL_IN_AGGR_P (d
) = 0;
25476 /* The initializer is placed in DECL_INITIAL by
25477 regenerate_decl_from_template so we don't need to
25478 push/pop_access_scope again here. Pull it out so that
25479 cp_finish_decl can process it. */
25480 init
= DECL_INITIAL (d
);
25481 DECL_INITIAL (d
) = NULL_TREE
;
25482 DECL_INITIALIZED_P (d
) = 0;
25484 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25485 initializer. That function will defer actual emission until
25486 we have a chance to determine linkage. */
25487 DECL_EXTERNAL (d
) = 0;
25489 /* Enter the scope of D so that access-checking works correctly. */
25490 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25492 push_nested_class (DECL_CONTEXT (d
));
25494 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25495 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25496 ? LOOKUP_CONSTINIT
: 0);
25497 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25500 pop_nested_class ();
25502 if (variable_template_p (gen_tmpl
))
25503 note_variable_template_instantiation (d
);
25505 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25506 synthesize_method (d
);
25507 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25509 /* Set up the list of local specializations. */
25510 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25511 tree block
= NULL_TREE
;
25513 /* Set up context. */
25514 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25515 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25516 block
= push_stmt_list ();
25518 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25520 /* Some typedefs referenced from within the template code need to be
25521 access checked at template instantiation time, i.e now. These
25522 types were added to the template at parsing time. Let's get those
25523 and perform the access checks then. */
25524 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
25527 /* Create substitution entries for the parameters. */
25528 register_parameter_specializations (code_pattern
, d
);
25530 /* Substitute into the body of the function. */
25531 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25532 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25533 tf_warning_or_error
, tmpl
);
25536 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25537 tf_warning_or_error
, tmpl
,
25538 /*integral_constant_expression_p=*/false);
25540 /* Set the current input_location to the end of the function
25541 so that finish_function knows where we are. */
25543 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25545 /* Remember if we saw an infinite loop in the template. */
25546 current_function_infinite_loop
25547 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25550 /* Finish the function. */
25551 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25552 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25553 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25556 d
= finish_function (/*inline_p=*/false);
25557 expand_or_defer_fn (d
);
25560 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25561 cp_check_omp_declare_reduction (d
);
25564 /* We're not deferring instantiation any more. */
25565 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25568 pop_from_top_level ();
25570 pop_function_context ();
25573 restore_omp_privatization_clauses (omp_privatization_save
);
25576 pop_deferring_access_checks ();
25577 timevar_pop (TV_TEMPLATE_INST
);
25578 pop_tinst_level ();
25579 input_location
= saved_loc
;
25580 cp_unevaluated_operand
= saved_unevaluated_operand
;
25581 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25586 /* Run through the list of templates that we wish we could
25587 instantiate, and instantiate any we can. RETRIES is the
25588 number of times we retry pending template instantiation. */
25591 instantiate_pending_templates (int retries
)
25594 location_t saved_loc
= input_location
;
25596 /* Instantiating templates may trigger vtable generation. This in turn
25597 may require further template instantiations. We place a limit here
25598 to avoid infinite loop. */
25599 if (pending_templates
&& retries
>= max_tinst_depth
)
25601 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25603 fatal_error (input_location
,
25604 "template instantiation depth exceeds maximum of %d"
25605 " instantiating %q+D, possibly from virtual table generation"
25606 " (use %<-ftemplate-depth=%> to increase the maximum)",
25607 max_tinst_depth
, decl
);
25608 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25609 /* Pretend that we defined it. */
25610 DECL_INITIAL (decl
) = error_mark_node
;
25616 struct pending_template
**t
= &pending_templates
;
25617 struct pending_template
*last
= NULL
;
25621 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25622 bool complete
= false;
25624 if (TYPE_P (instantiation
))
25626 if (!COMPLETE_TYPE_P (instantiation
))
25628 instantiate_class_template (instantiation
);
25629 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25630 for (tree fld
= TYPE_FIELDS (instantiation
);
25631 fld
; fld
= TREE_CHAIN (fld
))
25633 || (TREE_CODE (fld
) == FUNCTION_DECL
25634 && !DECL_ARTIFICIAL (fld
)))
25635 && DECL_TEMPLATE_INSTANTIATION (fld
))
25636 instantiate_decl (fld
,
25637 /*defer_ok=*/false,
25638 /*expl_inst_class_mem_p=*/false);
25640 if (COMPLETE_TYPE_P (instantiation
))
25644 complete
= COMPLETE_TYPE_P (instantiation
);
25648 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25649 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25652 = instantiate_decl (instantiation
,
25653 /*defer_ok=*/false,
25654 /*expl_inst_class_mem_p=*/false);
25655 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25659 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25660 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25665 /* If INSTANTIATION has been instantiated, then we don't
25666 need to consider it again in the future. */
25667 struct pending_template
*drop
= *t
;
25669 set_refcount_ptr (drop
->tinst
);
25670 pending_template_freelist ().free (drop
);
25678 set_refcount_ptr (current_tinst_level
);
25680 last_pending_template
= last
;
25682 while (reconsider
);
25684 input_location
= saved_loc
;
25687 /* Substitute ARGVEC into T, which is a list of initializers for
25688 either base class or a non-static data member. The TREE_PURPOSEs
25689 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25690 instantiate_decl. */
25693 tsubst_initializer_list (tree t
, tree argvec
)
25695 tree inits
= NULL_TREE
;
25696 tree target_ctor
= error_mark_node
;
25698 for (; t
; t
= TREE_CHAIN (t
))
25702 tree expanded_bases
= NULL_TREE
;
25703 tree expanded_arguments
= NULL_TREE
;
25706 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25711 /* Expand the base class expansion type into separate base
25713 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25714 tf_warning_or_error
,
25716 if (expanded_bases
== error_mark_node
)
25719 /* We'll be building separate TREE_LISTs of arguments for
25721 len
= TREE_VEC_LENGTH (expanded_bases
);
25722 expanded_arguments
= make_tree_vec (len
);
25723 for (i
= 0; i
< len
; i
++)
25724 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25726 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25727 expand each argument in the TREE_VALUE of t. */
25728 expr
= make_node (EXPR_PACK_EXPANSION
);
25729 PACK_EXPANSION_LOCAL_P (expr
) = true;
25730 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25731 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25733 if (TREE_VALUE (t
) == void_type_node
)
25734 /* VOID_TYPE_NODE is used to indicate
25735 value-initialization. */
25737 for (i
= 0; i
< len
; i
++)
25738 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25742 /* Substitute parameter packs into each argument in the
25744 in_base_initializer
= 1;
25745 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25747 tree expanded_exprs
;
25749 /* Expand the argument. */
25750 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25752 = tsubst_pack_expansion (expr
, argvec
,
25753 tf_warning_or_error
,
25755 if (expanded_exprs
== error_mark_node
)
25758 /* Prepend each of the expanded expressions to the
25759 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25760 for (i
= 0; i
< len
; i
++)
25762 TREE_VEC_ELT (expanded_arguments
, i
) =
25763 tree_cons (NULL_TREE
,
25764 TREE_VEC_ELT (expanded_exprs
, i
),
25765 TREE_VEC_ELT (expanded_arguments
, i
));
25768 in_base_initializer
= 0;
25770 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25771 since we built them backwards. */
25772 for (i
= 0; i
< len
; i
++)
25774 TREE_VEC_ELT (expanded_arguments
, i
) =
25775 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25780 for (i
= 0; i
< len
; ++i
)
25782 if (expanded_bases
)
25784 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25785 decl
= expand_member_init (decl
);
25786 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25791 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25792 tf_warning_or_error
, NULL_TREE
);
25794 decl
= expand_member_init (decl
);
25795 if (decl
&& !DECL_P (decl
))
25796 in_base_initializer
= 1;
25798 init
= TREE_VALUE (t
);
25800 if (init
!= void_type_node
)
25801 init
= tsubst_expr (init
, argvec
,
25802 tf_warning_or_error
, NULL_TREE
,
25803 /*integral_constant_expression_p=*/false);
25804 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25805 /* If we had an initializer but it instantiated to nothing,
25806 value-initialize the object. This will only occur when
25807 the initializer was a pack expansion where the parameter
25808 packs used in that expansion were of length zero. */
25809 init
= void_type_node
;
25810 in_base_initializer
= 0;
25813 if (target_ctor
!= error_mark_node
25814 && init
!= error_mark_node
)
25816 error ("mem-initializer for %qD follows constructor delegation",
25820 /* Look for a target constructor. */
25821 if (init
!= error_mark_node
25822 && decl
&& CLASS_TYPE_P (decl
)
25823 && same_type_p (decl
, current_class_type
))
25825 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25828 error ("constructor delegation follows mem-initializer for %qD",
25829 TREE_PURPOSE (inits
));
25832 target_ctor
= init
;
25837 init
= build_tree_list (decl
, init
);
25838 TREE_CHAIN (init
) = inits
;
25846 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25849 set_current_access_from_decl (tree decl
)
25851 if (TREE_PRIVATE (decl
))
25852 current_access_specifier
= access_private_node
;
25853 else if (TREE_PROTECTED (decl
))
25854 current_access_specifier
= access_protected_node
;
25856 current_access_specifier
= access_public_node
;
25859 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25860 is the instantiation (which should have been created with
25861 start_enum) and ARGS are the template arguments to use. */
25864 tsubst_enum (tree tag
, tree newtag
, tree args
)
25868 if (SCOPED_ENUM_P (newtag
))
25869 begin_scope (sk_scoped_enum
, newtag
);
25871 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25876 decl
= TREE_VALUE (e
);
25877 /* Note that in a template enum, the TREE_VALUE is the
25878 CONST_DECL, not the corresponding INTEGER_CST. */
25879 value
= tsubst_expr (DECL_INITIAL (decl
),
25880 args
, tf_warning_or_error
, NULL_TREE
,
25881 /*integral_constant_expression_p=*/true);
25883 /* Give this enumeration constant the correct access. */
25884 set_current_access_from_decl (decl
);
25886 /* Actually build the enumerator itself. Here we're assuming that
25887 enumerators can't have dependent attributes. */
25888 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25889 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25892 if (SCOPED_ENUM_P (newtag
))
25895 finish_enum_value_list (newtag
);
25896 finish_enum (newtag
);
25898 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25899 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25902 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25903 its type -- but without substituting the innermost set of template
25904 arguments. So, innermost set of template parameters will appear in
25908 get_mostly_instantiated_function_type (tree decl
)
25910 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25911 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
25914 /* Return truthvalue if we're processing a template different from
25915 the last one involved in diagnostics. */
25917 problematic_instantiation_changed (void)
25919 return current_tinst_level
!= last_error_tinst_level
;
25922 /* Remember current template involved in diagnostics. */
25924 record_last_problematic_instantiation (void)
25926 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
25929 struct tinst_level
*
25930 current_instantiation (void)
25932 return current_tinst_level
;
25935 /* Return TRUE if current_function_decl is being instantiated, false
25939 instantiating_current_function_p (void)
25941 return (current_instantiation ()
25942 && (current_instantiation ()->maybe_get_node ()
25943 == current_function_decl
));
25946 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25947 type. Return false for ok, true for disallowed. Issue error and
25948 inform messages under control of COMPLAIN. */
25951 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25953 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25955 else if (TYPE_PTR_P (type
))
25957 else if (TYPE_REF_P (type
)
25958 && !TYPE_REF_IS_RVALUE (type
))
25960 else if (TYPE_PTRMEM_P (type
))
25962 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25964 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx2a
)
25966 if (complain
& tf_error
)
25967 error ("non-type template parameters of deduced class type only "
25968 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25973 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25975 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25977 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25979 /* A bound template template parm could later be instantiated to have a valid
25980 nontype parm type via an alias template. */
25981 else if (cxx_dialect
>= cxx11
25982 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25984 else if (CLASS_TYPE_P (type
))
25986 if (cxx_dialect
< cxx2a
)
25988 if (complain
& tf_error
)
25989 error ("non-type template parameters of class type only available "
25990 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25993 if (dependent_type_p (type
))
25995 if (!complete_type_or_else (type
, NULL_TREE
))
25997 if (!structural_type_p (type
))
25999 if (complain
& tf_error
)
26001 auto_diagnostic_group d
;
26002 error ("%qT is not a valid type for a template non-type "
26003 "parameter because it is not structural", type
);
26004 structural_type_p (type
, true);
26011 if (complain
& tf_error
)
26013 if (type
== error_mark_node
)
26014 inform (input_location
, "invalid template non-type parameter");
26016 error ("%q#T is not a valid type for a template non-type parameter",
26022 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26023 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26026 dependent_type_p_r (tree type
)
26032 A type is dependent if it is:
26034 -- a template parameter. Template template parameters are types
26035 for us (since TYPE_P holds true for them) so we handle
26037 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26038 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26040 /* -- a qualified-id with a nested-name-specifier which contains a
26041 class-name that names a dependent type or whose unqualified-id
26042 names a dependent type. */
26043 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26046 /* An alias template specialization can be dependent even if the
26047 resulting type is not. */
26048 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26051 /* -- a cv-qualified type where the cv-unqualified type is
26053 No code is necessary for this bullet; the code below handles
26054 cv-qualified types, and we don't want to strip aliases with
26055 TYPE_MAIN_VARIANT because of DR 1558. */
26056 /* -- a compound type constructed from any dependent type. */
26057 if (TYPE_PTRMEM_P (type
))
26058 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26059 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26061 else if (INDIRECT_TYPE_P (type
))
26062 return dependent_type_p (TREE_TYPE (type
));
26063 else if (FUNC_OR_METHOD_TYPE_P (type
))
26067 if (dependent_type_p (TREE_TYPE (type
)))
26069 for (arg_type
= TYPE_ARG_TYPES (type
);
26071 arg_type
= TREE_CHAIN (arg_type
))
26072 if (dependent_type_p (TREE_VALUE (arg_type
)))
26074 if (cxx_dialect
>= cxx17
)
26075 /* A value-dependent noexcept-specifier makes the type dependent. */
26076 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26077 if (tree noex
= TREE_PURPOSE (spec
))
26078 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26079 affect overload resolution and treating it as dependent breaks
26080 things. Same for an unparsed noexcept expression. */
26081 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26082 && TREE_CODE (noex
) != DEFERRED_PARSE
26083 && value_dependent_expression_p (noex
))
26087 /* -- an array type constructed from any dependent type or whose
26088 size is specified by a constant expression that is
26091 We checked for type- and value-dependence of the bounds in
26092 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26093 if (TREE_CODE (type
) == ARRAY_TYPE
)
26095 if (TYPE_DOMAIN (type
)
26096 && dependent_type_p (TYPE_DOMAIN (type
)))
26098 return dependent_type_p (TREE_TYPE (type
));
26101 /* -- a template-id in which either the template name is a template
26103 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26105 /* ... or any of the template arguments is a dependent type or
26106 an expression that is type-dependent or value-dependent. */
26107 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26108 && (any_dependent_template_arguments_p
26109 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26112 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26113 dependent; if the argument of the `typeof' expression is not
26114 type-dependent, then it should already been have resolved. */
26115 if (TREE_CODE (type
) == TYPEOF_TYPE
26116 || TREE_CODE (type
) == DECLTYPE_TYPE
26117 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26120 /* A template argument pack is dependent if any of its packed
26122 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26124 tree args
= ARGUMENT_PACK_ARGS (type
);
26125 int i
, len
= TREE_VEC_LENGTH (args
);
26126 for (i
= 0; i
< len
; ++i
)
26127 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26131 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26132 be template parameters. */
26133 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26136 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26139 /* The standard does not specifically mention types that are local
26140 to template functions or local classes, but they should be
26141 considered dependent too. For example:
26143 template <int I> void f() {
26148 The size of `E' cannot be known until the value of `I' has been
26149 determined. Therefore, `E' must be considered dependent. */
26150 scope
= TYPE_CONTEXT (type
);
26151 if (scope
&& TYPE_P (scope
))
26152 return dependent_type_p (scope
);
26153 /* Don't use type_dependent_expression_p here, as it can lead
26154 to infinite recursion trying to determine whether a lambda
26155 nested in a lambda is dependent (c++/47687). */
26156 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26157 && DECL_LANG_SPECIFIC (scope
)
26158 && DECL_TEMPLATE_INFO (scope
)
26159 && (any_dependent_template_arguments_p
26160 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26163 /* Other types are non-dependent. */
26167 /* Returns TRUE if TYPE is dependent, in the sense of
26168 [temp.dep.type]. Note that a NULL type is considered dependent. */
26171 dependent_type_p (tree type
)
26173 /* If there are no template parameters in scope, then there can't be
26174 any dependent types. */
26175 if (!processing_template_decl
)
26177 /* If we are not processing a template, then nobody should be
26178 providing us with a dependent type. */
26180 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26184 /* If the type is NULL, we have not computed a type for the entity
26185 in question; in that case, the type is dependent. */
26189 /* Erroneous types can be considered non-dependent. */
26190 if (type
== error_mark_node
)
26193 /* Getting here with global_type_node means we improperly called this
26194 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26195 gcc_checking_assert (type
!= global_type_node
);
26197 /* If we have not already computed the appropriate value for TYPE,
26199 if (!TYPE_DEPENDENT_P_VALID (type
))
26201 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26202 TYPE_DEPENDENT_P_VALID (type
) = 1;
26205 return TYPE_DEPENDENT_P (type
);
26208 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26209 lookup. In other words, a dependent type that is not the current
26213 dependent_scope_p (tree scope
)
26215 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26216 && !currently_open_class (scope
));
26219 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26220 an unknown base of 'this' (and is therefore instantiation-dependent). */
26223 unknown_base_ref_p (tree t
)
26225 if (!current_class_ptr
)
26228 tree mem
= TREE_OPERAND (t
, 1);
26229 if (shared_member_p (mem
))
26232 tree cur
= current_nonlambda_class_type ();
26233 if (!any_dependent_bases_p (cur
))
26236 tree ctx
= TREE_OPERAND (t
, 0);
26237 if (DERIVED_FROM_P (ctx
, cur
))
26243 /* T is a SCOPE_REF; return whether we need to consider it
26244 instantiation-dependent so that we can check access at instantiation
26245 time even though we know which member it resolves to. */
26248 instantiation_dependent_scope_ref_p (tree t
)
26250 if (DECL_P (TREE_OPERAND (t
, 1))
26251 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26252 && !unknown_base_ref_p (t
)
26253 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26254 TREE_OPERAND (t
, 1)))
26260 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26261 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26264 /* Note that this predicate is not appropriate for general expressions;
26265 only constant expressions (that satisfy potential_constant_expression)
26266 can be tested for value dependence. */
26269 value_dependent_expression_p (tree expression
)
26271 if (!processing_template_decl
|| expression
== NULL_TREE
)
26274 /* A type-dependent expression is also value-dependent. */
26275 if (type_dependent_expression_p (expression
))
26278 switch (TREE_CODE (expression
))
26281 /* A dependent member function of the current instantiation. */
26282 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26284 case FUNCTION_DECL
:
26285 /* A dependent member function of the current instantiation. */
26286 if (DECL_CLASS_SCOPE_P (expression
)
26287 && dependent_type_p (DECL_CONTEXT (expression
)))
26291 case IDENTIFIER_NODE
:
26292 /* A name that has not been looked up -- must be dependent. */
26295 case TEMPLATE_PARM_INDEX
:
26296 /* A non-type template parm. */
26300 /* A non-type template parm. */
26301 if (DECL_TEMPLATE_PARM_P (expression
))
26303 return value_dependent_expression_p (DECL_INITIAL (expression
));
26306 /* A constant with literal type and is initialized
26307 with an expression that is value-dependent. */
26308 if (DECL_DEPENDENT_INIT_P (expression
)
26309 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26310 || TYPE_REF_P (TREE_TYPE (expression
)))
26312 if (DECL_HAS_VALUE_EXPR_P (expression
))
26314 tree value_expr
= DECL_VALUE_EXPR (expression
);
26315 if (value_dependent_expression_p (value_expr
)
26316 /* __PRETTY_FUNCTION__ inside a template function is dependent
26317 on the name of the function. */
26318 || (DECL_PRETTY_FUNCTION_P (expression
)
26319 /* It might be used in a template, but not a template
26320 function, in which case its DECL_VALUE_EXPR will be
26322 && value_expr
== error_mark_node
))
26327 case DYNAMIC_CAST_EXPR
:
26328 case STATIC_CAST_EXPR
:
26329 case CONST_CAST_EXPR
:
26330 case REINTERPRET_CAST_EXPR
:
26332 case IMPLICIT_CONV_EXPR
:
26333 /* These expressions are value-dependent if the type to which
26334 the cast occurs is dependent or the expression being casted
26335 is value-dependent. */
26337 tree type
= TREE_TYPE (expression
);
26339 if (dependent_type_p (type
))
26342 /* A functional cast has a list of operands. */
26343 expression
= TREE_OPERAND (expression
, 0);
26346 /* If there are no operands, it must be an expression such
26347 as "int()". This should not happen for aggregate types
26348 because it would form non-constant expressions. */
26349 gcc_assert (cxx_dialect
>= cxx11
26350 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26355 if (TREE_CODE (expression
) == TREE_LIST
)
26356 return any_value_dependent_elements_p (expression
);
26358 return value_dependent_expression_p (expression
);
26362 if (SIZEOF_EXPR_TYPE_P (expression
))
26363 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26367 /* A `sizeof' expression is value-dependent if the operand is
26368 type-dependent or is a pack expansion. */
26369 expression
= TREE_OPERAND (expression
, 0);
26370 if (PACK_EXPANSION_P (expression
))
26372 else if (TYPE_P (expression
))
26373 return dependent_type_p (expression
);
26374 return instantiation_dependent_uneval_expression_p (expression
);
26376 case AT_ENCODE_EXPR
:
26377 /* An 'encode' expression is value-dependent if the operand is
26379 expression
= TREE_OPERAND (expression
, 0);
26380 return dependent_type_p (expression
);
26382 case NOEXCEPT_EXPR
:
26383 expression
= TREE_OPERAND (expression
, 0);
26384 return instantiation_dependent_uneval_expression_p (expression
);
26387 /* All instantiation-dependent expressions should also be considered
26388 value-dependent. */
26389 return instantiation_dependent_scope_ref_p (expression
);
26391 case COMPONENT_REF
:
26392 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26393 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26395 case NONTYPE_ARGUMENT_PACK
:
26396 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26397 is value-dependent. */
26399 tree values
= ARGUMENT_PACK_ARGS (expression
);
26400 int i
, len
= TREE_VEC_LENGTH (values
);
26402 for (i
= 0; i
< len
; ++i
)
26403 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26411 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26413 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26419 if (TREE_CODE (type2
) != TREE_LIST
)
26420 return dependent_type_p (type2
);
26422 for (; type2
; type2
= TREE_CHAIN (type2
))
26423 if (dependent_type_p (TREE_VALUE (type2
)))
26430 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26431 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26434 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26435 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26439 tree op
= TREE_OPERAND (expression
, 0);
26440 return (value_dependent_expression_p (op
)
26441 || has_value_dependent_address (op
));
26444 case REQUIRES_EXPR
:
26445 /* Treat all requires-expressions as value-dependent so
26446 we don't try to fold them. */
26450 return dependent_type_p (TREE_OPERAND (expression
, 0));
26454 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26456 tree fn
= get_callee_fndecl (expression
);
26458 nargs
= call_expr_nargs (expression
);
26459 for (i
= 0; i
< nargs
; ++i
)
26461 tree op
= CALL_EXPR_ARG (expression
, i
);
26462 /* In a call to a constexpr member function, look through the
26463 implicit ADDR_EXPR on the object argument so that it doesn't
26464 cause the call to be considered value-dependent. We also
26465 look through it in potential_constant_expression. */
26466 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26467 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26468 && TREE_CODE (op
) == ADDR_EXPR
)
26469 op
= TREE_OPERAND (op
, 0);
26470 if (value_dependent_expression_p (op
))
26476 case TEMPLATE_ID_EXPR
:
26477 return concept_definition_p (TREE_OPERAND (expression
, 0));
26483 if (dependent_type_p (TREE_TYPE (expression
)))
26485 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26486 if (value_dependent_expression_p (val
))
26492 /* Treat a GNU statement expression as dependent to avoid crashing
26493 under instantiate_non_dependent_expr; it can't be constant. */
26497 /* A constant expression is value-dependent if any subexpression is
26498 value-dependent. */
26499 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26501 case tcc_reference
:
26503 case tcc_comparison
:
26505 case tcc_expression
:
26508 int i
, len
= cp_tree_operand_length (expression
);
26510 for (i
= 0; i
< len
; i
++)
26512 tree t
= TREE_OPERAND (expression
, i
);
26514 /* In some cases, some of the operands may be missing.
26515 (For example, in the case of PREDECREMENT_EXPR, the
26516 amount to increment by may be missing.) That doesn't
26517 make the expression dependent. */
26518 if (t
&& value_dependent_expression_p (t
))
26529 /* The expression is not value-dependent. */
26533 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26534 [temp.dep.expr]. Note that an expression with no type is
26535 considered dependent. Other parts of the compiler arrange for an
26536 expression with type-dependent subexpressions to have no type, so
26537 this function doesn't have to be fully recursive. */
26540 type_dependent_expression_p (tree expression
)
26542 if (!processing_template_decl
)
26545 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26548 STRIP_ANY_LOCATION_WRAPPER (expression
);
26550 /* An unresolved name is always dependent. */
26551 if (identifier_p (expression
)
26552 || TREE_CODE (expression
) == USING_DECL
26553 || TREE_CODE (expression
) == WILDCARD_DECL
)
26556 /* A lambda-expression in template context is dependent. dependent_type_p is
26557 true for a lambda in the scope of a class or function template, but that
26558 doesn't cover all template contexts, like a default template argument. */
26559 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26562 /* A fold expression is type-dependent. */
26563 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26564 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26565 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26566 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26569 /* Some expression forms are never type-dependent. */
26570 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
26571 || TREE_CODE (expression
) == SIZEOF_EXPR
26572 || TREE_CODE (expression
) == ALIGNOF_EXPR
26573 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26574 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26575 || TREE_CODE (expression
) == TRAIT_EXPR
26576 || TREE_CODE (expression
) == TYPEID_EXPR
26577 || TREE_CODE (expression
) == DELETE_EXPR
26578 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26579 || TREE_CODE (expression
) == THROW_EXPR
26580 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26583 /* The types of these expressions depends only on the type to which
26584 the cast occurs. */
26585 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26586 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26587 || TREE_CODE (expression
) == CONST_CAST_EXPR
26588 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26589 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26590 || TREE_CODE (expression
) == CAST_EXPR
)
26591 return dependent_type_p (TREE_TYPE (expression
));
26593 /* The types of these expressions depends only on the type created
26594 by the expression. */
26595 if (TREE_CODE (expression
) == NEW_EXPR
26596 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26598 /* For NEW_EXPR tree nodes created inside a template, either
26599 the object type itself or a TREE_LIST may appear as the
26601 tree type
= TREE_OPERAND (expression
, 1);
26602 if (TREE_CODE (type
) == TREE_LIST
)
26603 /* This is an array type. We need to check array dimensions
26605 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26606 || value_dependent_expression_p
26607 (TREE_OPERAND (TREE_VALUE (type
), 1));
26609 return dependent_type_p (type
);
26612 if (TREE_CODE (expression
) == SCOPE_REF
)
26614 tree scope
= TREE_OPERAND (expression
, 0);
26615 tree name
= TREE_OPERAND (expression
, 1);
26617 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26618 contains an identifier associated by name lookup with one or more
26619 declarations declared with a dependent type, or...a
26620 nested-name-specifier or qualified-id that names a member of an
26621 unknown specialization. */
26622 return (type_dependent_expression_p (name
)
26623 || dependent_scope_p (scope
));
26626 if (TREE_CODE (expression
) == TEMPLATE_DECL
26627 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26628 return uses_outer_template_parms (expression
);
26630 if (TREE_CODE (expression
) == STMT_EXPR
)
26631 expression
= stmt_expr_value_expr (expression
);
26633 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26640 if (type_dependent_expression_p (elt
))
26646 /* A static data member of the current instantiation with incomplete
26647 array type is type-dependent, as the definition and specializations
26648 can have different bounds. */
26649 if (VAR_P (expression
)
26650 && DECL_CLASS_SCOPE_P (expression
)
26651 && dependent_type_p (DECL_CONTEXT (expression
))
26652 && VAR_HAD_UNKNOWN_BOUND (expression
))
26655 /* An array of unknown bound depending on a variadic parameter, eg:
26657 template<typename... Args>
26658 void foo (Args... args)
26660 int arr[] = { args... };
26663 template<int... vals>
26666 int arr[] = { vals... };
26669 If the array has no length and has an initializer, it must be that
26670 we couldn't determine its length in cp_complete_array_type because
26671 it is dependent. */
26672 if (VAR_P (expression
)
26673 && TREE_TYPE (expression
) != NULL_TREE
26674 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26675 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26676 && DECL_INITIAL (expression
))
26679 /* A function or variable template-id is type-dependent if it has any
26680 dependent template arguments. */
26681 if (VAR_OR_FUNCTION_DECL_P (expression
)
26682 && DECL_LANG_SPECIFIC (expression
)
26683 && DECL_TEMPLATE_INFO (expression
))
26685 /* Consider the innermost template arguments, since those are the ones
26686 that come from the template-id; the template arguments for the
26687 enclosing class do not make it type-dependent unless they are used in
26688 the type of the decl. */
26689 if (instantiates_primary_template_p (expression
)
26690 && (any_dependent_template_arguments_p
26691 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26695 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26696 type-dependent. Checking this is important for functions with auto return
26697 type, which looks like a dependent type. */
26698 if (TREE_CODE (expression
) == FUNCTION_DECL
26699 && !(DECL_CLASS_SCOPE_P (expression
)
26700 && dependent_type_p (DECL_CONTEXT (expression
)))
26701 && !(DECL_LANG_SPECIFIC (expression
)
26702 && DECL_FRIEND_P (expression
)
26703 && (!DECL_FRIEND_CONTEXT (expression
)
26704 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26705 && !DECL_LOCAL_FUNCTION_P (expression
))
26707 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26708 || undeduced_auto_decl (expression
));
26712 /* Always dependent, on the number of arguments if nothing else. */
26713 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26716 if (TREE_TYPE (expression
) == unknown_type_node
)
26718 if (TREE_CODE (expression
) == ADDR_EXPR
)
26719 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26720 if (TREE_CODE (expression
) == COMPONENT_REF
26721 || TREE_CODE (expression
) == OFFSET_REF
)
26723 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26725 expression
= TREE_OPERAND (expression
, 1);
26726 if (identifier_p (expression
))
26729 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26730 if (TREE_CODE (expression
) == SCOPE_REF
)
26733 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26734 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
26735 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
26738 if (BASELINK_P (expression
))
26740 if (BASELINK_OPTYPE (expression
)
26741 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26743 expression
= BASELINK_FUNCTIONS (expression
);
26746 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26748 if (any_dependent_template_arguments_p
26749 (TREE_OPERAND (expression
, 1)))
26751 expression
= TREE_OPERAND (expression
, 0);
26752 if (identifier_p (expression
))
26756 gcc_assert (OVL_P (expression
));
26758 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26759 if (type_dependent_expression_p (*iter
))
26765 /* The type of a non-type template parm declared with a placeholder type
26766 depends on the corresponding template argument, even though
26767 placeholders are not normally considered dependent. */
26768 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26769 && is_auto (TREE_TYPE (expression
)))
26772 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26774 /* Dependent type attributes might not have made it from the decl to
26776 if (DECL_P (expression
)
26777 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26780 return (dependent_type_p (TREE_TYPE (expression
)));
26783 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26784 type-dependent if the expression refers to a member of the current
26785 instantiation and the type of the referenced member is dependent, or the
26786 class member access expression refers to a member of an unknown
26789 This function returns true if the OBJECT in such a class member access
26790 expression is of an unknown specialization. */
26793 type_dependent_object_expression_p (tree object
)
26795 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26797 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26799 tree scope
= TREE_TYPE (object
);
26800 return (!scope
|| dependent_scope_p (scope
));
26803 /* walk_tree callback function for instantiation_dependent_expression_p,
26804 below. Returns non-zero if a dependent subexpression is found. */
26807 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26812 /* We don't have to worry about decltype currently because decltype
26813 of an instantiation-dependent expr is a dependent type. This
26814 might change depending on the resolution of DR 1172. */
26815 *walk_subtrees
= false;
26818 enum tree_code code
= TREE_CODE (*tp
);
26821 /* Don't treat an argument list as dependent just because it has no
26825 case NONTYPE_ARGUMENT_PACK
:
26828 case TEMPLATE_PARM_INDEX
:
26829 if (dependent_type_p (TREE_TYPE (*tp
)))
26831 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26833 /* We'll check value-dependence separately. */
26836 /* Handle expressions with type operands. */
26840 case AT_ENCODE_EXPR
:
26842 tree op
= TREE_OPERAND (*tp
, 0);
26843 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26844 op
= TREE_TYPE (op
);
26847 if (dependent_type_p (op
))
26851 *walk_subtrees
= false;
26858 case COMPONENT_REF
:
26859 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26860 /* In a template, finish_class_member_access_expr creates a
26861 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26862 type-dependent, so that we can check access control at
26863 instantiation time (PR 42277). See also Core issue 1273. */
26868 if (instantiation_dependent_scope_ref_p (*tp
))
26873 /* Treat statement-expressions as dependent. */
26877 /* Treat requires-expressions as dependent. */
26878 case REQUIRES_EXPR
:
26882 /* Treat concept checks as dependent. */
26883 if (concept_check_p (*tp
))
26887 case TEMPLATE_ID_EXPR
:
26888 /* Treat concept checks as dependent. */
26889 if (concept_check_p (*tp
))
26894 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26902 if (type_dependent_expression_p (*tp
))
26908 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26909 sense defined by the ABI:
26911 "An expression is instantiation-dependent if it is type-dependent
26912 or value-dependent, or it has a subexpression that is type-dependent
26913 or value-dependent."
26915 Except don't actually check value-dependence for unevaluated expressions,
26916 because in sizeof(i) we don't care about the value of i. Checking
26917 type-dependence will in turn check value-dependence of array bounds/template
26918 arguments as needed. */
26921 instantiation_dependent_uneval_expression_p (tree expression
)
26925 if (!processing_template_decl
)
26928 if (expression
== error_mark_node
)
26931 result
= cp_walk_tree_without_duplicates (&expression
,
26932 instantiation_dependent_r
, NULL
);
26933 return result
!= NULL_TREE
;
26936 /* As above, but also check value-dependence of the expression as a whole. */
26939 instantiation_dependent_expression_p (tree expression
)
26941 return (instantiation_dependent_uneval_expression_p (expression
)
26942 || value_dependent_expression_p (expression
));
26945 /* Like type_dependent_expression_p, but it also works while not processing
26946 a template definition, i.e. during substitution or mangling. */
26949 type_dependent_expression_p_push (tree expr
)
26952 ++processing_template_decl
;
26953 b
= type_dependent_expression_p (expr
);
26954 --processing_template_decl
;
26958 /* Returns TRUE if ARGS contains a type-dependent expression. */
26961 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
26966 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
26968 if (type_dependent_expression_p (arg
))
26974 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26975 expressions) contains any type-dependent expressions. */
26978 any_type_dependent_elements_p (const_tree list
)
26980 for (; list
; list
= TREE_CHAIN (list
))
26981 if (type_dependent_expression_p (TREE_VALUE (list
)))
26987 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26988 expressions) contains any value-dependent expressions. */
26991 any_value_dependent_elements_p (const_tree list
)
26993 for (; list
; list
= TREE_CHAIN (list
))
26994 if (value_dependent_expression_p (TREE_VALUE (list
)))
27000 /* Returns TRUE if the ARG (a template argument) is dependent. */
27003 dependent_template_arg_p (tree arg
)
27005 if (!processing_template_decl
)
27008 /* Assume a template argument that was wrongly written by the user
27009 is dependent. This is consistent with what
27010 any_dependent_template_arguments_p [that calls this function]
27012 if (!arg
|| arg
== error_mark_node
)
27015 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27016 arg
= argument_pack_select_arg (arg
);
27018 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27020 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27022 if (DECL_TEMPLATE_PARM_P (arg
))
27024 /* A member template of a dependent class is not necessarily
27025 type-dependent, but it is a dependent template argument because it
27026 will be a member of an unknown specialization to that template. */
27027 tree scope
= CP_DECL_CONTEXT (arg
);
27028 return TYPE_P (scope
) && dependent_type_p (scope
);
27030 else if (ARGUMENT_PACK_P (arg
))
27032 tree args
= ARGUMENT_PACK_ARGS (arg
);
27033 int i
, len
= TREE_VEC_LENGTH (args
);
27034 for (i
= 0; i
< len
; ++i
)
27036 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27042 else if (TYPE_P (arg
))
27043 return dependent_type_p (arg
);
27045 return value_dependent_expression_p (arg
);
27048 /* Returns true if ARGS (a collection of template arguments) contains
27049 any types that require structural equality testing. */
27052 any_template_arguments_need_structural_equality_p (tree args
)
27059 if (args
== error_mark_node
)
27062 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27064 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27065 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27067 tree arg
= TREE_VEC_ELT (level
, j
);
27068 tree packed_args
= NULL_TREE
;
27071 if (ARGUMENT_PACK_P (arg
))
27073 /* Look inside the argument pack. */
27074 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27075 len
= TREE_VEC_LENGTH (packed_args
);
27078 for (k
= 0; k
< len
; ++k
)
27081 arg
= TREE_VEC_ELT (packed_args
, k
);
27083 if (error_operand_p (arg
))
27085 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27087 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27089 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27090 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27099 /* Returns true if ARGS (a collection of template arguments) contains
27100 any dependent arguments. */
27103 any_dependent_template_arguments_p (const_tree args
)
27110 if (args
== error_mark_node
)
27113 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27115 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27116 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27117 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27124 /* Returns true if ARGS contains any errors. */
27127 any_erroneous_template_args_p (const_tree args
)
27132 if (args
== error_mark_node
)
27135 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27137 if (tree ti
= get_template_info (args
))
27138 args
= TI_ARGS (ti
);
27146 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27148 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27149 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27150 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27157 /* Returns TRUE if the template TMPL is type-dependent. */
27160 dependent_template_p (tree tmpl
)
27162 if (TREE_CODE (tmpl
) == OVERLOAD
)
27164 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27165 if (dependent_template_p (*iter
))
27170 /* Template template parameters are dependent. */
27171 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27172 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27174 /* So are names that have not been looked up. */
27175 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27180 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27183 dependent_template_id_p (tree tmpl
, tree args
)
27185 return (dependent_template_p (tmpl
)
27186 || any_dependent_template_arguments_p (args
));
27189 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27193 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27197 if (!processing_template_decl
)
27200 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27202 tree decl
= TREE_VEC_ELT (declv
, i
);
27203 tree init
= TREE_VEC_ELT (initv
, i
);
27204 tree cond
= TREE_VEC_ELT (condv
, i
);
27205 tree incr
= TREE_VEC_ELT (incrv
, i
);
27207 if (type_dependent_expression_p (decl
)
27208 || TREE_CODE (decl
) == SCOPE_REF
)
27211 if (init
&& type_dependent_expression_p (init
))
27214 if (cond
== global_namespace
)
27217 if (type_dependent_expression_p (cond
))
27220 if (COMPARISON_CLASS_P (cond
)
27221 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27222 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27225 if (TREE_CODE (incr
) == MODOP_EXPR
)
27227 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27228 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27231 else if (type_dependent_expression_p (incr
))
27233 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27235 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27237 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27239 tree t
= TREE_OPERAND (incr
, 1);
27240 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27241 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27244 /* If this loop has a class iterator with != comparison
27245 with increment other than i++/++i/i--/--i, make sure the
27246 increment is constant. */
27247 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27248 && TREE_CODE (cond
) == NE_EXPR
)
27250 if (TREE_OPERAND (t
, 0) == decl
)
27251 t
= TREE_OPERAND (t
, 1);
27253 t
= TREE_OPERAND (t
, 0);
27254 if (TREE_CODE (t
) != INTEGER_CST
)
27264 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27265 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27266 no such TYPE can be found. Note that this function peers inside
27267 uninstantiated templates and therefore should be used only in
27268 extremely limited situations. ONLY_CURRENT_P restricts this
27269 peering to the currently open classes hierarchy (which is required
27270 when comparing types). */
27273 resolve_typename_type (tree type
, bool only_current_p
)
27282 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27284 scope
= TYPE_CONTEXT (type
);
27285 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27286 gcc_checking_assert (uses_template_parms (scope
));
27288 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27289 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27290 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27291 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27292 identifier of the TYPENAME_TYPE anymore.
27293 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27294 TYPENAME_TYPE instead, we avoid messing up with a possible
27295 typedef variant case. */
27296 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27298 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27299 it first before we can figure out what NAME refers to. */
27300 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27302 if (TYPENAME_IS_RESOLVING_P (scope
))
27303 /* Given a class template A with a dependent base with nested type C,
27304 typedef typename A::C::C C will land us here, as trying to resolve
27305 the initial A::C leads to the local C typedef, which leads back to
27306 A::C::C. So we break the recursion now. */
27309 scope
= resolve_typename_type (scope
, only_current_p
);
27311 /* If we don't know what SCOPE refers to, then we cannot resolve the
27313 if (!CLASS_TYPE_P (scope
))
27315 /* If this is a typedef, we don't want to look inside (c++/11987). */
27316 if (typedef_variant_p (type
))
27318 /* If SCOPE isn't the template itself, it will not have a valid
27319 TYPE_FIELDS list. */
27320 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27321 /* scope is either the template itself or a compatible instantiation
27322 like X<T>, so look up the name in the original template. */
27323 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27324 /* If scope has no fields, it can't be a current instantiation. Check this
27325 before currently_open_class to avoid infinite recursion (71515). */
27326 if (!TYPE_FIELDS (scope
))
27328 /* If the SCOPE is not the current instantiation, there's no reason
27329 to look inside it. */
27330 if (only_current_p
&& !currently_open_class (scope
))
27332 /* Enter the SCOPE so that name lookup will be resolved as if we
27333 were in the class definition. In particular, SCOPE will no
27334 longer be considered a dependent type. */
27335 pushed_scope
= push_scope (scope
);
27336 /* Look up the declaration. */
27337 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27338 tf_warning_or_error
);
27340 result
= NULL_TREE
;
27342 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27343 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27344 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27347 else if (identifier_p (fullname
)
27348 && TREE_CODE (decl
) == TYPE_DECL
)
27350 result
= TREE_TYPE (decl
);
27351 if (result
== error_mark_node
)
27352 result
= NULL_TREE
;
27354 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27355 && DECL_CLASS_TEMPLATE_P (decl
))
27357 /* Obtain the template and the arguments. */
27358 tree tmpl
= TREE_OPERAND (fullname
, 0);
27359 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27361 /* We get here with a plain identifier because a previous tentative
27362 parse of the nested-name-specifier as part of a ptr-operator saw
27363 ::template X<A>. The use of ::template is necessary in a
27364 ptr-operator, but wrong in a declarator-id.
27366 [temp.names]: In a qualified-id of a declarator-id, the keyword
27367 template shall not appear at the top level. */
27368 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27369 "keyword %<template%> not allowed in declarator-id");
27372 tree args
= TREE_OPERAND (fullname
, 1);
27373 /* Instantiate the template. */
27374 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27375 /*entering_scope=*/true,
27376 tf_error
| tf_user
);
27377 if (result
== error_mark_node
)
27378 result
= NULL_TREE
;
27381 /* Leave the SCOPE. */
27383 pop_scope (pushed_scope
);
27385 /* If we failed to resolve it, return the original typename. */
27389 /* If lookup found a typename type, resolve that too. */
27390 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27392 /* Ill-formed programs can cause infinite recursion here, so we
27393 must catch that. */
27394 TYPENAME_IS_RESOLVING_P (result
) = 1;
27395 result
= resolve_typename_type (result
, only_current_p
);
27396 TYPENAME_IS_RESOLVING_P (result
) = 0;
27399 /* Qualify the resulting type. */
27400 quals
= cp_type_quals (type
);
27402 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27407 /* EXPR is an expression which is not type-dependent. Return a proxy
27408 for EXPR that can be used to compute the types of larger
27409 expressions containing EXPR. */
27412 build_non_dependent_expr (tree expr
)
27414 tree orig_expr
= expr
;
27417 /* When checking, try to get a constant value for all non-dependent
27418 expressions in order to expose bugs in *_dependent_expression_p
27419 and constexpr. This can affect code generation, see PR70704, so
27420 only do this for -fchecking=2. */
27421 if (flag_checking
> 1
27422 && cxx_dialect
>= cxx11
27423 /* Don't do this during nsdmi parsing as it can lead to
27424 unexpected recursive instantiations. */
27425 && !parsing_nsdmi ()
27426 /* Don't do this during concept processing either and for
27427 the same reason. */
27428 && !processing_constraint_expression_p ())
27429 fold_non_dependent_expr (expr
, tf_none
);
27431 STRIP_ANY_LOCATION_WRAPPER (expr
);
27433 /* Preserve OVERLOADs; the functions must be available to resolve
27436 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27437 inner_expr
= stmt_expr_value_expr (inner_expr
);
27438 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27439 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27440 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27441 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27442 if (is_overloaded_fn (inner_expr
)
27443 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27445 /* There is no need to return a proxy for a variable or enumerator. */
27446 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27448 /* Preserve string constants; conversions from string constants to
27449 "char *" are allowed, even though normally a "const char *"
27450 cannot be used to initialize a "char *". */
27451 if (TREE_CODE (expr
) == STRING_CST
)
27453 /* Preserve void and arithmetic constants, as an optimization -- there is no
27454 reason to create a new node. */
27455 if (TREE_CODE (expr
) == VOID_CST
27456 || TREE_CODE (expr
) == INTEGER_CST
27457 || TREE_CODE (expr
) == REAL_CST
)
27459 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27460 There is at least one place where we want to know that a
27461 particular expression is a throw-expression: when checking a ?:
27462 expression, there are special rules if the second or third
27463 argument is a throw-expression. */
27464 if (TREE_CODE (expr
) == THROW_EXPR
)
27467 /* Don't wrap an initializer list, we need to be able to look inside. */
27468 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27471 /* Don't wrap a dummy object, we need to be able to test for it. */
27472 if (is_dummy_object (expr
))
27475 if (TREE_CODE (expr
) == COND_EXPR
)
27476 return build3 (COND_EXPR
,
27478 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27479 (TREE_OPERAND (expr
, 1)
27480 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27481 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27482 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27483 if (TREE_CODE (expr
) == COMPOUND_EXPR
27484 && !COMPOUND_EXPR_OVERLOADED (expr
))
27485 return build2 (COMPOUND_EXPR
,
27487 TREE_OPERAND (expr
, 0),
27488 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27490 /* If the type is unknown, it can't really be non-dependent */
27491 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27493 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27494 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27495 TREE_TYPE (expr
), expr
);
27498 /* ARGS is a vector of expressions as arguments to a function call.
27499 Replace the arguments with equivalent non-dependent expressions.
27500 This modifies ARGS in place. */
27503 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27508 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27510 tree newarg
= build_non_dependent_expr (arg
);
27512 (*args
)[ix
] = newarg
;
27516 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27517 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27518 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27521 make_auto_1 (tree name
, bool set_canonical
)
27523 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27524 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27525 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27526 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27527 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27528 TYPE_NAME (au
), NULL_TREE
);
27530 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27531 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27532 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27533 if (name
== decltype_auto_identifier
)
27534 AUTO_IS_DECLTYPE (au
) = true;
27540 make_decltype_auto (void)
27542 return make_auto_1 (decltype_auto_identifier
, true);
27548 return make_auto_1 (auto_identifier
, true);
27551 /* Return a C++17 deduction placeholder for class template TMPL. */
27554 make_template_placeholder (tree tmpl
)
27556 tree t
= make_auto_1 (auto_identifier
, false);
27557 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27558 /* Our canonical type depends on the placeholder. */
27559 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27563 /* True iff T is a C++17 class template deduction placeholder. */
27566 template_placeholder_p (tree t
)
27568 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27571 /* Make a "constrained auto" type-specifier. This is an auto or
27572 decltype(auto) type with constraints that must be associated after
27573 deduction. The constraint is formed from the given concept CON
27574 and its optional sequence of template arguments ARGS.
27576 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27579 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27581 /* Build the constraint. */
27582 tree tmpl
= DECL_TI_TEMPLATE (con
);
27584 if (TREE_CODE (con
) == FUNCTION_DECL
)
27585 expr
= ovl_make (tmpl
);
27586 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27588 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27590 /* Our canonical type depends on the constraint. */
27591 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27593 /* Attach the constraint to the type declaration. */
27594 return TYPE_NAME (type
);
27597 /* Make a "constrained auto" type-specifier. */
27600 make_constrained_auto (tree con
, tree args
)
27602 tree type
= make_auto_1 (auto_identifier
, false);
27603 return make_constrained_placeholder_type (type
, con
, args
);
27606 /* Make a "constrained decltype(auto)" type-specifier. */
27609 make_constrained_decltype_auto (tree con
, tree args
)
27611 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27612 return make_constrained_placeholder_type (type
, con
, args
);
27615 /* Build and return a concept definition. Like other templates, the
27616 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27617 the TEMPLATE_DECL. */
27620 finish_concept_definition (cp_expr id
, tree init
)
27622 gcc_assert (identifier_p (id
));
27623 gcc_assert (processing_template_decl
);
27625 location_t loc
= id
.get_location();
27627 /* A concept-definition shall not have associated constraints. */
27628 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27630 error_at (loc
, "a concept cannot be constrained");
27631 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27634 /* A concept-definition shall appear in namespace scope. Templates
27635 aren't allowed in block scope, so we only need to check for class
27637 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27639 error_at (loc
, "concept %qE not in namespace scope", *id
);
27640 return error_mark_node
;
27643 /* Initially build the concept declaration; it's type is bool. */
27644 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27645 DECL_CONTEXT (decl
) = current_scope ();
27646 DECL_INITIAL (decl
) = init
;
27648 /* Push the enclosing template. */
27649 return push_template_decl (decl
);
27652 /* Given type ARG, return std::initializer_list<ARG>. */
27657 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27659 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27661 gcc_rich_location
richloc (input_location
);
27662 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27663 error_at (&richloc
,
27664 "deducing from brace-enclosed initializer list"
27665 " requires %<#include <initializer_list>%>");
27667 return error_mark_node
;
27669 tree argvec
= make_tree_vec (1);
27670 TREE_VEC_ELT (argvec
, 0) = arg
;
27672 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27673 NULL_TREE
, 0, tf_warning_or_error
);
27676 /* Replace auto in TYPE with std::initializer_list<auto>. */
27679 listify_autos (tree type
, tree auto_node
)
27681 tree init_auto
= listify (strip_top_quals (auto_node
));
27682 tree argvec
= make_tree_vec (1);
27683 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27684 if (processing_template_decl
)
27685 argvec
= add_to_template_args (current_template_args (), argvec
);
27686 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27689 /* Hash traits for hashing possibly constrained 'auto'
27690 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27692 struct auto_hash
: default_hash_traits
<tree
>
27694 static inline hashval_t
hash (tree
);
27695 static inline bool equal (tree
, tree
);
27698 /* Hash the 'auto' T. */
27701 auto_hash::hash (tree t
)
27703 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27704 /* Matching constrained-type-specifiers denote the same template
27705 parameter, so hash the constraint. */
27706 return hash_placeholder_constraint (c
);
27708 /* But unconstrained autos are all separate, so just hash the pointer. */
27709 return iterative_hash_object (t
, 0);
27712 /* Compare two 'auto's. */
27715 auto_hash::equal (tree t1
, tree t2
)
27720 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27721 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27723 /* Two unconstrained autos are distinct. */
27727 return equivalent_placeholder_constraints (c1
, c2
);
27730 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27731 constrained) auto, add it to the vector. */
27734 extract_autos_r (tree t
, void *data
)
27736 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27739 /* All the autos were built with index 0; fix that up now. */
27740 tree
*p
= hash
.find_slot (t
, INSERT
);
27743 /* If this is a repeated constrained-type-specifier, use the index we
27745 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27748 /* Otherwise this is new, so use the current count. */
27750 idx
= hash
.elements () - 1;
27752 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27755 /* Always keep walking. */
27759 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27760 says they can appear anywhere in the type. */
27763 extract_autos (tree type
)
27765 hash_set
<tree
> visited
;
27766 hash_table
<auto_hash
> hash (2);
27768 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27770 tree tree_vec
= make_tree_vec (hash
.elements());
27771 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27772 iter
!= hash
.end(); ++iter
)
27775 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27776 TREE_VEC_ELT (tree_vec
, i
)
27777 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27783 /* The stem for deduction guide names. */
27784 const char *const dguide_base
= "__dguide_";
27786 /* Return the name for a deduction guide for class template TMPL. */
27789 dguide_name (tree tmpl
)
27791 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27792 tree tname
= TYPE_IDENTIFIER (type
);
27793 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27794 + IDENTIFIER_LENGTH (tname
));
27795 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27796 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27797 IDENTIFIER_LENGTH (tname
) + 1);
27798 tree dname
= get_identifier (buf
);
27799 TREE_TYPE (dname
) = type
;
27803 /* True if NAME is the name of a deduction guide. */
27806 dguide_name_p (tree name
)
27808 return (TREE_CODE (name
) == IDENTIFIER_NODE
27809 && TREE_TYPE (name
)
27810 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27811 strlen (dguide_base
)));
27814 /* True if FN is a deduction guide. */
27817 deduction_guide_p (const_tree fn
)
27820 if (tree name
= DECL_NAME (fn
))
27821 return dguide_name_p (name
);
27825 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27828 copy_guide_p (const_tree fn
)
27830 gcc_assert (deduction_guide_p (fn
));
27831 if (!DECL_ARTIFICIAL (fn
))
27833 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27834 return (TREE_CHAIN (parms
) == void_list_node
27835 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27838 /* True if FN is a guide generated from a constructor template. */
27841 template_guide_p (const_tree fn
)
27843 gcc_assert (deduction_guide_p (fn
));
27844 if (!DECL_ARTIFICIAL (fn
))
27846 tree tmpl
= DECL_TI_TEMPLATE (fn
);
27847 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
27848 return PRIMARY_TEMPLATE_P (org
);
27852 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27853 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27854 template parameter types. Note that the handling of template template
27855 parameters relies on current_template_parms being set appropriately for the
27859 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
27860 tree tsubst_args
, tsubst_flags_t complain
)
27862 if (olddecl
== error_mark_node
)
27863 return error_mark_node
;
27865 tree oldidx
= get_template_parm_index (olddecl
);
27868 if (TREE_CODE (olddecl
) == TYPE_DECL
27869 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27871 tree oldtype
= TREE_TYPE (olddecl
);
27872 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27873 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27874 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27875 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27876 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27880 newtype
= TREE_TYPE (olddecl
);
27881 if (type_uses_auto (newtype
))
27883 // Substitute once to fix references to other template parameters.
27884 newtype
= tsubst (newtype
, tsubst_args
,
27885 complain
|tf_partial
, NULL_TREE
);
27886 // Now substitute again to reduce the level of the auto.
27887 newtype
= tsubst (newtype
, current_template_args (),
27888 complain
, NULL_TREE
);
27891 newtype
= tsubst (newtype
, tsubst_args
,
27892 complain
, NULL_TREE
);
27896 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27897 DECL_NAME (olddecl
), newtype
);
27898 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27901 if (TREE_CODE (olddecl
) == TYPE_DECL
27902 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27904 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27905 = build_template_parm_index (index
, level
, level
,
27907 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27908 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27909 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27910 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
27912 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27914 DECL_TEMPLATE_RESULT (newdecl
)
27915 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
27916 DECL_NAME (olddecl
), newtype
);
27917 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
27918 // First create a copy (ttargs) of tsubst_args with an
27919 // additional level for the template template parameter's own
27920 // template parameters (ttparms).
27921 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
27922 (DECL_TEMPLATE_PARMS (olddecl
)));
27923 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
27924 tree ttargs
= make_tree_vec (depth
+ 1);
27925 for (int i
= 0; i
< depth
; ++i
)
27926 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
27927 TREE_VEC_ELT (ttargs
, depth
)
27928 = template_parms_level_to_args (ttparms
);
27929 // Substitute ttargs into ttparms to fix references to
27930 // other template parameters.
27931 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27932 complain
|tf_partial
);
27933 // Now substitute again with args based on tparms, to reduce
27934 // the level of the ttparms.
27935 ttargs
= current_template_args ();
27936 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27938 // Finally, tack the adjusted parms onto tparms.
27939 ttparms
= tree_cons (size_int (depth
), ttparms
,
27940 current_template_parms
);
27941 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
27946 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
27948 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
27949 TREE_CODE (oldconst
),
27950 DECL_NAME (oldconst
), newtype
);
27951 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
27952 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
27953 SET_DECL_TEMPLATE_PARM_P (newconst
);
27954 newidx
= build_template_parm_index (index
, level
, level
,
27955 newconst
, newtype
);
27956 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27957 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27958 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
27964 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
27965 template parameter. */
27968 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
27969 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
27971 tree olddecl
= TREE_VALUE (oldelt
);
27972 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27974 if (newdecl
== error_mark_node
)
27975 return error_mark_node
;
27976 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27977 targs
, complain
, NULL_TREE
);
27978 tree list
= build_tree_list (newdef
, newdecl
);
27979 TEMPLATE_PARM_CONSTRAINTS (list
)
27980 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27981 targs
, complain
, NULL_TREE
);
27982 int depth
= TMPL_ARGS_DEPTH (targs
);
27983 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
27987 /* Returns a C++17 class deduction guide template based on the constructor
27988 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27989 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27990 aggregate initialization guide. */
27993 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
27995 tree tparms
, targs
, fparms
, fargs
, ci
;
27996 bool memtmpl
= false;
27999 tree fn_tmpl
= NULL_TREE
;
28003 ++processing_template_decl
;
28004 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28005 --processing_template_decl
;
28008 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28012 bool copy_p
= TYPE_REF_P (ctor
);
28014 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28016 fparms
= void_list_node
;
28018 else if (TREE_CODE (ctor
) == TREE_LIST
)
28021 gcc_unreachable ();
28023 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28024 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28025 targs
= CLASSTYPE_TI_ARGS (type
);
28028 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28029 explicit_p
= false;
28033 ++processing_template_decl
;
28037 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28038 : DECL_TI_TEMPLATE (ctor
));
28040 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28041 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28043 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28044 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28045 fully specialized args for the enclosing class. Strip those off, as
28046 the deduction guide won't have those template parameters. */
28047 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28048 TMPL_PARMS_DEPTH (tparms
));
28049 /* Discard the 'this' parameter. */
28050 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28051 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28052 ci
= get_constraints (ctor
);
28053 loc
= DECL_SOURCE_LOCATION (ctor
);
28054 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28056 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28060 /* For a member template constructor, we need to flatten the two
28061 template parameter lists into one, and then adjust the function
28062 signature accordingly. This gets...complicated. */
28063 tree save_parms
= current_template_parms
;
28065 /* For a member template we should have two levels of parms/args, one
28066 for the class and one for the constructor. We stripped
28067 specialized args for further enclosing classes above. */
28068 const int depth
= 2;
28069 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28071 /* Template args for translating references to the two-level template
28072 parameters into references to the one-level template parameters we
28074 tree tsubst_args
= copy_node (targs
);
28075 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28076 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28078 /* Template parms for the constructor template. */
28079 tree ftparms
= TREE_VALUE (tparms
);
28080 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28081 /* Template parms for the class template. */
28082 tparms
= TREE_CHAIN (tparms
);
28083 tree ctparms
= TREE_VALUE (tparms
);
28084 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28085 /* Template parms for the deduction guide start as a copy of the
28086 template parms for the class. We set current_template_parms for
28087 lookup_template_class_1. */
28088 current_template_parms
= tparms
= copy_node (tparms
);
28089 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28090 for (unsigned i
= 0; i
< clen
; ++i
)
28091 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28093 /* Now we need to rewrite the constructor parms to append them to the
28095 for (unsigned i
= 0; i
< flen
; ++i
)
28097 unsigned index
= i
+ clen
;
28098 unsigned level
= 1;
28099 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28101 = rewrite_tparm_list (oldelt
, index
, level
,
28102 tsubst_args
, i
, complain
);
28103 if (newelt
== error_mark_node
)
28105 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28108 /* Now we have a final set of template parms to substitute into the
28109 function signature. */
28110 targs
= template_parms_to_args (tparms
);
28111 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28113 if (fparms
== error_mark_node
)
28116 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28118 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28119 cp_unevaluated_operand. */
28121 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28122 current_template_parms
= save_parms
;
28125 --processing_template_decl
;
28127 return error_mark_node
;
28132 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28133 tparms
= copy_node (tparms
);
28134 INNERMOST_TEMPLATE_PARMS (tparms
)
28135 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28138 tree fntype
= build_function_type (type
, fparms
);
28139 tree ded_fn
= build_lang_decl_loc (loc
,
28141 dguide_name (type
), fntype
);
28142 DECL_ARGUMENTS (ded_fn
) = fargs
;
28143 DECL_ARTIFICIAL (ded_fn
) = true;
28144 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28145 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28146 DECL_ARTIFICIAL (ded_tmpl
) = true;
28147 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
28148 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
28149 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28150 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28152 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28154 set_constraints (ded_tmpl
, ci
);
28159 /* Add to LIST the member types for the reshaped initializer CTOR. */
28162 collect_ctor_idx_types (tree ctor
, tree list
)
28164 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28165 tree idx
, val
; unsigned i
;
28166 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28168 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28169 && CONSTRUCTOR_NELTS (val
))
28170 if (tree subidx
= CONSTRUCTOR_ELT (val
, 0)->index
)
28171 if (TREE_CODE (subidx
) == FIELD_DECL
)
28173 list
= collect_ctor_idx_types (val
, list
);
28176 tree ftype
= finish_decltype_type (idx
, true, tf_none
);
28177 list
= tree_cons (NULL_TREE
, ftype
, list
);
28183 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28186 is_spec_or_derived (tree etype
, tree tmpl
)
28188 if (!etype
|| !CLASS_TYPE_P (etype
))
28191 tree type
= TREE_TYPE (tmpl
);
28192 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28193 (DECL_TEMPLATE_PARMS (tmpl
)));
28194 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28195 int err
= unify (tparms
, targs
, type
, etype
,
28196 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28201 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28205 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28207 if (cxx_dialect
< cxx2a
)
28210 if (init
== NULL_TREE
)
28213 tree type
= TREE_TYPE (tmpl
);
28214 if (!CP_AGGREGATE_TYPE_P (type
))
28217 /* No aggregate candidate for copy-initialization. */
28218 if (args
->length() == 1)
28220 tree val
= (*args
)[0];
28221 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28225 /* If we encounter a problem, we just won't add the candidate. */
28226 tsubst_flags_t complain
= tf_none
;
28228 tree parms
= NULL_TREE
;
28229 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28231 init
= reshape_init (type
, init
, complain
);
28232 if (init
== error_mark_node
)
28234 parms
= collect_ctor_idx_types (init
, parms
);
28236 else if (TREE_CODE (init
) == TREE_LIST
)
28238 int len
= list_length (init
);
28239 for (tree field
= TYPE_FIELDS (type
);
28241 --len
, field
= DECL_CHAIN (field
))
28243 field
= next_initializable_field (field
);
28246 tree ftype
= finish_decltype_type (field
, true, complain
);
28247 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28251 /* Aggregate initialization doesn't apply to an initializer expression. */
28257 parms
= nreverse (parms
);
28258 TREE_CHAIN (last
) = void_list_node
;
28259 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28266 /* UGUIDES are the deduction guides for the underlying template of alias
28267 template TMPL; adjust them to be deduction guides for TMPL. */
28270 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28272 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28273 class type (9.2.8.2) where the template-name names an alias template A,
28274 the defining-type-id of A must be of the form
28276 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28278 as specified in 9.2.8.2. The guides of A are the set of functions or
28279 function templates formed as follows. For each function or function
28280 template f in the guides of the template named by the simple-template-id
28281 of the defining-type-id, the template arguments of the return type of f
28282 are deduced from the defining-type-id of A according to the process in
28283 13.10.2.5 with the exception that deduction does not fail if not all
28284 template arguments are deduced. Let g denote the result of substituting
28285 these deductions into f. If substitution succeeds, form a function or
28286 function template f' with the following properties and add it to the set
28289 * The function type of f' is the function type of g.
28291 * If f is a function template, f' is a function template whose template
28292 parameter list consists of all the template parameters of A (including
28293 their default template arguments) that appear in the above deductions or
28294 (recursively) in their default template arguments, followed by the
28295 template parameters of f that were not deduced (including their default
28296 template arguments), otherwise f' is not a function template.
28298 * The associated constraints (13.5.2) are the conjunction of the
28299 associated constraints of g and a constraint that is satisfied if and only
28300 if the arguments of A are deducible (see below) from the return type.
28302 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28305 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28306 considered to be so as well.
28308 * The explicit-specifier of f' is the explicit-specifier of g (if
28311 /* This implementation differs from the above in two significant ways:
28313 1) We include all template parameters of A, not just some.
28314 2) The added constraint is same_type instead of deducible.
28316 I believe that while it's probably possible to construct a testcase that
28317 behaves differently with this simplification, it should have the same
28318 effect for real uses. Including all template parameters means that we
28319 deduce all parameters of A when resolving the call, so when we're in the
28320 constraint we don't need to deduce them again, we can just check whether
28321 the deduction produced the desired result. */
28323 tsubst_flags_t complain
= tf_warning_or_error
;
28324 tree atype
= TREE_TYPE (tmpl
);
28325 tree aguides
= NULL_TREE
;
28326 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28327 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28328 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28329 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28333 location_t loc
= DECL_SOURCE_LOCATION (f
);
28334 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28336 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28338 processing_template_decl_sentinel
ptds (/*reset*/false);
28339 ++processing_template_decl
;
28341 /* Deduce template arguments for f from the type-id of A. */
28342 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28343 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28344 tree targs
= make_tree_vec (len
);
28345 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28348 /* The number of parms for f' is the number of parms for A plus
28349 non-deduced parms of f. */
28350 unsigned ndlen
= 0;
28352 for (unsigned i
= 0; i
< len
; ++i
)
28353 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28355 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28357 /* First copy over the parms of A. */
28358 for (j
= 0; j
< natparms
; ++j
)
28359 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28360 /* Now rewrite the non-deduced parms of f. */
28361 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28362 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28365 unsigned index
= j
++;
28366 unsigned level
= 1;
28367 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28368 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28369 targs
, i
, complain
);
28370 TREE_VEC_ELT (gtparms
, index
) = list
;
28372 gtparms
= build_tree_list (size_one_node
, gtparms
);
28374 /* Substitute the deduced arguments plus the rewritten template
28375 parameters into f to get g. This covers the type, copyness,
28376 guideness, and explicit-specifier. */
28377 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28378 if (g
== error_mark_node
)
28379 return error_mark_node
;
28380 DECL_USE_TEMPLATE (g
) = 0;
28381 fprime
= build_template_decl (g
, gtparms
, false);
28382 DECL_TEMPLATE_RESULT (fprime
) = g
;
28383 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28384 tree gtargs
= template_parms_to_args (gtparms
);
28385 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28386 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28388 /* Substitute the associated constraints. */
28389 tree ci
= get_constraints (f
);
28391 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28392 if (ci
== error_mark_node
)
28393 return error_mark_node
;
28395 /* Add a constraint that the return type matches the instantiation of
28396 A with the same template arguments. */
28397 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28398 if (!same_type_p (atype
, ret
)
28399 /* FIXME this should mean they don't compare as equivalent. */
28400 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28402 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28403 ci
= append_constraint (ci
, same
);
28407 set_constraints (fprime
, ci
);
28411 /* For a non-template deduction guide, if the arguments of A aren't
28412 deducible from the return type, don't add the candidate. */
28413 tree targs
= make_tree_vec (natparms
);
28414 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28415 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28416 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28422 aguides
= lookup_add (fprime
, aguides
);
28428 /* Return artificial deduction guides built from the constructors of class
28432 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28434 tree type
= TREE_TYPE (tmpl
);
28435 tree outer_args
= NULL_TREE
;
28436 if (DECL_CLASS_SCOPE_P (tmpl
)
28437 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28439 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28440 type
= TREE_TYPE (most_general_template (tmpl
));
28443 tree cands
= NULL_TREE
;
28445 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28447 /* Skip inherited constructors. */
28448 if (iter
.using_p ())
28451 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28452 cands
= lookup_add (guide
, cands
);
28455 /* Add implicit default constructor deduction guide. */
28456 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28458 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28460 cands
= lookup_add (guide
, cands
);
28463 /* Add copy guide. */
28465 tree gtype
= build_reference_type (type
);
28466 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28468 cands
= lookup_add (guide
, cands
);
28474 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28476 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28477 aggregate candidate is added separately because it depends on the
28481 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28483 tree guides
= NULL_TREE
;
28484 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28486 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28487 tree tinfo
= get_template_info (under
);
28488 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28492 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28493 dguide_name (tmpl
),
28494 /*type*/false, /*complain*/false,
28496 if (guides
== error_mark_node
)
28497 guides
= NULL_TREE
;
28500 /* Cache the deduction guides for a template. We also remember the result of
28501 lookup, and rebuild everything if it changes; should be very rare. */
28502 tree_pair_p cache
= NULL
;
28504 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28507 if (cache
->purpose
== guides
)
28508 return cache
->value
;
28512 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28513 cache
->purpose
= guides
;
28516 tree cands
= NULL_TREE
;
28517 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28518 cands
= alias_ctad_tweaks (tmpl
, guides
);
28521 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28522 for (ovl_iterator
it (guides
); it
; ++it
)
28523 cands
= lookup_add (*it
, cands
);
28526 cache
->value
= cands
;
28530 /* Return whether TMPL is a (class template argument-) deducible template. */
28533 ctad_template_p (tree tmpl
)
28535 /* A deducible template is either a class template or is an alias template
28536 whose defining-type-id is of the form
28538 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28540 where the nested-name-specifier (if any) is non-dependent and the
28541 template-name of the simple-template-id names a deducible template. */
28543 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28544 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28546 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28548 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28549 if (tree tinfo
= get_template_info (orig
))
28550 return ctad_template_p (TI_TEMPLATE (tinfo
));
28554 /* Deduce template arguments for the class template placeholder PTYPE for
28555 template TMPL based on the initializer INIT, and return the resulting
28559 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28560 int flags
, tsubst_flags_t complain
)
28562 /* We should have handled this in the caller. */
28563 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28566 /* Look through alias templates that just rename another template. */
28567 tmpl
= get_underlying_template (tmpl
);
28568 if (!ctad_template_p (tmpl
))
28570 if (complain
& tf_error
)
28571 error ("non-deducible template %qT used without template arguments", tmpl
);
28572 return error_mark_node
;
28574 else if (cxx_dialect
< cxx2a
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28576 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28577 if (complain
& tf_warning_or_error
)
28578 pedwarn (input_location
, 0, "alias template deduction only available "
28579 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28582 if (init
&& TREE_TYPE (init
) == ptype
)
28583 /* Using the template parm as its own argument. */
28586 tree type
= TREE_TYPE (tmpl
);
28588 bool try_list_ctor
= false;
28590 releasing_vec rv_args
= NULL
;
28591 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28592 if (init
== NULL_TREE
)
28593 args
= make_tree_vector ();
28594 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28596 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28597 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28599 /* As an exception, the first phase in 16.3.1.7 (considering the
28600 initializer list as a single argument) is omitted if the
28601 initializer list consists of a single expression of type cv U,
28602 where U is a specialization of C or a class derived from a
28603 specialization of C. */
28604 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28605 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28606 try_list_ctor
= false;
28608 if (try_list_ctor
|| is_std_init_list (type
))
28609 args
= make_tree_vector_single (init
);
28611 args
= make_tree_vector_from_ctor (init
);
28613 else if (TREE_CODE (init
) == TREE_LIST
)
28614 args
= make_tree_vector_from_list (init
);
28616 args
= make_tree_vector_single (init
);
28618 /* Do this now to avoid problems with erroneous args later on. */
28619 args
= resolve_args (args
, complain
);
28621 return error_mark_node
;
28623 tree cands
= deduction_guides_for (tmpl
, complain
);
28624 if (cands
== error_mark_node
)
28625 return error_mark_node
;
28627 /* Prune explicit deduction guides in copy-initialization context. */
28628 bool elided
= false;
28629 if (flags
& LOOKUP_ONLYCONVERTING
)
28631 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28632 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28637 /* Found a nonconverting guide, prune the candidates. */
28638 tree pruned
= NULL_TREE
;
28639 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28640 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28641 pruned
= lookup_add (*iter
, pruned
);
28647 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
28648 cands
= lookup_add (guide
, cands
);
28650 tree call
= error_mark_node
;
28652 /* If this is list-initialization and the class has a list constructor, first
28653 try deducing from the list as a single argument, as [over.match.list]. */
28654 tree list_cands
= NULL_TREE
;
28655 if (try_list_ctor
&& cands
)
28656 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28659 if (is_list_ctor (dg
))
28660 list_cands
= lookup_add (dg
, list_cands
);
28664 ++cp_unevaluated_operand
;
28665 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28666 --cp_unevaluated_operand
;
28668 if (call
== error_mark_node
)
28670 /* That didn't work, now try treating the list as a sequence of
28672 release_tree_vector (args
);
28673 args
= make_tree_vector_from_ctor (init
);
28677 if (elided
&& !cands
)
28679 error ("cannot deduce template arguments for copy-initialization"
28680 " of %qT, as it has no non-explicit deduction guides or "
28681 "user-declared constructors", type
);
28682 return error_mark_node
;
28684 else if (!cands
&& call
== error_mark_node
)
28686 error ("cannot deduce template arguments of %qT, as it has no viable "
28687 "deduction guides", type
);
28688 return error_mark_node
;
28691 if (call
== error_mark_node
)
28693 ++cp_unevaluated_operand
;
28694 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28695 --cp_unevaluated_operand
;
28698 if (call
== error_mark_node
28699 && (complain
& tf_warning_or_error
))
28701 error ("class template argument deduction failed:");
28703 ++cp_unevaluated_operand
;
28704 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28705 --cp_unevaluated_operand
;
28708 inform (input_location
, "explicit deduction guides not considered "
28709 "for copy-initialization");
28712 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28715 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28716 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28717 The CONTEXT determines the context in which auto deduction is performed
28718 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28719 OUTER_TARGS are used during template argument deduction
28720 (context == adc_unify) to properly substitute the result, and is ignored
28723 For partial-concept-ids, extra args may be appended to the list of deduced
28724 template arguments prior to determining constraint satisfaction. */
28727 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28728 tsubst_flags_t complain
, auto_deduction_context context
,
28729 tree outer_targs
, int flags
)
28733 if (init
== error_mark_node
)
28734 return error_mark_node
;
28736 if (init
&& type_dependent_expression_p (init
)
28737 && context
!= adc_unify
)
28738 /* Defining a subset of type-dependent expressions that we can deduce
28739 from ahead of time isn't worth the trouble. */
28742 /* Similarly, we can't deduce from another undeduced decl. */
28743 if (init
&& undeduced_auto_decl (init
))
28746 /* We may be doing a partial substitution, but we still want to replace
28748 complain
&= ~tf_partial
;
28750 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28751 /* C++17 class template argument deduction. */
28752 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28754 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28755 /* Nothing we can do with this, even in deduction context. */
28758 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28759 with either a new invented type template parameter U or, if the
28760 initializer is a braced-init-list (8.5.4), with
28761 std::initializer_list<U>. */
28762 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28764 if (!DIRECT_LIST_INIT_P (init
))
28765 type
= listify_autos (type
, auto_node
);
28766 else if (CONSTRUCTOR_NELTS (init
) == 1)
28767 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28770 if (complain
& tf_warning_or_error
)
28772 if (permerror (input_location
, "direct-list-initialization of "
28773 "%<auto%> requires exactly one element"))
28774 inform (input_location
,
28775 "for deduction to %<std::initializer_list%>, use copy-"
28776 "list-initialization (i.e. add %<=%> before the %<{%>)");
28778 type
= listify_autos (type
, auto_node
);
28782 if (type
== error_mark_node
)
28783 return error_mark_node
;
28785 init
= resolve_nondeduced_context (init
, complain
);
28787 if (context
== adc_decomp_type
28788 && auto_node
== type
28789 && init
!= error_mark_node
28790 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
28791 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28792 and initializer has array type, deduce cv-qualified array type. */
28793 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
28795 else if (AUTO_IS_DECLTYPE (auto_node
))
28797 tree stripped_init
= tree_strip_any_location_wrapper (init
);
28798 bool id
= (DECL_P (stripped_init
)
28799 || ((TREE_CODE (init
) == COMPONENT_REF
28800 || TREE_CODE (init
) == SCOPE_REF
)
28801 && !REF_PARENTHESIZED_P (init
)));
28802 targs
= make_tree_vec (1);
28803 TREE_VEC_ELT (targs
, 0)
28804 = finish_decltype_type (init
, id
, tf_warning_or_error
);
28805 if (type
!= auto_node
)
28807 if (complain
& tf_error
)
28808 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
28809 return error_mark_node
;
28814 if (error_operand_p (init
))
28815 return error_mark_node
;
28817 tree parms
= build_tree_list (NULL_TREE
, type
);
28821 tparms
= extract_autos (type
);
28824 tparms
= make_tree_vec (1);
28825 TREE_VEC_ELT (tparms
, 0)
28826 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
28829 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28830 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28832 NULL
, /*explain_p=*/false);
28835 if (processing_template_decl
)
28836 /* Try again at instantiation time. */
28838 if (type
&& type
!= error_mark_node
28839 && (complain
& tf_error
))
28840 /* If type is error_mark_node a diagnostic must have been
28841 emitted by now. Also, having a mention to '<type error>'
28842 in the diagnostic is not really useful to the user. */
28845 && FNDECL_USED_AUTO (current_function_decl
)
28847 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
28848 && LAMBDA_FUNCTION_P (current_function_decl
))
28849 error ("unable to deduce lambda return type from %qE", init
);
28851 error ("unable to deduce %qT from %qE", type
, init
);
28852 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28854 NULL
, /*explain_p=*/true);
28856 return error_mark_node
;
28860 /* Check any placeholder constraints against the deduced type. */
28861 if (flag_concepts
&& !processing_template_decl
)
28862 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
28864 /* Use the deduced type to check the associated constraints. If we
28865 have a partial-concept-id, rebuild the argument list so that
28866 we check using the extra arguments. */
28867 check
= unpack_concept_check (check
);
28868 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
28869 tree
cdecl = TREE_OPERAND (check
, 0);
28871 cdecl = OVL_FIRST (cdecl);
28872 tree cargs
= TREE_OPERAND (check
, 1);
28873 if (TREE_VEC_LENGTH (cargs
) > 1)
28875 cargs
= copy_node (cargs
);
28876 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
28881 /* Rebuild the check using the deduced arguments. */
28882 check
= build_concept_check (cdecl, cargs
, tf_none
);
28884 if (!constraints_satisfied_p (check
))
28886 if (complain
& tf_warning_or_error
)
28888 auto_diagnostic_group d
;
28891 case adc_unspecified
:
28893 error("placeholder constraints not satisfied");
28895 case adc_variable_type
:
28896 case adc_decomp_type
:
28897 error ("deduced initializer does not satisfy "
28898 "placeholder constraints");
28900 case adc_return_type
:
28901 error ("deduced return type does not satisfy "
28902 "placeholder constraints");
28904 case adc_requirement
:
28905 error ("deduced expression type does not satisfy "
28906 "placeholder constraints");
28909 diagnose_constraints (input_location
, check
, targs
);
28911 return error_mark_node
;
28915 if (processing_template_decl
&& context
!= adc_unify
)
28916 outer_targs
= current_template_args ();
28917 targs
= add_to_template_args (outer_targs
, targs
);
28918 return tsubst (type
, targs
, complain
, NULL_TREE
);
28921 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28925 splice_late_return_type (tree type
, tree late_return_type
)
28927 if (late_return_type
)
28929 gcc_assert (is_auto (type
) || seen_error ());
28930 return late_return_type
;
28933 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
28935 tree idx
= get_template_parm_index (*auto_node
);
28936 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
28937 /* In an abbreviated function template we didn't know we were dealing
28938 with a function template when we saw the auto return type, so update
28939 it to have the correct level. */
28940 *auto_node
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), true);
28945 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28946 'decltype(auto)' or a deduced class template. */
28949 is_auto (const_tree type
)
28951 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
28952 && (TYPE_IDENTIFIER (type
) == auto_identifier
28953 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
28959 /* for_each_template_parm callback for type_uses_auto. */
28962 is_auto_r (tree tp
, void */
*data*/
)
28964 return is_auto (tp
);
28967 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28968 a use of `auto'. Returns NULL_TREE otherwise. */
28971 type_uses_auto (tree type
)
28973 if (type
== NULL_TREE
)
28975 else if (flag_concepts
)
28977 /* The Concepts TS allows multiple autos in one type-specifier; just
28978 return the first one we find, do_auto_deduction will collect all of
28980 if (uses_template_parms (type
))
28981 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
28982 /*visited*/NULL
, /*nondeduced*/false);
28986 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
28992 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28993 concepts are enabled, auto is acceptable in template arguments, but
28994 only when TEMPL identifies a template class. Return TRUE if any
28995 such errors were reported. */
28998 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29000 /* If there were previous errors, nevermind. */
29001 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29004 /* If TMPL is an identifier, we're parsing and we can't tell yet
29005 whether TMPL is supposed to be a type, a function or a variable.
29006 We'll only be able to tell during template substitution, so we
29007 expect to be called again then. If concepts are enabled and we
29008 know we have a type, we're ok. */
29010 && (identifier_p (tmpl
)
29012 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29013 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29016 /* Quickly search for any occurrences of auto; usually there won't
29017 be any, and then we'll avoid allocating the vector. */
29018 if (!type_uses_auto (args
))
29021 bool errors
= false;
29023 tree vec
= extract_autos (args
);
29024 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29026 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29027 error_at (DECL_SOURCE_LOCATION (xauto
),
29028 "invalid use of %qT in template argument", xauto
);
29035 /* For a given template T, return the vector of typedefs referenced
29036 in T for which access check is needed at T instantiation time.
29037 T is either a FUNCTION_DECL or a RECORD_TYPE.
29038 Those typedefs were added to T by the function
29039 append_type_to_template_for_access_check. */
29041 vec
<qualified_typedef_usage_t
, va_gc
> *
29042 get_types_needing_access_check (tree t
)
29045 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
29047 if (!t
|| t
== error_mark_node
)
29050 if (!(ti
= get_template_info (t
)))
29053 if (CLASS_TYPE_P (t
)
29054 || TREE_CODE (t
) == FUNCTION_DECL
)
29056 if (!TI_TEMPLATE (ti
))
29059 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
29065 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29066 tied to T. That list of typedefs will be access checked at
29067 T instantiation time.
29068 T is either a FUNCTION_DECL or a RECORD_TYPE.
29069 TYPE_DECL is a TYPE_DECL node representing a typedef.
29070 SCOPE is the scope through which TYPE_DECL is accessed.
29071 LOCATION is the location of the usage point of TYPE_DECL.
29073 This function is a subroutine of
29074 append_type_to_template_for_access_check. */
29077 append_type_to_template_for_access_check_1 (tree t
,
29080 location_t location
)
29082 qualified_typedef_usage_t typedef_usage
;
29085 if (!t
|| t
== error_mark_node
)
29088 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
29089 || CLASS_TYPE_P (t
))
29091 && TREE_CODE (type_decl
) == TYPE_DECL
29094 if (!(ti
= get_template_info (t
)))
29097 gcc_assert (TI_TEMPLATE (ti
));
29099 typedef_usage
.typedef_decl
= type_decl
;
29100 typedef_usage
.context
= scope
;
29101 typedef_usage
.locus
= location
;
29103 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
29106 /* Append TYPE_DECL to the template TEMPL.
29107 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29108 At TEMPL instanciation time, TYPE_DECL will be checked to see
29109 if it can be accessed through SCOPE.
29110 LOCATION is the location of the usage point of TYPE_DECL.
29112 e.g. consider the following code snippet:
29119 template<class U> struct S
29121 C::myint mi; // <-- usage point of the typedef C::myint
29126 At S<char> instantiation time, we need to check the access of C::myint
29127 In other words, we need to check the access of the myint typedef through
29128 the C scope. For that purpose, this function will add the myint typedef
29129 and the scope C through which its being accessed to a list of typedefs
29130 tied to the template S. That list will be walked at template instantiation
29131 time and access check performed on each typedefs it contains.
29132 Note that this particular code snippet should yield an error because
29133 myint is private to C. */
29136 append_type_to_template_for_access_check (tree templ
,
29139 location_t location
)
29141 qualified_typedef_usage_t
*iter
;
29144 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
29146 /* Make sure we don't append the type to the template twice. */
29147 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
29148 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
29151 append_type_to_template_for_access_check_1 (templ
, type_decl
,
29155 /* Recursively walk over && expressions searching for EXPR. Return a reference
29156 to that expression. */
29158 static tree
*find_template_requirement (tree
*t
, tree key
)
29162 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29164 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29166 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29172 /* Convert the generic type parameters in PARM that match the types given in the
29173 range [START_IDX, END_IDX) from the current_template_parms into generic type
29177 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29179 tree current
= current_template_parms
;
29180 int depth
= TMPL_PARMS_DEPTH (current
);
29181 current
= INNERMOST_TEMPLATE_PARMS (current
);
29182 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29184 for (int i
= 0; i
< start_idx
; ++i
)
29185 TREE_VEC_ELT (replacement
, i
)
29186 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29188 for (int i
= start_idx
; i
< end_idx
; ++i
)
29190 /* Create a distinct parameter pack type from the current parm and add it
29191 to the replacement args to tsubst below into the generic function
29193 tree node
= TREE_VEC_ELT (current
, i
);
29194 tree o
= TREE_TYPE (TREE_VALUE (node
));
29195 tree t
= copy_type (o
);
29196 TEMPLATE_TYPE_PARM_INDEX (t
)
29197 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29199 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29200 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29201 TYPE_MAIN_VARIANT (t
) = t
;
29202 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29203 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29204 TREE_VEC_ELT (replacement
, i
) = t
;
29206 /* Replace the current template parameter with new pack. */
29207 TREE_VALUE (node
) = TREE_CHAIN (t
);
29209 /* Surgically adjust the associated constraint of adjusted parameter
29210 and it's corresponding contribution to the current template
29212 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29214 tree id
= unpack_concept_check (constr
);
29215 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
29216 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29217 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29219 /* If there was a constraint, we also need to replace that in
29220 the template requirements, which we've already built. */
29221 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29222 reqs
= find_template_requirement (reqs
, constr
);
29227 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29228 TREE_VEC_ELT (replacement
, i
)
29229 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29231 /* If there are more levels then build up the replacement with the outer
29234 replacement
= add_to_template_args (template_parms_to_args
29235 (TREE_CHAIN (current_template_parms
)),
29238 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29241 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29245 declare_integer_pack (void)
29247 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29248 build_function_type_list (integer_type_node
,
29251 NULL_TREE
, ECF_CONST
);
29252 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29253 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29254 CP_BUILT_IN_INTEGER_PACK
);
29257 /* Set up the hash tables for template instantiations. */
29260 init_template_processing (void)
29262 /* FIXME: enable sanitization (PR87847) */
29263 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
29264 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
29266 if (cxx_dialect
>= cxx11
)
29267 declare_integer_pack ();
29270 /* Print stats about the template hash tables for -fstats. */
29273 print_template_statistics (void)
29275 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29276 "%f collisions\n", (long) decl_specializations
->size (),
29277 (long) decl_specializations
->elements (),
29278 decl_specializations
->collisions ());
29279 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29280 "%f collisions\n", (long) type_specializations
->size (),
29281 (long) type_specializations
->elements (),
29282 type_specializations
->collisions ());
29287 namespace selftest
{
29289 /* Verify that build_non_dependent_expr () works, for various expressions,
29290 and that location wrappers don't affect the results. */
29293 test_build_non_dependent_expr ()
29295 location_t loc
= BUILTINS_LOCATION
;
29297 /* Verify constants, without and with location wrappers. */
29298 tree int_cst
= build_int_cst (integer_type_node
, 42);
29299 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29301 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29302 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29303 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29305 tree string_lit
= build_string (4, "foo");
29306 TREE_TYPE (string_lit
) = char_array_type_node
;
29307 string_lit
= fix_string_type (string_lit
);
29308 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29310 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29311 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29312 ASSERT_EQ (wrapped_string_lit
,
29313 build_non_dependent_expr (wrapped_string_lit
));
29316 /* Verify that type_dependent_expression_p () works correctly, even
29317 in the presence of location wrapper nodes. */
29320 test_type_dependent_expression_p ()
29322 location_t loc
= BUILTINS_LOCATION
;
29324 tree name
= get_identifier ("foo");
29326 /* If no templates are involved, nothing is type-dependent. */
29327 gcc_assert (!processing_template_decl
);
29328 ASSERT_FALSE (type_dependent_expression_p (name
));
29330 ++processing_template_decl
;
29332 /* Within a template, an unresolved name is always type-dependent. */
29333 ASSERT_TRUE (type_dependent_expression_p (name
));
29335 /* Ensure it copes with NULL_TREE and errors. */
29336 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29337 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29339 /* A USING_DECL in a template should be type-dependent, even if wrapped
29340 with a location wrapper (PR c++/83799). */
29341 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29342 TREE_TYPE (using_decl
) = integer_type_node
;
29343 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29344 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29345 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29346 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29348 --processing_template_decl
;
29351 /* Run all of the selftests within this file. */
29356 test_build_non_dependent_expr ();
29357 test_type_dependent_expression_p ();
29360 } // namespace selftest
29362 #endif /* #if CHECKING_P */
29364 #include "gt-cp-pt.h"